Skip to contentSkip to conversation

vhost: cache guest/vhost physical address mapping

PATCH In review Marvin Liu 5 participants 31 messages Mar 16, 2020 – Apr 29, 2020 Network drivers

lore
Share & tools
lore

Marvin Liu

author
#0 lore raw

PATCH 1/1 vhost: cache guest/vhost physical address mapping

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, now performance is back.

lib/librte_vhost/vhost.h +14 -0
index 2087d1400..de2c09e7e 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -554,11 +556,23 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 	uint32_t i;
 	struct guest_page *page;
 
+	for (i = 0; i < dev->nr_cached; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}
+
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev->cached_guest_pages[dev->nr_cached],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
lib/librte_vhost/vhost_user.c +18 -1
index bd1be0104..573e99066 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	free(dev->guest_pages);
+	free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -905,7 +907,10 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		old_pages = dev->guest_pages;
 		dev->guest_pages = realloc(dev->guest_pages,
 					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->cached_guest_pages = realloc(dev->cached_guest_pages,
+					dev->max_guest_pages * sizeof(*page));
+		dev->nr_cached = 0;
+		if (!dev->guest_pages || !dev->cached_guest_pages) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
 			free(old_pages);
 			return -1;
@@ -1075,6 +1080,18 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (!dev->cached_guest_pages) {
+		dev->cached_guest_pages = malloc(dev->max_guest_pages *
+						sizeof(struct guest_page));
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
 	if (dev->mem == NULL) {
Signature · 1 line
2.17.1
#1 lore raw

Hi, Marvin

Quoted 13 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, now performance is back.

lib/librte_vhost/vhost.h +0 -0
index 2087d1400..de2c09e7e 100644

lib/librte_vhost/vhost.h struct virtio_net { · line 371

Quoted 4 lines
lib/librte_vhost/vhost.h +1 -0
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached;

What about naming it nr_cached_guest_pages to make it more self-explanatory as nr_cached is too generic?

Quoted 44 lines
lib/librte_vhost/vhost.h +13 -0
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -554,11 +556,23 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 	uint32_t i;
 	struct guest_page *page;
 
+	for (i = 0; i < dev->nr_cached; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}
+
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev->cached_guest_pages[dev->nr_cached],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
lib/librte_vhost/vhost_user.c +2 -0
index bd1be0104..573e99066 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	free(dev->guest_pages);
+	free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, · line 913

Quoted 8 lines
lib/librte_vhost/vhost_user.c +4 -1
@@ -905,7 +907,10 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		old_pages = dev->guest_pages;
 		dev->guest_pages = realloc(dev->guest_pages,
 					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->cached_guest_pages = realloc(dev->cached_guest_pages,
+					dev->max_guest_pages * sizeof(*page));
+		dev->nr_cached = 0;
+		if (!dev->guest_pages || !dev->cached_guest_pages) {

Better to compare pointer to NULL according to DPDK's coding style.

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, · line 1082

Quoted 7 lines
lib/librte_vhost/vhost_user.c +0 -0
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
 			free(old_pages);
 			return -1;
@@ -1075,6 +1080,18 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 		}
 	}
 

Do we need initialize dev->nr_cached to 0 explicitly here?

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, · line 1085

Quoted 3 lines
lib/librte_vhost/vhost_user.c +3 -0
+	if (!dev->cached_guest_pages) {
+		dev->cached_guest_pages = malloc(dev->max_guest_pages *
+						sizeof(struct guest_page));

I'm wondering why use malloc/realloc/free for cached_guest_pages instead of DPDK memory allocator APIs, I can see current code uses malloc/realloc/free for guest_pages, Is there any history reason I don't know?

Thanks,
Xiaolong

Quoted 15 lines
lib/librte_vhost/vhost_user.c +9 -0
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
 	if (dev->mem == NULL) {
Signature · 1 line
2.17.1
author
#2 lore raw

Thanks, xiaolong.

Quoted 26 lines

-----Original Message-----
From: Ye, Xiaolong <[email protected]>
Sent: Monday, March 16, 2020 9:48 PM
To: Liu, Yong <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [PATCH] vhost: cache guest/vhost physical address mapping

Hi, Marvin

Quoted 15 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, now performance is back.

lib/librte_vhost/vhost.h +0 -0
index 2087d1400..de2c09e7e 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
Quoted 6 lines

lib/librte_vhost/vhost.h struct virtio_net { · line 371

Quoted 2 lines
lib/librte_vhost/vhost.h +1 -0
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached;

What about naming it nr_cached_guest_pages to make it more self- explanatory
as nr_cached is too generic?

Agreed, naming is too generic. Will be changed in next version.

Quoted 57 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa,

Quoted 7 lines
lib/librte_vhost/vhost.h +1 -0
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -554,11 +556,23 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa,

uint64_t size)

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev,

Quoted 39 lines
lib/librte_vhost/vhost.h +12 -0
 	uint32_t i;
 	struct guest_page *page;
 
+	for (i = 0; i < dev->nr_cached; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}
+
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev->cached_guest_pages[dev-
 nr_cached],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
		}
lib/librte_vhost/vhost_user.c +2 -0
index bd1be0104..573e99066 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	free(dev->guest_pages);
+	free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -905,7 +907,10 @@ add_one_guest_page(struct virtio_net *dev,

uint64_t guest_phys_addr,

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,

Quoted 3 lines
lib/librte_vhost/vhost_user.c +0 -0
 		old_pages = dev->guest_pages;
 		dev->guest_pages = realloc(dev->guest_pages,
 					dev->max_guest_pages *

sizeof(*page));

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, · line 912

Quoted 4 lines
lib/librte_vhost/vhost_user.c +2 -1
-		if (!dev->guest_pages) {
+		dev->cached_guest_pages = realloc(dev-
 cached_guest_pages,
+					dev->max_guest_pages *

sizeof(*page));

Quoted 5 lines

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, · line 913

Quoted 2 lines
lib/librte_vhost/vhost_user.c +2 -0
+		dev->nr_cached = 0;
+		if (!dev->guest_pages || !dev->cached_guest_pages) {

Better to compare pointer to NULL according to DPDK's coding style.

OK, will change it.

Quoted 6 lines

lib/librte_vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, · line 915

Quoted 1 line
lib/librte_vhost/vhost_user.c +0 -0
 			VHOST_LOG_CONFIG(ERR, "cannot realloc

guest_pages\n");

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net

Quoted 3 lines
lib/librte_vhost/vhost_user.c +0 -0
 			free(old_pages);
			return -1;
lib/librte_vhost/vhost_user.c +0 -0
@@ -1075,6 +1080,18 @@ vhost_user_set_mem_table(struct virtio_net

**pdev, struct VhostUserMsg *msg,

Quoted 6 lines

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net · line 1082

Quoted 3 lines
lib/librte_vhost/vhost_user.c +0 -0
 		}
 	}
 

Do we need initialize dev->nr_cached to 0 explicitly here?

Structure vhost_virtqueue has been cleared in function init_vring_queue, it is not needed to do initialization in other place.

Quoted 10 lines

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, · line 1085

Quoted 3 lines
lib/librte_vhost/vhost_user.c +3 -0
+	if (!dev->cached_guest_pages) {
+		dev->cached_guest_pages = malloc(dev->max_guest_pages *
+						sizeof(struct guest_page));

I'm wondering why use malloc/realloc/free for cached_guest_pages instead of DPDK
memory allocator APIs, I can see current code uses malloc/realloc/free for guest_pages,
Is there any history reason I don't know?

I don't think there's specific reason to use glibc malloc/realloc functions.
History may be lost since code was added few years ago. I will changed these functions to dpdk API in next version.

Quoted 20 lines

Thanks,
Xiaolong

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,

Quoted 10 lines
lib/librte_vhost/vhost_user.c +9 -0
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net · line 1096

Quoted 1 line
lib/librte_vhost/vhost_user.c +0 -0
 		sizeof(struct rte_vhost_mem_region) * memory->nregions,

0);

Quoted 4 lines
lib/librte_vhost/vhost_user.c +0 -1
 	if (dev->mem == NULL) {
--

2.17.1

author
#3 lore raw

PATCH v2 1/2 vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v2 2/2 vhost: cache gpa to hpa translation

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +15 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+
+	for (i = 0; i < cached_pages; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}
 
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev->cached_guest_pages[cached_pages],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
lib/librte_vhost/vhost_user.c +28 -2
index 79fcb9d19..1bae1fddc 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages * sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev->cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
 	if (dev->mem == NULL) {
Signature · 1 line
2.17.1
#5 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 35 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +2 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa,
 uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, · line 568

Quoted 10 lines
lib/librte_vhost/vhost.h +10 -0
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+
+	for (i = 0; i < cached_pages; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}

Sorry, I did not see any speedup with cached guest pages in comparison to the old code below. Is it not a simple copy?
Is it a better idea to use hash instead to speed up the translation?
/Gavin

Quoted 88 lines
lib/librte_vhost/vhost.h +3 -0
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev-
 >cached_guest_pages[cached_pages],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
lib/librte_vhost/vhost.h +0 -0
 		}
lib/librte_vhost/vhost_user.c +3 -1
index 79fcb9d19..1bae1fddc 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
lib/librte_vhost/vhost_user.c +25 -1
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages *
 sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev-
 >cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc
 guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net
 **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +
sizeof(struct rte_vhost_mem_region) * memory->nregions,
0);
if (dev->mem == NULL) {
--
2.17.1

author
#6 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 55 lines

-----Original Message-----
From: Gavin Hu <[email protected]>
Sent: Wednesday, April 1, 2020 6:07 PM
To: Liu, Yong <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 42 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +9 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa,
 uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+
+	for (i = 0; i < cached_pages; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 567

Quoted 3 lines
lib/librte_vhost/vhost.h +3 -0
+				page->host_phys_addr;
+		}
+	}

Sorry, I did not see any speedup with cached guest pages in comparison to the old code below.
Is it not a simple copy?
Is it a better idea to use hash instead to speed up the translation? /Gavin

Hi Gavin,
Here just resort the overall mapping table according to using sequence.
Most likely virtio driver will reuse recently cycled buffers, thus search will find match in beginning. That is simple fix for performance enhancement. If use hash for index, it will take much more cost in normal case.

Regards,
Marvin

Quoted 88 lines
Quoted 88 lines
lib/librte_vhost/vhost.h +3 -0
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev-
 >cached_guest_pages[cached_pages],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
lib/librte_vhost/vhost.h +0 -0
 		}
lib/librte_vhost/vhost_user.c +3 -1
index 79fcb9d19..1bae1fddc 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
lib/librte_vhost/vhost_user.c +25 -1
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages *
 sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev-
 >cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc
 guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net
 **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +
sizeof(struct rte_vhost_mem_region) * memory->nregions,
0);
if (dev->mem == NULL) {
--
2.17.1

#7 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 54 lines

-----Original Message-----
From: Liu, Yong <[email protected]>
Sent: Wednesday, April 1, 2020 9:01 PM
To: Gavin Hu <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 34 lines

-----Original Message-----
From: Gavin Hu <[email protected]>
Sent: Wednesday, April 1, 2020 6:07 PM
To: Liu, Yong <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 23 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +0 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t

Quoted 8 lines
lib/librte_vhost/vhost.h +2 -0
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t

gpa,

Quoted 6 lines
Quoted 6 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 559

Quoted 6 lines
lib/librte_vhost/vhost.h +2 -0
 uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+

Add a comment here, something like "Firstly look up in the cached pages"?

Quoted 1 line
Quoted 1 line

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 560

Quoted 1 line
lib/librte_vhost/vhost.h +1 -0
+	for (i = 0; i < cached_pages; i++) {

Should the searching order reversed here to search the most recent entries?

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
lib/librte_vhost/vhost.h +4 -0
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 567

Quoted 3 lines
lib/librte_vhost/vhost.h +3 -0
+				page->host_phys_addr;
+		}
+	}

Sorry, I did not see any speedup with cached guest pages in comparison to the old code below.
Is it not a simple copy?
Is it a better idea to use hash instead to speed up the translation? /Gavin

Hi Gavin,
Here just resort the overall mapping table according to using sequence. Most likely virtio driver will reuse recently cycled buffers, thus search will find match in beginning.
That is simple fix for performance enhancement. If use hash for index, it will take much more cost in normal case.

Quoted 2 lines

Regards,
Marvin

There are issues here, the cached table is growing over time, will it become less efficient when growing too big and even bigger than the original table and overflow happen? Is it a good idea to limit the cached entries to small therefore make the search quicker?
/Gavin

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Add a comment here, something like "Fall back to normal lookup if failed to get from the cached table"?

Quoted 9 lines
Quoted 9 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 576

Quoted 9 lines
lib/librte_vhost/vhost.h +1 -0
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev-
Quoted 1 line

cached_guest_pages[cached_pages],

lib/librte_vhost/vhost.h +2 -0
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;

Will overflow happen over time when there are many translations?

Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
lib/librte_vhost/vhost.h +0 -0
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
lib/librte_vhost/vhost_user.c +3 -1
index 79fcb9d19..1bae1fddc 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
lib/librte_vhost/vhost_user.c +25 -1
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages *
 sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev-
 >cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc
 guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net
 **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +
sizeof(struct rte_vhost_mem_region) * memory->nregions,
0);
if (dev->mem == NULL) {
--
2.17.1

author
#8 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 107 lines

-----Original Message-----
From: Gavin Hu <[email protected]>
Sent: Thursday, April 2, 2020 11:05 AM
To: Liu, Yong <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 19 lines

-----Original Message-----
From: Liu, Yong <[email protected]>
Sent: Wednesday, April 1, 2020 9:01 PM
To: Gavin Hu <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 8 lines

-----Original Message-----
From: Gavin Hu <[email protected]>
Sent: Wednesday, April 1, 2020 6:07 PM
To: Liu, Yong <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa

translation

Quoted 11 lines
Quoted 11 lines

Hi Marvin,

Quoted 8 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using

2M

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +0 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t

Quoted 8 lines
lib/librte_vhost/vhost.h +2 -0
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t

gpa,

Quoted 6 lines
Quoted 6 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 559

Quoted 6 lines
lib/librte_vhost/vhost.h +2 -0
 uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+

Add a comment here, something like "Firstly look up in the cached pages"?

Quoted 1 line
Quoted 1 line

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 560

Quoted 1 line
lib/librte_vhost/vhost.h +1 -0
+	for (i = 0; i < cached_pages; i++) {

Should the searching order reversed here to search the most recent entries?

Quoted 8 lines
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 567

Quoted 7 lines
lib/librte_vhost/vhost.h +7 -0
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}

Sorry, I did not see any speedup with cached guest pages in comparison

to

Quoted 10 lines
Quoted 4 lines

the old code below.
Is it not a simple copy?
Is it a better idea to use hash instead to speed up the translation? /Gavin

Hi Gavin,
Here just resort the overall mapping table according to using sequence. Most likely virtio driver will reuse recently cycled buffers, thus search will find match in beginning.
That is simple fix for performance enhancement. If use hash for index, it

will

Quoted 4 lines

take much more cost in normal case.

Regards,
Marvin

Quoted 7 lines

There are issues here, the cached table is growing over time, will it become less efficient when growing too big and even bigger than the original table and overflow happen?
Is it a good idea to limit the cached entries to small therefore make the search quicker?
/Gavin

Quoted 1 line

Gavin,
Cached table size is the same as mapping table, it only recorded entries from original table which have been used.
At worst case like every access of guest memory is random, cached table will be same size of original size. Search cost is same as before.
It will be hard to predict which size is more suitable for caching, that is depend on how guest driver allocate buffer. Maybe less than ten when using 2M page and thousands when using 4K page. So here just add resorted table, which cost is much less in normal case and same as before at worst case.

Thanks,
Marvin

Quoted 97 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Add a comment here, something like "Fall back to normal lookup if failed to get from the cached table"?

Quoted 9 lines
Quoted 9 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 576

Quoted 9 lines
lib/librte_vhost/vhost.h +1 -0
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev-
Quoted 1 line

cached_guest_pages[cached_pages],

lib/librte_vhost/vhost.h +2 -0
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;

Will overflow happen over time when there are many translations?

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
lib/librte_vhost/vhost.h +0 -0
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
(quoted diff) +0 -0

b/lib/librte_vhost/vhost_user.c

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines

index 79fcb9d19..1bae1fddc 100644

lib/librte_vhost/vhost_user.c +3 -1
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
lib/librte_vhost/vhost_user.c +2 -1
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages *
 sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev-
 >cached_guest_pages,
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
lib/librte_vhost/vhost_user.c +6 -0
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc
 guest_pages\n");
Quoted 8 lines

lib/librte_vhost/vhost_user.c vhost_user_set_mem_table(struct

Quoted 8 lines
lib/librte_vhost/vhost_user.c +3 -0
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct

virtio_net

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
lib/librte_vhost/vhost_user.c +14 -0
 **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +
sizeof(struct rte_vhost_mem_region) * memory->nregions,
0);
if (dev->mem == NULL) {
--
2.17.1

Tested-by
#9 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

In reply to Liu, Yong

Regards,
Ma,lihong

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Liu, Yong
Sent: Wednesday, April 1, 2020 9:01 PM
To: Gavin Hu <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 54 lines

-----Original Message-----
From: Gavin Hu <[email protected]>
Sent: Wednesday, April 1, 2020 6:07 PM
To: Liu, Yong <[email protected]>; [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; nd <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 42 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +9 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t 
 gpa, uint64_t size)  {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+
+	for (i = 0; i < cached_pages; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
Quoted 8 lines

lib/librte_vhost/vhost.h gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) · line 567

Quoted 3 lines
lib/librte_vhost/vhost.h +3 -0
+				page->host_phys_addr;
+		}
+	}

Sorry, I did not see any speedup with cached guest pages in comparison to the old code below.
Is it not a simple copy?
Is it a better idea to use hash instead to speed up the translation? /Gavin

Hi Gavin,
Here just resort the overall mapping table according to using sequence.
Most likely virtio driver will reuse recently cycled buffers, thus search will find match in beginning. That is simple fix for performance enhancement. If use hash for index, it will take much more cost in normal case.

Regards,
Marvin

Quoted 85 lines
Quoted 85 lines
lib/librte_vhost/vhost.h +3 -0
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev-
 >cached_guest_pages[cached_pages],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
(quoted diff) +0 -0

b/lib/librte_vhost/vhost_user.c index 79fcb9d19..1bae1fddc 100644

lib/librte_vhost/vhost_user.c +2 -1
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size); @@ 
-898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t 

guest_phys_addr,

lib/librte_vhost/vhost_user.c +12 -2
 		   uint64_t host_phys_addr, uint64_t size)  {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev, 
 uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages *
 sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev-
 >cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
lib/librte_vhost/vhost_user.c +14 -0
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net 
 **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct

rte_vhost_memory) +

lib/librte_vhost/vhost_user.c +0 -1
 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
 	if (dev->mem == NULL) {
--

2.17.1

Reviewed-by on v2 2/2
#10 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

In reply to Marvin Liu
Quoted 116 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

lib/librte_vhost/vhost.h +15 -0
index 2087d1400..5cb0e83dd 100644
@@ -368,7 +368,9 @@ struct virtio_net {
 	struct vhost_device_ops const *notify_ops;
 
 	uint32_t		nr_guest_pages;
+	uint32_t		nr_cached_guest_pages;
 	uint32_t		max_guest_pages;
+	struct guest_page       *cached_guest_pages;
 	struct guest_page       *guest_pages;
 
 	int			slave_req_fd;
@@ -553,12 +555,25 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
+	uint32_t cached_pages = dev->nr_cached_guest_pages;
+
+	for (i = 0; i < cached_pages; i++) {
+		page = &dev->cached_guest_pages[i];
+		if (gpa >= page->guest_phys_addr &&
+			gpa + size < page->guest_phys_addr + page->size) {
+			return gpa - page->guest_phys_addr +
+				page->host_phys_addr;
+		}
+	}
 
 	for (i = 0; i < dev->nr_guest_pages; i++) {
 		page = &dev->guest_pages[i];
 
 		if (gpa >= page->guest_phys_addr &&
 		    gpa + size < page->guest_phys_addr + page->size) {
+			rte_memcpy(&dev->cached_guest_pages[cached_pages],
+				   page, sizeof(struct guest_page));
+			dev->nr_cached_guest_pages++;
 			return gpa - page->guest_phys_addr +
 			       page->host_phys_addr;
 		}
lib/librte_vhost/vhost_user.c +28 -2
index 79fcb9d19..1bae1fddc 100644
@@ -192,7 +192,9 @@ vhost_backend_cleanup(struct virtio_net *dev)
 	}
 
 	rte_free(dev->guest_pages);
+	rte_free(dev->cached_guest_pages);
 	dev->guest_pages = NULL;
+	dev->cached_guest_pages = NULL;
 
 	if (dev->log_addr) {
 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
@@ -898,7 +900,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		   uint64_t host_phys_addr, uint64_t size)
 {
 	struct guest_page *page, *last_page;
-	struct guest_page *old_pages;
+	struct guest_page *old_pages, *old_cached_pages;
 
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
@@ -906,9 +908,19 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 		dev->guest_pages = rte_realloc(dev->guest_pages,
 					dev->max_guest_pages * sizeof(*page),
 					RTE_CACHE_LINE_SIZE);
-		if (dev->guest_pages == NULL) {
+		old_cached_pages = dev->cached_guest_pages;
+		dev->cached_guest_pages = rte_realloc(dev->cached_guest_pages,
+						dev->max_guest_pages *
+						sizeof(*page),
+						RTE_CACHE_LINE_SIZE);
+		dev->nr_cached_guest_pages = 0;
+		if (dev->guest_pages == NULL ||
+				dev->cached_guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
 			rte_free(old_pages);
+			rte_free(old_cached_pages);
+			dev->guest_pages = NULL;
+			dev->cached_guest_pages = NULL;
 			return -1;
 		}
 	}
@@ -1078,6 +1090,20 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 		}
 	}
 
+	if (dev->cached_guest_pages == NULL) {
+		dev->cached_guest_pages = rte_zmalloc(NULL,
+						dev->max_guest_pages *
+						sizeof(struct guest_page),
+						RTE_CACHE_LINE_SIZE);
+		if (dev->cached_guest_pages == NULL) {
+			VHOST_LOG_CONFIG(ERR,
+				"(%d) failed to allocate memory "
+				"for dev->cached_guest_pages\n",
+				dev->vid);
+			return RTE_VHOST_MSG_RESULT_ERR;
+		}
+	}
+
 	dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
 	if (dev->mem == NULL) {
Signature · 1 line
2.17.1
#11 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

In reply to Marvin Liu

Hi Marvin,

Quoted 8 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

I don't like the approach, as I think it could have nasty effects. For example, the system is loaded normally and let's say 25% of the pages are used. Then we have a small spike, and buffers that were never used start to be used, it will cause writing new entries into the cache in the hot path when it is already overloaded. Wouldn't it increase the number of packets dropped?

At set_mem_table time, instead of adding the guest pages unsorted, maybe better to add them sorted there. Then you can use a better algorithm than linear searching (O(n)), like binary search (O(log n)).

Thanks,
Maxime

author
#12 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] vhost: cache gpa to hpa translation

Quoted 29 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Monday, April 27, 2020 4:45 PM
To: Liu, Yong <[email protected]>; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v2 2/2] vhost: cache gpa to hpa translation

Hi Marvin,

Quoted 8 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now add cached mapping table which will sorted by using sequence. Address translation will first check cached mapping table, then check unsorted mapping table if no match found.

I don't like the approach, as I think it could have nasty effects. For example, the system is loaded normally and let's say 25% of the pages are used. Then we have a small spike, and buffers that were never used start to be used, it will cause writing new entries into the cache in the hot path when it is already overloaded. Wouldn't it increase the number of packets dropped?

Quoted 4 lines

At set_mem_table time, instead of adding the guest pages unsorted, maybe better to add them sorted there. Then you can use a better algorithm than linear searching (O(n)), like binary search (O(log n)).

Maxime,
Thanks for input. Previous sorted way is according using sequence, it may cause more packets drop if accessing pages sequence varied a lot. Based on current dpdk and virtio-net implementation, it is unlikely to be happened. Anyway, it is not the best choice. I will use binary search replace current cache solution.

Regards,
Marvin

Quoted 3 lines

Thanks,
Maxime

Reviewed-by on v2 1/2
#14 lore raw

Re: [dpdk-dev] [PATCH v2 1/2] vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu
Quoted 53 lines · Marvin Liu

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
Tested-by on v2 1/2
#15 lore raw

Re: [dpdk-dev] [PATCH v2 1/2] vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu

Regards,
Ma,lihong

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Marvin Liu
Sent: Wednesday, April 1, 2020 10:50 PM
To: [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; Liu, Yong <[email protected]>
Subject: [dpdk-dev] [PATCH v2 1/2] vhost: utilize dpdk dynamic memory allocator

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "

--
2.17.1

author
#17 lore raw

PATCH v3 1/2 vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
author
#18 lore raw

PATCH v3 2/2 vhost: binary search address mapping table

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set threshold to 256 entries for linear search.

lib/librte_vhost/Makefile +1 -1
index e592795f2..8769afaad 100644
@@ -10,7 +10,7 @@ EXPORT_MAP := rte_vhost_version.map
 
 CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3
 CFLAGS += -I vhost_user
-CFLAGS += -fno-strict-aliasing
+CFLAGS += -fno-strict-aliasing -Wno-maybe-uninitialized
 LDLIBS += -lpthread
 
 ifeq ($(RTE_TOOLCHAIN), gcc)
lib/librte_vhost/vhost.h +34 -8

54 lines not on this page · raw message

lib/librte_vhost/vhost_user.c +6 -0
index 79fcb9d19..15e50d27d 100644
@@ -965,6 +965,12 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
 		reg_size -= size;
 	}
 
+	/* sort guest page array if over binary search threshold */
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
+			sizeof(struct guest_page), guest_page_addrcmp);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#19 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] vhost: binary search address mapping table

Quoted 49 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set threshold to 256 entries for linear search.

lib/librte_vhost/Makefile +1 -1
index e592795f2..8769afaad 100644
@@ -10,7 +10,7 @@ EXPORT_MAP := rte_vhost_version.map
 
 CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3
 CFLAGS += -I vhost_user
-CFLAGS += -fno-strict-aliasing
+CFLAGS += -fno-strict-aliasing -Wno-maybe-uninitialized
 LDLIBS += -lpthread
 
 ifeq ($(RTE_TOOLCHAIN), gcc)
lib/librte_vhost/vhost.h +14 -1
index 507dbf214..a0fee39d5 100644
@@ -546,20 +546,46 @@ extern int vhost_data_log_level;
 #define MAX_VHOST_DEVICE	1024
 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
+#define VHOST_BINARY_SEARCH_THRESH 256
+static int guest_page_addrcmp(const void *p1, const void *p2)
+{
+	const struct guest_page *page1 = (const struct guest_page *)p1;
+	const struct guest_page *page2 = (const struct guest_page *)p2;
+
+	if (page1->guest_phys_addr > page2->guest_phys_addr)
+		return 1;
+	if (page1->guest_phys_addr < page2->guest_phys_addr)
+		return -1;
+
+	return 0;
+}
+
 /* Convert guest physical address to host physical address */
 static __rte_always_inline rte_iova_t
 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
-

lib/librte_vhost/vhost.h extern int vhost_data_log_level; · line 571

Quoted 10 lines
lib/librte_vhost/vhost.h +3 -7
-	for (i = 0; i < dev->nr_guest_pages; i++) {
-		page = &dev->guest_pages[i];
-
-		if (gpa >= page->guest_phys_addr &&
-		    gpa + size < page->guest_phys_addr + page->size) {
-			return gpa - page->guest_phys_addr +
-			       page->host_phys_addr;
+	struct guest_page key;
+
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {

I would have expected the binary search to be more efficient for much smaller number of pages. Have you done some tests to define this threshold value?

lib/librte_vhost/vhost.h extern int vhost_data_log_level; · line 579

Quoted 8 lines
lib/librte_vhost/vhost.h +8 -0
+		key.guest_phys_addr = gpa;
+		page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
+			       sizeof(struct guest_page), guest_page_addrcmp);
+		if (page) {
+			if (gpa + size < page->guest_phys_addr + page->size)
+				return gpa - page->guest_phys_addr +
+					page->host_phys_addr;
+		}

Is all the generated code inlined?

I see that in the elf file:
2386: 0000000000874f70 16 FUNC LOCAL DEFAULT 13 guest_page_addrcmp

Quoted 30 lines
lib/librte_vhost/vhost.h +9 -0
+	} else {
+		for (i = 0; i < dev->nr_guest_pages; i++) {
+			page = &dev->guest_pages[i];
+
+			if (gpa >= page->guest_phys_addr &&
+			    gpa + size < page->guest_phys_addr +
+			    page->size)
+				return gpa - page->guest_phys_addr +
+				       page->host_phys_addr;
 		}
 	}
 
lib/librte_vhost/vhost_user.c +6 -0
index 79fcb9d19..15e50d27d 100644
@@ -965,6 +965,12 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
 		reg_size -= size;
 	}
 
+	/* sort guest page array if over binary search threshold */
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
+			sizeof(struct guest_page), guest_page_addrcmp);
+	}
+
 	return 0;
 }
 
author
#20 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] vhost: binary search address mapping table

Quoted 68 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Tuesday, April 28, 2020 11:28 PM
To: Liu, Yong <[email protected]>; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v3 2/2] vhost: binary search address mapping table

