Skip to contentSkip to conversation

vhost_vdpa: better PACKED support

PATCH Superseded Shannon Nelson via Virtualization 2 participants 9 messages Apr 21, 2023 – Apr 24, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vhost_vdpa: better PACKED support

While testing our vDPA driver with QEMU we found that vhost_vdpa seems to be missing some support for PACKED VQs. Adding these helped us get further in our testing. The first patch makes sure that the vhost_vdpa vqs are given the feature flags, as done in other vhost variants. The second and third patches use the feature flags to properly format and pass set/get_vq requests.

Shannon Nelson (3):
vhost_vdpa: tell vqs about the negotiated
vhost: support PACKED when setting-getting vring_base
vhost_vdpa: support PACKED when setting-getting vring_base

 drivers/vhost/vdpa.c  | 32 ++++++++++++++++++++++++++++----
 drivers/vhost/vhost.c | 18 +++++++++++++-----
 2 files changed, 41 insertions(+), 9 deletions(-)
Signature · 6 lines
2.17.1

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

PATCH 2/3 vhost: support PACKED when setting-getting vring_base

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
 drivers/vhost/vhost.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +13 -5
index f11bdbe4c2c5..f64efda48f21 100644
@@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
 			r = -EFAULT;
 			break;
 		}
-		if (s.num > 0xffff) {
-			r = -EINVAL;
-			break;
+		if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+			vq->last_avail_idx = s.num & 0xffff;
+			vq->last_used_idx = (s.num >> 16) & 0xffff;
+		} else {
+			if (s.num > 0xffff) {
+				r = -EINVAL;
+				break;
+			}
+			vq->last_avail_idx = s.num;
 		}
-		vq->last_avail_idx = s.num;
 		/* Forget the cached index value. */
 		vq->avail_idx = vq->last_avail_idx;
 		break;
 	case VHOST_GET_VRING_BASE:
 		s.index = idx;
-		s.num = vq->last_avail_idx;
+		if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED))
+			s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16);
+		else
+			s.num = vq->last_avail_idx;
 		if (copy_to_user(argp, &s, sizeof s))
 			r = -EFAULT;
 		break;
Signature · 6 lines
2.17.1

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

PATCH 1/3 vhost_vdpa: tell vqs about the negotiated

In reply to Shannon Nelson via Virtualization

As is done in the net, iscsi, and vsock vhost support, let the vdpa vqs know about the features that have been negotiated. This allows vhost to more safely make decisions based on the features, such as when using PACKED vs split queues.

---
 drivers/vhost/vdpa.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index 7be9d9d8f01c..599b8cc238c7 100644
@@ -385,7 +385,10 @@ static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
 {
 	struct vdpa_device *vdpa = v->vdpa;
 	const struct vdpa_config_ops *ops = vdpa->config;
+	struct vhost_dev *d = &v->vdev;
+	u64 actual_features;
 	u64 features;
+	int i;
 
 	/*
 	 * It's not allowed to change the features after they have
@@ -400,6 +403,16 @@ static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
 	if (vdpa_set_features(vdpa, features))
 		return -EINVAL;
 
+	/* let the vqs know what has been configured */
+	actual_features = ops->get_driver_features(vdpa);
+	for (i = 0; i < d->nvqs; ++i) {
+		struct vhost_virtqueue *vq = d->vqs[i];
+
+		mutex_lock(&vq->mutex);
+		vq->acked_features = actual_features;
+		mutex_unlock(&vq->mutex);
+	}
+
 	return 0;
 }
 
Signature · 6 lines
2.17.1

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

PATCH 3/3 vhost_vdpa: support PACKED when setting-getting vring_base

In reply to Shannon Nelson via Virtualization

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
 drivers/vhost/vdpa.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/vhost/vdpa.c +15 -4
index 599b8cc238c7..2543ae9d5939 100644
@@ -585,7 +585,12 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 		if (r)
 			return r;
 
-		vq->last_avail_idx = vq_state.split.avail_index;
+		if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+			vq->last_avail_idx = vq_state.packed.last_avail_idx;
+			vq->last_used_idx = vq_state.packed.last_used_idx;
+		} else {
+			vq->last_avail_idx = vq_state.split.avail_index;
+		}
 		break;
 	}
 
@@ -603,9 +608,15 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
 		break;
 
 	case VHOST_SET_VRING_BASE:
