1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Client connection-specific management code.
4 * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * Client connections need to be cached for a little while after they've made a
8 * call so as to handle retransmitted DATA packets in case the server didn't
9 * receive the final ACK or terminating ABORT we sent it.
11 * There are flags of relevance to the cache:
13 * (2) DONT_REUSE - The connection should be discarded as soon as possible and
14 * should not be reused. This is set when an exclusive connection is used
15 * or a call ID counter overflows.
17 * The caching state may only be changed if the cache lock is held.
19 * There are two idle client connection expiry durations. If the total number
20 * of connections is below the reap threshold, we use the normal duration; if
21 * it's above, we use the fast duration.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/timer.h>
29 #include <linux/sched/signal.h>
31 #include "ar-internal.h"
33 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
34 __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
35 __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
38 * We use machine-unique IDs for our client connections.
40 DEFINE_IDR(rxrpc_client_conn_ids);
41 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
43 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle);
46 * Get a connection ID and epoch for a client connection from the global pool.
47 * The connection struct pointer is then recorded in the idr radix tree. The
48 * epoch doesn't change until the client is rebooted (or, at least, unless the
49 * module is unloaded).
51 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
54 struct rxrpc_net *rxnet = conn->params.local->rxnet;
60 spin_lock(&rxrpc_conn_id_lock);
62 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
63 1, 0x40000000, GFP_NOWAIT);
67 spin_unlock(&rxrpc_conn_id_lock);
70 conn->proto.epoch = rxnet->epoch;
71 conn->proto.cid = id << RXRPC_CIDSHIFT;
72 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
73 _leave(" [CID %x]", conn->proto.cid);
77 spin_unlock(&rxrpc_conn_id_lock);
84 * Release a connection ID for a client connection from the global pool.
86 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
88 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
89 spin_lock(&rxrpc_conn_id_lock);
90 idr_remove(&rxrpc_client_conn_ids,
91 conn->proto.cid >> RXRPC_CIDSHIFT);
92 spin_unlock(&rxrpc_conn_id_lock);
97 * Destroy the client connection ID tree.
99 void rxrpc_destroy_client_conn_ids(void)
101 struct rxrpc_connection *conn;
104 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
105 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
106 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
107 conn, refcount_read(&conn->ref));
112 idr_destroy(&rxrpc_client_conn_ids);
116 * Allocate a connection bundle.
118 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp,
121 struct rxrpc_bundle *bundle;
123 bundle = kzalloc(sizeof(*bundle), gfp);
125 bundle->params = *cp;
126 rxrpc_get_peer(bundle->params.peer);
127 refcount_set(&bundle->ref, 1);
128 atomic_set(&bundle->active, 1);
129 spin_lock_init(&bundle->channel_lock);
130 INIT_LIST_HEAD(&bundle->waiting_calls);
135 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle)
137 refcount_inc(&bundle->ref);
141 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
143 rxrpc_put_peer(bundle->params.peer);
147 void rxrpc_put_bundle(struct rxrpc_bundle *bundle)
149 unsigned int d = bundle->debug_id;
153 dead = __refcount_dec_and_test(&bundle->ref, &r);
155 _debug("PUT B=%x %d", d, r - 1);
157 rxrpc_free_bundle(bundle);
161 * Allocate a client connection.
163 static struct rxrpc_connection *
164 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
166 struct rxrpc_connection *conn;
167 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
172 conn = rxrpc_alloc_connection(gfp);
174 _leave(" = -ENOMEM");
175 return ERR_PTR(-ENOMEM);
178 refcount_set(&conn->ref, 1);
179 conn->bundle = bundle;
180 conn->params = bundle->params;
181 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
182 conn->state = RXRPC_CONN_CLIENT;
183 conn->service_id = conn->params.service_id;
185 ret = rxrpc_get_client_connection_id(conn, gfp);
189 ret = rxrpc_init_client_conn_security(conn);
193 atomic_inc(&rxnet->nr_conns);
194 write_lock(&rxnet->conn_lock);
195 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
196 write_unlock(&rxnet->conn_lock);
198 rxrpc_get_bundle(bundle);
199 rxrpc_get_peer(conn->params.peer);
200 rxrpc_get_local(conn->params.local);
201 key_get(conn->params.key);
203 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_client,
204 refcount_read(&conn->ref),
205 __builtin_return_address(0));
207 atomic_inc(&rxnet->nr_client_conns);
208 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
209 _leave(" = %p", conn);
213 rxrpc_put_client_connection_id(conn);
216 _leave(" = %d", ret);
221 * Determine if a connection may be reused.
223 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
225 struct rxrpc_net *rxnet;
226 int id_cursor, id, distance, limit;
231 rxnet = conn->params.local->rxnet;
232 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
235 if (conn->state != RXRPC_CONN_CLIENT ||
236 conn->proto.epoch != rxnet->epoch)
237 goto mark_dont_reuse;
239 /* The IDR tree gets very expensive on memory if the connection IDs are
240 * widely scattered throughout the number space, so we shall want to
241 * kill off connections that, say, have an ID more than about four
242 * times the maximum number of client conns away from the current
243 * allocation point to try and keep the IDs concentrated.
245 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
246 id = conn->proto.cid >> RXRPC_CIDSHIFT;
247 distance = id - id_cursor;
249 distance = -distance;
250 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
251 if (distance > limit)
252 goto mark_dont_reuse;
257 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
263 * Look up the conn bundle that matches the connection parameters, adding it if
264 * it doesn't yet exist.
266 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
269 static atomic_t rxrpc_bundle_id;
270 struct rxrpc_bundle *bundle, *candidate;
271 struct rxrpc_local *local = cp->local;
272 struct rb_node *p, **pp, *parent;
275 _enter("{%px,%x,%u,%u}",
276 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
279 return rxrpc_alloc_bundle(cp, gfp);
281 /* First, see if the bundle is already there. */
283 spin_lock(&local->client_bundles_lock);
284 p = local->client_bundles.rb_node;
286 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
288 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
291 cmp(security_level) ?:
301 spin_unlock(&local->client_bundles_lock);
304 /* It wasn't. We need to add one. */
305 candidate = rxrpc_alloc_bundle(cp, gfp);
310 spin_lock(&local->client_bundles_lock);
311 pp = &local->client_bundles.rb_node;
315 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
317 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
320 cmp(security_level) ?:
324 pp = &(*pp)->rb_left;
326 pp = &(*pp)->rb_right;
328 goto found_bundle_free;
331 _debug("new bundle");
332 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
333 rb_link_node(&candidate->local_node, parent, pp);
334 rb_insert_color(&candidate->local_node, &local->client_bundles);
335 rxrpc_get_bundle(candidate);
336 spin_unlock(&local->client_bundles_lock);
337 _leave(" = %u [new]", candidate->debug_id);
341 rxrpc_free_bundle(candidate);
343 rxrpc_get_bundle(bundle);
344 atomic_inc(&bundle->active);
345 spin_unlock(&local->client_bundles_lock);
346 _leave(" = %u [found]", bundle->debug_id);
351 * Create or find a client bundle to use for a call.
353 * If we return with a connection, the call will be on its waiting list. It's
354 * left to the caller to assign a channel and wake up the call.
356 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
357 struct rxrpc_call *call,
358 struct rxrpc_conn_parameters *cp,
359 struct sockaddr_rxrpc *srx,
362 struct rxrpc_bundle *bundle;
364 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
366 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
370 call->cong_cwnd = cp->peer->cong_cwnd;
371 if (call->cong_cwnd >= call->cong_ssthresh)
372 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
374 call->cong_mode = RXRPC_CALL_SLOW_START;
376 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
378 /* Find the client connection bundle. */
379 bundle = rxrpc_look_up_bundle(cp, gfp);
383 /* Get this call queued. Someone else may activate it whilst we're
384 * lining up a new connection, but that's fine.
386 spin_lock(&bundle->channel_lock);
387 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
388 spin_unlock(&bundle->channel_lock);
390 _leave(" = [B=%x]", bundle->debug_id);
394 _leave(" = -ENOMEM");
395 return ERR_PTR(-ENOMEM);
399 * Allocate a new connection and add it into a bundle.
401 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
402 __releases(bundle->channel_lock)
404 struct rxrpc_connection *candidate = NULL, *old = NULL;
410 conflict = bundle->alloc_conn;
412 bundle->alloc_conn = true;
413 spin_unlock(&bundle->channel_lock);
419 candidate = rxrpc_alloc_client_connection(bundle, gfp);
421 spin_lock(&bundle->channel_lock);
422 bundle->alloc_conn = false;
424 if (IS_ERR(candidate)) {
425 bundle->alloc_error = PTR_ERR(candidate);
426 spin_unlock(&bundle->channel_lock);
427 _leave(" [err %ld]", PTR_ERR(candidate));
431 bundle->alloc_error = 0;
433 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
434 unsigned int shift = i * RXRPC_MAXCALLS;
437 old = bundle->conns[i];
438 if (!rxrpc_may_reuse_conn(old)) {
440 trace_rxrpc_client(old, -1, rxrpc_client_replace);
441 candidate->bundle_shift = shift;
442 atomic_inc(&bundle->active);
443 bundle->conns[i] = candidate;
444 for (j = 0; j < RXRPC_MAXCALLS; j++)
445 set_bit(shift + j, &bundle->avail_chans);
453 spin_unlock(&bundle->channel_lock);
456 _debug("discard C=%x", candidate->debug_id);
457 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
458 rxrpc_put_connection(candidate);
461 rxrpc_put_connection(old);
466 * Add a connection to a bundle if there are no usable connections or we have
467 * connections waiting for extra capacity.
469 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
471 struct rxrpc_call *call;
476 spin_lock(&bundle->channel_lock);
478 /* See if there are any usable connections. */
480 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
481 if (rxrpc_may_reuse_conn(bundle->conns[i]))
484 if (!usable && !list_empty(&bundle->waiting_calls)) {
485 call = list_first_entry(&bundle->waiting_calls,
486 struct rxrpc_call, chan_wait_link);
487 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
488 bundle->try_upgrade = true;
494 if (!bundle->avail_chans &&
495 !bundle->try_upgrade &&
496 !list_empty(&bundle->waiting_calls) &&
497 usable < ARRAY_SIZE(bundle->conns))
500 spin_unlock(&bundle->channel_lock);
505 return rxrpc_add_conn_to_bundle(bundle, gfp);
509 * Assign a channel to the call at the front of the queue and wake the call up.
510 * We don't increment the callNumber counter until this number has been exposed
513 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
514 unsigned int channel)
516 struct rxrpc_channel *chan = &conn->channels[channel];
517 struct rxrpc_bundle *bundle = conn->bundle;
518 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
519 struct rxrpc_call, chan_wait_link);
520 u32 call_id = chan->call_counter + 1;
522 _enter("C=%x,%u", conn->debug_id, channel);
524 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
526 /* Cancel the final ACK on the previous call if it hasn't been sent yet
527 * as the DATA packet will implicitly ACK it.
529 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
530 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
532 rxrpc_see_call(call);
533 list_del_init(&call->chan_wait_link);
534 call->peer = rxrpc_get_peer(conn->params.peer);
535 call->conn = rxrpc_get_connection(conn);
536 call->cid = conn->proto.cid | channel;
537 call->call_id = call_id;
538 call->security = conn->security;
539 call->security_ix = conn->security_ix;
540 call->service_id = conn->service_id;
542 trace_rxrpc_connect_call(call);
543 _net("CONNECT call %08x:%08x as call %d on conn %d",
544 call->cid, call->call_id, call->debug_id, conn->debug_id);
546 write_lock_bh(&call->state_lock);
547 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
548 write_unlock_bh(&call->state_lock);
550 /* Paired with the read barrier in rxrpc_connect_call(). This orders
551 * cid and epoch in the connection wrt to call_id without the need to
552 * take the channel_lock.
554 * We provisionally assign a callNumber at this point, but we don't
555 * confirm it until the call is about to be exposed.
557 * TODO: Pair with a barrier in the data_ready handler when that looks
558 * at the call ID through a connection channel.
562 chan->call_id = call_id;
563 chan->call_debug_id = call->debug_id;
564 rcu_assign_pointer(chan->call, call);
565 wake_up(&call->waitq);
569 * Remove a connection from the idle list if it's on it.
571 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
573 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
576 if (!list_empty(&conn->cache_link)) {
578 spin_lock(&rxnet->client_conn_cache_lock);
579 if (!list_empty(&conn->cache_link)) {
580 list_del_init(&conn->cache_link);
583 spin_unlock(&rxnet->client_conn_cache_lock);
585 rxrpc_put_connection(conn);
590 * Assign channels and callNumbers to waiting calls with channel_lock
593 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
595 struct rxrpc_connection *conn;
596 unsigned long avail, mask;
597 unsigned int channel, slot;
599 if (bundle->try_upgrade)
604 while (!list_empty(&bundle->waiting_calls)) {
605 avail = bundle->avail_chans & mask;
608 channel = __ffs(avail);
609 clear_bit(channel, &bundle->avail_chans);
611 slot = channel / RXRPC_MAXCALLS;
612 conn = bundle->conns[slot];
616 if (bundle->try_upgrade)
617 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
618 rxrpc_unidle_conn(bundle, conn);
620 channel &= (RXRPC_MAXCALLS - 1);
621 conn->act_chans |= 1 << channel;
622 rxrpc_activate_one_channel(conn, channel);
627 * Assign channels and callNumbers to waiting calls.
629 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
631 _enter("B=%x", bundle->debug_id);
633 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
635 if (!bundle->avail_chans)
638 spin_lock(&bundle->channel_lock);
639 rxrpc_activate_channels_locked(bundle);
640 spin_unlock(&bundle->channel_lock);
645 * Wait for a callNumber and a channel to be granted to a call.
647 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
648 struct rxrpc_call *call, gfp_t gfp)
650 DECLARE_WAITQUEUE(myself, current);
653 _enter("%d", call->debug_id);
655 if (!gfpflags_allow_blocking(gfp)) {
656 rxrpc_maybe_add_conn(bundle, gfp);
657 rxrpc_activate_channels(bundle);
658 ret = bundle->alloc_error ?: -EAGAIN;
662 add_wait_queue_exclusive(&call->waitq, &myself);
664 rxrpc_maybe_add_conn(bundle, gfp);
665 rxrpc_activate_channels(bundle);
666 ret = bundle->alloc_error;
670 switch (call->interruptibility) {
671 case RXRPC_INTERRUPTIBLE:
672 case RXRPC_PREINTERRUPTIBLE:
673 set_current_state(TASK_INTERRUPTIBLE);
675 case RXRPC_UNINTERRUPTIBLE:
677 set_current_state(TASK_UNINTERRUPTIBLE);
680 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
682 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
683 call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
684 signal_pending(current)) {
690 remove_wait_queue(&call->waitq, &myself);
691 __set_current_state(TASK_RUNNING);
694 _leave(" = %d", ret);
699 * find a connection for a call
700 * - called in process context with IRQs enabled
702 int rxrpc_connect_call(struct rxrpc_sock *rx,
703 struct rxrpc_call *call,
704 struct rxrpc_conn_parameters *cp,
705 struct sockaddr_rxrpc *srx,
708 struct rxrpc_bundle *bundle;
709 struct rxrpc_net *rxnet = cp->local->rxnet;
712 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
714 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
716 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
717 if (IS_ERR(bundle)) {
718 ret = PTR_ERR(bundle);
722 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
723 ret = rxrpc_wait_for_channel(bundle, call, gfp);
729 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
733 rxrpc_deactivate_bundle(bundle);
734 rxrpc_put_bundle(bundle);
736 _leave(" = %d", ret);
740 spin_lock(&bundle->channel_lock);
741 list_del_init(&call->chan_wait_link);
742 spin_unlock(&bundle->channel_lock);
744 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
746 goto granted_channel;
749 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
750 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
751 rxrpc_disconnect_client_call(bundle, call);
756 * Note that a call, and thus a connection, is about to be exposed to the
759 void rxrpc_expose_client_call(struct rxrpc_call *call)
761 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
762 struct rxrpc_connection *conn = call->conn;
763 struct rxrpc_channel *chan = &conn->channels[channel];
765 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
766 /* Mark the call ID as being used. If the callNumber counter
767 * exceeds ~2 billion, we kill the connection after its
768 * outstanding calls have finished so that the counter doesn't
771 chan->call_counter++;
772 if (chan->call_counter >= INT_MAX)
773 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
774 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
779 * Set the reap timer.
781 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
783 if (!rxnet->kill_all_client_conns) {
784 unsigned long now = jiffies;
785 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
788 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
793 * Disconnect a client call.
795 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
797 struct rxrpc_connection *conn;
798 struct rxrpc_channel *chan = NULL;
799 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
800 unsigned int channel;
804 _enter("c=%x", call->debug_id);
806 spin_lock(&bundle->channel_lock);
807 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
809 /* Calls that have never actually been assigned a channel can simply be
814 _debug("call is waiting");
815 ASSERTCMP(call->call_id, ==, 0);
816 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
817 list_del_init(&call->chan_wait_link);
822 channel = cid & RXRPC_CHANNELMASK;
823 chan = &conn->channels[channel];
824 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
826 if (rcu_access_pointer(chan->call) != call) {
827 spin_unlock(&bundle->channel_lock);
831 may_reuse = rxrpc_may_reuse_conn(conn);
833 /* If a client call was exposed to the world, we save the result for
836 * We use a barrier here so that the call number and abort code can be
837 * read without needing to take a lock.
839 * TODO: Make the incoming packet handler check this and handle
840 * terminal retransmission without requiring access to the call.
842 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
843 _debug("exposed %u,%u", call->call_id, call->abort_code);
844 __rxrpc_disconnect_call(conn, call);
846 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
847 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
848 bundle->try_upgrade = false;
850 rxrpc_activate_channels_locked(bundle);
855 /* See if we can pass the channel directly to another call. */
856 if (may_reuse && !list_empty(&bundle->waiting_calls)) {
857 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
858 rxrpc_activate_one_channel(conn, channel);
862 /* Schedule the final ACK to be transmitted in a short while so that it
863 * can be skipped if we find a follow-on call. The first DATA packet
864 * of the follow on call will implicitly ACK this call.
866 if (call->completion == RXRPC_CALL_SUCCEEDED &&
867 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
868 unsigned long final_ack_at = jiffies + 2;
870 WRITE_ONCE(chan->final_ack_at, final_ack_at);
871 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
872 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
873 rxrpc_reduce_conn_timer(conn, final_ack_at);
876 /* Deactivate the channel. */
877 rcu_assign_pointer(chan->call, NULL);
878 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
879 conn->act_chans &= ~(1 << channel);
881 /* If no channels remain active, then put the connection on the idle
882 * list for a short while. Give it a ref to stop it going away if it
885 if (!conn->act_chans) {
886 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
887 conn->idle_timestamp = jiffies;
889 rxrpc_get_connection(conn);
890 spin_lock(&rxnet->client_conn_cache_lock);
891 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
892 spin_unlock(&rxnet->client_conn_cache_lock);
894 rxrpc_set_client_reap_timer(rxnet);
898 spin_unlock(&bundle->channel_lock);
904 * Remove a connection from a bundle.
906 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
908 struct rxrpc_bundle *bundle = conn->bundle;
910 bool need_drop = false;
913 _enter("C=%x", conn->debug_id);
915 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
916 rxrpc_process_delayed_final_acks(conn, true);
918 spin_lock(&bundle->channel_lock);
919 bindex = conn->bundle_shift / RXRPC_MAXCALLS;
920 if (bundle->conns[bindex] == conn) {
921 _debug("clear slot %u", bindex);
922 bundle->conns[bindex] = NULL;
923 for (i = 0; i < RXRPC_MAXCALLS; i++)
924 clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
927 spin_unlock(&bundle->channel_lock);
930 rxrpc_deactivate_bundle(bundle);
931 rxrpc_put_connection(conn);
936 * Drop the active count on a bundle.
938 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle)
940 struct rxrpc_local *local = bundle->params.local;
941 bool need_put = false;
943 if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) {
944 if (!bundle->params.exclusive) {
945 _debug("erase bundle");
946 rb_erase(&bundle->local_node, &local->client_bundles);
950 spin_unlock(&local->client_bundles_lock);
952 rxrpc_put_bundle(bundle);
957 * Clean up a dead client connection.
959 static void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
961 struct rxrpc_local *local = conn->params.local;
962 struct rxrpc_net *rxnet = local->rxnet;
964 _enter("C=%x", conn->debug_id);
966 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
967 atomic_dec(&rxnet->nr_client_conns);
969 rxrpc_put_client_connection_id(conn);
970 rxrpc_kill_connection(conn);
974 * Clean up a dead client connections.
976 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
978 const void *here = __builtin_return_address(0);
979 unsigned int debug_id = conn->debug_id;
983 dead = __refcount_dec_and_test(&conn->ref, &r);
984 trace_rxrpc_conn(debug_id, rxrpc_conn_put_client, r - 1, here);
986 rxrpc_kill_client_conn(conn);
990 * Discard expired client connections from the idle list. Each conn in the
991 * idle list has been exposed and holds an extra ref because of that.
993 * This may be called from conn setup or from a work item so cannot be
994 * considered non-reentrant.
996 void rxrpc_discard_expired_client_conns(struct work_struct *work)
998 struct rxrpc_connection *conn;
999 struct rxrpc_net *rxnet =
1000 container_of(work, struct rxrpc_net, client_conn_reaper);
1001 unsigned long expiry, conn_expires_at, now;
1002 unsigned int nr_conns;
1006 if (list_empty(&rxnet->idle_client_conns)) {
1011 /* Don't double up on the discarding */
1012 if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
1013 _leave(" [already]");
1017 /* We keep an estimate of what the number of conns ought to be after
1018 * we've discarded some so that we don't overdo the discarding.
1020 nr_conns = atomic_read(&rxnet->nr_client_conns);
1023 spin_lock(&rxnet->client_conn_cache_lock);
1025 if (list_empty(&rxnet->idle_client_conns))
1028 conn = list_entry(rxnet->idle_client_conns.next,
1029 struct rxrpc_connection, cache_link);
1031 if (!rxnet->kill_all_client_conns) {
1032 /* If the number of connections is over the reap limit, we
1033 * expedite discard by reducing the expiry timeout. We must,
1034 * however, have at least a short grace period to be able to do
1035 * final-ACK or ABORT retransmission.
1037 expiry = rxrpc_conn_idle_client_expiry;
1038 if (nr_conns > rxrpc_reap_client_connections)
1039 expiry = rxrpc_conn_idle_client_fast_expiry;
1040 if (conn->params.local->service_closed)
1041 expiry = rxrpc_closed_conn_expiry * HZ;
1043 conn_expires_at = conn->idle_timestamp + expiry;
1045 now = READ_ONCE(jiffies);
1046 if (time_after(conn_expires_at, now))
1047 goto not_yet_expired;
1050 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1051 list_del_init(&conn->cache_link);
1053 spin_unlock(&rxnet->client_conn_cache_lock);
1055 rxrpc_unbundle_conn(conn);
1056 rxrpc_put_connection(conn); /* Drop the ->cache_link ref */
1062 /* The connection at the front of the queue hasn't yet expired, so
1063 * schedule the work item for that point if we discarded something.
1065 * We don't worry if the work item is already scheduled - it can look
1066 * after rescheduling itself at a later time. We could cancel it, but
1067 * then things get messier.
1070 if (!rxnet->kill_all_client_conns)
1071 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1074 spin_unlock(&rxnet->client_conn_cache_lock);
1075 spin_unlock(&rxnet->client_conn_discard_lock);
1080 * Preemptively destroy all the client connection records rather than waiting
1081 * for them to time out
1083 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1087 spin_lock(&rxnet->client_conn_cache_lock);
1088 rxnet->kill_all_client_conns = true;
1089 spin_unlock(&rxnet->client_conn_cache_lock);
1091 del_timer_sync(&rxnet->client_conn_reap_timer);
1093 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1094 _debug("destroy: queue failed");
1100 * Clean up the client connections on a local endpoint.
1102 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1104 struct rxrpc_connection *conn, *tmp;
1105 struct rxrpc_net *rxnet = local->rxnet;
1106 LIST_HEAD(graveyard);
1110 spin_lock(&rxnet->client_conn_cache_lock);
1112 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1114 if (conn->params.local == local) {
1115 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1116 list_move(&conn->cache_link, &graveyard);
1120 spin_unlock(&rxnet->client_conn_cache_lock);
1122 while (!list_empty(&graveyard)) {
1123 conn = list_entry(graveyard.next,
1124 struct rxrpc_connection, cache_link);
1125 list_del_init(&conn->cache_link);
1126 rxrpc_unbundle_conn(conn);
1127 rxrpc_put_connection(conn);
1130 _leave(" [culled]");