1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Client connection-specific management code. 3 * 4 * Copyright (C) 2016, 2020 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 * 7 * Client connections need to be cached for a little while after they've made a 8 * call so as to handle retransmitted DATA packets in case the server didn't 9 * receive the final ACK or terminating ABORT we sent it. 10 * 11 * There are flags of relevance to the cache: 12 * 13 * (2) DONT_REUSE - The connection should be discarded as soon as possible and 14 * should not be reused. This is set when an exclusive connection is used 15 * or a call ID counter overflows. 16 * 17 * The caching state may only be changed if the cache lock is held. 18 * 19 * There are two idle client connection expiry durations. If the total number 20 * of connections is below the reap threshold, we use the normal duration; if 21 * it's above, we use the fast duration. 22 */ 23 24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 25 26 #include <linux/slab.h> 27 #include <linux/idr.h> 28 #include <linux/timer.h> 29 #include <linux/sched/signal.h> 30 31 #include "ar-internal.h" 32 33 __read_mostly unsigned int rxrpc_reap_client_connections = 900; 34 __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ; 35 __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ; 36 37 static void rxrpc_activate_bundle(struct rxrpc_bundle *bundle) 38 { 39 atomic_inc(&bundle->active); 40 } 41 42 /* 43 * Release a connection ID for a client connection. 44 */ 45 static void rxrpc_put_client_connection_id(struct rxrpc_local *local, 46 struct rxrpc_connection *conn) 47 { 48 idr_remove(&local->conn_ids, conn->proto.cid >> RXRPC_CIDSHIFT); 49 } 50 51 /* 52 * Destroy the client connection ID tree. 53 */ 54 static void rxrpc_destroy_client_conn_ids(struct rxrpc_local *local) 55 { 56 struct rxrpc_connection *conn; 57 int id; 58 59 if (!idr_is_empty(&local->conn_ids)) { 60 idr_for_each_entry(&local->conn_ids, conn, id) { 61 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n", 62 conn, refcount_read(&conn->ref)); 63 } 64 BUG(); 65 } 66 67 idr_destroy(&local->conn_ids); 68 } 69 70 /* 71 * Allocate a connection bundle. 72 */ 73 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_call *call, 74 gfp_t gfp) 75 { 76 static atomic_t rxrpc_bundle_id; 77 struct rxrpc_bundle *bundle; 78 79 bundle = kzalloc(sizeof(*bundle), gfp); 80 if (bundle) { 81 bundle->local = call->local; 82 bundle->peer = rxrpc_get_peer(call->peer, rxrpc_peer_get_bundle); 83 bundle->key = key_get(call->key); 84 bundle->security = call->security; 85 bundle->exclusive = test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags); 86 bundle->upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags); 87 bundle->service_id = call->dest_srx.srx_service; 88 bundle->security_level = call->security_level; 89 bundle->debug_id = atomic_inc_return(&rxrpc_bundle_id); 90 refcount_set(&bundle->ref, 1); 91 atomic_set(&bundle->active, 1); 92 INIT_LIST_HEAD(&bundle->waiting_calls); 93 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new); 94 } 95 return bundle; 96 } 97 98 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle, 99 enum rxrpc_bundle_trace why) 100 { 101 int r; 102 103 __refcount_inc(&bundle->ref, &r); 104 trace_rxrpc_bundle(bundle->debug_id, r + 1, why); 105 return bundle; 106 } 107 108 static void rxrpc_free_bundle(struct rxrpc_bundle *bundle) 109 { 110 trace_rxrpc_bundle(bundle->debug_id, refcount_read(&bundle->ref), 111 rxrpc_bundle_free); 112 rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle); 113 key_put(bundle->key); 114 kfree(bundle); 115 } 116 117 void rxrpc_put_bundle(struct rxrpc_bundle *bundle, enum rxrpc_bundle_trace why) 118 { 119 unsigned int id; 120 bool dead; 121 int r; 122 123 if (bundle) { 124 id = bundle->debug_id; 125 dead = __refcount_dec_and_test(&bundle->ref, &r); 126 trace_rxrpc_bundle(id, r - 1, why); 127 if (dead) 128 rxrpc_free_bundle(bundle); 129 } 130 } 131 132 /* 133 * Get rid of outstanding client connection preallocations when a local 134 * endpoint is destroyed. 135 */ 136 void rxrpc_purge_client_connections(struct rxrpc_local *local) 137 { 138 rxrpc_destroy_client_conn_ids(local); 139 } 140 141 /* 142 * Allocate a client connection. 143 */ 144 static struct rxrpc_connection * 145 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle) 146 { 147 struct rxrpc_connection *conn; 148 struct rxrpc_local *local = bundle->local; 149 struct rxrpc_net *rxnet = local->rxnet; 150 int id; 151 152 _enter(""); 153 154 conn = rxrpc_alloc_connection(rxnet, GFP_ATOMIC | __GFP_NOWARN); 155 if (!conn) 156 return ERR_PTR(-ENOMEM); 157 158 id = idr_alloc_cyclic(&local->conn_ids, conn, 1, 0x40000000, 159 GFP_ATOMIC | __GFP_NOWARN); 160 if (id < 0) { 161 kfree(conn); 162 return ERR_PTR(id); 163 } 164 165 refcount_set(&conn->ref, 1); 166 conn->proto.cid = id << RXRPC_CIDSHIFT; 167 conn->proto.epoch = local->rxnet->epoch; 168 conn->out_clientflag = RXRPC_CLIENT_INITIATED; 169 conn->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn); 170 conn->local = rxrpc_get_local(bundle->local, rxrpc_local_get_client_conn); 171 conn->peer = rxrpc_get_peer(bundle->peer, rxrpc_peer_get_client_conn); 172 conn->key = key_get(bundle->key); 173 conn->security = bundle->security; 174 conn->exclusive = bundle->exclusive; 175 conn->upgrade = bundle->upgrade; 176 conn->orig_service_id = bundle->service_id; 177 conn->security_level = bundle->security_level; 178 conn->state = RXRPC_CONN_CLIENT_UNSECURED; 179 conn->service_id = conn->orig_service_id; 180 181 if (conn->security == &rxrpc_no_security) 182 conn->state = RXRPC_CONN_CLIENT; 183 184 atomic_inc(&rxnet->nr_conns); 185 write_lock(&rxnet->conn_lock); 186 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); 187 write_unlock(&rxnet->conn_lock); 188 189 rxrpc_see_connection(conn, rxrpc_conn_new_client); 190 191 atomic_inc(&rxnet->nr_client_conns); 192 trace_rxrpc_client(conn, -1, rxrpc_client_alloc); 193 return conn; 194 } 195 196 /* 197 * Determine if a connection may be reused. 198 */ 199 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn) 200 { 201 struct rxrpc_net *rxnet; 202 int id_cursor, id, distance, limit; 203 204 if (!conn) 205 goto dont_reuse; 206 207 rxnet = conn->rxnet; 208 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags)) 209 goto dont_reuse; 210 211 if ((conn->state != RXRPC_CONN_CLIENT_UNSECURED && 212 conn->state != RXRPC_CONN_CLIENT) || 213 conn->proto.epoch != rxnet->epoch) 214 goto mark_dont_reuse; 215 216 /* The IDR tree gets very expensive on memory if the connection IDs are 217 * widely scattered throughout the number space, so we shall want to 218 * kill off connections that, say, have an ID more than about four 219 * times the maximum number of client conns away from the current 220 * allocation point to try and keep the IDs concentrated. 221 */ 222 id_cursor = idr_get_cursor(&conn->local->conn_ids); 223 id = conn->proto.cid >> RXRPC_CIDSHIFT; 224 distance = id - id_cursor; 225 if (distance < 0) 226 distance = -distance; 227 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024); 228 if (distance > limit) 229 goto mark_dont_reuse; 230 231 return true; 232 233 mark_dont_reuse: 234 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); 235 dont_reuse: 236 return false; 237 } 238 239 /* 240 * Look up the conn bundle that matches the connection parameters, adding it if 241 * it doesn't yet exist. 242 */ 243 int rxrpc_look_up_bundle(struct rxrpc_call *call, gfp_t gfp) 244 { 245 struct rxrpc_bundle *bundle, *candidate; 246 struct rxrpc_local *local = call->local; 247 struct rb_node *p, **pp, *parent; 248 long diff; 249 bool upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags); 250 251 _enter("{%px,%x,%u,%u}", 252 call->peer, key_serial(call->key), call->security_level, 253 upgrade); 254 255 if (test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags)) { 256 call->bundle = rxrpc_alloc_bundle(call, gfp); 257 return call->bundle ? 0 : -ENOMEM; 258 } 259 260 /* First, see if the bundle is already there. */ 261 _debug("search 1"); 262 spin_lock(&local->client_bundles_lock); 263 p = local->client_bundles.rb_node; 264 while (p) { 265 bundle = rb_entry(p, struct rxrpc_bundle, local_node); 266 267 #define cmp(X, Y) ((long)(X) - (long)(Y)) 268 diff = (cmp(bundle->peer, call->peer) ?: 269 cmp(bundle->key, call->key) ?: 270 cmp(bundle->security_level, call->security_level) ?: 271 cmp(bundle->upgrade, upgrade)); 272 #undef cmp 273 if (diff < 0) 274 p = p->rb_left; 275 else if (diff > 0) 276 p = p->rb_right; 277 else 278 goto found_bundle; 279 } 280 spin_unlock(&local->client_bundles_lock); 281 _debug("not found"); 282 283 /* It wasn't. We need to add one. */ 284 candidate = rxrpc_alloc_bundle(call, gfp); 285 if (!candidate) 286 return -ENOMEM; 287 288 _debug("search 2"); 289 spin_lock(&local->client_bundles_lock); 290 pp = &local->client_bundles.rb_node; 291 parent = NULL; 292 while (*pp) { 293 parent = *pp; 294 bundle = rb_entry(parent, struct rxrpc_bundle, local_node); 295 296 #define cmp(X, Y) ((long)(X) - (long)(Y)) 297 diff = (cmp(bundle->peer, call->peer) ?: 298 cmp(bundle->key, call->key) ?: 299 cmp(bundle->security_level, call->security_level) ?: 300 cmp(bundle->upgrade, upgrade)); 301 #undef cmp 302 if (diff < 0) 303 pp = &(*pp)->rb_left; 304 else if (diff > 0) 305 pp = &(*pp)->rb_right; 306 else 307 goto found_bundle_free; 308 } 309 310 _debug("new bundle"); 311 rb_link_node(&candidate->local_node, parent, pp); 312 rb_insert_color(&candidate->local_node, &local->client_bundles); 313 call->bundle = rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call); 314 spin_unlock(&local->client_bundles_lock); 315 _leave(" = B=%u [new]", call->bundle->debug_id); 316 return 0; 317 318 found_bundle_free: 319 rxrpc_free_bundle(candidate); 320 found_bundle: 321 call->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call); 322 rxrpc_activate_bundle(bundle); 323 spin_unlock(&local->client_bundles_lock); 324 _leave(" = B=%u [found]", call->bundle->debug_id); 325 return 0; 326 } 327 328 /* 329 * Allocate a new connection and add it into a bundle. 330 */ 331 static bool rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, 332 unsigned int slot) 333 { 334 struct rxrpc_connection *conn, *old; 335 unsigned int shift = slot * RXRPC_MAXCALLS; 336 unsigned int i; 337 338 old = bundle->conns[slot]; 339 if (old) { 340 bundle->conns[slot] = NULL; 341 trace_rxrpc_client(old, -1, rxrpc_client_replace); 342 rxrpc_put_connection(old, rxrpc_conn_put_noreuse); 343 } 344 345 conn = rxrpc_alloc_client_connection(bundle); 346 if (IS_ERR(conn)) { 347 bundle->alloc_error = PTR_ERR(conn); 348 return false; 349 } 350 351 rxrpc_activate_bundle(bundle); 352 conn->bundle_shift = shift; 353 bundle->conns[slot] = conn; 354 for (i = 0; i < RXRPC_MAXCALLS; i++) 355 set_bit(shift + i, &bundle->avail_chans); 356 return true; 357 } 358 359 /* 360 * Add a connection to a bundle if there are no usable connections or we have 361 * connections waiting for extra capacity. 362 */ 363 static bool rxrpc_bundle_has_space(struct rxrpc_bundle *bundle) 364 { 365 int slot = -1, i, usable; 366 367 _enter(""); 368 369 bundle->alloc_error = 0; 370 371 /* See if there are any usable connections. */ 372 usable = 0; 373 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) { 374 if (rxrpc_may_reuse_conn(bundle->conns[i])) 375 usable++; 376 else if (slot == -1) 377 slot = i; 378 } 379 380 if (!usable && bundle->upgrade) 381 bundle->try_upgrade = true; 382 383 if (!usable) 384 goto alloc_conn; 385 386 if (!bundle->avail_chans && 387 !bundle->try_upgrade && 388 usable < ARRAY_SIZE(bundle->conns)) 389 goto alloc_conn; 390 391 _leave(""); 392 return usable; 393 394 alloc_conn: 395 return slot >= 0 ? rxrpc_add_conn_to_bundle(bundle, slot) : false; 396 } 397 398 /* 399 * Assign a channel to the call at the front of the queue and wake the call up. 400 * We don't increment the callNumber counter until this number has been exposed 401 * to the world. 402 */ 403 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn, 404 unsigned int channel) 405 { 406 struct rxrpc_channel *chan = &conn->channels[channel]; 407 struct rxrpc_bundle *bundle = conn->bundle; 408 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next, 409 struct rxrpc_call, wait_link); 410 u32 call_id = chan->call_counter + 1; 411 412 _enter("C=%x,%u", conn->debug_id, channel); 413 414 list_del_init(&call->wait_link); 415 416 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate); 417 418 /* Cancel the final ACK on the previous call if it hasn't been sent yet 419 * as the DATA packet will implicitly ACK it. 420 */ 421 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); 422 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans); 423 424 rxrpc_see_call(call, rxrpc_call_see_activate_client); 425 call->conn = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call); 426 call->cid = conn->proto.cid | channel; 427 call->call_id = call_id; 428 call->dest_srx.srx_service = conn->service_id; 429 call->cong_ssthresh = call->peer->cong_ssthresh; 430 if (call->cong_cwnd >= call->cong_ssthresh) 431 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE; 432 else 433 call->cong_mode = RXRPC_CALL_SLOW_START; 434 435 chan->call_id = call_id; 436 chan->call_debug_id = call->debug_id; 437 chan->call = call; 438 439 rxrpc_see_call(call, rxrpc_call_see_connected); 440 trace_rxrpc_connect_call(call); 441 call->tx_last_sent = ktime_get_real(); 442 rxrpc_start_call_timer(call); 443 rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_SEND_REQUEST); 444 wake_up(&call->waitq); 445 } 446 447 /* 448 * Remove a connection from the idle list if it's on it. 449 */ 450 static void rxrpc_unidle_conn(struct rxrpc_connection *conn) 451 { 452 if (!list_empty(&conn->cache_link)) { 453 list_del_init(&conn->cache_link); 454 rxrpc_put_connection(conn, rxrpc_conn_put_unidle); 455 } 456 } 457 458 /* 459 * Assign channels and callNumbers to waiting calls. 460 */ 461 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle) 462 { 463 struct rxrpc_connection *conn; 464 unsigned long avail, mask; 465 unsigned int channel, slot; 466 467 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans); 468 469 if (bundle->try_upgrade) 470 mask = 1; 471 else 472 mask = ULONG_MAX; 473 474 while (!list_empty(&bundle->waiting_calls)) { 475 avail = bundle->avail_chans & mask; 476 if (!avail) 477 break; 478 channel = __ffs(avail); 479 clear_bit(channel, &bundle->avail_chans); 480 481 slot = channel / RXRPC_MAXCALLS; 482 conn = bundle->conns[slot]; 483 if (!conn) 484 break; 485 486 if (bundle->try_upgrade) 487 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags); 488 rxrpc_unidle_conn(conn); 489 490 channel &= (RXRPC_MAXCALLS - 1); 491 conn->act_chans |= 1 << channel; 492 rxrpc_activate_one_channel(conn, channel); 493 } 494 } 495 496 /* 497 * Connect waiting channels (called from the I/O thread). 498 */ 499 void rxrpc_connect_client_calls(struct rxrpc_local *local) 500 { 501 struct rxrpc_call *call; 502 503 while ((call = list_first_entry_or_null(&local->new_client_calls, 504 struct rxrpc_call, wait_link)) 505 ) { 506 struct rxrpc_bundle *bundle = call->bundle; 507 508 spin_lock(&local->client_call_lock); 509 list_move_tail(&call->wait_link, &bundle->waiting_calls); 510 rxrpc_see_call(call, rxrpc_call_see_waiting_call); 511 spin_unlock(&local->client_call_lock); 512 513 if (rxrpc_bundle_has_space(bundle)) 514 rxrpc_activate_channels(bundle); 515 } 516 } 517 518 /* 519 * Note that a call, and thus a connection, is about to be exposed to the 520 * world. 521 */ 522 void rxrpc_expose_client_call(struct rxrpc_call *call) 523 { 524 unsigned int channel = call->cid & RXRPC_CHANNELMASK; 525 struct rxrpc_connection *conn = call->conn; 526 struct rxrpc_channel *chan = &conn->channels[channel]; 527 528 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 529 /* Mark the call ID as being used. If the callNumber counter 530 * exceeds ~2 billion, we kill the connection after its 531 * outstanding calls have finished so that the counter doesn't 532 * wrap. 533 */ 534 chan->call_counter++; 535 if (chan->call_counter >= INT_MAX) 536 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); 537 trace_rxrpc_client(conn, channel, rxrpc_client_exposed); 538 539 spin_lock(&call->peer->lock); 540 hlist_add_head(&call->error_link, &call->peer->error_targets); 541 spin_unlock(&call->peer->lock); 542 } 543 } 544 545 /* 546 * Set the reap timer. 547 */ 548 static void rxrpc_set_client_reap_timer(struct rxrpc_local *local) 549 { 550 if (!local->kill_all_client_conns) { 551 unsigned long now = jiffies; 552 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry; 553 554 if (local->rxnet->live) 555 timer_reduce(&local->client_conn_reap_timer, reap_at); 556 } 557 } 558 559 /* 560 * Disconnect a client call. 561 */ 562 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call) 563 { 564 struct rxrpc_connection *conn; 565 struct rxrpc_channel *chan = NULL; 566 struct rxrpc_local *local = bundle->local; 567 unsigned int channel; 568 bool may_reuse; 569 u32 cid; 570 571 _enter("c=%x", call->debug_id); 572 573 /* Calls that have never actually been assigned a channel can simply be 574 * discarded. 575 */ 576 conn = call->conn; 577 if (!conn) { 578 _debug("call is waiting"); 579 ASSERTCMP(call->call_id, ==, 0); 580 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags)); 581 /* May still be on ->new_client_calls. */ 582 spin_lock(&local->client_call_lock); 583 list_del_init(&call->wait_link); 584 spin_unlock(&local->client_call_lock); 585 return; 586 } 587 588 cid = call->cid; 589 channel = cid & RXRPC_CHANNELMASK; 590 chan = &conn->channels[channel]; 591 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect); 592 593 if (WARN_ON(chan->call != call)) 594 return; 595 596 may_reuse = rxrpc_may_reuse_conn(conn); 597 598 /* If a client call was exposed to the world, we save the result for 599 * retransmission. 600 * 601 * We use a barrier here so that the call number and abort code can be 602 * read without needing to take a lock. 603 * 604 * TODO: Make the incoming packet handler check this and handle 605 * terminal retransmission without requiring access to the call. 606 */ 607 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 608 _debug("exposed %u,%u", call->call_id, call->abort_code); 609 __rxrpc_disconnect_call(conn, call); 610 611 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) { 612 trace_rxrpc_client(conn, channel, rxrpc_client_to_active); 613 bundle->try_upgrade = false; 614 if (may_reuse) 615 rxrpc_activate_channels(bundle); 616 } 617 } 618 619 /* See if we can pass the channel directly to another call. */ 620 if (may_reuse && !list_empty(&bundle->waiting_calls)) { 621 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass); 622 rxrpc_activate_one_channel(conn, channel); 623 return; 624 } 625 626 /* Schedule the final ACK to be transmitted in a short while so that it 627 * can be skipped if we find a follow-on call. The first DATA packet 628 * of the follow on call will implicitly ACK this call. 629 */ 630 if (call->completion == RXRPC_CALL_SUCCEEDED && 631 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 632 unsigned long final_ack_at = jiffies + 2; 633 634 WRITE_ONCE(chan->final_ack_at, final_ack_at); 635 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */ 636 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); 637 rxrpc_reduce_conn_timer(conn, final_ack_at); 638 } 639 640 /* Deactivate the channel. */ 641 chan->call = NULL; 642 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans); 643 conn->act_chans &= ~(1 << channel); 644 645 /* If no channels remain active, then put the connection on the idle 646 * list for a short while. Give it a ref to stop it going away if it 647 * becomes unbundled. 648 */ 649 if (!conn->act_chans) { 650 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle); 651 conn->idle_timestamp = jiffies; 652 653 rxrpc_get_connection(conn, rxrpc_conn_get_idle); 654 list_move_tail(&conn->cache_link, &local->idle_client_conns); 655 656 rxrpc_set_client_reap_timer(local); 657 } 658 } 659 660 /* 661 * Remove a connection from a bundle. 662 */ 663 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn) 664 { 665 struct rxrpc_bundle *bundle = conn->bundle; 666 unsigned int bindex; 667 int i; 668 669 _enter("C=%x", conn->debug_id); 670 671 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) 672 rxrpc_process_delayed_final_acks(conn, true); 673 674 bindex = conn->bundle_shift / RXRPC_MAXCALLS; 675 if (bundle->conns[bindex] == conn) { 676 _debug("clear slot %u", bindex); 677 bundle->conns[bindex] = NULL; 678 for (i = 0; i < RXRPC_MAXCALLS; i++) 679 clear_bit(conn->bundle_shift + i, &bundle->avail_chans); 680 rxrpc_put_client_connection_id(bundle->local, conn); 681 rxrpc_deactivate_bundle(bundle); 682 rxrpc_put_connection(conn, rxrpc_conn_put_unbundle); 683 } 684 } 685 686 /* 687 * Drop the active count on a bundle. 688 */ 689 void rxrpc_deactivate_bundle(struct rxrpc_bundle *bundle) 690 { 691 struct rxrpc_local *local; 692 bool need_put = false; 693 694 if (!bundle) 695 return; 696 697 local = bundle->local; 698 if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) { 699 if (!bundle->exclusive) { 700 _debug("erase bundle"); 701 rb_erase(&bundle->local_node, &local->client_bundles); 702 need_put = true; 703 } 704 705 spin_unlock(&local->client_bundles_lock); 706 if (need_put) 707 rxrpc_put_bundle(bundle, rxrpc_bundle_put_discard); 708 } 709 } 710 711 /* 712 * Clean up a dead client connection. 713 */ 714 void rxrpc_kill_client_conn(struct rxrpc_connection *conn) 715 { 716 struct rxrpc_local *local = conn->local; 717 struct rxrpc_net *rxnet = local->rxnet; 718 719 _enter("C=%x", conn->debug_id); 720 721 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup); 722 atomic_dec(&rxnet->nr_client_conns); 723 724 rxrpc_put_client_connection_id(local, conn); 725 } 726 727 /* 728 * Discard expired client connections from the idle list. Each conn in the 729 * idle list has been exposed and holds an extra ref because of that. 730 * 731 * This may be called from conn setup or from a work item so cannot be 732 * considered non-reentrant. 733 */ 734 void rxrpc_discard_expired_client_conns(struct rxrpc_local *local) 735 { 736 struct rxrpc_connection *conn; 737 unsigned long expiry, conn_expires_at, now; 738 unsigned int nr_conns; 739 740 _enter(""); 741 742 /* We keep an estimate of what the number of conns ought to be after 743 * we've discarded some so that we don't overdo the discarding. 744 */ 745 nr_conns = atomic_read(&local->rxnet->nr_client_conns); 746 747 next: 748 conn = list_first_entry_or_null(&local->idle_client_conns, 749 struct rxrpc_connection, cache_link); 750 if (!conn) 751 return; 752 753 if (!local->kill_all_client_conns) { 754 /* If the number of connections is over the reap limit, we 755 * expedite discard by reducing the expiry timeout. We must, 756 * however, have at least a short grace period to be able to do 757 * final-ACK or ABORT retransmission. 758 */ 759 expiry = rxrpc_conn_idle_client_expiry; 760 if (nr_conns > rxrpc_reap_client_connections) 761 expiry = rxrpc_conn_idle_client_fast_expiry; 762 if (conn->local->service_closed) 763 expiry = rxrpc_closed_conn_expiry * HZ; 764 765 conn_expires_at = conn->idle_timestamp + expiry; 766 767 now = READ_ONCE(jiffies); 768 if (time_after(conn_expires_at, now)) 769 goto not_yet_expired; 770 } 771 772 atomic_dec(&conn->active); 773 trace_rxrpc_client(conn, -1, rxrpc_client_discard); 774 list_del_init(&conn->cache_link); 775 776 rxrpc_unbundle_conn(conn); 777 /* Drop the ->cache_link ref */ 778 rxrpc_put_connection(conn, rxrpc_conn_put_discard_idle); 779 780 nr_conns--; 781 goto next; 782 783 not_yet_expired: 784 /* The connection at the front of the queue hasn't yet expired, so 785 * schedule the work item for that point if we discarded something. 786 * 787 * We don't worry if the work item is already scheduled - it can look 788 * after rescheduling itself at a later time. We could cancel it, but 789 * then things get messier. 790 */ 791 _debug("not yet"); 792 if (!local->kill_all_client_conns) 793 timer_reduce(&local->client_conn_reap_timer, conn_expires_at); 794 795 _leave(""); 796 } 797 798 /* 799 * Clean up the client connections on a local endpoint. 800 */ 801 void rxrpc_clean_up_local_conns(struct rxrpc_local *local) 802 { 803 struct rxrpc_connection *conn; 804 805 _enter(""); 806 807 local->kill_all_client_conns = true; 808 809 del_timer_sync(&local->client_conn_reap_timer); 810 811 while ((conn = list_first_entry_or_null(&local->idle_client_conns, 812 struct rxrpc_connection, cache_link))) { 813 list_del_init(&conn->cache_link); 814 atomic_dec(&conn->active); 815 trace_rxrpc_client(conn, -1, rxrpc_client_discard); 816 rxrpc_unbundle_conn(conn); 817 rxrpc_put_connection(conn, rxrpc_conn_put_local_dead); 818 } 819 820 _leave(" [culled]"); 821 } 822