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