Skip to contentSkip to conversation

vdpa: Fix endian bugs in shadow virtqueue

PATCH In review Konstantin Shkolnyy 5 participants 13 messages Feb 12, 2025 – Feb 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vdpa: Fix endian bugs in shadow virtqueue

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

---
Changes in v2: Change desc_next[] from LE format to "CPU".

 hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +10 -8
index 37aca8b431..4af0d7c669 100644
@@ -165,10 +165,10 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,
         descs[i].len = cpu_to_le32(iovec[n].iov_len);
 
         last = i;
-        i = cpu_to_le16(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
-    svq->free_head = le16_to_cpu(svq->desc_next[last]);
+    svq->free_head = svq->desc_next[last];
     return true;
 }
 
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
     smp_mb();
 
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));
         needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
     } else {
-        needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
+        needs_kick =
+                !(svq->vring.used->flags & cpu_to_le16(VRING_USED_F_NO_NOTIFY));
     }
 
     if (!needs_kick) {
@@ -365,7 +367,7 @@ static bool vhost_svq_more_used(VhostShadowVirtqueue *svq)
         return true;
     }
 
-    svq->shadow_used_idx = cpu_to_le16(*(volatile uint16_t *)used_idx);
+    svq->shadow_used_idx = le16_to_cpu(*(volatile uint16_t *)used_idx);
 
     return svq->last_used_idx != svq->shadow_used_idx;
 }
@@ -383,7 +385,7 @@ static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq)
 {
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
         uint16_t *used_event = (uint16_t *)&svq->vring.avail->ring[svq->vring.num];
-        *used_event = svq->shadow_used_idx;
+        *used_event = cpu_to_le16(svq->shadow_used_idx);
     } else {
         svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
     }
@@ -408,7 +410,7 @@ static uint16_t vhost_svq_last_desc_of_chain(const VhostShadowVirtqueue *svq,
                                              uint16_t num, uint16_t i)
 {
     for (uint16_t j = 0; j < (num - 1); ++j) {
-        i = le16_to_cpu(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
     return i;
@@ -683,7 +685,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
     svq->desc_state = g_new0(SVQDescState, svq->vring.num);
     svq->desc_next = g_new0(uint16_t, svq->vring.num);
     for (unsigned i = 0; i < svq->vring.num - 1; i++) {
-        svq->desc_next[i] = cpu_to_le16(i + 1);
+        svq->desc_next[i] = i + 1;
     }
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 12 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

---
Changes in v2: Change desc_next[] from LE format to "CPU".

  hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
  1 file changed, 10 insertions(+), 8 deletions(-)

hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_kick(VhostShadowVirtqueue *svq) · line 232

Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
      smp_mb();
 
      if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));

Nitpicking, sometimes using the ld/st API is cleaner (here lduw_le_p).

Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
          needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
      } else {
-        needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
+        needs_kick =
+                !(svq->vring.used->flags & cpu_to_le16(VRING_USED_F_NO_NOTIFY));
      }
#2 lore raw
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

---
Changes in v2: Change desc_next[] from LE format to "CPU".

  hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
  1 file changed, 10 insertions(+), 8 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +0 -0
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
      smp_mb();
 
Quoted 7 lines

hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_kick(VhostShadowVirtqueue *svq) · line 232

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
      if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));

Nitpicking, sometimes using the ld/st API is cleaner (here lduw_le_p).

I'm not sure if it is right in SVQ, as it is not accessing guest memory but QEMU memory that has been mapped to a device. But if you think it is still a valid use case for ld* and st* family I'd be totally ok with that usage.

Quoted 7 lines
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
          needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
      } else {
-        needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
+        needs_kick =
+                !(svq->vring.used->flags & cpu_to_le16(VRING_USED_F_NO_NOTIFY));
      }
#3 lore raw
Quoted 26 lines · Eugenio Perez Martin
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

