Skip to contentSkip to conversation

net: prevent mixing incompatible memory providers in skbs

PATCH Superseded Mina Almasry 3 participants 11 messages Nov 15 – Nov 19

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover net: prevent mixing incompatible memory providers in skbs

Enforce that all fragments in a nonlinear skb come from compatible memory providers (and for devmem, the exact same binding).

Patch 1 fixes skb->unreadable propagation in skb_split() and skb_shift(). Patch 2 adds skb_net_iov_provider(), skb_mem_provider_compat(), and skb_frags_can_coalesce(), and enforces provider/binding compatibility across TX, TCP, GRO, skb_shift(), and skb_try_coalesce(). Patch 3 propagates the skb_zcopy() uarg to the probe skb in tcp_mtu_probe() / tcp_clone_payload() and rejects coalescing across different uargs.

v4 -> v5:
- Drop skb_has_net_iovs() and check skb_frags_readable() and skb_shinfo(skb)->nr_frags directly in skb_net_iov_provider() and net_devmem_skb_binding().
- Add patch 3 to propagate the skb_zcopy() uarg in tcp_mtu_probe() / tcp_clone_payload().
- v4: https://lore.kernel.org/netdev/[email protected]/

v3 -> v4:
- Fold skb_shinfo(skb)->nr_frags into skb_has_net_iovs() and drop redundant nr_frags checks in skb_net_iov_provider() and net_devmem_skb_binding() (Stanislav Fomichev).
- Remove the empty-skb check and redundant !skb_has_net_iovs(skb1) branch from skb_mem_provider_compat(), and add a comment on the
early skb_has_net_iovs() check (Stanislav Fomichev).
- Propagate skb->unreadable and compare skb_frag_netmem() directly in tcp_clone_payload() instead of rejecting unreadable skbs in tcp_can_coalesce_send_queue_head().
- v3: https://lore.kernel.org/netdev/[email protected]/

v2 -> v3:
- Drop patch 2 ('netmem: store memory provider private pointer directly in struct net_iov') and compare net_iov_owner(niov) (for devmem) / niov->desc.pp (for other net_iov providers) directly in skb_mem_provider_compat() (Stanislav Fomichev).
- Squash the skb_frags_can_coalesce() helper patch into its caller patch (Stanislav Fomichev).
- v2: https://lore.kernel.org/netdev/[email protected]/

v1 -> v2:
- Generalize the check across TX, TCP collapse, GRO, IP defrag, skb_shift(), and skb_try_coalesce() by storing mp_priv directly in struct net_iov and introducing skb_frags_can_coalesce().
- Fix skb->unreadable propagation in skb_split() and skb_shift().
- Add KUnit coverage for cross-provider and cross-binding rejection.
- v1: https://lore.kernel.org/netdev/[email protected]/

Mina Almasry (3):
net: fix skb->unreadable propagation in skb_split() and skb_shift()
net: prevent mixing frags from different memory providers or bindings
tcp: propagate skb_zcopy() uarg in tcp_mtu_probe()

 include/linux/skbuff.h | 55 ++++++++++++++++++++++++++++++++++++++++++
 include/net/tcp.h      |  5 ++--
 net/core/datagram.c    |  4 +--
 net/core/dev.c         | 14 ++++-------
 net/core/devmem.h      | 22 +++++++++++++++++
 net/core/gro.c         | 16 ++++--------
 net/core/net_test.c    | 42 +++++++++++++++++++++++++++-----
 net/core/skbuff.c      | 32 ++++++++++++------------
 net/ipv4/tcp.c         |  3 ++-
 net/ipv4/tcp_output.c  |  9 +++++--
 10 files changed, 153 insertions(+), 49 deletions(-)

base-commit: 6964ff21c5d8b75649211a52dbfd92753e605649

Signature · 1 line
2.56.0.611.g1030f0b940-goog
#1 lore raw

PATCH v5 1/3 net: fix skb->unreadable propagation in skb_split() and skb_shift()

skb_split() and skb_shift() do not accurately maintain skb->unreadable when moving fragments between skbs:

- skb_split_inside_header() moves all fragments out of skb into skb1 (leaving skb with nr_frags == 0 and only host-readable linear memory), but leaves skb->unreadable set on skb.
- skb_split_no_header() unconditionally copies skb->unreadable to skb1 even if 0 fragments were moved to skb1 (k == 0), and leaves skb->unreadable set on skb when splitting at skb_headlen(skb) moves all fragments into skb1 (!skb_shinfo(skb)->nr_frags).
- skb_shift() does not clear skb->unreadable after all fragments are shifted out of skb.

Fix all three cases so skb->unreadable accurately tracks whether the remaining fragments in each skb are unreadable.

This issue is theoretical and was found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
---
 net/core/skbuff.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/core/skbuff.c +7 -1
index 2e81aacc998b..4b18717e230e 100644
@@ -4508,6 +4508,7 @@ static inline void skb_split_inside_header(struct sk_buff *skb,
 
 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
 	skb1->unreadable	   = skb->unreadable;
+	skb->unreadable		   = 0;
 	skb_shinfo(skb)->nr_frags  = 0;
 	skb1->data_len		   = skb->data_len;
 	skb1->len		   += skb1->data_len;
@@ -4556,7 +4557,10 @@ static inline void skb_split_no_header(struct sk_buff *skb,
 	}
 	skb_shinfo(skb1)->nr_frags = k;
 
-	skb1->unreadable = skb->unreadable;
+	if (k)
+		skb1->unreadable = skb->unreadable;
+	if (!skb_shinfo(skb)->nr_frags)
+		skb->unreadable = 0;
 }
 
 /**
@@ -4730,6 +4734,8 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
 	skb->ip_summed = CHECKSUM_PARTIAL;
 
 	skb_shinfo(tgt)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
+	if (!skb_shinfo(skb)->nr_frags && !skb_has_frag_list(skb))
+		skb->unreadable = 0;
 
 	skb_len_add(skb, -shiftlen);
 	skb_len_add(tgt, shiftlen);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#2 lore raw

PATCH v5 2/3 net: prevent mixing frags from different memory providers or bindings

In reply to Mina Almasry

Prevent coalescing, shifting, or appending skb fragments from incompatible memory providers or different devmem bindings.

Add skb_net_iov_provider(), skb_mem_provider_compat(), and skb_frags_can_coalesce() to check readability and fragment provider compatibility (net_iov_owner(niov) for devmem, niov->desc.pp for other net_iov providers such as io_uring zcrx, and NULL for page-backed skbs) across skb_try_coalesce(), skb_shift(), skb_gro_receive(), skb_gro_receive_list(), tcp_skb_can_collapse(), and tcp_skb_can_collapse_rx().

Also add net_devmem_skb_binding() and use it in tcp_sendmsg_locked(), zerocopy_fill_skb_from_devmem(), and validate_xmit_unreadable_skb() so TX skbs cannot mix different devmem bindings or transmit non-devmem unreadable frags on DMA TX devices. Reject converting linear head_frag payloads into page frags on unreadable skbs in skb_try_coalesce() and skb_gro_receive(), and propagate skb->unreadable and compare skb_frag_netmem() in tcp_clone_payload().

Update netmem_ref_test_net_iov and netmem_ref_test_try_coalesce so their target skbs carry a compatible unreadable net_iov frag before shifting or coalescing.

This issue was found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Fixes bd61848900bf ("net: devmem: Implement TX path")
---
 include/linux/skbuff.h | 55 ++++++++++++++++++++++++++++++++++++++++++
 include/net/tcp.h      |  5 ++--
 net/core/datagram.c    |  4 +--
 net/core/dev.c         | 14 ++++-------
 net/core/devmem.h      | 22 +++++++++++++++++
 net/core/gro.c         | 16 ++++--------
 net/core/net_test.c    | 42 +++++++++++++++++++++++++++-----
 net/core/skbuff.c      | 24 +++++++-----------
 net/ipv4/tcp.c         |  3 ++-
 net/ipv4/tcp_output.c  |  4 ++-
 10 files changed, 142 insertions(+), 47 deletions(-)
include/linux/skbuff.h +55 -0
index 1d6832878930..867e3ab17c17 100644
@@ -3770,6 +3770,61 @@ static inline void *skb_frag_address(const skb_frag_t *frag)
 	return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
 }
 
+/* Return an opaque pointer identifying the memory provider instance of @skb's
+ * net_iov fragments:
+ * - For devmem (NET_IOV_DMABUF), net_iov_owner(niov) (&binding->area, 1:1 with
+ *   the binding across RX queues and TX).
+ * - For other net_iov providers (e.g., io_uring zcrx), niov->desc.pp (shared
+ *   across all areas of an io_zcrx_ifq).
+ */
+static inline void *skb_net_iov_provider(const struct sk_buff *skb)
+{
+	struct net_iov *niov;
+
+	if (skb_frags_readable(skb) || !skb_shinfo(skb)->nr_frags)
+		return NULL;
+
+	niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+	if (!niov)
+		return NULL;
+
+	if (net_is_devmem_iov(niov))
+		return net_iov_owner(niov);
+
+	return niov->desc.pp;
+}
+
+/* Return true if the memory providers of @skb1 and @skb2 are compatible
+ * for coalescing. For any struct page-backed provider (the buddy allocator
+ * or a future provider handing out struct pages), coalescing is assumed
+ * feasible. For net_iov-backed providers, the provider instance pointer
+ * must match.
+ */
+static inline bool skb_mem_provider_compat(const struct sk_buff *skb1,
+					   const struct sk_buff *skb2)
+{
+	/* Check skb_frags_readable() first to reject readable/unreadable
+	 * mismatches and avoid chasing frags[0] and struct net_iov when
+	 * only one skb has unreadable frags.
+	 */
+	if (skb_frags_readable(skb1) != skb_frags_readable(skb2))
+		return false;
+
+	return skb_net_iov_provider(skb1) == skb_net_iov_provider(skb2);
+}
+
+static inline bool skb_frags_can_coalesce(struct sk_buff *to,
+					  struct sk_buff *from)
+{
+	if (to->pp_recycle != from->pp_recycle)
+		return false;
+
+	if (skb_zcopy(to) || skb_zcopy(from))
+		return false;
+
+	return skb_mem_provider_compat(to, from);
+}
+
 /**
  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  * @frag: the paged fragment buffer
include/net/tcp.h +3 -2
index 25d11fab1b03..bf5f28e52fca 100644
@@ -1275,14 +1275,15 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 		      mptcp_skb_can_collapse(to, from) &&
 		      !skb_cmp_decrypted(to, from) &&
 		      skb_pure_zcopy_same(to, from) &&
-		      skb_frags_readable(to) == skb_frags_readable(from));
+		      skb_mem_provider_compat(to, from));
 }
 
 static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
 					   const struct sk_buff *from)
 {
 	return likely(mptcp_skb_can_collapse(to, from) &&
-		      !skb_cmp_decrypted(to, from));
+		      !skb_cmp_decrypted(to, from) &&
+		      skb_mem_provider_compat(to, from));
 }
 
 /* Events passed to congestion control interface */
net/core/datagram.c +2 -2
index ecc30a6f8302..88cc0a6c62f6 100644
@@ -712,8 +712,8 @@ zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
 	size_t virt_addr, size, off;
 	struct net_iov *niov;
 
-	if (i && skb_frags_readable(skb))
-		return -EFAULT;
+	if (i && net_devmem_skb_binding(skb) != binding)
+		return -EMSGSIZE;
 
 	/* Devmem filling works by taking an IOVEC from the user where the
 	 * iov_addrs are interpreted as an offset in bytes into the dma-buf to
net/core/dev.c +5 -9
index a230ecce9327..51f5e4800eee 100644
@@ -4175,8 +4175,7 @@ static struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
 static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
 						    struct net_device *dev)
 {
-	struct skb_shared_info *shinfo;
-	struct net_iov *niov;
+	struct net_devmem_dmabuf_binding *binding;
 
 	if (likely(skb_frags_readable(skb) ||
 		   dev->netmem_tx == NETMEM_TX_NO_DMA))
@@ -4185,14 +4184,11 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
 	if (dev->netmem_tx == NETMEM_TX_NONE)
 		goto out_free;
 
-	shinfo = skb_shinfo(skb);
-
 	/* All net_iovs in an unreadable skb share the same binding. */
