Skip to contentSkip to conversation

hw/virtio: Minor housekeeping patches

PATCH Superseded Philippe Mathieu-Daudé 3 participants 12 messages Aug 26, 2021 – Sep 2, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover hw/virtio: Minor housekeeping patches

Hi,

This series contains few patches I gathered while tooking notes trying to understand issues #300-#302. Nothing very exciting :)

Philippe Mathieu-Daudé (3):
hw/virtio: Document virtio_queue_packed_empty_rcu is called within RCU hw/virtio: Remove NULL check in virtio_free_region_cache() hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

 hw/virtio/virtio.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/3 hw/virtio: Document virtio_queue_packed_empty_rcu is called within RCU

While virtio_queue_packed_empty_rcu() uses the '_rcu' suffix, it is not obvious it is called within rcu_read_lock(). All other functions from this file called with the RCU locked have a comment describing it. Document this one similarly for consistency.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index 874377f37a7..a5214bca612 100644
@@ -634,6 +634,7 @@ static int virtio_queue_split_empty(VirtQueue *vq)
     return empty;
 }
 
+/* Called within rcu_read_lock().  */
 static int virtio_queue_packed_empty_rcu(VirtQueue *vq)
 {
     struct VRingPackedDesc desc;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] hw/virtio: Document virtio_queue_packed_empty_rcu is called within RCU

Quoted 26 lines · Philippe Mathieu-Daudé

While virtio_queue_packed_empty_rcu() uses the '_rcu' suffix, it is not obvious it is called within rcu_read_lock(). All other functions from this file called with the RCU locked have a comment describing it. Document this one similarly for consistency.

---
hw/virtio/virtio.c | 1 +

1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index 874377f37a7..a5214bca612 100644
@@ -634,6 +634,7 @@ static int virtio_queue_split_empty(VirtQueue *vq)
    return empty;
 }
 
+/* Called within rcu_read_lock().  */
 static int virtio_queue_packed_empty_rcu(VirtQueue *vq)
 {
    struct VRingPackedDesc desc;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] hw/virtio: Document virtio_queue_packed_empty_rcu is called within RCU

In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

While virtio_queue_packed_empty_rcu() uses the '_rcu' suffix, it is not obvious it is called within rcu_read_lock(). All other functions from this file called with the RCU locked have a comment describing it. Document this one similarly for consistency.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
#4 lore raw

PATCH 2/3 hw/virtio: Remove NULL check in virtio_free_region_cache()

In reply to Philippe Mathieu-Daudé

virtio_free_region_cache() is called within call_rcu(), always with a non-NULL argument. Ensure new code keep it that way by replacing the NULL check by an assertion. Add a comment this function is called within call_rcu().

---
 hw/virtio/virtio.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/virtio/virtio.c +2 -4
index a5214bca612..3a1f6c520cb 100644
@@ -133,12 +133,10 @@ struct VirtQueue
     QLIST_ENTRY(VirtQueue) node;
 };
 
+/* Called within call_rcu().  */
 static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
 {
-    if (!caches) {
-        return;
-    }
-
+    assert(caches != NULL);
     address_space_cache_destroy(&caches->desc);
     address_space_cache_destroy(&caches->avail);
     address_space_cache_destroy(&caches->used);
Signature · 1 line
2.31.1
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] hw/virtio: Remove NULL check in virtio_free_region_cache()

Quoted 33 lines · Philippe Mathieu-Daudé

virtio_free_region_cache() is called within call_rcu(), always with a non-NULL argument. Ensure new code keep it that way by replacing the NULL check by an assertion. Add a comment this function is called within call_rcu().

---
hw/virtio/virtio.c | 6 ++----

1 file changed, 2 insertions(+), 4 deletions(-)
hw/virtio/virtio.c +2 -4
index a5214bca612..3a1f6c520cb 100644
@@ -133,12 +133,10 @@ struct VirtQueue
    QLIST_ENTRY(VirtQueue) node;
 };
 
+/* Called within call_rcu().  */
 static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
 {
-    if (!caches) {
-        return;
-    }
-
+    assert(caches != NULL);
    address_space_cache_destroy(&caches->desc);
    address_space_cache_destroy(&caches->avail);
    address_space_cache_destroy(&caches->used);
Signature · 1 line
2.31.1
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] hw/virtio: Remove NULL check in virtio_free_region_cache()

In reply to Philippe Mathieu-Daudé
Quoted 19 lines · Philippe Mathieu-Daudé

virtio_free_region_cache() is called within call_rcu(), always with a non-NULL argument. Ensure new code keep it that way by replacing the NULL check by an assertion. Add a comment this function is called within call_rcu().

---
 hw/virtio/virtio.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/virtio/virtio.c +1 -0
index a5214bca612..3a1f6c520cb 100644
@@ -133,12 +133,10 @@ struct VirtQueue
     QLIST_ENTRY(VirtQueue) node;
 };
 
+/* Called within call_rcu().  */

hw/virtio/virtio.c struct VirtQueue · line 142

Quoted 10 lines
hw/virtio/virtio.c +1 -4
 static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
 {
-    if (!caches) {
-        return;
-    }
-
+    assert(caches != NULL);
     address_space_cache_destroy(&caches->desc);
     address_space_cache_destroy(&caches->avail);
     address_space_cache_destroy(&caches->used);

Looks like an artifact that was left in when the code was originally introduced in commit c611c76417f52b335ecaab01c61743e3b705eb7c ("virtio:
add MemoryListener to cache ring translations"). Paolo could confirm this.

#7 lore raw

RFC PATCH 3/3 hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

In reply to Philippe Mathieu-Daudé

Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument.

---
RFC because I'm not sure this is safe enough

---
 hw/virtio/virtio.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)
hw/virtio/virtio.c +12 -17
index 3a1f6c520cb..8237693a567 100644
@@ -984,28 +984,23 @@ static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
     return VIRTQUEUE_READ_DESC_MORE;
 }
 
+/* Called within rcu_read_lock().  */
 static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
                             unsigned int *in_bytes, unsigned int *out_bytes,
-                            unsigned max_in_bytes, unsigned max_out_bytes)
+                            unsigned max_in_bytes, unsigned max_out_bytes,
+                            VRingMemoryRegionCaches *caches)
 {
     VirtIODevice *vdev = vq->vdev;
     unsigned int max, idx;
     unsigned int total_bufs, in_total, out_total;
-    VRingMemoryRegionCaches *caches;
     MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
     int64_t len = 0;
     int rc;
 
-    RCU_READ_LOCK_GUARD();
-
     idx = vq->last_avail_idx;
     total_bufs = in_total = out_total = 0;
 
     max = vq->vring.num;
-    caches = vring_get_region_caches(vq);
-    if (!caches) {
-        goto err;
-    }
 
     while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
         MemoryRegionCache *desc_cache = &caches->desc;
@@ -1124,32 +1119,28 @@ static int virtqueue_packed_read_next_desc(VirtQueue *vq,
     return VIRTQUEUE_READ_DESC_MORE;
 }
 
+/* Called within rcu_read_lock().  */
 static void virtqueue_packed_get_avail_bytes(VirtQueue *vq,
                                              unsigned int *in_bytes,
                                              unsigned int *out_bytes,
                                              unsigned max_in_bytes,
-                                             unsigned max_out_bytes)
+                                             unsigned max_out_bytes,
+                                             VRingMemoryRegionCaches *caches)
 {
     VirtIODevice *vdev = vq->vdev;
     unsigned int max, idx;
     unsigned int total_bufs, in_total, out_total;
     MemoryRegionCache *desc_cache;
-    VRingMemoryRegionCaches *caches;
     MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
     int64_t len = 0;
     VRingPackedDesc desc;
     bool wrap_counter;
 
-    RCU_READ_LOCK_GUARD();
     idx = vq->last_avail_idx;
     wrap_counter = vq->last_avail_wrap_counter;
     total_bufs = in_total = out_total = 0;
 
     max = vq->vring.num;
-    caches = vring_get_region_caches(vq);
-    if (!caches) {
-        goto err;
-    }
 
     for (;;) {
         unsigned int num_bufs = total_bufs;
@@ -1250,6 +1241,8 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
     uint16_t desc_size;
     VRingMemoryRegionCaches *caches;
 
+    RCU_READ_LOCK_GUARD();
+
     if (unlikely(!vq->vring.desc)) {
         goto err;
     }
@@ -1268,10 +1261,12 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
 
     if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
         virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes,
-                                         max_in_bytes, max_out_bytes);
+                                         max_in_bytes, max_out_bytes,
+                                         caches);
     } else {
         virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
-                                        max_in_bytes, max_out_bytes);
+                                        max_in_bytes, max_out_bytes,
+                                        caches);
     }
 
     return;
Signature · 1 line
2.31.1
#8 lore raw

Re: [RFC PATCH 3/3] hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

Quoted 8 lines · Philippe Mathieu-Daudé

Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument.

---
RFC because I'm not sure this is safe enough

It seems safe to me.

While reviewing I saw that vring_get_region_caches() has
/* Called within rcu_read_lock(). */ comment, but it seems to me that we call that function in places where we haven't acquired it, which shouldn't be a problem, but should we remove that comment?

Thanks,
Stefano

Quoted 104 lines

---
hw/virtio/virtio.c | 29 ++++++++++++-----------------

1 file changed, 12 insertions(+), 17 deletions(-)
hw/virtio/virtio.c +12 -17
index 3a1f6c520cb..8237693a567 100644
@@ -984,28 +984,23 @@ static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
    return VIRTQUEUE_READ_DESC_MORE;
 }
 
+/* Called within rcu_read_lock().  */
 static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
                            unsigned int *in_bytes, unsigned int *out_bytes,
-                            unsigned max_in_bytes, unsigned max_out_bytes)
+                            unsigned max_in_bytes, unsigned max_out_bytes,
+                            VRingMemoryRegionCaches *caches)
 {
    VirtIODevice *vdev = vq->vdev;
    unsigned int max, idx;
    unsigned int total_bufs, in_total, out_total;
-    VRingMemoryRegionCaches *caches;
    MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
    int64_t len = 0;
    int rc;
 
-    RCU_READ_LOCK_GUARD();
-
    idx = vq->last_avail_idx;
    total_bufs = in_total = out_total = 0;
 
    max = vq->vring.num;
-    caches = vring_get_region_caches(vq);
-    if (!caches) {
-        goto err;
-    }
 
    while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
        MemoryRegionCache *desc_cache = &caches->desc;
@@ -1124,32 +1119,28 @@ static int virtqueue_packed_read_next_desc(VirtQueue *vq,
    return VIRTQUEUE_READ_DESC_MORE;
 }
 
+/* Called within rcu_read_lock().  */
 static void virtqueue_packed_get_avail_bytes(VirtQueue *vq,
                                             unsigned int *in_bytes,
                                             unsigned int *out_bytes,
                                             unsigned max_in_bytes,
-                                             unsigned max_out_bytes)
+                                             unsigned max_out_bytes,
+                                             VRingMemoryRegionCaches *caches)
 {
    VirtIODevice *vdev = vq->vdev;
    unsigned int max, idx;
    unsigned int total_bufs, in_total, out_total;
    MemoryRegionCache *desc_cache;
-    VRingMemoryRegionCaches *caches;
    MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
    int64_t len = 0;
    VRingPackedDesc desc;
    bool wrap_counter;
 
-    RCU_READ_LOCK_GUARD();
    idx = vq->last_avail_idx;
    wrap_counter = vq->last_avail_wrap_counter;
    total_bufs = in_total = out_total = 0;
 
    max = vq->vring.num;
-    caches = vring_get_region_caches(vq);
-    if (!caches) {
-        goto err;
-    }
 
    for (;;) {
        unsigned int num_bufs = total_bufs;
@@ -1250,6 +1241,8 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
    uint16_t desc_size;
    VRingMemoryRegionCaches *caches;
 
+    RCU_READ_LOCK_GUARD();
+
    if (unlikely(!vq->vring.desc)) {
        goto err;
    }
@@ -1268,10 +1261,12 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
 
    if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
        virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes,
-                                         max_in_bytes, max_out_bytes);
+                                         max_in_bytes, max_out_bytes,
+                                         caches);
    } else {
        virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
-                                        max_in_bytes, max_out_bytes);
+                                        max_in_bytes, max_out_bytes,
+                                        caches);
    }
 
    return;
Signature · 1 line
2.31.1
#9 lore raw

Re: [RFC PATCH 3/3] hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

Quoted 10 lines · Stefano Garzarella
Quoted 8 lines · Philippe Mathieu-Daudé

Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument.

---
RFC because I'm not sure this is safe enough

Quoted 6 lines

It seems safe to me.

While reviewing I saw that vring_get_region_caches() has
/* Called within rcu_read_lock(). */ comment, but it seems to me that we call that function in places where we haven't acquired it, which shouldn't be a problem, but should we remove that comment?

Do you have specific examples? That sounds worrying because the caller can't do much with the returned pointer if it was fetched outside the RCU read lock.

Stefan

#10 lore raw

Re: [RFC PATCH 3/3] hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Stefano Garzarella
Quoted 8 lines · Philippe Mathieu-Daudé

Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument.

---
RFC because I'm not sure this is safe enough

It seems safe to me.

Quoted 9 lines
Quoted 4 lines

While reviewing I saw that vring_get_region_caches() has
/* Called within rcu_read_lock(). */ comment, but it seems to me that we call that function in places where we haven't acquired it, which shouldn't be a problem, but should we remove that comment?

Do you have specific examples? That sounds worrying because the caller can't do much with the returned pointer if it was fetched outside the RCU read lock.

One was the virtqueue_get_avail_bytes(), but Phil is fixing it in this patch.

Should we fix it in a separate patch to backport to stable branches?

Other suspicious places seem to be these:
- virtqueue_packed_fill_desc()
- virtqueue_packed_drop_all()

Stefano

#11 lore raw

Re: [RFC PATCH 3/3] hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees

Quoted 25 lines · Stefano Garzarella
Quoted 19 lines · Stefan Hajnoczi
Quoted 10 lines · Stefano Garzarella
Quoted 8 lines · Philippe Mathieu-Daudé

Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument.

---
RFC because I'm not sure this is safe enough

Quoted 7 lines

It seems safe to me.

While reviewing I saw that vring_get_region_caches() has
/* Called within rcu_read_lock(). */ comment, but it seems to me that we
call that function in places where we haven't acquired it, which shouldn't be a problem, but should we remove that comment?

Quoted 4 lines

Do you have specific examples? That sounds worrying because the caller can't do much with the returned pointer if it was fetched outside the RCU read lock.

Quoted 7 lines

One was the virtqueue_get_avail_bytes(), but Phil is fixing it in this patch.

Should we fix it in a separate patch to backport to stable branches?

Other suspicious places seem to be these:
- virtqueue_packed_fill_desc()

This seems to be safe in practice: only hw/net/virtio-net.c:virtio_net_receive_rcu() calls it via virtqueue_flush(). virtqueue_flush() needs a doc comment indicating that it must be called under the RCU read lock though.

Quoted 1 line

- virtqueue_packed_drop_all()

This looks broken.

Stefan