Skip to contentSkip to conversation

virtio_net: add page_pool support

PATCH Superseded Vishwanath Seshagiri 2 participants 5 messages Feb 6 – Feb 7

Subsystems (5)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v5 cover virtio_net: add page_pool support

Introduce page_pool support in virtio_net driver to enable page recycling in RX buffer allocation and avoid repeated page allocator calls. This applies to mergeable and small buffer modes.

Beyond performance improvements, this patch is a prerequisite for enabling memory provider-based zero-copy features in virtio_net, specifically devmem TCP and io_uring ZCRX, which require drivers to use page_pool for buffer management.

The implementation preserves the DMA premapping optimization introduced in commit 31f3cd4e5756 ("virtio-net: rq submits premapped per-buffer") by conditionally using PP_FLAG_DMA_MAP when the virtio backend supports standard DMA API (vhost, virtio-pci), and falling back to allocation-only mode for backends with custom DMA mechanisms (VDUSE).

================================================================================
VIRTIO-NET PAGE POOL BENCHMARK RESULTS
================================================================================

CONFIGURATION
-------------
- Host: pktgen TX -> tap interface -> vhost-net
- Guest: virtio-net RX -> XDP_DROP
- Packet sizes: small buffers - 64; merge receivable 64, 1500

SMALL PACKETS (64 bytes)
==================================================

Queues | Base (pps)  | Page Pool (pps) | Improvement | Base (Gb/s) |PP (Gb/s)
-------|-------------|-----------------|-------------|-------------|----------
  1Q   |    853,493  |      868,923    |   +1.8%     |    0.44     | 0.44
  2Q   |  1,655,793  |    1,696,707    |   +2.5%     |    0.85     | 0.87
  4Q   |  3,143,375  |    3,302,511    |   +5.1%     |    1.61     | 1.69
  8Q   |  6,082,590  |    6,156,894    |   +1.2%     |    3.11     | 3.15

RECEIVE MERGEABLE (64 bytes)
======================================================

Queues | Base (pps)  | Page Pool (pps) | Improvement | Base (Gb/s) |PP (Gb/s)
-------|-------------|-----------------|-------------|-------------|----------
   1Q  |    766,168  |      814,493    |   +6.3%     |    0.39     | 0.42
   2Q  |  1,384,871  |    1,670,639    |  +20.6%     |    0.71     | 0.86
   4Q  |  2,773,081  |    3,080,574    |  +11.1%     |    1.42     | 1.58
   8Q  |  5,600,615  |    6,043,891    |   +7.9%     |    2.87     | 3.10

RECEIVE MERGEABLE (1500 bytes)
========================================================

Queues | Base (pps)  | Page Pool (pps) | Improvement | Base (Gb/s) |PP (Gb/s)
-------|-------------|-----------------|-------------|-------------|----------
  1Q   |    741,579  |      785,442    |   +5.9%     |    8.90     | 9.43
  2Q   |  1,310,043  |    1,534,554    |  +17.1%     |   15.72     | 18.41
  4Q   |  2,748,700  |    2,890,582    |   +5.2%     |   32.98     | 34.69
  8Q   |  5,348,589  |    5,618,664    |   +5.0%     |   64.18     | 67.42

The page_pool implementation showed consistent performance improvements by eliminating per packet overhead of allocating and deallocating memory. When running the performance benchmarks, I noticed that page_pool also had a consistent throughput performance compared to the base patch where performance variability was due to accessing free_list for getting the next set of pages.

Changes in v5
=============

Addressing reviewer feedback from v4:

- Add page_pool_frag_offset_add() helper to page_pool API to advance fragment offset when drivers extend buffers to consume unused page
space (hole optimization). (Michael S. Tsirkin)
- Unify big_packets condition checks and added an explanatory comment (Michael S. Tsirkin)
- Add page_pool_dma_sync_for_cpu() calls in receive paths before
reading buffer data when using PP_FLAG_DMA_MAP
(Michael S. Tsirkin)
- Remove virtnet_rq_unmap() and free_receive_page_frags() entirely, replacing with page_pool lifecycle management (Jason Wang)
- Dropped selftests patch from the series
- v4 link: https://lore.kernel.org/virtualization/[email protected]/

Changes in v4
=============

Addressing reviewer feedback from v3:

- Remove unnecessary !rq->page_pool check in page_to_skb()
- Reorder put_xdp_frags() parameters
- Remove unnecessary xdp_page = NULL initialization in receive_small_xdp()
- Move big_packets mode check outside the loop in virtnet_create_page_pools() for efficiency
- Remove unrelated whitespace changes
- v3 link: https://lore.kernel.org/virtualization/[email protected]/

Changes in v3
=============

Addressing reviewer feedback from v2:

- Fix CI null-ptr-deref crash: use max_queue_pairs instead of curr_queue_pairs in virtnet_create_page_pools() to ensure page pools
are created for all queues (Jason Wang, Jakub Kicinski)
- Preserve big_packets mode page->private chaining in page_to_skb() with conditional checks (Jason Wang)
- Use page_pool_alloc_pages() in xdp_linearize_page() and mergeable_xdp_get_buf() to eliminate xdp_page tracking logic and simplify skb_mark_for_recycle() calls (Jason Wang)
- Add page_pool_page_is_pp() check in virtnet_put_page() to safely
handle both page_pool and non-page_pool pages (Michael S. Tsirkin)
- Remove unrelated rx_mode_work_enabled changes (Jason Wang)
- Selftest: use random ephemeral port instead of hardcoded port to avoid conflicts when running tests in parallel (Michael S. Tsirkin)
- v2 link: https://lore.kernel.org/virtualization/[email protected]/

