Skip to contentSkip to conversation

virtio_net: add page_pool support for buffer allocation

PATCH Applied Vishwanath Seshagiri 6 participants 21 messages Mar 10 – Mar 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v11 1/1 virtio_net: add page_pool support for buffer allocation

Use page_pool for RX buffer allocation in mergeable and small buffer modes to enable page recycling and avoid repeated page allocator calls. skb_mark_for_recycle() enables page reuse in the network stack.

Big packets mode is unchanged because it uses page->private for linked list chaining of multiple pages per buffer, which conflicts with page_pool's internal use of page->private.

Implement conditional DMA premapping using virtqueue_dma_dev():
- When non-NULL (vhost, virtio-pci): use PP_FLAG_DMA_MAP with page_pool handling DMA mapping, submit via virtqueue_add_inbuf_premapped()
- When NULL (VDUSE, direct physical): page_pool handles allocation only, submit via virtqueue_add_inbuf_ctx()

This preserves the DMA premapping optimization from commit 31f3cd4e5756b ("virtio-net: rq submits premapped per-buffer") while adding page_pool support as a prerequisite for future zero-copy features (devmem TCP, io_uring ZCRX).

Page pools are created in probe and destroyed in remove (not open/close), following existing driver behavior where RX buffers remain in virtqueues across interface state changes.

---
Changes in v11:
- add_recvbuf_small: encode alloc_len and xdp_headroom in ctx via mergeable_len_to_ctx() so receive_small() recovers the actual buflen via mergeable_ctx_to_truesize() (Michael S. Tsirkin)
- receive_small_build_skb, receive_small_xdp: accept buflen parameter instead of recomputing it, to use the actual allocation size
- v10:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v10:
- add_recvbuf_small: use alloc_len to avoid clobbering len; v9 feedback was about truesize under-accounting, not variable naming — misunderstood the comment in v9
- v9:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v9:
- Fix virtnet_skb_append_frag() for XSK callers (Michael S. Tsirkin)
- v8:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v8:
- Remove virtnet_no_page_pool() helper, replace with direct !rq->page_pool checks or inlined conditions (Xuan Zhuo)
- Extract virtnet_rq_submit() helper to consolidate DMA/non-DMA buffer submission in add_recvbuf_small() and add_recvbuf_mergeable()
- Add skb_mark_for_recycle(nskb) for overflow frag_list skbs in virtnet_skb_append_frag() to ensure page_pool pages are returned to the pool instead of freed via put_page()
- Rebase on net-next (kzalloc_objs API)
- v7:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v7:
- Replace virtnet_put_page() helper with direct page_pool_put_page()
calls (Xuan Zhuo)
- Add virtnet_no_page_pool() helper to consolidate big_packets mode check (Michael S. Tsirkin)
- Add DMA sync_for_cpu for subsequent buffers in xdp_linearize_page() when use_page_pool_dma is set (Michael S. Tsirkin)
- Remove unused pp_params.dev assignment in non-DMA path
- Add page pool recreation in virtnet_restore_up() for freeze/restore support (Chris Mason's Review Prompt)
- v6:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v6:
- Drop page_pool_frag_offset_add() helper and switch to page_pool_alloc_va(); page_pool_alloc_netmem() already handles internal fragmentation internally (Jakub Kicinski)
- v5:
https://lore.kernel.org/virtualization/[email protected]/

Benchmark results:

Configuration: pktgen TX -> tap -> vhost-net | virtio-net RX -> XDP_DROP

Small packets (64 bytes, mrg_rxbuf=off):
1Q: 853,493 -> 868,923 pps (+1.8%)
2Q: 1,655,793 -> 1,696,707 pps (+2.5%)
4Q: 3,143,375 -> 3,302,511 pps (+5.1%)
8Q: 6,082,590 -> 6,156,894 pps (+1.2%)

Mergeable RX (64 bytes):
1Q: 766,168 -> 814,493 pps (+6.3%)
2Q: 1,384,871 -> 1,670,639 pps (+20.6%)
4Q: 2,773,081 -> 3,080,574 pps (+11.1%)
8Q: 5,600,615 -> 6,043,891 pps (+7.9%)

Mergeable RX (1500 bytes):
1Q: 741,579 -> 785,442 pps (+5.9%)
2Q: 1,310,043 -> 1,534,554 pps (+17.1%)
4Q: 2,748,700 -> 2,890,582 pps (+5.2%)
8Q: 5,348,589 -> 5,618,664 pps (+5.0%)

 drivers/net/Kconfig      |   1 +
 drivers/net/virtio_net.c | 497 ++++++++++++++++++++-------------------
 2 files changed, 251 insertions(+), 247 deletions(-)
drivers/net/Kconfig +1 -0
index 17108c359216..b2fd90466bab 100644
@@ -452,6 +452,7 @@ config VIRTIO_NET
 	depends on VIRTIO
 	select NET_FAILOVER
 	select DIMLIB
+	select PAGE_POOL
 	help
 	  This is the virtual network driver for virtio.  It can be used with
 	  QEMU based VMMs (like KVM or Xen).  Say Y or M.
drivers/net/virtio_net.c +250 -247

914 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw
Quoted 402 lines · Vishwanath Seshagiri

Use page_pool for RX buffer allocation in mergeable and small buffer modes to enable page recycling and avoid repeated page allocator calls. skb_mark_for_recycle() enables page reuse in the network stack.

Big packets mode is unchanged because it uses page->private for linked list chaining of multiple pages per buffer, which conflicts with page_pool's internal use of page->private.

Implement conditional DMA premapping using virtqueue_dma_dev():
- When non-NULL (vhost, virtio-pci): use PP_FLAG_DMA_MAP with page_pool handling DMA mapping, submit via virtqueue_add_inbuf_premapped()
- When NULL (VDUSE, direct physical): page_pool handles allocation only, submit via virtqueue_add_inbuf_ctx()

This preserves the DMA premapping optimization from commit 31f3cd4e5756b ("virtio-net: rq submits premapped per-buffer") while adding page_pool support as a prerequisite for future zero-copy features (devmem TCP, io_uring ZCRX).

Page pools are created in probe and destroyed in remove (not open/close), following existing driver behavior where RX buffers remain in virtqueues across interface state changes.

---
Changes in v11:
- add_recvbuf_small: encode alloc_len and xdp_headroom in ctx via mergeable_len_to_ctx() so receive_small() recovers the actual buflen via mergeable_ctx_to_truesize() (Michael S. Tsirkin)
- receive_small_build_skb, receive_small_xdp: accept buflen parameter instead of recomputing it, to use the actual allocation size
- v10:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v10:
- add_recvbuf_small: use alloc_len to avoid clobbering len; v9 feedback was about truesize under-accounting, not variable naming — misunderstood the comment in v9
- v9:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v9:
- Fix virtnet_skb_append_frag() for XSK callers (Michael S. Tsirkin)
- v8:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v8:
- Remove virtnet_no_page_pool() helper, replace with direct !rq->page_pool checks or inlined conditions (Xuan Zhuo)
- Extract virtnet_rq_submit() helper to consolidate DMA/non-DMA buffer submission in add_recvbuf_small() and add_recvbuf_mergeable()
- Add skb_mark_for_recycle(nskb) for overflow frag_list skbs in virtnet_skb_append_frag() to ensure page_pool pages are returned to the pool instead of freed via put_page()
- Rebase on net-next (kzalloc_objs API)
- v7:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v7:
- Replace virtnet_put_page() helper with direct page_pool_put_page()
calls (Xuan Zhuo)
- Add virtnet_no_page_pool() helper to consolidate big_packets mode check (Michael S. Tsirkin)
- Add DMA sync_for_cpu for subsequent buffers in xdp_linearize_page() when use_page_pool_dma is set (Michael S. Tsirkin)
- Remove unused pp_params.dev assignment in non-DMA path
- Add page pool recreation in virtnet_restore_up() for freeze/restore support (Chris Mason's Review Prompt)
- v6:
https://lore.kernel.org/virtualization/[email protected]/

Changes in v6:
- Drop page_pool_frag_offset_add() helper and switch to page_pool_alloc_va(); page_pool_alloc_netmem() already handles internal fragmentation internally (Jakub Kicinski)
- v5:
https://lore.kernel.org/virtualization/[email protected]/

Benchmark results:

Configuration: pktgen TX -> tap -> vhost-net | virtio-net RX -> XDP_DROP

Small packets (64 bytes, mrg_rxbuf=off):
1Q: 853,493 -> 868,923 pps (+1.8%)
2Q: 1,655,793 -> 1,696,707 pps (+2.5%)
4Q: 3,143,375 -> 3,302,511 pps (+5.1%)
8Q: 6,082,590 -> 6,156,894 pps (+1.2%)

Mergeable RX (64 bytes):
1Q: 766,168 -> 814,493 pps (+6.3%)
2Q: 1,384,871 -> 1,670,639 pps (+20.6%)
4Q: 2,773,081 -> 3,080,574 pps (+11.1%)
8Q: 5,600,615 -> 6,043,891 pps (+7.9%)

Mergeable RX (1500 bytes):
1Q: 741,579 -> 785,442 pps (+5.9%)
2Q: 1,310,043 -> 1,534,554 pps (+17.1%)
4Q: 2,748,700 -> 2,890,582 pps (+5.2%)
8Q: 5,348,589 -> 5,618,664 pps (+5.0%)

 drivers/net/Kconfig      |   1 +
 drivers/net/virtio_net.c | 497 ++++++++++++++++++++-------------------
 2 files changed, 251 insertions(+), 247 deletions(-)
drivers/net/Kconfig +1 -0
index 17108c359216..b2fd90466bab 100644
@@ -452,6 +452,7 @@ config VIRTIO_NET
        depends on VIRTIO
        select NET_FAILOVER
        select DIMLIB
+       select PAGE_POOL
        help
          This is the virtual network driver for virtio.  It can be used with
          QEMU based VMMs (like KVM or Xen).  Say Y or M.
drivers/net/virtio_net.c +27 -148
index 72d6a9c6a5a2..a85d75a7f539 100644
@@ -26,6 +26,7 @@
 #include <net/netdev_rx_queue.h>
 #include <net/netdev_queues.h>
 #include <net/xdp_sock_drv.h>
+#include <net/page_pool/helpers.h>
 
 static int napi_weight = NAPI_POLL_WEIGHT;
 module_param(napi_weight, int, 0444);
@@ -290,14 +291,6 @@ struct virtnet_interrupt_coalesce {
        u32 max_usecs;
 };
 
-/* The dma information of pages allocated at a time. */
-struct virtnet_rq_dma {
-       dma_addr_t addr;
-       u32 ref;
-       u16 len;
-       u16 need_sync;
-};
-
 /* Internal representation of a send virtqueue */
 struct send_queue {
        /* Virtqueue associated with this send _queue */
@@ -356,8 +349,10 @@ struct receive_queue {
        /* Average packet length for mergeable receive buffers. */
        struct ewma_pkt_len mrg_avg_pkt_len;
 
-       /* Page frag for packet buffer allocation. */
-       struct page_frag alloc_frag;
+       struct page_pool *page_pool;
+
+       /* True if page_pool handles DMA mapping via PP_FLAG_DMA_MAP */
+       bool use_page_pool_dma;
 
        /* RX: fragments + linear part + virtio header */
        struct scatterlist sg[MAX_SKB_FRAGS + 2];
@@ -370,9 +365,6 @@ struct receive_queue {
 
        struct xdp_rxq_info xdp_rxq;
 
-       /* Record the last dma info to free after new pages is allocated. */
-       struct virtnet_rq_dma *last_dma;
-
        struct xsk_buff_pool *xsk_pool;
 
        /* xdp rxq used by xsk */
@@ -521,11 +513,14 @@ static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
                               struct virtnet_rq_stats *stats);
 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
                                 struct sk_buff *skb, u8 flags);
-static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
+static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
+                                              struct sk_buff *head_skb,
                                               struct sk_buff *curr_skb,
                                               struct page *page, void *buf,
                                               int len, int truesize);
 static void virtnet_xsk_completed(struct send_queue *sq, int num);
+static void free_unused_bufs(struct virtnet_info *vi);
+static void virtnet_del_vqs(struct virtnet_info *vi);
 
 enum virtnet_xmit_type {
        VIRTNET_XMIT_TYPE_SKB,
@@ -709,12 +704,10 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
 static void virtnet_rq_free_buf(struct virtnet_info *vi,
                                struct receive_queue *rq, void *buf)
 {
-       if (vi->mergeable_rx_bufs)
-               put_page(virt_to_head_page(buf));
-       else if (vi->big_packets)
+       if (!rq->page_pool)
                give_pages(rq, buf);
        else
-               put_page(virt_to_head_page(buf));
+               page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
 }
 
 static void enable_rx_mode_work(struct virtnet_info *vi)
@@ -876,10 +869,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                skb = virtnet_build_skb(buf, truesize, p - buf, len);
                if (unlikely(!skb))
                        return NULL;
+               /* Big packets mode chains pages via page->private, which is
+                * incompatible with the way page_pool uses page->private.
+                * Currently, big packets mode doesn't use page pools.
+                */
+               if (!rq->page_pool) {
+                       page = (struct page *)page->private;
+                       if (page)
+                               give_pages(rq, page);
+               }
 
-               page = (struct page *)page->private;
-               if (page)
-                       give_pages(rq, page);
                goto ok;
        }
 
@@ -925,133 +924,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
        hdr = skb_vnet_common_hdr(skb);
        memcpy(hdr, hdr_p, hdr_len);
        if (page_to_free)
-               put_page(page_to_free);
+               page_pool_put_page(rq->page_pool, page_to_free, -1, true);
 
        return skb;
 }
 
-static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct page *page = virt_to_head_page(buf);
-       struct virtnet_rq_dma *dma;
-       void *head;
-       int offset;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(page);
-
-       dma = head;
-
-       --dma->ref;
-
-       if (dma->need_sync && len) {
-               offset = buf - (head + sizeof(*dma));
-
-               virtqueue_map_sync_single_range_for_cpu(rq->vq, dma->addr,
-                                                       offset, len,
-                                                       DMA_FROM_DEVICE);
-       }
-
-       if (dma->ref)
-               return;
-
-       virtqueue_unmap_single_attrs(rq->vq, dma->addr, dma->len,
-                                    DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
-       put_page(page);
-}
-
 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
 {
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       void *buf;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
+       BUG_ON(!rq->page_pool);
 
-       buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
-       if (buf)
-               virtnet_rq_unmap(rq, buf, *len);
-
-       return buf;
-}
-
-static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       dma_addr_t addr;
-       u32 offset;
-       void *head;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(rq->alloc_frag.page);
-
-       offset = buf - head;
-
-       dma = head;
-
-       addr = dma->addr - sizeof(*dma) + offset;
-
-       sg_init_table(rq->sg, 1);
-       sg_fill_dma(rq->sg, addr, len);
-}
-
-static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
-{
-       struct page_frag *alloc_frag = &rq->alloc_frag;
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       void *buf, *head;
-       dma_addr_t addr;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(alloc_frag->page);
-
-       dma = head;
-
-       /* new pages */
-       if (!alloc_frag->offset) {
-               if (rq->last_dma) {
-                       /* Now, the new page is allocated, the last dma
-                        * will not be used. So the dma can be unmapped
-                        * if the ref is 0.
-                        */
-                       virtnet_rq_unmap(rq, rq->last_dma, 0);
-                       rq->last_dma = NULL;
-               }
-
-               dma->len = alloc_frag->size - sizeof(*dma);
-
-               addr = virtqueue_map_single_attrs(rq->vq, dma + 1,
-                                                 dma->len, DMA_FROM_DEVICE, 0);
-               if (virtqueue_map_mapping_error(rq->vq, addr))
-                       return NULL;
-
-               dma->addr = addr;
-               dma->need_sync = virtqueue_map_need_sync(rq->vq, addr);
-
-               /* Add a reference to dma to prevent the entire dma from
-                * being released during error handling. This reference
-                * will be freed after the pages are no longer used.
-                */
-               get_page(alloc_frag->page);
-               dma->ref = 1;
-               alloc_frag->offset = sizeof(*dma);
-
-               rq->last_dma = dma;
-       }
-
-       ++dma->ref;
-
-       buf = head + alloc_frag->offset;
-
-       get_page(alloc_frag->page);
-       alloc_frag->offset += size;
-
-       return buf;
+       return virtqueue_get_buf_ctx(rq->vq, len, ctx);
 }
 
 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
