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