Quoted 55 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set threshold to 256 entries for linear search.

lib/librte_vhost/Makefile +1 -1
index e592795f2..8769afaad 100644
@@ -10,7 +10,7 @@ EXPORT_MAP := rte_vhost_version.map
 
 CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3
 CFLAGS += -I vhost_user
-CFLAGS += -fno-strict-aliasing
+CFLAGS += -fno-strict-aliasing -Wno-maybe-uninitialized
 LDLIBS += -lpthread
 
 ifeq ($(RTE_TOOLCHAIN), gcc)
lib/librte_vhost/vhost.h +14 -7
index 507dbf214..a0fee39d5 100644
@@ -546,20 +546,46 @@ extern int vhost_data_log_level;
 #define MAX_VHOST_DEVICE	1024
 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
+#define VHOST_BINARY_SEARCH_THRESH 256
+static int guest_page_addrcmp(const void *p1, const void *p2)
+{
+	const struct guest_page *page1 = (const struct guest_page *)p1;
+	const struct guest_page *page2 = (const struct guest_page *)p2;
+
+	if (page1->guest_phys_addr > page2->guest_phys_addr)
+		return 1;
+	if (page1->guest_phys_addr < page2->guest_phys_addr)
+		return -1;
+
+	return 0;
+}
+
 /* Convert guest physical address to host physical address */
 static __rte_always_inline rte_iova_t
 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
-
-	for (i = 0; i < dev->nr_guest_pages; i++) {
-		page = &dev->guest_pages[i];
-
-		if (gpa >= page->guest_phys_addr &&
-		    gpa + size < page->guest_phys_addr + page->size) {
-			return gpa - page->guest_phys_addr +
Quoted 9 lines

lib/librte_vhost/vhost.h extern int vhost_data_log_level; · line 571

Quoted 4 lines
lib/librte_vhost/vhost.h +3 -1
-			       page->host_phys_addr;
+	struct guest_page key;
+
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {

I would have expected the binary search to be more efficient for much smaller number of pages. Have you done some tests to define this threshold value?

Maxime,
In my unit test, binary search will be better when size over 16. But it won't be the case with real VM. I have tested around 128 to 1024 pages, the benefit can be seen around 256. So threshold is set to it.

Thanks,
Marvin

Quoted 12 lines

lib/librte_vhost/vhost.h extern int vhost_data_log_level; · line 579

Quoted 9 lines
lib/librte_vhost/vhost.h +8 -0
+		key.guest_phys_addr = gpa;
+		page = bsearch(&key, dev->guest_pages, dev-
 nr_guest_pages,
+			       sizeof(struct guest_page), guest_page_addrcmp);
+		if (page) {
+			if (gpa + size < page->guest_phys_addr + page->size)
+				return gpa - page->guest_phys_addr +
+					page->host_phys_addr;
+		}

Is all the generated code inlined?

The compare function hasn't been inlined. Will inline it in next version.

Quoted 35 lines

I see that in the elf file:
2386: 0000000000874f70 16 FUNC LOCAL DEFAULT 13
guest_page_addrcmp

lib/librte_vhost/vhost_user.c add_guest_pages(struct virtio_net *dev, struct

Quoted 17 lines
lib/librte_vhost/vhost.h +9 -0
+	} else {
+		for (i = 0; i < dev->nr_guest_pages; i++) {
+			page = &dev->guest_pages[i];
+
+			if (gpa >= page->guest_phys_addr &&
+			    gpa + size < page->guest_phys_addr +
+			    page->size)
+				return gpa - page->guest_phys_addr +
+				       page->host_phys_addr;
 		}
 	}
lib/librte_vhost/vhost_user.c +0 -0
index 79fcb9d19..15e50d27d 100644
@@ -965,6 +965,12 @@ add_guest_pages(struct virtio_net *dev, struct

rte_vhost_mem_region *reg,

Quoted 13 lines
lib/librte_vhost/vhost_user.c +6 -0
 		reg_size -= size;
 	}
 
+	/* sort guest page array if over binary search threshold */
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
+			sizeof(struct guest_page), guest_page_addrcmp);
+	}
+
 	return 0;
 }
 
