BPF List
 help / color / mirror / Atom feed
* [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
@ 2026-11-24 13:06 Xuanqiang Luo
  2026-11-24 13:06 ` [PATCH net-next v1 1/6] tcp: avoid a stack canary in __tcp_select_window() Xuanqiang Luo
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
passes the address of one of its locals to another function needs a stack
canary. The canary is set up and checked on every call, also when the
local is only used on a path that almost never runs.

This series removes the canary from functions of the TCP/IPv4 fast paths,
by returning results by value, by using storage that is not on the stack,
or by not calling a function that has nothing to do.

Patch 1 returns the two mptcp_space() values in a structure, so that
__tcp_select_window() no longer needs a canary.

Patch 2 changes ip_neigh_gw4() to take a pointer to the address, like
ip_neigh_gw6(), so that ip_finish_output2() no longer needs a canary.

Patch 3 uses pskb_may_pull() in icmp_filter(), so that
raw_local_deliver() no longer needs a canary.

Patch 4 turns skb_try_coalesce() into an inline wrapper over a function
returning its results by value, so that its callers no longer need a
canary because of it.

Patch 5 returns the result of the TCP receive coalescing by value, so
that tcp_rcv_established() and tcp_data_queue() no longer need a canary.

Patch 6 calls bpf_skops_write_hdr_opt(), which needs a canary, only if a
BPF program reserved room for TCP options.

The bloat-o-meter numbers are from gcc 16.2 on x86_64. The functions
named in patches 1 to 5 also lose their canary with clang 23.1.6 on
x86_64, with and without ThinLTO, and on arm64.

Xuanqiang Luo (6):
  tcp: avoid a stack canary in __tcp_select_window()
  ipv4: avoid a stack canary in ip_finish_output2()
  ipv4: raw: do not use skb_header_pointer() in icmp_filter()
  net: avoid stack canaries in skb_try_coalesce() callers
  tcp: avoid a stack canary in tcp_rcv_established()
  tcp: do not call bpf_skops_write_hdr_opt() unless needed

 include/linux/filter.h           |  2 +-
 include/linux/skbuff.h           | 35 ++++++++++-
 include/net/mptcp.h              | 13 ++++-
 include/net/route.h              | 16 +++---
 net/core/filter.c                |  2 +-
 net/core/skbuff.c                | 50 ++++++++--------
 net/ipv4/raw.c                   | 25 ++++----
 net/ipv4/route.c                 | 15 +++--
 net/ipv4/tcp_input.c             | 99 +++++++++++++++++---------------
 net/ipv4/tcp_output.c            | 25 ++++----
 net/mptcp/subflow.c              |  9 ++-
 net/netfilter/nf_flow_table_ip.c |  4 +-
 12 files changed, 178 insertions(+), 117 deletions(-)


base-commit: a4c450594f5d1dc0fc4daacfb95d40554d99dead
-- 
2.43.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 1/6] tcp: avoid a stack canary in __tcp_select_window()
  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 ` Xuanqiang Luo
  2026-11-24 13:06 ` [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2() Xuanqiang Luo
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

__tcp_select_window() passes the address of two locals to
mptcp_space(), but only for MPTCP subflows.

With CONFIG_STACKPROTECTOR_STRONG=y, this forces a stack canary in
__tcp_select_window() for all TCP sockets.

Return both values in a small structure instead.

While we are at it, do not compute tcp_space() and tcp_full_space()
for MPTCP subflows, their values were overwritten.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-51 (-51)
Function                                     old     new   delta
mptcp_space                                   87      86      -1
tcp_connect                                 5892    5876     -16
__tcp_select_window                          706     672     -34
Total: Before=24068967, After=24068916, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 include/net/mptcp.h   | 13 +++++++++++--
 net/ipv4/tcp_output.c | 14 ++++++++++----
 net/mptcp/subflow.c   |  9 ++++++---
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/include/net/mptcp.h b/include/net/mptcp.h
index ad7005b574bb..95ebcfd5950d 100644
--- a/include/net/mptcp.h
+++ b/include/net/mptcp.h
@@ -126,6 +126,12 @@ struct mptcp_pm_ops {
 	void (*release)(struct mptcp_sock *msk);
 } ____cacheline_aligned_in_smp;
 
+/* Result of mptcp_space(). */
+struct mptcp_space_info {
+	int space;
+	int full_space;
+};
+
 #ifdef CONFIG_MPTCP
 void mptcp_init(void);
 
@@ -144,7 +150,7 @@ static inline bool rsk_drop_req(const struct request_sock *req)
 	return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
 }
 
-void mptcp_space(const struct sock *ssk, int *space, int *full_space);
+struct mptcp_space_info mptcp_space(const struct sock *ssk);
 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
 		       unsigned int *size, struct mptcp_out_options *opts);
 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
@@ -291,7 +297,10 @@ static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
 	return true;
 }
 
-static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
+static inline struct mptcp_space_info mptcp_space(const struct sock *ssk)
+{
+	return (struct mptcp_space_info){ };
+}
 static inline void mptcp_seq_show(struct seq_file *seq) { }
 
 static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 08d0d4a639fe..c2022fc9ad52 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -3391,12 +3391,18 @@ u32 __tcp_select_window(struct sock *sk)
 	 * fluctuations.  --SAW  1998/11/1
 	 */
 	int mss = icsk->icsk_ack.rcv_mss;
-	int free_space = tcp_space(sk);
-	int allowed_space = tcp_full_space(sk);
+	int free_space, allowed_space;
 	int full_space, window;
 
-	if (sk_is_mptcp(sk))
-		mptcp_space(sk, &free_space, &allowed_space);
+	if (sk_is_mptcp(sk)) {
+		struct mptcp_space_info info = mptcp_space(sk);
+
+		free_space = info.space;
+		allowed_space = info.full_space;
+	} else {
+		free_space = tcp_space(sk);
+		allowed_space = tcp_full_space(sk);
+	}
 
 	full_space = min_t(int, tp->window_clamp, allowed_space);
 
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index 00677fb3efcd..d22ee068f599 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -1532,13 +1532,16 @@ bool mptcp_subflow_data_available(struct sock *sk)
  * as far as mptcp peer is concerned that data is still inflight.
  * DSS ACK is updated when skb is moved to the mptcp rx queue.
  */
-void mptcp_space(const struct sock *ssk, int *space, int *full_space)
+struct mptcp_space_info mptcp_space(const struct sock *ssk)
 {
 	const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
 	const struct sock *sk = subflow->conn;
+	struct mptcp_space_info info;
 
-	*space = __mptcp_space(sk);
-	*full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
+	info.space = __mptcp_space(sk);
+	info.full_space = mptcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
+
+	return info;
 }
 
 static void subflow_error_report(struct sock *ssk)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2()
  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 ` 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
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

ip_neigh_gw4() passes the address of its by-value @daddr argument to
__neigh_create() when the lockless lookup finds no neighbour.

Once inlined, this forces a stack canary (CONFIG_STACKPROTECTOR_STRONG=y)
into its callers, ip_finish_output2() being the one in the transmit path
of every IPv4 packet, although creating a neighbour is rare compared to
finding one.

Change ip_neigh_gw4() to take a pointer to the address, like
ip_neigh_gw6(). All callers have the address in the route, in the packet
or in another object that is not on their stack.

Call it only once in ip_neigh_for_gw() and ipv4_dst_neigh_lookup(), so
that the lookup and the creation are no longer inlined twice there.

Change the type of ipv4_nh in struct bpf_nh_params to __be32, since its
address is now given to ip_neigh_gw4().

Note that skb_do_redirect() and nf_flow_offload_ip_hook() still need a
canary for other reasons.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 1/5 up/down: 3/-846 (-843)
Function                                     old     new   delta
nf_flow_offload_ip_hook                     2065    2068      +3
ip_do_fragment                              1441    1425     -16
ipv4_dst_neigh_lookup                        684     587     -97
skb_do_redirect                             4382    4222    -160
vrf_finish_output                           1614    1345    -269
ip_finish_output2                           1775    1471    -304
Total: Before=24068916, After=24068073, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 include/linux/filter.h           |  2 +-
 include/net/route.h              | 16 ++++++++--------
 net/core/filter.c                |  2 +-
 net/ipv4/route.c                 | 15 +++++++--------
 net/netfilter/nf_flow_table_ip.c |  4 +++-
 5 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0cd67caccb..e258ba4741af 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -871,7 +871,7 @@ struct bpf_skb_data_end {
 struct bpf_nh_params {
 	u32 nh_family;
 	union {
-		u32 ipv4_nh;
+		__be32 ipv4_nh;
 		struct in6_addr ipv6_nh;
 	};
 };
diff --git a/include/net/route.h b/include/net/route.h
index 3fccd31eb74d..1571e1c6082c 100644
--- a/include/net/route.h
+++ b/include/net/route.h
@@ -406,15 +406,15 @@ static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
 }
 
 static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
-					     __be32 daddr)
+					     const __be32 *daddr)
 {
 	struct neighbour *neigh;
 
-	neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr);
+	neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)*daddr);
 	if (unlikely(!neigh)) {
 		struct neigh_table *tbl = arp_table(dev_net(dev));
 
-		neigh = __neigh_create(tbl, &daddr, dev, false);
+		neigh = __neigh_create(tbl, daddr, dev, false);
 	}
 
 	return neigh;
