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