Article View: linux.kernel.netdev
Article #12718[PATCH net v2 3/3] virtio_net: Keep AF_XDP zero-copy pools bound across suspend
From: Haishuang Yan
Date: Sun, 08 Nov 2026 19:38
Date: Sun, 08 Nov 2026 19:38
238 lines
16587 bytes
16587 bytes
virtnet_freeze() frees the receive and send queues and virtnet_restore()
allocates new ones, but AF_XDP sockets stay bound to the device in the
xsk core. Whatever the driver set up for a zero-copy pool is lost on
freeze: the pool's DMA mapping, the xsk_buffs array, the TX header and
its mapping are leaked, and the queue comes back with no pool. When the
socket is closed afterwards, the core asks the driver to disable the
pool again:
WARNING: net/core/xdp.c:140 ... Missing register, driver bug
BUG: KASAN: null-ptr-deref in xp_dma_unmap
Read of size 8 at addr 0000000000000090
as virtnet_xsk_pool_disable() unregisters an xdp_rxq_info that was
never registered and passes the NULL rq->xsk_pool to
xsk_pool_dma_unmap().
Keep the pools DMA mapped across suspend, as the DMA device does not
change, and only move the driver state of a pool from the old queues to
the new ones. On freeze, release it in remove_vq_common() once the
device is reset and the posted buffers are back in the pool. On restore,
set it up again before virtnet_open() fills the queues and enables NAPI.
If that fails for a queue, the pool is left DMA mapped and
virtnet_xsk_pool_disable() only unmaps it when the socket is closed. A
pool left above curr_queue_pairs by removing XDP is not set up again,
so it is unmapped on freeze.
This was found by code review of the suspend path and reproduced in
QEMU by running a suspend cycle with an AF_XDP socket bound, not in
production.
Fixes: 09d2b3182c8e ("virtio_net: xsk: bind/unbind xsk for rx")
Signed-off-by: Haishuang Yan <yanhaishuang@cmss.chinamobile.com>
---
drivers/net/virtio_net.c | 151 +++++++++++++++++++++++++++++++++++++--
1 file changed, 145 insertions(+), 6 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index f9d227060375..d9c920f42126 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -5774,6 +5774,8 @@ static void virtnet_freeze_down(struct virtio_device *vdev)
}
static int init_vqs(struct virtnet_info *vi);
+static void virtnet_xsk_pools_release(struct virtnet_info *vi);
+static void virtnet_xsk_pools_attach(struct virtnet_info *vi);
static int virtnet_restore_up(struct virtio_device *vdev)
{
@@ -5790,13 +5792,13 @@ static int virtnet_restore_up(struct virtio_device *vdev)
virtio_device_ready(vdev);
- if (netif_running(vi->dev)) {
- rtnl_lock();
+ rtnl_lock();
+ virtnet_xsk_pools_attach(vi);
+ if (netif_running(vi->dev))
err = virtnet_open(vi->dev);
- rtnl_unlock();
- if (err)
- goto err_destroy_pools;
- }
+ rtnl_unlock();
+ if (err)
+ goto err_destroy_pools;
netif_tx_lock_bh(vi->dev);
netif_device_attach(vi->dev);
@@ -5808,6 +5810,7 @@ static int virtnet_restore_up(struct virtio_device *vdev)
err_destroy_pools:
virtio_reset_device(vdev);
free_unused_bufs(vi);
+ virtnet_xsk_pools_release(vi);
virtnet_destroy_page_pools(vi);
virtnet_del_vqs(vi);
return err;
@@ -6038,6 +6041,13 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
rq = &vi->rq[qid];
pool = rq->xsk_pool;
+ if (!pool) {
+ /* Binding it again on restore failed, only the DMA map is left */
+ pool = xsk_get_pool_from_qid(dev, qid);
+ if (pool)
+ xsk_pool_dma_unmap(pool, 0);
+ return 0;
+ }
err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
@@ -6053,6 +6063,133 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
return err;
}
+/*
+ * Freeze frees the receive and send queues, but the AF_XDP sockets stay
+ * bound and the xsk core keeps their pools. Release only the driver state
+ * of the zero-copy pools here, once the device is reset and the buffers
+ * posted to it are back in the pools. The pools stay DMA mapped, since the
+ * DMA device is still the same after restore and the buffers on their free
+ * lists keep their DMA addresses.
+ *
+ * A pool left above curr_queue_pairs by removing XDP is not bound again
+ * on restore, so unmap it as well.
+ */
+static void virtnet_xsk_pools_release(struct virtnet_info *vi)
+{
+ int i;
+
+ for (i = 0; i < vi->max_queue_pairs; i++) {
+ struct receive_queue *rq = &vi->rq[i];
+ struct send_queue *sq = &vi->sq[i];
+
+ if (rq->xsk_pool) {
+ if (i >= vi->curr_queue_pairs)
+ xsk_pool_dma_unmap(rq->xsk_pool, 0);
+ xdp_rxq_info_unreg(&rq->xsk_rxq_info);
+ kvfree(rq->xsk_buffs);
+ rq->xsk_buffs = NULL;
+ rq->xsk_pool = NULL;
+ }
+
+ if (sq->xsk_pool) {
+ 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;
+ sq->xsk_pool = NULL;
+ }
+ }
+}
+
+/* Set up the driver state of a still DMA mapped pool on a new queue pair. */
+static int virtnet_xsk_pool_attach(struct virtnet_info *vi, u16 qid,
+ struct xsk_buff_pool *pool)
+{
+ struct receive_queue *rq = &vi->rq[qid];
+ struct send_queue *sq = &vi->sq[qid];
+ struct device *dma_dev;
+ dma_addr_t hdr_dma;
+ int err;
+
+ dma_dev = virtqueue_dma_dev(rq->vq);
+ if (!rq->page_pool || !dma_dev || dma_dev != virtqueue_dma_dev(sq->vq))
+ return -EINVAL;
+
+ rq->xsk_buffs = kvzalloc_objs(*rq->xsk_buffs,
+ virtqueue_get_vring_size(rq->vq));
+ if (!rq->xsk_buffs)
+ return -ENOMEM;
+
+ 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_hdr;
+ }
+
+ err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qid,
+ rq->napi.napi_id);
+ if (err < 0)
+ goto err_unmap_hdr;
+
+ err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info,
+ MEM_TYPE_XSK_BUFF_POOL, NULL);
+ if (err < 0)
+ goto err_unreg;
+
+ xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info);
+
+ sq->xsk_hdr_dma_addr = hdr_dma;
+ sq->xsk_pool = pool;
+ rq->xsk_pool = pool;
+
+ return 0;
+
+err_unreg:
+ xdp_rxq_info_unreg(&rq->xsk_rxq_info);
+err_unmap_hdr:
+ virtqueue_unmap_single_attrs(sq->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);
+ rq->xsk_buffs = NULL;
+ return err;
+}
+
+/*
+ * On restore, set the zero-copy pools up again on the new queues before
+ * they are filled and their NAPI is enabled.
+ */
+static void virtnet_xsk_pools_attach(struct virtnet_info *vi)
+{
+ struct xsk_buff_pool *pool;
+ int i, err;
+
+ ASSERT_RTNL();
+
+ for (i = 0; i < vi->curr_queue_pairs; i++) {
+ pool = xsk_get_pool_from_qid(vi->dev, i);
+ if (!pool || !pool->umem->zc)
+ continue;
+
+ /* The pool stays DMA mapped until it is disabled */
+ err = virtnet_xsk_pool_attach(vi, i, pool);
+ if (err)
+ netdev_warn(vi->dev,
+ "failed to bind AF_XDP pool to queue %d: %d\n",
+ i, err);
+ }
+}
+
static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp)
{
if (xdp->xsk.pool)
@@ -7208,6 +7345,8 @@ static void remove_vq_common(struct virtnet_info *vi)
/* Free unused buffers in both send and recv, if any. */
free_unused_bufs(vi);
+ virtnet_xsk_pools_release(vi);
+
/*
* Rule of thumb is netdev_tx_reset_queue() should follow any
* skb freeing not followed by netdev_tx_completed_queue()
--
2.43.0
Technical Details
Message-ID:
<20261108193845.73621-4-yanhaishuang@cmss.chinamobile.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358704-linux-kernel-netdev=dewy.ruReferences:
<20261108193845.73621-1-yanhaishuang@cmss.chinamobile.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358704-linux-kernel-netdev=dewy.ru From: yanhaishuang@cmss.chinamobile.com (Haishuang Yan) Newsgroups: linux.kernel.netdev Subject: [PATCH net v2 3/3] virtio_net: Keep AF_XDP zero-copy pools bound across suspend Date: Mon, 9 Nov 2026 04:38:39 +0900 Organization: Usenet Network Message-ID: <20261108193845.73621-4-yanhaishuang@cmss.chinamobile.com> References: <20261108193845.73621-1-yanhaishuang@cmss.chinamobile.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="115442"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:kiQwAZAyDhpjrcKUMOWnbVAvxHA= sha256:nYrC2D++keq0e8F8vEWBhATTWqY3QwnZKxY9SNS+vg8= sha1:OppgNQSlEs9bvfUxpNZdj3kNsQ0= sha256:6ZODnkxQkPwr+Rxpu+KsPGFvf8qg/UV1uZ7m6XVKqjM= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id CCCCE113565; Sun, 8 Nov 2026 14:41:45 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.749 X-Spam-Status: No, score=-2.749 tagged_above=-9999 required=6.31 tests=[DATE_IN_FUTURE_06_12=0.001, DCC_REPUT_70_89=0.1, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=fail (1024-bit key) reason="fail (body has been altered)" header.d=cmss.chinamobile.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id M0_Zxh82afAK; Sun, 8 Nov 2026 14:41:35 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-358704-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=cmss.chinamobile.com X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id A5F0F113563; Sun, 8 Nov 2026 14:41:27 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 5311E48CA2; Sun, 8 Nov 2026 11:39:45 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794137978; b=gDWnPnvARmgES0K6Xllab/khKsp4Q5FfeukU+M9tagGqgid6jBWhkoQ93nb/LbhAY3oa pBQ6VGFxbpS/QcwChs1kyB7CEZwYPHzOe8F911hdGuNcwB+yT0jNnnRjqN6+z0yrAjP2X O+UM1zakYKW0MbFJKr8YkvPoxBBnlCPEalNrEG33XLON9O8e950AQROs6zFufBoN0EQtl MCxiEQ9MxP+Q48Vf1oZBd0dNYuLI1upFhWjZ+OoTkNw2m6lI9AyAFcEZ+AcKf6sYIVGla K1GZJ+JKeNEbh1UTfogQiqEAfS1D3Eu0awVampmj2N7sjOOyaQKTNIANRvt+jmikcXA== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794137978; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:X-RM-TagInfo: X-RM-SPAM-FLAG:Received:X-RM-TRANSID:X-RM-TagInfo:X-RM-SPAM-FLAG: Received:X-RM-TRANSID:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-Mailing-List:List-Id:List-Post:List-Help: List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=YI+QK96xKMFwFE4sByJjvSpEiF0SUOoelmRtxJdJZqo=; b=dpuiq70WHy/3N6nJF6rYVPsvlu+vcZxzCV/+TgEcaDPV1zBulcnSknJD6C7GyJ+PFv/a IBosL+VXcqTHTvMiEeZQWj1IKOHRDu6e34fgBtRMob23rAI5mEvtP3LN3P4PAWzj3Xzc0 9mkir8UHvEf18i9e5F7rRymzgr+fKAcwF+7LwUExcK6Dbhgc49QvdtPPAImkhlk7FbF6q 1h2VosaZvGw3UBmZGvAbzuDr2f0s0GqUK3byaa5JWHeIdGaxUnKQygYgh12v3WupWQVJr 6S3R6crAFwm93RW6KA7oayUaDEN0vu8CXqKT86ZoZpxGvU0wsCEhtkvehuNHtQBbS7w== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=WHFiWLPc; dmarc=pass header.from=cmss.chinamobile.com; spf=pass smtp.mailfrom=cmss.chinamobile.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92894606D807; Sun, 8 Nov 2026 11:39:38 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=WHFiWLPc X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=111.22.67.139 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794137972; b=eAQ6riO5qCIfI9QG+KPtIYctTaGccgAadAdQI6y6QAKZqNPtKAVy2/LTGw8sPAN8hQaW IY5C1wTCbP6yuzSpr5DprpFgI3EP7D/vAiJka0k7q9tyhefa9/D351ItQ+ad1XJoqATp7 8MtcI/JgPe9nzhkxav/0IRkYRQkWqoDIoZaK+OOx7IGDV35G7BjaKeEgqDNCKCymFRdag CzyYB4IpSHRDt3NkR6MN7jNHSsa6b8EMX/8l4uBGtPgAa+X8TRWex1cJEtsYs7WlNzYVz iZ9rWG0woF2/P+ORIK/TyPHhx03G0Wj1ESmEh7o3RwA/101kbELTw6qp3nFcQNH7huQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794137972; h=DMARC-Filter:DKIM-Signature:X-RM-TagInfo:X-RM-SPAM-FLAG:Received: X-RM-TRANSID:X-RM-TagInfo:X-RM-SPAM-FLAG:Received:X-RM-TRANSID:From: To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=YI+QK96xKMFwFE4sByJjvSpEiF0SUOoelmRtxJdJZqo=; b=DzTyCAwW9T+2JKzb7d7n94FkLikcUz0E2AOtCgsirQutH036CghphzvH8+DJMydQtgIF CngKAHRUMV6oJEe19V1RjzYyvdBqcUp8wPpi7taQ4EDeF6MlietKWOoFf0EOaWfdmvipA SZCWSmbqZSUyTXrvjazoi1/3YImzNKHApsyQzjLnMDid3G71VOuc7Fo0JDwflGVBtJWHj LqF+pQUPhiTwMZ/zwGlcUxCFYRIvMmzpcr8wKrFFYLf7XWaePp71qP9/7d3r0EXN4SdLw wOZYP2NtXZAJlyYcRvN3EEdtAdv1bih+utNDQdAelzTxOwIRaJ/fXo7ElnIIXzkNPHQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=cmss.chinamobile.com; spf=pass smtp.mailfrom=cmss.chinamobile.com; dkim=pass header.d=cmss.chinamobile.com header.i=@cmss.chinamobile.com header.a=rsa-sha256 header.s=default header.b=WHFiWLPc; arc=none smtp.remote-ip=111.22.67.139 X-Received: from cmccmta6.chinamobile.com (cmccmta6.chinamobile.com [111.22.67.139]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 857A5606A32A; Sun, 8 Nov 2026 11:39:30 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 857A5606A32A X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmss.chinamobile.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmss.chinamobile.com X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmss.chinamobile.com; s=default; l=0; h=from:subject:message-id:to:cc:mime-version; bh=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=; b=WHFiWLPcp/qEP9VqwRM22BotBJx+p+jvBvs2luGpaDiHO+IJzqfTqp/v8Ijkm7gvf9zjnlnHGi68X yKVDcRmEFqblpH6C3LZzwJhvgcuE2EiUo7TAvdNCxhT0d/SeBeeDBOwUi99xdaw2G0bL2pHsiE4jy7 GnRUhOakAvV8PzDY= X-RM-TagInfo: emlType=0 X-RM-SPAM-FLAG: 00000000 X-Received: from spf.mail.chinamobile.com (unknown[10.188.0.87]) by rmmx-syy-dmz-app01-12001 (RichMail) with SMTP id 2ee16af05f6cd92-9705a; Sun, 08 Nov 2026 19:39:29 +0800 (CST) X-RM-TRANSID: 2ee16af05f6cd92-9705a X-RM-TagInfo: emlType=0 X-RM-SPAM-FLAG: 00000000 X-Received: from (unknown[74.82.203.40]) by rmsmtp-syy-appsvr10-12010 (RichMail) with SMTP id 2eea6af05f47e53-666cf; Sun, 08 Nov 2026 19:39:29 +0800 (CST) X-RM-TRANSID: 2eea6af05f47e53-666cf X-Cc: Eugenio PΓ©rez <eperezma@redhat.com>, Andrew Lunn <andrew+netdev@lunn.ch>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Jesper Dangaard Brouer <hawk@kernel.org>, John Fastabend <john.fastabend@gmail.com>, Stanislav Fomichev <sdf@fomichev.me>, netdev@vger.kernel.org, virtualization@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: git-send-email 2.43.0 X-In-Reply-To: <20261108193845.73621-1-yanhaishuang@cmss.chinamobile.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list