author
#22 lore raw

PATCH v4 1/2 net/virtio: add support Virtio link speed feature

In reply to Marvin Liu

From: Ivan Dyukov <[email protected]>

This patch adds a support of VIRTIO_NET_F_SPEED_DUPLEX feature for virtio driver.

There are two ways to specify speed of the link:
'speed' devarg
negotiate speed from qemu via VIRTIO_NET_F_SPEED_DUPLEX
The highest priority is devarg. If devarg is not specified, drivers tries to negotiate it from qemu.

drivers/net/virtio/virtio_ethdev.c +23 -3
index 5058990d1..37766cbb6 100644
@@ -1662,7 +1662,8 @@ virtio_configure_intr(struct rte_eth_dev *dev)
 
 	return 0;
 }
-
+#define SPEED_UNKNOWN    0xffffffff
+#define DUPLEX_UNKNOWN   0xff
 /* reset device and renegotiate features if needed */
 static int
 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
@@ -1718,6 +1719,25 @@ virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
 
+	if (hw->speed == SPEED_UNKNOWN) {
+		if (vtpci_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
+			config = &local_config;
+			vtpci_read_dev_config(hw,
+				offsetof(struct virtio_net_config, speed),
+				&config->speed, sizeof(config->speed));
+			vtpci_read_dev_config(hw,
+				offsetof(struct virtio_net_config, duplex),
+				&config->duplex, sizeof(config->duplex));
+			hw->speed = config->speed;
+			hw->duplex = config->duplex;
+		}
+	}
+	if (hw->speed == SPEED_UNKNOWN)
+		hw->speed = ETH_SPEED_NUM_10G;
+	if (hw->duplex == DUPLEX_UNKNOWN)
+		hw->duplex = ETH_LINK_FULL_DUPLEX;
+	PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
+		hw->speed, hw->duplex);
 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
 		config = &local_config;
 
@@ -1865,7 +1885,7 @@ int
 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 {
 	struct virtio_hw *hw = eth_dev->data->dev_private;
-	uint32_t speed = ETH_SPEED_NUM_10G;
+	uint32_t speed = SPEED_UNKNOWN;
 	int ret;
 
 	if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
@@ -2450,7 +2470,7 @@ virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complet
 	struct virtio_hw *hw = dev->data->dev_private;
 
 	memset(&link, 0, sizeof(link));
-	link.link_duplex = ETH_LINK_FULL_DUPLEX;
+	link.link_duplex = hw->duplex;
 	link.link_speed  = hw->speed;
 	link.link_autoneg = ETH_LINK_AUTONEG;
 
drivers/net/virtio/virtio_ethdev.h +2 -1
index cd8947656..febaf17a8 100644
@@ -37,7 +37,8 @@
 	 1ULL << VIRTIO_F_RING_PACKED	  |	\
 	 1ULL << VIRTIO_F_IOMMU_PLATFORM  |	\
 	 1ULL << VIRTIO_F_ORDER_PLATFORM  |	\
-	 1ULL << VIRTIO_F_NOTIFICATION_DATA)
+	 1ULL << VIRTIO_F_NOTIFICATION_DATA | \
+	 1ULL << VIRTIO_NET_F_SPEED_DUPLEX)
 
 #define VIRTIO_PMD_SUPPORTED_GUEST_FEATURES	\
 	(VIRTIO_PMD_DEFAULT_GUEST_FEATURES |	\
drivers/net/virtio/virtio_pci.h +16 -0
index ed98e11c3..bd89357e4 100644
@@ -141,6 +141,9 @@ struct virtnet_ctl;
  */
 #define VIRTIO_F_NOTIFICATION_DATA 38
 
+/* Device set linkspeed and duplex */
+#define VIRTIO_NET_F_SPEED_DUPLEX 63
+
 /* The Guest publishes the used index for which it expects an interrupt
  * at the end of the avail ring. Host should ignore the avail->flags field. */
 /* The Host publishes the avail index for which it expects a kick
@@ -260,6 +263,7 @@ struct virtio_hw {
 	uint8_t     mac_addr[RTE_ETHER_ADDR_LEN];
 	uint32_t    notify_off_multiplier;
 	uint32_t    speed;  /* link speed in MB */
+	uint8_t     duplex;
 	uint8_t     *isr;
 	uint16_t    *notify_base;
 	struct virtio_pci_common_cfg *common_cfg;
@@ -306,6 +310,18 @@ struct virtio_net_config {
 	uint16_t   status;
 	uint16_t   max_virtqueue_pairs;
 	uint16_t   mtu;
+	/*
+	 * speed, in units of 1Mb. All values 0 to INT_MAX are legal.
+	 * Any other value stands for unknown.
+	 */
+	uint32_t speed;
+	/*
+	 * 0x00 - half duplex
+	 * 0x01 - full duplex
+	 * Any other value stands for unknown.
+	 */
+	uint8_t duplex;
+
 } __attribute__((packed));
 
 /*
Signature · 1 line
2.17.1
author
#23 lore raw

PATCH v4 1/2 vhost: utilize dpdk dynamic memory allocator

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
author
#24 lore raw

PATCH v4 2/2 vhost: binary search address mapping table

In reply to Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set the threshold to 256 entries for linear search.

lib/librte_vhost/vhost.h +36 -8
index 507dbf214..998e133ad 100644
@@ -546,20 +546,48 @@ extern int vhost_data_log_level;
 #define MAX_VHOST_DEVICE	1024
 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
+#define VHOST_BINARY_SEARCH_THRESH 256
+
+static __rte_always_inline int guest_page_addrcmp(const void *p1,
+						const void *p2)
+{
+	const struct guest_page *page1 = (const struct guest_page *)p1;
+	const struct guest_page *page2 = (const struct guest_page *)p2;
+
+	if (page1->guest_phys_addr > page2->guest_phys_addr)
+		return 1;
+	if (page1->guest_phys_addr < page2->guest_phys_addr)
+		return -1;
+
+	return 0;
+}
+
 /* Convert guest physical address to host physical address */
 static __rte_always_inline rte_iova_t
 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
-
-	for (i = 0; i < dev->nr_guest_pages; i++) {
-		page = &dev->guest_pages[i];
-
-		if (gpa >= page->guest_phys_addr &&
-		    gpa + size < page->guest_phys_addr + page->size) {
-			return gpa - page->guest_phys_addr +
-			       page->host_phys_addr;
+	struct guest_page key;
+
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		key.guest_phys_addr = gpa;
+		page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
+			       sizeof(struct guest_page), guest_page_addrcmp);
+		if (page) {
+			if (gpa + size < page->guest_phys_addr + page->size)
+				return gpa - page->guest_phys_addr +
+					page->host_phys_addr;
+		}
+	} else {
+		for (i = 0; i < dev->nr_guest_pages; i++) {
+			page = &dev->guest_pages[i];
+
+			if (gpa >= page->guest_phys_addr &&
+			    gpa + size < page->guest_phys_addr +
+			    page->size)
+				return gpa - page->guest_phys_addr +
+				       page->host_phys_addr;
 		}
 	}
 
lib/librte_vhost/vhost_user.c +6 -0
index 79fcb9d19..15e50d27d 100644
@@ -965,6 +965,12 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
 		reg_size -= size;
 	}
 