@@ -425,17 +425,17 @@ static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
 						bool *is_v6gw)
 {
 	struct net_device *dev = rt->dst.dev;
-	struct neighbour *neigh;
+	const __be32 *daddr;
 
 	if (likely(rt->rt_gw_family == AF_INET)) {
-		neigh = ip_neigh_gw4(dev, rt->rt_gw4);
+		daddr = &rt->rt_gw4;
 	} else if (rt->rt_gw_family == AF_INET6) {
-		neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
 		*is_v6gw = true;
+		return ip_neigh_gw6(dev, &rt->rt_gw6);
 	} else {
-		neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
+		daddr = &ip_hdr(skb)->daddr;
 	}
-	return neigh;
+	return ip_neigh_gw4(dev, daddr);
 }
 
 #endif	/* _ROUTE_H */
diff --git a/net/core/filter.c b/net/core/filter.c
index f42ecfb5841f..8ee023c87c24 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2376,7 +2376,7 @@ static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
 		neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
 		is_v6gw = true;
 	} else if (nh->nh_family == AF_INET) {
-		neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
+		neigh = ip_neigh_gw4(dev, &nh->ipv4_nh);
 	} else {
 		rcu_read_unlock();
 		goto out_drop;
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index d0930ae4c5ae..585c10743e1b 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -419,15 +419,14 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,
 
 	rcu_read_lock();
 	dev = dst_dev_rcu(dst);
-	if (likely(rt->rt_gw_family == AF_INET)) {
-		n = ip_neigh_gw4(dev, rt->rt_gw4);
-	} else if (rt->rt_gw_family == AF_INET6) {
+	if (unlikely(rt->rt_gw_family == AF_INET6)) {
 		n = ip_neigh_gw6(dev, &rt->rt_gw6);
-        } else {
-		__be32 pkey;
-
-		pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
-		n = ip_neigh_gw4(dev, pkey);
+	} else {
+		if (likely(rt->rt_gw_family == AF_INET))
+			daddr = &rt->rt_gw4;
+		else if (skb)
+			daddr = &ip_hdr(skb)->daddr;
+		n = ip_neigh_gw4(dev, daddr);
 	}
 
 	if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
index 78bba416dd5b..19f62005b517 100644
--- a/net/netfilter/nf_flow_table_ip.c
+++ b/net/netfilter/nf_flow_table_ip.c
@@ -872,7 +872,9 @@ static int nf_flow_queue_xmit4(struct sk_buff *skb,
 			flow_offload_teardown(flow);
 			return NF_DROP;
 		}
-		neigh = ip_neigh_gw4(rt->dst.dev, rt_nexthop(rt, ip_addr->ip));
+		neigh = ip_neigh_gw4(rt->dst.dev,
+				     rt->rt_gw_family == AF_INET ?
+				     &rt->rt_gw4 : &ip_addr->ip);
 		if (IS_ERR(neigh)) {
 			flow_offload_teardown(flow);
 			return NF_DROP;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 3/6] ipv4: raw: do not use skb_header_pointer() in icmp_filter()
  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-24 13:06 ` Xuanqiang Luo
  2026-11-24 13:06 ` [PATCH net-next v1 4/6] net: avoid stack canaries in skb_try_coalesce() callers Xuanqiang Luo
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

raw_local_deliver() is called for every IPv4 packet delivered locally.

icmp_filter() passes the address of a local struct icmphdr to
skb_header_pointer(). Once inlined, this forces a stack canary
(CONFIG_STACKPROTECTOR_STRONG=y) into raw_local_deliver(), although
only ICMP packets matching a raw socket need the filter.

Prefer pskb_may_pull(), as commit a14d93179045 ("ipv6: do not use
skb_header_pointer() in icmpv6_filter()") did for IPv6.

skb->data already points to the transport header here.

skb->head can change, reload iph pointer in raw_v4_input().

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95 (-95)
Function                                     old     new   delta
raw_local_deliver                            648     553     -95
Total: Before=24068073, After=24067978, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 net/ipv4/raw.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index f33a9dfc5309..f72163699040 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -140,16 +140,14 @@ EXPORT_SYMBOL_GPL(raw_v4_match);
  *	0 - deliver
  *	1 - block
  */
-static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
+static int icmp_filter(const struct sock *sk, struct sk_buff *skb)
 {
-	struct icmphdr _hdr;
 	const struct icmphdr *hdr;
 
-	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
-				 sizeof(_hdr), &_hdr);
-	if (!hdr)
+	if (!pskb_may_pull(skb, sizeof(*hdr)))
 		return 1;
 
+	hdr = (const struct icmphdr *)skb->data;
 	if (hdr->type < 32) {
 		__u32 data = raw_sk(sk)->filter.data;
 
@@ -166,10 +164,10 @@ static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
  * RFC 1122: SHOULD pass TOS value up to the transport layer.
  * -> It does. And not only TOS, but all IP header.
  */
-static int raw_v4_input(struct net *net, struct sk_buff *skb,
-			const struct iphdr *iph, int hash)
+static int raw_v4_input(struct net *net, struct sk_buff *skb, int hash)
 {
 	int sdif = inet_sdif(skb);
+	const struct iphdr *iph;
 	struct hlist_head *hlist;
 	int dif = inet_iif(skb);
 	int delivered = 0;
@@ -178,13 +176,19 @@ static int raw_v4_input(struct net *net, struct sk_buff *skb,
 	hlist = &raw_v4_hashinfo.ht[hash];
 	rcu_read_lock();
 	sk_for_each_rcu(sk, hlist) {
+		iph = ip_hdr(skb);
 		if (!raw_v4_match(net, sk, iph->protocol,
 				  iph->saddr, iph->daddr, dif, sdif))
 			continue;
 
 		delivered = 1;
-		if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
-		    ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
+		if (iph->protocol == IPPROTO_ICMP) {
+			if (icmp_filter(sk, skb))
+				continue;
+			/* icmp_filter() might have changed skb->head. */
+			iph = ip_hdr(skb);
+		}
+		if (ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
 				   skb->dev->ifindex, sdif)) {
 			struct sk_buff *clone;
 
@@ -208,8 +212,7 @@ int raw_local_deliver(struct sk_buff *skb, int protocol)
 {
 	struct net *net = dev_net(skb->dev);
 
-	return raw_v4_input(net, skb, ip_hdr(skb),
-			    raw_hashfunc(net, protocol));
+	return raw_v4_input(net, skb, raw_hashfunc(net, protocol));
 }
 
 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 4/6] net: avoid stack canaries in skb_try_coalesce() callers
  2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
                   ` (2 preceding siblings ...)
  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 ` Xuanqiang Luo
  2026-11-24 13:06 ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() Xuanqiang Luo
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

skb_try_coalesce() returns the stolen head indication and the truesize
delta through pointers to locals of its callers.

With CONFIG_STACKPROTECTOR_STRONG=y, this forces a stack canary in
all of them, some are in TCP receive fast paths.

Rename it to __skb_try_coalesce(), and return its results by value in
a small structure.

skb_try_coalesce() is now an inline wrapper, callers do not change.

A successful merge never has a negative delta, so that a negative value
can be used to report a failure.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 2/2 grow/shrink: 1/8 up/down: 1303/-1677 (-374)
Function                                     old     new   delta
__skb_try_coalesce                             -    1255   +1255
tcp_data_queue                              4393    4425     +32
__pfx___skb_try_coalesce                       -      16     +16
mptcp_recvmsg                               2108    2092     -16
alloc_skb_with_frags                         534     518     -16
__pfx_skb_try_coalesce                        16       -     -16
tcp_try_coalesce                             386     359     -27
skb_copy_ubufs                              1645    1613     -32
__mptcp_move_skbs_from_subflow              1215    1167     -48
tcp_add_backlog                             1576    1527     -49
mptcp_try_coalesce                           235     168     -67
inet_frag_reasm_finish                       672     598     -74
skb_try_coalesce                            1332       -   -1332
Total: Before=24067978, After=24067604, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 include/linux/skbuff.h | 35 +++++++++++++++++++++++++++--
 net/core/skbuff.c      | 50 +++++++++++++++++++++++-------------------
 2 files changed, 60 insertions(+), 25 deletions(-)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 55ab6392301a..bbdf64811aed 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1370,8 +1370,39 @@ void __consume_stateless_skb(struct sk_buff *skb);
 void  __kfree_skb(struct sk_buff *skb);
 
 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
-bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
-		      bool *fragstolen, int *delta_truesize);
+
+/* Result of __skb_try_coalesce(). */
+struct skb_coalesce_res {
+	int	delta_truesize;	/* negative if not coalesced */
+	bool	fragstolen;
+};
+
+struct skb_coalesce_res __skb_try_coalesce(struct sk_buff *to,
+					   struct sk_buff *from);
+
+/**
+ * skb_try_coalesce - try to merge skb to prior one
+ * @to: prior buffer
+ * @from: buffer to add
+ * @fragstolen: pointer to boolean
+ * @delta_truesize: how much more was allocated than was requested
+ *
+ * Return: true if @from was merged into @to, false otherwise.
+ */
+static __always_inline bool skb_try_coalesce(struct sk_buff *to,
+					     struct sk_buff *from,
+					     bool *fragstolen,
+					     int *delta_truesize)
+{
+	struct skb_coalesce_res res = __skb_try_coalesce(to, from);
+
+	*fragstolen = res.fragstolen;
+	if (res.delta_truesize < 0)
+		return false;
+
+	*delta_truesize = res.delta_truesize;
+	return true;
+}
 
 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
 			    int node);
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index e5b0209aec1c..1da20a7a0bb4 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -6602,23 +6602,26 @@ void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
 EXPORT_SYMBOL(kfree_skb_partial);
 
 /**
- * skb_try_coalesce - try to merge skb to prior one
+ * __skb_try_coalesce - try to merge skb to prior one
  * @to: prior buffer
  * @from: buffer to add
- * @fragstolen: pointer to boolean
- * @delta_truesize: how much more was allocated than was requested
+ *
+ * Helper for skb_try_coalesce(), returning its results by value so that
+ * callers do not need a stack canary.
+ *
+ * Return: @delta_truesize is negative if @from was not merged.
  */
-bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
-		      bool *fragstolen, int *delta_truesize)
+struct skb_coalesce_res __skb_try_coalesce(struct sk_buff *to,
+					   struct sk_buff *from)
 {
+	const struct skb_coalesce_res fail = { .delta_truesize = -1 };
 	struct skb_shared_info *to_shinfo, *from_shinfo;
 	unsigned int len = from->len;
+	bool fragstolen = false;
 	int i, delta;
 
-	*fragstolen = false;
-
 	if (skb_cloned(to))
-		return false;
+		return fail;
 
 	/* In general, avoid mixing page_pool and non-page_pool allocated
 	 * pages within the same SKB. In theory we could take full
@@ -6627,14 +6630,14 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	 * rare, so not worth dealing with.
 	 */
 	if (to->pp_recycle != from->pp_recycle)
-		return false;
+		return fail;
 
 	/* All frags in an skb must either all be struct page-backed or all
 	 * belong to the same memory provider instance; do not coalesce skbs
 	 * that would mix them.
 	 */
 	if (skb_frags_readable(from) != skb_frags_readable(to))
-		return false;
+		return fail;
 
 	if (len <= skb_tailroom(to) && skb_frags_readable(from)) {
 		if (len) {
@@ -6642,41 +6645,40 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 						   skb_tail_pointer(to),
 						   len))) {
 				DEBUG_NET_WARN_ON_ONCE(1);
-				return false;
+				return fail;
 			}
 			skb_put(to, len);
 		}
-		*delta_truesize = 0;
-		return true;
+		return (struct skb_coalesce_res){ .delta_truesize = 0 };
 	}
 
 	to_shinfo = skb_shinfo(to);
 	from_shinfo = skb_shinfo(from);
 	if (to_shinfo->frag_list || from_shinfo->frag_list)
-		return false;
+		return fail;
 	if (skb_zcopy(to) || skb_zcopy(from))
-		return false;
+		return fail;
 
 	if (skb_headlen(from) != 0) {
 		if (to_shinfo->nr_frags +
 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
-			return false;
+			return fail;
 
 		if (skb_head_is_locked(from))
-			return false;
+			return fail;
 
 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
 	} else {
 		if (to_shinfo->nr_frags +
 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
-			return false;
+			return fail;
 
 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
 	}
 
 	/* @from claims more data than it has memory for.  Leave @to alone. */
 	if (WARN_ON_ONCE(delta < 0 || delta < len))
-		return false;
+		return fail;
 
 	if (skb_headlen(from) != 0) {
 		struct page *page = virt_to_head_page(from->head);
@@ -6685,7 +6687,7 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 		offset = from->data - (unsigned char *)page_address(page);
 		skb_fill_page_desc(to, to_shinfo->nr_frags,
 				   page, offset, skb_headlen(from));
-		*fragstolen = true;
+		fragstolen = true;
 	}
 
 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
@@ -6708,10 +6710,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	to->len += len;
 	to->data_len += len;
 
-	*delta_truesize = delta;
-	return true;
+	return (struct skb_coalesce_res){
+		.delta_truesize = delta,
+		.fragstolen = fragstolen,
+	};
 }
-EXPORT_SYMBOL(skb_try_coalesce);
+EXPORT_SYMBOL(__skb_try_coalesce);
 
 static void skb_ext_scrub(struct sk_buff *skb)
 {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established()
  2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
                   ` (3 preceding siblings ...)
  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
  2026-11-25 13:09   ` 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
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH net-next v1 6/6] tcp: do not call bpf_skops_write_hdr_opt() unless needed
  2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
                   ` (4 preceding siblings ...)
  2026-11-24 13:06 ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() Xuanqiang Luo
@ 2026-11-24 13:06 ` 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-26 10:20 ` David Laight
  7 siblings, 0 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-24 13:06 UTC (permalink / raw)
  To: netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

__tcp_transmit_skb() calls bpf_skops_write_hdr_opt() for every skb. The
function returns at once if no BPF program reserved room for TCP options
(opts->bpf_opt_len == 0), which is the common case.

It has a struct bpf_sock_ops_kern on its stack. With
CONFIG_STACKPROTECTOR_STRONG=y, its stack canary is set up and checked
even when it has nothing to do.

Test opts.bpf_opt_len in its callers instead.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 2/2 up/down: 28/-196 (-168)
Function                                     old     new   delta
tcp_make_synack                             2006    2020     +14
__tcp_transmit_skb                          4114    4128     +14
tcp_options_write.isra                      1699    1651     -48
bpf_skops_write_hdr_opt.isra                1206    1058    -148
Total: Before=24067405, After=24067237, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 net/ipv4/tcp_output.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index c2022fc9ad52..3c0be5a675ea 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -538,9 +538,6 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
 {
 	u8 first_opt_off, nr_written = 0, max_opt_len = opts->bpf_opt_len;
 
-	if (likely(!max_opt_len))
-		return;
-
 	first_opt_off = tcp_hdrlen(skb) - max_opt_len;
 
 	if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) &&
@@ -1731,7 +1728,8 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
 	}
 
 	/* BPF prog is the last one writing header option */
-	bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);
+	if (unlikely(opts.bpf_opt_len))
+		bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);
 
 #if IS_ENABLED(CONFIG_IPV6)
 	if (likely(icsk->icsk_af_ops->net_header_len == sizeof(struct ipv6hdr)))
@@ -4146,8 +4144,9 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
 	rcu_read_unlock();
 #endif
 
-	bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb,
-				synack_type, &opts);
+	if (unlikely(opts.bpf_opt_len))
+		bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb,
+					synack_type, &opts);
 
 	skb_set_delivery_time(skb, now, SKB_CLOCK_MONOTONIC);
 	tcp_add_tx_delay(skb, tp);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2()
  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
  0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 13:09 UTC (permalink / raw)
  To: Xuanqiang Luo; +Cc: bpf, mptcp

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] bpf: device mismatch in bpf_out_neigh_v4() on RTN_LOCAL routes
  [1]
--

commit ca3d30f8ff839a5ead3ac3b6137813b8e1486837
Author: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

ipv4: avoid a stack canary in ip_finish_output2()

This commit changes ip_neigh_gw4() to take a pointer to the IPv4 address
instead of passing it by value, avoiding a stack canary in callers such as
ip_finish_output2() and deduplicating inlined calls in ip_neigh_for_gw()
and ipv4_dst_neigh_lookup().

[ ... ]

> diff --git a/include/net/route.h b/include/net/route.h
> index 3fccd31eb74df..1571e1c6082cc 100644
> --- a/include/net/route.h
> +++ b/include/net/route.h

[ ... ]

> @@ -425,17 +425,17 @@ static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
>  						bool *is_v6gw)
>  {
>  	struct net_device *dev = rt->dst.dev;
> -	struct neighbour *neigh;
> +	const __be32 *daddr;
>  
>  	if (likely(rt->rt_gw_family == AF_INET)) {
> -		neigh = ip_neigh_gw4(dev, rt->rt_gw4);
> +		daddr = &rt->rt_gw4;
>  	} else if (rt->rt_gw_family == AF_INET6) {
> -		neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
>  		*is_v6gw = true;
> +		return ip_neigh_gw6(dev, &rt->rt_gw6);
>  	} else {
> -		neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
> +		daddr = &ip_hdr(skb)->daddr;
>  	}
> -	return neigh;
> +	return ip_neigh_gw4(dev, daddr);
>  }
>  
>  #endif	/* _ROUTE_H */
> diff --git a/net/core/filter.c b/net/core/filter.c
> index f42ecfb5841f9..8ee023c87c243 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -2376,7 +2376,7 @@ static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
>  		neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
>  		is_v6gw = true;
>  	} else if (nh->nh_family == AF_INET) {
> -		neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
> +		neigh = ip_neigh_gw4(dev, &nh->ipv4_nh);
>  	} else {
>  		rcu_read_unlock();
>  		goto out_drop;

[Severity: High]
This problem wasn't introduced by this patch, but can bpf_out_neigh_v4()
resolve the neighbour on the wrong device when nh is NULL [1]?

When __bpf_redirect_neigh_v4() resolves an RTN_LOCAL route via
ip_route_output_flow(), rt->dst.dev is set to net->loopback_dev (or the VRF
device) rather than the target redirect device dev:

net/core/filter.c:bpf_out_neigh_v4() {
	...
	skb->dev = dev;
	skb_clear_tstamp(skb);

	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
		skb = skb_expand_head(skb, hh_len);
		if (!skb)
			return -ENOMEM;
	}

	rcu_read_lock();
	if (!nh) {
		struct rtable *rt = skb_rtable(skb);

		neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
	...
}

Because ip_neigh_for_gw() uses rt->dst.dev instead of dev, neigh_output()
is invoked with a loopback_dev neighbour (which uses eth_header_ops) while
skb->dev points to dev.  If dev is an L3 device where dev->header_ops is
NULL (such as a tun interface), bpf_out_neigh_v4() skips skb_expand_head(),
and neigh_output() pushes an Ethernet header without sufficient headroom,
triggering skb_under_panic().

[1] https://sashiko.dev/#/bug/linux-26626829-3abf-4baa-b906-b783eeb1b107

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124130631.1886963-1-xuanqiang.luo@linux.dev?part=2

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established()
  2026-11-24 13:06 ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() Xuanqiang Luo
@ 2026-11-25 13:09   ` sashiko-bot
  0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-25 13:09 UTC (permalink / raw)
  To: Xuanqiang Luo; +Cc: bpf, mptcp

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] tcp: post-FIN segments queued in tcp_rcv_state_process() and
  tcp_data_queue() [1]
--

commit a4b62910075d79274253c31ee8181da76c593e87
Author: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

tcp: avoid a stack canary in tcp_rcv_established()

This commit changes tcp_try_coalesce(), tcp_ooo_try_coalesce() and
tcp_queue_rcv() to return an enum tcp_eaten by value instead of taking a
boolean pointer for fragstolen, avoiding a stack canary in
tcp_rcv_established() and tcp_data_queue().

> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 801c76744ed8a..3d0a8b7e883e4 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c

[ ... ]

> @@ -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)

[Severity: High]
This problem wasn't introduced by this patch, but can a peer queue
post-FIN payload onto sk->sk_receive_queue and advance tp->rcv_nxt after
EOF [1]?

After an in-order FIN has been processed, RCV_SHUTDOWN is set and
tp->rcv_nxt has advanced past the FIN sequence number.  If a socket in
TCP_CLOSE_WAIT, TCP_CLOSING, or TCP_LAST_ACK with a non-zero receive
window receives a segment with seq == tp->rcv_nxt - 1, 1 byte of payload,
and TCPHDR_FIN set (end_seq == tp->rcv_nxt + 1), both checks in
tcp_rcv_state_process() are bypassed:

net/ipv4/tcp_input.c:tcp_rcv_state_process() {
    ...
	case TCP_CLOSE_WAIT:
	case TCP_CLOSING:
	case TCP_LAST_ACK:
		if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
			...
			break;
		}
		fallthrough;
	case TCP_FIN_WAIT1:
	case TCP_FIN_WAIT2:
		if (sk->sk_shutdown & RCV_SHUTDOWN) {
			if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
			    after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
				NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
				tcp_reset(sk, skb);
				return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
			}
		}
		fallthrough;
	case TCP_ESTABLISHED:
		tcp_data_queue(sk, skb);
    ...
}