@@ -1067,9 +949,6 @@ static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
                return;
        }
 
-       if (!vi->big_packets || vi->mergeable_rx_bufs)
-               virtnet_rq_unmap(rq, buf, 0);
-
        virtnet_rq_free_buf(vi, rq, buf);
 }
 
@@ -1335,7 +1214,7 @@ static int xsk_append_merge_buffer(struct virtnet_info *vi,
 
                truesize = len;
 
-               curr_skb  = virtnet_skb_append_frag(head_skb, curr_skb, page,
+               curr_skb  = virtnet_skb_append_frag(rq, head_skb, curr_skb, page,
                                                    buf, len, truesize);
                if (!curr_skb) {
                        put_page(page);
@@ -1771,7 +1650,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
        return ret;
 }
 
-static void put_xdp_frags(struct xdp_buff *xdp)
+static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
 {
        struct skb_shared_info *shinfo;
        struct page *xdp_page;
@@ -1781,7 +1660,7 @@ static void put_xdp_frags(struct xdp_buff *xdp)
                shinfo = xdp_get_shared_info_from_buff(xdp);
                for (i = 0; i < shinfo->nr_frags; i++) {
                        xdp_page = skb_frag_page(&shinfo->frags[i]);
-                       put_page(xdp_page);
+                       page_pool_put_page(rq->page_pool, xdp_page, -1, true);
                }
        }
 }
@@ -1873,7 +1752,7 @@ static struct page *xdp_linearize_page(struct net_device *dev,
        if (page_off + *len + tailroom > PAGE_SIZE)
                return NULL;
 
-       page = alloc_page(GFP_ATOMIC);
+       page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
        if (!page)
                return NULL;
 

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1780

Quoted 6 lines
drivers/net/virtio_net.c +3 -0
@@ -1896,8 +1775,12 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                p = virt_to_head_page(buf);
                off = buf - page_address(p);
 
+               if (rq->use_page_pool_dma)
+                       page_pool_dma_sync_for_cpu(rq->page_pool, p,
+                                                  off, buflen);

Intresting, I think we need a patch for -stable to sync for cpu as well (and probably the XDP_TX path).

Quoted 39 lines
drivers/net/virtio_net.c +7 -5
+
                if (check_mergeable_len(dev, ctx, buflen)) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                        goto err_buf;
                }
 
@@ -1905,38 +1788,36 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                 * is sending packet larger than the MTU.
                 */
                if ((page_off + buflen + tailroom) > PAGE_SIZE) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                        goto err_buf;
                }
 
                memcpy(page_address(page) + page_off,
                       page_address(p) + off, buflen);
                page_off += buflen;
-               put_page(p);
+               page_pool_put_page(rq->page_pool, p, -1, true);
        }
 
        /* Headroom does not contribute to packet length */
        *len = page_off - XDP_PACKET_HEADROOM;
        return page;
 err_buf:
-       __free_pages(page, 0);
+       page_pool_put_page(rq->page_pool, page, -1, true);
        return NULL;
 }
 
 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
                                               unsigned int xdp_headroom,
                                               void *buf,
