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->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->local = cp->local;
126 bundle->peer = rxrpc_get_peer(cp->peer, rxrpc_peer_get_bundle);
127 bundle->key = cp->key;
128 bundle->exclusive = cp->exclusive;
129 bundle->upgrade = cp->upgrade;
130 bundle->service_id = cp->service_id;
131 bundle->security_level = cp->security_level;
132 refcount_set(&bundle->ref, 1);
133 atomic_set(&bundle->active, 1);
134 spin_lock_init(&bundle->channel_lock);
135 INIT_LIST_HEAD(&bundle->waiting_calls);
136 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new);
141 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle,
142 enum rxrpc_bundle_trace why)
146 __refcount_inc(&bundle->ref, &r);
147 trace_rxrpc_bundle(bundle->debug_id, r + 1, why);
151 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
153 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_free);
154 rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle);
158 void rxrpc_put_bundle(struct rxrpc_bundle *bundle, enum rxrpc_bundle_trace why)
160 unsigned int id = bundle->debug_id;
164 dead = __refcount_dec_and_test(&bundle->ref, &r);
165 trace_rxrpc_bundle(id, r - 1, why);
167 rxrpc_free_bundle(bundle);
171 * Allocate a client connection.
173 static struct rxrpc_connection *
174 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
176 struct rxrpc_connection *conn;
177 struct rxrpc_net *rxnet = bundle->local->rxnet;
182 conn = rxrpc_alloc_connection(rxnet, gfp);
184 _leave(" = -ENOMEM");
185 return ERR_PTR(-ENOMEM);
188 refcount_set(&conn->ref, 1);
189 conn->bundle = bundle;
190 conn->local = bundle->local;
191 conn->peer = bundle->peer;
192 conn->key = bundle->key;
193 conn->exclusive = bundle->exclusive;
194 conn->upgrade = bundle->upgrade;
195 conn->orig_service_id = bundle->service_id;
196 conn->security_level = bundle->security_level;
197 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
198 conn->state = RXRPC_CONN_CLIENT;
199 conn->service_id = conn->orig_service_id;
201 ret = rxrpc_get_client_connection_id(conn, gfp);
205 ret = rxrpc_init_client_conn_security(conn);
209 atomic_inc(&rxnet->nr_conns);
210 write_lock(&rxnet->conn_lock);
211 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
212 write_unlock(&rxnet->conn_lock);
214 rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn);
215 rxrpc_get_peer(conn->peer, rxrpc_peer_get_client_conn);
216 rxrpc_get_local(conn->local, rxrpc_local_get_client_conn);
219 trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
220 rxrpc_conn_new_client);
222 atomic_inc(&rxnet->nr_client_conns);
223 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
224 _leave(" = %p", conn);
228 rxrpc_put_client_connection_id(conn);
231 _leave(" = %d", ret);
236 * Determine if a connection may be reused.
238 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
240 struct rxrpc_net *rxnet;
241 int id_cursor, id, distance, limit;
247 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
250 if (conn->state != RXRPC_CONN_CLIENT ||
251 conn->proto.epoch != rxnet->epoch)
252 goto mark_dont_reuse;
254 /* The IDR tree gets very expensive on memory if the connection IDs are
255 * widely scattered throughout the number space, so we shall want to
256 * kill off connections that, say, have an ID more than about four
257 * times the maximum number of client conns away from the current
258 * allocation point to try and keep the IDs concentrated.
260 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
261 id = conn->proto.cid >> RXRPC_CIDSHIFT;
262 distance = id - id_cursor;
264 distance = -distance;
265 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
266 if (distance > limit)
267 goto mark_dont_reuse;
272 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
278 * Look up the conn bundle that matches the connection parameters, adding it if
279 * it doesn't yet exist.
281 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
284 static atomic_t rxrpc_bundle_id;
285 struct rxrpc_bundle *bundle, *candidate;
286 struct rxrpc_local *local = cp->local;
287 struct rb_node *p, **pp, *parent;
290 _enter("{%px,%x,%u,%u}",
291 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
294 return rxrpc_alloc_bundle(cp, gfp);
296 /* First, see if the bundle is already there. */
298 spin_lock(&local->client_bundles_lock);
299 p = local->client_bundles.rb_node;
301 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
303 #define cmp(X) ((long)bundle->X - (long)cp->X)
306 cmp(security_level) ?:
316 spin_unlock(&local->client_bundles_lock);
319 /* It wasn't. We need to add one. */
320 candidate = rxrpc_alloc_bundle(cp, gfp);
325 spin_lock(&local->client_bundles_lock);
326 pp = &local->client_bundles.rb_node;
330 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
332 #define cmp(X) ((long)bundle->X - (long)cp->X)
335 cmp(security_level) ?:
339 pp = &(*pp)->rb_left;
341 pp = &(*pp)->rb_right;
343 goto found_bundle_free;
346 _debug("new bundle");
347 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
348 rb_link_node(&candidate->local_node, parent, pp);
349 rb_insert_color(&candidate->local_node, &local->client_bundles);
350 rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call);
351 spin_unlock(&local->client_bundles_lock);
352 _leave(" = %u [new]", candidate->debug_id);
356 rxrpc_free_bundle(candidate);
358 rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call);
359 atomic_inc(&bundle->active);
360 spin_unlock(&local->client_bundles_lock);
361 _leave(" = %u [found]", bundle->debug_id);
366 * Create or find a client bundle to use for a call.
368 * If we return with a connection, the call will be on its waiting list. It's
369 * left to the caller to assign a channel and wake up the call.
371 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
372 struct rxrpc_call *call,
373 struct rxrpc_conn_parameters *cp,
374 struct sockaddr_rxrpc *srx,
377 struct rxrpc_bundle *bundle;
379 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
381 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
385 call->tx_last_sent = ktime_get_real();
386 call->cong_ssthresh = cp->peer->cong_ssthresh;
387 if (call->cong_cwnd >= call->cong_ssthresh)
388 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
390 call->cong_mode = RXRPC_CALL_SLOW_START;
392 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
394 /* Find the client connection bundle. */
395 bundle = rxrpc_look_up_bundle(cp, gfp);
399 /* Get this call queued. Someone else may activate it whilst we're
400 * lining up a new connection, but that's fine.
402 spin_lock(&bundle->channel_lock);
403 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
404 spin_unlock(&bundle->channel_lock);
406 _leave(" = [B=%x]", bundle->debug_id);
410 _leave(" = -ENOMEM");
411 return ERR_PTR(-ENOMEM);
415 * Allocate a new connection and add it into a bundle.
417 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
418 __releases(bundle->channel_lock)
420 struct rxrpc_connection *candidate = NULL, *old = NULL;
426 conflict = bundle->alloc_conn;
428 bundle->alloc_conn = true;
429 spin_unlock(&bundle->channel_lock);
435 candidate = rxrpc_alloc_client_connection(bundle, gfp);
437 spin_lock(&bundle->channel_lock);
438 bundle->alloc_conn = false;
440 if (IS_ERR(candidate)) {
441 bundle->alloc_error = PTR_ERR(candidate);
442 spin_unlock(&bundle->channel_lock);
443 _leave(" [err %ld]", PTR_ERR(candidate));
447 bundle->alloc_error = 0;
449 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
450 unsigned int shift = i * RXRPC_MAXCALLS;
453 old = bundle->conns[i];
454 if (!rxrpc_may_reuse_conn(old)) {
456 trace_rxrpc_client(old, -1, rxrpc_client_replace);
457 candidate->bundle_shift = shift;
458 atomic_inc(&bundle->active);
459 bundle->conns[i] = candidate;
460 for (j = 0; j < RXRPC_MAXCALLS; j++)
461 set_bit(shift + j, &bundle->avail_chans);
469 spin_unlock(&bundle->channel_lock);
472 _debug("discard C=%x", candidate->debug_id);
473 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
474 rxrpc_put_connection(candidate, rxrpc_conn_put_discard);
477 rxrpc_put_connection(old, rxrpc_conn_put_noreuse);
482 * Add a connection to a bundle if there are no usable connections or we have
483 * connections waiting for extra capacity.
485 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
487 struct rxrpc_call *call;
492 spin_lock(&bundle->channel_lock);
494 /* See if there are any usable connections. */
496 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
497 if (rxrpc_may_reuse_conn(bundle->conns[i]))
500 if (!usable && !list_empty(&bundle->waiting_calls)) {
501 call = list_first_entry(&bundle->waiting_calls,
502 struct rxrpc_call, chan_wait_link);
503 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
504 bundle->try_upgrade = true;
510 if (!bundle->avail_chans &&
511 !bundle->try_upgrade &&
512 !list_empty(&bundle->waiting_calls) &&
513 usable < ARRAY_SIZE(bundle->conns))
516 spin_unlock(&bundle->channel_lock);
521 return rxrpc_add_conn_to_bundle(bundle, gfp);
525 * Assign a channel to the call at the front of the queue and wake the call up.
526 * We don't increment the callNumber counter until this number has been exposed
529 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
530 unsigned int channel)
532 struct rxrpc_channel *chan = &conn->channels[channel];
533 struct rxrpc_bundle *bundle = conn->bundle;
534 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
535 struct rxrpc_call, chan_wait_link);
536 u32 call_id = chan->call_counter + 1;
538 _enter("C=%x,%u", conn->debug_id, channel);
540 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
542 /* Cancel the final ACK on the previous call if it hasn't been sent yet
543 * as the DATA packet will implicitly ACK it.
545 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
546 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
548 rxrpc_see_call(call, rxrpc_call_see_activate_client);
549 list_del_init(&call->chan_wait_link);
550 call->peer = rxrpc_get_peer(conn->peer, rxrpc_peer_get_activate_call);
551 call->conn = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call);
552 call->cid = conn->proto.cid | channel;
553 call->call_id = call_id;
554 call->dest_srx.srx_service = conn->service_id;
556 trace_rxrpc_connect_call(call);
558 write_lock(&call->state_lock);
559 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
560 write_unlock(&call->state_lock);
562 /* Paired with the read barrier in rxrpc_connect_call(). This orders
563 * cid and epoch in the connection wrt to call_id without the need to
564 * take the channel_lock.
566 * We provisionally assign a callNumber at this point, but we don't
567 * confirm it until the call is about to be exposed.
569 * TODO: Pair with a barrier in the data_ready handler when that looks
570 * at the call ID through a connection channel.
574 chan->call_id = call_id;
575 chan->call_debug_id = call->debug_id;
576 rcu_assign_pointer(chan->call, call);
577 wake_up(&call->waitq);
581 * Remove a connection from the idle list if it's on it.
583 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
585 struct rxrpc_net *rxnet = bundle->local->rxnet;
588 if (!list_empty(&conn->cache_link)) {
590 spin_lock(&rxnet->client_conn_cache_lock);
591 if (!list_empty(&conn->cache_link)) {
592 list_del_init(&conn->cache_link);
595 spin_unlock(&rxnet->client_conn_cache_lock);
597 rxrpc_put_connection(conn, rxrpc_conn_put_unidle);
602 * Assign channels and callNumbers to waiting calls with channel_lock
605 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
607 struct rxrpc_connection *conn;
608 unsigned long avail, mask;
609 unsigned int channel, slot;
611 if (bundle->try_upgrade)
616 while (!list_empty(&bundle->waiting_calls)) {
617 avail = bundle->avail_chans & mask;
620 channel = __ffs(avail);
621 clear_bit(channel, &bundle->avail_chans);
623 slot = channel / RXRPC_MAXCALLS;
624 conn = bundle->conns[slot];
628 if (bundle->try_upgrade)
629 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
630 rxrpc_unidle_conn(bundle, conn);
632 channel &= (RXRPC_MAXCALLS - 1);
633 conn->act_chans |= 1 << channel;
634 rxrpc_activate_one_channel(conn, channel);
639 * Assign channels and callNumbers to waiting calls.
641 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
643 _enter("B=%x", bundle->debug_id);
645 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
647 if (!bundle->avail_chans)
650 spin_lock(&bundle->channel_lock);
651 rxrpc_activate_channels_locked(bundle);
652 spin_unlock(&bundle->channel_lock);
657 * Wait for a callNumber and a channel to be granted to a call.
659 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
660 struct rxrpc_call *call, gfp_t gfp)
662 DECLARE_WAITQUEUE(myself, current);
665 _enter("%d", call->debug_id);
667 if (!gfpflags_allow_blocking(gfp)) {
668 rxrpc_maybe_add_conn(bundle, gfp);
669 rxrpc_activate_channels(bundle);
670 ret = bundle->alloc_error ?: -EAGAIN;
674 add_wait_queue_exclusive(&call->waitq, &myself);
676 rxrpc_maybe_add_conn(bundle, gfp);
677 rxrpc_activate_channels(bundle);
678 ret = bundle->alloc_error;
682 switch (call->interruptibility) {
683 case RXRPC_INTERRUPTIBLE:
684 case RXRPC_PREINTERRUPTIBLE:
685 set_current_state(TASK_INTERRUPTIBLE);
687 case RXRPC_UNINTERRUPTIBLE:
689 set_current_state(TASK_UNINTERRUPTIBLE);
692 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
694 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
695 call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
696 signal_pending(current)) {
702 remove_wait_queue(&call->waitq, &myself);
703 __set_current_state(TASK_RUNNING);
706 _leave(" = %d", ret);
711 * find a connection for a call
712 * - called in process context with IRQs enabled
714 int rxrpc_connect_call(struct rxrpc_sock *rx,
715 struct rxrpc_call *call,
716 struct rxrpc_conn_parameters *cp,
717 struct sockaddr_rxrpc *srx,
720 struct rxrpc_bundle *bundle;
721 struct rxrpc_net *rxnet = cp->local->rxnet;
724 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
726 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
728 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
729 if (IS_ERR(bundle)) {
730 ret = PTR_ERR(bundle);
734 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
735 ret = rxrpc_wait_for_channel(bundle, call, gfp);
741 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
745 rxrpc_deactivate_bundle(bundle);
746 rxrpc_put_bundle(bundle, rxrpc_bundle_get_client_call);
748 _leave(" = %d", ret);
752 spin_lock(&bundle->channel_lock);
753 list_del_init(&call->chan_wait_link);
754 spin_unlock(&bundle->channel_lock);
756 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
758 goto granted_channel;
761 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
762 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
763 rxrpc_disconnect_client_call(bundle, call);
768 * Note that a call, and thus a connection, is about to be exposed to the
771 void rxrpc_expose_client_call(struct rxrpc_call *call)
773 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
774 struct rxrpc_connection *conn = call->conn;
775 struct rxrpc_channel *chan = &conn->channels[channel];
777 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
778 /* Mark the call ID as being used. If the callNumber counter
779 * exceeds ~2 billion, we kill the connection after its
780 * outstanding calls have finished so that the counter doesn't
783 chan->call_counter++;
784 if (chan->call_counter >= INT_MAX)
785 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
786 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
788 spin_lock(&call->peer->lock);
789 hlist_add_head(&call->error_link, &call->peer->error_targets);
790 spin_unlock(&call->peer->lock);
795 * Set the reap timer.
797 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
799 if (!rxnet->kill_all_client_conns) {
800 unsigned long now = jiffies;
801 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
804 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
809 * Disconnect a client call.
811 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
813 struct rxrpc_connection *conn;
814 struct rxrpc_channel *chan = NULL;
815 struct rxrpc_net *rxnet = bundle->local->rxnet;
816 unsigned int channel;
820 _enter("c=%x", call->debug_id);
822 spin_lock(&bundle->channel_lock);
823 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
825 /* Calls that have never actually been assigned a channel can simply be
830 _debug("call is waiting");
831 ASSERTCMP(call->call_id, ==, 0);
832 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
833 list_del_init(&call->chan_wait_link);
838 channel = cid & RXRPC_CHANNELMASK;
839 chan = &conn->channels[channel];
840 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
842 if (rcu_access_pointer(chan->call) != call) {
843 spin_unlock(&bundle->channel_lock);
847 may_reuse = rxrpc_may_reuse_conn(conn);
849 /* If a client call was exposed to the world, we save the result for
852 * We use a barrier here so that the call number and abort code can be
853 * read without needing to take a lock.
855 * TODO: Make the incoming packet handler check this and handle
856 * terminal retransmission without requiring access to the call.
858 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
859 _debug("exposed %u,%u", call->call_id, call->abort_code);
860 __rxrpc_disconnect_call(conn, call);
862 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
863 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
864 bundle->try_upgrade = false;
866 rxrpc_activate_channels_locked(bundle);
871 /* See if we can pass the channel directly to another call. */
872 if (may_reuse && !list_empty(&bundle->waiting_calls)) {
873 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
874 rxrpc_activate_one_channel(conn, channel);
878 /* Schedule the final ACK to be transmitted in a short while so that it
879 * can be skipped if we find a follow-on call. The first DATA packet
880 * of the follow on call will implicitly ACK this call.
882 if (call->completion == RXRPC_CALL_SUCCEEDED &&
883 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
884 unsigned long final_ack_at = jiffies + 2;
886 WRITE_ONCE(chan->final_ack_at, final_ack_at);
887 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
888 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
889 rxrpc_reduce_conn_timer(conn, final_ack_at);
892 /* Deactivate the channel. */
893 rcu_assign_pointer(chan->call, NULL);
894 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
895 conn->act_chans &= ~(1 << channel);
897 /* If no channels remain active, then put the connection on the idle
898 * list for a short while. Give it a ref to stop it going away if it
901 if (!conn->act_chans) {
902 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
903 conn->idle_timestamp = jiffies;
905 rxrpc_get_connection(conn, rxrpc_conn_get_idle);
906 spin_lock(&rxnet->client_conn_cache_lock);
907 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
908 spin_unlock(&rxnet->client_conn_cache_lock);
910 rxrpc_set_client_reap_timer(rxnet);
914 spin_unlock(&bundle->channel_lock);
920 * Remove a connection from a bundle.
922 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
924 struct rxrpc_bundle *bundle = conn->bundle;
926 bool need_drop = false;
929 _enter("C=%x", conn->debug_id);
931 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
932 rxrpc_process_delayed_final_acks(conn, true);
934 spin_lock(&bundle->channel_lock);
935 bindex = conn->bundle_shift / RXRPC_MAXCALLS;
936 if (bundle->conns[bindex] == conn) {
937 _debug("clear slot %u", bindex);
938 bundle->conns[bindex] = NULL;
939 for (i = 0; i < RXRPC_MAXCALLS; i++)
940 clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
943 spin_unlock(&bundle->channel_lock);
946 rxrpc_deactivate_bundle(bundle);
947 rxrpc_put_connection(conn, rxrpc_conn_put_unbundle);
952 * Drop the active count on a bundle.
954 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle)
956 struct rxrpc_local *local = bundle->local;
957 bool need_put = false;
959 if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) {
960 if (!bundle->exclusive) {
961 _debug("erase bundle");
962 rb_erase(&bundle->local_node, &local->client_bundles);
966 spin_unlock(&local->client_bundles_lock);
968 rxrpc_put_bundle(bundle, rxrpc_bundle_put_discard);
973 * Clean up a dead client connection.
975 void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
977 struct rxrpc_local *local = conn->local;
978 struct rxrpc_net *rxnet = local->rxnet;
980 _enter("C=%x", conn->debug_id);
982 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
983 atomic_dec(&rxnet->nr_client_conns);
985 rxrpc_put_client_connection_id(conn);
989 * Discard expired client connections from the idle list. Each conn in the
990 * idle list has been exposed and holds an extra ref because of that.
992 * This may be called from conn setup or from a work item so cannot be
993 * considered non-reentrant.
995 void rxrpc_discard_expired_client_conns(struct work_struct *work)
997 struct rxrpc_connection *conn;
998 struct rxrpc_net *rxnet =
999 container_of(work, struct rxrpc_net, client_conn_reaper);
1000 unsigned long expiry, conn_expires_at, now;
1001 unsigned int nr_conns;
1005 if (list_empty(&rxnet->idle_client_conns)) {
1010 /* Don't double up on the discarding */
1011 if (!mutex_trylock(&rxnet->client_conn_discard_lock)) {
1012 _leave(" [already]");
1016 /* We keep an estimate of what the number of conns ought to be after
1017 * we've discarded some so that we don't overdo the discarding.
1019 nr_conns = atomic_read(&rxnet->nr_client_conns);
1022 spin_lock(&rxnet->client_conn_cache_lock);
1024 if (list_empty(&rxnet->idle_client_conns))
1027 conn = list_entry(rxnet->idle_client_conns.next,
1028 struct rxrpc_connection, cache_link);
1030 if (!rxnet->kill_all_client_conns) {
1031 /* If the number of connections is over the reap limit, we
1032 * expedite discard by reducing the expiry timeout. We must,
1033 * however, have at least a short grace period to be able to do
1034 * final-ACK or ABORT retransmission.
1036 expiry = rxrpc_conn_idle_client_expiry;
1037 if (nr_conns > rxrpc_reap_client_connections)
1038 expiry = rxrpc_conn_idle_client_fast_expiry;
1039 if (conn->local->service_closed)
1040 expiry = rxrpc_closed_conn_expiry * HZ;
1042 conn_expires_at = conn->idle_timestamp + expiry;
1044 now = READ_ONCE(jiffies);
1045 if (time_after(conn_expires_at, now))
1046 goto not_yet_expired;
1049 atomic_dec(&conn->active);
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 /* Drop the ->cache_link ref */
1057 rxrpc_put_connection(conn, rxrpc_conn_put_discard_idle);
1063 /* The connection at the front of the queue hasn't yet expired, so
1064 * schedule the work item for that point if we discarded something.
1066 * We don't worry if the work item is already scheduled - it can look
1067 * after rescheduling itself at a later time. We could cancel it, but
1068 * then things get messier.
1071 if (!rxnet->kill_all_client_conns)
1072 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1075 spin_unlock(&rxnet->client_conn_cache_lock);
1076 mutex_unlock(&rxnet->client_conn_discard_lock);
1081 * Preemptively destroy all the client connection records rather than waiting
1082 * for them to time out
1084 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1088 spin_lock(&rxnet->client_conn_cache_lock);
1089 rxnet->kill_all_client_conns = true;
1090 spin_unlock(&rxnet->client_conn_cache_lock);
1092 del_timer_sync(&rxnet->client_conn_reap_timer);
1094 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1095 _debug("destroy: queue failed");
1101 * Clean up the client connections on a local endpoint.
1103 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1105 struct rxrpc_connection *conn, *tmp;
1106 struct rxrpc_net *rxnet = local->rxnet;
1107 LIST_HEAD(graveyard);
1111 spin_lock(&rxnet->client_conn_cache_lock);
1113 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1115 if (conn->local == local) {
1116 atomic_dec(&conn->active);
1117 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1118 list_move(&conn->cache_link, &graveyard);
1122 spin_unlock(&rxnet->client_conn_cache_lock);
1124 while (!list_empty(&graveyard)) {
1125 conn = list_entry(graveyard.next,
1126 struct rxrpc_connection, cache_link);
1127 list_del_init(&conn->cache_link);
1128 rxrpc_unbundle_conn(conn);
1129 rxrpc_put_connection(conn, rxrpc_conn_put_local_dead);
1132 _leave(" [culled]");