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;
37 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle);
40 * Get a connection ID and epoch for a client connection from the global pool.
41 * The connection struct pointer is then recorded in the idr radix tree. The
42 * epoch doesn't change until the client is rebooted (or, at least, unless the
43 * module is unloaded).
45 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
48 struct rxrpc_local *local = conn->local;
54 spin_lock(&local->conn_lock);
56 id = idr_alloc_cyclic(&local->conn_ids, conn,
57 1, 0x40000000, GFP_NOWAIT);
61 spin_unlock(&local->conn_lock);
64 conn->proto.epoch = local->rxnet->epoch;
65 conn->proto.cid = id << RXRPC_CIDSHIFT;
66 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
67 _leave(" [CID %x]", conn->proto.cid);
71 spin_unlock(&local->conn_lock);
78 * Release a connection ID for a client connection.
80 static void rxrpc_put_client_connection_id(struct rxrpc_local *local,
81 struct rxrpc_connection *conn)
83 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
84 spin_lock(&local->conn_lock);
85 idr_remove(&local->conn_ids, conn->proto.cid >> RXRPC_CIDSHIFT);
86 spin_unlock(&local->conn_lock);
91 * Destroy the client connection ID tree.
93 void rxrpc_destroy_client_conn_ids(struct rxrpc_local *local)
95 struct rxrpc_connection *conn;
98 if (!idr_is_empty(&local->conn_ids)) {
99 idr_for_each_entry(&local->conn_ids, conn, id) {
100 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
101 conn, refcount_read(&conn->ref));
106 idr_destroy(&local->conn_ids);
110 * Allocate a connection bundle.
112 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp,
115 struct rxrpc_bundle *bundle;
117 bundle = kzalloc(sizeof(*bundle), gfp);
119 bundle->local = cp->local;
120 bundle->peer = rxrpc_get_peer(cp->peer, rxrpc_peer_get_bundle);
121 bundle->key = cp->key;
122 bundle->exclusive = cp->exclusive;
123 bundle->upgrade = cp->upgrade;
124 bundle->service_id = cp->service_id;
125 bundle->security_level = cp->security_level;
126 refcount_set(&bundle->ref, 1);
127 atomic_set(&bundle->active, 1);
128 spin_lock_init(&bundle->channel_lock);
129 INIT_LIST_HEAD(&bundle->waiting_calls);
130 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new);
135 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle,
136 enum rxrpc_bundle_trace why)
140 __refcount_inc(&bundle->ref, &r);
141 trace_rxrpc_bundle(bundle->debug_id, r + 1, why);
145 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
147 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_free);
148 rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle);
152 void rxrpc_put_bundle(struct rxrpc_bundle *bundle, enum rxrpc_bundle_trace why)
154 unsigned int id = bundle->debug_id;
158 dead = __refcount_dec_and_test(&bundle->ref, &r);
159 trace_rxrpc_bundle(id, r - 1, why);
161 rxrpc_free_bundle(bundle);
165 * Allocate a client connection.
167 static struct rxrpc_connection *
168 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
170 struct rxrpc_connection *conn;
171 struct rxrpc_net *rxnet = bundle->local->rxnet;
176 conn = rxrpc_alloc_connection(rxnet, gfp);
178 _leave(" = -ENOMEM");
179 return ERR_PTR(-ENOMEM);
182 refcount_set(&conn->ref, 1);
183 conn->bundle = bundle;
184 conn->local = bundle->local;
185 conn->peer = bundle->peer;
186 conn->key = bundle->key;
187 conn->exclusive = bundle->exclusive;
188 conn->upgrade = bundle->upgrade;
189 conn->orig_service_id = bundle->service_id;
190 conn->security_level = bundle->security_level;
191 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
192 conn->state = RXRPC_CONN_CLIENT;
193 conn->service_id = conn->orig_service_id;
195 ret = rxrpc_get_client_connection_id(conn, gfp);
199 ret = rxrpc_init_client_conn_security(conn);
203 atomic_inc(&rxnet->nr_conns);
204 write_lock(&rxnet->conn_lock);
205 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
206 write_unlock(&rxnet->conn_lock);
208 rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn);
209 rxrpc_get_peer(conn->peer, rxrpc_peer_get_client_conn);
210 rxrpc_get_local(conn->local, rxrpc_local_get_client_conn);
213 trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
214 rxrpc_conn_new_client);
216 atomic_inc(&rxnet->nr_client_conns);
217 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
218 _leave(" = %p", conn);
222 rxrpc_put_client_connection_id(bundle->local, conn);
225 _leave(" = %d", ret);
230 * Determine if a connection may be reused.
232 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
234 struct rxrpc_net *rxnet;
235 int id_cursor, id, distance, limit;
241 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
244 if (conn->state != RXRPC_CONN_CLIENT ||
245 conn->proto.epoch != rxnet->epoch)
246 goto mark_dont_reuse;
248 /* The IDR tree gets very expensive on memory if the connection IDs are
249 * widely scattered throughout the number space, so we shall want to
250 * kill off connections that, say, have an ID more than about four
251 * times the maximum number of client conns away from the current
252 * allocation point to try and keep the IDs concentrated.
254 id_cursor = idr_get_cursor(&conn->local->conn_ids);
255 id = conn->proto.cid >> RXRPC_CIDSHIFT;
256 distance = id - id_cursor;
258 distance = -distance;
259 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
260 if (distance > limit)
261 goto mark_dont_reuse;
266 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
272 * Look up the conn bundle that matches the connection parameters, adding it if
273 * it doesn't yet exist.
275 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
278 static atomic_t rxrpc_bundle_id;
279 struct rxrpc_bundle *bundle, *candidate;
280 struct rxrpc_local *local = cp->local;
281 struct rb_node *p, **pp, *parent;
284 _enter("{%px,%x,%u,%u}",
285 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
288 return rxrpc_alloc_bundle(cp, gfp);
290 /* First, see if the bundle is already there. */
292 spin_lock(&local->client_bundles_lock);
293 p = local->client_bundles.rb_node;
295 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
297 #define cmp(X) ((long)bundle->X - (long)cp->X)
300 cmp(security_level) ?:
310 spin_unlock(&local->client_bundles_lock);
313 /* It wasn't. We need to add one. */
314 candidate = rxrpc_alloc_bundle(cp, gfp);
319 spin_lock(&local->client_bundles_lock);
320 pp = &local->client_bundles.rb_node;
324 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
326 #define cmp(X) ((long)bundle->X - (long)cp->X)
329 cmp(security_level) ?:
333 pp = &(*pp)->rb_left;
335 pp = &(*pp)->rb_right;
337 goto found_bundle_free;
340 _debug("new bundle");
341 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
342 rb_link_node(&candidate->local_node, parent, pp);
343 rb_insert_color(&candidate->local_node, &local->client_bundles);
344 rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call);
345 spin_unlock(&local->client_bundles_lock);
346 _leave(" = %u [new]", candidate->debug_id);
350 rxrpc_free_bundle(candidate);
352 rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call);
353 atomic_inc(&bundle->active);
354 spin_unlock(&local->client_bundles_lock);
355 _leave(" = %u [found]", bundle->debug_id);
360 * Create or find a client bundle to use for a call.
362 * If we return with a connection, the call will be on its waiting list. It's
363 * left to the caller to assign a channel and wake up the call.
365 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
366 struct rxrpc_call *call,
367 struct rxrpc_conn_parameters *cp,
368 struct sockaddr_rxrpc *srx,
371 struct rxrpc_bundle *bundle;
373 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
375 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
379 call->tx_last_sent = ktime_get_real();
380 call->cong_ssthresh = cp->peer->cong_ssthresh;
381 if (call->cong_cwnd >= call->cong_ssthresh)
382 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
384 call->cong_mode = RXRPC_CALL_SLOW_START;
386 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
388 /* Find the client connection bundle. */
389 bundle = rxrpc_look_up_bundle(cp, gfp);
393 /* Get this call queued. Someone else may activate it whilst we're
394 * lining up a new connection, but that's fine.
396 spin_lock(&bundle->channel_lock);
397 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
398 spin_unlock(&bundle->channel_lock);
400 _leave(" = [B=%x]", bundle->debug_id);
404 _leave(" = -ENOMEM");
405 return ERR_PTR(-ENOMEM);
409 * Allocate a new connection and add it into a bundle.
411 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
412 __releases(bundle->channel_lock)
414 struct rxrpc_connection *candidate = NULL, *old = NULL;
420 conflict = bundle->alloc_conn;
422 bundle->alloc_conn = true;
423 spin_unlock(&bundle->channel_lock);
429 candidate = rxrpc_alloc_client_connection(bundle, gfp);
431 spin_lock(&bundle->channel_lock);
432 bundle->alloc_conn = false;
434 if (IS_ERR(candidate)) {
435 bundle->alloc_error = PTR_ERR(candidate);
436 spin_unlock(&bundle->channel_lock);
437 _leave(" [err %ld]", PTR_ERR(candidate));
441 bundle->alloc_error = 0;
443 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
444 unsigned int shift = i * RXRPC_MAXCALLS;
447 old = bundle->conns[i];
448 if (!rxrpc_may_reuse_conn(old)) {
450 trace_rxrpc_client(old, -1, rxrpc_client_replace);
451 candidate->bundle_shift = shift;
452 atomic_inc(&bundle->active);
453 bundle->conns[i] = candidate;
454 for (j = 0; j < RXRPC_MAXCALLS; j++)
455 set_bit(shift + j, &bundle->avail_chans);
463 spin_unlock(&bundle->channel_lock);
466 _debug("discard C=%x", candidate->debug_id);
467 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
468 rxrpc_put_connection(candidate, rxrpc_conn_put_discard);
471 rxrpc_put_connection(old, rxrpc_conn_put_noreuse);
476 * Add a connection to a bundle if there are no usable connections or we have
477 * connections waiting for extra capacity.
479 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
481 struct rxrpc_call *call;
486 spin_lock(&bundle->channel_lock);
488 /* See if there are any usable connections. */
490 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
491 if (rxrpc_may_reuse_conn(bundle->conns[i]))
494 if (!usable && !list_empty(&bundle->waiting_calls)) {
495 call = list_first_entry(&bundle->waiting_calls,
496 struct rxrpc_call, chan_wait_link);
497 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
498 bundle->try_upgrade = true;
504 if (!bundle->avail_chans &&
505 !bundle->try_upgrade &&
506 !list_empty(&bundle->waiting_calls) &&
507 usable < ARRAY_SIZE(bundle->conns))
510 spin_unlock(&bundle->channel_lock);
515 return rxrpc_add_conn_to_bundle(bundle, gfp);
519 * Assign a channel to the call at the front of the queue and wake the call up.
520 * We don't increment the callNumber counter until this number has been exposed
523 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
524 unsigned int channel)
526 struct rxrpc_channel *chan = &conn->channels[channel];
527 struct rxrpc_bundle *bundle = conn->bundle;
528 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
529 struct rxrpc_call, chan_wait_link);
530 u32 call_id = chan->call_counter + 1;
532 _enter("C=%x,%u", conn->debug_id, channel);
534 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
536 /* Cancel the final ACK on the previous call if it hasn't been sent yet
537 * as the DATA packet will implicitly ACK it.
539 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
540 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
542 rxrpc_see_call(call, rxrpc_call_see_activate_client);
543 list_del_init(&call->chan_wait_link);
544 call->peer = rxrpc_get_peer(conn->peer, rxrpc_peer_get_activate_call);
545 call->conn = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call);
546 call->cid = conn->proto.cid | channel;
547 call->call_id = call_id;
548 call->dest_srx.srx_service = conn->service_id;
550 trace_rxrpc_connect_call(call);
552 write_lock(&call->state_lock);
553 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
554 write_unlock(&call->state_lock);
556 /* Paired with the read barrier in rxrpc_connect_call(). This orders
557 * cid and epoch in the connection wrt to call_id without the need to
558 * take the channel_lock.
560 * We provisionally assign a callNumber at this point, but we don't
561 * confirm it until the call is about to be exposed.
563 * TODO: Pair with a barrier in the data_ready handler when that looks
564 * at the call ID through a connection channel.
568 chan->call_id = call_id;
569 chan->call_debug_id = call->debug_id;
570 rcu_assign_pointer(chan->call, call);
571 wake_up(&call->waitq);
575 * Remove a connection from the idle list if it's on it.
577 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
579 struct rxrpc_net *rxnet = bundle->local->rxnet;
582 if (!list_empty(&conn->cache_link)) {
584 spin_lock(&rxnet->client_conn_cache_lock);
585 if (!list_empty(&conn->cache_link)) {
586 list_del_init(&conn->cache_link);
589 spin_unlock(&rxnet->client_conn_cache_lock);
591 rxrpc_put_connection(conn, rxrpc_conn_put_unidle);
596 * Assign channels and callNumbers to waiting calls with channel_lock
599 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
601 struct rxrpc_connection *conn;
602 unsigned long avail, mask;
603 unsigned int channel, slot;
605 if (bundle->try_upgrade)
610 while (!list_empty(&bundle->waiting_calls)) {
611 avail = bundle->avail_chans & mask;
614 channel = __ffs(avail);
615 clear_bit(channel, &bundle->avail_chans);
617 slot = channel / RXRPC_MAXCALLS;
618 conn = bundle->conns[slot];
622 if (bundle->try_upgrade)
623 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
624 rxrpc_unidle_conn(bundle, conn);
626 channel &= (RXRPC_MAXCALLS - 1);
627 conn->act_chans |= 1 << channel;
628 rxrpc_activate_one_channel(conn, channel);
633 * Assign channels and callNumbers to waiting calls.
635 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
637 _enter("B=%x", bundle->debug_id);
639 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
641 if (!bundle->avail_chans)
644 spin_lock(&bundle->channel_lock);
645 rxrpc_activate_channels_locked(bundle);
646 spin_unlock(&bundle->channel_lock);
651 * Wait for a callNumber and a channel to be granted to a call.
653 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
654 struct rxrpc_call *call, gfp_t gfp)
656 DECLARE_WAITQUEUE(myself, current);
659 _enter("%d", call->debug_id);
661 if (!gfpflags_allow_blocking(gfp)) {
662 rxrpc_maybe_add_conn(bundle, gfp);
663 rxrpc_activate_channels(bundle);
664 ret = bundle->alloc_error ?: -EAGAIN;
668 add_wait_queue_exclusive(&call->waitq, &myself);
670 rxrpc_maybe_add_conn(bundle, gfp);
671 rxrpc_activate_channels(bundle);
672 ret = bundle->alloc_error;
676 switch (call->interruptibility) {
677 case RXRPC_INTERRUPTIBLE:
678 case RXRPC_PREINTERRUPTIBLE:
679 set_current_state(TASK_INTERRUPTIBLE);
681 case RXRPC_UNINTERRUPTIBLE:
683 set_current_state(TASK_UNINTERRUPTIBLE);
686 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
688 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
689 call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
690 signal_pending(current)) {
696 remove_wait_queue(&call->waitq, &myself);
697 __set_current_state(TASK_RUNNING);
700 _leave(" = %d", ret);
705 * find a connection for a call
706 * - called in process context with IRQs enabled
708 int rxrpc_connect_call(struct rxrpc_sock *rx,
709 struct rxrpc_call *call,
710 struct rxrpc_conn_parameters *cp,
711 struct sockaddr_rxrpc *srx,
714 struct rxrpc_bundle *bundle;
715 struct rxrpc_net *rxnet = cp->local->rxnet;
718 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
720 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
722 rxrpc_get_call(call, rxrpc_call_get_io_thread);
724 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
725 if (IS_ERR(bundle)) {
726 rxrpc_put_call(call, rxrpc_call_get_io_thread);
727 ret = PTR_ERR(bundle);
731 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
732 ret = rxrpc_wait_for_channel(bundle, call, gfp);
738 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
742 rxrpc_deactivate_bundle(bundle);
743 rxrpc_put_bundle(bundle, rxrpc_bundle_get_client_call);
745 _leave(" = %d", ret);
749 spin_lock(&bundle->channel_lock);
750 list_del_init(&call->chan_wait_link);
751 spin_unlock(&bundle->channel_lock);
753 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
755 goto granted_channel;
758 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
759 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
760 rxrpc_disconnect_client_call(bundle, call);
765 * Note that a call, and thus a connection, is about to be exposed to the
768 void rxrpc_expose_client_call(struct rxrpc_call *call)
770 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
771 struct rxrpc_connection *conn = call->conn;
772 struct rxrpc_channel *chan = &conn->channels[channel];
774 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
775 /* Mark the call ID as being used. If the callNumber counter
776 * exceeds ~2 billion, we kill the connection after its
777 * outstanding calls have finished so that the counter doesn't
780 chan->call_counter++;
781 if (chan->call_counter >= INT_MAX)
782 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
783 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
785 spin_lock(&call->peer->lock);
786 hlist_add_head(&call->error_link, &call->peer->error_targets);
787 spin_unlock(&call->peer->lock);
792 * Set the reap timer.
794 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
796 if (!rxnet->kill_all_client_conns) {
797 unsigned long now = jiffies;
798 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
801 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
806 * Disconnect a client call.
808 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
810 struct rxrpc_connection *conn;
811 struct rxrpc_channel *chan = NULL;
812 struct rxrpc_net *rxnet = bundle->local->rxnet;
813 unsigned int channel;
817 _enter("c=%x", call->debug_id);
819 spin_lock(&bundle->channel_lock);
821 /* Calls that have never actually been assigned a channel can simply be
826 _debug("call is waiting");
827 ASSERTCMP(call->call_id, ==, 0);
828 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
829 list_del_init(&call->chan_wait_link);
834 channel = cid & RXRPC_CHANNELMASK;
835 chan = &conn->channels[channel];
836 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
838 if (rcu_access_pointer(chan->call) != call) {
839 spin_unlock(&bundle->channel_lock);
843 may_reuse = rxrpc_may_reuse_conn(conn);
845 /* If a client call was exposed to the world, we save the result for
848 * We use a barrier here so that the call number and abort code can be
849 * read without needing to take a lock.
851 * TODO: Make the incoming packet handler check this and handle
852 * terminal retransmission without requiring access to the call.
854 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
855 _debug("exposed %u,%u", call->call_id, call->abort_code);
856 __rxrpc_disconnect_call(conn, call);
858 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
859 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
860 bundle->try_upgrade = false;
862 rxrpc_activate_channels_locked(bundle);
867 /* See if we can pass the channel directly to another call. */
868 if (may_reuse && !list_empty(&bundle->waiting_calls)) {
869 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
870 rxrpc_activate_one_channel(conn, channel);
874 /* Schedule the final ACK to be transmitted in a short while so that it
875 * can be skipped if we find a follow-on call. The first DATA packet
876 * of the follow on call will implicitly ACK this call.
878 if (call->completion == RXRPC_CALL_SUCCEEDED &&
879 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
880 unsigned long final_ack_at = jiffies + 2;
882 WRITE_ONCE(chan->final_ack_at, final_ack_at);
883 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
884 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
885 rxrpc_reduce_conn_timer(conn, final_ack_at);
888 /* Deactivate the channel. */
889 rcu_assign_pointer(chan->call, NULL);
890 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
891 conn->act_chans &= ~(1 << channel);
893 /* If no channels remain active, then put the connection on the idle
894 * list for a short while. Give it a ref to stop it going away if it
897 if (!conn->act_chans) {
898 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
899 conn->idle_timestamp = jiffies;
901 rxrpc_get_connection(conn, rxrpc_conn_get_idle);
902 spin_lock(&rxnet->client_conn_cache_lock);
903 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
904 spin_unlock(&rxnet->client_conn_cache_lock);
906 rxrpc_set_client_reap_timer(rxnet);
910 spin_unlock(&bundle->channel_lock);
914 * Remove a connection from a bundle.
916 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
918 struct rxrpc_bundle *bundle = conn->bundle;
920 bool need_drop = false;
923 _enter("C=%x", conn->debug_id);
925 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
926 rxrpc_process_delayed_final_acks(conn, true);
928 spin_lock(&bundle->channel_lock);
929 bindex = conn->bundle_shift / RXRPC_MAXCALLS;
930 if (bundle->conns[bindex] == conn) {
931 _debug("clear slot %u", bindex);
932 bundle->conns[bindex] = NULL;
933 for (i = 0; i < RXRPC_MAXCALLS; i++)
934 clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
937 spin_unlock(&bundle->channel_lock);
940 rxrpc_deactivate_bundle(bundle);
941 rxrpc_put_connection(conn, rxrpc_conn_put_unbundle);
946 * Drop the active count on a bundle.
948 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle)
950 struct rxrpc_local *local = bundle->local;
951 bool need_put = false;
953 if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) {
954 if (!bundle->exclusive) {
955 _debug("erase bundle");
956 rb_erase(&bundle->local_node, &local->client_bundles);
960 spin_unlock(&local->client_bundles_lock);
962 rxrpc_put_bundle(bundle, rxrpc_bundle_put_discard);
967 * Clean up a dead client connection.
969 void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
971 struct rxrpc_local *local = conn->local;
972 struct rxrpc_net *rxnet = local->rxnet;
974 _enter("C=%x", conn->debug_id);
976 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
977 atomic_dec(&rxnet->nr_client_conns);
979 rxrpc_put_client_connection_id(local, conn);
983 * Discard expired client connections from the idle list. Each conn in the
984 * idle list has been exposed and holds an extra ref because of that.
986 * This may be called from conn setup or from a work item so cannot be
987 * considered non-reentrant.
989 void rxrpc_discard_expired_client_conns(struct work_struct *work)
991 struct rxrpc_connection *conn;
992 struct rxrpc_net *rxnet =
993 container_of(work, struct rxrpc_net, client_conn_reaper);
994 unsigned long expiry, conn_expires_at, now;
995 unsigned int nr_conns;
999 if (list_empty(&rxnet->idle_client_conns)) {
1004 /* Don't double up on the discarding */
1005 if (!mutex_trylock(&rxnet->client_conn_discard_lock)) {
1006 _leave(" [already]");
1010 /* We keep an estimate of what the number of conns ought to be after
1011 * we've discarded some so that we don't overdo the discarding.
1013 nr_conns = atomic_read(&rxnet->nr_client_conns);
1016 spin_lock(&rxnet->client_conn_cache_lock);
1018 if (list_empty(&rxnet->idle_client_conns))
1021 conn = list_entry(rxnet->idle_client_conns.next,
1022 struct rxrpc_connection, cache_link);
1024 if (!rxnet->kill_all_client_conns) {
1025 /* If the number of connections is over the reap limit, we
1026 * expedite discard by reducing the expiry timeout. We must,
1027 * however, have at least a short grace period to be able to do
1028 * final-ACK or ABORT retransmission.
1030 expiry = rxrpc_conn_idle_client_expiry;
1031 if (nr_conns > rxrpc_reap_client_connections)
1032 expiry = rxrpc_conn_idle_client_fast_expiry;
1033 if (conn->local->service_closed)
1034 expiry = rxrpc_closed_conn_expiry * HZ;
1036 conn_expires_at = conn->idle_timestamp + expiry;
1038 now = READ_ONCE(jiffies);
1039 if (time_after(conn_expires_at, now))
1040 goto not_yet_expired;
1043 atomic_dec(&conn->active);
1044 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1045 list_del_init(&conn->cache_link);
1047 spin_unlock(&rxnet->client_conn_cache_lock);
1049 rxrpc_unbundle_conn(conn);
1050 /* Drop the ->cache_link ref */
1051 rxrpc_put_connection(conn, rxrpc_conn_put_discard_idle);
1057 /* The connection at the front of the queue hasn't yet expired, so
1058 * schedule the work item for that point if we discarded something.
1060 * We don't worry if the work item is already scheduled - it can look
1061 * after rescheduling itself at a later time. We could cancel it, but
1062 * then things get messier.
1065 if (!rxnet->kill_all_client_conns)
1066 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1069 spin_unlock(&rxnet->client_conn_cache_lock);
1070 mutex_unlock(&rxnet->client_conn_discard_lock);
1075 * Preemptively destroy all the client connection records rather than waiting
1076 * for them to time out
1078 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1082 spin_lock(&rxnet->client_conn_cache_lock);
1083 rxnet->kill_all_client_conns = true;
1084 spin_unlock(&rxnet->client_conn_cache_lock);
1086 del_timer_sync(&rxnet->client_conn_reap_timer);
1088 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1089 _debug("destroy: queue failed");
1095 * Clean up the client connections on a local endpoint.
1097 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1099 struct rxrpc_connection *conn, *tmp;
1100 struct rxrpc_net *rxnet = local->rxnet;
1101 LIST_HEAD(graveyard);
1105 spin_lock(&rxnet->client_conn_cache_lock);
1107 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1109 if (conn->local == local) {
1110 atomic_dec(&conn->active);
1111 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1112 list_move(&conn->cache_link, &graveyard);
1116 spin_unlock(&rxnet->client_conn_cache_lock);
1118 while (!list_empty(&graveyard)) {
1119 conn = list_entry(graveyard.next,
1120 struct rxrpc_connection, cache_link);
1121 list_del_init(&conn->cache_link);
1122 rxrpc_unbundle_conn(conn);
1123 rxrpc_put_connection(conn, rxrpc_conn_put_local_dead);
1126 _leave(" [culled]");