blob: aea82f909c60801d347c8f02eeb5d9b9790bff91 [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 *
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * 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 *
11 * Client connections can be in one of a number of cache states:
12 *
13 * (1) INACTIVE - The connection is not held in any list and may not have been
14 * exposed to the world. If it has been previously exposed, it was
15 * discarded from the idle list after expiring.
16 *
17 * (2) WAITING - The connection is waiting for the number of client conns to
18 * drop below the maximum capacity. Calls may be in progress upon it from
19 * when it was active and got culled.
20 *
21 * The connection is on the rxrpc_waiting_client_conns list which is kept
22 * in to-be-granted order. Culled conns with waiters go to the back of
23 * the queue just like new conns.
24 *
25 * (3) ACTIVE - The connection has at least one call in progress upon it, it
26 * may freely grant available channels to new calls and calls may be
27 * waiting on it for channels to become available.
28 *
David Howells2baec2c2017-05-24 17:02:32 +010029 * The connection is on the rxnet->active_client_conns list which is kept
David Howells45025bc2016-08-24 07:30:52 +010030 * in activation order for culling purposes.
31 *
32 * rxrpc_nr_active_client_conns is held incremented also.
33 *
David Howells4e255722017-06-05 14:30:49 +010034 * (4) UPGRADE - As for ACTIVE, but only one call may be in progress and is
35 * being used to probe for service upgrade.
36 *
37 * (5) CULLED - The connection got summarily culled to try and free up
David Howells45025bc2016-08-24 07:30:52 +010038 * capacity. Calls currently in progress on the connection are allowed to
39 * continue, but new calls will have to wait. There can be no waiters in
40 * this state - the conn would have to go to the WAITING state instead.
41 *
David Howells4e255722017-06-05 14:30:49 +010042 * (6) IDLE - The connection has no calls in progress upon it and must have
David Howells45025bc2016-08-24 07:30:52 +010043 * been exposed to the world (ie. the EXPOSED flag must be set). When it
44 * expires, the EXPOSED flag is cleared and the connection transitions to
45 * the INACTIVE state.
46 *
David Howells2baec2c2017-05-24 17:02:32 +010047 * The connection is on the rxnet->idle_client_conns list which is kept in
David Howells45025bc2016-08-24 07:30:52 +010048 * order of how soon they'll expire.
49 *
50 * There are flags of relevance to the cache:
51 *
52 * (1) EXPOSED - The connection ID got exposed to the world. If this flag is
53 * set, an extra ref is added to the connection preventing it from being
54 * reaped when it has no calls outstanding. This flag is cleared and the
55 * ref dropped when a conn is discarded from the idle list.
56 *
57 * This allows us to move terminal call state retransmission to the
58 * connection and to discard the call immediately we think it is done
59 * with. It also give us a chance to reuse the connection.
60 *
61 * (2) DONT_REUSE - The connection should be discarded as soon as possible and
62 * should not be reused. This is set when an exclusive connection is used
63 * or a call ID counter overflows.
64 *
65 * The caching state may only be changed if the cache lock is held.
66 *
67 * There are two idle client connection expiry durations. If the total number
68 * of connections is below the reap threshold, we use the normal duration; if
69 * it's above, we use the fast duration.
David Howells4a3388c2016-04-04 14:00:37 +010070 */
71
72#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
73
74#include <linux/slab.h>
75#include <linux/idr.h>
76#include <linux/timer.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010077#include <linux/sched/signal.h>
78
David Howells4a3388c2016-04-04 14:00:37 +010079#include "ar-internal.h"
80
David Howells45025bc2016-08-24 07:30:52 +010081__read_mostly unsigned int rxrpc_max_client_connections = 1000;
82__read_mostly unsigned int rxrpc_reap_client_connections = 900;
David Howellsa158bdd2017-11-24 10:18:41 +000083__read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
84__read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
David Howells45025bc2016-08-24 07:30:52 +010085
David Howells4a3388c2016-04-04 14:00:37 +010086/*
87 * We use machine-unique IDs for our client connections.
88 */
89DEFINE_IDR(rxrpc_client_conn_ids);
90static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
91
David Howells2baec2c2017-05-24 17:02:32 +010092static void rxrpc_cull_active_client_conns(struct rxrpc_net *);
David Howells45025bc2016-08-24 07:30:52 +010093
David Howells4a3388c2016-04-04 14:00:37 +010094/*
95 * Get a connection ID and epoch for a client connection from the global pool.
96 * The connection struct pointer is then recorded in the idr radix tree. The
David Howells090f85d2016-09-04 13:14:46 +010097 * epoch doesn't change until the client is rebooted (or, at least, unless the
98 * module is unloaded).
David Howells4a3388c2016-04-04 14:00:37 +010099 */
David Howellsc6d2b8d2016-04-04 14:00:40 +0100100static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
101 gfp_t gfp)
David Howells4a3388c2016-04-04 14:00:37 +0100102{
David Howells2baec2c2017-05-24 17:02:32 +0100103 struct rxrpc_net *rxnet = conn->params.local->rxnet;
David Howells4a3388c2016-04-04 14:00:37 +0100104 int id;
105
106 _enter("");
107
108 idr_preload(gfp);
David Howells4a3388c2016-04-04 14:00:37 +0100109 spin_lock(&rxrpc_conn_id_lock);
110
David Howells090f85d2016-09-04 13:14:46 +0100111 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
112 1, 0x40000000, GFP_NOWAIT);
113 if (id < 0)
114 goto error;
David Howells4a3388c2016-04-04 14:00:37 +0100115
116 spin_unlock(&rxrpc_conn_id_lock);
David Howells4a3388c2016-04-04 14:00:37 +0100117 idr_preload_end();
118
David Howells2baec2c2017-05-24 17:02:32 +0100119 conn->proto.epoch = rxnet->epoch;
David Howells4a3388c2016-04-04 14:00:37 +0100120 conn->proto.cid = id << RXRPC_CIDSHIFT;
121 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
David Howells090f85d2016-09-04 13:14:46 +0100122 _leave(" [CID %x]", conn->proto.cid);
David Howells4a3388c2016-04-04 14:00:37 +0100123 return 0;
124
125error:
126 spin_unlock(&rxrpc_conn_id_lock);
David Howells4a3388c2016-04-04 14:00:37 +0100127 idr_preload_end();
128 _leave(" = %d", id);
129 return id;
130}
131
132/*
133 * Release a connection ID for a client connection from the global pool.
134 */
David Howells001c1122016-06-30 10:45:22 +0100135static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
David Howells4a3388c2016-04-04 14:00:37 +0100136{
137 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
138 spin_lock(&rxrpc_conn_id_lock);
139 idr_remove(&rxrpc_client_conn_ids,
140 conn->proto.cid >> RXRPC_CIDSHIFT);
141 spin_unlock(&rxrpc_conn_id_lock);
142 }
143}
David Howellseb9b9d22016-06-27 10:32:02 +0100144
145/*
146 * Destroy the client connection ID tree.
147 */
148void rxrpc_destroy_client_conn_ids(void)
149{
150 struct rxrpc_connection *conn;
151 int id;
152
153 if (!idr_is_empty(&rxrpc_client_conn_ids)) {
154 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
155 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
156 conn, atomic_read(&conn->usage));
157 }
158 BUG();
159 }
160
161 idr_destroy(&rxrpc_client_conn_ids);
162}
David Howellsc6d2b8d2016-04-04 14:00:40 +0100163
164/*
David Howells45025bc2016-08-24 07:30:52 +0100165 * Allocate a client connection.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100166 */
167static struct rxrpc_connection *
168rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
169{
170 struct rxrpc_connection *conn;
David Howells2baec2c2017-05-24 17:02:32 +0100171 struct rxrpc_net *rxnet = cp->local->rxnet;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100172 int ret;
173
174 _enter("");
175
176 conn = rxrpc_alloc_connection(gfp);
177 if (!conn) {
178 _leave(" = -ENOMEM");
179 return ERR_PTR(-ENOMEM);
180 }
181
David Howells45025bc2016-08-24 07:30:52 +0100182 atomic_set(&conn->usage, 1);
David Howells8732db62016-09-29 22:37:15 +0100183 if (cp->exclusive)
David Howells45025bc2016-08-24 07:30:52 +0100184 __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
David Howells4e255722017-06-05 14:30:49 +0100185 if (cp->upgrade)
186 __set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
David Howells45025bc2016-08-24 07:30:52 +0100187
David Howellsc6d2b8d2016-04-04 14:00:40 +0100188 conn->params = *cp;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100189 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
190 conn->state = RXRPC_CONN_CLIENT;
David Howells68d6d1a2017-06-05 14:30:49 +0100191 conn->service_id = cp->service_id;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100192
David Howellsc6d2b8d2016-04-04 14:00:40 +0100193 ret = rxrpc_get_client_connection_id(conn, gfp);
194 if (ret < 0)
195 goto error_0;
196
197 ret = rxrpc_init_client_conn_security(conn);
198 if (ret < 0)
199 goto error_1;
200
201 ret = conn->security->prime_packet_security(conn);
202 if (ret < 0)
203 goto error_2;
204
David Howells31f5f9a162018-03-30 21:05:33 +0100205 atomic_inc(&rxnet->nr_conns);
David Howells2baec2c2017-05-24 17:02:32 +0100206 write_lock(&rxnet->conn_lock);
207 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
208 write_unlock(&rxnet->conn_lock);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100209
210 /* We steal the caller's peer ref. */
211 cp->peer = NULL;
212 rxrpc_get_local(conn->params.local);
213 key_get(conn->params.key);
214
David Howells363deea2016-09-17 10:49:14 +0100215 trace_rxrpc_conn(conn, rxrpc_conn_new_client, atomic_read(&conn->usage),
216 __builtin_return_address(0));
217 trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100218 _leave(" = %p", conn);
219 return conn;
220
221error_2:
222 conn->security->clear(conn);
223error_1:
224 rxrpc_put_client_connection_id(conn);
225error_0:
226 kfree(conn);
227 _leave(" = %d", ret);
228 return ERR_PTR(ret);
229}
230
231/*
David Howells45025bc2016-08-24 07:30:52 +0100232 * Determine if a connection may be reused.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100233 */
David Howells45025bc2016-08-24 07:30:52 +0100234static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
235{
David Howells2baec2c2017-05-24 17:02:32 +0100236 struct rxrpc_net *rxnet = conn->params.local->rxnet;
David Howells45025bc2016-08-24 07:30:52 +0100237 int id_cursor, id, distance, limit;
238
239 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
240 goto dont_reuse;
241
David Howells2baec2c2017-05-24 17:02:32 +0100242 if (conn->proto.epoch != rxnet->epoch)
David Howells45025bc2016-08-24 07:30:52 +0100243 goto mark_dont_reuse;
244
245 /* The IDR tree gets very expensive on memory if the connection IDs are
246 * widely scattered throughout the number space, so we shall want to
247 * kill off connections that, say, have an ID more than about four
248 * times the maximum number of client conns away from the current
249 * allocation point to try and keep the IDs concentrated.
250 */
Matthew Wilcox44430612016-12-14 15:09:19 -0800251 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
David Howells45025bc2016-08-24 07:30:52 +0100252 id = conn->proto.cid >> RXRPC_CIDSHIFT;
253 distance = id - id_cursor;
254 if (distance < 0)
255 distance = -distance;
Matthew Wilcox44430612016-12-14 15:09:19 -0800256 limit = max(rxrpc_max_client_connections * 4, 1024U);
David Howells45025bc2016-08-24 07:30:52 +0100257 if (distance > limit)
258 goto mark_dont_reuse;
259
260 return true;
261
262mark_dont_reuse:
263 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
264dont_reuse:
265 return false;
266}
267
268/*
269 * Create or find a client connection to use for a call.
270 *
271 * If we return with a connection, the call will be on its waiting list. It's
272 * left to the caller to assign a channel and wake up the call.
273 */
David Howells5e33a232018-10-05 14:05:34 +0100274static int rxrpc_get_client_conn(struct rxrpc_sock *rx,
275 struct rxrpc_call *call,
David Howells45025bc2016-08-24 07:30:52 +0100276 struct rxrpc_conn_parameters *cp,
277 struct sockaddr_rxrpc *srx,
278 gfp_t gfp)
David Howellsc6d2b8d2016-04-04 14:00:40 +0100279{
280 struct rxrpc_connection *conn, *candidate = NULL;
281 struct rxrpc_local *local = cp->local;
282 struct rb_node *p, **pp, *parent;
283 long diff;
David Howells45025bc2016-08-24 07:30:52 +0100284 int ret = -ENOMEM;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100285
286 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
287
David Howells5e33a232018-10-05 14:05:34 +0100288 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100289 if (!cp->peer)
David Howells45025bc2016-08-24 07:30:52 +0100290 goto error;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100291
David Howellsf7aec122017-06-14 17:56:50 +0100292 call->cong_cwnd = cp->peer->cong_cwnd;
293 if (call->cong_cwnd >= call->cong_ssthresh)
294 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
295 else
296 call->cong_mode = RXRPC_CALL_SLOW_START;
297
David Howells45025bc2016-08-24 07:30:52 +0100298 /* If the connection is not meant to be exclusive, search the available
299 * connections to see if the connection we want to use already exists.
300 */
David Howellsc6d2b8d2016-04-04 14:00:40 +0100301 if (!cp->exclusive) {
David Howellsc6d2b8d2016-04-04 14:00:40 +0100302 _debug("search 1");
303 spin_lock(&local->client_conns_lock);
304 p = local->client_conns.rb_node;
305 while (p) {
306 conn = rb_entry(p, struct rxrpc_connection, client_node);
307
308#define cmp(X) ((long)conn->params.X - (long)cp->X)
309 diff = (cmp(peer) ?:
310 cmp(key) ?:
David Howells4e255722017-06-05 14:30:49 +0100311 cmp(security_level) ?:
312 cmp(upgrade));
David Howells45025bc2016-08-24 07:30:52 +0100313#undef cmp
314 if (diff < 0) {
David Howellsc6d2b8d2016-04-04 14:00:40 +0100315 p = p->rb_left;
David Howells45025bc2016-08-24 07:30:52 +0100316 } else if (diff > 0) {
David Howellsc6d2b8d2016-04-04 14:00:40 +0100317 p = p->rb_right;
David Howells45025bc2016-08-24 07:30:52 +0100318 } else {
319 if (rxrpc_may_reuse_conn(conn) &&
320 rxrpc_get_connection_maybe(conn))
321 goto found_extant_conn;
322 /* The connection needs replacing. It's better
323 * to effect that when we have something to
324 * replace it with so that we don't have to
325 * rebalance the tree twice.
326 */
327 break;
328 }
David Howellsc6d2b8d2016-04-04 14:00:40 +0100329 }
330 spin_unlock(&local->client_conns_lock);
331 }
332
David Howells45025bc2016-08-24 07:30:52 +0100333 /* There wasn't a connection yet or we need an exclusive connection.
334 * We need to create a candidate and then potentially redo the search
335 * in case we're racing with another thread also trying to connect on a
336 * shareable connection.
337 */
338 _debug("new conn");
David Howellsc6d2b8d2016-04-04 14:00:40 +0100339 candidate = rxrpc_alloc_client_connection(cp, gfp);
David Howells45025bc2016-08-24 07:30:52 +0100340 if (IS_ERR(candidate)) {
341 ret = PTR_ERR(candidate);
342 goto error_peer;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100343 }
344
David Howells45025bc2016-08-24 07:30:52 +0100345 /* Add the call to the new connection's waiting list in case we're
346 * going to have to wait for the connection to come live. It's our
347 * connection, so we want first dibs on the channel slots. We would
348 * normally have to take channel_lock but we do this before anyone else
349 * can see the connection.
350 */
David Howells69ffaeb2019-03-09 00:29:58 +0000351 list_add(&call->chan_wait_link, &candidate->waiting_calls);
David Howells45025bc2016-08-24 07:30:52 +0100352
David Howellsc6d2b8d2016-04-04 14:00:40 +0100353 if (cp->exclusive) {
David Howells45025bc2016-08-24 07:30:52 +0100354 call->conn = candidate;
David Howells278ac0c2016-09-07 15:19:25 +0100355 call->security_ix = candidate->security_ix;
David Howells68d6d1a2017-06-05 14:30:49 +0100356 call->service_id = candidate->service_id;
David Howells45025bc2016-08-24 07:30:52 +0100357 _leave(" = 0 [exclusive %d]", candidate->debug_id);
358 return 0;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100359 }
360
David Howells45025bc2016-08-24 07:30:52 +0100361 /* Publish the new connection for userspace to find. We need to redo
362 * the search before doing this lest we race with someone else adding a
363 * conflicting instance.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100364 */
365 _debug("search 2");
366 spin_lock(&local->client_conns_lock);
367
368 pp = &local->client_conns.rb_node;
369 parent = NULL;
370 while (*pp) {
371 parent = *pp;
372 conn = rb_entry(parent, struct rxrpc_connection, client_node);
373
David Howells45025bc2016-08-24 07:30:52 +0100374#define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
David Howellsc6d2b8d2016-04-04 14:00:40 +0100375 diff = (cmp(peer) ?:
376 cmp(key) ?:
David Howells4e255722017-06-05 14:30:49 +0100377 cmp(security_level) ?:
378 cmp(upgrade));
David Howells45025bc2016-08-24 07:30:52 +0100379#undef cmp
380 if (diff < 0) {
David Howellsc6d2b8d2016-04-04 14:00:40 +0100381 pp = &(*pp)->rb_left;
David Howells45025bc2016-08-24 07:30:52 +0100382 } else if (diff > 0) {
David Howellsc6d2b8d2016-04-04 14:00:40 +0100383 pp = &(*pp)->rb_right;
David Howells45025bc2016-08-24 07:30:52 +0100384 } else {
385 if (rxrpc_may_reuse_conn(conn) &&
386 rxrpc_get_connection_maybe(conn))
387 goto found_extant_conn;
388 /* The old connection is from an outdated epoch. */
389 _debug("replace conn");
390 clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
391 rb_replace_node(&conn->client_node,
392 &candidate->client_node,
393 &local->client_conns);
David Howells363deea2016-09-17 10:49:14 +0100394 trace_rxrpc_client(conn, -1, rxrpc_client_replace);
David Howells45025bc2016-08-24 07:30:52 +0100395 goto candidate_published;
396 }
David Howellsc6d2b8d2016-04-04 14:00:40 +0100397 }
398
David Howellsc6d2b8d2016-04-04 14:00:40 +0100399 _debug("new conn");
David Howells001c1122016-06-30 10:45:22 +0100400 rb_link_node(&candidate->client_node, parent, pp);
401 rb_insert_color(&candidate->client_node, &local->client_conns);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100402
David Howells45025bc2016-08-24 07:30:52 +0100403candidate_published:
404 set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
405 call->conn = candidate;
David Howells278ac0c2016-09-07 15:19:25 +0100406 call->security_ix = candidate->security_ix;
David Howells68d6d1a2017-06-05 14:30:49 +0100407 call->service_id = candidate->service_id;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100408 spin_unlock(&local->client_conns_lock);
David Howells45025bc2016-08-24 07:30:52 +0100409 _leave(" = 0 [new %d]", candidate->debug_id);
David Howellsc6d2b8d2016-04-04 14:00:40 +0100410 return 0;
411
David Howells45025bc2016-08-24 07:30:52 +0100412 /* We come here if we found a suitable connection already in existence.
413 * Discard any candidate we may have allocated, and try to get a
414 * channel on this one.
David Howellsc6d2b8d2016-04-04 14:00:40 +0100415 */
416found_extant_conn:
417 _debug("found conn");
David Howellsc6d2b8d2016-04-04 14:00:40 +0100418 spin_unlock(&local->client_conns_lock);
419
David Howells363deea2016-09-17 10:49:14 +0100420 if (candidate) {
421 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
422 rxrpc_put_connection(candidate);
423 candidate = NULL;
424 }
David Howellsc6d2b8d2016-04-04 14:00:40 +0100425
David Howellsc6d2b8d2016-04-04 14:00:40 +0100426 spin_lock(&conn->channel_lock);
David Howells45025bc2016-08-24 07:30:52 +0100427 call->conn = conn;
David Howells278ac0c2016-09-07 15:19:25 +0100428 call->security_ix = conn->security_ix;
David Howells68d6d1a2017-06-05 14:30:49 +0100429 call->service_id = conn->service_id;
David Howells69ffaeb2019-03-09 00:29:58 +0000430 list_add_tail(&call->chan_wait_link, &conn->waiting_calls);
David Howells45025bc2016-08-24 07:30:52 +0100431 spin_unlock(&conn->channel_lock);
432 _leave(" = 0 [extant %d]", conn->debug_id);
433 return 0;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100434
David Howells45025bc2016-08-24 07:30:52 +0100435error_peer:
David Howellsc6d2b8d2016-04-04 14:00:40 +0100436 rxrpc_put_peer(cp->peer);
437 cp->peer = NULL;
David Howells45025bc2016-08-24 07:30:52 +0100438error:
439 _leave(" = %d", ret);
440 return ret;
David Howellsc6d2b8d2016-04-04 14:00:40 +0100441}
David Howells001c1122016-06-30 10:45:22 +0100442
443/*
David Howells45025bc2016-08-24 07:30:52 +0100444 * Activate a connection.
David Howells001c1122016-06-30 10:45:22 +0100445 */
David Howells2baec2c2017-05-24 17:02:32 +0100446static void rxrpc_activate_conn(struct rxrpc_net *rxnet,
447 struct rxrpc_connection *conn)
David Howells001c1122016-06-30 10:45:22 +0100448{
David Howells4e255722017-06-05 14:30:49 +0100449 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
450 trace_rxrpc_client(conn, -1, rxrpc_client_to_upgrade);
451 conn->cache_state = RXRPC_CONN_CLIENT_UPGRADE;
452 } else {
453 trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
454 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
455 }
David Howells2baec2c2017-05-24 17:02:32 +0100456 rxnet->nr_active_client_conns++;
457 list_move_tail(&conn->cache_link, &rxnet->active_client_conns);
David Howells45025bc2016-08-24 07:30:52 +0100458}
David Howells001c1122016-06-30 10:45:22 +0100459
David Howells45025bc2016-08-24 07:30:52 +0100460/*
461 * Attempt to animate a connection for a new call.
462 *
463 * If it's not exclusive, the connection is in the endpoint tree, and we're in
464 * the conn's list of those waiting to grab a channel. There is, however, a
465 * limit on the number of live connections allowed at any one time, so we may
466 * have to wait for capacity to become available.
467 *
468 * Note that a connection on the waiting queue might *also* have active
469 * channels if it has been culled to make space and then re-requested by a new
470 * call.
471 */
David Howells2baec2c2017-05-24 17:02:32 +0100472static void rxrpc_animate_client_conn(struct rxrpc_net *rxnet,
473 struct rxrpc_connection *conn)
David Howells45025bc2016-08-24 07:30:52 +0100474{
475 unsigned int nr_conns;
476
477 _enter("%d,%d", conn->debug_id, conn->cache_state);
478
David Howells4e255722017-06-05 14:30:49 +0100479 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE ||
480 conn->cache_state == RXRPC_CONN_CLIENT_UPGRADE)
David Howells45025bc2016-08-24 07:30:52 +0100481 goto out;
482
David Howells2baec2c2017-05-24 17:02:32 +0100483 spin_lock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100484
David Howells2baec2c2017-05-24 17:02:32 +0100485 nr_conns = rxnet->nr_client_conns;
David Howells363deea2016-09-17 10:49:14 +0100486 if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
487 trace_rxrpc_client(conn, -1, rxrpc_client_count);
David Howells2baec2c2017-05-24 17:02:32 +0100488 rxnet->nr_client_conns = nr_conns + 1;
David Howells363deea2016-09-17 10:49:14 +0100489 }
David Howells45025bc2016-08-24 07:30:52 +0100490
491 switch (conn->cache_state) {
492 case RXRPC_CONN_CLIENT_ACTIVE:
David Howells4e255722017-06-05 14:30:49 +0100493 case RXRPC_CONN_CLIENT_UPGRADE:
David Howells45025bc2016-08-24 07:30:52 +0100494 case RXRPC_CONN_CLIENT_WAITING:
495 break;
496
497 case RXRPC_CONN_CLIENT_INACTIVE:
498 case RXRPC_CONN_CLIENT_CULLED:
499 case RXRPC_CONN_CLIENT_IDLE:
500 if (nr_conns >= rxrpc_max_client_connections)
501 goto wait_for_capacity;
502 goto activate_conn;
503
504 default:
505 BUG();
506 }
507
508out_unlock:
David Howells2baec2c2017-05-24 17:02:32 +0100509 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100510out:
511 _leave(" [%d]", conn->cache_state);
512 return;
513
514activate_conn:
515 _debug("activate");
David Howells2baec2c2017-05-24 17:02:32 +0100516 rxrpc_activate_conn(rxnet, conn);
David Howells45025bc2016-08-24 07:30:52 +0100517 goto out_unlock;
518
519wait_for_capacity:
520 _debug("wait");
David Howells363deea2016-09-17 10:49:14 +0100521 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
David Howells45025bc2016-08-24 07:30:52 +0100522 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
David Howells2baec2c2017-05-24 17:02:32 +0100523 list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns);
David Howells45025bc2016-08-24 07:30:52 +0100524 goto out_unlock;
525}
526
527/*
528 * Deactivate a channel.
529 */
530static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
531 unsigned int channel)
532{
533 struct rxrpc_channel *chan = &conn->channels[channel];
534
535 rcu_assign_pointer(chan->call, NULL);
536 conn->active_chans &= ~(1 << channel);
537}
538
539/*
540 * Assign a channel to the call at the front of the queue and wake the call up.
541 * We don't increment the callNumber counter until this number has been exposed
542 * to the world.
543 */
544static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
545 unsigned int channel)
546{
547 struct rxrpc_channel *chan = &conn->channels[channel];
548 struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
549 struct rxrpc_call, chan_wait_link);
550 u32 call_id = chan->call_counter + 1;
551
David Howells363deea2016-09-17 10:49:14 +0100552 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
553
David Howells3136ef42017-11-24 10:18:41 +0000554 /* Cancel the final ACK on the previous call if it hasn't been sent yet
555 * as the DATA packet will implicitly ACK it.
556 */
557 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
558
David Howellsaf338a92016-09-04 13:10:10 +0100559 write_lock_bh(&call->state_lock);
David Howellse122d842019-01-10 16:59:13 +0000560 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
David Howellsaf338a92016-09-04 13:10:10 +0100561 write_unlock_bh(&call->state_lock);
562
David Howellse34d4232016-08-30 09:49:29 +0100563 rxrpc_see_call(call);
David Howells45025bc2016-08-24 07:30:52 +0100564 list_del_init(&call->chan_wait_link);
565 conn->active_chans |= 1 << channel;
566 call->peer = rxrpc_get_peer(conn->params.peer);
567 call->cid = conn->proto.cid | channel;
568 call->call_id = call_id;
569
David Howells89ca6942017-04-06 10:12:00 +0100570 trace_rxrpc_connect_call(call);
David Howells45025bc2016-08-24 07:30:52 +0100571 _net("CONNECT call %08x:%08x as call %d on conn %d",
572 call->cid, call->call_id, call->debug_id, conn->debug_id);
573
574 /* Paired with the read barrier in rxrpc_wait_for_channel(). This
575 * orders cid and epoch in the connection wrt to call_id without the
576 * need to take the channel_lock.
577 *
578 * We provisionally assign a callNumber at this point, but we don't
579 * confirm it until the call is about to be exposed.
580 *
581 * TODO: Pair with a barrier in the data_ready handler when that looks
582 * at the call ID through a connection channel.
583 */
584 smp_wmb();
585 chan->call_id = call_id;
David Howells4764c0d2018-07-23 17:18:37 +0100586 chan->call_debug_id = call->debug_id;
David Howells45025bc2016-08-24 07:30:52 +0100587 rcu_assign_pointer(chan->call, call);
588 wake_up(&call->waitq);
589}
590
591/*
David Howells2629c7f2016-09-29 22:37:15 +0100592 * Assign channels and callNumbers to waiting calls with channel_lock
593 * held by caller.
594 */
595static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
596{
597 u8 avail, mask;
598
599 switch (conn->cache_state) {
600 case RXRPC_CONN_CLIENT_ACTIVE:
601 mask = RXRPC_ACTIVE_CHANS_MASK;
602 break;
David Howells4e255722017-06-05 14:30:49 +0100603 case RXRPC_CONN_CLIENT_UPGRADE:
604 mask = 0x01;
605 break;
David Howells2629c7f2016-09-29 22:37:15 +0100606 default:
607 return;
608 }
609
610 while (!list_empty(&conn->waiting_calls) &&
611 (avail = ~conn->active_chans,
612 avail &= mask,
613 avail != 0))
614 rxrpc_activate_one_channel(conn, __ffs(avail));
615}
616
617/*
David Howells45025bc2016-08-24 07:30:52 +0100618 * Assign channels and callNumbers to waiting calls.
619 */
620static void rxrpc_activate_channels(struct rxrpc_connection *conn)
621{
David Howells45025bc2016-08-24 07:30:52 +0100622 _enter("%d", conn->debug_id);
623
David Howells363deea2016-09-17 10:49:14 +0100624 trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
625
David Howells2629c7f2016-09-29 22:37:15 +0100626 if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
David Howells45025bc2016-08-24 07:30:52 +0100627 return;
628
629 spin_lock(&conn->channel_lock);
David Howells2629c7f2016-09-29 22:37:15 +0100630 rxrpc_activate_channels_locked(conn);
David Howells45025bc2016-08-24 07:30:52 +0100631 spin_unlock(&conn->channel_lock);
632 _leave("");
633}
634
635/*
636 * Wait for a callNumber and a channel to be granted to a call.
637 */
638static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
639{
640 int ret = 0;
641
642 _enter("%d", call->debug_id);
643
644 if (!call->call_id) {
645 DECLARE_WAITQUEUE(myself, current);
646
647 if (!gfpflags_allow_blocking(gfp)) {
648 ret = -EAGAIN;
649 goto out;
650 }
651
652 add_wait_queue_exclusive(&call->waitq, &myself);
653 for (;;) {
David Howellsb960a342019-05-09 08:21:21 +0100654 if (test_bit(RXRPC_CALL_IS_INTR, &call->flags))
655 set_current_state(TASK_INTERRUPTIBLE);
656 else
657 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells45025bc2016-08-24 07:30:52 +0100658 if (call->call_id)
659 break;
David Howellsb960a342019-05-09 08:21:21 +0100660 if (test_bit(RXRPC_CALL_IS_INTR, &call->flags) &&
661 signal_pending(current)) {
David Howells45025bc2016-08-24 07:30:52 +0100662 ret = -ERESTARTSYS;
663 break;
664 }
665 schedule();
666 }
667 remove_wait_queue(&call->waitq, &myself);
668 __set_current_state(TASK_RUNNING);
669 }
670
671 /* Paired with the write barrier in rxrpc_activate_one_channel(). */
672 smp_rmb();
673
674out:
675 _leave(" = %d", ret);
676 return ret;
677}
678
679/*
680 * find a connection for a call
681 * - called in process context with IRQs enabled
682 */
David Howells5e33a232018-10-05 14:05:34 +0100683int rxrpc_connect_call(struct rxrpc_sock *rx,
684 struct rxrpc_call *call,
David Howells45025bc2016-08-24 07:30:52 +0100685 struct rxrpc_conn_parameters *cp,
686 struct sockaddr_rxrpc *srx,
687 gfp_t gfp)
688{
David Howells2baec2c2017-05-24 17:02:32 +0100689 struct rxrpc_net *rxnet = cp->local->rxnet;
David Howells45025bc2016-08-24 07:30:52 +0100690 int ret;
691
692 _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
693
David Howells3d18cbb2017-11-24 10:18:42 +0000694 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
David Howells2baec2c2017-05-24 17:02:32 +0100695 rxrpc_cull_active_client_conns(rxnet);
David Howells45025bc2016-08-24 07:30:52 +0100696
David Howells5e33a232018-10-05 14:05:34 +0100697 ret = rxrpc_get_client_conn(rx, call, cp, srx, gfp);
David Howells45025bc2016-08-24 07:30:52 +0100698 if (ret < 0)
David Howellsc038a582017-08-29 10:19:01 +0100699 goto out;
David Howells45025bc2016-08-24 07:30:52 +0100700
David Howells2baec2c2017-05-24 17:02:32 +0100701 rxrpc_animate_client_conn(rxnet, call->conn);
David Howells45025bc2016-08-24 07:30:52 +0100702 rxrpc_activate_channels(call->conn);
703
704 ret = rxrpc_wait_for_channel(call, gfp);
David Howellsc038a582017-08-29 10:19:01 +0100705 if (ret < 0) {
David Howells930c9f92019-03-08 12:48:39 +0000706 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
David Howells45025bc2016-08-24 07:30:52 +0100707 rxrpc_disconnect_client_call(call);
David Howellsc038a582017-08-29 10:19:01 +0100708 goto out;
709 }
David Howells45025bc2016-08-24 07:30:52 +0100710
David Howellsc038a582017-08-29 10:19:01 +0100711 spin_lock_bh(&call->conn->params.peer->lock);
David Howellsf3344302018-09-27 15:13:09 +0100712 hlist_add_head_rcu(&call->error_link,
713 &call->conn->params.peer->error_targets);
David Howellsc038a582017-08-29 10:19:01 +0100714 spin_unlock_bh(&call->conn->params.peer->lock);
715
716out:
David Howells45025bc2016-08-24 07:30:52 +0100717 _leave(" = %d", ret);
718 return ret;
719}
720
721/*
722 * Note that a connection is about to be exposed to the world. Once it is
723 * exposed, we maintain an extra ref on it that stops it from being summarily
724 * discarded before it's (a) had a chance to deal with retransmission and (b)
725 * had a chance at re-use (the per-connection security negotiation is
726 * expensive).
727 */
David Howells363deea2016-09-17 10:49:14 +0100728static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
729 unsigned int channel)
David Howells45025bc2016-08-24 07:30:52 +0100730{
David Howells363deea2016-09-17 10:49:14 +0100731 if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
732 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
David Howells45025bc2016-08-24 07:30:52 +0100733 rxrpc_get_connection(conn);
David Howells363deea2016-09-17 10:49:14 +0100734 }
David Howells45025bc2016-08-24 07:30:52 +0100735}
736
737/*
738 * Note that a call, and thus a connection, is about to be exposed to the
739 * world.
740 */
741void rxrpc_expose_client_call(struct rxrpc_call *call)
742{
David Howells363deea2016-09-17 10:49:14 +0100743 unsigned int channel = call->cid & RXRPC_CHANNELMASK;
David Howells45025bc2016-08-24 07:30:52 +0100744 struct rxrpc_connection *conn = call->conn;
David Howells363deea2016-09-17 10:49:14 +0100745 struct rxrpc_channel *chan = &conn->channels[channel];
David Howells45025bc2016-08-24 07:30:52 +0100746
747 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
748 /* Mark the call ID as being used. If the callNumber counter
749 * exceeds ~2 billion, we kill the connection after its
750 * outstanding calls have finished so that the counter doesn't
751 * wrap.
752 */
753 chan->call_counter++;
754 if (chan->call_counter >= INT_MAX)
755 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
David Howells363deea2016-09-17 10:49:14 +0100756 rxrpc_expose_client_conn(conn, channel);
David Howells45025bc2016-08-24 07:30:52 +0100757 }
758}
759
760/*
David Howells3d18cbb2017-11-24 10:18:42 +0000761 * Set the reap timer.
762 */
763static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
764{
765 unsigned long now = jiffies;
766 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
767
768 if (rxnet->live)
769 timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
770}
771
772/*
David Howells45025bc2016-08-24 07:30:52 +0100773 * Disconnect a client call.
774 */
775void rxrpc_disconnect_client_call(struct rxrpc_call *call)
776{
David Howells45025bc2016-08-24 07:30:52 +0100777 struct rxrpc_connection *conn = call->conn;
David Howells930c9f92019-03-08 12:48:39 +0000778 struct rxrpc_channel *chan = NULL;
David Howells88f2a8252018-03-30 21:05:17 +0100779 struct rxrpc_net *rxnet = conn->params.local->rxnet;
David Howells930c9f92019-03-08 12:48:39 +0000780 unsigned int channel = -1;
781 u32 cid;
David Howells45025bc2016-08-24 07:30:52 +0100782
783 spin_lock(&conn->channel_lock);
784
David Howells930c9f92019-03-08 12:48:39 +0000785 cid = call->cid;
786 if (cid) {
787 channel = cid & RXRPC_CHANNELMASK;
788 chan = &conn->channels[channel];
789 }
790 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
791 call->conn = NULL;
792
David Howells45025bc2016-08-24 07:30:52 +0100793 /* Calls that have never actually been assigned a channel can simply be
794 * discarded. If the conn didn't get used either, it will follow
795 * immediately unless someone else grabs it in the meantime.
796 */
797 if (!list_empty(&call->chan_wait_link)) {
798 _debug("call is waiting");
799 ASSERTCMP(call->call_id, ==, 0);
800 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
801 list_del_init(&call->chan_wait_link);
802
David Howells363deea2016-09-17 10:49:14 +0100803 trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
804
David Howells45025bc2016-08-24 07:30:52 +0100805 /* We must deactivate or idle the connection if it's now
806 * waiting for nothing.
807 */
David Howells2baec2c2017-05-24 17:02:32 +0100808 spin_lock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100809 if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
810 list_empty(&conn->waiting_calls) &&
811 !conn->active_chans)
812 goto idle_connection;
813 goto out;
814 }
815
David Howells930c9f92019-03-08 12:48:39 +0000816 if (rcu_access_pointer(chan->call) != call) {
817 spin_unlock(&conn->channel_lock);
818 BUG();
819 }
David Howells45025bc2016-08-24 07:30:52 +0100820
821 /* If a client call was exposed to the world, we save the result for
822 * retransmission.
823 *
824 * We use a barrier here so that the call number and abort code can be
825 * read without needing to take a lock.
826 *
827 * TODO: Make the incoming packet handler check this and handle
828 * terminal retransmission without requiring access to the call.
829 */
830 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100831 _debug("exposed %u,%u", call->call_id, call->abort_code);
David Howells45025bc2016-08-24 07:30:52 +0100832 __rxrpc_disconnect_call(conn, call);
833 }
834
835 /* See if we can pass the channel directly to another call. */
836 if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
837 !list_empty(&conn->waiting_calls)) {
David Howells363deea2016-09-17 10:49:14 +0100838 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
David Howells45025bc2016-08-24 07:30:52 +0100839 rxrpc_activate_one_channel(conn, channel);
840 goto out_2;
841 }
842
David Howells3136ef42017-11-24 10:18:41 +0000843 /* Schedule the final ACK to be transmitted in a short while so that it
844 * can be skipped if we find a follow-on call. The first DATA packet
845 * of the follow on call will implicitly ACK this call.
846 */
David Howells17e9e232018-02-06 16:44:12 +0000847 if (call->completion == RXRPC_CALL_SUCCEEDED &&
848 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
David Howells3136ef42017-11-24 10:18:41 +0000849 unsigned long final_ack_at = jiffies + 2;
850
851 WRITE_ONCE(chan->final_ack_at, final_ack_at);
852 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
853 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
854 rxrpc_reduce_conn_timer(conn, final_ack_at);
855 }
856
David Howells45025bc2016-08-24 07:30:52 +0100857 /* Things are more complex and we need the cache lock. We might be
858 * able to simply idle the conn or it might now be lurking on the wait
859 * list. It might even get moved back to the active list whilst we're
860 * waiting for the lock.
861 */
David Howells2baec2c2017-05-24 17:02:32 +0100862 spin_lock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100863
864 switch (conn->cache_state) {
David Howells4e255722017-06-05 14:30:49 +0100865 case RXRPC_CONN_CLIENT_UPGRADE:
866 /* Deal with termination of a service upgrade probe. */
867 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
868 clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
869 trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
870 conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
871 rxrpc_activate_channels_locked(conn);
872 }
873 /* fall through */
David Howells45025bc2016-08-24 07:30:52 +0100874 case RXRPC_CONN_CLIENT_ACTIVE:
875 if (list_empty(&conn->waiting_calls)) {
876 rxrpc_deactivate_one_channel(conn, channel);
877 if (!conn->active_chans) {
David Howells2baec2c2017-05-24 17:02:32 +0100878 rxnet->nr_active_client_conns--;
David Howells45025bc2016-08-24 07:30:52 +0100879 goto idle_connection;
880 }
881 goto out;
882 }
883
David Howells363deea2016-09-17 10:49:14 +0100884 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
David Howells45025bc2016-08-24 07:30:52 +0100885 rxrpc_activate_one_channel(conn, channel);
886 goto out;
887
888 case RXRPC_CONN_CLIENT_CULLED:
889 rxrpc_deactivate_one_channel(conn, channel);
890 ASSERT(list_empty(&conn->waiting_calls));
891 if (!conn->active_chans)
892 goto idle_connection;
893 goto out;
894
895 case RXRPC_CONN_CLIENT_WAITING:
896 rxrpc_deactivate_one_channel(conn, channel);
897 goto out;
898
899 default:
900 BUG();
901 }
902
903out:
David Howells2baec2c2017-05-24 17:02:32 +0100904 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100905out_2:
906 spin_unlock(&conn->channel_lock);
907 rxrpc_put_connection(conn);
908 _leave("");
909 return;
910
911idle_connection:
912 /* As no channels remain active, the connection gets deactivated
913 * immediately or moved to the idle list for a short while.
914 */
915 if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
David Howells363deea2016-09-17 10:49:14 +0100916 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
David Howells45025bc2016-08-24 07:30:52 +0100917 conn->idle_timestamp = jiffies;
918 conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
David Howells2baec2c2017-05-24 17:02:32 +0100919 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
920 if (rxnet->idle_client_conns.next == &conn->cache_link &&
921 !rxnet->kill_all_client_conns)
David Howells3d18cbb2017-11-24 10:18:42 +0000922 rxrpc_set_client_reap_timer(rxnet);
David Howells45025bc2016-08-24 07:30:52 +0100923 } else {
David Howells363deea2016-09-17 10:49:14 +0100924 trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
David Howells45025bc2016-08-24 07:30:52 +0100925 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
926 list_del_init(&conn->cache_link);
927 }
928 goto out;
929}
930
931/*
932 * Clean up a dead client connection.
933 */
934static struct rxrpc_connection *
935rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
936{
David Howells66d58af2016-09-17 10:49:12 +0100937 struct rxrpc_connection *next = NULL;
David Howells45025bc2016-08-24 07:30:52 +0100938 struct rxrpc_local *local = conn->params.local;
David Howells2baec2c2017-05-24 17:02:32 +0100939 struct rxrpc_net *rxnet = local->rxnet;
David Howells45025bc2016-08-24 07:30:52 +0100940 unsigned int nr_conns;
941
David Howells363deea2016-09-17 10:49:14 +0100942 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
943
David Howells45025bc2016-08-24 07:30:52 +0100944 if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
945 spin_lock(&local->client_conns_lock);
946 if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
947 &conn->flags))
948 rb_erase(&conn->client_node, &local->client_conns);
949 spin_unlock(&local->client_conns_lock);
950 }
David Howells001c1122016-06-30 10:45:22 +0100951
952 rxrpc_put_client_connection_id(conn);
David Howells45025bc2016-08-24 07:30:52 +0100953
954 ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
955
David Howells66d58af2016-09-17 10:49:12 +0100956 if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
David Howells363deea2016-09-17 10:49:14 +0100957 trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
David Howells2baec2c2017-05-24 17:02:32 +0100958 spin_lock(&rxnet->client_conn_cache_lock);
959 nr_conns = --rxnet->nr_client_conns;
David Howells45025bc2016-08-24 07:30:52 +0100960
David Howells66d58af2016-09-17 10:49:12 +0100961 if (nr_conns < rxrpc_max_client_connections &&
David Howells2baec2c2017-05-24 17:02:32 +0100962 !list_empty(&rxnet->waiting_client_conns)) {
963 next = list_entry(rxnet->waiting_client_conns.next,
David Howells66d58af2016-09-17 10:49:12 +0100964 struct rxrpc_connection, cache_link);
965 rxrpc_get_connection(next);
David Howells2baec2c2017-05-24 17:02:32 +0100966 rxrpc_activate_conn(rxnet, next);
David Howells66d58af2016-09-17 10:49:12 +0100967 }
David Howells45025bc2016-08-24 07:30:52 +0100968
David Howells2baec2c2017-05-24 17:02:32 +0100969 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +0100970 }
971
David Howells45025bc2016-08-24 07:30:52 +0100972 rxrpc_kill_connection(conn);
David Howells45025bc2016-08-24 07:30:52 +0100973 if (next)
974 rxrpc_activate_channels(next);
975
976 /* We need to get rid of the temporary ref we took upon next, but we
977 * can't call rxrpc_put_connection() recursively.
978 */
979 return next;
980}
981
982/*
983 * Clean up a dead client connections.
984 */
985void rxrpc_put_client_conn(struct rxrpc_connection *conn)
986{
David Howells363deea2016-09-17 10:49:14 +0100987 const void *here = __builtin_return_address(0);
988 int n;
David Howells45025bc2016-08-24 07:30:52 +0100989
990 do {
David Howells363deea2016-09-17 10:49:14 +0100991 n = atomic_dec_return(&conn->usage);
992 trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
993 if (n > 0)
994 return;
995 ASSERTCMP(n, >=, 0);
David Howells45025bc2016-08-24 07:30:52 +0100996
David Howells363deea2016-09-17 10:49:14 +0100997 conn = rxrpc_put_one_client_conn(conn);
998 } while (conn);
David Howells45025bc2016-08-24 07:30:52 +0100999}
1000
1001/*
1002 * Kill the longest-active client connections to make room for new ones.
1003 */
David Howells2baec2c2017-05-24 17:02:32 +01001004static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
David Howells45025bc2016-08-24 07:30:52 +01001005{
1006 struct rxrpc_connection *conn;
David Howells2baec2c2017-05-24 17:02:32 +01001007 unsigned int nr_conns = rxnet->nr_client_conns;
David Howells45025bc2016-08-24 07:30:52 +01001008 unsigned int nr_active, limit;
1009
1010 _enter("");
1011
1012 ASSERTCMP(nr_conns, >=, 0);
1013 if (nr_conns < rxrpc_max_client_connections) {
1014 _leave(" [ok]");
1015 return;
1016 }
1017 limit = rxrpc_reap_client_connections;
1018
David Howells2baec2c2017-05-24 17:02:32 +01001019 spin_lock(&rxnet->client_conn_cache_lock);
1020 nr_active = rxnet->nr_active_client_conns;
David Howells45025bc2016-08-24 07:30:52 +01001021
1022 while (nr_active > limit) {
David Howells2baec2c2017-05-24 17:02:32 +01001023 ASSERT(!list_empty(&rxnet->active_client_conns));
1024 conn = list_entry(rxnet->active_client_conns.next,
David Howells45025bc2016-08-24 07:30:52 +01001025 struct rxrpc_connection, cache_link);
David Howells4e255722017-06-05 14:30:49 +01001026 ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE,
1027 conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE);
David Howells45025bc2016-08-24 07:30:52 +01001028
1029 if (list_empty(&conn->waiting_calls)) {
David Howells363deea2016-09-17 10:49:14 +01001030 trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
David Howells45025bc2016-08-24 07:30:52 +01001031 conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
1032 list_del_init(&conn->cache_link);
1033 } else {
David Howells363deea2016-09-17 10:49:14 +01001034 trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
David Howells45025bc2016-08-24 07:30:52 +01001035 conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
1036 list_move_tail(&conn->cache_link,
David Howells2baec2c2017-05-24 17:02:32 +01001037 &rxnet->waiting_client_conns);
David Howells45025bc2016-08-24 07:30:52 +01001038 }
1039
1040 nr_active--;
1041 }
1042
David Howells2baec2c2017-05-24 17:02:32 +01001043 rxnet->nr_active_client_conns = nr_active;
1044 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001045 ASSERTCMP(nr_active, >=, 0);
1046 _leave(" [culled]");
1047}
1048
1049/*
1050 * Discard expired client connections from the idle list. Each conn in the
1051 * idle list has been exposed and holds an extra ref because of that.
1052 *
1053 * This may be called from conn setup or from a work item so cannot be
1054 * considered non-reentrant.
1055 */
David Howells2baec2c2017-05-24 17:02:32 +01001056void rxrpc_discard_expired_client_conns(struct work_struct *work)
David Howells45025bc2016-08-24 07:30:52 +01001057{
1058 struct rxrpc_connection *conn;
David Howells2baec2c2017-05-24 17:02:32 +01001059 struct rxrpc_net *rxnet =
David Howells3d18cbb2017-11-24 10:18:42 +00001060 container_of(work, struct rxrpc_net, client_conn_reaper);
David Howells45025bc2016-08-24 07:30:52 +01001061 unsigned long expiry, conn_expires_at, now;
1062 unsigned int nr_conns;
David Howells45025bc2016-08-24 07:30:52 +01001063
David Howells2baec2c2017-05-24 17:02:32 +01001064 _enter("");
David Howells45025bc2016-08-24 07:30:52 +01001065
David Howells2baec2c2017-05-24 17:02:32 +01001066 if (list_empty(&rxnet->idle_client_conns)) {
David Howells45025bc2016-08-24 07:30:52 +01001067 _leave(" [empty]");
1068 return;
1069 }
1070
1071 /* Don't double up on the discarding */
David Howells2baec2c2017-05-24 17:02:32 +01001072 if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
David Howells45025bc2016-08-24 07:30:52 +01001073 _leave(" [already]");
1074 return;
1075 }
1076
1077 /* We keep an estimate of what the number of conns ought to be after
1078 * we've discarded some so that we don't overdo the discarding.
1079 */
David Howells2baec2c2017-05-24 17:02:32 +01001080 nr_conns = rxnet->nr_client_conns;
David Howells45025bc2016-08-24 07:30:52 +01001081
1082next:
David Howells2baec2c2017-05-24 17:02:32 +01001083 spin_lock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001084
David Howells2baec2c2017-05-24 17:02:32 +01001085 if (list_empty(&rxnet->idle_client_conns))
David Howells45025bc2016-08-24 07:30:52 +01001086 goto out;
1087
David Howells2baec2c2017-05-24 17:02:32 +01001088 conn = list_entry(rxnet->idle_client_conns.next,
David Howells45025bc2016-08-24 07:30:52 +01001089 struct rxrpc_connection, cache_link);
1090 ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
1091
David Howells2baec2c2017-05-24 17:02:32 +01001092 if (!rxnet->kill_all_client_conns) {
David Howells45025bc2016-08-24 07:30:52 +01001093 /* If the number of connections is over the reap limit, we
1094 * expedite discard by reducing the expiry timeout. We must,
1095 * however, have at least a short grace period to be able to do
1096 * final-ACK or ABORT retransmission.
1097 */
1098 expiry = rxrpc_conn_idle_client_expiry;
1099 if (nr_conns > rxrpc_reap_client_connections)
1100 expiry = rxrpc_conn_idle_client_fast_expiry;
David Howellsf859ab62017-11-24 10:18:42 +00001101 if (conn->params.local->service_closed)
1102 expiry = rxrpc_closed_conn_expiry * HZ;
David Howells45025bc2016-08-24 07:30:52 +01001103
1104 conn_expires_at = conn->idle_timestamp + expiry;
1105
1106 now = READ_ONCE(jiffies);
1107 if (time_after(conn_expires_at, now))
1108 goto not_yet_expired;
1109 }
1110
David Howells363deea2016-09-17 10:49:14 +01001111 trace_rxrpc_client(conn, -1, rxrpc_client_discard);
David Howells45025bc2016-08-24 07:30:52 +01001112 if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
1113 BUG();
1114 conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
1115 list_del_init(&conn->cache_link);
1116
David Howells2baec2c2017-05-24 17:02:32 +01001117 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001118
1119 /* When we cleared the EXPOSED flag, we took on responsibility for the
1120 * reference that that had on the usage count. We deal with that here.
1121 * If someone re-sets the flag and re-gets the ref, that's fine.
1122 */
1123 rxrpc_put_connection(conn);
David Howells45025bc2016-08-24 07:30:52 +01001124 nr_conns--;
1125 goto next;
1126
1127not_yet_expired:
1128 /* The connection at the front of the queue hasn't yet expired, so
1129 * schedule the work item for that point if we discarded something.
1130 *
1131 * We don't worry if the work item is already scheduled - it can look
1132 * after rescheduling itself at a later time. We could cancel it, but
1133 * then things get messier.
1134 */
1135 _debug("not yet");
David Howells2baec2c2017-05-24 17:02:32 +01001136 if (!rxnet->kill_all_client_conns)
David Howells3d18cbb2017-11-24 10:18:42 +00001137 timer_reduce(&rxnet->client_conn_reap_timer,
1138 conn_expires_at);
David Howells45025bc2016-08-24 07:30:52 +01001139
1140out:
David Howells2baec2c2017-05-24 17:02:32 +01001141 spin_unlock(&rxnet->client_conn_cache_lock);
1142 spin_unlock(&rxnet->client_conn_discard_lock);
David Howells45025bc2016-08-24 07:30:52 +01001143 _leave("");
1144}
1145
1146/*
1147 * Preemptively destroy all the client connection records rather than waiting
1148 * for them to time out
1149 */
David Howells2baec2c2017-05-24 17:02:32 +01001150void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
David Howells45025bc2016-08-24 07:30:52 +01001151{
1152 _enter("");
1153
David Howells2baec2c2017-05-24 17:02:32 +01001154 spin_lock(&rxnet->client_conn_cache_lock);
1155 rxnet->kill_all_client_conns = true;
1156 spin_unlock(&rxnet->client_conn_cache_lock);
David Howells45025bc2016-08-24 07:30:52 +01001157
David Howells3d18cbb2017-11-24 10:18:42 +00001158 del_timer_sync(&rxnet->client_conn_reap_timer);
David Howells45025bc2016-08-24 07:30:52 +01001159
David Howells3d18cbb2017-11-24 10:18:42 +00001160 if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
David Howells45025bc2016-08-24 07:30:52 +01001161 _debug("destroy: queue failed");
1162
1163 _leave("");
David Howells001c1122016-06-30 10:45:22 +01001164}