+	/* sort guest page array if over binary search threshold */
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
+			sizeof(struct guest_page), guest_page_addrcmp);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#25 lore raw

PATCH v4 2/2 vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
author
#26 lore raw

Re: [dpdk-dev] [PATCH v4 2/2] vhost: utilize dpdk dynamic memory allocator

This is sent by mistake, please ignore.

Quoted 66 lines

-----Original Message-----
From: Liu, Yong <[email protected]>
Sent: Wednesday, April 29, 2020 9:01 AM
To: [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; Liu, Yong <[email protected]>
Subject: [PATCH v4 2/2] vhost: utilize dpdk dynamic memory allocator

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev,
 uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages *
 sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages *
 sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc
 guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net
 **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,

"(%d) failed to allocate memory "
--
2.17.1

#27 lore raw

Re: [dpdk-dev] [PATCH v4 2/2] vhost: utilize dpdk dynamic memory allocator

Quoted 15 lines · Yong

This is sent by mistake, please ignore.

Quoted 13 lines

-----Original Message-----
From: Liu, Yong <[email protected]>
Sent: Wednesday, April 29, 2020 9:01 AM
To: [email protected]; Ye, Xiaolong <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]; Liu, Yong <[email protected]>
Subject: [PATCH v4 2/2] vhost: utilize dpdk dynamic memory allocator

