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