-                                              unsigned int len)
+                                              unsigned int len,
+                                              unsigned int buflen)
 {

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1821

Quoted 10 lines
drivers/net/virtio_net.c +0 -3
        unsigned int header_offset;
        unsigned int headroom;
-       unsigned int buflen;
        struct sk_buff *skb;
 
        header_offset = VIRTNET_RX_PAD + xdp_headroom;
        headroom = vi->hdr_len + header_offset;
-       buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
-               SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 

Any reason for remvoing this?

The rest looks fine.

Thanks

#2 lore raw
Quoted 458 lines · Jason Wang
Quoted 402 lines · Vishwanath Seshagiri

Use page_pool for RX buffer allocation in mergeable and small buffer modes to enable page recycling and avoid repeated page allocator calls. skb_mark_for_recycle() enables page reuse in the network stack.

Big packets mode is unchanged because it uses page->private for linked list chaining of multiple pages per buffer, which conflicts with page_pool's internal use of page->private.

Implement conditional DMA premapping using virtqueue_dma_dev():
- When non-NULL (vhost, virtio-pci): use PP_FLAG_DMA_MAP with page_pool handling DMA mapping, submit via virtqueue_add_inbuf_premapped()
- When NULL (VDUSE, direct physical): page_pool handles allocation only, submit via virtqueue_add_inbuf_ctx()

This preserves the DMA premapping optimization from commit 31f3cd4e5756b ("virtio-net: rq submits premapped per-buffer") while adding page_pool support as a prerequisite for future zero-copy features (devmem TCP, io_uring ZCRX).

Page pools are created in probe and destroyed in remove (not open/close), following existing driver behavior where RX buffers remain in virtqueues across interface state changes.

---
Changes in v11:
- add_recvbuf_small: encode alloc_len and xdp_headroom in ctx via mergeable_len_to_ctx() so receive_small() recovers the actual buflen via mergeable_ctx_to_truesize() (Michael S. Tsirkin)
- receive_small_build_skb, receive_small_xdp: accept buflen parameter instead of recomputing it, to use the actual allocation size
- v10:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v10:
- add_recvbuf_small: use alloc_len to avoid clobbering len; v9 feedback was about truesize under-accounting, not variable naming — misunderstood the comment in v9
- v9:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v9:
- Fix virtnet_skb_append_frag() for XSK callers (Michael S. Tsirkin)
- v8:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v8:
- Remove virtnet_no_page_pool() helper, replace with direct !rq->page_pool checks or inlined conditions (Xuan Zhuo)
- Extract virtnet_rq_submit() helper to consolidate DMA/non-DMA buffer submission in add_recvbuf_small() and add_recvbuf_mergeable()
- Add skb_mark_for_recycle(nskb) for overflow frag_list skbs in virtnet_skb_append_frag() to ensure page_pool pages are returned to the pool instead of freed via put_page()
- Rebase on net-next (kzalloc_objs API)
- v7:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v7:
- Replace virtnet_put_page() helper with direct page_pool_put_page()
calls (Xuan Zhuo)
- Add virtnet_no_page_pool() helper to consolidate big_packets mode check (Michael S. Tsirkin)
- Add DMA sync_for_cpu for subsequent buffers in xdp_linearize_page() when use_page_pool_dma is set (Michael S. Tsirkin)
- Remove unused pp_params.dev assignment in non-DMA path
- Add page pool recreation in virtnet_restore_up() for freeze/restore support (Chris Mason's Review Prompt)
- v6:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v6:
- Drop page_pool_frag_offset_add() helper and switch to page_pool_alloc_va(); page_pool_alloc_netmem() already handles internal fragmentation internally (Jakub Kicinski)
- v5:

   https://lore.kernel.org/virtualization/[email protected]/

Benchmark results:

Configuration: pktgen TX -> tap -> vhost-net | virtio-net RX -> XDP_DROP

Small packets (64 bytes, mrg_rxbuf=off):

   1Q:  853,493 -> 868,923 pps  (+1.8%)
   2Q: 1,655,793 -> 1,696,707 pps (+2.5%)
   4Q: 3,143,375 -> 3,302,511 pps (+5.1%)
   8Q: 6,082,590 -> 6,156,894 pps (+1.2%)

Mergeable RX (64 bytes):

   1Q:   766,168 ->   814,493 pps  (+6.3%)
   2Q: 1,384,871 -> 1,670,639 pps (+20.6%)
   4Q: 2,773,081 -> 3,080,574 pps (+11.1%)
   8Q: 5,600,615 -> 6,043,891 pps  (+7.9%)

Mergeable RX (1500 bytes):

   1Q:   741,579 ->   785,442 pps  (+5.9%)
   2Q: 1,310,043 -> 1,534,554 pps (+17.1%)
   4Q: 2,748,700 -> 2,890,582 pps  (+5.2%)
   8Q: 5,348,589 -> 5,618,664 pps  (+5.0%)
  drivers/net/Kconfig      |   1 +
  drivers/net/virtio_net.c | 497 ++++++++++++++++++++-------------------
  2 files changed, 251 insertions(+), 247 deletions(-)
drivers/net/Kconfig +1 -0
index 17108c359216..b2fd90466bab 100644
@@ -452,6 +452,7 @@ config VIRTIO_NET
         depends on VIRTIO
         select NET_FAILOVER
         select DIMLIB
+       select PAGE_POOL
         help
           This is the virtual network driver for virtio.  It can be used with
           QEMU based VMMs (like KVM or Xen).  Say Y or M.
drivers/net/virtio_net.c +27 -148
index 72d6a9c6a5a2..a85d75a7f539 100644
@@ -26,6 +26,7 @@
  #include <net/netdev_rx_queue.h>
  #include <net/netdev_queues.h>
  #include <net/xdp_sock_drv.h>
+#include <net/page_pool/helpers.h>
 
  static int napi_weight = NAPI_POLL_WEIGHT;
  module_param(napi_weight, int, 0444);
@@ -290,14 +291,6 @@ struct virtnet_interrupt_coalesce {
         u32 max_usecs;
  };
 
-/* The dma information of pages allocated at a time. */
-struct virtnet_rq_dma {
-       dma_addr_t addr;
-       u32 ref;
-       u16 len;
-       u16 need_sync;
-};
-
  /* Internal representation of a send virtqueue */
  struct send_queue {
         /* Virtqueue associated with this send _queue */
@@ -356,8 +349,10 @@ struct receive_queue {
         /* Average packet length for mergeable receive buffers. */
         struct ewma_pkt_len mrg_avg_pkt_len;
 
-       /* Page frag for packet buffer allocation. */
-       struct page_frag alloc_frag;
+       struct page_pool *page_pool;
+
+       /* True if page_pool handles DMA mapping via PP_FLAG_DMA_MAP */
+       bool use_page_pool_dma;
 
         /* RX: fragments + linear part + virtio header */
         struct scatterlist sg[MAX_SKB_FRAGS + 2];
@@ -370,9 +365,6 @@ struct receive_queue {
 
         struct xdp_rxq_info xdp_rxq;
 
-       /* Record the last dma info to free after new pages is allocated. */
-       struct virtnet_rq_dma *last_dma;
-
         struct xsk_buff_pool *xsk_pool;
 
         /* xdp rxq used by xsk */
@@ -521,11 +513,14 @@ static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
                                struct virtnet_rq_stats *stats);
  static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
                                  struct sk_buff *skb, u8 flags);
-static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
+static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
+                                              struct sk_buff *head_skb,
                                                struct sk_buff *curr_skb,
                                                struct page *page, void *buf,
                                                int len, int truesize);
  static void virtnet_xsk_completed(struct send_queue *sq, int num);
+static void free_unused_bufs(struct virtnet_info *vi);
+static void virtnet_del_vqs(struct virtnet_info *vi);
 
  enum virtnet_xmit_type {
         VIRTNET_XMIT_TYPE_SKB,
@@ -709,12 +704,10 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  static void virtnet_rq_free_buf(struct virtnet_info *vi,
                                 struct receive_queue *rq, void *buf)
  {
-       if (vi->mergeable_rx_bufs)
-               put_page(virt_to_head_page(buf));
-       else if (vi->big_packets)
+       if (!rq->page_pool)
                 give_pages(rq, buf);
         else
-               put_page(virt_to_head_page(buf));
+               page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
  }
 
  static void enable_rx_mode_work(struct virtnet_info *vi)
@@ -876,10 +869,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                 skb = virtnet_build_skb(buf, truesize, p - buf, len);
                 if (unlikely(!skb))
                         return NULL;
+               /* Big packets mode chains pages via page->private, which is
+                * incompatible with the way page_pool uses page->private.
+                * Currently, big packets mode doesn't use page pools.
+                */
+               if (!rq->page_pool) {
+                       page = (struct page *)page->private;
+                       if (page)
+                               give_pages(rq, page);
+               }
 
-               page = (struct page *)page->private;
-               if (page)
-                       give_pages(rq, page);
                 goto ok;
         }
 
@@ -925,133 +924,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
         hdr = skb_vnet_common_hdr(skb);
         memcpy(hdr, hdr_p, hdr_len);
         if (page_to_free)
-               put_page(page_to_free);
+               page_pool_put_page(rq->page_pool, page_to_free, -1, true);
 
         return skb;
  }
 
-static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct page *page = virt_to_head_page(buf);
-       struct virtnet_rq_dma *dma;
-       void *head;
-       int offset;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(page);
-
-       dma = head;
-
-       --dma->ref;
-
-       if (dma->need_sync && len) {
-               offset = buf - (head + sizeof(*dma));
-
-               virtqueue_map_sync_single_range_for_cpu(rq->vq, dma->addr,
-                                                       offset, len,
-                                                       DMA_FROM_DEVICE);
-       }
-
-       if (dma->ref)
-               return;
-
-       virtqueue_unmap_single_attrs(rq->vq, dma->addr, dma->len,
-                                    DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
-       put_page(page);
-}
-
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       void *buf;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
+       BUG_ON(!rq->page_pool);
 
-       buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
-       if (buf)
-               virtnet_rq_unmap(rq, buf, *len);
-
-       return buf;
-}
-
-static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       dma_addr_t addr;
-       u32 offset;
-       void *head;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(rq->alloc_frag.page);
-
-       offset = buf - head;
-
-       dma = head;
-
-       addr = dma->addr - sizeof(*dma) + offset;
-
-       sg_init_table(rq->sg, 1);
-       sg_fill_dma(rq->sg, addr, len);
-}
-
-static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
-{
-       struct page_frag *alloc_frag = &rq->alloc_frag;
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       void *buf, *head;
-       dma_addr_t addr;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(alloc_frag->page);
-
-       dma = head;
-
-       /* new pages */
-       if (!alloc_frag->offset) {
-               if (rq->last_dma) {
-                       /* Now, the new page is allocated, the last dma
-                        * will not be used. So the dma can be unmapped
-                        * if the ref is 0.
-                        */
-                       virtnet_rq_unmap(rq, rq->last_dma, 0);
-                       rq->last_dma = NULL;
-               }
-
-               dma->len = alloc_frag->size - sizeof(*dma);
-
-               addr = virtqueue_map_single_attrs(rq->vq, dma + 1,
-                                                 dma->len, DMA_FROM_DEVICE, 0);
-               if (virtqueue_map_mapping_error(rq->vq, addr))
-                       return NULL;
-
-               dma->addr = addr;
-               dma->need_sync = virtqueue_map_need_sync(rq->vq, addr);
-
-               /* Add a reference to dma to prevent the entire dma from
-                * being released during error handling. This reference
-                * will be freed after the pages are no longer used.
-                */
-               get_page(alloc_frag->page);
-               dma->ref = 1;
-               alloc_frag->offset = sizeof(*dma);
-
-               rq->last_dma = dma;
-       }
-
-       ++dma->ref;
-
-       buf = head + alloc_frag->offset;
-
-       get_page(alloc_frag->page);
-       alloc_frag->offset += size;
-
-       return buf;
+       return virtqueue_get_buf_ctx(rq->vq, len, ctx);
  }
 
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
@@ -1067,9 +949,6 @@ static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
                 return;
         }
 
-       if (!vi->big_packets || vi->mergeable_rx_bufs)
-               virtnet_rq_unmap(rq, buf, 0);
-
         virtnet_rq_free_buf(vi, rq, buf);
  }
 
@@ -1335,7 +1214,7 @@ static int xsk_append_merge_buffer(struct virtnet_info *vi,
 
                 truesize = len;
 
-               curr_skb  = virtnet_skb_append_frag(head_skb, curr_skb, page,
+               curr_skb  = virtnet_skb_append_frag(rq, head_skb, curr_skb, page,
                                                     buf, len, truesize);
                 if (!curr_skb) {
                         put_page(page);
@@ -1771,7 +1650,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
         return ret;
  }
 
-static void put_xdp_frags(struct xdp_buff *xdp)
+static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
  {
         struct skb_shared_info *shinfo;
         struct page *xdp_page;
@@ -1781,7 +1660,7 @@ static void put_xdp_frags(struct xdp_buff *xdp)
                 shinfo = xdp_get_shared_info_from_buff(xdp);
                 for (i = 0; i < shinfo->nr_frags; i++) {
                         xdp_page = skb_frag_page(&shinfo->frags[i]);
-                       put_page(xdp_page);
+                       page_pool_put_page(rq->page_pool, xdp_page, -1, true);
                 }
         }
  }
@@ -1873,7 +1752,7 @@ static struct page *xdp_linearize_page(struct net_device *dev,
         if (page_off + *len + tailroom > PAGE_SIZE)
                 return NULL;
 
-       page = alloc_page(GFP_ATOMIC);
+       page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
         if (!page)
                 return NULL;
 

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1780

Quoted 6 lines
drivers/net/virtio_net.c +3 -0
@@ -1896,8 +1775,12 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                 p = virt_to_head_page(buf);
                 off = buf - page_address(p);
 
+               if (rq->use_page_pool_dma)
+                       page_pool_dma_sync_for_cpu(rq->page_pool, p,
+                                                  off, buflen);

Intresting, I think we need a patch for -stable to sync for cpu as well (and probably the XDP_TX path).

Quoted 44 lines
drivers/net/virtio_net.c +7 -6
+
                 if (check_mergeable_len(dev, ctx, buflen)) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }
 
@@ -1905,38 +1788,36 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                  * is sending packet larger than the MTU.
                  */
                 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }
 
                 memcpy(page_address(page) + page_off,
                        page_address(p) + off, buflen);
                 page_off += buflen;
-               put_page(p);
+               page_pool_put_page(rq->page_pool, p, -1, true);
         }
 
         /* Headroom does not contribute to packet length */
         *len = page_off - XDP_PACKET_HEADROOM;
         return page;
  err_buf:
