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);
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 trace_rxrpc_conn(conn, rxrpc_conn_new_client, atomic_read(&conn->usage),
224 __builtin_return_address(0));
225 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
226 _leave(" = %p", conn);
230 conn->security->clear(conn);
232 rxrpc_put_client_connection_id(conn);
235 _leave(" = %d", ret);
240 * Determine if a connection may be reused.
242 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
244 int id_cursor, id, distance, limit;
246 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
249 if (conn->proto.epoch != rxrpc_epoch)
250 goto mark_dont_reuse;
252 /* The IDR tree gets very expensive on memory if the connection IDs are
253 * widely scattered throughout the number space, so we shall want to
254 * kill off connections that, say, have an ID more than about four
255 * times the maximum number of client conns away from the current
256 * allocation point to try and keep the IDs concentrated.
258 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
259 id = conn->proto.cid >> RXRPC_CIDSHIFT;
260 distance = id - id_cursor;
262 distance = -distance;
263 limit = max(rxrpc_max_client_connections * 4, 1024U);
264 if (distance > limit)
265 goto mark_dont_reuse;
270 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
276 * Create or find a client connection to use for a call.
278 * If we return with a connection, the call will be on its waiting list. It's
279 * left to the caller to assign a channel and wake up the call.
281 static int rxrpc_get_client_conn(struct rxrpc_call *call,
282 struct rxrpc_conn_parameters *cp,
283 struct sockaddr_rxrpc *srx,
286 struct rxrpc_connection *conn, *candidate = NULL;
287 struct rxrpc_local *local = cp->local;
288 struct rb_node *p, **pp, *parent;
292 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
294 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
298 /* If the connection is not meant to be exclusive, search the available
299 * connections to see if the connection we want to use already exists.
301 if (!cp->exclusive) {
303 spin_lock(&local->client_conns_lock);
304 p = local->client_conns.rb_node;
306 conn = rb_entry(p, struct rxrpc_connection, client_node);
308 #define cmp(X) ((long)conn->params.X - (long)cp->X)
311 cmp(security_level));
315 } else if (diff > 0) {
318 if (rxrpc_may_reuse_conn(conn) &&
319 rxrpc_get_connection_maybe(conn))
320 goto found_extant_conn;
321 /* The connection needs replacing. It's better
322 * to effect that when we have something to
323 * replace it with so that we don't have to
324 * rebalance the tree twice.
329 spin_unlock(&local->client_conns_lock);
332 /* There wasn't a connection yet or we need an exclusive connection.
333 * We need to create a candidate and then potentially redo the search
334 * in case we're racing with another thread also trying to connect on a
335 * shareable connection.
338 candidate = rxrpc_alloc_client_connection(cp, gfp);
339 if (IS_ERR(candidate)) {
340 ret = PTR_ERR(candidate);
344 /* Add the call to the new connection's waiting list in case we're
345 * going to have to wait for the connection to come live. It's our
346 * connection, so we want first dibs on the channel slots. We would
347 * normally have to take channel_lock but we do this before anyone else
348 * can see the connection.
350 list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
353 call->conn = candidate;
354 call->security_ix = candidate->security_ix;
355 _leave(" = 0 [exclusive %d]", candidate->debug_id);
359 /* Publish the new connection for userspace to find. We need to redo
360 * the search before doing this lest we race with someone else adding a
361 * conflicting instance.
364 spin_lock(&local->client_conns_lock);
366 pp = &local->client_conns.rb_node;
370 conn = rb_entry(parent, struct rxrpc_connection, client_node);
372 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
375 cmp(security_level));
378 pp = &(*pp)->rb_left;
379 } else if (diff > 0) {
380 pp = &(*pp)->rb_right;
382 if (rxrpc_may_reuse_conn(conn) &&
383 rxrpc_get_connection_maybe(conn))
384 goto found_extant_conn;
385 /* The old connection is from an outdated epoch. */
386 _debug("replace conn");
387 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
388 rb_replace_node(&conn->client_node,
389 &candidate->client_node,
390 &local->client_conns);
391 trace_rxrpc_client(conn, -1, rxrpc_client_replace);
392 goto candidate_published;
397 rb_link_node(&candidate->client_node, parent, pp);
398 rb_insert_color(&candidate->client_node, &local->client_conns);
401 set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
402 call->conn = candidate;
403 call->security_ix = candidate->security_ix;
404 spin_unlock(&local->client_conns_lock);
405 _leave(" = 0 [new %d]", candidate->debug_id);
408 /* We come here if we found a suitable connection already in existence.
409 * Discard any candidate we may have allocated, and try to get a
410 * channel on this one.
413 _debug("found conn");
414 spin_unlock(&local->client_conns_lock);
417 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
418 rxrpc_put_connection(candidate);
422 spin_lock(&conn->channel_lock);
424 call->security_ix = conn->security_ix;
425 list_add(&call->chan_wait_link, &conn->waiting_calls);
426 spin_unlock(&conn->channel_lock);
427 _leave(" = 0 [extant %d]", conn->debug_id);
431 rxrpc_put_peer(cp->peer);
434 _leave(" = %d", ret);
439 * Activate a connection.
441 static void rxrpc_activate_conn(struct rxrpc_connection *conn)
443 trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
444 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
445 rxrpc_nr_active_client_conns++;
446 list_move_tail(&conn->cache_link, &rxrpc_active_client_conns);
450 * Attempt to animate a connection for a new call.
452 * If it's not exclusive, the connection is in the endpoint tree, and we're in
453 * the conn's list of those waiting to grab a channel. There is, however, a
454 * limit on the number of live connections allowed at any one time, so we may
455 * have to wait for capacity to become available.
457 * Note that a connection on the waiting queue might *also* have active
458 * channels if it has been culled to make space and then re-requested by a new
461 static void rxrpc_animate_client_conn(struct rxrpc_connection *conn)
463 unsigned int nr_conns;
465 _enter("%d,%d", conn->debug_id, conn->cache_state);
467 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE)
470 spin_lock(&rxrpc_client_conn_cache_lock);
472 nr_conns = rxrpc_nr_client_conns;
473 if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
474 trace_rxrpc_client(conn, -1, rxrpc_client_count);
475 rxrpc_nr_client_conns = nr_conns + 1;
478 switch (conn->cache_state) {
479 case RXRPC_CONN_CLIENT_ACTIVE:
480 case RXRPC_CONN_CLIENT_WAITING:
483 case RXRPC_CONN_CLIENT_INACTIVE:
484 case RXRPC_CONN_CLIENT_CULLED:
485 case RXRPC_CONN_CLIENT_IDLE:
486 if (nr_conns >= rxrpc_max_client_connections)
487 goto wait_for_capacity;
495 spin_unlock(&rxrpc_client_conn_cache_lock);
497 _leave(" [%d]", conn->cache_state);
502 rxrpc_activate_conn(conn);
507 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
508 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
509 list_move_tail(&conn->cache_link, &rxrpc_waiting_client_conns);
514 * Deactivate a channel.
516 static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
517 unsigned int channel)
519 struct rxrpc_channel *chan = &conn->channels[channel];
521 rcu_assign_pointer(chan->call, NULL);
522 conn->active_chans &= ~(1 << channel);
526 * Assign a channel to the call at the front of the queue and wake the call up.
527 * We don't increment the callNumber counter until this number has been exposed
530 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
531 unsigned int channel)
533 struct rxrpc_channel *chan = &conn->channels[channel];
534 struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
535 struct rxrpc_call, chan_wait_link);
536 u32 call_id = chan->call_counter + 1;
538 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
540 write_lock_bh(&call->state_lock);
541 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
542 write_unlock_bh(&call->state_lock);
544 rxrpc_see_call(call);
545 list_del_init(&call->chan_wait_link);
546 conn->active_chans |= 1 << channel;
547 call->peer = rxrpc_get_peer(conn->params.peer);
548 call->cid = conn->proto.cid | channel;
549 call->call_id = call_id;
551 _net("CONNECT call %08x:%08x as call %d on conn %d",
552 call->cid, call->call_id, call->debug_id, conn->debug_id);
554 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
555 * orders cid and epoch in the connection wrt to call_id without the
556 * need to take the channel_lock.
558 * We provisionally assign a callNumber at this point, but we don't
559 * confirm it until the call is about to be exposed.
561 * TODO: Pair with a barrier in the data_ready handler when that looks
562 * at the call ID through a connection channel.
565 chan->call_id = call_id;
566 rcu_assign_pointer(chan->call, call);
567 wake_up(&call->waitq);
571 * Assign channels and callNumbers to waiting calls with channel_lock
574 static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
578 switch (conn->cache_state) {
579 case RXRPC_CONN_CLIENT_ACTIVE:
580 mask = RXRPC_ACTIVE_CHANS_MASK;
586 while (!list_empty(&conn->waiting_calls) &&
587 (avail = ~conn->active_chans,
590 rxrpc_activate_one_channel(conn, __ffs(avail));
594 * Assign channels and callNumbers to waiting calls.
596 static void rxrpc_activate_channels(struct rxrpc_connection *conn)
598 _enter("%d", conn->debug_id);
600 trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
602 if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
605 spin_lock(&conn->channel_lock);
606 rxrpc_activate_channels_locked(conn);
607 spin_unlock(&conn->channel_lock);
612 * Wait for a callNumber and a channel to be granted to a call.
614 static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
618 _enter("%d", call->debug_id);
620 if (!call->call_id) {
621 DECLARE_WAITQUEUE(myself, current);
623 if (!gfpflags_allow_blocking(gfp)) {
628 add_wait_queue_exclusive(&call->waitq, &myself);
630 set_current_state(TASK_INTERRUPTIBLE);
633 if (signal_pending(current)) {
639 remove_wait_queue(&call->waitq, &myself);
640 __set_current_state(TASK_RUNNING);
643 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
647 _leave(" = %d", ret);
652 * find a connection for a call
653 * - called in process context with IRQs enabled
655 int rxrpc_connect_call(struct rxrpc_call *call,
656 struct rxrpc_conn_parameters *cp,
657 struct sockaddr_rxrpc *srx,
662 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
664 rxrpc_discard_expired_client_conns(NULL);
665 rxrpc_cull_active_client_conns();
667 ret = rxrpc_get_client_conn(call, cp, srx, gfp);
671 rxrpc_animate_client_conn(call->conn);
672 rxrpc_activate_channels(call->conn);
674 ret = rxrpc_wait_for_channel(call, gfp);
676 rxrpc_disconnect_client_call(call);
678 _leave(" = %d", ret);
683 * Note that a connection is about to be exposed to the world. Once it is
684 * exposed, we maintain an extra ref on it that stops it from being summarily
685 * discarded before it's (a) had a chance to deal with retransmission and (b)
686 * had a chance at re-use (the per-connection security negotiation is
689 static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
690 unsigned int channel)
692 if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
693 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
694 rxrpc_get_connection(conn);
699 * Note that a call, and thus a connection, is about to be exposed to the
702 void rxrpc_expose_client_call(struct rxrpc_call *call)
704 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
705 struct rxrpc_connection *conn = call->conn;
706 struct rxrpc_channel *chan = &conn->channels[channel];
708 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
709 /* Mark the call ID as being used. If the callNumber counter
710 * exceeds ~2 billion, we kill the connection after its
711 * outstanding calls have finished so that the counter doesn't
714 chan->call_counter++;
715 if (chan->call_counter >= INT_MAX)
716 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
717 rxrpc_expose_client_conn(conn, channel);
722 * Disconnect a client call.
724 void rxrpc_disconnect_client_call(struct rxrpc_call *call)
726 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
727 struct rxrpc_connection *conn = call->conn;
728 struct rxrpc_channel *chan = &conn->channels[channel];
730 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
733 spin_lock(&conn->channel_lock);
735 /* Calls that have never actually been assigned a channel can simply be
736 * discarded. If the conn didn't get used either, it will follow
737 * immediately unless someone else grabs it in the meantime.
739 if (!list_empty(&call->chan_wait_link)) {
740 _debug("call is waiting");
741 ASSERTCMP(call->call_id, ==, 0);
742 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
743 list_del_init(&call->chan_wait_link);
745 trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
747 /* We must deactivate or idle the connection if it's now
748 * waiting for nothing.
750 spin_lock(&rxrpc_client_conn_cache_lock);
751 if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
752 list_empty(&conn->waiting_calls) &&
754 goto idle_connection;
758 ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
760 /* If a client call was exposed to the world, we save the result for
763 * We use a barrier here so that the call number and abort code can be
764 * read without needing to take a lock.
766 * TODO: Make the incoming packet handler check this and handle
767 * terminal retransmission without requiring access to the call.
769 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
770 _debug("exposed %u,%u", call->call_id, call->abort_code);
771 __rxrpc_disconnect_call(conn, call);
774 /* See if we can pass the channel directly to another call. */
775 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
776 !list_empty(&conn->waiting_calls)) {
777 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
778 rxrpc_activate_one_channel(conn, channel);
782 /* Things are more complex and we need the cache lock. We might be
783 * able to simply idle the conn or it might now be lurking on the wait
784 * list. It might even get moved back to the active list whilst we're
785 * waiting for the lock.
787 spin_lock(&rxrpc_client_conn_cache_lock);
789 switch (conn->cache_state) {
790 case RXRPC_CONN_CLIENT_ACTIVE:
791 if (list_empty(&conn->waiting_calls)) {
792 rxrpc_deactivate_one_channel(conn, channel);
793 if (!conn->active_chans) {
794 rxrpc_nr_active_client_conns--;
795 goto idle_connection;
800 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
801 rxrpc_activate_one_channel(conn, channel);
804 case RXRPC_CONN_CLIENT_CULLED:
805 rxrpc_deactivate_one_channel(conn, channel);
806 ASSERT(list_empty(&conn->waiting_calls));
807 if (!conn->active_chans)
808 goto idle_connection;
811 case RXRPC_CONN_CLIENT_WAITING:
812 rxrpc_deactivate_one_channel(conn, channel);
820 spin_unlock(&rxrpc_client_conn_cache_lock);
822 spin_unlock(&conn->channel_lock);
823 rxrpc_put_connection(conn);
828 /* As no channels remain active, the connection gets deactivated
829 * immediately or moved to the idle list for a short while.
831 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
832 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
833 conn->idle_timestamp = jiffies;
834 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
835 list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns);
836 if (rxrpc_idle_client_conns.next == &conn->cache_link &&
837 !rxrpc_kill_all_client_conns)
838 queue_delayed_work(rxrpc_workqueue,
839 &rxrpc_client_conn_reap,
840 rxrpc_conn_idle_client_expiry);
842 trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
843 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
844 list_del_init(&conn->cache_link);
850 * Clean up a dead client connection.
852 static struct rxrpc_connection *
853 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
855 struct rxrpc_connection *next = NULL;
856 struct rxrpc_local *local = conn->params.local;
857 unsigned int nr_conns;
859 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
861 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
862 spin_lock(&local->client_conns_lock);
863 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
865 rb_erase(&conn->client_node, &local->client_conns);
866 spin_unlock(&local->client_conns_lock);
869 rxrpc_put_client_connection_id(conn);
871 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
873 if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
874 trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
875 spin_lock(&rxrpc_client_conn_cache_lock);
876 nr_conns = --rxrpc_nr_client_conns;
878 if (nr_conns < rxrpc_max_client_connections &&
879 !list_empty(&rxrpc_waiting_client_conns)) {
880 next = list_entry(rxrpc_waiting_client_conns.next,
881 struct rxrpc_connection, cache_link);
882 rxrpc_get_connection(next);
883 rxrpc_activate_conn(next);
886 spin_unlock(&rxrpc_client_conn_cache_lock);
889 rxrpc_kill_connection(conn);
891 rxrpc_activate_channels(next);
893 /* We need to get rid of the temporary ref we took upon next, but we
894 * can't call rxrpc_put_connection() recursively.
900 * Clean up a dead client connections.
902 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
904 const void *here = __builtin_return_address(0);
908 n = atomic_dec_return(&conn->usage);
909 trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
914 conn = rxrpc_put_one_client_conn(conn);
919 * Kill the longest-active client connections to make room for new ones.
921 static void rxrpc_cull_active_client_conns(void)
923 struct rxrpc_connection *conn;
924 unsigned int nr_conns = rxrpc_nr_client_conns;
925 unsigned int nr_active, limit;
929 ASSERTCMP(nr_conns, >=, 0);
930 if (nr_conns < rxrpc_max_client_connections) {
934 limit = rxrpc_reap_client_connections;
936 spin_lock(&rxrpc_client_conn_cache_lock);
937 nr_active = rxrpc_nr_active_client_conns;
939 while (nr_active > limit) {
940 ASSERT(!list_empty(&rxrpc_active_client_conns));
941 conn = list_entry(rxrpc_active_client_conns.next,
942 struct rxrpc_connection, cache_link);
943 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
945 if (list_empty(&conn->waiting_calls)) {
946 trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
947 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
948 list_del_init(&conn->cache_link);
950 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
951 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
952 list_move_tail(&conn->cache_link,
953 &rxrpc_waiting_client_conns);
959 rxrpc_nr_active_client_conns = nr_active;
960 spin_unlock(&rxrpc_client_conn_cache_lock);
961 ASSERTCMP(nr_active, >=, 0);
966 * Discard expired client connections from the idle list. Each conn in the
967 * idle list has been exposed and holds an extra ref because of that.
969 * This may be called from conn setup or from a work item so cannot be
970 * considered non-reentrant.
972 static void rxrpc_discard_expired_client_conns(struct work_struct *work)
974 struct rxrpc_connection *conn;
975 unsigned long expiry, conn_expires_at, now;
976 unsigned int nr_conns;
977 bool did_discard = false;
979 _enter("%c", work ? 'w' : 'n');
981 if (list_empty(&rxrpc_idle_client_conns)) {
986 /* Don't double up on the discarding */
987 if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) {
988 _leave(" [already]");
992 /* We keep an estimate of what the number of conns ought to be after
993 * we've discarded some so that we don't overdo the discarding.
995 nr_conns = rxrpc_nr_client_conns;
998 spin_lock(&rxrpc_client_conn_cache_lock);
1000 if (list_empty(&rxrpc_idle_client_conns))
1003 conn = list_entry(rxrpc_idle_client_conns.next,
1004 struct rxrpc_connection, cache_link);
1005 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
1007 if (!rxrpc_kill_all_client_conns) {
1008 /* If the number of connections is over the reap limit, we
1009 * expedite discard by reducing the expiry timeout. We must,
1010 * however, have at least a short grace period to be able to do
1011 * final-ACK or ABORT retransmission.
1013 expiry = rxrpc_conn_idle_client_expiry;
1014 if (nr_conns > rxrpc_reap_client_connections)
1015 expiry = rxrpc_conn_idle_client_fast_expiry;
1017 conn_expires_at = conn->idle_timestamp + expiry;
1019 now = READ_ONCE(jiffies);
1020 if (time_after(conn_expires_at, now))
1021 goto not_yet_expired;
1024 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1025 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
1027 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
1028 list_del_init(&conn->cache_link);
1030 spin_unlock(&rxrpc_client_conn_cache_lock);
1032 /* When we cleared the EXPOSED flag, we took on responsibility for the
1033 * reference that that had on the usage count. We deal with that here.
1034 * If someone re-sets the flag and re-gets the ref, that's fine.
1036 rxrpc_put_connection(conn);
1042 /* The connection at the front of the queue hasn't yet expired, so
1043 * schedule the work item for that point if we discarded something.
1045 * We don't worry if the work item is already scheduled - it can look
1046 * after rescheduling itself at a later time. We could cancel it, but
1047 * then things get messier.
1050 if (!rxrpc_kill_all_client_conns)
1051 queue_delayed_work(rxrpc_workqueue,
1052 &rxrpc_client_conn_reap,
1053 conn_expires_at - now);
1056 spin_unlock(&rxrpc_client_conn_cache_lock);
1057 spin_unlock(&rxrpc_client_conn_discard_mutex);
1062 * Preemptively destroy all the client connection records rather than waiting
1063 * for them to time out
1065 void __exit rxrpc_destroy_all_client_connections(void)
1069 spin_lock(&rxrpc_client_conn_cache_lock);
1070 rxrpc_kill_all_client_conns = true;
1071 spin_unlock(&rxrpc_client_conn_cache_lock);
1073 cancel_delayed_work(&rxrpc_client_conn_reap);
1075 if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0))
1076 _debug("destroy: queue failed");