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);
724 ASSERTCMP(atomic_read(&conn->usage), >=, 2);
726 /* If a client call was exposed to the world, we save the result for
729 * We use a barrier here so that the call number and abort code can be
730 * read without needing to take a lock.
732 * TODO: Make the incoming packet handler check this and handle
733 * terminal retransmission without requiring access to the call.
735 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
736 _debug("exposed %u,%u", call->call_id, call->abort_code);
737 __rxrpc_disconnect_call(conn, call);
740 /* See if we can pass the channel directly to another call. */
741 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
742 !list_empty(&conn->waiting_calls)) {
744 rxrpc_activate_one_channel(conn, channel);
748 /* Things are more complex and we need the cache lock. We might be
749 * able to simply idle the conn or it might now be lurking on the wait
750 * list. It might even get moved back to the active list whilst we're
751 * waiting for the lock.
753 spin_lock(&rxrpc_client_conn_cache_lock);
755 switch (conn->cache_state) {
756 case RXRPC_CONN_CLIENT_ACTIVE:
757 if (list_empty(&conn->waiting_calls)) {
758 rxrpc_deactivate_one_channel(conn, channel);
759 if (!conn->active_chans) {
760 rxrpc_nr_active_client_conns--;
761 goto idle_connection;
766 _debug("pass chan 2");
767 rxrpc_activate_one_channel(conn, channel);
770 case RXRPC_CONN_CLIENT_CULLED:
771 rxrpc_deactivate_one_channel(conn, channel);
772 ASSERT(list_empty(&conn->waiting_calls));
773 if (!conn->active_chans)
774 goto idle_connection;
777 case RXRPC_CONN_CLIENT_WAITING:
778 rxrpc_deactivate_one_channel(conn, channel);
786 spin_unlock(&rxrpc_client_conn_cache_lock);
788 spin_unlock(&conn->channel_lock);
789 rxrpc_put_connection(conn);
794 /* As no channels remain active, the connection gets deactivated
795 * immediately or moved to the idle list for a short while.
797 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
799 conn->idle_timestamp = jiffies;
800 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
801 list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns);
802 if (rxrpc_idle_client_conns.next == &conn->cache_link &&
803 !rxrpc_kill_all_client_conns)
804 queue_delayed_work(rxrpc_workqueue,
805 &rxrpc_client_conn_reap,
806 rxrpc_conn_idle_client_expiry);
808 _debug("make inactive");
809 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
810 list_del_init(&conn->cache_link);
816 * Clean up a dead client connection.
818 static struct rxrpc_connection *
819 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
821 struct rxrpc_connection *next;
822 struct rxrpc_local *local = conn->params.local;
823 unsigned int nr_conns;
825 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
826 spin_lock(&local->client_conns_lock);
827 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
829 rb_erase(&conn->client_node, &local->client_conns);
830 spin_unlock(&local->client_conns_lock);
833 rxrpc_put_client_connection_id(conn);
835 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
837 if (!test_bit(RXRPC_CONN_COUNTED, &conn->flags))
840 spin_lock(&rxrpc_client_conn_cache_lock);
841 nr_conns = --rxrpc_nr_client_conns;
844 if (nr_conns < rxrpc_max_client_connections &&
845 !list_empty(&rxrpc_waiting_client_conns)) {
846 next = list_entry(rxrpc_waiting_client_conns.next,
847 struct rxrpc_connection, cache_link);
848 rxrpc_get_connection(next);
849 rxrpc_activate_conn(next);
852 spin_unlock(&rxrpc_client_conn_cache_lock);
853 rxrpc_kill_connection(conn);
856 rxrpc_activate_channels(next);
858 /* We need to get rid of the temporary ref we took upon next, but we
859 * can't call rxrpc_put_connection() recursively.
865 * Clean up a dead client connections.
867 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
869 struct rxrpc_connection *next;
872 _enter("%p{u=%d,d=%d}",
873 conn, atomic_read(&conn->usage), conn->debug_id);
875 next = rxrpc_put_one_client_conn(conn);
880 } while (atomic_dec_and_test(&conn->usage));
886 * Kill the longest-active client connections to make room for new ones.
888 static void rxrpc_cull_active_client_conns(void)
890 struct rxrpc_connection *conn;
891 unsigned int nr_conns = rxrpc_nr_client_conns;
892 unsigned int nr_active, limit;
896 ASSERTCMP(nr_conns, >=, 0);
897 if (nr_conns < rxrpc_max_client_connections) {
901 limit = rxrpc_reap_client_connections;
903 spin_lock(&rxrpc_client_conn_cache_lock);
904 nr_active = rxrpc_nr_active_client_conns;
906 while (nr_active > limit) {
907 ASSERT(!list_empty(&rxrpc_active_client_conns));
908 conn = list_entry(rxrpc_active_client_conns.next,
909 struct rxrpc_connection, cache_link);
910 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
912 if (list_empty(&conn->waiting_calls)) {
913 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
914 list_del_init(&conn->cache_link);
916 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
917 list_move_tail(&conn->cache_link,
918 &rxrpc_waiting_client_conns);
924 rxrpc_nr_active_client_conns = nr_active;
925 spin_unlock(&rxrpc_client_conn_cache_lock);
926 ASSERTCMP(nr_active, >=, 0);
931 * Discard expired client connections from the idle list. Each conn in the
932 * idle list has been exposed and holds an extra ref because of that.
934 * This may be called from conn setup or from a work item so cannot be
935 * considered non-reentrant.
937 static void rxrpc_discard_expired_client_conns(struct work_struct *work)
939 struct rxrpc_connection *conn;
940 unsigned long expiry, conn_expires_at, now;
941 unsigned int nr_conns;
942 bool did_discard = false;
944 _enter("%c", work ? 'w' : 'n');
946 if (list_empty(&rxrpc_idle_client_conns)) {
951 /* Don't double up on the discarding */
952 if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) {
953 _leave(" [already]");
957 /* We keep an estimate of what the number of conns ought to be after
958 * we've discarded some so that we don't overdo the discarding.
960 nr_conns = rxrpc_nr_client_conns;
963 spin_lock(&rxrpc_client_conn_cache_lock);
965 if (list_empty(&rxrpc_idle_client_conns))
968 conn = list_entry(rxrpc_idle_client_conns.next,
969 struct rxrpc_connection, cache_link);
970 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
972 if (!rxrpc_kill_all_client_conns) {
973 /* If the number of connections is over the reap limit, we
974 * expedite discard by reducing the expiry timeout. We must,
975 * however, have at least a short grace period to be able to do
976 * final-ACK or ABORT retransmission.
978 expiry = rxrpc_conn_idle_client_expiry;
979 if (nr_conns > rxrpc_reap_client_connections)
980 expiry = rxrpc_conn_idle_client_fast_expiry;
982 conn_expires_at = conn->idle_timestamp + expiry;
984 now = READ_ONCE(jiffies);
985 if (time_after(conn_expires_at, now))
986 goto not_yet_expired;
989 _debug("discard conn %d", conn->debug_id);
990 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
992 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
993 list_del_init(&conn->cache_link);
995 spin_unlock(&rxrpc_client_conn_cache_lock);
997 /* When we cleared the EXPOSED flag, we took on responsibility for the
998 * reference that that had on the usage count. We deal with that here.
999 * If someone re-sets the flag and re-gets the ref, that's fine.
1001 rxrpc_put_connection(conn);
1007 /* The connection at the front of the queue hasn't yet expired, so
1008 * schedule the work item for that point if we discarded something.
1010 * We don't worry if the work item is already scheduled - it can look
1011 * after rescheduling itself at a later time. We could cancel it, but
1012 * then things get messier.
1015 if (!rxrpc_kill_all_client_conns)
1016 queue_delayed_work(rxrpc_workqueue,
1017 &rxrpc_client_conn_reap,
1018 conn_expires_at - now);
1021 spin_unlock(&rxrpc_client_conn_cache_lock);
1022 spin_unlock(&rxrpc_client_conn_discard_mutex);
1027 * Preemptively destroy all the client connection records rather than waiting
1028 * for them to time out
1030 void __exit rxrpc_destroy_all_client_connections(void)
1034 spin_lock(&rxrpc_client_conn_cache_lock);
1035 rxrpc_kill_all_client_conns = true;
1036 spin_unlock(&rxrpc_client_conn_cache_lock);
1038 cancel_delayed_work(&rxrpc_client_conn_reap);
1040 if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0))
1041 _debug("destroy: queue failed");