Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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 | * |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | * |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 29 | * The connection is on the rxnet->active_client_conns list which is kept |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 30 | * in activation order for culling purposes. |
| 31 | * |
| 32 | * rxrpc_nr_active_client_conns is held incremented also. |
| 33 | * |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 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 |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | * |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 42 | * (6) IDLE - The connection has no calls in progress upon it and must have |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | * |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 47 | * The connection is on the rxnet->idle_client_conns list which is kept in |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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. |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 77 | #include <linux/sched/signal.h> |
| 78 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 79 | #include "ar-internal.h" |
| 80 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 81 | __read_mostly unsigned int rxrpc_max_client_connections = 1000; |
| 82 | __read_mostly unsigned int rxrpc_reap_client_connections = 900; |
David Howells | a158bdd | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 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; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 85 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 92 | static void rxrpc_cull_active_client_conns(struct rxrpc_net *); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 93 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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 |
David Howells | 090f85d | 2016-09-04 13:14:46 +0100 | [diff] [blame] | 97 | * epoch doesn't change until the client is rebooted (or, at least, unless the |
| 98 | * module is unloaded). |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 99 | */ |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 100 | static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn, |
| 101 | gfp_t gfp) |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 102 | { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 103 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 104 | int id; |
| 105 | |
| 106 | _enter(""); |
| 107 | |
| 108 | idr_preload(gfp); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 109 | spin_lock(&rxrpc_conn_id_lock); |
| 110 | |
David Howells | 090f85d | 2016-09-04 13:14:46 +0100 | [diff] [blame] | 111 | id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn, |
| 112 | 1, 0x40000000, GFP_NOWAIT); |
| 113 | if (id < 0) |
| 114 | goto error; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 115 | |
| 116 | spin_unlock(&rxrpc_conn_id_lock); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 117 | idr_preload_end(); |
| 118 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 119 | conn->proto.epoch = rxnet->epoch; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 120 | conn->proto.cid = id << RXRPC_CIDSHIFT; |
| 121 | set_bit(RXRPC_CONN_HAS_IDR, &conn->flags); |
David Howells | 090f85d | 2016-09-04 13:14:46 +0100 | [diff] [blame] | 122 | _leave(" [CID %x]", conn->proto.cid); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 123 | return 0; |
| 124 | |
| 125 | error: |
| 126 | spin_unlock(&rxrpc_conn_id_lock); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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 | */ |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 135 | static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn) |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 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 | } |
David Howells | eb9b9d2 | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 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 | } |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 163 | |
| 164 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 165 | * Allocate a client connection. |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 171 | struct rxrpc_net *rxnet = cp->local->rxnet; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 182 | atomic_set(&conn->usage, 1); |
David Howells | 8732db6 | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 183 | if (cp->exclusive) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 184 | __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 185 | if (cp->upgrade) |
| 186 | __set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 187 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 188 | conn->params = *cp; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 189 | conn->out_clientflag = RXRPC_CLIENT_INITIATED; |
| 190 | conn->state = RXRPC_CONN_CLIENT; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 191 | conn->service_id = cp->service_id; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 192 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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 | |
David Howells | 31f5f9a16 | 2018-03-30 21:05:33 +0100 | [diff] [blame] | 205 | atomic_inc(&rxnet->nr_conns); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 206 | write_lock(&rxnet->conn_lock); |
| 207 | list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); |
| 208 | write_unlock(&rxnet->conn_lock); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 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); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 232 | * Determine if a connection may be reused. |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 233 | */ |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 234 | static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn) |
| 235 | { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 236 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 237 | int id_cursor, id, distance, limit; |
| 238 | |
| 239 | if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags)) |
| 240 | goto dont_reuse; |
| 241 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 242 | if (conn->proto.epoch != rxnet->epoch) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 251 | id_cursor = idr_get_cursor(&rxrpc_client_conn_ids); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 252 | id = conn->proto.cid >> RXRPC_CIDSHIFT; |
| 253 | distance = id - id_cursor; |
| 254 | if (distance < 0) |
| 255 | distance = -distance; |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 256 | limit = max(rxrpc_max_client_connections * 4, 1024U); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 5e33a23 | 2018-10-05 14:05:34 +0100 | [diff] [blame] | 274 | static int rxrpc_get_client_conn(struct rxrpc_sock *rx, |
| 275 | struct rxrpc_call *call, |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 276 | struct rxrpc_conn_parameters *cp, |
| 277 | struct sockaddr_rxrpc *srx, |
| 278 | gfp_t gfp) |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 284 | int ret = -ENOMEM; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 285 | |
| 286 | _enter("{%d,%lx},", call->debug_id, call->user_call_ID); |
| 287 | |
David Howells | 5e33a23 | 2018-10-05 14:05:34 +0100 | [diff] [blame] | 288 | cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 289 | if (!cp->peer) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 290 | goto error; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 291 | |
David Howells | f7aec12 | 2017-06-14 17:56:50 +0100 | [diff] [blame] | 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 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 301 | if (!cp->exclusive) { |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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) ?: |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 311 | cmp(security_level) ?: |
| 312 | cmp(upgrade)); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 313 | #undef cmp |
| 314 | if (diff < 0) { |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 315 | p = p->rb_left; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 316 | } else if (diff > 0) { |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 317 | p = p->rb_right; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | } |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 329 | } |
| 330 | spin_unlock(&local->client_conns_lock); |
| 331 | } |
| 332 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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"); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 339 | candidate = rxrpc_alloc_client_connection(cp, gfp); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 340 | if (IS_ERR(candidate)) { |
| 341 | ret = PTR_ERR(candidate); |
| 342 | goto error_peer; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 343 | } |
| 344 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 69ffaeb | 2019-03-09 00:29:58 +0000 | [diff] [blame] | 351 | list_add(&call->chan_wait_link, &candidate->waiting_calls); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 352 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 353 | if (cp->exclusive) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 354 | call->conn = candidate; |
David Howells | 278ac0c | 2016-09-07 15:19:25 +0100 | [diff] [blame] | 355 | call->security_ix = candidate->security_ix; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 356 | call->service_id = candidate->service_id; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 357 | _leave(" = 0 [exclusive %d]", candidate->debug_id); |
| 358 | return 0; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 359 | } |
| 360 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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. |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 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 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 374 | #define cmp(X) ((long)conn->params.X - (long)candidate->params.X) |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 375 | diff = (cmp(peer) ?: |
| 376 | cmp(key) ?: |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 377 | cmp(security_level) ?: |
| 378 | cmp(upgrade)); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 379 | #undef cmp |
| 380 | if (diff < 0) { |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 381 | pp = &(*pp)->rb_left; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 382 | } else if (diff > 0) { |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 383 | pp = &(*pp)->rb_right; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 394 | trace_rxrpc_client(conn, -1, rxrpc_client_replace); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 395 | goto candidate_published; |
| 396 | } |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 397 | } |
| 398 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 399 | _debug("new conn"); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 400 | rb_link_node(&candidate->client_node, parent, pp); |
| 401 | rb_insert_color(&candidate->client_node, &local->client_conns); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 402 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 403 | candidate_published: |
| 404 | set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags); |
| 405 | call->conn = candidate; |
David Howells | 278ac0c | 2016-09-07 15:19:25 +0100 | [diff] [blame] | 406 | call->security_ix = candidate->security_ix; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 407 | call->service_id = candidate->service_id; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 408 | spin_unlock(&local->client_conns_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 409 | _leave(" = 0 [new %d]", candidate->debug_id); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 410 | return 0; |
| 411 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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. |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 415 | */ |
| 416 | found_extant_conn: |
| 417 | _debug("found conn"); |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 418 | spin_unlock(&local->client_conns_lock); |
| 419 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 420 | if (candidate) { |
| 421 | trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate); |
| 422 | rxrpc_put_connection(candidate); |
| 423 | candidate = NULL; |
| 424 | } |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 425 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 426 | spin_lock(&conn->channel_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 427 | call->conn = conn; |
David Howells | 278ac0c | 2016-09-07 15:19:25 +0100 | [diff] [blame] | 428 | call->security_ix = conn->security_ix; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 429 | call->service_id = conn->service_id; |
David Howells | 69ffaeb | 2019-03-09 00:29:58 +0000 | [diff] [blame] | 430 | list_add_tail(&call->chan_wait_link, &conn->waiting_calls); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 431 | spin_unlock(&conn->channel_lock); |
| 432 | _leave(" = 0 [extant %d]", conn->debug_id); |
| 433 | return 0; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 434 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 435 | error_peer: |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 436 | rxrpc_put_peer(cp->peer); |
| 437 | cp->peer = NULL; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 438 | error: |
| 439 | _leave(" = %d", ret); |
| 440 | return ret; |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 441 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 442 | |
| 443 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 444 | * Activate a connection. |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 445 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 446 | static void rxrpc_activate_conn(struct rxrpc_net *rxnet, |
| 447 | struct rxrpc_connection *conn) |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 448 | { |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 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 | } |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 456 | rxnet->nr_active_client_conns++; |
| 457 | list_move_tail(&conn->cache_link, &rxnet->active_client_conns); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 458 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 459 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 472 | static void rxrpc_animate_client_conn(struct rxrpc_net *rxnet, |
| 473 | struct rxrpc_connection *conn) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 474 | { |
| 475 | unsigned int nr_conns; |
| 476 | |
| 477 | _enter("%d,%d", conn->debug_id, conn->cache_state); |
| 478 | |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 479 | if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE || |
| 480 | conn->cache_state == RXRPC_CONN_CLIENT_UPGRADE) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 481 | goto out; |
| 482 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 483 | spin_lock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 484 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 485 | nr_conns = rxnet->nr_client_conns; |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 486 | if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) { |
| 487 | trace_rxrpc_client(conn, -1, rxrpc_client_count); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 488 | rxnet->nr_client_conns = nr_conns + 1; |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 489 | } |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 490 | |
| 491 | switch (conn->cache_state) { |
| 492 | case RXRPC_CONN_CLIENT_ACTIVE: |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 493 | case RXRPC_CONN_CLIENT_UPGRADE: |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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(); |
| 506 | } |
| 507 | |
| 508 | out_unlock: |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 509 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 510 | out: |
| 511 | _leave(" [%d]", conn->cache_state); |
| 512 | return; |
| 513 | |
| 514 | activate_conn: |
| 515 | _debug("activate"); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 516 | rxrpc_activate_conn(rxnet, conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 517 | goto out_unlock; |
| 518 | |
| 519 | wait_for_capacity: |
| 520 | _debug("wait"); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 521 | trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 522 | conn->cache_state = RXRPC_CONN_CLIENT_WAITING; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 523 | list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 552 | trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate); |
| 553 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 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 | |
David Howells | af338a9 | 2016-09-04 13:10:10 +0100 | [diff] [blame] | 559 | write_lock_bh(&call->state_lock); |
David Howells | e122d84 | 2019-01-10 16:59:13 +0000 | [diff] [blame] | 560 | call->state = RXRPC_CALL_CLIENT_SEND_REQUEST; |
David Howells | af338a9 | 2016-09-04 13:10:10 +0100 | [diff] [blame] | 561 | write_unlock_bh(&call->state_lock); |
| 562 | |
David Howells | e34d423 | 2016-08-30 09:49:29 +0100 | [diff] [blame] | 563 | rxrpc_see_call(call); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 89ca694 | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 570 | trace_rxrpc_connect_call(call); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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; |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 586 | chan->call_debug_id = call->debug_id; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 587 | rcu_assign_pointer(chan->call, call); |
| 588 | wake_up(&call->waitq); |
| 589 | } |
| 590 | |
| 591 | /* |
David Howells | 2629c7f | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 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; |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 603 | case RXRPC_CONN_CLIENT_UPGRADE: |
| 604 | mask = 0x01; |
| 605 | break; |
David Howells | 2629c7f | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 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 | |
| 617 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 618 | * Assign channels and callNumbers to waiting calls. |
| 619 | */ |
| 620 | static void rxrpc_activate_channels(struct rxrpc_connection *conn) |
| 621 | { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 622 | _enter("%d", conn->debug_id); |
| 623 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 624 | trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans); |
| 625 | |
David Howells | 2629c7f | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 626 | if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 627 | return; |
| 628 | |
| 629 | spin_lock(&conn->channel_lock); |
David Howells | 2629c7f | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 630 | rxrpc_activate_channels_locked(conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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); |
| 646 | |
| 647 | if (!gfpflags_allow_blocking(gfp)) { |
| 648 | ret = -EAGAIN; |
| 649 | goto out; |
| 650 | } |
| 651 | |
| 652 | add_wait_queue_exclusive(&call->waitq, &myself); |
| 653 | for (;;) { |
David Howells | b960a34 | 2019-05-09 08:21:21 +0100 | [diff] [blame] | 654 | if (test_bit(RXRPC_CALL_IS_INTR, &call->flags)) |
| 655 | set_current_state(TASK_INTERRUPTIBLE); |
| 656 | else |
| 657 | set_current_state(TASK_UNINTERRUPTIBLE); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 658 | if (call->call_id) |
| 659 | break; |
David Howells | b960a34 | 2019-05-09 08:21:21 +0100 | [diff] [blame] | 660 | if (test_bit(RXRPC_CALL_IS_INTR, &call->flags) && |
| 661 | signal_pending(current)) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 662 | ret = -ERESTARTSYS; |
| 663 | break; |
| 664 | } |
| 665 | schedule(); |
| 666 | } |
| 667 | remove_wait_queue(&call->waitq, &myself); |
| 668 | __set_current_state(TASK_RUNNING); |
| 669 | } |
| 670 | |
| 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 | */ |
David Howells | 5e33a23 | 2018-10-05 14:05:34 +0100 | [diff] [blame] | 683 | int rxrpc_connect_call(struct rxrpc_sock *rx, |
| 684 | struct rxrpc_call *call, |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 685 | struct rxrpc_conn_parameters *cp, |
| 686 | struct sockaddr_rxrpc *srx, |
| 687 | gfp_t gfp) |
| 688 | { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 689 | struct rxrpc_net *rxnet = cp->local->rxnet; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 690 | int ret; |
| 691 | |
| 692 | _enter("{%d,%lx},", call->debug_id, call->user_call_ID); |
| 693 | |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 694 | rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 695 | rxrpc_cull_active_client_conns(rxnet); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 696 | |
David Howells | 5e33a23 | 2018-10-05 14:05:34 +0100 | [diff] [blame] | 697 | ret = rxrpc_get_client_conn(rx, call, cp, srx, gfp); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 698 | if (ret < 0) |
David Howells | c038a58 | 2017-08-29 10:19:01 +0100 | [diff] [blame] | 699 | goto out; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 700 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 701 | rxrpc_animate_client_conn(rxnet, call->conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 702 | rxrpc_activate_channels(call->conn); |
| 703 | |
| 704 | ret = rxrpc_wait_for_channel(call, gfp); |
David Howells | c038a58 | 2017-08-29 10:19:01 +0100 | [diff] [blame] | 705 | if (ret < 0) { |
David Howells | 930c9f9 | 2019-03-08 12:48:39 +0000 | [diff] [blame] | 706 | trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 707 | rxrpc_disconnect_client_call(call); |
David Howells | c038a58 | 2017-08-29 10:19:01 +0100 | [diff] [blame] | 708 | goto out; |
| 709 | } |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 710 | |
David Howells | c038a58 | 2017-08-29 10:19:01 +0100 | [diff] [blame] | 711 | spin_lock_bh(&call->conn->params.peer->lock); |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 712 | hlist_add_head_rcu(&call->error_link, |
| 713 | &call->conn->params.peer->error_targets); |
David Howells | c038a58 | 2017-08-29 10:19:01 +0100 | [diff] [blame] | 714 | spin_unlock_bh(&call->conn->params.peer->lock); |
| 715 | |
| 716 | out: |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 728 | static void rxrpc_expose_client_conn(struct rxrpc_connection *conn, |
| 729 | unsigned int channel) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 730 | { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 731 | if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) { |
| 732 | trace_rxrpc_client(conn, channel, rxrpc_client_exposed); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 733 | rxrpc_get_connection(conn); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 734 | } |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 743 | unsigned int channel = call->cid & RXRPC_CHANNELMASK; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 744 | struct rxrpc_connection *conn = call->conn; |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 745 | struct rxrpc_channel *chan = &conn->channels[channel]; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 756 | rxrpc_expose_client_conn(conn, channel); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | /* |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 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 | |
| 772 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 773 | * Disconnect a client call. |
| 774 | */ |
| 775 | void rxrpc_disconnect_client_call(struct rxrpc_call *call) |
| 776 | { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 777 | struct rxrpc_connection *conn = call->conn; |
David Howells | 930c9f9 | 2019-03-08 12:48:39 +0000 | [diff] [blame] | 778 | struct rxrpc_channel *chan = NULL; |
David Howells | 88f2a825 | 2018-03-30 21:05:17 +0100 | [diff] [blame] | 779 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
David Howells | 930c9f9 | 2019-03-08 12:48:39 +0000 | [diff] [blame] | 780 | unsigned int channel = -1; |
| 781 | u32 cid; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 782 | |
| 783 | spin_lock(&conn->channel_lock); |
| 784 | |
David Howells | 930c9f9 | 2019-03-08 12:48:39 +0000 | [diff] [blame] | 785 | cid = call->cid; |
| 786 | if (cid) { |
| 787 | channel = cid & RXRPC_CHANNELMASK; |
| 788 | chan = &conn->channels[channel]; |
| 789 | } |
| 790 | trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect); |
| 791 | call->conn = NULL; |
| 792 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 803 | trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted); |
| 804 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 805 | /* We must deactivate or idle the connection if it's now |
| 806 | * waiting for nothing. |
| 807 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 808 | spin_lock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 930c9f9 | 2019-03-08 12:48:39 +0000 | [diff] [blame] | 816 | if (rcu_access_pointer(chan->call) != call) { |
| 817 | spin_unlock(&conn->channel_lock); |
| 818 | BUG(); |
| 819 | } |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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)) { |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 831 | _debug("exposed %u,%u", call->call_id, call->abort_code); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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)) { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 838 | trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 839 | rxrpc_activate_one_channel(conn, channel); |
| 840 | goto out_2; |
| 841 | } |
| 842 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 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 | */ |
David Howells | 17e9e23 | 2018-02-06 16:44:12 +0000 | [diff] [blame] | 847 | if (call->completion == RXRPC_CALL_SUCCEEDED && |
| 848 | test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 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 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 862 | spin_lock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 863 | |
| 864 | switch (conn->cache_state) { |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 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 */ |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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) { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 878 | rxnet->nr_active_client_conns--; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 879 | goto idle_connection; |
| 880 | } |
| 881 | goto out; |
| 882 | } |
| 883 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 884 | trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | } |
| 902 | |
| 903 | out: |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 904 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 905 | out_2: |
| 906 | spin_unlock(&conn->channel_lock); |
| 907 | rxrpc_put_connection(conn); |
| 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)) { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 916 | trace_rxrpc_client(conn, channel, rxrpc_client_to_idle); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 917 | conn->idle_timestamp = jiffies; |
| 918 | conn->cache_state = RXRPC_CONN_CLIENT_IDLE; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 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) |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 922 | rxrpc_set_client_reap_timer(rxnet); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 923 | } else { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 924 | trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 925 | conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE; |
| 926 | list_del_init(&conn->cache_link); |
| 927 | } |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Clean up a dead client connection. |
| 933 | */ |
| 934 | static struct rxrpc_connection * |
| 935 | rxrpc_put_one_client_conn(struct rxrpc_connection *conn) |
| 936 | { |
David Howells | 66d58af | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 937 | struct rxrpc_connection *next = NULL; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 938 | struct rxrpc_local *local = conn->params.local; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 939 | struct rxrpc_net *rxnet = local->rxnet; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 940 | unsigned int nr_conns; |
| 941 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 942 | trace_rxrpc_client(conn, -1, rxrpc_client_cleanup); |
| 943 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 951 | |
| 952 | rxrpc_put_client_connection_id(conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 953 | |
| 954 | ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE); |
| 955 | |
David Howells | 66d58af | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 956 | if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 957 | trace_rxrpc_client(conn, -1, rxrpc_client_uncount); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 958 | spin_lock(&rxnet->client_conn_cache_lock); |
| 959 | nr_conns = --rxnet->nr_client_conns; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 960 | |
David Howells | 66d58af | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 961 | if (nr_conns < rxrpc_max_client_connections && |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 962 | !list_empty(&rxnet->waiting_client_conns)) { |
| 963 | next = list_entry(rxnet->waiting_client_conns.next, |
David Howells | 66d58af | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 964 | struct rxrpc_connection, cache_link); |
| 965 | rxrpc_get_connection(next); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 966 | rxrpc_activate_conn(rxnet, next); |
David Howells | 66d58af | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 967 | } |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 968 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 969 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 970 | } |
| 971 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 972 | rxrpc_kill_connection(conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 987 | const void *here = __builtin_return_address(0); |
| 988 | int n; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 989 | |
| 990 | do { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 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); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 996 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 997 | conn = rxrpc_put_one_client_conn(conn); |
| 998 | } while (conn); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Kill the longest-active client connections to make room for new ones. |
| 1003 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1004 | static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1005 | { |
| 1006 | struct rxrpc_connection *conn; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1007 | unsigned int nr_conns = rxnet->nr_client_conns; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1019 | spin_lock(&rxnet->client_conn_cache_lock); |
| 1020 | nr_active = rxnet->nr_active_client_conns; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1021 | |
| 1022 | while (nr_active > limit) { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1023 | ASSERT(!list_empty(&rxnet->active_client_conns)); |
| 1024 | conn = list_entry(rxnet->active_client_conns.next, |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1025 | struct rxrpc_connection, cache_link); |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 1026 | ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE, |
| 1027 | conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1028 | |
| 1029 | if (list_empty(&conn->waiting_calls)) { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 1030 | trace_rxrpc_client(conn, -1, rxrpc_client_to_culled); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1031 | conn->cache_state = RXRPC_CONN_CLIENT_CULLED; |
| 1032 | list_del_init(&conn->cache_link); |
| 1033 | } else { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 1034 | trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1035 | conn->cache_state = RXRPC_CONN_CLIENT_WAITING; |
| 1036 | list_move_tail(&conn->cache_link, |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1037 | &rxnet->waiting_client_conns); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | nr_active--; |
| 1041 | } |
| 1042 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1043 | rxnet->nr_active_client_conns = nr_active; |
| 1044 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1056 | void rxrpc_discard_expired_client_conns(struct work_struct *work) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1057 | { |
| 1058 | struct rxrpc_connection *conn; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1059 | struct rxrpc_net *rxnet = |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 1060 | container_of(work, struct rxrpc_net, client_conn_reaper); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1061 | unsigned long expiry, conn_expires_at, now; |
| 1062 | unsigned int nr_conns; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1063 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1064 | _enter(""); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1065 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1066 | if (list_empty(&rxnet->idle_client_conns)) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1067 | _leave(" [empty]"); |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | /* Don't double up on the discarding */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1072 | if (!spin_trylock(&rxnet->client_conn_discard_lock)) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1080 | nr_conns = rxnet->nr_client_conns; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1081 | |
| 1082 | next: |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1083 | spin_lock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1084 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1085 | if (list_empty(&rxnet->idle_client_conns)) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1086 | goto out; |
| 1087 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1088 | conn = list_entry(rxnet->idle_client_conns.next, |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1089 | struct rxrpc_connection, cache_link); |
| 1090 | ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags)); |
| 1091 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1092 | if (!rxnet->kill_all_client_conns) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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; |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 1101 | if (conn->params.local->service_closed) |
| 1102 | expiry = rxrpc_closed_conn_expiry * HZ; |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 1111 | trace_rxrpc_client(conn, -1, rxrpc_client_discard); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1117 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 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"); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1136 | if (!rxnet->kill_all_client_conns) |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 1137 | timer_reduce(&rxnet->client_conn_reap_timer, |
| 1138 | conn_expires_at); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1139 | |
| 1140 | out: |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1141 | spin_unlock(&rxnet->client_conn_cache_lock); |
| 1142 | spin_unlock(&rxnet->client_conn_discard_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1143 | _leave(""); |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Preemptively destroy all the client connection records rather than waiting |
| 1148 | * for them to time out |
| 1149 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1150 | void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1151 | { |
| 1152 | _enter(""); |
| 1153 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 1154 | spin_lock(&rxnet->client_conn_cache_lock); |
| 1155 | rxnet->kill_all_client_conns = true; |
| 1156 | spin_unlock(&rxnet->client_conn_cache_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1157 | |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 1158 | del_timer_sync(&rxnet->client_conn_reap_timer); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1159 | |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 1160 | if (!rxrpc_queue_work(&rxnet->client_conn_reaper)) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 1161 | _debug("destroy: queue failed"); |
| 1162 | |
| 1163 | _leave(""); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 1164 | } |