Skip to contentSkip to conversation

hw/accel: Use RCU_READ macros

PATCH In review Philippe Mathieu-Daudé 6 participants 20 messages Jan 24, 2024 – Feb 16, 2024

lore
Share & tools
lore
#0 lore raw

cover hw/accel: Use RCU_READ macros

Use RCU_READ_LOCK_GUARD and WITH_RCU_READ_LOCK_GUARD to replace the manual rcu_read_(un)lock calls.

Philippe Mathieu-Daudé (6):
accel/tcg/cpu-exec: Use RCU_READ macro hw/9pfs/9p-synth: Use RCU_READ macro hw/display/virtio-gpu-udmabuf: Use RCU_READ macro hw/scsi/virtio-scsi: Use RCU_READ macro hw/vfio/common: Use RCU_READ macros hw/virtio/vhost: Use RCU_READ macro

 accel/tcg/cpu-exec.c            | 24 +++++++++++------------
 hw/9pfs/9p-synth.c              | 24 +++++++++++------------
 hw/display/virtio-gpu-udmabuf.c |  6 +++---
 hw/scsi/virtio-scsi.c           | 12 ++++++------
 hw/vfio/common.c                | 34 ++++++++++++++++-----------------
 hw/virtio/vhost.c               |  6 +++---
 6 files changed, 52 insertions(+), 54 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/6 accel/tcg/cpu-exec: Use RCU_READ macro

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 accel/tcg/cpu-exec.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
accel/tcg/cpu-exec.c +12 -12
index 67eda9865e..6b3f66930e 100644
@@ -1070,21 +1070,21 @@ int cpu_exec(CPUState *cpu)
         return EXCP_HALTED;
     }
 
-    rcu_read_lock();
-    cpu_exec_enter(cpu);
+    WITH_RCU_READ_LOCK_GUARD() {
+        cpu_exec_enter(cpu);
 
-    /*
-     * Calculate difference between guest clock and host clock.
-     * This delay includes the delay of the last cycle, so
-     * what we have to do is sleep until it is 0. As for the
-     * advance/delay we gain here, we try to fix it next time.
-     */
-    init_delay_params(&sc, cpu);
+        /*
+         * Calculate difference between guest clock and host clock.
+         * This delay includes the delay of the last cycle, so
+         * what we have to do is sleep until it is 0. As for the
+         * advance/delay we gain here, we try to fix it next time.
+         */
+        init_delay_params(&sc, cpu);
 
-    ret = cpu_exec_setjmp(cpu, &sc);
+        ret = cpu_exec_setjmp(cpu, &sc);
 
-    cpu_exec_exit(cpu);
-    rcu_read_unlock();
+        cpu_exec_exit(cpu);
+    };
 
     return ret;
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] accel/tcg/cpu-exec: Use RCU_READ macro

Quoted 50 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
accel/tcg/cpu-exec.c | 24 ++++++++++++------------

1 file changed, 12 insertions(+), 12 deletions(-)
accel/tcg/cpu-exec.c +12 -12
index 67eda9865e..6b3f66930e 100644
@@ -1070,21 +1070,21 @@ int cpu_exec(CPUState *cpu)
        return EXCP_HALTED;
    }
 
-    rcu_read_lock();
-    cpu_exec_enter(cpu);
+    WITH_RCU_READ_LOCK_GUARD() {
+        cpu_exec_enter(cpu);
 
-    /*
-     * Calculate difference between guest clock and host clock.
-     * This delay includes the delay of the last cycle, so
-     * what we have to do is sleep until it is 0. As for the
-     * advance/delay we gain here, we try to fix it next time.
-     */
-    init_delay_params(&sc, cpu);
+        /*
+         * Calculate difference between guest clock and host clock.
+         * This delay includes the delay of the last cycle, so
+         * what we have to do is sleep until it is 0. As for the
+         * advance/delay we gain here, we try to fix it next time.
+         */
+        init_delay_params(&sc, cpu);
 
-    ret = cpu_exec_setjmp(cpu, &sc);
+        ret = cpu_exec_setjmp(cpu, &sc);
 
-    cpu_exec_exit(cpu);
-    rcu_read_unlock();
+        cpu_exec_exit(cpu);
+    };
 
    return ret;
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 1/6
#3 lore raw

Re: [PATCH 1/6] accel/tcg/cpu-exec: Use RCU_READ macro

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

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
  accel/tcg/cpu-exec.c | 24 ++++++++++++------------
  1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 1 line

+ };

Stray ;

r~

#4 lore raw

Re: [PATCH 1/6] accel/tcg/cpu-exec: Use RCU_READ macro

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

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
  accel/tcg/cpu-exec.c | 24 ++++++++++++------------
  1 file changed, 12 insertions(+), 12 deletions(-)
accel/tcg/cpu-exec.c +9 -9
index 67eda9865e..6b3f66930e 100644
@@ -1070,21 +1070,21 @@ int cpu_exec(CPUState *cpu)
          return EXCP_HALTED;
      }
 
-    rcu_read_lock();
-    cpu_exec_enter(cpu);
+    WITH_RCU_READ_LOCK_GUARD() {
+        cpu_exec_enter(cpu);
 
-    /*
-     * Calculate difference between guest clock and host clock.
-     * This delay includes the delay of the last cycle, so
-     * what we have to do is sleep until it is 0. As for the
-     * advance/delay we gain here, we try to fix it next time.
-     */
-    init_delay_params(&sc, cpu);
+        /*
+         * Calculate difference between guest clock and host clock.
+         * This delay includes the delay of the last cycle, so
+         * what we have to do is sleep until it is 0. As for the
+         * advance/delay we gain here, we try to fix it next time.
+         */
+        init_delay_params(&sc, cpu);
 

accel/tcg/cpu-exec.c int cpu_exec(CPUState *cpu) · line 1090

Quoted 10 lines
accel/tcg/cpu-exec.c +3 -3
-    ret = cpu_exec_setjmp(cpu, &sc);
+        ret = cpu_exec_setjmp(cpu, &sc);
 
-    cpu_exec_exit(cpu);
-    rcu_read_unlock();
+        cpu_exec_exit(cpu);
+    };
 
      return ret;
  }

I've tweaked this to use RCU_READ_LOCK_GUARD instead -- there's little point
in excluding the return from the block, and thus avoid the re-indent -- and queued this one patch to tcg-next.

r~

#5 lore raw

PATCH 2/6 hw/9pfs/9p-synth: Use RCU_READ macro

In reply to Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/9pfs/9p-synth.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
hw/9pfs/9p-synth.c +12 -12
index 0ac79a500b..419ea69e3a 100644
@@ -241,15 +241,15 @@ static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
     int i = 0;
     V9fsSynthNode *node;
 
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir->child, sibling) {
-        /* This is the off child of the directory */
-        if (i == off) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir->child, sibling) {
+            /* This is the off child of the directory */
+            if (i == off) {
+                break;
+            }
+            i++;
         }
-        i++;
     }
-    rcu_read_unlock();
     if (!node) {
         /* end of directory */
         return NULL;
@@ -494,13 +494,13 @@ static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
         goto out;
     }
     /* search for the name in the children */
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir_node->child, sibling) {
-        if (!strcmp(node->name, name)) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir_node->child, sibling) {
+            if (!strcmp(node->name, name)) {
+                break;
+            }
         }
     }
-    rcu_read_unlock();
 
     if (!node) {
         errno = ENOENT;
Signature · 1 line
2.41.0
Acked-by on v1 2/6
#6 lore raw

Re: [PATCH 2/6] hw/9pfs/9p-synth: Use RCU_READ macro

Quoted 9 lines · On Wed, 24 Jan 2024 08:41:57 +0100 Philippe Mathieu-Daudé <[email protected]>…

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/9pfs/9p-synth.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 46 lines
hw/9pfs/9p-synth.c +12 -12
index 0ac79a500b..419ea69e3a 100644
@@ -241,15 +241,15 @@ static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
     int i = 0;
     V9fsSynthNode *node;
 
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir->child, sibling) {
-        /* This is the off child of the directory */
-        if (i == off) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir->child, sibling) {
+            /* This is the off child of the directory */
+            if (i == off) {
+                break;
+            }
+            i++;
         }
-        i++;
     }
-    rcu_read_unlock();
     if (!node) {
         /* end of directory */
         return NULL;
@@ -494,13 +494,13 @@ static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
         goto out;
     }
     /* search for the name in the children */
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir_node->child, sibling) {
-        if (!strcmp(node->name, name)) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir_node->child, sibling) {
+            if (!strcmp(node->name, name)) {
+                break;
+            }
         }
     }
-    rcu_read_unlock();
 
     if (!node) {
         errno = ENOENT;
Signature · 1 line
Greg
Acked-by on v1 2/6
#7 lore raw

Re: [PATCH 2/6] hw/9pfs/9p-synth: Use RCU_READ macro

In reply to Philippe Mathieu-Daudé
Quoted 6 lines · CET Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---

Quoted 50 lines
 hw/9pfs/9p-synth.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
hw/9pfs/9p-synth.c +12 -12
index 0ac79a500b..419ea69e3a 100644
@@ -241,15 +241,15 @@ static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
     int i = 0;
     V9fsSynthNode *node;
 
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir->child, sibling) {
-        /* This is the off child of the directory */
-        if (i == off) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir->child, sibling) {
+            /* This is the off child of the directory */
+            if (i == off) {
+                break;
+            }
+            i++;
         }
-        i++;
     }
-    rcu_read_unlock();
     if (!node) {
         /* end of directory */
         return NULL;
@@ -494,13 +494,13 @@ static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
         goto out;
     }
     /* search for the name in the children */
-    rcu_read_lock();
-    QLIST_FOREACH(node, &dir_node->child, sibling) {
-        if (!strcmp(node->name, name)) {
-            break;
+    WITH_RCU_READ_LOCK_GUARD() {
+        QLIST_FOREACH(node, &dir_node->child, sibling) {
+            if (!strcmp(node->name, name)) {
+                break;
+            }
         }
     }
-    rcu_read_unlock();
 
     if (!node) {
         errno = ENOENT;
#8 lore raw

PATCH 3/6 hw/display/virtio-gpu-udmabuf: Use RCU_READ macro

In reply to Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/display/virtio-gpu-udmabuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-udmabuf.c +3 -3
index d51184d658..0ee6685803 100644
@@ -42,9 +42,9 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
                      sizeof(struct udmabuf_create_item) * res->iov_cnt);
 
     for (i = 0; i < res->iov_cnt; i++) {
-        rcu_read_lock();
-        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
-        rcu_read_unlock();
+        WITH_RCU_READ_LOCK_GUARD() {
+            rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
+        }
 
         if (!rb || rb->fd < 0) {
             g_free(list);
Signature · 1 line
2.41.0
Reviewed-by on v1 3/6
#9 lore raw

Re: [PATCH 3/6] hw/display/virtio-gpu-udmabuf: Use RCU_READ macro

Quoted 28 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/display/virtio-gpu-udmabuf.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-udmabuf.c +3 -3
index d51184d658..0ee6685803 100644
@@ -42,9 +42,9 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
                     sizeof(struct udmabuf_create_item) * res->iov_cnt);
 
    for (i = 0; i < res->iov_cnt; i++) {
-        rcu_read_lock();
-        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
-        rcu_read_unlock();
+        WITH_RCU_READ_LOCK_GUARD() {
+            rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
+        }
 
        if (!rb || rb->fd < 0) {
            g_free(list);
Signature · 1 line
2.41.0
#10 lore raw

PATCH 4/6 hw/scsi/virtio-scsi: Use RCU_READ macro

In reply to Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/scsi/virtio-scsi.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
hw/scsi/virtio-scsi.c +6 -6
index 690aceec45..998227404a 100644
@@ -343,14 +343,14 @@ static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq *req)
         target = req->req.tmf.lun[1];
         qatomic_inc(&s->resetting);
 
-        rcu_read_lock();
-        QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
-            SCSIDevice *d1 = SCSI_DEVICE(kid->child);
-            if (d1->channel == 0 && d1->id == target) {
-                device_cold_reset(&d1->qdev);
+        WITH_RCU_READ_LOCK_GUARD() {
+            QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
+                SCSIDevice *d1 = SCSI_DEVICE(kid->child);
+                if (d1->channel == 0 && d1->id == target) {
+                    device_cold_reset(&d1->qdev);
+                }
             }
         }
-        rcu_read_unlock();
 
         qatomic_dec(&s->resetting);
         break;
Signature · 1 line
2.41.0
Reviewed-by on v1 4/6
#11 lore raw

Re: [PATCH 4/6] hw/scsi/virtio-scsi: Use RCU_READ macro

Quoted 37 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/scsi/virtio-scsi.c | 12 ++++++------

1 file changed, 6 insertions(+), 6 deletions(-)
hw/scsi/virtio-scsi.c +6 -6
index 690aceec45..998227404a 100644
@@ -343,14 +343,14 @@ static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq *req)
        target = req->req.tmf.lun[1];
        qatomic_inc(&s->resetting);
 
-        rcu_read_lock();
-        QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
-            SCSIDevice *d1 = SCSI_DEVICE(kid->child);
-            if (d1->channel == 0 && d1->id == target) {
-                device_cold_reset(&d1->qdev);
+        WITH_RCU_READ_LOCK_GUARD() {
+            QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
+                SCSIDevice *d1 = SCSI_DEVICE(kid->child);
+                if (d1->channel == 0 && d1->id == target) {
+                    device_cold_reset(&d1->qdev);
+                }
            }
        }
-        rcu_read_unlock();
 
        qatomic_dec(&s->resetting);
        break;
Signature · 1 line
2.41.0

Unrelated to your patch: I just noticed in hw/scsi/virtio-scsi.c, s->resetting is used to flag whether the bus is resetting; but there's no check if a resetting is taking place before starting another. Is this single threaded code so it's not necessary?

As for the patch:

#12 lore raw

Re: [PATCH 4/6] hw/scsi/virtio-scsi: Use RCU_READ macro

Quoted 11 lines · Manos Pitsidianakis
Quoted 8 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/scsi/virtio-scsi.c | 12 ++++++------

1 file changed, 6 insertions(+), 6 deletions(-)

Unrelated to your patch: I just noticed in hw/scsi/virtio-scsi.c, s->resetting is used to flag whether the bus is resetting; but there's no check if a resetting is taking place before starting another. Is this single threaded code so it's not necessary?

No clue about this device, I'll let the maintainers answer you :)

#13 lore raw

PATCH 5/6 hw/vfio/common: Use RCU_READ macros

In reply to Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the *RCU_READ_LOCK_GUARD macros (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/vfio/common.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
hw/vfio/common.c +16 -18
index 4aa86f563c..09878a3603 100644
@@ -308,13 +308,13 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
         return;
     }
 
-    rcu_read_lock();
+    RCU_READ_LOCK_GUARD();
 
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
-            goto out;
+            return;
         }
         /*
          * vaddr is only valid until rcu_read_unlock(). But after
@@ -343,8 +343,6 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
             vfio_set_migration_error(ret);
         }
     }
-out:
-    rcu_read_unlock();
 }
 
 static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl,
@@ -1223,23 +1221,23 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if (iotlb->target_as != &address_space_memory) {
         error_report("Wrong target AS \"%s\", only system memory is allowed",
                      iotlb->target_as->name ? iotlb->target_as->name : "none");
-        goto out;
-    }
-
-    rcu_read_lock();
-    if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
-        ret = vfio_get_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
-                                    translated_addr);
-        if (ret) {
-            error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%s)",
-                         bcontainer, iova, iotlb->addr_mask + 1, ret,
-                         strerror(-ret));
+    } else {
+        WITH_RCU_READ_LOCK_GUARD() {
+            if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
+                ret = vfio_get_dirty_bitmap(bcontainer, iova,
+                                            iotlb->addr_mask + 1,
+                                            translated_addr);
+                if (ret) {
+                    error_report("vfio_iommu_map_dirty_notify(%p,"
+                                 " 0x%"HWADDR_PRIx
+                                 ", 0x%"HWADDR_PRIx") = %d (%s)",
+                                 bcontainer, iova, iotlb->addr_mask + 1, ret,
+                                 strerror(-ret));
+                }
+            }
         }
     }
-    rcu_read_unlock();
 
-out:
     if (ret) {
         vfio_set_migration_error(ret);
     }
Signature · 1 line
2.41.0
Reviewed-by on v1 5/6
#14 lore raw

Re: [PATCH 5/6] hw/vfio/common: Use RCU_READ macros

Quoted 16 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the *RCU_READ_LOCK_GUARD macros (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/vfio/common.c | 34 ++++++++++++++++------------------

1 file changed, 16 insertions(+), 18 deletions(-)
hw/vfio/common.c +0 -0
index 4aa86f563c..09878a3603 100644
@@ -308,13 +308,13 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
        return;
    }

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 317

Quoted 10 lines
hw/vfio/common.c +2 -2
 
-    rcu_read_lock();
+    RCU_READ_LOCK_GUARD();
 
    if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
        bool read_only;
 
        if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
-            goto out;
+            return;

Since this is the only early return, we could alternatively do:

- if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
+ if (vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {

remove the goto/return, and wrap the rest of the codeflow in this if's brackets. And then we could use WITH_RCU_READ_LOCK_GUARD instead. That'd increase the code indentation however.

Quoted 53 lines
hw/vfio/common.c +14 -16
        }
        /*
         * vaddr is only valid until rcu_read_unlock(). But after
@@ -343,8 +343,6 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
            vfio_set_migration_error(ret);
        }
    }
-out:
-    rcu_read_unlock();
 }
 
 static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl,
@@ -1223,23 +1221,23 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
    if (iotlb->target_as != &address_space_memory) {
        error_report("Wrong target AS \"%s\", only system memory is allowed",
                     iotlb->target_as->name ? iotlb->target_as->name : "none");
-        goto out;
-    }
-
-    rcu_read_lock();
-    if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
-        ret = vfio_get_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
-                                    translated_addr);
-        if (ret) {
-            error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
-                         "0x%"HWADDR_PRIx") = %d (%s)",
-                         bcontainer, iova, iotlb->addr_mask + 1, ret,
-                         strerror(-ret));
+    } else {
+        WITH_RCU_READ_LOCK_GUARD() {
+            if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
+                ret = vfio_get_dirty_bitmap(bcontainer, iova,
+                                            iotlb->addr_mask + 1,
+                                            translated_addr);
+                if (ret) {
+                    error_report("vfio_iommu_map_dirty_notify(%p,"
+                                 " 0x%"HWADDR_PRIx
+                                 ", 0x%"HWADDR_PRIx") = %d (%s)",
+                                 bcontainer, iova, iotlb->addr_mask + 1, ret,
+                                 strerror(-ret));
+                }
+            }
        }
    }
-    rcu_read_unlock();
 
-out:
    if (ret) {
        vfio_set_migration_error(ret);
    }
Signature · 1 line
2.41.0
#15 lore raw

Re: [PATCH 5/6] hw/vfio/common: Use RCU_READ macros

Quoted 31 lines · Manos Pitsidianakis
Quoted 17 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the *RCU_READ_LOCK_GUARD macros (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/vfio/common.c | 34 ++++++++++++++++------------------

1 file changed, 16 insertions(+), 18 deletions(-)
hw/vfio/common.c +0 -0
index 4aa86f563c..09878a3603 100644
@@ -308,13 +308,13 @@ static void vfio_iommu_map_notify(IOMMUNotifier 
 *n, IOMMUTLBEntry *iotlb)
         return;
     }

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier · line 318

Quoted 10 lines
hw/vfio/common.c +2 -2
 
-    rcu_read_lock();
+    RCU_READ_LOCK_GUARD();
 
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
-            goto out;
+            return;

Since this is the only early return, we could alternatively do:

Quoted 4 lines

-         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) { +         if (vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {

remove the goto/return, and wrap the rest of the codeflow in this if's brackets. And then we could use WITH_RCU_READ_LOCK_GUARD instead. That'd increase the code indentation however.

If the maintainer agrees with the style & code churn, I don't mind respining.

Quoted 14 lines

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier · line 347

Quoted 12 lines
hw/vfio/common.c +0 -0
         }
         /*

         * vaddr is only valid until rcu_read_unlock(). But after

hw/vfio/common.c +0 -2
@@ -343,8 +343,6 @@ static void vfio_iommu_map_notify(IOMMUNotifier 
 *n, IOMMUTLBEntry *iotlb)
             vfio_set_migration_error(ret);
         }
     }
-out:
-    rcu_read_unlock();
 }

Thanks!

#16 lore raw

Re: [PATCH 5/6] hw/vfio/common: Use RCU_READ macros

Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Manos Pitsidianakis
Quoted 17 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the *RCU_READ_LOCK_GUARD macros (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/vfio/common.c | 34 ++++++++++++++++------------------

1 file changed, 16 insertions(+), 18 deletions(-)
hw/vfio/common.c +0 -0
index 4aa86f563c..09878a3603 100644
@@ -308,13 +308,13 @@ static void vfio_iommu_map_notify(IOMMUNotifier 
 *n, IOMMUTLBEntry *iotlb)
         return;
     }

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier · line 318

Quoted 10 lines
hw/vfio/common.c +2 -2
 
-    rcu_read_lock();
+    RCU_READ_LOCK_GUARD();
 
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
-            goto out;
+            return;

Since this is the only early return, we could alternatively do:

Quoted 7 lines
Quoted 4 lines

-         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) { +         if (vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {

remove the goto/return, and wrap the rest of the codeflow in this if's brackets. And then we could use WITH_RCU_READ_LOCK_GUARD instead. That'd increase the code indentation however.

If the maintainer agrees with the style & code churn, I don't mind respining.

Alex, Cédric, any preference?

Quoted 17 lines
Quoted 15 lines

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier · line 347

Quoted 13 lines
hw/vfio/common.c +0 -0
         }
         /*

         * vaddr is only valid until rcu_read_unlock(). But after

hw/vfio/common.c +0 -2
@@ -343,8 +343,6 @@ static void vfio_iommu_map_notify(IOMMUNotifier 
 *n, IOMMUTLBEntry *iotlb)
             vfio_set_migration_error(ret);
         }
     }
-out:
-    rcu_read_unlock();
 }

Thanks!

#17 lore raw

Re: [PATCH 5/6] hw/vfio/common: Use RCU_READ macros

Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Manos Pitsidianakis
Quoted 16 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the *RCU_READ_LOCK_GUARD macros (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
hw/vfio/common.c | 34 ++++++++++++++++------------------

1 file changed, 16 insertions(+), 18 deletions(-)
hw/vfio/common.c +0 -0
index 4aa86f563c..09878a3603 100644
@@ -308,13 +308,13 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
         return;
     }

hw/vfio/common.c static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) · line 317

Quoted 10 lines
hw/vfio/common.c +2 -2
 
-    rcu_read_lock();
+    RCU_READ_LOCK_GUARD();
 
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {
-            goto out;
+            return;

Since this is the only early return, we could alternatively do:

-         if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) { +         if (vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) {

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

remove the goto/return, and wrap the rest of the codeflow in this if's brackets. And then we could use WITH_RCU_READ_LOCK_GUARD instead. That'd increase the code indentation however.

If the maintainer agrees with the style & code churn, I don't mind respining.

Alex, Cédric, any preference?

my choice would be to keep the 'goto' statement and protect the vfio_get_xlat_addr() call with :

+ WITH_RCU_READ_LOCK_GUARD() {
+ if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) {
+ ret = vfio_get_dirty_bitmap(bcontainer, iova,
+ iotlb->addr_mask + 1,
+ translated_addr);
+ if (ret) {
+ error_report("vfio_iommu_map_dirty_notify(%p,"

+                                 " 0x%"HWADDR_PRIx
+                                 ", 0x%"HWADDR_PRIx") = %d (%s)",
+                                 bcontainer, iova, iotlb->addr_mask + 1, ret,
+                                 strerror(-ret));
+                }
+            }
         }

Thanks,

C.

#18 lore raw

PATCH 6/6 hw/virtio/vhost: Use RCU_READ macro

In reply to Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

---
 hw/virtio/vhost.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/virtio/vhost.c +3 -3
index 2c9ac79468..1f5ecb843e 100644
@@ -186,12 +186,12 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
             hwaddr phys, s, offset;
 
             while (used_size) {
-                rcu_read_lock();
-                iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
+                WITH_RCU_READ_LOCK_GUARD() {
+                    iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
                                                       used_phys,
                                                       true,
                                                       MEMTXATTRS_UNSPECIFIED);
-                rcu_read_unlock();
+                }
 
                 if (!iotlb.target_as) {
                     qemu_log_mask(LOG_GUEST_ERROR, "translation "
Signature · 1 line
2.41.0
Reviewed-by on v1 6/6
#19 lore raw

Re: [PATCH 6/6] hw/virtio/vhost: Use RCU_READ macro

Quoted 32 lines · Philippe Mathieu-Daudé

Replace the manual rcu_read_(un)lock calls by the WITH_RCU_READ_LOCK_GUARD macro (See commit ef46ae67ba "docs/style: call out the use of GUARD macros").

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

1 file changed, 3 insertions(+), 3 deletions(-)
hw/virtio/vhost.c +3 -3
index 2c9ac79468..1f5ecb843e 100644
@@ -186,12 +186,12 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
            hwaddr phys, s, offset;
 
            while (used_size) {
-                rcu_read_lock();
-                iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
+                WITH_RCU_READ_LOCK_GUARD() {
+                    iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
                                                      used_phys,
                                                      true,
                                                      MEMTXATTRS_UNSPECIFIED);
-                rcu_read_unlock();
+                }
 
                if (!iotlb.target_as) {
                    qemu_log_mask(LOG_GUEST_ERROR, "translation "
Signature · 1 line
2.41.0