Skip to contentSkip to conversation

hw/virtio: Fix obtain the buffer id from the last descriptor

PATCH Superseded Wafer 4 participants 8 messages Apr 22, 2024 – May 9, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wafer

author
#0 lore raw

PATCH 1/1 hw/virtio: Fix obtain the buffer id from the last descriptor

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c +5 -0
index 871674f9be..f65d4b4161 100644
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
                                              desc_cache ==
                                              &indirect_desc_cache);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 25 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c +0 -0
index 871674f9be..f65d4b4161 100644

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) · line 1745

Quoted 7 lines
hw/virtio/virtio.c +4 -0
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }

It looks to me we can move this out of the loop.

Others look good.

Thanks

Quoted 7 lines
hw/virtio/virtio.c +1 -1
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

author
#2 lore raw
Quoted 36 lines · Jason Wang

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue

Quoted 25 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c index renamed from hw/virtio/virtio.c +0 -0

871674f9be..f65d4b4161 100644

hw/virtio/virtio.c +0 -0
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue

*vq, size_t sz)

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) · line 1745

Quoted 7 lines
hw/virtio/virtio.c +4 -0
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
Quoted 6 lines

It looks to me we can move this out of the loop.

Others look good.

Thanks

Thank you for your suggestion, I'll move out.

Quoted 8 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue · line 1747

Quoted 2 lines
hw/virtio/virtio.c +1 -0
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,

&i,

Quoted 5 lines
hw/virtio/virtio.c +0 -1
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

#3 lore raw
Quoted 40 lines · Wafer
Quoted 36 lines · Jason Wang

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue

Quoted 25 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c index renamed from hw/virtio/virtio.c +0 -0

871674f9be..f65d4b4161 100644

hw/virtio/virtio.c +0 -0
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue

*vq, size_t sz)

Quoted 7 lines
hw/virtio/virtio.c +4 -0
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
Quoted 9 lines
Quoted 6 lines

It looks to me we can move this out of the loop.

Others look good.

Thanks

Thank you for your suggestion, I'll move out.

Please add my

When you do.

Thanks!

Quoted 9 lines
Quoted 8 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue · line 1747

Quoted 2 lines
hw/virtio/virtio.c +1 -0
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,

&i,

Quoted 5 lines
hw/virtio/virtio.c +0 -1
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

#4 lore raw
In reply to Wafer
Quoted 6 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

Quoted 7 lines

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

This is actually trickier. While it is true the standard mandates it, both linux virtio_ring driver and QEMU trusts the ID will be the first descriptor of the chain. Does merging this change in QEMU without merging the corresponding one in the linux kernel break things? Or am I missing something?

If it breaks I guess this requires more thinking. I didn't check DPDK, neither as driver nor as vhost-user device.

Thanks!

Quoted 26 lines
Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c +5 -1
index 871674f9be..f65d4b4161 100644
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

#5 lore raw
Quoted 21 lines · Eugenio Perez Martin
Quoted 6 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

Quoted 7 lines

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

This is actually trickier. While it is true the standard mandates it, both linux virtio_ring driver and QEMU trusts the ID will be the first descriptor of the chain. Does merging this change in QEMU without merging the corresponding one in the linux kernel break things? Or am I missing something?

Quoted 4 lines

If it breaks I guess this requires more thinking. I didn't check DPDK, neither as driver nor as vhost-user device.

Thanks!

I think that if the driver is out of spec we should for starters fix it ASAP.

Quoted 26 lines
Quoted 26 lines
Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/virtio.c +5 -1
index 871674f9be..f65d4b4161 100644
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

author
#6 lore raw
Quoted 17 lines · Michael S. Tsirkin
Quoted 15 lines · Eugenio Perez Martin
Quoted 13 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Quoted 6 lines
Quoted 6 lines

This is actually trickier. While it is true the standard mandates it, both linux virtio_ring driver and QEMU trusts the ID will be the first descriptor of the chain. Does merging this change in QEMU without merging the corresponding one in the linux kernel break things? Or am I missing something?

The linux virtio_ring driver set the buffer id into all the descriptors of the chain.

So Bad things can't happen, with this patch, the Linux VirtIO driver can work properly.

I have tested it.

Quoted 6 lines
Quoted 4 lines

If it breaks I guess this requires more thinking. I didn't check DPDK, neither as driver nor as vhost-user device.

Thanks!

I think that if the driver is out of spec we should for starters fix it ASAP.

The linux driver is within spec.

Quoted 29 lines
Quoted 8 lines
Quoted 8 lines
Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 5 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue

Quoted 5 lines
hw/virtio/virtio.c index renamed from hw/virtio/virtio.c +0 -0

871674f9be..f65d4b4161 100644

hw/virtio/virtio.c +0 -0
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue

*vq, size_t sz)

Quoted 9 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)

Quoted 9 lines
hw/virtio/virtio.c +5 -0
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,

&i,

Quoted 5 lines
Quoted 5 lines
hw/virtio/virtio.c +0 -1
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0

#7 lore raw
Quoted 21 lines · Wafer
Quoted 17 lines · Michael S. Tsirkin
Quoted 15 lines · Eugenio Perez Martin
Quoted 13 lines · Wafer

The virtio-1.3 specification
<https://docs.oasis-open.org/virtio/virtio/v1.3/virtio-v1.3.html> writes:
2.8.6 Next Flag: Descriptor Chaining
Buffer ID is included in the last descriptor in the list.

If the feature (_F_INDIRECT_DESC) has been negotiated, install only one descriptor in the virtqueue.
Therefor the buffer id should be obtained from the first descriptor.

In descriptor chaining scenarios, the buffer id should be obtained from the last descriptor.

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

This is actually trickier. While it is true the standard mandates it, both linux virtio_ring driver and QEMU trusts the ID will be the first descriptor of the chain. Does merging this change in QEMU without merging the corresponding one in the linux kernel break things? Or am I missing something?

The linux virtio_ring driver set the buffer id into all the descriptors of the chain.

Ok now after reading the driver code again I see how I missed that. Sorry for the noise!

Quoted 43 lines

So Bad things can't happen, with this patch, the Linux VirtIO driver can work properly.

I have tested it.

Quoted 6 lines
Quoted 4 lines

If it breaks I guess this requires more thinking. I didn't check DPDK, neither as driver nor as vhost-user device.

Thanks!

I think that if the driver is out of spec we should for starters fix it ASAP.

The linux driver is within spec.

Quoted 29 lines
Quoted 8 lines
Quoted 8 lines
Fixes 86044b24e8 ("virtio: basic packed virtqueue support")
---
 hw/virtio/virtio.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 5 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue

Quoted 5 lines
hw/virtio/virtio.c index renamed from hw/virtio/virtio.c +0 -0

871674f9be..f65d4b4161 100644

hw/virtio/virtio.c +0 -0
@@ -1739,6 +1739,11 @@ static void *virtqueue_packed_pop(VirtQueue

*vq, size_t sz)

Quoted 9 lines

hw/virtio/virtio.c static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)

Quoted 9 lines
hw/virtio/virtio.c +5 -0
             goto err_undo_map;
         }
 
+        if (desc_cache != &indirect_desc_cache) {
+            /* Buffer ID is included in the last descriptor in the list. */
+            id = desc.id;
+        }
+
         rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,

&i,

Quoted 5 lines
Quoted 5 lines
hw/virtio/virtio.c +0 -1
                                              desc_cache ==
                                              &indirect_desc_cache);
--

2.27.0