Skip to contentSkip to conversation

hw/core/qdev: cleanup Error ** variables

PATCH In review Vladimir Sementsov-Ogievskiy 3 participants 6 messages Nov 27, 2019 – Dec 6, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 hw/core/qdev: cleanup Error ** variables

Rename Error ** parameter in check_only_migratable to common errp.

In device_set_realized:

- Move "if (local_err != NULL)" closer to error setters.

- Drop 'Error **local_errp': it doesn't save any LoCs, but it's very unusual.

---

v6: enhance grammar in comment [Eric]
add r-b by Eric and Marc-André

 hw/core/qdev.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
hw/core/qdev.c +13 -15
index cf1ba28fe3..82d3ee590a 100644
@@ -820,12 +820,12 @@ static bool device_get_realized(Object *obj, Error **errp)
     return dev->realized;
 }
 
-static bool check_only_migratable(Object *obj, Error **err)
+static bool check_only_migratable(Object *obj, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(obj);
 
     if (!vmstate_check_only_migratable(dc->vmsd)) {
-        error_setg(err, "Device %s is not migratable, but "
+        error_setg(errp, "Device %s is not migratable, but "
                    "--only-migratable was specified",
                    object_get_typename(obj));
         return false;
@@ -874,10 +874,9 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
 
         if (dc->realize) {
             dc->realize(dev, &local_err);
-        }
-
-        if (local_err != NULL) {
-            goto fail;
+            if (local_err != NULL) {
+                goto fail;
+            }
         }
 
         DEVICE_LISTENER_CALL(realize, Forward, dev);
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
        }
 
     } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
             object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
         }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
         if (dc->unrealize) {
-            local_errp = local_err ? NULL : &local_err;
-            dc->unrealize(dev, local_errp);
+            dc->unrealize(dev, local_err ? NULL : &local_err);
         }
         dev->pending_deleted_event = true;
         DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
-    }
 
-    if (local_err != NULL) {
-        goto fail;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
+    assert(local_err == NULL);
     dev->realized = value;
     return;
 
@@ -976,7 +974,7 @@ static bool device_get_hotpluggable(Object *obj, Error **errp)
                                 qbus_is_hotpluggable(dev->parent_bus));
 }
 
-static bool device_get_hotplugged(Object *obj, Error **err)
+static bool device_get_hotplugged(Object *obj, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
 
Signature · 1 line
2.21.0
Reviewed-by on v6 1/1
#1 lore raw
Quoted 41 lines · Vladimir Sementsov-Ogievskiy

Rename Error ** parameter in check_only_migratable to common errp.

In device_set_realized:

- Move "if (local_err != NULL)" closer to error setters.

- Drop 'Error **local_errp': it doesn't save any LoCs, but it's very unusual.

---

v6: enhance grammar in comment [Eric]
add r-b by Eric and Marc-André

 hw/core/qdev.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
hw/core/qdev.c +2 -2
index cf1ba28fe3..82d3ee590a 100644
@@ -820,12 +820,12 @@ static bool device_get_realized(Object *obj, Error **errp)
     return dev->realized;
 }
 
-static bool check_only_migratable(Object *obj, Error **err)
+static bool check_only_migratable(Object *obj, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(obj);
 
     if (!vmstate_check_only_migratable(dc->vmsd)) {
-        error_setg(err, "Device %s is not migratable, but "
+        error_setg(errp, "Device %s is not migratable, but "
                    "--only-migratable was specified",
                    object_get_typename(obj));
         return false;
@@ -874,10 +874,9 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
 

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 880

Quoted 10 lines
hw/core/qdev.c +3 -4
         if (dc->realize) {
             dc->realize(dev, &local_err);
-        }
-
-        if (local_err != NULL) {
-            goto fail;
+            if (local_err != NULL) {
+                goto fail;
+            }
         }

Yes, this is the more conventional usage.

Quoted 4 lines
hw/core/qdev.c +0 -0
         DEVICE_LISTENER_CALL(realize, Forward, dev);
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
        }

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 924

Quoted 10 lines
hw/core/qdev.c +2 -3
 
     } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
             object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
         }

This is a rather unusual way to keep the first error of several. qapi/error.h advises:

* Receive and accumulate multiple errors (first one wins):

 *     Error *err = NULL, *local_err = NULL;
 *     foo(arg, &err);
 *     bar(arg, &local_err);
 *     error_propagate(&err, local_err);
 *     if (err) {

* handle the error...
* }

If replacing this by the usual way is too troublesome now, we can do it after the ERRP_AUTO_PROPAGATE() conversion. Your choice.

Quoted 10 lines
hw/core/qdev.c +1 -2
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
         if (dc->unrealize) {
-            local_errp = local_err ? NULL : &local_err;
-            dc->unrealize(dev, local_errp);
+            dc->unrealize(dev, local_err ? NULL : &local_err);
         }
         dev->pending_deleted_event = true;
         DEVICE_LISTENER_CALL(unrealize, Reverse, dev);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 939

Quoted 10 lines
hw/core/qdev.c +4 -3
-    }
 
-    if (local_err != NULL) {
-        goto fail;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
+    assert(local_err == NULL);

Not sure this one's worth it's keep with the usage of local_err cleaned up.

hw/core/qdev.c static bool device_get_hotpluggable(Object *obj, Error **errp) · line 979

Quoted 11 lines
hw/core/qdev.c +1 -1
     dev->realized = value;
     return;
 
@@ -976,7 +974,7 @@ static bool device_get_hotpluggable(Object *obj, Error **errp)
                                 qbus_is_hotpluggable(dev->parent_bus));
 }
 
-static bool device_get_hotplugged(Object *obj, Error **err)
+static bool device_get_hotplugged(Object *obj, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);

In case you want to clean up afterwards rather than now:

#2 lore raw
Quoted 74 lines · Markus Armbruster
Quoted 41 lines · Vladimir Sementsov-Ogievskiy

Rename Error ** parameter in check_only_migratable to common errp.

In device_set_realized:

- Move "if (local_err != NULL)" closer to error setters.

- Drop 'Error **local_errp': it doesn't save any LoCs, but it's very unusual.

---

v6: enhance grammar in comment [Eric]
add r-b by Eric and Marc-André

  hw/core/qdev.c | 28 +++++++++++++---------------
  1 file changed, 13 insertions(+), 15 deletions(-)
hw/core/qdev.c +2 -2
index cf1ba28fe3..82d3ee590a 100644
@@ -820,12 +820,12 @@ static bool device_get_realized(Object *obj, Error **errp)
      return dev->realized;
  }
 
-static bool check_only_migratable(Object *obj, Error **err)
+static bool check_only_migratable(Object *obj, Error **errp)
  {
      DeviceClass *dc = DEVICE_GET_CLASS(obj);
 
      if (!vmstate_check_only_migratable(dc->vmsd)) {
-        error_setg(err, "Device %s is not migratable, but "
+        error_setg(errp, "Device %s is not migratable, but "
                     "--only-migratable was specified",
                     object_get_typename(obj));
          return false;
@@ -874,10 +874,9 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
 

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 880

Quoted 10 lines
hw/core/qdev.c +3 -4
          if (dc->realize) {
              dc->realize(dev, &local_err);
-        }
-
-        if (local_err != NULL) {
-            goto fail;
+            if (local_err != NULL) {
+                goto fail;
+            }
          }

Yes, this is the more conventional usage.

Quoted 4 lines
hw/core/qdev.c +0 -0
          DEVICE_LISTENER_CALL(realize, Forward, dev);
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
         }

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 924

Quoted 10 lines
hw/core/qdev.c +2 -3
 
      } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
          QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
              object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
          }

This is a rather unusual way to keep the first error of several. qapi/error.h advises:

Quoted 8 lines

* Receive and accumulate multiple errors (first one wins):

  *     Error *err = NULL, *local_err = NULL;
  *     foo(arg, &err);
  *     bar(arg, &local_err);
  *     error_propagate(&err, local_err);
  *     if (err) {

* handle the error...
* }

Hmm, honestly, I like more what I've written:

1. less code
2. logic is more clean: we store first error to local_err, and after first error pass NULL as a parameter. No propagation or extra error variables.
3. more efficient (no propagation, no extra allocation for errors which we'll drop anyway) (I understand that efficiency of error path is not thing to care about, so it's at third place)

Also, propagation which you propose is also unusual thing (it proposed in comment, but who reads it :). I've never seen it before, and I've to go and check that error_propagate works correctly when first argument is already set.

So, I'd prefer to keep now this patch as is, and to convert later if we really need it.

Quoted 35 lines

If replacing this by the usual way is too troublesome now, we can do it after the ERRP_AUTO_PROPAGATE() conversion. Your choice.

Quoted 10 lines
hw/core/qdev.c +1 -2
          if (qdev_get_vmsd(dev)) {
              vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
          }
          if (dc->unrealize) {
-            local_errp = local_err ? NULL : &local_err;
-            dc->unrealize(dev, local_errp);
+            dc->unrealize(dev, local_err ? NULL : &local_err);
          }
          dev->pending_deleted_event = true;
          DEVICE_LISTENER_CALL(unrealize, Reverse, dev);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 939

Quoted 10 lines
hw/core/qdev.c +4 -3
-    }
 
-    if (local_err != NULL) {
-        goto fail;
+        if (local_err != NULL) {
+            goto fail;
+        }
      }
 
+    assert(local_err == NULL);

Not sure this one's worth it's keep with the usage of local_err cleaned up.

Quoted 7 lines
hw/core/qdev.c +0 -0
      dev->realized = value;
      return;
 
@@ -976,7 +974,7 @@ static bool device_get_hotpluggable(Object *obj, Error **errp)
                                  qbus_is_hotpluggable(dev->parent_bus));
  }
 
Quoted 8 lines

hw/core/qdev.c static bool device_get_hotpluggable(Object *obj, Error **errp) · line 979

Quoted 4 lines
hw/core/qdev.c +1 -1
-static bool device_get_hotplugged(Object *obj, Error **err)
+static bool device_get_hotplugged(Object *obj, Error **errp)
  {
      DeviceState *dev = DEVICE(obj);

In case you want to clean up afterwards rather than now:

Thanks!

Signature · 2 lines
Best regards,
Vladimir
#3 lore raw
Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 14 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 924

Quoted 12 lines
hw/core/qdev.c +2 -3
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
          }
 
       } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
           QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
               object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
           }

This is a rather unusual way to keep the first error of several.

It may be unusual, but has the benefit of avoiding error_propagate...

Quoted 20 lines
Quoted 10 lines

qapi/error.h advises:

* Receive and accumulate multiple errors (first one wins):

   *     Error *err = NULL, *local_err = NULL;
   *     foo(arg, &err);
   *     bar(arg, &local_err);
   *     error_propagate(&err, local_err);
   *     if (err) {

* handle the error...
* }

Hmm, honestly, I like more what I've written:

1. less code
2. logic is more clean: we store first error to local_err, and after first error pass NULL as a parameter. No propagation or extra error variables.
3. more efficient (no propagation, no extra allocation for errors which we'll drop anyway) (I understand that efficiency of error path is not thing to care about, so it's at third place)

Quoted 9 lines

Also, propagation which you propose is also unusual thing (it proposed in comment, but who reads it :). I've never seen it before, and I've to go and check that error_propagate works correctly when first argument is already set.

So, I'd prefer to keep now this patch as is, and to convert later if we really need it.

Quoted 3 lines

If replacing this by the usual way is too troublesome now, we can do it after the ERRP_AUTO_PROPAGATE() conversion. Your choice.

...and after conversion to use ERRP_AUTO_PROPATATE(), the use of error_propagate() should NOT occur in any code _except_ for the macro definition (any other use of the function points out a place where we failed to use the macro to get rid of boilerplate).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#4 lore raw
Quoted 16 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines
Quoted 5 lines
hw/core/qdev.c +1 -1
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
          }
          } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 924

Quoted 6 lines
hw/core/qdev.c +1 -2
           QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
               object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
           }

This is a rather unusual way to keep the first error of several.

Quoted 1 line

It may be unusual, but has the benefit of avoiding error_propagate...

Non-issue if the error_propagate() gets replaced by ERRP_AUTO_PROPAGATE(), isn't it?

Quoted 20 lines
Quoted 20 lines
Quoted 10 lines

qapi/error.h advises:

* Receive and accumulate multiple errors (first one wins):

   *     Error *err = NULL, *local_err = NULL;
   *     foo(arg, &err);
   *     bar(arg, &local_err);
   *     error_propagate(&err, local_err);
   *     if (err) {

* handle the error...
* }

Hmm, honestly, I like more what I've written:

