Skip to contentSkip to conversation

virtio: zero vq->inuse in virtio_reset()

PATCH In review Stefan Hajnoczi 4 participants 4 messages Sep 7, 2016 – Sep 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: zero vq->inuse in virtio_reset()

vq->inuse must be zeroed upon device reset like most other virtqueue fields.

In theory, virtio_reset() just needs assert(vq->inuse == 0) since devices must clean up in-flight requests during reset (requests cannot not be leaked!).

In practice, it is difficult to achieve vq->inuse == 0 across reset because balloon, blk, 9p, etc implement various different strategies for cleaning up requests. Most devices call g_free(elem) directly without telling virtio.c that the VirtQueueElement is cleaned up. Therefore vq->inuse is not decremented during reset.

This patch zeroes vq->inuse and trusts that devices are not leaking VirtQueueElements across reset.

I will send a follow-up series that refactors request life-cycle across all devices and converts vq->inuse = 0 into assert(vq->inuse == 0) but this more invasive approach is not appropriate for stable trees.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index 74c085c..e8a13a5 100644
@@ -822,6 +822,7 @@ void virtio_reset(void *opaque)
         vdev->vq[i].signalled_used_valid = false;
         vdev->vq[i].notification = true;
         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
+        vdev->vq[i].inuse = 0;
     }
 }
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 38 lines · Stefan Hajnoczi

vq->inuse must be zeroed upon device reset like most other virtqueue fields.

In theory, virtio_reset() just needs assert(vq->inuse == 0) since devices must clean up in-flight requests during reset (requests cannot not be leaked!).

In practice, it is difficult to achieve vq->inuse == 0 across reset because balloon, blk, 9p, etc implement various different strategies for cleaning up requests. Most devices call g_free(elem) directly without telling virtio.c that the VirtQueueElement is cleaned up. Therefore vq->inuse is not decremented during reset.

This patch zeroes vq->inuse and trusts that devices are not leaking VirtQueueElements across reset.

I will send a follow-up series that refactors request life-cycle across all devices and converts vq->inuse = 0 into assert(vq->inuse == 0) but this more invasive approach is not appropriate for stable trees.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -1
index 74c085c..e8a13a5 100644
@@ -822,6 +822,7 @@ void virtio_reset(void *opaque)
         vdev->vq[i].signalled_used_valid = false;
         vdev->vq[i].notification = true;
         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
+        vdev->vq[i].inuse = 0;
     }
 }
 
--
Quoted 1 line

2.7.4

CCing qemu-stable

Reviewed-by
#2 lore raw
Quoted 38 lines · Stefan Hajnoczi
Quoted 37 lines · Stefan Hajnoczi

vq->inuse must be zeroed upon device reset like most other virtqueue fields.

In theory, virtio_reset() just needs assert(vq->inuse == 0) since devices must clean up in-flight requests during reset (requests cannot not be leaked!).

In practice, it is difficult to achieve vq->inuse == 0 across reset because balloon, blk, 9p, etc implement various different strategies for cleaning up requests. Most devices call g_free(elem) directly without telling virtio.c that the VirtQueueElement is cleaned up. Therefore vq->inuse is not decremented during reset.

This patch zeroes vq->inuse and trusts that devices are not leaking VirtQueueElements across reset.

I will send a follow-up series that refactors request life-cycle across all devices and converts vq->inuse = 0 into assert(vq->inuse == 0) but this more invasive approach is not appropriate for stable trees.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index 74c085c..e8a13a5 100644
@@ -822,6 +822,7 @@ void virtio_reset(void *opaque)
         vdev->vq[i].signalled_used_valid = false;
         vdev->vq[i].notification = true;
         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
+        vdev->vq[i].inuse = 0;
     }
 }
 
Quoted 4 lines
Quoted 2 lines

--
2.7.4

CCing qemu-stable

Reviewed-by on v1 1/1
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · On Wed, 7 Sep 2016 11:51:25 -0400 Stefan Hajnoczi <[email protected]> wrote:

vq->inuse must be zeroed upon device reset like most other virtqueue fields.

In theory, virtio_reset() just needs assert(vq->inuse == 0) since devices must clean up in-flight requests during reset (requests cannot not be leaked!).

In practice, it is difficult to achieve vq->inuse == 0 across reset because balloon, blk, 9p, etc implement various different strategies for cleaning up requests. Most devices call g_free(elem) directly without telling virtio.c that the VirtQueueElement is cleaned up. Therefore vq->inuse is not decremented during reset.

This patch zeroes vq->inuse and trusts that devices are not leaking VirtQueueElements across reset.

I will send a follow-up series that refactors request life-cycle across all devices and converts vq->inuse = 0 into assert(vq->inuse == 0) but this more invasive approach is not appropriate for stable trees.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +0 -0
index 74c085c..e8a13a5 100644

hw/virtio/virtio.c void virtio_reset(void *opaque) · line 828

Quoted 7 lines
hw/virtio/virtio.c +1 -0
@@ -822,6 +822,7 @@ void virtio_reset(void *opaque)
         vdev->vq[i].signalled_used_valid = false;
         vdev->vq[i].notification = true;
         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
+        vdev->vq[i].inuse = 0;
     }
 }
 

This looks sane as a minimal change, and survives my smoketests.