1 /* Client connection-specific management code.
3 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 * Client connections need to be cached for a little while after they've made a
13 * call so as to handle retransmitted DATA packets in case the server didn't
14 * receive the final ACK or terminating ABORT we sent it.
16 * Client connections can be in one of a number of cache states:
18 * (1) INACTIVE - The connection is not held in any list and may not have been
19 * exposed to the world. If it has been previously exposed, it was
20 * discarded from the idle list after expiring.
22 * (2) WAITING - The connection is waiting for the number of client conns to
23 * drop below the maximum capacity. Calls may be in progress upon it from
24 * when it was active and got culled.
26 * The connection is on the rxrpc_waiting_client_conns list which is kept
27 * in to-be-granted order. Culled conns with waiters go to the back of
28 * the queue just like new conns.
30 * (3) ACTIVE - The connection has at least one call in progress upon it, it
31 * may freely grant available channels to new calls and calls may be
32 * waiting on it for channels to become available.
34 * The connection is on the rxrpc_active_client_conns list which is kept
35 * in activation order for culling purposes.
37 * rxrpc_nr_active_client_conns is held incremented also.
39 * (4) CULLED - The connection got summarily culled to try and free up
40 * capacity. Calls currently in progress on the connection are allowed to
41 * continue, but new calls will have to wait. There can be no waiters in
42 * this state - the conn would have to go to the WAITING state instead.
44 * (5) IDLE - The connection has no calls in progress upon it and must have
45 * been exposed to the world (ie. the EXPOSED flag must be set). When it
46 * expires, the EXPOSED flag is cleared and the connection transitions to
49 * The connection is on the rxrpc_idle_client_conns list which is kept in
50 * order of how soon they'll expire.
52 * There are flags of relevance to the cache:
54 * (1) EXPOSED - The connection ID got exposed to the world. If this flag is
55 * set, an extra ref is added to the connection preventing it from being
56 * reaped when it has no calls outstanding. This flag is cleared and the
57 * ref dropped when a conn is discarded from the idle list.
59 * This allows us to move terminal call state retransmission to the
60 * connection and to discard the call immediately we think it is done
61 * with. It also give us a chance to reuse the connection.
63 * (2) DONT_REUSE - The connection should be discarded as soon as possible and
64 * should not be reused. This is set when an exclusive connection is used
65 * or a call ID counter overflows.
67 * The caching state may only be changed if the cache lock is held.
69 * There are two idle client connection expiry durations. If the total number
70 * of connections is below the reap threshold, we use the normal duration; if
71 * it's above, we use the fast duration.
74 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
76 #include <linux/slab.h>
77 #include <linux/idr.h>
78 #include <linux/timer.h>
79 #include "ar-internal.h"
81 __read_mostly unsigned int rxrpc_max_client_connections = 1000;
82 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
83 __read_mostly unsigned int rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
84 __read_mostly unsigned int rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
86 static unsigned int rxrpc_nr_client_conns;
87 static unsigned int rxrpc_nr_active_client_conns;
88 static __read_mostly bool rxrpc_kill_all_client_conns;
90 static DEFINE_SPINLOCK(rxrpc_client_conn_cache_lock);
91 static DEFINE_SPINLOCK(rxrpc_client_conn_discard_mutex);
92 static LIST_HEAD(rxrpc_waiting_client_conns);
93 static LIST_HEAD(rxrpc_active_client_conns);
94 static LIST_HEAD(rxrpc_idle_client_conns);
97 * We use machine-unique IDs for our client connections.
99 DEFINE_IDR(rxrpc_client_conn_ids);
100 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
102 static void rxrpc_cull_active_client_conns(void);
103 static void rxrpc_discard_expired_client_conns(struct work_struct *);
105 static DECLARE_DELAYED_WORK(rxrpc_client_conn_reap,
106 rxrpc_discard_expired_client_conns);
109 * Get a connection ID and epoch for a client connection from the global pool.
110 * The connection struct pointer is then recorded in the idr radix tree. The
111 * epoch doesn't change until the client is rebooted (or, at least, unless the
112 * module is unloaded).
114 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
122 spin_lock(&rxrpc_conn_id_lock);
124 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
125 1, 0x40000000, GFP_NOWAIT);
129 spin_unlock(&rxrpc_conn_id_lock);
132 conn->proto.epoch = rxrpc_epoch;
133 conn->proto.cid = id << RXRPC_CIDSHIFT;
134 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
135 _leave(" [CID %x]", conn->proto.cid);
139 spin_unlock(&rxrpc_conn_id_lock);
146 * Release a connection ID for a client connection from the global pool.
148 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
150 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
151 spin_lock(&rxrpc_conn_id_lock);
152 idr_remove(&rxrpc_client_conn_ids,
153 conn->proto.cid >> RXRPC_CIDSHIFT);
154 spin_unlock(&rxrpc_conn_id_lock);
159 * Destroy the client connection ID tree.
161 void rxrpc_destroy_client_conn_ids(void)
163 struct rxrpc_connection *conn;
166 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
167 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
168 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
169 conn, atomic_read(&conn->usage));
174 idr_destroy(&rxrpc_client_conn_ids);
178 * Allocate a client connection.
180 static struct rxrpc_connection *
181 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
183 struct rxrpc_connection *conn;
188 conn = rxrpc_alloc_connection(gfp);
190 _leave(" = -ENOMEM");
191 return ERR_PTR(-ENOMEM);
194 atomic_set(&conn->usage, 1);
195 if (conn->params.exclusive)
196 __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
199 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
200 conn->state = RXRPC_CONN_CLIENT;
202 ret = rxrpc_get_client_connection_id(conn, gfp);
206 ret = rxrpc_init_client_conn_security(conn);
210 ret = conn->security->prime_packet_security(conn);
214 write_lock(&rxrpc_connection_lock);
215 list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list);
216 write_unlock(&rxrpc_connection_lock);
218 /* We steal the caller's peer ref. */
220 rxrpc_get_local(conn->params.local);
221 key_get(conn->params.key);
223 _leave(" = %p", conn);
227 conn->security->clear(conn);
229 rxrpc_put_client_connection_id(conn);
232 _leave(" = %d", ret);
237 * Determine if a connection may be reused.
239 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
241 int id_cursor, id, distance, limit;
243 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
246 if (conn->proto.epoch != rxrpc_epoch)
247 goto mark_dont_reuse;
249 /* The IDR tree gets very expensive on memory if the connection IDs are
250 * widely scattered throughout the number space, so we shall want to
251 * kill off connections that, say, have an ID more than about four
252 * times the maximum number of client conns away from the current
253 * allocation point to try and keep the IDs concentrated.
255 id_cursor = READ_ONCE(rxrpc_client_conn_ids.cur);
256 id = conn->proto.cid >> RXRPC_CIDSHIFT;
257 distance = id - id_cursor;
259 distance = -distance;
260 limit = round_up(rxrpc_max_client_connections, IDR_SIZE) * 4;
261 if (distance > limit)
262 goto mark_dont_reuse;
267 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
273 * Create or find a client connection to use for a call.
275 * If we return with a connection, the call will be on its waiting list. It's
276 * left to the caller to assign a channel and wake up the call.
278 static int rxrpc_get_client_conn(struct rxrpc_call *call,
279 struct rxrpc_conn_parameters *cp,
280 struct sockaddr_rxrpc *srx,
283 struct rxrpc_connection *conn, *candidate = NULL;
284 struct rxrpc_local *local = cp->local;
285 struct rb_node *p, **pp, *parent;
289 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
291 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
295 /* If the connection is not meant to be exclusive, search the available
296 * connections to see if the connection we want to use already exists.
298 if (!cp->exclusive) {
300 spin_lock(&local->client_conns_lock);
301 p = local->client_conns.rb_node;
303 conn = rb_entry(p, struct rxrpc_connection, client_node);
305 #define cmp(X) ((long)conn->params.X - (long)cp->X)
308 cmp(security_level));
312 } else if (diff > 0) {
315 if (rxrpc_may_reuse_conn(conn) &&
316 rxrpc_get_connection_maybe(conn))
317 goto found_extant_conn;
318 /* The connection needs replacing. It's better
319 * to effect that when we have something to
320 * replace it with so that we don't have to
321 * rebalance the tree twice.
326 spin_unlock(&local->client_conns_lock);
329 /* There wasn't a connection yet or we need an exclusive connection.
330 * We need to create a candidate and then potentially redo the search
331 * in case we're racing with another thread also trying to connect on a
332 * shareable connection.
335 candidate = rxrpc_alloc_client_connection(cp, gfp);
336 if (IS_ERR(candidate)) {
337 ret = PTR_ERR(candidate);
341 /* Add the call to the new connection's waiting list in case we're
342 * going to have to wait for the connection to come live. It's our
343 * connection, so we want first dibs on the channel slots. We would
344 * normally have to take channel_lock but we do this before anyone else
345 * can see the connection.
347 list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
350 call->conn = candidate;
351 call->security_ix = candidate->security_ix;
352 _leave(" = 0 [exclusive %d]", candidate->debug_id);
356 /* Publish the new connection for userspace to find. We need to redo
357 * the search before doing this lest we race with someone else adding a
358 * conflicting instance.
361 spin_lock(&local->client_conns_lock);
363 pp = &local->client_conns.rb_node;
367 conn = rb_entry(parent, struct rxrpc_connection, client_node);
369 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
372 cmp(security_level));
375 pp = &(*pp)->rb_left;
376 } else if (diff > 0) {
377 pp = &(*pp)->rb_right;
379 if (rxrpc_may_reuse_conn(conn) &&
380 rxrpc_get_connection_maybe(conn))
381 goto found_extant_conn;
382 /* The old connection is from an outdated epoch. */
383 _debug("replace conn");
384 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
385 rb_replace_node(&conn->client_node,
386 &candidate->client_node,
387 &local->client_conns);
388 goto candidate_published;
393 rb_link_node(&candidate->client_node, parent, pp);
394 rb_insert_color(&candidate->client_node, &local->client_conns);
397 set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
398 call->conn = candidate;
399 call->security_ix = candidate->security_ix;
400 spin_unlock(&local->client_conns_lock);
401 _leave(" = 0 [new %d]", candidate->debug_id);
404 /* We come here if we found a suitable connection already in existence.
405 * Discard any candidate we may have allocated, and try to get a
406 * channel on this one.
409 _debug("found conn");
410 spin_unlock(&local->client_conns_lock);
412 rxrpc_put_connection(candidate);
415 spin_lock(&conn->channel_lock);
417 call->security_ix = conn->security_ix;
418 list_add(&call->chan_wait_link, &conn->waiting_calls);
419 spin_unlock(&conn->channel_lock);
420 _leave(" = 0 [extant %d]", conn->debug_id);
424 rxrpc_put_peer(cp->peer);
427 _leave(" = %d", ret);
432 * Activate a connection.
434 static void rxrpc_activate_conn(struct rxrpc_connection *conn)
436 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
437 rxrpc_nr_active_client_conns++;
438 list_move_tail(&conn->cache_link, &rxrpc_active_client_conns);
442 * Attempt to animate a connection for a new call.
444 * If it's not exclusive, the connection is in the endpoint tree, and we're in
445 * the conn's list of those waiting to grab a channel. There is, however, a
446 * limit on the number of live connections allowed at any one time, so we may
447 * have to wait for capacity to become available.
449 * Note that a connection on the waiting queue might *also* have active
450 * channels if it has been culled to make space and then re-requested by a new
453 static void rxrpc_animate_client_conn(struct rxrpc_connection *conn)
455 unsigned int nr_conns;
457 _enter("%d,%d", conn->debug_id, conn->cache_state);
459 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE)
462 spin_lock(&rxrpc_client_conn_cache_lock);
464 nr_conns = rxrpc_nr_client_conns;
465 if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags))
466 rxrpc_nr_client_conns = nr_conns + 1;
468 switch (conn->cache_state) {
469 case RXRPC_CONN_CLIENT_ACTIVE:
470 case RXRPC_CONN_CLIENT_WAITING:
473 case RXRPC_CONN_CLIENT_INACTIVE:
474 case RXRPC_CONN_CLIENT_CULLED:
475 case RXRPC_CONN_CLIENT_IDLE:
476 if (nr_conns >= rxrpc_max_client_connections)
477 goto wait_for_capacity;
485 spin_unlock(&rxrpc_client_conn_cache_lock);
487 _leave(" [%d]", conn->cache_state);
492 rxrpc_activate_conn(conn);
497 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
498 list_move_tail(&conn->cache_link, &rxrpc_waiting_client_conns);
503 * Deactivate a channel.
505 static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
506 unsigned int channel)
508 struct rxrpc_channel *chan = &conn->channels[channel];
510 rcu_assign_pointer(chan->call, NULL);
511 conn->active_chans &= ~(1 << channel);
515 * Assign a channel to the call at the front of the queue and wake the call up.
516 * We don't increment the callNumber counter until this number has been exposed
519 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
520 unsigned int channel)
522 struct rxrpc_channel *chan = &conn->channels[channel];
523 struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
524 struct rxrpc_call, chan_wait_link);
525 u32 call_id = chan->call_counter + 1;
527 write_lock_bh(&call->state_lock);
528 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
529 write_unlock_bh(&call->state_lock);
531 rxrpc_see_call(call);
532 list_del_init(&call->chan_wait_link);
533 conn->active_chans |= 1 << channel;
534 call->peer = rxrpc_get_peer(conn->params.peer);
535 call->cid = conn->proto.cid | channel;
536 call->call_id = call_id;
538 _net("CONNECT call %08x:%08x as call %d on conn %d",
539 call->cid, call->call_id, call->debug_id, conn->debug_id);
541 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
542 * orders cid and epoch in the connection wrt to call_id without the
543 * need to take the channel_lock.
545 * We provisionally assign a callNumber at this point, but we don't
546 * confirm it until the call is about to be exposed.
548 * TODO: Pair with a barrier in the data_ready handler when that looks
549 * at the call ID through a connection channel.
552 chan->call_id = call_id;
553 rcu_assign_pointer(chan->call, call);
554 wake_up(&call->waitq);
558 * Assign channels and callNumbers to waiting calls.
560 static void rxrpc_activate_channels(struct rxrpc_connection *conn)
564 _enter("%d", conn->debug_id);
566 if (conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE ||
567 conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
570 spin_lock(&conn->channel_lock);
572 while (!list_empty(&conn->waiting_calls) &&
573 (mask = ~conn->active_chans,
574 mask &= RXRPC_ACTIVE_CHANS_MASK,
576 rxrpc_activate_one_channel(conn, __ffs(mask));
578 spin_unlock(&conn->channel_lock);
583 * Wait for a callNumber and a channel to be granted to a call.
585 static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
589 _enter("%d", call->debug_id);
591 if (!call->call_id) {
592 DECLARE_WAITQUEUE(myself, current);
594 if (!gfpflags_allow_blocking(gfp)) {
599 add_wait_queue_exclusive(&call->waitq, &myself);
601 set_current_state(TASK_INTERRUPTIBLE);
604 if (signal_pending(current)) {
610 remove_wait_queue(&call->waitq, &myself);
611 __set_current_state(TASK_RUNNING);
614 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
618 _leave(" = %d", ret);
623 * find a connection for a call
624 * - called in process context with IRQs enabled
626 int rxrpc_connect_call(struct rxrpc_call *call,
627 struct rxrpc_conn_parameters *cp,
628 struct sockaddr_rxrpc *srx,
633 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
635 rxrpc_discard_expired_client_conns(NULL);
636 rxrpc_cull_active_client_conns();
638 ret = rxrpc_get_client_conn(call, cp, srx, gfp);
642 rxrpc_animate_client_conn(call->conn);
643 rxrpc_activate_channels(call->conn);
645 ret = rxrpc_wait_for_channel(call, gfp);
647 rxrpc_disconnect_client_call(call);
649 _leave(" = %d", ret);
654 * Note that a connection is about to be exposed to the world. Once it is
655 * exposed, we maintain an extra ref on it that stops it from being summarily
656 * discarded before it's (a) had a chance to deal with retransmission and (b)
657 * had a chance at re-use (the per-connection security negotiation is
660 static void rxrpc_expose_client_conn(struct rxrpc_connection *conn)
662 if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags))
663 rxrpc_get_connection(conn);
667 * Note that a call, and thus a connection, is about to be exposed to the
670 void rxrpc_expose_client_call(struct rxrpc_call *call)
672 struct rxrpc_connection *conn = call->conn;
673 struct rxrpc_channel *chan =
674 &conn->channels[call->cid & RXRPC_CHANNELMASK];
676 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
677 /* Mark the call ID as being used. If the callNumber counter
678 * exceeds ~2 billion, we kill the connection after its
679 * outstanding calls have finished so that the counter doesn't
682 chan->call_counter++;
683 if (chan->call_counter >= INT_MAX)
684 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
685 rxrpc_expose_client_conn(conn);
690 * Disconnect a client call.
692 void rxrpc_disconnect_client_call(struct rxrpc_call *call)
694 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
695 struct rxrpc_connection *conn = call->conn;
696 struct rxrpc_channel *chan = &conn->channels[channel];
700 spin_lock(&conn->channel_lock);
702 /* Calls that have never actually been assigned a channel can simply be
703 * discarded. If the conn didn't get used either, it will follow
704 * immediately unless someone else grabs it in the meantime.
706 if (!list_empty(&call->chan_wait_link)) {
707 _debug("call is waiting");
708 ASSERTCMP(call->call_id, ==, 0);
709 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
710 list_del_init(&call->chan_wait_link);
712 /* We must deactivate or idle the connection if it's now
713 * waiting for nothing.
715 spin_lock(&rxrpc_client_conn_cache_lock);
716 if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
717 list_empty(&conn->waiting_calls) &&
719 goto idle_connection;
723 ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
725 /* If a client call was exposed to the world, we save the result for
728 * We use a barrier here so that the call number and abort code can be
729 * read without needing to take a lock.
731 * TODO: Make the incoming packet handler check this and handle
732 * terminal retransmission without requiring access to the call.
734 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
735 _debug("exposed %u,%u", call->call_id, call->abort_code);
736 __rxrpc_disconnect_call(conn, call);
739 /* See if we can pass the channel directly to another call. */
740 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
741 !list_empty(&conn->waiting_calls)) {
743 rxrpc_activate_one_channel(conn, channel);
747 /* Things are more complex and we need the cache lock. We might be
748 * able to simply idle the conn or it might now be lurking on the wait
749 * list. It might even get moved back to the active list whilst we're
750 * waiting for the lock.
752 spin_lock(&rxrpc_client_conn_cache_lock);
754 switch (conn->cache_state) {
755 case RXRPC_CONN_CLIENT_ACTIVE:
756 if (list_empty(&conn->waiting_calls)) {
757 rxrpc_deactivate_one_channel(conn, channel);
758 if (!conn->active_chans) {
759 rxrpc_nr_active_client_conns--;
760 goto idle_connection;
765 _debug("pass chan 2");
766 rxrpc_activate_one_channel(conn, channel);
769 case RXRPC_CONN_CLIENT_CULLED:
770 rxrpc_deactivate_one_channel(conn, channel);
771 ASSERT(list_empty(&conn->waiting_calls));
772 if (!conn->active_chans)
773 goto idle_connection;
776 case RXRPC_CONN_CLIENT_WAITING:
777 rxrpc_deactivate_one_channel(conn, channel);
785 spin_unlock(&rxrpc_client_conn_cache_lock);
787 spin_unlock(&conn->channel_lock);
788 rxrpc_put_connection(conn);
793 /* As no channels remain active, the connection gets deactivated
794 * immediately or moved to the idle list for a short while.
796 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
798 conn->idle_timestamp = jiffies;
799 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
800 list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns);
801 if (rxrpc_idle_client_conns.next == &conn->cache_link &&
802 !rxrpc_kill_all_client_conns)
803 queue_delayed_work(rxrpc_workqueue,
804 &rxrpc_client_conn_reap,
805 rxrpc_conn_idle_client_expiry);
807 _debug("make inactive");
808 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
809 list_del_init(&conn->cache_link);
815 * Clean up a dead client connection.
817 static struct rxrpc_connection *
818 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
820 struct rxrpc_connection *next = NULL;
821 struct rxrpc_local *local = conn->params.local;
822 unsigned int nr_conns;
824 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
825 spin_lock(&local->client_conns_lock);
826 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
828 rb_erase(&conn->client_node, &local->client_conns);
829 spin_unlock(&local->client_conns_lock);
832 rxrpc_put_client_connection_id(conn);
834 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
836 if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
837 spin_lock(&rxrpc_client_conn_cache_lock);
838 nr_conns = --rxrpc_nr_client_conns;
840 if (nr_conns < rxrpc_max_client_connections &&
841 !list_empty(&rxrpc_waiting_client_conns)) {
842 next = list_entry(rxrpc_waiting_client_conns.next,
843 struct rxrpc_connection, cache_link);
844 rxrpc_get_connection(next);
845 rxrpc_activate_conn(next);
848 spin_unlock(&rxrpc_client_conn_cache_lock);
851 rxrpc_kill_connection(conn);
853 rxrpc_activate_channels(next);
855 /* We need to get rid of the temporary ref we took upon next, but we
856 * can't call rxrpc_put_connection() recursively.
862 * Clean up a dead client connections.
864 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
866 struct rxrpc_connection *next;
869 _enter("%p{u=%d,d=%d}",
870 conn, atomic_read(&conn->usage), conn->debug_id);
872 next = rxrpc_put_one_client_conn(conn);
877 } while (atomic_dec_and_test(&conn->usage));
883 * Kill the longest-active client connections to make room for new ones.
885 static void rxrpc_cull_active_client_conns(void)
887 struct rxrpc_connection *conn;
888 unsigned int nr_conns = rxrpc_nr_client_conns;
889 unsigned int nr_active, limit;
893 ASSERTCMP(nr_conns, >=, 0);
894 if (nr_conns < rxrpc_max_client_connections) {
898 limit = rxrpc_reap_client_connections;
900 spin_lock(&rxrpc_client_conn_cache_lock);
901 nr_active = rxrpc_nr_active_client_conns;
903 while (nr_active > limit) {
904 ASSERT(!list_empty(&rxrpc_active_client_conns));
905 conn = list_entry(rxrpc_active_client_conns.next,
906 struct rxrpc_connection, cache_link);
907 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
909 if (list_empty(&conn->waiting_calls)) {
910 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
911 list_del_init(&conn->cache_link);
913 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
914 list_move_tail(&conn->cache_link,
915 &rxrpc_waiting_client_conns);
921 rxrpc_nr_active_client_conns = nr_active;
922 spin_unlock(&rxrpc_client_conn_cache_lock);
923 ASSERTCMP(nr_active, >=, 0);
928 * Discard expired client connections from the idle list. Each conn in the
929 * idle list has been exposed and holds an extra ref because of that.
931 * This may be called from conn setup or from a work item so cannot be
932 * considered non-reentrant.
934 static void rxrpc_discard_expired_client_conns(struct work_struct *work)
936 struct rxrpc_connection *conn;
937 unsigned long expiry, conn_expires_at, now;
938 unsigned int nr_conns;
939 bool did_discard = false;
941 _enter("%c", work ? 'w' : 'n');
943 if (list_empty(&rxrpc_idle_client_conns)) {
948 /* Don't double up on the discarding */
949 if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) {
950 _leave(" [already]");
954 /* We keep an estimate of what the number of conns ought to be after
955 * we've discarded some so that we don't overdo the discarding.
957 nr_conns = rxrpc_nr_client_conns;
960 spin_lock(&rxrpc_client_conn_cache_lock);
962 if (list_empty(&rxrpc_idle_client_conns))
965 conn = list_entry(rxrpc_idle_client_conns.next,
966 struct rxrpc_connection, cache_link);
967 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
969 if (!rxrpc_kill_all_client_conns) {
970 /* If the number of connections is over the reap limit, we
971 * expedite discard by reducing the expiry timeout. We must,
972 * however, have at least a short grace period to be able to do
973 * final-ACK or ABORT retransmission.
975 expiry = rxrpc_conn_idle_client_expiry;
976 if (nr_conns > rxrpc_reap_client_connections)
977 expiry = rxrpc_conn_idle_client_fast_expiry;
979 conn_expires_at = conn->idle_timestamp + expiry;
981 now = READ_ONCE(jiffies);
982 if (time_after(conn_expires_at, now))
983 goto not_yet_expired;
986 _debug("discard conn %d", conn->debug_id);
987 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
989 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
990 list_del_init(&conn->cache_link);
992 spin_unlock(&rxrpc_client_conn_cache_lock);
994 /* When we cleared the EXPOSED flag, we took on responsibility for the
995 * reference that that had on the usage count. We deal with that here.
996 * If someone re-sets the flag and re-gets the ref, that's fine.
998 rxrpc_put_connection(conn);
1004 /* The connection at the front of the queue hasn't yet expired, so
1005 * schedule the work item for that point if we discarded something.
1007 * We don't worry if the work item is already scheduled - it can look
1008 * after rescheduling itself at a later time. We could cancel it, but
1009 * then things get messier.
1012 if (!rxrpc_kill_all_client_conns)
1013 queue_delayed_work(rxrpc_workqueue,
1014 &rxrpc_client_conn_reap,
1015 conn_expires_at - now);
1018 spin_unlock(&rxrpc_client_conn_cache_lock);
1019 spin_unlock(&rxrpc_client_conn_discard_mutex);
1024 * Preemptively destroy all the client connection records rather than waiting
1025 * for them to time out
1027 void __exit rxrpc_destroy_all_client_connections(void)
1031 spin_lock(&rxrpc_client_conn_cache_lock);
1032 rxrpc_kill_all_client_conns = true;
1033 spin_unlock(&rxrpc_client_conn_cache_lock);
1035 cancel_delayed_work(&rxrpc_client_conn_reap);
1037 if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0))
1038 _debug("destroy: queue failed");