xref: /linux/net/rxrpc/conn_client.c (revision 9ee0034b8f49aaaa7e7c2da8db1038915db99c19)
1 /* Client connection-specific management code.
2  *
3  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  *
11  *
12  * Client connections need to be cached for a little while after they've made a
13  * call so as to handle retransmitted DATA packets in case the server didn't
14  * receive the final ACK or terminating ABORT we sent it.
15  *
16  * Client connections can be in one of a number of cache states:
17  *
18  *  (1) INACTIVE - The connection is not held in any list and may not have been
19  *      exposed to the world.  If it has been previously exposed, it was
20  *      discarded from the idle list after expiring.
21  *
22  *  (2) WAITING - The connection is waiting for the number of client conns to
23  *      drop below the maximum capacity.  Calls may be in progress upon it from
24  *      when it was active and got culled.
25  *
26  *	The connection is on the rxrpc_waiting_client_conns list which is kept
27  *	in to-be-granted order.  Culled conns with waiters go to the back of
28  *	the queue just like new conns.
29  *
30  *  (3) ACTIVE - The connection has at least one call in progress upon it, it
31  *      may freely grant available channels to new calls and calls may be
32  *      waiting on it for channels to become available.
33  *
34  *	The connection is on the rxrpc_active_client_conns list which is kept
35  *	in activation order for culling purposes.
36  *
37  *	rxrpc_nr_active_client_conns is held incremented also.
38  *
39  *  (4) CULLED - The connection got summarily culled to try and free up
40  *      capacity.  Calls currently in progress on the connection are allowed to
41  *      continue, but new calls will have to wait.  There can be no waiters in
42  *      this state - the conn would have to go to the WAITING state instead.
43  *
44  *  (5) IDLE - The connection has no calls in progress upon it and must have
45  *      been exposed to the world (ie. the EXPOSED flag must be set).  When it
46  *      expires, the EXPOSED flag is cleared and the connection transitions to
47  *      the INACTIVE state.
48  *
49  *	The connection is on the rxrpc_idle_client_conns list which is kept in
50  *	order of how soon they'll expire.
51  *
52  * There are flags of relevance to the cache:
53  *
54  *  (1) EXPOSED - The connection ID got exposed to the world.  If this flag is
55  *      set, an extra ref is added to the connection preventing it from being
56  *      reaped when it has no calls outstanding.  This flag is cleared and the
57  *      ref dropped when a conn is discarded from the idle list.
58  *
59  *      This allows us to move terminal call state retransmission to the
60  *      connection and to discard the call immediately we think it is done
61  *      with.  It also give us a chance to reuse the connection.
62  *
63  *  (2) DONT_REUSE - The connection should be discarded as soon as possible and
64  *      should not be reused.  This is set when an exclusive connection is used
65  *      or a call ID counter overflows.
66  *
67  * The caching state may only be changed if the cache lock is held.
68  *
69  * There are two idle client connection expiry durations.  If the total number
70  * of connections is below the reap threshold, we use the normal duration; if
71  * it's above, we use the fast duration.
72  */
73 
74 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
75 
76 #include <linux/slab.h>
77 #include <linux/idr.h>
78 #include <linux/timer.h>
79 #include "ar-internal.h"
80 
81 __read_mostly unsigned int rxrpc_max_client_connections = 1000;
82 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
83 __read_mostly unsigned int rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
84 __read_mostly unsigned int rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
85 
86 static unsigned int rxrpc_nr_client_conns;
87 static unsigned int rxrpc_nr_active_client_conns;
88 static __read_mostly bool rxrpc_kill_all_client_conns;
89 
90 static DEFINE_SPINLOCK(rxrpc_client_conn_cache_lock);
91 static DEFINE_SPINLOCK(rxrpc_client_conn_discard_mutex);
92 static LIST_HEAD(rxrpc_waiting_client_conns);
93 static LIST_HEAD(rxrpc_active_client_conns);
94 static LIST_HEAD(rxrpc_idle_client_conns);
95 
96 /*
97  * We use machine-unique IDs for our client connections.
98  */
99 DEFINE_IDR(rxrpc_client_conn_ids);
100 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
101 
102 static void rxrpc_cull_active_client_conns(void);
103 static void rxrpc_discard_expired_client_conns(struct work_struct *);
104 
105 static DECLARE_DELAYED_WORK(rxrpc_client_conn_reap,
106 			    rxrpc_discard_expired_client_conns);
107 
108 /*
109  * Get a connection ID and epoch for a client connection from the global pool.
110  * The connection struct pointer is then recorded in the idr radix tree.  The
111  * epoch doesn't change until the client is rebooted (or, at least, unless the
112  * module is unloaded).
113  */
114 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
115 					  gfp_t gfp)
116 {
117 	int id;
118 
119 	_enter("");
120 
121 	idr_preload(gfp);
122 	spin_lock(&rxrpc_conn_id_lock);
123 
124 	id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
125 			      1, 0x40000000, GFP_NOWAIT);
126 	if (id < 0)
127 		goto error;
128 
129 	spin_unlock(&rxrpc_conn_id_lock);
130 	idr_preload_end();
131 
132 	conn->proto.epoch = rxrpc_epoch;
133 	conn->proto.cid = id << RXRPC_CIDSHIFT;
134 	set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
135 	_leave(" [CID %x]", conn->proto.cid);
136 	return 0;
137 
138 error:
139 	spin_unlock(&rxrpc_conn_id_lock);
140 	idr_preload_end();
141 	_leave(" = %d", id);
142 	return id;
143 }
144 
145 /*
146  * Release a connection ID for a client connection from the global pool.
147  */
148 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
149 {
150 	if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
151 		spin_lock(&rxrpc_conn_id_lock);
152 		idr_remove(&rxrpc_client_conn_ids,
153 			   conn->proto.cid >> RXRPC_CIDSHIFT);
154 		spin_unlock(&rxrpc_conn_id_lock);
155 	}
156 }
157 
158 /*
159  * Destroy the client connection ID tree.
160  */
161 void rxrpc_destroy_client_conn_ids(void)
162 {
163 	struct rxrpc_connection *conn;
164 	int id;
165 
166 	if (!idr_is_empty(&rxrpc_client_conn_ids)) {
167 		idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
168 			pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
169 			       conn, atomic_read(&conn->usage));
170 		}
171 		BUG();
172 	}
173 
174 	idr_destroy(&rxrpc_client_conn_ids);
175 }
176 
177 /*
178  * Allocate a client connection.
179  */
180 static struct rxrpc_connection *
181 rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
182 {
183 	struct rxrpc_connection *conn;
184 	int ret;
185 
186 	_enter("");
187 
188 	conn = rxrpc_alloc_connection(gfp);
189 	if (!conn) {
190 		_leave(" = -ENOMEM");
191 		return ERR_PTR(-ENOMEM);
192 	}
193 
194 	atomic_set(&conn->usage, 1);
195 	if (conn->params.exclusive)
196 		__set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
197 
198 	conn->params		= *cp;
199 	conn->out_clientflag	= RXRPC_CLIENT_INITIATED;
200 	conn->state		= RXRPC_CONN_CLIENT;
201 
202 	ret = rxrpc_get_client_connection_id(conn, gfp);
203 	if (ret < 0)
204 		goto error_0;
205 
206 	ret = rxrpc_init_client_conn_security(conn);
207 	if (ret < 0)
208 		goto error_1;
209 
210 	ret = conn->security->prime_packet_security(conn);
211 	if (ret < 0)
212 		goto error_2;
213 
214 	write_lock(&rxrpc_connection_lock);
215 	list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list);
216 	write_unlock(&rxrpc_connection_lock);
217 
218 	/* We steal the caller's peer ref. */
219 	cp->peer = NULL;
220 	rxrpc_get_local(conn->params.local);
221 	key_get(conn->params.key);
222 
223 	_leave(" = %p", conn);
224 	return conn;
225 
226 error_2:
227 	conn->security->clear(conn);
228 error_1:
229 	rxrpc_put_client_connection_id(conn);
230 error_0:
231 	kfree(conn);
232 	_leave(" = %d", ret);
233 	return ERR_PTR(ret);
234 }
235 
236 /*
237  * Determine if a connection may be reused.
238  */
239 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
240 {
241 	int id_cursor, id, distance, limit;
242 
243 	if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
244 		goto dont_reuse;
245 
246 	if (conn->proto.epoch != rxrpc_epoch)
247 		goto mark_dont_reuse;
248 
249 	/* The IDR tree gets very expensive on memory if the connection IDs are
250 	 * widely scattered throughout the number space, so we shall want to
251 	 * kill off connections that, say, have an ID more than about four
252 	 * times the maximum number of client conns away from the current
253 	 * allocation point to try and keep the IDs concentrated.
254 	 */
255 	id_cursor = READ_ONCE(rxrpc_client_conn_ids.cur);
256 	id = conn->proto.cid >> RXRPC_CIDSHIFT;
257 	distance = id - id_cursor;
258 	if (distance < 0)
259 		distance = -distance;
260 	limit = round_up(rxrpc_max_client_connections, IDR_SIZE) * 4;
261 	if (distance > limit)
262 		goto mark_dont_reuse;
263 
264 	return true;
265 
266 mark_dont_reuse:
267 	set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
268 dont_reuse:
269 	return false;
270 }
271 
272 /*
273  * Create or find a client connection to use for a call.
274  *
275  * If we return with a connection, the call will be on its waiting list.  It's
276  * left to the caller to assign a channel and wake up the call.
277  */
278 static int rxrpc_get_client_conn(struct rxrpc_call *call,
279 				 struct rxrpc_conn_parameters *cp,
280 				 struct sockaddr_rxrpc *srx,
281 				 gfp_t gfp)
282 {
283 	struct rxrpc_connection *conn, *candidate = NULL;
284 	struct rxrpc_local *local = cp->local;
285 	struct rb_node *p, **pp, *parent;
286 	long diff;
287 	int ret = -ENOMEM;
288 
289 	_enter("{%d,%lx},", call->debug_id, call->user_call_ID);
290 
291 	cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
292 	if (!cp->peer)
293 		goto error;
294 
295 	/* If the connection is not meant to be exclusive, search the available
296 	 * connections to see if the connection we want to use already exists.
297 	 */
298 	if (!cp->exclusive) {
299 		_debug("search 1");
300 		spin_lock(&local->client_conns_lock);
301 		p = local->client_conns.rb_node;
302 		while (p) {
303 			conn = rb_entry(p, struct rxrpc_connection, client_node);
304 
305 #define cmp(X) ((long)conn->params.X - (long)cp->X)
306 			diff = (cmp(peer) ?:
307 				cmp(key) ?:
308 				cmp(security_level));
309 #undef cmp
310 			if (diff < 0) {
311 				p = p->rb_left;
312 			} else if (diff > 0) {
313 				p = p->rb_right;
314 			} else {
315 				if (rxrpc_may_reuse_conn(conn) &&
316 				    rxrpc_get_connection_maybe(conn))
317 					goto found_extant_conn;
318 				/* The connection needs replacing.  It's better
319 				 * to effect that when we have something to
320 				 * replace it with so that we don't have to
321 				 * rebalance the tree twice.
322 				 */
323 				break;
324 			}
325 		}
326 		spin_unlock(&local->client_conns_lock);
327 	}
328 
329 	/* There wasn't a connection yet or we need an exclusive connection.
330 	 * We need to create a candidate and then potentially redo the search
331 	 * in case we're racing with another thread also trying to connect on a
332 	 * shareable connection.
333 	 */
334 	_debug("new conn");
335 	candidate = rxrpc_alloc_client_connection(cp, gfp);
336 	if (IS_ERR(candidate)) {
337 		ret = PTR_ERR(candidate);
338 		goto error_peer;
339 	}
340 
341 	/* Add the call to the new connection's waiting list in case we're
342 	 * going to have to wait for the connection to come live.  It's our
343 	 * connection, so we want first dibs on the channel slots.  We would
344 	 * normally have to take channel_lock but we do this before anyone else
345 	 * can see the connection.
346 	 */
347 	list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
348 
349 	if (cp->exclusive) {
350 		call->conn = candidate;
351 		call->security_ix = candidate->security_ix;
352 		_leave(" = 0 [exclusive %d]", candidate->debug_id);
353 		return 0;
354 	}
355 
356 	/* Publish the new connection for userspace to find.  We need to redo
357 	 * the search before doing this lest we race with someone else adding a
358 	 * conflicting instance.
359 	 */
360 	_debug("search 2");
361 	spin_lock(&local->client_conns_lock);
362 
363 	pp = &local->client_conns.rb_node;
364 	parent = NULL;
365 	while (*pp) {
366 		parent = *pp;
367 		conn = rb_entry(parent, struct rxrpc_connection, client_node);
368 
369 #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
370 		diff = (cmp(peer) ?:
371 			cmp(key) ?:
372 			cmp(security_level));
373 #undef cmp
374 		if (diff < 0) {
375 			pp = &(*pp)->rb_left;
376 		} else if (diff > 0) {
377 			pp = &(*pp)->rb_right;
378 		} else {
379 			if (rxrpc_may_reuse_conn(conn) &&
380 			    rxrpc_get_connection_maybe(conn))
381 				goto found_extant_conn;
382 			/* The old connection is from an outdated epoch. */
383 			_debug("replace conn");
384 			clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
385 			rb_replace_node(&conn->client_node,
386 					&candidate->client_node,
387 					&local->client_conns);
388 			goto candidate_published;
389 		}
390 	}
391 
392 	_debug("new conn");
393 	rb_link_node(&candidate->client_node, parent, pp);
394 	rb_insert_color(&candidate->client_node, &local->client_conns);
395 
396 candidate_published:
397 	set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
398 	call->conn = candidate;
399 	call->security_ix = candidate->security_ix;
400 	spin_unlock(&local->client_conns_lock);
401 	_leave(" = 0 [new %d]", candidate->debug_id);
402 	return 0;
403 
404 	/* We come here if we found a suitable connection already in existence.
405 	 * Discard any candidate we may have allocated, and try to get a
406 	 * channel on this one.
407 	 */
408 found_extant_conn:
409 	_debug("found conn");
410 	spin_unlock(&local->client_conns_lock);
411 
412 	rxrpc_put_connection(candidate);
413 	candidate = NULL;
414 
415 	spin_lock(&conn->channel_lock);
416 	call->conn = conn;
417 	call->security_ix = conn->security_ix;
418 	list_add(&call->chan_wait_link, &conn->waiting_calls);
419 	spin_unlock(&conn->channel_lock);
420 	_leave(" = 0 [extant %d]", conn->debug_id);
421 	return 0;
422 
423 error_peer:
424 	rxrpc_put_peer(cp->peer);
425 	cp->peer = NULL;
426 error:
427 	_leave(" = %d", ret);
428 	return ret;
429 }
430 
431 /*
432  * Activate a connection.
433  */
434 static void rxrpc_activate_conn(struct rxrpc_connection *conn)
435 {
436 	conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
437 	rxrpc_nr_active_client_conns++;
438 	list_move_tail(&conn->cache_link, &rxrpc_active_client_conns);
439 }
440 
441 /*
442  * Attempt to animate a connection for a new call.
443  *
444  * If it's not exclusive, the connection is in the endpoint tree, and we're in
445  * the conn's list of those waiting to grab a channel.  There is, however, a
446  * limit on the number of live connections allowed at any one time, so we may
447  * have to wait for capacity to become available.
448  *
449  * Note that a connection on the waiting queue might *also* have active
450  * channels if it has been culled to make space and then re-requested by a new
451  * call.
452  */
453 static void rxrpc_animate_client_conn(struct rxrpc_connection *conn)
454 {
455 	unsigned int nr_conns;
456 
457 	_enter("%d,%d", conn->debug_id, conn->cache_state);
458 
459 	if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE)
460 		goto out;
461 
462 	spin_lock(&rxrpc_client_conn_cache_lock);
463 
464 	nr_conns = rxrpc_nr_client_conns;
465 	if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags))
466 		rxrpc_nr_client_conns = nr_conns + 1;
467 
468 	switch (conn->cache_state) {
469 	case RXRPC_CONN_CLIENT_ACTIVE:
470 	case RXRPC_CONN_CLIENT_WAITING:
471 		break;
472 
473 	case RXRPC_CONN_CLIENT_INACTIVE:
474 	case RXRPC_CONN_CLIENT_CULLED:
475 	case RXRPC_CONN_CLIENT_IDLE:
476 		if (nr_conns >= rxrpc_max_client_connections)
477 			goto wait_for_capacity;
478 		goto activate_conn;
479 
480 	default:
481 		BUG();
482 	}
483 
484 out_unlock:
485 	spin_unlock(&rxrpc_client_conn_cache_lock);
486 out:
487 	_leave(" [%d]", conn->cache_state);
488 	return;
489 
490 activate_conn:
491 	_debug("activate");
492 	rxrpc_activate_conn(conn);
493 	goto out_unlock;
494 
495 wait_for_capacity:
496 	_debug("wait");
497 	conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
498 	list_move_tail(&conn->cache_link, &rxrpc_waiting_client_conns);
499 	goto out_unlock;
500 }
501 
502 /*
503  * Deactivate a channel.
504  */
505 static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
506 					 unsigned int channel)
507 {
508 	struct rxrpc_channel *chan = &conn->channels[channel];
509 
510 	rcu_assign_pointer(chan->call, NULL);
511 	conn->active_chans &= ~(1 << channel);
512 }
513 
514 /*
515  * Assign a channel to the call at the front of the queue and wake the call up.
516  * We don't increment the callNumber counter until this number has been exposed
517  * to the world.
518  */
519 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
520 				       unsigned int channel)
521 {
522 	struct rxrpc_channel *chan = &conn->channels[channel];
523 	struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
524 					     struct rxrpc_call, chan_wait_link);
525 	u32 call_id = chan->call_counter + 1;
526 
527 	write_lock_bh(&call->state_lock);
528 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
529 	write_unlock_bh(&call->state_lock);
530 
531 	rxrpc_see_call(call);
532 	list_del_init(&call->chan_wait_link);
533 	conn->active_chans |= 1 << channel;
534 	call->peer	= rxrpc_get_peer(conn->params.peer);
535 	call->cid	= conn->proto.cid | channel;
536 	call->call_id	= call_id;
537 
538 	_net("CONNECT call %08x:%08x as call %d on conn %d",
539 	     call->cid, call->call_id, call->debug_id, conn->debug_id);
540 
541 	/* Paired with the read barrier in rxrpc_wait_for_channel().  This
542 	 * orders cid and epoch in the connection wrt to call_id without the
543 	 * need to take the channel_lock.
544 	 *
545 	 * We provisionally assign a callNumber at this point, but we don't
546 	 * confirm it until the call is about to be exposed.
547 	 *
548 	 * TODO: Pair with a barrier in the data_ready handler when that looks
549 	 * at the call ID through a connection channel.
550 	 */
551 	smp_wmb();
552 	chan->call_id	= call_id;
553 	rcu_assign_pointer(chan->call, call);
554 	wake_up(&call->waitq);
555 }
556 
557 /*
558  * Assign channels and callNumbers to waiting calls.
559  */
560 static void rxrpc_activate_channels(struct rxrpc_connection *conn)
561 {
562 	unsigned char mask;
563 
564 	_enter("%d", conn->debug_id);
565 
566 	if (conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE ||
567 	    conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
568 		return;
569 
570 	spin_lock(&conn->channel_lock);
571 
572 	while (!list_empty(&conn->waiting_calls) &&
573 	       (mask = ~conn->active_chans,
574 		mask &= RXRPC_ACTIVE_CHANS_MASK,
575 		mask != 0))
576 		rxrpc_activate_one_channel(conn, __ffs(mask));
577 
578 	spin_unlock(&conn->channel_lock);
579 	_leave("");
580 }
581 
582 /*
583  * Wait for a callNumber and a channel to be granted to a call.
584  */
585 static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
586 {
587 	int ret = 0;
588 
589 	_enter("%d", call->debug_id);
590 
591 	if (!call->call_id) {
592 		DECLARE_WAITQUEUE(myself, current);
593 
594 		if (!gfpflags_allow_blocking(gfp)) {
595 			ret = -EAGAIN;
596 			goto out;
597 		}
598 
599 		add_wait_queue_exclusive(&call->waitq, &myself);
600 		for (;;) {
601 			set_current_state(TASK_INTERRUPTIBLE);
602 			if (call->call_id)
603 				break;
604 			if (signal_pending(current)) {
605 				ret = -ERESTARTSYS;
606 				break;
607 			}
608 			schedule();
609 		}
610 		remove_wait_queue(&call->waitq, &myself);
611 		__set_current_state(TASK_RUNNING);
612 	}
613 
614 	/* Paired with the write barrier in rxrpc_activate_one_channel(). */
615 	smp_rmb();
616 
617 out:
618 	_leave(" = %d", ret);
619 	return ret;
620 }
621 
622 /*
623  * find a connection for a call
624  * - called in process context with IRQs enabled
625  */
626 int rxrpc_connect_call(struct rxrpc_call *call,
627 		       struct rxrpc_conn_parameters *cp,
628 		       struct sockaddr_rxrpc *srx,
629 		       gfp_t gfp)
630 {
631 	int ret;
632 
633 	_enter("{%d,%lx},", call->debug_id, call->user_call_ID);
634 
635 	rxrpc_discard_expired_client_conns(NULL);
636 	rxrpc_cull_active_client_conns();
637 
638 	ret = rxrpc_get_client_conn(call, cp, srx, gfp);
639 	if (ret < 0)
640 		return ret;
641 
642 	rxrpc_animate_client_conn(call->conn);
643 	rxrpc_activate_channels(call->conn);
644 
645 	ret = rxrpc_wait_for_channel(call, gfp);
646 	if (ret < 0)
647 		rxrpc_disconnect_client_call(call);
648 
649 	_leave(" = %d", ret);
650 	return ret;
651 }
652 
653 /*
654  * Note that a connection is about to be exposed to the world.  Once it is
655  * exposed, we maintain an extra ref on it that stops it from being summarily
656  * discarded before it's (a) had a chance to deal with retransmission and (b)
657  * had a chance at re-use (the per-connection security negotiation is
658  * expensive).
659  */
660 static void rxrpc_expose_client_conn(struct rxrpc_connection *conn)
661 {
662 	if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags))
663 		rxrpc_get_connection(conn);
664 }
665 
666 /*
667  * Note that a call, and thus a connection, is about to be exposed to the
668  * world.
669  */
670 void rxrpc_expose_client_call(struct rxrpc_call *call)
671 {
672 	struct rxrpc_connection *conn = call->conn;
673 	struct rxrpc_channel *chan =
674 		&conn->channels[call->cid & RXRPC_CHANNELMASK];
675 
676 	if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
677 		/* Mark the call ID as being used.  If the callNumber counter
678 		 * exceeds ~2 billion, we kill the connection after its
679 		 * outstanding calls have finished so that the counter doesn't
680 		 * wrap.
681 		 */
682 		chan->call_counter++;
683 		if (chan->call_counter >= INT_MAX)
684 			set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
685 		rxrpc_expose_client_conn(conn);
686 	}
687 }
688 
689 /*
690  * Disconnect a client call.
691  */
692 void rxrpc_disconnect_client_call(struct rxrpc_call *call)
693 {
694 	unsigned int channel = call->cid & RXRPC_CHANNELMASK;
695 	struct rxrpc_connection *conn = call->conn;
696 	struct rxrpc_channel *chan = &conn->channels[channel];
697 
698 	call->conn = NULL;
699 
700 	spin_lock(&conn->channel_lock);
701 
702 	/* Calls that have never actually been assigned a channel can simply be
703 	 * discarded.  If the conn didn't get used either, it will follow
704 	 * immediately unless someone else grabs it in the meantime.
705 	 */
706 	if (!list_empty(&call->chan_wait_link)) {
707 		_debug("call is waiting");
708 		ASSERTCMP(call->call_id, ==, 0);
709 		ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
710 		list_del_init(&call->chan_wait_link);
711 
712 		/* We must deactivate or idle the connection if it's now
713 		 * waiting for nothing.
714 		 */
715 		spin_lock(&rxrpc_client_conn_cache_lock);
716 		if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
717 		    list_empty(&conn->waiting_calls) &&
718 		    !conn->active_chans)
719 			goto idle_connection;
720 		goto out;
721 	}
722 
723 	ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
724 	ASSERTCMP(atomic_read(&conn->usage), >=, 2);
725 
726 	/* If a client call was exposed to the world, we save the result for
727 	 * retransmission.
728 	 *
729 	 * We use a barrier here so that the call number and abort code can be
730 	 * read without needing to take a lock.
731 	 *
732 	 * TODO: Make the incoming packet handler check this and handle
733 	 * terminal retransmission without requiring access to the call.
734 	 */
735 	if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
736 		_debug("exposed %u,%u", call->call_id, call->abort_code);
737 		__rxrpc_disconnect_call(conn, call);
738 	}
739 
740 	/* See if we can pass the channel directly to another call. */
741 	if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
742 	    !list_empty(&conn->waiting_calls)) {
743 		_debug("pass chan");
744 		rxrpc_activate_one_channel(conn, channel);
745 		goto out_2;
746 	}
747 
748 	/* Things are more complex and we need the cache lock.  We might be
749 	 * able to simply idle the conn or it might now be lurking on the wait
750 	 * list.  It might even get moved back to the active list whilst we're
751 	 * waiting for the lock.
752 	 */
753 	spin_lock(&rxrpc_client_conn_cache_lock);
754 
755 	switch (conn->cache_state) {
756 	case RXRPC_CONN_CLIENT_ACTIVE:
757 		if (list_empty(&conn->waiting_calls)) {
758 			rxrpc_deactivate_one_channel(conn, channel);
759 			if (!conn->active_chans) {
760 				rxrpc_nr_active_client_conns--;
761 				goto idle_connection;
762 			}
763 			goto out;
764 		}
765 
766 		_debug("pass chan 2");
767 		rxrpc_activate_one_channel(conn, channel);
768 		goto out;
769 
770 	case RXRPC_CONN_CLIENT_CULLED:
771 		rxrpc_deactivate_one_channel(conn, channel);
772 		ASSERT(list_empty(&conn->waiting_calls));
773 		if (!conn->active_chans)
774 			goto idle_connection;
775 		goto out;
776 
777 	case RXRPC_CONN_CLIENT_WAITING:
778 		rxrpc_deactivate_one_channel(conn, channel);
779 		goto out;
780 
781 	default:
782 		BUG();
783 	}
784 
785 out:
786 	spin_unlock(&rxrpc_client_conn_cache_lock);
787 out_2:
788 	spin_unlock(&conn->channel_lock);
789 	rxrpc_put_connection(conn);
790 	_leave("");
791 	return;
792 
793 idle_connection:
794 	/* As no channels remain active, the connection gets deactivated
795 	 * immediately or moved to the idle list for a short while.
796 	 */
797 	if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
798 		_debug("make idle");
799 		conn->idle_timestamp = jiffies;
800 		conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
801 		list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns);
802 		if (rxrpc_idle_client_conns.next == &conn->cache_link &&
803 		    !rxrpc_kill_all_client_conns)
804 			queue_delayed_work(rxrpc_workqueue,
805 					   &rxrpc_client_conn_reap,
806 					   rxrpc_conn_idle_client_expiry);
807 	} else {
808 		_debug("make inactive");
809 		conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
810 		list_del_init(&conn->cache_link);
811 	}
812 	goto out;
813 }
814 
815 /*
816  * Clean up a dead client connection.
817  */
818 static struct rxrpc_connection *
819 rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
820 {
821 	struct rxrpc_connection *next;
822 	struct rxrpc_local *local = conn->params.local;
823 	unsigned int nr_conns;
824 
825 	if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
826 		spin_lock(&local->client_conns_lock);
827 		if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
828 				       &conn->flags))
829 			rb_erase(&conn->client_node, &local->client_conns);
830 		spin_unlock(&local->client_conns_lock);
831 	}
832 
833 	rxrpc_put_client_connection_id(conn);
834 
835 	ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
836 
837 	if (!test_bit(RXRPC_CONN_COUNTED, &conn->flags))
838 		return NULL;
839 
840 	spin_lock(&rxrpc_client_conn_cache_lock);
841 	nr_conns = --rxrpc_nr_client_conns;
842 
843 	next = NULL;
844 	if (nr_conns < rxrpc_max_client_connections &&
845 	    !list_empty(&rxrpc_waiting_client_conns)) {
846 		next = list_entry(rxrpc_waiting_client_conns.next,
847 				  struct rxrpc_connection, cache_link);
848 		rxrpc_get_connection(next);
849 		rxrpc_activate_conn(next);
850 	}
851 
852 	spin_unlock(&rxrpc_client_conn_cache_lock);
853 	rxrpc_kill_connection(conn);
854 
855 	if (next)
856 		rxrpc_activate_channels(next);
857 
858 	/* We need to get rid of the temporary ref we took upon next, but we
859 	 * can't call rxrpc_put_connection() recursively.
860 	 */
861 	return next;
862 }
863 
864 /*
865  * Clean up a dead client connections.
866  */
867 void rxrpc_put_client_conn(struct rxrpc_connection *conn)
868 {
869 	struct rxrpc_connection *next;
870 
871 	do {
872 		_enter("%p{u=%d,d=%d}",
873 		       conn, atomic_read(&conn->usage), conn->debug_id);
874 
875 		next = rxrpc_put_one_client_conn(conn);
876 
877 		if (!next)
878 			break;
879 		conn = next;
880 	} while (atomic_dec_and_test(&conn->usage));
881 
882 	_leave("");
883 }
884 
885 /*
886  * Kill the longest-active client connections to make room for new ones.
887  */
888 static void rxrpc_cull_active_client_conns(void)
889 {
890 	struct rxrpc_connection *conn;
891 	unsigned int nr_conns = rxrpc_nr_client_conns;
892 	unsigned int nr_active, limit;
893 
894 	_enter("");
895 
896 	ASSERTCMP(nr_conns, >=, 0);
897 	if (nr_conns < rxrpc_max_client_connections) {
898 		_leave(" [ok]");
899 		return;
900 	}
901 	limit = rxrpc_reap_client_connections;
902 
903 	spin_lock(&rxrpc_client_conn_cache_lock);
904 	nr_active = rxrpc_nr_active_client_conns;
905 
906 	while (nr_active > limit) {
907 		ASSERT(!list_empty(&rxrpc_active_client_conns));
908 		conn = list_entry(rxrpc_active_client_conns.next,
909 				  struct rxrpc_connection, cache_link);
910 		ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE);
911 
912 		if (list_empty(&conn->waiting_calls)) {
913 			conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
914 			list_del_init(&conn->cache_link);
915 		} else {
916 			conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
917 			list_move_tail(&conn->cache_link,
918 				       &rxrpc_waiting_client_conns);
919 		}
920 
921 		nr_active--;
922 	}
923 
924 	rxrpc_nr_active_client_conns = nr_active;
925 	spin_unlock(&rxrpc_client_conn_cache_lock);
926 	ASSERTCMP(nr_active, >=, 0);
927 	_leave(" [culled]");
928 }
929 
930 /*
931  * Discard expired client connections from the idle list.  Each conn in the
932  * idle list has been exposed and holds an extra ref because of that.
933  *
934  * This may be called from conn setup or from a work item so cannot be
935  * considered non-reentrant.
936  */
937 static void rxrpc_discard_expired_client_conns(struct work_struct *work)
938 {
939 	struct rxrpc_connection *conn;
940 	unsigned long expiry, conn_expires_at, now;
941 	unsigned int nr_conns;
942 	bool did_discard = false;
943 
944 	_enter("%c", work ? 'w' : 'n');
945 
946 	if (list_empty(&rxrpc_idle_client_conns)) {
947 		_leave(" [empty]");
948 		return;
949 	}
950 
951 	/* Don't double up on the discarding */
952 	if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) {
953 		_leave(" [already]");
954 		return;
955 	}
956 
957 	/* We keep an estimate of what the number of conns ought to be after
958 	 * we've discarded some so that we don't overdo the discarding.
959 	 */
960 	nr_conns = rxrpc_nr_client_conns;
961 
962 next:
963 	spin_lock(&rxrpc_client_conn_cache_lock);
964 
965 	if (list_empty(&rxrpc_idle_client_conns))
966 		goto out;
967 
968 	conn = list_entry(rxrpc_idle_client_conns.next,
969 			  struct rxrpc_connection, cache_link);
970 	ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
971 
972 	if (!rxrpc_kill_all_client_conns) {
973 		/* If the number of connections is over the reap limit, we
974 		 * expedite discard by reducing the expiry timeout.  We must,
975 		 * however, have at least a short grace period to be able to do
976 		 * final-ACK or ABORT retransmission.
977 		 */
978 		expiry = rxrpc_conn_idle_client_expiry;
979 		if (nr_conns > rxrpc_reap_client_connections)
980 			expiry = rxrpc_conn_idle_client_fast_expiry;
981 
982 		conn_expires_at = conn->idle_timestamp + expiry;
983 
984 		now = READ_ONCE(jiffies);
985 		if (time_after(conn_expires_at, now))
986 			goto not_yet_expired;
987 	}
988 
989 	_debug("discard conn %d", conn->debug_id);
990 	if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
991 		BUG();
992 	conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
993 	list_del_init(&conn->cache_link);
994 
995 	spin_unlock(&rxrpc_client_conn_cache_lock);
996 
997 	/* When we cleared the EXPOSED flag, we took on responsibility for the
998 	 * reference that that had on the usage count.  We deal with that here.
999 	 * If someone re-sets the flag and re-gets the ref, that's fine.
1000 	 */
1001 	rxrpc_put_connection(conn);
1002 	did_discard = true;
1003 	nr_conns--;
1004 	goto next;
1005 
1006 not_yet_expired:
1007 	/* The connection at the front of the queue hasn't yet expired, so
1008 	 * schedule the work item for that point if we discarded something.
1009 	 *
1010 	 * We don't worry if the work item is already scheduled - it can look
1011 	 * after rescheduling itself at a later time.  We could cancel it, but
1012 	 * then things get messier.
1013 	 */
1014 	_debug("not yet");
1015 	if (!rxrpc_kill_all_client_conns)
1016 		queue_delayed_work(rxrpc_workqueue,
1017 				   &rxrpc_client_conn_reap,
1018 				   conn_expires_at - now);
1019 
1020 out:
1021 	spin_unlock(&rxrpc_client_conn_cache_lock);
1022 	spin_unlock(&rxrpc_client_conn_discard_mutex);
1023 	_leave("");
1024 }
1025 
1026 /*
1027  * Preemptively destroy all the client connection records rather than waiting
1028  * for them to time out
1029  */
1030 void __exit rxrpc_destroy_all_client_connections(void)
1031 {
1032 	_enter("");
1033 
1034 	spin_lock(&rxrpc_client_conn_cache_lock);
1035 	rxrpc_kill_all_client_conns = true;
1036 	spin_unlock(&rxrpc_client_conn_cache_lock);
1037 
1038 	cancel_delayed_work(&rxrpc_client_conn_reap);
1039 
1040 	if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0))
1041 		_debug("destroy: queue failed");
1042 
1043 	_leave("");
1044 }
1045