Netdev List
 help / color / mirror / Atom feed
* [PATCH net] page_pool: keep frag_offset aligned for odd-sized requests
@ 2026-08-26 13:52 Florian Schauer
  2026-08-26 15:43 ` Eric Dumazet
  0 siblings, 1 reply; 2+ messages in thread
From: Florian Schauer @ 2026-08-26 13:52 UTC (permalink / raw)
  To: hawk, ilias.apalodimas; +Cc: netdev, davem, edumazet, kuba, pabeni, horms, bpf

page_pool_alloc_frag_netmem() rounds the requested fragment size with

	size = ALIGN(size, dma_get_cache_alignment());

dma_get_cache_alignment() returns 1 unless the architecture defines
ARCH_DMA_MINALIGN, which DMA-coherent architectures such as x86 do not.
There the ALIGN() is a no-op and pool->frag_offset advances by the raw,
unrounded size.

A single caller asking for an odd size then leaves frag_offset misaligned
for every fragment carved out of that page afterwards.  The pool is shared,
so the damage is not confined to the caller that caused it.

The per-cpu system_page_pool used by generic XDP hits this.
skb_pp_cow_data() allocates data fragments with the raw packet length:

	size = min_t(u32, len, PAGE_SIZE);
	truesize = size;
	page = page_pool_dev_alloc(pool, &page_off, &truesize);

and later calls page_pool_dev_alloc_va() for a new skb head, which
napi_build_skb() installs as skb->head.  Once frag_offset is odd that head
is misaligned, and so is skb_shinfo(skb) == skb->head + skb->end.

struct skb_shared_info begins with an atomic_t dataref, so the increment in
__skb_clone() straddles a cache line.  On x86 with split lock detection --
fatal for kernel split locks by default -- this panics the machine:

  Oops: Split lock detected
  RIP: 0010:skb_clone+0x154/0x1e0
  Call Trace:
   <IRQ>
   raw_local_deliver+0x1ed/0x2c0
   ip_protocol_deliver_rcu+0x54/0x1c0
   ip_local_deliver_finish+0x85/0x100
   ip_local_deliver+0x67/0x100
   __netif_receive_skb_one_core+0x85/0xa0
   process_backlog+0x87/0x130

Reproduced by attaching any generic-mode XDP program to loopback and
opening a RAW IPPROTO_UDP socket, which makes raw_local_deliver() clone
every locally delivered UDP packet; ordinary DNS traffic then triggers it,
roughly once per 2500 clones.  Observed on 6.12.101 and 7.1.8.

Tracing page_pool_alloc_frag_netmem() over one such run shows the
amplification -- two odd-sized requests, nine misaligned offsets:

  requested size & 7:   0: 17035    5: 1    7: 1
  frag_offset & 7:      0: 17028    3: 1    4: 1    5: 1    6: 1    7: 5

and skb_pp_cow_data() returning heads that were aligned on entry:

  head 0xffff8f4c86aeac00 -> 0xffff8f4c53a9a9c4 (&7=4)
  head 0xffff8f4d6a8a42c0 -> 0xffff8f4c4f7b7a45 (&7=5)

Round the fragment size up to at least the natural word size so fragments
are always suitably aligned for the objects callers build on them.
Architectures needing a larger DMA alignment keep it.

This also makes the remainder computed in page_pool_alloc_netmem(),

	*size = max_size - *offset;

aligned, since max_size is a power of two -- which fixes the matching
misalignment of skb->end.

Verified with a controlled A/B under QEMU/KVM: same tree, same config, same
compiler, same rootfs and identical traffic, differing only by this patch.  A
SEC("xdp.frags") XDP_PASS program on lo plus UDP datagrams larger than
max_head_size drives skb_pp_cow_data()'s fragment loop, which passes raw packet
lengths to the pool.  Measured at the return of skb_pp_cow_data():

                          unpatched   patched
  skb_pp_cow_data calls       40800     40800
  misaligned skb->head         1120         0
  dataref at line offset >60     80         0

The last row counts the accesses that actually fault: skb_shinfo()->dataref
sits at head+end+0x20 and is a 4-byte atomic, so `lock incl` splits a 64-byte
cache line only when that address lands at offset 61..63.  All 80 occurrences
were at offset 61; the panic reported above was at offset 62.  Eliminating the
misalignment removes every one of them.

Same class of bug as commit 3bed3cc4156e ("net: Do not allocate page
fragments that are not skb aligned"), which fixed the older
netdev_alloc_frag()/napi_alloc_frag() allocators.

Fixes: 53e0961da1c7 ("page_pool: add frag page recycling support in page pool")
Signed-off-by: Florian Schauer <florian@schauer.to>
---
 net/core/page_pool.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index 1234567..89abcde 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -1065,7 +1065,14 @@ netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,
 	if (WARN_ON(size > max_size))
 		return 0;
 
-	size = ALIGN(size, dma_get_cache_alignment());
+	/* dma_get_cache_alignment() is 1 on DMA-coherent architectures, so on
+	 * its own it cannot keep frag_offset aligned.  Callers build objects
+	 * on these fragments -- skb_pp_cow_data() turns one into skb->head,
+	 * where skb_shinfo() must be aligned -- and the pool is shared, so an
+	 * odd-sized request must not skew every fragment that follows it.
+	 */
+	size = ALIGN(size, max_t(unsigned int, dma_get_cache_alignment(),
+				 sizeof(long)));
 	*offset = pool->frag_offset;
 
 	if (netmem && *offset + size > max_size) {

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

* Re: [PATCH net] page_pool: keep frag_offset aligned for odd-sized requests
  2026-08-26 13:52 [PATCH net] page_pool: keep frag_offset aligned for odd-sized requests Florian Schauer
@ 2026-08-26 15:43 ` Eric Dumazet
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Dumazet @ 2026-08-26 15:43 UTC (permalink / raw)
  To: Florian Schauer
  Cc: hawk, ilias.apalodimas, netdev, davem, kuba, pabeni, horms, bpf

On Wed, Aug 26, 2026 at 3:53 PM Florian Schauer <florian@schauer.to> wrote:
>
> page_pool_alloc_frag_netmem() rounds the requested fragment size with
>
>         size = ALIGN(size, dma_get_cache_alignment());
>
> dma_get_cache_alignment() returns 1 unless the architecture defines
> ARCH_DMA_MINALIGN, which DMA-coherent architectures such as x86 do not.
> There the ALIGN() is a no-op and pool->frag_offset advances by the raw,
> unrounded size.
>
> A single caller asking for an odd size then leaves frag_offset misaligned
> for every fragment carved out of that page afterwards.  The pool is shared,
> so the damage is not confined to the caller that caused it.
>
> The per-cpu system_page_pool used by generic XDP hits this.
> skb_pp_cow_data() allocates data fragments with the raw packet length:
>
>         size = min_t(u32, len, PAGE_SIZE);
>         truesize = size;
>         page = page_pool_dev_alloc(pool, &page_off, &truesize);
>
> and later calls page_pool_dev_alloc_va() for a new skb head, which
> napi_build_skb() installs as skb->head.  Once frag_offset is odd that head
> is misaligned, and so is skb_shinfo(skb) == skb->head + skb->end.
>
> struct skb_shared_info begins with an atomic_t dataref, so the increment in
> __skb_clone() straddles a cache line.  On x86 with split lock detection --
> fatal for kernel split locks by default -- this panics the machine:
>
>   Oops: Split lock detected
>   RIP: 0010:skb_clone+0x154/0x1e0
>   Call Trace:
>    <IRQ>
>    raw_local_deliver+0x1ed/0x2c0
>    ip_protocol_deliver_rcu+0x54/0x1c0
>    ip_local_deliver_finish+0x85/0x100
>    ip_local_deliver+0x67/0x100
>    __netif_receive_skb_one_core+0x85/0xa0
>    process_backlog+0x87/0x130
>
> Reproduced by attaching any generic-mode XDP program to loopback and
> opening a RAW IPPROTO_UDP socket, which makes raw_local_deliver() clone
> every locally delivered UDP packet; ordinary DNS traffic then triggers it,
> roughly once per 2500 clones.  Observed on 6.12.101 and 7.1.8.
>
> Tracing page_pool_alloc_frag_netmem() over one such run shows the
> amplification -- two odd-sized requests, nine misaligned offsets:
>
>   requested size & 7:   0: 17035    5: 1    7: 1
>   frag_offset & 7:      0: 17028    3: 1    4: 1    5: 1    6: 1    7: 5
>
> and skb_pp_cow_data() returning heads that were aligned on entry:
>
>   head 0xffff8f4c86aeac00 -> 0xffff8f4c53a9a9c4 (&7=4)
>   head 0xffff8f4d6a8a42c0 -> 0xffff8f4c4f7b7a45 (&7=5)
>
> Round the fragment size up to at least the natural word size so fragments
> are always suitably aligned for the objects callers build on them.
> Architectures needing a larger DMA alignment keep it.
>
> This also makes the remainder computed in page_pool_alloc_netmem(),
>
>         *size = max_size - *offset;
>
> aligned, since max_size is a power of two -- which fixes the matching
> misalignment of skb->end.
>
> Verified with a controlled A/B under QEMU/KVM: same tree, same config, same
> compiler, same rootfs and identical traffic, differing only by this patch.  A
> SEC("xdp.frags") XDP_PASS program on lo plus UDP datagrams larger than
> max_head_size drives skb_pp_cow_data()'s fragment loop, which passes raw packet
> lengths to the pool.  Measured at the return of skb_pp_cow_data():
>
>                           unpatched   patched
>   skb_pp_cow_data calls       40800     40800
>   misaligned skb->head         1120         0
>   dataref at line offset >60     80         0
>
> The last row counts the accesses that actually fault: skb_shinfo()->dataref
> sits at head+end+0x20 and is a 4-byte atomic, so `lock incl` splits a 64-byte
> cache line only when that address lands at offset 61..63.  All 80 occurrences
> were at offset 61; the panic reported above was at offset 62.  Eliminating the
> misalignment removes every one of them.
>
> Same class of bug as commit 3bed3cc4156e ("net: Do not allocate page
> fragments that are not skb aligned"), which fixed the older
> netdev_alloc_frag()/napi_alloc_frag() allocators.
>
> Fixes: 53e0961da1c7 ("page_pool: add frag page recycling support in page pool")
> Signed-off-by: Florian Schauer <florian@schauer.to>
> ---
>  net/core/page_pool.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/net/core/page_pool.c b/net/core/page_pool.c
> index 1234567..89abcde 100644
> --- a/net/core/page_pool.c
> +++ b/net/core/page_pool.c
> @@ -1065,7 +1065,14 @@ netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,
>         if (WARN_ON(size > max_size))
>                 return 0;
>
> -       size = ALIGN(size, dma_get_cache_alignment());
> +       /* dma_get_cache_alignment() is 1 on DMA-coherent architectures, so on
> +        * its own it cannot keep frag_offset aligned.  Callers build objects
> +        * on these fragments -- skb_pp_cow_data() turns one into skb->head,
> +        * where skb_shinfo() must be aligned -- and the pool is shared, so an
> +        * odd-sized request must not skew every fragment that follows it.
> +        */
> +       size = ALIGN(size, max_t(unsigned int, dma_get_cache_alignment(),
> +                                sizeof(long)));

Perhaps the comment would not be needed with something more descriptive.

size = ALIGN(size, max_t(unsigned int, dma_get_cache_alignment(),
                      __alignof__(struct skb_shared_info)));

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

end of thread, other threads:[~2026-08-26 15:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 13:52 [PATCH net] page_pool: keep frag_offset aligned for odd-sized requests Florian Schauer
2026-08-26 15:43 ` Eric Dumazet

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