| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* RxRPC remote transport endpoint record management |
|---|
| 2 | 3 | * |
|---|
| 3 | 4 | * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 11 | 7 | |
|---|
| 12 | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| .. | .. |
|---|
| 125 | 121 | |
|---|
| 126 | 122 | hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) { |
|---|
| 127 | 123 | if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 && |
|---|
| 128 | | - atomic_read(&peer->usage) > 0) |
|---|
| 124 | + refcount_read(&peer->ref) > 0) |
|---|
| 129 | 125 | return peer; |
|---|
| 130 | 126 | } |
|---|
| 131 | 127 | |
|---|
| .. | .. |
|---|
| 144 | 140 | peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key); |
|---|
| 145 | 141 | if (peer) { |
|---|
| 146 | 142 | _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport); |
|---|
| 147 | | - _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage)); |
|---|
| 143 | + _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref)); |
|---|
| 148 | 144 | } |
|---|
| 149 | 145 | return peer; |
|---|
| 150 | 146 | } |
|---|
| .. | .. |
|---|
| 213 | 209 | */ |
|---|
| 214 | 210 | struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp) |
|---|
| 215 | 211 | { |
|---|
| 212 | + const void *here = __builtin_return_address(0); |
|---|
| 216 | 213 | struct rxrpc_peer *peer; |
|---|
| 217 | 214 | |
|---|
| 218 | 215 | _enter(""); |
|---|
| 219 | 216 | |
|---|
| 220 | 217 | peer = kzalloc(sizeof(struct rxrpc_peer), gfp); |
|---|
| 221 | 218 | if (peer) { |
|---|
| 222 | | - atomic_set(&peer->usage, 1); |
|---|
| 219 | + refcount_set(&peer->ref, 1); |
|---|
| 223 | 220 | peer->local = rxrpc_get_local(local); |
|---|
| 224 | 221 | INIT_HLIST_HEAD(&peer->error_targets); |
|---|
| 225 | 222 | peer->service_conns = RB_ROOT; |
|---|
| .. | .. |
|---|
| 228 | 225 | spin_lock_init(&peer->rtt_input_lock); |
|---|
| 229 | 226 | peer->debug_id = atomic_inc_return(&rxrpc_debug_id); |
|---|
| 230 | 227 | |
|---|
| 228 | + rxrpc_peer_init_rtt(peer); |
|---|
| 229 | + |
|---|
| 231 | 230 | if (RXRPC_TX_SMSS > 2190) |
|---|
| 232 | 231 | peer->cong_cwnd = 2; |
|---|
| 233 | 232 | else if (RXRPC_TX_SMSS > 1095) |
|---|
| 234 | 233 | peer->cong_cwnd = 3; |
|---|
| 235 | 234 | else |
|---|
| 236 | 235 | peer->cong_cwnd = 4; |
|---|
| 236 | + trace_rxrpc_peer(peer->debug_id, rxrpc_peer_new, 1, here); |
|---|
| 237 | 237 | } |
|---|
| 238 | 238 | |
|---|
| 239 | 239 | _leave(" = %p", peer); |
|---|
| .. | .. |
|---|
| 378 | 378 | |
|---|
| 379 | 379 | _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport); |
|---|
| 380 | 380 | |
|---|
| 381 | | - _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage)); |
|---|
| 381 | + _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref)); |
|---|
| 382 | 382 | return peer; |
|---|
| 383 | 383 | } |
|---|
| 384 | 384 | |
|---|
| .. | .. |
|---|
| 388 | 388 | struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer) |
|---|
| 389 | 389 | { |
|---|
| 390 | 390 | const void *here = __builtin_return_address(0); |
|---|
| 391 | | - int n; |
|---|
| 391 | + int r; |
|---|
| 392 | 392 | |
|---|
| 393 | | - n = atomic_inc_return(&peer->usage); |
|---|
| 394 | | - trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, n, here); |
|---|
| 393 | + __refcount_inc(&peer->ref, &r); |
|---|
| 394 | + trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, r + 1, here); |
|---|
| 395 | 395 | return peer; |
|---|
| 396 | 396 | } |
|---|
| 397 | 397 | |
|---|
| .. | .. |
|---|
| 401 | 401 | struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer) |
|---|
| 402 | 402 | { |
|---|
| 403 | 403 | const void *here = __builtin_return_address(0); |
|---|
| 404 | + int r; |
|---|
| 404 | 405 | |
|---|
| 405 | 406 | if (peer) { |
|---|
| 406 | | - int n = atomic_fetch_add_unless(&peer->usage, 1, 0); |
|---|
| 407 | | - if (n > 0) |
|---|
| 408 | | - trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, n + 1, here); |
|---|
| 407 | + if (__refcount_inc_not_zero(&peer->ref, &r)) |
|---|
| 408 | + trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, r + 1, here); |
|---|
| 409 | 409 | else |
|---|
| 410 | 410 | peer = NULL; |
|---|
| 411 | 411 | } |
|---|
| .. | .. |
|---|
| 436 | 436 | { |
|---|
| 437 | 437 | const void *here = __builtin_return_address(0); |
|---|
| 438 | 438 | unsigned int debug_id; |
|---|
| 439 | | - int n; |
|---|
| 439 | + bool dead; |
|---|
| 440 | + int r; |
|---|
| 440 | 441 | |
|---|
| 441 | 442 | if (peer) { |
|---|
| 442 | 443 | debug_id = peer->debug_id; |
|---|
| 443 | | - n = atomic_dec_return(&peer->usage); |
|---|
| 444 | | - trace_rxrpc_peer(debug_id, rxrpc_peer_put, n, here); |
|---|
| 445 | | - if (n == 0) |
|---|
| 444 | + dead = __refcount_dec_and_test(&peer->ref, &r); |
|---|
| 445 | + trace_rxrpc_peer(debug_id, rxrpc_peer_put, r - 1, here); |
|---|
| 446 | + if (dead) |
|---|
| 446 | 447 | __rxrpc_put_peer(peer); |
|---|
| 447 | 448 | } |
|---|
| 448 | 449 | } |
|---|
| .. | .. |
|---|
| 455 | 456 | { |
|---|
| 456 | 457 | const void *here = __builtin_return_address(0); |
|---|
| 457 | 458 | unsigned int debug_id = peer->debug_id; |
|---|
| 458 | | - int n; |
|---|
| 459 | + bool dead; |
|---|
| 460 | + int r; |
|---|
| 459 | 461 | |
|---|
| 460 | | - n = atomic_dec_return(&peer->usage); |
|---|
| 461 | | - trace_rxrpc_peer(debug_id, rxrpc_peer_put, n, here); |
|---|
| 462 | | - if (n == 0) { |
|---|
| 462 | + dead = __refcount_dec_and_test(&peer->ref, &r); |
|---|
| 463 | + trace_rxrpc_peer(debug_id, rxrpc_peer_put, r - 1, here); |
|---|
| 464 | + if (dead) { |
|---|
| 463 | 465 | hash_del_rcu(&peer->hash_link); |
|---|
| 464 | 466 | list_del_init(&peer->keepalive_link); |
|---|
| 465 | 467 | rxrpc_free_peer(peer); |
|---|
| .. | .. |
|---|
| 481 | 483 | hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) { |
|---|
| 482 | 484 | pr_err("Leaked peer %u {%u} %pISp\n", |
|---|
| 483 | 485 | peer->debug_id, |
|---|
| 484 | | - atomic_read(&peer->usage), |
|---|
| 486 | + refcount_read(&peer->ref), |
|---|
| 485 | 487 | &peer->srx.transport); |
|---|
| 486 | 488 | } |
|---|
| 487 | 489 | } |
|---|
| .. | .. |
|---|
| 503 | 505 | EXPORT_SYMBOL(rxrpc_kernel_get_peer); |
|---|
| 504 | 506 | |
|---|
| 505 | 507 | /** |
|---|
| 506 | | - * rxrpc_kernel_get_rtt - Get a call's peer RTT |
|---|
| 508 | + * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT |
|---|
| 507 | 509 | * @sock: The socket on which the call is in progress. |
|---|
| 508 | 510 | * @call: The call to query |
|---|
| 511 | + * @_srtt: Where to store the SRTT value. |
|---|
| 509 | 512 | * |
|---|
| 510 | | - * Get the call's peer RTT. |
|---|
| 513 | + * Get the call's peer smoothed RTT in uS. |
|---|
| 511 | 514 | */ |
|---|
| 512 | | -u64 rxrpc_kernel_get_rtt(struct socket *sock, struct rxrpc_call *call) |
|---|
| 515 | +bool rxrpc_kernel_get_srtt(struct socket *sock, struct rxrpc_call *call, |
|---|
| 516 | + u32 *_srtt) |
|---|
| 513 | 517 | { |
|---|
| 514 | | - return call->peer->rtt; |
|---|
| 518 | + struct rxrpc_peer *peer = call->peer; |
|---|
| 519 | + |
|---|
| 520 | + if (peer->rtt_count == 0) { |
|---|
| 521 | + *_srtt = 1000000; /* 1S */ |
|---|
| 522 | + return false; |
|---|
| 523 | + } |
|---|
| 524 | + |
|---|
| 525 | + *_srtt = call->peer->srtt_us >> 3; |
|---|
| 526 | + return true; |
|---|
| 515 | 527 | } |
|---|
| 516 | | -EXPORT_SYMBOL(rxrpc_kernel_get_rtt); |
|---|
| 528 | +EXPORT_SYMBOL(rxrpc_kernel_get_srtt); |
|---|