Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Björn Töpel" <bjorn@kernel.org>, bpf@vger.kernel.org
Cc: linux-rdma@vger.kernel.org
Subject: Re: [PATCH net-next 06/15] xsk: Lend UMEM chunks to the page pools of a queue
Date: Wed, 25 Nov 2026 13:00:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-233416@kernel.org> (raw)
In-Reply-To: <20261124125654.183091-6-bjorn@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] xsk: per-queue DMA device mismatch in xsk_sink_bind() and
  xp_find_dma_map()
- [High] xsk: xsk_sink_take_fill() recycles old-pool chunks instead of
  releasing them
- [Medium] xsk: xp_assign_dev() blocks standard zero-copy when
  MP_XDP_ONLY is active
It also noticed 2 related pre-existing issues this patch does not
introduce:
- [High] xsk: DMA mapping leak in __xp_dma_unmap() when IOVA is 0 [1]
- [Critical] xsk: false match in xp_find_dma_map() due to dangling
  netdev pointer [2]
--

commit 58bc067256d91ca8a9a5c3294236af1fb45e76be
Author: Björn Töpel <bjorn@kernel.org>

xsk: Lend UMEM chunks to the page pools of a queue

This commit adds the XDP_UMEM_DRV_LAYOUT flag and a page pool memory
provider (xsk_sink) so an AF_XDP socket can lend page-sized UMEM chunks to
the page pools of a bound RX queue.

> diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
> index 5ec092f7cb6f7..7ece7d9e39376 100644
> --- a/net/xdp/xsk_buff_pool.c
> +++ b/net/xdp/xsk_buff_pool.c
[ ... ]
> @@ -211,6 +215,13 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
>  	 */
>  	hlist_add_head(&pool->dev_node, &netdev->xsk_pools);
>  
> +	if (pool->umem->flags & XDP_UMEM_DRV_LAYOUT) {
> +		err = xsk_sink_bind(pool, netdev, queue_id, flags);
> +		if (err)
> +			goto err_unbind;
> +		return 0;
> +	}
> +
>  	if (force_copy)
>  		/* For copy-mode, we are done. */
>  		return 0;

[Severity: Medium]
Once xsk_sink_bind() installs the MP_XDP_ONLY provider on a queue,
dev_get_min_mp_channel_count(netdev) returns non-zero for the whole
net_device.  Because xp_assign_dev() still checks
dev_get_min_mp_channel_count(netdev) rather than dev_mp_blocks_xdp(netdev)
further down, does binding an XDP_UMEM_DRV_LAYOUT socket on one queue
cause standard AF_XDP sockets on other queues of the same device to fail
with -EBUSY or silently fall back to copy mode?

[ ... ]

> diff --git a/net/xdp/xsk_sink.c b/net/xdp/xsk_sink.c
> new file mode 100644
> index 0000000000000..b78116f25e712
> --- /dev/null
> +++ b/net/xdp/xsk_sink.c
> @@ -0,0 +1,416 @@
[ ... ]
> +static u32 xsk_sink_take_fill(struct xsk_sink *sink, struct page_pool *pp)
> +{
[ ... ]
> +	for (i = 0; i < entries; i++) {
> +		addr = ring->desc[(cons + i) & mask];
> +		niov = &niovs[array_index_nospec((u32)(addr >> PAGE_SHIFT),
> +						 nr)];
> +		/* A chunk lent twice would be in two places at once */
> +		if (unlikely(addr >= (u64)nr << PAGE_SHIFT ||
> +			     READ_ONCE(niov->mp_state) == XSK_SINK_LENT)) {
> +			fq->invalid_descs++;
> +			continue;
> +		}
> +
> +		WRITE_ONCE(niov->mp_state, XSK_SINK_LENT);
> +		netmem = net_iov_to_netmem(niov);
> +		owner = netmem_get_pp(netmem);
> +		if (owner && owner != pp) {
> +			page_pool_put_unrefed_netmem(owner, netmem, -1, false);
> +			continue;
> +		}
> +		if (!owner)
> +			net_mp_niov_set_page_pool(pp, niov);
> +		pp->alloc.cache[n++] = netmem;
> +	}

[Severity: High]
When a chunk in the FILL ring still belongs to an earlier page pool
(owner != pp), does calling page_pool_put_unrefed_netmem(owner, ...)
recycle the chunk into owner->ring instead of releasing it onto
sink->released?

net/core/page_pool.c:page_pool_put_unrefed_netmem() {
	...
	netmem = __page_pool_put_page(pool, netmem, dma_sync_size,
				      allow_direct);
	if (netmem && !page_pool_recycle_in_ring(pool, netmem)) {
		/* Cache full, fallback to free pages */
		recycle_stat_inc(pool, ring_full);
		page_pool_return_netmem(pool, netmem);
	}
}

Because __page_pool_page_can_be_recycled() is always true for a net_iov,
page_pool_put_unrefed_netmem() puts the chunk into owner->ring and only
calls page_pool_return_netmem() -> xsk_sink_release_netmem() when that
ring is full.  During netdev_rx_queue_reconfig(), ndo_queue_mem_alloc()
and ndo_queue_start() refill the new page pool before ndo_queue_mem_free()
destroys the old one, and even after page_pool_destroy(owner) runs, any
in-flight chunks keep owner alive in deferred release.  Refills on the new
pool will consume FILL descriptors and strand those chunks in the old
pool's ptr_ring instead of supplying buffers to pp.

Calling page_pool_return_netmem(owner, netmem) (or transferring the chunk
directly to pp after releasing it from owner) would avoid recycling into
the old pool.

[ ... ]

> +/* No queue DMAs to the UMEM any longer */
> +static void xsk_sink_stop(struct xsk_sink *sink)
> +{
> +	bool unmap;
> +
> +	spin_lock_bh(&sink->lock);
> +	WRITE_ONCE(sink->rxq, NULL);
> +	unmap = sink->fq;
> +	sink->fq = NULL;
> +	spin_unlock_bh(&sink->lock);
> +
> +	/* Wait for wakeups that found the sink or the queue */
> +	synchronize_net();
> +	if (unmap)
> +		xp_dma_unmap(sink->pool, XSK_SINK_DMA_ATTR);
> +}

[Severity: High]
This problem wasn't introduced by this patch, but xp_dma_unmap() ->
__xp_dma_unmap() checks if (*dma) before unmapping each entry [1],
can that leak DMA mappings when a page is assigned a valid IOVA of 0?

net/xdp/xsk_buff_pool.c:__xp_dma_unmap() {
	...
	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
		dma = &dma_map->dma_pages[i];
		if (*dma) {
			*dma &= ~XSK_NEXT_PG_CONTIG_MASK;
			dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
					     DMA_BIDIRECTIONAL, attrs);
			*dma = 0;
		}
	}
	...
}