Changes in v2
=============

Addressing reviewer feedback from v1:

- Add "select PAGE_POOL" to Kconfig (Jason Wang)
- Move page pool creation from ndo_open to probe for device lifetime management (Xuan Zhuo, Jason Wang)
- Implement conditional DMA strategy using virtqueue_dma_dev():
- When non-NULL: use PP_FLAG_DMA_MAP for page_pool-managed DMA premapping
- When NULL (VDUSE): page_pool handles allocation only
- Use page_pool_get_dma_addr() + virtqueue_add_inbuf_premapped() to preserve DMA premapping optimization from commit 31f3cd4e5756 ("virtio-net: rq submits premapped per-buffer") (Jason Wang)
- Remove dual allocation code paths - page_pool now always used for small/mergeable modes (Jason Wang)
- Remove unused virtnet_rq_alloc/virtnet_rq_init_one_sg functions
- Add comprehensive performance data (Michael S. Tsirkin)
- v1 link:
https://lore.kernel.org/virtualization/[email protected]/

Vishwanath Seshagiri (2):
page_pool: add page_pool_frag_offset_add() helper
virtio_net: add page_pool support for buffer allocation

 drivers/net/Kconfig             |   1 +
 drivers/net/virtio_net.c        | 430 ++++++++++++++++----------------
 include/net/page_pool/helpers.h |  20 ++
 3 files changed, 241 insertions(+), 210 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v5 1/2 page_pool: add page_pool_frag_offset_add() helper

Add a helper function to advance the fragment offset without performing an allocation. This is needed by drivers that extend a buffer to consume unused space at the end of a page fragment to avoid internal fragmentation.

When a driver uses page_pool_alloc_frag() and determines that the remaining space in the page is too small for another buffer, it may extend the current buffer to include that space. However, page_pool's internal frag_offset is not aware of this extension, which could cause the next allocation to overlap with the extended buffer.

page_pool_frag_offset_add() allows drivers to advance frag_offset to match the actual consumed space.

---
 include/net/page_pool/helpers.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/net/page_pool/helpers.h +20 -0
index 3247026e096a..14907c3badae 100644
@@ -96,6 +96,26 @@ static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
 	return page_pool_alloc_pages(pool, gfp);
 }
 
+/**
+ * page_pool_frag_offset_add() - advance fragment offset without allocation
+ * @pool: pool to update
+ * @bytes: number of bytes to skip
+ *
+ * Advance the fragment offset by @bytes without performing an allocation.
+ * This is useful when a driver extends a buffer to consume unused space
+ * at the end of a page fragment (to avoid internal fragmentation), and
+ * needs to ensure the next allocation doesn't overlap.
+ *
+ * Must be called in the same context as page_pool_alloc_frag() to avoid
+ * racing with fragment allocations.
+ *
+ */
+static inline void page_pool_frag_offset_add(struct page_pool *pool,
+					     unsigned int bytes)
+{
+	pool->frag_offset += bytes;
+}
+
 /**
  * page_pool_dev_alloc_frag() - allocate a page fragment.
  * @pool: pool from which to allocate
Signature · 1 line
2.47.3
#2 lore raw

PATCH v5 2/2 virtio_net: add page_pool support for buffer allocation

In reply to 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.

---
 drivers/net/Kconfig      |   1 +
 drivers/net/virtio_net.c | 430 ++++++++++++++++++++-------------------
 2 files changed, 221 insertions(+), 210 deletions(-)
drivers/net/Kconfig +1 -0
index ac12eaf11755..f1e6b6b0a86f 100644
@@ -450,6 +450,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 +220 -210

778 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#3 lore raw

Re: [PATCH net-next v5 2/2] virtio_net: add page_pool support for buffer allocation

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct virtnet_info *vi, · line 2714

Quoted 5 lines · On Thu, 5 Feb 2026 16:27:15 -0800 Vishwanath Seshagiri wrote:
drivers/net/virtio_net.c +3 -0
+	page = page_pool_alloc_frag(rq->page_pool, &offset, len + room, gfp);
+	if (unlikely(!page))
 		return -ENOMEM;
 
+	buf = page_address(page) + offset;

Please take a look at page_pool_alloc_va()

I mean all the way down until you reach page_pool_alloc_netmem() and realize the helper you're adding in patch 1 is a solved problem

Signature · 1 line
pw-bot: cr
#4 lore raw

Re: [PATCH net-next v5 2/2] virtio_net: add page_pool support for buffer allocation

Quoted 11 lines · Jakub Kicinski

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct virtnet_info *vi, · line 2714

Quoted 5 lines · On Thu, 5 Feb 2026 16:27:15 -0800 Vishwanath Seshagiri wrote:
drivers/net/virtio_net.c +3 -0
+	page = page_pool_alloc_frag(rq->page_pool, &offset, len + room, gfp);
+	if (unlikely(!page))
  		return -ENOMEM;
 
+	buf = page_address(page) + offset;

Please take a look at page_pool_alloc_va()

I mean all the way down until you reach page_pool_alloc_netmem() and realize the helper you're adding in patch 1 is a solved problem

I apologize for adding patch 1. I will use page_pool_alloc_va and send v6 with only patch 2.