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