---
Changes in v2: Change desc_next[] from LE format to "CPU".

   hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
   1 file changed, 10 insertions(+), 8 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +0 -0
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
       smp_mb();
 
Quoted 7 lines

hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_kick(VhostShadowVirtqueue *svq) · line 232

Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
       if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));

Nitpicking, sometimes using the ld/st API is cleaner (here lduw_le_p).

Quoted 4 lines

I'm not sure if it is right in SVQ, as it is not accessing guest memory but QEMU memory that has been mapped to a device. But if you think it is still a valid use case for ld* and st* family I'd be totally ok with that usage.

No need to change, better use a consistent API over the file.

Regards,

Phil.

Acked-by on v2 1/1
#4 lore raw
In reply to Konstantin Shkolnyy
Quoted 4 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

Fixes 10857ec0ad ("vhost: Add VhostShadowVirtqueue")
Quoted 1 line

Thanks!

Quoted 77 lines

---
Changes in v2: Change desc_next[] from LE format to "CPU".

 hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +10 -9
index 37aca8b431..4af0d7c669 100644
@@ -165,10 +165,10 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,
         descs[i].len = cpu_to_le32(iovec[n].iov_len);
 
         last = i;
-        i = cpu_to_le16(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
-    svq->free_head = le16_to_cpu(svq->desc_next[last]);
+    svq->free_head = svq->desc_next[last];
     return true;
 }
 
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
     smp_mb();
 
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));
         needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
     } else {
-        needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
+        needs_kick =
+                !(svq->vring.used->flags & cpu_to_le16(VRING_USED_F_NO_NOTIFY));
     }
 
     if (!needs_kick) {
@@ -365,7 +367,7 @@ static bool vhost_svq_more_used(VhostShadowVirtqueue *svq)
         return true;
     }
 
-    svq->shadow_used_idx = cpu_to_le16(*(volatile uint16_t *)used_idx);
+    svq->shadow_used_idx = le16_to_cpu(*(volatile uint16_t *)used_idx);
 
     return svq->last_used_idx != svq->shadow_used_idx;
 }
@@ -383,7 +385,7 @@ static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq)
 {
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
         uint16_t *used_event = (uint16_t *)&svq->vring.avail->ring[svq->vring.num];
-        *used_event = svq->shadow_used_idx;
+        *used_event = cpu_to_le16(svq->shadow_used_idx);
     } else {
         svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
     }
@@ -408,7 +410,7 @@ static uint16_t vhost_svq_last_desc_of_chain(const VhostShadowVirtqueue *svq,
                                              uint16_t num, uint16_t i)
 {
     for (uint16_t j = 0; j < (num - 1); ++j) {
-        i = le16_to_cpu(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
     return i;
@@ -683,7 +685,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
     svq->desc_state = g_new0(SVQDescState, svq->vring.num);
     svq->desc_next = g_new0(uint16_t, svq->vring.num);
     for (unsigned i = 0; i < svq->vring.num - 1; i++) {
-        svq->desc_next[i] = cpu_to_le16(i + 1);
+        svq->desc_next[i] = i + 1;
     }
 }
 
--

2.34.1

Tested-by
#5 lore raw

I tested this patch with vdpa's regression tests, everything works fine.

Quoted 94 lines · Eugenio Perez Martin
Quoted 4 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

Fixes 10857ec0ad ("vhost: Add VhostShadowVirtqueue")
Quoted 1 line

Thanks!

Quoted 77 lines

---
Changes in v2: Change desc_next[] from LE format to "CPU".

 hw/virtio/vhost-shadow-virtqueue.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +10 -9
index 37aca8b431..4af0d7c669 100644
@@ -165,10 +165,10 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,
         descs[i].len = cpu_to_le32(iovec[n].iov_len);
 
         last = i;
-        i = cpu_to_le16(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
-    svq->free_head = le16_to_cpu(svq->desc_next[last]);
+    svq->free_head = svq->desc_next[last];
     return true;
 }
 
@@ -228,10 +228,12 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq)
     smp_mb();
 
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
-        uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
+        uint16_t avail_event = le16_to_cpu(
+                *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]));
         needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
     } else {
-        needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
+        needs_kick =
+                !(svq->vring.used->flags & cpu_to_le16(VRING_USED_F_NO_NOTIFY));
     }
 
     if (!needs_kick) {
@@ -365,7 +367,7 @@ static bool vhost_svq_more_used(VhostShadowVirtqueue *svq)
         return true;
     }
 
