Skip to contentSkip to conversation

virtio_net: add page_pool support for buffer allocation

PATCH Superseded Vishwanath Seshagiri 2 participants 3 messages Feb 28 – Mar 1

Subsystems (4)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 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 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 resend:
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 | 460 +++++++++++++++++++--------------------
 2 files changed, 231 insertions(+), 230 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 +230 -230

837 lines not on this page · raw message

Signature · 1 line
2.47.3
#1 lore raw

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 2355

Quoted 9 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -2446,7 +2349,7 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
 
 	offset = buf - page_address(page);
 	if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
-		put_page(page);
+		page_pool_put_page(rq->page_pool, page, -1, true);
 		skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
 				     len, truesize);
 	} else {

This part seems wrong to me: can this not be called from xsk_append_merge_buffer? Because that one seems to still allocate with napi_alloc_frag.

Signature · 1 line
MST
#2 lore raw
Quoted 15 lines · Michael S. Tsirkin

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 2355

Quoted 9 lines · Vishwanath Seshagiri
drivers/net/virtio_net.c +1 -1
@@ -2446,7 +2349,7 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
 
  	offset = buf - page_address(page);
  	if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
-		put_page(page);
+		page_pool_put_page(rq->page_pool, page, -1, true);
  		skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
  				     len, truesize);
  	} else {

This part seems wrong to me: can this not be called from xsk_append_merge_buffer? Because that one seems to still allocate with napi_alloc_frag.

Ack. will fix this in v9.

Quoted 3 lines