Commit | Line | Data |
---|---|---|
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 | ||
86 | static unsigned int rxrpc_nr_client_conns; | |
87 | static unsigned int rxrpc_nr_active_client_conns; | |
88 | static __read_mostly bool rxrpc_kill_all_client_conns; | |
89 | ||
90 | static DEFINE_SPINLOCK(rxrpc_client_conn_cache_lock); | |
91 | static DEFINE_SPINLOCK(rxrpc_client_conn_discard_mutex); | |
92 | static LIST_HEAD(rxrpc_waiting_client_conns); | |
93 | static LIST_HEAD(rxrpc_active_client_conns); | |
94 | static LIST_HEAD(rxrpc_idle_client_conns); | |
95 | ||
4a3388c8 DH |
96 | /* |
97 | * We use machine-unique IDs for our client connections. | |
98 | */ | |
99 | DEFINE_IDR(rxrpc_client_conn_ids); | |
100 | static DEFINE_SPINLOCK(rxrpc_conn_id_lock); | |
101 | ||
45025bce DH |
102 | static void rxrpc_cull_active_client_conns(void); |
103 | static void rxrpc_discard_expired_client_conns(struct work_struct *); | |
104 | ||
105 | static 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 | |
111 | * epoch is changed if this wraps. | |
4a3388c8 | 112 | */ |
c6d2b8d7 DH |
113 | static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn, |
114 | gfp_t gfp) | |
4a3388c8 DH |
115 | { |
116 | u32 epoch; | |
117 | int id; | |
118 | ||
119 | _enter(""); | |
120 | ||
121 | idr_preload(gfp); | |
4a3388c8 DH |
122 | spin_lock(&rxrpc_conn_id_lock); |
123 | ||
124 | epoch = rxrpc_epoch; | |
125 | ||
126 | /* We could use idr_alloc_cyclic() here, but we really need to know | |
127 | * when the thing wraps so that we can advance the epoch. | |
128 | */ | |
129 | if (rxrpc_client_conn_ids.cur == 0) | |
130 | rxrpc_client_conn_ids.cur = 1; | |
131 | id = idr_alloc(&rxrpc_client_conn_ids, conn, | |
132 | rxrpc_client_conn_ids.cur, 0x40000000, GFP_NOWAIT); | |
133 | if (id < 0) { | |
134 | if (id != -ENOSPC) | |
135 | goto error; | |
136 | id = idr_alloc(&rxrpc_client_conn_ids, conn, | |
137 | 1, 0x40000000, GFP_NOWAIT); | |
138 | if (id < 0) | |
139 | goto error; | |
140 | epoch++; | |
141 | rxrpc_epoch = epoch; | |
142 | } | |
143 | rxrpc_client_conn_ids.cur = id + 1; | |
144 | ||
145 | spin_unlock(&rxrpc_conn_id_lock); | |
4a3388c8 DH |
146 | idr_preload_end(); |
147 | ||
148 | conn->proto.epoch = epoch; | |
149 | conn->proto.cid = id << RXRPC_CIDSHIFT; | |
150 | set_bit(RXRPC_CONN_HAS_IDR, &conn->flags); | |
151 | _leave(" [CID %x:%x]", epoch, conn->proto.cid); | |
152 | return 0; | |
153 | ||
154 | error: | |
155 | spin_unlock(&rxrpc_conn_id_lock); | |
4a3388c8 DH |
156 | idr_preload_end(); |
157 | _leave(" = %d", id); | |
158 | return id; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Release a connection ID for a client connection from the global pool. | |
163 | */ | |
001c1122 | 164 | static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn) |
4a3388c8 DH |
165 | { |
166 | if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) { | |
167 | spin_lock(&rxrpc_conn_id_lock); | |
168 | idr_remove(&rxrpc_client_conn_ids, | |
169 | conn->proto.cid >> RXRPC_CIDSHIFT); | |
170 | spin_unlock(&rxrpc_conn_id_lock); | |
171 | } | |
172 | } | |
eb9b9d22 DH |
173 | |
174 | /* | |
175 | * Destroy the client connection ID tree. | |
176 | */ | |
177 | void rxrpc_destroy_client_conn_ids(void) | |
178 | { | |
179 | struct rxrpc_connection *conn; | |
180 | int id; | |
181 | ||
182 | if (!idr_is_empty(&rxrpc_client_conn_ids)) { | |
183 | idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) { | |
184 | pr_err("AF_RXRPC: Leaked client conn %p {%d}\n", | |
185 | conn, atomic_read(&conn->usage)); | |
186 | } | |
187 | BUG(); | |
188 | } | |
189 | ||
190 | idr_destroy(&rxrpc_client_conn_ids); | |
191 | } | |
c6d2b8d7 DH |
192 | |
193 | /* | |
45025bce | 194 | * Allocate a client connection. |
c6d2b8d7 DH |
195 | */ |
196 | static struct rxrpc_connection * | |
197 | rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp) | |
198 | { | |
199 | struct rxrpc_connection *conn; | |
200 | int ret; | |
201 | ||
202 | _enter(""); | |
203 | ||
204 | conn = rxrpc_alloc_connection(gfp); | |
205 | if (!conn) { | |
206 | _leave(" = -ENOMEM"); | |
207 | return ERR_PTR(-ENOMEM); | |
208 | } | |
209 | ||
45025bce DH |
210 | atomic_set(&conn->usage, 1); |
211 | if (conn->params.exclusive) | |
212 | __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); | |
213 | ||
c6d2b8d7 | 214 | conn->params = *cp; |
c6d2b8d7 DH |
215 | conn->out_clientflag = RXRPC_CLIENT_INITIATED; |
216 | conn->state = RXRPC_CONN_CLIENT; | |
217 | ||
c6d2b8d7 DH |
218 | ret = rxrpc_get_client_connection_id(conn, gfp); |
219 | if (ret < 0) | |
220 | goto error_0; | |
221 | ||
222 | ret = rxrpc_init_client_conn_security(conn); | |
223 | if (ret < 0) | |
224 | goto error_1; | |
225 | ||
226 | ret = conn->security->prime_packet_security(conn); | |
227 | if (ret < 0) | |
228 | goto error_2; | |
229 | ||
230 | write_lock(&rxrpc_connection_lock); | |
4d028b2c | 231 | list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list); |
c6d2b8d7 DH |
232 | write_unlock(&rxrpc_connection_lock); |
233 | ||
234 | /* We steal the caller's peer ref. */ | |
235 | cp->peer = NULL; | |
236 | rxrpc_get_local(conn->params.local); | |
237 | key_get(conn->params.key); | |
238 | ||
239 | _leave(" = %p", conn); | |
240 | return conn; | |
241 | ||
242 | error_2: | |
243 | conn->security->clear(conn); | |
244 | error_1: | |
245 | rxrpc_put_client_connection_id(conn); | |
246 | error_0: | |
247 | kfree(conn); | |
248 | _leave(" = %d", ret); | |
249 | return ERR_PTR(ret); | |
250 | } | |
251 | ||
252 | /* | |
45025bce | 253 | * Determine if a connection may be reused. |
c6d2b8d7 | 254 | */ |
45025bce DH |
255 | static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn) |
256 | { | |
257 | int id_cursor, id, distance, limit; | |
258 | ||
259 | if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags)) | |
260 | goto dont_reuse; | |
261 | ||
262 | if (conn->proto.epoch != rxrpc_epoch) | |
263 | goto mark_dont_reuse; | |
264 | ||
265 | /* The IDR tree gets very expensive on memory if the connection IDs are | |
266 | * widely scattered throughout the number space, so we shall want to | |
267 | * kill off connections that, say, have an ID more than about four | |
268 | * times the maximum number of client conns away from the current | |
269 | * allocation point to try and keep the IDs concentrated. | |
270 | */ | |
271 | id_cursor = READ_ONCE(rxrpc_client_conn_ids.cur); | |
272 | id = conn->proto.cid >> RXRPC_CIDSHIFT; | |
273 | distance = id - id_cursor; | |
274 | if (distance < 0) | |
275 | distance = -distance; | |
276 | limit = round_up(rxrpc_max_client_connections, IDR_SIZE) * 4; | |
277 | if (distance > limit) | |
278 | goto mark_dont_reuse; | |
279 | ||
280 | return true; | |
281 | ||
282 | mark_dont_reuse: | |
283 | set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); | |
284 | dont_reuse: | |
285 | return false; | |
286 | } | |
287 | ||
288 | /* | |
289 | * Create or find a client connection to use for a call. | |
290 | * | |
291 | * If we return with a connection, the call will be on its waiting list. It's | |
292 | * left to the caller to assign a channel and wake up the call. | |
293 | */ | |
294 | static int rxrpc_get_client_conn(struct rxrpc_call *call, | |
295 | struct rxrpc_conn_parameters *cp, | |
296 | struct sockaddr_rxrpc *srx, | |
297 | gfp_t gfp) | |
c6d2b8d7 DH |
298 | { |
299 | struct rxrpc_connection *conn, *candidate = NULL; | |
300 | struct rxrpc_local *local = cp->local; | |
301 | struct rb_node *p, **pp, *parent; | |
302 | long diff; | |
45025bce | 303 | int ret = -ENOMEM; |
c6d2b8d7 DH |
304 | |
305 | _enter("{%d,%lx},", call->debug_id, call->user_call_ID); | |
306 | ||
307 | cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp); | |
308 | if (!cp->peer) | |
45025bce | 309 | goto error; |
c6d2b8d7 | 310 | |
45025bce DH |
311 | /* If the connection is not meant to be exclusive, search the available |
312 | * connections to see if the connection we want to use already exists. | |
313 | */ | |
c6d2b8d7 | 314 | if (!cp->exclusive) { |
c6d2b8d7 DH |
315 | _debug("search 1"); |
316 | spin_lock(&local->client_conns_lock); | |
317 | p = local->client_conns.rb_node; | |
318 | while (p) { | |
319 | conn = rb_entry(p, struct rxrpc_connection, client_node); | |
320 | ||
321 | #define cmp(X) ((long)conn->params.X - (long)cp->X) | |
322 | diff = (cmp(peer) ?: | |
323 | cmp(key) ?: | |
324 | cmp(security_level)); | |
45025bce DH |
325 | #undef cmp |
326 | if (diff < 0) { | |
c6d2b8d7 | 327 | p = p->rb_left; |
45025bce | 328 | } else if (diff > 0) { |
c6d2b8d7 | 329 | p = p->rb_right; |
45025bce DH |
330 | } else { |
331 | if (rxrpc_may_reuse_conn(conn) && | |
332 | rxrpc_get_connection_maybe(conn)) | |
333 | goto found_extant_conn; | |
334 | /* The connection needs replacing. It's better | |
335 | * to effect that when we have something to | |
336 | * replace it with so that we don't have to | |
337 | * rebalance the tree twice. | |
338 | */ | |
339 | break; | |
340 | } | |
c6d2b8d7 DH |
341 | } |
342 | spin_unlock(&local->client_conns_lock); | |
343 | } | |
344 | ||
45025bce DH |
345 | /* There wasn't a connection yet or we need an exclusive connection. |
346 | * We need to create a candidate and then potentially redo the search | |
347 | * in case we're racing with another thread also trying to connect on a | |
348 | * shareable connection. | |
349 | */ | |
350 | _debug("new conn"); | |
c6d2b8d7 | 351 | candidate = rxrpc_alloc_client_connection(cp, gfp); |
45025bce DH |
352 | if (IS_ERR(candidate)) { |
353 | ret = PTR_ERR(candidate); | |
354 | goto error_peer; | |
c6d2b8d7 DH |
355 | } |
356 | ||
45025bce DH |
357 | /* Add the call to the new connection's waiting list in case we're |
358 | * going to have to wait for the connection to come live. It's our | |
359 | * connection, so we want first dibs on the channel slots. We would | |
360 | * normally have to take channel_lock but we do this before anyone else | |
361 | * can see the connection. | |
362 | */ | |
363 | list_add_tail(&call->chan_wait_link, &candidate->waiting_calls); | |
364 | ||
c6d2b8d7 | 365 | if (cp->exclusive) { |
45025bce DH |
366 | call->conn = candidate; |
367 | _leave(" = 0 [exclusive %d]", candidate->debug_id); | |
368 | return 0; | |
c6d2b8d7 DH |
369 | } |
370 | ||
45025bce DH |
371 | /* Publish the new connection for userspace to find. We need to redo |
372 | * the search before doing this lest we race with someone else adding a | |
373 | * conflicting instance. | |
c6d2b8d7 DH |
374 | */ |
375 | _debug("search 2"); | |
376 | spin_lock(&local->client_conns_lock); | |
377 | ||
378 | pp = &local->client_conns.rb_node; | |
379 | parent = NULL; | |
380 | while (*pp) { | |
381 | parent = *pp; | |
382 | conn = rb_entry(parent, struct rxrpc_connection, client_node); | |
383 | ||
45025bce | 384 | #define cmp(X) ((long)conn->params.X - (long)candidate->params.X) |
c6d2b8d7 DH |
385 | diff = (cmp(peer) ?: |
386 | cmp(key) ?: | |
387 | cmp(security_level)); | |
45025bce DH |
388 | #undef cmp |
389 | if (diff < 0) { | |
c6d2b8d7 | 390 | pp = &(*pp)->rb_left; |
45025bce | 391 | } else if (diff > 0) { |
c6d2b8d7 | 392 | pp = &(*pp)->rb_right; |
45025bce DH |
393 | } else { |
394 | if (rxrpc_may_reuse_conn(conn) && | |
395 | rxrpc_get_connection_maybe(conn)) | |
396 | goto found_extant_conn; | |
397 | /* The old connection is from an outdated epoch. */ | |
398 | _debug("replace conn"); | |
399 | clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags); | |
400 | rb_replace_node(&conn->client_node, | |
401 | &candidate->client_node, | |
402 | &local->client_conns); | |
403 | goto candidate_published; | |
404 | } | |
c6d2b8d7 DH |
405 | } |
406 | ||
c6d2b8d7 | 407 | _debug("new conn"); |
001c1122 DH |
408 | rb_link_node(&candidate->client_node, parent, pp); |
409 | rb_insert_color(&candidate->client_node, &local->client_conns); | |
c6d2b8d7 | 410 | |
45025bce DH |
411 | candidate_published: |
412 | set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags); | |
413 | call->conn = candidate; | |
c6d2b8d7 | 414 | spin_unlock(&local->client_conns_lock); |
45025bce DH |
415 | _leave(" = 0 [new %d]", candidate->debug_id); |
416 | return 0; | |
c6d2b8d7 | 417 | |
45025bce DH |
418 | /* We come here if we found a suitable connection already in existence. |
419 | * Discard any candidate we may have allocated, and try to get a | |
420 | * channel on this one. | |
421 | */ | |
422 | found_extant_conn: | |
423 | _debug("found conn"); | |
424 | spin_unlock(&local->client_conns_lock); | |
c6d2b8d7 | 425 | |
45025bce DH |
426 | rxrpc_put_connection(candidate); |
427 | candidate = NULL; | |
c6d2b8d7 | 428 | |
45025bce DH |
429 | spin_lock(&conn->channel_lock); |
430 | call->conn = conn; | |
431 | list_add(&call->chan_wait_link, &conn->waiting_calls); | |
c6d2b8d7 | 432 | spin_unlock(&conn->channel_lock); |
45025bce DH |
433 | _leave(" = 0 [extant %d]", conn->debug_id); |
434 | return 0; | |
435 | ||
436 | error_peer: | |
c6d2b8d7 DH |
437 | rxrpc_put_peer(cp->peer); |
438 | cp->peer = NULL; | |
45025bce DH |
439 | error: |
440 | _leave(" = %d", ret); | |
441 | return ret; | |
442 | } | |
c6d2b8d7 | 443 | |
45025bce DH |
444 | /* |
445 | * Activate a connection. | |
446 | */ | |
447 | static void rxrpc_activate_conn(struct rxrpc_connection *conn) | |
448 | { | |
449 | conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE; | |
450 | rxrpc_nr_active_client_conns++; | |
451 | list_move_tail(&conn->cache_link, &rxrpc_active_client_conns); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Attempt to animate a connection for a new call. | |
456 | * | |
457 | * If it's not exclusive, the connection is in the endpoint tree, and we're in | |
458 | * the conn's list of those waiting to grab a channel. There is, however, a | |
459 | * limit on the number of live connections allowed at any one time, so we may | |
460 | * have to wait for capacity to become available. | |
461 | * | |
462 | * Note that a connection on the waiting queue might *also* have active | |
463 | * channels if it has been culled to make space and then re-requested by a new | |
464 | * call. | |
465 | */ | |
466 | static void rxrpc_animate_client_conn(struct rxrpc_connection *conn) | |
467 | { | |
468 | unsigned int nr_conns; | |
469 | ||
470 | _enter("%d,%d", conn->debug_id, conn->cache_state); | |
471 | ||
472 | if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE) | |
473 | goto out; | |
474 | ||
475 | spin_lock(&rxrpc_client_conn_cache_lock); | |
476 | ||
477 | nr_conns = rxrpc_nr_client_conns; | |
478 | if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) | |
479 | rxrpc_nr_client_conns = nr_conns + 1; | |
480 | ||
481 | switch (conn->cache_state) { | |
482 | case RXRPC_CONN_CLIENT_ACTIVE: | |
483 | case RXRPC_CONN_CLIENT_WAITING: | |
484 | break; | |
485 | ||
486 | case RXRPC_CONN_CLIENT_INACTIVE: | |
487 | case RXRPC_CONN_CLIENT_CULLED: | |
488 | case RXRPC_CONN_CLIENT_IDLE: | |
489 | if (nr_conns >= rxrpc_max_client_connections) | |
490 | goto wait_for_capacity; | |
491 | goto activate_conn; | |
492 | ||
493 | default: | |
494 | BUG(); | |
001c1122 DH |
495 | } |
496 | ||
45025bce DH |
497 | out_unlock: |
498 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
499 | out: | |
500 | _leave(" [%d]", conn->cache_state); | |
501 | return; | |
c6d2b8d7 | 502 | |
45025bce DH |
503 | activate_conn: |
504 | _debug("activate"); | |
505 | rxrpc_activate_conn(conn); | |
506 | goto out_unlock; | |
507 | ||
508 | wait_for_capacity: | |
509 | _debug("wait"); | |
510 | conn->cache_state = RXRPC_CONN_CLIENT_WAITING; | |
511 | list_move_tail(&conn->cache_link, &rxrpc_waiting_client_conns); | |
512 | goto out_unlock; | |
513 | } | |
514 | ||
515 | /* | |
516 | * Deactivate a channel. | |
517 | */ | |
518 | static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn, | |
519 | unsigned int channel) | |
520 | { | |
521 | struct rxrpc_channel *chan = &conn->channels[channel]; | |
522 | ||
523 | rcu_assign_pointer(chan->call, NULL); | |
524 | conn->active_chans &= ~(1 << channel); | |
525 | } | |
526 | ||
527 | /* | |
528 | * Assign a channel to the call at the front of the queue and wake the call up. | |
529 | * We don't increment the callNumber counter until this number has been exposed | |
530 | * to the world. | |
531 | */ | |
532 | static void rxrpc_activate_one_channel(struct rxrpc_connection *conn, | |
533 | unsigned int channel) | |
534 | { | |
535 | struct rxrpc_channel *chan = &conn->channels[channel]; | |
536 | struct rxrpc_call *call = list_entry(conn->waiting_calls.next, | |
537 | struct rxrpc_call, chan_wait_link); | |
538 | u32 call_id = chan->call_counter + 1; | |
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 | ||
570 | /* | |
571 | * Assign channels and callNumbers to waiting calls. | |
572 | */ | |
573 | static void rxrpc_activate_channels(struct rxrpc_connection *conn) | |
574 | { | |
575 | unsigned char mask; | |
576 | ||
577 | _enter("%d", conn->debug_id); | |
578 | ||
579 | if (conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE || | |
580 | conn->active_chans == RXRPC_ACTIVE_CHANS_MASK) | |
581 | return; | |
582 | ||
583 | spin_lock(&conn->channel_lock); | |
584 | ||
585 | while (!list_empty(&conn->waiting_calls) && | |
586 | (mask = ~conn->active_chans, | |
587 | mask &= RXRPC_ACTIVE_CHANS_MASK, | |
588 | mask != 0)) | |
589 | rxrpc_activate_one_channel(conn, __ffs(mask)); | |
590 | ||
591 | spin_unlock(&conn->channel_lock); | |
592 | _leave(""); | |
593 | } | |
594 | ||
595 | /* | |
596 | * Wait for a callNumber and a channel to be granted to a call. | |
597 | */ | |
598 | static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp) | |
599 | { | |
600 | int ret = 0; | |
601 | ||
602 | _enter("%d", call->debug_id); | |
603 | ||
604 | if (!call->call_id) { | |
605 | DECLARE_WAITQUEUE(myself, current); | |
c6d2b8d7 | 606 | |
c6d2b8d7 | 607 | if (!gfpflags_allow_blocking(gfp)) { |
45025bce DH |
608 | ret = -EAGAIN; |
609 | goto out; | |
c6d2b8d7 DH |
610 | } |
611 | ||
45025bce | 612 | add_wait_queue_exclusive(&call->waitq, &myself); |
c6d2b8d7 DH |
613 | for (;;) { |
614 | set_current_state(TASK_INTERRUPTIBLE); | |
45025bce DH |
615 | if (call->call_id) |
616 | break; | |
617 | if (signal_pending(current)) { | |
618 | ret = -ERESTARTSYS; | |
c6d2b8d7 | 619 | break; |
45025bce | 620 | } |
c6d2b8d7 DH |
621 | schedule(); |
622 | } | |
45025bce | 623 | remove_wait_queue(&call->waitq, &myself); |
c6d2b8d7 DH |
624 | __set_current_state(TASK_RUNNING); |
625 | } | |
626 | ||
45025bce DH |
627 | /* Paired with the write barrier in rxrpc_activate_one_channel(). */ |
628 | smp_rmb(); | |
629 | ||
630 | out: | |
631 | _leave(" = %d", ret); | |
632 | return ret; | |
633 | } | |
634 | ||
635 | /* | |
636 | * find a connection for a call | |
637 | * - called in process context with IRQs enabled | |
638 | */ | |
639 | int rxrpc_connect_call(struct rxrpc_call *call, | |
640 | struct rxrpc_conn_parameters *cp, | |
641 | struct sockaddr_rxrpc *srx, | |
642 | gfp_t gfp) | |
643 | { | |
644 | int ret; | |
645 | ||
646 | _enter("{%d,%lx},", call->debug_id, call->user_call_ID); | |
647 | ||
648 | rxrpc_discard_expired_client_conns(NULL); | |
649 | rxrpc_cull_active_client_conns(); | |
650 | ||
651 | ret = rxrpc_get_client_conn(call, cp, srx, gfp); | |
652 | if (ret < 0) | |
653 | return ret; | |
654 | ||
655 | rxrpc_animate_client_conn(call->conn); | |
656 | rxrpc_activate_channels(call->conn); | |
657 | ||
658 | ret = rxrpc_wait_for_channel(call, gfp); | |
659 | if (ret < 0) | |
660 | rxrpc_disconnect_client_call(call); | |
661 | ||
662 | _leave(" = %d", ret); | |
663 | return ret; | |
664 | } | |
665 | ||
666 | /* | |
667 | * Note that a connection is about to be exposed to the world. Once it is | |
668 | * exposed, we maintain an extra ref on it that stops it from being summarily | |
669 | * discarded before it's (a) had a chance to deal with retransmission and (b) | |
670 | * had a chance at re-use (the per-connection security negotiation is | |
671 | * expensive). | |
672 | */ | |
673 | static void rxrpc_expose_client_conn(struct rxrpc_connection *conn) | |
674 | { | |
675 | if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) | |
676 | rxrpc_get_connection(conn); | |
677 | } | |
678 | ||
679 | /* | |
680 | * Note that a call, and thus a connection, is about to be exposed to the | |
681 | * world. | |
682 | */ | |
683 | void rxrpc_expose_client_call(struct rxrpc_call *call) | |
684 | { | |
685 | struct rxrpc_connection *conn = call->conn; | |
686 | struct rxrpc_channel *chan = | |
687 | &conn->channels[call->cid & RXRPC_CHANNELMASK]; | |
688 | ||
689 | if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) { | |
690 | /* Mark the call ID as being used. If the callNumber counter | |
691 | * exceeds ~2 billion, we kill the connection after its | |
692 | * outstanding calls have finished so that the counter doesn't | |
693 | * wrap. | |
694 | */ | |
695 | chan->call_counter++; | |
696 | if (chan->call_counter >= INT_MAX) | |
697 | set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); | |
698 | rxrpc_expose_client_conn(conn); | |
699 | } | |
700 | } | |
701 | ||
702 | /* | |
703 | * Disconnect a client call. | |
704 | */ | |
705 | void rxrpc_disconnect_client_call(struct rxrpc_call *call) | |
706 | { | |
707 | unsigned int channel = call->cid & RXRPC_CHANNELMASK; | |
708 | struct rxrpc_connection *conn = call->conn; | |
709 | struct rxrpc_channel *chan = &conn->channels[channel]; | |
710 | ||
711 | call->conn = NULL; | |
712 | ||
c6d2b8d7 DH |
713 | spin_lock(&conn->channel_lock); |
714 | ||
45025bce DH |
715 | /* Calls that have never actually been assigned a channel can simply be |
716 | * discarded. If the conn didn't get used either, it will follow | |
717 | * immediately unless someone else grabs it in the meantime. | |
718 | */ | |
719 | if (!list_empty(&call->chan_wait_link)) { | |
720 | _debug("call is waiting"); | |
721 | ASSERTCMP(call->call_id, ==, 0); | |
722 | ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags)); | |
723 | list_del_init(&call->chan_wait_link); | |
724 | ||
725 | /* We must deactivate or idle the connection if it's now | |
726 | * waiting for nothing. | |
727 | */ | |
728 | spin_lock(&rxrpc_client_conn_cache_lock); | |
729 | if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING && | |
730 | list_empty(&conn->waiting_calls) && | |
731 | !conn->active_chans) | |
732 | goto idle_connection; | |
733 | goto out; | |
734 | } | |
735 | ||
736 | ASSERTCMP(rcu_access_pointer(chan->call), ==, call); | |
737 | ASSERTCMP(atomic_read(&conn->usage), >=, 2); | |
738 | ||
739 | /* If a client call was exposed to the world, we save the result for | |
740 | * retransmission. | |
741 | * | |
742 | * We use a barrier here so that the call number and abort code can be | |
743 | * read without needing to take a lock. | |
744 | * | |
745 | * TODO: Make the incoming packet handler check this and handle | |
746 | * terminal retransmission without requiring access to the call. | |
747 | */ | |
748 | if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { | |
f5c17aae | 749 | _debug("exposed %u,%u", call->call_id, call->abort_code); |
45025bce DH |
750 | __rxrpc_disconnect_call(conn, call); |
751 | } | |
752 | ||
753 | /* See if we can pass the channel directly to another call. */ | |
754 | if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE && | |
755 | !list_empty(&conn->waiting_calls)) { | |
756 | _debug("pass chan"); | |
757 | rxrpc_activate_one_channel(conn, channel); | |
758 | goto out_2; | |
759 | } | |
760 | ||
761 | /* Things are more complex and we need the cache lock. We might be | |
762 | * able to simply idle the conn or it might now be lurking on the wait | |
763 | * list. It might even get moved back to the active list whilst we're | |
764 | * waiting for the lock. | |
765 | */ | |
766 | spin_lock(&rxrpc_client_conn_cache_lock); | |
767 | ||
768 | switch (conn->cache_state) { | |
769 | case RXRPC_CONN_CLIENT_ACTIVE: | |
770 | if (list_empty(&conn->waiting_calls)) { | |
771 | rxrpc_deactivate_one_channel(conn, channel); | |
772 | if (!conn->active_chans) { | |
773 | rxrpc_nr_active_client_conns--; | |
774 | goto idle_connection; | |
775 | } | |
776 | goto out; | |
777 | } | |
778 | ||
779 | _debug("pass chan 2"); | |
780 | rxrpc_activate_one_channel(conn, channel); | |
781 | goto out; | |
782 | ||
783 | case RXRPC_CONN_CLIENT_CULLED: | |
784 | rxrpc_deactivate_one_channel(conn, channel); | |
785 | ASSERT(list_empty(&conn->waiting_calls)); | |
786 | if (!conn->active_chans) | |
787 | goto idle_connection; | |
788 | goto out; | |
789 | ||
790 | case RXRPC_CONN_CLIENT_WAITING: | |
791 | rxrpc_deactivate_one_channel(conn, channel); | |
792 | goto out; | |
793 | ||
794 | default: | |
795 | BUG(); | |
796 | } | |
c6d2b8d7 | 797 | |
45025bce DH |
798 | out: |
799 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
800 | out_2: | |
801 | spin_unlock(&conn->channel_lock); | |
c6d2b8d7 | 802 | rxrpc_put_connection(conn); |
45025bce DH |
803 | _leave(""); |
804 | return; | |
805 | ||
806 | idle_connection: | |
807 | /* As no channels remain active, the connection gets deactivated | |
808 | * immediately or moved to the idle list for a short while. | |
809 | */ | |
810 | if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) { | |
811 | _debug("make idle"); | |
812 | conn->idle_timestamp = jiffies; | |
813 | conn->cache_state = RXRPC_CONN_CLIENT_IDLE; | |
814 | list_move_tail(&conn->cache_link, &rxrpc_idle_client_conns); | |
815 | if (rxrpc_idle_client_conns.next == &conn->cache_link && | |
816 | !rxrpc_kill_all_client_conns) | |
817 | queue_delayed_work(rxrpc_workqueue, | |
818 | &rxrpc_client_conn_reap, | |
819 | rxrpc_conn_idle_client_expiry); | |
820 | } else { | |
821 | _debug("make inactive"); | |
822 | conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE; | |
823 | list_del_init(&conn->cache_link); | |
824 | } | |
825 | goto out; | |
c6d2b8d7 | 826 | } |
001c1122 DH |
827 | |
828 | /* | |
45025bce | 829 | * Clean up a dead client connection. |
001c1122 | 830 | */ |
45025bce DH |
831 | static struct rxrpc_connection * |
832 | rxrpc_put_one_client_conn(struct rxrpc_connection *conn) | |
001c1122 | 833 | { |
45025bce | 834 | struct rxrpc_connection *next; |
001c1122 | 835 | struct rxrpc_local *local = conn->params.local; |
45025bce | 836 | unsigned int nr_conns; |
001c1122 | 837 | |
45025bce DH |
838 | if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) { |
839 | spin_lock(&local->client_conns_lock); | |
840 | if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, | |
841 | &conn->flags)) | |
842 | rb_erase(&conn->client_node, &local->client_conns); | |
843 | spin_unlock(&local->client_conns_lock); | |
844 | } | |
001c1122 DH |
845 | |
846 | rxrpc_put_client_connection_id(conn); | |
45025bce DH |
847 | |
848 | ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE); | |
849 | ||
850 | if (!test_bit(RXRPC_CONN_COUNTED, &conn->flags)) | |
851 | return NULL; | |
852 | ||
853 | spin_lock(&rxrpc_client_conn_cache_lock); | |
854 | nr_conns = --rxrpc_nr_client_conns; | |
855 | ||
856 | next = NULL; | |
857 | if (nr_conns < rxrpc_max_client_connections && | |
858 | !list_empty(&rxrpc_waiting_client_conns)) { | |
859 | next = list_entry(rxrpc_waiting_client_conns.next, | |
860 | struct rxrpc_connection, cache_link); | |
861 | rxrpc_get_connection(next); | |
862 | rxrpc_activate_conn(next); | |
863 | } | |
864 | ||
865 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
866 | rxrpc_kill_connection(conn); | |
867 | ||
868 | if (next) | |
869 | rxrpc_activate_channels(next); | |
870 | ||
871 | /* We need to get rid of the temporary ref we took upon next, but we | |
872 | * can't call rxrpc_put_connection() recursively. | |
873 | */ | |
874 | return next; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Clean up a dead client connections. | |
879 | */ | |
880 | void rxrpc_put_client_conn(struct rxrpc_connection *conn) | |
881 | { | |
882 | struct rxrpc_connection *next; | |
883 | ||
884 | do { | |
885 | _enter("%p{u=%d,d=%d}", | |
886 | conn, atomic_read(&conn->usage), conn->debug_id); | |
887 | ||
888 | next = rxrpc_put_one_client_conn(conn); | |
889 | ||
890 | if (!next) | |
891 | break; | |
892 | conn = next; | |
893 | } while (atomic_dec_and_test(&conn->usage)); | |
894 | ||
895 | _leave(""); | |
896 | } | |
897 | ||
898 | /* | |
899 | * Kill the longest-active client connections to make room for new ones. | |
900 | */ | |
901 | static void rxrpc_cull_active_client_conns(void) | |
902 | { | |
903 | struct rxrpc_connection *conn; | |
904 | unsigned int nr_conns = rxrpc_nr_client_conns; | |
905 | unsigned int nr_active, limit; | |
906 | ||
907 | _enter(""); | |
908 | ||
909 | ASSERTCMP(nr_conns, >=, 0); | |
910 | if (nr_conns < rxrpc_max_client_connections) { | |
911 | _leave(" [ok]"); | |
912 | return; | |
913 | } | |
914 | limit = rxrpc_reap_client_connections; | |
915 | ||
916 | spin_lock(&rxrpc_client_conn_cache_lock); | |
917 | nr_active = rxrpc_nr_active_client_conns; | |
918 | ||
919 | while (nr_active > limit) { | |
920 | ASSERT(!list_empty(&rxrpc_active_client_conns)); | |
921 | conn = list_entry(rxrpc_active_client_conns.next, | |
922 | struct rxrpc_connection, cache_link); | |
923 | ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_ACTIVE); | |
924 | ||
925 | if (list_empty(&conn->waiting_calls)) { | |
926 | conn->cache_state = RXRPC_CONN_CLIENT_CULLED; | |
927 | list_del_init(&conn->cache_link); | |
928 | } else { | |
929 | conn->cache_state = RXRPC_CONN_CLIENT_WAITING; | |
930 | list_move_tail(&conn->cache_link, | |
931 | &rxrpc_waiting_client_conns); | |
932 | } | |
933 | ||
934 | nr_active--; | |
935 | } | |
936 | ||
937 | rxrpc_nr_active_client_conns = nr_active; | |
938 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
939 | ASSERTCMP(nr_active, >=, 0); | |
940 | _leave(" [culled]"); | |
941 | } | |
942 | ||
943 | /* | |
944 | * Discard expired client connections from the idle list. Each conn in the | |
945 | * idle list has been exposed and holds an extra ref because of that. | |
946 | * | |
947 | * This may be called from conn setup or from a work item so cannot be | |
948 | * considered non-reentrant. | |
949 | */ | |
950 | static void rxrpc_discard_expired_client_conns(struct work_struct *work) | |
951 | { | |
952 | struct rxrpc_connection *conn; | |
953 | unsigned long expiry, conn_expires_at, now; | |
954 | unsigned int nr_conns; | |
955 | bool did_discard = false; | |
956 | ||
957 | _enter("%c", work ? 'w' : 'n'); | |
958 | ||
959 | if (list_empty(&rxrpc_idle_client_conns)) { | |
960 | _leave(" [empty]"); | |
961 | return; | |
962 | } | |
963 | ||
964 | /* Don't double up on the discarding */ | |
965 | if (!spin_trylock(&rxrpc_client_conn_discard_mutex)) { | |
966 | _leave(" [already]"); | |
967 | return; | |
968 | } | |
969 | ||
970 | /* We keep an estimate of what the number of conns ought to be after | |
971 | * we've discarded some so that we don't overdo the discarding. | |
972 | */ | |
973 | nr_conns = rxrpc_nr_client_conns; | |
974 | ||
975 | next: | |
976 | spin_lock(&rxrpc_client_conn_cache_lock); | |
977 | ||
978 | if (list_empty(&rxrpc_idle_client_conns)) | |
979 | goto out; | |
980 | ||
981 | conn = list_entry(rxrpc_idle_client_conns.next, | |
982 | struct rxrpc_connection, cache_link); | |
983 | ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags)); | |
984 | ||
985 | if (!rxrpc_kill_all_client_conns) { | |
986 | /* If the number of connections is over the reap limit, we | |
987 | * expedite discard by reducing the expiry timeout. We must, | |
988 | * however, have at least a short grace period to be able to do | |
989 | * final-ACK or ABORT retransmission. | |
990 | */ | |
991 | expiry = rxrpc_conn_idle_client_expiry; | |
992 | if (nr_conns > rxrpc_reap_client_connections) | |
993 | expiry = rxrpc_conn_idle_client_fast_expiry; | |
994 | ||
995 | conn_expires_at = conn->idle_timestamp + expiry; | |
996 | ||
997 | now = READ_ONCE(jiffies); | |
998 | if (time_after(conn_expires_at, now)) | |
999 | goto not_yet_expired; | |
1000 | } | |
1001 | ||
1002 | _debug("discard conn %d", conn->debug_id); | |
1003 | if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags)) | |
1004 | BUG(); | |
1005 | conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE; | |
1006 | list_del_init(&conn->cache_link); | |
1007 | ||
1008 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
1009 | ||
1010 | /* When we cleared the EXPOSED flag, we took on responsibility for the | |
1011 | * reference that that had on the usage count. We deal with that here. | |
1012 | * If someone re-sets the flag and re-gets the ref, that's fine. | |
1013 | */ | |
1014 | rxrpc_put_connection(conn); | |
1015 | did_discard = true; | |
1016 | nr_conns--; | |
1017 | goto next; | |
1018 | ||
1019 | not_yet_expired: | |
1020 | /* The connection at the front of the queue hasn't yet expired, so | |
1021 | * schedule the work item for that point if we discarded something. | |
1022 | * | |
1023 | * We don't worry if the work item is already scheduled - it can look | |
1024 | * after rescheduling itself at a later time. We could cancel it, but | |
1025 | * then things get messier. | |
1026 | */ | |
1027 | _debug("not yet"); | |
1028 | if (!rxrpc_kill_all_client_conns) | |
1029 | queue_delayed_work(rxrpc_workqueue, | |
1030 | &rxrpc_client_conn_reap, | |
1031 | conn_expires_at - now); | |
1032 | ||
1033 | out: | |
1034 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
1035 | spin_unlock(&rxrpc_client_conn_discard_mutex); | |
1036 | _leave(""); | |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Preemptively destroy all the client connection records rather than waiting | |
1041 | * for them to time out | |
1042 | */ | |
1043 | void __exit rxrpc_destroy_all_client_connections(void) | |
1044 | { | |
1045 | _enter(""); | |
1046 | ||
1047 | spin_lock(&rxrpc_client_conn_cache_lock); | |
1048 | rxrpc_kill_all_client_conns = true; | |
1049 | spin_unlock(&rxrpc_client_conn_cache_lock); | |
1050 | ||
1051 | cancel_delayed_work(&rxrpc_client_conn_reap); | |
1052 | ||
1053 | if (!queue_delayed_work(rxrpc_workqueue, &rxrpc_client_conn_reap, 0)) | |
1054 | _debug("destroy: queue failed"); | |
1055 | ||
1056 | _leave(""); | |
001c1122 | 1057 | } |