Subtracting th->fin from end_seq yields tp->rcv_nxt, so the RCV_SHUTDOWN
check does not reset the connection and calls tcp_data_queue().  Because
seq < tp->rcv_nxt < end_seq, tcp_data_queue() treats the packet as a
partial overlap and jumps to queue_and_out, where tcp_queue_rcv() queues
the post-FIN data and advances tp->rcv_nxt.

This corrupts tcp_inq() unread byte accounting after EOF and
desynchronizes tp->copied_seq from kTLS record consumption in
tcp_read_done(), which stops at the first skb carrying TCPHDR_FIN while
tls_strp_check_queue_ok() accepts multi-skb records across FIN boundaries.

[1] https://sashiko.dev/#/bug/linux-ddef83cf-eb5a-48d6-b4ef-687a557a5260

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124130631.1886963-1-xuanqiang.luo@linux.dev?part=5

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
  2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
                   ` (5 preceding siblings ...)
  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 ` gang.yan
  2026-11-27  6:18   ` Xuanqiang Luo
  2026-11-26 10:20 ` David Laight
  7 siblings, 1 reply; 14+ messages in thread
From: gang.yan @ 2026-11-26  8:48 UTC (permalink / raw)
  To: Xuanqiang Luo, netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo

November 24, 2026 at 9:06 PM, "Xuanqiang Luo" <xuanqiang.luo@linux.dev mailto:xuanqiang.luo@linux.dev?to=%22Xuanqiang%20Luo%22%20%3Cxuanqiang.luo%40linux.dev%3E > wrote:


> 
> From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
> 
> With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
> passes the address of one of its locals to another function needs a stack
> canary. The canary is set up and checked on every call, also when the
> local is only used on a path that almost never runs.
> 
> This series removes the canary from functions of the TCP/IPv4 fast paths,
> by returning results by value, by using storage that is not on the stack,
> or by not calling a function that has nothing to do.
> 
> Patch 1 returns the two mptcp_space() values in a structure, so that
> __tcp_select_window() no longer needs a canary.
> 
> Patch 2 changes ip_neigh_gw4() to take a pointer to the address, like
> ip_neigh_gw6(), so that ip_finish_output2() no longer needs a canary.
> 
> Patch 3 uses pskb_may_pull() in icmp_filter(), so that
> raw_local_deliver() no longer needs a canary.
> 
> Patch 4 turns skb_try_coalesce() into an inline wrapper over a function
> returning its results by value, so that its callers no longer need a
> canary because of it.
> 
> Patch 5 returns the result of the TCP receive coalescing by value, so
> that tcp_rcv_established() and tcp_data_queue() no longer need a canary.
> 
> Patch 6 calls bpf_skops_write_hdr_opt(), which needs a canary, only if a
> BPF program reserved room for TCP options.
> 
> The bloat-o-meter numbers are from gcc 16.2 on x86_64. The functions
> named in patches 1 to 5 also lose their canary with clang 23.1.6 on
> x86_64, with and without ThinLTO, and on arm64.
> 

