From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C33D384249 for ; Sat, 14 Mar 2026 20:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773519284; cv=none; b=f1WTgdcZ+R8qRktdOGaBwzqnGVPVh78PDjkfyYViEfRh3E9+YN/nIK+FR8x3oyV3a+JgFBvNrMSQ/JWF5A9cUE5NNWTtr0yes9exHXJ/iLE0MH9czOjl0wwHdgtfCSut7p/rEDtKAqmgdA9IBNbgpyHYdjraQVkjZc2kozAigyU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773519284; c=relaxed/simple; bh=x7JZBaiPMjila4i+dsi/muzHxZeS9PJ/SGrV4i00JNc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dgz9f7e0OrKN1quxH8YPjwLf2/mW6d54b4sQWNd5+WSqD4N1xkn2yPUbWo5aMX6tCS9Mf0l9/lPNbop7FCpGmcMCvRHE+rq/lVTlm7qd2ujHRaRdGF8uYbaSw7A0j7+3ao6T41NPmhgp+eev3Igt0QPWJvb4YEZGZeWSnDAm5B0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mqKeAoog; arc=none smtp.client-ip=209.85.160.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mqKeAoog" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-415b23dd6e5so1123617fac.3 for ; Sat, 14 Mar 2026 13:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773519282; x=1774124082; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iO8Gig+uUsNsJX3c3oLWHfAhxFR79WVWca0SWR3nLZY=; b=mqKeAoogoXdX44MM/Zkhxuq7XAcLseuh0W7fIbHATuhIua+Qfk078TRgI0hpu9EzZ/ YvyOe7D6pmskgno+wE3PGrhfzYggbuXfg8tNJggg7chyoOMVjftFX4FEIOt6Y1KRu5Zx KWe7Ln/UHWVAVTKdR9PPgVpBda773gI3pk8izQo96R6z7wPNriZKzmIfWDdzPIn/RtES +dhvKnWe9Z4vdQ0Qmslpp4KnYttd/Yr4f6sD3Y6gToNhTlptsBcYFk+CPOKEBNq2vME3 vyXf3ZT1kVo1dmqSYU6rToBhT6EzhT+6aVisnVAvdW1Pf0bCe94R/4UTviNJzm0PwuJS DGpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773519282; x=1774124082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=iO8Gig+uUsNsJX3c3oLWHfAhxFR79WVWca0SWR3nLZY=; b=PevIlk3pG+5qghXEF/04j21PlJ0UctxFT5FDDycxO131obt+30+bTNpSKKq03z3nv8 t09+2cLM15NxATu+5Gi0/ihpPwfH+dUAVA+znRcN43mL05yWnhwL+MZQPVAi4i5O6IJA 54eSkvfzif4YSsWIc0z1hOZrKvVNDpFytE7NSnO07fNg1yJUaZojM99+CWFCJK+wqwV3 x8/szUuw3BBRI12Eol7HEJSvo+LBfu7SAq+L+mfyqiLjNNobhmG4svbG0CxPKNCv5PhM kC+Zw9EmVD6cUZd5sAk+KGIcZ2lkmrHCNmVs5Dutgm4t8QfiCGEuTeXe07cHRGG1oDTE 2U9w== X-Forwarded-Encrypted: i=1; AJvYcCXDglKetYTNQN2GTgtUvjtJhkI6JwQUZ9ZcCus7NdcwGsedfqxQYrYD7dqTx7xGWVarCtl3tQ==@lists.linux.dev X-Gm-Message-State: AOJu0Yypbdv8xFU2iA6Jwpj4/YP3Cn2Zv0SoqfxRfcEP03BwxEhtUjcU 9JHskBFXVb04Xavgli9aPPjjcVj11l78RstpUH7SHRoDuio6ht4r3lul X-Gm-Gg: ATEYQzwF2o9TTp7WhtMuagkrDxkNJOgLHBPreS5zn6F4FkA/EQFFd+SS7urEGpHr2UW vBJRHgFt2QZRg1ijXui9QekyUmrsFUyOHnzfWthZ/OzXX/QY7gquvqYsLdvcyC2rSVuDLpa0EOV PeWR+j12+yLBEWlp9BE4G9YVrOdPTT43bbv7qFLhm1HJ3LjeBwGNPUiWgtlsj0yAWTniUGXKZCI vKjeKaqhfZmbwzvhQjAcHaTjBx+IRSWpV1HpvM0NzYzqd98CiQS9ltXH0kr2+9pN9pyujGdK7pg mMA8fw0A8bjk4TkMP+L3stXpL4FZK+VR3AwCoAIEv9pDIwJKrxIQauvfFTvIk8MCVtsKQA1HRx4 tN5Dc18oAG4Ob1Lx3fi608HHViikUBd74jsS+ib/BtGg1xSgdOZOHagqFTLEGmPltQxXVqdxtQs gC723ZsjPTkdCl1pvE8aRVsTfIhcMWss4sLIYdwVdOwtsr4OemtTDwvhUikAi/RGAne+f1HepVT GfbNJzDhM119MgPY+5YjGC/NvryeNlOrHk4Qf/h X-Received: by 2002:a05:6870:d8c:b0:404:33e1:3cc2 with SMTP id 586e51a60fabf-417b91b2c8fmr4474193fac.13.1773519282232; Sat, 14 Mar 2026 13:14:42 -0700 (PDT) Received: from Atwell-Laptop.. (108-212-132-20.lightspeed.irvnca.sbcglobal.net. [108.212.132.20]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4177e5e8185sm11914165fac.12.2026.03.14.13.14.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Mar 2026 13:14:41 -0700 (PDT) From: atwellwea@gmail.com To: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, edumazet@google.com, ncardwell@google.com Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mptcp@lists.linux.dev, dsahern@kernel.org, horms@kernel.org, kuniyu@google.com, andrew+netdev@lunn.ch, willemdebruijn.kernel@gmail.com, jasowang@redhat.com, skhan@linuxfoundation.org, corbet@lwn.net, matttbe@kernel.org, martineau@kernel.org, geliang@kernel.org, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, 0x7f454c46@gmail.com Subject: [PATCH net-next v2 01/14] tcp: factor receive-memory accounting helpers Date: Sat, 14 Mar 2026 14:13:35 -0600 Message-ID: <20260314201348.1786972-2-atwellwea@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260314201348.1786972-1-atwellwea@gmail.com> References: <20260314201348.1786972-1-atwellwea@gmail.com> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Wesley Atwell 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 --- 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