blob: 8120138dac01810854c8376acdec90e3c13dd4a8 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells4a3388c2016-04-04 14:00:37 +01002/* Client connection-specific management code.
3 *
David Howells245500d2020-07-01 11:15:32 +01004 * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved.
David Howells4a3388c2016-04-04 14:00:37 +01005 * Written by David Howells (dhowells@redhat.com)
6 *
David Howells45025bc2016-08-24 07:30:52 +01007 * 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 *
David Howells45025bc2016-08-24 07:30:52 +010011 * There are flags of relevance to the cache:
12 *
David Howells45025bc2016-08-24 07:30:52 +010013 * (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.
David Howells4a3388c2016-04-04 14:00:37 +010022 */
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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010029#include <linux/sched/signal.h>
30
David Howells4a3388c2016-04-04 14:00:37 +010031#include "ar-internal.h"
32
David Howells45025bc2016-08-24 07:30:52 +010033__read_mostly unsigned int rxrpc_reap_client_connections = 900;
David Howellsa158bdd2017-11-24 10:18:41 +000034__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;
David Howells45025bc2016-08-24 07:30:52 +010036
David Howells4a3388c2016-04-04 14:00:37 +010037/*
38 * We use machine-unique IDs for our client connections.
39 */
40DEFINE_IDR(rxrpc_client_conn_ids);
41static 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
David Howells090f85d2016-09-04 13:14:46 +010046 * epoch doesn't change until the client is rebooted (or, at least, unless the
47 * module is unloaded).
David Howells4a3388c2016-04-04 14:00:37 +010048 */
David Howellsc6d2b8d2016-04-04 14:00:40 +010049static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
50 gfp_t gfp)
David Howells4a3388c2016-04-04 14:00:37 +010051{
David Howells2baec2c2017-05-24 17:02:32 +010052 struct rxrpc_net *rxnet = conn->params.local->rxnet;
David Howells4a3388c2016-04-04 14:00:37 +010053 int id;
54
55 _enter("");
56
57 idr_preload(gfp);
David Howells4a3388c2016-04-04 14:00:37 +010058 spin_lock(&rxrpc_conn_id_lock);
59
David Howells090f85d2016-09-04 13:14:46 +010060 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
61 1, 0x40000000, GFP_NOWAIT);
62 if (id < 0)
63 goto error;
David Howells4a3388c2016-04-04 14:00:37 +010064
65 spin_unlock(&rxrpc_conn_id_lock);
David Howells4a3388c2016-04-04 14:00:37 +010066 idr_preload_end();
67
David Howells2baec2c2017-05-24 17:02:32 +010068 conn->proto.epoch = rxnet->epoch;
David Howells4a3388c2016-04-04 14:00:37 +010069 conn->proto.cid = id << RXRPC_CIDSHIFT;
70 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
David Howells090f85d2016-09-04 13:14:46 +010071 _leave(" [CID %x]", conn->proto.cid);
David Howells4a3388c2016-04-04 14:00:37 +010072 return 0;
73
74error:
75 spin_unlock(&rxrpc_conn_id_lock);
David Howells4a3388c2016-04-04 14:00:37 +010076 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 */
David Howells001c1122016-06-30 10:45:22 +010084static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
David Howells4a3388c2016-04-04 14:00:37 +010085{
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}
David Howellseb9b9d22016-06-27 10:32:02 +010093
94/*
95 * Destroy the client connection ID tree.
96 */
97void 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, atomic_read(&conn->usage));
106 }
107 BUG();
108 }
109
110 idr_destroy(&rxrpc_client_conn_ids);
111}
David Howellsc6d2b8d2016-04-04 14:00:40 +0100112
113/*
David Howells245500d2020-07-01 11:15:32 +0100114 * Allocate a connection bundle.
115 */
116static 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 atomic_set(&bundle->usage, 1);
126 spin_lock_init(&bundle->channel_lock);
127 INIT_LIST_HEAD(&bundle->waiting_calls);
128 }
129 return bundle;
130}
131
132struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle)
133{
134 atomic_inc(&bundle->usage);
135 return bundle;
136}
137
Eiichi Tsukataca77fba2021-11-21 04:16:07 +0000138static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
139{
140 rxrpc_put_peer(bundle->params.peer);
141 kfree(bundle);
142}
143
David Howells245500d2020-07-01 11:15:32 +0100144void rxrpc_put_bundle(struct rxrpc_bundle *bundle)
145{
146 unsigned int d = bundle->debug_id;
147 unsigned int u = atomic_dec_return(&bundle->usage);
148
149 _debug("PUT B=%x %u", d, u);
Eiichi Tsukataca77fba2021-11-21 04:16:07 +0000150 if (u == 0)
151 rxrpc_free_bundle(bundle);
David Howells245500d2020-07-01 11:15:32 +0100152}
153
154/*
David Howells45025bc2016-08-24 07:30:52 +0100155 * Allocate a client connection.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100156 */
157static struct rxrpc_connection *
David Howells245500d2020-07-01 11:15:32 +0100158rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
David Howellsc6d2b8d2016-04-04 14:00:40 +0100159{
160 struct rxrpc_connection *conn;
David Howells245500d2020-07-01 11:15:32 +0100161 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100162 int ret;
163
164 _enter("");
165
166 conn = rxrpc_alloc_connection(gfp);
167 if (!conn) {
168 _leave(" = -ENOMEM");
169 return ERR_PTR(-ENOMEM);
170 }
171
David Howells45025bc2016-08-24 07:30:52 +0100172 atomic_set(&conn->usage, 1);
David Howells245500d2020-07-01 11:15:32 +0100173 conn->bundle = bundle;
174 conn->params = bundle->params;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100175 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
176 conn->state = RXRPC_CONN_CLIENT;
David Howells245500d2020-07-01 11:15:32 +0100177 conn->service_id = conn->params.service_id;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100178
David Howellsc6d2b8d2016-04-04 14:00:40 +0100179 ret = rxrpc_get_client_connection_id(conn, gfp);
180 if (ret < 0)
181 goto error_0;
182
183 ret = rxrpc_init_client_conn_security(conn);
184 if (ret < 0)
185 goto error_1;
186
David Howells31f5f9a162018-03-30 21:05:33 +0100187 atomic_inc(&rxnet->nr_conns);
David Howells2baec2c2017-05-24 17:02:32 +0100188 write_lock(&rxnet->conn_lock);
189 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
190 write_unlock(&rxnet->conn_lock);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100191
David Howells245500d2020-07-01 11:15:32 +0100192 rxrpc_get_bundle(bundle);
193 rxrpc_get_peer(conn->params.peer);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100194 rxrpc_get_local(conn->params.local);
195 key_get(conn->params.key);
196
David Howells4c1295d2019-10-07 10:58:29 +0100197 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_client,
198 atomic_read(&conn->usage),
David Howells363deea2016-09-17 10:49:14 +0100199 __builtin_return_address(0));
David Howells245500d2020-07-01 11:15:32 +0100200
201 atomic_inc(&rxnet->nr_client_conns);
David Howells363deea2016-09-17 10:49:14 +0100202 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100203 _leave(" = %p", conn);
204 return conn;
205
David Howellsc6d2b8d2016-04-04 14:00:40 +0100206error_1:
207 rxrpc_put_client_connection_id(conn);
208error_0:
209 kfree(conn);
210 _leave(" = %d", ret);
211 return ERR_PTR(ret);
212}
213
214/*
David Howells45025bc2016-08-24 07:30:52 +0100215 * Determine if a connection may be reused.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100216 */
David Howells45025bc2016-08-24 07:30:52 +0100217static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
218{
David Howells245500d2020-07-01 11:15:32 +0100219 struct rxrpc_net *rxnet;
David Howells45025bc2016-08-24 07:30:52 +0100220 int id_cursor, id, distance, limit;
221
David Howells245500d2020-07-01 11:15:32 +0100222 if (!conn)
223 goto dont_reuse;
224
225 rxnet = conn->params.local->rxnet;
David Howells45025bc2016-08-24 07:30:52 +0100226 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
227 goto dont_reuse;
228
David Howells245500d2020-07-01 11:15:32 +0100229 if (conn->state != RXRPC_CONN_CLIENT ||
230 conn->proto.epoch != rxnet->epoch)
David Howells45025bc2016-08-24 07:30:52 +0100231 goto mark_dont_reuse;
232
233 /* The IDR tree gets very expensive on memory if the connection IDs are
234 * widely scattered throughout the number space, so we shall want to
235 * kill off connections that, say, have an ID more than about four
236 * times the maximum number of client conns away from the current
237 * allocation point to try and keep the IDs concentrated.
238 */
Matthew Wilcox44430612016-12-14 15:09:19 -0800239 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
David Howells45025bc2016-08-24 07:30:52 +0100240 id = conn->proto.cid >> RXRPC_CIDSHIFT;
241 distance = id - id_cursor;
242 if (distance < 0)
243 distance = -distance;
David Howells245500d2020-07-01 11:15:32 +0100244 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
David Howells45025bc2016-08-24 07:30:52 +0100245 if (distance > limit)
246 goto mark_dont_reuse;
247
248 return true;
249
250mark_dont_reuse:
251 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
252dont_reuse:
253 return false;
254}
255
256/*
David Howells245500d2020-07-01 11:15:32 +0100257 * Look up the conn bundle that matches the connection parameters, adding it if
258 * it doesn't yet exist.
259 */
260static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
261 gfp_t gfp)
262{
263 static atomic_t rxrpc_bundle_id;
264 struct rxrpc_bundle *bundle, *candidate;
265 struct rxrpc_local *local = cp->local;
266 struct rb_node *p, **pp, *parent;
267 long diff;
268
269 _enter("{%px,%x,%u,%u}",
270 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
271
272 if (cp->exclusive)
273 return rxrpc_alloc_bundle(cp, gfp);
274
275 /* First, see if the bundle is already there. */
276 _debug("search 1");
277 spin_lock(&local->client_bundles_lock);
278 p = local->client_bundles.rb_node;
279 while (p) {
280 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
281
282#define cmp(X) ((long)bundle->params.X - (long)cp->X)
283 diff = (cmp(peer) ?:
284 cmp(key) ?:
285 cmp(security_level) ?:
286 cmp(upgrade));
287#undef cmp
288 if (diff < 0)
289 p = p->rb_left;
290 else if (diff > 0)
291 p = p->rb_right;
292 else
293 goto found_bundle;
294 }
295 spin_unlock(&local->client_bundles_lock);
296 _debug("not found");
297
298 /* It wasn't. We need to add one. */
299 candidate = rxrpc_alloc_bundle(cp, gfp);
300 if (!candidate)
301 return NULL;
302
303 _debug("search 2");
304 spin_lock(&local->client_bundles_lock);
305 pp = &local->client_bundles.rb_node;
306 parent = NULL;
307 while (*pp) {
308 parent = *pp;
309 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
310
311#define cmp(X) ((long)bundle->params.X - (long)cp->X)
312 diff = (cmp(peer) ?:
313 cmp(key) ?:
314 cmp(security_level) ?:
315 cmp(upgrade));
316#undef cmp
317 if (diff < 0)
318 pp = &(*pp)->rb_left;
319 else if (diff > 0)
320 pp = &(*pp)->rb_right;
321 else
322 goto found_bundle_free;
323 }
324
325 _debug("new bundle");
326 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
327 rb_link_node(&candidate->local_node, parent, pp);
328 rb_insert_color(&candidate->local_node, &local->client_bundles);
329 rxrpc_get_bundle(candidate);
330 spin_unlock(&local->client_bundles_lock);
331 _leave(" = %u [new]", candidate->debug_id);
332 return candidate;
333
334found_bundle_free:
Eiichi Tsukataca77fba2021-11-21 04:16:07 +0000335 rxrpc_free_bundle(candidate);
David Howells245500d2020-07-01 11:15:32 +0100336found_bundle:
337 rxrpc_get_bundle(bundle);
338 spin_unlock(&local->client_bundles_lock);
339 _leave(" = %u [found]", bundle->debug_id);
340 return bundle;
341}
342
343/*
344 * Create or find a client bundle to use for a call.
David Howells45025bc2016-08-24 07:30:52 +0100345 *
346 * If we return with a connection, the call will be on its waiting list. It's
347 * left to the caller to assign a channel and wake up the call.
348 */
David Howells245500d2020-07-01 11:15:32 +0100349static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
350 struct rxrpc_call *call,
351 struct rxrpc_conn_parameters *cp,
352 struct sockaddr_rxrpc *srx,
353 gfp_t gfp)
David Howellsc6d2b8d2016-04-04 14:00:40 +0100354{
David Howells245500d2020-07-01 11:15:32 +0100355 struct rxrpc_bundle *bundle;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100356
357 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
358
David Howells5e33a232018-10-05 14:05:34 +0100359 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100360 if (!cp->peer)
David Howells45025bc2016-08-24 07:30:52 +0100361 goto error;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100362
David Howellsf7aec122017-06-14 17:56:50 +0100363 call->cong_cwnd = cp->peer->cong_cwnd;
364 if (call->cong_cwnd >= call->cong_ssthresh)
365 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
366 else
367 call->cong_mode = RXRPC_CALL_SLOW_START;
David Howells245500d2020-07-01 11:15:32 +0100368 if (cp->upgrade)
369 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
David Howellsf7aec122017-06-14 17:56:50 +0100370
David Howells245500d2020-07-01 11:15:32 +0100371 /* Find the client connection bundle. */
372 bundle = rxrpc_look_up_bundle(cp, gfp);
373 if (!bundle)
374 goto error;
375
376 /* Get this call queued. Someone else may activate it whilst we're
377 * lining up a new connection, but that's fine.
David Howells45025bc2016-08-24 07:30:52 +0100378 */
David Howells245500d2020-07-01 11:15:32 +0100379 spin_lock(&bundle->channel_lock);
380 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
381 spin_unlock(&bundle->channel_lock);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100382
David Howells245500d2020-07-01 11:15:32 +0100383 _leave(" = [B=%x]", bundle->debug_id);
384 return bundle;
385
386error:
387 _leave(" = -ENOMEM");
388 return ERR_PTR(-ENOMEM);
389}
390
391/*
392 * Allocate a new connection and add it into a bundle.
393 */
394static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
395 __releases(bundle->channel_lock)
396{
397 struct rxrpc_connection *candidate = NULL, *old = NULL;
398 bool conflict;
399 int i;
400
401 _enter("");
402
403 conflict = bundle->alloc_conn;
404 if (!conflict)
405 bundle->alloc_conn = true;
406 spin_unlock(&bundle->channel_lock);
407 if (conflict) {
408 _leave(" [conf]");
409 return;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100410 }
411
David Howells245500d2020-07-01 11:15:32 +0100412 candidate = rxrpc_alloc_client_connection(bundle, gfp);
413
414 spin_lock(&bundle->channel_lock);
415 bundle->alloc_conn = false;
416
David Howells45025bc2016-08-24 07:30:52 +0100417 if (IS_ERR(candidate)) {
David Howells245500d2020-07-01 11:15:32 +0100418 bundle->alloc_error = PTR_ERR(candidate);
419 spin_unlock(&bundle->channel_lock);
420 _leave(" [err %ld]", PTR_ERR(candidate));
421 return;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100422 }
423
David Howells245500d2020-07-01 11:15:32 +0100424 bundle->alloc_error = 0;
David Howells45025bc2016-08-24 07:30:52 +0100425
David Howells245500d2020-07-01 11:15:32 +0100426 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
427 unsigned int shift = i * RXRPC_MAXCALLS;
428 int j;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100429
David Howells245500d2020-07-01 11:15:32 +0100430 old = bundle->conns[i];
431 if (!rxrpc_may_reuse_conn(old)) {
432 if (old)
433 trace_rxrpc_client(old, -1, rxrpc_client_replace);
David Howells245500d2020-07-01 11:15:32 +0100434 candidate->bundle_shift = shift;
435 bundle->conns[i] = candidate;
436 for (j = 0; j < RXRPC_MAXCALLS; j++)
437 set_bit(shift + j, &bundle->avail_chans);
438 candidate = NULL;
439 break;
David Howells45025bc2016-08-24 07:30:52 +0100440 }
David Howells245500d2020-07-01 11:15:32 +0100441
442 old = NULL;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100443 }
444
David Howells245500d2020-07-01 11:15:32 +0100445 spin_unlock(&bundle->channel_lock);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100446
David Howells363deea2016-09-17 10:49:14 +0100447 if (candidate) {
David Howells245500d2020-07-01 11:15:32 +0100448 _debug("discard C=%x", candidate->debug_id);
David Howells363deea2016-09-17 10:49:14 +0100449 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
450 rxrpc_put_connection(candidate);
David Howells363deea2016-09-17 10:49:14 +0100451 }
David Howellsc6d2b8d2016-04-04 14:00:40 +0100452
David Howells245500d2020-07-01 11:15:32 +0100453 rxrpc_put_connection(old);
454 _leave("");
David Howellsc6d2b8d2016-04-04 14:00:40 +0100455}
David Howells001c1122016-06-30 10:45:22 +0100456
457/*
David Howells245500d2020-07-01 11:15:32 +0100458 * Add a connection to a bundle if there are no usable connections or we have
459 * connections waiting for extra capacity.
David Howells001c1122016-06-30 10:45:22 +0100460 */
David Howells245500d2020-07-01 11:15:32 +0100461static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
David Howells001c1122016-06-30 10:45:22 +0100462{
David Howells245500d2020-07-01 11:15:32 +0100463 struct rxrpc_call *call;
464 int i, usable;
David Howells001c1122016-06-30 10:45:22 +0100465
David Howells245500d2020-07-01 11:15:32 +0100466 _enter("");
David Howells45025bc2016-08-24 07:30:52 +0100467
David Howells245500d2020-07-01 11:15:32 +0100468 spin_lock(&bundle->channel_lock);
David Howells45025bc2016-08-24 07:30:52 +0100469
David Howells245500d2020-07-01 11:15:32 +0100470 /* See if there are any usable connections. */
471 usable = 0;
472 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
473 if (rxrpc_may_reuse_conn(bundle->conns[i]))
474 usable++;
David Howells45025bc2016-08-24 07:30:52 +0100475
David Howells245500d2020-07-01 11:15:32 +0100476 if (!usable && !list_empty(&bundle->waiting_calls)) {
477 call = list_first_entry(&bundle->waiting_calls,
478 struct rxrpc_call, chan_wait_link);
479 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
480 bundle->try_upgrade = true;
David Howells363deea2016-09-17 10:49:14 +0100481 }
David Howells45025bc2016-08-24 07:30:52 +0100482
David Howells245500d2020-07-01 11:15:32 +0100483 if (!usable)
484 goto alloc_conn;
David Howells45025bc2016-08-24 07:30:52 +0100485
David Howells288827d2020-07-03 00:39:47 +0100486 if (!bundle->avail_chans &&
487 !bundle->try_upgrade &&
488 !list_empty(&bundle->waiting_calls) &&
489 usable < ARRAY_SIZE(bundle->conns))
490 goto alloc_conn;
491
David Howells245500d2020-07-01 11:15:32 +0100492 spin_unlock(&bundle->channel_lock);
493 _leave("");
David Howells45025bc2016-08-24 07:30:52 +0100494 return;
495
David Howells245500d2020-07-01 11:15:32 +0100496alloc_conn:
497 return rxrpc_add_conn_to_bundle(bundle, gfp);
David Howells45025bc2016-08-24 07:30:52 +0100498}
499
500/*
501 * Assign a channel to the call at the front of the queue and wake the call up.
502 * We don't increment the callNumber counter until this number has been exposed
503 * to the world.
504 */
505static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
506 unsigned int channel)
507{
508 struct rxrpc_channel *chan = &conn->channels[channel];
David Howells245500d2020-07-01 11:15:32 +0100509 struct rxrpc_bundle *bundle = conn->bundle;
510 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
David Howells45025bc2016-08-24 07:30:52 +0100511 struct rxrpc_call, chan_wait_link);
512 u32 call_id = chan->call_counter + 1;
513
David Howells245500d2020-07-01 11:15:32 +0100514 _enter("C=%x,%u", conn->debug_id, channel);
515
David Howells363deea2016-09-17 10:49:14 +0100516 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
517
David Howells3136ef42017-11-24 10:18:41 +0000518 /* Cancel the final ACK on the previous call if it hasn't been sent yet
519 * as the DATA packet will implicitly ACK it.
520 */
521 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
David Howells245500d2020-07-01 11:15:32 +0100522 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
David Howellsaf338a92016-09-04 13:10:10 +0100523
David Howellse34d4232016-08-30 09:49:29 +0100524 rxrpc_see_call(call);
David Howells45025bc2016-08-24 07:30:52 +0100525 list_del_init(&call->chan_wait_link);
David Howells45025bc2016-08-24 07:30:52 +0100526 call->peer = rxrpc_get_peer(conn->params.peer);
David Howells245500d2020-07-01 11:15:32 +0100527 call->conn = rxrpc_get_connection(conn);
David Howells45025bc2016-08-24 07:30:52 +0100528 call->cid = conn->proto.cid | channel;
529 call->call_id = call_id;
David Howells245500d2020-07-01 11:15:32 +0100530 call->security = conn->security;
531 call->security_ix = conn->security_ix;
532 call->service_id = conn->service_id;
David Howells45025bc2016-08-24 07:30:52 +0100533
David Howells89ca6942017-04-06 10:12:00 +0100534 trace_rxrpc_connect_call(call);
David Howells45025bc2016-08-24 07:30:52 +0100535 _net("CONNECT call %08x:%08x as call %d on conn %d",
536 call->cid, call->call_id, call->debug_id, conn->debug_id);
537
David Howells245500d2020-07-01 11:15:32 +0100538 write_lock_bh(&call->state_lock);
539 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
540 write_unlock_bh(&call->state_lock);
541
542 /* Paired with the read barrier in rxrpc_connect_call(). This orders
543 * cid and epoch in the connection wrt to call_id without the need to
544 * take the channel_lock.
David Howells45025bc2016-08-24 07:30:52 +0100545 *
546 * We provisionally assign a callNumber at this point, but we don't
547 * confirm it until the call is about to be exposed.
548 *
549 * TODO: Pair with a barrier in the data_ready handler when that looks
550 * at the call ID through a connection channel.
551 */
552 smp_wmb();
David Howells245500d2020-07-01 11:15:32 +0100553
554 chan->call_id = call_id;
555 chan->call_debug_id = call->debug_id;
David Howells45025bc2016-08-24 07:30:52 +0100556 rcu_assign_pointer(chan->call, call);
557 wake_up(&call->waitq);
558}
559
560/*
David Howells245500d2020-07-01 11:15:32 +0100561 * Remove a connection from the idle list if it's on it.
562 */
563static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
564{
565 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
566 bool drop_ref;
567
568 if (!list_empty(&conn->cache_link)) {
569 drop_ref = false;
570 spin_lock(&rxnet->client_conn_cache_lock);
571 if (!list_empty(&conn->cache_link)) {
572 list_del_init(&conn->cache_link);
573 drop_ref = true;
574 }
575 spin_unlock(&rxnet->client_conn_cache_lock);
576 if (drop_ref)
577 rxrpc_put_connection(conn);
578 }
579}
580
581/*
David Howells2629c7f2016-09-29 22:37:15 +0100582 * Assign channels and callNumbers to waiting calls with channel_lock
583 * held by caller.
584 */
David Howells245500d2020-07-01 11:15:32 +0100585static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
David Howells2629c7f2016-09-29 22:37:15 +0100586{
David Howells245500d2020-07-01 11:15:32 +0100587 struct rxrpc_connection *conn;
588 unsigned long avail, mask;
589 unsigned int channel, slot;
David Howells2629c7f2016-09-29 22:37:15 +0100590
David Howells245500d2020-07-01 11:15:32 +0100591 if (bundle->try_upgrade)
592 mask = 1;
593 else
594 mask = ULONG_MAX;
595
596 while (!list_empty(&bundle->waiting_calls)) {
597 avail = bundle->avail_chans & mask;
598 if (!avail)
599 break;
600 channel = __ffs(avail);
601 clear_bit(channel, &bundle->avail_chans);
602
603 slot = channel / RXRPC_MAXCALLS;
604 conn = bundle->conns[slot];
605 if (!conn)
606 break;
607
608 if (bundle->try_upgrade)
609 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
610 rxrpc_unidle_conn(bundle, conn);
611
612 channel &= (RXRPC_MAXCALLS - 1);
613 conn->act_chans |= 1 << channel;
614 rxrpc_activate_one_channel(conn, channel);
David Howells2629c7f2016-09-29 22:37:15 +0100615 }
David Howells2629c7f2016-09-29 22:37:15 +0100616}
617
618/*
David Howells45025bc2016-08-24 07:30:52 +0100619 * Assign channels and callNumbers to waiting calls.
620 */
David Howells245500d2020-07-01 11:15:32 +0100621static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
David Howells45025bc2016-08-24 07:30:52 +0100622{
David Howells245500d2020-07-01 11:15:32 +0100623 _enter("B=%x", bundle->debug_id);
David Howells45025bc2016-08-24 07:30:52 +0100624
David Howells245500d2020-07-01 11:15:32 +0100625 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
David Howells363deea2016-09-17 10:49:14 +0100626
David Howells245500d2020-07-01 11:15:32 +0100627 if (!bundle->avail_chans)
David Howells45025bc2016-08-24 07:30:52 +0100628 return;
629
David Howells245500d2020-07-01 11:15:32 +0100630 spin_lock(&bundle->channel_lock);
631 rxrpc_activate_channels_locked(bundle);
632 spin_unlock(&bundle->channel_lock);
David Howells45025bc2016-08-24 07:30:52 +0100633 _leave("");
634}
635
636/*
637 * Wait for a callNumber and a channel to be granted to a call.
638 */
David Howells245500d2020-07-01 11:15:32 +0100639static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
640 struct rxrpc_call *call, gfp_t gfp)
David Howells45025bc2016-08-24 07:30:52 +0100641{
David Howells245500d2020-07-01 11:15:32 +0100642 DECLARE_WAITQUEUE(myself, current);
David Howells45025bc2016-08-24 07:30:52 +0100643 int ret = 0;
644
645 _enter("%d", call->debug_id);
646
David Howells245500d2020-07-01 11:15:32 +0100647 if (!gfpflags_allow_blocking(gfp)) {
648 rxrpc_maybe_add_conn(bundle, gfp);
649 rxrpc_activate_channels(bundle);
650 ret = bundle->alloc_error ?: -EAGAIN;
651 goto out;
David Howells45025bc2016-08-24 07:30:52 +0100652 }
653
David Howells245500d2020-07-01 11:15:32 +0100654 add_wait_queue_exclusive(&call->waitq, &myself);
655 for (;;) {
656 rxrpc_maybe_add_conn(bundle, gfp);
657 rxrpc_activate_channels(bundle);
658 ret = bundle->alloc_error;
659 if (ret < 0)
660 break;
661
662 switch (call->interruptibility) {
663 case RXRPC_INTERRUPTIBLE:
664 case RXRPC_PREINTERRUPTIBLE:
665 set_current_state(TASK_INTERRUPTIBLE);
666 break;
667 case RXRPC_UNINTERRUPTIBLE:
668 default:
669 set_current_state(TASK_UNINTERRUPTIBLE);
670 break;
671 }
672 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
673 break;
674 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
675 call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
676 signal_pending(current)) {
677 ret = -ERESTARTSYS;
678 break;
679 }
680 schedule();
681 }
682 remove_wait_queue(&call->waitq, &myself);
683 __set_current_state(TASK_RUNNING);
David Howells45025bc2016-08-24 07:30:52 +0100684
685out:
686 _leave(" = %d", ret);
687 return ret;
688}
689
690/*
691 * find a connection for a call
692 * - called in process context with IRQs enabled
693 */
David Howells5e33a232018-10-05 14:05:34 +0100694int rxrpc_connect_call(struct rxrpc_sock *rx,
695 struct rxrpc_call *call,
David Howells45025bc2016-08-24 07:30:52 +0100696 struct rxrpc_conn_parameters *cp,
697 struct sockaddr_rxrpc *srx,
698 gfp_t gfp)
699{
David Howells245500d2020-07-01 11:15:32 +0100700 struct rxrpc_bundle *bundle;
David Howells2baec2c2017-05-24 17:02:32 +0100701 struct rxrpc_net *rxnet = cp->local->rxnet;
David Howells245500d2020-07-01 11:15:32 +0100702 int ret = 0;
David Howells45025bc2016-08-24 07:30:52 +0100703
704 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
705
David Howells3d18cbb2017-11-24 10:18:42 +0000706 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
David Howells45025bc2016-08-24 07:30:52 +0100707
David Howells245500d2020-07-01 11:15:32 +0100708 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
709 if (IS_ERR(bundle)) {
710 ret = PTR_ERR(bundle);
David Howellsc038a582017-08-29 10:19:01 +0100711 goto out;
712 }
David Howells45025bc2016-08-24 07:30:52 +0100713
David Howells245500d2020-07-01 11:15:32 +0100714 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
715 ret = rxrpc_wait_for_channel(bundle, call, gfp);
716 if (ret < 0)
717 goto wait_failed;
718 }
719
720granted_channel:
721 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
722 smp_rmb();
David Howellsc038a582017-08-29 10:19:01 +0100723
David Howells456b2f22020-09-14 12:57:13 +0100724out_put_bundle:
David Howells245500d2020-07-01 11:15:32 +0100725 rxrpc_put_bundle(bundle);
David Howells456b2f22020-09-14 12:57:13 +0100726out:
David Howells45025bc2016-08-24 07:30:52 +0100727 _leave(" = %d", ret);
728 return ret;
David Howells45025bc2016-08-24 07:30:52 +0100729
David Howells245500d2020-07-01 11:15:32 +0100730wait_failed:
731 spin_lock(&bundle->channel_lock);
732 list_del_init(&call->chan_wait_link);
733 spin_unlock(&bundle->channel_lock);
734
735 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
736 ret = 0;
737 goto granted_channel;
David Howells363deea2016-09-17 10:49:14 +0100738 }
David Howells245500d2020-07-01 11:15:32 +0100739
740 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
741 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
742 rxrpc_disconnect_client_call(bundle, call);
David Howells456b2f22020-09-14 12:57:13 +0100743 goto out_put_bundle;
David Howells45025bc2016-08-24 07:30:52 +0100744}
745
746/*
747 * Note that a call, and thus a connection, is about to be exposed to the
748 * world.
749 */
750void rxrpc_expose_client_call(struct rxrpc_call *call)
751{
David Howells363deea2016-09-17 10:49:14 +0100752 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
David Howells45025bc2016-08-24 07:30:52 +0100753 struct rxrpc_connection *conn = call->conn;
David Howells363deea2016-09-17 10:49:14 +0100754 struct rxrpc_channel *chan = &conn->channels[channel];
David Howells45025bc2016-08-24 07:30:52 +0100755
756 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
757 /* Mark the call ID as being used. If the callNumber counter
758 * exceeds ~2 billion, we kill the connection after its
759 * outstanding calls have finished so that the counter doesn't
760 * wrap.
761 */
762 chan->call_counter++;
763 if (chan->call_counter >= INT_MAX)
764 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
David Howells245500d2020-07-01 11:15:32 +0100765 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
David Howells45025bc2016-08-24 07:30:52 +0100766 }
767}
768
769/*
David Howells3d18cbb2017-11-24 10:18:42 +0000770 * Set the reap timer.
771 */
772static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
773{
David Howells245500d2020-07-01 11:15:32 +0100774 if (!rxnet->kill_all_client_conns) {
775 unsigned long now = jiffies;
776 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
David Howells3d18cbb2017-11-24 10:18:42 +0000777
David Howells245500d2020-07-01 11:15:32 +0100778 if (rxnet->live)
779 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
780 }
David Howells3d18cbb2017-11-24 10:18:42 +0000781}
782
783/*
David Howells45025bc2016-08-24 07:30:52 +0100784 * Disconnect a client call.
785 */
David Howells245500d2020-07-01 11:15:32 +0100786void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
David Howells45025bc2016-08-24 07:30:52 +0100787{
David Howells245500d2020-07-01 11:15:32 +0100788 struct rxrpc_connection *conn;
David Howells930c9f92019-03-08 12:48:39 +0000789 struct rxrpc_channel *chan = NULL;
David Howells245500d2020-07-01 11:15:32 +0100790 struct rxrpc_net *rxnet = bundle->params.local->rxnet;
791 unsigned int channel;
792 bool may_reuse;
David Howells930c9f92019-03-08 12:48:39 +0000793 u32 cid;
David Howells45025bc2016-08-24 07:30:52 +0100794
David Howells245500d2020-07-01 11:15:32 +0100795 _enter("c=%x", call->debug_id);
796
797 spin_lock(&bundle->channel_lock);
David Howells5273a192020-01-30 21:50:36 +0000798 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
David Howells45025bc2016-08-24 07:30:52 +0100799
800 /* Calls that have never actually been assigned a channel can simply be
David Howells245500d2020-07-01 11:15:32 +0100801 * discarded.
David Howells45025bc2016-08-24 07:30:52 +0100802 */
David Howells245500d2020-07-01 11:15:32 +0100803 conn = call->conn;
804 if (!conn) {
David Howells45025bc2016-08-24 07:30:52 +0100805 _debug("call is waiting");
806 ASSERTCMP(call->call_id, ==, 0);
807 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
808 list_del_init(&call->chan_wait_link);
David Howells45025bc2016-08-24 07:30:52 +0100809 goto out;
810 }
811
David Howells245500d2020-07-01 11:15:32 +0100812 cid = call->cid;
813 channel = cid & RXRPC_CHANNELMASK;
814 chan = &conn->channels[channel];
815 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
816
David Howells930c9f92019-03-08 12:48:39 +0000817 if (rcu_access_pointer(chan->call) != call) {
David Howells245500d2020-07-01 11:15:32 +0100818 spin_unlock(&bundle->channel_lock);
David Howells930c9f92019-03-08 12:48:39 +0000819 BUG();
820 }
David Howells45025bc2016-08-24 07:30:52 +0100821
David Howells245500d2020-07-01 11:15:32 +0100822 may_reuse = rxrpc_may_reuse_conn(conn);
823
David Howells45025bc2016-08-24 07:30:52 +0100824 /* If a client call was exposed to the world, we save the result for
825 * retransmission.
826 *
827 * We use a barrier here so that the call number and abort code can be
828 * read without needing to take a lock.
829 *
830 * TODO: Make the incoming packet handler check this and handle
831 * terminal retransmission without requiring access to the call.
832 */
833 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100834 _debug("exposed %u,%u", call->call_id, call->abort_code);
David Howells45025bc2016-08-24 07:30:52 +0100835 __rxrpc_disconnect_call(conn, call);
David Howells245500d2020-07-01 11:15:32 +0100836
837 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
838 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
839 bundle->try_upgrade = false;
840 if (may_reuse)
841 rxrpc_activate_channels_locked(bundle);
842 }
843
David Howells45025bc2016-08-24 07:30:52 +0100844 }
845
846 /* See if we can pass the channel directly to another call. */
David Howells245500d2020-07-01 11:15:32 +0100847 if (may_reuse && !list_empty(&bundle->waiting_calls)) {
David Howells363deea2016-09-17 10:49:14 +0100848 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
David Howells45025bc2016-08-24 07:30:52 +0100849 rxrpc_activate_one_channel(conn, channel);
David Howells245500d2020-07-01 11:15:32 +0100850 goto out;
David Howells45025bc2016-08-24 07:30:52 +0100851 }
852
David Howells3136ef42017-11-24 10:18:41 +0000853 /* Schedule the final ACK to be transmitted in a short while so that it
854 * can be skipped if we find a follow-on call. The first DATA packet
855 * of the follow on call will implicitly ACK this call.
856 */
David Howells17e9e232018-02-06 16:44:12 +0000857 if (call->completion == RXRPC_CALL_SUCCEEDED &&
858 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
David Howells3136ef42017-11-24 10:18:41 +0000859 unsigned long final_ack_at = jiffies + 2;
860
861 WRITE_ONCE(chan->final_ack_at, final_ack_at);
862 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
863 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
864 rxrpc_reduce_conn_timer(conn, final_ack_at);
865 }
866
David Howells245500d2020-07-01 11:15:32 +0100867 /* Deactivate the channel. */
868 rcu_assign_pointer(chan->call, NULL);
869 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
870 conn->act_chans &= ~(1 << channel);
871
872 /* If no channels remain active, then put the connection on the idle
873 * list for a short while. Give it a ref to stop it going away if it
874 * becomes unbundled.
David Howells45025bc2016-08-24 07:30:52 +0100875 */
David Howells245500d2020-07-01 11:15:32 +0100876 if (!conn->act_chans) {
877 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
878 conn->idle_timestamp = jiffies;
David Howells45025bc2016-08-24 07:30:52 +0100879
David Howells245500d2020-07-01 11:15:32 +0100880 rxrpc_get_connection(conn);
881 spin_lock(&rxnet->client_conn_cache_lock);
882 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
883 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100884
David Howells245500d2020-07-01 11:15:32 +0100885 rxrpc_set_client_reap_timer(rxnet);
David Howells45025bc2016-08-24 07:30:52 +0100886 }
887
888out:
David Howells245500d2020-07-01 11:15:32 +0100889 spin_unlock(&bundle->channel_lock);
David Howells45025bc2016-08-24 07:30:52 +0100890 _leave("");
891 return;
David Howells245500d2020-07-01 11:15:32 +0100892}
David Howells45025bc2016-08-24 07:30:52 +0100893
David Howells245500d2020-07-01 11:15:32 +0100894/*
895 * Remove a connection from a bundle.
896 */
897static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
898{
899 struct rxrpc_bundle *bundle = conn->bundle;
900 struct rxrpc_local *local = bundle->params.local;
901 unsigned int bindex;
David Howellsf3af4ad2020-09-29 22:29:44 +0100902 bool need_drop = false, need_put = false;
David Howells245500d2020-07-01 11:15:32 +0100903 int i;
904
905 _enter("C=%x", conn->debug_id);
906
David Howellsddc78342020-09-30 23:54:44 +0100907 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
908 rxrpc_process_delayed_final_acks(conn, true);
909
David Howells245500d2020-07-01 11:15:32 +0100910 spin_lock(&bundle->channel_lock);
911 bindex = conn->bundle_shift / RXRPC_MAXCALLS;
912 if (bundle->conns[bindex] == conn) {
913 _debug("clear slot %u", bindex);
914 bundle->conns[bindex] = NULL;
915 for (i = 0; i < RXRPC_MAXCALLS; i++)
916 clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
917 need_drop = true;
David Howells45025bc2016-08-24 07:30:52 +0100918 }
David Howells245500d2020-07-01 11:15:32 +0100919 spin_unlock(&bundle->channel_lock);
920
921 /* If there are no more connections, remove the bundle */
922 if (!bundle->avail_chans) {
923 _debug("maybe unbundle");
924 spin_lock(&local->client_bundles_lock);
925
926 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
927 if (bundle->conns[i])
928 break;
929 if (i == ARRAY_SIZE(bundle->conns) && !bundle->params.exclusive) {
930 _debug("erase bundle");
931 rb_erase(&bundle->local_node, &local->client_bundles);
David Howellsf3af4ad2020-09-29 22:29:44 +0100932 need_put = true;
David Howells245500d2020-07-01 11:15:32 +0100933 }
934
935 spin_unlock(&local->client_bundles_lock);
David Howellsf3af4ad2020-09-29 22:29:44 +0100936 if (need_put)
David Howells245500d2020-07-01 11:15:32 +0100937 rxrpc_put_bundle(bundle);
938 }
939
940 if (need_drop)
941 rxrpc_put_connection(conn);
942 _leave("");
David Howells45025bc2016-08-24 07:30:52 +0100943}
944
945/*
946 * Clean up a dead client connection.
947 */
David Howells245500d2020-07-01 11:15:32 +0100948static void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
David Howells45025bc2016-08-24 07:30:52 +0100949{
David Howells45025bc2016-08-24 07:30:52 +0100950 struct rxrpc_local *local = conn->params.local;
David Howells2baec2c2017-05-24 17:02:32 +0100951 struct rxrpc_net *rxnet = local->rxnet;
David Howells245500d2020-07-01 11:15:32 +0100952
953 _enter("C=%x", conn->debug_id);
David Howells45025bc2016-08-24 07:30:52 +0100954
David Howells363deea2016-09-17 10:49:14 +0100955 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
David Howells245500d2020-07-01 11:15:32 +0100956 atomic_dec(&rxnet->nr_client_conns);
David Howells001c1122016-06-30 10:45:22 +0100957
958 rxrpc_put_client_connection_id(conn);
David Howells45025bc2016-08-24 07:30:52 +0100959 rxrpc_kill_connection(conn);
David Howells45025bc2016-08-24 07:30:52 +0100960}
961
962/*
963 * Clean up a dead client connections.
964 */
965void rxrpc_put_client_conn(struct rxrpc_connection *conn)
966{
David Howells363deea2016-09-17 10:49:14 +0100967 const void *here = __builtin_return_address(0);
David Howells4c1295d2019-10-07 10:58:29 +0100968 unsigned int debug_id = conn->debug_id;
David Howells363deea2016-09-17 10:49:14 +0100969 int n;
David Howells45025bc2016-08-24 07:30:52 +0100970
David Howells245500d2020-07-01 11:15:32 +0100971 n = atomic_dec_return(&conn->usage);
972 trace_rxrpc_conn(debug_id, rxrpc_conn_put_client, n, here);
973 if (n <= 0) {
David Howells363deea2016-09-17 10:49:14 +0100974 ASSERTCMP(n, >=, 0);
David Howells245500d2020-07-01 11:15:32 +0100975 rxrpc_kill_client_conn(conn);
David Howells45025bc2016-08-24 07:30:52 +0100976 }
David Howells45025bc2016-08-24 07:30:52 +0100977}
978
979/*
980 * Discard expired client connections from the idle list. Each conn in the
981 * idle list has been exposed and holds an extra ref because of that.
982 *
983 * This may be called from conn setup or from a work item so cannot be
984 * considered non-reentrant.
985 */
David Howells2baec2c2017-05-24 17:02:32 +0100986void rxrpc_discard_expired_client_conns(struct work_struct *work)
David Howells45025bc2016-08-24 07:30:52 +0100987{
988 struct rxrpc_connection *conn;
David Howells2baec2c2017-05-24 17:02:32 +0100989 struct rxrpc_net *rxnet =
David Howells3d18cbb2017-11-24 10:18:42 +0000990 container_of(work, struct rxrpc_net, client_conn_reaper);
David Howells45025bc2016-08-24 07:30:52 +0100991 unsigned long expiry, conn_expires_at, now;
992 unsigned int nr_conns;
David Howells45025bc2016-08-24 07:30:52 +0100993
David Howells2baec2c2017-05-24 17:02:32 +0100994 _enter("");
David Howells45025bc2016-08-24 07:30:52 +0100995
David Howells2baec2c2017-05-24 17:02:32 +0100996 if (list_empty(&rxnet->idle_client_conns)) {
David Howells45025bc2016-08-24 07:30:52 +0100997 _leave(" [empty]");
998 return;
999 }
1000
1001 /* Don't double up on the discarding */
David Howells2baec2c2017-05-24 17:02:32 +01001002 if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
David Howells45025bc2016-08-24 07:30:52 +01001003 _leave(" [already]");
1004 return;
1005 }
1006
1007 /* We keep an estimate of what the number of conns ought to be after
1008 * we've discarded some so that we don't overdo the discarding.
1009 */
David Howells245500d2020-07-01 11:15:32 +01001010 nr_conns = atomic_read(&rxnet->nr_client_conns);
David Howells45025bc2016-08-24 07:30:52 +01001011
1012next:
David Howells2baec2c2017-05-24 17:02:32 +01001013 spin_lock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001014
David Howells2baec2c2017-05-24 17:02:32 +01001015 if (list_empty(&rxnet->idle_client_conns))
David Howells45025bc2016-08-24 07:30:52 +01001016 goto out;
1017
David Howells2baec2c2017-05-24 17:02:32 +01001018 conn = list_entry(rxnet->idle_client_conns.next,
David Howells45025bc2016-08-24 07:30:52 +01001019 struct rxrpc_connection, cache_link);
David Howells45025bc2016-08-24 07:30:52 +01001020
David Howells2baec2c2017-05-24 17:02:32 +01001021 if (!rxnet->kill_all_client_conns) {
David Howells45025bc2016-08-24 07:30:52 +01001022 /* If the number of connections is over the reap limit, we
1023 * expedite discard by reducing the expiry timeout. We must,
1024 * however, have at least a short grace period to be able to do
1025 * final-ACK or ABORT retransmission.
1026 */
1027 expiry = rxrpc_conn_idle_client_expiry;
1028 if (nr_conns > rxrpc_reap_client_connections)
1029 expiry = rxrpc_conn_idle_client_fast_expiry;
David Howellsf859ab62017-11-24 10:18:42 +00001030 if (conn->params.local->service_closed)
1031 expiry = rxrpc_closed_conn_expiry * HZ;
David Howells45025bc2016-08-24 07:30:52 +01001032
1033 conn_expires_at = conn->idle_timestamp + expiry;
1034
1035 now = READ_ONCE(jiffies);
1036 if (time_after(conn_expires_at, now))
1037 goto not_yet_expired;
1038 }
1039
David Howells363deea2016-09-17 10:49:14 +01001040 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
David Howells45025bc2016-08-24 07:30:52 +01001041 list_del_init(&conn->cache_link);
1042
David Howells2baec2c2017-05-24 17:02:32 +01001043 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001044
David Howells245500d2020-07-01 11:15:32 +01001045 rxrpc_unbundle_conn(conn);
1046 rxrpc_put_connection(conn); /* Drop the ->cache_link ref */
1047
David Howells45025bc2016-08-24 07:30:52 +01001048 nr_conns--;
1049 goto next;
1050
1051not_yet_expired:
1052 /* The connection at the front of the queue hasn't yet expired, so
1053 * schedule the work item for that point if we discarded something.
1054 *
1055 * We don't worry if the work item is already scheduled - it can look
1056 * after rescheduling itself at a later time. We could cancel it, but
1057 * then things get messier.
1058 */
1059 _debug("not yet");
David Howells2baec2c2017-05-24 17:02:32 +01001060 if (!rxnet->kill_all_client_conns)
David Howells245500d2020-07-01 11:15:32 +01001061 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
David Howells45025bc2016-08-24 07:30:52 +01001062
1063out:
David Howells2baec2c2017-05-24 17:02:32 +01001064 spin_unlock(&rxnet->client_conn_cache_lock);
1065 spin_unlock(&rxnet->client_conn_discard_lock);
David Howells45025bc2016-08-24 07:30:52 +01001066 _leave("");
1067}
1068
1069/*
1070 * Preemptively destroy all the client connection records rather than waiting
1071 * for them to time out
1072 */
David Howells2baec2c2017-05-24 17:02:32 +01001073void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
David Howells45025bc2016-08-24 07:30:52 +01001074{
1075 _enter("");
1076
David Howells2baec2c2017-05-24 17:02:32 +01001077 spin_lock(&rxnet->client_conn_cache_lock);
1078 rxnet->kill_all_client_conns = true;
1079 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001080
David Howells3d18cbb2017-11-24 10:18:42 +00001081 del_timer_sync(&rxnet->client_conn_reap_timer);
David Howells45025bc2016-08-24 07:30:52 +01001082
David Howells3d18cbb2017-11-24 10:18:42 +00001083 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
David Howells45025bc2016-08-24 07:30:52 +01001084 _debug("destroy: queue failed");
1085
1086 _leave("");
David Howells001c1122016-06-30 10:45:22 +01001087}
David Howellsd12040b2019-08-29 14:12:11 +01001088
1089/*
1090 * Clean up the client connections on a local endpoint.
1091 */
1092void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1093{
1094 struct rxrpc_connection *conn, *tmp;
1095 struct rxrpc_net *rxnet = local->rxnet;
David Howellsd12040b2019-08-29 14:12:11 +01001096 LIST_HEAD(graveyard);
1097
1098 _enter("");
1099
1100 spin_lock(&rxnet->client_conn_cache_lock);
David Howellsd12040b2019-08-29 14:12:11 +01001101
1102 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1103 cache_link) {
1104 if (conn->params.local == local) {
David Howellsd12040b2019-08-29 14:12:11 +01001105 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
David Howellsd12040b2019-08-29 14:12:11 +01001106 list_move(&conn->cache_link, &graveyard);
David Howellsd12040b2019-08-29 14:12:11 +01001107 }
1108 }
1109
David Howellsd12040b2019-08-29 14:12:11 +01001110 spin_unlock(&rxnet->client_conn_cache_lock);
David Howellsd12040b2019-08-29 14:12:11 +01001111
1112 while (!list_empty(&graveyard)) {
1113 conn = list_entry(graveyard.next,
1114 struct rxrpc_connection, cache_link);
1115 list_del_init(&conn->cache_link);
David Howells546a4242020-09-14 13:26:47 +01001116 rxrpc_unbundle_conn(conn);
David Howellsd12040b2019-08-29 14:12:11 +01001117 rxrpc_put_connection(conn);
1118 }
1119
1120 _leave(" [culled]");
1121}