-       __free_pages(page, 0);
+       page_pool_put_page(rq->page_pool, page, -1, true);
         return NULL;
  }
 
  static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
                                                unsigned int xdp_headroom,
                                                void *buf,
-                                              unsigned int len)
+                                              unsigned int len,
+                                              unsigned int buflen)
  {
         unsigned int header_offset;
         unsigned int headroom;
-       unsigned int buflen;
         struct sk_buff *skb;
 
Quoted 7 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1821

Quoted 5 lines
drivers/net/virtio_net.c +0 -2
         header_offset = VIRTNET_RX_PAD + xdp_headroom;
         headroom = vi->hdr_len + header_offset;
-       buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
-               SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 

Any reason for remvoing this?

page_pool_alloc_va() can return a larger allocation than requested as it appends remaining fragment space to avoid truesize underestimation (comment in page_pool_alloc_netmem() in helpers.h). The old hardcoded computation would always produce the requested ~512 bytes, ignoring any extra space page_pool gave us, so build_skb() would set skb->truesize too low. To pass the real size through: add_recvbuf_small() encodes alloc_len and xdp_headroom into ctx via mergeable_len_to_ctx(alloc_len, xdp_headroom). On the receive side, receive_small() extracts it with mergeable_ctx_to_truesize(ctx) and passes it as the buflen parameter to receive_small_build_skb().

Quoted 5 lines

The rest looks fine.

Thanks

#4 lore raw
In reply to Jason Wang
Quoted 27 lines · Jason Wang
Quoted 27 lines
drivers/net/virtio_net.c +3 -3
-static void put_xdp_frags(struct xdp_buff *xdp)
+static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
  {
         struct skb_shared_info *shinfo;
         struct page *xdp_page;
@@ -1781,7 +1660,7 @@ static void put_xdp_frags(struct xdp_buff *xdp)
                 shinfo = xdp_get_shared_info_from_buff(xdp);
                 for (i = 0; i < shinfo->nr_frags; i++) {
                         xdp_page = skb_frag_page(&shinfo->frags[i]);
-                       put_page(xdp_page);
+                       page_pool_put_page(rq->page_pool, xdp_page, -1, true);
                 }
         }
  }
@@ -1873,7 +1752,7 @@ static struct page *xdp_linearize_page(struct net_device *dev,
         if (page_off + *len + tailroom > PAGE_SIZE)
                 return NULL;
 
-       page = alloc_page(GFP_ATOMIC);
+       page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
         if (!page)
                 return NULL;
 
@@ -1896,8 +1775,12 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                 p = virt_to_head_page(buf);
                 off = buf - page_address(p);
 
Quoted 6 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1780

Quoted 3 lines
drivers/net/virtio_net.c +3 -0
+               if (rq->use_page_pool_dma)
+                       page_pool_dma_sync_for_cpu(rq->page_pool, p,
+                                                  off, buflen);

Intresting, I think we need a patch for -stable to sync for cpu as well (and probably the XDP_TX path).

In the old code, I believed the sync was handled implicitly inside virtnet_rq_get_buf() -> virtnet_rq_unmap() before data was accessed. Could you point me at the specific path you're concerned about for -stable? I want to make sure I understand the issue correctly before sending a fix.

Quoted 8 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1785

Quoted 8 lines
drivers/net/virtio_net.c +2 -1
+
                 if (check_mergeable_len(dev, ctx, buflen)) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }

Sorry for missing that comment in the earlier email

#5 lore raw
Quoted 38 lines · Vishwanath Seshagiri
Quoted 27 lines · Jason Wang
Quoted 27 lines
drivers/net/virtio_net.c +3 -3
-static void put_xdp_frags(struct xdp_buff *xdp)
+static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
  {
         struct skb_shared_info *shinfo;
         struct page *xdp_page;
@@ -1781,7 +1660,7 @@ static void put_xdp_frags(struct xdp_buff *xdp)
                 shinfo = xdp_get_shared_info_from_buff(xdp);
                 for (i = 0; i < shinfo->nr_frags; i++) {
                         xdp_page = skb_frag_page(&shinfo->frags[i]);
-                       put_page(xdp_page);
+                       page_pool_put_page(rq->page_pool, xdp_page, -1, true);
                 }
         }
  }
@@ -1873,7 +1752,7 @@ static struct page *xdp_linearize_page(struct net_device *dev,
         if (page_off + *len + tailroom > PAGE_SIZE)
                 return NULL;
 
-       page = alloc_page(GFP_ATOMIC);
+       page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
         if (!page)
                 return NULL;
 
@@ -1896,8 +1775,12 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                 p = virt_to_head_page(buf);
                 off = buf - page_address(p);
 
Quoted 6 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1780

Quoted 3 lines
drivers/net/virtio_net.c +3 -0
+               if (rq->use_page_pool_dma)
+                       page_pool_dma_sync_for_cpu(rq->page_pool, p,
+                                                  off, buflen);

Intresting, I think we need a patch for -stable to sync for cpu as well (and probably the XDP_TX path).

Quoted 5 lines

In the old code, I believed the sync was handled implicitly inside virtnet_rq_get_buf() -> virtnet_rq_unmap() before data was accessed. Could you point me at the specific path you're concerned about for -stable? I want to make sure I understand the issue correctly before sending a fix.

You're right; the code seems fine.

(Note that, when big_packets is true but small is used, we will go virtqueue_get_buf(), so the unamp is done via deatch_buf().

Quoted 11 lines
Quoted 9 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1785

Quoted 9 lines
drivers/net/virtio_net.c +2 -1
+
                 if (check_mergeable_len(dev, ctx, buflen)) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }

Sorry for missing that comment in the earlier email

No worries.

Thanks

Quoted 1 line
Acked-by
#3 lore raw
In reply to Vishwanath Seshagiri
Quoted 468 lines · Vishwanath Seshagiri
Quoted 458 lines · Jason Wang
Quoted 402 lines · Vishwanath Seshagiri

Use page_pool for RX buffer allocation in mergeable and small buffer modes to enable page recycling and avoid repeated page allocator calls. skb_mark_for_recycle() enables page reuse in the network stack.

Big packets mode is unchanged because it uses page->private for linked list chaining of multiple pages per buffer, which conflicts with page_pool's internal use of page->private.

Implement conditional DMA premapping using virtqueue_dma_dev():
- When non-NULL (vhost, virtio-pci): use PP_FLAG_DMA_MAP with page_pool handling DMA mapping, submit via virtqueue_add_inbuf_premapped()
- When NULL (VDUSE, direct physical): page_pool handles allocation only, submit via virtqueue_add_inbuf_ctx()

This preserves the DMA premapping optimization from commit 31f3cd4e5756b ("virtio-net: rq submits premapped per-buffer") while adding page_pool support as a prerequisite for future zero-copy features (devmem TCP, io_uring ZCRX).

Page pools are created in probe and destroyed in remove (not open/close), following existing driver behavior where RX buffers remain in virtqueues across interface state changes.

---
Changes in v11:
- add_recvbuf_small: encode alloc_len and xdp_headroom in ctx via mergeable_len_to_ctx() so receive_small() recovers the actual buflen via mergeable_ctx_to_truesize() (Michael S. Tsirkin)
- receive_small_build_skb, receive_small_xdp: accept buflen parameter instead of recomputing it, to use the actual allocation size
- v10:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v10:
- add_recvbuf_small: use alloc_len to avoid clobbering len; v9 feedback was about truesize under-accounting, not variable naming — misunderstood the comment in v9
- v9:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v9:
- Fix virtnet_skb_append_frag() for XSK callers (Michael S. Tsirkin)
- v8:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v8:
- Remove virtnet_no_page_pool() helper, replace with direct !rq->page_pool checks or inlined conditions (Xuan Zhuo)
- Extract virtnet_rq_submit() helper to consolidate DMA/non-DMA buffer submission in add_recvbuf_small() and add_recvbuf_mergeable()
- Add skb_mark_for_recycle(nskb) for overflow frag_list skbs in virtnet_skb_append_frag() to ensure page_pool pages are returned to the pool instead of freed via put_page()
- Rebase on net-next (kzalloc_objs API)
- v7:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v7:
- Replace virtnet_put_page() helper with direct page_pool_put_page()
calls (Xuan Zhuo)
- Add virtnet_no_page_pool() helper to consolidate big_packets mode check (Michael S. Tsirkin)
- Add DMA sync_for_cpu for subsequent buffers in xdp_linearize_page() when use_page_pool_dma is set (Michael S. Tsirkin)
- Remove unused pp_params.dev assignment in non-DMA path
- Add page pool recreation in virtnet_restore_up() for freeze/restore support (Chris Mason's Review Prompt)
- v6:

   https://lore.kernel.org/virtualization/[email protected]/

Changes in v6:
- Drop page_pool_frag_offset_add() helper and switch to page_pool_alloc_va(); page_pool_alloc_netmem() already handles internal fragmentation internally (Jakub Kicinski)
- v5:

   https://lore.kernel.org/virtualization/[email protected]/

Benchmark results:

Configuration: pktgen TX -> tap -> vhost-net | virtio-net RX -> XDP_DROP

Small packets (64 bytes, mrg_rxbuf=off):

   1Q:  853,493 -> 868,923 pps  (+1.8%)
   2Q: 1,655,793 -> 1,696,707 pps (+2.5%)
   4Q: 3,143,375 -> 3,302,511 pps (+5.1%)
   8Q: 6,082,590 -> 6,156,894 pps (+1.2%)

Mergeable RX (64 bytes):

   1Q:   766,168 ->   814,493 pps  (+6.3%)
   2Q: 1,384,871 -> 1,670,639 pps (+20.6%)
   4Q: 2,773,081 -> 3,080,574 pps (+11.1%)
   8Q: 5,600,615 -> 6,043,891 pps  (+7.9%)

Mergeable RX (1500 bytes):

   1Q:   741,579 ->   785,442 pps  (+5.9%)
   2Q: 1,310,043 -> 1,534,554 pps (+17.1%)
   4Q: 2,748,700 -> 2,890,582 pps  (+5.2%)
   8Q: 5,348,589 -> 5,618,664 pps  (+5.0%)
  drivers/net/Kconfig      |   1 +
  drivers/net/virtio_net.c | 497 ++++++++++++++++++++-------------------
  2 files changed, 251 insertions(+), 247 deletions(-)
drivers/net/Kconfig +1 -0
index 17108c359216..b2fd90466bab 100644
@@ -452,6 +452,7 @@ config VIRTIO_NET
         depends on VIRTIO
         select NET_FAILOVER
         select DIMLIB
+       select PAGE_POOL
         help
           This is the virtual network driver for virtio.  It can be used with
           QEMU based VMMs (like KVM or Xen).  Say Y or M.
drivers/net/virtio_net.c +27 -148
index 72d6a9c6a5a2..a85d75a7f539 100644
@@ -26,6 +26,7 @@
  #include <net/netdev_rx_queue.h>
  #include <net/netdev_queues.h>
  #include <net/xdp_sock_drv.h>
+#include <net/page_pool/helpers.h>
 
  static int napi_weight = NAPI_POLL_WEIGHT;
  module_param(napi_weight, int, 0444);
@@ -290,14 +291,6 @@ struct virtnet_interrupt_coalesce {
         u32 max_usecs;
  };
 
-/* The dma information of pages allocated at a time. */
-struct virtnet_rq_dma {
-       dma_addr_t addr;
-       u32 ref;
-       u16 len;
-       u16 need_sync;
-};
-
  /* Internal representation of a send virtqueue */
  struct send_queue {
         /* Virtqueue associated with this send _queue */
@@ -356,8 +349,10 @@ struct receive_queue {
         /* Average packet length for mergeable receive buffers. */
         struct ewma_pkt_len mrg_avg_pkt_len;
 
-       /* Page frag for packet buffer allocation. */
-       struct page_frag alloc_frag;
+       struct page_pool *page_pool;
+
+       /* True if page_pool handles DMA mapping via PP_FLAG_DMA_MAP */
+       bool use_page_pool_dma;
 
         /* RX: fragments + linear part + virtio header */
         struct scatterlist sg[MAX_SKB_FRAGS + 2];
@@ -370,9 +365,6 @@ struct receive_queue {
 
         struct xdp_rxq_info xdp_rxq;
 
-       /* Record the last dma info to free after new pages is allocated. */
-       struct virtnet_rq_dma *last_dma;
-
         struct xsk_buff_pool *xsk_pool;
 
         /* xdp rxq used by xsk */
@@ -521,11 +513,14 @@ static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
                                struct virtnet_rq_stats *stats);
  static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
                                  struct sk_buff *skb, u8 flags);
-static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
+static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
+                                              struct sk_buff *head_skb,
                                                struct sk_buff *curr_skb,
                                                struct page *page, void *buf,
                                                int len, int truesize);
  static void virtnet_xsk_completed(struct send_queue *sq, int num);
+static void free_unused_bufs(struct virtnet_info *vi);
+static void virtnet_del_vqs(struct virtnet_info *vi);
 
  enum virtnet_xmit_type {
         VIRTNET_XMIT_TYPE_SKB,
@@ -709,12 +704,10 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
  static void virtnet_rq_free_buf(struct virtnet_info *vi,
                                 struct receive_queue *rq, void *buf)
  {
-       if (vi->mergeable_rx_bufs)
-               put_page(virt_to_head_page(buf));
-       else if (vi->big_packets)
+       if (!rq->page_pool)
                 give_pages(rq, buf);
         else
-               put_page(virt_to_head_page(buf));
+               page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
  }
 
  static void enable_rx_mode_work(struct virtnet_info *vi)
@@ -876,10 +869,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                 skb = virtnet_build_skb(buf, truesize, p - buf, len);
                 if (unlikely(!skb))
                         return NULL;
+               /* Big packets mode chains pages via page->private, which is
+                * incompatible with the way page_pool uses page->private.
+                * Currently, big packets mode doesn't use page pools.
+                */
+               if (!rq->page_pool) {
+                       page = (struct page *)page->private;
+                       if (page)
+                               give_pages(rq, page);
+               }
 
-               page = (struct page *)page->private;
-               if (page)
-                       give_pages(rq, page);
                 goto ok;
         }
 
@@ -925,133 +924,16 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
         hdr = skb_vnet_common_hdr(skb);
         memcpy(hdr, hdr_p, hdr_len);
         if (page_to_free)
-               put_page(page_to_free);
+               page_pool_put_page(rq->page_pool, page_to_free, -1, true);
 
         return skb;
  }
 
-static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct page *page = virt_to_head_page(buf);
-       struct virtnet_rq_dma *dma;
-       void *head;
-       int offset;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(page);
-
-       dma = head;
-
-       --dma->ref;
-
-       if (dma->need_sync && len) {
-               offset = buf - (head + sizeof(*dma));
-
-               virtqueue_map_sync_single_range_for_cpu(rq->vq, dma->addr,
-                                                       offset, len,
-                                                       DMA_FROM_DEVICE);
-       }
-
-       if (dma->ref)
-               return;
-
-       virtqueue_unmap_single_attrs(rq->vq, dma->addr, dma->len,
-                                    DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
-       put_page(page);
-}
-
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       void *buf;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
+       BUG_ON(!rq->page_pool);
 
-       buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
-       if (buf)
-               virtnet_rq_unmap(rq, buf, *len);
-
-       return buf;
-}
-
-static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
-{
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       dma_addr_t addr;
-       u32 offset;
-       void *head;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(rq->alloc_frag.page);
-
-       offset = buf - head;
-
-       dma = head;
-
-       addr = dma->addr - sizeof(*dma) + offset;
-
-       sg_init_table(rq->sg, 1);
-       sg_fill_dma(rq->sg, addr, len);
-}
-
-static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
-{
-       struct page_frag *alloc_frag = &rq->alloc_frag;
-       struct virtnet_info *vi = rq->vq->vdev->priv;
-       struct virtnet_rq_dma *dma;
-       void *buf, *head;
-       dma_addr_t addr;
-
-       BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
-
-       head = page_address(alloc_frag->page);
-
-       dma = head;
-
-       /* new pages */
-       if (!alloc_frag->offset) {
-               if (rq->last_dma) {
-                       /* Now, the new page is allocated, the last dma
-                        * will not be used. So the dma can be unmapped
-                        * if the ref is 0.
-                        */
-                       virtnet_rq_unmap(rq, rq->last_dma, 0);
-                       rq->last_dma = NULL;
-               }
-
-               dma->len = alloc_frag->size - sizeof(*dma);
-
-               addr = virtqueue_map_single_attrs(rq->vq, dma + 1,
-                                                 dma->len, DMA_FROM_DEVICE, 0);
-               if (virtqueue_map_mapping_error(rq->vq, addr))
-                       return NULL;
-
-               dma->addr = addr;
-               dma->need_sync = virtqueue_map_need_sync(rq->vq, addr);
-
-               /* Add a reference to dma to prevent the entire dma from
-                * being released during error handling. This reference
-                * will be freed after the pages are no longer used.
-                */
-               get_page(alloc_frag->page);
-               dma->ref = 1;
-               alloc_frag->offset = sizeof(*dma);
-
-               rq->last_dma = dma;
-       }
-
-       ++dma->ref;
-
-       buf = head + alloc_frag->offset;
-
-       get_page(alloc_frag->page);
-       alloc_frag->offset += size;
-
-       return buf;
+       return virtqueue_get_buf_ctx(rq->vq, len, ctx);
  }
 
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
@@ -1067,9 +949,6 @@ static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
                 return;
         }
 