Hi Xuanqiang,

Thanks for this series. Besides the code size, do you have any runtime perf
numbers (TCP_RR, cycles per packet...)? I think it may help the maintainers
judge the real gain.

Thanks
Gang

> Xuanqiang Luo (6):
>  tcp: avoid a stack canary in __tcp_select_window()
>  ipv4: avoid a stack canary in ip_finish_output2()
>  ipv4: raw: do not use skb_header_pointer() in icmp_filter()
>  net: avoid stack canaries in skb_try_coalesce() callers
>  tcp: avoid a stack canary in tcp_rcv_established()
>  tcp: do not call bpf_skops_write_hdr_opt() unless needed
> 
>  include/linux/filter.h | 2 +-
>  include/linux/skbuff.h | 35 ++++++++++-
>  include/net/mptcp.h | 13 ++++-
>  include/net/route.h | 16 +++---
>  net/core/filter.c | 2 +-
>  net/core/skbuff.c | 50 ++++++++--------
>  net/ipv4/raw.c | 25 ++++----
>  net/ipv4/route.c | 15 +++--
>  net/ipv4/tcp_input.c | 99 +++++++++++++++++---------------
>  net/ipv4/tcp_output.c | 25 ++++----
>  net/mptcp/subflow.c | 9 ++-
>  net/netfilter/nf_flow_table_ip.c | 4 +-
>  12 files changed, 178 insertions(+), 117 deletions(-)
> 
> base-commit: a4c450594f5d1dc0fc4daacfb95d40554d99dead
> -- 
> 2.43.0
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
  2026-11-24 13:06 [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths Xuanqiang Luo
                   ` (6 preceding siblings ...)
  2026-11-26  8:48 ` [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths gang.yan
@ 2026-11-26 10:20 ` David Laight
  2026-11-27  6:33   ` Xuanqiang Luo
  7 siblings, 1 reply; 14+ messages in thread
From: David Laight @ 2026-11-26 10:20 UTC (permalink / raw)
  To: Xuanqiang Luo
  Cc: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
	Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
	Matthieu Baerts, Mat Martineau, Geliang Tang, David Ahern,
	Ido Schimmel, Stanislav Fomichev, Neal Cardwell,
	Kuniyuki Iwashima, Pablo Neira Ayuso, Florian Westphal,
	Phil Sutter, bpf, linux-kernel, mptcp, netfilter-devel, coreteam,
	Xuanqiang Luo

On Tue, 24 Nov 2026 22:06:25 +0900
Xuanqiang Luo <xuanqiang.luo@linux.dev> wrote:

> From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
> 
> With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
> passes the address of one of its locals to another function needs a stack
> canary. The canary is set up and checked on every call, also when the
> local is only used on a path that almost never runs.
> 
> This series removes the canary from functions of the TCP/IPv4 fast paths,
> by returning results by value, by using storage that is not on the stack,
> or by not calling a function that has nothing to do.
> 
> Patch 1 returns the two mptcp_space() values in a structure, so that
> __tcp_select_window() no longer needs a canary.

While all (I think) architectures can return a big integer in two
registers (dx:ax on x86), not all ABI do the same for small structures.
For those architectures returning a struct by value is implemented by
passing the address of a hidden on stack structure.
So code generation may be worse than passing the values by reference.
I can't remember the list of architectures, mostly old ones - but not
32bit x86 because the compiler option was set when regparm=3 was added
for 2.6.
(Note that this is different from passing small structures by value.)

David

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
  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
  0 siblings, 0 replies; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-27  6:18 UTC (permalink / raw)
  To: gang.yan, netdev
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Matthieu Baerts, Mat Martineau,
	Geliang Tang, David Ahern, Ido Schimmel, Stanislav Fomichev,
	Neal Cardwell, Kuniyuki Iwashima, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, bpf, linux-kernel, mptcp,
	netfilter-devel, coreteam, Xuanqiang Luo, david.laight.linux

On 2026/11/26 at 17:48 gang.yan@linux.dev wrote:
> November 24, 2026 at 9:06 PM, "Xuanqiang Luo" <xuanqiang.luo@linux.dev mailto:xuanqiang.luo@linux.dev?to=%22Xuanqiang%20Luo%22%20%3Cxuanqiang.luo%40linux.dev%3E > wrote:
> 
> 
>>
>> From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
>>
>> With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
>> passes the address of one of its locals to another function needs a stack
>> canary. The canary is set up and checked on every call, also when the
>> local is only used on a path that almost never runs.
>>
>> This series removes the canary from functions of the TCP/IPv4 fast paths,
>> by returning results by value, by using storage that is not on the stack,
>> or by not calling a function that has nothing to do.
>>
>> Patch 1 returns the two mptcp_space() values in a structure, so that
>> __tcp_select_window() no longer needs a canary.
>>
>> Patch 2 changes ip_neigh_gw4() to take a pointer to the address, like
>> ip_neigh_gw6(), so that ip_finish_output2() no longer needs a canary.
>>
>> Patch 3 uses pskb_may_pull() in icmp_filter(), so that
>> raw_local_deliver() no longer needs a canary.
>>
>> Patch 4 turns skb_try_coalesce() into an inline wrapper over a function
>> returning its results by value, so that its callers no longer need a
>> canary because of it.
>>
>> Patch 5 returns the result of the TCP receive coalescing by value, so
>> that tcp_rcv_established() and tcp_data_queue() no longer need a canary.
>>
>> Patch 6 calls bpf_skops_write_hdr_opt(), which needs a canary, only if a
>> BPF program reserved room for TCP options.
>>
>> The bloat-o-meter numbers are from gcc 16.2 on x86_64. The functions
>> named in patches 1 to 5 also lose their canary with clang 23.1.6 on
>> x86_64, with and without ThinLTO, and on arm64.
>>
> 
> Hi Xuanqiang,
> 
> Thanks for this series. Besides the code size, do you have any runtime perf
> numbers (TCP_RR, cycles per packet...)? I think it may help the maintainers
> judge the real gain.
Hi Gang,

I omitted runtime numbers from the cover letter because earlier cross-NIC
TCP_RR runs were within boot-to-boot noise, and I did not want to overstate
the gain. I re-ran the full 6-patch series against its base commit
(a4c450594f5d) in a tighter setup on an x86_64 server (Hygon C86-4G,
ConnectX-5 10GbE mlx5, GCC 12.3, CONFIG_STACKPROTECTOR_STRONG=y, identical
.config, nokaslr, boost off, isolated CPUs/IRQs, busy_read=0, 3 alternating
boots per kernel x 4 runs):

1. Single-CPU loopback 1-byte TCP_RR (both endpoints on 1 CPU running the
    kernel under test, 99.9% busy with no cross-CPU lock contention; each
    RR counts 2 TX + 2 RX = 4 Tcp InSegs+OutSegs):
    - instructions:k / tx:  33,562.3 -> 33,410.6 (-151.8, -0.45%, CV 0.01%)
    - instructions:k / seg:  8,372.8 ->  8,336.1 ( -36.8, -0.44%, CV 0.02%)
    - branches:k / tx:       7,627.9 ->  7,604.4 ( -23.5, -0.31%, CV 0.03%)
    - cycles:k / tx:        48,883.8 -> 48,613.8 (-270.0, -0.55%)
    - throughput:           52,804   -> 53,068 tps (+0.50%)

2. Cross-host 10GbE mlx5 8-flow 1-byte TCP_RR (server CPU-bound on 1 app
    CPU + 1 IRQ CPU, client pinned on a fixed peer kernel):
    - instructions:k / pkt: 12,040.6 -> 11,946.0 ( -94.6, -0.79%, CV 0.43%)
    - cycles:k / pkt:       19,652.2 -> 19,450.1 (-202.1, -1.03%, CV 0.52%)
    - throughput:           79,455   -> 79,883 tps (+0.54%)

3. Loopback small-write TCP_STREAM (-m 512 -D, 2 CPUs):
    - instructions:k / seg:  5,805.6 ->  5,713.7 ( -91.9, -1.58%)
    - cycles:k / seg:        8,538.5 ->  8,416.8 (-121.7, -1.42%)
    - throughput:            641.8   ->  650.5 Mbit/s (+1.37%)
    (Note: in the 2-CPU / multi-flow cases above, perf shows part of the
     larger per-packet drop comes from fewer spinlock-slowpath and idle-loop
     samples in addition to the direct TCP path savings; test 1 isolates
     the direct instruction/branch reduction.)

4. perf record (cycles:k and instructions:k) confirms
    bpf_skops_write_hdr_opt() drops from 0.04-0.12% to 0.00% across all
    profiled workloads, with consistent drops in tcp_rcv_established(),
    __tcp_transmit_skb(), and __tcp_select_window().

One caveat to be transparent: on a loopback 1-byte UDP_RR control (which
only exercises patch 2's ip_finish_output2() canary removal),
instructions:k/tx changed by the expected -23.2 insns/tx (-0.07%) and
branches:k/tx was flat (+0.02%), yet wall-clock tps/cycles still shifted
by -0.80%/+0.68% due to fixed code-layout/IPC differences under nokaslr.

So the reliable runtime signal here is the deterministic drop in kernel
instructions and branches (~152 insns / ~23.5 branches per loopback TCP_RR)
and the disappearance of bpf_skops_write_hdr_opt() from the profile, while
wall-clock TCP_RR tps/cycles (~0.5%) remain in the same range as
code-alignment effects on this machine.

Thanks,
Xuanqiang

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
  2026-11-26 10:20 ` David Laight
@ 2026-11-27  6:33   ` Xuanqiang Luo
  2026-11-27 10:41     ` David Laight
  0 siblings, 1 reply; 14+ messages in thread
From: Xuanqiang Luo @ 2026-11-27  6:33 UTC (permalink / raw)
  To: David Laight
  Cc: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
	Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
	Matthieu Baerts, Mat Martineau, Geliang Tang, David Ahern,
	Ido Schimmel, Stanislav Fomichev, Neal Cardwell,
	Kuniyuki Iwashima, Pablo Neira Ayuso, Florian Westphal,
	Phil Sutter, bpf, linux-kernel, mptcp, netfilter-devel, coreteam,
	Xuanqiang Luo

On 2026/11/26 at 19:20 David Laight wrote:
> On Tue, 24 Nov 2026 22:06:25 +0900
> Xuanqiang Luo <xuanqiang.luo@linux.dev> wrote:
> 
>> From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
>>
>> With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
>> passes the address of one of its locals to another function needs a stack
>> canary. The canary is set up and checked on every call, also when the
>> local is only used on a path that almost never runs.
>>
>> This series removes the canary from functions of the TCP/IPv4 fast paths,
>> by returning results by value, by using storage that is not on the stack,
>> or by not calling a function that has nothing to do.
>>
>> Patch 1 returns the two mptcp_space() values in a structure, so that
>> __tcp_select_window() no longer needs a canary.
> 
> While all (I think) architectures can return a big integer in two
> registers (dx:ax on x86), not all ABI do the same for small structures.
> For those architectures returning a struct by value is implemented by
> passing the address of a hidden on stack structure.
> So code generation may be worse than passing the values by reference.
> I can't remember the list of architectures, mostly old ones - but not
> 32bit x86 because the compiler option was set when regparm=3 was added
> for 2.6.
> (Note that this is different from passing small structures by value.)
> 
> David

Hi David,

Good point! thanks for bringing that up.

The small-struct-by-value pattern in patches 1 and 4 follows the same
approach Eric used in commit 7a4cd71fa451 ("net: add
vlan_get_protocol_offset_inline() helper"), which returns a 2-field
struct vlan_type_depth to avoid stack canaries in callers.

I did note the 8-byte struct ABI distinction you mentioned:
- On the main 64-bit targets (x86_64, arm64, riscv64, ppc64le, loongarch64)
   as well as 32-bit x86 (with -mregparm=3 / reg-struct-return), both
   8-byte structs (struct mptcp_space_info and struct skb_coalesce_res) are
   returned in registers and the stack canaries are dropped cleanly.
- On ABIs that return structs (> 4 bytes on 32-bit ARM EABI, or any struct
   on s390x) via a hidden stack buffer, it indeed falls back to a hidden
   pointer. Even there, the callers previously passed *two* pointers to
   locals (&space/&full_space in patch 1, &fragstolen/&delta_truesize in
   patch 4), so passing one hidden 8-byte return slot should not be worse
   than before, while keeping the code cleaner and type-safe.

That said, if maintainers prefer packing the two fields into a u64 so that
even 32-bit ARM / s390x return them in registers, I'd be happy to change
patches 1 and 4 to do that in v2.

Thanks,
Xuanqiang

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH net-next v1 0/6] net: avoid stack canaries in TCP/IPv4 fast paths
  2026-11-27  6:33   ` Xuanqiang Luo
@ 2026-11-27 10:41     ` David Laight
  0 siblings, 0 replies; 14+ messages in thread
From: David Laight @ 2026-11-27 10:41 UTC (permalink / raw)
  To: Xuanqiang Luo
  Cc: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
	Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
	Matthieu Baerts, Mat Martineau, Geliang Tang, David Ahern,
	Ido Schimmel, Stanislav Fomichev, Neal Cardwell,
	Kuniyuki Iwashima, Pablo Neira Ayuso, Florian Westphal,
	Phil Sutter, bpf, linux-kernel, mptcp, netfilter-devel, coreteam,
	Xuanqiang Luo

On Fri, 27 Nov 2026 15:33:15 +0900
Xuanqiang Luo <xuanqiang.luo@linux.dev> wrote:

> On 2026/11/26 at 19:20 David Laight wrote:
> > On Tue, 24 Nov 2026 22:06:25 +0900
> > Xuanqiang Luo <xuanqiang.luo@linux.dev> wrote:
> >   
> >> From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
> >>
> >> With CONFIG_STACKPROTECTOR_STRONG=y, a function that has a local array or
> >> passes the address of one of its locals to another function needs a stack
> >> canary. The canary is set up and checked on every call, also when the
> >> local is only used on a path that almost never runs.
> >>
> >> This series removes the canary from functions of the TCP/IPv4 fast paths,
> >> by returning results by value, by using storage that is not on the stack,
> >> or by not calling a function that has nothing to do.
> >>
> >> Patch 1 returns the two mptcp_space() values in a structure, so that
> >> __tcp_select_window() no longer needs a canary.  
> > 
> > While all (I think) architectures can return a big integer in two
> > registers (dx:ax on x86), not all ABI do the same for small structures.
> > For those architectures returning a struct by value is implemented by
> > passing the address of a hidden on stack structure.
> > So code generation may be worse than passing the values by reference.
> > I can't remember the list of architectures, mostly old ones - but not
> > 32bit x86 because the compiler option was set when regparm=3 was added
> > for 2.6.
> > (Note that this is different from passing small structures by value.)
> > 
> > David  
> 
> Hi David,
> 
> Good point! thanks for bringing that up.
> 
> The small-struct-by-value pattern in patches 1 and 4 follows the same
> approach Eric used in commit 7a4cd71fa451 ("net: add
> vlan_get_protocol_offset_inline() helper"), which returns a 2-field
> struct vlan_type_depth to avoid stack canaries in callers.
> 
> I did note the 8-byte struct ABI distinction you mentioned:
> - On the main 64-bit targets (x86_64, arm64, riscv64, ppc64le, loongarch64)
>    as well as 32-bit x86 (with -mregparm=3 / reg-struct-return), both
>    8-byte structs (struct mptcp_space_info and struct skb_coalesce_res) are
>    returned in registers and the stack canaries are dropped cleanly.
> - On ABIs that return structs (> 4 bytes on 32-bit ARM EABI, or any struct
>    on s390x) via a hidden stack buffer, it indeed falls back to a hidden
>    pointer. Even there, the callers previously passed *two* pointers to
>    locals (&space/&full_space in patch 1, &fragstolen/&delta_truesize in
>    patch 4), so passing one hidden 8-byte return slot should not be worse
>    than before, while keeping the code cleaner and type-safe.

There might be a subtle difference in the function itself.
I think the result structure gets allocated on stack and then a memcpy()
used to copy that to the caller's buffer during the function return.
That may, or may not, be faster.

> 
> That said, if maintainers prefer packing the two fields into a u64 so that
> even 32-bit ARM / s390x return them in registers, I'd be happy to change
> patches 1 and 4 to do that in v2.

That is horrid ... and you'd need to use a u128 on 64bit.
It really is a shame that C doesn't have a syntax for returning 2 integers
even though pretty much all ABI have always supported it.

	David

> 
> Thanks,
> Xuanqiang


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-11-27 10:41 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH net-next v1 5/6] tcp: avoid a stack canary in tcp_rcv_established() Xuanqiang Luo
2026-11-25 13:09   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox