From: Wesley Atwell <[email protected]> Factor the core receive-memory byte accounting into small helpers so window selection, pressure checks, and prune decisions all start from one set of quantities.
This is preparatory only. Later patches will use the same helpers when tying sender-visible receive-window state back to hard memory admission. Signed-off-by: Wesley Atwell <[email protected]> --- include/net/tcp.h | 32 +++++++++++++++++++++++++++----- net/ipv4/tcp_input.c | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index f87bdacb5a69..3a0060599afe 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1751,12 +1751,34 @@ static inline void tcp_scaling_ratio_init(struct sock *sk) tcp_sk(sk)->scaling_ratio = TCP_DEFAULT_SCALING_RATIO; } +/* TCP receive-side accounting reuses sk_rcvbuf as both a hard memory limit + * and as the source material for the advertised receive window after + * scaling_ratio conversion. Keep the byte accounting explicit so admission, + * pruning, and rwnd selection all start from the same quantities. + */ +static inline int tcp_rmem_used(const struct sock *sk) +{ + return atomic_read(&sk->sk_rmem_alloc); +} + +static inline int tcp_rmem_avail(const struct sock *sk) +{ + return READ_ONCE(sk->sk_rcvbuf) - tcp_rmem_used(sk); +} + +/* Sender-visible rwnd headroom also reserves bytes already queued on backlog. + * Those bytes are not free to advertise again until __release_sock() drains + * backlog and clears sk_backlog.len. + */ +static inline int tcp_rwnd_avail(const struct sock *sk) +{ + return tcp_rmem_avail(sk) - READ_ONCE(sk->sk_backlog.len); +} + /* Note: caller must be prepared to deal with negative returns */ static inline int tcp_space(const struct sock *sk) { - return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) - - READ_ONCE(sk->sk_backlog.len) - - atomic_read(&sk->sk_rmem_alloc)); + return tcp_win_from_space(sk, tcp_rwnd_avail(sk)); } static inline int tcp_full_space(const struct sock *sk) @@ -1799,7 +1821,7 @@ static inline bool tcp_rmem_pressure(const struct sock *sk) rcvbuf = READ_ONCE(sk->sk_rcvbuf); threshold = rcvbuf - (rcvbuf >> 3); - return atomic_read(&sk->sk_rmem_alloc) > threshold; + return tcp_rmem_used(sk) > threshold; } static inline bool tcp_epollin_ready(const struct sock *sk, int target) @@ -1949,7 +1971,7 @@ static inline void tcp_fast_path_check(struct sock *sk) if (RB_EMPTY_ROOT(&tp->out_of_order_queue) && tp->rcv_wnd && - atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf && + tcp_rmem_avail(sk) > 0 && !tp->urg_data) tcp_fast_path_on(tp); } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index e6b2f4be7723..b8e65e31255e 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5959,7 +5959,7 @@ static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb) struct tcp_sock *tp = tcp_sk(sk); /* Do nothing if our queues are empty. */ - if (!atomic_read(&sk->sk_rmem_alloc)) + if (!tcp_rmem_used(sk)) return -1; NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED); -- 2.43.0
