Skip to contentSkip to conversation

net: keep skbs with a prefetched socket out of gro_cells gro

PATCH Superseded Nikolay Aleksandrov 4 participants 7 messages Nov 21 – Nov 26

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: keep skbs with a prefetched socket out of gro_cells gro

bpf_sk_assign at tc ingress holds a reference on a socket that is not rcu freed, such as an established tcp socket, until the receive path steals it. ip_rcv_core keeps a prefetched socket so when the assigned packet is an ipip, gre or esp packet the socket stays attached after decap and their rx paths pass the skb to gro_cells_receive. GRO merges skbs with the same skb->sk into one flow and clears skb->sk and skb->destructor of each merged skb without calling the destructor so every merged skb leaks a socket ref.

Send skbs with a prefetched socket to netif_rx instead as gro_cells_receive already does for cloned skbs, where each skb is consumed on its own and no reference is lost.

skb_sk_is_prefetched tests skb->destructor which shares a cache line with skb->cloned that is read just before so the common case adds a single cheap test.

Reproduced with a tcx ingress program on a veth in a receiver netns with an ipip tunnel which assigns an established tcp socket to ipip packets. The sender sends 32 tcp segments of one flow inside ipip through a netem slot qdisc so they are decapsulated in one batch and merged by gro. Then the socket is closed and the receiver netns is deleted. The leaked socket holds a ref on the netns so it is never torn down and its devices are never unregistered:

Before:
bpf_sk_assign() calls 32, sk_refcnt 32 after the burst receiver netns deleted, 30s later its veth peer still exists

After:
bpf_sk_assign() calls 32, sk_refcnt 1 after the burst receiver netns deleted, its veth peer is gone

The problem was found during reviews of patches that add bpf_sk_assign egress support.

Fixes cf7fbe660f2d ("bpf: Add socket assign support")

---
CC: David S. Miller <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Jakub Kicinski <[email protected]>
CC: Paolo Abeni <[email protected]>
CC: Simon Horman <[email protected]>
CC: Alexei Starovoitov <[email protected]>
CC: Martin KaFai Lau <[email protected]>
CC: Joe Stringer <[email protected]>
CC: Daniel Borkmann <[email protected]>
CC: [email protected]

 net/core/gro_cells.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/gro_cells.c +3 -1
index 31352cac355c..8c74b2a1227b 100644
@@ -4,6 +4,7 @@
 #include <linux/netdevice.h>
 #include <net/gro_cells.h>
 #include <net/hotdata.h>
+#include <net/sock.h>
 
 struct gro_cell {
 	struct sk_buff_head	napi_skbs;
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
 
 	skb_unset_transport_header(skb);
 
-	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+	    unlikely(skb_sk_is_prefetched(skb))) {
 		res = netif_rx(skb);
 		goto unlock;
 	}
Signature · 1 line
2.47.3
#1 lore raw
Quoted 50 lines · Nikolay Aleksandrov

bpf_sk_assign at tc ingress holds a reference on a socket that is not rcu freed, such as an established tcp socket, until the receive path steals it. ip_rcv_core keeps a prefetched socket so when the assigned packet is an ipip, gre or esp packet the socket stays attached after decap and their rx paths pass the skb to gro_cells_receive. GRO merges skbs with the same skb->sk into one flow and clears skb->sk and skb->destructor of each merged skb without calling the destructor so every merged skb leaks a socket ref.

Send skbs with a prefetched socket to netif_rx instead as gro_cells_receive already does for cloned skbs, where each skb is consumed on its own and no reference is lost.

skb_sk_is_prefetched tests skb->destructor which shares a cache line with skb->cloned that is read just before so the common case adds a single cheap test.

Reproduced with a tcx ingress program on a veth in a receiver netns with an ipip tunnel which assigns an established tcp socket to ipip packets. The sender sends 32 tcp segments of one flow inside ipip through a netem slot qdisc so they are decapsulated in one batch and merged by gro. Then the socket is closed and the receiver netns is deleted. The leaked socket holds a ref on the netns so it is never torn down and its devices are never unregistered:

