Skip to contentSkip to conversation

hw/virtio: Fix getting the correct ring number on loading

PATCH In review Wafer 3 participants 4 messages Nov 22, 2024 – Nov 23, 2024

lore
Share & tools
lore

Wafer

author
#0 lore raw

PATCH 1/1 hw/virtio: Fix getting the correct ring number on loading

From: Wafer Xie <[email protected]>

The virtio-1.2 specification writes:

2.7.6 The Virtqueue Available Ring:
"idx field indicates where the driver would put the next descriptor entry in the ring (modulo the queue size). This starts at 0, and increases"

The idx will increase from 0 to 0xFFFF and repeat, So idx may be less than last_avail_idx.

Fixes 616a6552 (virtio: add endian-ambivalent support to VirtIODevice)
---
 hw/virtio/virtio.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +7 -1
index a26f18908e..ae7d407113 100644
@@ -3362,7 +3362,13 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
                 continue;
             }
 
-            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
+            if (vring_avail_idx(&vdev->vq[i]) >= vdev->vq[i].last_avail_idx) {
+                nheads = vring_avail_idx(&vdev->vq[i]) -
+                         vdev->vq[i].last_avail_idx;
+            } else {
+                nheads = UINT16_MAX - vdev->vq[i].last_avail_idx +
+                         vring_avail_idx(&vdev->vq[i]) + 1;
+            }
             /* Check it isn't doing strange things with descriptor numbers. */
             if (nheads > vdev->vq[i].vring.num) {
                 virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
Signature · 1 line
2.31.1
#1 lore raw

Hi Wafer,

Quoted 7 lines · Wafer

From: Wafer Xie <[email protected]>

The virtio-1.2 specification writes:

2.7.6 The Virtqueue Available Ring:
"idx field indicates where the driver would put the next descriptor entry in the ring (modulo the queue size). This starts at 0, and increases"

"modulo" ...

Quoted 5 lines

The idx will increase from 0 to 0xFFFF and repeat, So idx may be less than last_avail_idx.

Fixes 616a6552 (virtio: add endian-ambivalent support to VirtIODevice)

This commit is only about endianness... Do you mean 1abeb5a65d ("virtio: fix up VQ checks") or 258dc7c96b ("virtio: sanity-check available index")?

Quoted 12 lines
---
  hw/virtio/virtio.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +0 -0
index a26f18908e..ae7d407113 100644
@@ -3362,7 +3362,13 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
                  continue;

hw/virtio/virtio.c virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 3371

Quoted 10 lines
hw/virtio/virtio.c +7 -1
              }
 
-            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
+            if (vring_avail_idx(&vdev->vq[i]) >= vdev->vq[i].last_avail_idx) {
+                nheads = vring_avail_idx(&vdev->vq[i]) -
+                         vdev->vq[i].last_avail_idx;
+            } else {
+                nheads = UINT16_MAX - vdev->vq[i].last_avail_idx +
+                         vring_avail_idx(&vdev->vq[i]) + 1;
+            }

... nheads %= UINT16_MAX; ?

Quoted 3 lines
hw/virtio/virtio.c +0 -0
              /* Check it isn't doing strange things with descriptor numbers. */
              if (nheads > vdev->vq[i].vring.num) {
                  virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
#2 lore raw
Quoted 14 lines · On Fri, 22 Nov 2024 08:01:34 +0100 Philippe Mathieu-Daudé <[email protected]>…

Hi Wafer,

Quoted 7 lines · Wafer

From: Wafer Xie <[email protected]>

The virtio-1.2 specification writes:

2.7.6 The Virtqueue Available Ring:
"idx field indicates where the driver would put the next descriptor entry in the ring (modulo the queue size). This starts at 0, and increases"

"modulo" ...

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

The idx will increase from 0 to 0xFFFF and repeat, So idx may be less than last_avail_idx.

Fixes 616a6552 (virtio: add endian-ambivalent support to VirtIODevice)

This commit is only about endianness...

I totally agree with you Philippe (bonjour BTW). Commit 616a6552 simply moves pre-existing VQ check code around. Contributor should git blame deeper ;-)

