1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Client connection-specific management code.
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * Client connections need to be cached for a little while after they've made a
8 * call so as to handle retransmitted DATA packets in case the server didn't
9 * receive the final ACK or terminating ABORT we sent it.
11 * Client connections can be in one of a number of cache states:
13 * (1) INACTIVE - The connection is not held in any list and may not have been
14 * exposed to the world. If it has been previously exposed, it was
15 * discarded from the idle list after expiring.
17 * (2) WAITING - The connection is waiting for the number of client conns to
18 * drop below the maximum capacity. Calls may be in progress upon it from
19 * when it was active and got culled.
21 * The connection is on the rxrpc_waiting_client_conns list which is kept
22 * in to-be-granted order. Culled conns with waiters go to the back of
23 * the queue just like new conns.
25 * (3) ACTIVE - The connection has at least one call in progress upon it, it
26 * may freely grant available channels to new calls and calls may be
27 * waiting on it for channels to become available.
29 * The connection is on the rxnet->active_client_conns list which is kept
30 * in activation order for culling purposes.
32 * rxrpc_nr_active_client_conns is held incremented also.
34 * (4) UPGRADE - As for ACTIVE, but only one call may be in progress and is
35 * being used to probe for service upgrade.
37 * (5) CULLED - The connection got summarily culled to try and free up
38 * capacity. Calls currently in progress on the connection are allowed to
39 * continue, but new calls will have to wait. There can be no waiters in
40 * this state - the conn would have to go to the WAITING state instead.
42 * (6) IDLE - The connection has no calls in progress upon it and must have
43 * been exposed to the world (ie. the EXPOSED flag must be set). When it
44 * expires, the EXPOSED flag is cleared and the connection transitions to
47 * The connection is on the rxnet->idle_client_conns list which is kept in
48 * order of how soon they'll expire.
50 * There are flags of relevance to the cache:
52 * (1) EXPOSED - The connection ID got exposed to the world. If this flag is
53 * set, an extra ref is added to the connection preventing it from being
54 * reaped when it has no calls outstanding. This flag is cleared and the
55 * ref dropped when a conn is discarded from the idle list.
57 * This allows us to move terminal call state retransmission to the
58 * connection and to discard the call immediately we think it is done
59 * with. It also give us a chance to reuse the connection.
61 * (2) DONT_REUSE - The connection should be discarded as soon as possible and
62 * should not be reused. This is set when an exclusive connection is used
63 * or a call ID counter overflows.
65 * The caching state may only be changed if the cache lock is held.
67 * There are two idle client connection expiry durations. If the total number
68 * of connections is below the reap threshold, we use the normal duration; if
69 * it's above, we use the fast duration.
72 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
74 #include <linux/slab.h>
75 #include <linux/idr.h>
76 #include <linux/timer.h>
77 #include <linux/sched/signal.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 long rxrpc_conn_idle_client_expiry
= 2 * 60 * HZ
;
84 __read_mostly
unsigned long rxrpc_conn_idle_client_fast_expiry
= 2 * HZ
;
87 * We use machine-unique IDs for our client connections.
89 DEFINE_IDR(rxrpc_client_conn_ids
);
90 static DEFINE_SPINLOCK(rxrpc_conn_id_lock
);
92 static void rxrpc_cull_active_client_conns(struct rxrpc_net
*);
95 * Get a connection ID and epoch for a client connection from the global pool.
96 * The connection struct pointer is then recorded in the idr radix tree. The
97 * epoch doesn't change until the client is rebooted (or, at least, unless the
98 * module is unloaded).
100 static int rxrpc_get_client_connection_id(struct rxrpc_connection
*conn
,
103 struct rxrpc_net
*rxnet
= conn
->params
.local
->rxnet
;
109 spin_lock(&rxrpc_conn_id_lock
);
111 id
= idr_alloc_cyclic(&rxrpc_client_conn_ids
, conn
,
112 1, 0x40000000, GFP_NOWAIT
);
116 spin_unlock(&rxrpc_conn_id_lock
);
119 conn
->proto
.epoch
= rxnet
->epoch
;
120 conn
->proto
.cid
= id
<< RXRPC_CIDSHIFT
;
121 set_bit(RXRPC_CONN_HAS_IDR
, &conn
->flags
);
122 _leave(" [CID %x]", conn
->proto
.cid
);
126 spin_unlock(&rxrpc_conn_id_lock
);
133 * Release a connection ID for a client connection from the global pool.
135 static void rxrpc_put_client_connection_id(struct rxrpc_connection
*conn
)
137 if (test_bit(RXRPC_CONN_HAS_IDR
, &conn
->flags
)) {
138 spin_lock(&rxrpc_conn_id_lock
);
139 idr_remove(&rxrpc_client_conn_ids
,
140 conn
->proto
.cid
>> RXRPC_CIDSHIFT
);
141 spin_unlock(&rxrpc_conn_id_lock
);
146 * Destroy the client connection ID tree.
148 void rxrpc_destroy_client_conn_ids(void)
150 struct rxrpc_connection
*conn
;
153 if (!idr_is_empty(&rxrpc_client_conn_ids
)) {
154 idr_for_each_entry(&rxrpc_client_conn_ids
, conn
, id
) {
155 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
156 conn
, atomic_read(&conn
->usage
));
161 idr_destroy(&rxrpc_client_conn_ids
);
165 * Allocate a client connection.
167 static struct rxrpc_connection
*
168 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters
*cp
, gfp_t gfp
)
170 struct rxrpc_connection
*conn
;
171 struct rxrpc_net
*rxnet
= cp
->local
->rxnet
;
176 conn
= rxrpc_alloc_connection(gfp
);
178 _leave(" = -ENOMEM");
179 return ERR_PTR(-ENOMEM
);
182 atomic_set(&conn
->usage
, 1);
184 __set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
186 __set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE
, &conn
->flags
);
189 conn
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
190 conn
->state
= RXRPC_CONN_CLIENT
;
191 conn
->service_id
= cp
->service_id
;
193 ret
= rxrpc_get_client_connection_id(conn
, gfp
);
197 ret
= rxrpc_init_client_conn_security(conn
);
201 ret
= conn
->security
->prime_packet_security(conn
);
205 atomic_inc(&rxnet
->nr_conns
);
206 write_lock(&rxnet
->conn_lock
);
207 list_add_tail(&conn
->proc_link
, &rxnet
->conn_proc_list
);
208 write_unlock(&rxnet
->conn_lock
);
210 /* We steal the caller's peer ref. */
212 rxrpc_get_local(conn
->params
.local
);
213 key_get(conn
->params
.key
);
215 trace_rxrpc_conn(conn
->debug_id
, rxrpc_conn_new_client
,
216 atomic_read(&conn
->usage
),
217 __builtin_return_address(0));
218 trace_rxrpc_client(conn
, -1, rxrpc_client_alloc
);
219 _leave(" = %p", conn
);
223 conn
->security
->clear(conn
);
225 rxrpc_put_client_connection_id(conn
);
228 _leave(" = %d", ret
);
233 * Determine if a connection may be reused.
235 static bool rxrpc_may_reuse_conn(struct rxrpc_connection
*conn
)
237 struct rxrpc_net
*rxnet
= conn
->params
.local
->rxnet
;
238 int id_cursor
, id
, distance
, limit
;
240 if (test_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
))
243 if (conn
->proto
.epoch
!= rxnet
->epoch
)
244 goto mark_dont_reuse
;
246 /* The IDR tree gets very expensive on memory if the connection IDs are
247 * widely scattered throughout the number space, so we shall want to
248 * kill off connections that, say, have an ID more than about four
249 * times the maximum number of client conns away from the current
250 * allocation point to try and keep the IDs concentrated.
252 id_cursor
= idr_get_cursor(&rxrpc_client_conn_ids
);
253 id
= conn
->proto
.cid
>> RXRPC_CIDSHIFT
;
254 distance
= id
- id_cursor
;
256 distance
= -distance
;
257 limit
= max(rxrpc_max_client_connections
* 4, 1024U);
258 if (distance
> limit
)
259 goto mark_dont_reuse
;
264 set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
270 * Create or find a client connection to use for a call.
272 * If we return with a connection, the call will be on its waiting list. It's
273 * left to the caller to assign a channel and wake up the call.
275 static int rxrpc_get_client_conn(struct rxrpc_sock
*rx
,
276 struct rxrpc_call
*call
,
277 struct rxrpc_conn_parameters
*cp
,
278 struct sockaddr_rxrpc
*srx
,
281 struct rxrpc_connection
*conn
, *candidate
= NULL
;
282 struct rxrpc_local
*local
= cp
->local
;
283 struct rb_node
*p
, **pp
, *parent
;
287 _enter("{%d,%lx},", call
->debug_id
, call
->user_call_ID
);
289 cp
->peer
= rxrpc_lookup_peer(rx
, cp
->local
, srx
, gfp
);
293 call
->cong_cwnd
= cp
->peer
->cong_cwnd
;
294 if (call
->cong_cwnd
>= call
->cong_ssthresh
)
295 call
->cong_mode
= RXRPC_CALL_CONGEST_AVOIDANCE
;
297 call
->cong_mode
= RXRPC_CALL_SLOW_START
;
299 /* If the connection is not meant to be exclusive, search the available
300 * connections to see if the connection we want to use already exists.
302 if (!cp
->exclusive
) {
304 spin_lock(&local
->client_conns_lock
);
305 p
= local
->client_conns
.rb_node
;
307 conn
= rb_entry(p
, struct rxrpc_connection
, client_node
);
309 #define cmp(X) ((long)conn->params.X - (long)cp->X)
312 cmp(security_level
) ?:
317 } else if (diff
> 0) {
320 if (rxrpc_may_reuse_conn(conn
) &&
321 rxrpc_get_connection_maybe(conn
))
322 goto found_extant_conn
;
323 /* The connection needs replacing. It's better
324 * to effect that when we have something to
325 * replace it with so that we don't have to
326 * rebalance the tree twice.
331 spin_unlock(&local
->client_conns_lock
);
334 /* There wasn't a connection yet or we need an exclusive connection.
335 * We need to create a candidate and then potentially redo the search
336 * in case we're racing with another thread also trying to connect on a
337 * shareable connection.
340 candidate
= rxrpc_alloc_client_connection(cp
, gfp
);
341 if (IS_ERR(candidate
)) {
342 ret
= PTR_ERR(candidate
);
346 /* Add the call to the new connection's waiting list in case we're
347 * going to have to wait for the connection to come live. It's our
348 * connection, so we want first dibs on the channel slots. We would
349 * normally have to take channel_lock but we do this before anyone else
350 * can see the connection.
352 list_add(&call
->chan_wait_link
, &candidate
->waiting_calls
);
355 call
->conn
= candidate
;
356 call
->security
= candidate
->security
;
357 call
->security_ix
= candidate
->security_ix
;
358 call
->service_id
= candidate
->service_id
;
359 _leave(" = 0 [exclusive %d]", candidate
->debug_id
);
363 /* Publish the new connection for userspace to find. We need to redo
364 * the search before doing this lest we race with someone else adding a
365 * conflicting instance.
368 spin_lock(&local
->client_conns_lock
);
370 pp
= &local
->client_conns
.rb_node
;
374 conn
= rb_entry(parent
, struct rxrpc_connection
, client_node
);
376 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
379 cmp(security_level
) ?:
383 pp
= &(*pp
)->rb_left
;
384 } else if (diff
> 0) {
385 pp
= &(*pp
)->rb_right
;
387 if (rxrpc_may_reuse_conn(conn
) &&
388 rxrpc_get_connection_maybe(conn
))
389 goto found_extant_conn
;
390 /* The old connection is from an outdated epoch. */
391 _debug("replace conn");
392 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &conn
->flags
);
393 rb_replace_node(&conn
->client_node
,
394 &candidate
->client_node
,
395 &local
->client_conns
);
396 trace_rxrpc_client(conn
, -1, rxrpc_client_replace
);
397 goto candidate_published
;
402 rb_link_node(&candidate
->client_node
, parent
, pp
);
403 rb_insert_color(&candidate
->client_node
, &local
->client_conns
);
406 set_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &candidate
->flags
);
407 call
->conn
= candidate
;
408 call
->security
= candidate
->security
;
409 call
->security_ix
= candidate
->security_ix
;
410 call
->service_id
= candidate
->service_id
;
411 spin_unlock(&local
->client_conns_lock
);
412 _leave(" = 0 [new %d]", candidate
->debug_id
);
415 /* We come here if we found a suitable connection already in existence.
416 * Discard any candidate we may have allocated, and try to get a
417 * channel on this one.
420 _debug("found conn");
421 spin_unlock(&local
->client_conns_lock
);
424 trace_rxrpc_client(candidate
, -1, rxrpc_client_duplicate
);
425 rxrpc_put_connection(candidate
);
429 spin_lock(&conn
->channel_lock
);
431 call
->security
= conn
->security
;
432 call
->security_ix
= conn
->security_ix
;
433 call
->service_id
= conn
->service_id
;
434 list_add_tail(&call
->chan_wait_link
, &conn
->waiting_calls
);
435 spin_unlock(&conn
->channel_lock
);
436 _leave(" = 0 [extant %d]", conn
->debug_id
);
440 rxrpc_put_peer(cp
->peer
);
443 _leave(" = %d", ret
);
448 * Activate a connection.
450 static void rxrpc_activate_conn(struct rxrpc_net
*rxnet
,
451 struct rxrpc_connection
*conn
)
453 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE
, &conn
->flags
)) {
454 trace_rxrpc_client(conn
, -1, rxrpc_client_to_upgrade
);
455 conn
->cache_state
= RXRPC_CONN_CLIENT_UPGRADE
;
457 trace_rxrpc_client(conn
, -1, rxrpc_client_to_active
);
458 conn
->cache_state
= RXRPC_CONN_CLIENT_ACTIVE
;
460 rxnet
->nr_active_client_conns
++;
461 list_move_tail(&conn
->cache_link
, &rxnet
->active_client_conns
);
465 * Attempt to animate a connection for a new call.
467 * If it's not exclusive, the connection is in the endpoint tree, and we're in
468 * the conn's list of those waiting to grab a channel. There is, however, a
469 * limit on the number of live connections allowed at any one time, so we may
470 * have to wait for capacity to become available.
472 * Note that a connection on the waiting queue might *also* have active
473 * channels if it has been culled to make space and then re-requested by a new
476 static void rxrpc_animate_client_conn(struct rxrpc_net
*rxnet
,
477 struct rxrpc_connection
*conn
)
479 unsigned int nr_conns
;
481 _enter("%d,%d", conn
->debug_id
, conn
->cache_state
);
483 if (conn
->cache_state
== RXRPC_CONN_CLIENT_ACTIVE
||
484 conn
->cache_state
== RXRPC_CONN_CLIENT_UPGRADE
)
487 spin_lock(&rxnet
->client_conn_cache_lock
);
489 nr_conns
= rxnet
->nr_client_conns
;
490 if (!test_and_set_bit(RXRPC_CONN_COUNTED
, &conn
->flags
)) {
491 trace_rxrpc_client(conn
, -1, rxrpc_client_count
);
492 rxnet
->nr_client_conns
= nr_conns
+ 1;
495 switch (conn
->cache_state
) {
496 case RXRPC_CONN_CLIENT_ACTIVE
:
497 case RXRPC_CONN_CLIENT_UPGRADE
:
498 case RXRPC_CONN_CLIENT_WAITING
:
501 case RXRPC_CONN_CLIENT_INACTIVE
:
502 case RXRPC_CONN_CLIENT_CULLED
:
503 case RXRPC_CONN_CLIENT_IDLE
:
504 if (nr_conns
>= rxrpc_max_client_connections
)
505 goto wait_for_capacity
;
513 spin_unlock(&rxnet
->client_conn_cache_lock
);
515 _leave(" [%d]", conn
->cache_state
);
520 rxrpc_activate_conn(rxnet
, conn
);
525 trace_rxrpc_client(conn
, -1, rxrpc_client_to_waiting
);
526 conn
->cache_state
= RXRPC_CONN_CLIENT_WAITING
;
527 list_move_tail(&conn
->cache_link
, &rxnet
->waiting_client_conns
);
532 * Deactivate a channel.
534 static void rxrpc_deactivate_one_channel(struct rxrpc_connection
*conn
,
535 unsigned int channel
)
537 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
539 rcu_assign_pointer(chan
->call
, NULL
);
540 conn
->active_chans
&= ~(1 << channel
);
544 * Assign a channel to the call at the front of the queue and wake the call up.
545 * We don't increment the callNumber counter until this number has been exposed
548 static void rxrpc_activate_one_channel(struct rxrpc_connection
*conn
,
549 unsigned int channel
)
551 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
552 struct rxrpc_call
*call
= list_entry(conn
->waiting_calls
.next
,
553 struct rxrpc_call
, chan_wait_link
);
554 u32 call_id
= chan
->call_counter
+ 1;
556 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_activate
);
558 /* Cancel the final ACK on the previous call if it hasn't been sent yet
559 * as the DATA packet will implicitly ACK it.
561 clear_bit(RXRPC_CONN_FINAL_ACK_0
+ channel
, &conn
->flags
);
563 write_lock_bh(&call
->state_lock
);
564 call
->state
= RXRPC_CALL_CLIENT_SEND_REQUEST
;
565 write_unlock_bh(&call
->state_lock
);
567 rxrpc_see_call(call
);
568 list_del_init(&call
->chan_wait_link
);
569 conn
->active_chans
|= 1 << channel
;
570 call
->peer
= rxrpc_get_peer(conn
->params
.peer
);
571 call
->cid
= conn
->proto
.cid
| channel
;
572 call
->call_id
= call_id
;
574 trace_rxrpc_connect_call(call
);
575 _net("CONNECT call %08x:%08x as call %d on conn %d",
576 call
->cid
, call
->call_id
, call
->debug_id
, conn
->debug_id
);
578 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
579 * orders cid and epoch in the connection wrt to call_id without the
580 * need to take the channel_lock.
582 * We provisionally assign a callNumber at this point, but we don't
583 * confirm it until the call is about to be exposed.
585 * TODO: Pair with a barrier in the data_ready handler when that looks
586 * at the call ID through a connection channel.
589 chan
->call_id
= call_id
;
590 chan
->call_debug_id
= call
->debug_id
;
591 rcu_assign_pointer(chan
->call
, call
);
592 wake_up(&call
->waitq
);
596 * Assign channels and callNumbers to waiting calls with channel_lock
599 static void rxrpc_activate_channels_locked(struct rxrpc_connection
*conn
)
603 switch (conn
->cache_state
) {
604 case RXRPC_CONN_CLIENT_ACTIVE
:
605 mask
= RXRPC_ACTIVE_CHANS_MASK
;
607 case RXRPC_CONN_CLIENT_UPGRADE
:
614 while (!list_empty(&conn
->waiting_calls
) &&
615 (avail
= ~conn
->active_chans
,
618 rxrpc_activate_one_channel(conn
, __ffs(avail
));
622 * Assign channels and callNumbers to waiting calls.
624 static void rxrpc_activate_channels(struct rxrpc_connection
*conn
)
626 _enter("%d", conn
->debug_id
);
628 trace_rxrpc_client(conn
, -1, rxrpc_client_activate_chans
);
630 if (conn
->active_chans
== RXRPC_ACTIVE_CHANS_MASK
)
633 spin_lock(&conn
->channel_lock
);
634 rxrpc_activate_channels_locked(conn
);
635 spin_unlock(&conn
->channel_lock
);
640 * Wait for a callNumber and a channel to be granted to a call.
642 static int rxrpc_wait_for_channel(struct rxrpc_call
*call
, gfp_t gfp
)
646 _enter("%d", call
->debug_id
);
648 if (!call
->call_id
) {
649 DECLARE_WAITQUEUE(myself
, current
);
651 if (!gfpflags_allow_blocking(gfp
)) {
656 add_wait_queue_exclusive(&call
->waitq
, &myself
);
658 if (test_bit(RXRPC_CALL_IS_INTR
, &call
->flags
))
659 set_current_state(TASK_INTERRUPTIBLE
);
661 set_current_state(TASK_UNINTERRUPTIBLE
);
664 if (test_bit(RXRPC_CALL_IS_INTR
, &call
->flags
) &&
665 signal_pending(current
)) {
671 remove_wait_queue(&call
->waitq
, &myself
);
672 __set_current_state(TASK_RUNNING
);
675 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
679 _leave(" = %d", ret
);
684 * find a connection for a call
685 * - called in process context with IRQs enabled
687 int rxrpc_connect_call(struct rxrpc_sock
*rx
,
688 struct rxrpc_call
*call
,
689 struct rxrpc_conn_parameters
*cp
,
690 struct sockaddr_rxrpc
*srx
,
693 struct rxrpc_net
*rxnet
= cp
->local
->rxnet
;
696 _enter("{%d,%lx},", call
->debug_id
, call
->user_call_ID
);
698 rxrpc_discard_expired_client_conns(&rxnet
->client_conn_reaper
);
699 rxrpc_cull_active_client_conns(rxnet
);
701 ret
= rxrpc_get_client_conn(rx
, call
, cp
, srx
, gfp
);
705 rxrpc_animate_client_conn(rxnet
, call
->conn
);
706 rxrpc_activate_channels(call
->conn
);
708 ret
= rxrpc_wait_for_channel(call
, gfp
);
710 trace_rxrpc_client(call
->conn
, ret
, rxrpc_client_chan_wait_failed
);
711 rxrpc_disconnect_client_call(call
);
715 spin_lock_bh(&call
->conn
->params
.peer
->lock
);
716 hlist_add_head_rcu(&call
->error_link
,
717 &call
->conn
->params
.peer
->error_targets
);
718 spin_unlock_bh(&call
->conn
->params
.peer
->lock
);
721 _leave(" = %d", ret
);
726 * Note that a connection is about to be exposed to the world. Once it is
727 * exposed, we maintain an extra ref on it that stops it from being summarily
728 * discarded before it's (a) had a chance to deal with retransmission and (b)
729 * had a chance at re-use (the per-connection security negotiation is
732 static void rxrpc_expose_client_conn(struct rxrpc_connection
*conn
,
733 unsigned int channel
)
735 if (!test_and_set_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
)) {
736 trace_rxrpc_client(conn
, channel
, rxrpc_client_exposed
);
737 rxrpc_get_connection(conn
);
742 * Note that a call, and thus a connection, is about to be exposed to the
745 void rxrpc_expose_client_call(struct rxrpc_call
*call
)
747 unsigned int channel
= call
->cid
& RXRPC_CHANNELMASK
;
748 struct rxrpc_connection
*conn
= call
->conn
;
749 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
751 if (!test_and_set_bit(RXRPC_CALL_EXPOSED
, &call
->flags
)) {
752 /* Mark the call ID as being used. If the callNumber counter
753 * exceeds ~2 billion, we kill the connection after its
754 * outstanding calls have finished so that the counter doesn't
757 chan
->call_counter
++;
758 if (chan
->call_counter
>= INT_MAX
)
759 set_bit(RXRPC_CONN_DONT_REUSE
, &conn
->flags
);
760 rxrpc_expose_client_conn(conn
, channel
);
765 * Set the reap timer.
767 static void rxrpc_set_client_reap_timer(struct rxrpc_net
*rxnet
)
769 unsigned long now
= jiffies
;
770 unsigned long reap_at
= now
+ rxrpc_conn_idle_client_expiry
;
773 timer_reduce(&rxnet
->client_conn_reap_timer
, reap_at
);
777 * Disconnect a client call.
779 void rxrpc_disconnect_client_call(struct rxrpc_call
*call
)
781 struct rxrpc_connection
*conn
= call
->conn
;
782 struct rxrpc_channel
*chan
= NULL
;
783 struct rxrpc_net
*rxnet
= conn
->params
.local
->rxnet
;
784 unsigned int channel
= -1;
787 spin_lock(&conn
->channel_lock
);
791 channel
= cid
& RXRPC_CHANNELMASK
;
792 chan
= &conn
->channels
[channel
];
794 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_disconnect
);
797 /* Calls that have never actually been assigned a channel can simply be
798 * discarded. If the conn didn't get used either, it will follow
799 * immediately unless someone else grabs it in the meantime.
801 if (!list_empty(&call
->chan_wait_link
)) {
802 _debug("call is waiting");
803 ASSERTCMP(call
->call_id
, ==, 0);
804 ASSERT(!test_bit(RXRPC_CALL_EXPOSED
, &call
->flags
));
805 list_del_init(&call
->chan_wait_link
);
807 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_unstarted
);
809 /* We must deactivate or idle the connection if it's now
810 * waiting for nothing.
812 spin_lock(&rxnet
->client_conn_cache_lock
);
813 if (conn
->cache_state
== RXRPC_CONN_CLIENT_WAITING
&&
814 list_empty(&conn
->waiting_calls
) &&
816 goto idle_connection
;
820 if (rcu_access_pointer(chan
->call
) != call
) {
821 spin_unlock(&conn
->channel_lock
);
825 /* If a client call was exposed to the world, we save the result for
828 * We use a barrier here so that the call number and abort code can be
829 * read without needing to take a lock.
831 * TODO: Make the incoming packet handler check this and handle
832 * terminal retransmission without requiring access to the call.
834 if (test_bit(RXRPC_CALL_EXPOSED
, &call
->flags
)) {
835 _debug("exposed %u,%u", call
->call_id
, call
->abort_code
);
836 __rxrpc_disconnect_call(conn
, call
);
839 /* See if we can pass the channel directly to another call. */
840 if (conn
->cache_state
== RXRPC_CONN_CLIENT_ACTIVE
&&
841 !list_empty(&conn
->waiting_calls
)) {
842 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_pass
);
843 rxrpc_activate_one_channel(conn
, channel
);
847 /* Schedule the final ACK to be transmitted in a short while so that it
848 * can be skipped if we find a follow-on call. The first DATA packet
849 * of the follow on call will implicitly ACK this call.
851 if (call
->completion
== RXRPC_CALL_SUCCEEDED
&&
852 test_bit(RXRPC_CALL_EXPOSED
, &call
->flags
)) {
853 unsigned long final_ack_at
= jiffies
+ 2;
855 WRITE_ONCE(chan
->final_ack_at
, final_ack_at
);
856 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
857 set_bit(RXRPC_CONN_FINAL_ACK_0
+ channel
, &conn
->flags
);
858 rxrpc_reduce_conn_timer(conn
, final_ack_at
);
861 /* Things are more complex and we need the cache lock. We might be
862 * able to simply idle the conn or it might now be lurking on the wait
863 * list. It might even get moved back to the active list whilst we're
864 * waiting for the lock.
866 spin_lock(&rxnet
->client_conn_cache_lock
);
868 switch (conn
->cache_state
) {
869 case RXRPC_CONN_CLIENT_UPGRADE
:
870 /* Deal with termination of a service upgrade probe. */
871 if (test_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
)) {
872 clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE
, &conn
->flags
);
873 trace_rxrpc_client(conn
, channel
, rxrpc_client_to_active
);
874 conn
->cache_state
= RXRPC_CONN_CLIENT_ACTIVE
;
875 rxrpc_activate_channels_locked(conn
);
878 case RXRPC_CONN_CLIENT_ACTIVE
:
879 if (list_empty(&conn
->waiting_calls
)) {
880 rxrpc_deactivate_one_channel(conn
, channel
);
881 if (!conn
->active_chans
) {
882 rxnet
->nr_active_client_conns
--;
883 goto idle_connection
;
888 trace_rxrpc_client(conn
, channel
, rxrpc_client_chan_pass
);
889 rxrpc_activate_one_channel(conn
, channel
);
892 case RXRPC_CONN_CLIENT_CULLED
:
893 rxrpc_deactivate_one_channel(conn
, channel
);
894 ASSERT(list_empty(&conn
->waiting_calls
));
895 if (!conn
->active_chans
)
896 goto idle_connection
;
899 case RXRPC_CONN_CLIENT_WAITING
:
900 rxrpc_deactivate_one_channel(conn
, channel
);
908 spin_unlock(&rxnet
->client_conn_cache_lock
);
910 spin_unlock(&conn
->channel_lock
);
911 rxrpc_put_connection(conn
);
916 /* As no channels remain active, the connection gets deactivated
917 * immediately or moved to the idle list for a short while.
919 if (test_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
)) {
920 trace_rxrpc_client(conn
, channel
, rxrpc_client_to_idle
);
921 conn
->idle_timestamp
= jiffies
;
922 conn
->cache_state
= RXRPC_CONN_CLIENT_IDLE
;
923 list_move_tail(&conn
->cache_link
, &rxnet
->idle_client_conns
);
924 if (rxnet
->idle_client_conns
.next
== &conn
->cache_link
&&
925 !rxnet
->kill_all_client_conns
)
926 rxrpc_set_client_reap_timer(rxnet
);
928 trace_rxrpc_client(conn
, channel
, rxrpc_client_to_inactive
);
929 conn
->cache_state
= RXRPC_CONN_CLIENT_INACTIVE
;
930 list_del_init(&conn
->cache_link
);
936 * Clean up a dead client connection.
938 static struct rxrpc_connection
*
939 rxrpc_put_one_client_conn(struct rxrpc_connection
*conn
)
941 struct rxrpc_connection
*next
= NULL
;
942 struct rxrpc_local
*local
= conn
->params
.local
;
943 struct rxrpc_net
*rxnet
= local
->rxnet
;
944 unsigned int nr_conns
;
946 trace_rxrpc_client(conn
, -1, rxrpc_client_cleanup
);
948 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS
, &conn
->flags
)) {
949 spin_lock(&local
->client_conns_lock
);
950 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS
,
952 rb_erase(&conn
->client_node
, &local
->client_conns
);
953 spin_unlock(&local
->client_conns_lock
);
956 rxrpc_put_client_connection_id(conn
);
958 ASSERTCMP(conn
->cache_state
, ==, RXRPC_CONN_CLIENT_INACTIVE
);
960 if (test_bit(RXRPC_CONN_COUNTED
, &conn
->flags
)) {
961 trace_rxrpc_client(conn
, -1, rxrpc_client_uncount
);
962 spin_lock(&rxnet
->client_conn_cache_lock
);
963 nr_conns
= --rxnet
->nr_client_conns
;
965 if (nr_conns
< rxrpc_max_client_connections
&&
966 !list_empty(&rxnet
->waiting_client_conns
)) {
967 next
= list_entry(rxnet
->waiting_client_conns
.next
,
968 struct rxrpc_connection
, cache_link
);
969 rxrpc_get_connection(next
);
970 rxrpc_activate_conn(rxnet
, next
);
973 spin_unlock(&rxnet
->client_conn_cache_lock
);
976 rxrpc_kill_connection(conn
);
978 rxrpc_activate_channels(next
);
980 /* We need to get rid of the temporary ref we took upon next, but we
981 * can't call rxrpc_put_connection() recursively.
987 * Clean up a dead client connections.
989 void rxrpc_put_client_conn(struct rxrpc_connection
*conn
)
991 const void *here
= __builtin_return_address(0);
992 unsigned int debug_id
= conn
->debug_id
;
996 n
= atomic_dec_return(&conn
->usage
);
997 trace_rxrpc_conn(debug_id
, rxrpc_conn_put_client
, n
, here
);
1000 ASSERTCMP(n
, >=, 0);
1002 conn
= rxrpc_put_one_client_conn(conn
);
1007 * Kill the longest-active client connections to make room for new ones.
1009 static void rxrpc_cull_active_client_conns(struct rxrpc_net
*rxnet
)
1011 struct rxrpc_connection
*conn
;
1012 unsigned int nr_conns
= rxnet
->nr_client_conns
;
1013 unsigned int nr_active
, limit
;
1017 ASSERTCMP(nr_conns
, >=, 0);
1018 if (nr_conns
< rxrpc_max_client_connections
) {
1022 limit
= rxrpc_reap_client_connections
;
1024 spin_lock(&rxnet
->client_conn_cache_lock
);
1025 nr_active
= rxnet
->nr_active_client_conns
;
1027 while (nr_active
> limit
) {
1028 ASSERT(!list_empty(&rxnet
->active_client_conns
));
1029 conn
= list_entry(rxnet
->active_client_conns
.next
,
1030 struct rxrpc_connection
, cache_link
);
1031 ASSERTIFCMP(conn
->cache_state
!= RXRPC_CONN_CLIENT_ACTIVE
,
1032 conn
->cache_state
, ==, RXRPC_CONN_CLIENT_UPGRADE
);
1034 if (list_empty(&conn
->waiting_calls
)) {
1035 trace_rxrpc_client(conn
, -1, rxrpc_client_to_culled
);
1036 conn
->cache_state
= RXRPC_CONN_CLIENT_CULLED
;
1037 list_del_init(&conn
->cache_link
);
1039 trace_rxrpc_client(conn
, -1, rxrpc_client_to_waiting
);
1040 conn
->cache_state
= RXRPC_CONN_CLIENT_WAITING
;
1041 list_move_tail(&conn
->cache_link
,
1042 &rxnet
->waiting_client_conns
);
1048 rxnet
->nr_active_client_conns
= nr_active
;
1049 spin_unlock(&rxnet
->client_conn_cache_lock
);
1050 ASSERTCMP(nr_active
, >=, 0);
1051 _leave(" [culled]");
1055 * Discard expired client connections from the idle list. Each conn in the
1056 * idle list has been exposed and holds an extra ref because of that.
1058 * This may be called from conn setup or from a work item so cannot be
1059 * considered non-reentrant.
1061 void rxrpc_discard_expired_client_conns(struct work_struct
*work
)
1063 struct rxrpc_connection
*conn
;
1064 struct rxrpc_net
*rxnet
=
1065 container_of(work
, struct rxrpc_net
, client_conn_reaper
);
1066 unsigned long expiry
, conn_expires_at
, now
;
1067 unsigned int nr_conns
;
1071 if (list_empty(&rxnet
->idle_client_conns
)) {
1076 /* Don't double up on the discarding */
1077 if (!spin_trylock(&rxnet
->client_conn_discard_lock
)) {
1078 _leave(" [already]");
1082 /* We keep an estimate of what the number of conns ought to be after
1083 * we've discarded some so that we don't overdo the discarding.
1085 nr_conns
= rxnet
->nr_client_conns
;
1088 spin_lock(&rxnet
->client_conn_cache_lock
);
1090 if (list_empty(&rxnet
->idle_client_conns
))
1093 conn
= list_entry(rxnet
->idle_client_conns
.next
,
1094 struct rxrpc_connection
, cache_link
);
1095 ASSERT(test_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
));
1097 if (!rxnet
->kill_all_client_conns
) {
1098 /* If the number of connections is over the reap limit, we
1099 * expedite discard by reducing the expiry timeout. We must,
1100 * however, have at least a short grace period to be able to do
1101 * final-ACK or ABORT retransmission.
1103 expiry
= rxrpc_conn_idle_client_expiry
;
1104 if (nr_conns
> rxrpc_reap_client_connections
)
1105 expiry
= rxrpc_conn_idle_client_fast_expiry
;
1106 if (conn
->params
.local
->service_closed
)
1107 expiry
= rxrpc_closed_conn_expiry
* HZ
;
1109 conn_expires_at
= conn
->idle_timestamp
+ expiry
;
1111 now
= READ_ONCE(jiffies
);
1112 if (time_after(conn_expires_at
, now
))
1113 goto not_yet_expired
;
1116 trace_rxrpc_client(conn
, -1, rxrpc_client_discard
);
1117 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
))
1119 conn
->cache_state
= RXRPC_CONN_CLIENT_INACTIVE
;
1120 list_del_init(&conn
->cache_link
);
1122 spin_unlock(&rxnet
->client_conn_cache_lock
);
1124 /* When we cleared the EXPOSED flag, we took on responsibility for the
1125 * reference that that had on the usage count. We deal with that here.
1126 * If someone re-sets the flag and re-gets the ref, that's fine.
1128 rxrpc_put_connection(conn
);
1133 /* The connection at the front of the queue hasn't yet expired, so
1134 * schedule the work item for that point if we discarded something.
1136 * We don't worry if the work item is already scheduled - it can look
1137 * after rescheduling itself at a later time. We could cancel it, but
1138 * then things get messier.
1141 if (!rxnet
->kill_all_client_conns
)
1142 timer_reduce(&rxnet
->client_conn_reap_timer
,
1146 spin_unlock(&rxnet
->client_conn_cache_lock
);
1147 spin_unlock(&rxnet
->client_conn_discard_lock
);
1152 * Preemptively destroy all the client connection records rather than waiting
1153 * for them to time out
1155 void rxrpc_destroy_all_client_connections(struct rxrpc_net
*rxnet
)
1159 spin_lock(&rxnet
->client_conn_cache_lock
);
1160 rxnet
->kill_all_client_conns
= true;
1161 spin_unlock(&rxnet
->client_conn_cache_lock
);
1163 del_timer_sync(&rxnet
->client_conn_reap_timer
);
1165 if (!rxrpc_queue_work(&rxnet
->client_conn_reaper
))
1166 _debug("destroy: queue failed");
1172 * Clean up the client connections on a local endpoint.
1174 void rxrpc_clean_up_local_conns(struct rxrpc_local
*local
)
1176 struct rxrpc_connection
*conn
, *tmp
;
1177 struct rxrpc_net
*rxnet
= local
->rxnet
;
1178 unsigned int nr_active
;
1179 LIST_HEAD(graveyard
);
1183 spin_lock(&rxnet
->client_conn_cache_lock
);
1184 nr_active
= rxnet
->nr_active_client_conns
;
1186 list_for_each_entry_safe(conn
, tmp
, &rxnet
->idle_client_conns
,
1188 if (conn
->params
.local
== local
) {
1189 ASSERTCMP(conn
->cache_state
, ==, RXRPC_CONN_CLIENT_IDLE
);
1191 trace_rxrpc_client(conn
, -1, rxrpc_client_discard
);
1192 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED
, &conn
->flags
))
1194 conn
->cache_state
= RXRPC_CONN_CLIENT_INACTIVE
;
1195 list_move(&conn
->cache_link
, &graveyard
);
1200 rxnet
->nr_active_client_conns
= nr_active
;
1201 spin_unlock(&rxnet
->client_conn_cache_lock
);
1202 ASSERTCMP(nr_active
, >=, 0);
1204 while (!list_empty(&graveyard
)) {
1205 conn
= list_entry(graveyard
.next
,
1206 struct rxrpc_connection
, cache_link
);
1207 list_del_init(&conn
->cache_link
);
1209 rxrpc_put_connection(conn
);
1212 _leave(" [culled]");