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);
44 * Get a connection ID and epoch for a client connection from the global pool.
45 * The connection struct pointer is then recorded in the idr radix tree. The
46 * epoch doesn't change until the client is rebooted (or, at least, unless the
47 * module is unloaded).
49 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
52 struct rxrpc_net *rxnet = conn->params.local->rxnet;
58 spin_lock(&rxrpc_conn_id_lock);
60 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
61 1, 0x40000000, GFP_NOWAIT);
65 spin_unlock(&rxrpc_conn_id_lock);
68 conn->proto.epoch = rxnet->epoch;
69 conn->proto.cid = id << RXRPC_CIDSHIFT;
70 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
71 _leave(" [CID %x]", conn->proto.cid);
75 spin_unlock(&rxrpc_conn_id_lock);
82 * Release a connection ID for a client connection from the global pool.
84 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
86 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
87 spin_lock(&rxrpc_conn_id_lock);
88 idr_remove(&rxrpc_client_conn_ids,
89 conn->proto.cid >> RXRPC_CIDSHIFT);
90 spin_unlock(&rxrpc_conn_id_lock);
95 * Destroy the client connection ID tree.
97 void rxrpc_destroy_client_conn_ids(void)
99 struct rxrpc_connection *conn;
102 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
103 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
104 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
105 conn, atomic_read(&conn->usage));
110 idr_destroy(&rxrpc_client_conn_ids);
114 * Allocate a connection bundle.
116 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp,
119 struct rxrpc_bundle *bundle;
121 bundle = kzalloc(sizeof(*bundle), gfp);
123 bundle->params = *cp;
124 rxrpc_get_peer(bundle->params.peer);
125 atomic_set(&bundle->usage, 1);
126 spin_lock_init(&bundle->channel_lock);
127 INIT_LIST_HEAD(&bundle->waiting_calls);
132 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle)
134 atomic_inc(&bundle->usage);
138 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
140 rxrpc_put_peer(bundle->params.peer);
144 void rxrpc_put_bundle(struct rxrpc_bundle *bundle)
146 unsigned int d = bundle->debug_id;
147 unsigned int u = atomic_dec_return(&bundle->usage);
149 _debug("PUT B=%x %u", d, u);
151 rxrpc_free_bundle(bundle);
155 * Allocate a client connection.
157 static struct rxrpc_connection *
158 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
160 struct rxrpc_connection *conn;
161 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
166 conn = rxrpc_alloc_connection(gfp);
168 _leave(" = -ENOMEM");
169 return ERR_PTR(-ENOMEM);
172 atomic_set(&conn->usage, 1);
173 conn->bundle = bundle;
174 conn->params = bundle->params;
175 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
176 conn->state = RXRPC_CONN_CLIENT;
177 conn->service_id = conn->params.service_id;
179 ret = rxrpc_get_client_connection_id(conn, gfp);
183 ret = rxrpc_init_client_conn_security(conn);
187 atomic_inc(&rxnet->nr_conns);
188 write_lock(&rxnet->conn_lock);
189 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
190 write_unlock(&rxnet->conn_lock);
192 rxrpc_get_bundle(bundle);
193 rxrpc_get_peer(conn->params.peer);
194 rxrpc_get_local(conn->params.local);
195 key_get(conn->params.key);
197 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_client,
198 atomic_read(&conn->usage),
199 __builtin_return_address(0));
201 atomic_inc(&rxnet->nr_client_conns);
202 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
203 _leave(" = %p", conn);
207 rxrpc_put_client_connection_id(conn);
210 _leave(" = %d", ret);
215 * Determine if a connection may be reused.
217 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
219 struct rxrpc_net *rxnet;
220 int id_cursor, id, distance, limit;
225 rxnet = conn->params.local->rxnet;
226 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
229 if (conn->state != RXRPC_CONN_CLIENT ||
230 conn->proto.epoch != rxnet->epoch)
231 goto mark_dont_reuse;
233 /* The IDR tree gets very expensive on memory if the connection IDs are
234 * widely scattered throughout the number space, so we shall want to
235 * kill off connections that, say, have an ID more than about four
236 * times the maximum number of client conns away from the current
237 * allocation point to try and keep the IDs concentrated.
239 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
240 id = conn->proto.cid >> RXRPC_CIDSHIFT;
241 distance = id - id_cursor;
243 distance = -distance;
244 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
245 if (distance > limit)
246 goto mark_dont_reuse;
251 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
257 * Look up the conn bundle that matches the connection parameters, adding it if
258 * it doesn't yet exist.
260 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
263 static atomic_t rxrpc_bundle_id;
264 struct rxrpc_bundle *bundle, *candidate;
265 struct rxrpc_local *local = cp->local;
266 struct rb_node *p, **pp, *parent;
269 _enter("{%px,%x,%u,%u}",
270 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
273 return rxrpc_alloc_bundle(cp, gfp);
275 /* First, see if the bundle is already there. */
277 spin_lock(&local->client_bundles_lock);
278 p = local->client_bundles.rb_node;
280 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
282 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
285 cmp(security_level) ?:
295 spin_unlock(&local->client_bundles_lock);
298 /* It wasn't. We need to add one. */
299 candidate = rxrpc_alloc_bundle(cp, gfp);
304 spin_lock(&local->client_bundles_lock);
305 pp = &local->client_bundles.rb_node;
309 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
311 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
314 cmp(security_level) ?:
318 pp = &(*pp)->rb_left;
320 pp = &(*pp)->rb_right;
322 goto found_bundle_free;
325 _debug("new bundle");
326 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
327 rb_link_node(&candidate->local_node, parent, pp);
328 rb_insert_color(&candidate->local_node, &local->client_bundles);
329 rxrpc_get_bundle(candidate);
330 spin_unlock(&local->client_bundles_lock);
331 _leave(" = %u [new]", candidate->debug_id);
335 rxrpc_free_bundle(candidate);
337 rxrpc_get_bundle(bundle);
338 spin_unlock(&local->client_bundles_lock);
339 _leave(" = %u [found]", bundle->debug_id);
344 * Create or find a client bundle to use for a call.
346 * If we return with a connection, the call will be on its waiting list. It's
347 * left to the caller to assign a channel and wake up the call.
349 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
350 struct rxrpc_call *call,
351 struct rxrpc_conn_parameters *cp,
352 struct sockaddr_rxrpc *srx,
355 struct rxrpc_bundle *bundle;
357 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
359 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
363 call->cong_cwnd = cp->peer->cong_cwnd;
364 if (call->cong_cwnd >= call->cong_ssthresh)
365 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
367 call->cong_mode = RXRPC_CALL_SLOW_START;
369 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
371 /* Find the client connection bundle. */
372 bundle = rxrpc_look_up_bundle(cp, gfp);
376 /* Get this call queued. Someone else may activate it whilst we're
377 * lining up a new connection, but that's fine.
379 spin_lock(&bundle->channel_lock);
380 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
381 spin_unlock(&bundle->channel_lock);
383 _leave(" = [B=%x]", bundle->debug_id);
387 _leave(" = -ENOMEM");
388 return ERR_PTR(-ENOMEM);
392 * Allocate a new connection and add it into a bundle.
394 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
395 __releases(bundle->channel_lock)
397 struct rxrpc_connection *candidate = NULL, *old = NULL;
403 conflict = bundle->alloc_conn;
405 bundle->alloc_conn = true;
406 spin_unlock(&bundle->channel_lock);
412 candidate = rxrpc_alloc_client_connection(bundle, gfp);
414 spin_lock(&bundle->channel_lock);
415 bundle->alloc_conn = false;
417 if (IS_ERR(candidate)) {
418 bundle->alloc_error = PTR_ERR(candidate);
419 spin_unlock(&bundle->channel_lock);
420 _leave(" [err %ld]", PTR_ERR(candidate));
424 bundle->alloc_error = 0;
426 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
427 unsigned int shift = i * RXRPC_MAXCALLS;
430 old = bundle->conns[i];
431 if (!rxrpc_may_reuse_conn(old)) {
433 trace_rxrpc_client(old, -1, rxrpc_client_replace);
434 candidate->bundle_shift = shift;
435 bundle->conns[i] = candidate;
436 for (j = 0; j < RXRPC_MAXCALLS; j++)
437 set_bit(shift + j, &bundle->avail_chans);
445 spin_unlock(&bundle->channel_lock);
448 _debug("discard C=%x", candidate->debug_id);
449 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
450 rxrpc_put_connection(candidate);
453 rxrpc_put_connection(old);
458 * Add a connection to a bundle if there are no usable connections or we have
459 * connections waiting for extra capacity.
461 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
463 struct rxrpc_call *call;
468 spin_lock(&bundle->channel_lock);
470 /* See if there are any usable connections. */
472 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
473 if (rxrpc_may_reuse_conn(bundle->conns[i]))
476 if (!usable && !list_empty(&bundle->waiting_calls)) {
477 call = list_first_entry(&bundle->waiting_calls,
478 struct rxrpc_call, chan_wait_link);
479 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
480 bundle->try_upgrade = true;
486 if (!bundle->avail_chans &&
487 !bundle->try_upgrade &&
488 !list_empty(&bundle->waiting_calls) &&
489 usable < ARRAY_SIZE(bundle->conns))
492 spin_unlock(&bundle->channel_lock);
497 return rxrpc_add_conn_to_bundle(bundle, gfp);
501 * Assign a channel to the call at the front of the queue and wake the call up.
502 * We don't increment the callNumber counter until this number has been exposed
505 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
506 unsigned int channel)
508 struct rxrpc_channel *chan = &conn->channels[channel];
509 struct rxrpc_bundle *bundle = conn->bundle;
510 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
511 struct rxrpc_call, chan_wait_link);
512 u32 call_id = chan->call_counter + 1;
514 _enter("C=%x,%u", conn->debug_id, channel);
516 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
518 /* Cancel the final ACK on the previous call if it hasn't been sent yet
519 * as the DATA packet will implicitly ACK it.
521 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
522 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
524 rxrpc_see_call(call);
525 list_del_init(&call->chan_wait_link);
526 call->peer = rxrpc_get_peer(conn->params.peer);
527 call->conn = rxrpc_get_connection(conn);
528 call->cid = conn->proto.cid | channel;
529 call->call_id = call_id;
530 call->security = conn->security;
531 call->security_ix = conn->security_ix;
532 call->service_id = conn->service_id;
534 trace_rxrpc_connect_call(call);
535 _net("CONNECT call %08x:%08x as call %d on conn %d",
536 call->cid, call->call_id, call->debug_id, conn->debug_id);
538 write_lock_bh(&call->state_lock);
539 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
540 write_unlock_bh(&call->state_lock);
542 /* Paired with the read barrier in rxrpc_connect_call(). This orders
543 * cid and epoch in the connection wrt to call_id without the need to
544 * take the channel_lock.
546 * We provisionally assign a callNumber at this point, but we don't
547 * confirm it until the call is about to be exposed.
549 * TODO: Pair with a barrier in the data_ready handler when that looks
550 * at the call ID through a connection channel.
554 chan->call_id = call_id;
555 chan->call_debug_id = call->debug_id;
556 rcu_assign_pointer(chan->call, call);
557 wake_up(&call->waitq);
561 * Remove a connection from the idle list if it's on it.
563 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
565 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
568 if (!list_empty(&conn->cache_link)) {
570 spin_lock(&rxnet->client_conn_cache_lock);
571 if (!list_empty(&conn->cache_link)) {
572 list_del_init(&conn->cache_link);
575 spin_unlock(&rxnet->client_conn_cache_lock);
577 rxrpc_put_connection(conn);
582 * Assign channels and callNumbers to waiting calls with channel_lock
585 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
587 struct rxrpc_connection *conn;
588 unsigned long avail, mask;
589 unsigned int channel, slot;
591 if (bundle->try_upgrade)
596 while (!list_empty(&bundle->waiting_calls)) {
597 avail = bundle->avail_chans & mask;
600 channel = __ffs(avail);
601 clear_bit(channel, &bundle->avail_chans);
603 slot = channel / RXRPC_MAXCALLS;
604 conn = bundle->conns[slot];
608 if (bundle->try_upgrade)
609 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
610 rxrpc_unidle_conn(bundle, conn);
612 channel &= (RXRPC_MAXCALLS - 1);
613 conn->act_chans |= 1 << channel;
614 rxrpc_activate_one_channel(conn, channel);
619 * Assign channels and callNumbers to waiting calls.
621 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
623 _enter("B=%x", bundle->debug_id);
625 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
627 if (!bundle->avail_chans)
630 spin_lock(&bundle->channel_lock);
631 rxrpc_activate_channels_locked(bundle);
632 spin_unlock(&bundle->channel_lock);
637 * Wait for a callNumber and a channel to be granted to a call.
639 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
640 struct rxrpc_call *call, gfp_t gfp)
642 DECLARE_WAITQUEUE(myself, current);
645 _enter("%d", call->debug_id);
647 if (!gfpflags_allow_blocking(gfp)) {
648 rxrpc_maybe_add_conn(bundle, gfp);
649 rxrpc_activate_channels(bundle);
650 ret = bundle->alloc_error ?: -EAGAIN;
654 add_wait_queue_exclusive(&call->waitq, &myself);
656 rxrpc_maybe_add_conn(bundle, gfp);
657 rxrpc_activate_channels(bundle);
658 ret = bundle->alloc_error;
662 switch (call->interruptibility) {
663 case RXRPC_INTERRUPTIBLE:
664 case RXRPC_PREINTERRUPTIBLE:
665 set_current_state(TASK_INTERRUPTIBLE);
667 case RXRPC_UNINTERRUPTIBLE:
669 set_current_state(TASK_UNINTERRUPTIBLE);
672 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
674 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
675 call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
676 signal_pending(current)) {
682 remove_wait_queue(&call->waitq, &myself);
683 __set_current_state(TASK_RUNNING);
686 _leave(" = %d", ret);
691 * find a connection for a call
692 * - called in process context with IRQs enabled
694 int rxrpc_connect_call(struct rxrpc_sock *rx,
695 struct rxrpc_call *call,
696 struct rxrpc_conn_parameters *cp,
697 struct sockaddr_rxrpc *srx,
700 struct rxrpc_bundle *bundle;
701 struct rxrpc_net *rxnet = cp->local->rxnet;
704 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
706 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
708 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
709 if (IS_ERR(bundle)) {
710 ret = PTR_ERR(bundle);
714 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
715 ret = rxrpc_wait_for_channel(bundle, call, gfp);
721 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
725 rxrpc_put_bundle(bundle);
727 _leave(" = %d", ret);
731 spin_lock(&bundle->channel_lock);
732 list_del_init(&call->chan_wait_link);
733 spin_unlock(&bundle->channel_lock);
735 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
737 goto granted_channel;
740 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
741 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
742 rxrpc_disconnect_client_call(bundle, call);
747 * Note that a call, and thus a connection, is about to be exposed to the
750 void rxrpc_expose_client_call(struct rxrpc_call *call)
752 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
753 struct rxrpc_connection *conn = call->conn;
754 struct rxrpc_channel *chan = &conn->channels[channel];
756 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
757 /* Mark the call ID as being used. If the callNumber counter
758 * exceeds ~2 billion, we kill the connection after its
759 * outstanding calls have finished so that the counter doesn't
762 chan->call_counter++;
763 if (chan->call_counter >= INT_MAX)
764 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
765 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
770 * Set the reap timer.
772 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
774 if (!rxnet->kill_all_client_conns) {
775 unsigned long now = jiffies;
776 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
779 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
784 * Disconnect a client call.
786 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
788 struct rxrpc_connection *conn;
789 struct rxrpc_channel *chan = NULL;
790 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
791 unsigned int channel;
795 _enter("c=%x", call->debug_id);
797 spin_lock(&bundle->channel_lock);
798 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
800 /* Calls that have never actually been assigned a channel can simply be
805 _debug("call is waiting");
806 ASSERTCMP(call->call_id, ==, 0);
807 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
808 list_del_init(&call->chan_wait_link);
813 channel = cid & RXRPC_CHANNELMASK;
814 chan = &conn->channels[channel];
815 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
817 if (rcu_access_pointer(chan->call) != call) {
818 spin_unlock(&bundle->channel_lock);
822 may_reuse = rxrpc_may_reuse_conn(conn);
824 /* If a client call was exposed to the world, we save the result for
827 * We use a barrier here so that the call number and abort code can be
828 * read without needing to take a lock.
830 * TODO: Make the incoming packet handler check this and handle
831 * terminal retransmission without requiring access to the call.
833 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
834 _debug("exposed %u,%u", call->call_id, call->abort_code);
835 __rxrpc_disconnect_call(conn, call);
837 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
838 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
839 bundle->try_upgrade = false;
841 rxrpc_activate_channels_locked(bundle);
846 /* See if we can pass the channel directly to another call. */
847 if (may_reuse && !list_empty(&bundle->waiting_calls)) {
848 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
849 rxrpc_activate_one_channel(conn, channel);
853 /* Schedule the final ACK to be transmitted in a short while so that it
854 * can be skipped if we find a follow-on call. The first DATA packet
855 * of the follow on call will implicitly ACK this call.
857 if (call->completion == RXRPC_CALL_SUCCEEDED &&
858 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
859 unsigned long final_ack_at = jiffies + 2;
861 WRITE_ONCE(chan->final_ack_at, final_ack_at);
862 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
863 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
864 rxrpc_reduce_conn_timer(conn, final_ack_at);
867 /* Deactivate the channel. */
868 rcu_assign_pointer(chan->call, NULL);
869 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
870 conn->act_chans &= ~(1 << channel);
872 /* If no channels remain active, then put the connection on the idle
873 * list for a short while. Give it a ref to stop it going away if it
876 if (!conn->act_chans) {
877 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
878 conn->idle_timestamp = jiffies;
880 rxrpc_get_connection(conn);
881 spin_lock(&rxnet->client_conn_cache_lock);
882 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
883 spin_unlock(&rxnet->client_conn_cache_lock);
885 rxrpc_set_client_reap_timer(rxnet);
889 spin_unlock(&bundle->channel_lock);
895 * Remove a connection from a bundle.
897 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
899 struct rxrpc_bundle *bundle = conn->bundle;
900 struct rxrpc_local *local = bundle->params.local;
902 bool need_drop = false, need_put = false;
905 _enter("C=%x", conn->debug_id);
907 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
908 rxrpc_process_delayed_final_acks(conn, true);
910 spin_lock(&bundle->channel_lock);
911 bindex = conn->bundle_shift / RXRPC_MAXCALLS;
912 if (bundle->conns[bindex] == conn) {
913 _debug("clear slot %u", bindex);
914 bundle->conns[bindex] = NULL;
915 for (i = 0; i < RXRPC_MAXCALLS; i++)
916 clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
919 spin_unlock(&bundle->channel_lock);
921 /* If there are no more connections, remove the bundle */
922 if (!bundle->avail_chans) {
923 _debug("maybe unbundle");
924 spin_lock(&local->client_bundles_lock);
926 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
927 if (bundle->conns[i])
929 if (i == ARRAY_SIZE(bundle->conns) && !bundle->params.exclusive) {
930 _debug("erase bundle");
931 rb_erase(&bundle->local_node, &local->client_bundles);
935 spin_unlock(&local->client_bundles_lock);
937 rxrpc_put_bundle(bundle);
941 rxrpc_put_connection(conn);
946 * Clean up a dead client connection.
948 static void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
950 struct rxrpc_local *local = conn->params.local;
951 struct rxrpc_net *rxnet = local->rxnet;
953 _enter("C=%x", conn->debug_id);
955 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
956 atomic_dec(&rxnet->nr_client_conns);
958 rxrpc_put_client_connection_id(conn);
959 rxrpc_kill_connection(conn);
963 * Clean up a dead client connections.
965 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
967 const void *here = __builtin_return_address(0);
968 unsigned int debug_id = conn->debug_id;
971 n = atomic_dec_return(&conn->usage);
972 trace_rxrpc_conn(debug_id, rxrpc_conn_put_client, n, here);
975 rxrpc_kill_client_conn(conn);
980 * Discard expired client connections from the idle list. Each conn in the
981 * idle list has been exposed and holds an extra ref because of that.
983 * This may be called from conn setup or from a work item so cannot be
984 * considered non-reentrant.
986 void rxrpc_discard_expired_client_conns(struct work_struct *work)
988 struct rxrpc_connection *conn;
989 struct rxrpc_net *rxnet =
990 container_of(work, struct rxrpc_net, client_conn_reaper);
991 unsigned long expiry, conn_expires_at, now;
992 unsigned int nr_conns;
996 if (list_empty(&rxnet->idle_client_conns)) {
1001 /* Don't double up on the discarding */
1002 if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
1003 _leave(" [already]");
1007 /* We keep an estimate of what the number of conns ought to be after
1008 * we've discarded some so that we don't overdo the discarding.
1010 nr_conns = atomic_read(&rxnet->nr_client_conns);
1013 spin_lock(&rxnet->client_conn_cache_lock);
1015 if (list_empty(&rxnet->idle_client_conns))
1018 conn = list_entry(rxnet->idle_client_conns.next,
1019 struct rxrpc_connection, cache_link);
1021 if (!rxnet->kill_all_client_conns) {
1022 /* If the number of connections is over the reap limit, we
1023 * expedite discard by reducing the expiry timeout. We must,
1024 * however, have at least a short grace period to be able to do
1025 * final-ACK or ABORT retransmission.
1027 expiry = rxrpc_conn_idle_client_expiry;
1028 if (nr_conns > rxrpc_reap_client_connections)
1029 expiry = rxrpc_conn_idle_client_fast_expiry;
1030 if (conn->params.local->service_closed)
1031 expiry = rxrpc_closed_conn_expiry * HZ;
1033 conn_expires_at = conn->idle_timestamp + expiry;
1035 now = READ_ONCE(jiffies);
1036 if (time_after(conn_expires_at, now))
1037 goto not_yet_expired;
1040 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1041 list_del_init(&conn->cache_link);
1043 spin_unlock(&rxnet->client_conn_cache_lock);
1045 rxrpc_unbundle_conn(conn);
1046 rxrpc_put_connection(conn); /* Drop the ->cache_link ref */
1052 /* The connection at the front of the queue hasn't yet expired, so
1053 * schedule the work item for that point if we discarded something.
1055 * We don't worry if the work item is already scheduled - it can look
1056 * after rescheduling itself at a later time. We could cancel it, but
1057 * then things get messier.
1060 if (!rxnet->kill_all_client_conns)
1061 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1064 spin_unlock(&rxnet->client_conn_cache_lock);
1065 spin_unlock(&rxnet->client_conn_discard_lock);
1070 * Preemptively destroy all the client connection records rather than waiting
1071 * for them to time out
1073 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1077 spin_lock(&rxnet->client_conn_cache_lock);
1078 rxnet->kill_all_client_conns = true;
1079 spin_unlock(&rxnet->client_conn_cache_lock);
1081 del_timer_sync(&rxnet->client_conn_reap_timer);
1083 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1084 _debug("destroy: queue failed");
1090 * Clean up the client connections on a local endpoint.
1092 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1094 struct rxrpc_connection *conn, *tmp;
1095 struct rxrpc_net *rxnet = local->rxnet;
1096 LIST_HEAD(graveyard);
1100 spin_lock(&rxnet->client_conn_cache_lock);
1102 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1104 if (conn->params.local == local) {
1105 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1106 list_move(&conn->cache_link, &graveyard);
1110 spin_unlock(&rxnet->client_conn_cache_lock);
1112 while (!list_empty(&graveyard)) {
1113 conn = list_entry(graveyard.next,
1114 struct rxrpc_connection, cache_link);
1115 list_del_init(&conn->cache_link);
1116 rxrpc_unbundle_conn(conn);
1117 rxrpc_put_connection(conn);
1120 _leave(" [culled]");