Skip to contentSkip to conversation

virtio_net: Fix an unsafe reference to the page chain

PATCH In review Liang Chen 8 participants 56 messages May 26, 2023 – Dec 4, 2023 Page 2 of 2

Subsystems (4)
lore
Share & tools
lore
author
#48 lore raw

Re: [PATCH net-next 5/5] virtio_net: Implement DMA pre-handler

In reply to Jason Wang
Quoted 85 lines · Jason Wang
Quoted 48 lines · Liang Chen

Adding a DMA pre-handler that utilizes page pool for managing DMA mappings. When IOMMU is enabled, turning on the page_pool_dma_map module parameter to select page pool for DMA mapping management gives a significant reduction in the overhead caused by DMA mappings.

In testing environments with a single core vm and qemu emulated IOMMU, significant performance improvements can be observed:
Upstream codebase: 1.76 Gbits/sec
Upstream codebase with page pool fragmentation support: 1.81 Gbits/sec Upstream codebase with page pool fragmentation and DMA support: 19.3 Gbits/sec

---
 drivers/net/virtio_net.c | 55 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
drivers/net/virtio_net.c +6 -0
index ac40b8c66c59..73cc4f9fe4fa 100644
@@ -22,6 +22,7 @@
 #include <net/route.h>
 #include <net/xdp.h>
 #include <net/net_failover.h>
+#include <linux/iommu.h>
 
 static int napi_weight = NAPI_POLL_WEIGHT;
 module_param(napi_weight, int, 0444);
@@ -33,8 +34,10 @@ module_param(napi_tx, bool, 0644);
 
 static bool page_pool_enabled;
 static bool page_pool_frag;
+static bool page_pool_dma_map;
 module_param(page_pool_enabled, bool, 0400);
 module_param(page_pool_frag, bool, 0400);
+module_param(page_pool_dma_map, bool, 0400);
 
 /* FIXME: MTU in config. */
 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
@@ -3830,6 +3833,49 @@ static void virtnet_del_vqs(struct virtnet_info *vi)
        virtnet_free_queues(vi);
 }
 
+static dma_addr_t virtnet_pp_dma_map_page(struct device *dev, struct page *page,
+                                         unsigned long offset, size_t size,
+                                         enum dma_data_direction dir, unsigned long attrs)

drivers/net/virtio_net.c static void virtnet_del_vqs(struct virtnet_info *vi) · line 3848

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+{
+       struct page *head_page;
+
+       if (dir != DMA_FROM_DEVICE)
+               return 0;
+
+       head_page = compound_head(page);
+       return page_pool_get_dma_addr(head_page)
+               + (page - head_page) * PAGE_SIZE
+               + offset;

So it's not a map, it is just a query from the dma address from the pool.

Quoted 8 lines
drivers/net/virtio_net.c +8 -0
+}
+
+static bool virtnet_pp_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
+                                     size_t size, enum dma_data_direction dir,
+                                     unsigned long attrs)
+{
+       phys_addr_t phys;
+

drivers/net/virtio_net.c static void virtnet_del_vqs(struct virtnet_info *vi) · line 3866

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       /* Handle only the RX direction, and sync the DMA memory only if it's not
+        * a DMA coherent architecture.
+        */
+       if (dir != DMA_FROM_DEVICE)
+               return false;
+
+       if (dev_is_dma_coherent(dev))
+               return true;
+
+       phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);

This would be somehow slow. If we track the mapping by driver, it would be much faster.

Quoted 6 lines

More could be seen here:

https://lists.linuxfoundation.org/pipermail/virtualization/2023-May/066778.html

Thanks

Thanks for the information. I agree with your suggestion, and I will drop the last two patches on v2 and wait for Xuan's patch to land for dma mapping management.

