2022-08-17 14:02:03

by Guo Zhi

[permalink] [raw]
Subject: [RFC v2 1/7] vhost: expose used buffers

Follow VIRTIO 1.1 spec, only writing out a single used ring for a batch
of descriptors.

Signed-off-by: Guo Zhi <[email protected]>
---
drivers/vhost/vhost.c | 14 ++++++++++++--
drivers/vhost/vhost.h | 1 +
2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 40097826cff0..7b20fa5a46c3 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -2376,10 +2376,20 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
vring_used_elem_t __user *used;
u16 old, new;
int start;
+ int copy_n = count;

+ /**
+ * If in order feature negotiated, devices can notify the use of a batch of buffers to
+ * the driver by only writing out a single used ring entry with the id corresponding
+ * to the head entry of the descriptor chain describing the last buffer in the batch.
+ */
+ if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
+ copy_n = 1;
+ heads = &heads[count - 1];
+ }
start = vq->last_used_idx & (vq->num - 1);
used = vq->used->ring + start;
- if (vhost_put_used(vq, heads, start, count)) {
+ if (vhost_put_used(vq, heads, start, copy_n)) {
vq_err(vq, "Failed to write used");
return -EFAULT;
}
@@ -2410,7 +2420,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,

start = vq->last_used_idx & (vq->num - 1);
n = vq->num - start;
- if (n < count) {
+ if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
r = __vhost_add_used_n(vq, heads, n);
if (r < 0)
return r;
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index d9109107af08..0d5c49a30421 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -236,6 +236,7 @@ enum {
VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
(1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
(1ULL << VIRTIO_RING_F_EVENT_IDX) |
+ (1ULL << VIRTIO_F_IN_ORDER) |
(1ULL << VHOST_F_LOG_ALL) |
(1ULL << VIRTIO_F_ANY_LAYOUT) |
(1ULL << VIRTIO_F_VERSION_1)
--
2.17.1


2022-08-18 06:34:44

by Eugenio Perez Martin

[permalink] [raw]
Subject: Re: [RFC v2 1/7] vhost: expose used buffers

On Wed, Aug 17, 2022 at 3:58 PM Guo Zhi <[email protected]> wrote:
>
> Follow VIRTIO 1.1 spec, only writing out a single used ring for a batch
> of descriptors.
>
> Signed-off-by: Guo Zhi <[email protected]>
> ---
> drivers/vhost/vhost.c | 14 ++++++++++++--
> drivers/vhost/vhost.h | 1 +
> 2 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 40097826cff0..7b20fa5a46c3 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -2376,10 +2376,20 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
> vring_used_elem_t __user *used;
> u16 old, new;
> int start;
> + int copy_n = count;
>
> + /**
> + * If in order feature negotiated, devices can notify the use of a batch of buffers to
> + * the driver by only writing out a single used ring entry with the id corresponding
> + * to the head entry of the descriptor chain describing the last buffer in the batch.
> + */
> + if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
> + copy_n = 1;
> + heads = &heads[count - 1];
> + }
> start = vq->last_used_idx & (vq->num - 1);
> used = vq->used->ring + start;
> - if (vhost_put_used(vq, heads, start, count)) {
> + if (vhost_put_used(vq, heads, start, copy_n)) {
> vq_err(vq, "Failed to write used");
> return -EFAULT;
> }
> @@ -2410,7 +2420,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
>
> start = vq->last_used_idx & (vq->num - 1);
> n = vq->num - start;
> - if (n < count) {
> + if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
> r = __vhost_add_used_n(vq, heads, n);
> if (r < 0)
> return r;

It would be simpler to use vhost_add_used in vsock/vhost_test to add a
batch of used descriptors, and leave vhost_add_used_n untouched.

Since it's the upper layer the one that manages the in_order in this
version, we could:
* Always call vhost_add_used(vq, last_head_of_batch, ...) for the tx
queue, that does not need used length info.
* Call vhost_add_used_n for the rx queue, since the driver needs the
used length info.

> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
> index d9109107af08..0d5c49a30421 100644
> --- a/drivers/vhost/vhost.h
> +++ b/drivers/vhost/vhost.h
> @@ -236,6 +236,7 @@ enum {
> VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
> (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
> (1ULL << VIRTIO_RING_F_EVENT_IDX) |
> + (1ULL << VIRTIO_F_IN_ORDER) |
> (1ULL << VHOST_F_LOG_ALL) |
> (1ULL << VIRTIO_F_ANY_LAYOUT) |
> (1ULL << VIRTIO_F_VERSION_1)
> --
> 2.17.1
>

2022-08-19 11:06:59

by Guo Zhi

[permalink] [raw]
Subject: Re: [RFC v2 1/7] vhost: expose used buffers



----- Original Message -----
> From: "eperezma" <[email protected]>
> To: "Guo Zhi" <[email protected]>
> Cc: "jasowang" <[email protected]>, "sgarzare" <[email protected]>, "Michael Tsirkin" <[email protected]>, "netdev"
> <[email protected]>, "linux-kernel" <[email protected]>, "kvm list" <[email protected]>,
> "virtualization" <[email protected]>
> Sent: Thursday, August 18, 2022 2:16:40 PM
> Subject: Re: [RFC v2 1/7] vhost: expose used buffers

> On Wed, Aug 17, 2022 at 3:58 PM Guo Zhi <[email protected]> wrote:
>>
>> Follow VIRTIO 1.1 spec, only writing out a single used ring for a batch
>> of descriptors.
>>
>> Signed-off-by: Guo Zhi <[email protected]>
>> ---
>> drivers/vhost/vhost.c | 14 ++++++++++++--
>> drivers/vhost/vhost.h | 1 +
>> 2 files changed, 13 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>> index 40097826cff0..7b20fa5a46c3 100644
>> --- a/drivers/vhost/vhost.c
>> +++ b/drivers/vhost/vhost.c
>> @@ -2376,10 +2376,20 @@ static int __vhost_add_used_n(struct vhost_virtqueue
>> *vq,
>> vring_used_elem_t __user *used;
>> u16 old, new;
>> int start;
>> + int copy_n = count;
>>
>> + /**
>> + * If in order feature negotiated, devices can notify the use of a batch
>> of buffers to
>> + * the driver by only writing out a single used ring entry with the id
>> corresponding
>> + * to the head entry of the descriptor chain describing the last buffer
>> in the batch.
>> + */
>> + if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
>> + copy_n = 1;
>> + heads = &heads[count - 1];
>> + }
>> start = vq->last_used_idx & (vq->num - 1);
>> used = vq->used->ring + start;
>> - if (vhost_put_used(vq, heads, start, count)) {
>> + if (vhost_put_used(vq, heads, start, copy_n)) {
>> vq_err(vq, "Failed to write used");
>> return -EFAULT;
>> }
>> @@ -2410,7 +2420,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct
>> vring_used_elem *heads,
>>
>> start = vq->last_used_idx & (vq->num - 1);
>> n = vq->num - start;
>> - if (n < count) {
>> + if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
>> r = __vhost_add_used_n(vq, heads, n);
>> if (r < 0)
>> return r;
>
> It would be simpler to use vhost_add_used in vsock/vhost_test to add a
> batch of used descriptors, and leave vhost_add_used_n untouched.
>
> Since it's the upper layer the one that manages the in_order in this
> version, we could:
> * Always call vhost_add_used(vq, last_head_of_batch, ...) for the tx
> queue, that does not need used length info.
> * Call vhost_add_used_n for the rx queue, since the driver needs the
> used length info.

Very insightful view!

At first, vhost_add_used will write used ring for skipped buffer in a batch,
so we can't let vhost unmodified to enable in order feature.

Secondly, Current changes to vhost_add_used_n will affect the rx queue,
as driver have to get each buffer's length from used ring.

So I would propose a flag parameter in vhost_add_used_n to decide
whether the batch for in order buffer is done or nor.

>> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
>> index d9109107af08..0d5c49a30421 100644
>> --- a/drivers/vhost/vhost.h
>> +++ b/drivers/vhost/vhost.h
>> @@ -236,6 +236,7 @@ enum {
>> VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
>> (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
>> (1ULL << VIRTIO_RING_F_EVENT_IDX) |
>> + (1ULL << VIRTIO_F_IN_ORDER) |
>> (1ULL << VHOST_F_LOG_ALL) |
>> (1ULL << VIRTIO_F_ANY_LAYOUT) |
>> (1ULL << VIRTIO_F_VERSION_1)
>> --
>> 2.17.1
>>

2022-08-25 08:04:40

by Jason Wang

[permalink] [raw]
Subject: Re: [RFC v2 1/7] vhost: expose used buffers


在 2022/8/17 21:57, Guo Zhi 写道:
> Follow VIRTIO 1.1 spec, only writing out a single used ring for a batch
> of descriptors.
>
> Signed-off-by: Guo Zhi <[email protected]>
> ---
> drivers/vhost/vhost.c | 14 ++++++++++++--
> drivers/vhost/vhost.h | 1 +
> 2 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 40097826cff0..7b20fa5a46c3 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -2376,10 +2376,20 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
> vring_used_elem_t __user *used;
> u16 old, new;
> int start;
> + int copy_n = count;
>
> + /**
> + * If in order feature negotiated, devices can notify the use of a batch of buffers to
> + * the driver by only writing out a single used ring entry with the id corresponding
> + * to the head entry of the descriptor chain describing the last buffer in the batch.
> + */
> + if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
> + copy_n = 1;
> + heads = &heads[count - 1];


Do we need to check whether or not the buffer is fully used before doing
this?


> + }
> start = vq->last_used_idx & (vq->num - 1);
> used = vq->used->ring + start;
> - if (vhost_put_used(vq, heads, start, count)) {
> + if (vhost_put_used(vq, heads, start, copy_n)) {
> vq_err(vq, "Failed to write used");
> return -EFAULT;
> }
> @@ -2410,7 +2420,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
>
> start = vq->last_used_idx & (vq->num - 1);
> n = vq->num - start;
> - if (n < count) {
> + if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
> r = __vhost_add_used_n(vq, heads, n);
> if (r < 0)
> return r;
> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
> index d9109107af08..0d5c49a30421 100644
> --- a/drivers/vhost/vhost.h
> +++ b/drivers/vhost/vhost.h
> @@ -236,6 +236,7 @@ enum {
> VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
> (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
> (1ULL << VIRTIO_RING_F_EVENT_IDX) |
> + (1ULL << VIRTIO_F_IN_ORDER) |
> (1ULL << VHOST_F_LOG_ALL) |


Are we sure all vhost devices can support in-order (especially the SCSI)?

It looks better to start from a device specific one.

Thanks


> (1ULL << VIRTIO_F_ANY_LAYOUT) |
> (1ULL << VIRTIO_F_VERSION_1)

2022-08-26 03:13:13

by Guo Zhi

[permalink] [raw]
Subject: Re: [RFC v2 1/7] vhost: expose used buffers



----- Original Message -----
> From: "jasowang" <[email protected]>
> To: "Guo Zhi" <[email protected]>, "eperezma" <[email protected]>, "sgarzare" <[email protected]>, "Michael
> Tsirkin" <[email protected]>
> Cc: "netdev" <[email protected]>, "linux-kernel" <[email protected]>, "kvm list" <[email protected]>,
> "virtualization" <[email protected]>
> Sent: Thursday, August 25, 2022 3:01:31 PM
> Subject: Re: [RFC v2 1/7] vhost: expose used buffers

> ?? 2022/8/17 21:57, Guo Zhi д??:
>> Follow VIRTIO 1.1 spec, only writing out a single used ring for a batch
>> of descriptors.
>>
>> Signed-off-by: Guo Zhi <[email protected]>
>> ---
>> drivers/vhost/vhost.c | 14 ++++++++++++--
>> drivers/vhost/vhost.h | 1 +
>> 2 files changed, 13 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>> index 40097826cff0..7b20fa5a46c3 100644
>> --- a/drivers/vhost/vhost.c
>> +++ b/drivers/vhost/vhost.c
>> @@ -2376,10 +2376,20 @@ static int __vhost_add_used_n(struct vhost_virtqueue
>> *vq,
>> vring_used_elem_t __user *used;
>> u16 old, new;
>> int start;
>> + int copy_n = count;
>>
>> + /**
>> + * If in order feature negotiated, devices can notify the use of a batch of
>> buffers to
>> + * the driver by only writing out a single used ring entry with the id
>> corresponding
>> + * to the head entry of the descriptor chain describing the last buffer in the
>> batch.
>> + */
>> + if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
>> + copy_n = 1;
>> + heads = &heads[count - 1];
>
>
> Do we need to check whether or not the buffer is fully used before doing
> this?
>

This is the caller / user of __vhost_add_used_n's duty to make sure all buffer is fully used.
The device will only batch if all heads are fully used.

>
>> + }
>> start = vq->last_used_idx & (vq->num - 1);
>> used = vq->used->ring + start;
>> - if (vhost_put_used(vq, heads, start, count)) {
>> + if (vhost_put_used(vq, heads, start, copy_n)) {
>> vq_err(vq, "Failed to write used");
>> return -EFAULT;
>> }
>> @@ -2410,7 +2420,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct
>> vring_used_elem *heads,
>>
>> start = vq->last_used_idx & (vq->num - 1);
>> n = vq->num - start;
>> - if (n < count) {
>> + if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) {
>> r = __vhost_add_used_n(vq, heads, n);
>> if (r < 0)
>> return r;
>> diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
>> index d9109107af08..0d5c49a30421 100644
>> --- a/drivers/vhost/vhost.h
>> +++ b/drivers/vhost/vhost.h
>> @@ -236,6 +236,7 @@ enum {
>> VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
>> (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
>> (1ULL << VIRTIO_RING_F_EVENT_IDX) |
>> + (1ULL << VIRTIO_F_IN_ORDER) |
>> (1ULL << VHOST_F_LOG_ALL) |
>
>
> Are we sure all vhost devices can support in-order (especially the SCSI)?
>
> It looks better to start from a device specific one.
>
> Thanks
>
Sorry for my mistake, I will change it.

>
>> (1ULL << VIRTIO_F_ANY_LAYOUT) |
>> (1ULL << VIRTIO_F_VERSION_1)