On IOMMU or architecture configurations where 0 is a valid DMA address,
__xp_dma_unmap() skips dma_unmap_page_attrs() for that page, leaking the
mapping across pool teardown.

[ ... ]

> +int xsk_sink_bind(struct xsk_buff_pool *pool, struct net_device *dev,
> +		  u16 queue_id, u16 flags)
> +{
[ ... ]
> +	/* The device of the queue's page pools, which may differ per queue */
> +	dma_dev = netdev_queue_get_dma_dev(dev, queue_id, NETDEV_QUEUE_TYPE_RX);
> +	if (!dma_dev)
> +		return -EOPNOTSUPP;
> +
> +	/* Without a program, drivers would build skbs from the buffers */
> +	if (!dev_xdp_drv_prog_held(dev))
> +		return -ENOENT;
> +
> +	err = xp_dma_map(pool, dma_dev, XSK_SINK_DMA_ATTR, pool->umem->pgs,
> +			 pool->umem->npgs);

[Severity: Critical]
This problem wasn't introduced by this patch, but xp_dma_map() looks up
existing mappings in xp_find_dma_map() by comparing raw dma_map->netdev ==
pool->netdev pointers without holding a netdev reference on
dma_map->netdev [2].  If the original net_device is freed while
the UMEM stays alive and a new net_device is allocated at the same address,
xp_find_dma_map() can match the dangling pointer and reuse stale DMA
addresses on the new device.

> +	if (err)
> +		return err;
> +
> +	sink = xsk_sink_create(pool, dma_dev);

[Severity: High]
When a UMEM is shared with XDP_SHARED_UMEM across two queues of the same
net_device that have different per-queue DMA devices, can xp_dma_map() and
xsk_sink_create() pair sink->dev with DMA addresses mapped for another
device?

net/xdp/xsk_buff_pool.c:xp_find_dma_map() {
	...
	list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
		if (dma_map->netdev == pool->netdev)
			return dma_map;
	}
}

xp_find_dma_map() checks only dma_map->netdev == pool->netdev and does not
check that dma_map->dev matches dma_dev.  On a multi-DMA-device netdev,
binding a second shared-UMEM socket reuses the first queue's dma_pages
while xsk_sink_create(pool, dma_dev) sets sink->dev = dma_dev, so
xsk_sink_init() sees pp->p.dev == sink->dev and the second queue performs
DMA syncs and transfers with IOVAs mapped for the first queue's DMA
device.

[1] https://sashiko.dev/#/bug/linux-5e2b8325-f1ba-4785-8981-9032096f3c3f
[2] https://sashiko.dev/#/bug/linux-fb37d535-f34d-40eb-b2c2-d903fa118a91

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124112707.118678-1-bjorn@kernel.org?part=6

  parent reply	other threads:[~2026-11-25 13:00 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 11:26 [PATCH net-next 00/15] xsk: Receive in the device's buffer layout Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 01/15] xdp: Let memory providers offer XDP a view of their net_iovs Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 02/15] xdp: Keep the shared info of a buffer in kernel memory Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 03/15] net: Add memory providers that only feed XDP Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 04/15] page_pool: Let memory providers take back net_iovs Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 05/15] xdp: Run programs on buffers that must not hold kernel structures Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 06/15] xsk: Lend UMEM chunks to the page pools of a queue Björn Töpel
2026-11-24 14:34   ` Magnus Karlsson
2026-11-24 15:12     ` Björn Töpel
2026-11-25 13:00   ` sashiko-bot [this message]
2026-11-24 12:56 ` [PATCH net-next 07/15] xsk: Deliver to the socket in the device's buffer layout Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 08/15] xsk: Receive " Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 09/15] eth: fbnic: Support XDP redirect Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 10/15] eth: fbnic: Receive into XDP-only memory providers Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 11/15] net: Add netif_rxq_has_xdp_only_mp() Björn Töpel
2026-11-24 12:56 ` [PATCH net-next 12/15] eth: mlx5e: Receive into XDP-only memory providers Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 13/15] eth: bnxt: " Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 14/15] selftests: net: xsk: Test the device's RX buffer layout Björn Töpel
2026-11-25 13:00   ` sashiko-bot
2026-11-24 12:56 ` [PATCH net-next 15/15] Documentation: networking: Describe AF_XDP RX in the device's layout Björn Töpel
2026-11-25 13:00 ` [PATCH net-next 00/15] xsk: Receive in the device's buffer layout 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-233416@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --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