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
);
108 const char rxrpc_conn_cache_states
[RXRPC_CONN__NR_CACHE_STATES
][5] = {
109 [RXRPC_CONN_CLIENT_INACTIVE
] = "Inac",
110 [RXRPC_CONN_CLIENT_WAITING
] = "Wait",
111 [RXRPC_CONN_CLIENT_ACTIVE
] = "Actv",
112 [RXRPC_CONN_CLIENT_CULLED
] = "Cull",
113 [RXRPC_CONN_CLIENT_IDLE
] = "Idle",
117 * Get a connection ID and epoch for a client connection from the global pool.
118 * The connection struct pointer is then recorded in the idr radix tree. The
119 * epoch doesn't change until the client is rebooted (or, at least, unless the
120 * module is unloaded).
122 static int rxrpc_get_client_connection_id(struct rxrpc_connection
*conn
,
130 spin_lock(&rxrpc_conn_id_lock
);
132 id
= idr_alloc_cyclic(&rxrpc_client_conn_ids
, conn
,
133 1, 0x40000000, GFP_NOWAIT
);
137 spin_unlock(&rxrpc_conn_id_lock
);
140 conn
->proto
.epoch
= rxrpc_epoch
;
141 conn
->proto
.cid
= id
<< RXRPC_CIDSHIFT
;
142 set_bit(RXRPC_CONN_HAS_IDR
, &conn
->flags
);
143 _leave(" [CID %x]", conn
->proto
.cid
);
147 spin_unlock(&rxrpc_conn_id_lock
);
154 * Release a connection ID for a client connection from the global pool.
156 static void rxrpc_put_client_connection_id(struct rxrpc_connection
*conn
)
158 if (test_bit(RXRPC_CONN_HAS_IDR
, &conn
->flags
)) {
159 spin_lock(&rxrpc_conn_id_lock
);
160 idr_remove(&rxrpc_client_conn_ids
,
161 conn
->proto
.cid
>> RXRPC_CIDSHIFT
);
162 spin_unlock(&rxrpc_conn_id_lock
);
167 * Destroy the client connection ID tree.
169 void rxrpc_destroy_client_conn_ids(void)
171 struct rxrpc_connection
*conn
;
174 if (!idr_is_empty(&rxrpc_client_conn_ids
)) {
175 idr_for_each_entry(&rxrpc_client_conn_ids
, conn
, id
) {
176 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
177 conn
, atomic_read(&conn
->usage
));
182 idr_destroy(&rxrpc_client_conn_ids
);
186 * Allocate a client connection.
188 static struct rxrpc_connection
*
189 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters
*cp
, gfp_t gfp
)
191 struct rxrpc_connection
*conn
;
196 conn
= rxrpc_alloc_connection(gfp
);
198 _leave(" = -ENOMEM");
199 return ERR_PTR(-ENOMEM
);
202 atomic_set(&conn
->usage
, 1);
204 __set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
207 conn
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
208 conn
->state
= RXRPC_CONN_CLIENT
;
210 ret
= rxrpc_get_client_connection_id(conn
, gfp
);
214 ret
= rxrpc_init_client_conn_security(conn
);
218 ret
= conn
->security
->prime_packet_security(conn
);
222 write_lock(&rxrpc_connection_lock
);
223 list_add_tail(&conn
->proc_link
, &rxrpc_connection_proc_list
);
224 write_unlock(&rxrpc_connection_lock
);
226 /* We steal the caller's peer ref. */
228 rxrpc_get_local(conn
->params
.local
);
229 key_get(conn
->params
.key
);
231 trace_rxrpc_conn(conn
, rxrpc_conn_new_client
, atomic_read(&conn
->usage
),
232 __builtin_return_address(0));
233 trace_rxrpc_client(conn
, -1, rxrpc_client_alloc
);
234 _leave(" = %p", conn
);
238 conn
->security
->clear(conn
);
240 rxrpc_put_client_connection_id(conn
);
243 _leave(" = %d", ret
);
248 * Determine if a connection may be reused.
250 static bool rxrpc_may_reuse_conn(struct rxrpc_connection
*conn
)
252 int id_cursor
, id
, distance
, limit
;
254 if (test_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
))
257 if (conn
->proto
.epoch
!= rxrpc_epoch
)
258 goto mark_dont_reuse
;
260 /* The IDR tree gets very expensive on memory if the connection IDs are
261 * widely scattered throughout the number space, so we shall want to
262 * kill off connections that, say, have an ID more than about four
263 * times the maximum number of client conns away from the current
264 * allocation point to try and keep the IDs concentrated.
266 id_cursor
= idr_get_cursor(&rxrpc_client_conn_ids
);
267 id
= conn
->proto
.cid
>> RXRPC_CIDSHIFT
;
268 distance
= id
- id_cursor
;
270 distance
= -distance
;
271 limit
= max(rxrpc_max_client_connections
* 4, 1024U);
272 if (distance
> limit
)
273 goto mark_dont_reuse
;
278 set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
284 * Create or find a client connection to use for a call.
286 * If we return with a connection, the call will be on its waiting list. It's
287 * left to the caller to assign a channel and wake up the call.
289 static int rxrpc_get_client_conn(struct rxrpc_call
*call
,
290 struct rxrpc_conn_parameters
*cp
,
291 struct sockaddr_rxrpc
*srx
,
294 struct rxrpc_connection
*conn
, *candidate
= NULL
;
295 struct rxrpc_local
*local
= cp
->local
;
296 struct rb_node
*p
, **pp
, *parent
;
300 _enter("{%d,%lx},", call
->debug_id
, call
->user_call_ID
);
302 cp
->peer
= rxrpc_lookup_peer(cp
->local
, srx
, gfp
);
306 /* If the connection is not meant to be exclusive, search the available
307 * connections to see if the connection we want to use already exists.
309 if (!cp
->exclusive
) {
311 spin_lock(&local
->client_conns_lock
);
312 p
= local
->client_conns
.rb_node
;
314 conn
= rb_entry(p
, struct rxrpc_connection
, client_node
);
316 #define cmp(X) ((long)conn->params.X - (long)cp->X)
319 cmp(security_level
));
323 } else if (diff
> 0) {
326 if (rxrpc_may_reuse_conn(conn
) &&
327 rxrpc_get_connection_maybe(conn
))
328 goto found_extant_conn
;
329 /* The connection needs replacing. It's better
330 * to effect that when we have something to
331 * replace it with so that we don't have to
332 * rebalance the tree twice.
337 spin_unlock(&local
->client_conns_lock
);
340 /* There wasn't a connection yet or we need an exclusive connection.
341 * We need to create a candidate and then potentially redo the search
342 * in case we're racing with another thread also trying to connect on a
343 * shareable connection.
346 candidate
= rxrpc_alloc_client_connection(cp
, gfp
);
347 if (IS_ERR(candidate
)) {
348 ret
= PTR_ERR(candidate
);
352 /* Add the call to the new connection's waiting list in case we're
353 * going to have to wait for the connection to come live. It's our
354 * connection, so we want first dibs on the channel slots. We would
355 * normally have to take channel_lock but we do this before anyone else
356 * can see the connection.
358 list_add_tail(&call
->chan_wait_link
, &candidate
->waiting_calls
);
361 call
->conn
= candidate
;
362 call
->security_ix
= candidate
->security_ix
;
363 _leave(" = 0 [exclusive %d]", candidate
->debug_id
);
367 /* Publish the new connection for userspace to find. We need to redo
368 * the search before doing this lest we race with someone else adding a
369 * conflicting instance.
372 spin_lock(&local
->client_conns_lock
);
374 pp
= &local
->client_conns
.rb_node
;
378 conn
= rb_entry(parent
, struct rxrpc_connection
, client_node
);
380 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
383 cmp(security_level
));
386 pp
= &(*pp
)->rb_left
;
387 } else if (diff
> 0) {
388 pp
= &(*pp
)->rb_right
;
390 if (rxrpc_may_reuse_conn(conn
) &&
391 rxrpc_get_connection_maybe(conn
))
392 goto found_extant_conn
;
393 /* The old connection is from an outdated epoch. */
394 _debug("replace conn");
395 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &conn
->flags
);
396 rb_replace_node(&conn
->client_node
,
397 &candidate
->client_node
,
398 &local
->client_conns
);
399 trace_rxrpc_client(conn
, -1, rxrpc_client_replace
);
400 goto candidate_published
;
405 rb_link_node(&candidate
->client_node
, parent
, pp
);
406 rb_insert_color(&candidate
->client_node
, &local
->client_conns
);
409 set_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &candidate
->flags
);
410 call
->conn
= candidate
;
411 call
->security_ix
= candidate
->security_ix
;
412 spin_unlock(&local
->client_conns_lock
);
413 _leave(" = 0 [new %d]", candidate
->debug_id
);
416 /* We come here if we found a suitable connection already in existence.
417 * Discard any candidate we may have allocated, and try to get a
418 * channel on this one.
421 _debug("found conn");
422 spin_unlock(&local
->client_conns_lock
);
425 trace_rxrpc_client(candidate
, -1, rxrpc_client_duplicate
);
426 rxrpc_put_connection(candidate
);
430 spin_lock(&conn
->channel_lock
);
432 call
->security_ix
= conn
->security_ix
;
433 list_add(&call
->chan_wait_link
, &conn
->waiting_calls
);
434 spin_unlock(&conn
->channel_lock
);
435 _leave(" = 0 [extant %d]", conn
->debug_id
);
439 rxrpc_put_peer(cp
->peer
);
442 _leave(" = %d", ret
);
447 * Activate a connection.
449 static void rxrpc_activate_conn(struct rxrpc_connection
*conn
)
451 trace_rxrpc_client(conn
, -1, rxrpc_client_to_active
);
452 conn
->cache_state
= RXRPC_CONN_CLIENT_ACTIVE
;
453 rxrpc_nr_active_client_conns
++;
454 list_move_tail(&conn
->cache_link
, &rxrpc_active_client_conns
);
458 * Attempt to animate a connection for a new call.
460 * If it's not exclusive, the connection is in the endpoint tree, and we're in
461 * the conn's list of those waiting to grab a channel. There is, however, a
462 * limit on the number of live connections allowed at any one time, so we may
463 * have to wait for capacity to become available.
465 * Note that a connection on the waiting queue might *also* have active
466 * channels if it has been culled to make space and then re-requested by a new
469 static void rxrpc_animate_client_conn(struct rxrpc_connection
*conn
)
471 unsigned int nr_conns
;
473 _enter("%d,%d", conn
->debug_id
, conn
->cache_state
);
475 if (conn
->cache_state
== RXRPC_CONN_CLIENT_ACTIVE
)
478 spin_lock(&rxrpc_client_conn_cache_lock
);
480 nr_conns
= rxrpc_nr_client_conns
;
481 if (!test_and_set_bit(RXRPC_CONN_COUNTED
, &conn
->flags
)) {
482 trace_rxrpc_client(conn
, -1, rxrpc_client_count
);
483 rxrpc_nr_client_conns
= nr_conns
+ 1;
486 switch (conn
->cache_state
) {
487 case RXRPC_CONN_CLIENT_ACTIVE
:
488 case RXRPC_CONN_CLIENT_WAITING
:
491 case RXRPC_CONN_CLIENT_INACTIVE
:
492 case RXRPC_CONN_CLIENT_CULLED
:
493 case RXRPC_CONN_CLIENT_IDLE
:
494 if (nr_conns
>= rxrpc_max_client_connections
)
495 goto wait_for_capacity
;
503 spin_unlock(&rxrpc_client_conn_cache_lock
);
505 _leave(" [%d]", conn
->cache_state
);
510 rxrpc_activate_conn(conn
);
515 trace_rxrpc_client(conn
, -1, rxrpc_client_to_waiting
);
516 conn
->cache_state
= RXRPC_CONN_CLIENT_WAITING
;
517 list_move_tail(&conn
->cache_link
, &rxrpc_waiting_client_conns
);
522 * Deactivate a channel.
524 static void rxrpc_deactivate_one_channel(struct rxrpc_connection
*conn
,
525 unsigned int channel
)
527 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
529 rcu_assign_pointer(chan
->call
, NULL
);
530 conn
->active_chans
&= ~(1 << channel
);
534 * Assign a channel to the call at the front of the queue and wake the call up.
535 * We don't increment the callNumber counter until this number has been exposed
538 static void rxrpc_activate_one_channel(struct rxrpc_connection
*conn
,
539 unsigned int channel
)
541 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
542 struct rxrpc_call
*call
= list_entry(conn
->waiting_calls
.next
,
543 struct rxrpc_call
, chan_wait_link
);
544 u32 call_id
= chan
->call_counter
+ 1;
546 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_activate
);
548 write_lock_bh(&call
->state_lock
);
549 call
->state
= RXRPC_CALL_CLIENT_SEND_REQUEST
;
550 write_unlock_bh(&call
->state_lock
);
552 rxrpc_see_call(call
);
553 list_del_init(&call
->chan_wait_link
);
554 conn
->active_chans
|= 1 << channel
;
555 call
->peer
= rxrpc_get_peer(conn
->params
.peer
);
556 call
->cid
= conn
->proto
.cid
| channel
;
557 call
->call_id
= call_id
;
559 _net("CONNECT call %08x:%08x as call %d on conn %d",
560 call
->cid
, call
->call_id
, call
->debug_id
, conn
->debug_id
);
562 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
563 * orders cid and epoch in the connection wrt to call_id without the
564 * need to take the channel_lock.
566 * We provisionally assign a callNumber at this point, but we don't
567 * confirm it until the call is about to be exposed.
569 * TODO: Pair with a barrier in the data_ready handler when that looks
570 * at the call ID through a connection channel.
573 chan
->call_id
= call_id
;
574 rcu_assign_pointer(chan
->call
, call
);
575 wake_up(&call
->waitq
);
579 * Assign channels and callNumbers to waiting calls with channel_lock
582 static void rxrpc_activate_channels_locked(struct rxrpc_connection
*conn
)
586 switch (conn
->cache_state
) {
587 case RXRPC_CONN_CLIENT_ACTIVE
:
588 mask
= RXRPC_ACTIVE_CHANS_MASK
;
594 while (!list_empty(&conn
->waiting_calls
) &&
595 (avail
= ~conn
->active_chans
,
598 rxrpc_activate_one_channel(conn
, __ffs(avail
));
602 * Assign channels and callNumbers to waiting calls.
604 static void rxrpc_activate_channels(struct rxrpc_connection
*conn
)
606 _enter("%d", conn
->debug_id
);
608 trace_rxrpc_client(conn
, -1, rxrpc_client_activate_chans
);
610 if (conn
->active_chans
== RXRPC_ACTIVE_CHANS_MASK
)
613 spin_lock(&conn
->channel_lock
);
614 rxrpc_activate_channels_locked(conn
);
615 spin_unlock(&conn
->channel_lock
);
620 * Wait for a callNumber and a channel to be granted to a call.
622 static int rxrpc_wait_for_channel(struct rxrpc_call
*call
, gfp_t gfp
)
626 _enter("%d", call
->debug_id
);
628 if (!call
->call_id
) {
629 DECLARE_WAITQUEUE(myself
, current
);
631 if (!gfpflags_allow_blocking(gfp
)) {
636 add_wait_queue_exclusive(&call
->waitq
, &myself
);
638 set_current_state(TASK_INTERRUPTIBLE
);
641 if (signal_pending(current
)) {
647 remove_wait_queue(&call
->waitq
, &myself
);
648 __set_current_state(TASK_RUNNING
);
651 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
655 _leave(" = %d", ret
);
660 * find a connection for a call
661 * - called in process context with IRQs enabled
663 int rxrpc_connect_call(struct rxrpc_call
*call
,
664 struct rxrpc_conn_parameters
*cp
,
665 struct sockaddr_rxrpc
*srx
,
670 _enter("{%d,%lx},", call
->debug_id
, call
->user_call_ID
);
672 rxrpc_discard_expired_client_conns(NULL
);
673 rxrpc_cull_active_client_conns();
675 ret
= rxrpc_get_client_conn(call
, cp
, srx
, gfp
);
679 rxrpc_animate_client_conn(call
->conn
);
680 rxrpc_activate_channels(call
->conn
);
682 ret
= rxrpc_wait_for_channel(call
, gfp
);
684 rxrpc_disconnect_client_call(call
);
686 _leave(" = %d", ret
);
691 * Note that a connection is about to be exposed to the world. Once it is
692 * exposed, we maintain an extra ref on it that stops it from being summarily
693 * discarded before it's (a) had a chance to deal with retransmission and (b)
694 * had a chance at re-use (the per-connection security negotiation is
697 static void rxrpc_expose_client_conn(struct rxrpc_connection
*conn
,
698 unsigned int channel
)
700 if (!test_and_set_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
)) {
701 trace_rxrpc_client(conn
, channel
, rxrpc_client_exposed
);
702 rxrpc_get_connection(conn
);
707 * Note that a call, and thus a connection, is about to be exposed to the
710 void rxrpc_expose_client_call(struct rxrpc_call
*call
)
712 unsigned int channel
= call
->cid
& RXRPC_CHANNELMASK
;
713 struct rxrpc_connection
*conn
= call
->conn
;
714 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
716 if (!test_and_set_bit(RXRPC_CALL_EXPOSED
, &call
->flags
)) {
717 /* Mark the call ID as being used. If the callNumber counter
718 * exceeds ~2 billion, we kill the connection after its
719 * outstanding calls have finished so that the counter doesn't
722 chan
->call_counter
++;
723 if (chan
->call_counter
>= INT_MAX
)
724 set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
725 rxrpc_expose_client_conn(conn
, channel
);
730 * Disconnect a client call.
732 void rxrpc_disconnect_client_call(struct rxrpc_call
*call
)
734 unsigned int channel
= call
->cid
& RXRPC_CHANNELMASK
;
735 struct rxrpc_connection
*conn
= call
->conn
;
736 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
738 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_disconnect
);
741 spin_lock(&conn
->channel_lock
);
743 /* Calls that have never actually been assigned a channel can simply be
744 * discarded. If the conn didn't get used either, it will follow
745 * immediately unless someone else grabs it in the meantime.
747 if (!list_empty(&call
->chan_wait_link
)) {
748 _debug("call is waiting");
749 ASSERTCMP(call
->call_id
, ==, 0);
750 ASSERT(!test_bit(RXRPC_CALL_EXPOSED
, &call
->flags
));
751 list_del_init(&call
->chan_wait_link
);
753 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_unstarted
);
755 /* We must deactivate or idle the connection if it's now
756 * waiting for nothing.
758 spin_lock(&rxrpc_client_conn_cache_lock
);
759 if (conn
->cache_state
== RXRPC_CONN_CLIENT_WAITING
&&
760 list_empty(&conn
->waiting_calls
) &&
762 goto idle_connection
;
766 ASSERTCMP(rcu_access_pointer(chan
->call
), ==, call
);
768 /* If a client call was exposed to the world, we save the result for
771 * We use a barrier here so that the call number and abort code can be
772 * read without needing to take a lock.
774 * TODO: Make the incoming packet handler check this and handle
775 * terminal retransmission without requiring access to the call.
777 if (test_bit(RXRPC_CALL_EXPOSED
, &call
->flags
)) {
778 _debug("exposed %u,%u", call
->call_id
, call
->abort_code
);
779 __rxrpc_disconnect_call(conn
, call
);
782 /* See if we can pass the channel directly to another call. */
783 if (conn
->cache_state
== RXRPC_CONN_CLIENT_ACTIVE
&&
784 !list_empty(&conn
->waiting_calls
)) {
785 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_pass
);
786 rxrpc_activate_one_channel(conn
, channel
);
790 /* Things are more complex and we need the cache lock. We might be
791 * able to simply idle the conn or it might now be lurking on the wait
792 * list. It might even get moved back to the active list whilst we're
793 * waiting for the lock.
795 spin_lock(&rxrpc_client_conn_cache_lock
);
797 switch (conn
->cache_state
) {
798 case RXRPC_CONN_CLIENT_ACTIVE
:
799 if (list_empty(&conn
->waiting_calls
)) {
800 rxrpc_deactivate_one_channel(conn
, channel
);
801 if (!conn
->active_chans
) {
802 rxrpc_nr_active_client_conns
--;
803 goto idle_connection
;
808 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_pass
);
809 rxrpc_activate_one_channel(conn
, channel
);
812 case RXRPC_CONN_CLIENT_CULLED
:
813 rxrpc_deactivate_one_channel(conn
, channel
);
814 ASSERT(list_empty(&conn
->waiting_calls
));
815 if (!conn
->active_chans
)
816 goto idle_connection
;
819 case RXRPC_CONN_CLIENT_WAITING
:
820 rxrpc_deactivate_one_channel(conn
, channel
);
828 spin_unlock(&rxrpc_client_conn_cache_lock
);
830 spin_unlock(&conn
->channel_lock
);
831 rxrpc_put_connection(conn
);
836 /* As no channels remain active, the connection gets deactivated
837 * immediately or moved to the idle list for a short while.
839 if (test_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
)) {
840 trace_rxrpc_client(conn
, channel
, rxrpc_client_to_idle
);
841 conn
->idle_timestamp
= jiffies
;
842 conn
->cache_state
= RXRPC_CONN_CLIENT_IDLE
;
843 list_move_tail(&conn
->cache_link
, &rxrpc_idle_client_conns
);
844 if (rxrpc_idle_client_conns
.next
== &conn
->cache_link
&&
845 !rxrpc_kill_all_client_conns
)
846 queue_delayed_work(rxrpc_workqueue
,
847 &rxrpc_client_conn_reap
,
848 rxrpc_conn_idle_client_expiry
);
850 trace_rxrpc_client(conn
, channel
, rxrpc_client_to_inactive
);
851 conn
->cache_state
= RXRPC_CONN_CLIENT_INACTIVE
;
852 list_del_init(&conn
->cache_link
);
858 * Clean up a dead client connection.
860 static struct rxrpc_connection
*
861 rxrpc_put_one_client_conn(struct rxrpc_connection
*conn
)
863 struct rxrpc_connection
*next
= NULL
;
864 struct rxrpc_local
*local
= conn
->params
.local
;
865 unsigned int nr_conns
;
867 trace_rxrpc_client(conn
, -1, rxrpc_client_cleanup
);
869 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &conn
->flags
)) {
870 spin_lock(&local
->client_conns_lock
);
871 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS
,
873 rb_erase(&conn
->client_node
, &local
->client_conns
);
874 spin_unlock(&local
->client_conns_lock
);
877 rxrpc_put_client_connection_id(conn
);
879 ASSERTCMP(conn
->cache_state
, ==, RXRPC_CONN_CLIENT_INACTIVE
);
881 if (test_bit(RXRPC_CONN_COUNTED
, &conn
->flags
)) {
882 trace_rxrpc_client(conn
, -1, rxrpc_client_uncount
);
883 spin_lock(&rxrpc_client_conn_cache_lock
);
884 nr_conns
= --rxrpc_nr_client_conns
;
886 if (nr_conns
< rxrpc_max_client_connections
&&
887 !list_empty(&rxrpc_waiting_client_conns
)) {
888 next
= list_entry(rxrpc_waiting_client_conns
.next
,
889 struct rxrpc_connection
, cache_link
);
890 rxrpc_get_connection(next
);
891 rxrpc_activate_conn(next
);
894 spin_unlock(&rxrpc_client_conn_cache_lock
);
897 rxrpc_kill_connection(conn
);
899 rxrpc_activate_channels(next
);
901 /* We need to get rid of the temporary ref we took upon next, but we
902 * can't call rxrpc_put_connection() recursively.
908 * Clean up a dead client connections.
910 void rxrpc_put_client_conn(struct rxrpc_connection
*conn
)
912 const void *here
= __builtin_return_address(0);
916 n
= atomic_dec_return(&conn
->usage
);
917 trace_rxrpc_conn(conn
, rxrpc_conn_put_client
, n
, here
);
922 conn
= rxrpc_put_one_client_conn(conn
);
927 * Kill the longest-active client connections to make room for new ones.
929 static void rxrpc_cull_active_client_conns(void)
931 struct rxrpc_connection
*conn
;
932 unsigned int nr_conns
= rxrpc_nr_client_conns
;
933 unsigned int nr_active
, limit
;
937 ASSERTCMP(nr_conns
, >=, 0);
938 if (nr_conns
< rxrpc_max_client_connections
) {
942 limit
= rxrpc_reap_client_connections
;
944 spin_lock(&rxrpc_client_conn_cache_lock
);
945 nr_active
= rxrpc_nr_active_client_conns
;
947 while (nr_active
> limit
) {
948 ASSERT(!list_empty(&rxrpc_active_client_conns
));
949 conn
= list_entry(rxrpc_active_client_conns
.next
,
950 struct rxrpc_connection
, cache_link
);
951 ASSERTCMP(conn
->cache_state
, ==, RXRPC_CONN_CLIENT_ACTIVE
);
953 if (list_empty(&conn
->waiting_calls
)) {
954 trace_rxrpc_client(conn
, -1, rxrpc_client_to_culled
);
955 conn
->cache_state
= RXRPC_CONN_CLIENT_CULLED
;
956 list_del_init(&conn
->cache_link
);
958 trace_rxrpc_client(conn
, -1, rxrpc_client_to_waiting
);
959 conn
->cache_state
= RXRPC_CONN_CLIENT_WAITING
;
960 list_move_tail(&conn
->cache_link
,
961 &rxrpc_waiting_client_conns
);
967 rxrpc_nr_active_client_conns
= nr_active
;
968 spin_unlock(&rxrpc_client_conn_cache_lock
);
969 ASSERTCMP(nr_active
, >=, 0);
974 * Discard expired client connections from the idle list. Each conn in the
975 * idle list has been exposed and holds an extra ref because of that.
977 * This may be called from conn setup or from a work item so cannot be
978 * considered non-reentrant.
980 static void rxrpc_discard_expired_client_conns(struct work_struct
*work
)
982 struct rxrpc_connection
*conn
;
983 unsigned long expiry
, conn_expires_at
, now
;
984 unsigned int nr_conns
;
985 bool did_discard
= false;
987 _enter("%c", work
? 'w' : 'n');
989 if (list_empty(&rxrpc_idle_client_conns
)) {
994 /* Don't double up on the discarding */
995 if (!spin_trylock(&rxrpc_client_conn_discard_mutex
)) {
996 _leave(" [already]");
1000 /* We keep an estimate of what the number of conns ought to be after
1001 * we've discarded some so that we don't overdo the discarding.
1003 nr_conns
= rxrpc_nr_client_conns
;
1006 spin_lock(&rxrpc_client_conn_cache_lock
);
1008 if (list_empty(&rxrpc_idle_client_conns
))
1011 conn
= list_entry(rxrpc_idle_client_conns
.next
,
1012 struct rxrpc_connection
, cache_link
);
1013 ASSERT(test_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
));
1015 if (!rxrpc_kill_all_client_conns
) {
1016 /* If the number of connections is over the reap limit, we
1017 * expedite discard by reducing the expiry timeout. We must,
1018 * however, have at least a short grace period to be able to do
1019 * final-ACK or ABORT retransmission.
1021 expiry
= rxrpc_conn_idle_client_expiry
;
1022 if (nr_conns
> rxrpc_reap_client_connections
)
1023 expiry
= rxrpc_conn_idle_client_fast_expiry
;
1025 conn_expires_at
= conn
->idle_timestamp
+ expiry
;
1027 now
= READ_ONCE(jiffies
);
1028 if (time_after(conn_expires_at
, now
))
1029 goto not_yet_expired
;
1032 trace_rxrpc_client(conn
, -1, rxrpc_client_discard
);
1033 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
))
1035 conn
->cache_state
= RXRPC_CONN_CLIENT_INACTIVE
;
1036 list_del_init(&conn
->cache_link
);
1038 spin_unlock(&rxrpc_client_conn_cache_lock
);
1040 /* When we cleared the EXPOSED flag, we took on responsibility for the
1041 * reference that that had on the usage count. We deal with that here.
1042 * If someone re-sets the flag and re-gets the ref, that's fine.
1044 rxrpc_put_connection(conn
);
1050 /* The connection at the front of the queue hasn't yet expired, so
1051 * schedule the work item for that point if we discarded something.
1053 * We don't worry if the work item is already scheduled - it can look
1054 * after rescheduling itself at a later time. We could cancel it, but
1055 * then things get messier.
1058 if (!rxrpc_kill_all_client_conns
)
1059 queue_delayed_work(rxrpc_workqueue
,
1060 &rxrpc_client_conn_reap
,
1061 conn_expires_at
- now
);
1064 spin_unlock(&rxrpc_client_conn_cache_lock
);
1065 spin_unlock(&rxrpc_client_conn_discard_mutex
);
1070 * Preemptively destroy all the client connection records rather than waiting
1071 * for them to time out
1073 void __exit
rxrpc_destroy_all_client_connections(void)
1077 spin_lock(&rxrpc_client_conn_cache_lock
);
1078 rxrpc_kill_all_client_conns
= true;
1079 spin_unlock(&rxrpc_client_conn_cache_lock
);
1081 cancel_delayed_work(&rxrpc_client_conn_reap
);
1083 if (!queue_delayed_work(rxrpc_workqueue
, &rxrpc_client_conn_reap
, 0))
1084 _debug("destroy: queue failed");