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 is changed if this wraps.
113 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
122 spin_lock(&rxrpc_conn_id_lock);
126 /* We could use idr_alloc_cyclic() here, but we really need to know
127 * when the thing wraps so that we can advance the epoch.
129 if (rxrpc_client_conn_ids.cur == 0)
130 rxrpc_client_conn_ids.cur = 1;
131 id = idr_alloc(&rxrpc_client_conn_ids, conn,
132 rxrpc_client_conn_ids.cur, 0x40000000, GFP_NOWAIT);
136 id = idr_alloc(&rxrpc_client_conn_ids, conn,
137 1, 0x40000000, GFP_NOWAIT);
143 rxrpc_client_conn_ids.cur = id + 1;
145 spin_unlock(&rxrpc_conn_id_lock);
148 conn->proto.epoch = epoch;
149 conn->proto.cid = id << RXRPC_CIDSHIFT;
150 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
151 _leave(" [CID %x:%x]", epoch, conn->proto.cid);
155 spin_unlock(&rxrpc_conn_id_lock);
162 * Release a connection ID for a client connection from the global pool.
164 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
166 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
167 spin_lock(&rxrpc_conn_id_lock);
168 idr_remove(&rxrpc_client_conn_ids,
169 conn->proto.cid >> RXRPC_CIDSHIFT);
170 spin_unlock(&rxrpc_conn_id_lock);
175 * Destroy the client connection ID tree.
177 void rxrpc_destroy_client_conn_ids(void)
179 struct rxrpc_connection *conn;
182 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
183 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
184 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
185 conn, atomic_read(&conn->usage));
190 idr_destroy(&rxrpc_client_conn_ids);
194 * Allocate a client connection.
196 static struct rxrpc_connection *
197 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
199 struct rxrpc_connection *conn;
204 conn = rxrpc_alloc_connection(gfp);
206 _leave(" = -ENOMEM");
207 return ERR_PTR(-ENOMEM);
210 atomic_set(&conn->usage, 1);
211 if (conn->params.exclusive)
212 __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
215 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
216 conn->state = RXRPC_CONN_CLIENT;
218 ret = rxrpc_get_client_connection_id(conn, gfp);
222 ret = rxrpc_init_client_conn_security(conn);
226 ret = conn->security->prime_packet_security(conn);
230 write_lock(&rxrpc_connection_lock);
231 list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list);
232 write_unlock(&rxrpc_connection_lock);
234 /* We steal the caller's peer ref. */
236 rxrpc_get_local(conn->params.local);
237 key_get(conn->params.key);
239 _leave(" = %p", conn);
243 conn->security->clear(conn);
245 rxrpc_put_client_connection_id(conn);
248 _leave(" = %d", ret);
253 * Determine if a connection may be reused.
255 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
257 int id_cursor, id, distance, limit;
259 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
262 if (conn->proto.epoch != rxrpc_epoch)
263 goto mark_dont_reuse;
265 /* The IDR tree gets very expensive on memory if the connection IDs are
266 * widely scattered throughout the number space, so we shall want to
267 * kill off connections that, say, have an ID more than about four
268 * times the maximum number of client conns away from the current
269 * allocation point to try and keep the IDs concentrated.
271 id_cursor = READ_ONCE(rxrpc_client_conn_ids.cur);
272 id = conn->proto.cid >> RXRPC_CIDSHIFT;
273 distance = id - id_cursor;
275 distance = -distance;
276 limit = round_up(rxrpc_max_client_connections, IDR_SIZE) * 4;
277 if (distance > limit)
278 goto mark_dont_reuse;
283 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
289 * Create or find a client connection to use for a call.
291 * If we return with a connection, the call will be on its waiting list. It's
292 * left to the caller to assign a channel and wake up the call.
294 static int rxrpc_get_client_conn(struct rxrpc_call *call,
295 struct rxrpc_conn_parameters *cp,
296 struct sockaddr_rxrpc *srx,
299 struct rxrpc_connection *conn, *candidate = NULL;
300 struct rxrpc_local *local = cp->local;
301 struct rb_node *p, **pp, *parent;
305 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
307 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
311 /* If the connection is not meant to be exclusive, search the available
312 * connections to see if the connection we want to use already exists.
314 if (!cp->exclusive) {
316 spin_lock(&local->client_conns_lock);
317 p = local->client_conns.rb_node;
319 conn = rb_entry(p, struct rxrpc_connection, client_node);
321 #define cmp(X) ((long)conn->params.X - (long)cp->X)
324 cmp(security_level));
328 } else if (diff > 0) {
331 if (rxrpc_may_reuse_conn(conn) &&
332 rxrpc_get_connection_maybe(conn))
333 goto found_extant_conn;
334 /* The connection needs replacing. It's better
335 * to effect that when we have something to
336 * replace it with so that we don't have to
337 * rebalance the tree twice.
342 spin_unlock(&local->client_conns_lock);
345 /* There wasn't a connection yet or we need an exclusive connection.
346 * We need to create a candidate and then potentially redo the search
347 * in case we're racing with another thread also trying to connect on a
348 * shareable connection.
351 candidate = rxrpc_alloc_client_connection(cp, gfp);
352 if (IS_ERR(candidate)) {
353 ret = PTR_ERR(candidate);
357 /* Add the call to the new connection's waiting list in case we're
358 * going to have to wait for the connection to come live. It's our
359 * connection, so we want first dibs on the channel slots. We would
360 * normally have to take channel_lock but we do this before anyone else
361 * can see the connection.
363 list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
366 call->conn = candidate;
367 _leave(" = 0 [exclusive %d]", candidate->debug_id);
371 /* Publish the new connection for userspace to find. We need to redo
372 * the search before doing this lest we race with someone else adding a
373 * conflicting instance.
376 spin_lock(&local->client_conns_lock);
378 pp = &local->client_conns.rb_node;
382 conn = rb_entry(parent, struct rxrpc_connection, client_node);
384 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
387 cmp(security_level));
390 pp = &(*pp)->rb_left;
391 } else if (diff > 0) {
392 pp = &(*pp)->rb_right;
394 if (rxrpc_may_reuse_conn(conn) &&
395 rxrpc_get_connection_maybe(conn))
396 goto found_extant_conn;
397 /* The old connection is from an outdated epoch. */
398 _debug("replace conn");
399 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
400 rb_replace_node(&conn->client_node,
401 &candidate->client_node,
402 &local->client_conns);
403 goto candidate_published;
408 rb_link_node(&candidate->client_node, parent, pp);
409 rb_insert_color(&candidate->client_node, &local->client_conns);
412 set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
413 call->conn = candidate;
414 spin_unlock(&local->client_conns_lock);
415 _leave(" = 0 [new %d]", candidate->debug_id);
418 /* We come here if we found a suitable connection already in existence.
419 * Discard any candidate we may have allocated, and try to get a
420 * channel on this one.
423 _debug("found conn");
424 spin_unlock(&local->client_conns_lock);
426 rxrpc_put_connection(candidate);
429 spin_lock(&conn->channel_lock);
431 list_add(&call->chan_wait_link, &conn->waiting_calls);
432 spin_unlock(&conn->channel_lock);
433 _leave(" = 0 [extant %d]", conn->debug_id);
437 rxrpc_put_peer(cp->peer);
440 _leave(" = %d", ret);
445 * Activate a connection.
447 static void rxrpc_activate_conn(struct rxrpc_connection *conn)
449 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
450 rxrpc_nr_active_client_conns++;
451 list_move_tail(&conn->cache_link, &rxrpc_active_client_conns);
455 * Attempt to animate a connection for a new call.
457 * If it's not exclusive, the connection is in the endpoint tree, and we're in
458 * the conn's list of those waiting to grab a channel. There is, however, a
459 * limit on the number of live connections allowed at any one time, so we may
460 * have to wait for capacity to become available.
462 * Note that a connection on the waiting queue might *also* have active
463 * channels if it has been culled to make space and then re-requested by a new
466 static void rxrpc_animate_client_conn(struct rxrpc_connection *conn)
468 unsigned int nr_conns;
470 _enter("%d,%d", conn->debug_id, conn->cache_state);
472 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE)
475 spin_lock(&rxrpc_client_conn_cache_lock);
477 nr_conns = rxrpc_nr_client_conns;
478 if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags))
479 rxrpc_nr_client_conns = nr_conns + 1;
481 switch (conn->cache_state) {
482 case RXRPC_CONN_CLIENT_ACTIVE:
483 case RXRPC_CONN_CLIENT_WAITING:
486 case RXRPC_CONN_CLIENT_INACTIVE:
487 case RXRPC_CONN_CLIENT_CULLED:
488 case RXRPC_CONN_CLIENT_IDLE:
489 if (nr_conns >= rxrpc_max_client_connections)
490 goto wait_for_capacity;
498 spin_unlock(&rxrpc_client_conn_cache_lock);
500 _leave(" [%d]", conn->cache_state);
505 rxrpc_activate_conn(conn);
510 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
511 list_move_tail(&conn->cache_link, &rxrpc_waiting_client_conns);
516 * Deactivate a channel.
518 static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
519 unsigned int channel)
521 struct rxrpc_channel *chan = &conn->channels[channel];
523 rcu_assign_pointer(chan->call, NULL);
524 conn->active_chans &= ~(1 << channel);
528 * Assign a channel to the call at the front of the queue and wake the call up.
529 * We don't increment the callNumber counter until this number has been exposed
532 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
533 unsigned int channel)
535 struct rxrpc_channel *chan = &conn->channels[channel];
536 struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
537 struct rxrpc_call, chan_wait_link);
538 u32 call_id = chan->call_counter + 1;
540 list_del_init(&call->chan_wait_link);
541 conn->active_chans |= 1 << channel;
542 call->peer = rxrpc_get_peer(conn->params.peer);
543 call->cid = conn->proto.cid | channel;
544 call->call_id = call_id;
546 _net("CONNECT call %08x:%08x as call %d on conn %d",
547 call->cid, call->call_id, call->debug_id, conn->debug_id);
549 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
550 * orders cid and epoch in the connection wrt to call_id without the
551 * need to take the channel_lock.
553 * We provisionally assign a callNumber at this point, but we don't
554 * confirm it until the call is about to be exposed.
556 * TODO: Pair with a barrier in the data_ready handler when that looks
557 * at the call ID through a connection channel.
560 chan->call_id = call_id;
561 rcu_assign_pointer(chan->call, call);
562 wake_up(&call->waitq);
566 * Assign channels and callNumbers to waiting calls.
568 static void rxrpc_activate_channels(struct rxrpc_connection *conn)
572 _enter("%d", conn->debug_id);
574 if (conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE ||
575 conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
578 spin_lock(&conn->channel_lock);
580 while (!list_empty(&conn->waiting_calls) &&
581 (mask = ~conn->active_chans,
582 mask &= RXRPC_ACTIVE_CHANS_MASK,
584 rxrpc_activate_one_channel(conn, __ffs(mask));
586 spin_unlock(&conn->channel_lock);
591 * Wait for a callNumber and a channel to be granted to a call.
593 static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
597 _enter("%d", call->debug_id);
599 if (!call->call_id) {
600 DECLARE_WAITQUEUE(myself, current);
602 if (!gfpflags_allow_blocking(gfp)) {
607 add_wait_queue_exclusive(&call->waitq, &myself);
609 set_current_state(TASK_INTERRUPTIBLE);
612 if (signal_pending(current)) {
618 remove_wait_queue(&call->waitq, &myself);
619 __set_current_state(TASK_RUNNING);
622 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
626 _leave(" = %d", ret);
631 * find a connection for a call
632 * - called in process context with IRQs enabled
634 int rxrpc_connect_call(struct rxrpc_call *call,
635 struct rxrpc_conn_parameters *cp,
636 struct sockaddr_rxrpc *srx,
641 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
643 rxrpc_discard_expired_client_conns(NULL);
644 rxrpc_cull_active_client_conns();
646 ret = rxrpc_get_client_conn(call, cp, srx, gfp);
650 rxrpc_animate_client_conn(call->conn);
651 rxrpc_activate_channels(call->conn);
653 ret = rxrpc_wait_for_channel(call, gfp);
655 rxrpc_disconnect_client_call(call);
657 _leave(" = %d", ret);
662 * Note that a connection is about to be exposed to the world. Once it is
663 * exposed, we maintain an extra ref on it that stops it from being summarily
664 * discarded before it's (a) had a chance to deal with retransmission and (b)
665 * had a chance at re-use (the per-connection security negotiation is
668 static void rxrpc_expose_client_conn(struct rxrpc_connection *conn)
670 if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags))
671 rxrpc_get_connection(conn);
675 * Note that a call, and thus a connection, is about to be exposed to the
678 void rxrpc_expose_client_call(struct rxrpc_call *call)
680 struct rxrpc_connection *conn = call->conn;
681 struct rxrpc_channel *chan =
682 &conn->channels[call->cid & RXRPC_CHANNELMASK];
684 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
685 /* Mark the call ID as being used. If the callNumber counter
686 * exceeds ~2 billion, we kill the connection after its
687 * outstanding calls have finished so that the counter doesn't
690 chan->call_counter++;
691 if (chan->call_counter >= INT_MAX)
692 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
693 rxrpc_expose_client_conn(conn);
698 * Disconnect a client call.
700 void rxrpc_disconnect_client_call(struct rxrpc_call *call)
702 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
703 struct rxrpc_connection *conn = call->conn;
704 struct rxrpc_channel *chan = &conn->channels[channel];
708 spin_lock(&conn->channel_lock);
710 /* Calls that have never actually been assigned a channel can simply be
711 * discarded. If the conn didn't get used either, it will follow
712 * immediately unless someone else grabs it in the meantime.
714 if (!list_empty(&call->chan_wait_link)) {
715 _debug("call is waiting");
716 ASSERTCMP(call->call_id, ==, 0);
717 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
718 list_del_init(&call->chan_wait_link);
720 /* We must deactivate or idle the connection if it's now
721 * waiting for nothing.
723 spin_lock(&rxrpc_client_conn_cache_lock);
724 if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
725 list_empty(&conn->waiting_calls) &&
727 goto idle_connection;
731 ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
732 ASSERTCMP(atomic_read(&conn->usage), >=, 2);
734 /* If a client call was exposed to the world, we save the result for
737 * We use a barrier here so that the call number and abort code can be
738 * read without needing to take a lock.
740 * TODO: Make the incoming packet handler check this and handle
741 * terminal retransmission without requiring access to the call.
743 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
744 _debug("exposed %u,%u", call->call_id, call->local_abort);
745 __rxrpc_disconnect_call(conn, call);
748 /* See if we can pass the channel directly to another call. */
749 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
750 !list_empty(&conn->waiting_calls)) {
752 rxrpc_activate_one_channel(conn, channel);
756 /* Things are more complex and we need the cache lock. We might be
757 * able to simply idle the conn or it might now be lurking on the wait
758 * list. It might even get moved back to the active list whilst we're
759 * waiting for the lock.
761 spin_lock(&rxrpc_client_conn_cache_lock);
763 switch (conn->cache_state) {
764 case RXRPC_CONN_CLIENT_ACTIVE:
765 if (list_empty(&conn->waiting_calls)) {
766 rxrpc_deactivate_one_channel(conn, channel);
767 if (!conn->active_chans) {
768 rxrpc_nr_active_client_conns--;
769 goto idle_connection;
774 _debug("pass chan 2");
775 rxrpc_activate_one_channel(conn, channel);
778 case RXRPC_CONN_CLIENT_CULLED:
779 rxrpc_deactivate_one_channel(conn, channel);
780 ASSERT(list_empty(&conn->waiting_calls));
781 if (!conn->active_chans)
782 goto idle_connection;
785 case RXRPC_CONN_CLIENT_WAITING:
786 rxrpc_deactivate_one_channel(conn, channel);
794 spin_unlock(&rxrpc_client_conn_cache_lock);
796 spin_unlock(&conn->channel_lock);
797 rxrpc_put_connection(conn);
802 /* As no channels remain active, the connection gets deactivated
803 * immediately or moved to the idle list for a short while.
805 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
807 conn->idle_timestamp = jiffies;
808 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
809 list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns);
810 if (rxrpc_idle_client_conns.next == &conn->cache_link &&
811 !rxrpc_kill_all_client_conns)
812 queue_delayed_work(rxrpc_workqueue,
813 &rxrpc_client_conn_reap,
814 rxrpc_conn_idle_client_expiry);
816 _debug("make inactive");
817 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
818 list_del_init(&conn->cache_link);
824 * Clean up a dead client connection.
826 static struct rxrpc_connection *
827 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
829 struct rxrpc_connection *next;
830 struct rxrpc_local *local = conn->params.local;
831 unsigned int nr_conns;
833 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
834 spin_lock(&local->client_conns_lock);
835 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
837 rb_erase(&conn->client_node, &local->client_conns);
838 spin_unlock(&local->client_conns_lock);
841 rxrpc_put_client_connection_id(conn);
843 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
845 if (!test_bit(RXRPC_CONN_COUNTED, &conn->flags))
848 spin_lock(&rxrpc_client_conn_cache_lock);
849 nr_conns = --rxrpc_nr_client_conns;
852 if (nr_conns < rxrpc_max_client_connections &&
853 !list_empty(&rxrpc_waiting_client_conns)) {
854 next = list_entry(rxrpc_waiting_client_conns.next,
855 struct rxrpc_connection, cache_link);
856 rxrpc_get_connection(next);
857 rxrpc_activate_conn(next);
860 spin_unlock(&rxrpc_client_conn_cache_lock);
861 rxrpc_kill_connection(conn);
864 rxrpc_activate_channels(next);
866 /* We need to get rid of the temporary ref we took upon next, but we
867 * can't call rxrpc_put_connection() recursively.
873 * Clean up a dead client connections.
875 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
877 struct rxrpc_connection *next;
880 _enter("%p{u=%d,d=%d}",
881 conn, atomic_read(&conn->usage), conn->debug_id);
883 next = rxrpc_put_one_client_conn(conn);
888 } while (atomic_dec_and_test(&conn->usage));
894 * Kill the longest-active client connections to make room for new ones.
896 static void rxrpc_cull_active_client_conns(void)
898 struct rxrpc_connection *conn;
899 unsigned int nr_conns = rxrpc_nr_client_conns;
900 unsigned int nr_active, limit;
904 ASSERTCMP(nr_conns, >=, 0);
905 if (nr_conns < rxrpc_max_client_connections) {
909 limit = rxrpc_reap_client_connections;
911 spin_lock(&rxrpc_client_conn_cache_lock);
912 nr_active = rxrpc_nr_active_client_conns;
914 while (nr_active > limit) {
915 ASSERT(!list_empty(&rxrpc_active_client_conns));
916 conn = list_entry(rxrpc_active_client_conns.next,
917 struct rxrpc_connection, cache_link);
918 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
920 if (list_empty(&conn->waiting_calls)) {
921 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
922 list_del_init(&conn->cache_link);
924 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
925 list_move_tail(&conn->cache_link,
926 &rxrpc_waiting_client_conns);
932 rxrpc_nr_active_client_conns = nr_active;
933 spin_unlock(&rxrpc_client_conn_cache_lock);
934 ASSERTCMP(nr_active, >=, 0);
939 * Discard expired client connections from the idle list. Each conn in the
940 * idle list has been exposed and holds an extra ref because of that.
942 * This may be called from conn setup or from a work item so cannot be
943 * considered non-reentrant.
945 static void rxrpc_discard_expired_client_conns(struct work_struct *work)
947 struct rxrpc_connection *conn;
948 unsigned long expiry, conn_expires_at, now;
949 unsigned int nr_conns;
950 bool did_discard = false;
952 _enter("%c", work ? 'w' : 'n');
954 if (list_empty(&rxrpc_idle_client_conns)) {
959 /* Don't double up on the discarding */
960 if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) {
961 _leave(" [already]");
965 /* We keep an estimate of what the number of conns ought to be after
966 * we've discarded some so that we don't overdo the discarding.
968 nr_conns = rxrpc_nr_client_conns;
971 spin_lock(&rxrpc_client_conn_cache_lock);
973 if (list_empty(&rxrpc_idle_client_conns))
976 conn = list_entry(rxrpc_idle_client_conns.next,
977 struct rxrpc_connection, cache_link);
978 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
980 if (!rxrpc_kill_all_client_conns) {
981 /* If the number of connections is over the reap limit, we
982 * expedite discard by reducing the expiry timeout. We must,
983 * however, have at least a short grace period to be able to do
984 * final-ACK or ABORT retransmission.
986 expiry = rxrpc_conn_idle_client_expiry;
987 if (nr_conns > rxrpc_reap_client_connections)
988 expiry = rxrpc_conn_idle_client_fast_expiry;
990 conn_expires_at = conn->idle_timestamp + expiry;
992 now = READ_ONCE(jiffies);
993 if (time_after(conn_expires_at, now))
994 goto not_yet_expired;
997 _debug("discard conn %d", conn->debug_id);
998 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
1000 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
1001 list_del_init(&conn->cache_link);
1003 spin_unlock(&rxrpc_client_conn_cache_lock);
1005 /* When we cleared the EXPOSED flag, we took on responsibility for the
1006 * reference that that had on the usage count. We deal with that here.
1007 * If someone re-sets the flag and re-gets the ref, that's fine.
1009 rxrpc_put_connection(conn);
1015 /* The connection at the front of the queue hasn't yet expired, so
1016 * schedule the work item for that point if we discarded something.
1018 * We don't worry if the work item is already scheduled - it can look
1019 * after rescheduling itself at a later time. We could cancel it, but
1020 * then things get messier.
1023 if (!rxrpc_kill_all_client_conns)
1024 queue_delayed_work(rxrpc_workqueue,
1025 &rxrpc_client_conn_reap,
1026 conn_expires_at - now);
1029 spin_unlock(&rxrpc_client_conn_cache_lock);
1030 spin_unlock(&rxrpc_client_conn_discard_mutex);
1035 * Preemptively destroy all the client connection records rather than waiting
1036 * for them to time out
1038 void __exit rxrpc_destroy_all_client_connections(void)
1042 spin_lock(&rxrpc_client_conn_cache_lock);
1043 rxrpc_kill_all_client_conns = true;
1044 spin_unlock(&rxrpc_client_conn_cache_lock);
1046 cancel_delayed_work(&rxrpc_client_conn_reap);
1048 if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0))
1049 _debug("destroy: queue failed");