Before:
bpf_sk_assign() calls 32, sk_refcnt 32 after the burst receiver netns deleted, 30s later its veth peer still exists

After:
bpf_sk_assign() calls 32, sk_refcnt 1 after the burst receiver netns deleted, its veth peer is gone

The problem was found during reviews of patches that add bpf_sk_assign egress support.

Fixes cf7fbe660f2d ("bpf: Add socket assign support")

---
CC: David S. Miller <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Jakub Kicinski <[email protected]>
CC: Paolo Abeni <[email protected]>
CC: Simon Horman <[email protected]>
CC: Alexei Starovoitov <[email protected]>
CC: Martin KaFai Lau <[email protected]>
CC: Joe Stringer <[email protected]>
CC: Daniel Borkmann <[email protected]>
CC: [email protected]

Quoted 3 lines
  net/core/gro_cells.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

TBH it can be applied to next, this is an old problem ("never worked") that is a corner case found by LLM reviews of a patch-set, which would make it easier to reach. I've tested it with net-next as well.

Cheers,
Nik

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Nikolay Aleksandrov
Quoted 34 lines · Nikolay Aleksandrov

bpf_sk_assign at tc ingress holds a reference on a socket that is not rcu freed, such as an established tcp socket, until the receive path steals it. ip_rcv_core keeps a prefetched socket so when the assigned packet is an ipip, gre or esp packet the socket stays attached after decap and their rx paths pass the skb to gro_cells_receive. GRO merges skbs with the same skb->sk into one flow and clears skb->sk and skb->destructor of each merged skb without calling the destructor so every merged skb leaks a socket ref.

Send skbs with a prefetched socket to netif_rx instead as gro_cells_receive already does for cloned skbs, where each skb is consumed on its own and no reference is lost.

skb_sk_is_prefetched tests skb->destructor which shares a cache line with skb->cloned that is read just before so the common case adds a single cheap test.

Reproduced with a tcx ingress program on a veth in a receiver netns with an ipip tunnel which assigns an established tcp socket to ipip packets. The sender sends 32 tcp segments of one flow inside ipip through a netem slot qdisc so they are decapsulated in one batch and merged by gro. Then the socket is closed and the receiver netns is deleted. The leaked socket holds a ref on the netns so it is never torn down and its devices are never unregistered:

Before:
bpf_sk_assign() calls 32, sk_refcnt 32 after the burst receiver netns deleted, 30s later its veth peer still exists

After:
bpf_sk_assign() calls 32, sk_refcnt 1 after the burst receiver netns deleted, its veth peer is gone

Quoted 5 lines

The problem was found during reviews of patches that add bpf_sk_assign egress support.

Fixes cf7fbe660f2d ("bpf: Add socket assign support")

Right, GRO callers are supposed to only feed skbs with no socket, or sock_wfree() ones (see skb_prepare_for_gro() used by veth), and gro_cells never enforced this.

#3 lore raw
In reply to Nikolay Aleksandrov
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete.

--

net: keep skbs with a prefetched socket out of gro_cells gro

This patch makes gro_cells_receive() send skbs that carry a prefetched socket from bpf_sk_assign() to netif_rx() instead of the GRO cell. The goal is to stop GRO from clearing skb->sk and skb->destructor on merged skbs, which leaks one socket reference per merged skb.

Quoted 4 lines
net/core/gro_cells.c +0 -0
index 31352cac355ce..4e8b9c3cd7da0 100644

[ ... ]

net/core/gro_cells.c int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) · line 31

Quoted 7 lines
net/core/gro_cells.c +2 -1
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
 
 	skb_unset_transport_header(skb);
 
-	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+	    unlikely(skb_sk_is_prefetched(skb))) {

[Severity: High]
Is matching only skb->destructor == sock_pfree enough here? An skb that got its socket from bpf_sk_assign() can still reach gro_cells_receive() with a different destructor that owns one sk_refcnt reference per skb.

One path: bpf_sk_assign() at tc ingress attaches a non-refcounted socket, such as a TCP listener or a UDP socket. It sets sock_pfree and takes no reference. ip_rcv_core() keeps it. Then an NFQUEUE rule matches the outer ipip/gre/esp packet, and __nf_queue() converts it:

net/netfilter/nf_queue.c:__nf_queue() {
    ...
	if (skb_sk_is_prefetched(skb)) {
		struct sock *sk = skb->sk;

		if (!sk_is_refcounted(sk)) {
			if (!refcount_inc_not_zero(&sk->sk_refcnt))
				return -ENOTCONN;

			/* drop refcount on skb_orphan */
			skb->destructor = sock_edemux;
		}
	}
    ...
}

After an NF_ACCEPT reinject, the packet is decapsulated. With !xnet, skb_scrub_packet() does not orphan it, so the skb arrives here with skb->sk set and destructor == sock_edemux. skb_sk_is_prefetched() now returns false, and the skb is queued for GRO.

skb_gro_receive() then clears both fields on each merged skb without calling sock_edemux():

net/core/gro.c:skb_gro_receive() {
    ...
	/* sk ownership - if any - completely transferred to the aggregated packet */
	skb->destructor = NULL;
	skb->sk = NULL;
    ...
}

The frag_list merge path in the same function does the same thing. gro_list_prepare() still allows the merge because p->sk == skb->sk.

Wouldn't each merged skb then leak the reference that __nf_queue() took? That is the same socket and netns teardown leak described in the commit message.

The tree already has a general rule for this in skb_prepare_for_gro():

include/net/sock.h:skb_prepare_for_gro() {
	if (skb->destructor != sock_wfree) {
		skb_orphan(skb);
		return;
	}
	skb->slow_gro = 1;
}

veth_xdp_rcv_skb() calls it before napi_gro_receive(). Could gro_cells_receive() divert or orphan any skb that still has skb->sk set with a destructor other than sock_wfree, rather than checking for sock_pfree only? Checking skb->sk directly would be one way to do it.

Separately, the "sk ownership - if any - completely transferred to the aggregated packet" comments in skb_gro_receive() look wrong for destructors that hold one reference per skb (sock_pfree on refcounted sockets, sock_edemux, sock_efree).

Quoted 3 lines
net/core/gro_cells.c +0 -0
 		res = netif_rx(skb);
 		goto unlock;
 	}
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121160204.4024204-1-razor%40blackwall.org
#4 lore raw
Quoted 42 lines · On 24/11/2026 05:03, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete.

--

net: keep skbs with a prefetched socket out of gro_cells gro

This patch makes gro_cells_receive() send skbs that carry a prefetched socket from bpf_sk_assign() to netif_rx() instead of the GRO cell. The goal is to stop GRO from clearing skb->sk and skb->destructor on merged skbs, which leaks one socket reference per merged skb.

Quoted 4 lines
net/core/gro_cells.c +0 -0
index 31352cac355ce..4e8b9c3cd7da0 100644

[ ... ]

net/core/gro_cells.c int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) · line 31

Quoted 7 lines
net/core/gro_cells.c +2 -1
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
 
  	skb_unset_transport_header(skb);
 
-	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+	    unlikely(skb_sk_is_prefetched(skb))) {

[Severity: High]
Is matching only skb->destructor == sock_pfree enough here? An skb that got its socket from bpf_sk_assign() can still reach gro_cells_receive() with a different destructor that owns one sk_refcnt reference per skb.

Quoted 5 lines

One path: bpf_sk_assign() at tc ingress attaches a non-refcounted socket, such as a TCP listener or a UDP socket. It sets sock_pfree and takes no reference. ip_rcv_core() keeps it. Then an NFQUEUE rule matches the outer ipip/gre/esp packet, and __nf_queue() converts it:

Oh well the bot seems right even though it's a weird case, I'd suggest to change the check and orphan on any skb->sk set, i.e.: || unlikely(skb->sk) instead of unlikely(skb_sk_is_prefetched(skb)), that is cache hot too as skb->dev is read before and will catch

Also tried asking an LLM if I'm missing something and the only case it could come up that will not be aggregated is:
"the only correct aggregation that's lost is software ESP over veth or netdevsim in tunnel mode" IMO that is ok.

What do you think?

Quoted 63 lines
net/netfilter/nf_queue.c:__nf_queue() {
     ...
	if (skb_sk_is_prefetched(skb)) {
		struct sock *sk = skb->sk;

		if (!sk_is_refcounted(sk)) {
			if (!refcount_inc_not_zero(&sk->sk_refcnt))
				return -ENOTCONN;

			/* drop refcount on skb_orphan */
			skb->destructor = sock_edemux;
		}
	}
     ...
}

After an NF_ACCEPT reinject, the packet is decapsulated. With !xnet, skb_scrub_packet() does not orphan it, so the skb arrives here with skb->sk set and destructor == sock_edemux. skb_sk_is_prefetched() now returns false, and the skb is queued for GRO.

skb_gro_receive() then clears both fields on each merged skb without calling sock_edemux():

net/core/gro.c:skb_gro_receive() {
     ...
	/* sk ownership - if any - completely transferred to the aggregated packet */
	skb->destructor = NULL;
	skb->sk = NULL;
     ...
}

The frag_list merge path in the same function does the same thing. gro_list_prepare() still allows the merge because p->sk == skb->sk.

Wouldn't each merged skb then leak the reference that __nf_queue() took? That is the same socket and netns teardown leak described in the commit message.

The tree already has a general rule for this in skb_prepare_for_gro():

include/net/sock.h:skb_prepare_for_gro() {
	if (skb->destructor != sock_wfree) {
		skb_orphan(skb);
		return;
	}
	skb->slow_gro = 1;
}

veth_xdp_rcv_skb() calls it before napi_gro_receive(). Could gro_cells_receive() divert or orphan any skb that still has skb->sk set with a destructor other than sock_wfree, rather than checking for sock_pfree only? Checking skb->sk directly would be one way to do it.

Separately, the "sk ownership - if any - completely transferred to the aggregated packet" comments in skb_gro_receive() look wrong for destructors that hold one reference per skb (sock_pfree on refcounted sockets, sock_edemux, sock_efree).

Quoted 3 lines
net/core/gro_cells.c +0 -0
  		res = netif_rx(skb);
  		goto unlock;
  	}
#5 lore raw
Quoted 59 lines · Nikolay Aleksandrov
Quoted 8 lines · On 24/11/2026 05:03, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let

patchwork

Quoted 1 line

know by including "pw-bot: cr" as a separate line at the end of your

reply

Quoted 13 lines

(one such reply per series is enough).

- [High] The fix is incomplete.

--

net: keep skbs with a prefetched socket out of gro_cells gro

This patch makes gro_cells_receive() send skbs that carry a prefetched socket from bpf_sk_assign() to netif_rx() instead of the GRO cell. The goal is to stop GRO from clearing skb->sk and skb->destructor on merged skbs, which leaks one socket reference per merged skb.

Quoted 8 lines
Quoted 4 lines
net/core/gro_cells.c +0 -0
index 31352cac355ce..4e8b9c3cd7da0 100644

[ ... ]

net/core/gro_cells.c int gro_cells_receive(struct gro_cells *gcells,

Quoted 1 line
net/core/gro_cells.c +0 -0
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells,  

struct sk_buff *skb)

Quoted 12 lines

net/core/gro_cells.c int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) · line 31

Quoted 6 lines
net/core/gro_cells.c +2 -1
  	skb_unset_transport_header(skb);
 
-	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+	    unlikely(skb_sk_is_prefetched(skb))) {

[Severity: High]
Is matching only skb->destructor == sock_pfree enough here? An skb that got its socket from bpf_sk_assign() can still reach gro_cells_receive() with a different destructor that owns one sk_refcnt reference per skb.

Quoted 1 line

One path: bpf_sk_assign() at tc ingress attaches a non-refcounted

socket,

Quoted 2 lines

such as a TCP listener or a UDP socket. It sets sock_pfree and takes no reference. ip_rcv_core() keeps it. Then an NFQUEUE rule matches the

outer

Quoted 2 lines

ipip/gre/esp packet, and __nf_queue() converts it:

Oh well the bot seems right even though it's a weird case, I'd suggest to change the check and orphan on any skb->sk set, i.e.: || unlikely(skb->sk) instead of unlikely(skb_sk_is_prefetched(skb)), that is cache hot too as skb->dev is read before and will catch

Quoted 6 lines

Also tried asking an LLM if I'm missing something and the only case it could come up that will not be aggregated is:
"the only correct aggregation that's lost is software ESP over veth or netdevsim in tunnel mode" IMO that is ok.

What do you think?

This also affects ESP over tuntap with IFF_NAPI. But that is not a common production configuration.

So LGTM.

Just a small note that redirect to netif_rx != orphan. No need to orphan.

#6 lore raw
Quoted 60 lines · On 26/11/2026 02:36, [email protected] wrote:
Quoted 58 lines · Nikolay Aleksandrov
Quoted 42 lines · On 24/11/2026 05:03, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete.

--

net: keep skbs with a prefetched socket out of gro_cells gro

This patch makes gro_cells_receive() send skbs that carry a prefetched socket from bpf_sk_assign() to netif_rx() instead of the GRO cell. The goal is to stop GRO from clearing skb->sk and skb->destructor on merged skbs, which leaks one socket reference per merged skb.

Quoted 4 lines
net/core/gro_cells.c +0 -0
index 31352cac355ce..4e8b9c3cd7da0 100644

[ ... ]

net/core/gro_cells.c int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb) · line 31

Quoted 7 lines
net/core/gro_cells.c +2 -1
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
 
        skb_unset_transport_header(skb);
 
-    if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+    if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+        unlikely(skb_sk_is_prefetched(skb))) {

[Severity: High]
Is matching only skb->destructor == sock_pfree enough here? An skb that got its socket from bpf_sk_assign() can still reach gro_cells_receive() with a different destructor that owns one sk_refcnt reference per skb.

Quoted 5 lines

One path: bpf_sk_assign() at tc ingress attaches a non-refcounted socket, such as a TCP listener or a UDP socket. It sets sock_pfree and takes no reference. ip_rcv_core() keeps it. Then an NFQUEUE rule matches the outer ipip/gre/esp packet, and __nf_queue() converts it:

Oh well the bot seems right even though it's a weird case, I'd suggest to change the check and orphan on any skb->sk set, i.e.: || unlikely(skb->sk) instead of unlikely(skb_sk_is_prefetched(skb)), that is cache hot too as skb->dev is read before and will catch

Also tried asking an LLM if I'm missing something and the only case it could come up that will not be aggregated is:
"the only correct aggregation that's lost is software ESP over veth or netdevsim in tunnel mode" IMO that is ok.

Quoted 8 lines
Quoted 1 line

What do you think?

This also affects ESP over tuntap with IFF_NAPI. But that is not a common production configuration.

So LGTM.

Just a small note that redirect to netif_rx != orphan. No need to orphan.

Oh yes, of course. I was thinking and working on bpf_sk_assign egress support which needs to orphan in a few places and blurped orphan when replying :)

Thanks!