Quoted 4 lines

Do you mean 1abeb5a65d
("virtio: fix up VQ checks") or 258dc7c96b ("virtio: sanity-check available index")?

The code this patch is changing was originally introduced by the latter.

Quoted 14 lines
Quoted 14 lines
---
  hw/virtio/virtio.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +0 -0
index a26f18908e..ae7d407113 100644
@@ -3362,7 +3362,13 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
                  continue;
              }
 
Quoted 11 lines

hw/virtio/virtio.c virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 3371

Quoted 8 lines
hw/virtio/virtio.c +7 -1
-            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
+            if (vring_avail_idx(&vdev->vq[i]) >= vdev->vq[i].last_avail_idx) {
+                nheads = vring_avail_idx(&vdev->vq[i]) -
+                         vdev->vq[i].last_avail_idx;
+            } else {
+                nheads = UINT16_MAX - vdev->vq[i].last_avail_idx +
+                         vring_avail_idx(&vdev->vq[i]) + 1;
+            }

... nheads %= UINT16_MAX; ?

Exactly and since everything is uint16_t there, we get that for free with the existing code base. IOW both arms of the if produce exactly the same result... I don't know the motivation behind this patch but it does not change anything.

Cheers,

--
Greg

Quoted 4 lines
Quoted 3 lines
hw/virtio/virtio.c +0 -0
              /* Check it isn't doing strange things with descriptor numbers. */
              if (nheads > vdev->vq[i].vring.num) {
                  virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
author
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 33 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: 2024年11月22日 15:02
To: Wafer <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; Angus Chen <[email protected]>
Subject: Re: [PATCH] hw/virtio: Fix getting the correct ring number on loading

External Mail: This email originated from OUTSIDE of the organization! Do not click links, open attachments or provide ANY information unless you recognize the sender and know the content is safe.

Hi Wafer,

Quoted 7 lines · Wafer

From: Wafer Xie <[email protected]>

The virtio-1.2 specification writes:

2.7.6 The Virtqueue Available Ring:
"idx field indicates where the driver would put the next descriptor entry in the ring (modulo the queue size). This starts at 0, and increases"

"modulo" ...

Quoted 4 lines

The idx will increase from 0 to 0xFFFF and repeat, So idx may be less than last_avail_idx.

Quoted 6 lines
Quoted 2 lines
Fixes 616a6552 (virtio: add endian-ambivalent support to

VirtIODevice)

This commit is only about endianness... Do you mean 1abeb5a65d ("virtio: fix up VQ checks") or 258dc7c96b ("virtio: sanity-check available index")?

Thanks, I mean 258dc7c96b ("virtio: sanity-check available index") I will make changes in the next version.

Quoted 25 lines

hw/virtio/virtio.c virtio_load(VirtIODevice *vdev, QEMUFile *f, int

Quoted 12 lines
---
  hw/virtio/virtio.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/virtio/virtio.c index renamed from hw/virtio/virtio.c +0 -0

a26f18908e..ae7d407113 100644

hw/virtio/virtio.c +0 -0
@@ -3362,7 +3362,13 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int

version_id)

hw/virtio/virtio.c virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 3371

Quoted 11 lines
hw/virtio/virtio.c +7 -1
                  continue;
              }
 
-            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
+            if (vring_avail_idx(&vdev->vq[i]) >= vdev->vq[i].last_avail_idx) {
+                nheads = vring_avail_idx(&vdev->vq[i]) -
+                         vdev->vq[i].last_avail_idx;
+            } else {
+                nheads = UINT16_MAX - vdev->vq[i].last_avail_idx +
+                         vring_avail_idx(&vdev->vq[i]) + 1;
+            }
Quoted 1 line

... nheads %= UINT16_MAX; ?

nheads cannot exceed UINT16_MAX, but is invalid if it exceed vring.num

Quoted 3 lines
Quoted 3 lines
hw/virtio/virtio.c +0 -0
              /* Check it isn't doing strange things with descriptor numbers. */
              if (nheads > vdev->vq[i].vring.num) {
                  virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "