Skip to contentSkip to conversation

vhost: ring format independence

RFC PATCH Superseded Michael S. Tsirkin 2 participants 29 messages Oct 11, 2019 – Oct 16, 2019

Subsystems (5)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

cover vhost: ring format independence

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

 drivers/vhost/test.c  |  19 ++-
 drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |  20 ++-
 3 files changed, 365 insertions(+), 7 deletions(-)
Signature · 1 line
MST
author maintainer
#1 lore raw

RFC PATCH 1/2 vhost: option to fetch descriptors through an independent struct

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
 drivers/vhost/test.c  |  17 ++-
 drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |  16 +++
 3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
 #include "test.h"
 #include "vhost.h"
 
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
 /* Max number of bytes transferred before requeueing the job.
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
 	vhost_disable_notify(&n->dev, vq);
 
 	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
 		/* On error, stop handling until the next kick. */
 		if (unlikely(head < 0))
 			break;
drivers/vhost/vhost.c +298 -1

329 lines not on this page · raw message

drivers/vhost/vhost.h +16 -0
index e9ed2722b633..1724f61b6c2d 100644
@@ -80,6 +80,13 @@ enum vhost_uaddr_type {
 	VHOST_NUM_ADDRS = 3,
 };
 
+struct vhost_desc {
+	u64 addr;
+	u32 len;
+	u16 flags; /* VRING_DESC_F_WRITE, VRING_DESC_F_NEXT */
+	u16 id;
+};
+
 /* The virtqueue structure describes a queue attached to a device. */
 struct vhost_virtqueue {
 	struct vhost_dev *dev;
@@ -90,6 +97,11 @@ struct vhost_virtqueue {
 	struct vring_desc __user *desc;
 	struct vring_avail __user *avail;
 	struct vring_used __user *used;
+
+	struct vhost_desc *descs;
+	int ndescs;
+	int max_descs;
+
 	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
 	struct file *kick;
 	struct eventfd_ctx *call_ctx;
@@ -190,6 +202,10 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
 bool vhost_log_access_ok(struct vhost_dev *);
 
+int vhost_get_vq_desc_batch(struct vhost_virtqueue *,
+		      struct iovec iov[], unsigned int iov_count,
+		      unsigned int *out_num, unsigned int *in_num,
+		      struct vhost_log *log, unsigned int *log_num);
 int vhost_get_vq_desc(struct vhost_virtqueue *,
 		      struct iovec iov[], unsigned int iov_count,
 		      unsigned int *out_num, unsigned int *in_num,
Signature · 1 line
MST
author maintainer
#2 lore raw

RFC PATCH 1/2 vhost: option to fetch descriptors through an independent struct

In reply to Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
 drivers/vhost/test.c  |  17 ++-
 drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |  16 +++
 3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
 #include "test.h"
 #include "vhost.h"
 
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
 /* Max number of bytes transferred before requeueing the job.
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
 	vhost_disable_notify(&n->dev, vq);
 
 	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
 		/* On error, stop handling until the next kick. */
 		if (unlikely(head < 0))
 			break;
drivers/vhost/vhost.c +298 -1

329 lines not on this page · raw message

drivers/vhost/vhost.h +16 -0
index e9ed2722b633..1724f61b6c2d 100644
@@ -80,6 +80,13 @@ enum vhost_uaddr_type {
 	VHOST_NUM_ADDRS = 3,
 };
 
+struct vhost_desc {
+	u64 addr;
+	u32 len;
+	u16 flags; /* VRING_DESC_F_WRITE, VRING_DESC_F_NEXT */
+	u16 id;
+};
+
 /* The virtqueue structure describes a queue attached to a device. */
 struct vhost_virtqueue {
 	struct vhost_dev *dev;
@@ -90,6 +97,11 @@ struct vhost_virtqueue {
 	struct vring_desc __user *desc;
 	struct vring_avail __user *avail;
 	struct vring_used __user *used;
+
+	struct vhost_desc *descs;
+	int ndescs;
+	int max_descs;
+
 	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
 	struct file *kick;
 	struct eventfd_ctx *call_ctx;
@@ -190,6 +202,10 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
 bool vhost_log_access_ok(struct vhost_dev *);
 
+int vhost_get_vq_desc_batch(struct vhost_virtqueue *,
+		      struct iovec iov[], unsigned int iov_count,
+		      unsigned int *out_num, unsigned int *in_num,
+		      struct vhost_log *log, unsigned int *log_num);
 int vhost_get_vq_desc(struct vhost_virtqueue *,
 		      struct iovec iov[], unsigned int iov_count,
 		      unsigned int *out_num, unsigned int *in_num,
Signature · 1 line
MST
#3 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

Quoted 81 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
  drivers/vhost/test.c  |  17 ++-
  drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  16 +++
  3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
  #include "test.h"
  #include "vhost.h"
 
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
  /* Max number of bytes transferred before requeueing the job.
   * Using this limit prevents one virtqueue from starving others. */
  #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
  	vhost_disable_notify(&n->dev, vq);
 
  	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
  		/* On error, stop handling until the next kick. */
  		if (unlikely(head < 0))
  			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  			   struct vhost_virtqueue *vq)
  {
  	vq->num = 1;
+	vq->ndescs = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
 
  static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
  	kfree(vq->indirect);
  	vq->indirect = NULL;
  	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 395

Quoted 7 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
 
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

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

#4 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

In reply to Michael S. Tsirkin
Quoted 81 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
  drivers/vhost/test.c  |  17 ++-
  drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  16 +++
  3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
  #include "test.h"
  #include "vhost.h"
 
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
  /* Max number of bytes transferred before requeueing the job.
   * Using this limit prevents one virtqueue from starving others. */
  #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
  	vhost_disable_notify(&n->dev, vq);
 
  	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
  		/* On error, stop handling until the next kick. */
  		if (unlikely(head < 0))
  			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  			   struct vhost_virtqueue *vq)
  {
  	vq->num = 1;
+	vq->ndescs = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
 
  static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
  	kfree(vq->indirect);
  	vq->indirect = NULL;
  	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 395

Quoted 7 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
 
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Thanks

author maintainer
#5 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

Quoted 92 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
  drivers/vhost/test.c  |  17 ++-
  drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  16 +++
  3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
  #include "test.h"
  #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
  /* Max number of bytes transferred before requeueing the job.
   * Using this limit prevents one virtqueue from starving others. */
  #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
  	vhost_disable_notify(&n->dev, vq);
  	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
  		/* On error, stop handling until the next kick. */
  		if (unlikely(head < 0))
  			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  			   struct vhost_virtqueue *vq)
  {
  	vq->num = 1;
+	vq->ndescs = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
  static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
  	kfree(vq->indirect);
  	vq->indirect = NULL;
  	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

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

author maintainer
#6 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

In reply to Jason Wang
Quoted 92 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
  drivers/vhost/test.c  |  17 ++-
  drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  16 +++
  3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
  #include "test.h"
  #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
  /* Max number of bytes transferred before requeueing the job.
   * Using this limit prevents one virtqueue from starving others. */
  #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
  	vhost_disable_notify(&n->dev, vq);
  	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
  		/* On error, stop handling until the next kick. */
  		if (unlikely(head < 0))
  			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  			   struct vhost_virtqueue *vq)
  {
  	vq->num = 1;
+	vq->ndescs = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
  static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
  	kfree(vq->indirect);
  	vq->indirect = NULL;
  	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

#7 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

Quoted 91 lines · Michael S. Tsirkin
Quoted 90 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
   drivers/vhost/test.c  |  17 ++-
   drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
   drivers/vhost/vhost.h |  16 +++
   3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
   #include "test.h"
   #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
   /* Max number of bytes transferred before requeueing the job.
    * Using this limit prevents one virtqueue from starving others. */
   #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
   	vhost_disable_notify(&n->dev, vq);
   	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
   		/* On error, stop handling until the next kick. */
   		if (unlikely(head < 0))
   			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   			   struct vhost_virtqueue *vq)
   {
   	vq->num = 1;
+	vq->ndescs = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
   static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
   {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
   	kfree(vq->indirect);
   	vq->indirect = NULL;
   	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Or we may need circular index (head, tail) in this case?

Thanks

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

#8 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

In reply to Michael S. Tsirkin
Quoted 91 lines · Michael S. Tsirkin
Quoted 90 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
   drivers/vhost/test.c  |  17 ++-
   drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
   drivers/vhost/vhost.h |  16 +++
   3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
   #include "test.h"
   #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
   /* Max number of bytes transferred before requeueing the job.
    * Using this limit prevents one virtqueue from starving others. */
   #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
   	vhost_disable_notify(&n->dev, vq);
   	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
   		/* On error, stop handling until the next kick. */
   		if (unlikely(head < 0))
   			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   			   struct vhost_virtqueue *vq)
   {
   	vq->num = 1;
+	vq->ndescs = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
   static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
   {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
   	kfree(vq->indirect);
   	vq->indirect = NULL;
   	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Or we may need circular index (head, tail) in this case?

Thanks

author maintainer
#9 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

Quoted 102 lines · Jason Wang
Quoted 91 lines · Michael S. Tsirkin
Quoted 90 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
   drivers/vhost/test.c  |  17 ++-
   drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
   drivers/vhost/vhost.h |  16 +++
   3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
   #include "test.h"
   #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
   /* Max number of bytes transferred before requeueing the job.
    * Using this limit prevents one virtqueue from starving others. */
   #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
   	vhost_disable_notify(&n->dev, vq);
   	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
   		/* On error, stop handling until the next kick. */
   		if (unlikely(head < 0))
   			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   			   struct vhost_virtqueue *vq)
   {
   	vq->num = 1;
+	vq->ndescs = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
   static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
   {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
   	kfree(vq->indirect);
   	vq->indirect = NULL;
   	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Quoted 7 lines

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Or we may need circular index (head, tail) in this case?

Thanks

We never supported more than IOV_MAX descriptors.
And we don't batch more than iov_limit - IOV_MAX.

so buffer never overflows.

Signature · 5 lines
MST
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#10 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

In reply to Jason Wang
Quoted 102 lines · Jason Wang
Quoted 91 lines · Michael S. Tsirkin
Quoted 90 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
   drivers/vhost/test.c  |  17 ++-
   drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
   drivers/vhost/vhost.h |  16 +++
   3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
   #include "test.h"
   #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
   /* Max number of bytes transferred before requeueing the job.
    * Using this limit prevents one virtqueue from starving others. */
   #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
   	vhost_disable_notify(&n->dev, vq);
   	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
   		/* On error, stop handling until the next kick. */
   		if (unlikely(head < 0))
   			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   			   struct vhost_virtqueue *vq)
   {
   	vq->num = 1;
+	vq->ndescs = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
   static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
   {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
   	kfree(vq->indirect);
   	vq->indirect = NULL;
   	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Quoted 7 lines

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Or we may need circular index (head, tail) in this case?

Thanks

We never supported more than IOV_MAX descriptors.
And we don't batch more than iov_limit - IOV_MAX.

so buffer never overflows.

Signature · 1 line
MST
#11 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Jason Wang
Quoted 90 lines · Michael S. Tsirkin
Quoted 89 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
    drivers/vhost/test.c  |  17 ++-
    drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
    drivers/vhost/vhost.h |  16 +++
    3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
    #include "test.h"
    #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
    /* Max number of bytes transferred before requeueing the job.
     * Using this limit prevents one virtqueue from starving others. */
    #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
    	vhost_disable_notify(&n->dev, vq);
    	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
    		/* On error, stop handling until the next kick. */
    		if (unlikely(head < 0))
    			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
    			   struct vhost_virtqueue *vq)
    {
    	vq->num = 1;
+	vq->ndescs = 0;
    	vq->desc = NULL;
    	vq->avail = NULL;
    	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
    static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
    {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
    	kfree(vq->indirect);
    	vq->indirect = NULL;
    	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
    	for (i = 0; i < dev->nvqs; ++i) {
    		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Quoted 5 lines
Quoted 3 lines

Or we may need circular index (head, tail) in this case?

Thanks

We never supported more than IOV_MAX descriptors.
And we don't batch more than iov_limit - IOV_MAX.

Ok, but what happens when we've already batched 63 descriptors then come a 3 descriptor chain?

And it looks to me we need forget the cached descriptor during set_vring_base()

Thanks

Quoted 3 lines

so buffer never overflows.

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

#12 lore raw

Re: [PATCH RFC v1 1/2] vhost: option to fetch descriptors through an independent struct

In reply to Michael S. Tsirkin
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Jason Wang
Quoted 90 lines · Michael S. Tsirkin
Quoted 89 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin

The idea is to support multiple ring formats by converting to a format-independent array of descriptors.

This costs extra cycles, but we gain in ability to fetch a batch of descriptors in one go, which is good for code cache locality.

To simplify benchmarking, I kept the old code
around so one can switch back and forth by
writing into a module parameter.
This will go away in the final submission.

This patch causes a minor performance degradation, it's been kept as simple as possible for ease of review. Next patch gets us back the performance by adding batching.

---
    drivers/vhost/test.c  |  17 ++-
    drivers/vhost/vhost.c | 299 +++++++++++++++++++++++++++++++++++++++++-
    drivers/vhost/vhost.h |  16 +++
    3 files changed, 327 insertions(+), 5 deletions(-)
drivers/vhost/test.c +13 -4
index 056308008288..39a018a7af2d 100644
@@ -18,6 +18,9 @@
    #include "test.h"
    #include "vhost.h"
+static int newcode = 0;
+module_param(newcode, int, 0644);
+
    /* Max number of bytes transferred before requeueing the job.
     * Using this limit prevents one virtqueue from starving others. */
    #define VHOST_TEST_WEIGHT 0x80000
@@ -58,10 +61,16 @@ static void handle_vq(struct vhost_test *n)
    	vhost_disable_notify(&n->dev, vq);
    	for (;;) {
-		head = vhost_get_vq_desc(vq, vq->iov,
-					 ARRAY_SIZE(vq->iov),
-					 &out, &in,
-					 NULL, NULL);
+		if (newcode)
+			head = vhost_get_vq_desc_batch(vq, vq->iov,
+						       ARRAY_SIZE(vq->iov),
+						       &out, &in,
+						       NULL, NULL);
+		else
+			head = vhost_get_vq_desc(vq, vq->iov,
+						 ARRAY_SIZE(vq->iov),
+						 &out, &in,
+						 NULL, NULL);
    		/* On error, stop handling until the next kick. */
    		if (unlikely(head < 0))
    			break;
drivers/vhost/vhost.c +4 -0
index 36ca2cf419bf..36661d6cb51f 100644
@@ -301,6 +301,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
    			   struct vhost_virtqueue *vq)
    {
    	vq->num = 1;
+	vq->ndescs = 0;
    	vq->desc = NULL;
    	vq->avail = NULL;
    	vq->used = NULL;
@@ -369,6 +370,9 @@ static int vhost_worker(void *data)
    static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
    {
+	kfree(vq->descs);
+	vq->descs = NULL;
+	vq->max_descs = 0;
    	kfree(vq->indirect);
    	vq->indirect = NULL;
    	kfree(vq->log);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 394

Quoted 6 lines
drivers/vhost/vhost.c +4 -0
@@ -385,6 +389,10 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
    	for (i = 0; i < dev->nvqs; ++i) {
    		vq = dev->vqs[i];
+		vq->max_descs = dev->iov_limit;
+		vq->descs = kmalloc_array(vq->max_descs,
+					  sizeof(*vq->descs),
+					  GFP_KERNEL);

Is iov_limit too much here? It can obviously increase the footprint. I guess the batching can only be done for descriptor without indirect or next set. Then we may batch 16 or 64.

Quoted 8 lines
Quoted 1 line

Thanks

Yes, next patch only batches up to 64. But we do need iov_limit because guest can pass a long chain of scatter/gather.
We already have iovecs in a huge array so this does not look like a big deal. If we ever teach the code to avoid the huge
iov arrays by handling huge s/g lists piece by piece,
we can make the desc array smaller at the same point.

Another possible issue, if we try to batch descriptor chain when we've already batched some descriptors, we may reach the limit then some of the descriptors might need re-read.

Quoted 5 lines
Quoted 3 lines

Or we may need circular index (head, tail) in this case?

Thanks

We never supported more than IOV_MAX descriptors.
And we don't batch more than iov_limit - IOV_MAX.

Ok, but what happens when we've already batched 63 descriptors then come a 3 descriptor chain?

And it looks to me we need forget the cached descriptor during set_vring_base()

Thanks

Quoted 3 lines

so buffer never overflows.

author maintainer
#13 lore raw

RFC PATCH 2/2 vhost: batching fetches

In reply to Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
 drivers/vhost/test.c  |  2 +-
 drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
 drivers/vhost/vhost.h |  4 +++-
 3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
 	dev = &n->dev;
 	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
 	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
 		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
 
 	f->private_data = n;
drivers/vhost/vhost.c +42 -8

120 lines not on this page · raw message

drivers/vhost/vhost.h +3 -1
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
 
 	struct vhost_desc *descs;
 	int ndescs;
+	int first_desc;
 	int max_descs;
+	int batch_descs;
 
 	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
 	struct file *kick;
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
 
 #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
 		if ((vq)->error_ctx)                               \
 				eventfd_signal((vq)->error_ctx, 1);\
 	} while (0)
Signature · 1 line
MST
author maintainer
#14 lore raw

RFC PATCH 2/2 vhost: batching fetches

In reply to Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
 drivers/vhost/test.c  |  2 +-
 drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
 drivers/vhost/vhost.h |  4 +++-
 3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
 	dev = &n->dev;
 	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
 	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
 		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
 
 	f->private_data = n;
drivers/vhost/vhost.c +42 -8

120 lines not on this page · raw message

drivers/vhost/vhost.h +3 -1
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
 
 	struct vhost_desc *descs;
 	int ndescs;
+	int first_desc;
 	int max_descs;
+	int batch_descs;
 
 	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
 	struct file *kick;
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
 
 #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
 		if ((vq)->error_ctx)                               \
 				eventfd_signal((vq)->error_ctx, 1);\
 	} while (0)
Signature · 1 line
MST
#15 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

Quoted 94 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
  drivers/vhost/test.c  |  2 +-
  drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
  drivers/vhost/vhost.h |  4 +++-
  3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
  	dev = &n->dev;
  	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
  		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
 
  	f->private_data = n;
drivers/vhost/vhost.c +15 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  {
  	vq->num = 1;
  	vq->ndescs = 0;
+	vq->first_desc = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
  		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
  		vq->descs = kmalloc_array(vq->max_descs,
  					  sizeof(*vq->descs),
  					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
  	--vq->ndescs;
  }
 
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
  static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
  {
  	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
  	h = &vq->descs[vq->ndescs++];
  	h->addr = vhost64_to_cpu(vq, desc->addr);
  	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
  	h->id = id;
 
  	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
  	return 0;
  }
 
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
  {
  	struct vring_desc desc;
  	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	/* Check it isn't doing very strange things with descriptor numbers. */
  	last_avail_idx = vq->last_avail_idx;
 
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
  		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
  			vq_err(vq, "Failed to access avail idx at %p\n",
  				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	return 0;
  }
 
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
+
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

Quoted 32 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
  /* This looks in the virtqueue and for the first available buffer, and converts
   * it to an iovec for convenient access.  Since descriptors consist of some
   * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (ret)
  		return ret;
 
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
  	last = peek_split_desc(vq);
  	id = last->id;
 
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (unlikely(log))
  		*log_num = 0;
 
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
  		unsigned iov_count = *in_num + *out_num;
  		struct vhost_desc *desc = &vq->descs[i];
  		int access;
 
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
  			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
  			       desc->flags, desc->id);
  			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  			}
  			*out_num += ret;
  		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
  	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Thanks

Quoted 30 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
 
  	return ret;
 
  err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
  	vq->ndescs = 0;
 
  	return ret;
drivers/vhost/vhost.h +2 -0
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
 
  	struct vhost_desc *descs;
  	int ndescs;
+	int first_desc;
  	int max_descs;
+	int batch_descs;
 
  	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
  	struct file *kick;

drivers/vhost/vhost.h ssize_t vhost_chr_write_iter(struct vhost_dev *dev, · line 253

Quoted 8 lines
drivers/vhost/vhost.h +1 -1
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
 
  #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
  		if ((vq)->error_ctx)                               \
  				eventfd_signal((vq)->error_ctx, 1);\
  	} while (0)

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

#16 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

In reply to Michael S. Tsirkin
Quoted 94 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
  drivers/vhost/test.c  |  2 +-
  drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
  drivers/vhost/vhost.h |  4 +++-
  3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
  	dev = &n->dev;
  	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
  		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
 
  	f->private_data = n;
drivers/vhost/vhost.c +15 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  {
  	vq->num = 1;
  	vq->ndescs = 0;
+	vq->first_desc = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
  		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
  		vq->descs = kmalloc_array(vq->max_descs,
  					  sizeof(*vq->descs),
  					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
  	--vq->ndescs;
  }
 
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
  static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
  {
  	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
  	h = &vq->descs[vq->ndescs++];
  	h->addr = vhost64_to_cpu(vq, desc->addr);
  	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
  	h->id = id;
 
  	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
  	return 0;
  }
 
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
  {
  	struct vring_desc desc;
  	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	/* Check it isn't doing very strange things with descriptor numbers. */
  	last_avail_idx = vq->last_avail_idx;
 
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
  		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
  			vq_err(vq, "Failed to access avail idx at %p\n",
  				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	return 0;
  }
 
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
+
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

Quoted 32 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
  /* This looks in the virtqueue and for the first available buffer, and converts
   * it to an iovec for convenient access.  Since descriptors consist of some
   * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (ret)
  		return ret;
 
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
  	last = peek_split_desc(vq);
  	id = last->id;
 
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (unlikely(log))
  		*log_num = 0;
 
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
  		unsigned iov_count = *in_num + *out_num;
  		struct vhost_desc *desc = &vq->descs[i];
  		int access;
 
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
  			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
  			       desc->flags, desc->id);
  			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  			}
  			*out_num += ret;
  		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
  	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Thanks

Quoted 38 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
 
  	return ret;
 
  err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
  	vq->ndescs = 0;
 
  	return ret;
drivers/vhost/vhost.h +3 -1
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
 
  	struct vhost_desc *descs;
  	int ndescs;
+	int first_desc;
  	int max_descs;
+	int batch_descs;
 
  	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
  	struct file *kick;
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
 
  #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
  		if ((vq)->error_ctx)                               \
  				eventfd_signal((vq)->error_ctx, 1);\
  	} while (0)
author maintainer
#17 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

Quoted 95 lines · Jason Wang
Quoted 93 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
  drivers/vhost/test.c  |  2 +-
  drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
  drivers/vhost/vhost.h |  4 +++-
  3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
  	dev = &n->dev;
  	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
  		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
  	f->private_data = n;
drivers/vhost/vhost.c +20 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  {
  	vq->num = 1;
  	vq->ndescs = 0;
+	vq->first_desc = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
  		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
  		vq->descs = kmalloc_array(vq->max_descs,
  					  sizeof(*vq->descs),
  					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
  	--vq->ndescs;
  }
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
  static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
  {
  	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
  	h = &vq->descs[vq->ndescs++];
  	h->addr = vhost64_to_cpu(vq, desc->addr);
  	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
  	h->id = id;
  	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
  	return 0;
  }
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
  {
  	struct vring_desc desc;
  	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	/* Check it isn't doing very strange things with descriptor numbers. */
  	last_avail_idx = vq->last_avail_idx;
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
  		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
  			vq_err(vq, "Failed to access avail idx at %p\n",
  				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	return 0;
  }
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
+
Quoted 8 lines

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

It should work because fetch_buf fetches a whole buf, following the chain. Seems to work in a small test ... what issues do you see?

Quoted 42 lines
Quoted 29 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
  /* This looks in the virtqueue and for the first available buffer, and converts
   * it to an iovec for convenient access.  Since descriptors consist of some
   * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (ret)
  		return ret;
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
  	last = peek_split_desc(vq);
  	id = last->id;
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (unlikely(log))
  		*log_num = 0;
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
  		unsigned iov_count = *in_num + *out_num;
  		struct vhost_desc *desc = &vq->descs[i];
  		int access;
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
  			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
  			       desc->flags, desc->id);
  			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  			}
  			*out_num += ret;
  		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
  	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Quoted 1 line

Thanks

This can't happen: descriptors are pushed by push_split_desc each time we go through a loop in fetch_buf. The only way to exit the loop with return code 0 is if next_desc return -1 that is when VRING_DESC_F_NEXT is clear.

But it's a good idea to add a BUG_ON here, I'll do it in the next version.

Quoted 24 lines
Quoted 24 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
  	return ret;
  err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
  	vq->ndescs = 0;
  	return ret;
drivers/vhost/vhost.h +2 -0
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
  	struct vhost_desc *descs;
  	int ndescs;
+	int first_desc;
  	int max_descs;
+	int batch_descs;
  	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
  	struct file *kick;
Quoted 8 lines

drivers/vhost/vhost.h ssize_t vhost_chr_write_iter(struct vhost_dev *dev, · line 252

Quoted 8 lines
drivers/vhost/vhost.h +1 -1
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
  		if ((vq)->error_ctx)                               \
  				eventfd_signal((vq)->error_ctx, 1);\
  	} while (0)

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

author maintainer
#18 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

In reply to Jason Wang
Quoted 95 lines · Jason Wang
Quoted 93 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
  drivers/vhost/test.c  |  2 +-
  drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
  drivers/vhost/vhost.h |  4 +++-
  3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
  	dev = &n->dev;
  	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
  		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
  	f->private_data = n;
drivers/vhost/vhost.c +20 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
  {
  	vq->num = 1;
  	vq->ndescs = 0;
+	vq->first_desc = 0;
  	vq->desc = NULL;
  	vq->avail = NULL;
  	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  	for (i = 0; i < dev->nvqs; ++i) {
  		vq = dev->vqs[i];
  		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
  		vq->descs = kmalloc_array(vq->max_descs,
  					  sizeof(*vq->descs),
  					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
  	--vq->ndescs;
  }
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
  static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
  {
  	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
  	h = &vq->descs[vq->ndescs++];
  	h->addr = vhost64_to_cpu(vq, desc->addr);
  	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
  	h->id = id;
  	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
  	return 0;
  }
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
  {
  	struct vring_desc desc;
  	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	/* Check it isn't doing very strange things with descriptor numbers. */
  	last_avail_idx = vq->last_avail_idx;
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
  		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
  			vq_err(vq, "Failed to access avail idx at %p\n",
  				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
  	return 0;
  }
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
+
Quoted 8 lines

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

It should work because fetch_buf fetches a whole buf, following the chain. Seems to work in a small test ... what issues do you see?

Quoted 42 lines
Quoted 29 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
  /* This looks in the virtqueue and for the first available buffer, and converts
   * it to an iovec for convenient access.  Since descriptors consist of some
   * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (ret)
  		return ret;
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
  	last = peek_split_desc(vq);
  	id = last->id;
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  	if (unlikely(log))
  		*log_num = 0;
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
  		unsigned iov_count = *in_num + *out_num;
  		struct vhost_desc *desc = &vq->descs[i];
  		int access;
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
  			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
  			       desc->flags, desc->id);
  			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
  			}
  			*out_num += ret;
  		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
  	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Quoted 1 line

Thanks

This can't happen: descriptors are pushed by push_split_desc each time we go through a loop in fetch_buf. The only way to exit the loop with return code 0 is if next_desc return -1 that is when VRING_DESC_F_NEXT is clear.

But it's a good idea to add a BUG_ON here, I'll do it in the next version.

Quoted 32 lines
Quoted 32 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
  	return ret;
  err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
  	vq->ndescs = 0;
  	return ret;
drivers/vhost/vhost.h +3 -1
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
  	struct vhost_desc *descs;
  	int ndescs;
+	int first_desc;
  	int max_descs;
+	int batch_descs;
  	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
  	struct file *kick;
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
  		if ((vq)->error_ctx)                               \
  				eventfd_signal((vq)->error_ctx, 1);\
  	} while (0)
#19 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

Quoted 102 lines · Michael S. Tsirkin
Quoted 93 lines · Jason Wang
Quoted 92 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
   drivers/vhost/test.c  |  2 +-
   drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
   drivers/vhost/vhost.h |  4 +++-
   3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
   	dev = &n->dev;
   	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
   	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
   		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
   	f->private_data = n;
drivers/vhost/vhost.c +19 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   {
   	vq->num = 1;
   	vq->ndescs = 0;
+	vq->first_desc = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
   		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
   		vq->descs = kmalloc_array(vq->max_descs,
   					  sizeof(*vq->descs),
   					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
   	--vq->ndescs;
   }
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
   static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
   {
   	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
   	h = &vq->descs[vq->ndescs++];
   	h->addr = vhost64_to_cpu(vq, desc->addr);
   	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
   	h->id = id;
   	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
   	return 0;
   }
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
   {
   	struct vring_desc desc;
   	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
   	/* Check it isn't doing very strange things with descriptor numbers. */
   	last_avail_idx = vq->last_avail_idx;
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
   		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
   			vq_err(vq, "Failed to access avail idx at %p\n",
   				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
   	return 0;
   }
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
Quoted 8 lines

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 6 lines
drivers/vhost/vhost.c +6 -0
+
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

Quoted 2 lines

It should work because fetch_buf fetches a whole buf, following the chain. Seems to work in a small test ... what issues do you see?

Consider the case when fetch_buf return -EINVAL when push_split_desc() fail. This means we only have partial descriptors.

Do we need decreasing last_avail_idx and vq->ndescs here? Or having a head,tail instead of first_desc?

Thanks

Quoted 73 lines
Quoted 41 lines
Quoted 29 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
   /* This looks in the virtqueue and for the first available buffer, and converts
    * it to an iovec for convenient access.  Since descriptors consist of some
    * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   	if (ret)
   		return ret;
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
   	last = peek_split_desc(vq);
   	id = last->id;
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   	if (unlikely(log))
   		*log_num = 0;
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
   		unsigned iov_count = *in_num + *out_num;
   		struct vhost_desc *desc = &vq->descs[i];
   		int access;
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
   			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
   			       desc->flags, desc->id);
   			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   			}
   			*out_num += ret;
   		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
   	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Quoted 1 line

Thanks

This can't happen: descriptors are pushed by push_split_desc each time we go through a loop in fetch_buf. The only way to exit the loop with return code 0 is if next_desc return -1 that is when VRING_DESC_F_NEXT is clear.

But it's a good idea to add a BUG_ON here, I'll do it in the next version.

Quoted 23 lines
Quoted 23 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
   	return ret;
   err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
   	vq->ndescs = 0;
   	return ret;
drivers/vhost/vhost.h +2 -0
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
   	struct vhost_desc *descs;
   	int ndescs;
+	int first_desc;
   	int max_descs;
+	int batch_descs;
   	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
   	struct file *kick;
Quoted 8 lines
Quoted 8 lines

drivers/vhost/vhost.h ssize_t vhost_chr_write_iter(struct vhost_dev *dev, · line 252

Quoted 8 lines
drivers/vhost/vhost.h +1 -1
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
   int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
   #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
   		if ((vq)->error_ctx)                               \
   				eventfd_signal((vq)->error_ctx, 1);\
   	} while (0)

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

#20 lore raw

Re: [PATCH RFC v1 2/2] vhost: batching fetches

In reply to Michael S. Tsirkin
Quoted 102 lines · Michael S. Tsirkin
Quoted 93 lines · Jason Wang
Quoted 92 lines · Michael S. Tsirkin

With this patch applied, new and old code perform identically.

Lots of extra optimizations are now possible, e.g. we can fetch multiple heads with copy_from/to_user now. We can get rid of maintaining the log array. Etc etc.

---
   drivers/vhost/test.c  |  2 +-
   drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++-------
   drivers/vhost/vhost.h |  4 +++-
   3 files changed, 46 insertions(+), 10 deletions(-)
drivers/vhost/test.c +1 -1
index 39a018a7af2d..e3a8e9db22cd 100644
@@ -128,7 +128,7 @@ static int vhost_test_open(struct inode *inode, struct file *f)
   	dev = &n->dev;
   	vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
   	n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
-	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
+	vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV + 64,
   		       VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
   	f->private_data = n;
drivers/vhost/vhost.c +19 -3
index 36661d6cb51f..aa383e847865 100644
@@ -302,6 +302,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
   {
   	vq->num = 1;
   	vq->ndescs = 0;
+	vq->first_desc = 0;
   	vq->desc = NULL;
   	vq->avail = NULL;
   	vq->used = NULL;
@@ -390,6 +391,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
   	for (i = 0; i < dev->nvqs; ++i) {
   		vq = dev->vqs[i];
   		vq->max_descs = dev->iov_limit;
+		vq->batch_descs = dev->iov_limit - UIO_MAXIOV;
   		vq->descs = kmalloc_array(vq->max_descs,
   					  sizeof(*vq->descs),
   					  GFP_KERNEL);
@@ -2366,6 +2368,8 @@ static void pop_split_desc(struct vhost_virtqueue *vq)
   	--vq->ndescs;
   }
+#define VHOST_DESC_FLAGS (VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE | \
+			  VRING_DESC_F_NEXT)
   static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc, u16 id)
   {
   	struct vhost_desc *h;
@@ -2375,7 +2379,7 @@ static int push_split_desc(struct vhost_virtqueue *vq, struct vring_desc *desc,
   	h = &vq->descs[vq->ndescs++];
   	h->addr = vhost64_to_cpu(vq, desc->addr);
   	h->len = vhost32_to_cpu(vq, desc->len);
-	h->flags = vhost16_to_cpu(vq, desc->flags);
+	h->flags = vhost16_to_cpu(vq, desc->flags) & VHOST_DESC_FLAGS;
   	h->id = id;
   	return 0;
@@ -2450,7 +2454,7 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq,
   	return 0;
   }
-static int fetch_descs(struct vhost_virtqueue *vq)
+static int fetch_buf(struct vhost_virtqueue *vq)
   {
   	struct vring_desc desc;
   	unsigned int i, head, found = 0;
@@ -2462,7 +2466,11 @@ static int fetch_descs(struct vhost_virtqueue *vq)
   	/* Check it isn't doing very strange things with descriptor numbers. */
   	last_avail_idx = vq->last_avail_idx;
-	if (vq->avail_idx == vq->last_avail_idx) {
+	if (unlikely(vq->avail_idx == vq->last_avail_idx)) {
+		/* If we already have work to do, don't bother re-checking. */
+		if (likely(vq->ndescs))
+			return vq->num;
+
   		if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
   			vq_err(vq, "Failed to access avail idx at %p\n",
   				&vq->avail->idx);
@@ -2541,6 +2549,24 @@ static int fetch_descs(struct vhost_virtqueue *vq)
   	return 0;
   }
+static int fetch_descs(struct vhost_virtqueue *vq)
+{
+	int ret = 0;
+
+	if (unlikely(vq->first_desc >= vq->ndescs)) {
+		vq->first_desc = 0;
+		vq->ndescs = 0;
+	}
Quoted 8 lines

drivers/vhost/vhost.c static int fetch_descs(struct vhost_virtqueue *vq) · line 2565

Quoted 6 lines
drivers/vhost/vhost.c +6 -0
+
+	if (vq->ndescs)
+		return 0;
+
+	while (!ret && vq->ndescs <= vq->batch_descs)
+		ret = fetch_buf(vq);

It looks to me descriptor chaining might be broken here.

Quoted 2 lines

It should work because fetch_buf fetches a whole buf, following the chain. Seems to work in a small test ... what issues do you see?

Consider the case when fetch_buf return -EINVAL when push_split_desc() fail. This means we only have partial descriptors.

Do we need decreasing last_avail_idx and vq->ndescs here? Or having a head,tail instead of first_desc?

Thanks

Quoted 81 lines
Quoted 41 lines
Quoted 29 lines
drivers/vhost/vhost.c +8 -2
+
+	return vq->ndescs ? 0 : ret;
+}
+
   /* This looks in the virtqueue and for the first available buffer, and converts
    * it to an iovec for convenient access.  Since descriptors consist of some
    * number of output then some number of input descriptors, it's actually two
@@ -2562,6 +2588,8 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   	if (ret)
   		return ret;
+	/* Note: indirect descriptors are not batched */
+	/* TODO: batch up to a limit */
   	last = peek_split_desc(vq);
   	id = last->id;
@@ -2584,12 +2612,12 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   	if (unlikely(log))
   		*log_num = 0;
-	for (i = 0; i < vq->ndescs; ++i) {
+	for (i = vq->first_desc; i < vq->ndescs; ++i) {
   		unsigned iov_count = *in_num + *out_num;
   		struct vhost_desc *desc = &vq->descs[i];
   		int access;
-		if (desc->flags & ~(VRING_DESC_F_INDIRECT | VRING_DESC_F_WRITE)) {
+		if (desc->flags & ~VHOST_DESC_FLAGS) {
   			vq_err(vq, "Unexpected flags: 0x%x at descriptor id 0x%x\n",
   			       desc->flags, desc->id);
   			ret = -EINVAL;

drivers/vhost/vhost.c int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, · line 2664

Quoted 9 lines
drivers/vhost/vhost.c +5 -0
@@ -2628,15 +2656,21 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq,
   			}
   			*out_num += ret;
   		}
+
+		ret = desc->id;
+
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			break;
   	}

What happens if we reach vq->ndescs but VRING_DESC_F_NEXT is still set?

Quoted 1 line

Thanks

This can't happen: descriptors are pushed by push_split_desc each time we go through a loop in fetch_buf. The only way to exit the loop with return code 0 is if next_desc return -1 that is when VRING_DESC_F_NEXT is clear.

But it's a good idea to add a BUG_ON here, I'll do it in the next version.

Quoted 31 lines
Quoted 31 lines
drivers/vhost/vhost.c +4 -3
-	ret = id;
-	vq->ndescs = 0;
+	vq->first_desc = i + 1;
   	return ret;
   err:
-	vhost_discard_vq_desc(vq, 1);
+	for (i = vq->first_desc; i < vq->ndescs; ++i)
+		if (!(desc->flags & VRING_DESC_F_NEXT))
+			vhost_discard_vq_desc(vq, 1);
   	vq->ndescs = 0;
   	return ret;
drivers/vhost/vhost.h +3 -1
index 1724f61b6c2d..8b88e0c903da 100644
@@ -100,7 +100,9 @@ struct vhost_virtqueue {
   	struct vhost_desc *descs;
   	int ndescs;
+	int first_desc;
   	int max_descs;
+	int batch_descs;
   	const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS];
   	struct file *kick;
@@ -245,7 +247,7 @@ ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
   int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
   #define vq_err(vq, fmt, ...) do {                                  \
-		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
+		pr_err(pr_fmt(fmt), ##__VA_ARGS__);       \
   		if ((vq)->error_ctx)                               \
   				eventfd_signal((vq)->error_ctx, 1);\
   	} while (0)
#21 lore raw
In reply to Michael S. Tsirkin
Quoted 17 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Quoted 6 lines

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

It would be better to convert vhost_net then I can do some benchmark on that.

Thanks

Quoted 12 lines

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)

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

#22 lore raw
In reply to Michael S. Tsirkin
Quoted 17 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Quoted 6 lines

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

It would be better to convert vhost_net then I can do some benchmark on that.

Thanks

Quoted 12 lines

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)
author maintainer
#23 lore raw
Quoted 27 lines · Jason Wang
Quoted 17 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Quoted 6 lines

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Quoted 4 lines

It would be better to convert vhost_net then I can do some benchmark on that.

Thanks

Sure, I post a small patch that does this.

Quoted 8 lines
Quoted 8 lines

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

Quoted 5 lines
Quoted 5 lines
  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)

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

author maintainer
#24 lore raw
In reply to Jason Wang
Quoted 27 lines · Jason Wang
Quoted 17 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Quoted 6 lines

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Quoted 4 lines

It would be better to convert vhost_net then I can do some benchmark on that.

Thanks

Sure, I post a small patch that does this.

Quoted 13 lines
Quoted 13 lines

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)
#25 lore raw
In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

I wonder this may help for performance:

- another indirection layer, increased footprint

- won't help or even degrade when there's no batch

- an extra overhead in the case of in order where we should already had tight loop

- need carefully deal with indirect and chain or make it only work for packet sit just in a single descriptor

Thanks

Quoted 17 lines

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Quoted 9 lines

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)

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

#26 lore raw
In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

I wonder this may help for performance:

- another indirection layer, increased footprint

- won't help or even degrade when there's no batch

- an extra overhead in the case of in order where we should already had tight loop

- need carefully deal with indirect and chain or make it only work for packet sit just in a single descriptor

Thanks

Quoted 26 lines

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)
author maintainer
#27 lore raw
Quoted 6 lines · Jason Wang
Quoted 4 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

Quoted 8 lines
Quoted 5 lines

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

I wonder this may help for performance:

Could you try it out and report please?
Would be very much appreciated.

Quoted 1 line

- another indirection layer, increased footprint

Seems to be offset off by improved batching.
For sure will be even better if we can move stac/clac out, or replace some get/put user with bigger copy to/from.

Quoted 1 line

- won't help or even degrade when there's no batch

I couldn't measure a difference. I'm guessing

Quoted 2 lines

- an extra overhead in the case of in order where we should already had tight loop

it's not so tight with translation in there.
this exactly makes the loop tight.

Quoted 4 lines

- need carefully deal with indirect and chain or make it only work for packet sit just in a single descriptor

Thanks

I don't understand this last comment.

Quoted 22 lines
Quoted 22 lines

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

Quoted 5 lines
Quoted 5 lines
  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)

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

author maintainer
#28 lore raw
In reply to Jason Wang
Quoted 6 lines · Jason Wang
Quoted 4 lines · Michael S. Tsirkin

So the idea is as follows: we convert descriptors to an independent format first, and process that converting to iov later.

Quoted 8 lines
Quoted 5 lines

The point is that we have a tight loop that fetches descriptors, which is good for cache utilization. This will also allow all kind of batching tricks - e.g. it seems possible to keep SMAP disabled while we are fetching multiple descriptors.

I wonder this may help for performance:

Could you try it out and report please?
Would be very much appreciated.

Quoted 1 line

- another indirection layer, increased footprint

Seems to be offset off by improved batching.
For sure will be even better if we can move stac/clac out, or replace some get/put user with bigger copy to/from.

Quoted 1 line

- won't help or even degrade when there's no batch

I couldn't measure a difference. I'm guessing

Quoted 2 lines

- an extra overhead in the case of in order where we should already had tight loop

it's not so tight with translation in there.
this exactly makes the loop tight.

Quoted 4 lines

- need carefully deal with indirect and chain or make it only work for packet sit just in a single descriptor

Thanks

I don't understand this last comment.

Quoted 27 lines
Quoted 27 lines

And perhaps more importantly, this is a very good fit for the packed ring layout, where we get and put descriptors in order.

This patchset seems to already perform exactly the same as the original code already based on a microbenchmark. More testing would be very much appreciated.

Biggest TODO before this first step is ready to go in is to batch indirect descriptors as well.

Integrating into vhost-net is basically s/vhost_get_vq_desc/vhost_get_vq_desc_batch/ - or add a module parameter like I did in the test module.

Michael S. Tsirkin (2):
vhost: option to fetch descriptors through an independent struct
vhost: batching fetches

  drivers/vhost/test.c  |  19 ++-
  drivers/vhost/vhost.c | 333 +++++++++++++++++++++++++++++++++++++++++-
  drivers/vhost/vhost.h |  20 ++-
  3 files changed, 365 insertions(+), 7 deletions(-)