tcp: remove one ktime_get() from recvmsg() fast path
Subsystems (3)
PATCH 1/1 tcp: remove one ktime_get() from recvmsg() fast path
Each time some payload is consumed by user space (recvmsg() and friends), TCP calls tcp_rcv_space_adjust() to run DRS algorithm to check if an increase of sk->sk_rcvbuf is needed.
This function is based on time sampling, and currently calls tcp_mstamp_refresh(tp), which is a wrapper around ktime_get_ns().
ktime_get_ns() has a high cost on some platforms. 100+ cycles for rdtscp on AMD EPYC Turin for instance.
We do not have to refresh tp->tcp_mpstamp, using the last cached value is enough. We only need to refresh it from __tcp_cleanup_rbuf() if an ACK must be sent (this is a rare event).
--- net/ipv4/tcp.c | 4 +++- net/ipv4/tcp_input.c | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-)
net/ipv4/tcp.c +3 -1
@@ -1556,8 +1556,10 @@ void __tcp_cleanup_rbuf(struct sock *sk, int copied) time_to_ack = true; } } -if (time_to_ack) +if (time_to_ack) { +tcp_mstamp_refresh (tp);tcp_send_ack (sk); + } }void tcp_cleanup_rbuf (struct sock *sk,int copied)
net/ipv4/tcp_input.c +8 -2
@@ -928,9 +928,15 @@ void tcp_rcv_space_adjust(struct sock *sk)trace_tcp_rcv_space_adjust (sk); -tcp_mstamp_refresh (tp); +if (unlikely (!tp->rcv_rtt_est.rtt_us)) +return ; + +/* We do not refresh tp->tcp_mstamp here. +* Some platforms have expensive ktime_get() implementations. +* Using the last cached value is enough for DRS. +*/ time =tcp_stamp_us_delta (tp->tcp_mstamp, tp->rcvq_space.time); -if (time < (tp->rcv_rtt_est.rtt_us >>3 ) || tp->rcv_rtt_est.rtt_us ==0 ) +if (time < (tp->rcv_rtt_est.rtt_us >>3 ))return ;/* Number of bytes copied to user in last RTT */
Signature · 1 line
2.51.1.821.gb6fe4d2222-goog
Quoted 8 lines · Eric Dumazet
Each time some payload is consumed by user space (
recvmsg()and friends), TCP callstcp_rcv_space_adjust()to run DRS algorithm to check if an increase ofsk->sk_rcvbufis needed.This function is based on time sampling, and currently calls
tcp_mstamp_refresh(tp), which is a wrapper aroundktime_get_ns().
Quoted 8 lines
ktime_get_ns()has a high cost on some platforms. 100+ cycles for rdtscp on AMD EPYC Turin for instance.We do not have to refresh
tp->tcp_mpstamp, using the last cached value is enough. We only need to refresh it from__tcp_cleanup_rbuf()if an ACK must be sent (this is a rare event).Signed-off-by Eric Dumazet <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Fri, 24 Oct 2025 12:07:07 +0000 you wrote:
Each time some payload is consumed by user space (
recvmsg()and friends), TCP callstcp_rcv_space_adjust()to run DRS algorithm to check if an increase ofsk->sk_rcvbufis needed.This function is based on time sampling, and currently calls
tcp_mstamp_refresh(tp), which is a wrapper aroundktime_get_ns().[...]
Here is the summary with links:
- [net-next] tcp: remove one ktime_get() from recvmsg() fast path
https://git.kernel.org/netdev/net-next/c/0ae1ac7335ca
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html