-       if (!vi->big_packets || vi->mergeable_rx_bufs)
-               virtnet_rq_unmap(rq, buf, 0);
-
         virtnet_rq_free_buf(vi, rq, buf);
  }
 
@@ -1335,7 +1214,7 @@ static int xsk_append_merge_buffer(struct virtnet_info *vi,
 
                 truesize = len;
 
-               curr_skb  = virtnet_skb_append_frag(head_skb, curr_skb, page,
+               curr_skb  = virtnet_skb_append_frag(rq, head_skb, curr_skb, page,
                                                     buf, len, truesize);
                 if (!curr_skb) {
                         put_page(page);
@@ -1771,7 +1650,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
         return ret;
  }
 
-static void put_xdp_frags(struct xdp_buff *xdp)
+static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
  {
         struct skb_shared_info *shinfo;
         struct page *xdp_page;
@@ -1781,7 +1660,7 @@ static void put_xdp_frags(struct xdp_buff *xdp)
                 shinfo = xdp_get_shared_info_from_buff(xdp);
                 for (i = 0; i < shinfo->nr_frags; i++) {
                         xdp_page = skb_frag_page(&shinfo->frags[i]);
-                       put_page(xdp_page);
+                       page_pool_put_page(rq->page_pool, xdp_page, -1, true);
                 }
         }
  }
@@ -1873,7 +1752,7 @@ static struct page *xdp_linearize_page(struct net_device *dev,
         if (page_off + *len + tailroom > PAGE_SIZE)
                 return NULL;
 
-       page = alloc_page(GFP_ATOMIC);
+       page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
         if (!page)
                 return NULL;
 

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1780

Quoted 6 lines
drivers/net/virtio_net.c +3 -0
@@ -1896,8 +1775,12 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                 p = virt_to_head_page(buf);
                 off = buf - page_address(p);
 
+               if (rq->use_page_pool_dma)
+                       page_pool_dma_sync_for_cpu(rq->page_pool, p,
+                                                  off, buflen);

Intresting, I think we need a patch for -stable to sync for cpu as well (and probably the XDP_TX path).

Quoted 44 lines
drivers/net/virtio_net.c +7 -6
+
                 if (check_mergeable_len(dev, ctx, buflen)) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }
 
@@ -1905,38 +1788,36 @@ static struct page *xdp_linearize_page(struct net_device *dev,
                  * is sending packet larger than the MTU.
                  */
                 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
-                       put_page(p);
+                       page_pool_put_page(rq->page_pool, p, -1, true);
                         goto err_buf;
                 }
 
                 memcpy(page_address(page) + page_off,
                        page_address(p) + off, buflen);
                 page_off += buflen;
-               put_page(p);
+               page_pool_put_page(rq->page_pool, p, -1, true);
         }
 
         /* Headroom does not contribute to packet length */
         *len = page_off - XDP_PACKET_HEADROOM;
         return page;
  err_buf:
-       __free_pages(page, 0);
+       page_pool_put_page(rq->page_pool, page, -1, true);
         return NULL;
  }
 
  static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
                                                unsigned int xdp_headroom,
                                                void *buf,