-		vq_state.split.avail_index = vq->last_avail_idx;
-		if (ops->set_vq_state(vdpa, idx, &vq_state))
-			r = -EINVAL;
+		if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+			vq_state.packed.last_avail_idx = vq->last_avail_idx & 0x7fff;
+			vq_state.packed.last_avail_counter = !!(vq->last_avail_idx & 0x8000);
+			vq_state.packed.last_used_idx = vq->last_used_idx & 0x7fff;
+			vq_state.packed.last_used_counter = !!(vq->last_used_idx & 0x8000);
+		} else {
+			vq_state.split.avail_index = vq->last_avail_idx;
+		}
+		r = ops->set_vq_state(vdpa, idx, &vq_state);
 		break;
 
 	case VHOST_SET_VRING_CALL:
Signature · 6 lines
2.17.1

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

Re: [PATCH 2/3] vhost: support PACKED when setting-getting vring_base

In reply to Shannon Nelson via Virtualization
Quoted 14 lines · Shannon Nelson

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
 drivers/vhost/vhost.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +0 -0
index f11bdbe4c2c5..f64efda48f21 100644

drivers/vhost/vhost.c long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg · line 1638

Quoted 9 lines
drivers/vhost/vhost.c +3 -3
@@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
                        r = -EFAULT;
                        break;
                }
-               if (s.num > 0xffff) {
-                       r = -EINVAL;
-                       break;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+                       vq->last_avail_idx = s.num & 0xffff;
+                       vq->last_used_idx = (s.num >> 16) & 0xffff;

I think we need to tweak the comment around last_avail_idx and last_used_idx:

        /* Last available index we saw. */
        u16 last_avail_idx;

        /* Last index we used. */
        u16 last_used_idx;

To describe that it contains wrap counters as well in the case of packed virtqueue or maybe it's time to rename them (since they are invented for split virtqueue).

Thanks

Quoted 22 lines
drivers/vhost/vhost.c +10 -3
+               } else {
+                       if (s.num > 0xffff) {
+                               r = -EINVAL;
+                               break;
+                       }
+                       vq->last_avail_idx = s.num;
                }
-               vq->last_avail_idx = s.num;
                /* Forget the cached index value. */
                vq->avail_idx = vq->last_avail_idx;
                break;
        case VHOST_GET_VRING_BASE:
                s.index = idx;
-               s.num = vq->last_avail_idx;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED))
+                       s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16);
+               else
+                       s.num = vq->last_avail_idx;
                if (copy_to_user(argp, &s, sizeof s))
                        r = -EFAULT;
                break;
--
Quoted 2 lines

2.17.1

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

Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] vhost_vdpa: tell vqs about the negotiated

In reply to Shannon Nelson via Virtualization
Quoted 7 lines · Shannon Nelson

As is done in the net, iscsi, and vsock vhost support, let the vdpa vqs know about the features that have been negotiated. This allows vhost to more safely make decisions based on the features, such as when using PACKED vs split queues.

Thanks

Quoted 37 lines
---
 drivers/vhost/vdpa.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -1
index 7be9d9d8f01c..599b8cc238c7 100644
@@ -385,7 +385,10 @@ static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
 {
        struct vdpa_device *vdpa = v->vdpa;
        const struct vdpa_config_ops *ops = vdpa->config;
+       struct vhost_dev *d = &v->vdev;
+       u64 actual_features;
        u64 features;
+       int i;
 
        /*
         * It's not allowed to change the features after they have
@@ -400,6 +403,16 @@ static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
        if (vdpa_set_features(vdpa, features))
                return -EINVAL;
 
+       /* let the vqs know what has been configured */
+       actual_features = ops->get_driver_features(vdpa);
+       for (i = 0; i < d->nvqs; ++i) {
+               struct vhost_virtqueue *vq = d->vqs[i];
+
+               mutex_lock(&vq->mutex);
+               vq->acked_features = actual_features;
+               mutex_unlock(&vq->mutex);
+       }
+
        return 0;
 }
 
--
Quoted 2 lines

2.17.1

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

#7 lore raw

Re: [PATCH 3/3] vhost_vdpa: support PACKED when setting-getting vring_base

In reply to Shannon Nelson via Virtualization
Quoted 14 lines · Shannon Nelson

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
 drivers/vhost/vdpa.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/vhost/vdpa.c +0 -0
index 599b8cc238c7..2543ae9d5939 100644

drivers/vhost/vdpa.c static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, · line 590

Quoted 7 lines
drivers/vhost/vdpa.c +3 -1
@@ -585,7 +585,12 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
                if (r)
                        return r;
 
-               vq->last_avail_idx = vq_state.split.avail_index;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+                       vq->last_avail_idx = vq_state.packed.last_avail_idx;
+                       vq->last_used_idx = vq_state.packed.last_used_idx;

I wonder if the compiler will guarantee a piggyback for the wrap counter here? If not, should we explicitly packing wrap counter?

Thanks

Quoted 26 lines
drivers/vhost/vdpa.c +12 -4
+               } else {
+                       vq->last_avail_idx = vq_state.split.avail_index;
+               }
                break;
        }
 
@@ -603,9 +608,15 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
                break;
 
        case VHOST_SET_VRING_BASE:
-               vq_state.split.avail_index = vq->last_avail_idx;
-               if (ops->set_vq_state(vdpa, idx, &vq_state))
-                       r = -EINVAL;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+                       vq_state.packed.last_avail_idx = vq->last_avail_idx & 0x7fff;
+                       vq_state.packed.last_avail_counter = !!(vq->last_avail_idx & 0x8000);
+                       vq_state.packed.last_used_idx = vq->last_used_idx & 0x7fff;
+                       vq_state.packed.last_used_counter = !!(vq->last_used_idx & 0x8000);
+               } else {
+                       vq_state.split.avail_index = vq->last_avail_idx;
+               }
+               r = ops->set_vq_state(vdpa, idx, &vq_state);
                break;
 
        case VHOST_SET_VRING_CALL:
--
Quoted 2 lines

2.17.1

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

#5 lore raw

Re: [PATCH 2/3] vhost: support PACKED when setting-getting vring_base

In reply to Jason Wang
Quoted 27 lines · Jason Wang
Quoted 14 lines · Shannon Nelson

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
  drivers/vhost/vhost.c | 18 +++++++++++++-----
  1 file changed, 13 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +0 -0
index f11bdbe4c2c5..f64efda48f21 100644

drivers/vhost/vhost.c long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg · line 1638

Quoted 9 lines
drivers/vhost/vhost.c +3 -3
@@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
                         r = -EFAULT;
                         break;
                 }
-               if (s.num > 0xffff) {
-                       r = -EINVAL;
-                       break;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+                       vq->last_avail_idx = s.num & 0xffff;
+                       vq->last_used_idx = (s.num >> 16) & 0xffff;

I think we need to tweak the comment around last_avail_idx and last_used_idx:

Quoted 8 lines
         /* Last available index we saw. */
         u16 last_avail_idx;

         /* Last index we used. */
         u16 last_used_idx;

To describe that it contains wrap counters as well in the case of packed virtqueue

Sure, I can add into the comments that these counters are limited to 0x7fff and the high bits are used for a wrap counter/flag.

Quoted 2 lines

or maybe it's time to rename them (since they are
invented for split virtqueue).

Should we change them to bitfields as in struct vdpa_vq_state_packed?
Or perhaps just add new fields for bool/u16/u8 last_avail_counter and last_used_counter?

That might be a later patch in order to also deal with whatever fallout happens from a new name.

sln

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

#8 lore raw

Re: [PATCH 3/3] vhost_vdpa: support PACKED when setting-getting vring_base

In reply to Jason Wang
Quoted 18 lines · Jason Wang
Quoted 17 lines · Shannon Nelson

Use the right structs for PACKED or split vqs when setting and getting the vring base.

---
  drivers/vhost/vdpa.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
drivers/vhost/vdpa.c +0 -0
index 599b8cc238c7..2543ae9d5939 100644
@@ -585,7 +585,12 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
                 if (r)
                         return r;
 
Quoted 7 lines

drivers/vhost/vdpa.c static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, · line 590

Quoted 4 lines
drivers/vhost/vdpa.c +3 -1
-               vq->last_avail_idx = vq_state.split.avail_index;
+               if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) {
+                       vq->last_avail_idx = vq_state.packed.last_avail_idx;
+                       vq->last_used_idx = vq_state.packed.last_used_idx;

I wonder if the compiler will guarantee a piggyback for the wrap counter here? If not, should we explicitly packing wrap counter?

Yes, good catch, I'll add the wrap counter.

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