Quoted 19 lines
Quoted 19 lines
drivers/net/virtio_net.c +12 -0
+       if (WARN_ON(!phys))
+               return false;
+
+       arch_sync_dma_for_cpu(phys, size, dir);
+       return true;
+}
+
+static struct virtqueue_pre_dma_ops virtnet_pp_pre_dma_ops = {
+       .map_page = virtnet_pp_dma_map_page,
+       .unmap_page = virtnet_pp_dma_unmap_page,
+};
+
 static void virtnet_alloc_page_pool(struct receive_queue *rq)
 {
        struct virtio_device *vdev = rq->vq->vdev;
@@ -3845,6 +3891,15 @@ static void virtnet_alloc_page_pool(struct receive_queue *rq)
        if (page_pool_frag)
                pp_params.flags |= PP_FLAG_PAGE_FRAG;
 
Quoted 16 lines
Quoted 13 lines
drivers/net/virtio_net.c +9 -1
+       /* Consider using page pool DMA support only when DMA API is used. */
+       if (virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM) &&
+           page_pool_dma_map) {
+               pp_params.flags |= PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
+               pp_params.dma_dir = DMA_FROM_DEVICE;
+               pp_params.max_len = PAGE_SIZE << pp_params.order;
+               virtqueue_register_pre_dma_ops(rq->vq, &virtnet_pp_pre_dma_ops);
+       }
+
        rq->page_pool = page_pool_create(&pp_params);
        if (IS_ERR(rq->page_pool)) {
                dev_warn(&vdev->dev, "page pool creation failed: %ld\n",
--
Quoted 2 lines

2.31.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#49 lore raw

Re: [PATCH net-next 5/5] virtio_net: Implement DMA pre-handler

In reply to Liang Chen
kernel test robot noticed the following build errors:

Hi Liang,

kernel test robot noticed the following build errors:

[auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Liang-Chen/virtio_net-Add-page_pool-support-to-improve-performance/20230526-135805
base:   net-next/main
patch link:    https://lore.kernel.org/r/20230526054621.18371-5-liangchen.linux%40gmail.com
patch subject: [PATCH net-next 5/5] virtio_net: Implement DMA pre-handler
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20230527/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/e968bb5cacd30b672d0ccf705a24f1a792ff45aa
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Liang-Chen/virtio_net-Add-page_pool-support-to-improve-performance/20230526-135805 git checkout e968bb5cacd30b672d0ccf705a24f1a792ff45aa
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 ~/bin/make.cross W=1 O=build_dir ARCH=m68k olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 ~/bin/make.cross W=1 O=build_dir ARCH=m68k SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "iommu_get_dma_domain" [drivers/net/virtio_net.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#50 lore raw
In reply to Liang Chen
Quoted 5 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

If this issue happens only in the case of page_pool, it would be better to squash it there.

Thanks

Quoted 20 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +1 -2
index 5a7f7a76b920..c5dca0d92e64 100644
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                        return NULL;
 
                page = (struct page *)page->private;
-               if (page)
+               if (!vi->mergeable_rx_bufs && page)
                        give_pages(rq, page);
                goto ok;
        }
--
Quoted 2 lines

2.31.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#51 lore raw
Quoted 8 lines · Jason Wang
Quoted 5 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

Quoted 4 lines

If this issue happens only in the case of page_pool, it would be better to squash it there.

Thanks

Sure, thanks!

Quoted 24 lines
Quoted 23 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +1 -2
index 5a7f7a76b920..c5dca0d92e64 100644
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                        return NULL;
 
                page = (struct page *)page->private;
-               if (page)
+               if (!vi->mergeable_rx_bufs && page)
                        give_pages(rq, page);
                goto ok;
        }
--

2.31.1

#52 lore raw
In reply to Jason Wang
Quoted 11 lines · Jason Wang
Quoted 5 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

If this issue happens only in the case of page_pool, it would be better to squash it there.

Thanks

This is a tiny patch so I don't care. Generally it's ok to first rework code then change functionality. in this case what Jason says os right especially because you then do not need to explain that current code is ok.

Quoted 22 lines
Quoted 22 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +1 -2
index 5a7f7a76b920..c5dca0d92e64 100644
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                        return NULL;
 
                page = (struct page *)page->private;
-               if (page)
+               if (!vi->mergeable_rx_bufs && page)
                        give_pages(rq, page);
                goto ok;
        }
--

2.31.1

author
#53 lore raw
Quoted 12 lines · Michael S. Tsirkin
Quoted 10 lines · Jason Wang
Quoted 5 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

If this issue happens only in the case of page_pool, it would be better to squash it there.

Quoted 8 lines
Quoted 1 line

Thanks

This is a tiny patch so I don't care. Generally it's ok to first rework code then change functionality. in this case what Jason says os right especially because you then do not need to explain that current code is ok.

Sure. it will be squashed into the page pool enablement patch. Thanks!

Quoted 23 lines
Quoted 22 lines
Quoted 22 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +1 -2
index 5a7f7a76b920..c5dca0d92e64 100644
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                        return NULL;
 
                page = (struct page *)page->private;
-               if (page)
+               if (!vi->mergeable_rx_bufs && page)
                        give_pages(rq, page);
                goto ok;
        }
--

2.31.1

#54 lore raw
In reply to Liang Chen
Quoted 6 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

Ugh the subject makes it looks like current code has a problem but I don't think so because I don't think anything besides big packets uses page->private.

The reason patch is needed is because follow up patches use page_pool.
pls adjust commit log and subject to make all this clear.

Quoted 9 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 5a7f7a76b920..c5dca0d92e64 100644

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

Quoted 5 lines
drivers/net/virtio_net.c +1 -1
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 			return NULL;
 
 		page = (struct page *)page->private;
-		if (page)
+		if (!vi->mergeable_rx_bufs && page)

To be safe let's limit to big packets too:

	if (!vi->mergeable_rx_bufs && vi->big_packets && page)
Quoted 5 lines
drivers/net/virtio_net.c +0 -0
 			give_pages(rq, page);
 		goto ok;
 	}
Signature · 1 line
2.31.1
author
#55 lore raw
Quoted 29 lines · Michael S. Tsirkin
Quoted 6 lines · Liang Chen

"private" of buffer page is currently used for big mode to chain pages. But in mergeable mode, that offset of page could mean something else, e.g. when page_pool page is used instead. So excluding mergeable mode to avoid such a problem.

Ugh the subject makes it looks like current code has a problem but I don't think so because I don't think anything besides big packets uses page->private.

The reason patch is needed is because follow up patches use page_pool.
pls adjust commit log and subject to make all this clear.

Quoted 11 lines
---
 drivers/net/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 5a7f7a76b920..c5dca0d92e64 100644
@@ -497,7 +497,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                      return NULL;
 
Quoted 10 lines

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

Quoted 3 lines
drivers/net/virtio_net.c +1 -1
              page = (struct page *)page->private;
-             if (page)
+             if (!vi->mergeable_rx_bufs && page)

To be safe let's limit to big packets too:

        if (!vi->mergeable_rx_bufs && vi->big_packets && page)

Sure, thanks!

Quoted 6 lines
Quoted 5 lines
drivers/net/virtio_net.c +0 -1
                      give_pages(rq, page);
              goto ok;
      }
--

2.31.1