Skip to contentSkip to conversation

xsk: respect the offsets when copying frags

PATCH Superseded Bui Quang Minh 5 participants 9 messages Apr 23, 2025 – Apr 26, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xsk: respect the offsets when copying frags

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
 net/core/xdp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/core/xdp.c +4 -3
index f86eedad586a..a723dc301f94 100644
@@ -697,7 +697,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 	nr_frags = xinfo->nr_frags;
 
 	for (u32 i = 0; i < nr_frags; i++) {
-		u32 len = skb_frag_size(&xinfo->frags[i]);
+		const skb_frag_t *frag = &xinfo->frags[i];
+		u32 len = skb_frag_size(frag);
 		u32 offset, truesize = len;
 		netmem_ref netmem;
 
@@ -707,8 +708,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 			return false;
 		}
 
-		memcpy(__netmem_address(netmem),
-		       __netmem_address(xinfo->frags[i].netmem),
+		memcpy(__netmem_address(netmem) + offset,
+		       __netmem_address(frag->netmem) + skb_frag_off(frag),
 		       LARGEST_ALIGN(len));
 		__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 1 line · Bui Quang Minh

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Can you please share more about how you've hit this problem? I don't see the caller of this function (xdp_build_skb_from_zc) being used at all.

Alexander, do you have plans to use it? Or should we remove it for now?

#2 lore raw
Quoted 7 lines · Stanislav Fomichev
Quoted 1 line · Bui Quang Minh

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Can you please share more about how you've hit this problem? I don't see the caller of this function (xdp_build_skb_from_zc) being used at all.

Alexander, do you have plans to use it? Or should we remove it for now?

Hi,

I've been playing around to add support for zerocopy XDP socket with multi buffer mergeable buffer in virtio-net (this TODO: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/tree/drivers/net/virtio_net.c#n1312). In that case, I'll have a XDP buff with frags. When we have XDP_PASS return, I need to convert the XDP buff with frags to skb with frags, so I think the helper is quite helpful. I used it and got packet dropped due to checksum error. Debugging the problem, I've found out this issue which makes the skb's frag data incorrect.

Thanks,
Quang Minh.

#3 lore raw
Quoted 8 lines · Bui Quang Minh
Quoted 7 lines · Stanislav Fomichev
Quoted 1 line · Bui Quang Minh

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Can you please share more about how you've hit this problem? I don't see the caller of this function (xdp_build_skb_from_zc) being used at all.

Alexander, do you have plans to use it? Or should we remove it for now?

libeth xdp support uses this:
https://lore.kernel.org/netdev/[email protected]/

Quoted 9 lines

Hi,

I've been playing around to add support for zerocopy XDP socket with multi
buffer mergeable buffer in virtio-net (this TODO: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/tree/drivers/net/virtio_net.c#n1312).
In that case, I'll have a XDP buff with frags. When we have XDP_PASS return,
I need to convert the XDP buff with frags to skb with frags, so I think the
helper is quite helpful. I used it and got packet dropped due to checksum
error. Debugging the problem, I've found out this issue which makes the
skb's frag data incorrect.

Nice! I'll take a look at patch content tomorrow and probably ack it.

Quoted 3 lines

Thanks,
Quang Minh.

#4 lore raw
In reply to Bui Quang Minh

From: Bui Quang Minh <[email protected]>
Date: Wed, 23 Apr 2025 17:10:47 +0700

Quoted 23 lines

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
 net/core/xdp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/core/xdp.c +2 -1
index f86eedad586a..a723dc301f94 100644
@@ -697,7 +697,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 	nr_frags = xinfo->nr_frags;
 
 	for (u32 i = 0; i < nr_frags; i++) {
-		u32 len = skb_frag_size(&xinfo->frags[i]);
+		const skb_frag_t *frag = &xinfo->frags[i];
+		u32 len = skb_frag_size(frag);
 		u32 offset, truesize = len;
 		netmem_ref netmem;
 

net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 714

Quoted 9 lines
net/core/xdp.c +2 -2
@@ -707,8 +708,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 			return false;
 		}
 
-		memcpy(__netmem_address(netmem),
-		       __netmem_address(xinfo->frags[i].netmem),
+		memcpy(__netmem_address(netmem) + offset,
+		       __netmem_address(frag->netmem) + skb_frag_off(frag),
 		       LARGEST_ALIGN(len));
 		__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);

Incorrect fix.

page_pool_dev_alloc_netmem() allocates a buffer of skb_frag_size() len, but then you pass offset when copying, which may lead to access beyond the end of the buffer.

I know that my code here is incorrect as well, but the idea was to allocate only skb_frag_size() and copy the actual payload without any offset to the new buffer. So, you need to pass the offset only to the second argument of memcpy() and then pass 0 as @offset to __skb_fill_netmem_desc_noacc().

Thanks,
Olek

#5 lore raw
Quoted 41 lines · Alexander Lobakin

From: Bui Quang Minh <[email protected]>
Date: Wed, 23 Apr 2025 17:10:47 +0700

Quoted 23 lines

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
  net/core/xdp.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
net/core/xdp.c +2 -1
index f86eedad586a..a723dc301f94 100644
@@ -697,7 +697,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  	nr_frags = xinfo->nr_frags;
 
  	for (u32 i = 0; i < nr_frags; i++) {
-		u32 len = skb_frag_size(&xinfo->frags[i]);
+		const skb_frag_t *frag = &xinfo->frags[i];
+		u32 len = skb_frag_size(frag);
  		u32 offset, truesize = len;
  		netmem_ref netmem;
 

net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 714

Quoted 9 lines
net/core/xdp.c +2 -2
@@ -707,8 +708,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  			return false;
  		}
 
-		memcpy(__netmem_address(netmem),
-		       __netmem_address(xinfo->frags[i].netmem),
+		memcpy(__netmem_address(netmem) + offset,
+		       __netmem_address(frag->netmem) + skb_frag_off(frag),
  		       LARGEST_ALIGN(len));
  		__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);

Incorrect fix.

page_pool_dev_alloc_netmem() allocates a buffer of skb_frag_size() len, but then you pass offset when copying, which may lead to access beyond the end of the buffer.

Quoted 5 lines

I know that my code here is incorrect as well, but the idea was to allocate only skb_frag_size() and copy the actual payload without any offset to the new buffer. So, you need to pass the offset only to the second argument of memcpy() and then pass 0 as @offset to __skb_fill_netmem_desc_noacc().

I'm not quite familiar with the page_pool API so I might be wrong. AFAICS, the netmem_ref is just a wrapper around struct page now. page_pool_dev_alloc_netmem(pp, &offset, &truesize) returns the allocated page for our request but we must use the returned offset to access our allocated space. The start of page may be currently used by other user.

That's my understanding when looking at this code path

page_pool_dev_alloc_netmem
-> page_pool_alloc_netmem
    -> page_pool_alloc_frag_netmem <- specifically this function

Thanks,
Quang Minh.

maintainer
#6 lore raw
In reply to Bui Quang Minh
Quoted 4 lines · On Wed, 23 Apr 2025 17:10:47 +0700 Bui Quang Minh wrote:

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

I think the fix is right but I dislike the use of netmem here :( Could we switch back to page_pool_dev_alloc() ?
Allocating a netmem to immediately call __netmem_address() is strange. At least to me. Because netmem is supposed to be potentially unreadable. And using normal page allocation will avoid the confusion and bug we're dealing with now.

As Stanislav pointed out this function is not used anywhere today, so let's target the rewrite to net-next and explain in the commit message where the bug comes from and why it doesn't need to be backported (and drop the Fixes tag)?

#7 lore raw
In reply to Bui Quang Minh
Quoted 23 lines · Bui Quang Minh

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
  net/core/xdp.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
net/core/xdp.c +2 -1
index f86eedad586a..a723dc301f94 100644
@@ -697,7 +697,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  	nr_frags = xinfo->nr_frags;
 
  	for (u32 i = 0; i < nr_frags; i++) {
-		u32 len = skb_frag_size(&xinfo->frags[i]);
+		const skb_frag_t *frag = &xinfo->frags[i];
+		u32 len = skb_frag_size(frag);
  		u32 offset, truesize = len;
  		netmem_ref netmem;
 

net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 715

Quoted 10 lines
net/core/xdp.c +2 -2
@@ -707,8 +708,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  			return false;
  		}
 
-		memcpy(__netmem_address(netmem),
-		       __netmem_address(xinfo->frags[i].netmem),
+		memcpy(__netmem_address(netmem) + offset,
+		       __netmem_address(frag->netmem) + skb_frag_off(frag),
  		       LARGEST_ALIGN(len));
  		__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);
 

I know it's very unlikely but do we need to kmap_local_page(skb_frag_page(frag) before using __netmem_address(frag->netmem) to make sure the frag's page is mapped? Or it is impossible that the frag's page to be highmem and unmapped? Thanks, Quang Minh.

maintainer
#8 lore raw
Quoted 27 lines · On Fri, 25 Apr 2025 22:46:35 +0700 Bui Quang Minh wrote:
Quoted 26 lines · Bui Quang Minh

Add the missing offsets when copying frags in xdp_copy_frags_from_zc().

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
  net/core/xdp.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
net/core/xdp.c +2 -1
index f86eedad586a..a723dc301f94 100644
@@ -697,7 +697,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  	nr_frags = xinfo->nr_frags;
 
  	for (u32 i = 0; i < nr_frags; i++) {
-		u32 len = skb_frag_size(&xinfo->frags[i]);
+		const skb_frag_t *frag = &xinfo->frags[i];
+		u32 len = skb_frag_size(frag);
  		u32 offset, truesize = len;
  		netmem_ref netmem;
 
@@ -707,8 +708,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  			return false;
  		}
 
Quoted 12 lines

net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 715

Quoted 7 lines
net/core/xdp.c +2 -2
-		memcpy(__netmem_address(netmem),
-		       __netmem_address(xinfo->frags[i].netmem),
+		memcpy(__netmem_address(netmem) + offset,
+		       __netmem_address(frag->netmem) + skb_frag_off(frag),
  		       LARGEST_ALIGN(len));
  		__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);
 

I know it's very unlikely but do we need to kmap_local_page(skb_frag_page(frag) before using __netmem_address(frag->netmem) to make sure the frag's page is mapped? Or it is impossible that the frag's page to be highmem and unmapped?

AFAIU these frags come from a AF_XDP umem so they should be mapped already.