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 rxnet->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 rxnet->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 <linux/sched/signal.h>
81 #include "ar-internal.h"
83 __read_mostly unsigned int rxrpc_max_client_connections = 1000;
84 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
85 __read_mostly unsigned int rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
86 __read_mostly unsigned int rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
89 * We use machine-unique IDs for our client connections.
91 DEFINE_IDR(rxrpc_client_conn_ids);
92 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
94 static void rxrpc_cull_active_client_conns(struct rxrpc_net *);
97 * Get a connection ID and epoch for a client connection from the global pool.
98 * The connection struct pointer is then recorded in the idr radix tree. The
99 * epoch doesn't change until the client is rebooted (or, at least, unless the
100 * module is unloaded).
102 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
105 struct rxrpc_net *rxnet = conn->params.local->rxnet;
111 spin_lock(&rxrpc_conn_id_lock);
113 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
114 1, 0x40000000, GFP_NOWAIT);
118 spin_unlock(&rxrpc_conn_id_lock);
121 conn->proto.epoch = rxnet->epoch;
122 conn->proto.cid = id << RXRPC_CIDSHIFT;
123 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
124 _leave(" [CID %x]", conn->proto.cid);
128 spin_unlock(&rxrpc_conn_id_lock);
135 * Release a connection ID for a client connection from the global pool.
137 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
139 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
140 spin_lock(&rxrpc_conn_id_lock);
141 idr_remove(&rxrpc_client_conn_ids,
142 conn->proto.cid >> RXRPC_CIDSHIFT);
143 spin_unlock(&rxrpc_conn_id_lock);
148 * Destroy the client connection ID tree.
150 void rxrpc_destroy_client_conn_ids(void)
152 struct rxrpc_connection *conn;
155 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
156 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
157 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
158 conn, atomic_read(&conn->usage));
163 idr_destroy(&rxrpc_client_conn_ids);
167 * Allocate a client connection.
169 static struct rxrpc_connection *
170 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
172 struct rxrpc_connection *conn;
173 struct rxrpc_net *rxnet = cp->local->rxnet;
178 conn = rxrpc_alloc_connection(gfp);
180 _leave(" = -ENOMEM");
181 return ERR_PTR(-ENOMEM);
184 atomic_set(&conn->usage, 1);
186 __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
189 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
190 conn->state = RXRPC_CONN_CLIENT;
192 ret = rxrpc_get_client_connection_id(conn, gfp);
196 ret = rxrpc_init_client_conn_security(conn);
200 ret = conn->security->prime_packet_security(conn);
204 write_lock(&rxnet->conn_lock);
205 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
206 write_unlock(&rxnet->conn_lock);
208 /* We steal the caller's peer ref. */
210 rxrpc_get_local(conn->params.local);
211 key_get(conn->params.key);
213 trace_rxrpc_conn(conn, rxrpc_conn_new_client, atomic_read(&conn->usage),
214 __builtin_return_address(0));
215 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
216 _leave(" = %p", conn);
220 conn->security->clear(conn);
222 rxrpc_put_client_connection_id(conn);
225 _leave(" = %d", ret);
230 * Determine if a connection may be reused.
232 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
234 struct rxrpc_net *rxnet = conn->params.local->rxnet;
235 int id_cursor, id, distance, limit;
237 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
240 if (conn->proto.epoch != rxnet->epoch)
241 goto mark_dont_reuse;
243 /* The IDR tree gets very expensive on memory if the connection IDs are
244 * widely scattered throughout the number space, so we shall want to
245 * kill off connections that, say, have an ID more than about four
246 * times the maximum number of client conns away from the current
247 * allocation point to try and keep the IDs concentrated.
249 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
250 id = conn->proto.cid >> RXRPC_CIDSHIFT;
251 distance = id - id_cursor;
253 distance = -distance;
254 limit = max(rxrpc_max_client_connections * 4, 1024U);
255 if (distance > limit)
256 goto mark_dont_reuse;
261 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
267 * Create or find a client connection to use for a call.
269 * If we return with a connection, the call will be on its waiting list. It's
270 * left to the caller to assign a channel and wake up the call.
272 static int rxrpc_get_client_conn(struct rxrpc_call *call,
273 struct rxrpc_conn_parameters *cp,
274 struct sockaddr_rxrpc *srx,
277 struct rxrpc_connection *conn, *candidate = NULL;
278 struct rxrpc_local *local = cp->local;
279 struct rb_node *p, **pp, *parent;
283 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
285 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
289 /* If the connection is not meant to be exclusive, search the available
290 * connections to see if the connection we want to use already exists.
292 if (!cp->exclusive) {
294 spin_lock(&local->client_conns_lock);
295 p = local->client_conns.rb_node;
297 conn = rb_entry(p, struct rxrpc_connection, client_node);
299 #define cmp(X) ((long)conn->params.X - (long)cp->X)
302 cmp(security_level));
306 } else if (diff > 0) {
309 if (rxrpc_may_reuse_conn(conn) &&
310 rxrpc_get_connection_maybe(conn))
311 goto found_extant_conn;
312 /* The connection needs replacing. It's better
313 * to effect that when we have something to
314 * replace it with so that we don't have to
315 * rebalance the tree twice.
320 spin_unlock(&local->client_conns_lock);
323 /* There wasn't a connection yet or we need an exclusive connection.
324 * We need to create a candidate and then potentially redo the search
325 * in case we're racing with another thread also trying to connect on a
326 * shareable connection.
329 candidate = rxrpc_alloc_client_connection(cp, gfp);
330 if (IS_ERR(candidate)) {
331 ret = PTR_ERR(candidate);
335 /* Add the call to the new connection's waiting list in case we're
336 * going to have to wait for the connection to come live. It's our
337 * connection, so we want first dibs on the channel slots. We would
338 * normally have to take channel_lock but we do this before anyone else
339 * can see the connection.
341 list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
344 call->conn = candidate;
345 call->security_ix = candidate->security_ix;
346 _leave(" = 0 [exclusive %d]", candidate->debug_id);
350 /* Publish the new connection for userspace to find. We need to redo
351 * the search before doing this lest we race with someone else adding a
352 * conflicting instance.
355 spin_lock(&local->client_conns_lock);
357 pp = &local->client_conns.rb_node;
361 conn = rb_entry(parent, struct rxrpc_connection, client_node);
363 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
366 cmp(security_level));
369 pp = &(*pp)->rb_left;
370 } else if (diff > 0) {
371 pp = &(*pp)->rb_right;
373 if (rxrpc_may_reuse_conn(conn) &&
374 rxrpc_get_connection_maybe(conn))
375 goto found_extant_conn;
376 /* The old connection is from an outdated epoch. */
377 _debug("replace conn");
378 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
379 rb_replace_node(&conn->client_node,
380 &candidate->client_node,
381 &local->client_conns);
382 trace_rxrpc_client(conn, -1, rxrpc_client_replace);
383 goto candidate_published;
388 rb_link_node(&candidate->client_node, parent, pp);
389 rb_insert_color(&candidate->client_node, &local->client_conns);
392 set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
393 call->conn = candidate;
394 call->security_ix = candidate->security_ix;
395 spin_unlock(&local->client_conns_lock);
396 _leave(" = 0 [new %d]", candidate->debug_id);
399 /* We come here if we found a suitable connection already in existence.
400 * Discard any candidate we may have allocated, and try to get a
401 * channel on this one.
404 _debug("found conn");
405 spin_unlock(&local->client_conns_lock);
408 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
409 rxrpc_put_connection(candidate);
413 spin_lock(&conn->channel_lock);
415 call->security_ix = conn->security_ix;
416 list_add(&call->chan_wait_link, &conn->waiting_calls);
417 spin_unlock(&conn->channel_lock);
418 _leave(" = 0 [extant %d]", conn->debug_id);
422 rxrpc_put_peer(cp->peer);
425 _leave(" = %d", ret);
430 * Activate a connection.
432 static void rxrpc_activate_conn(struct rxrpc_net *rxnet,
433 struct rxrpc_connection *conn)
435 trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
436 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
437 rxnet->nr_active_client_conns++;
438 list_move_tail(&conn->cache_link, &rxnet->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_net *rxnet,
454 struct rxrpc_connection *conn)
456 unsigned int nr_conns;
458 _enter("%d,%d", conn->debug_id, conn->cache_state);
460 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE)
463 spin_lock(&rxnet->client_conn_cache_lock);
465 nr_conns = rxnet->nr_client_conns;
466 if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
467 trace_rxrpc_client(conn, -1, rxrpc_client_count);
468 rxnet->nr_client_conns = nr_conns + 1;
471 switch (conn->cache_state) {
472 case RXRPC_CONN_CLIENT_ACTIVE:
473 case RXRPC_CONN_CLIENT_WAITING:
476 case RXRPC_CONN_CLIENT_INACTIVE:
477 case RXRPC_CONN_CLIENT_CULLED:
478 case RXRPC_CONN_CLIENT_IDLE:
479 if (nr_conns >= rxrpc_max_client_connections)
480 goto wait_for_capacity;
488 spin_unlock(&rxnet->client_conn_cache_lock);
490 _leave(" [%d]", conn->cache_state);
495 rxrpc_activate_conn(rxnet, conn);
500 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
501 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
502 list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns);
507 * Deactivate a channel.
509 static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
510 unsigned int channel)
512 struct rxrpc_channel *chan = &conn->channels[channel];
514 rcu_assign_pointer(chan->call, NULL);
515 conn->active_chans &= ~(1 << channel);
519 * Assign a channel to the call at the front of the queue and wake the call up.
520 * We don't increment the callNumber counter until this number has been exposed
523 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
524 unsigned int channel)
526 struct rxrpc_channel *chan = &conn->channels[channel];
527 struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
528 struct rxrpc_call, chan_wait_link);
529 u32 call_id = chan->call_counter + 1;
531 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
533 write_lock_bh(&call->state_lock);
534 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
535 write_unlock_bh(&call->state_lock);
537 rxrpc_see_call(call);
538 list_del_init(&call->chan_wait_link);
539 conn->active_chans |= 1 << channel;
540 call->peer = rxrpc_get_peer(conn->params.peer);
541 call->cid = conn->proto.cid | channel;
542 call->call_id = call_id;
544 trace_rxrpc_connect_call(call);
545 _net("CONNECT call %08x:%08x as call %d on conn %d",
546 call->cid, call->call_id, call->debug_id, conn->debug_id);
548 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
549 * orders cid and epoch in the connection wrt to call_id without the
550 * need to take the channel_lock.
552 * We provisionally assign a callNumber at this point, but we don't
553 * confirm it until the call is about to be exposed.
555 * TODO: Pair with a barrier in the data_ready handler when that looks
556 * at the call ID through a connection channel.
559 chan->call_id = call_id;
560 rcu_assign_pointer(chan->call, call);
561 wake_up(&call->waitq);
565 * Assign channels and callNumbers to waiting calls with channel_lock
568 static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
572 switch (conn->cache_state) {
573 case RXRPC_CONN_CLIENT_ACTIVE:
574 mask = RXRPC_ACTIVE_CHANS_MASK;
580 while (!list_empty(&conn->waiting_calls) &&
581 (avail = ~conn->active_chans,
584 rxrpc_activate_one_channel(conn, __ffs(avail));
588 * Assign channels and callNumbers to waiting calls.
590 static void rxrpc_activate_channels(struct rxrpc_connection *conn)
592 _enter("%d", conn->debug_id);
594 trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
596 if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
599 spin_lock(&conn->channel_lock);
600 rxrpc_activate_channels_locked(conn);
601 spin_unlock(&conn->channel_lock);
606 * Wait for a callNumber and a channel to be granted to a call.
608 static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
612 _enter("%d", call->debug_id);
614 if (!call->call_id) {
615 DECLARE_WAITQUEUE(myself, current);
617 if (!gfpflags_allow_blocking(gfp)) {
622 add_wait_queue_exclusive(&call->waitq, &myself);
624 set_current_state(TASK_INTERRUPTIBLE);
627 if (signal_pending(current)) {
633 remove_wait_queue(&call->waitq, &myself);
634 __set_current_state(TASK_RUNNING);
637 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
641 _leave(" = %d", ret);
646 * find a connection for a call
647 * - called in process context with IRQs enabled
649 int rxrpc_connect_call(struct rxrpc_call *call,
650 struct rxrpc_conn_parameters *cp,
651 struct sockaddr_rxrpc *srx,
654 struct rxrpc_net *rxnet = cp->local->rxnet;
657 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
659 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper.work);
660 rxrpc_cull_active_client_conns(rxnet);
662 ret = rxrpc_get_client_conn(call, cp, srx, gfp);
666 rxrpc_animate_client_conn(rxnet, call->conn);
667 rxrpc_activate_channels(call->conn);
669 ret = rxrpc_wait_for_channel(call, gfp);
671 rxrpc_disconnect_client_call(call);
673 _leave(" = %d", ret);
678 * Note that a connection is about to be exposed to the world. Once it is
679 * exposed, we maintain an extra ref on it that stops it from being summarily
680 * discarded before it's (a) had a chance to deal with retransmission and (b)
681 * had a chance at re-use (the per-connection security negotiation is
684 static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
685 unsigned int channel)
687 if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
688 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
689 rxrpc_get_connection(conn);
694 * Note that a call, and thus a connection, is about to be exposed to the
697 void rxrpc_expose_client_call(struct rxrpc_call *call)
699 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
700 struct rxrpc_connection *conn = call->conn;
701 struct rxrpc_channel *chan = &conn->channels[channel];
703 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
704 /* Mark the call ID as being used. If the callNumber counter
705 * exceeds ~2 billion, we kill the connection after its
706 * outstanding calls have finished so that the counter doesn't
709 chan->call_counter++;
710 if (chan->call_counter >= INT_MAX)
711 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
712 rxrpc_expose_client_conn(conn, channel);
717 * Disconnect a client call.
719 void rxrpc_disconnect_client_call(struct rxrpc_call *call)
721 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
722 struct rxrpc_connection *conn = call->conn;
723 struct rxrpc_channel *chan = &conn->channels[channel];
724 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&call->socket->sk));
726 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
729 spin_lock(&conn->channel_lock);
731 /* Calls that have never actually been assigned a channel can simply be
732 * discarded. If the conn didn't get used either, it will follow
733 * immediately unless someone else grabs it in the meantime.
735 if (!list_empty(&call->chan_wait_link)) {
736 _debug("call is waiting");
737 ASSERTCMP(call->call_id, ==, 0);
738 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
739 list_del_init(&call->chan_wait_link);
741 trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
743 /* We must deactivate or idle the connection if it's now
744 * waiting for nothing.
746 spin_lock(&rxnet->client_conn_cache_lock);
747 if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
748 list_empty(&conn->waiting_calls) &&
750 goto idle_connection;
754 ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
756 /* If a client call was exposed to the world, we save the result for
759 * We use a barrier here so that the call number and abort code can be
760 * read without needing to take a lock.
762 * TODO: Make the incoming packet handler check this and handle
763 * terminal retransmission without requiring access to the call.
765 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
766 _debug("exposed %u,%u", call->call_id, call->abort_code);
767 __rxrpc_disconnect_call(conn, call);
770 /* See if we can pass the channel directly to another call. */
771 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
772 !list_empty(&conn->waiting_calls)) {
773 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
774 rxrpc_activate_one_channel(conn, channel);
778 /* Things are more complex and we need the cache lock. We might be
779 * able to simply idle the conn or it might now be lurking on the wait
780 * list. It might even get moved back to the active list whilst we're
781 * waiting for the lock.
783 spin_lock(&rxnet->client_conn_cache_lock);
785 switch (conn->cache_state) {
786 case RXRPC_CONN_CLIENT_ACTIVE:
787 if (list_empty(&conn->waiting_calls)) {
788 rxrpc_deactivate_one_channel(conn, channel);
789 if (!conn->active_chans) {
790 rxnet->nr_active_client_conns--;
791 goto idle_connection;
796 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
797 rxrpc_activate_one_channel(conn, channel);
800 case RXRPC_CONN_CLIENT_CULLED:
801 rxrpc_deactivate_one_channel(conn, channel);
802 ASSERT(list_empty(&conn->waiting_calls));
803 if (!conn->active_chans)
804 goto idle_connection;
807 case RXRPC_CONN_CLIENT_WAITING:
808 rxrpc_deactivate_one_channel(conn, channel);
816 spin_unlock(&rxnet->client_conn_cache_lock);
818 spin_unlock(&conn->channel_lock);
819 rxrpc_put_connection(conn);
824 /* As no channels remain active, the connection gets deactivated
825 * immediately or moved to the idle list for a short while.
827 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
828 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
829 conn->idle_timestamp = jiffies;
830 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
831 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
832 if (rxnet->idle_client_conns.next == &conn->cache_link &&
833 !rxnet->kill_all_client_conns)
834 queue_delayed_work(rxrpc_workqueue,
835 &rxnet->client_conn_reaper,
836 rxrpc_conn_idle_client_expiry);
838 trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
839 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
840 list_del_init(&conn->cache_link);
846 * Clean up a dead client connection.
848 static struct rxrpc_connection *
849 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
851 struct rxrpc_connection *next = NULL;
852 struct rxrpc_local *local = conn->params.local;
853 struct rxrpc_net *rxnet = local->rxnet;
854 unsigned int nr_conns;
856 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
858 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
859 spin_lock(&local->client_conns_lock);
860 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
862 rb_erase(&conn->client_node, &local->client_conns);
863 spin_unlock(&local->client_conns_lock);
866 rxrpc_put_client_connection_id(conn);
868 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
870 if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
871 trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
872 spin_lock(&rxnet->client_conn_cache_lock);
873 nr_conns = --rxnet->nr_client_conns;
875 if (nr_conns < rxrpc_max_client_connections &&
876 !list_empty(&rxnet->waiting_client_conns)) {
877 next = list_entry(rxnet->waiting_client_conns.next,
878 struct rxrpc_connection, cache_link);
879 rxrpc_get_connection(next);
880 rxrpc_activate_conn(rxnet, next);
883 spin_unlock(&rxnet->client_conn_cache_lock);
886 rxrpc_kill_connection(conn);
888 rxrpc_activate_channels(next);
890 /* We need to get rid of the temporary ref we took upon next, but we
891 * can't call rxrpc_put_connection() recursively.
897 * Clean up a dead client connections.
899 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
901 const void *here = __builtin_return_address(0);
905 n = atomic_dec_return(&conn->usage);
906 trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
911 conn = rxrpc_put_one_client_conn(conn);
916 * Kill the longest-active client connections to make room for new ones.
918 static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
920 struct rxrpc_connection *conn;
921 unsigned int nr_conns = rxnet->nr_client_conns;
922 unsigned int nr_active, limit;
926 ASSERTCMP(nr_conns, >=, 0);
927 if (nr_conns < rxrpc_max_client_connections) {
931 limit = rxrpc_reap_client_connections;
933 spin_lock(&rxnet->client_conn_cache_lock);
934 nr_active = rxnet->nr_active_client_conns;
936 while (nr_active > limit) {
937 ASSERT(!list_empty(&rxnet->active_client_conns));
938 conn = list_entry(rxnet->active_client_conns.next,
939 struct rxrpc_connection, cache_link);
940 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
942 if (list_empty(&conn->waiting_calls)) {
943 trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
944 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
945 list_del_init(&conn->cache_link);
947 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
948 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
949 list_move_tail(&conn->cache_link,
950 &rxnet->waiting_client_conns);
956 rxnet->nr_active_client_conns = nr_active;
957 spin_unlock(&rxnet->client_conn_cache_lock);
958 ASSERTCMP(nr_active, >=, 0);
963 * Discard expired client connections from the idle list. Each conn in the
964 * idle list has been exposed and holds an extra ref because of that.
966 * This may be called from conn setup or from a work item so cannot be
967 * considered non-reentrant.
969 void rxrpc_discard_expired_client_conns(struct work_struct *work)
971 struct rxrpc_connection *conn;
972 struct rxrpc_net *rxnet =
973 container_of(to_delayed_work(work),
974 struct rxrpc_net, client_conn_reaper);
975 unsigned long expiry, conn_expires_at, now;
976 unsigned int nr_conns;
977 bool did_discard = false;
981 if (list_empty(&rxnet->idle_client_conns)) {
986 /* Don't double up on the discarding */
987 if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
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 = rxnet->nr_client_conns;
998 spin_lock(&rxnet->client_conn_cache_lock);
1000 if (list_empty(&rxnet->idle_client_conns))
1003 conn = list_entry(rxnet->idle_client_conns.next,
1004 struct rxrpc_connection, cache_link);
1005 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
1007 if (!rxnet->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(&rxnet->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 (!rxnet->kill_all_client_conns)
1051 queue_delayed_work(rxrpc_workqueue,
1052 &rxnet->client_conn_reaper,
1053 conn_expires_at - now);
1056 spin_unlock(&rxnet->client_conn_cache_lock);
1057 spin_unlock(&rxnet->client_conn_discard_lock);
1062 * Preemptively destroy all the client connection records rather than waiting
1063 * for them to time out
1065 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1069 spin_lock(&rxnet->client_conn_cache_lock);
1070 rxnet->kill_all_client_conns = true;
1071 spin_unlock(&rxnet->client_conn_cache_lock);
1073 cancel_delayed_work(&rxnet->client_conn_reaper);
1075 if (!queue_delayed_work(rxrpc_workqueue, &rxnet->client_conn_reaper, 0))
1076 _debug("destroy: queue failed");