Replace dynamic memory allocator with dpdk memory allocator.

Applied to dpdk-next-virtio/master.

Thanks,
Maxime

author
#28 lore raw

PATCH v4 1/2 vhost: utilize dpdk dynamic memory allocator

In reply to Marvin Liu

Replace dynamic memory allocator with dpdk memory allocator.

lib/librte_vhost/vhost_user.c +11 -8
index bd1be0104..79fcb9d19 100644
@@ -191,7 +191,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		dev->mem = NULL;
 	}
 
-	free(dev->guest_pages);
+	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
 
 	if (dev->log_addr) {
@@ -903,11 +903,12 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
 	if (dev->nr_guest_pages == dev->max_guest_pages) {
 		dev->max_guest_pages *= 2;
 		old_pages = dev->guest_pages;
-		dev->guest_pages = realloc(dev->guest_pages,
-					dev->max_guest_pages * sizeof(*page));
-		if (!dev->guest_pages) {
+		dev->guest_pages = rte_realloc(dev->guest_pages,
+					dev->max_guest_pages * sizeof(*page),
+					RTE_CACHE_LINE_SIZE);
+		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
-			free(old_pages);
+			rte_free(old_pages);
 			return -1;
 		}
 	}
@@ -1062,10 +1063,12 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
 	dev->nr_guest_pages = 0;
-	if (!dev->guest_pages) {
+	if (dev->guest_pages == NULL) {
 		dev->max_guest_pages = 8;
-		dev->guest_pages = malloc(dev->max_guest_pages *
-						sizeof(struct guest_page));
+		dev->guest_pages = rte_zmalloc(NULL,
+					dev->max_guest_pages *
+					sizeof(struct guest_page),
+					RTE_CACHE_LINE_SIZE);
 		if (dev->guest_pages == NULL) {
 			VHOST_LOG_CONFIG(ERR,
 				"(%d) failed to allocate memory "
Signature · 1 line
2.17.1
author
#29 lore raw

PATCH v4 2/2 vhost: binary search address mapping table

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set the threshold to 256 entries for linear search.

lib/librte_vhost/vhost.h +36 -8
index 507dbf214..998e133ad 100644
@@ -546,20 +546,48 @@ extern int vhost_data_log_level;
 #define MAX_VHOST_DEVICE	1024
 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
+#define VHOST_BINARY_SEARCH_THRESH 256
+
+static __rte_always_inline int guest_page_addrcmp(const void *p1,
+						const void *p2)
+{
+	const struct guest_page *page1 = (const struct guest_page *)p1;
+	const struct guest_page *page2 = (const struct guest_page *)p2;
+
+	if (page1->guest_phys_addr > page2->guest_phys_addr)
+		return 1;
+	if (page1->guest_phys_addr < page2->guest_phys_addr)
+		return -1;
+
+	return 0;
+}
+
 /* Convert guest physical address to host physical address */
 static __rte_always_inline rte_iova_t
 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 {
 	uint32_t i;
 	struct guest_page *page;
-
-	for (i = 0; i < dev->nr_guest_pages; i++) {
-		page = &dev->guest_pages[i];
-
-		if (gpa >= page->guest_phys_addr &&
-		    gpa + size < page->guest_phys_addr + page->size) {
-			return gpa - page->guest_phys_addr +
-			       page->host_phys_addr;
+	struct guest_page key;
+
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		key.guest_phys_addr = gpa;
+		page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
+			       sizeof(struct guest_page), guest_page_addrcmp);
+		if (page) {
+			if (gpa + size < page->guest_phys_addr + page->size)
+				return gpa - page->guest_phys_addr +
+					page->host_phys_addr;
+		}
+	} else {
+		for (i = 0; i < dev->nr_guest_pages; i++) {
+			page = &dev->guest_pages[i];
+
+			if (gpa >= page->guest_phys_addr &&
+			    gpa + size < page->guest_phys_addr +
+			    page->size)
+				return gpa - page->guest_phys_addr +
+				       page->host_phys_addr;
 		}
 	}
 
lib/librte_vhost/vhost_user.c +6 -0
index 79fcb9d19..15e50d27d 100644
@@ -965,6 +965,12 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
 		reg_size -= size;
 	}
 
+	/* sort guest page array if over binary search threshold */
+	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
+		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
+			sizeof(struct guest_page), guest_page_addrcmp);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
Reviewed-by on v4 2/2
#30 lore raw

Re: [dpdk-dev] [PATCH v4 2/2] vhost: binary search address mapping table

Quoted 6 lines · Marvin Liu

If Tx zero copy enabled, gpa to hpa mapping table is updated one by one. This will harm performance when guest memory backend using 2M hugepages. Now utilize binary search to find the entry in mapping table, meanwhile set the threshold to 256 entries for linear search.

Thanks,
Maxime