-                                              unsigned int len)
+                                              unsigned int len,
+                                              unsigned int buflen)
  {
         unsigned int header_offset;
         unsigned int headroom;
-       unsigned int buflen;
         struct sk_buff *skb;
 
Quoted 7 lines

drivers/net/virtio_net.c static struct page *xdp_linearize_page(struct net_device *dev, · line 1821

Quoted 5 lines
drivers/net/virtio_net.c +0 -2
         header_offset = VIRTNET_RX_PAD + xdp_headroom;
         headroom = vi->hdr_len + header_offset;
-       buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
-               SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 

Any reason for remvoing this?

Quoted 11 lines

page_pool_alloc_va() can return a larger allocation than requested as it appends remaining fragment space to avoid truesize underestimation (comment in page_pool_alloc_netmem() in helpers.h). The old hardcoded computation would always produce the requested ~512 bytes, ignoring any extra space page_pool gave us, so build_skb() would set skb->truesize too low. To pass the real size through: add_recvbuf_small() encodes alloc_len and xdp_headroom into ctx via mergeable_len_to_ctx(alloc_len, xdp_headroom). On the receive side, receive_small() extracts it with mergeable_ctx_to_truesize(ctx) and passes it as the buflen parameter to receive_small_build_skb().

Right.

So

Thanks

#6 lore raw
In reply to Vishwanath Seshagiri

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5869

Quoted 9 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -5857,7 +5863,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
 	/* In big_packets mode, xdp cannot work, so there is no need to
 	 * initialize xsk of rq.
 	 */
-	if (vi->big_packets && !vi->mergeable_rx_bufs)
+	if (!vi->rq[qid].page_pool)
 		return -ENOENT;
 
 	if (qid >= vi->curr_queue_pairs)

It seems that a qid that exceeds curr_queue_pairs would previously get -EINVAL and now gets -ENOENT. Maybe reorder the checks:

        if (qid >= vi->curr_queue_pairs)
                return -EINVAL;

        /* In big_packets mode, xdp cannot work, so there is no need to
         * initialize xsk of rq.
         */ 
       if (!vi->rq[qid].page_pool)
               return -ENOENT;

Alternatively I think we can completely drop this chunk: we already seem to init page_pull at all times except for big packets mode, so the current code is fine I think.

Signature · 1 line
MST
#7 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -5857,7 +5863,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
 	/* In big_packets mode, xdp cannot work, so there is no need to
 	 * initialize xsk of rq.
 	 */
-	if (vi->big_packets && !vi->mergeable_rx_bufs)
+	if (!vi->rq[qid].page_pool)
 		return -ENOENT;
 
Quoted 6 lines

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5869

Quoted 1 line
drivers/net/virtio_net.c +0 -0
 	if (qid >= vi->curr_queue_pairs)

It seems that a qid that exceeds curr_queue_pairs would previously get -EINVAL and now gets -ENOENT.

Or maybe this if (qid >= vi->curr_queue_pairs) is dead code? I looked at it some more and I can't find a path where this triggers.

Quoted 19 lines

Maybe reorder the checks:

        if (qid >= vi->curr_queue_pairs)
                return -EINVAL;

        /* In big_packets mode, xdp cannot work, so there is no need to
         * initialize xsk of rq.
         */ 
       if (!vi->rq[qid].page_pool)
               return -ENOENT;

Alternatively I think we can completely drop this chunk: we already seem to init page_pull at all times except for big packets mode, so the current code is fine I think.

Signature · 1 line
MST
#8 lore raw
Quoted 17 lines · Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -5857,7 +5863,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
  	/* In big_packets mode, xdp cannot work, so there is no need to
  	 * initialize xsk of rq.
  	 */
-	if (vi->big_packets && !vi->mergeable_rx_bufs)
+	if (!vi->rq[qid].page_pool)
  		return -ENOENT;
 
Quoted 6 lines

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5869

Quoted 1 line
drivers/net/virtio_net.c +0 -0
  	if (qid >= vi->curr_queue_pairs)

It seems that a qid that exceeds curr_queue_pairs would previously get -EINVAL and now gets -ENOENT.

Quoted 19 lines

Or maybe this if (qid >= vi->curr_queue_pairs) is dead code? I looked at it some more and I can't find a path where this triggers.

Quoted 15 lines

Maybe reorder the checks:

         if (qid >= vi->curr_queue_pairs)
                 return -EINVAL;

         /* In big_packets mode, xdp cannot work, so there is no need to
          * initialize xsk of rq.
          */
        if (!vi->rq[qid].page_pool)
                return -ENOENT;

Alternatively I think we can completely drop this chunk: we already seem to init page_pull at all times except for big packets mode, so the current code is fine I think.

Yes, I agree qid >= curr_queue_pairs appears to be dead code. xsk_reg_pool_at_qid() in the XSK core already validates queue_id < max(real_num_rx_queues, real_num_tx_queues) before ndo_bpf is called, and real_num_rx_queues == curr_queue_pairs is an invariant in virtio_net. Both paths hold rtnl_lock so no race is possible. That said, I'll adopt your suggested reorder for v12 to ensure vi->rq[qid] isn't accessed before a bounds check, even if the check is currently redundant.

Quoted 5 lines
Quoted 4 lines
Signature · 2 lines
-- 
MST
maintainer Acked-by
#9 lore raw
Quoted 37 lines · Vishwanath Seshagiri
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Michael S. Tsirkin

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5868

Quoted 8 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -5857,7 +5863,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
  	/* In big_packets mode, xdp cannot work, so there is no need to
  	 * initialize xsk of rq.
  	 */
-	if (vi->big_packets && !vi->mergeable_rx_bufs)
+	if (!vi->rq[qid].page_pool)
  		return -ENOENT;
  	if (qid >= vi->curr_queue_pairs)

It seems that a qid that exceeds curr_queue_pairs would previously get -EINVAL and now gets -ENOENT.

Quoted 19 lines

Or maybe this if (qid >= vi->curr_queue_pairs) is dead code? I looked at it some more and I can't find a path where this triggers.

Quoted 15 lines

Maybe reorder the checks:

         if (qid >= vi->curr_queue_pairs)
                 return -EINVAL;

         /* In big_packets mode, xdp cannot work, so there is no need to
          * initialize xsk of rq.
          */
        if (!vi->rq[qid].page_pool)
                return -ENOENT;

Alternatively I think we can completely drop this chunk: we already seem to init page_pull at all times except for big packets mode, so the current code is fine I think.

Quoted 8 lines

Yes, I agree qid >= curr_queue_pairs appears to be dead code. xsk_reg_pool_at_qid() in the XSK core already validates queue_id < max(real_num_rx_queues, real_num_tx_queues) before ndo_bpf is called, and real_num_rx_queues == curr_queue_pairs is an invariant in virtio_net. Both paths hold rtnl_lock so no race is possible. That said, I'll adopt your suggested reorder for v12 to ensure vi->rq[qid] isn't accessed before a bounds check, even if the check is currently redundant.

I think we can just take this as-is, and do more cleanups on top.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Signature · 2 lines
-- 
MST
#10 lore raw
In reply to Vishwanath Seshagiri
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Tue, 10 Mar 2026 11:31:04 -0700 you wrote:

Use page_pool for RX buffer allocation in mergeable and small buffer modes to enable page recycling and avoid repeated page allocator calls. skb_mark_for_recycle() enables page reuse in the network stack.

Big packets mode is unchanged because it uses page->private for linked list chaining of multiple pages per buffer, which conflicts with page_pool's internal use of page->private.

[...]

Here is the summary with links:
- [net-next,v11] virtio_net: add page_pool support for buffer allocation

    https://git.kernel.org/netdev/net-next/c/24fbd3967f3f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#11 lore raw
In reply to Vishwanath Seshagiri

Hi,

I've been testing linux-next (tags later than 03/17) and hit new issues in virtio-net on s390x. I bisected the issue, and I found this patch to be the first buggy commit.

The issue seems to only be reproducible when running in Secure Execution. Tested in a KVM guest, the virtio-net performance appears greatly reduced, and the dmesg output shows many instances of the following error messages.

Partial relevant logs
=====================
[   49.332028] macvtap0: bad gso: type: 0, size: 0, flags 1 tunnel 0 tnl csum 0 [   74.365668] macvtap0: bad gso: type: 2e, size: 27948, flags 0 tunnel 0 tnl csum 0 [  403.302168] macvtap0: bad csum: flags: 2, gso_type: 23 rx_tnl_csum 0 [  403.302271] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0 [  403.302279] macvtap0: bad csum: flags: 2, gso_type: e1 rx_tnl_csum 0 [  403.309492] macvtap0: bad csum: flags: 2, gso_type: 4c rx_tnl_csum 0 [  403.317029] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0

Steps to reproduce
==================
1. Boot a Linux guest implementing this patch under QEMU/KVM (*) with SE enabled and a virtio-net-ccw device attached.
2. Run dmesg. The error message is usually already present at boot time, but if not, it can be reproduced by creating any network traffic.

(*) This patch was not tested in a non-KVM hypervisor environment.

I've further confirmed that reverting this patch onto its parent commit resolves the issue. Please let me know if you'd like me to test a fix or if you would need more information.

Thanks in advance.

Best,
Omar

#12 lore raw
Quoted 29 lines · Omar Elghoul

Hi,

I've been testing linux-next (tags later than 03/17) and hit new issues in virtio-net on s390x. I bisected the issue, and I found this patch to be the first buggy commit.

The issue seems to only be reproducible when running in Secure Execution. Tested in a KVM guest, the virtio-net performance appears greatly reduced, and the dmesg output shows many instances of the following error messages.

Partial relevant logs
=====================
[   49.332028] macvtap0: bad gso: type: 0, size: 0, flags 1 tunnel 0 tnl csum 0 [   74.365668] macvtap0: bad gso: type: 2e, size: 27948, flags 0 tunnel 0 tnl csum 0 [  403.302168] macvtap0: bad csum: flags: 2, gso_type: 23 rx_tnl_csum 0 [  403.302271] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0 [  403.302279] macvtap0: bad csum: flags: 2, gso_type: e1 rx_tnl_csum 0 [  403.309492] macvtap0: bad csum: flags: 2, gso_type: 4c rx_tnl_csum 0 [  403.317029] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0

Steps to reproduce
==================
1. Boot a Linux guest implementing this patch under QEMU/KVM (*) with SE enabled and a virtio-net-ccw device attached.
2. Run dmesg. The error message is usually already present at boot time, but if not, it can be reproduced by creating any network traffic.

(*) This patch was not tested in a non-KVM hypervisor environment.

Quoted 8 lines

I've further confirmed that reverting this patch onto its parent commit resolves the issue. Please let me know if you'd like me to test a fix or if you would need more information.

Thanks in advance.

Best,
Omar

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
        struct net_device *dev = vi->dev;
        struct sk_buff *skb;
        u8 flags;
                
        if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                pr_debug("%s: short packet %i\n", dev->name, len);
                DEV_STATS_INC(dev, rx_length_errors);
                virtnet_rq_free_buf(vi, rq, buf);
                return;
        }
        
        /* About the flags below:
         * 1. Save the flags early, as the XDP program might overwrite them.
         * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
         * stay valid after XDP processing.
         * 2. XDP doesn't work with partially checksummed packets (refer to
         * virtnet_xdp_set()), so packets marked as
         * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
         */
                
        if (vi->mergeable_rx_bufs) {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                        stats);
        } else if (vi->big_packets) {
                void *p = page_address((struct page *)buf);
                
                flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                skb = receive_big(dev, vi, rq, buf, len, stats);
        } else {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
        }       

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{               
        struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
        int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
        struct page *page = virt_to_head_page(buf);
        int offset = buf - page_address(page);
        struct sk_buff *head_skb, *curr_skb;     
        unsigned int truesize = mergeable_ctx_to_truesize(ctx);
        unsigned int headroom = mergeable_ctx_to_headroom(ctx);
                
        head_skb = NULL;
                
        if (rq->use_page_pool_dma)
                page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
 {
+	void *buf;
+
 	BUG_ON(!rq->page_pool);
 
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
+
+	return buf;
 }
 
 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
Signature · 1 line
MST
#13 lore raw
Quoted 44 lines · Michael S. Tsirkin
Quoted 1 line · Omar Elghoul

[...]

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
                 
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
                 DEV_STATS_INC(dev, rx_length_errors);
                 virtnet_rq_free_buf(vi, rq, buf);
                 return;
         }
         
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
          */
                 
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                         stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf);
                 
                 flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                 skb = receive_big(dev, vi, rq, buf, len, stats);
         } else {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         }
Quoted 2 lines

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

Thank you for your analysis and explanation.

Quoted 25 lines

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
         struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx);
         unsigned int headroom = mergeable_ctx_to_headroom(ctx);
                 
         head_skb = NULL;
                 
         if (rq->use_page_pool_dma)
                 page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
Quoted 3 lines

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

I applied your patch and tested it on my system. With this change, I could not reproduce the same error anymore. I would be happy to test a proper fix once you have one.

Quoted 31 lines
drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
+	void *buf;
+
  	BUG_ON(!rq->page_pool);
 
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
+
+	return buf;
  }
 
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
#15 lore raw
In reply to Michael S. Tsirkin
Quoted 144 lines · Michael S. Tsirkin
Quoted 29 lines · Omar Elghoul

Hi,

I've been testing linux-next (tags later than 03/17) and hit new issues in virtio-net on s390x. I bisected the issue, and I found this patch to be the first buggy commit.

The issue seems to only be reproducible when running in Secure Execution. Tested in a KVM guest, the virtio-net performance appears greatly reduced, and the dmesg output shows many instances of the following error messages.

Partial relevant logs
=====================
[   49.332028] macvtap0: bad gso: type: 0, size: 0, flags 1 tunnel 0 tnl csum 0 [   74.365668] macvtap0: bad gso: type: 2e, size: 27948, flags 0 tunnel 0 tnl csum 0 [  403.302168] macvtap0: bad csum: flags: 2, gso_type: 23 rx_tnl_csum 0 [  403.302271] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0 [  403.302279] macvtap0: bad csum: flags: 2, gso_type: e1 rx_tnl_csum 0 [  403.309492] macvtap0: bad csum: flags: 2, gso_type: 4c rx_tnl_csum 0 [  403.317029] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0

Steps to reproduce
==================
1. Boot a Linux guest implementing this patch under QEMU/KVM (*) with SE enabled and a virtio-net-ccw device attached.
2. Run dmesg. The error message is usually already present at boot time, but if not, it can be reproduced by creating any network traffic.

(*) This patch was not tested in a non-KVM hypervisor environment.

Quoted 8 lines

I've further confirmed that reverting this patch onto its parent commit resolves the issue. Please let me know if you'd like me to test a fix or if you would need more information.

Thanks in advance.

Best,
Omar

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
        struct net_device *dev = vi->dev;
        struct sk_buff *skb;
        u8 flags;
                
        if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                pr_debug("%s: short packet %i\n", dev->name, len);
                DEV_STATS_INC(dev, rx_length_errors);
                virtnet_rq_free_buf(vi, rq, buf);
                return;
        }
        
        /* About the flags below:
         * 1. Save the flags early, as the XDP program might overwrite them.
         * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
         * stay valid after XDP processing.
         * 2. XDP doesn't work with partially checksummed packets (refer to
         * virtnet_xdp_set()), so packets marked as
         * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
         */
                
        if (vi->mergeable_rx_bufs) {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                        stats);
        } else if (vi->big_packets) {
                void *p = page_address((struct page *)buf);
                
                flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                skb = receive_big(dev, vi, rq, buf, len, stats);
        } else {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
        }       

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{               
        struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
        int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
        struct page *page = virt_to_head_page(buf);
        int offset = buf - page_address(page);
        struct sk_buff *head_skb, *curr_skb;     
        unsigned int truesize = mergeable_ctx_to_truesize(ctx);
        unsigned int headroom = mergeable_ctx_to_headroom(ctx);
                
        head_skb = NULL;
                
        if (rq->use_page_pool_dma)
                page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
 {
+	void *buf;
+
 	BUG_ON(!rq->page_pool);
 
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
+
+	return buf;
 }
 
 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
Signature · 2 lines
-- 
MST

or maybe like this:

drivers/net/virtio_net.c +7 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct net_device *dev,
 	 */
 	buf -= VIRTNET_RX_PAD + xdp_headroom;
 
-	if (rq->use_page_pool_dma) {
-		int offset = buf - page_address(page) +
-			     VIRTNET_RX_PAD + xdp_headroom;
-
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-	}
-
 	len -= vi->hdr_len;
 	u64_stats_add(&stats->bytes, len);
 
@@ -2398,9 +2391,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 
 	head_skb = NULL;
 
-	if (rq->use_page_pool_dma)
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
 	u64_stats_add(&stats->bytes, len - vi->hdr_len);
 
 	if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
 		return;
 	}
 
+	if (rq->use_page_pool_dma) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+	}
+
 	/* About the flags below:
 	 * 1. Save the flags early, as the XDP program might overwrite them.
 	 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
#16 lore raw
Quoted 101 lines · Michael S. Tsirkin
Quoted 100 lines · Michael S. Tsirkin
Quoted 1 line · Omar Elghoul

[...]

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
                 
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
                 DEV_STATS_INC(dev, rx_length_errors);
                 virtnet_rq_free_buf(vi, rq, buf);
                 return;
         }
         
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
          */
                 
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                         stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf);
                 
                 flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                 skb = receive_big(dev, vi, rq, buf, len, stats);
         } else {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         }

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
         struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx);
         unsigned int headroom = mergeable_ctx_to_headroom(ctx);
                 
         head_skb = NULL;
                 
         if (rq->use_page_pool_dma)
                 page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
+	void *buf;
+
  	BUG_ON(!rq->page_pool);
 
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
+
+	return buf;
  }
 
Quoted 8 lines
Quoted 7 lines
drivers/net/virtio_net.c +0 -0
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
Signature · 2 lines
-- 
MST

or maybe like this:

Both of these patches resolve the issue on my end.

Quoted 44 lines
drivers/net/virtio_net.c +7 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct net_device *dev,
  	 */
  	buf -= VIRTNET_RX_PAD + xdp_headroom;
 
-	if (rq->use_page_pool_dma) {
-		int offset = buf - page_address(page) +
-			     VIRTNET_RX_PAD + xdp_headroom;
-
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-	}
-
  	len -= vi->hdr_len;
  	u64_stats_add(&stats->bytes, len);
 
@@ -2398,9 +2391,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 
  	head_skb = NULL;
 
-	if (rq->use_page_pool_dma)
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
  	u64_stats_add(&stats->bytes, len - vi->hdr_len);
 
  	if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  		return;
  	}
 
+	if (rq->use_page_pool_dma) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+	}
+
  	/* About the flags below:
  	 * 1. Save the flags early, as the XDP program might overwrite them.
  	 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
#14 lore raw
In reply to Omar Elghoul
Quoted 94 lines · Omar Elghoul
Quoted 40 lines · Michael S. Tsirkin
Quoted 1 line · Omar Elghoul

[...]

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
                 DEV_STATS_INC(dev, rx_length_errors);
                 virtnet_rq_free_buf(vi, rq, buf);
                 return;
         }
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
          */
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                         stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf);
                 flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                 skb = receive_big(dev, vi, rq, buf, len, stats);
         } else {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         }
Quoted 2 lines

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

Thank you for your analysis and explanation.

Quoted 22 lines

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
         struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx);
         unsigned int headroom = mergeable_ctx_to_headroom(ctx);
         head_skb = NULL;
         if (rq->use_page_pool_dma)
                 page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
Quoted 3 lines

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

I applied your patch and tested it on my system. With this change, I could not reproduce the same error anymore. I would be happy to test a proper fix once you have one.

Quoted 20 lines
drivers/net/virtio_net.c +9 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
+	void *buf;
+
  	BUG_ON(!rq->page_pool);
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
Quoted 8 lines

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi,

Quoted 8 lines
drivers/net/virtio_net.c +2 -0
+
+	return buf;
  }
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
 
 
 

just sent

#17 lore raw
In reply to Omar Elghoul
Quoted 138 lines · Omar Elghoul
Quoted 99 lines · Michael S. Tsirkin
Quoted 98 lines · Michael S. Tsirkin
Quoted 1 line · Omar Elghoul

[...]

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
                         struct virtnet_rq_stats *stats)
{
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) { pr_debug("%s: short packet %i\n", dev->name, len); DEV_STATS_INC(dev, rx_length_errors); virtnet_rq_free_buf(vi, rq, buf);
                 return;
         }
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing. */
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)-

Quoted 1 line

hdr.flags;

                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf); flags = ((struct virtio_net_common_hdr *)p)->hdr.flags; skb = receive_big(dev, vi, rq, buf, len, stats); } else {
                 flags = ((struct virtio_net_common_hdr *)buf)-

Quoted 1 line

hdr.flags;

                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); }

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev, struct virtnet_info *vi, struct receive_queue *rq, void *buf,
                                          void *ctx,
                                          unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats) {
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx); unsigned int headroom = mergeable_ctx_to_headroom(ctx); head_skb = NULL;
         if (rq->use_page_pool_dma) page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct 
 virtnet_info *vi,
   static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
   {
+    void *buf;
+
       BUG_ON(!rq->page_pool);
-    return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+    buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+    if (buf && rq->use_page_pool_dma && *len) {
+        struct page *page = virt_to_head_page(buf);
+        int offset = buf - page_address(page);
+
+        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+    }
+
+    return buf;
   }
   static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
 
 
Quoted 3 lines
Quoted 2 lines
Signature · 1 line
MST

or maybe like this:

Both of these patches resolve the issue on my end.

Quoted 33 lines
drivers/net/virtio_net.c +3 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct 
 net_device *dev,
        */
       buf -= VIRTNET_RX_PAD + xdp_headroom;
-    if (rq->use_page_pool_dma) {
-        int offset = buf - page_address(page) +
-                 VIRTNET_RX_PAD + xdp_headroom;
-
-        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-    }
-
       len -= vi->hdr_len;
       u64_stats_add(&stats->bytes, len);
@@ -2398,9 +2391,6 @@ static struct sk_buff *receive_mergeable(struct 
 net_device *dev,
       head_skb = NULL;
-    if (rq->use_page_pool_dma)
-        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
       u64_stats_add(&stats->bytes, len - vi->hdr_len);
       if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info 
 *vi, struct receive_queue *rq,
           return;
       }
+    if (rq->use_page_pool_dma) {
+        struct page *page = virt_to_head_page(buf);
+        int offset = buf - page_address(page);
Quoted 8 lines

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 2565

Quoted 8 lines
drivers/net/virtio_net.c +4 -0
+
+        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+    }
+
       /* About the flags below:
        * 1. Save the flags early, as the XDP program might overwrite them.
        * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID

Hello Omar. Thank you for the bisect, apologies for breaking the functionality. Thank you Michael for the patch, I will be careful next time about DMA sync issues. Looks like the bug is fixed, is there anything else needed from my end?

#18 lore raw
Quoted 158 lines · Vishwanath Seshagiri
Quoted 155 lines · Omar Elghoul
Quoted 110 lines · Michael S. Tsirkin
Quoted 109 lines · Michael S. Tsirkin
Quoted 1 line · Omar Elghoul

[...]

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
                         void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
                         struct virtnet_rq_stats *stats) {
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) { pr_debug("%s: short packet %i\n", dev->name, len); DEV_STATS_INC(dev, rx_length_errors); virtnet_rq_free_buf(vi, rq, buf); return;
         }
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
          */
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)-

Quoted 1 line

hdr.flags;

                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                         stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf); flags = ((struct virtio_net_common_hdr *)p)->hdr.flags; skb = receive_big(dev, vi, rq, buf, len, stats); } else {
                 flags = ((struct virtio_net_common_hdr *)buf)-

Quoted 1 line

hdr.flags;

                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         }

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev, struct virtnet_info *vi, struct receive_queue *rq, void *buf, void *ctx, unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)
{
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx); unsigned int headroom = mergeable_ctx_to_headroom(ctx); head_skb = NULL;
         if (rq->use_page_pool_dma) page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing
twice.

drivers/net/virtio_net.c +0 -0
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct
 virtnet_info *vi,

  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
+    void *buf;
+
      BUG_ON(!rq->page_pool);
-    return virtqueue_get_buf_ctx(rq->vq, len, ctx); +    buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+    if (buf && rq->use_page_pool_dma && *len) {
+        struct page *page = virt_to_head_page(buf); +        int offset = buf - page_address(page);

drivers/net/virtio_net.c +5 -0
+
+        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+    }
+
+    return buf;

  }
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)

Quoted 3 lines
Quoted 2 lines
Signature · 1 line
MST

or maybe like this:

Both of these patches resolve the issue on my end.

Quoted 39 lines
drivers/net/virtio_net.c +7 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct
 net_device *dev,
        */
       buf -= VIRTNET_RX_PAD + xdp_headroom;
-    if (rq->use_page_pool_dma) {
-        int offset = buf - page_address(page) +
-                 VIRTNET_RX_PAD + xdp_headroom;
-
-        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-    }
-
       len -= vi->hdr_len;
       u64_stats_add(&stats->bytes, len);
@@ -2398,9 +2391,6 @@ static struct sk_buff
 *receive_mergeable(struct net_device *dev,
       head_skb = NULL;
-    if (rq->use_page_pool_dma)
-        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
       u64_stats_add(&stats->bytes, len - vi->hdr_len);
       if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info
 *vi, struct receive_queue *rq,
           return;
       }
+    if (rq->use_page_pool_dma) {
+        struct page *page = virt_to_head_page(buf);
+        int offset = buf - page_address(page);
+
+        page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+    }
+
       /* About the flags below:
        * 1. Save the flags early, as the XDP program might overwrite
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

them.
       * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID

Hello Omar. Thank you for the bisect, apologies for breaking the functionality. Thank you Michael for the patch, I will be careful next time about DMA sync issues. Looks like the bug is fixed, is there anything else needed from my end?

Would be great to have you test the patch on your end and report the performace. Thanks!

Signature · 1 line
MST
Acked-by
#19 lore raw
In reply to Michael S. Tsirkin
Quoted 182 lines · Michael S. Tsirkin
Quoted 143 lines · Michael S. Tsirkin
Quoted 29 lines · Omar Elghoul

Hi,

I've been testing linux-next (tags later than 03/17) and hit new issues in virtio-net on s390x. I bisected the issue, and I found this patch to be the first buggy commit.

The issue seems to only be reproducible when running in Secure Execution. Tested in a KVM guest, the virtio-net performance appears greatly reduced, and the dmesg output shows many instances of the following error messages.

Partial relevant logs
=====================

[   49.332028] macvtap0: bad gso: type: 0, size: 0, flags 1 tunnel 0 tnl csum 0
[   74.365668] macvtap0: bad gso: type: 2e, size: 27948, flags 0 tunnel 0 tnl csum 0
[  403.302168] macvtap0: bad csum: flags: 2, gso_type: 23 rx_tnl_csum 0
[  403.302271] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0
[  403.302279] macvtap0: bad csum: flags: 2, gso_type: e1 rx_tnl_csum 0
[  403.309492] macvtap0: bad csum: flags: 2, gso_type: 4c rx_tnl_csum 0
[  403.317029] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0

Steps to reproduce
==================
1. Boot a Linux guest implementing this patch under QEMU/KVM (*) with SE enabled and a virtio-net-ccw device attached.
2. Run dmesg. The error message is usually already present at boot time, but if not, it can be reproduced by creating any network traffic.

(*) This patch was not tested in a non-KVM hypervisor environment.

Quoted 8 lines

I've further confirmed that reverting this patch onto its parent commit resolves the issue. Please let me know if you'd like me to test a fix or if you would need more information.

Thanks in advance.

Best,
Omar

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
        struct net_device *dev = vi->dev;
        struct sk_buff *skb;
        u8 flags;

        if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                pr_debug("%s: short packet %i\n", dev->name, len);
                DEV_STATS_INC(dev, rx_length_errors);
                virtnet_rq_free_buf(vi, rq, buf);
                return;
        }

        /* About the flags below:
         * 1. Save the flags early, as the XDP program might overwrite them.
         * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
         * stay valid after XDP processing.
         * 2. XDP doesn't work with partially checksummed packets (refer to
         * virtnet_xdp_set()), so packets marked as
         * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
         */

        if (vi->mergeable_rx_bufs) {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                        stats);
        } else if (vi->big_packets) {
                void *p = page_address((struct page *)buf);

                flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                skb = receive_big(dev, vi, rq, buf, len, stats);
        } else {
                flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
        }

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{
        struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
        int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
        struct page *page = virt_to_head_page(buf);
        int offset = buf - page_address(page);
        struct sk_buff *head_skb, *curr_skb;
        unsigned int truesize = mergeable_ctx_to_truesize(ctx);
        unsigned int headroom = mergeable_ctx_to_headroom(ctx);

        head_skb = NULL;

        if (rq->use_page_pool_dma)
                page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -2
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
 {
+     void *buf;
+
      BUG_ON(!rq->page_pool);
 
-     return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+     buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+     if (buf && rq->use_page_pool_dma && *len) {
+             struct page *page = virt_to_head_page(buf);
+             int offset = buf - page_address(page);
+
+             page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+     }
+
+     return buf;
 }
 
 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
 
 
 
 
--
Quoted 1 line

MST

or maybe like this:

drivers/net/virtio_net.c +0 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct net_device *dev,
         */
        buf -= VIRTNET_RX_PAD + xdp_headroom;
 
-       if (rq->use_page_pool_dma) {
-               int offset = buf - page_address(page) +
-                            VIRTNET_RX_PAD + xdp_headroom;
-
-               page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-       }
-
        len -= vi->hdr_len;
        u64_stats_add(&stats->bytes, len);
 
@@ -2398,9 +2391,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 
        head_skb = NULL;
 
-       if (rq->use_page_pool_dma)
-               page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
        u64_stats_add(&stats->bytes, len - vi->hdr_len);
 
        if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
                return;
        }
 

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 2565

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
+       if (rq->use_page_pool_dma) {
+               struct page *page = virt_to_head_page(buf);
+               int offset = buf - page_address(page);
+
+               page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+       }
+
        /* About the flags below:
         * 1. Save the flags early, as the XDP program might overwrite them.
         * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID

This looks better.

Thanks

#20 lore raw
In reply to Michael S. Tsirkin
Quoted 181 lines · Michael S. Tsirkin
Quoted 144 lines · Michael S. Tsirkin
Quoted 29 lines · Omar Elghoul

Hi,

I've been testing linux-next (tags later than 03/17) and hit new issues in virtio-net on s390x. I bisected the issue, and I found this patch to be the first buggy commit.

The issue seems to only be reproducible when running in Secure Execution. Tested in a KVM guest, the virtio-net performance appears greatly reduced, and the dmesg output shows many instances of the following error messages.

Partial relevant logs
=====================
[   49.332028] macvtap0: bad gso: type: 0, size: 0, flags 1 tunnel 0 tnl csum 0 [   74.365668] macvtap0: bad gso: type: 2e, size: 27948, flags 0 tunnel 0 tnl csum 0 [  403.302168] macvtap0: bad csum: flags: 2, gso_type: 23 rx_tnl_csum 0 [  403.302271] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0 [  403.302279] macvtap0: bad csum: flags: 2, gso_type: e1 rx_tnl_csum 0 [  403.309492] macvtap0: bad csum: flags: 2, gso_type: 4c rx_tnl_csum 0 [  403.317029] macvtap0: bad csum: flags: 2, gso_type: e0 rx_tnl_csum 0

Steps to reproduce
==================
1. Boot a Linux guest implementing this patch under QEMU/KVM (*) with SE enabled and a virtio-net-ccw device attached.
2. Run dmesg. The error message is usually already present at boot time, but if not, it can be reproduced by creating any network traffic.

(*) This patch was not tested in a non-KVM hypervisor environment.

Quoted 8 lines

I've further confirmed that reverting this patch onto its parent commit resolves the issue. Please let me know if you'd like me to test a fix or if you would need more information.

Thanks in advance.

Best,
Omar

Well... I am not sure how I missed it. Obvious in hindsight:

static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, unsigned int len, void **ctx, unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)

{
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
         u8 flags;
                 
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
                 DEV_STATS_INC(dev, rx_length_errors);
                 virtnet_rq_free_buf(vi, rq, buf);
                 return;
         }
         
         /* About the flags below:
          * 1. Save the flags early, as the XDP program might overwrite them.
          * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
          * stay valid after XDP processing.
          * 2. XDP doesn't work with partially checksummed packets (refer to
          * virtnet_xdp_set()), so packets marked as
          * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
          */
                 
         if (vi->mergeable_rx_bufs) {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
                                         stats);
         } else if (vi->big_packets) {
                 void *p = page_address((struct page *)buf);
                 
                 flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
                 skb = receive_big(dev, vi, rq, buf, len, stats);
         } else {
                 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
                 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
         }

So we are reading the header, before dma sync, which is within receive_mergeable and friends:

static struct sk_buff *receive_mergeable(struct net_device *dev,
struct virtnet_info *vi, struct receive_queue *rq, void *buf,
void *ctx,
unsigned int len, unsigned int *xdp_xmit, struct virtnet_rq_stats *stats)

{
         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
         int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
         struct page *page = virt_to_head_page(buf);
         int offset = buf - page_address(page);
         struct sk_buff *head_skb, *curr_skb;
         unsigned int truesize = mergeable_ctx_to_truesize(ctx);
         unsigned int headroom = mergeable_ctx_to_headroom(ctx);
                 
         head_skb = NULL;
                 
         if (rq->use_page_pool_dma)
                 page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);

Just as a test, the below should fix it (compiled only), but the real fix is more complex since we need to be careful to avoid expensive syncing twice.

drivers/net/virtio_net.c +11 -1
index 97035b49bae7..57b4f5954bed 100644
@@ -931,9 +931,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 
  static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
  {
+	void *buf;
+
  	BUG_ON(!rq->page_pool);
 
-	return virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
+	if (buf && rq->use_page_pool_dma && *len) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, *len);
+	}
+
+	return buf;
  }
 
  static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
Signature · 2 lines
-- 
MST

or maybe like this:

drivers/net/virtio_net.c +0 -10
index 97035b49bae7..835f52651006 100644
@@ -1956,13 +1956,6 @@ static struct sk_buff *receive_small(struct net_device *dev,
  	 */
  	buf -= VIRTNET_RX_PAD + xdp_headroom;
 
-	if (rq->use_page_pool_dma) {
-		int offset = buf - page_address(page) +
-			     VIRTNET_RX_PAD + xdp_headroom;
-
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-	}
-
  	len -= vi->hdr_len;
  	u64_stats_add(&stats->bytes, len);
 
@@ -2398,9 +2391,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 
  	head_skb = NULL;
 
-	if (rq->use_page_pool_dma)
-		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
-
  	u64_stats_add(&stats->bytes, len - vi->hdr_len);
 
  	if (check_mergeable_len(dev, ctx, len))
@@ -2563,6 +2553,13 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  		return;
  	}
 

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 2565

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
+	if (rq->use_page_pool_dma) {
+		struct page *page = virt_to_head_page(buf);
+		int offset = buf - page_address(page);
+
+		page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
+	}
+
  	/* About the flags below:
  	 * 1. Save the flags early, as the XDP program might overwrite them.
  	 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID

I see the same virtio_net regression introduced by commit 24fbd3967f3f (“virtio_net: add page_pool support for buffer allocation”) on AMD EPYC (KVM/QEMU secure guests with virtio-net), in addition to s390x as in Omar Elghoul’s report [1].

The failure mode matches: broken guest networking until that commit is reverted or worked around.

I built and tested both of Michael Tsirkin’s proposed fixes in this thread [2] [3]; each resolves the issue on my AMD EPYC setup.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/#t