Intel-Wired-Lan Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kuniyuki Iwashima" <kuniyu@google.com>
Cc: intel-wired-lan@lists.osuosl.org
Subject: Re: [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages.
Date: Sat, 05 Dec 2026 13:15:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-251555@kernel.org> (raw)
In-Reply-To: <20261205125237.2290932-5-kuniyu@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] page_pool: unordered dma_unmap_cnt increment risks UAF on
  pool destroy
- [Medium] page_pool: dma_unmap_cnt shares cacheline on 128B/256B SMP
  architectures
--

commit 478e1772cca7ebcaee62ee863cf1ca0ac47a1dc8
Author: Kuniyuki Iwashima <kuniyu@google.com>

page_pool: Allow drivers to unmap DMA before returning pages.

This commit adds opt-in early DMA unmapping to page_pool so drivers can
track active DMA mappings and unmap in-flight pages before they are
returned to the pool.

> diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h
> index 5306e34105eb6..89bde9df0872c 100644
> --- a/include/net/page_pool/types.h
> +++ b/include/net/page_pool/types.h

[ ... ]

> @@ -261,6 +263,9 @@ struct page_pool {
>  		ktime_t detach_time;
>  		u32 id;
>  	} user;
> +
> +	/* Separated from pages_state_release_cnt by slow/user fields */
> +	atomic_t dma_unmap_cnt;
>  };

[Severity: Medium]
Can dma_unmap_cnt still share a cacheline with pages_state_release_cnt on
architectures with 128-byte or 256-byte cachelines?

struct ptr_ring is SMP_CACHE_BYTES-aligned, so mp_priv begins at cacheline
offset 0.  On 64-bit kernels, pages_state_release_cnt sits at +40 (+32
without CONFIG_PAGE_POOL_STATS) and dma_unmap_cnt sits at +120 (+112), so
both share a cacheline on powerpc64 (128B) and s390 (256B).

When early DMA unmapping is active, the NAPI CPU updates dma_unmap_cnt in
page_pool_dma_unmap_netmem() while remote CPUs freeing skbs update
pages_state_release_cnt in page_pool_return_netmem(), causing cross-CPU
false sharing.

Marking dma_unmap_cnt with ____cacheline_aligned_in_smp would guarantee
separation across all SMP_CACHE_BYTES values.

[ ... ]

> diff --git a/net/core/page_pool.c b/net/core/page_pool.c
> index d7c88c0b67e53..cf2afa2952827 100644
> --- a/net/core/page_pool.c
> +++ b/net/core/page_pool.c

[ ... ]

> @@ -752,7 +755,23 @@ static __always_inline void __page_pool_release_netmem_dma(struct page_pool *poo
>  	if (!pool->dma_map)
>  		return;
>  
> -	__page_pool_unmap_netmem_dma(pool, netmem);
> +	if (static_branch_unlikely(&page_pool_dma_early_unmap) &&
> +	    pool->dma_unmap) {
> +		/* Pair with page_pool_dma_unmap_netmem() / releasing CPU
> +		 * before reading pp_magic in netmem_get_dma_index().
> +		 */
> +		smp_acquire__after_ctrl_dep();
> +
> +		if (PP_DMA_INDEX_BITS && !netmem_get_dma_index(netmem))
> +			return;
> +
> +		__page_pool_unmap_netmem_dma(pool, netmem);
> +
> +		atomic_inc(&pool->dma_unmap_cnt);
> +	} else {
> +		__page_pool_unmap_netmem_dma(pool, netmem);
> +	}
> +
>  	page_pool_set_dma_addr_netmem(netmem, 0);
>  	if (likely(PP_DMA_INDEX_BITS))
>  		netmem_set_dma_index(netmem, 0);

[Severity: Medium]
Can this unordered atomic_inc(&pool->dma_unmap_cnt) write to freed memory
when the last page is returned concurrently with pool teardown?

In page_pool_return_netmem(), __page_pool_release_netmem_dma() increments
dma_unmap_cnt after xa_cmpxchg() unlocks xa_lock, and the caller then
updates pages_state_release_cnt with atomic_inc_return_relaxed().  Both
atomics are unordered and sit on separate cachelines.

CPU0 (last page return)           CPU1 (teardown)
page_pool_return_netmem()
  __page_pool_release_netmem_dma()
    atomic_inc(&pool->dma_unmap_cnt)
      (delayed / reordered)
  atomic_inc_return_relaxed(
      &pool->pages_state_release_cnt)
                                  page_pool_release()
                                    page_pool_inflight()
                                      atomic_read(&pages_state_release_cnt)
                                      /* sees inflight == 0 */
                                    __page_pool_destroy(pool)
                                      kfree(pool)
    dma_unmap_cnt write commits
      <-- use-after-free

On weakly ordered architectures such as arm64, the pages_state_release_cnt
increment can become visible before atomic_inc(&pool->dma_unmap_cnt)
completes.

Because page_pool_enable_dma_unmap() requires !dma_dev_need_sync(),
page_pool_scrub() skips synchronize_net().  When another pool still has
dma_unmap enabled, static_branch_dec() takes the lockless fast path,
leaving nothing to order the write before kfree(pool).

Using release ordering on pages_state_release_cnt (such as
atomic_inc_return_release()) paired with acquire ordering when
page_pool_release() observes inflight == 0 would close this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205125237.2290932-1-kuniyu@google.com?part=4

  reply	other threads:[~2026-12-05 13:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 12:52 [PATCH v2 iwl-next 0/5] idpf: Avoid IOVA exhaustion under DMA-FQ IOMMU Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 1/5] idpf: Reuse idpf_rx_update_bufq_desc() in idpf_rx_post_buf_desc() Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 2/5] idpf: Use coherent DMA buffer for Rx header split without XDP Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 3/5] netmem: Annotate data-races around pp_magic Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages Kuniyuki Iwashima
2026-12-05 13:15   ` sashiko-bot [this message]
2026-12-05 12:52 ` [PATCH v2 iwl-next 5/5] idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ IOMMU Kuniyuki Iwashima
2026-12-05 13:16   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251555@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=kuniyu@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox