David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC virtual connection handler |
| 2 | * |
| 3 | * Copyright (C) 2007 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 License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include <linux/net.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/crypto.h> |
| 19 | #include <net/sock.h> |
| 20 | #include <net/af_rxrpc.h> |
| 21 | #include "ar-internal.h" |
| 22 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Time till a connection expires after last use (in seconds). |
| 25 | */ |
David Howells | dad8aff | 2016-03-09 23:22:56 +0000 | [diff] [blame] | 26 | unsigned int rxrpc_connection_expiry = 10 * 60; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 27 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 28 | static void rxrpc_connection_reaper(struct work_struct *work); |
| 29 | |
| 30 | LIST_HEAD(rxrpc_connections); |
| 31 | DEFINE_RWLOCK(rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | static DECLARE_DELAYED_WORK(rxrpc_connection_reap, rxrpc_connection_reaper); |
| 33 | |
| 34 | /* |
| 35 | * allocate a new client connection bundle |
| 36 | */ |
| 37 | static struct rxrpc_conn_bundle *rxrpc_alloc_bundle(gfp_t gfp) |
| 38 | { |
| 39 | struct rxrpc_conn_bundle *bundle; |
| 40 | |
| 41 | _enter(""); |
| 42 | |
| 43 | bundle = kzalloc(sizeof(struct rxrpc_conn_bundle), gfp); |
| 44 | if (bundle) { |
| 45 | INIT_LIST_HEAD(&bundle->unused_conns); |
| 46 | INIT_LIST_HEAD(&bundle->avail_conns); |
| 47 | INIT_LIST_HEAD(&bundle->busy_conns); |
| 48 | init_waitqueue_head(&bundle->chanwait); |
| 49 | atomic_set(&bundle->usage, 1); |
| 50 | } |
| 51 | |
| 52 | _leave(" = %p", bundle); |
| 53 | return bundle; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * compare bundle parameters with what we're looking for |
| 58 | * - return -ve, 0 or +ve |
| 59 | */ |
| 60 | static inline |
| 61 | int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle *bundle, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 62 | struct key *key, u16 service_id) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 63 | { |
| 64 | return (bundle->service_id - service_id) ?: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 65 | ((unsigned long)bundle->key - (unsigned long)key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * get bundle of client connections that a client socket can make use of |
| 70 | */ |
| 71 | struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *rx, |
| 72 | struct rxrpc_transport *trans, |
| 73 | struct key *key, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 74 | u16 service_id, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 75 | gfp_t gfp) |
| 76 | { |
| 77 | struct rxrpc_conn_bundle *bundle, *candidate; |
| 78 | struct rb_node *p, *parent, **pp; |
| 79 | |
| 80 | _enter("%p{%x},%x,%hx,", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 81 | rx, key_serial(key), trans->debug_id, service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 82 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 83 | /* search the extant bundles first for one that matches the specified |
| 84 | * user ID */ |
| 85 | spin_lock(&trans->client_lock); |
| 86 | |
| 87 | p = trans->bundles.rb_node; |
| 88 | while (p) { |
| 89 | bundle = rb_entry(p, struct rxrpc_conn_bundle, node); |
| 90 | |
| 91 | if (rxrpc_cmp_bundle(bundle, key, service_id) < 0) |
| 92 | p = p->rb_left; |
| 93 | else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0) |
| 94 | p = p->rb_right; |
| 95 | else |
| 96 | goto found_extant_bundle; |
| 97 | } |
| 98 | |
| 99 | spin_unlock(&trans->client_lock); |
| 100 | |
| 101 | /* not yet present - create a candidate for a new record and then |
| 102 | * redo the search */ |
| 103 | candidate = rxrpc_alloc_bundle(gfp); |
| 104 | if (!candidate) { |
| 105 | _leave(" = -ENOMEM"); |
| 106 | return ERR_PTR(-ENOMEM); |
| 107 | } |
| 108 | |
| 109 | candidate->key = key_get(key); |
| 110 | candidate->service_id = service_id; |
| 111 | |
| 112 | spin_lock(&trans->client_lock); |
| 113 | |
| 114 | pp = &trans->bundles.rb_node; |
| 115 | parent = NULL; |
| 116 | while (*pp) { |
| 117 | parent = *pp; |
| 118 | bundle = rb_entry(parent, struct rxrpc_conn_bundle, node); |
| 119 | |
| 120 | if (rxrpc_cmp_bundle(bundle, key, service_id) < 0) |
| 121 | pp = &(*pp)->rb_left; |
| 122 | else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0) |
| 123 | pp = &(*pp)->rb_right; |
| 124 | else |
| 125 | goto found_extant_second; |
| 126 | } |
| 127 | |
| 128 | /* second search also failed; add the new bundle */ |
| 129 | bundle = candidate; |
| 130 | candidate = NULL; |
| 131 | |
| 132 | rb_link_node(&bundle->node, parent, pp); |
| 133 | rb_insert_color(&bundle->node, &trans->bundles); |
| 134 | spin_unlock(&trans->client_lock); |
| 135 | _net("BUNDLE new on trans %d", trans->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 136 | _leave(" = %p [new]", bundle); |
| 137 | return bundle; |
| 138 | |
| 139 | /* we found the bundle in the list immediately */ |
| 140 | found_extant_bundle: |
| 141 | atomic_inc(&bundle->usage); |
| 142 | spin_unlock(&trans->client_lock); |
| 143 | _net("BUNDLE old on trans %d", trans->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 144 | _leave(" = %p [extant %d]", bundle, atomic_read(&bundle->usage)); |
| 145 | return bundle; |
| 146 | |
| 147 | /* we found the bundle on the second time through the list */ |
| 148 | found_extant_second: |
| 149 | atomic_inc(&bundle->usage); |
| 150 | spin_unlock(&trans->client_lock); |
| 151 | kfree(candidate); |
| 152 | _net("BUNDLE old2 on trans %d", trans->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 153 | _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage)); |
| 154 | return bundle; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * release a bundle |
| 159 | */ |
| 160 | void rxrpc_put_bundle(struct rxrpc_transport *trans, |
| 161 | struct rxrpc_conn_bundle *bundle) |
| 162 | { |
| 163 | _enter("%p,%p{%d}",trans, bundle, atomic_read(&bundle->usage)); |
| 164 | |
| 165 | if (atomic_dec_and_lock(&bundle->usage, &trans->client_lock)) { |
| 166 | _debug("Destroy bundle"); |
| 167 | rb_erase(&bundle->node, &trans->bundles); |
| 168 | spin_unlock(&trans->client_lock); |
| 169 | ASSERT(list_empty(&bundle->unused_conns)); |
| 170 | ASSERT(list_empty(&bundle->avail_conns)); |
| 171 | ASSERT(list_empty(&bundle->busy_conns)); |
| 172 | ASSERTCMP(bundle->num_conns, ==, 0); |
| 173 | key_put(bundle->key); |
| 174 | kfree(bundle); |
| 175 | } |
| 176 | |
| 177 | _leave(""); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * allocate a new connection |
| 182 | */ |
| 183 | static struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp) |
| 184 | { |
| 185 | struct rxrpc_connection *conn; |
| 186 | |
| 187 | _enter(""); |
| 188 | |
| 189 | conn = kzalloc(sizeof(struct rxrpc_connection), gfp); |
| 190 | if (conn) { |
| 191 | INIT_WORK(&conn->processor, &rxrpc_process_connection); |
| 192 | INIT_LIST_HEAD(&conn->bundle_link); |
| 193 | conn->calls = RB_ROOT; |
| 194 | skb_queue_head_init(&conn->rx_queue); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 195 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 196 | rwlock_init(&conn->lock); |
| 197 | spin_lock_init(&conn->state_lock); |
| 198 | atomic_set(&conn->usage, 1); |
| 199 | conn->debug_id = atomic_inc_return(&rxrpc_debug_id); |
| 200 | conn->avail_calls = RXRPC_MAXCALLS; |
| 201 | conn->size_align = 4; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 202 | conn->header_size = sizeof(struct rxrpc_wire_header); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Adrian Bunk | 16c61ad | 2007-06-15 15:15:43 -0700 | [diff] [blame] | 205 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 206 | return conn; |
| 207 | } |
| 208 | |
| 209 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 210 | * add a call to a connection's call-by-ID tree |
| 211 | */ |
| 212 | static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn, |
| 213 | struct rxrpc_call *call) |
| 214 | { |
| 215 | struct rxrpc_call *xcall; |
| 216 | struct rb_node *parent, **p; |
| 217 | __be32 call_id; |
| 218 | |
| 219 | write_lock_bh(&conn->lock); |
| 220 | |
| 221 | call_id = call->call_id; |
| 222 | p = &conn->calls.rb_node; |
| 223 | parent = NULL; |
| 224 | while (*p) { |
| 225 | parent = *p; |
| 226 | xcall = rb_entry(parent, struct rxrpc_call, conn_node); |
| 227 | |
| 228 | if (call_id < xcall->call_id) |
| 229 | p = &(*p)->rb_left; |
| 230 | else if (call_id > xcall->call_id) |
| 231 | p = &(*p)->rb_right; |
| 232 | else |
| 233 | BUG(); |
| 234 | } |
| 235 | |
| 236 | rb_link_node(&call->conn_node, parent, p); |
| 237 | rb_insert_color(&call->conn_node, &conn->calls); |
| 238 | |
| 239 | write_unlock_bh(&conn->lock); |
| 240 | } |
| 241 | |
| 242 | /* |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 243 | * Allocate a client connection. |
| 244 | */ |
| 245 | static struct rxrpc_connection * |
| 246 | rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, |
| 247 | struct rxrpc_transport *trans, |
| 248 | gfp_t gfp) |
| 249 | { |
| 250 | struct rxrpc_connection *conn; |
| 251 | int ret; |
| 252 | |
| 253 | _enter(""); |
| 254 | |
| 255 | conn = rxrpc_alloc_connection(gfp); |
| 256 | if (!conn) { |
| 257 | _leave(" = -ENOMEM"); |
| 258 | return ERR_PTR(-ENOMEM); |
| 259 | } |
| 260 | |
| 261 | conn->params = *cp; |
| 262 | conn->proto.local = cp->local; |
| 263 | conn->proto.epoch = rxrpc_epoch; |
| 264 | conn->proto.cid = 0; |
| 265 | conn->proto.in_clientflag = 0; |
| 266 | conn->proto.family = cp->peer->srx.transport.family; |
| 267 | conn->out_clientflag = RXRPC_CLIENT_INITIATED; |
| 268 | conn->state = RXRPC_CONN_CLIENT; |
| 269 | |
| 270 | switch (conn->proto.family) { |
| 271 | case AF_INET: |
| 272 | conn->proto.addr_size = sizeof(conn->proto.ipv4_addr); |
| 273 | conn->proto.ipv4_addr = cp->peer->srx.transport.sin.sin_addr; |
| 274 | conn->proto.port = cp->peer->srx.transport.sin.sin_port; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | ret = rxrpc_get_client_connection_id(conn, trans, gfp); |
| 279 | if (ret < 0) |
| 280 | goto error_0; |
| 281 | |
| 282 | ret = rxrpc_init_client_conn_security(conn); |
| 283 | if (ret < 0) |
| 284 | goto error_1; |
| 285 | |
| 286 | conn->security->prime_packet_security(conn); |
| 287 | |
| 288 | write_lock(&rxrpc_connection_lock); |
| 289 | list_add_tail(&conn->link, &rxrpc_connections); |
| 290 | write_unlock(&rxrpc_connection_lock); |
| 291 | |
| 292 | key_get(conn->params.key); |
| 293 | |
| 294 | _leave(" = %p", conn); |
| 295 | return conn; |
| 296 | |
| 297 | error_1: |
| 298 | rxrpc_put_client_connection_id(conn); |
| 299 | error_0: |
| 300 | kfree(conn); |
| 301 | _leave(" = %d", ret); |
| 302 | return ERR_PTR(ret); |
| 303 | } |
| 304 | |
| 305 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 306 | * connect a call on an exclusive connection |
| 307 | */ |
| 308 | static int rxrpc_connect_exclusive(struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 309 | struct rxrpc_conn_parameters *cp, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 310 | struct rxrpc_transport *trans, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 311 | struct rxrpc_call *call, |
| 312 | gfp_t gfp) |
| 313 | { |
| 314 | struct rxrpc_connection *conn; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 315 | int chan; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 316 | |
| 317 | _enter(""); |
| 318 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 319 | conn = rxrpc_alloc_client_connection(cp, trans, gfp); |
| 320 | if (IS_ERR(conn)) { |
| 321 | _leave(" = %ld", PTR_ERR(conn)); |
| 322 | return PTR_ERR(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 323 | } |
| 324 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 325 | atomic_inc(&trans->usage); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 326 | conn->trans = trans; |
| 327 | conn->bundle = NULL; |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 328 | |
| 329 | _net("CONNECT EXCL new %d on TRANS %d", |
| 330 | conn->debug_id, conn->trans->debug_id); |
| 331 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 332 | /* Since no one else can use the connection, we just use the first |
| 333 | * channel. |
| 334 | */ |
| 335 | chan = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 336 | atomic_inc(&conn->usage); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 337 | conn->avail_calls = RXRPC_MAXCALLS - 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 338 | conn->channels[chan] = call; |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 339 | conn->call_counter = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 340 | call->conn = conn; |
| 341 | call->channel = chan; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 342 | call->cid = conn->proto.cid | chan; |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 343 | call->call_id = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 344 | |
| 345 | _net("CONNECT client on conn %d chan %d as call %x", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 346 | conn->debug_id, chan, call->call_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 347 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 348 | rxrpc_add_call_ID_to_conn(conn, call); |
| 349 | _leave(" = 0"); |
| 350 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * find a connection for a call |
| 355 | * - called in process context with IRQs enabled |
| 356 | */ |
| 357 | int rxrpc_connect_call(struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 358 | struct rxrpc_conn_parameters *cp, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 359 | struct rxrpc_transport *trans, |
| 360 | struct rxrpc_conn_bundle *bundle, |
| 361 | struct rxrpc_call *call, |
| 362 | gfp_t gfp) |
| 363 | { |
| 364 | struct rxrpc_connection *conn, *candidate; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 365 | int chan; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 366 | |
| 367 | DECLARE_WAITQUEUE(myself, current); |
| 368 | |
| 369 | _enter("%p,%lx,", rx, call->user_call_ID); |
| 370 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 371 | if (cp->exclusive) |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 372 | return rxrpc_connect_exclusive(rx, cp, trans, call, gfp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 373 | |
| 374 | spin_lock(&trans->client_lock); |
| 375 | for (;;) { |
| 376 | /* see if the bundle has a call slot available */ |
| 377 | if (!list_empty(&bundle->avail_conns)) { |
| 378 | _debug("avail"); |
| 379 | conn = list_entry(bundle->avail_conns.next, |
| 380 | struct rxrpc_connection, |
| 381 | bundle_link); |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 382 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 383 | list_del_init(&conn->bundle_link); |
| 384 | bundle->num_conns--; |
| 385 | continue; |
| 386 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 387 | if (--conn->avail_calls == 0) |
| 388 | list_move(&conn->bundle_link, |
| 389 | &bundle->busy_conns); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 390 | ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS); |
| 391 | ASSERT(conn->channels[0] == NULL || |
| 392 | conn->channels[1] == NULL || |
| 393 | conn->channels[2] == NULL || |
| 394 | conn->channels[3] == NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 395 | atomic_inc(&conn->usage); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | if (!list_empty(&bundle->unused_conns)) { |
| 400 | _debug("unused"); |
| 401 | conn = list_entry(bundle->unused_conns.next, |
| 402 | struct rxrpc_connection, |
| 403 | bundle_link); |
David Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 404 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 405 | list_del_init(&conn->bundle_link); |
| 406 | bundle->num_conns--; |
| 407 | continue; |
| 408 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 409 | ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS); |
| 410 | conn->avail_calls = RXRPC_MAXCALLS - 1; |
| 411 | ASSERT(conn->channels[0] == NULL && |
| 412 | conn->channels[1] == NULL && |
| 413 | conn->channels[2] == NULL && |
| 414 | conn->channels[3] == NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 415 | atomic_inc(&conn->usage); |
| 416 | list_move(&conn->bundle_link, &bundle->avail_conns); |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | /* need to allocate a new connection */ |
| 421 | _debug("get new conn [%d]", bundle->num_conns); |
| 422 | |
| 423 | spin_unlock(&trans->client_lock); |
| 424 | |
| 425 | if (signal_pending(current)) |
| 426 | goto interrupted; |
| 427 | |
| 428 | if (bundle->num_conns >= 20) { |
| 429 | _debug("too many conns"); |
| 430 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 431 | if (!gfpflags_allow_blocking(gfp)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 432 | _leave(" = -EAGAIN"); |
| 433 | return -EAGAIN; |
| 434 | } |
| 435 | |
| 436 | add_wait_queue(&bundle->chanwait, &myself); |
| 437 | for (;;) { |
| 438 | set_current_state(TASK_INTERRUPTIBLE); |
| 439 | if (bundle->num_conns < 20 || |
| 440 | !list_empty(&bundle->unused_conns) || |
| 441 | !list_empty(&bundle->avail_conns)) |
| 442 | break; |
| 443 | if (signal_pending(current)) |
| 444 | goto interrupted_dequeue; |
| 445 | schedule(); |
| 446 | } |
| 447 | remove_wait_queue(&bundle->chanwait, &myself); |
| 448 | __set_current_state(TASK_RUNNING); |
| 449 | spin_lock(&trans->client_lock); |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | /* not yet present - create a candidate for a new connection and then |
| 454 | * redo the check */ |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 455 | candidate = rxrpc_alloc_client_connection(cp, trans, gfp); |
Dan Carpenter | 0975ecb | 2009-05-21 15:22:02 -0700 | [diff] [blame] | 456 | if (!candidate) { |
| 457 | _leave(" = -ENOMEM"); |
| 458 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 459 | } |
| 460 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 461 | atomic_inc(&bundle->usage); |
| 462 | atomic_inc(&trans->usage); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 463 | candidate->trans = trans; |
| 464 | candidate->bundle = bundle; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 465 | |
| 466 | spin_lock(&trans->client_lock); |
| 467 | |
| 468 | list_add(&candidate->bundle_link, &bundle->unused_conns); |
| 469 | bundle->num_conns++; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 470 | |
| 471 | _net("CONNECT new %d on TRANS %d", |
| 472 | candidate->debug_id, candidate->trans->debug_id); |
| 473 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 474 | /* leave the candidate lurking in zombie mode attached to the |
| 475 | * bundle until we're ready for it */ |
| 476 | rxrpc_put_connection(candidate); |
| 477 | candidate = NULL; |
| 478 | } |
| 479 | |
| 480 | /* we've got a connection with a free channel and we can now attach the |
| 481 | * call to it |
| 482 | * - we're holding the transport's client lock |
| 483 | * - we're holding a reference on the connection |
| 484 | * - we're holding a reference on the bundle |
| 485 | */ |
| 486 | for (chan = 0; chan < RXRPC_MAXCALLS; chan++) |
| 487 | if (!conn->channels[chan]) |
| 488 | goto found_channel; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 489 | ASSERT(conn->channels[0] == NULL || |
| 490 | conn->channels[1] == NULL || |
| 491 | conn->channels[2] == NULL || |
| 492 | conn->channels[3] == NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 493 | BUG(); |
| 494 | |
| 495 | found_channel: |
| 496 | conn->channels[chan] = call; |
| 497 | call->conn = conn; |
| 498 | call->channel = chan; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 499 | call->cid = conn->proto.cid | chan; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 500 | call->call_id = ++conn->call_counter; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 501 | |
| 502 | _net("CONNECT client on conn %d chan %d as call %x", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 503 | conn->debug_id, chan, call->call_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 504 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 505 | ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 506 | spin_unlock(&trans->client_lock); |
| 507 | |
| 508 | rxrpc_add_call_ID_to_conn(conn, call); |
| 509 | |
| 510 | _leave(" = 0"); |
| 511 | return 0; |
| 512 | |
| 513 | interrupted_dequeue: |
| 514 | remove_wait_queue(&bundle->chanwait, &myself); |
| 515 | __set_current_state(TASK_RUNNING); |
| 516 | interrupted: |
| 517 | _leave(" = -ERESTARTSYS"); |
| 518 | return -ERESTARTSYS; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * get a record of an incoming connection |
| 523 | */ |
| 524 | struct rxrpc_connection * |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 525 | rxrpc_incoming_connection(struct rxrpc_transport *trans, struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 526 | { |
| 527 | struct rxrpc_connection *conn, *candidate = NULL; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 528 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 529 | struct rb_node *p, **pp; |
| 530 | const char *new = "old"; |
| 531 | __be32 epoch; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 532 | u32 cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 533 | |
| 534 | _enter(""); |
| 535 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 536 | ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 537 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 538 | epoch = sp->hdr.epoch; |
| 539 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 540 | |
| 541 | /* search the connection list first */ |
| 542 | read_lock_bh(&trans->conn_lock); |
| 543 | |
| 544 | p = trans->server_conns.rb_node; |
| 545 | while (p) { |
| 546 | conn = rb_entry(p, struct rxrpc_connection, node); |
| 547 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 548 | _debug("maybe %x", conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 549 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 550 | if (epoch < conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 551 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 552 | else if (epoch > conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 553 | p = p->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 554 | else if (cid < conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 555 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 556 | else if (cid > conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 557 | p = p->rb_right; |
| 558 | else |
| 559 | goto found_extant_connection; |
| 560 | } |
| 561 | read_unlock_bh(&trans->conn_lock); |
| 562 | |
| 563 | /* not yet present - create a candidate for a new record and then |
| 564 | * redo the search */ |
David Howells | 843099c | 2016-04-07 17:23:37 +0100 | [diff] [blame] | 565 | candidate = rxrpc_alloc_connection(GFP_NOIO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 566 | if (!candidate) { |
| 567 | _leave(" = -ENOMEM"); |
| 568 | return ERR_PTR(-ENOMEM); |
| 569 | } |
| 570 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 571 | candidate->trans = trans; |
| 572 | candidate->proto.local = trans->local; |
| 573 | candidate->proto.epoch = sp->hdr.epoch; |
| 574 | candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 575 | candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED; |
| 576 | candidate->params.local = trans->local; |
| 577 | candidate->params.peer = trans->peer; |
| 578 | candidate->params.service_id = sp->hdr.serviceId; |
| 579 | candidate->security_ix = sp->hdr.securityIndex; |
| 580 | candidate->out_clientflag = 0; |
| 581 | candidate->state = RXRPC_CONN_SERVER; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 582 | if (candidate->params.service_id) |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 583 | candidate->state = RXRPC_CONN_SERVER_UNSECURED; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 584 | |
| 585 | write_lock_bh(&trans->conn_lock); |
| 586 | |
| 587 | pp = &trans->server_conns.rb_node; |
| 588 | p = NULL; |
| 589 | while (*pp) { |
| 590 | p = *pp; |
| 591 | conn = rb_entry(p, struct rxrpc_connection, node); |
| 592 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 593 | if (epoch < conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 594 | pp = &(*pp)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 595 | else if (epoch > conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 596 | pp = &(*pp)->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 597 | else if (cid < conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 598 | pp = &(*pp)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 599 | else if (cid > conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 600 | pp = &(*pp)->rb_right; |
| 601 | else |
| 602 | goto found_extant_second; |
| 603 | } |
| 604 | |
| 605 | /* we can now add the new candidate to the list */ |
| 606 | conn = candidate; |
| 607 | candidate = NULL; |
| 608 | rb_link_node(&conn->node, p, pp); |
| 609 | rb_insert_color(&conn->node, &trans->server_conns); |
| 610 | atomic_inc(&conn->trans->usage); |
| 611 | |
| 612 | write_unlock_bh(&trans->conn_lock); |
| 613 | |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 614 | write_lock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 615 | list_add_tail(&conn->link, &rxrpc_connections); |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 616 | write_unlock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 617 | |
| 618 | new = "new"; |
| 619 | |
| 620 | success: |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 621 | _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 622 | |
| 623 | _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage)); |
| 624 | return conn; |
| 625 | |
| 626 | /* we found the connection in the list immediately */ |
| 627 | found_extant_connection: |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 628 | if (sp->hdr.securityIndex != conn->security_ix) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 629 | read_unlock_bh(&trans->conn_lock); |
| 630 | goto security_mismatch; |
| 631 | } |
| 632 | atomic_inc(&conn->usage); |
| 633 | read_unlock_bh(&trans->conn_lock); |
| 634 | goto success; |
| 635 | |
| 636 | /* we found the connection on the second time through the list */ |
| 637 | found_extant_second: |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 638 | if (sp->hdr.securityIndex != conn->security_ix) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 639 | write_unlock_bh(&trans->conn_lock); |
| 640 | goto security_mismatch; |
| 641 | } |
| 642 | atomic_inc(&conn->usage); |
| 643 | write_unlock_bh(&trans->conn_lock); |
| 644 | kfree(candidate); |
| 645 | goto success; |
| 646 | |
| 647 | security_mismatch: |
| 648 | kfree(candidate); |
| 649 | _leave(" = -EKEYREJECTED"); |
| 650 | return ERR_PTR(-EKEYREJECTED); |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * find a connection based on transport and RxRPC connection ID for an incoming |
| 655 | * packet |
| 656 | */ |
| 657 | struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans, |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 658 | struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 659 | { |
| 660 | struct rxrpc_connection *conn; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 661 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 662 | struct rb_node *p; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 663 | u32 epoch, cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 664 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 665 | _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 666 | |
| 667 | read_lock_bh(&trans->conn_lock); |
| 668 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 669 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 670 | epoch = sp->hdr.epoch; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 671 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 672 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 673 | p = trans->server_conns.rb_node; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 674 | while (p) { |
| 675 | conn = rb_entry(p, struct rxrpc_connection, node); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 676 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 677 | _debug("maybe %x", conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 678 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 679 | if (epoch < conn->proto.epoch) |
| 680 | p = p->rb_left; |
| 681 | else if (epoch > conn->proto.epoch) |
| 682 | p = p->rb_right; |
| 683 | else if (cid < conn->proto.cid) |
| 684 | p = p->rb_left; |
| 685 | else if (cid > conn->proto.cid) |
| 686 | p = p->rb_right; |
| 687 | else |
| 688 | goto found; |
| 689 | } |
| 690 | } else { |
| 691 | conn = idr_find(&rxrpc_client_conn_ids, cid >> RXRPC_CIDSHIFT); |
| 692 | if (conn && conn->proto.epoch == epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 693 | goto found; |
| 694 | } |
| 695 | |
| 696 | read_unlock_bh(&trans->conn_lock); |
| 697 | _leave(" = NULL"); |
| 698 | return NULL; |
| 699 | |
| 700 | found: |
| 701 | atomic_inc(&conn->usage); |
| 702 | read_unlock_bh(&trans->conn_lock); |
| 703 | _leave(" = %p", conn); |
| 704 | return conn; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * release a virtual connection |
| 709 | */ |
| 710 | void rxrpc_put_connection(struct rxrpc_connection *conn) |
| 711 | { |
| 712 | _enter("%p{u=%d,d=%d}", |
| 713 | conn, atomic_read(&conn->usage), conn->debug_id); |
| 714 | |
| 715 | ASSERTCMP(atomic_read(&conn->usage), >, 0); |
| 716 | |
Ksenija Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 717 | conn->put_time = ktime_get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 718 | if (atomic_dec_and_test(&conn->usage)) { |
| 719 | _debug("zombie"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 720 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | _leave(""); |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * destroy a virtual connection |
| 728 | */ |
| 729 | static void rxrpc_destroy_connection(struct rxrpc_connection *conn) |
| 730 | { |
| 731 | _enter("%p{%d}", conn, atomic_read(&conn->usage)); |
| 732 | |
| 733 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 734 | |
| 735 | _net("DESTROY CONN %d", conn->debug_id); |
| 736 | |
| 737 | if (conn->bundle) |
| 738 | rxrpc_put_bundle(conn->trans, conn->bundle); |
| 739 | |
| 740 | ASSERT(RB_EMPTY_ROOT(&conn->calls)); |
| 741 | rxrpc_purge_queue(&conn->rx_queue); |
| 742 | |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 743 | conn->security->clear(conn); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 744 | key_put(conn->params.key); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 745 | key_put(conn->server_key); |
| 746 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 747 | rxrpc_put_transport(conn->trans); |
| 748 | kfree(conn); |
| 749 | _leave(""); |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * reap dead connections |
| 754 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 755 | static void rxrpc_connection_reaper(struct work_struct *work) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 756 | { |
| 757 | struct rxrpc_connection *conn, *_p; |
| 758 | unsigned long now, earliest, reap_time; |
| 759 | |
| 760 | LIST_HEAD(graveyard); |
| 761 | |
| 762 | _enter(""); |
| 763 | |
Ksenija Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 764 | now = ktime_get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 765 | earliest = ULONG_MAX; |
| 766 | |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 767 | write_lock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 768 | list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) { |
| 769 | _debug("reap CONN %d { u=%d,t=%ld }", |
| 770 | conn->debug_id, atomic_read(&conn->usage), |
| 771 | (long) now - (long) conn->put_time); |
| 772 | |
| 773 | if (likely(atomic_read(&conn->usage) > 0)) |
| 774 | continue; |
| 775 | |
| 776 | spin_lock(&conn->trans->client_lock); |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 777 | write_lock_bh(&conn->trans->conn_lock); |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 778 | reap_time = conn->put_time + rxrpc_connection_expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 779 | |
| 780 | if (atomic_read(&conn->usage) > 0) { |
| 781 | ; |
| 782 | } else if (reap_time <= now) { |
| 783 | list_move_tail(&conn->link, &graveyard); |
| 784 | if (conn->out_clientflag) |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame^] | 785 | rxrpc_put_client_connection_id(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 786 | else |
| 787 | rb_erase(&conn->node, |
| 788 | &conn->trans->server_conns); |
| 789 | if (conn->bundle) { |
| 790 | list_del_init(&conn->bundle_link); |
| 791 | conn->bundle->num_conns--; |
| 792 | } |
| 793 | |
| 794 | } else if (reap_time < earliest) { |
| 795 | earliest = reap_time; |
| 796 | } |
| 797 | |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 798 | write_unlock_bh(&conn->trans->conn_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 799 | spin_unlock(&conn->trans->client_lock); |
| 800 | } |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 801 | write_unlock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 802 | |
| 803 | if (earliest != ULONG_MAX) { |
| 804 | _debug("reschedule reaper %ld", (long) earliest - now); |
| 805 | ASSERTCMP(earliest, >, now); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 806 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, |
| 807 | (earliest - now) * HZ); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* then destroy all those pulled out */ |
| 811 | while (!list_empty(&graveyard)) { |
| 812 | conn = list_entry(graveyard.next, struct rxrpc_connection, |
| 813 | link); |
| 814 | list_del_init(&conn->link); |
| 815 | |
| 816 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 817 | rxrpc_destroy_connection(conn); |
| 818 | } |
| 819 | |
| 820 | _leave(""); |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * preemptively destroy all the connection records rather than waiting for them |
| 825 | * to time out |
| 826 | */ |
| 827 | void __exit rxrpc_destroy_all_connections(void) |
| 828 | { |
| 829 | _enter(""); |
| 830 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 831 | rxrpc_connection_expiry = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 832 | cancel_delayed_work(&rxrpc_connection_reap); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 833 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 834 | |
| 835 | _leave(""); |
| 836 | } |