All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xuanqiang Luo <xuanqiang.luo@linux.dev>
To: netdev@vger.kernel.org
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Matthieu Baerts <matttbe@kernel.org>,
	Mat Martineau <martineau@kernel.org>,
	Geliang Tang <geliang@kernel.org>,
	David Ahern <dsahern@kernel.org>,
	Ido Schimmel <idosch@nvidia.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	mptcp@lists.linux.dev, netfilter-devel@vger.kernel.org,
	coreteam@netfilter.org, Xuanqiang Luo <luoxuanqiang@kylinos.cn>
Subject: [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established()
Date: Tue, 24 Nov 2026 22:06:30 +0900	[thread overview]
Message-ID: <20261124130631.1886963-6-xuanqiang.luo@linux.dev> (raw)
In-Reply-To: <20261124130631.1886963-1-xuanqiang.luo@linux.dev>

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

tcp_rcv_established() passes the address of a local boolean to
tcp_queue_rcv(), to know if skb->head was stolen while coalescing.

With CONFIG_STACKPROTECTOR_STRONG=y, this forces a stack canary in
tcp_rcv_established(), which is paid for every received packet,
including pure ACKs. tcp_data_queue() has the same issue.

Change tcp_try_coalesce(), tcp_ooo_try_coalesce() and tcp_queue_rcv()
to return this information by value, in a new enum tcp_eaten.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 1/5 up/down: 16/-215 (-199)
Function                                     old     new   delta
tcp_validate_incoming                       2487    2503     +16
tcp_send_rcvq                                502     490     -12
tcp_try_coalesce                             359     336     -23
tcp_queue_rcv                                752     723     -29
tcp_data_queue                              4425    4350     -75
tcp_rcv_established                         3287    3211     -76
Total: Before=24067604, After=24067405, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 net/ipv4/tcp_input.c | 99 +++++++++++++++++++++++---------------------
 1 file changed, 52 insertions(+), 47 deletions(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 801c76744ed8..3d0a8b7e883e 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5276,41 +5276,47 @@ static void tcp_sack_remove(struct tcp_sock *tp)
 	tp->rx_opt.num_sacks = num_sacks;
 }
 
+/* Result of an attempt to merge an skb to a prior one. */
+enum tcp_eaten {
+	TCP_NOT_EATEN,
+	TCP_EATEN,
+	TCP_EATEN_STOLEN,	/* skb->head was stolen */
+};
+
 /**
  * tcp_try_coalesce - try to merge skb to prior one
  * @sk: socket
  * @to: prior buffer
  * @from: buffer to add in queue
- * @fragstolen: pointer to boolean
  *
  * Before queueing skb @from after @to, try to merge them
  * to reduce overall memory use and queue lengths, if cost is small.
  * Packets in ofo or receive queues can stay a long time.
  * Better try to coalesce them right now to avoid future collapses.
- * Returns true if caller should free @from instead of queueing it
+ *
+ * Return: TCP_NOT_EATEN if @from has to be queued. Otherwise the caller
+ * should free @from with kfree_skb_partial().
  */
-static bool tcp_try_coalesce(struct sock *sk,
-			     struct sk_buff *to,
-			     struct sk_buff *from,
-			     bool *fragstolen)
+static enum tcp_eaten tcp_try_coalesce(struct sock *sk,
+				       struct sk_buff *to,
+				       struct sk_buff *from)
 {
+	bool fragstolen;
 	int delta;
 
-	*fragstolen = false;
-
 	/* Its possible this segment overlaps with prior segment in queue */
 	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
-		return false;
+		return TCP_NOT_EATEN;
 
 	if (!tcp_skb_can_collapse_rx(to, from))
-		return false;
+		return TCP_NOT_EATEN;
 
 	/* Nothing follows a FIN. */
 	if (unlikely(TCP_SKB_CB(to)->tcp_flags & TCPHDR_FIN))
-		return false;
+		return TCP_NOT_EATEN;
 
-	if (!skb_try_coalesce(to, from, fragstolen, &delta))
-		return false;
+	if (!skb_try_coalesce(to, from, &fragstolen, &delta))
+		return TCP_NOT_EATEN;
 
 	atomic_add(delta, &sk->sk_rmem_alloc);
 	sk_mem_charge(sk, delta);
@@ -5325,15 +5331,14 @@ static bool tcp_try_coalesce(struct sock *sk,
 		skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
 	}
 
-	return true;
+	return fragstolen ? TCP_EATEN_STOLEN : TCP_EATEN;
 }
 
-static bool tcp_ooo_try_coalesce(struct sock *sk,
-			     struct sk_buff *to,
-			     struct sk_buff *from,
-			     bool *fragstolen)
+static enum tcp_eaten tcp_ooo_try_coalesce(struct sock *sk,
+					   struct sk_buff *to,
+					   struct sk_buff *from)
 {
-	bool res = tcp_try_coalesce(sk, to, from, fragstolen);
+	enum tcp_eaten res = tcp_try_coalesce(sk, to, from);
 
 	/* In case tcp_drop_reason() is called later, update to->gso_segs */
 	if (res) {
@@ -5359,9 +5364,10 @@ static void tcp_ofo_queue(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	__u32 dsack_high = tp->rcv_nxt;
-	bool fin, fragstolen, eaten;
 	struct sk_buff *skb, *tail;
+	enum tcp_eaten eaten;
 	struct rb_node *p;
+	bool fin;
 
 	p = rb_first(&tp->out_of_order_queue);
 	while (p) {
@@ -5387,13 +5393,13 @@ static void tcp_ofo_queue(struct sock *sk)
 		bpf_tcp_ops_call_flag(enqueue_rcvq, RCVQ, sk, skb);
 
 		tail = skb_peek_tail(&sk->sk_receive_queue);
-		eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
+		eaten = tail ? tcp_try_coalesce(sk, tail, skb) : TCP_NOT_EATEN;
 		tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
 		fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
 		if (!eaten)
 			tcp_add_receive_queue(sk, skb);
 		else
-			kfree_skb_partial(skb, fragstolen);
+			kfree_skb_partial(skb, eaten == TCP_EATEN_STOLEN);
 
 		if (unlikely(fin)) {
 			tcp_fin(sk);
@@ -5460,8 +5466,8 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 	enum skb_drop_reason reason;
 	struct rb_node **p, *parent;
 	struct sk_buff *skb1;
+	enum tcp_eaten eaten;
 	u32 seq, end_seq;
-	bool fragstolen;
 	int rmem;
 
 	tcp_save_lrcv_flowlabel(sk, skb);
@@ -5504,15 +5510,15 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 	/* In the typical case, we are adding an skb to the end of the list.
 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
 	 */
-	if (tcp_ooo_try_coalesce(sk, tp->ooo_last_skb,
-				 skb, &fragstolen)) {
+	eaten = tcp_ooo_try_coalesce(sk, tp->ooo_last_skb, skb);
+	if (eaten) {
 coalesce_done:
 		/* For non sack flows, do not grow window to force DUPACK
 		 * and trigger fast retransmit.
 		 */
 		if (tcp_is_sack(tp))
 			tcp_grow_window(sk, skb, true);
-		kfree_skb_partial(skb, fragstolen);
+		kfree_skb_partial(skb, eaten == TCP_EATEN_STOLEN);
 		skb = NULL;
 		goto add_sack;
 	}
@@ -5561,9 +5567,10 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 						SKB_DROP_REASON_TCP_OFOMERGE);
 				goto merge_right;
 			}
-		} else if (tcp_ooo_try_coalesce(sk, skb1,
-						skb, &fragstolen)) {
-			goto coalesce_done;
+		} else {
+			eaten = tcp_ooo_try_coalesce(sk, skb1, skb);
+			if (eaten)
+				goto coalesce_done;
 		}
 		p = &parent->rb_right;
 	}
@@ -5614,18 +5621,16 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 		tcp_rcvbuf_grow(sk, tp->rcvq_space.space);
 }
 
-static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,
-				      bool *fragstolen)
+static enum tcp_eaten __must_check tcp_queue_rcv(struct sock *sk,
+						 struct sk_buff *skb)
 {
 	int rmem = atomic_read(&sk->sk_rmem_alloc);
 	struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
-	int eaten;
+	enum tcp_eaten eaten;
 
 	bpf_tcp_ops_call_flag(enqueue_rcvq, RCVQ, sk, skb);
 
-	eaten = (tail &&
-		 tcp_try_coalesce(sk, tail,
-				  skb, fragstolen)) ? 1 : 0;
+	eaten = tail ? tcp_try_coalesce(sk, tail, skb) : TCP_NOT_EATEN;
 	tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
 	if (!eaten) {
 		tcp_add_receive_queue(sk, skb);
@@ -5637,10 +5642,10 @@ static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,
 
 int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
 {
+	enum tcp_eaten eaten;
 	struct sk_buff *skb;
 	int err = -ENOMEM;
 	int data_len = 0;
-	bool fragstolen;
 
 	if (size == 0)
 		return 0;
@@ -5674,8 +5679,9 @@ int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
 	TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
 
-	if (tcp_queue_rcv(sk, skb, &fragstolen)) {
-		WARN_ON_ONCE(fragstolen); /* should not happen */
+	eaten = tcp_queue_rcv(sk, skb);
+	if (eaten) {
+		WARN_ON_ONCE(eaten == TCP_EATEN_STOLEN); /* should not happen */
 		__kfree_skb(skb);
 	}
 	return size;
@@ -5697,8 +5703,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	enum skb_drop_reason reason;
-	bool fragstolen;
-	int eaten;
+	enum tcp_eaten eaten;
 
 	/* If a subflow has been reset, the packet should not continue
 	 * to be processed, drop the packet.
@@ -5756,7 +5761,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
 			sk_forced_mem_schedule(sk, skb->truesize);
 		}
 
-		eaten = tcp_queue_rcv(sk, skb, &fragstolen);
+		eaten = tcp_queue_rcv(sk, skb);
 		if (skb->len)
 			tcp_event_data_recv(sk, skb);
 		if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
@@ -5777,8 +5782,8 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
 
 		tcp_fast_path_check(sk);
 
-		if (eaten > 0)
-			kfree_skb_partial(skb, fragstolen);
+		if (eaten)
+			kfree_skb_partial(skb, eaten == TCP_EATEN_STOLEN);
 		if (!sock_flag(sk, SOCK_DEAD))
 			tcp_data_ready(sk);
 		return;
@@ -6815,8 +6820,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
 				goto discard;
 			}
 		} else {
-			int eaten = 0;
-			bool fragstolen = false;
+			enum tcp_eaten eaten;
 
 			if (tcp_checksum_complete(skb))
 				goto csum_error;
@@ -6847,7 +6851,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
 			__skb_pull(skb, tcp_header_len);
 			tcp_ecn_received_counters(sk, skb,
 						  len - tcp_header_len);
-			eaten = tcp_queue_rcv(sk, skb, &fragstolen);
+			eaten = tcp_queue_rcv(sk, skb);
 
 			tcp_event_data_recv(sk, skb);
 
@@ -6864,7 +6868,8 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
 			__tcp_ack_snd_check(sk, 0);
 no_ack:
 			if (eaten)
-				kfree_skb_partial(skb, fragstolen);
+				kfree_skb_partial(skb,
+						  eaten == TCP_EATEN_STOLEN);
 			tcp_data_ready(sk);
 			return;
 		}
-- 
2.43.0


  parent reply	other threads:[~2026-11-24 13:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
2026-11-24 13:06 ` [PATCH net-next v1 1/6] tcp: avoid a stack canary in __tcp_select_window() Xuanqiang Luo
2026-11-24 13:06 ` [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2() Xuanqiang Luo
2026-11-25 13:09   ` sashiko-bot
2026-11-24 13:06 ` [PATCH net-next v1 3/6] ipv4: raw: do not use skb_header_pointer() in icmp_filter() Xuanqiang Luo
2026-11-24 13:06 ` [PATCH net-next v1 4/6] net: avoid stack canaries in skb_try_coalesce() callers Xuanqiang Luo
2026-11-24 13:06 ` Xuanqiang Luo [this message]
2026-11-25 13:09   ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() sashiko-bot
2026-11-24 13:06 ` [PATCH net-next v1 6/6] tcp: do not call bpf_skops_write_hdr_opt() unless needed Xuanqiang Luo
2026-11-26  8:48 ` [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths gang.yan
2026-11-27  6:18   ` Xuanqiang Luo
2026-11-26 10:20 ` David Laight
2026-11-27  6:33   ` Xuanqiang Luo
2026-11-27 10:41     ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124130631.1886963-6-xuanqiang.luo@linux.dev \
    --to=xuanqiang.luo@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=coreteam@netfilter.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=fw@strlen.de \
    --cc=geliang@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luoxuanqiang@kylinos.cn \
    --cc=martin.lau@linux.dev \
    --cc=martineau@kernel.org \
    --cc=matttbe@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mptcp@lists.linux.dev \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.