* [PATCH net v3 1/2] virtio_net: Don't DMA map the kernel image for the AF_XDP TX header
2026-11-10 15:29 [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues Haishuang Yan
@ 2026-11-10 15:30 ` Haishuang Yan
2026-11-10 15:30 ` [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs Haishuang Yan
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Haishuang Yan @ 2026-11-10 15:30 UTC (permalink / raw)
To: Michael S. Tsirkin, Jason Wang, Xuan Zhuo
Cc: Eugenio Pérez, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
Jesper Dangaard Brouer, John Fastabend, Stanislav Fomichev,
netdev, virtualization, bpf, linux-kernel
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.
Fixes: 21a4e3ce6dc7 ("virtio_net: xsk: bind/unbind xsk for tx")
Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
drivers/net/virtio_net.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index dd2c41ef6fbe..4f5d8812f75b 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -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
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
2026-11-10 15:29 [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues Haishuang Yan
2026-11-10 15:30 ` [PATCH net v3 1/2] virtio_net: Don't DMA map the kernel image for the AF_XDP TX header Haishuang Yan
@ 2026-11-10 15:30 ` Haishuang Yan
2026-11-17 0:08 ` Jakub Kicinski
2026-11-17 0:20 ` [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues patchwork-bot+netdevbpf
2026-11-17 0:20 ` patchwork-bot+netdevbpf
3 siblings, 1 reply; 6+ messages in thread
From: Haishuang Yan @ 2026-11-10 15:30 UTC (permalink / raw)
To: Michael S. Tsirkin, Jason Wang, Xuan Zhuo
Cc: Eugenio Pérez, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
Jesper Dangaard Brouer, John Fastabend, Stanislav Fomichev,
netdev, virtualization, bpf, linux-kernel
With 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.
Fixes: 09d2b3182c8e ("virtio_net: xsk: bind/unbind xsk for rx")
Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
drivers/net/virtio_net.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 4f5d8812f75b..f9d227060375 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -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];
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs
2026-11-10 15:30 ` [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs Haishuang Yan
@ 2026-11-17 0:08 ` Jakub Kicinski
0 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2026-11-17 0:08 UTC (permalink / raw)
To: Haishuang Yan
Cc: Michael S. Tsirkin, Jason Wang, Xuan Zhuo, Eugenio Pérez,
Andrew Lunn, David S. Miller, Eric Dumazet, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev, netdev, virtualization, bpf,
linux-kernel
On Wed, 11 Nov 2026 00:30:01 +0900 Haishuang Yan wrote:
> With 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.
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues
2026-11-10 15:29 [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues Haishuang Yan
2026-11-10 15:30 ` [PATCH net v3 1/2] virtio_net: Don't DMA map the kernel image for the AF_XDP TX header Haishuang Yan
2026-11-10 15:30 ` [PATCH net v3 2/2] virtio_net: Disable AF_XDP pools on queues above curr_queue_pairs Haishuang Yan
@ 2026-11-17 0:20 ` patchwork-bot+netdevbpf
2026-11-17 0:20 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-11-17 0:20 UTC (permalink / raw)
To: Haishuang Yan
Cc: mst, jasowangio, xuanzhuo, eperezma, andrew+netdev, davem,
edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend, sdf,
netdev, virtualization, bpf, linux-kernel
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Wed, 11 Nov 2026 00:29:59 +0900 you wrote:
> 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.
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues
2026-11-10 15:29 [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues Haishuang Yan
` (2 preceding siblings ...)
2026-11-17 0:20 ` [PATCH net v3 0/2] virtio_net: AF_XDP zero-copy fixes for arm64 and XDP queues patchwork-bot+netdevbpf
@ 2026-11-17 0:20 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-11-17 0:20 UTC (permalink / raw)
To: Haishuang Yan
Cc: mst, jasowangio, xuanzhuo, eperezma, andrew+netdev, davem,
edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend, sdf,
netdev, virtualization, bpf, linux-kernel
Hello:
This series was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Wed, 11 Nov 2026 00:29:59 +0900 you wrote:
> 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.
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread