Skip to contentSkip to conversation

Minor cleanups: eliminate redundant code in QOM, block-backend and vl.c

PATCH In review Bin Guo 5 participants 12 messages Apr 29 – May 12 Block layer

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

cover Minor cleanups: eliminate redundant code in QOM, block-backend and vl.c

This series contains three independent cleanups that remove redundant code and improve consistency across different subsystems.

Patch 1 merges a double hash-table traversal in object_property_del_child() into a single pass, halving the number of hash table operations in the common case.

Patch 2 makes blk_co_preadv() a one-liner wrapper around blk_co_preadv_part(), matching the existing pattern already used by the write side (blk_co_pwritev / blk_co_pwritev_part).

Patch 3 inlines qemu_opts_parse_noisily() return-value checks directly into if-conditions for options that do not use the returned pointer, matching the style already used by QEMU_OPTION_action.

All three patches are independent and can be applied in any order.

Bin Guo (3):
qom/object: merge double hash table traversal in
object_property_del_child
block/block-backend: delegate blk_co_preadv to blk_co_preadv_part system/vl.c: inline qemu_opts_parse_noisily() result checks

 block/block-backend.c |  8 +-------
 qom/object.c          |  7 -------
 system/vl.c           | 37 +++++++++++++++----------------------
 3 files changed, 16 insertions(+), 36 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH 1/3 qom/object: merge double hash table traversal in object_property_del_child

object_property_del_child() previously performed two full iterations over obj->properties: the first to find the matching child property and call its release callback, the second to find the same property again and remove it from the hash table.

Merge the two loops into one: when the matching property is found, call the release callback and immediately remove the entry via g_hash_table_iter_remove(), then break. This halves the number of hash table operations in the common case and avoids the redundant second scan.

---
 qom/object.c | 7 -------
 1 file changed, 7 deletions(-)
qom/object.c +0 -7
index f981e27044..9c0e8dfd02 100644
@@ -629,13 +629,6 @@ static void object_property_del_child(Object *obj, Object *child)
                 prop->release(obj, prop->name, prop->opaque);
                 prop->release = NULL;
             }
-            break;
-        }
-    }
-    g_hash_table_iter_init(&iter, obj->properties);
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        prop = value;
-        if (object_property_is_child(prop) && prop->opaque == child) {
             g_hash_table_iter_remove(&iter);
             break;
         }
Signature · 1 line
2.50.1 (Apple Git-155)
author
#8 lore raw

PATCH 3/3 system/vl.c: inline qemu_opts_parse_noisily() result checks

In reply to Bin Guo

In qemu_init()'s option parsing switch, several cases assigned the return value of qemu_opts_parse_noisily() to the shared 'opts' variable solely to check for NULL, without using the pointer afterwards. Inline the call directly into the if-condition, matching the style already used by QEMU_OPTION_action.

This affects the following options:
-drive, -numa, -iscsi, -m, -mon, -chardev, -fsdev, -fwcfg

Cases where the returned QemuOpts* is subsequently used (e.g. -acpitable, -smbios, -virtfs) are left unchanged.

---
 system/vl.c | 37 +++++++++++++++----------------------
 1 file changed, 15 insertions(+), 22 deletions(-)
system/vl.c +15 -22
index 0e1fc217b4..0e0d3cb761 100644
@@ -2961,9 +2961,8 @@ void qemu_init(int argc, char **argv)
                     break;
                 }
             case QEMU_OPTION_drive:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
-                                               optarg, false);
-                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("drive"),
+                                             optarg, false)) {
                     exit(1);
                 }
                 break;
@@ -2988,9 +2987,8 @@ void qemu_init(int argc, char **argv)
                 replay_add_blocker("-snapshot");
                 break;
             case QEMU_OPTION_numa:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
-                                               optarg, true);
-                if (!opts) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("numa"),
+                                             optarg, true)) {
                     exit(1);
                 }
                 break;
@@ -3049,9 +3047,8 @@ void qemu_init(int argc, char **argv)
                 break;
 #ifdef CONFIG_LIBISCSI
             case QEMU_OPTION_iscsi:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
-                                               optarg, false);
-                if (!opts) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
+                                             optarg, false)) {
                     exit(1);
                 }
                 break;
@@ -3104,8 +3101,8 @@ void qemu_init(int argc, char **argv)
                 exit(0);
                 break;
             case QEMU_OPTION_m:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
-                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("memory"),
+                                             optarg, true)) {
                     exit(1);
                 }
                 break;
@@ -3226,17 +3223,15 @@ void qemu_init(int argc, char **argv)
                 default_monitor = 0;
                 break;
             case QEMU_OPTION_mon:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
-                                               true);
-                if (!opts) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
+                                             true)) {
                     exit(1);
                 }
                 default_monitor = 0;
                 break;
             case QEMU_OPTION_chardev:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
-                                               optarg, true);
-                if (!opts) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("chardev"),
+                                             optarg, true)) {
                     exit(1);
                 }
                 break;
@@ -3246,8 +3241,7 @@ void qemu_init(int argc, char **argv)
                     error_report("fsdev support is disabled");
                     exit(1);
                 }
-                opts = qemu_opts_parse_noisily(olist, optarg, true);
-                if (!opts) {
+                if (!qemu_opts_parse_noisily(olist, optarg, true)) {
                     exit(1);
                 }
                 break;
@@ -3386,9 +3380,8 @@ void qemu_init(int argc, char **argv)
                 smbios_entry_add(opts, &error_fatal);
                 break;
             case QEMU_OPTION_fwcfg:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
-                                               optarg, true);
-                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
+                                             optarg, true)) {
                     exit(1);
                 }
                 break;
Signature · 1 line
2.50.1 (Apple Git-155)
author
#5 lore raw

PATCH 2/3 block/block-backend: delegate blk_co_preadv to blk_co_preadv_part

In reply to Bin Guo

blk_co_preadv() and blk_co_preadv_part() share identical bodies except that blk_co_preadv() always passes qiov_offset=0. blk_co_pwritev() already uses this pattern and simply calls blk_co_pwritev_part() with qiov_offset=0. Apply the same simplification to the read side so that both pairs are consistent and the shared logic lives in a single place.

Before this change blk_co_preadv() duplicated the blk_inc_in_flight / blk_co_do_preadv_part / blk_dec_in_flight sequence. After this change it is a one-liner wrapper, matching the write side.

---
 block/block-backend.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
block/block-backend.c +1 -7
index 9944657120..490b149bf8 100644
@@ -1376,14 +1376,8 @@ int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
                                int64_t bytes, QEMUIOVector *qiov,
                                BdrvRequestFlags flags)
 {
-    int ret;
     IO_OR_GS_CODE();
-
-    blk_inc_in_flight(blk);
-    ret = blk_co_do_preadv_part(blk, offset, bytes, qiov, 0, flags);
-    blk_dec_in_flight(blk);
-
-    return ret;
+    return blk_co_preadv_part(blk, offset, bytes, qiov, 0, flags);
 }
 
 int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] system/vl.c: inline qemu_opts_parse_noisily() result checks

In reply to Bin Guo
Quoted 23 lines · Bin Guo

In qemu_init()'s option parsing switch, several cases assigned the return value of qemu_opts_parse_noisily() to the shared 'opts' variable solely to check for NULL, without using the pointer afterwards. Inline the call directly into the if-condition, matching the style already used by QEMU_OPTION_action.

This affects the following options:

   -drive, -numa, -iscsi, -m, -mon, -chardev, -fsdev, -fwcfg

Cases where the returned QemuOpts* is subsequently used (e.g. -acpitable, -smbios, -virtfs) are left unchanged.

---
  system/vl.c | 37 +++++++++++++++----------------------
  1 file changed, 15 insertions(+), 22 deletions(-)
system/vl.c +0 -0
index 0e1fc217b4..0e0d3cb761 100644
@@ -2961,9 +2961,8 @@ void qemu_init(int argc, char **argv)
                      break;

system/vl.c void qemu_init(int argc, char **argv) · line 2968

Quoted 10 lines
system/vl.c +2 -3
                  }
              case QEMU_OPTION_drive:
-                opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
-                                               optarg, false);
-                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("drive"),
+                                             optarg, false)) {
                      exit(1);
                  }
                  break;

For clarity I'd also squash in this patch:

-- >8 --

system/vl.c +2 -1
index cab8106984a..237334236aa 100644
@@ -2842,3 +2842,2 @@ void qemu_init(int argc, char **argv)
 {
-    QemuOpts *opts;
     QemuOpts *icount_opts = NULL, *accel_opts = NULL;
@@ -2921,2 +2920,4 @@ void qemu_init(int argc, char **argv)
     for(;;) {
+        QemuOpts *opts;
+
         if (optind >= argc)

---

#10 lore raw

Re: [PATCH 3/3] system/vl.c: inline qemu_opts_parse_noisily() result checks

Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Bin Guo

In qemu_init()'s option parsing switch, several cases assigned the return value of qemu_opts_parse_noisily() to the shared 'opts' variable solely to check for NULL, without using the pointer afterwards.  Inline the call directly into the if-condition, matching the style already used by QEMU_OPTION_action.

This affects the following options:
   -drive, -numa, -iscsi, -m, -mon, -chardev, -fsdev, -fwcfg

Cases where the returned QemuOpts* is subsequently used (e.g. -acpitable, -smbios, -virtfs) are left unchanged.

---
  system/vl.c | 37 +++++++++++++++----------------------

  1 file changed, 15 insertions(+), 22 deletions(-)
system/vl.c +0 -0
index 0e1fc217b4..0e0d3cb761 100644
@@ -2961,9 +2961,8 @@ void qemu_init(int argc, char **argv)
                       break;
                   }

              case QEMU_OPTION_drive:

Quoted 8 lines

-                opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), -                                               optarg, false); -                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("drive"), +                                             optarg, false)) { exit(1);
                  }
                  break;

For clarity I'd also squash in this patch:

-- >8 --

system/vl.c +2 -2
index cab8106984a..237334236aa 100644
@@ -2842,3 +2842,2 @@ void qemu_init(int argc, char **argv)
  {
-    QemuOpts *opts;
      QemuOpts *icount_opts = NULL, *accel_opts = NULL;
@@ -2921,2 +2920,4 @@ void qemu_init(int argc, char **argv)
      for(;;) {
+        QemuOpts *opts;
+
          if (optind >= argc)
---
Quoted 1 line

If you're going to move the decl, the innermost block is just below with

        } else {
            const QEMUOption *popt;

+ QemuOpts *opts;

            popt = lookup_opt(argc, argv, &optarg, &optind);

r~

Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] qom/object: merge double hash table traversal in object_property_del_child

In reply to Bin Guo
Quoted 23 lines · Bin Guo

object_property_del_child() previously performed two full iterations over obj->properties: the first to find the matching child property and call its release callback, the second to find the same property again and remove it from the hash table.

Merge the two loops into one: when the matching property is found, call the release callback and immediately remove the entry via g_hash_table_iter_remove(), then break. This halves the number of hash table operations in the common case and avoids the redundant second scan.

---
  qom/object.c | 7 -------
  1 file changed, 7 deletions(-)
qom/object.c +0 -0
index f981e27044..9c0e8dfd02 100644
@@ -629,13 +629,6 @@ static void object_property_del_child(Object *obj, Object *child)
                  prop->release(obj, prop->name, prop->opaque);
                  prop->release = NULL;
              }

qom/object.c static void object_property_del_child(Object *obj, Object *child) · line 634

Quoted 10 lines
qom/object.c +0 -7
-            break;
-        }
-    }
-    g_hash_table_iter_init(&iter, obj->properties);
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        prop = value;
-        if (object_property_is_child(prop) && prop->opaque == child) {
              g_hash_table_iter_remove(&iter);
              break;
          }
Fixes b604a854e84 ("qom: Replace object property list with GHashTable")

r~

Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] block/block-backend: delegate blk_co_preadv to blk_co_preadv_part

In reply to Bin Guo
Quoted 26 lines · Bin Guo

blk_co_preadv() and blk_co_preadv_part() share identical bodies except that blk_co_preadv() always passes qiov_offset=0. blk_co_pwritev() already uses this pattern and simply calls blk_co_pwritev_part() with qiov_offset=0. Apply the same simplification to the read side so that both pairs are consistent and the shared logic lives in a single place.

Before this change blk_co_preadv() duplicated the blk_inc_in_flight / blk_co_do_preadv_part / blk_dec_in_flight sequence. After this change it is a one-liner wrapper, matching the write side.

---
  block/block-backend.c | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)
block/block-backend.c +0 -1
index 9944657120..490b149bf8 100644
@@ -1376,14 +1376,8 @@ int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
                                 int64_t bytes, QEMUIOVector *qiov,
                                 BdrvRequestFlags flags)
  {
-    int ret;
      IO_OR_GS_CODE();

block/block-backend.c int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset, · line 1383

Quoted 10 lines
block/block-backend.c +1 -6
-
-    blk_inc_in_flight(blk);
-    ret = blk_co_do_preadv_part(blk, offset, bytes, qiov, 0, flags);
-    blk_dec_in_flight(blk);
-
-    return ret;
+    return blk_co_preadv_part(blk, offset, bytes, qiov, 0, flags);
  }
 
  int coroutine_fn blk_co_preadv_part(BlockBackend *blk, int64_t offset,

r~

#11 lore raw

Re: [PATCH 3/3] system/vl.c: inline qemu_opts_parse_noisily() result checks

In reply to Richard Henderson
Quoted 58 lines · Richard Henderson
Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Bin Guo

In qemu_init()'s option parsing switch, several cases assigned the return value of qemu_opts_parse_noisily() to the shared 'opts' variable solely to check for NULL, without using the pointer afterwards.  Inline the call directly into the if-condition, matching the style already used by QEMU_OPTION_action.

This affects the following options:
   -drive, -numa, -iscsi, -m, -mon, -chardev, -fsdev, -fwcfg

Cases where the returned QemuOpts* is subsequently used (e.g. -acpitable, -smbios, -virtfs) are left unchanged.

---
  system/vl.c | 37 +++++++++++++++----------------------

  1 file changed, 15 insertions(+), 22 deletions(-)
system/vl.c +0 -0
index 0e1fc217b4..0e0d3cb761 100644
@@ -2961,9 +2961,8 @@ void qemu_init(int argc, char **argv)
                       break;
                   }

              case QEMU_OPTION_drive:

Quoted 8 lines

-                opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), -                                               optarg, false); -                if (opts == NULL) {
+                if (!qemu_opts_parse_noisily(qemu_find_opts("drive"), +                                             optarg, false)) { exit(1);
                  }
                  break;

For clarity I'd also squash in this patch:

-- >8 --

system/vl.c +2 -2
index cab8106984a..237334236aa 100644
@@ -2842,3 +2842,2 @@ void qemu_init(int argc, char **argv)
   {
-    QemuOpts *opts;
       QemuOpts *icount_opts = NULL, *accel_opts = NULL;
@@ -2921,2 +2920,4 @@ void qemu_init(int argc, char **argv)
       for(;;) {
+        QemuOpts *opts;
+
           if (optind >= argc)
---
Quoted 1 line

If you're going to move the decl, the innermost block is just below with

Quoted 5 lines

        } else {
            const QEMUOption *popt;
+           QemuOpts *opts;

            popt = lookup_opt(argc, argv, &optarg, &optind);

Good catch.

#3 lore raw

Re: [PATCH 1/3] qom/object: merge double hash table traversal in object_property_del_child

In reply to Richard Henderson
Quoted 28 lines · Richard Henderson
Quoted 27 lines · Bin Guo

object_property_del_child() previously performed two full iterations over obj->properties: the first to find the matching child property and call its release callback, the second to find the same property again and remove it from the hash table.

Merge the two loops into one: when the matching property is found, call the release callback and immediately remove the entry via g_hash_table_iter_remove(), then break. This halves the number of hash table operations in the common case and avoids the redundant second scan.

---
  qom/object.c | 7 -------
  1 file changed, 7 deletions(-)
qom/object.c +0 -4
index f981e27044..9c0e8dfd02 100644
@@ -629,13 +629,6 @@ static void object_property_del_child(Object *obj, Object *child)
                  prop->release(obj, prop->name, prop->opaque);
                  prop->release = NULL;
              }
-            break;
-        }
-    }
-    g_hash_table_iter_init(&iter, obj->properties);
Quoted 8 lines

qom/object.c static void object_property_del_child(Object *obj, Object *child) · line 634

Quoted 6 lines
qom/object.c +0 -3
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        prop = value;
-        if (object_property_is_child(prop) && prop->opaque == child) {
              g_hash_table_iter_remove(&iter);
              break;
          }
Fixes b604a854e84 ("qom: Replace object property list with GHashTable")

On the contrary, this proposed patch breaks that commit.

The original proposal for that old commit had a single iteration, but it was discovered that this broke cleanup for certain devices, as prop->release can have side-effects that trigger assertions in g_hash_table_iter_remove. This requires a two phase iteration to be safe.

A test for that problem was added in 8c4d156c187c84b574d287bd4b9ddf9a6975de7c and this new patch proposal duly breaks the check-qom-proplist test.

With regards,
Daniel

#4 lore raw

Re: [PATCH 1/3] qom/object: merge double hash table traversal in object_property_del_child

Quoted 38 lines · Daniel P. Berrangé
Quoted 28 lines · Richard Henderson
Quoted 27 lines · Bin Guo

object_property_del_child() previously performed two full iterations over obj->properties: the first to find the matching child property and call its release callback, the second to find the same property again and remove it from the hash table.

Merge the two loops into one: when the matching property is found, call the release callback and immediately remove the entry via g_hash_table_iter_remove(), then break. This halves the number of hash table operations in the common case and avoids the redundant second scan.

---
   qom/object.c | 7 -------
   1 file changed, 7 deletions(-)
qom/object.c +0 -4
index f981e27044..9c0e8dfd02 100644
@@ -629,13 +629,6 @@ static void object_property_del_child(Object *obj, Object *child)
                   prop->release(obj, prop->name, prop->opaque);
                   prop->release = NULL;
               }
-            break;
-        }
-    }
-    g_hash_table_iter_init(&iter, obj->properties);
Quoted 8 lines

qom/object.c static void object_property_del_child(Object *obj, Object *child) · line 634

Quoted 6 lines
qom/object.c +0 -3
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        prop = value;
-        if (object_property_is_child(prop) && prop->opaque == child) {
               g_hash_table_iter_remove(&iter);
               break;
           }
Fixes b604a854e84 ("qom: Replace object property list with GHashTable")
Quoted 7 lines

On the contrary, this proposed patch breaks that commit.

The original proposal for that old commit had a single iteration, but it was discovered that this broke cleanup for certain devices, as prop->release can have side-effects that trigger assertions in g_hash_table_iter_remove. This requires a two phase iteration to be safe.

Worth a comment in the code :)

Quoted 7 lines

A test for that problem was added in 8c4d156c187c84b574d287bd4b9ddf9a6975de7c and this new patch proposal duly breaks the check-qom-proplist test.

With regards,
Daniel

Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] block/block-backend: delegate blk_co_preadv to blk_co_preadv_part

In reply to Bin Guo

Am 29.04.2026 um 08:20 hat Bin Guo geschrieben:

Quoted 6 lines

blk_co_preadv() and blk_co_preadv_part() share identical bodies except that blk_co_preadv() always passes qiov_offset=0. blk_co_pwritev() already uses this pattern and simply calls blk_co_pwritev_part() with qiov_offset=0. Apply the same simplification to the read side so that both pairs are consistent and the shared logic lives in a single place.

Quoted 6 lines

Before this change blk_co_preadv() duplicated the blk_inc_in_flight / blk_co_do_preadv_part / blk_dec_in_flight sequence. After this change it is a one-liner wrapper, matching the write side.