mirror of https://lore.kernel.org/lkml/
 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 2/6] ipv4: avoid a stack canary in ip_finish_output2()
Date: Tue, 24 Nov 2026 22:06:27 +0900	[thread overview]
Message-ID: <20261124130631.1886963-3-xuanqiang.luo@linux.dev> (raw)
In-Reply-To: <20261124130631.1886963-1-xuanqiang.luo@linux.dev>

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


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

Thread overview: 7+ 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 ` Xuanqiang Luo [this message]
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-24 13:06 ` [PATCH net-next v1 6/6] tcp: do not call bpf_skops_write_hdr_opt() unless needed Xuanqiang Luo

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-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®