Skip to contentSkip to conversation

hw/virtio/vhost-shadow-virtqueue: clear ndescs after use

PATCH In review Connor Kite 2 participants 2 messages Nov 23 – Nov 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/virtio/vhost-shadow-virtqueue: clear ndescs after use

When a shadow virtqueue detaches the buffer associated with a used element, it adds the descriptor chain to the head of the free-elements list. However, this currently happens after svq->desc_state[last_used].ndescs is set to 0 in vhost_svq_get_buf, introducing a bug. Only the first element in the used descriptor chain is added back into the free-elements list, with subsequent elements being potentially orphaned.

Zero out ndescs only after the buffer is detached.

---
 hw/virtio/vhost-shadow-virtqueue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/virtio/vhost-shadow-virtqueue.c +3 -1
index 462c86dc1e..1afd3edf87 100644
@@ -604,6 +604,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
                                            uint32_t *len)
 {
     uint16_t last_used;
+    VirtQueueElement *detached_buf;
 
     if (!vhost_svq_more_used(svq)) {
         return NULL;
@@ -637,9 +638,10 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
         return NULL;
     }
 
+    detached_buf = vhost_svq_detach_buf(svq, last_used);
     svq->num_free += svq->desc_state[last_used].ndescs;
     svq->desc_state[last_used].ndescs = 0;
-    return vhost_svq_detach_buf(svq, last_used);
+    return detached_buf;
 }
 
 /**
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Connor,

Quoted 6 lines · Connor Kite

When a shadow virtqueue detaches the buffer associated with a used element, it adds the descriptor chain to the head of the free-elements list. However, this currently happens after svq->desc_state[last_used].ndescs is set to 0 in vhost_svq_get_buf, introducing a bug. Only the first element in the used descriptor chain is added back into the free-elements list, with subsequent elements being potentially orphaned.

Not really added to the 'head of the free-elements list', but the former chain tail to free_head, then makes id the new head. Suggested reword:

vhost_svq_detach_buf() uses the head descriptor's ndescs to find the last descriptor of the chain and reinsert the whole chain into the free list. vhost_svq_get_buf() currently clears ndescs before calling it, so multi-descriptor buffers are recycled as single descriptors and the remaining descriptors are orphaned.

Quoted 3 lines

Zero out ndescs only after the buffer is detached.

Fixes 86f5f2546f03 ("vhost: use SVQ element ndescs instead of opaque data for desc validation")

(Please check this is the correct commit introducing the zero-before-detach behavior).

Quoted 18 lines
---
  hw/virtio/vhost-shadow-virtqueue.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
hw/virtio/vhost-shadow-virtqueue.c +1 -0
index 462c86dc1e..1afd3edf87 100644
@@ -604,6 +604,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
                                             uint32_t *len)
  {
      uint16_t last_used;
+    VirtQueueElement *detached_buf;
 
      if (!vhost_svq_more_used(svq)) {
          return NULL;

hw/virtio/vhost-shadow-virtqueue.c static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq, · line 645

Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c +2 -1
@@ -637,9 +638,10 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
          return NULL;
      }
 
+    detached_buf = vhost_svq_detach_buf(svq, last_used);
      svq->num_free += svq->desc_state[last_used].ndescs;
      svq->desc_state[last_used].ndescs = 0;
-    return vhost_svq_detach_buf(svq, last_used);
+    return detached_buf;
  }