1. less code
2. logic is more clean: we store first error to local_err, and after first error pass NULL as a parameter. No propagation or extra error variables.
3. more efficient (no propagation, no extra allocation for errors which we'll drop anyway) (I understand that efficiency of error path is not thing to care about, so it's at third place)

Quoted 3 lines
Quoted 3 lines

Also, propagation which you propose is also unusual thing (it proposed in comment, but who reads it :). I've never seen it before, and I've to go and check that error_propagate works correctly when first argument is already set.

When you think you can improve on the common, documented pattern, you're invited to update the documentation and the existing uses of the pattern.

If everybody "improved" on common, documented patterns locally, the code would become needlessly hard to read for developers with experience in the pattern's area.

Quoted 1 line
Quoted 1 line

So, I'd prefer to keep now this patch as is, and to convert later if we really need it.

I want this to match the common, documented pattern. Whether we make it match before or after your ERRP_AUTO_PROPAGATE() work doesn't matter to me.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

If replacing this by the usual way is too troublesome now, we can do it after the ERRP_AUTO_PROPAGATE() conversion. Your choice.

...and after conversion to use ERRP_AUTO_PROPATATE(), the use of error_propagate() should NOT occur in any code _except_ for the macro definition (any other use of the function points out a place where we failed to use the macro to get rid of boilerplate).

I figure we still need it in the (rare) cases where we want to ignore some of a function's errors, as we do in fit_load_fdt(). If that bothers us, we can try to find a solution that avoids the boilerplate.

#5 lore raw
Quoted 47 lines · Markus Armbruster
Quoted 16 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines
Quoted 5 lines
hw/core/qdev.c +1 -1
@@ -918,27 +917,26 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
           }
           } else if (!value && dev->realized) {
-        Error **local_errp = NULL;
+        /* We want local_err to track only the first error */
Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 924

Quoted 6 lines
hw/core/qdev.c +1 -2
            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-            local_errp = local_err ? NULL : &local_err;
                object_property_set_bool(OBJECT(bus), false, "realized",
-                                     local_errp);
+                                     local_err ? NULL : &local_err);
            }

This is a rather unusual way to keep the first error of several.

Quoted 1 line

It may be unusual, but has the benefit of avoiding error_propagate...

Non-issue if the error_propagate() gets replaced by ERRP_AUTO_PROPAGATE(), isn't it?

Quoted 20 lines
Quoted 20 lines
Quoted 10 lines

qapi/error.h advises:

* Receive and accumulate multiple errors (first one wins):
* Error *err = NULL, *local_err = NULL;
* foo(arg, &err);
* bar(arg, &local_err);
* error_propagate(&err, local_err);
* if (err) {
* handle the error...
* }

Hmm, honestly, I like more what I've written:

1. less code
2. logic is more clean: we store first error to local_err, and after first error pass NULL as a parameter. No propagation or extra error variables.
3. more efficient (no propagation, no extra allocation for errors which we'll drop anyway) (I understand that efficiency of error path is not thing to care about, so it's at third place)

Quoted 3 lines
Quoted 3 lines

Also, propagation which you propose is also unusual thing (it proposed in comment, but who reads it :). I've never seen it before, and I've to go and check that error_propagate works correctly when first argument is already set.

Quoted 7 lines

When you think you can improve on the common, documented pattern, you're invited to update the documentation and the existing uses of the pattern.

If everybody "improved" on common, documented patterns locally, the code would become needlessly hard to read for developers with experience in the pattern's area.

That's reasonable, I understand.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

So, I'd prefer to keep now this patch as is, and to convert later if we really need it.

I want this to match the common, documented pattern. Whether we make it match before or after your ERRP_AUTO_PROPAGATE() work doesn't matter to me.

Then, let's after.

Quoted 13 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

If replacing this by the usual way is too troublesome now, we can do it after the ERRP_AUTO_PROPAGATE() conversion. Your choice.

...and after conversion to use ERRP_AUTO_PROPATATE(), the use of error_propagate() should NOT occur in any code _except_ for the macro definition (any other use of the function points out a place where we failed to use the macro to get rid of boilerplate).

I figure we still need it in the (rare) cases where we want to ignore some of a function's errors, as we do in fit_load_fdt(). If that bothers us, we can try to find a solution that avoids the boilerplate.

Signature · 2 lines
Best regards,
Vladimir