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 /* 38 * We use machine-unique IDs for our client connections. 39 */ 40 DEFINE_IDR(rxrpc_client_conn_ids); 41 static DEFINE_SPINLOCK(rxrpc_conn_id_lock); 42 43 /* 44 * Get a connection ID and epoch for a client connection from the global pool. 45 * The connection struct pointer is then recorded in the idr radix tree. The 46 * epoch doesn't change until the client is rebooted (or, at least, unless the 47 * module is unloaded). 48 */ 49 static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn, 50 gfp_t gfp) 51 { 52 struct rxrpc_net *rxnet = conn->params.local->rxnet; 53 int id; 54 55 _enter(""); 56 57 idr_preload(gfp); 58 spin_lock(&rxrpc_conn_id_lock); 59 60 id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn, 61 1, 0x40000000, GFP_NOWAIT); 62 if (id < 0) 63 goto error; 64 65 spin_unlock(&rxrpc_conn_id_lock); 66 idr_preload_end(); 67 68 conn->proto.epoch = rxnet->epoch; 69 conn->proto.cid = id << RXRPC_CIDSHIFT; 70 set_bit(RXRPC_CONN_HAS_IDR, &conn->flags); 71 _leave(" [CID %x]", conn->proto.cid); 72 return 0; 73 74 error: 75 spin_unlock(&rxrpc_conn_id_lock); 76 idr_preload_end(); 77 _leave(" = %d", id); 78 return id; 79 } 80 81 /* 82 * Release a connection ID for a client connection from the global pool. 83 */ 84 static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn) 85 { 86 if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) { 87 spin_lock(&rxrpc_conn_id_lock); 88 idr_remove(&rxrpc_client_conn_ids, 89 conn->proto.cid >> RXRPC_CIDSHIFT); 90 spin_unlock(&rxrpc_conn_id_lock); 91 } 92 } 93 94 /* 95 * Destroy the client connection ID tree. 96 */ 97 void rxrpc_destroy_client_conn_ids(void) 98 { 99 struct rxrpc_connection *conn; 100 int id; 101 102 if (!idr_is_empty(&rxrpc_client_conn_ids)) { 103 idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) { 104 pr_err("AF_RXRPC: Leaked client conn %p {%d}\n", 105 conn, atomic_read(&conn->usage)); 106 } 107 BUG(); 108 } 109 110 idr_destroy(&rxrpc_client_conn_ids); 111 } 112 113 /* 114 * Allocate a connection bundle. 115 */ 116 static struct rxrpc_bundle *rxrpc_alloc_bundle(struct rxrpc_conn_parameters *cp, 117 gfp_t gfp) 118 { 119 struct rxrpc_bundle *bundle; 120 121 bundle = kzalloc(sizeof(*bundle), gfp); 122 if (bundle) { 123 bundle->params = *cp; 124 rxrpc_get_peer(bundle->params.peer); 125 atomic_set(&bundle->usage, 1); 126 spin_lock_init(&bundle->channel_lock); 127 INIT_LIST_HEAD(&bundle->waiting_calls); 128 } 129 return bundle; 130 } 131 132 struct rxrpc_bundle *rxrpc_get_bundle(struct rxrpc_bundle *bundle) 133 { 134 atomic_inc(&bundle->usage); 135 return bundle; 136 } 137 138 void rxrpc_put_bundle(struct rxrpc_bundle *bundle) 139 { 140 unsigned int d = bundle->debug_id; 141 unsigned int u = atomic_dec_return(&bundle->usage); 142 143 _debug("PUT B=%x %u", d, u); 144 if (u == 0) { 145 rxrpc_put_peer(bundle->params.peer); 146 kfree(bundle); 147 } 148 } 149 150 /* 151 * Allocate a client connection. 152 */ 153 static struct rxrpc_connection * 154 rxrpc_alloc_client_connection(struct rxrpc_bundle *bundle, gfp_t gfp) 155 { 156 struct rxrpc_connection *conn; 157 struct rxrpc_net *rxnet = bundle->params.local->rxnet; 158 int ret; 159 160 _enter(""); 161 162 conn = rxrpc_alloc_connection(gfp); 163 if (!conn) { 164 _leave(" = -ENOMEM"); 165 return ERR_PTR(-ENOMEM); 166 } 167 168 atomic_set(&conn->usage, 1); 169 conn->bundle = bundle; 170 conn->params = bundle->params; 171 conn->out_clientflag = RXRPC_CLIENT_INITIATED; 172 conn->state = RXRPC_CONN_CLIENT; 173 conn->service_id = conn->params.service_id; 174 175 ret = rxrpc_get_client_connection_id(conn, gfp); 176 if (ret < 0) 177 goto error_0; 178 179 ret = rxrpc_init_client_conn_security(conn); 180 if (ret < 0) 181 goto error_1; 182 183 ret = conn->security->prime_packet_security(conn); 184 if (ret < 0) 185 goto error_2; 186 187 atomic_inc(&rxnet->nr_conns); 188 write_lock(&rxnet->conn_lock); 189 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); 190 write_unlock(&rxnet->conn_lock); 191 192 rxrpc_get_bundle(bundle); 193 rxrpc_get_peer(conn->params.peer); 194 rxrpc_get_local(conn->params.local); 195 key_get(conn->params.key); 196 197 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_client, 198 atomic_read(&conn->usage), 199 __builtin_return_address(0)); 200 201 atomic_inc(&rxnet->nr_client_conns); 202 trace_rxrpc_client(conn, -1, rxrpc_client_alloc); 203 _leave(" = %p", conn); 204 return conn; 205 206 error_2: 207 conn->security->clear(conn); 208 error_1: 209 rxrpc_put_client_connection_id(conn); 210 error_0: 211 kfree(conn); 212 _leave(" = %d", ret); 213 return ERR_PTR(ret); 214 } 215 216 /* 217 * Determine if a connection may be reused. 218 */ 219 static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn) 220 { 221 struct rxrpc_net *rxnet; 222 int id_cursor, id, distance, limit; 223 224 if (!conn) 225 goto dont_reuse; 226 227 rxnet = conn->params.local->rxnet; 228 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags)) 229 goto dont_reuse; 230 231 if (conn->state != RXRPC_CONN_CLIENT || 232 conn->proto.epoch != rxnet->epoch) 233 goto mark_dont_reuse; 234 235 /* The IDR tree gets very expensive on memory if the connection IDs are 236 * widely scattered throughout the number space, so we shall want to 237 * kill off connections that, say, have an ID more than about four 238 * times the maximum number of client conns away from the current 239 * allocation point to try and keep the IDs concentrated. 240 */ 241 id_cursor = idr_get_cursor(&rxrpc_client_conn_ids); 242 id = conn->proto.cid >> RXRPC_CIDSHIFT; 243 distance = id - id_cursor; 244 if (distance < 0) 245 distance = -distance; 246 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024); 247 if (distance > limit) 248 goto mark_dont_reuse; 249 250 return true; 251 252 mark_dont_reuse: 253 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); 254 dont_reuse: 255 return false; 256 } 257 258 /* 259 * Look up the conn bundle that matches the connection parameters, adding it if 260 * it doesn't yet exist. 261 */ 262 static struct rxrpc_bundle *rxrpc_look_up_bundle(struct rxrpc_conn_parameters *cp, 263 gfp_t gfp) 264 { 265 static atomic_t rxrpc_bundle_id; 266 struct rxrpc_bundle *bundle, *candidate; 267 struct rxrpc_local *local = cp->local; 268 struct rb_node *p, **pp, *parent; 269 long diff; 270 271 _enter("{%px,%x,%u,%u}", 272 cp->peer, key_serial(cp->key), cp->security_level, cp->upgrade); 273 274 if (cp->exclusive) 275 return rxrpc_alloc_bundle(cp, gfp); 276 277 /* First, see if the bundle is already there. */ 278 _debug("search 1"); 279 spin_lock(&local->client_bundles_lock); 280 p = local->client_bundles.rb_node; 281 while (p) { 282 bundle = rb_entry(p, struct rxrpc_bundle, local_node); 283 284 #define cmp(X) ((long)bundle->params.X - (long)cp->X) 285 diff = (cmp(peer) ?: 286 cmp(key) ?: 287 cmp(security_level) ?: 288 cmp(upgrade)); 289 #undef cmp 290 if (diff < 0) 291 p = p->rb_left; 292 else if (diff > 0) 293 p = p->rb_right; 294 else 295 goto found_bundle; 296 } 297 spin_unlock(&local->client_bundles_lock); 298 _debug("not found"); 299 300 /* It wasn't. We need to add one. */ 301 candidate = rxrpc_alloc_bundle(cp, gfp); 302 if (!candidate) 303 return NULL; 304 305 _debug("search 2"); 306 spin_lock(&local->client_bundles_lock); 307 pp = &local->client_bundles.rb_node; 308 parent = NULL; 309 while (*pp) { 310 parent = *pp; 311 bundle = rb_entry(parent, struct rxrpc_bundle, local_node); 312 313 #define cmp(X) ((long)bundle->params.X - (long)cp->X) 314 diff = (cmp(peer) ?: 315 cmp(key) ?: 316 cmp(security_level) ?: 317 cmp(upgrade)); 318 #undef cmp 319 if (diff < 0) 320 pp = &(*pp)->rb_left; 321 else if (diff > 0) 322 pp = &(*pp)->rb_right; 323 else 324 goto found_bundle_free; 325 } 326 327 _debug("new bundle"); 328 candidate->debug_id = atomic_inc_return(&rxrpc_bundle_id); 329 rb_link_node(&candidate->local_node, parent, pp); 330 rb_insert_color(&candidate->local_node, &local->client_bundles); 331 rxrpc_get_bundle(candidate); 332 spin_unlock(&local->client_bundles_lock); 333 _leave(" = %u [new]", candidate->debug_id); 334 return candidate; 335 336 found_bundle_free: 337 kfree(candidate); 338 found_bundle: 339 rxrpc_get_bundle(bundle); 340 spin_unlock(&local->client_bundles_lock); 341 _leave(" = %u [found]", bundle->debug_id); 342 return bundle; 343 } 344 345 /* 346 * Create or find a client bundle to use for a call. 347 * 348 * If we return with a connection, the call will be on its waiting list. It's 349 * left to the caller to assign a channel and wake up the call. 350 */ 351 static struct rxrpc_bundle *rxrpc_prep_call(struct rxrpc_sock *rx, 352 struct rxrpc_call *call, 353 struct rxrpc_conn_parameters *cp, 354 struct sockaddr_rxrpc *srx, 355 gfp_t gfp) 356 { 357 struct rxrpc_bundle *bundle; 358 359 _enter("{%d,%lx},", call->debug_id, call->user_call_ID); 360 361 cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp); 362 if (!cp->peer) 363 goto error; 364 365 call->cong_cwnd = cp->peer->cong_cwnd; 366 if (call->cong_cwnd >= call->cong_ssthresh) 367 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE; 368 else 369 call->cong_mode = RXRPC_CALL_SLOW_START; 370 if (cp->upgrade) 371 __set_bit(RXRPC_CALL_UPGRADE, &call->flags); 372 373 /* Find the client connection bundle. */ 374 bundle = rxrpc_look_up_bundle(cp, gfp); 375 if (!bundle) 376 goto error; 377 378 /* Get this call queued. Someone else may activate it whilst we're 379 * lining up a new connection, but that's fine. 380 */ 381 spin_lock(&bundle->channel_lock); 382 list_add_tail(&call->chan_wait_link, &bundle->waiting_calls); 383 spin_unlock(&bundle->channel_lock); 384 385 _leave(" = [B=%x]", bundle->debug_id); 386 return bundle; 387 388 error: 389 _leave(" = -ENOMEM"); 390 return ERR_PTR(-ENOMEM); 391 } 392 393 /* 394 * Allocate a new connection and add it into a bundle. 395 */ 396 static void rxrpc_add_conn_to_bundle(struct rxrpc_bundle *bundle, gfp_t gfp) 397 __releases(bundle->channel_lock) 398 { 399 struct rxrpc_connection *candidate = NULL, *old = NULL; 400 bool conflict; 401 int i; 402 403 _enter(""); 404 405 conflict = bundle->alloc_conn; 406 if (!conflict) 407 bundle->alloc_conn = true; 408 spin_unlock(&bundle->channel_lock); 409 if (conflict) { 410 _leave(" [conf]"); 411 return; 412 } 413 414 candidate = rxrpc_alloc_client_connection(bundle, gfp); 415 416 spin_lock(&bundle->channel_lock); 417 bundle->alloc_conn = false; 418 419 if (IS_ERR(candidate)) { 420 bundle->alloc_error = PTR_ERR(candidate); 421 spin_unlock(&bundle->channel_lock); 422 _leave(" [err %ld]", PTR_ERR(candidate)); 423 return; 424 } 425 426 bundle->alloc_error = 0; 427 428 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) { 429 unsigned int shift = i * RXRPC_MAXCALLS; 430 int j; 431 432 old = bundle->conns[i]; 433 if (!rxrpc_may_reuse_conn(old)) { 434 if (old) 435 trace_rxrpc_client(old, -1, rxrpc_client_replace); 436 candidate->bundle_shift = shift; 437 bundle->conns[i] = candidate; 438 for (j = 0; j < RXRPC_MAXCALLS; j++) 439 set_bit(shift + j, &bundle->avail_chans); 440 candidate = NULL; 441 break; 442 } 443 444 old = NULL; 445 } 446 447 spin_unlock(&bundle->channel_lock); 448 449 if (candidate) { 450 _debug("discard C=%x", candidate->debug_id); 451 trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate); 452 rxrpc_put_connection(candidate); 453 } 454 455 rxrpc_put_connection(old); 456 _leave(""); 457 } 458 459 /* 460 * Add a connection to a bundle if there are no usable connections or we have 461 * connections waiting for extra capacity. 462 */ 463 static void rxrpc_maybe_add_conn(struct rxrpc_bundle *bundle, gfp_t gfp) 464 { 465 struct rxrpc_call *call; 466 int i, usable; 467 468 _enter(""); 469 470 spin_lock(&bundle->channel_lock); 471 472 /* See if there are any usable connections. */ 473 usable = 0; 474 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) 475 if (rxrpc_may_reuse_conn(bundle->conns[i])) 476 usable++; 477 478 if (!usable && !list_empty(&bundle->waiting_calls)) { 479 call = list_first_entry(&bundle->waiting_calls, 480 struct rxrpc_call, chan_wait_link); 481 if (test_bit(RXRPC_CALL_UPGRADE, &call->flags)) 482 bundle->try_upgrade = true; 483 } 484 485 if (!usable) 486 goto alloc_conn; 487 488 if (!bundle->avail_chans && 489 !bundle->try_upgrade && 490 !list_empty(&bundle->waiting_calls) && 491 usable < ARRAY_SIZE(bundle->conns)) 492 goto alloc_conn; 493 494 spin_unlock(&bundle->channel_lock); 495 _leave(""); 496 return; 497 498 alloc_conn: 499 return rxrpc_add_conn_to_bundle(bundle, gfp); 500 } 501 502 /* 503 * Assign a channel to the call at the front of the queue and wake the call up. 504 * We don't increment the callNumber counter until this number has been exposed 505 * to the world. 506 */ 507 static void rxrpc_activate_one_channel(struct rxrpc_connection *conn, 508 unsigned int channel) 509 { 510 struct rxrpc_channel *chan = &conn->channels[channel]; 511 struct rxrpc_bundle *bundle = conn->bundle; 512 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next, 513 struct rxrpc_call, chan_wait_link); 514 u32 call_id = chan->call_counter + 1; 515 516 _enter("C=%x,%u", conn->debug_id, channel); 517 518 trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate); 519 520 /* Cancel the final ACK on the previous call if it hasn't been sent yet 521 * as the DATA packet will implicitly ACK it. 522 */ 523 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); 524 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans); 525 526 rxrpc_see_call(call); 527 list_del_init(&call->chan_wait_link); 528 call->peer = rxrpc_get_peer(conn->params.peer); 529 call->conn = rxrpc_get_connection(conn); 530 call->cid = conn->proto.cid | channel; 531 call->call_id = call_id; 532 call->security = conn->security; 533 call->security_ix = conn->security_ix; 534 call->service_id = conn->service_id; 535 536 trace_rxrpc_connect_call(call); 537 _net("CONNECT call %08x:%08x as call %d on conn %d", 538 call->cid, call->call_id, call->debug_id, conn->debug_id); 539 540 write_lock_bh(&call->state_lock); 541 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST; 542 write_unlock_bh(&call->state_lock); 543 544 /* Paired with the read barrier in rxrpc_connect_call(). This orders 545 * cid and epoch in the connection wrt to call_id without the need to 546 * take the channel_lock. 547 * 548 * We provisionally assign a callNumber at this point, but we don't 549 * confirm it until the call is about to be exposed. 550 * 551 * TODO: Pair with a barrier in the data_ready handler when that looks 552 * at the call ID through a connection channel. 553 */ 554 smp_wmb(); 555 556 chan->call_id = call_id; 557 chan->call_debug_id = call->debug_id; 558 rcu_assign_pointer(chan->call, call); 559 wake_up(&call->waitq); 560 } 561 562 /* 563 * Remove a connection from the idle list if it's on it. 564 */ 565 static void rxrpc_unidle_conn(struct rxrpc_bundle *bundle, struct rxrpc_connection *conn) 566 { 567 struct rxrpc_net *rxnet = bundle->params.local->rxnet; 568 bool drop_ref; 569 570 if (!list_empty(&conn->cache_link)) { 571 drop_ref = false; 572 spin_lock(&rxnet->client_conn_cache_lock); 573 if (!list_empty(&conn->cache_link)) { 574 list_del_init(&conn->cache_link); 575 drop_ref = true; 576 } 577 spin_unlock(&rxnet->client_conn_cache_lock); 578 if (drop_ref) 579 rxrpc_put_connection(conn); 580 } 581 } 582 583 /* 584 * Assign channels and callNumbers to waiting calls with channel_lock 585 * held by caller. 586 */ 587 static void rxrpc_activate_channels_locked(struct rxrpc_bundle *bundle) 588 { 589 struct rxrpc_connection *conn; 590 unsigned long avail, mask; 591 unsigned int channel, slot; 592 593 if (bundle->try_upgrade) 594 mask = 1; 595 else 596 mask = ULONG_MAX; 597 598 while (!list_empty(&bundle->waiting_calls)) { 599 avail = bundle->avail_chans & mask; 600 if (!avail) 601 break; 602 channel = __ffs(avail); 603 clear_bit(channel, &bundle->avail_chans); 604 605 slot = channel / RXRPC_MAXCALLS; 606 conn = bundle->conns[slot]; 607 if (!conn) 608 break; 609 610 if (bundle->try_upgrade) 611 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags); 612 rxrpc_unidle_conn(bundle, conn); 613 614 channel &= (RXRPC_MAXCALLS - 1); 615 conn->act_chans |= 1 << channel; 616 rxrpc_activate_one_channel(conn, channel); 617 } 618 } 619 620 /* 621 * Assign channels and callNumbers to waiting calls. 622 */ 623 static void rxrpc_activate_channels(struct rxrpc_bundle *bundle) 624 { 625 _enter("B=%x", bundle->debug_id); 626 627 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans); 628 629 if (!bundle->avail_chans) 630 return; 631 632 spin_lock(&bundle->channel_lock); 633 rxrpc_activate_channels_locked(bundle); 634 spin_unlock(&bundle->channel_lock); 635 _leave(""); 636 } 637 638 /* 639 * Wait for a callNumber and a channel to be granted to a call. 640 */ 641 static int rxrpc_wait_for_channel(struct rxrpc_bundle *bundle, 642 struct rxrpc_call *call, gfp_t gfp) 643 { 644 DECLARE_WAITQUEUE(myself, current); 645 int ret = 0; 646 647 _enter("%d", call->debug_id); 648 649 if (!gfpflags_allow_blocking(gfp)) { 650 rxrpc_maybe_add_conn(bundle, gfp); 651 rxrpc_activate_channels(bundle); 652 ret = bundle->alloc_error ?: -EAGAIN; 653 goto out; 654 } 655 656 add_wait_queue_exclusive(&call->waitq, &myself); 657 for (;;) { 658 rxrpc_maybe_add_conn(bundle, gfp); 659 rxrpc_activate_channels(bundle); 660 ret = bundle->alloc_error; 661 if (ret < 0) 662 break; 663 664 switch (call->interruptibility) { 665 case RXRPC_INTERRUPTIBLE: 666 case RXRPC_PREINTERRUPTIBLE: 667 set_current_state(TASK_INTERRUPTIBLE); 668 break; 669 case RXRPC_UNINTERRUPTIBLE: 670 default: 671 set_current_state(TASK_UNINTERRUPTIBLE); 672 break; 673 } 674 if (READ_ONCE(call->state) != RXRPC_CALL_CLIENT_AWAIT_CONN) 675 break; 676 if ((call->interruptibility == RXRPC_INTERRUPTIBLE || 677 call->interruptibility == RXRPC_PREINTERRUPTIBLE) && 678 signal_pending(current)) { 679 ret = -ERESTARTSYS; 680 break; 681 } 682 schedule(); 683 } 684 remove_wait_queue(&call->waitq, &myself); 685 __set_current_state(TASK_RUNNING); 686 687 out: 688 _leave(" = %d", ret); 689 return ret; 690 } 691 692 /* 693 * find a connection for a call 694 * - called in process context with IRQs enabled 695 */ 696 int rxrpc_connect_call(struct rxrpc_sock *rx, 697 struct rxrpc_call *call, 698 struct rxrpc_conn_parameters *cp, 699 struct sockaddr_rxrpc *srx, 700 gfp_t gfp) 701 { 702 struct rxrpc_bundle *bundle; 703 struct rxrpc_net *rxnet = cp->local->rxnet; 704 int ret = 0; 705 706 _enter("{%d,%lx},", call->debug_id, call->user_call_ID); 707 708 rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper); 709 710 bundle = rxrpc_prep_call(rx, call, cp, srx, gfp); 711 if (IS_ERR(bundle)) { 712 ret = PTR_ERR(bundle); 713 goto out; 714 } 715 716 if (call->state == RXRPC_CALL_CLIENT_AWAIT_CONN) { 717 ret = rxrpc_wait_for_channel(bundle, call, gfp); 718 if (ret < 0) 719 goto wait_failed; 720 } 721 722 granted_channel: 723 /* Paired with the write barrier in rxrpc_activate_one_channel(). */ 724 smp_rmb(); 725 726 out_put_bundle: 727 rxrpc_put_bundle(bundle); 728 out: 729 _leave(" = %d", ret); 730 return ret; 731 732 wait_failed: 733 spin_lock(&bundle->channel_lock); 734 list_del_init(&call->chan_wait_link); 735 spin_unlock(&bundle->channel_lock); 736 737 if (call->state != RXRPC_CALL_CLIENT_AWAIT_CONN) { 738 ret = 0; 739 goto granted_channel; 740 } 741 742 trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed); 743 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 0, ret); 744 rxrpc_disconnect_client_call(bundle, call); 745 goto out_put_bundle; 746 } 747 748 /* 749 * Note that a call, and thus a connection, is about to be exposed to the 750 * world. 751 */ 752 void rxrpc_expose_client_call(struct rxrpc_call *call) 753 { 754 unsigned int channel = call->cid & RXRPC_CHANNELMASK; 755 struct rxrpc_connection *conn = call->conn; 756 struct rxrpc_channel *chan = &conn->channels[channel]; 757 758 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 759 /* Mark the call ID as being used. If the callNumber counter 760 * exceeds ~2 billion, we kill the connection after its 761 * outstanding calls have finished so that the counter doesn't 762 * wrap. 763 */ 764 chan->call_counter++; 765 if (chan->call_counter >= INT_MAX) 766 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); 767 trace_rxrpc_client(conn, channel, rxrpc_client_exposed); 768 } 769 } 770 771 /* 772 * Set the reap timer. 773 */ 774 static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet) 775 { 776 if (!rxnet->kill_all_client_conns) { 777 unsigned long now = jiffies; 778 unsigned long reap_at = now + rxrpc_conn_idle_client_expiry; 779 780 if (rxnet->live) 781 timer_reduce(&rxnet->client_conn_reap_timer, reap_at); 782 } 783 } 784 785 /* 786 * Disconnect a client call. 787 */ 788 void rxrpc_disconnect_client_call(struct rxrpc_bundle *bundle, struct rxrpc_call *call) 789 { 790 struct rxrpc_connection *conn; 791 struct rxrpc_channel *chan = NULL; 792 struct rxrpc_net *rxnet = bundle->params.local->rxnet; 793 unsigned int channel; 794 bool may_reuse; 795 u32 cid; 796 797 _enter("c=%x", call->debug_id); 798 799 spin_lock(&bundle->channel_lock); 800 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); 801 802 /* Calls that have never actually been assigned a channel can simply be 803 * discarded. 804 */ 805 conn = call->conn; 806 if (!conn) { 807 _debug("call is waiting"); 808 ASSERTCMP(call->call_id, ==, 0); 809 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags)); 810 list_del_init(&call->chan_wait_link); 811 goto out; 812 } 813 814 cid = call->cid; 815 channel = cid & RXRPC_CHANNELMASK; 816 chan = &conn->channels[channel]; 817 trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect); 818 819 if (rcu_access_pointer(chan->call) != call) { 820 spin_unlock(&bundle->channel_lock); 821 BUG(); 822 } 823 824 may_reuse = rxrpc_may_reuse_conn(conn); 825 826 /* If a client call was exposed to the world, we save the result for 827 * retransmission. 828 * 829 * We use a barrier here so that the call number and abort code can be 830 * read without needing to take a lock. 831 * 832 * TODO: Make the incoming packet handler check this and handle 833 * terminal retransmission without requiring access to the call. 834 */ 835 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 836 _debug("exposed %u,%u", call->call_id, call->abort_code); 837 __rxrpc_disconnect_call(conn, call); 838 839 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) { 840 trace_rxrpc_client(conn, channel, rxrpc_client_to_active); 841 bundle->try_upgrade = false; 842 if (may_reuse) 843 rxrpc_activate_channels_locked(bundle); 844 } 845 846 } 847 848 /* See if we can pass the channel directly to another call. */ 849 if (may_reuse && !list_empty(&bundle->waiting_calls)) { 850 trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass); 851 rxrpc_activate_one_channel(conn, channel); 852 goto out; 853 } 854 855 /* Schedule the final ACK to be transmitted in a short while so that it 856 * can be skipped if we find a follow-on call. The first DATA packet 857 * of the follow on call will implicitly ACK this call. 858 */ 859 if (call->completion == RXRPC_CALL_SUCCEEDED && 860 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { 861 unsigned long final_ack_at = jiffies + 2; 862 863 WRITE_ONCE(chan->final_ack_at, final_ack_at); 864 smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */ 865 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); 866 rxrpc_reduce_conn_timer(conn, final_ack_at); 867 } 868 869 /* Deactivate the channel. */ 870 rcu_assign_pointer(chan->call, NULL); 871 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans); 872 conn->act_chans &= ~(1 << channel); 873 874 /* If no channels remain active, then put the connection on the idle 875 * list for a short while. Give it a ref to stop it going away if it 876 * becomes unbundled. 877 */ 878 if (!conn->act_chans) { 879 trace_rxrpc_client(conn, channel, rxrpc_client_to_idle); 880 conn->idle_timestamp = jiffies; 881 882 rxrpc_get_connection(conn); 883 spin_lock(&rxnet->client_conn_cache_lock); 884 list_move_tail(&conn->cache_link, &rxnet->idle_client_conns); 885 spin_unlock(&rxnet->client_conn_cache_lock); 886 887 rxrpc_set_client_reap_timer(rxnet); 888 } 889 890 out: 891 spin_unlock(&bundle->channel_lock); 892 _leave(""); 893 return; 894 } 895 896 /* 897 * Remove a connection from a bundle. 898 */ 899 static void rxrpc_unbundle_conn(struct rxrpc_connection *conn) 900 { 901 struct rxrpc_bundle *bundle = conn->bundle; 902 struct rxrpc_local *local = bundle->params.local; 903 unsigned int bindex; 904 bool need_drop = false; 905 int i; 906 907 _enter("C=%x", conn->debug_id); 908 909 spin_lock(&bundle->channel_lock); 910 bindex = conn->bundle_shift / RXRPC_MAXCALLS; 911 if (bundle->conns[bindex] == conn) { 912 _debug("clear slot %u", bindex); 913 bundle->conns[bindex] = NULL; 914 for (i = 0; i < RXRPC_MAXCALLS; i++) 915 clear_bit(conn->bundle_shift + i, &bundle->avail_chans); 916 need_drop = true; 917 } 918 spin_unlock(&bundle->channel_lock); 919 920 /* If there are no more connections, remove the bundle */ 921 if (!bundle->avail_chans) { 922 _debug("maybe unbundle"); 923 spin_lock(&local->client_bundles_lock); 924 925 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) 926 if (bundle->conns[i]) 927 break; 928 if (i == ARRAY_SIZE(bundle->conns) && !bundle->params.exclusive) { 929 _debug("erase bundle"); 930 rb_erase(&bundle->local_node, &local->client_bundles); 931 } 932 933 spin_unlock(&local->client_bundles_lock); 934 if (i == ARRAY_SIZE(bundle->conns)) 935 rxrpc_put_bundle(bundle); 936 } 937 938 if (need_drop) 939 rxrpc_put_connection(conn); 940 _leave(""); 941 } 942 943 /* 944 * Clean up a dead client connection. 945 */ 946 static void rxrpc_kill_client_conn(struct rxrpc_connection *conn) 947 { 948 struct rxrpc_local *local = conn->params.local; 949 struct rxrpc_net *rxnet = local->rxnet; 950 951 _enter("C=%x", conn->debug_id); 952 953 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup); 954 atomic_dec(&rxnet->nr_client_conns); 955 956 rxrpc_put_client_connection_id(conn); 957 rxrpc_kill_connection(conn); 958 } 959 960 /* 961 * Clean up a dead client connections. 962 */ 963 void rxrpc_put_client_conn(struct rxrpc_connection *conn) 964 { 965 const void *here = __builtin_return_address(0); 966 unsigned int debug_id = conn->debug_id; 967 int n; 968 969 n = atomic_dec_return(&conn->usage); 970 trace_rxrpc_conn(debug_id, rxrpc_conn_put_client, n, here); 971 if (n <= 0) { 972 ASSERTCMP(n, >=, 0); 973 rxrpc_kill_client_conn(conn); 974 } 975 } 976 977 /* 978 * Discard expired client connections from the idle list. Each conn in the 979 * idle list has been exposed and holds an extra ref because of that. 980 * 981 * This may be called from conn setup or from a work item so cannot be 982 * considered non-reentrant. 983 */ 984 void rxrpc_discard_expired_client_conns(struct work_struct *work) 985 { 986 struct rxrpc_connection *conn; 987 struct rxrpc_net *rxnet = 988 container_of(work, struct rxrpc_net, client_conn_reaper); 989 unsigned long expiry, conn_expires_at, now; 990 unsigned int nr_conns; 991 992 _enter(""); 993 994 if (list_empty(&rxnet->idle_client_conns)) { 995 _leave(" [empty]"); 996 return; 997 } 998 999 /* Don't double up on the discarding */ 1000 if (!spin_trylock(&rxnet->client_conn_discard_lock)) { 1001 _leave(" [already]"); 1002 return; 1003 } 1004 1005 /* We keep an estimate of what the number of conns ought to be after 1006 * we've discarded some so that we don't overdo the discarding. 1007 */ 1008 nr_conns = atomic_read(&rxnet->nr_client_conns); 1009 1010 next: 1011 spin_lock(&rxnet->client_conn_cache_lock); 1012 1013 if (list_empty(&rxnet->idle_client_conns)) 1014 goto out; 1015 1016 conn = list_entry(rxnet->idle_client_conns.next, 1017 struct rxrpc_connection, cache_link); 1018 1019 if (!rxnet->kill_all_client_conns) { 1020 /* If the number of connections is over the reap limit, we 1021 * expedite discard by reducing the expiry timeout. We must, 1022 * however, have at least a short grace period to be able to do 1023 * final-ACK or ABORT retransmission. 1024 */ 1025 expiry = rxrpc_conn_idle_client_expiry; 1026 if (nr_conns > rxrpc_reap_client_connections) 1027 expiry = rxrpc_conn_idle_client_fast_expiry; 1028 if (conn->params.local->service_closed) 1029 expiry = rxrpc_closed_conn_expiry * HZ; 1030 1031 conn_expires_at = conn->idle_timestamp + expiry; 1032 1033 now = READ_ONCE(jiffies); 1034 if (time_after(conn_expires_at, now)) 1035 goto not_yet_expired; 1036 } 1037 1038 trace_rxrpc_client(conn, -1, rxrpc_client_discard); 1039 list_del_init(&conn->cache_link); 1040 1041 spin_unlock(&rxnet->client_conn_cache_lock); 1042 1043 rxrpc_unbundle_conn(conn); 1044 rxrpc_put_connection(conn); /* Drop the ->cache_link ref */ 1045 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 (!rxnet->kill_all_client_conns) 1059 timer_reduce(&rxnet->client_conn_reap_timer, conn_expires_at); 1060 1061 out: 1062 spin_unlock(&rxnet->client_conn_cache_lock); 1063 spin_unlock(&rxnet->client_conn_discard_lock); 1064 _leave(""); 1065 } 1066 1067 /* 1068 * Preemptively destroy all the client connection records rather than waiting 1069 * for them to time out 1070 */ 1071 void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet) 1072 { 1073 _enter(""); 1074 1075 spin_lock(&rxnet->client_conn_cache_lock); 1076 rxnet->kill_all_client_conns = true; 1077 spin_unlock(&rxnet->client_conn_cache_lock); 1078 1079 del_timer_sync(&rxnet->client_conn_reap_timer); 1080 1081 if (!rxrpc_queue_work(&rxnet->client_conn_reaper)) 1082 _debug("destroy: queue failed"); 1083 1084 _leave(""); 1085 } 1086 1087 /* 1088 * Clean up the client connections on a local endpoint. 1089 */ 1090 void rxrpc_clean_up_local_conns(struct rxrpc_local *local) 1091 { 1092 struct rxrpc_connection *conn, *tmp; 1093 struct rxrpc_net *rxnet = local->rxnet; 1094 LIST_HEAD(graveyard); 1095 1096 _enter(""); 1097 1098 spin_lock(&rxnet->client_conn_cache_lock); 1099 1100 list_for_each_entry_safe(conn, tmp, &rxnet->idle_client_conns, 1101 cache_link) { 1102 if (conn->params.local == local) { 1103 trace_rxrpc_client(conn, -1, rxrpc_client_discard); 1104 list_move(&conn->cache_link, &graveyard); 1105 } 1106 } 1107 1108 spin_unlock(&rxnet->client_conn_cache_lock); 1109 1110 while (!list_empty(&graveyard)) { 1111 conn = list_entry(graveyard.next, 1112 struct rxrpc_connection, cache_link); 1113 list_del_init(&conn->cache_link); 1114 rxrpc_unbundle_conn(conn); 1115 rxrpc_put_connection(conn); 1116 } 1117 1118 _leave(" [culled]"); 1119 } 1120