Skip to contentSkip to conversation

virtio_ring: use alloc_pages_node for NUMA-aware allocation

PATCH In review Shile Zhang 2 participants 5 messages Jul 21, 2020 – Aug 4, 2020

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

PATCH v2 1/1 virtio_ring: use alloc_pages_node for NUMA-aware allocation

Use alloc_pages_node() allocate memory for vring queue with proper NUMA affinity.

---
Changelog
v1 -> v2:
- fixed compile warning reported by LKP.

---
 drivers/virtio/virtio_ring.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +6 -4
index 58b96baa8d48..d38fd6872c8c 100644
@@ -276,9 +276,11 @@ static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
 		return dma_alloc_coherent(vdev->dev.parent, size,
 					  dma_handle, flag);
 	} else {
-		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
-
-		if (queue) {
+		void *queue = NULL;
+		struct page *page = alloc_pages_node(dev_to_node(vdev->dev.parent),
+						     flag, get_order(size));
+		if (page) {
+			queue = page_address(page);
 			phys_addr_t phys_addr = virt_to_phys(queue);
 			*dma_handle = (dma_addr_t)phys_addr;
 
@@ -308,7 +310,7 @@ static void vring_free_queue(struct virtio_device *vdev, size_t size,
 	if (vring_use_dma_api(vdev))
 		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
 	else
-		free_pages_exact(queue, PAGE_ALIGN(size));
+		free_pages((unsigned long)queue, get_order(size));
 }
 
 /*
Signature · 1 line
2.24.0.rc2
#1 lore raw
Quoted 6 lines · Shile Zhang

Use alloc_pages_node() allocate memory for vring queue with proper NUMA affinity.

Do you observe any performance gains from this patch?

I also wonder why isn't the probe code run on the correct numa node? That would fix a wide class of issues like this without need to tweak drivers.

Bjorn, what do you think? Was this considered?

Quoted 38 lines

---
Changelog
v1 -> v2:
- fixed compile warning reported by LKP.

---
 drivers/virtio/virtio_ring.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +6 -4
index 58b96baa8d48..d38fd6872c8c 100644
@@ -276,9 +276,11 @@ static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
 		return dma_alloc_coherent(vdev->dev.parent, size,
 					  dma_handle, flag);
 	} else {
-		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
-
-		if (queue) {
+		void *queue = NULL;
+		struct page *page = alloc_pages_node(dev_to_node(vdev->dev.parent),
+						     flag, get_order(size));
+		if (page) {
+			queue = page_address(page);
 			phys_addr_t phys_addr = virt_to_phys(queue);
 			*dma_handle = (dma_addr_t)phys_addr;
 
@@ -308,7 +310,7 @@ static void vring_free_queue(struct virtio_device *vdev, size_t size,
 	if (vring_use_dma_api(vdev))
 		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
 	else
-		free_pages_exact(queue, PAGE_ALIGN(size));
+		free_pages((unsigned long)queue, get_order(size));
 }
 
 /*
Signature · 1 line
2.24.0.rc2
#2 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Shile Zhang

Use alloc_pages_node() allocate memory for vring queue with proper NUMA affinity.

Do you observe any performance gains from this patch?

Thanks for your comments!
Yes, the bandwidth can boost more than doubled (from 30Gbps to 80GBps) with this changes in my test env (8 numa nodes), with netperf test.

Quoted 4 lines

I also wonder why isn't the probe code run on the correct numa node? That would fix a wide class of issues like this without need to tweak drivers.

Good point, I'll check this, thanks!

Quoted 41 lines

Bjorn, what do you think? Was this considered?

Quoted 38 lines

---
Changelog
v1 -> v2:
- fixed compile warning reported by LKP.

---
  drivers/virtio/virtio_ring.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +6 -4
index 58b96baa8d48..d38fd6872c8c 100644
@@ -276,9 +276,11 @@ static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
  		return dma_alloc_coherent(vdev->dev.parent, size,
  					  dma_handle, flag);
  	} else {
-		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
-
-		if (queue) {
+		void *queue = NULL;
+		struct page *page = alloc_pages_node(dev_to_node(vdev->dev.parent),
+						     flag, get_order(size));
+		if (page) {
+			queue = page_address(page);
  			phys_addr_t phys_addr = virt_to_phys(queue);
  			*dma_handle = (dma_addr_t)phys_addr;
 
@@ -308,7 +310,7 @@ static void vring_free_queue(struct virtio_device *vdev, size_t size,
  	if (vring_use_dma_api(vdev))
  		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
  	else
-		free_pages_exact(queue, PAGE_ALIGN(size));
+		free_pages((unsigned long)queue, get_order(size));
  }
 
  /*
Signature · 1 line
2.24.0.rc2
#3 lore raw
Quoted 17 lines · Shile Zhang
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Shile Zhang

Use alloc_pages_node() allocate memory for vring queue with proper NUMA affinity.

Do you observe any performance gains from this patch?

Thanks for your comments!
Yes, the bandwidth can boost more than doubled (from 30Gbps to 80GBps) with this changes in my test env (8 numa nodes), with netperf test.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

I also wonder why isn't the probe code run on the correct numa node? That would fix a wide class of issues like this without need to tweak drivers.

Good point, I'll check this, thanks!

Sorry, I have no idea about how the probe code to grab the appropriate NUMA node.

Quoted 3 lines
Quoted 3 lines

Bjorn, what do you think? Was this considered?

Hi Bjorn, Could you please give any comments about this issue? Thanks!

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines

---
Changelog
v1 -> v2:
- fixed compile warning reported by LKP.
---
  drivers/virtio/virtio_ring.c | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 58b96baa8d48..d38fd6872c8c 100644
@@ -276,9 +276,11 @@ static void *vring_alloc_queue(struct 
 virtio_device *vdev, size_t size,
           return dma_alloc_coherent(vdev->dev.parent, size,
                         dma_handle, flag);
       } else {
-        void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
-
-        if (queue) {
+        void *queue = NULL;
+        struct page *page = 
 alloc_pages_node(dev_to_node(vdev->dev.parent),
+                             flag, get_order(size));
+        if (page) {
+            queue = page_address(page);
               phys_addr_t phys_addr = virt_to_phys(queue);

              *dma_handle = (dma_addr_t)phys_addr;

drivers/virtio/virtio_ring.c +0 -0
@@ -308,7 +310,7 @@ static void vring_free_queue(struct virtio_device 
 *vdev, size_t size,
       if (vring_use_dma_api(vdev))
           dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);

      else
-        free_pages_exact(queue, PAGE_ALIGN(size)); +        free_pages((unsigned long)queue, get_order(size)); }
  /*

Signature · 1 line
2.24.0.rc2
#4 lore raw

Hi Michael & Bjorn,

Sorry for the ping,
but how about this patch/issue? any comments/suggestions?

Thanks!

Quoted 75 lines · Shile Zhang
Quoted 17 lines · Shile Zhang
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Shile Zhang

Use alloc_pages_node() allocate memory for vring queue with proper NUMA affinity.

Do you observe any performance gains from this patch?

Thanks for your comments!
Yes, the bandwidth can boost more than doubled (from 30Gbps to 80GBps) with this changes in my test env (8 numa nodes), with netperf test.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

I also wonder why isn't the probe code run on the correct numa node? That would fix a wide class of issues like this without need to tweak drivers.

Good point, I'll check this, thanks!

Sorry, I have no idea about how the probe code to grab the appropriate NUMA node.

Quoted 3 lines
Quoted 3 lines

Bjorn, what do you think? Was this considered?

Hi Bjorn, Could you please give any comments about this issue? Thanks!

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines

---
Changelog
v1 -> v2:
- fixed compile warning reported by LKP.
---
  drivers/virtio/virtio_ring.c | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 58b96baa8d48..d38fd6872c8c 100644
@@ -276,9 +276,11 @@ static void *vring_alloc_queue(struct 
 virtio_device *vdev, size_t size,
           return dma_alloc_coherent(vdev->dev.parent, size,
                         dma_handle, flag);
       } else {
-        void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
-
-        if (queue) {
+        void *queue = NULL;
+        struct page *page = 
 alloc_pages_node(dev_to_node(vdev->dev.parent),
+                             flag, get_order(size));
+        if (page) {
+            queue = page_address(page);
               phys_addr_t phys_addr = virt_to_phys(queue);

              *dma_handle = (dma_addr_t)phys_addr;

drivers/virtio/virtio_ring.c +0 -0
@@ -308,7 +310,7 @@ static void vring_free_queue(struct 
 virtio_device *vdev, size_t size,
       if (vring_use_dma_api(vdev))
           dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);

      else
-        free_pages_exact(queue, PAGE_ALIGN(size)); +        free_pages((unsigned long)queue, get_order(size)); }
  /*

Signature · 1 line
2.24.0.rc2