virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues
Subsystems (5)
v3 cover virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues
Two AF_XDP zero-copy fixes for virtio_net.
Patch 1: on arm64, binding a zero-copy socket fails with -ENOMEM and a "rejecting DMA map of vmalloc memory" warning whenever the device uses the DMA API (e.g. VIRTIO_F_ACCESS_PLATFORM), because the static TX header lives in the kernel image, which arm64 maps in the vmalloc area.
Patch 2: removing XDP, explicitly or when the device is unregistered, can leave a zero-copy pool bound to a queue above curr_queue_pairs. Unbinding the socket then fails in the driver, and the queue is refilled from the freed pool.
The suspend fix that was patch 3 in v2 is dropped from this series. Review found problems with it for pools on queues above curr_queue_pairs; it will be reworked and sent separately on top of these two.
Testing used KASAN kernels in QEMU with virtio-net-pci and iommu_platform=on:
- Patch 1 on arm64 (TCG, virt): binding an XDP_ZEROCOPY socket fails without the patch and succeeds with it.
- Patch 2 on x86_64 (KVM, q35, intel-iommu, two queue pairs): with a socket bound to an XDP queue, removing XDP and then closing the socket gives "Failed to disable zero-copy!" and a KASAN use-after-free in xp_alloc_batch() on net, and no warnings with this series. Unbinding the device from virtio_net with XDP attached and a socket on an XDP queue gives "Failed to disable zero-copy!" on net, and no warning with this series. In both cases the unregister then keeps waiting for eth0 to become free; that also happens with only XDP attached and no socket, so it is a separate issue.
The issues were found with the help of an AI coding assistant.
Changes in v3:
- Drop patch 3, to be reworked and sent separately (Sashiko).
- Patch 2: mention the unregister path in the changelog (Sashiko).
- Rebased on net 1d5e2c57e9ff; the code of patches 1 and 2 is unchanged.
Changes in v2:
- Patch 1: drop the sentence about cache lines from the changelog (Sashiko).
- New patch 2, for a pool left on a queue above curr_queue_pairs (Sashiko).
- Patch 3: rework (Sashiko).
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
Haishuang Yan (2):
virtio_net: Don't DMA map the kernel image for the AF_XDP TX header
virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
drivers/net/virtio_net.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
base-commit: 1d5e2c57e9ffc134e14b3f837c8d7c53b508bd22
Signature · 1 line
2.43.0
PATCH v3 1/2 virtio_net: Don't DMA map the kernel image for the AF_XDP TX header
virtnet_xsk_pool_enable() DMA maps the static xsk_hdr, which lives in the kernel image. On arm64 the kernel image is mapped in the vmalloc area, so when the device uses the DMA API the mapping is rejected:
virtio_net virtio0: rejecting DMA map of vmalloc memory
WARNING: drivers/virtio/virtio_ring.c:3882 ...
and binding an AF_XDP zero-copy socket fails with -ENOMEM.
Allocate the all-zero header per send queue when the pool is enabled and free it when it is disabled.
This was found while testing AF_XDP in QEMU on arm64, not in production. Tested in QEMU (TCG, arm64 virt, virtio-net-pci with iommu_platform=on) with a KASAN kernel: binding an XDP_ZEROCOPY socket fails without this patch and succeeds with it.
--- drivers/net/virtio_net.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +19 -4
@@ -315,6 +315,8 @@ struct send_queue {struct xsk_buff_pool *xsk_pool; +/* All-zero header shared by the AF_XDP packets sent on this queue */ +struct virtio_net_common_hdr *xsk_hdr;dma_addr_t xsk_hdr_dma_addr; };
@@ -500,8 +502,6 @@ struct virtio_net_common_hdr { }; }; -static struct virtio_net_common_hdr xsk_hdr; -static void virtnet_sq_free_unused_buf (struct virtqueue *vq,void *buf);static void virtnet_sq_free_unused_buf_done (struct virtqueue *vq); static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
@@ -5971,11 +5971,21 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,if (!rq->xsk_buffs)return -ENOMEM ; - hdr_dma =virtqueue_map_single_attrs (sq->vq, &xsk_hdr, vi->hdr_len, +/* +* Don't map a static header: the kernel image is not in the linear +* map on all architectures (e.g. arm64), so it can't be DMA mapped. +*/ + sq->xsk_hdr =kzalloc_obj (*sq->xsk_hdr); +if (!sq->xsk_hdr) { + err = -ENOMEM ; + goto err_free_buffs; + } + + hdr_dma =virtqueue_map_single_attrs (sq->vq, sq->xsk_hdr, vi->hdr_len,DMA_TO_DEVICE ,0 );if (virtqueue_map_mapping_error (sq->vq, hdr_dma)) { err = -ENOMEM ; - goto err_free_buffs; + goto err_free_hdr; } err =xsk_pool_dma_map (pool, dma_dev,0 );
@@ -6004,6 +6014,9 @@ static int virtnet_xsk_pool_enable(struct net_device *dev, err_xsk_map:virtqueue_unmap_single_attrs (rq->vq, hdr_dma, vi->hdr_len,DMA_TO_DEVICE ,0 ); +err_free_hdr: +kfree (sq->xsk_hdr); + sq->xsk_hdr =NULL ; err_free_buffs:kvfree (rq->xsk_buffs);return err;
@@ -6032,6 +6045,8 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)virtqueue_unmap_single_attrs (sq->vq, sq->xsk_hdr_dma_addr, vi->hdr_len,DMA_TO_DEVICE ,0 ); +kfree (sq->xsk_hdr); + sq->xsk_hdr =NULL ;kvfree (rq->xsk_buffs);return err;
base-commit: 1d5e2c57e9ffc134e14b3f837c8d7c53b508bd22
Signature · 1 line
2.43.0
PATCH v3 2/2 virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
In reply to Haishuang YanWith an XDP program attached, virtnet_xdp_set() adds nr_cpu_ids queue pairs for XDP_TX and raises curr_queue_pairs and real_num_rx_queues to match. virtnet_xsk_pool_enable() accepts any queue below curr_queue_pairs, so an AF_XDP zero-copy socket can be bound to one of them.
When the program is removed, the queue count goes down again while the pool stays bound to the driver. When the socket is closed afterwards, virtnet_xsk_pool_disable() rejects the queue with -EINVAL, and the core frees the pool anyway:
WARNING: net/xdp/xsk_buff_pool.c:160 at xp_disable_drv_zc+0x1ee/0x260 BUG: KASAN: slab-use-after-free in xp_alloc_batch+0x930/0xb40 Read of size 8 at addr ffff8880028eb400 by task ksoftirqd/0/14 xp_alloc_batch+0x930/0xb40 try_fill_recv+0xb27/0x26e0
as rq->xsk_pool still points to it and the queue is refilled from it.
The device going away with XDP attached, e.g. on hot-unplug or driver unbind, ends the same way without an explicit removal:unregister_netdevice_many_notify() removes XDP before the xsk notifier unbinds the socket, so the pool is disabled on a queue above curr_queue_pairs there as well.
Removing XDP must not fail, so let virtnet_xsk_pool_disable() accept any queue the driver has, below max_queue_pairs. The receive and send queues above curr_queue_pairs still exist, so they are unbound as usual.
This was pointed out by the Sashiko review of v1 of this series, and reproduced in QEMU by binding a socket to an XDP queue and then either removing XDP and closing the socket, or unbinding the device from virtio_net.
--- drivers/net/virtio_net.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +2 -1
@@ -6030,7 +6030,8 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)struct send_queue *sq;int err; -if (qid >= vi->curr_queue_pairs) +/* Removing XDP can leave the pool on a queue above curr_queue_pairs */ +if (qid >= vi->max_queue_pairs)return -EINVAL ; sq = &vi->sq[qid];
Signature · 1 line
2.43.0
Jakub Kicinski
maintainerRe: [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
Quoted 5 lines · On Wed, 11 Nov 2026 00:30:01 +0900 Haishuang Yan wrote:
With an XDP program attached,
virtnet_xdp_set()addsnr_cpu_idsqueue pairs forXDP_TXand raisescurr_queue_pairsandreal_num_rx_queuesto match.virtnet_xsk_pool_enable()accepts any queue belowcurr_queue_pairs, so anAF_XDPzero-copy socket can be bound to one of them.
Alright. I still think that auto-changing real_num_rx_queues to use Rx queues from queue pairs allocated for XDP Tx is fruitcake behavior. But the bandaid is simple enough so whatever.
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 11 Nov 2026 00:29:59 +0900 you wrote:
Two
AF_XDPzero-copy fixes forvirtio_net.Patch 1: on arm64, binding a zero-copy socket fails with
-ENOMEMand a "rejecting DMA map of vmalloc memory" warning whenever the device uses the DMA API (e.g.VIRTIO_F_ACCESS_PLATFORM), because the static TX header lives in the kernel image, which arm64 maps in the vmalloc area.[...]
Here is the summary with links:
- [net,v3,1/2] virtio_net: Don't DMA map the kernel image for the AF_XDP TX header (no matching commit)
- [net,v3,2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
https://git.kernel.org/netdev/net/c/1932779607d2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 11 Nov 2026 00:29:59 +0900 you wrote:
Two
AF_XDPzero-copy fixes forvirtio_net.Patch 1: on arm64, binding a zero-copy socket fails with
-ENOMEMand a "rejecting DMA map of vmalloc memory" warning whenever the device uses the DMA API (e.g.VIRTIO_F_ACCESS_PLATFORM), because the static TX header lives in the kernel image, which arm64 maps in the vmalloc area.[...]
Here is the summary with links:
- [net,v3,1/2] virtio_net: Don't DMA map the kernel image for the AF_XDP TX header
https://git.kernel.org/netdev/net-next/c/5d761fccffdf
- [net,v3,2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html