-	if (shinfo->nr_frags > 0) {
-		niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
-		if (net_is_devmem_iov(niov) &&
-		    (net_devmem_iov_binding(niov)->direction != DMA_TO_DEVICE ||
-		     READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev))
+	if (skb_shinfo(skb)->nr_frags > 0) {
+		binding = net_devmem_skb_binding(skb);
+		if (!binding || binding->direction != DMA_TO_DEVICE ||
+		    READ_ONCE(binding->dev) != dev)
 			goto out_free;
 	}
 
net/core/devmem.h +22 -0
index c4f476947dfa..c685288f5021 100644
@@ -10,6 +10,7 @@
 #ifndef _NET_DEVMEM_H
 #define _NET_DEVMEM_H
 
+#include <linux/skbuff.h>
 #include <net/net_namespace.h>
 #include <net/netmem.h>
 #include <net/netdev_netlink.h>
@@ -117,6 +118,21 @@ net_devmem_iov_binding(const struct net_iov *niov)
 			    struct net_devmem_dmabuf_binding, area);
 }
 
+static inline struct net_devmem_dmabuf_binding *
+net_devmem_skb_binding(const struct sk_buff *skb)
+{
+	struct net_iov *niov;
+
+	if (skb_frags_readable(skb) || !skb_shinfo(skb)->nr_frags)
+		return NULL;
+
+	niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+	if (!niov || !net_is_devmem_iov(niov))
+		return NULL;
+
+	return net_devmem_iov_binding(niov);
+}
+
 static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
 {
 	return net_devmem_iov_binding(niov)->id;
@@ -250,6 +266,12 @@ net_devmem_iov_binding(const struct net_iov *niov)
 {
 	return NULL;
 }
+
+static inline struct net_devmem_dmabuf_binding *
+net_devmem_skb_binding(const struct sk_buff *skb)
+{
+	return NULL;
+}
 #endif
 
 #endif /* _NET_DEVMEM_H */
net/core/gro.c +5 -11
index c093fe388dfe..4671f6346df9 100644
@@ -100,16 +100,7 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 	struct sk_buff *lp;
 	int segs;
 
-	/* Do not splice page pool based packets w/ non-page pool
-	 * packets. This can result in reference count issues as page
-	 * pool pages will not decrement the reference count and will
-	 * instead be immediately returned to the pool or have frag
-	 * count decremented.
-	 */
-	if (p->pp_recycle != skb->pp_recycle)
-		return -ETOOMANYREFS;
-
-	if (skb_zcopy(p) || skb_zcopy(skb))
+	if (!skb_frags_can_coalesce(p, skb))
 		return -ETOOMANYREFS;
 
 	if (unlikely(p->len + len >= netif_get_gro_max_size(p->dev, p) ||
@@ -158,7 +149,7 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 
 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
 		goto done;
-	} else if (skb->head_frag) {
+	} else if (skb->head_frag && skb_frags_readable(skb)) {
 		int nr_frags = pinfo->nr_frags;
 		skb_frag_t *frag = pinfo->frags + nr_frags;
 		struct page *page = virt_to_head_page(skb->head);
@@ -233,6 +224,9 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 
 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
 {
+	if (!skb_mem_provider_compat(p, skb))
+		return -ETOOMANYREFS;
+
 	/* make sure to check flush flag and to not merge */
 	if (unlikely(p->len + skb->len >= 65536 ||
 		     NAPI_GRO_CB(skb)->flush))
net/core/net_test.c +36 -6

75 lines not on this page · raw message

net/core/skbuff.c +9 -15
index 4b18717e230e..deee396c9c3d 100644
@@ -4625,10 +4625,9 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
 
 	if (skb_headlen(skb))
 		return 0;
-	if (skb_zcopy(tgt) || skb_zcopy(skb))
+	if (!skb_frags_can_coalesce(tgt, skb))
 		return 0;
 
-	DEBUG_NET_WARN_ON_ONCE(tgt->pp_recycle != skb->pp_recycle);
 	DEBUG_NET_WARN_ON_ONCE(skb_cmp_decrypted(tgt, skb));
 
 	todo = shiftlen;
@@ -6609,17 +6608,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	 * tricky (due to potential race with the clone disappearing) and
 	 * rare, so not worth dealing with.
 	 */
-	if (to->pp_recycle != from->pp_recycle)
-		return false;
-
-	/* 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;
-
-	if (len <= skb_tailroom(to) && skb_frags_readable(from)) {
+	if (len <= skb_tailroom(to) && to->pp_recycle == from->pp_recycle &&
+	    skb_frags_readable(to) && skb_frags_readable(from)) {
 		if (len) {
 			if (unlikely(skb_copy_bits(from, 0,
 						   skb_tail_pointer(to),
@@ -6633,14 +6623,18 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 		return true;
 	}
 
+	if (!skb_frags_can_coalesce(to, from))
+		return false;
+
 	to_shinfo = skb_shinfo(to);
 	from_shinfo = skb_shinfo(from);
 	if (to_shinfo->frag_list || from_shinfo->frag_list)
 		return false;
-	if (skb_zcopy(to) || skb_zcopy(from))
-		return false;
 
 	if (skb_headlen(from) != 0) {
+		if (!skb_frags_readable(to) || !skb_frags_readable(from))
+			return false;
+
 		if (to_shinfo->nr_frags +
 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
 			return false;
net/ipv4/tcp.c +2 -1
index d510e982dcea..6d33c296058d 100644
@@ -1254,7 +1254,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 		trace_tcp_sendmsg_locked(sk, msg, skb, size_goal);
 
 		if (copy <= 0 || !tcp_skb_can_collapse_to(skb) ||
-		    unlikely(skb_frags_readable(skb) != !binding)) {
+		    unlikely(skb_frags_readable(skb) != !binding ||
+			     net_devmem_skb_binding(skb) != binding)) {
 			bool first_skb;
 
 new_segment:
net/ipv4/tcp_output.c +3 -1
index 08d0d4a639fe..19059d03152e 100644
@@ -2696,8 +2696,10 @@ static int tcp_clone_payload(struct sock *sk, struct sk_buff *to,
 				     probe_size - len);
 			len += todo;
 			skb_shinfo(to)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
+			to->unreadable |= skb->unreadable;
 			if (lastfrag &&
-			    skb_frag_page(fragfrom) == skb_frag_page(lastfrag) &&
+			    skb_frag_netmem(fragfrom) ==
+			    skb_frag_netmem(lastfrag) &&
 			    skb_frag_off(fragfrom) == skb_frag_off(lastfrag) +
 						      skb_frag_size(lastfrag)) {
 				skb_frag_size_add(lastfrag, todo);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#3 lore raw

Re: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

Quoted 150 lines · Mina Almasry

Prevent coalescing, shifting, or appending skb fragments from incompatible memory providers or different devmem bindings.

Add skb_net_iov_provider(), skb_mem_provider_compat(), and skb_frags_can_coalesce() to check readability and fragment provider compatibility (net_iov_owner(niov) for devmem, niov->desc.pp for other net_iov providers such as io_uring zcrx, and NULL for page-backed skbs) across skb_try_coalesce(), skb_shift(), skb_gro_receive(), skb_gro_receive_list(), tcp_skb_can_collapse(), and tcp_skb_can_collapse_rx().

Also add net_devmem_skb_binding() and use it in tcp_sendmsg_locked(), zerocopy_fill_skb_from_devmem(), and validate_xmit_unreadable_skb() so TX skbs cannot mix different devmem bindings or transmit non-devmem unreadable frags on DMA TX devices. Reject converting linear head_frag payloads into page frags on unreadable skbs in skb_try_coalesce() and skb_gro_receive(), and propagate skb->unreadable and compare skb_frag_netmem() in tcp_clone_payload().

Update netmem_ref_test_net_iov and netmem_ref_test_try_coalesce so their target skbs carry a compatible unreadable net_iov frag before shifting or coalescing.

This issue was found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Fixes bd61848900bf ("net: devmem: Implement TX path")
---
 include/linux/skbuff.h | 55 ++++++++++++++++++++++++++++++++++++++++++
 include/net/tcp.h      |  5 ++--
 net/core/datagram.c    |  4 +--
 net/core/dev.c         | 14 ++++-------
 net/core/devmem.h      | 22 +++++++++++++++++
 net/core/gro.c         | 16 ++++--------
 net/core/net_test.c    | 42 +++++++++++++++++++++++++++-----
 net/core/skbuff.c      | 24 +++++++-----------
 net/ipv4/tcp.c         |  3 ++-
 net/ipv4/tcp_output.c  |  4 ++-
 10 files changed, 142 insertions(+), 47 deletions(-)
include/linux/skbuff.h +55 -0
index 1d6832878930..867e3ab17c17 100644
@@ -3770,6 +3770,61 @@ static inline void *skb_frag_address(const skb_frag_t *frag)
 	return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
 }
 
+/* Return an opaque pointer identifying the memory provider instance of @skb's
+ * net_iov fragments:
+ * - For devmem (NET_IOV_DMABUF), net_iov_owner(niov) (&binding->area, 1:1 with
+ *   the binding across RX queues and TX).
+ * - For other net_iov providers (e.g., io_uring zcrx), niov->desc.pp (shared
+ *   across all areas of an io_zcrx_ifq).
+ */
+static inline void *skb_net_iov_provider(const struct sk_buff *skb)
+{
+	struct net_iov *niov;
+
+	if (skb_frags_readable(skb) || !skb_shinfo(skb)->nr_frags)
+		return NULL;
+
+	niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+	if (!niov)
+		return NULL;
+
+	if (net_is_devmem_iov(niov))
+		return net_iov_owner(niov);
+
+	return niov->desc.pp;
+}
+
+/* Return true if the memory providers of @skb1 and @skb2 are compatible
+ * for coalescing. For any struct page-backed provider (the buddy allocator
+ * or a future provider handing out struct pages), coalescing is assumed
+ * feasible. For net_iov-backed providers, the provider instance pointer
+ * must match.
+ */
+static inline bool skb_mem_provider_compat(const struct sk_buff *skb1,
+					   const struct sk_buff *skb2)
+{
+	/* Check skb_frags_readable() first to reject readable/unreadable
+	 * mismatches and avoid chasing frags[0] and struct net_iov when
+	 * only one skb has unreadable frags.
+	 */
+	if (skb_frags_readable(skb1) != skb_frags_readable(skb2))
+		return false;
+
+	return skb_net_iov_provider(skb1) == skb_net_iov_provider(skb2);
+}
+
+static inline bool skb_frags_can_coalesce(struct sk_buff *to,
+					  struct sk_buff *from)
+{
+	if (to->pp_recycle != from->pp_recycle)
+		return false;
+
+	if (skb_zcopy(to) || skb_zcopy(from))
+		return false;
+
+	return skb_mem_provider_compat(to, from);
+}
+
 /**
  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  * @frag: the paged fragment buffer
include/net/tcp.h +3 -2
index 25d11fab1b03..bf5f28e52fca 100644
@@ -1275,14 +1275,15 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 		      mptcp_skb_can_collapse(to, from) &&
 		      !skb_cmp_decrypted(to, from) &&
 		      skb_pure_zcopy_same(to, from) &&
-		      skb_frags_readable(to) == skb_frags_readable(from));
+		      skb_mem_provider_compat(to, from));
 }
 
 static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
 					   const struct sk_buff *from)
 {
 	return likely(mptcp_skb_can_collapse(to, from) &&
-		      !skb_cmp_decrypted(to, from));
+		      !skb_cmp_decrypted(to, from) &&
+		      skb_mem_provider_compat(to, from));
 }
 
 /* Events passed to congestion control interface */
net/core/datagram.c +2 -2
index ecc30a6f8302..88cc0a6c62f6 100644
@@ -712,8 +712,8 @@ zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
 	size_t virt_addr, size, off;
 	struct net_iov *niov;
 
-	if (i && skb_frags_readable(skb))
-		return -EFAULT;
+	if (i && net_devmem_skb_binding(skb) != binding)
+		return -EMSGSIZE;
 
 	/* Devmem filling works by taking an IOVEC from the user where the
 	 * iov_addrs are interpreted as an offset in bytes into the dma-buf to
net/core/dev.c +0 -0
index a230ecce9327..51f5e4800eee 100644

[..]

Quoted 19 lines
net/core/dev.c +1 -6
@@ -4175,8 +4175,7 @@ static struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
 static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
 						    struct net_device *dev)
 {
-	struct skb_shared_info *shinfo;
-	struct net_iov *niov;
+	struct net_devmem_dmabuf_binding *binding;
 
 	if (likely(skb_frags_readable(skb) ||
 		   dev->netmem_tx == NETMEM_TX_NO_DMA))
@@ -4185,14 +4184,11 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
 	if (dev->netmem_tx == NETMEM_TX_NONE)
 		goto out_free;
 
-	shinfo = skb_shinfo(skb);
-
 	/* All net_iovs in an unreadable skb share the same binding. */
-	if (shinfo->nr_frags > 0) {
-		niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));

net/core/dev.c static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb, · line 4194

Quoted 10 lines
net/core/dev.c +4 -3
-		if (net_is_devmem_iov(niov) &&
-		    (net_devmem_iov_binding(niov)->direction != DMA_TO_DEVICE ||
-		     READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev))
+	if (skb_shinfo(skb)->nr_frags > 0) {
+		binding = net_devmem_skb_binding(skb);
+		if (!binding || binding->direction != DMA_TO_DEVICE ||
+		    READ_ONCE(binding->dev) != dev)
 			goto out_free;
 	}
 

Should we limit this patch to the real fixes only? This part does not look essential. validate_xmit_unreadable_skb already does skb_frags_readable check above, it already does nr_frags > 0 check here...
so why go through the same stuff again via net_devmem_skb_binding?

If you want, maybe move this to a separate patch at least? And do something like:

binding = net_devmem_skb_binding(skb);
/* I hope likely() below properly unwraps net_devmem_skb_binding similar
* to existing likely(skb_frags_readable) case... */
if (likely(!binding)) goto out;
if (dev->netmem_tx == NETMEM_TX_NO_DMA) goto out;
if (dev->netmem_tx == NETMEM_TX_NONE) goto out_free;
if (READ_ONCE(binding->dev) != dev) goto out_free;
return skb;

And call it cleanup with no fixes tags.

#4 lore raw

Re: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

Quoted 181 lines · Stanislav Fomichev
Quoted 150 lines · Mina Almasry

Prevent coalescing, shifting, or appending skb fragments from incompatible memory providers or different devmem bindings.

Add skb_net_iov_provider(), skb_mem_provider_compat(), and skb_frags_can_coalesce() to check readability and fragment provider compatibility (net_iov_owner(niov) for devmem, niov->desc.pp for other net_iov providers such as io_uring zcrx, and NULL for page-backed skbs) across skb_try_coalesce(), skb_shift(), skb_gro_receive(), skb_gro_receive_list(), tcp_skb_can_collapse(), and tcp_skb_can_collapse_rx().

Also add net_devmem_skb_binding() and use it in tcp_sendmsg_locked(), zerocopy_fill_skb_from_devmem(), and validate_xmit_unreadable_skb() so TX skbs cannot mix different devmem bindings or transmit non-devmem unreadable frags on DMA TX devices. Reject converting linear head_frag payloads into page frags on unreadable skbs in skb_try_coalesce() and skb_gro_receive(), and propagate skb->unreadable and compare skb_frag_netmem() in tcp_clone_payload().

Update netmem_ref_test_net_iov and netmem_ref_test_try_coalesce so their target skbs carry a compatible unreadable net_iov frag before shifting or coalescing.

This issue was found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Fixes bd61848900bf ("net: devmem: Implement TX path")
---
 include/linux/skbuff.h | 55 ++++++++++++++++++++++++++++++++++++++++++
 include/net/tcp.h      |  5 ++--
 net/core/datagram.c    |  4 +--
 net/core/dev.c         | 14 ++++-------
 net/core/devmem.h      | 22 +++++++++++++++++
 net/core/gro.c         | 16 ++++--------
 net/core/net_test.c    | 42 +++++++++++++++++++++++++++-----
 net/core/skbuff.c      | 24 +++++++-----------
 net/ipv4/tcp.c         |  3 ++-
 net/ipv4/tcp_output.c  |  4 ++-
 10 files changed, 142 insertions(+), 47 deletions(-)
include/linux/skbuff.h +55 -0
index 1d6832878930..867e3ab17c17 100644
@@ -3770,6 +3770,61 @@ static inline void *skb_frag_address(const skb_frag_t *frag)
      return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
 }
 
+/* Return an opaque pointer identifying the memory provider instance of @skb's
+ * net_iov fragments:
+ * - For devmem (NET_IOV_DMABUF), net_iov_owner(niov) (&binding->area, 1:1 with
+ *   the binding across RX queues and TX).
+ * - For other net_iov providers (e.g., io_uring zcrx), niov->desc.pp (shared
+ *   across all areas of an io_zcrx_ifq).
+ */
+static inline void *skb_net_iov_provider(const struct sk_buff *skb)
+{
+     struct net_iov *niov;
+
+     if (skb_frags_readable(skb) || !skb_shinfo(skb)->nr_frags)
+             return NULL;
+
+     niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+     if (!niov)
+             return NULL;
+
+     if (net_is_devmem_iov(niov))
+             return net_iov_owner(niov);
+
+     return niov->desc.pp;
+}
+
+/* Return true if the memory providers of @skb1 and @skb2 are compatible
+ * for coalescing. For any struct page-backed provider (the buddy allocator
+ * or a future provider handing out struct pages), coalescing is assumed
+ * feasible. For net_iov-backed providers, the provider instance pointer
+ * must match.
+ */
+static inline bool skb_mem_provider_compat(const struct sk_buff *skb1,
+                                        const struct sk_buff *skb2)
+{
+     /* Check skb_frags_readable() first to reject readable/unreadable
+      * mismatches and avoid chasing frags[0] and struct net_iov when
+      * only one skb has unreadable frags.
+      */
+     if (skb_frags_readable(skb1) != skb_frags_readable(skb2))
+             return false;
+
+     return skb_net_iov_provider(skb1) == skb_net_iov_provider(skb2);
+}
+
+static inline bool skb_frags_can_coalesce(struct sk_buff *to,
+                                       struct sk_buff *from)
+{
+     if (to->pp_recycle != from->pp_recycle)
+             return false;
+
+     if (skb_zcopy(to) || skb_zcopy(from))
+             return false;
+
+     return skb_mem_provider_compat(to, from);
+}
+
 /**
  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  * @frag: the paged fragment buffer
include/net/tcp.h +3 -2
index 25d11fab1b03..bf5f28e52fca 100644
@@ -1275,14 +1275,15 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
                    mptcp_skb_can_collapse(to, from) &&
                    !skb_cmp_decrypted(to, from) &&
                    skb_pure_zcopy_same(to, from) &&
-                   skb_frags_readable(to) == skb_frags_readable(from));
+                   skb_mem_provider_compat(to, from));
 }
 
 static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
                                         const struct sk_buff *from)
 {
      return likely(mptcp_skb_can_collapse(to, from) &&
-                   !skb_cmp_decrypted(to, from));
+                   !skb_cmp_decrypted(to, from) &&
+                   skb_mem_provider_compat(to, from));
 }
 
 /* Events passed to congestion control interface */
net/core/datagram.c +2 -2
index ecc30a6f8302..88cc0a6c62f6 100644
@@ -712,8 +712,8 @@ zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
      size_t virt_addr, size, off;
      struct net_iov *niov;
 
-     if (i && skb_frags_readable(skb))
-             return -EFAULT;
+     if (i && net_devmem_skb_binding(skb) != binding)
+             return -EMSGSIZE;
 
      /* Devmem filling works by taking an IOVEC from the user where the
       * iov_addrs are interpreted as an offset in bytes into the dma-buf to
net/core/dev.c +0 -0
index a230ecce9327..51f5e4800eee 100644

[..]

Quoted 26 lines
net/core/dev.c +5 -9
@@ -4175,8 +4175,7 @@ static struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
 static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
                                                  struct net_device *dev)
 {
-     struct skb_shared_info *shinfo;
-     struct net_iov *niov;
+     struct net_devmem_dmabuf_binding *binding;
 
      if (likely(skb_frags_readable(skb) ||
                 dev->netmem_tx == NETMEM_TX_NO_DMA))
@@ -4185,14 +4184,11 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
      if (dev->netmem_tx == NETMEM_TX_NONE)
              goto out_free;
 
-     shinfo = skb_shinfo(skb);
-
      /* All net_iovs in an unreadable skb share the same binding. */
-     if (shinfo->nr_frags > 0) {
-             niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
-             if (net_is_devmem_iov(niov) &&
-                 (net_devmem_iov_binding(niov)->direction != DMA_TO_DEVICE ||
-                  READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev))
+     if (skb_shinfo(skb)->nr_frags > 0) {
+             binding = net_devmem_skb_binding(skb);
+             if (!binding || binding->direction != DMA_TO_DEVICE ||
+                 READ_ONCE(binding->dev) != dev)
Quoted 9 lines

net/core/dev.c static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb, · line 4194

Quoted 3 lines
net/core/dev.c +0 -0
                      goto out_free;
      }
 

Should we limit this patch to the real fixes only? This part does not look essential. validate_xmit_unreadable_skb already does skb_frags_readable check above, it already does nr_frags > 0 check here...
so why go through the same stuff again via net_devmem_skb_binding?

So this is a real fix actually, although I agree it's kinda clobbered into a patch with an unrelated fix. The existing checks do not catch io_uring zcrx RX packets that have been received and the stack is trying to forward them over a netdev where the memory is not mapped, and this patch fixes that.

Quoted 12 lines

If you want, maybe move this to a separate patch at least? And do something like:

binding = net_devmem_skb_binding(skb);
/* I hope likely() below properly unwraps net_devmem_skb_binding similar
* to existing likely(skb_frags_readable) case... */
if (likely(!binding)) goto out;
if (dev->netmem_tx == NETMEM_TX_NO_DMA) goto out;
if (dev->netmem_tx == NETMEM_TX_NONE) goto out_free;
if (READ_ONCE(binding->dev) != dev) goto out_free;
return skb;

Quoted 1 line

And call it cleanup with no fixes tags.

I could break it into a separate patch and reduce some of the duplicated checks, but it's still a fix. It may be good as-is once you realize it's a necessary change?

--
Thanks,
Mina

#5 lore raw

Re: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

Quoted 211 lines · Mina Almasry
Quoted 181 lines · Stanislav Fomichev
Quoted 150 lines · Mina Almasry

Prevent coalescing, shifting, or appending skb fragments from incompatible memory providers or different devmem bindings.

Add skb_net_iov_provider(), skb_mem_provider_compat(), and skb_frags_can_coalesce() to check readability and fragment provider compatibility (net_iov_owner(niov) for devmem, niov->desc.pp for other net_iov providers such as io_uring zcrx, and NULL for page-backed skbs) across skb_try_coalesce(), skb_shift(), skb_gro_receive(), skb_gro_receive_list(), tcp_skb_can_collapse(), and tcp_skb_can_collapse_rx().

Also add net_devmem_skb_binding() and use it in tcp_sendmsg_locked(), zerocopy_fill_skb_from_devmem(), and validate_xmit_unreadable_skb() so TX skbs cannot mix different devmem bindings or transmit non-devmem unreadable frags on DMA TX devices. Reject converting linear head_frag payloads into page frags on unreadable skbs in skb_try_coalesce() and skb_gro_receive(), and propagate skb->unreadable and compare skb_frag_netmem() in tcp_clone_payload().

Update netmem_ref_test_net_iov and netmem_ref_test_try_coalesce so their target skbs carry a compatible unreadable net_iov frag before shifting or coalescing.

This issue was found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Fixes bd61848900bf ("net: devmem: Implement TX path")
---
 include/linux/skbuff.h | 55 ++++++++++++++++++++++++++++++++++++++++++
 include/net/tcp.h      |  5 ++--
 net/core/datagram.c    |  4 +--
 net/core/dev.c         | 14 ++++-------
 net/core/devmem.h      | 22 +++++++++++++++++
 net/core/gro.c         | 16 ++++--------
 net/core/net_test.c    | 42 +++++++++++++++++++++++++++-----
 net/core/skbuff.c      | 24 +++++++-----------
 net/ipv4/tcp.c         |  3 ++-
 net/ipv4/tcp_output.c  |  4 ++-
 10 files changed, 142 insertions(+), 47 deletions(-)
include/linux/skbuff.h +55 -0
index 1d6832878930..867e3ab17c17 100644
@@ -3770,6 +3770,61 @@ static inline void *skb_frag_address(const skb_frag_t *frag)
      return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
 }
 
+/* Return an opaque pointer identifying the memory provider instance of @skb's
+ * net_iov fragments:
+ * - For devmem (NET_IOV_DMABUF), net_iov_owner(niov) (&binding->area, 1:1 with
+ *   the binding across RX queues and TX).
+ * - For other net_iov providers (e.g., io_uring zcrx), niov->desc.pp (shared
+ *   across all areas of an io_zcrx_ifq).
+ */
+static inline void *skb_net_iov_provider(const struct sk_buff *skb)
+{
+     struct net_iov *niov;
+
+     if (skb_frags_readable(skb) || !skb_shinfo(skb)->nr_frags)
+             return NULL;
+
+     niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+     if (!niov)
+             return NULL;
+
+     if (net_is_devmem_iov(niov))
+             return net_iov_owner(niov);
+
+     return niov->desc.pp;
+}
+
+/* Return true if the memory providers of @skb1 and @skb2 are compatible
+ * for coalescing. For any struct page-backed provider (the buddy allocator
+ * or a future provider handing out struct pages), coalescing is assumed
+ * feasible. For net_iov-backed providers, the provider instance pointer
+ * must match.
+ */
+static inline bool skb_mem_provider_compat(const struct sk_buff *skb1,
+                                        const struct sk_buff *skb2)
+{
+     /* Check skb_frags_readable() first to reject readable/unreadable
+      * mismatches and avoid chasing frags[0] and struct net_iov when
+      * only one skb has unreadable frags.
+      */
+     if (skb_frags_readable(skb1) != skb_frags_readable(skb2))
+             return false;
+
+     return skb_net_iov_provider(skb1) == skb_net_iov_provider(skb2);
+}
+
+static inline bool skb_frags_can_coalesce(struct sk_buff *to,
+                                       struct sk_buff *from)
+{
+     if (to->pp_recycle != from->pp_recycle)
+             return false;
+
+     if (skb_zcopy(to) || skb_zcopy(from))
+             return false;
+
+     return skb_mem_provider_compat(to, from);
+}
+
 /**
  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  * @frag: the paged fragment buffer
include/net/tcp.h +3 -2
index 25d11fab1b03..bf5f28e52fca 100644
@@ -1275,14 +1275,15 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
                    mptcp_skb_can_collapse(to, from) &&
                    !skb_cmp_decrypted(to, from) &&
                    skb_pure_zcopy_same(to, from) &&
-                   skb_frags_readable(to) == skb_frags_readable(from));
+                   skb_mem_provider_compat(to, from));
 }
 
 static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
                                         const struct sk_buff *from)
 {
      return likely(mptcp_skb_can_collapse(to, from) &&
-                   !skb_cmp_decrypted(to, from));
+                   !skb_cmp_decrypted(to, from) &&
+                   skb_mem_provider_compat(to, from));
 }
 
 /* Events passed to congestion control interface */
net/core/datagram.c +2 -2
index ecc30a6f8302..88cc0a6c62f6 100644
@@ -712,8 +712,8 @@ zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
      size_t virt_addr, size, off;
      struct net_iov *niov;
 
-     if (i && skb_frags_readable(skb))
-             return -EFAULT;
+     if (i && net_devmem_skb_binding(skb) != binding)
+             return -EMSGSIZE;
 
      /* Devmem filling works by taking an IOVEC from the user where the
       * iov_addrs are interpreted as an offset in bytes into the dma-buf to
net/core/dev.c +0 -0
index a230ecce9327..51f5e4800eee 100644

[..]

Quoted 26 lines
net/core/dev.c +5 -9
@@ -4175,8 +4175,7 @@ static struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
 static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
                                                  struct net_device *dev)
 {
-     struct skb_shared_info *shinfo;
-     struct net_iov *niov;
+     struct net_devmem_dmabuf_binding *binding;
 
      if (likely(skb_frags_readable(skb) ||
                 dev->netmem_tx == NETMEM_TX_NO_DMA))
@@ -4185,14 +4184,11 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
      if (dev->netmem_tx == NETMEM_TX_NONE)
              goto out_free;
 
-     shinfo = skb_shinfo(skb);
-
      /* All net_iovs in an unreadable skb share the same binding. */
-     if (shinfo->nr_frags > 0) {
-             niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
-             if (net_is_devmem_iov(niov) &&
-                 (net_devmem_iov_binding(niov)->direction != DMA_TO_DEVICE ||
-                  READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev))
+     if (skb_shinfo(skb)->nr_frags > 0) {
+             binding = net_devmem_skb_binding(skb);
+             if (!binding || binding->direction != DMA_TO_DEVICE ||
+                 READ_ONCE(binding->dev) != dev)
Quoted 9 lines

net/core/dev.c static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb, · line 4194

Quoted 3 lines
net/core/dev.c +0 -0
                      goto out_free;
      }
 

Should we limit this patch to the real fixes only? This part does not look essential. validate_xmit_unreadable_skb already does skb_frags_readable check above, it already does nr_frags > 0 check here...
so why go through the same stuff again via net_devmem_skb_binding?

So this is a real fix actually, although I agree it's kinda clobbered into a patch with an unrelated fix. The existing checks do not catch io_uring zcrx RX packets that have been received and the stack is trying to forward them over a netdev where the memory is not mapped, and this patch fixes that.

Quoted 12 lines

If you want, maybe move this to a separate patch at least? And do something like:

binding = net_devmem_skb_binding(skb);
/* I hope likely() below properly unwraps net_devmem_skb_binding similar
* to existing likely(skb_frags_readable) case... */
if (likely(!binding)) goto out;
if (dev->netmem_tx == NETMEM_TX_NO_DMA) goto out;
if (dev->netmem_tx == NETMEM_TX_NONE) goto out_free;
if (READ_ONCE(binding->dev) != dev) goto out_free;
return skb;

Quoted 5 lines
Quoted 1 line

And call it cleanup with no fixes tags.

I could break it into a separate patch and reduce some of the duplicated checks, but it's still a fix. It may be good as-is once you realize it's a necessary change?

I see, so that desc.pp part is missing currently :-/

If you happen to repost, I'd remove unrelated struct skb_shared_info *shinfo removal/etc to make it more clear.

maintainer
#6 lore raw

Re: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

In reply to Mina Almasry
Quoted 1 line · On Sun, 15 Nov 2026 00:40:02 +0000 Mina Almasry wrote:

Subject: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

This patch needs to be split up. There seems to be pure refactoring here (validate_xmit_unreadable_skb()) and the test code also should be in a separate patch. The rest needs a close look from perf perspective :S Lots of checks on the fast path to fix a hallucination..

#7 lore raw

Re: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

Quoted 6 lines · Jakub Kicinski
Quoted 1 line · On Sun, 15 Nov 2026 00:40:02 +0000 Mina Almasry wrote:

Subject: [PATCH net-next v5 2/3] net: prevent mixing frags from different memory providers or bindings

This patch needs to be split up. There seems to be pure refactoring here (validate_xmit_unreadable_skb())

That's actually a fix. Without this, iouring rcrx skbs forwarded could end up going through a device to which they aren't mapped, afaiu. I guess splitting the patches will make this more apparent.

Quoted 3 lines

and the test code also should
be in a separate patch. The rest needs a close look from perf perspective :S

FWIW I worked to minimize the impact, which is why the patch may look a bit convoluted. We should load zero extra cache lines in the case where both skbs are paged, and only load extra cachelines when we've confirmed both skbs have memory providers in them and we need to do an actual provider check. In the next version I will include some analysis on that in the cover letter.

However, we still pay the cost of the checks themselves.

Quoted 2 lines

Lots of checks on the fast path to fix a hallucination..

I wasn't doing this because sashiko hallucinated it, I was more doing it because I was aware that some mixing is possible, and saw folks were trying to include some new memory providers (AF_XDP, PISA), and I was worried some bot will be able to reproduce reliable crashes with these edge cases and it becomes a real issue. I thought to fix it before it became a real issue. Let me know if that's not valuable and you want me to punt this; for now I'll follow your instruction and repost with the split.

Signature · 2 lines
Thanks,
Mina
#8 lore raw

PATCH v5 3/3 tcp: propagate skb_zcopy() uarg in tcp_mtu_probe()

In reply to Mina Almasry

tcp_clone_payload() takes references on source skb fragments when building an MTU probe skb, but never attaches the source skb's zerocopy ubuf_info (uarg) to the probe skb. When tcp_mtu_probe() subsequently frees fully consumed source skbs via tcp_eat_one_skb(), skb_release_data() drops their uarg references and can deliver the zerocopy completion to userspace while the unacknowledged probe skb still references the user buffers.

Additionally, tcp_can_coalesce_send_queue_head() only checks skb_pure_zcopy_same() (which compares SKBFL_PURE_ZEROCOPY), allowing adjacent zerocopy skbs with different uargs (or a mixed-zerocopy skb and a non-zerocopy skb) to be coalesced into a single probe skb that can only hold one destructor_arg.

Reject coalescing across different skb_zcopy() uargs in tcp_can_coalesce_send_queue_head() and propagate the uarg to the probe skb via skb_zcopy_set() in tcp_clone_payload().

This is a pre-existing issue found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 736013292e3c ("tcp: let tcp_mtu_probe() build headless packets")
---
 net/ipv4/tcp_output.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/ipv4/tcp_output.c +4 -1
index 19059d03152e..bedf828cc0e0 100644
@@ -2664,7 +2664,9 @@ static bool tcp_can_coalesce_send_queue_head(struct sock *sk, int len)
 		if (len <= skb->len)
 			break;
 
-		if (tcp_has_tx_tstamp(skb) || !tcp_skb_can_collapse(skb, next))
+		if (tcp_has_tx_tstamp(skb) ||
+		    !tcp_skb_can_collapse(skb, next) ||
+		    skb_zcopy(skb) != skb_zcopy(next))
 			return false;
 
 		len -= skb->len;
@@ -2718,6 +2720,7 @@ static int tcp_clone_payload(struct sock *sk, struct sk_buff *to,
 	WARN_ON_ONCE(len != probe_size);
 	for (i = 0; i < nr_frags; i++)
 		skb_frag_ref(to, i);
+	skb_zcopy_set(to, skb_zcopy(tcp_send_head(sk)), NULL);
 
 	skb_shinfo(to)->nr_frags = nr_frags;
 	to->truesize += probe_size;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Acked-by on v5 3/3
#9 lore raw

Re: [PATCH net-next v5 3/3] tcp: propagate skb_zcopy() uarg in tcp_mtu_probe()

Quoted 18 lines · Mina Almasry

tcp_clone_payload() takes references on source skb fragments when building an MTU probe skb, but never attaches the source skb's zerocopy ubuf_info (uarg) to the probe skb. When tcp_mtu_probe() subsequently frees fully consumed source skbs via tcp_eat_one_skb(), skb_release_data() drops their uarg references and can deliver the zerocopy completion to userspace while the unacknowledged probe skb still references the user buffers.

Additionally, tcp_can_coalesce_send_queue_head() only checks skb_pure_zcopy_same() (which compares SKBFL_PURE_ZEROCOPY), allowing adjacent zerocopy skbs with different uargs (or a mixed-zerocopy skb and a non-zerocopy skb) to be coalesced into a single probe skb that can only hold one destructor_arg.

Reject coalescing across different skb_zcopy() uargs in tcp_can_coalesce_send_queue_head() and propagate the uarg to the probe skb via skb_zcopy_set() in tcp_clone_payload().

Quoted 6 lines

This is a pre-existing issue found by manual code inspection and LLM review; it was not triggered in production. Verified with the net_core KUnit test suite; not tested on real hardware.

Fixes 736013292e3c ("tcp: let tcp_mtu_probe() build headless packets")
maintainer
#10 lore raw
In reply to Mina Almasry
Quoted 2 lines · On Sun, 15 Nov 2026 00:40:00 +0000 Mina Almasry wrote:

Enforce that all fragments in a nonlinear skb come from compatible memory providers (and for devmem, the exact same binding).

This conflicts from some fixes from net, please rebase & repost after the net -> net-next merge.