rxrpc: Trace rxrpc_bundle refcount
[platform/kernel/linux-rpi.git] / net / rxrpc / conn_client.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Client connection-specific management code.
3  *
4  * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * 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  * There are flags of relevance to the cache:
12  *
13  *  (2) DONT_REUSE - The connection should be discarded as soon as possible and
14  *      should not be reused.  This is set when an exclusive connection is used
15  *      or a call ID counter overflows.
16  *
17  * The caching state may only be changed if the cache lock is held.
18  *
19  * There are two idle client connection expiry durations.  If the total number
20  * of connections is below the reap threshold, we use the normal duration; if
21  * it's above, we use the fast duration.
22  */
23
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/timer.h>
29 #include <linux/sched/signal.h>
30
31 #include "ar-internal.h"
32
33 __read_mostly unsigned int rxrpc_reap_client_connections = 900;
34 __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
35 __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
36
37 /*
38  * We use machine-unique IDs for our client connections.
39  */
40 DEFINE_IDR(rxrpc_client_conn_ids);
41 static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
42
43 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle);
44
45 /*
46  * Get a connection ID and epoch for a client connection from the global pool.
47  * The connection struct pointer is then recorded in the idr radix tree.  The
48  * epoch doesn't change until the client is rebooted (or, at least, unless the
49  * module is unloaded).
50  */
51 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
52                                           gfp_t gfp)
53 {
54         struct rxrpc_net *rxnet = conn->local->rxnet;
55         int id;
56
57         _enter("");
58
59         idr_preload(gfp);
60         spin_lock(&rxrpc_conn_id_lock);
61
62         id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
63                               1, 0x40000000, GFP_NOWAIT);
64         if (id < 0)
65                 goto error;
66
67         spin_unlock(&rxrpc_conn_id_lock);
68         idr_preload_end();
69
70         conn->proto.epoch = rxnet->epoch;
71         conn->proto.cid = id << RXRPC_CIDSHIFT;
72         set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
73         _leave(" [CID %x]", conn->proto.cid);
74         return 0;
75
76 error:
77         spin_unlock(&rxrpc_conn_id_lock);
78         idr_preload_end();
79         _leave(" = %d", id);
80         return id;
81 }
82
83 /*
84  * Release a connection ID for a client connection from the global pool.
85  */
86 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
87 {
88         if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
89                 spin_lock(&rxrpc_conn_id_lock);
90                 idr_remove(&rxrpc_client_conn_ids,
91                            conn->proto.cid >> RXRPC_CIDSHIFT);
92                 spin_unlock(&rxrpc_conn_id_lock);
93         }
94 }
95
96 /*
97  * Destroy the client connection ID tree.
98  */
99 void rxrpc_destroy_client_conn_ids(void)
100 {
101         struct rxrpc_connection *conn;
102         int id;
103
104         if (!idr_is_empty(&rxrpc_client_conn_ids)) {
105                 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
106                         pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
107                                conn, refcount_read(&conn->ref));
108                 }
109                 BUG();
110         }
111
112         idr_destroy(&rxrpc_client_conn_ids);
113 }
114
115 /*
116  * Allocate a connection bundle.
117  */
118 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp,
119                                                gfp_t gfp)
120 {
121         struct rxrpc_bundle *bundle;
122
123         bundle = kzalloc(sizeof(*bundle), gfp);
124         if (bundle) {
125                 bundle->local           = cp->local;
126                 bundle->peer            = rxrpc_get_peer(cp->peer, rxrpc_peer_get_bundle);
127                 bundle->key             = cp->key;
128                 bundle->exclusive       = cp->exclusive;
129                 bundle->upgrade         = cp->upgrade;
130                 bundle->service_id      = cp->service_id;
131                 bundle->security_level  = cp->security_level;
132                 refcount_set(&bundle->ref, 1);
133                 atomic_set(&bundle->active, 1);
134                 spin_lock_init(&bundle->channel_lock);
135                 INIT_LIST_HEAD(&bundle->waiting_calls);
136                 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new);
137         }
138         return bundle;
139 }
140
141 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle,
142                                       enum rxrpc_bundle_trace why)
143 {
144         int r;
145
146         __refcount_inc(&bundle->ref, &r);
147         trace_rxrpc_bundle(bundle->debug_id, r + 1, why);
148         return bundle;
149 }
150
151 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle)
152 {
153         trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_free);
154         rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle);
155         kfree(bundle);
156 }
157
158 void rxrpc_put_bundle(struct rxrpc_bundle *bundle, enum rxrpc_bundle_trace why)
159 {
160         unsigned int id = bundle->debug_id;
161         bool dead;
162         int r;
163
164         dead = __refcount_dec_and_test(&bundle->ref, &r);
165         trace_rxrpc_bundle(id, r - 1, why);
166         if (dead)
167                 rxrpc_free_bundle(bundle);
168 }
169
170 /*
171  * Allocate a client connection.
172  */
173 static struct rxrpc_connection *
174 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp)
175 {
176         struct rxrpc_connection *conn;
177         struct rxrpc_net *rxnet = bundle->local->rxnet;
178         int ret;
179
180         _enter("");
181
182         conn = rxrpc_alloc_connection(gfp);
183         if (!conn) {
184                 _leave(" = -ENOMEM");
185                 return ERR_PTR(-ENOMEM);
186         }
187
188         refcount_set(&conn->ref, 1);
189         conn->bundle            = bundle;
190         conn->local             = bundle->local;
191         conn->peer              = bundle->peer;
192         conn->key               = bundle->key;
193         conn->exclusive         = bundle->exclusive;
194         conn->upgrade           = bundle->upgrade;
195         conn->orig_service_id   = bundle->service_id;
196         conn->security_level    = bundle->security_level;
197         conn->out_clientflag    = RXRPC_CLIENT_INITIATED;
198         conn->state             = RXRPC_CONN_CLIENT;
199         conn->service_id        = conn->orig_service_id;
200
201         ret = rxrpc_get_client_connection_id(conn, gfp);
202         if (ret < 0)
203                 goto error_0;
204
205         ret = rxrpc_init_client_conn_security(conn);
206         if (ret < 0)
207                 goto error_1;
208
209         atomic_inc(&rxnet->nr_conns);
210         write_lock(&rxnet->conn_lock);
211         list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
212         write_unlock(&rxnet->conn_lock);
213
214         rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn);
215         rxrpc_get_peer(conn->peer, rxrpc_peer_get_client_conn);
216         rxrpc_get_local(conn->local, rxrpc_local_get_client_conn);
217         key_get(conn->key);
218
219         trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
220                          rxrpc_conn_new_client);
221
222         atomic_inc(&rxnet->nr_client_conns);
223         trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
224         _leave(" = %p", conn);
225         return conn;
226
227 error_1:
228         rxrpc_put_client_connection_id(conn);
229 error_0:
230         kfree(conn);
231         _leave(" = %d", ret);
232         return ERR_PTR(ret);
233 }
234
235 /*
236  * Determine if a connection may be reused.
237  */
238 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
239 {
240         struct rxrpc_net *rxnet;
241         int id_cursor, id, distance, limit;
242
243         if (!conn)
244                 goto dont_reuse;
245
246         rxnet = conn->local->rxnet;
247         if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
248                 goto dont_reuse;
249
250         if (conn->state != RXRPC_CONN_CLIENT ||
251             conn->proto.epoch != rxnet->epoch)
252                 goto mark_dont_reuse;
253
254         /* The IDR tree gets very expensive on memory if the connection IDs are
255          * widely scattered throughout the number space, so we shall want to
256          * kill off connections that, say, have an ID more than about four
257          * times the maximum number of client conns away from the current
258          * allocation point to try and keep the IDs concentrated.
259          */
260         id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
261         id = conn->proto.cid >> RXRPC_CIDSHIFT;
262         distance = id - id_cursor;
263         if (distance < 0)
264                 distance = -distance;
265         limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024);
266         if (distance > limit)
267                 goto mark_dont_reuse;
268
269         return true;
270
271 mark_dont_reuse:
272         set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
273 dont_reuse:
274         return false;
275 }
276
277 /*
278  * Look up the conn bundle that matches the connection parameters, adding it if
279  * it doesn't yet exist.
280  */
281 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp,
282                                                  gfp_t gfp)
283 {
284         static atomic_t rxrpc_bundle_id;
285         struct rxrpc_bundle *bundle, *candidate;
286         struct rxrpc_local *local = cp->local;
287         struct rb_node *p, **pp, *parent;
288         long diff;
289
290         _enter("{%px,%x,%u,%u}",
291                cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade);
292
293         if (cp->exclusive)
294                 return rxrpc_alloc_bundle(cp, gfp);
295
296         /* First, see if the bundle is already there. */
297         _debug("search 1");
298         spin_lock(&local->client_bundles_lock);
299         p = local->client_bundles.rb_node;
300         while (p) {
301                 bundle = rb_entry(p, struct rxrpc_bundle, local_node);
302
303 #define cmp(X) ((long)bundle->X - (long)cp->X)
304                 diff = (cmp(peer) ?:
305                         cmp(key) ?:
306                         cmp(security_level) ?:
307                         cmp(upgrade));
308 #undef cmp
309                 if (diff < 0)
310                         p = p->rb_left;
311                 else if (diff > 0)
312                         p = p->rb_right;
313                 else
314                         goto found_bundle;
315         }
316         spin_unlock(&local->client_bundles_lock);
317         _debug("not found");
318
319         /* It wasn't.  We need to add one. */
320         candidate = rxrpc_alloc_bundle(cp, gfp);
321         if (!candidate)
322                 return NULL;
323
324         _debug("search 2");
325         spin_lock(&local->client_bundles_lock);
326         pp = &local->client_bundles.rb_node;
327         parent = NULL;
328         while (*pp) {
329                 parent = *pp;
330                 bundle = rb_entry(parent, struct rxrpc_bundle, local_node);
331
332 #define cmp(X) ((long)bundle->X - (long)cp->X)
333                 diff = (cmp(peer) ?:
334                         cmp(key) ?:
335                         cmp(security_level) ?:
336                         cmp(upgrade));
337 #undef cmp
338                 if (diff < 0)
339                         pp = &(*pp)->rb_left;
340                 else if (diff > 0)
341                         pp = &(*pp)->rb_right;
342                 else
343                         goto found_bundle_free;
344         }
345
346         _debug("new bundle");
347         candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id);
348         rb_link_node(&candidate->local_node, parent, pp);
349         rb_insert_color(&candidate->local_node, &local->client_bundles);
350         rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call);
351         spin_unlock(&local->client_bundles_lock);
352         _leave(" = %u [new]", candidate->debug_id);
353         return candidate;
354
355 found_bundle_free:
356         rxrpc_free_bundle(candidate);
357 found_bundle:
358         rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call);
359         atomic_inc(&bundle->active);
360         spin_unlock(&local->client_bundles_lock);
361         _leave(" = %u [found]", bundle->debug_id);
362         return bundle;
363 }
364
365 /*
366  * Create or find a client bundle to use for a call.
367  *
368  * If we return with a connection, the call will be on its waiting list.  It's
369  * left to the caller to assign a channel and wake up the call.
370  */
371 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx,
372                                             struct rxrpc_call *call,
373                                             struct rxrpc_conn_parameters *cp,
374                                             struct sockaddr_rxrpc *srx,
375                                             gfp_t gfp)
376 {
377         struct rxrpc_bundle *bundle;
378
379         _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
380
381         cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
382         if (!cp->peer)
383                 goto error;
384
385         call->tx_last_sent = ktime_get_real();
386         call->cong_ssthresh = cp->peer->cong_ssthresh;
387         if (call->cong_cwnd >= call->cong_ssthresh)
388                 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
389         else
390                 call->cong_mode = RXRPC_CALL_SLOW_START;
391         if (cp->upgrade)
392                 __set_bit(RXRPC_CALL_UPGRADE, &call->flags);
393
394         /* Find the client connection bundle. */
395         bundle = rxrpc_look_up_bundle(cp, gfp);
396         if (!bundle)
397                 goto error;
398
399         /* Get this call queued.  Someone else may activate it whilst we're
400          * lining up a new connection, but that's fine.
401          */
402         spin_lock(&bundle->channel_lock);
403         list_add_tail(&call->chan_wait_link, &bundle->waiting_calls);
404         spin_unlock(&bundle->channel_lock);
405
406         _leave(" = [B=%x]", bundle->debug_id);
407         return bundle;
408
409 error:
410         _leave(" = -ENOMEM");
411         return ERR_PTR(-ENOMEM);
412 }
413
414 /*
415  * Allocate a new connection and add it into a bundle.
416  */
417 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp)
418         __releases(bundle->channel_lock)
419 {
420         struct rxrpc_connection *candidate = NULL, *old = NULL;
421         bool conflict;
422         int i;
423
424         _enter("");
425
426         conflict = bundle->alloc_conn;
427         if (!conflict)
428                 bundle->alloc_conn = true;
429         spin_unlock(&bundle->channel_lock);
430         if (conflict) {
431                 _leave(" [conf]");
432                 return;
433         }
434
435         candidate = rxrpc_alloc_client_connection(bundle, gfp);
436
437         spin_lock(&bundle->channel_lock);
438         bundle->alloc_conn = false;
439
440         if (IS_ERR(candidate)) {
441                 bundle->alloc_error = PTR_ERR(candidate);
442                 spin_unlock(&bundle->channel_lock);
443                 _leave(" [err %ld]", PTR_ERR(candidate));
444                 return;
445         }
446
447         bundle->alloc_error = 0;
448
449         for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) {
450                 unsigned int shift = i * RXRPC_MAXCALLS;
451                 int j;
452
453                 old = bundle->conns[i];
454                 if (!rxrpc_may_reuse_conn(old)) {
455                         if (old)
456                                 trace_rxrpc_client(old, -1, rxrpc_client_replace);
457                         candidate->bundle_shift = shift;
458                         atomic_inc(&bundle->active);
459                         bundle->conns[i] = candidate;
460                         for (j = 0; j < RXRPC_MAXCALLS; j++)
461                                 set_bit(shift + j, &bundle->avail_chans);
462                         candidate = NULL;
463                         break;
464                 }
465
466                 old = NULL;
467         }
468
469         spin_unlock(&bundle->channel_lock);
470
471         if (candidate) {
472                 _debug("discard C=%x", candidate->debug_id);
473                 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
474                 rxrpc_put_connection(candidate, rxrpc_conn_put_discard);
475         }
476
477         rxrpc_put_connection(old, rxrpc_conn_put_noreuse);
478         _leave("");
479 }
480
481 /*
482  * Add a connection to a bundle if there are no usable connections or we have
483  * connections waiting for extra capacity.
484  */
485 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp)
486 {
487         struct rxrpc_call *call;
488         int i, usable;
489
490         _enter("");
491
492         spin_lock(&bundle->channel_lock);
493
494         /* See if there are any usable connections. */
495         usable = 0;
496         for (i = 0; i < ARRAY_SIZE(bundle->conns); i++)
497                 if (rxrpc_may_reuse_conn(bundle->conns[i]))
498                         usable++;
499
500         if (!usable && !list_empty(&bundle->waiting_calls)) {
501                 call = list_first_entry(&bundle->waiting_calls,
502                                         struct rxrpc_call, chan_wait_link);
503                 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags))
504                         bundle->try_upgrade = true;
505         }
506
507         if (!usable)
508                 goto alloc_conn;
509
510         if (!bundle->avail_chans &&
511             !bundle->try_upgrade &&
512             !list_empty(&bundle->waiting_calls) &&
513             usable < ARRAY_SIZE(bundle->conns))
514                 goto alloc_conn;
515
516         spin_unlock(&bundle->channel_lock);
517         _leave("");
518         return;
519
520 alloc_conn:
521         return rxrpc_add_conn_to_bundle(bundle, gfp);
522 }
523
524 /*
525  * Assign a channel to the call at the front of the queue and wake the call up.
526  * We don't increment the callNumber counter until this number has been exposed
527  * to the world.
528  */
529 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
530                                        unsigned int channel)
531 {
532         struct rxrpc_channel *chan = &conn->channels[channel];
533         struct rxrpc_bundle *bundle = conn->bundle;
534         struct rxrpc_call *call = list_entry(bundle->waiting_calls.next,
535                                              struct rxrpc_call, chan_wait_link);
536         u32 call_id = chan->call_counter + 1;
537
538         _enter("C=%x,%u", conn->debug_id, channel);
539
540         trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
541
542         /* Cancel the final ACK on the previous call if it hasn't been sent yet
543          * as the DATA packet will implicitly ACK it.
544          */
545         clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
546         clear_bit(conn->bundle_shift + channel, &bundle->avail_chans);
547
548         rxrpc_see_call(call, rxrpc_call_see_activate_client);
549         list_del_init(&call->chan_wait_link);
550         call->peer      = rxrpc_get_peer(conn->peer, rxrpc_peer_get_activate_call);
551         call->conn      = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call);
552         call->cid       = conn->proto.cid | channel;
553         call->call_id   = call_id;
554         call->security  = conn->security;
555         call->security_ix = conn->security_ix;
556         call->service_id = conn->service_id;
557
558         trace_rxrpc_connect_call(call);
559
560         write_lock_bh(&call->state_lock);
561         call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
562         write_unlock_bh(&call->state_lock);
563
564         /* Paired with the read barrier in rxrpc_connect_call().  This orders
565          * cid and epoch in the connection wrt to call_id without the need to
566          * take the channel_lock.
567          *
568          * We provisionally assign a callNumber at this point, but we don't
569          * confirm it until the call is about to be exposed.
570          *
571          * TODO: Pair with a barrier in the data_ready handler when that looks
572          * at the call ID through a connection channel.
573          */
574         smp_wmb();
575
576         chan->call_id           = call_id;
577         chan->call_debug_id     = call->debug_id;
578         rcu_assign_pointer(chan->call, call);
579         wake_up(&call->waitq);
580 }
581
582 /*
583  * Remove a connection from the idle list if it's on it.
584  */
585 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn)
586 {
587         struct rxrpc_net *rxnet = bundle->local->rxnet;
588         bool drop_ref;
589
590         if (!list_empty(&conn->cache_link)) {
591                 drop_ref = false;
592                 spin_lock(&rxnet->client_conn_cache_lock);
593                 if (!list_empty(&conn->cache_link)) {
594                         list_del_init(&conn->cache_link);
595                         drop_ref = true;
596                 }
597                 spin_unlock(&rxnet->client_conn_cache_lock);
598                 if (drop_ref)
599                         rxrpc_put_connection(conn, rxrpc_conn_put_unidle);
600         }
601 }
602
603 /*
604  * Assign channels and callNumbers to waiting calls with channel_lock
605  * held by caller.
606  */
607 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle)
608 {
609         struct rxrpc_connection *conn;
610         unsigned long avail, mask;
611         unsigned int channel, slot;
612
613         if (bundle->try_upgrade)
614                 mask = 1;
615         else
616                 mask = ULONG_MAX;
617
618         while (!list_empty(&bundle->waiting_calls)) {
619                 avail = bundle->avail_chans & mask;
620                 if (!avail)
621                         break;
622                 channel = __ffs(avail);
623                 clear_bit(channel, &bundle->avail_chans);
624
625                 slot = channel / RXRPC_MAXCALLS;
626                 conn = bundle->conns[slot];
627                 if (!conn)
628                         break;
629
630                 if (bundle->try_upgrade)
631                         set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
632                 rxrpc_unidle_conn(bundle, conn);
633
634                 channel &= (RXRPC_MAXCALLS - 1);
635                 conn->act_chans |= 1 << channel;
636                 rxrpc_activate_one_channel(conn, channel);
637         }
638 }
639
640 /*
641  * Assign channels and callNumbers to waiting calls.
642  */
643 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle)
644 {
645         _enter("B=%x", bundle->debug_id);
646
647         trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans);
648
649         if (!bundle->avail_chans)
650                 return;
651
652         spin_lock(&bundle->channel_lock);
653         rxrpc_activate_channels_locked(bundle);
654         spin_unlock(&bundle->channel_lock);
655         _leave("");
656 }
657
658 /*
659  * Wait for a callNumber and a channel to be granted to a call.
660  */
661 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle,
662                                   struct rxrpc_call *call, gfp_t gfp)
663 {
664         DECLARE_WAITQUEUE(myself, current);
665         int ret = 0;
666
667         _enter("%d", call->debug_id);
668
669         if (!gfpflags_allow_blocking(gfp)) {
670                 rxrpc_maybe_add_conn(bundle, gfp);
671                 rxrpc_activate_channels(bundle);
672                 ret = bundle->alloc_error ?: -EAGAIN;
673                 goto out;
674         }
675
676         add_wait_queue_exclusive(&call->waitq, &myself);
677         for (;;) {
678                 rxrpc_maybe_add_conn(bundle, gfp);
679                 rxrpc_activate_channels(bundle);
680                 ret = bundle->alloc_error;
681                 if (ret < 0)
682                         break;
683
684                 switch (call->interruptibility) {
685                 case RXRPC_INTERRUPTIBLE:
686                 case RXRPC_PREINTERRUPTIBLE:
687                         set_current_state(TASK_INTERRUPTIBLE);
688                         break;
689                 case RXRPC_UNINTERRUPTIBLE:
690                 default:
691                         set_current_state(TASK_UNINTERRUPTIBLE);
692                         break;
693                 }
694                 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN)
695                         break;
696                 if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
697                      call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
698                     signal_pending(current)) {
699                         ret = -ERESTARTSYS;
700                         break;
701                 }
702                 schedule();
703         }
704         remove_wait_queue(&call->waitq, &myself);
705         __set_current_state(TASK_RUNNING);
706
707 out:
708         _leave(" = %d", ret);
709         return ret;
710 }
711
712 /*
713  * find a connection for a call
714  * - called in process context with IRQs enabled
715  */
716 int rxrpc_connect_call(struct rxrpc_sock *rx,
717                        struct rxrpc_call *call,
718                        struct rxrpc_conn_parameters *cp,
719                        struct sockaddr_rxrpc *srx,
720                        gfp_t gfp)
721 {
722         struct rxrpc_bundle *bundle;
723         struct rxrpc_net *rxnet = cp->local->rxnet;
724         int ret = 0;
725
726         _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
727
728         rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
729
730         bundle = rxrpc_prep_call(rx, call, cp, srx, gfp);
731         if (IS_ERR(bundle)) {
732                 ret = PTR_ERR(bundle);
733                 goto out;
734         }
735
736         if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) {
737                 ret = rxrpc_wait_for_channel(bundle, call, gfp);
738                 if (ret < 0)
739                         goto wait_failed;
740         }
741
742 granted_channel:
743         /* Paired with the write barrier in rxrpc_activate_one_channel(). */
744         smp_rmb();
745
746 out_put_bundle:
747         rxrpc_deactivate_bundle(bundle);
748         rxrpc_put_bundle(bundle, rxrpc_bundle_get_client_call);
749 out:
750         _leave(" = %d", ret);
751         return ret;
752
753 wait_failed:
754         spin_lock(&bundle->channel_lock);
755         list_del_init(&call->chan_wait_link);
756         spin_unlock(&bundle->channel_lock);
757
758         if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) {
759                 ret = 0;
760                 goto granted_channel;
761         }
762
763         trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed);
764         rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret);
765         rxrpc_disconnect_client_call(bundle, call);
766         goto out_put_bundle;
767 }
768
769 /*
770  * Note that a call, and thus a connection, is about to be exposed to the
771  * world.
772  */
773 void rxrpc_expose_client_call(struct rxrpc_call *call)
774 {
775         unsigned int channel = call->cid & RXRPC_CHANNELMASK;
776         struct rxrpc_connection *conn = call->conn;
777         struct rxrpc_channel *chan = &conn->channels[channel];
778
779         if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
780                 /* Mark the call ID as being used.  If the callNumber counter
781                  * exceeds ~2 billion, we kill the connection after its
782                  * outstanding calls have finished so that the counter doesn't
783                  * wrap.
784                  */
785                 chan->call_counter++;
786                 if (chan->call_counter >= INT_MAX)
787                         set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
788                 trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
789         }
790 }
791
792 /*
793  * Set the reap timer.
794  */
795 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
796 {
797         if (!rxnet->kill_all_client_conns) {
798                 unsigned long now = jiffies;
799                 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
800
801                 if (rxnet->live)
802                         timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
803         }
804 }
805
806 /*
807  * Disconnect a client call.
808  */
809 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call)
810 {
811         struct rxrpc_connection *conn;
812         struct rxrpc_channel *chan = NULL;
813         struct rxrpc_net *rxnet = bundle->local->rxnet;
814         unsigned int channel;
815         bool may_reuse;
816         u32 cid;
817
818         _enter("c=%x", call->debug_id);
819
820         spin_lock(&bundle->channel_lock);
821         set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
822
823         /* Calls that have never actually been assigned a channel can simply be
824          * discarded.
825          */
826         conn = call->conn;
827         if (!conn) {
828                 _debug("call is waiting");
829                 ASSERTCMP(call->call_id, ==, 0);
830                 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
831                 list_del_init(&call->chan_wait_link);
832                 goto out;
833         }
834
835         cid = call->cid;
836         channel = cid & RXRPC_CHANNELMASK;
837         chan = &conn->channels[channel];
838         trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
839
840         if (rcu_access_pointer(chan->call) != call) {
841                 spin_unlock(&bundle->channel_lock);
842                 BUG();
843         }
844
845         may_reuse = rxrpc_may_reuse_conn(conn);
846
847         /* If a client call was exposed to the world, we save the result for
848          * retransmission.
849          *
850          * We use a barrier here so that the call number and abort code can be
851          * read without needing to take a lock.
852          *
853          * TODO: Make the incoming packet handler check this and handle
854          * terminal retransmission without requiring access to the call.
855          */
856         if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
857                 _debug("exposed %u,%u", call->call_id, call->abort_code);
858                 __rxrpc_disconnect_call(conn, call);
859
860                 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
861                         trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
862                         bundle->try_upgrade = false;
863                         if (may_reuse)
864                                 rxrpc_activate_channels_locked(bundle);
865                 }
866
867         }
868
869         /* See if we can pass the channel directly to another call. */
870         if (may_reuse && !list_empty(&bundle->waiting_calls)) {
871                 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
872                 rxrpc_activate_one_channel(conn, channel);
873                 goto out;
874         }
875
876         /* Schedule the final ACK to be transmitted in a short while so that it
877          * can be skipped if we find a follow-on call.  The first DATA packet
878          * of the follow on call will implicitly ACK this call.
879          */
880         if (call->completion == RXRPC_CALL_SUCCEEDED &&
881             test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
882                 unsigned long final_ack_at = jiffies + 2;
883
884                 WRITE_ONCE(chan->final_ack_at, final_ack_at);
885                 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
886                 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
887                 rxrpc_reduce_conn_timer(conn, final_ack_at);
888         }
889
890         /* Deactivate the channel. */
891         rcu_assign_pointer(chan->call, NULL);
892         set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans);
893         conn->act_chans &= ~(1 << channel);
894
895         /* If no channels remain active, then put the connection on the idle
896          * list for a short while.  Give it a ref to stop it going away if it
897          * becomes unbundled.
898          */
899         if (!conn->act_chans) {
900                 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
901                 conn->idle_timestamp = jiffies;
902
903                 rxrpc_get_connection(conn, rxrpc_conn_get_idle);
904                 spin_lock(&rxnet->client_conn_cache_lock);
905                 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
906                 spin_unlock(&rxnet->client_conn_cache_lock);
907
908                 rxrpc_set_client_reap_timer(rxnet);
909         }
910
911 out:
912         spin_unlock(&bundle->channel_lock);
913         _leave("");
914         return;
915 }
916
917 /*
918  * Remove a connection from a bundle.
919  */
920 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn)
921 {
922         struct rxrpc_bundle *bundle = conn->bundle;
923         unsigned int bindex;
924         bool need_drop = false;
925         int i;
926
927         _enter("C=%x", conn->debug_id);
928
929         if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
930                 rxrpc_process_delayed_final_acks(conn, true);
931
932         spin_lock(&bundle->channel_lock);
933         bindex = conn->bundle_shift / RXRPC_MAXCALLS;
934         if (bundle->conns[bindex] == conn) {
935                 _debug("clear slot %u", bindex);
936                 bundle->conns[bindex] = NULL;
937                 for (i = 0; i < RXRPC_MAXCALLS; i++)
938                         clear_bit(conn->bundle_shift + i, &bundle->avail_chans);
939                 need_drop = true;
940         }
941         spin_unlock(&bundle->channel_lock);
942
943         if (need_drop) {
944                 rxrpc_deactivate_bundle(bundle);
945                 rxrpc_put_connection(conn, rxrpc_conn_put_unbundle);
946         }
947 }
948
949 /*
950  * Drop the active count on a bundle.
951  */
952 static void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle)
953 {
954         struct rxrpc_local *local = bundle->local;
955         bool need_put = false;
956
957         if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) {
958                 if (!bundle->exclusive) {
959                         _debug("erase bundle");
960                         rb_erase(&bundle->local_node, &local->client_bundles);
961                         need_put = true;
962                 }
963
964                 spin_unlock(&local->client_bundles_lock);
965                 if (need_put)
966                         rxrpc_put_bundle(bundle, rxrpc_bundle_put_discard);
967         }
968 }
969
970 /*
971  * Clean up a dead client connection.
972  */
973 static void rxrpc_kill_client_conn(struct rxrpc_connection *conn)
974 {
975         struct rxrpc_local *local = conn->local;
976         struct rxrpc_net *rxnet = local->rxnet;
977
978         _enter("C=%x", conn->debug_id);
979
980         trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
981         atomic_dec(&rxnet->nr_client_conns);
982
983         rxrpc_put_client_connection_id(conn);
984         rxrpc_kill_connection(conn);
985 }
986
987 /*
988  * Clean up a dead client connections.
989  */
990 void rxrpc_put_client_conn(struct rxrpc_connection *conn,
991                            enum rxrpc_conn_trace why)
992 {
993         unsigned int debug_id = conn->debug_id;
994         bool dead;
995         int r;
996
997         dead = __refcount_dec_and_test(&conn->ref, &r);
998         trace_rxrpc_conn(debug_id, r - 1, why);
999         if (dead)
1000                 rxrpc_kill_client_conn(conn);
1001 }
1002
1003 /*
1004  * Discard expired client connections from the idle list.  Each conn in the
1005  * idle list has been exposed and holds an extra ref because of that.
1006  *
1007  * This may be called from conn setup or from a work item so cannot be
1008  * considered non-reentrant.
1009  */
1010 void rxrpc_discard_expired_client_conns(struct work_struct *work)
1011 {
1012         struct rxrpc_connection *conn;
1013         struct rxrpc_net *rxnet =
1014                 container_of(work, struct rxrpc_net, client_conn_reaper);
1015         unsigned long expiry, conn_expires_at, now;
1016         unsigned int nr_conns;
1017
1018         _enter("");
1019
1020         if (list_empty(&rxnet->idle_client_conns)) {
1021                 _leave(" [empty]");
1022                 return;
1023         }
1024
1025         /* Don't double up on the discarding */
1026         if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
1027                 _leave(" [already]");
1028                 return;
1029         }
1030
1031         /* We keep an estimate of what the number of conns ought to be after
1032          * we've discarded some so that we don't overdo the discarding.
1033          */
1034         nr_conns = atomic_read(&rxnet->nr_client_conns);
1035
1036 next:
1037         spin_lock(&rxnet->client_conn_cache_lock);
1038
1039         if (list_empty(&rxnet->idle_client_conns))
1040                 goto out;
1041
1042         conn = list_entry(rxnet->idle_client_conns.next,
1043                           struct rxrpc_connection, cache_link);
1044
1045         if (!rxnet->kill_all_client_conns) {
1046                 /* If the number of connections is over the reap limit, we
1047                  * expedite discard by reducing the expiry timeout.  We must,
1048                  * however, have at least a short grace period to be able to do
1049                  * final-ACK or ABORT retransmission.
1050                  */
1051                 expiry = rxrpc_conn_idle_client_expiry;
1052                 if (nr_conns > rxrpc_reap_client_connections)
1053                         expiry = rxrpc_conn_idle_client_fast_expiry;
1054                 if (conn->local->service_closed)
1055                         expiry = rxrpc_closed_conn_expiry * HZ;
1056
1057                 conn_expires_at = conn->idle_timestamp + expiry;
1058
1059                 now = READ_ONCE(jiffies);
1060                 if (time_after(conn_expires_at, now))
1061                         goto not_yet_expired;
1062         }
1063
1064         trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1065         list_del_init(&conn->cache_link);
1066
1067         spin_unlock(&rxnet->client_conn_cache_lock);
1068
1069         rxrpc_unbundle_conn(conn);
1070         /* Drop the ->cache_link ref */
1071         rxrpc_put_connection(conn, rxrpc_conn_put_discard_idle);
1072
1073         nr_conns--;
1074         goto next;
1075
1076 not_yet_expired:
1077         /* The connection at the front of the queue hasn't yet expired, so
1078          * schedule the work item for that point if we discarded something.
1079          *
1080          * We don't worry if the work item is already scheduled - it can look
1081          * after rescheduling itself at a later time.  We could cancel it, but
1082          * then things get messier.
1083          */
1084         _debug("not yet");
1085         if (!rxnet->kill_all_client_conns)
1086                 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at);
1087
1088 out:
1089         spin_unlock(&rxnet->client_conn_cache_lock);
1090         spin_unlock(&rxnet->client_conn_discard_lock);
1091         _leave("");
1092 }
1093
1094 /*
1095  * Preemptively destroy all the client connection records rather than waiting
1096  * for them to time out
1097  */
1098 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
1099 {
1100         _enter("");
1101
1102         spin_lock(&rxnet->client_conn_cache_lock);
1103         rxnet->kill_all_client_conns = true;
1104         spin_unlock(&rxnet->client_conn_cache_lock);
1105
1106         del_timer_sync(&rxnet->client_conn_reap_timer);
1107
1108         if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
1109                 _debug("destroy: queue failed");
1110
1111         _leave("");
1112 }
1113
1114 /*
1115  * Clean up the client connections on a local endpoint.
1116  */
1117 void rxrpc_clean_up_local_conns(struct rxrpc_local *local)
1118 {
1119         struct rxrpc_connection *conn, *tmp;
1120         struct rxrpc_net *rxnet = local->rxnet;
1121         LIST_HEAD(graveyard);
1122
1123         _enter("");
1124
1125         spin_lock(&rxnet->client_conn_cache_lock);
1126
1127         list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns,
1128                                  cache_link) {
1129                 if (conn->local == local) {
1130                         trace_rxrpc_client(conn, -1, rxrpc_client_discard);
1131                         list_move(&conn->cache_link, &graveyard);
1132                 }
1133         }
1134
1135         spin_unlock(&rxnet->client_conn_cache_lock);
1136
1137         while (!list_empty(&graveyard)) {
1138                 conn = list_entry(graveyard.next,
1139                                   struct rxrpc_connection, cache_link);
1140                 list_del_init(&conn->cache_link);
1141                 rxrpc_unbundle_conn(conn);
1142                 rxrpc_put_connection(conn, rxrpc_conn_put_local_dead);
1143         }
1144
1145         _leave(" [culled]");
1146 }