xref: /openbmc/linux/net/rxrpc/conn_client.c (revision 3ca6c3b43c72a5fd0399d9ee1c7e5af978895ff1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Client connection-specific management code.
3  *
4  * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
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.
10  *
11  * There are flags of relevance to the cache:
12  *
13  *  (2) DONT_REUSE - The connection should be discarded as soon as possible and
14  *      should not be reused.  This is set when an exclusive connection is used
15  *      or a call ID counter overflows.
16  *
17  * The caching state may only be changed if the cache lock is held.
18  *
19  * There are two idle client connection expiry durations.  If the total number
20  * of connections is below the reap threshold, we use the normal duration; if
21  * it's above, we use the fast duration.
22  */
23 
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/timer.h>
29 #include <linux/sched/signal.h>
30 
31 #include "ar-internal.h"
32 
33 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
34 __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
35 __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
36 
37 /*
38  * We use machine-unique IDs for our client connections.
39  */
40 DEFINE_IDR(rxrpc_client_conn_ids);
41 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
42 
43 /*
44  * Get a connection ID and epoch for a client connection from the global pool.
45  * The connection struct pointer is then recorded in the idr radix tree.  The
46  * epoch doesn't change until the client is rebooted (or, at least, unless the
47  * module is unloaded).
48  */
49 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
50 					  gfp_t gfp)
51 {
52 	struct rxrpc_net *rxnet = conn->params.local->rxnet;
53 	int id;
54 
55 	_enter("");
56 
57 	idr_preload(gfp);
58 	spin_lock(&rxrpc_conn_id_lock);
59 
60 	id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
61 			      1, 0x40000000, GFP_NOWAIT);
62 	if (id < 0)
63 		goto error;
64 
65 	spin_unlock(&rxrpc_conn_id_lock);
66 	idr_preload_end();
67 
68 	conn->proto.epoch = rxnet->epoch;
69 	conn->proto.cid = id << RXRPC_CIDSHIFT;
70 	set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
71 	_leave(" [CID %x]", conn->proto.cid);
72 	return 0;
73 
74 error:
75 	spin_unlock(&rxrpc_conn_id_lock);
76 	idr_preload_end();
77 	_leave(" = %d", id);
78 	return id;
79 }
80 
81 /*
82  * Release a connection ID for a client connection from the global pool.
83  */
84 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
85 {
86 	if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
87 		spin_lock(&rxrpc_conn_id_lock);
88 		idr_remove(&rxrpc_client_conn_ids,
89 			   conn->proto.cid >> RXRPC_CIDSHIFT);
90 		spin_unlock(&rxrpc_conn_id_lock);
91 	}
92 }
93 
94 /*
95  * Destroy the client connection ID tree.
96  */
97 void rxrpc_destroy_client_conn_ids(void)
98 {
99 	struct rxrpc_connection *conn;
100 	int id;
101 
102 	if (!idr_is_empty(&rxrpc_client_conn_ids)) {
103 		idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
104 			pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
105 			       conn, refcount_read(&conn->ref));
106 		}
107 		BUG();
108 	}
109 
110 	idr_destroy(&rxrpc_client_conn_ids);
111 }
112 
113 /*
114  * Allocate a connection bundle.
115  */
116 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp,
117 					       gfp_t gfp)
118 {
119 	struct rxrpc_bundle *bundle;
120 
121 	bundle = kzalloc(sizeof(*bundle), gfp);
122 	if (bundle) {
123 		bundle->params = *cp;
124 		rxrpc_get_peer(bundle->params.peer);
125 		refcount_set(&bundle->ref, 1);
126 		spin_lock_init(&bundle->channel_lock);
127 		INIT_LIST_HEAD(&bundle->waiting_calls);
128 	}
129 	return bundle;
130 }
131 
132 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle)
133 {
134 	refcount_inc(&bundle->ref);
135 	return bundle;
136 }
137 
138 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
139 {
140 	rxrpc_put_peer(bundle->params.peer);
141 	kfree(bundle);
142 }
143 
144 void rxrpc_put_bundle(struct rxrpc_bundle *bundle)
145 {
146 	unsigned int d = bundle->debug_id;
147 	bool dead;
148 	int r;
149 
150 	dead = __refcount_dec_and_test(&bundle->ref, &r);
151 
152 	_debug("PUT B=%x %d", d, r);
153 	if (dead)
154 		rxrpc_free_bundle(bundle);
155 }
156 
157 /*
158  * Allocate a client connection.
159  */
160 static struct rxrpc_connection *
161 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
162 {
163 	struct rxrpc_connection *conn;
164 	struct rxrpc_net *rxnet = bundle->params.local->rxnet;
165 	int ret;
166 
167 	_enter("");
168 
169 	conn = rxrpc_alloc_connection(gfp);
170 	if (!conn) {
171 		_leave(" = -ENOMEM");
172 		return ERR_PTR(-ENOMEM);
173 	}
174 
175 	refcount_set(&conn->ref, 1);
176 	conn->bundle		= bundle;
177 	conn->params		= bundle->params;
178 	conn->out_clientflag	= RXRPC_CLIENT_INITIATED;
179 	conn->state		= RXRPC_CONN_CLIENT;
180 	conn->service_id	= conn->params.service_id;
181 
182 	ret = rxrpc_get_client_connection_id(conn, gfp);
183 	if (ret < 0)
184 		goto error_0;
185 
186 	ret = rxrpc_init_client_conn_security(conn);
187 	if (ret < 0)
188 		goto error_1;
189 
190 	atomic_inc(&rxnet->nr_conns);
191 	write_lock(&rxnet->conn_lock);
192 	list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
193 	write_unlock(&rxnet->conn_lock);
194 
195 	rxrpc_get_bundle(bundle);
196 	rxrpc_get_peer(conn->params.peer);
197 	rxrpc_get_local(conn->params.local);
198 	key_get(conn->params.key);
199 
200 	trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_client,
201 			 refcount_read(&conn->ref),
202 			 __builtin_return_address(0));
203 
204 	atomic_inc(&rxnet->nr_client_conns);
205 	trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
206 	_leave(" = %p", conn);
207 	return conn;
208 
209 error_1:
210 	rxrpc_put_client_connection_id(conn);
211 error_0:
212 	kfree(conn);
213 	_leave(" = %d", ret);
214 	return ERR_PTR(ret);
215 }
216 
217 /*
218  * Determine if a connection may be reused.
219  */
220 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
221 {
222 	struct rxrpc_net *rxnet;
223 	int id_cursor, id, distance, limit;
224 
225 	if (!conn)
226 		goto dont_reuse;
227 
228 	rxnet = conn->params.local->rxnet;
229 	if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
230 		goto dont_reuse;
231 
232 	if (conn->state != RXRPC_CONN_CLIENT ||
233 	    conn->proto.epoch != rxnet->epoch)
234 		goto mark_dont_reuse;
235 
236 	/* The IDR tree gets very expensive on memory if the connection IDs are
237 	 * widely scattered throughout the number space, so we shall want to
238 	 * kill off connections that, say, have an ID more than about four
239 	 * times the maximum number of client conns away from the current
240 	 * allocation point to try and keep the IDs concentrated.
241 	 */
242 	id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
243 	id = conn->proto.cid >> RXRPC_CIDSHIFT;
244 	distance = id - id_cursor;
245 	if (distance < 0)
246 		distance = -distance;
247 	limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
248 	if (distance > limit)
249 		goto mark_dont_reuse;
250 
251 	return true;
252 
253 mark_dont_reuse:
254 	set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
255 dont_reuse:
256 	return false;
257 }
258 
259 /*
260  * Look up the conn bundle that matches the connection parameters, adding it if
261  * it doesn't yet exist.
262  */
263 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
264 						 gfp_t gfp)
265 {
266 	static atomic_t rxrpc_bundle_id;
267 	struct rxrpc_bundle *bundle, *candidate;
268 	struct rxrpc_local *local = cp->local;
269 	struct rb_node *p, **pp, *parent;
270 	long diff;
271 
272 	_enter("{%px,%x,%u,%u}",
273 	       cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
274 
275 	if (cp->exclusive)
276 		return rxrpc_alloc_bundle(cp, gfp);
277 
278 	/* First, see if the bundle is already there. */
279 	_debug("search 1");
280 	spin_lock(&local->client_bundles_lock);
281 	p = local->client_bundles.rb_node;
282 	while (p) {
283 		bundle = rb_entry(p, struct rxrpc_bundle, local_node);
284 
285 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
286 		diff = (cmp(peer) ?:
287 			cmp(key) ?:
288 			cmp(security_level) ?:
289 			cmp(upgrade));
290 #undef cmp
291 		if (diff < 0)
292 			p = p->rb_left;
293 		else if (diff > 0)
294 			p = p->rb_right;
295 		else
296 			goto found_bundle;
297 	}
298 	spin_unlock(&local->client_bundles_lock);
299 	_debug("not found");
300 
301 	/* It wasn't.  We need to add one. */
302 	candidate = rxrpc_alloc_bundle(cp, gfp);
303 	if (!candidate)
304 		return NULL;
305 
306 	_debug("search 2");
307 	spin_lock(&local->client_bundles_lock);
308 	pp = &local->client_bundles.rb_node;
309 	parent = NULL;
310 	while (*pp) {
311 		parent = *pp;
312 		bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
313 
314 #define cmp(X) ((long)bundle->params.X - (long)cp->X)
315 		diff = (cmp(peer) ?:
316 			cmp(key) ?:
317 			cmp(security_level) ?:
318 			cmp(upgrade));
319 #undef cmp
320 		if (diff < 0)
321 			pp = &(*pp)->rb_left;
322 		else if (diff > 0)
323 			pp = &(*pp)->rb_right;
324 		else
325 			goto found_bundle_free;
326 	}
327 
328 	_debug("new bundle");
329 	candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
330 	rb_link_node(&candidate->local_node, parent, pp);
331 	rb_insert_color(&candidate->local_node, &local->client_bundles);
332 	rxrpc_get_bundle(candidate);
333 	spin_unlock(&local->client_bundles_lock);
334 	_leave(" = %u [new]", candidate->debug_id);
335 	return candidate;
336 
337 found_bundle_free:
338 	rxrpc_free_bundle(candidate);
339 found_bundle:
340 	rxrpc_get_bundle(bundle);
341 	spin_unlock(&local->client_bundles_lock);
342 	_leave(" = %u [found]", bundle->debug_id);
343 	return bundle;
344 }
345 
346 /*
347  * Create or find a client bundle to use for a call.
348  *
349  * If we return with a connection, the call will be on its waiting list.  It's
350  * left to the caller to assign a channel and wake up the call.
351  */
352 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
353 					    struct rxrpc_call *call,
354 					    struct rxrpc_conn_parameters *cp,
355 					    struct sockaddr_rxrpc *srx,
356 					    gfp_t gfp)
357 {
358 	struct rxrpc_bundle *bundle;
359 
360 	_enter("{%d,%lx},", call->debug_id, call->user_call_ID);
361 
362 	cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
363 	if (!cp->peer)
364 		goto error;
365 
366 	call->tx_last_sent = ktime_get_real();
367 	call->cong_ssthresh = cp->peer->cong_ssthresh;
368 	if (call->cong_cwnd >= call->cong_ssthresh)
369 		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
370 	else
371 		call->cong_mode = RXRPC_CALL_SLOW_START;
372 	if (cp->upgrade)
373 		__set_bit(RXRPC_CALL_UPGRADE, &call->flags);
374 
375 	/* Find the client connection bundle. */
376 	bundle = rxrpc_look_up_bundle(cp, gfp);
377 	if (!bundle)
378 		goto error;
379 
380 	/* Get this call queued.  Someone else may activate it whilst we're
381 	 * lining up a new connection, but that's fine.
382 	 */
383 	spin_lock(&bundle->channel_lock);
384 	list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
385 	spin_unlock(&bundle->channel_lock);
386 
387 	_leave(" = [B=%x]", bundle->debug_id);
388 	return bundle;
389 
390 error:
391 	_leave(" = -ENOMEM");
392 	return ERR_PTR(-ENOMEM);
393 }
394 
395 /*
396  * Allocate a new connection and add it into a bundle.
397  */
398 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
399 	__releases(bundle->channel_lock)
400 {
401 	struct rxrpc_connection *candidate = NULL, *old = NULL;
402 	bool conflict;
403 	int i;
404 
405 	_enter("");
406 
407 	conflict = bundle->alloc_conn;
408 	if (!conflict)
409 		bundle->alloc_conn = true;
410 	spin_unlock(&bundle->channel_lock);
411 	if (conflict) {
412 		_leave(" [conf]");
413 		return;
414 	}
415 
416 	candidate = rxrpc_alloc_client_connection(bundle, gfp);
417 
418 	spin_lock(&bundle->channel_lock);
419 	bundle->alloc_conn = false;
420 
421 	if (IS_ERR(candidate)) {
422 		bundle->alloc_error = PTR_ERR(candidate);
423 		spin_unlock(&bundle->channel_lock);
424 		_leave(" [err %ld]", PTR_ERR(candidate));
425 		return;
426 	}
427 
428 	bundle->alloc_error = 0;
429 
430 	for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
431 		unsigned int shift = i * RXRPC_MAXCALLS;
432 		int j;
433 
434 		old = bundle->conns[i];
435 		if (!rxrpc_may_reuse_conn(old)) {
436 			if (old)
437 				trace_rxrpc_client(old, -1, rxrpc_client_replace);
438 			candidate->bundle_shift = shift;
439 			bundle->conns[i] = candidate;
440 			for (j = 0; j < RXRPC_MAXCALLS; j++)
441 				set_bit(shift + j, &bundle->avail_chans);
442 			candidate = NULL;
443 			break;
444 		}
445 
446 		old = NULL;
447 	}
448 
449 	spin_unlock(&bundle->channel_lock);
450 
451 	if (candidate) {
452 		_debug("discard C=%x", candidate->debug_id);
453 		trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
454 		rxrpc_put_connection(candidate);
455 	}
456 
457 	rxrpc_put_connection(old);
458 	_leave("");
459 }
460 
461 /*
462  * Add a connection to a bundle if there are no usable connections or we have
463  * connections waiting for extra capacity.
464  */
465 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
466 {
467 	struct rxrpc_call *call;
468 	int i, usable;
469 
470 	_enter("");
471 
472 	spin_lock(&bundle->channel_lock);
473 
474 	/* See if there are any usable connections. */
475 	usable = 0;
476 	for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
477 		if (rxrpc_may_reuse_conn(bundle->conns[i]))
478 			usable++;
479 
480 	if (!usable && !list_empty(&bundle->waiting_calls)) {
481 		call = list_first_entry(&bundle->waiting_calls,
482 					struct rxrpc_call, chan_wait_link);
483 		if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
484 			bundle->try_upgrade = true;
485 	}
486 
487 	if (!usable)
488 		goto alloc_conn;
489 
490 	if (!bundle->avail_chans &&
491 	    !bundle->try_upgrade &&
492 	    !list_empty(&bundle->waiting_calls) &&
493 	    usable < ARRAY_SIZE(bundle->conns))
494 		goto alloc_conn;
495 
496 	spin_unlock(&bundle->channel_lock);
497 	_leave("");
498 	return;
499 
500 alloc_conn:
501 	return rxrpc_add_conn_to_bundle(bundle, gfp);
502 }
503 
504 /*
505  * Assign a channel to the call at the front of the queue and wake the call up.
506  * We don't increment the callNumber counter until this number has been exposed
507  * to the world.
508  */
509 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
510 				       unsigned int channel)
511 {
512 	struct rxrpc_channel *chan = &conn->channels[channel];
513 	struct rxrpc_bundle *bundle = conn->bundle;
514 	struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
515 					     struct rxrpc_call, chan_wait_link);
516 	u32 call_id = chan->call_counter + 1;
517 
518 	_enter("C=%x,%u", conn->debug_id, channel);
519 
520 	trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
521 
522 	/* Cancel the final ACK on the previous call if it hasn't been sent yet
523 	 * as the DATA packet will implicitly ACK it.
524 	 */
525 	clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
526 	clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
527 
528 	rxrpc_see_call(call);
529 	list_del_init(&call->chan_wait_link);
530 	call->peer	= rxrpc_get_peer(conn->params.peer);
531 	call->conn	= rxrpc_get_connection(conn);
532 	call->cid	= conn->proto.cid | channel;
533 	call->call_id	= call_id;
534 	call->security	= conn->security;
535 	call->security_ix = conn->security_ix;
536 	call->service_id = conn->service_id;
537 
538 	trace_rxrpc_connect_call(call);
539 	_net("CONNECT call %08x:%08x as call %d on conn %d",
540 	     call->cid, call->call_id, call->debug_id, conn->debug_id);
541 
542 	write_lock_bh(&call->state_lock);
543 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
544 	write_unlock_bh(&call->state_lock);
545 
546 	/* Paired with the read barrier in rxrpc_connect_call().  This orders
547 	 * cid and epoch in the connection wrt to call_id without the need to
548 	 * take the channel_lock.
549 	 *
550 	 * We provisionally assign a callNumber at this point, but we don't
551 	 * confirm it until the call is about to be exposed.
552 	 *
553 	 * TODO: Pair with a barrier in the data_ready handler when that looks
554 	 * at the call ID through a connection channel.
555 	 */
556 	smp_wmb();
557 
558 	chan->call_id		= call_id;
559 	chan->call_debug_id	= call->debug_id;
560 	rcu_assign_pointer(chan->call, call);
561 	wake_up(&call->waitq);
562 }
563 
564 /*
565  * Remove a connection from the idle list if it's on it.
566  */
567 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
568 {
569 	struct rxrpc_net *rxnet = bundle->params.local->rxnet;
570 	bool drop_ref;
571 
572 	if (!list_empty(&conn->cache_link)) {
573 		drop_ref = false;
574 		spin_lock(&rxnet->client_conn_cache_lock);
575 		if (!list_empty(&conn->cache_link)) {
576 			list_del_init(&conn->cache_link);
577 			drop_ref = true;
578 		}
579 		spin_unlock(&rxnet->client_conn_cache_lock);
580 		if (drop_ref)
581 			rxrpc_put_connection(conn);
582 	}
583 }
584 
585 /*
586  * Assign channels and callNumbers to waiting calls with channel_lock
587  * held by caller.
588  */
589 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
590 {
591 	struct rxrpc_connection *conn;
592 	unsigned long avail, mask;
593 	unsigned int channel, slot;
594 
595 	if (bundle->try_upgrade)
596 		mask = 1;
597 	else
598 		mask = ULONG_MAX;
599 
600 	while (!list_empty(&bundle->waiting_calls)) {
601 		avail = bundle->avail_chans & mask;
602 		if (!avail)
603 			break;
604 		channel = __ffs(avail);
605 		clear_bit(channel, &bundle->avail_chans);
606 
607 		slot = channel / RXRPC_MAXCALLS;
608 		conn = bundle->conns[slot];
609 		if (!conn)
610 			break;
611 
612 		if (bundle->try_upgrade)
613 			set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
614 		rxrpc_unidle_conn(bundle, conn);
615 
616 		channel &= (RXRPC_MAXCALLS - 1);
617 		conn->act_chans	|= 1 << channel;
618 		rxrpc_activate_one_channel(conn, channel);
619 	}
620 }
621 
622 /*
623  * Assign channels and callNumbers to waiting calls.
624  */
625 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
626 {
627 	_enter("B=%x", bundle->debug_id);
628 
629 	trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
630 
631 	if (!bundle->avail_chans)
632 		return;
633 
634 	spin_lock(&bundle->channel_lock);
635 	rxrpc_activate_channels_locked(bundle);
636 	spin_unlock(&bundle->channel_lock);
637 	_leave("");
638 }
639 
640 /*
641  * Wait for a callNumber and a channel to be granted to a call.
642  */
643 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
644 				  struct rxrpc_call *call, gfp_t gfp)
645 {
646 	DECLARE_WAITQUEUE(myself, current);
647 	int ret = 0;
648 
649 	_enter("%d", call->debug_id);
650 
651 	if (!gfpflags_allow_blocking(gfp)) {
652 		rxrpc_maybe_add_conn(bundle, gfp);
653 		rxrpc_activate_channels(bundle);
654 		ret = bundle->alloc_error ?: -EAGAIN;
655 		goto out;
656 	}
657 
658 	add_wait_queue_exclusive(&call->waitq, &myself);
659 	for (;;) {
660 		rxrpc_maybe_add_conn(bundle, gfp);
661 		rxrpc_activate_channels(bundle);
662 		ret = bundle->alloc_error;
663 		if (ret < 0)
664 			break;
665 
666 		switch (call->interruptibility) {
667 		case RXRPC_INTERRUPTIBLE:
668 		case RXRPC_PREINTERRUPTIBLE:
669 			set_current_state(TASK_INTERRUPTIBLE);
670 			break;
671 		case RXRPC_UNINTERRUPTIBLE:
672 		default:
673 			set_current_state(TASK_UNINTERRUPTIBLE);
674 			break;
675 		}
676 		if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
677 			break;
678 		if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
679 		     call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
680 		    signal_pending(current)) {
681 			ret = -ERESTARTSYS;
682 			break;
683 		}
684 		schedule();
685 	}
686 	remove_wait_queue(&call->waitq, &myself);
687 	__set_current_state(TASK_RUNNING);
688 
689 out:
690 	_leave(" = %d", ret);
691 	return ret;
692 }
693 
694 /*
695  * find a connection for a call
696  * - called in process context with IRQs enabled
697  */
698 int rxrpc_connect_call(struct rxrpc_sock *rx,
699 		       struct rxrpc_call *call,
700 		       struct rxrpc_conn_parameters *cp,
701 		       struct sockaddr_rxrpc *srx,
702 		       gfp_t gfp)
703 {
704 	struct rxrpc_bundle *bundle;
705 	struct rxrpc_net *rxnet = cp->local->rxnet;
706 	int ret = 0;
707 
708 	_enter("{%d,%lx},", call->debug_id, call->user_call_ID);
709 
710 	rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
711 
712 	bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
713 	if (IS_ERR(bundle)) {
714 		ret = PTR_ERR(bundle);
715 		goto out;
716 	}
717 
718 	if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
719 		ret = rxrpc_wait_for_channel(bundle, call, gfp);
720 		if (ret < 0)
721 			goto wait_failed;
722 	}
723 
724 granted_channel:
725 	/* Paired with the write barrier in rxrpc_activate_one_channel(). */
726 	smp_rmb();
727 
728 out_put_bundle:
729 	rxrpc_put_bundle(bundle);
730 out:
731 	_leave(" = %d", ret);
732 	return ret;
733 
734 wait_failed:
735 	spin_lock(&bundle->channel_lock);
736 	list_del_init(&call->chan_wait_link);
737 	spin_unlock(&bundle->channel_lock);
738 
739 	if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
740 		ret = 0;
741 		goto granted_channel;
742 	}
743 
744 	trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
745 	rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
746 	rxrpc_disconnect_client_call(bundle, call);
747 	goto out_put_bundle;
748 }
749 
750 /*
751  * Note that a call, and thus a connection, is about to be exposed to the
752  * world.
753  */
754 void rxrpc_expose_client_call(struct rxrpc_call *call)
755 {
756 	unsigned int channel = call->cid & RXRPC_CHANNELMASK;
757 	struct rxrpc_connection *conn = call->conn;
758 	struct rxrpc_channel *chan = &conn->channels[channel];
759 
760 	if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
761 		/* Mark the call ID as being used.  If the callNumber counter
762 		 * exceeds ~2 billion, we kill the connection after its
763 		 * outstanding calls have finished so that the counter doesn't
764 		 * wrap.
765 		 */
766 		chan->call_counter++;
767 		if (chan->call_counter >= INT_MAX)
768 			set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
769 		trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
770 	}
771 }
772 
773 /*
774  * Set the reap timer.
775  */
776 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
777 {
778 	if (!rxnet->kill_all_client_conns) {
779 		unsigned long now = jiffies;
780 		unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
781 
782 		if (rxnet->live)
783 			timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
784 	}
785 }
786 
787 /*
788  * Disconnect a client call.
789  */
790 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
791 {
792 	struct rxrpc_connection *conn;
793 	struct rxrpc_channel *chan = NULL;
794 	struct rxrpc_net *rxnet = bundle->params.local->rxnet;
795 	unsigned int channel;
796 	bool may_reuse;
797 	u32 cid;
798 
799 	_enter("c=%x", call->debug_id);
800 
801 	spin_lock(&bundle->channel_lock);
802 	set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
803 
804 	/* Calls that have never actually been assigned a channel can simply be
805 	 * discarded.
806 	 */
807 	conn = call->conn;
808 	if (!conn) {
809 		_debug("call is waiting");
810 		ASSERTCMP(call->call_id, ==, 0);
811 		ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
812 		list_del_init(&call->chan_wait_link);
813 		goto out;
814 	}
815 
816 	cid = call->cid;
817 	channel = cid & RXRPC_CHANNELMASK;
818 	chan = &conn->channels[channel];
819 	trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
820 
821 	if (rcu_access_pointer(chan->call) != call) {
822 		spin_unlock(&bundle->channel_lock);
823 		BUG();
824 	}
825 
826 	may_reuse = rxrpc_may_reuse_conn(conn);
827 
828 	/* If a client call was exposed to the world, we save the result for
829 	 * retransmission.
830 	 *
831 	 * We use a barrier here so that the call number and abort code can be
832 	 * read without needing to take a lock.
833 	 *
834 	 * TODO: Make the incoming packet handler check this and handle
835 	 * terminal retransmission without requiring access to the call.
836 	 */
837 	if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
838 		_debug("exposed %u,%u", call->call_id, call->abort_code);
839 		__rxrpc_disconnect_call(conn, call);
840 
841 		if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
842 			trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
843 			bundle->try_upgrade = false;
844 			if (may_reuse)
845 				rxrpc_activate_channels_locked(bundle);
846 		}
847 
848 	}
849 
850 	/* See if we can pass the channel directly to another call. */
851 	if (may_reuse && !list_empty(&bundle->waiting_calls)) {
852 		trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
853 		rxrpc_activate_one_channel(conn, channel);
854 		goto out;
855 	}
856 
857 	/* Schedule the final ACK to be transmitted in a short while so that it
858 	 * can be skipped if we find a follow-on call.  The first DATA packet
859 	 * of the follow on call will implicitly ACK this call.
860 	 */
861 	if (call->completion == RXRPC_CALL_SUCCEEDED &&
862 	    test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
863 		unsigned long final_ack_at = jiffies + 2;
864 
865 		WRITE_ONCE(chan->final_ack_at, final_ack_at);
866 		smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
867 		set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
868 		rxrpc_reduce_conn_timer(conn, final_ack_at);
869 	}
870 
871 	/* Deactivate the channel. */
872 	rcu_assign_pointer(chan->call, NULL);
873 	set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
874 	conn->act_chans	&= ~(1 << channel);
875 
876 	/* If no channels remain active, then put the connection on the idle
877 	 * list for a short while.  Give it a ref to stop it going away if it
878 	 * becomes unbundled.
879 	 */
880 	if (!conn->act_chans) {
881 		trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
882 		conn->idle_timestamp = jiffies;
883 
884 		rxrpc_get_connection(conn);
885 		spin_lock(&rxnet->client_conn_cache_lock);
886 		list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
887 		spin_unlock(&rxnet->client_conn_cache_lock);
888 
889 		rxrpc_set_client_reap_timer(rxnet);
890 	}
891 
892 out:
893 	spin_unlock(&bundle->channel_lock);
894 	_leave("");
895 	return;
896 }
897 
898 /*
899  * Remove a connection from a bundle.
900  */
901 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
902 {
903 	struct rxrpc_bundle *bundle = conn->bundle;
904 	struct rxrpc_local *local = bundle->params.local;
905 	unsigned int bindex;
906 	bool need_drop = false, need_put = false;
907 	int i;
908 
909 	_enter("C=%x", conn->debug_id);
910 
911 	if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
912 		rxrpc_process_delayed_final_acks(conn, true);
913 
914 	spin_lock(&bundle->channel_lock);
915 	bindex = conn->bundle_shift / RXRPC_MAXCALLS;
916 	if (bundle->conns[bindex] == conn) {
917 		_debug("clear slot %u", bindex);
918 		bundle->conns[bindex] = NULL;
919 		for (i = 0; i < RXRPC_MAXCALLS; i++)
920 			clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
921 		need_drop = true;
922 	}
923 	spin_unlock(&bundle->channel_lock);
924 
925 	/* If there are no more connections, remove the bundle */
926 	if (!bundle->avail_chans) {
927 		_debug("maybe unbundle");
928 		spin_lock(&local->client_bundles_lock);
929 
930 		for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
931 			if (bundle->conns[i])
932 				break;
933 		if (i == ARRAY_SIZE(bundle->conns) && !bundle->params.exclusive) {
934 			_debug("erase bundle");
935 			rb_erase(&bundle->local_node, &local->client_bundles);
936 			need_put = true;
937 		}
938 
939 		spin_unlock(&local->client_bundles_lock);
940 		if (need_put)
941 			rxrpc_put_bundle(bundle);
942 	}
943 
944 	if (need_drop)
945 		rxrpc_put_connection(conn);
946 	_leave("");
947 }
948 
949 /*
950  * Clean up a dead client connection.
951  */
952 static void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
953 {
954 	struct rxrpc_local *local = conn->params.local;
955 	struct rxrpc_net *rxnet = local->rxnet;
956 
957 	_enter("C=%x", conn->debug_id);
958 
959 	trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
960 	atomic_dec(&rxnet->nr_client_conns);
961 
962 	rxrpc_put_client_connection_id(conn);
963 	rxrpc_kill_connection(conn);
964 }
965 
966 /*
967  * Clean up a dead client connections.
968  */
969 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
970 {
971 	const void *here = __builtin_return_address(0);
972 	unsigned int debug_id = conn->debug_id;
973 	bool dead;
974 	int r;
975 
976 	dead = __refcount_dec_and_test(&conn->ref, &r);
977 	trace_rxrpc_conn(debug_id, rxrpc_conn_put_client, r - 1, here);
978 	if (dead)
979 		rxrpc_kill_client_conn(conn);
980 }
981 
982 /*
983  * Discard expired client connections from the idle list.  Each conn in the
984  * idle list has been exposed and holds an extra ref because of that.
985  *
986  * This may be called from conn setup or from a work item so cannot be
987  * considered non-reentrant.
988  */
989 void rxrpc_discard_expired_client_conns(struct work_struct *work)
990 {
991 	struct rxrpc_connection *conn;
992 	struct rxrpc_net *rxnet =
993 		container_of(work, struct rxrpc_net, client_conn_reaper);
994 	unsigned long expiry, conn_expires_at, now;
995 	unsigned int nr_conns;
996 
997 	_enter("");
998 
999 	if (list_empty(&rxnet->idle_client_conns)) {
1000 		_leave(" [empty]");
1001 		return;
1002 	}
1003 
1004 	/* Don't double up on the discarding */
1005 	if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
1006 		_leave(" [already]");
1007 		return;
1008 	}
1009 
1010 	/* We keep an estimate of what the number of conns ought to be after
1011 	 * we've discarded some so that we don't overdo the discarding.
1012 	 */
1013 	nr_conns = atomic_read(&rxnet->nr_client_conns);
1014 
1015 next:
1016 	spin_lock(&rxnet->client_conn_cache_lock);
1017 
1018 	if (list_empty(&rxnet->idle_client_conns))
1019 		goto out;
1020 
1021 	conn = list_entry(rxnet->idle_client_conns.next,
1022 			  struct rxrpc_connection, cache_link);
1023 
1024 	if (!rxnet->kill_all_client_conns) {
1025 		/* If the number of connections is over the reap limit, we
1026 		 * expedite discard by reducing the expiry timeout.  We must,
1027 		 * however, have at least a short grace period to be able to do
1028 		 * final-ACK or ABORT retransmission.
1029 		 */
1030 		expiry = rxrpc_conn_idle_client_expiry;
1031 		if (nr_conns > rxrpc_reap_client_connections)
1032 			expiry = rxrpc_conn_idle_client_fast_expiry;
1033 		if (conn->params.local->service_closed)
1034 			expiry = rxrpc_closed_conn_expiry * HZ;
1035 
1036 		conn_expires_at = conn->idle_timestamp + expiry;
1037 
1038 		now = READ_ONCE(jiffies);
1039 		if (time_after(conn_expires_at, now))
1040 			goto not_yet_expired;
1041 	}
1042 
1043 	trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1044 	list_del_init(&conn->cache_link);
1045 
1046 	spin_unlock(&rxnet->client_conn_cache_lock);
1047 
1048 	rxrpc_unbundle_conn(conn);
1049 	rxrpc_put_connection(conn); /* Drop the ->cache_link ref */
1050 
1051 	nr_conns--;
1052 	goto next;
1053 
1054 not_yet_expired:
1055 	/* The connection at the front of the queue hasn't yet expired, so
1056 	 * schedule the work item for that point if we discarded something.
1057 	 *
1058 	 * We don't worry if the work item is already scheduled - it can look
1059 	 * after rescheduling itself at a later time.  We could cancel it, but
1060 	 * then things get messier.
1061 	 */
1062 	_debug("not yet");
1063 	if (!rxnet->kill_all_client_conns)
1064 		timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1065 
1066 out:
1067 	spin_unlock(&rxnet->client_conn_cache_lock);
1068 	spin_unlock(&rxnet->client_conn_discard_lock);
1069 	_leave("");
1070 }
1071 
1072 /*
1073  * Preemptively destroy all the client connection records rather than waiting
1074  * for them to time out
1075  */
1076 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1077 {
1078 	_enter("");
1079 
1080 	spin_lock(&rxnet->client_conn_cache_lock);
1081 	rxnet->kill_all_client_conns = true;
1082 	spin_unlock(&rxnet->client_conn_cache_lock);
1083 
1084 	del_timer_sync(&rxnet->client_conn_reap_timer);
1085 
1086 	if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1087 		_debug("destroy: queue failed");
1088 
1089 	_leave("");
1090 }
1091 
1092 /*
1093  * Clean up the client connections on a local endpoint.
1094  */
1095 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1096 {
1097 	struct rxrpc_connection *conn, *tmp;
1098 	struct rxrpc_net *rxnet = local->rxnet;
1099 	LIST_HEAD(graveyard);
1100 
1101 	_enter("");
1102 
1103 	spin_lock(&rxnet->client_conn_cache_lock);
1104 
1105 	list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1106 				 cache_link) {
1107 		if (conn->params.local == local) {
1108 			trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1109 			list_move(&conn->cache_link, &graveyard);
1110 		}
1111 	}
1112 
1113 	spin_unlock(&rxnet->client_conn_cache_lock);
1114 
1115 	while (!list_empty(&graveyard)) {
1116 		conn = list_entry(graveyard.next,
1117 				  struct rxrpc_connection, cache_link);
1118 		list_del_init(&conn->cache_link);
1119 		rxrpc_unbundle_conn(conn);
1120 		rxrpc_put_connection(conn);
1121 	}
1122 
1123 	_leave(" [culled]");
1124 }
1125