-    svq->shadow_used_idx = cpu_to_le16(*(volatile uint16_t *)used_idx);
+    svq->shadow_used_idx = le16_to_cpu(*(volatile uint16_t *)used_idx);
 
     return svq->last_used_idx != svq->shadow_used_idx;
 }
@@ -383,7 +385,7 @@ static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq)
 {
     if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
         uint16_t *used_event = (uint16_t *)&svq->vring.avail->ring[svq->vring.num];
-        *used_event = svq->shadow_used_idx;
+        *used_event = cpu_to_le16(svq->shadow_used_idx);
     } else {
         svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
     }
@@ -408,7 +410,7 @@ static uint16_t vhost_svq_last_desc_of_chain(const VhostShadowVirtqueue *svq,
                                              uint16_t num, uint16_t i)
 {
     for (uint16_t j = 0; j < (num - 1); ++j) {
-        i = le16_to_cpu(svq->desc_next[i]);
+        i = svq->desc_next[i];
     }
 
     return i;
@@ -683,7 +685,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
     svq->desc_state = g_new0(SVQDescState, svq->vring.num);
     svq->desc_next = g_new0(uint16_t, svq->vring.num);
     for (unsigned i = 0; i < svq->vring.num - 1; i++) {
-        svq->desc_next[i] = cpu_to_le16(i + 1);
+        svq->desc_next[i] = i + 1;
     }
 }
 
--

2.34.1

#8 lore raw
Quoted 7 lines · Konstantin Shkolnyy
Quoted 3 lines · Lei Yang

I tested this patch with vdpa's regression tests, everything works fine.

Could you point me to those tests?

Sure, the test scenarios include ping, mq tests under netperf stress, hotplug/unplug, reboot,shutdown, pxe_boot etc.

Quoted 1 line
#10 lore raw
Quoted 8 lines · Michael Tokarev
Quoted 4 lines · Konstantin Shkolnyy

VDPA didn't work on a big-endian machine due to missing/incorrect CPU<->LE data format conversions.

This looks like a qemu-stable material.
Please let me know if it is not.

It won't help without my other "[PATCH v2] vdpa: Allow vDPA to work on big-endian machine". With both patches, VDPA works on a big-endian machine.

#11 lore raw
Quoted 6 lines · Konstantin Shkolnyy
Quoted 2 lines · Michael Tokarev

This looks like a qemu-stable material.
Please let me know if it is not.

It won't help without my other "[PATCH v2] vdpa: Allow vDPA to work on big-endian machine". With both patches, VDPA works on a big-endian machine.

Aha. And it is not in master yet. Thank you for letting me know!

How do you think, is it worth the effort to pick these up for older stable releases (7.2, 8.2) too?

Thanks,

/mjt

#12 lore raw
Quoted 6 lines · Michael Tokarev
Quoted 5 lines · Konstantin Shkolnyy
Quoted 2 lines · Michael Tokarev

This looks like a qemu-stable material.
Please let me know if it is not.

Quoted 6 lines
Quoted 1 line

It won't help without my other "[PATCH v2] vdpa: Allow vDPA to work on big-endian machine". With both patches, VDPA works on a big-endian machine.

Aha. And it is not in master yet.  Thank you for letting me know!

How do you think, is it worth the effort to pick these up for older stable releases (7.2, 8.2) too?

Yes. It's legitimate bugfixes. I suspect, more and more people will use VDPA as time goes by, so someone might try it on s390 with a stable QEMU.