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