Skip to contentSkip to conversation

error: auto propagated local_err

RFC Superseded Vladimir Sementsov-Ogievskiy 8 participants 45 messages Sep 18, 2019 – Sep 20, 2019 Block layer

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 error: auto propagated local_err

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
 include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
 block.c              |  63 ++++++++++++--------------
 block/backup.c       |   8 +++-
 block/gluster.c      |   7 +++
 4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +102 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                         ErrorClass err_class, const char *fmt, ...)
     GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}
+
+
 /*
  * Special error destination to abort on error.
  * See error_setg() and error_propagate() for details.
block.c +29 -34

134 lines not on this page · raw message

block/backup.c +6 -2
index 89f7f89200..462dea4fbb 100644
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
 static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                              Error **errp)
 {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);
     int ret;
     BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                     BACKUP_CLUSTER_SIZE_DEFAULT);
         return BACKUP_CLUSTER_SIZE_DEFAULT;
     } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
             "Couldn't determine the cluster size of the target image, "
             "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
             "Aborting, since this may create an unusable destination image\n");
         return ret;
     } else if (ret < 0 && target->backing) {
block/gluster.c +7 -0
index 64028b2cba..799a2dbeca 100644
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                               QDict *options, Error **errp)
 {
     int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call. Note, it must be placed exactly after
+     * all local variables defenition.
+     */
+    MAKE_ERRP_SAFE(errp);
+
     if (filename) {
         ret = qemu_gluster_parse_uri(gconf, filename);
         if (ret < 0) {
Signature · 1 line
2.21.0
#1 lore raw
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

Quoted 6 lines

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

It might have been nice to do a patch series of one proposal per patch, rather than cramming three in one, if only to see...

Quoted 10 lines

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
 include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
 block.c              |  63 ++++++++++++--------------
 block/backup.c       |   8 +++-
 block/gluster.c      |   7 +++
 4 files changed, 144 insertions(+), 36 deletions(-)

...relative diffstat sizing differences between them.

Of course, adding the framework requires growth in error.h. The real question, then, is how many lines do we save elsewhere by getting rid of boilerplate that is replaced by auto cleanup, and how many lines are mechanically churned to change variable names. And how much of that churn can itself be automated with Coccinelle.

But I think the idea is worth pursuing!

Quoted 9 lines
include/qapi/error.h +0 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                         ErrorClass err_class, const char *fmt, ...)
     GCC_FMT_ATTR(6, 7);
 

include/qapi/error.h void error_set_internal(Error **errp, · line 334

Quoted 10 lines
include/qapi/error.h +10 -0
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }

Technically, you don't need the 'if' here, since error_propogate() works just fine when prop->local_err is NULL.

include/qapi/error.h void error_set_internal(Error **errp, · line 338

Quoted 4 lines
include/qapi/error.h +4 -0
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+

Looks reasonable.

include/qapi/error.h void error_set_internal(Error **errp, · line 348

Quoted 10 lines
include/qapi/error.h +10 -0
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.

s/it's/its/ ("it's" is only appropriate if "it is" can be substituted)

include/qapi/error.h void error_set_internal(Error **errp, · line 360

Quoted 12 lines
include/qapi/error.h +12 -0
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}

I don't like the type-punning.

Quoted 4 lines
include/qapi/error.h +4 -0
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+

include/qapi/error.h void error_set_internal(Error **errp, · line 374

Quoted 10 lines
include/qapi/error.h +10 -0
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}

Again, more type-punning. Ends up declaring the equivalent of 'Error *auto_errp[2]' which devolves to 'Error **auto_errp'.

So, using this macro requires me to pass in the name of my local variable (which auto-propagates when it goes out of scope) and the errp parameter. Can we shortcut by declaring that the variable it propagates to MUST be named 'errp' rather than having that as a parameter name (doing so would force us to be consistent at using an Error **errp parameter).

Quoted 5 lines
include/qapi/error.h +5 -0
+
+
+/*
+ * Another variant:
+ *   Pros:

include/qapi/error.h void error_set_internal(Error **errp, · line 389

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;

No real difference from ErrorPropagator above.

include/qapi/error.h void error_set_internal(Error **errp, · line 395

Quoted 6 lines
include/qapi/error.h +6 -0
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }

Another useless if.

include/qapi/error.h void error_set_internal(Error **errp, · line 406

Quoted 11 lines
include/qapi/error.h +11 -0
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);

Not written to take a trailing semicolon in the caller.

Less type-punning, and and tries to reuse errp where possible.

include/qapi/error.h void error_set_internal(Error **errp, · line 417

Quoted 11 lines
include/qapi/error.h +11 -0
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.

Why? I see no reason why it can't be hoisted earlier than other declarations, and the only reason to not sink it after earlier code that doesn't touch errp would be our coding standards that frowns on declaration after code.

include/qapi/error.h void error_set_internal(Error **errp, · line 418

Quoted 1 line
include/qapi/error.h +1 -0
+ *     - like v2, several statements in one open macro

Not a drawback in my mind.

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines
include/qapi/error.h +6 -0
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

This is actually quite cool. And if you get rid of your insistence that it must occur after other variable declarations, you could instead easily automate that any function that has a parameter 'Error **errp' then has a MAKE_ERRP_SAFE(errp); as the first line of its function body (that becomes something that you could grep for, rather than having to use the smarts of coccinelle).

Or if we want to enforce consistency on the parameter naming, even go with:

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = errp}; \
errp = &__auto_errp_prop.local_err

block.c

Quoted 9 lines
include/qapi/error.h +2 -0
+
+
 /*
  * Special error destination to abort on error.
  * See error_setg() and error_propagate() for details.
block.c +0 -0
index 5944124845..5253663329 100644

So the above was the proposed implementations (I'm leaning towards option 3); the below is a demonstration of their use.

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1278

Quoted 6 lines
block.c +1 -1
@@ -1275,12 +1275,11 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
                             const char *node_name, QDict *options,
                             int open_flags, Error **errp)
 {
-    Error *local_err = NULL;
+    DEF_AUTO_ERRP_V2(auto_errp, errp);

This is option 2. Basically, you replace any declaration of local_err to instead use the magic macro...

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1282

Quoted 7 lines
block.c +2 -3
     int i, ret;
 
-    bdrv_assign_node_name(bs, node_name, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_assign_node_name(bs, node_name, auto_errp);
+    if (*auto_errp) {

then s/local_err/*auto_errp/ and delete resulting &* pairs and error_propagate() calls in the rest of the function. Coccinelle could make this type of conversion easy.

Quoted 17 lines
block.c +2 -2
         return -EINVAL;
     }
 
@@ -1290,20 +1289,21 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
 
     if (drv->bdrv_file_open) {
         assert(!drv->bdrv_needs_filename || bs->filename[0]);
-        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_file_open(bs, options, open_flags, auto_errp);
     } else if (drv->bdrv_open) {
-        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_open(bs, options, open_flags, auto_errp);
     } else {
         ret = 0;
     }
 
     if (ret < 0) {

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1303

Quoted 10 lines
block.c +4 -6
-        if (local_err) {
-            error_propagate(errp, local_err);
-        } else if (bs->filename[0]) {
-            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
-        } else {
-            error_setg_errno(errp, -ret, "Could not open image");
+        if (!*auto_errp) {
+            if (bs->filename[0]) {
+                error_setg_errno(errp, -ret, "Could not open '%s'",
+                                 bs->filename);

You reflowed the logic a bit here, but it is still worth pointing out that you still call error_setg*(errp) as before (no changing to auto_errp, although that would also have worked).

Quoted 19 lines
block.c +5 -3
+            } else {
+                error_setg_errno(errp, -ret, "Could not open image");
+            }
         }
         goto open_failed;
     }
@@ -1314,9 +1314,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
         return ret;
     }
 
-    bdrv_refresh_limits(bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_refresh_limits(bs, auto_errp);
+    if (*auto_errp) {
         return -EINVAL;
     }
 

block.c out: · line 4240

Quoted 5 lines
block.c +1 -1
@@ -4238,17 +4237,17 @@ out:
 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
                  Error **errp)
 {
-    Error *local_err = NULL;
+    g_auto(ErrorPropagator) prop = {.errp = errp};

This is option 1. It's rather open-coded, rather than having a nice wrapper macro.

block.c out: · line 4242

Quoted 5 lines
block.c +1 -3
-    bdrv_set_backing_hd(bs_new, bs_top, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    errp = &prop.local_err;

And by re-assigning errp manually,

block.c out: · line 4245

Quoted 3 lines
block.c +3 -0
+
+    bdrv_set_backing_hd(bs_new, bs_top, errp);
+    if (*errp) {

you can now safely use it in the rest of the function without worrying about NULL.

block.c out: · line 4252

Quoted 10 lines
block.c +2 -3
         goto out;
     }
 
-    bdrv_replace_node(bs_top, bs_new, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_replace_node(bs_top, bs_new, errp);
+    if (*errp) {
         bdrv_set_backing_hd(bs_new, NULL, &error_abort);
         goto out;

Not too bad (we'd probably want a coccinelle script to prove that you don't dereference *errp without first using the magic reassignment).

block.c void bdrv_init_with_whitelist(void) · line 5311

Quoted 6 lines
block.c +1 -0
     }
@@ -5309,9 +5308,9 @@ void bdrv_init_with_whitelist(void)
 static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
                                                   Error **errp)
 {
+    DEF_AUTO_ERRP(auto_errp, errp);

Option 1 again, but this time with a macro.

Quoted 59 lines
block.c +7 -12
     BdrvChild *child, *parent;
     uint64_t perm, shared_perm;
-    Error *local_err = NULL;
     int ret;
     BdrvDirtyBitmap *bm;
 
@@ -5324,9 +5323,8 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
     }
 
     QLIST_FOREACH(child, &bs->children, next) {
-        bdrv_co_invalidate_cache(child->bs, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        bdrv_co_invalidate_cache(child->bs, auto_errp);
+        if (*auto_errp) {
             return;
         }
     }
@@ -5346,19 +5344,17 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
      */
     bs->open_flags &= ~BDRV_O_INACTIVE;
     bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
-    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
+    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, auto_errp);
     if (ret < 0) {
         bs->open_flags |= BDRV_O_INACTIVE;
-        error_propagate(errp, local_err);
         return;
     }
     bdrv_set_perm(bs, perm, shared_perm);
 
     if (bs->drv->bdrv_co_invalidate_cache) {
-        bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
-        if (local_err) {
+        bs->drv->bdrv_co_invalidate_cache(bs, auto_errp);
+        if (*auto_errp) {
             bs->open_flags |= BDRV_O_INACTIVE;
-            error_propagate(errp, local_err);
             return;
         }
     }
@@ -5378,10 +5374,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
 
     QLIST_FOREACH(parent, &bs->parents, next_parent) {
         if (parent->role->activate) {
-            parent->role->activate(parent, &local_err);
-            if (local_err) {
+            parent->role->activate(parent, auto_errp);
+            if (*auto_errp) {
                 bs->open_flags |= BDRV_O_INACTIVE;
-                error_propagate(errp, local_err);
                 return;
             }
         }
block/backup.c +0 -0
index 89f7f89200..462dea4fbb 100644

block/backup.c static const BlockJobDriver backup_job_driver = { · line 589

Quoted 7 lines
block/backup.c +4 -0
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
 static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                              Error **errp)
 {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);

Option 1 again. Requires renaming errp to auto_errp in the rest of the function.

Quoted 21 lines
block/backup.c +2 -2
     int ret;
     BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                     BACKUP_CLUSTER_SIZE_DEFAULT);
         return BACKUP_CLUSTER_SIZE_DEFAULT;
     } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
             "Couldn't determine the cluster size of the target image, "
             "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
             "Aborting, since this may create an unusable destination image\n");
         return ret;
     } else if (ret < 0 && target->backing) {
block/gluster.c +0 -0
index 64028b2cba..799a2dbeca 100644

block/gluster.c static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, · line 700

Quoted 6 lines
block/gluster.c +3 -0
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                               QDict *options, Error **errp)
 {
     int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call.

Option 3. By far my favorite, whether or not we decide to take a macro parameter for the variable name to protect, or hard-code for consistency that 'errp' must be in scope when it is used.

Quoted 2 lines

Note, it must be placed exactly after
+ * all local variables defenition.

definition

Why do you think this limitation is necessary? Do you have an actual code sequence that misbehaves if the compiler-unwind cleanup is performed in a different order when the macro is used before other variable declarations?

block/gluster.c static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, · line 707

Quoted 7 lines
block/gluster.c +3 -0
+     */
+    MAKE_ERRP_SAFE(errp);
+
     if (filename) {
         ret = qemu_gluster_parse_uri(gconf, filename);
         if (ret < 0) {

This is sweet - you can safely use '*errp' in the rest of the function, without having to remember a second error name - while the caller can still pass NULL or error_abort as desired.

And I still think we can probably get Coccinelle to help make the conversions, both of using this macro in any function that has an Error **errp parameter, as well as getting rid of local_err declarations and error_propagate() calls rendered redundant once this macro is used.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 130 lines · Eric Blake
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

Quoted 6 lines

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

It might have been nice to do a patch series of one proposal per patch, rather than cramming three in one, if only to see...

Quoted 10 lines

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)

...relative diffstat sizing differences between them.

Of course, adding the framework requires growth in error.h. The real question, then, is how many lines do we save elsewhere by getting rid of boilerplate that is replaced by auto cleanup, and how many lines are mechanically churned to change variable names. And how much of that churn can itself be automated with Coccinelle.

But I think the idea is worth pursuing!

Quoted 9 lines
include/qapi/error.h +0 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                          ErrorClass err_class, const char *fmt, ...)
      GCC_FMT_ATTR(6, 7);
 

include/qapi/error.h void error_set_internal(Error **errp, · line 334

Quoted 10 lines
include/qapi/error.h +10 -0
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }

Technically, you don't need the 'if' here, since error_propogate() works just fine when prop->local_err is NULL.

include/qapi/error.h void error_set_internal(Error **errp, · line 338

Quoted 4 lines
include/qapi/error.h +4 -0
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+

Looks reasonable.

include/qapi/error.h void error_set_internal(Error **errp, · line 348

Quoted 10 lines
include/qapi/error.h +10 -0
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.

s/it's/its/ ("it's" is only appropriate if "it is" can be substituted)

include/qapi/error.h void error_set_internal(Error **errp, · line 360

Quoted 12 lines
include/qapi/error.h +12 -0
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}

I don't like the type-punning.

Quoted 4 lines
include/qapi/error.h +4 -0
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+

include/qapi/error.h void error_set_internal(Error **errp, · line 374

Quoted 10 lines
include/qapi/error.h +10 -0
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}

Again, more type-punning. Ends up declaring the equivalent of 'Error *auto_errp[2]' which devolves to 'Error **auto_errp'.

So, using this macro requires me to pass in the name of my local variable (which auto-propagates when it goes out of scope) and the errp parameter. Can we shortcut by declaring that the variable it propagates to MUST be named 'errp' rather than having that as a parameter name (doing so would force us to be consistent at using an Error **errp parameter).

Quoted 9 lines
include/qapi/error.h +9 -0
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
Quoted 8 lines

include/qapi/error.h void error_set_internal(Error **errp, · line 389

Quoted 6 lines
include/qapi/error.h +6 -0
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;

No real difference from ErrorPropagator above.

Sorry, I just forget to remove ErrorPropagator.

Quoted 27 lines

include/qapi/error.h void error_set_internal(Error **errp, · line 395

Quoted 7 lines
include/qapi/error.h +6 -0
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }

Another useless if.

include/qapi/error.h void error_set_internal(Error **errp, · line 406

Quoted 11 lines
include/qapi/error.h +11 -0
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);

Not written to take a trailing semicolon in the caller.

Less type-punning, and and tries to reuse errp where possible.

Quoted 16 lines

include/qapi/error.h void error_set_internal(Error **errp, · line 417

Quoted 11 lines
include/qapi/error.h +11 -0
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.

Why? I see no reason why it can't be hoisted earlier than other declarations, and the only reason to not sink it after earlier code that doesn't touch errp would be our coding standards that frowns on declaration after code.

Hmm, I thought compiler would warn about mixing code and definitions. Seems that gcc don't care, so it's OK.

Quoted 14 lines

include/qapi/error.h void error_set_internal(Error **errp, · line 418

Quoted 2 lines
include/qapi/error.h +1 -0
+ *     - like v2, several statements in one open macro

Not a drawback in my mind.

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines
include/qapi/error.h +6 -0
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 5 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

This is actually quite cool.

I glad to see that you like my favorite variant)

Quoted 7 lines

And if you get rid of your insistence that
it must occur after other variable declarations, you could instead easily automate that any function that has a parameter 'Error **errp' then has a MAKE_ERRP_SAFE(errp); as the first line of its function body (that becomes something that you could grep for, rather than having to use the smarts of coccinelle).

Quoted 6 lines

Or if we want to enforce consistency on the parameter naming, even go with:

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = errp}; \
errp = &__auto_errp_prop.local_err

I am for

Quoted 238 lines

block.c

Quoted 9 lines
include/qapi/error.h +2 -0
+
+
  /*
   * Special error destination to abort on error.
   * See error_setg() and error_propagate() for details.
block.c +0 -0
index 5944124845..5253663329 100644

So the above was the proposed implementations (I'm leaning towards option 3); the below is a demonstration of their use.

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1278

Quoted 6 lines
block.c +1 -1
@@ -1275,12 +1275,11 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
                              const char *node_name, QDict *options,
                              int open_flags, Error **errp)
  {
-    Error *local_err = NULL;
+    DEF_AUTO_ERRP_V2(auto_errp, errp);

This is option 2. Basically, you replace any declaration of local_err to instead use the magic macro...

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1282

Quoted 7 lines
block.c +2 -3
      int i, ret;
 
-    bdrv_assign_node_name(bs, node_name, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_assign_node_name(bs, node_name, auto_errp);
+    if (*auto_errp) {

then s/local_err/*auto_errp/ and delete resulting &* pairs and error_propagate() calls in the rest of the function. Coccinelle could make this type of conversion easy.

Quoted 17 lines
block.c +2 -2
          return -EINVAL;
      }
 
@@ -1290,20 +1289,21 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
 
      if (drv->bdrv_file_open) {
          assert(!drv->bdrv_needs_filename || bs->filename[0]);
-        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_file_open(bs, options, open_flags, auto_errp);
      } else if (drv->bdrv_open) {
-        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_open(bs, options, open_flags, auto_errp);
      } else {
          ret = 0;
      }
 
      if (ret < 0) {

block.c static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, · line 1303

Quoted 10 lines
block.c +4 -6
-        if (local_err) {
-            error_propagate(errp, local_err);
-        } else if (bs->filename[0]) {
-            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
-        } else {
-            error_setg_errno(errp, -ret, "Could not open image");
+        if (!*auto_errp) {
+            if (bs->filename[0]) {
+                error_setg_errno(errp, -ret, "Could not open '%s'",
+                                 bs->filename);

You reflowed the logic a bit here, but it is still worth pointing out that you still call error_setg*(errp) as before (no changing to auto_errp, although that would also have worked).

Quoted 19 lines
block.c +5 -3
+            } else {
+                error_setg_errno(errp, -ret, "Could not open image");
+            }
          }
          goto open_failed;
      }
@@ -1314,9 +1314,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
          return ret;
      }
 
-    bdrv_refresh_limits(bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_refresh_limits(bs, auto_errp);
+    if (*auto_errp) {
          return -EINVAL;
      }
 

block.c out: · line 4240

Quoted 5 lines
block.c +1 -1
@@ -4238,17 +4237,17 @@ out:
  void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
                   Error **errp)
  {
-    Error *local_err = NULL;
+    g_auto(ErrorPropagator) prop = {.errp = errp};

This is option 1. It's rather open-coded, rather than having a nice wrapper macro.

block.c out: · line 4242

Quoted 5 lines
block.c +1 -3
-    bdrv_set_backing_hd(bs_new, bs_top, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    errp = &prop.local_err;

And by re-assigning errp manually,

block.c out: · line 4245

Quoted 3 lines
block.c +3 -0
+
+    bdrv_set_backing_hd(bs_new, bs_top, errp);
+    if (*errp) {

you can now safely use it in the rest of the function without worrying about NULL.

block.c out: · line 4252

Quoted 10 lines
block.c +2 -3
          goto out;
      }
 
-    bdrv_replace_node(bs_top, bs_new, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_replace_node(bs_top, bs_new, errp);
+    if (*errp) {
          bdrv_set_backing_hd(bs_new, NULL, &error_abort);
          goto out;

Not too bad (we'd probably want a coccinelle script to prove that you don't dereference *errp without first using the magic reassignment).

block.c void bdrv_init_with_whitelist(void) · line 5311

Quoted 6 lines
block.c +1 -0
      }
@@ -5309,9 +5308,9 @@ void bdrv_init_with_whitelist(void)
  static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
                                                    Error **errp)
  {
+    DEF_AUTO_ERRP(auto_errp, errp);

Option 1 again, but this time with a macro.

Quoted 59 lines
block.c +7 -12
      BdrvChild *child, *parent;
      uint64_t perm, shared_perm;
-    Error *local_err = NULL;
      int ret;
      BdrvDirtyBitmap *bm;
 
@@ -5324,9 +5323,8 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
      }
 
      QLIST_FOREACH(child, &bs->children, next) {
-        bdrv_co_invalidate_cache(child->bs, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        bdrv_co_invalidate_cache(child->bs, auto_errp);
+        if (*auto_errp) {
              return;
          }
      }
@@ -5346,19 +5344,17 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
       */
      bs->open_flags &= ~BDRV_O_INACTIVE;
      bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
-    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
+    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, auto_errp);
      if (ret < 0) {
          bs->open_flags |= BDRV_O_INACTIVE;
-        error_propagate(errp, local_err);
          return;
      }
      bdrv_set_perm(bs, perm, shared_perm);
 
      if (bs->drv->bdrv_co_invalidate_cache) {
-        bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
-        if (local_err) {
+        bs->drv->bdrv_co_invalidate_cache(bs, auto_errp);
+        if (*auto_errp) {
              bs->open_flags |= BDRV_O_INACTIVE;
-            error_propagate(errp, local_err);
              return;
          }
      }
@@ -5378,10 +5374,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
 
      QLIST_FOREACH(parent, &bs->parents, next_parent) {
          if (parent->role->activate) {
-            parent->role->activate(parent, &local_err);
-            if (local_err) {
+            parent->role->activate(parent, auto_errp);
+            if (*auto_errp) {
                  bs->open_flags |= BDRV_O_INACTIVE;
-                error_propagate(errp, local_err);
                  return;
              }
          }
block/backup.c +0 -0
index 89f7f89200..462dea4fbb 100644

block/backup.c static const BlockJobDriver backup_job_driver = { · line 589

Quoted 7 lines
block/backup.c +4 -0
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
  static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                               Error **errp)
  {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);

Option 1 again. Requires renaming errp to auto_errp in the rest of the function.

Quoted 21 lines
block/backup.c +2 -2
      int ret;
      BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                      BACKUP_CLUSTER_SIZE_DEFAULT);
          return BACKUP_CLUSTER_SIZE_DEFAULT;
      } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
              "Couldn't determine the cluster size of the target image, "
              "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
              "Aborting, since this may create an unusable destination image\n");
          return ret;
      } else if (ret < 0 && target->backing) {
block/gluster.c +0 -0
index 64028b2cba..799a2dbeca 100644

block/gluster.c static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, · line 700

Quoted 6 lines
block/gluster.c +3 -0
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                                QDict *options, Error **errp)
  {
      int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call.

Option 3. By far my favorite, whether or not we decide to take a macro parameter for the variable name to protect, or hard-code for consistency that 'errp' must be in scope when it is used.

Quoted 2 lines

Note, it must be placed exactly after
+ * all local variables defenition.

Quoted 6 lines

definition

Why do you think this limitation is necessary? Do you have an actual code sequence that misbehaves if the compiler-unwind cleanup is performed in a different order when the macro is used before other variable declarations?

No I just thought that this is a requirement to not mix definitions with code, I was wrong and that's good.

Quoted 9 lines

block/gluster.c static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, · line 707

Quoted 8 lines
block/gluster.c +3 -0
+     */
+    MAKE_ERRP_SAFE(errp);
+
      if (filename) {
          ret = qemu_gluster_parse_uri(gconf, filename);
          if (ret < 0) {
Quoted 9 lines

This is sweet - you can safely use '*errp' in the rest of the function, without having to remember a second error name - while the caller can still pass NULL or error_abort as desired.

And I still think we can probably get Coccinelle to help make the conversions, both of using this macro in any function that has an Error **errp parameter, as well as getting rid of local_err declarations and error_propagate() calls rendered redundant once this macro is used.

Thanks! And sorry for dirty draft.

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

+/*
+ * Third variant:
+ * Pros:
+ * - simpler movement for functions which don't have local_err yet
+ * the only thing to do is to call one macro at function start.
+ * This extremely simplifies Greg's series
+ * Cons:
+ * - looks like errp shadowing.. Still seems safe.
+ * - must be after all definitions of local variables and before any
+ * code.

Quoted 7 lines
Quoted 4 lines

Why? I see no reason why it can't be hoisted earlier than other declarations, and the only reason to not sink it after earlier code that doesn't touch errp would be our coding standards that frowns on declaration after code.

Hmm, I thought compiler would warn about mixing code and definitions. Seems that gcc don't care, so it's OK.

C89 required all definitions before code, but that's historical. Meanwhile, we require a compiler that supports C99 as well as at least the __attribute__((cleanup)) extension (gcc and clang qualify, nothing else really does, but no one has been complaining). And C99 requires compiler support for intermixing definitions (in part because c++ did it first, then gcc allowed that as an extension in C89); so it's been permissible to intermix code and declarations for more than 20 years now. The only reason we don't do it more is because of habits and aesthetics, rather than necessity.

Quoted 12 lines
Quoted 2 lines

This is actually quite cool.

I glad to see that you like my favorite variant)

Quoted 7 lines

And if you get rid of your insistence that
it must occur after other variable declarations, you could instead easily automate that any function that has a parameter 'Error **errp' then has a MAKE_ERRP_SAFE(errp); as the first line of its function body (that becomes something that you could grep for, rather than having to use the smarts of coccinelle).

Quoted 8 lines
Quoted 6 lines

Or if we want to enforce consistency on the parameter naming, even go with:

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = errp}; \
errp = &__auto_errp_prop.local_err

I am for

So now to wait for other comments.

Quoted 13 lines
Quoted 13 lines

block/gluster.c static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, · line 707

Quoted 8 lines
block/gluster.c +3 -0
+     */
+    MAKE_ERRP_SAFE(errp);
+
      if (filename) {
          ret = qemu_gluster_parse_uri(gconf, filename);
          if (ret < 0) {

This is sweet - you can safely use '*errp' in the rest of the function, without having to remember a second error name - while the caller can still pass NULL or error_abort as desired.

Quoted 7 lines
Quoted 5 lines

And I still think we can probably get Coccinelle to help make the conversions, both of using this macro in any function that has an Error **errp parameter, as well as getting rid of local_err declarations and error_propagate() calls rendered redundant once this macro is used.

Thanks! And sorry for dirty draft.

It was titled RFC, after all :)

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#4 lore raw
Quoted 4 lines · Eric Blake
Quoted 4 lines
Quoted 4 lines

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = errp}; \
errp = &__auto_errp_prop.local_err

I tried to see if this could be done with just a single declaration line, as in:

typedef struct ErrorPropagator {
    Error **errp;
    Error *local_err;
} ErrorPropagator;

#define MAKE_ERRP_SAFE() \

  g_auto(ErrorPropagator) __auto_errp_prop = { \
    errp, ((errp = &__auto_errp_prop.local_err), NULL) }

But sadly, C17 paragraph 6.7.9P23 states:

"The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified."

which does not bode well for the assignment to __auto_errp_prop.errp. All changes to errp would have to be within the same initializer. Maybe:

#define MAKE_ERRP_SAFE() \

  g_auto(ErrorPropagator) __auto_errp_prop = { \
    .local_err = (__auto_errp_prop.err = errp, \
        (errp = &__auto_errp_prop.local_err), NULL) }

but by the time you get that complicated, just using a statement is easier to read.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#5 lore raw
Quoted 25 lines · Eric Blake
Quoted 4 lines · Eric Blake
Quoted 4 lines
Quoted 4 lines

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = errp}; \
errp = &__auto_errp_prop.local_err

I tried to see if this could be done with just a single declaration line, as in:

typedef struct ErrorPropagator {
     Error **errp;
     Error *local_err;
} ErrorPropagator;
#define MAKE_ERRP_SAFE() \
   g_auto(ErrorPropagator) __auto_errp_prop = { \
     errp, ((errp = &__auto_errp_prop.local_err), NULL) }

But sadly, C17 paragraph 6.7.9P23 states:

"The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified."

Quoted 7 lines

which does not bode well for the assignment to __auto_errp_prop.errp. All changes to errp would have to be within the same initializer. Maybe:

#define MAKE_ERRP_SAFE() \
   g_auto(ErrorPropagator) __auto_errp_prop = { \
     .local_err = (__auto_errp_prop.err = errp, \
         (errp = &__auto_errp_prop.local_err), NULL) }

Is it guaranteed that .errp will not be initialized to NULL after evaluating of .local_err initializer?

Quoted 4 lines

but by the time you get that complicated, just using a statement is easier to read.

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw
Quoted 7 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines

"The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified."

which does not bode well for the assignment to __auto_errp_prop.errp. All changes to errp would have to be within the same initializer. Maybe:

Quoted 7 lines
Quoted 4 lines
#define MAKE_ERRP_SAFE() \
   g_auto(ErrorPropagator) __auto_errp_prop = { \
     .local_err = (__auto_errp_prop.err = errp, \
         (errp = &__auto_errp_prop.local_err), NULL) }

Is it guaranteed that .errp will not be initialized to NULL after evaluating of .local_err initializer?

Probably not.

Quoted 4 lines
Quoted 4 lines

but by the time you get that complicated, just using a statement is easier to read.

Either two declarations (the second being an unused dummy variable declared solely for its initializer's side-effects) or a declaration and a statement are the only sane ways I can see to provide guaranteed ordering. It's hidden behind a macro, so I don't care which.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#7 lore raw
In reply to Eric Blake

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

Kevin

#8 lore raw
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 5 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened.

Hmm, never tested it, but looking at the code I can't understand how can that be. src/func/line are set in error_setg.. and in error_propagate() we just set the errp of the caller, src/func/line unchanged.

Still, I see that these useful fields are printed only for error_abort, for which we usually have coredump, which provides a lot more information.

Quoted 2 lines

It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Aha, understand this point, error_abort just don't work as desired, if we swap it by local_err. And we can fix it by using macro: never create local_err for error_abort, let it abort exactly on error_setg.

Quoted 11 lines

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

Agreed

Signature · 2 lines
Best regards,
Vladimir
#9 lore raw
In reply to Kevin Wolf
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Yeah, that is very frustrating. For personal development you can eventually figure it out, but with customer support requests, all you get is the stack trace and you've no core file to investigate, so you're stuck. IOW, as a general principle, we should strive to minimize the usage of error propagate.

The key reason why we typically need the propagate calls, is because we allow the passed in Error **errp parameter to be NULL, while at the same time we want to inspect it to see if its contents are non-NULL to detect failure. I venture to suggest that this is flawed API design on our part. We should not need to inspect the error object to see if a method call fails - the return value can be used for this purpose.

IOW, instead of this pattern with typically 'void' methods

     extern void somemethod(Error **errp);

     void thismethod(Error **errp) {
        Error *local_err = NULL;
	...
        somemethod(&local_err);
        if (local_err) {
	    error_propagate(errp, local_err);
	    return;
	}
        ...
     }

We should be preferring

     extern int somemethod(Error **errp);

     int thismethod(Error **errp) {
	...
        if (somemethod(errp) < 0) {
	    return -1;
	}
        ...
     }

ie only using the Error object to bubble up the *details* of the error, not as the mechanism for finding if an error occurred.

There is of course a downside with this approach, in that a method might set 'errp' but return 0, or not set 'errp' but return -1. I think this is outweighed by the simpler code pattern overall though.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw
Quoted 65 lines · Daniel P. Berrangé
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Yeah, that is very frustrating. For personal development you can eventually figure it out, but with customer support requests, all you get is the stack trace and you've no core file to investigate, so you're stuck. IOW, as a general principle, we should strive to minimize the usage of error propagate.

The key reason why we typically need the propagate calls, is because we allow the passed in Error **errp parameter to be NULL, while at the same time we want to inspect it to see if its contents are non-NULL to detect failure. I venture to suggest that this is flawed API design on our part. We should not need to inspect the error object to see if a method call fails - the return value can be used for this purpose.

IOW, instead of this pattern with typically 'void' methods

      extern void somemethod(Error **errp);

      void thismethod(Error **errp) {
         Error *local_err = NULL;
	...
         somemethod(&local_err);
         if (local_err) {
	    error_propagate(errp, local_err);
	    return;
	}
         ...
      }

We should be preferring

      extern int somemethod(Error **errp);

      int thismethod(Error **errp) {
	...
         if (somemethod(errp) < 0) {
	    return -1;
	}
         ...
      }
Quoted 9 lines

ie only using the Error object to bubble up the *details* of the error, not as the mechanism for finding if an error occurred.

There is of course a downside with this approach, in that a method might set 'errp' but return 0, or not set 'errp' but return -1. I think this is outweighed by the simpler code pattern overall though.

Agree. But seems that such change may be only done by hand.. Hmm, on the other hand, why not. May be I'll try do it for some parts of block layer.

Still, error_append_hint needs local_err in case of error_fatal.

Signature · 2 lines
Best regards,
Vladimir
#11 lore raw
Quoted 76 lines · Vladimir Sementsov-Ogievskiy
Quoted 65 lines · Daniel P. Berrangé
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Yeah, that is very frustrating. For personal development you can eventually figure it out, but with customer support requests, all you get is the stack trace and you've no core file to investigate, so you're stuck. IOW, as a general principle, we should strive to minimize the usage of error propagate.

The key reason why we typically need the propagate calls, is because we allow the passed in Error **errp parameter to be NULL, while at the same time we want to inspect it to see if its contents are non-NULL to detect failure. I venture to suggest that this is flawed API design on our part. We should not need to inspect the error object to see if a method call fails - the return value can be used for this purpose.

IOW, instead of this pattern with typically 'void' methods

      extern void somemethod(Error **errp);

      void thismethod(Error **errp) {
         Error *local_err = NULL;
	...
         somemethod(&local_err);
         if (local_err) {
	    error_propagate(errp, local_err);
	    return;
	}
         ...
      }

We should be preferring

      extern int somemethod(Error **errp);

      int thismethod(Error **errp) {
	...
         if (somemethod(errp) < 0) {
	    return -1;
	}
         ...
      }
Quoted 9 lines

ie only using the Error object to bubble up the *details* of the error, not as the mechanism for finding if an error occurred.

There is of course a downside with this approach, in that a method might set 'errp' but return 0, or not set 'errp' but return -1. I think this is outweighed by the simpler code pattern overall though.

Quoted 4 lines

Agree. But seems that such change may be only done by hand.. Hmm, on the other hand, why not. May be I'll try do it for some parts of block layer.

Still, error_append_hint needs local_err in case of error_fatal.

Yep, fortunately that usage is comparatively rare vs use of error_propagate in general.

To be clear I don't have any objections to your overall idea of using attribute cleanup to simplify error propagation. As you say, it can still be useful for the error_append_hint, even if we use the code pattern I suggest more widely to eliminate propagation where possible.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#12 lore raw
In reply to Kevin Wolf
Quoted 27 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 6 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

Interesting, that to handle error_append_hint problem, we don't need to create local_err in case of errp==NULL either..

So, possibly, we need the following steps:

1. implement MAKE_ERRP_SAFE_FOR_HINT (which only leave "*(errp) == error_fatal" in the if condition
2. rebase Greg's series on it, to fix hints for fatal errors
3. implement MAKE_ERRP_SAFE_FOR_DEREFERENCE (which only leave "(errp) == NULL" in the if condition
4. convert all (almost all) local_err usage to use MAKE_ERRP_SAFE_FOR_DEREFERENCE, which will fix problem with error_abort (and also drop a lot of calls of error_propagate)
5. merely convert "void func(.., errp)" to "int func(.., errp)" and drop MAKE_ERRP_SAFE_FOR_DEREFERENCE() magic, together with dereferencing.

Signature · 2 lines
Best regards,
Vladimir
#13 lore raw

Am 19.09.2019 um 14:00 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 40 lines
Quoted 27 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 6 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

Interesting, that to handle error_append_hint problem, we don't need to create local_err in case of errp==NULL either..

So, possibly, we need the following steps:

Quoted 7 lines

1. implement MAKE_ERRP_SAFE_FOR_HINT (which only leave "*(errp) == error_fatal" in the if condition
2. rebase Greg's series on it, to fix hints for fatal errors
3. implement MAKE_ERRP_SAFE_FOR_DEREFERENCE (which only leave "(errp) == NULL" in the if condition
4. convert all (almost all) local_err usage to use MAKE_ERRP_SAFE_FOR_DEREFERENCE, which will fix problem with error_abort (and also drop a lot of calls of error_propagate)
5. merely convert "void func(.., errp)" to "int func(.., errp)" and drop MAKE_ERRP_SAFE_FOR_DEREFERENCE() magic, together with dereferencing.

Long macro names, but as the parameter will always only be "errp", it fits easily on a line, so this is fine.

I think I like this plan.

Kevin

#14 lore raw
Quoted 49 lines · Kevin Wolf

Am 19.09.2019 um 14:00 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 40 lines
Quoted 27 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 6 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

Interesting, that to handle error_append_hint problem, we don't need to create local_err in case of errp==NULL either..

So, possibly, we need the following steps:

Quoted 7 lines

1. implement MAKE_ERRP_SAFE_FOR_HINT (which only leave "*(errp) == error_fatal" in the if condition
2. rebase Greg's series on it, to fix hints for fatal errors
3. implement MAKE_ERRP_SAFE_FOR_DEREFERENCE (which only leave "(errp) == NULL" in the if condition
4. convert all (almost all) local_err usage to use MAKE_ERRP_SAFE_FOR_DEREFERENCE, which will fix problem with error_abort (and also drop a lot of calls of error_propagate)
5. merely convert "void func(.., errp)" to "int func(.., errp)" and drop MAKE_ERRP_SAFE_FOR_DEREFERENCE() magic, together with dereferencing.

Quoted 2 lines

Long macro names, but as the parameter will always only be "errp", it fits easily on a line, so this is fine.

Yes, I wanted to stress their meaning in plan..

Other variants, I can imagine:

MAKE_ERRP_SAFE_FOR_DEREFERENCE
WRAP_ERRP_FOR_DEREFERENCE
WRAP_NULL_ERRP

MAKE_ERRP_SAFE_FOR_HINT
WRAP_ERRP_FOR_HINT
WRAP_FATAL_ERRP

Quoted 5 lines

I think I like this plan.

Kevin

Signature · 2 lines
Best regards,
Vladimir
#15 lore raw
In reply to Kevin Wolf
Quoted 11 lines · Kevin Wolf
Quoted 11 lines

Interesting, that to handle error_append_hint problem, we don't need to create local_err in case of errp==NULL either..

So, possibly, we need the following steps:

1. implement MAKE_ERRP_SAFE_FOR_HINT (which only leave "*(errp) == error_fatal" in the if condition
2. rebase Greg's series on it, to fix hints for fatal errors
3. implement MAKE_ERRP_SAFE_FOR_DEREFERENCE (which only leave "(errp) == NULL" in the if condition
4. convert all (almost all) local_err usage to use MAKE_ERRP_SAFE_FOR_DEREFERENCE, which will fix problem with error_abort (and also drop a lot of calls of error_propagate)

Why do we need two macros? A single MAKE_ERRP_SAFE that covers both NULL and &error_fatal at the same time is sufficient. You can then always use append_hint and/or *errp as you wish, and the error_propagate() call is only used in two situations: caller passed NULL (so we free the error as ignored), caller passed &error_fatal (so the hint got appended before we propagate it to flag a more useful message).

Quoted 2 lines
Quoted 2 lines

5. merely convert "void func(.., errp)" to "int func(.., errp)" and drop MAKE_ERRP_SAFE_FOR_DEREFERENCE() magic, together with dereferencing.

That's orthogonal, but may also be useful cleanups. I don't think we need to drop the macro, though.

Quoted 8 lines

Long macro names, but as the parameter will always only be "errp", it fits easily on a line, so this is fine.

I think I like this plan.

Kevin

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#16 lore raw
In reply to Kevin Wolf
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Okay, based on that, I see the following desirable semantics:

Caller: one of 4 calling paradigms:

pass errp=NULL (we don't care about failures)
pass errp=&error_abort (we want to abort() as soon as possible as close to the real problem as possible)
pass errp=&error_fatal (we want to exit(), but only after collecting as much information as possible)
pass errp = anything else (we are collecting an error for other reasons, we may report it or let the caller decide or ...)

Callee: we want a SINGLE paradigm:

func (Error **errp)
{
MAKE_ERRP_SAFE();

now we can pass errp to any child function, test '*errp', or do
anything else, and we DON'T have to call error_propagate.

I think that means we need:

#define MAKE_ERRP_SAFE() \
g_auto(...) __auto_errp = { .errp = errp }; \

  do { \
    if (!errp || errp == &error_fatal) { errp = &__auto_errp.local; } \

} while (0)

So back to the caller semantics:

if the caller passed NULL, we've redirected errp locally so that we can use *errp at will; the auto-cleanup will free our local error.

if the caller passed &error_abort, we keep errp unchanged. *errp tests will never trigger, because we'll have already aborted in the child on the original errp, giving developers the best stack trace.

if the caller passed &error_fatal, we redirect errp. auto-cleanup will then error_propagate that back to the caller, producing as much nice information as possible.

if the caller passed anything else, we keep errp unchanged, so no extra error_propagate in the mix.

Quoted 10 lines

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

We also want to handle the caller passing NULL, so that we no longer have to introduce 'Error *local_error = NULL' everywhere.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#17 lore raw
Quoted 74 lines · Eric Blake
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Okay, based on that, I see the following desirable semantics:

Caller: one of 4 calling paradigms:

pass errp=NULL (we don't care about failures)
pass errp=&error_abort (we want to abort() as soon as possible as close to the real problem as possible)
pass errp=&error_fatal (we want to exit(), but only after collecting as much information as possible)
pass errp = anything else (we are collecting an error for other reasons, we may report it or let the caller decide or ...)

Callee: we want a SINGLE paradigm:

func (Error **errp)
{
MAKE_ERRP_SAFE();

now we can pass errp to any child function, test '*errp', or do
anything else, and we DON'T have to call error_propagate.

I think that means we need:

#define MAKE_ERRP_SAFE() \
   g_auto(...) __auto_errp = { .errp = errp }; \
   do { \
     if (!errp || errp == &error_fatal) { errp = &__auto_errp.local; } \
   } while (0)

So back to the caller semantics:

if the caller passed NULL, we've redirected errp locally so that we can use *errp at will; the auto-cleanup will free our local error.

if the caller passed &error_abort, we keep errp unchanged. *errp tests will never trigger, because we'll have already aborted in the child on the original errp, giving developers the best stack trace.

if the caller passed &error_fatal, we redirect errp. auto-cleanup will then error_propagate that back to the caller, producing as much nice information as possible.

if the caller passed anything else, we keep errp unchanged, so no extra error_propagate in the mix.

Quoted 5 lines

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 9 lines
Quoted 5 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

We also want to handle the caller passing NULL, so that we no longer have to introduce 'Error *local_error = NULL' everywhere.

With my plan of two different macro, I at least messed the case when we need both dereferencing and hints, which means third macro, or one macro with parameters, saying what to wrap.

And my aim was to follow the idea of "do propagation only if it really necessary in this case".

But may be you are right, and we shouldn't care so much.

1. What is bad, if we wrap NULL, when we only want to use hints? Seems nothing. Some extra actions on error path, but who cares about it?

2. What is bad, if we wrap error_fatal, when we only want to dereference, and don't use hints? Seems nothing again, on error path we will return from higher level, and a bit of extra work, but nothing worse..

So I tend to agree. But honestly, I didn't understand first part of Kevin's paragraph against propagation, so, may be he have more reasons to minimize number of cases when we propagate.

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

Signature · 2 lines
Best regards,
Vladimir
#18 lore raw
Quoted 9 lines · Vladimir Sementsov-Ogievskiy

With my plan of two different macro, I at least messed the case when we need both dereferencing and hints, which means third macro, or one macro with parameters, saying what to wrap.

And my aim was to follow the idea of "do propagation only if it really necessary in this case".

But may be you are right, and we shouldn't care so much.

Quoted 8 lines

1. What is bad, if we wrap NULL, when we only want to use hints? Seems nothing. Some extra actions on error path, but who cares about it?

2. What is bad, if we wrap error_fatal, when we only want to dereference, and don't use hints? Seems nothing again, on error path we will return from higher level, and a bit of extra work, but nothing worse..

So I tend to agree. But honestly, I didn't understand first part of Kevin's paragraph against propagation, so, may be he have more reasons to minimize number of cases when we propagate.

Here's the problem.

error_propagate(&error_abort, ...)

produces an abort() with a stack trace tied to your CURRENT location, not the location at which the error was first detected. We DO copy the function name and line number from the original detection, but we have moved on since that point in time, so the backtrace does not match the reported location in the error struct. So for error_abort, you want to abort as soon as possible, without any intermediate error_propagate - we use this to flag programmer errors, and want to make life easy for the programmer.

But for error_propagate(&error_fatal, ...), we WANT to delay things to as late as possible, in order to gather up as many additional append_hints as possible, before finally telling the user their problem.
In that case, we don't care about the stack trace or even the function
and line number of the failure, but about giving the user as much as we can to help them fix their command-line problem.

Quoted 4 lines

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

error_propagate() serves two purposes:

It is a crutch to make it easier to paper over passing NULL as errp (in that case, we substitute a non-NULL pointer, then we can use 'if (local_error)', and error_propagate() then frees local_error). Hiding this crutch behind a macro is desirable (less boilerplate).

It helps transfer from one error to another. In this form, we are fine using it for error_fatal (because we WANT the transfer to happen as late as possible, because once we transfer the program exits so we can't add any more hints), but not for error_abort (because we don't want to lose context by transferring), and wasteful at other times (write directly to the original error, instead of having to transfer). So again, hiding it in a macro means we no longer have to call it directly, but only in the places where it helps.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#19 lore raw
In reply to Vladimir Sementsov-Ogievskiy

Am 19.09.2019 um 16:13 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 93 lines
Quoted 74 lines · Eric Blake
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Okay, based on that, I see the following desirable semantics:

Caller: one of 4 calling paradigms:

pass errp=NULL (we don't care about failures)
pass errp=&error_abort (we want to abort() as soon as possible as close to the real problem as possible)
pass errp=&error_fatal (we want to exit(), but only after collecting as much information as possible)
pass errp = anything else (we are collecting an error for other reasons, we may report it or let the caller decide or ...)

Callee: we want a SINGLE paradigm:

func (Error **errp)
{
MAKE_ERRP_SAFE();

now we can pass errp to any child function, test '*errp', or do
anything else, and we DON'T have to call error_propagate.

I think that means we need:

#define MAKE_ERRP_SAFE() \
   g_auto(...) __auto_errp = { .errp = errp }; \
   do { \
     if (!errp || errp == &error_fatal) { errp = &__auto_errp.local; } \
   } while (0)

So back to the caller semantics:

if the caller passed NULL, we've redirected errp locally so that we can use *errp at will; the auto-cleanup will free our local error.

if the caller passed &error_abort, we keep errp unchanged. *errp tests will never trigger, because we'll have already aborted in the child on the original errp, giving developers the best stack trace.

if the caller passed &error_fatal, we redirect errp. auto-cleanup will then error_propagate that back to the caller, producing as much nice information as possible.

if the caller passed anything else, we keep errp unchanged, so no extra error_propagate in the mix.

Quoted 5 lines

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 9 lines
Quoted 5 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

We also want to handle the caller passing NULL, so that we no longer have to introduce 'Error *local_error = NULL' everywhere.

With my plan of two different macro, I at least messed the case when we need both dereferencing and hints, which means third macro, or one macro with parameters, saying what to wrap.

And my aim was to follow the idea of "do propagation only if it really necessary in this case".

But may be you are right, and we shouldn't care so much.

Quoted 8 lines

1. What is bad, if we wrap NULL, when we only want to use hints? Seems nothing. Some extra actions on error path, but who cares about it?

2. What is bad, if we wrap error_fatal, when we only want to dereference, and don't use hints? Seems nothing again, on error path we will return from higher level, and a bit of extra work, but nothing worse..

So I tend to agree. But honestly, I didn't understand first part of Kevin's paragraph against propagation, so, may be he have more reasons to minimize number of cases when we propagate.

I think my concerns were really only about error_abort and "normal" non-NULL errp losing some information about the origin of the error. And from this thread, it seems that I misremebered and the normal one is actually supposed to just work.

In any case, wrapping NULL and error_fatal should be fine, so I agree that a single macro should do.

Quoted 2 lines

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"?

Hm, I think it's more obviously correct if done at the top, but I also can't see any reason why using it only in a block wouldn't work. So I'd put it at the top just as a matter of style.

Quoted 1 line

Kevin, is something bad in propagation, when it not related to error_abort?

Probably not, unless I didn't misremember, but we misread the code.

Kevin

#20 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 99 lines · Vladimir Sementsov-Ogievskiy
Quoted 74 lines · Eric Blake
Quoted 15 lines · Kevin Wolf

Am 18.09.2019 um 19:10 hat Eric Blake geschrieben:

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 6 lines

+ */

include/qapi/error.h +5 -0
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Not written to take a trailing semicolon in the caller.

Quoted 3 lines

You could even set __auto_errp_prop unconditionally rather than trying to reuse incoming errp (the difference being that error_propagate() gets called more frequently).

Quoted 7 lines

I think this difference is actually a problem.

When debugging things, I hate error_propagate(). It means that the Error (specifically its fields src/func/line) points to the outermost error_propagate() rather than the place where the error really happened. It also makes error_abort completely useless because at the point where the process gets aborted, the interesting information is already lost.

Okay, based on that, I see the following desirable semantics:

Caller: one of 4 calling paradigms:

pass errp=NULL (we don't care about failures)
pass errp=&error_abort (we want to abort() as soon as possible as close to the real problem as possible)
pass errp=&error_fatal (we want to exit(), but only after collecting as much information as possible)
pass errp = anything else (we are collecting an error for other reasons, we may report it or let the caller decide or ...)

Callee: we want a SINGLE paradigm:

func (Error **errp)
{
     MAKE_ERRP_SAFE();

     now we can pass errp to any child function, test '*errp', or do anything else, and we DON'T have to call error_propagate.

I think that means we need:

#define MAKE_ERRP_SAFE() \
   g_auto(...) __auto_errp = { .errp = errp }; \
   do { \
     if (!errp || errp == &error_fatal) { errp = &__auto_errp.local; } \
   } while (0)

So back to the caller semantics:

if the caller passed NULL, we've redirected errp locally so that we can use *errp at will; the auto-cleanup will free our local error.

if the caller passed &error_abort, we keep errp unchanged.  *errp tests will never trigger, because we'll have already aborted in the child on the original errp, giving developers the best stack trace.

if the caller passed &error_fatal, we redirect errp.  auto-cleanup will then error_propagate that back to the caller, producing as much nice information as possible.

if the caller passed anything else, we keep errp unchanged, so no extra error_propagate in the mix.

Quoted 5 lines

So I'd really like to restrict the use of error_propagate() to places where it's absolutely necessary. Unless, of course, you can fix these practical problems that error_propagate() causes for debugging.

Quoted 9 lines
Quoted 5 lines

In fact, in the context of Greg's series, I think we really only need to support hints for error_fatal, which are cases that users are supposed to see. We should exclude error_abort in MAKE_ERRP_SAFE() because these are things that are never supposed to happen. A good stack trace is more important there than adding a hint to the message.

We also want to handle the caller passing NULL, so that we no longer have to introduce 'Error *local_error = NULL' everywhere.

With my plan of two different macro, I at least messed the case when we need both dereferencing and hints, which means third macro, or one macro with parameters, saying what to wrap.

And my aim was to follow the idea of "do propagation only if it really necessary in this case".

But may be you are right, and we shouldn't care so much.

1. What is bad, if we wrap NULL, when we only want to use hints? Seems nothing. Some extra actions on error path, but who cares about it?

2. What is bad, if we wrap error_fatal, when we only want to dereference, and don't use hints? Seems nothing again, on error path we will return from higher level, and a bit of extra work, but nothing worse..

Quoted 8 lines

So I tend to agree. But honestly, I didn't understand first part of Kevin's paragraph against propagation, so, may be he have more reasons to minimize number of cases when we propagate.

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

Or, even, we may use MAKE_ERRP_SAFE on every function, which have Error **errp argument, even if we neither dereference it nor append hints? Is it what you propose by "SINGLE paradigm"? It's of course simpler to script, to check in checkpatch and to maintain.

Signature · 2 lines
Best regards,
Vladimir
#21 lore raw
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 6 lines

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

Or, even, we may use MAKE_ERRP_SAFE on every function, which have Error **errp argument, even if we neither dereference it nor append hints? Is it what you propose by "SINGLE paradigm"? It's of course simpler to script, to check in checkpatch and to maintain.

Yes. The simpler we make the rules, and the less boilerplate it entails, the more likely that we can:

a) avoid exceptions and corner cases that cost review time
b) automate the conversion into complying with the rule
c) codify those rules in syntax check to ensure they are followed post-conversion

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#22 lore raw
Quoted 29 lines · Eric Blake
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 6 lines

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

Or, even, we may use MAKE_ERRP_SAFE on every function, which have Error **errp argument, even if we neither dereference it nor append hints? Is it what you propose by "SINGLE paradigm"? It's of course simpler to script, to check in checkpatch and to maintain.

Yes. The simpler we make the rules, and the less boilerplate it entails, the more likely that we can:

a) avoid exceptions and corner cases that cost review time
b) automate the conversion into complying with the rule
c) codify those rules in syntax check to ensure they are followed post-conversion

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Quoted 2 lines

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#23 lore raw
Quoted 11 lines · Daniel P. Berrangé
Quoted 10 lines

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Quoted 8 lines

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

If 100% of our callsites use the macro, then new contributors will quickly learn by observation alone that the macro usage must be important on any new function taking Error **errp, whether or not they actually read the macro to see what it does. If only 5% of our callsites use the macro, it's harder to argue that a new user will pick up on the nuances by observation alone (presumably, our docs would also spell it out, but we know that not everyone reads those...).

However, if we can automate syntax checks to reach a near-100% accuracy, we don't HAVE to worry about whether a new programmer picks up on the nuances by observation, because they will instead pick up the nuances by CI rejection messages. This is true for _either_ style:

100% use of the macro: CI message would be "you added a method with a parameter 'Error **errp' but forgot to use MAKE_ERRP_SAFE"

use of the macro only where necessary (namely, functions that contain '*errp' and/or 'error_append_hint'): CI message would either be "your function body requires MAKE_ERRP_SAFE but you forgot it" or "your function body does not require MAKE_ERRP_SAFE but you forgot to remove it". And this would work even for experienced committers editing existing functions (such as ongoing work to convert away from 'void child(errp); if (*errp)' and towards 'if (int child(errp) < 0)').

Writing the CI engine for the first case is easy, writing it for the second is a bit harder, but still seems tractable (since, for any function with an 'Error **errp' parameter, it should be easy to scan the function body for instances of '*errp' or 'error_append_hint', as well as to scan whether MAKE_ERRP_SAFE was present or absent accordingly).

Quoted 3 lines

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

The benefit of getting rid of the 'Error *local_err = NULL; ... error_propagate()' boilerplate is worth the cleverness, in my opinion, but especially if also accompanied by CI coverage that we abide by our new rules.

I'd really like to hear Markus' opinion on the matter, as Error maintainer.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#24 lore raw
Quoted 22 lines · Eric Blake
Quoted 11 lines · Daniel P. Berrangé
Quoted 10 lines

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Quoted 8 lines

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

Quoted 7 lines

If 100% of our callsites use the macro, then new contributors will quickly learn by observation alone that the macro usage must be important on any new function taking Error **errp, whether or not they actually read the macro to see what it does. If only 5% of our callsites use the macro, it's harder to argue that a new user will pick up on the nuances by observation alone (presumably, our docs would also spell it out, but we know that not everyone reads those...).

When I was a new contributor, it was not simple to understand, when and why we need to use local_err, keeping in mind that (this is not only reason, but also a consequence) we have places where local_err is used for no reason.

Quoted 34 lines

However, if we can automate syntax checks to reach a near-100% accuracy, we don't HAVE to worry about whether a new programmer picks up on the nuances by observation, because they will instead pick up the nuances by CI rejection messages. This is true for _either_ style:

100% use of the macro: CI message would be "you added a method with a parameter 'Error **errp' but forgot to use MAKE_ERRP_SAFE"

use of the macro only where necessary (namely, functions that contain '*errp' and/or 'error_append_hint'): CI message would either be "your function body requires MAKE_ERRP_SAFE but you forgot it" or "your function body does not require MAKE_ERRP_SAFE but you forgot to remove it". And this would work even for experienced committers editing existing functions (such as ongoing work to convert away from 'void child(errp); if (*errp)' and towards 'if (int child(errp) < 0)').

Writing the CI engine for the first case is easy, writing it for the second is a bit harder, but still seems tractable (since, for any function with an 'Error **errp' parameter, it should be easy to scan the function body for instances of '*errp' or 'error_append_hint', as well as to scan whether MAKE_ERRP_SAFE was present or absent accordingly).

Quoted 3 lines

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

The benefit of getting rid of the 'Error *local_err = NULL; ... error_propagate()' boilerplate is worth the cleverness, in my opinion, but especially if also accompanied by CI coverage that we abide by our new rules.

I'd really like to hear Markus' opinion on the matter, as Error maintainer.

Signature · 2 lines
Best regards,
Vladimir
#25 lore raw
In reply to Eric Blake
Quoted 22 lines · Eric Blake
Quoted 11 lines · Daniel P. Berrangé
Quoted 10 lines

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Quoted 8 lines

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

Quoted 7 lines

If 100% of our callsites use the macro, then new contributors will quickly learn by observation alone that the macro usage must be important on any new function taking Error **errp, whether or not they actually read the macro to see what it does. If only 5% of our callsites use the macro, it's harder to argue that a new user will pick up on the nuances by observation alone (presumably, our docs would also spell it out, but we know that not everyone reads those...).

To get some slightly less made-up stats, I did some searching:

- 4408 methods with an 'errp' parameter declared

git grep 'Error \*\*errp'| wc -l

- 696 methods with an 'Error *local' declared (what other names do we use for this?)

git grep 'Error \*local' | wc -l

- 1243 methods with an 'errp' parameter which have void return value (fuzzy number - my matching is quite crude)

git grep 'Error \*\*errp'| grep -E '(^| )void' | wc -l

- 11 methods using error_append_hint with a local_err

git grep append_hint | grep local | wc -l

This suggests to me, that if we used the 'return 0 / return -1' convention to indicate failure for the methods which currently return void, we could potentially only have 11 cases where a local error object is genuinely needed.

If my numbers are at all accurate, I still believe we're better off changing the return type and eliminating essentially all use of local error variables, as void funcs/local error objects appear to be the minority coding pattern.

Quoted 8 lines
Quoted 3 lines

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

The benefit of getting rid of the 'Error *local_err = NULL; ... error_propagate()' boilerplate is worth the cleverness, in my opinion, but especially if also accompanied by CI coverage that we abide by our new rules.

At least we're both wanting to eliminate the local error + propagation. The difference is whether we're genuinely eliminating it, or just hiding it from view via auto-cleanup & macro magic

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#26 lore raw
Quoted 42 lines · Daniel P. Berrangé
Quoted 22 lines · Eric Blake
Quoted 11 lines · Daniel P. Berrangé
Quoted 10 lines

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Quoted 8 lines

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

Quoted 7 lines

If 100% of our callsites use the macro, then new contributors will quickly learn by observation alone that the macro usage must be important on any new function taking Error **errp, whether or not they actually read the macro to see what it does. If only 5% of our callsites use the macro, it's harder to argue that a new user will pick up on the nuances by observation alone (presumably, our docs would also spell it out, but we know that not everyone reads those...).

To get some slightly less made-up stats, I did some searching:

- 4408 methods with an 'errp' parameter declared

git grep 'Error \*\*errp'| wc -l

- 696 methods with an 'Error *local' declared (what other names do we use for this?)

git grep 'Error \*local' | wc -l

Quoted 8 lines

- 1243 methods with an 'errp' parameter which have void return value (fuzzy number - my matching is quite crude)

git grep 'Error \*\*errp'| grep -E '(^| )void' | wc -l

- 11 methods using error_append_hint with a local_err

git grep append_hint | grep local | wc -l

why do you count only with local? Greg's series is here to bring local to all functions with append_hint:

# git grep append_hint | wc -l
85

(still, some functions may append_hint several times, so the number should be less) and file list is big:

# git grep -l append_hint
block.c
block/backup.c
block/dirty-bitmap.c
block/file-posix.c
block/gluster.c
block/qcow.c
block/qcow2.c
block/vhdx-log.c
block/vpc.c
chardev/spice.c
hw/9pfs/9p-local.c
hw/9pfs/9p-proxy.c
hw/arm/msf2-soc.c
hw/arm/virt.c
hw/audio/intel-hda.c
hw/intc/arm_gicv3_kvm.c
hw/misc/msf2-sysreg.c
hw/pci-bridge/pci_bridge_dev.c
hw/pci-bridge/pcie_root_port.c
hw/ppc/mac_newworld.c
hw/ppc/spapr.c
hw/ppc/spapr_irq.c
hw/ppc/spapr_pci.c
hw/rdma/vmw/pvrdma_main.c
hw/s390x/s390-ccw.c
hw/scsi/megasas.c
hw/scsi/mptsas.c
hw/scsi/scsi-disk.c
hw/scsi/scsi-generic.c
hw/usb/ccid-card-emulated.c
hw/usb/hcd-xhci.c
hw/vfio/common.c
hw/vfio/pci-quirks.c
hw/vfio/pci.c
hw/virtio/virtio-pci.c
hw/xen/xen_pt.c
hw/xen/xen_pt_config_init.c
include/qapi/error.h
migration/migration.c
nbd/client.c
nbd/server.c
qdev-monitor.c
target/arm/cpu64.c
target/ppc/kvm.c
util/error.c
util/qemu-option.c
util/qemu-sockets.c

Also, conversion to use macro everywhere may be done (seems so) by coccinelle script. But conversion which you mean, only by hand I think. Converting 1243 methods by hand is a huge task..

I think there are three consistent ways:

1. Use macro everywhere
2. Drop error_append_hint
3. Drop error_fatal

Quoted 29 lines

This suggests to me, that if we used the 'return 0 / return -1' convention to indicate failure for the methods which currently return void, we could potentially only have 11 cases where a local error object is genuinely needed.

If my numbers are at all accurate, I still believe we're better off changing the return type and eliminating essentially all use of local error variables, as void funcs/local error objects appear to be the minority coding pattern.

Quoted 8 lines
Quoted 3 lines

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

The benefit of getting rid of the 'Error *local_err = NULL; ... error_propagate()' boilerplate is worth the cleverness, in my opinion, but especially if also accompanied by CI coverage that we abide by our new rules.

At least we're both wanting to eliminate the local error + propagation. The difference is whether we're genuinely eliminating it, or just hiding it from view via auto-cleanup & macro magic

Regards,
Daniel

Signature · 2 lines
Best regards,
Vladimir
#27 lore raw
Quoted 46 lines · Vladimir Sementsov-Ogievskiy
Quoted 45 lines · Daniel P. Berrangé
Quoted 22 lines · Eric Blake
Quoted 11 lines · Daniel P. Berrangé
Quoted 10 lines

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Quoted 8 lines

The flipside is that MAKE_ERRP_SAFE hides a bunch of logic, so you don't really know what its doing without looking at it, and this is QEMU custom concept so one more thing to learn for new contributors.

While I think it is a nice trick, personally I think we would be better off if we simply used a code pattern which does not require de-referencing 'errp' at all, aside from exceptional cases. IOW, no added macro in 95% of all our methods using Error **errp.

Quoted 7 lines

If 100% of our callsites use the macro, then new contributors will quickly learn by observation alone that the macro usage must be important on any new function taking Error **errp, whether or not they actually read the macro to see what it does. If only 5% of our callsites use the macro, it's harder to argue that a new user will pick up on the nuances by observation alone (presumably, our docs would also spell it out, but we know that not everyone reads those...).

To get some slightly less made-up stats, I did some searching:

- 4408 methods with an 'errp' parameter declared

git grep 'Error \*\*errp'| wc -l

- 696 methods with an 'Error *local' declared (what other names do we use for this?)

git grep 'Error \*local' | wc -l

- 1243 methods with an 'errp' parameter which have void return value (fuzzy number - my matching is quite crude)

Quoted 8 lines
Quoted 5 lines
      git grep 'Error \*\*errp'|  grep -E '(^| )void' | wc -l

- 11 methods using error_append_hint with a local_err

git grep append_hint | grep local | wc -l

why do you count only with local? Greg's series is here to bring local to all functions with append_hint:

I hadn't noticed the scope of Greg's series :-)

Quoted 9 lines
# git grep append_hint | wc -l
85

Also, conversion to use macro everywhere may be done (seems so) by coccinelle script. But conversion which you mean, only by hand I think. Converting 1243 methods by hand is a huge task..

Yeah, it would be a non-negligible amount of work.

Quoted 5 lines

I think there are three consistent ways:

1. Use macro everywhere
2. Drop error_append_hint
3. Drop error_fatal

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#28 lore raw
In reply to Daniel P. Berrangé
Quoted 10 lines · Daniel P. Berrangé

To get some slightly less made-up stats, I did some searching:

- 4408 methods with an 'errp' parameter declared

git grep 'Error \*\*errp'| wc -l

- 696 methods with an 'Error *local' declared (what other names do we use for this?)

git grep 'Error \*local' | wc -l

Covers 'local' and 'local_err'. We also have:

git grep 'Error \*err\b' | wc -l
553

So 1249 local errors.

Quoted 16 lines

- 1243 methods with an 'errp' parameter which have void return value (fuzzy number - my matching is quite crude)

git grep 'Error \*\*errp'| grep -E '(^| )void' | wc -l

- 11 methods using error_append_hint with a local_err

git grep append_hint | grep local | wc -l

This suggests to me, that if we used the 'return 0 / return -1' convention to indicate failure for the methods which currently return void, we could potentially only have 11 cases where a local error object is genuinely needed.

Quoted 4 lines

If my numbers are at all accurate, I still believe we're better off changing the return type and eliminating essentially all use of local error variables, as void funcs/local error objects appear to be the minority coding pattern.

When relying on a return value, you do have to check whether a negative return value happens if and only if errp is set; that's something that's harder for code grepping to spot. I'm not opposed to that coding pattern, just pointing out that it requires some semantic analysis, while MAKE_ERRP_SAFE() coupled with checking *errp is easier to prove at a glance that the check for whether an error happened is 1:1 associated with whether an error is reported.

Quoted 18 lines
Quoted 10 lines
Quoted 5 lines

There are lots of neat things we could do with auto-cleanup functions we I think we need to be wary of hiding too much cleverness behind macros when doing so overall.

The benefit of getting rid of the 'Error *local_err = NULL; ... error_propagate()' boilerplate is worth the cleverness, in my opinion, but especially if also accompanied by CI coverage that we abide by our new rules.

At least we're both wanting to eliminate the local error + propagation. The difference is whether we're genuinely eliminating it, or just hiding it from view via auto-cleanup & macro magic

Regards,
Daniel

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#29 lore raw
In reply to Eric Blake
Quoted 29 lines · Eric Blake
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 6 lines

To the same topic, of minimization: should we always call MAKE_ERRP_SAFE at function top, or only in block, where it is needed (assume, we dereference it only inside some "if" or "while"? Kevin, is something bad in propagation, when it not related to error_abort?

Or, even, we may use MAKE_ERRP_SAFE on every function, which have Error **errp argument, even if we neither dereference it nor append hints? Is it what you propose by "SINGLE paradigm"? It's of course simpler to script, to check in checkpatch and to maintain.

Yes. The simpler we make the rules, and the less boilerplate it entails, the more likely that we can:

a) avoid exceptions and corner cases that cost review time
b) automate the conversion into complying with the rule
c) codify those rules in syntax check to ensure they are followed post-conversion

ALWAYS using MAKE_ERRP_SAFE() on entry to any function that has an Error **errp parameter is dirt-simple to explain. It has no performance penalty if the user passed in a normal error or error_abort (the cost of an 'if' hidden in the macro is probably negligible compared to everything else we do), and has no semantic penalty if the user passed in NULL or error_fatal (we now get the behavior we want with less boilerplate).

Quoted 3 lines

Having to think 'does this method require me to use MAKE_ERRP_SAFE, or can I omit it?' does not provide the same simplicity.

Interesting: it's another story, but with this macro used in each errp-function we can collect the whole call-stack of the error int Error object, and report it.
It may be not good for end-user, but very useful for testing.

Or is it bad idea? Anyway, I often have the problem: I have some error reported, and need to understand where was it from.. git grep helps, but backtrace would be a lot better.

Signature · 2 lines
Best regards,
Vladimir
#30 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Subject: [Qemu-devel] [RFC] error: auto propagated local_err
Message-id: [email protected]
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Switched to a new branch 'test'
16e8874 error: auto propagated local_err

=== OUTPUT BEGIN ===
WARNING: line over 80 characters
#291: FILE: include/qapi/error.h:391:
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)

ERROR: Macros with multiple statements should be enclosed in a do - while loop #301: FILE: include/qapi/error.h:401:
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+ g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+ Error **auto_errp = \

+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);

ERROR: Macros with multiple statements should be enclosed in a do - while loop #320: FILE: include/qapi/error.h:420:
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+ (errp) = &__auto_errp_prop.local_err; \
+}

total: 2 errors, 1 warnings, 277 lines checked

Commit 16e8874947e7 (error: auto propagated local_err) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#31 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 121 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
 include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
 block.c              |  63 ++++++++++++--------------
 block/backup.c       |   8 +++-
 block/gluster.c      |   7 +++
 4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +90 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                         ErrorClass err_class, const char *fmt, ...)
     GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Using that idea, what about something like this:

hw/s390x/s390-virtio-ccw.c +23 -6
index 8bfb6684cb..043ad69f8b 100644
@@ -58,22 +58,42 @@ S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
     return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
 }
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+#define DEFINE_LOCAL_ERRP(_errp) \
+g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = (_errp)}; \
+Error **local_errp = &__auto_errp_prop.local_err
+
 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                               Error **errp)
 {
+    DEFINE_LOCAL_ERRP(errp);
     S390CPU *cpu = S390_CPU(object_new(typename));
-    Error *err = NULL;
 
-    object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
-    if (err != NULL) {
+    object_property_set_int(OBJECT(cpu), core_id, "core-id", local_errp);
+    if (*local_errp != NULL) {
         goto out;
     }
-    object_property_set_bool(OBJECT(cpu), true, "realized", &err);
+    object_property_set_bool(OBJECT(cpu), true, "realized", local_errp);
 
 out:
     object_unref(OBJECT(cpu));
-    if (err) {
-        error_propagate(errp, err);
+    if (*local_errp) {
         cpu = NULL;
     }
     return cpu;
Signature · 3 lines
Thanks,

David / dhildenb
#32 lore raw
Quoted 178 lines · David Hildenbrand
Quoted 121 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +90 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                          ErrorClass err_class, const char *fmt, ...)
      GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Using that idea, what about something like this:

hw/s390x/s390-virtio-ccw.c +21 -4
index 8bfb6684cb..043ad69f8b 100644
@@ -58,22 +58,42 @@ S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
      return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
  }
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+#define DEFINE_LOCAL_ERRP(_errp) \
+g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = (_errp)}; \
+Error **local_errp = &__auto_errp_prop.local_err
+
  static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                                Error **errp)
  {
+    DEFINE_LOCAL_ERRP(errp);
      S390CPU *cpu = S390_CPU(object_new(typename));
-    Error *err = NULL;
 
-    object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
-    if (err != NULL) {
+    object_property_set_int(OBJECT(cpu), core_id, "core-id", local_errp);
+    if (*local_errp != NULL) {
          goto out;
      }
-    object_property_set_bool(OBJECT(cpu), true, "realized", &err);

hw/s390x/s390-virtio-ccw.c S390CPU *s390_cpu_addr2state(uint16_t cpu_addr) · line 96

Quoted 10 lines
hw/s390x/s390-virtio-ccw.c +2 -2
+    object_property_set_bool(OBJECT(cpu), true, "realized", local_errp);
 
  out:
      object_unref(OBJECT(cpu));
-    if (err) {
-        error_propagate(errp, err);
+    if (*local_errp) {
          cpu = NULL;
      }
      return cpu;

So it's DEF_AUTO_ERRP_V2 with first parameter hardcoded to be local_errp. I still prefer MAKE_ERRP_SAFE(), to not introduce extra variables.

Signature · 2 lines
Best regards,
Vladimir
#33 lore raw
Quoted 179 lines · Vladimir Sementsov-Ogievskiy
Quoted 178 lines · David Hildenbrand
Quoted 121 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +90 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                          ErrorClass err_class, const char *fmt, ...)
      GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Using that idea, what about something like this:

hw/s390x/s390-virtio-ccw.c +22 -4
index 8bfb6684cb..043ad69f8b 100644
@@ -58,22 +58,42 @@ S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
      return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
  }
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+#define DEFINE_LOCAL_ERRP(_errp) \
+g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = (_errp)}; \
+Error **local_errp = &__auto_errp_prop.local_err
+
  static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                                Error **errp)
  {
+    DEFINE_LOCAL_ERRP(errp);
      S390CPU *cpu = S390_CPU(object_new(typename));
-    Error *err = NULL;
 
-    object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
-    if (err != NULL) {
+    object_property_set_int(OBJECT(cpu), core_id, "core-id", local_errp);
+    if (*local_errp != NULL) {
          goto out;
      }
-    object_property_set_bool(OBJECT(cpu), true, "realized", &err);
+    object_property_set_bool(OBJECT(cpu), true, "realized", local_errp);
 
Quoted 14 lines

hw/s390x/s390-virtio-ccw.c S390CPU *s390_cpu_addr2state(uint16_t cpu_addr) · line 96

Quoted 10 lines
hw/s390x/s390-virtio-ccw.c +1 -2
  out:
      object_unref(OBJECT(cpu));
-    if (err) {
-        error_propagate(errp, err);
+    if (*local_errp) {
          cpu = NULL;
      }
      return cpu;

So it's DEF_AUTO_ERRP_V2 with first parameter hardcoded to be local_errp. I still prefer MAKE_ERRP_SAFE(), to not introduce extra variables.

I lost track of the different approaches ;)

The local variable will most probably optimized out by the compiler. I dislike MAKE_ERRP_SAFE(), as it mixes defining a new variable with code.

Signature · 3 lines
Thanks,

David / dhildenb
#34 lore raw
Quoted 195 lines · David Hildenbrand
Quoted 179 lines · Vladimir Sementsov-Ogievskiy
Quoted 178 lines · David Hildenbrand
Quoted 121 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
   include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
   block.c              |  63 ++++++++++++--------------
   block/backup.c       |   8 +++-
   block/gluster.c      |   7 +++
   4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +90 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                           ErrorClass err_class, const char *fmt, ...)
       GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Using that idea, what about something like this:

hw/s390x/s390-virtio-ccw.c +22 -4
index 8bfb6684cb..043ad69f8b 100644
@@ -58,22 +58,42 @@ S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
       return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
   }
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+#define DEFINE_LOCAL_ERRP(_errp) \
+g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = (_errp)}; \
+Error **local_errp = &__auto_errp_prop.local_err
+
   static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                                 Error **errp)
   {
+    DEFINE_LOCAL_ERRP(errp);
       S390CPU *cpu = S390_CPU(object_new(typename));
-    Error *err = NULL;
 
-    object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
-    if (err != NULL) {
+    object_property_set_int(OBJECT(cpu), core_id, "core-id", local_errp);
+    if (*local_errp != NULL) {
           goto out;
       }
-    object_property_set_bool(OBJECT(cpu), true, "realized", &err);
+    object_property_set_bool(OBJECT(cpu), true, "realized", local_errp);
 
Quoted 14 lines

hw/s390x/s390-virtio-ccw.c S390CPU *s390_cpu_addr2state(uint16_t cpu_addr) · line 96

Quoted 10 lines
hw/s390x/s390-virtio-ccw.c +1 -2
   out:
       object_unref(OBJECT(cpu));
-    if (err) {
-        error_propagate(errp, err);
+    if (*local_errp) {
           cpu = NULL;
       }
       return cpu;

So it's DEF_AUTO_ERRP_V2 with first parameter hardcoded to be local_errp. I still prefer MAKE_ERRP_SAFE(), to not introduce extra variables.

Quoted 5 lines

I lost track of the different approaches ;)

The local variable will most probably optimized out by the compiler. I dislike MAKE_ERRP_SAFE(), as it mixes defining a new variable with code.

But it makes Greg's series extremely simple: just add MAKE_ERRP_SAFE() to some functions. And as Eric explains, mixing code and definitions is not a problem.

Still, we can do like this:

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = errp}; \
Error **__local_errp_unused __attribute__ ((unused)) = (errp = &__auto_errp_prop.local_err)

Which are two valid definitions.

Signature · 2 lines
Best regards,
Vladimir
#35 lore raw
Quoted 196 lines · Vladimir Sementsov-Ogievskiy
Quoted 195 lines · David Hildenbrand
Quoted 193 lines · Vladimir Sementsov-Ogievskiy
Quoted 178 lines · David Hildenbrand
Quoted 121 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
   include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
   block.c              |  63 ++++++++++++--------------
   block/backup.c       |   8 +++-
   block/gluster.c      |   7 +++
   4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +90 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                           ErrorClass err_class, const char *fmt, ...)
       GCC_FMT_ATTR(6, 7);
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:

include/qapi/error.h void error_set_internal(Error **errp, · line 424

Quoted 10 lines
include/qapi/error.h +10 -0
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}

Using that idea, what about something like this:

hw/s390x/s390-virtio-ccw.c +21 -4
index 8bfb6684cb..043ad69f8b 100644
@@ -58,22 +58,42 @@ S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
       return S390_CPU(ms->possible_cpus->cpus[cpu_addr].cpu);
   }
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+#define DEFINE_LOCAL_ERRP(_errp) \
+g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = (_errp)}; \
+Error **local_errp = &__auto_errp_prop.local_err
+
   static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                                 Error **errp)
   {
+    DEFINE_LOCAL_ERRP(errp);
       S390CPU *cpu = S390_CPU(object_new(typename));
-    Error *err = NULL;
 
-    object_property_set_int(OBJECT(cpu), core_id, "core-id", &err);
-    if (err != NULL) {
+    object_property_set_int(OBJECT(cpu), core_id, "core-id", local_errp);
+    if (*local_errp != NULL) {
           goto out;
       }
-    object_property_set_bool(OBJECT(cpu), true, "realized", &err);

hw/s390x/s390-virtio-ccw.c S390CPU *s390_cpu_addr2state(uint16_t cpu_addr) · line 96

Quoted 10 lines
hw/s390x/s390-virtio-ccw.c +2 -2
+    object_property_set_bool(OBJECT(cpu), true, "realized", local_errp);
 
   out:
       object_unref(OBJECT(cpu));
-    if (err) {
-        error_propagate(errp, err);
+    if (*local_errp) {
           cpu = NULL;
       }
       return cpu;

So it's DEF_AUTO_ERRP_V2 with first parameter hardcoded to be local_errp. I still prefer MAKE_ERRP_SAFE(), to not introduce extra variables.

Quoted 8 lines
Quoted 5 lines

I lost track of the different approaches ;)

The local variable will most probably optimized out by the compiler. I dislike MAKE_ERRP_SAFE(), as it mixes defining a new variable with code.

But it makes Greg's series extremely simple: just add MAKE_ERRP_SAFE() to some functions. And as Eric explains, mixing code and definitions is not a problem.

I still dislike it ;)

Quoted 8 lines

Still, we can do like this:

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagator) (__auto_errp_prop) = {.errp = errp}; \
Error **__local_errp_unused __attribute__ ((unused)) = (errp = &__auto_errp_prop.local_err)

Which are two valid definitions.

Yeah, I would prefer something like that!

Signature · 3 lines
Thanks,

David / dhildenb
#36 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

Quoted 7 lines
---
 include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
 block.c              |  63 ++++++++++++--------------
 block/backup.c       |   8 +++-
 block/gluster.c      |   7 +++
 4 files changed, 144 insertions(+), 36 deletions(-)

If the combination of “if (local_err) { error_propagate(...); ... }” is what’s cumbersome, can’t this be done simpler by adding an error_propagate() variant with a return value?

i.e.

bool has_error_then_propagate(Error **errp, Error *err)

{
    if (!err) {
        return false;
    }
    error_propagate(errp, err);
    return true;
}

And then turn all instances of

if (local_err) {
    error_propagate(errp, local_err);
    ...
}

into

if (has_error_then_propagate(errp, local_err)) {
    ...
}

?

Max

#37 lore raw
Quoted 48 lines · Max Reitz
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

Quoted 7 lines
---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)

If the combination of “if (local_err) { error_propagate(...); ... }” is what’s cumbersome, can’t this be done simpler by adding an error_propagate() variant with a return value?

i.e.

bool has_error_then_propagate(Error **errp, Error *err)

{
     if (!err) {
         return false;
     }
     error_propagate(errp, err);
     return true;
}

And then turn all instances of

if (local_err) {
     error_propagate(errp, local_err);
     ...
}

into

Quoted 8 lines
if (has_error_then_propagate(errp, local_err)) {
     ...
}

?

Max

No, originally cumbersome is introducing local_err in a lot of new places by Greg's series. MAKE_ERRP_SAFE macro makes it as simple as one macro call instead (in each function where we need local_err).

Also, auto-propagation seems correct thing to do, which fits good into g_auto concept, so even without any macro it just allows to drop several error_propagate calls (or may be several goto statements to do one error_propagate call) into one definitions. It's the same story like with g_autofree vs g_free.

Signature · 2 lines
Best regards,
Vladimir
#38 lore raw
Quoted 53 lines · Vladimir Sementsov-Ogievskiy
Quoted 52 lines · Max Reitz
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

Quoted 7 lines
---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)

If the combination of “if (local_err) { error_propagate(...); ... }” is what’s cumbersome, can’t this be done simpler by adding an error_propagate() variant with a return value?

i.e.

bool has_error_then_propagate(Error **errp, Error *err)

{
     if (!err) {
         return false;
     }
     error_propagate(errp, err);
     return true;
}

And then turn all instances of

if (local_err) {
     error_propagate(errp, local_err);
     ...
}

into

if (has_error_then_propagate(errp, local_err)) {
     ...
}
Quoted 8 lines
Quoted 4 lines

?

Max

No, originally cumbersome is introducing local_err in a lot of new places by Greg's series. MAKE_ERRP_SAFE macro makes it as simple as one macro call instead (in each function where we need local_err).

Does it need more than one local_err per function?

Because if it didn’t, I’d find one “Error *local_err;” simpler than one macro incantation.

(It has the same LoC, and it makes code readers ask the same question:
“Why do we need it?” Which has the same answer for both; but one is immediately readable code, whereas the other is a macro.)

Quoted 4 lines

Also, auto-propagation seems correct thing to do, which fits good into g_auto concept, so even without any macro it just allows to drop several error_propagate calls (or may be several goto statements to do one error_propagate call) into one definitions. It's the same story like with g_autofree vs g_free.

I don’t see the advantage here. You have to do the if () statement anyway, so it isn’t like you’re saving any LoC. In addition, I personally don’t find code hidden through __attribute__((cleanup)) easier to understand than explicitly written code.

It isn’t like I don’t like __attribute__((cleanup)). But it does count under “magic” in my book, and thus I’d avoid it if it doesn’t bring actual advantages. (It does bring actual advantages for things like auto-freeing memory, auto-closing FDs, or zeroing secret buffers before freeing them. In all those cases, you save LoC, and you prevent accidental leakage. I don’t quite see such advantages here, but I may well be mistaken.)

Now Kevin has given an actual advantage, which is that local_err complicates debugging. I’ve never had that problem myself, but that would indeed be an advantage that may warrant some magic.

Max

#39 lore raw
Quoted 63 lines · Max Reitz
Quoted 53 lines · Vladimir Sementsov-Ogievskiy
Quoted 52 lines · Max Reitz
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

Quoted 7 lines
---
   include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
   block.c              |  63 ++++++++++++--------------
   block/backup.c       |   8 +++-
   block/gluster.c      |   7 +++
   4 files changed, 144 insertions(+), 36 deletions(-)

If the combination of “if (local_err) { error_propagate(...); ... }” is what’s cumbersome, can’t this be done simpler by adding an error_propagate() variant with a return value?

i.e.

bool has_error_then_propagate(Error **errp, Error *err)

{
      if (!err) {
          return false;
      }
      error_propagate(errp, err);
      return true;
}

And then turn all instances of

if (local_err) {
      error_propagate(errp, local_err);
      ...
}

into

if (has_error_then_propagate(errp, local_err)) {
      ...
}
Quoted 8 lines
Quoted 4 lines

?

Max

No, originally cumbersome is introducing local_err in a lot of new places by Greg's series. MAKE_ERRP_SAFE macro makes it as simple as one macro call instead (in each function where we need local_err).

Quoted 8 lines

Does it need more than one local_err per function?

Because if it didn’t, I’d find one “Error *local_err;” simpler than one macro incantation.

(It has the same LoC, and it makes code readers ask the same question:
“Why do we need it?” Which has the same answer for both; but one is immediately readable code, whereas the other is a macro.)

Not the same, you didn't count error_propagate

And your example don't match Greg's case, there no "if (local_err)" in it, just "error_append_hint(errp)", which don't work for error_fatal and error_abort (Yes, I agree with Kevin, that it should work only for error_fatal)

Quoted 26 lines
Quoted 5 lines

Also, auto-propagation seems correct thing to do, which fits good into g_auto concept, so even without any macro it just allows to drop several error_propagate calls (or may be several goto statements to do one error_propagate call) into one definitions. It's the same story like with g_autofree vs g_free.

I don’t see the advantage here. You have to do the if () statement anyway, so it isn’t like you’re saving any LoC. In addition, I personally don’t find code hidden through __attribute__((cleanup)) easier to understand than explicitly written code.

It isn’t like I don’t like __attribute__((cleanup)). But it does count under “magic” in my book, and thus I’d avoid it if it doesn’t bring actual advantages. (It does bring actual advantages for things like auto-freeing memory, auto-closing FDs, or zeroing secret buffers before freeing them. In all those cases, you save LoC, and you prevent accidental leakage. I don’t quite see such advantages here, but I may well be mistaken.)

Now Kevin has given an actual advantage, which is that local_err complicates debugging. I’ve never had that problem myself, but that would indeed be an advantage that may warrant some magic.

Max

Signature · 2 lines
Best regards,
Vladimir
#40 lore raw
Quoted 66 lines · Vladimir Sementsov-Ogievskiy
Quoted 65 lines · Max Reitz
Quoted 53 lines · Vladimir Sementsov-Ogievskiy
Quoted 52 lines · Max Reitz
Quoted 15 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

Quoted 7 lines
---
   include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
   block.c              |  63 ++++++++++++--------------
   block/backup.c       |   8 +++-
   block/gluster.c      |   7 +++
   4 files changed, 144 insertions(+), 36 deletions(-)

If the combination of “if (local_err) { error_propagate(...); ... }” is what’s cumbersome, can’t this be done simpler by adding an error_propagate() variant with a return value?

i.e.

bool has_error_then_propagate(Error **errp, Error *err)

{
      if (!err) {
          return false;
      }
      error_propagate(errp, err);
      return true;
}

And then turn all instances of

if (local_err) {
      error_propagate(errp, local_err);
      ...
}

into

if (has_error_then_propagate(errp, local_err)) {
      ...
}
Quoted 8 lines
Quoted 4 lines

?

Max

No, originally cumbersome is introducing local_err in a lot of new places by Greg's series. MAKE_ERRP_SAFE macro makes it as simple as one macro call instead (in each function where we need local_err).

Does it need more than one local_err per function?

Quoted 8 lines
Quoted 6 lines

Because if it didn’t, I’d find one “Error *local_err;” simpler than one macro incantation.

(It has the same LoC, and it makes code readers ask the same question:
“Why do we need it?” Which has the same answer for both; but one is immediately readable code, whereas the other is a macro.)

Not the same, you didn't count error_propagate

I did, because it would part of the if () statement in my proposal.

Quoted 1 line

And your example don't match Greg's case, there no "if (local_err)" in it,

Ah, right, I see. OK then.

Quoted 2 lines

just "error_append_hint(errp)", which don't work for error_fatal and error_abort (Yes, I agree with Kevin, that it should work only for error_fatal)

True.

[...]

Quoted 3 lines
Quoted 3 lines

Now Kevin has given an actual advantage, which is that local_err complicates debugging. I’ve never had that problem myself, but that would indeed be an advantage that may warrant some magic.

Although after some more consideration I realized this probably cannot be achieved with this series, actually.

Max

#41 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 8 lines · On Wed, 18 Sep 2019 16:02:44 +0300 Vladimir Sementsov-Ogievskiy…

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

This will depend on whether we reach a consensus soon enough (soft freeze for 4.2 is 2019-10-29). Otherwise, I think my series should be merged as is, and auto-propagation to be delayed to 4.3.

Quoted 5 lines

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

When we have a good auto-propagation mechanism available, I guess this can be beneficial for most of the code, not only the places where we add hints (or prepend something, see below).

Quoted 17 lines

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
 include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
 block.c              |  63 ++++++++++++--------------
 block/backup.c       |   8 +++-
 block/gluster.c      |   7 +++
 4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +0 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                         ErrorClass err_class, const char *fmt, ...)
     GCC_FMT_ATTR(6, 7);

include/qapi/error.h void error_set_internal(Error **errp, · line 333

Quoted 10 lines
include/qapi/error.h +9 -0
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);

We also have error_propagate_prepend(), which is basically a variant of error_prepend()+error_propagate() that can cope with &error_fatal. This was introduced by Markus in commit 4b5766488fd3, for similar reasons that motivated my series. It has ~30 users in the tree.

There's no such thing a generic cleanup function with a printf-like interface, so all of these should be converted back to error_prepend() if we go for auto-propagation.

Aside from propagation, one can sometime choose to call things like error_free() or error_free_or_abort()... Auto-propagation should detect that and not call error_propagate() in this case.

Quoted 288 lines
include/qapi/error.h +93 -0
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}
+
+
 /*
  * Special error destination to abort on error.
  * See error_setg() and error_propagate() for details.
block.c +29 -34
index 5944124845..5253663329 100644
@@ -1275,12 +1275,11 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
                             const char *node_name, QDict *options,
                             int open_flags, Error **errp)
 {
-    Error *local_err = NULL;
+    DEF_AUTO_ERRP_V2(auto_errp, errp);
     int i, ret;
 
-    bdrv_assign_node_name(bs, node_name, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_assign_node_name(bs, node_name, auto_errp);
+    if (*auto_errp) {
         return -EINVAL;
     }
 
@@ -1290,20 +1289,21 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
 
     if (drv->bdrv_file_open) {
         assert(!drv->bdrv_needs_filename || bs->filename[0]);
-        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_file_open(bs, options, open_flags, auto_errp);
     } else if (drv->bdrv_open) {
-        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_open(bs, options, open_flags, auto_errp);
     } else {
         ret = 0;
     }
 
     if (ret < 0) {
-        if (local_err) {
-            error_propagate(errp, local_err);
-        } else if (bs->filename[0]) {
-            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
-        } else {
-            error_setg_errno(errp, -ret, "Could not open image");
+        if (!*auto_errp) {
+            if (bs->filename[0]) {
+                error_setg_errno(errp, -ret, "Could not open '%s'",
+                                 bs->filename);
+            } else {
+                error_setg_errno(errp, -ret, "Could not open image");
+            }
         }
         goto open_failed;
     }
@@ -1314,9 +1314,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
         return ret;
     }
 
-    bdrv_refresh_limits(bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_refresh_limits(bs, auto_errp);
+    if (*auto_errp) {
         return -EINVAL;
     }
 
@@ -4238,17 +4237,17 @@ out:
 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
                  Error **errp)
 {
-    Error *local_err = NULL;
+    g_auto(ErrorPropagator) prop = {.errp = errp};
 
-    bdrv_set_backing_hd(bs_new, bs_top, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    errp = &prop.local_err;
+
+    bdrv_set_backing_hd(bs_new, bs_top, errp);
+    if (*errp) {
         goto out;
     }
 
-    bdrv_replace_node(bs_top, bs_new, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_replace_node(bs_top, bs_new, errp);
+    if (*errp) {
         bdrv_set_backing_hd(bs_new, NULL, &error_abort);
         goto out;
     }
@@ -5309,9 +5308,9 @@ void bdrv_init_with_whitelist(void)
 static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
                                                   Error **errp)
 {
+    DEF_AUTO_ERRP(auto_errp, errp);
     BdrvChild *child, *parent;
     uint64_t perm, shared_perm;
-    Error *local_err = NULL;
     int ret;
     BdrvDirtyBitmap *bm;
 
@@ -5324,9 +5323,8 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
     }
 
     QLIST_FOREACH(child, &bs->children, next) {
-        bdrv_co_invalidate_cache(child->bs, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        bdrv_co_invalidate_cache(child->bs, auto_errp);
+        if (*auto_errp) {
             return;
         }
     }
@@ -5346,19 +5344,17 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
      */
     bs->open_flags &= ~BDRV_O_INACTIVE;
     bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
-    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
+    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, auto_errp);
     if (ret < 0) {
         bs->open_flags |= BDRV_O_INACTIVE;
-        error_propagate(errp, local_err);
         return;
     }
     bdrv_set_perm(bs, perm, shared_perm);
 
     if (bs->drv->bdrv_co_invalidate_cache) {
-        bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
-        if (local_err) {
+        bs->drv->bdrv_co_invalidate_cache(bs, auto_errp);
+        if (*auto_errp) {
             bs->open_flags |= BDRV_O_INACTIVE;
-            error_propagate(errp, local_err);
             return;
         }
     }
@@ -5378,10 +5374,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
 
     QLIST_FOREACH(parent, &bs->parents, next_parent) {
         if (parent->role->activate) {
-            parent->role->activate(parent, &local_err);
-            if (local_err) {
+            parent->role->activate(parent, auto_errp);
+            if (*auto_errp) {
                 bs->open_flags |= BDRV_O_INACTIVE;
-                error_propagate(errp, local_err);
                 return;
             }
         }
block/backup.c +6 -2
index 89f7f89200..462dea4fbb 100644
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
 static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                              Error **errp)
 {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);
     int ret;
     BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                     BACKUP_CLUSTER_SIZE_DEFAULT);
         return BACKUP_CLUSTER_SIZE_DEFAULT;
     } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
             "Couldn't determine the cluster size of the target image, "
             "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
             "Aborting, since this may create an unusable destination image\n");
         return ret;
     } else if (ret < 0 && target->backing) {
block/gluster.c +7 -0
index 64028b2cba..799a2dbeca 100644
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                               QDict *options, Error **errp)
 {
     int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call. Note, it must be placed exactly after
+     * all local variables defenition.
+     */
+    MAKE_ERRP_SAFE(errp);
+
     if (filename) {
         ret = qemu_gluster_parse_uri(gconf, filename);
         if (ret < 0) {
#42 lore raw
Quoted 59 lines · Greg Kurz
Quoted 8 lines · On Wed, 18 Sep 2019 16:02:44 +0300 Vladimir Sementsov-Ogievskiy…

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

This will depend on whether we reach a consensus soon enough (soft freeze for 4.2 is 2019-10-29). Otherwise, I think my series should be merged as is, and auto-propagation to be delayed to 4.3.

Quoted 5 lines

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

When we have a good auto-propagation mechanism available, I guess this can be beneficial for most of the code, not only the places where we add hints (or prepend something, see below).

Quoted 17 lines

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +0 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                          ErrorClass err_class, const char *fmt, ...)
      GCC_FMT_ATTR(6, 7);

include/qapi/error.h void error_set_internal(Error **errp, · line 333

Quoted 10 lines
include/qapi/error.h +9 -0
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);

We also have error_propagate_prepend(), which is basically a variant of error_prepend()+error_propagate() that can cope with &error_fatal. This was introduced by Markus in commit 4b5766488fd3, for similar reasons that motivated my series. It has ~30 users in the tree.

Quoted 7 lines

There's no such thing a generic cleanup function with a printf-like interface, so all of these should be converted back to error_prepend() if we go for auto-propagation.

Aside from propagation, one can sometime choose to call things like error_free() or error_free_or_abort()... Auto-propagation should detect that and not call error_propagate() in this case.

Hmm, for example, qmp_eject, which error_free or error_propagate.. We can leave such cases as is, not many of them. Or introduce safe_errp_free(Error **errp), which will zero pointer after freeing.

Quoted 290 lines
Quoted 289 lines
include/qapi/error.h +93 -0
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}
+
+
  /*
   * Special error destination to abort on error.
   * See error_setg() and error_propagate() for details.
block.c +29 -34
index 5944124845..5253663329 100644
@@ -1275,12 +1275,11 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
                              const char *node_name, QDict *options,
                              int open_flags, Error **errp)
  {
-    Error *local_err = NULL;
+    DEF_AUTO_ERRP_V2(auto_errp, errp);
      int i, ret;
 
-    bdrv_assign_node_name(bs, node_name, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_assign_node_name(bs, node_name, auto_errp);
+    if (*auto_errp) {
          return -EINVAL;
      }
 
@@ -1290,20 +1289,21 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
 
      if (drv->bdrv_file_open) {
          assert(!drv->bdrv_needs_filename || bs->filename[0]);
-        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_file_open(bs, options, open_flags, auto_errp);
      } else if (drv->bdrv_open) {
-        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_open(bs, options, open_flags, auto_errp);
      } else {
          ret = 0;
      }
 
      if (ret < 0) {
-        if (local_err) {
-            error_propagate(errp, local_err);
-        } else if (bs->filename[0]) {
-            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
-        } else {
-            error_setg_errno(errp, -ret, "Could not open image");
+        if (!*auto_errp) {
+            if (bs->filename[0]) {
+                error_setg_errno(errp, -ret, "Could not open '%s'",
+                                 bs->filename);
+            } else {
+                error_setg_errno(errp, -ret, "Could not open image");
+            }
          }
          goto open_failed;
      }
@@ -1314,9 +1314,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
          return ret;
      }
 
-    bdrv_refresh_limits(bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_refresh_limits(bs, auto_errp);
+    if (*auto_errp) {
          return -EINVAL;
      }
 
@@ -4238,17 +4237,17 @@ out:
  void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
                   Error **errp)
  {
-    Error *local_err = NULL;
+    g_auto(ErrorPropagator) prop = {.errp = errp};
 
-    bdrv_set_backing_hd(bs_new, bs_top, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    errp = &prop.local_err;
+
+    bdrv_set_backing_hd(bs_new, bs_top, errp);
+    if (*errp) {
          goto out;
      }
 
-    bdrv_replace_node(bs_top, bs_new, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_replace_node(bs_top, bs_new, errp);
+    if (*errp) {
          bdrv_set_backing_hd(bs_new, NULL, &error_abort);
          goto out;
      }
@@ -5309,9 +5308,9 @@ void bdrv_init_with_whitelist(void)
  static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
                                                    Error **errp)
  {
+    DEF_AUTO_ERRP(auto_errp, errp);
      BdrvChild *child, *parent;
      uint64_t perm, shared_perm;
-    Error *local_err = NULL;
      int ret;
      BdrvDirtyBitmap *bm;
 
@@ -5324,9 +5323,8 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
      }
 
      QLIST_FOREACH(child, &bs->children, next) {
-        bdrv_co_invalidate_cache(child->bs, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        bdrv_co_invalidate_cache(child->bs, auto_errp);
+        if (*auto_errp) {
              return;
          }
      }
@@ -5346,19 +5344,17 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
       */
      bs->open_flags &= ~BDRV_O_INACTIVE;
      bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
-    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
+    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, auto_errp);
      if (ret < 0) {
          bs->open_flags |= BDRV_O_INACTIVE;
-        error_propagate(errp, local_err);
          return;
      }
      bdrv_set_perm(bs, perm, shared_perm);
 
      if (bs->drv->bdrv_co_invalidate_cache) {
-        bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
-        if (local_err) {
+        bs->drv->bdrv_co_invalidate_cache(bs, auto_errp);
+        if (*auto_errp) {
              bs->open_flags |= BDRV_O_INACTIVE;
-            error_propagate(errp, local_err);
              return;
          }
      }
@@ -5378,10 +5374,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
 
      QLIST_FOREACH(parent, &bs->parents, next_parent) {
          if (parent->role->activate) {
-            parent->role->activate(parent, &local_err);
-            if (local_err) {
+            parent->role->activate(parent, auto_errp);
+            if (*auto_errp) {
                  bs->open_flags |= BDRV_O_INACTIVE;
-                error_propagate(errp, local_err);
                  return;
              }
          }
block/backup.c +6 -2
index 89f7f89200..462dea4fbb 100644
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
  static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                               Error **errp)
  {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);
      int ret;
      BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                      BACKUP_CLUSTER_SIZE_DEFAULT);
          return BACKUP_CLUSTER_SIZE_DEFAULT;
      } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
              "Couldn't determine the cluster size of the target image, "
              "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
              "Aborting, since this may create an unusable destination image\n");
          return ret;
      } else if (ret < 0 && target->backing) {
block/gluster.c +7 -0
index 64028b2cba..799a2dbeca 100644
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                                QDict *options, Error **errp)
  {
      int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call. Note, it must be placed exactly after
+     * all local variables defenition.
+     */
+    MAKE_ERRP_SAFE(errp);
+
      if (filename) {
          ret = qemu_gluster_parse_uri(gconf, filename);
          if (ret < 0) {
Signature · 2 lines
Best regards,
Vladimir
#43 lore raw
Quoted 64 lines · On Thu, 19 Sep 2019 09:28:11 +0000 Vladimir Sementsov-Ogievskiy…
Quoted 63 lines · Greg Kurz
Quoted 8 lines · On Wed, 18 Sep 2019 16:02:44 +0300 Vladimir Sementsov-Ogievskiy…

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

It also may help make Greg's series[1] about error_append_hint smaller.

This will depend on whether we reach a consensus soon enough (soft freeze for 4.2 is 2019-10-29). Otherwise, I think my series should be merged as is, and auto-propagation to be delayed to 4.3.

Quoted 5 lines

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

When we have a good auto-propagation mechanism available, I guess this can be beneficial for most of the code, not only the places where we add hints (or prepend something, see below).

Quoted 17 lines

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

---
  include/qapi/error.h | 102 +++++++++++++++++++++++++++++++++++++++++++
  block.c              |  63 ++++++++++++--------------
  block/backup.c       |   8 +++-
  block/gluster.c      |   7 +++
  4 files changed, 144 insertions(+), 36 deletions(-)
include/qapi/error.h +0 -0
index 3f95141a01..083e061014 100644
@@ -322,6 +322,108 @@ void error_set_internal(Error **errp,
                          ErrorClass err_class, const char *fmt, ...)
      GCC_FMT_ATTR(6, 7);

include/qapi/error.h void error_set_internal(Error **errp, · line 333

Quoted 10 lines
include/qapi/error.h +9 -0
 
+typedef struct ErrorPropagator {
+    Error **errp;
+    Error *local_err;
+} ErrorPropagator;
+
+static inline void error_propagator_cleanup(ErrorPropagator *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);

We also have error_propagate_prepend(), which is basically a variant of error_prepend()+error_propagate() that can cope with &error_fatal. This was introduced by Markus in commit 4b5766488fd3, for similar reasons that motivated my series. It has ~30 users in the tree.

There's no such thing a generic cleanup function with a printf-like interface, so all of these should be converted back to error_prepend() if we go for auto-propagation.

Quoted 8 lines
Quoted 3 lines

Aside from propagation, one can sometime choose to call things like error_free() or error_free_or_abort()... Auto-propagation should detect that and not call error_propagate() in this case.

Hmm, for example, qmp_eject, which error_free or error_propagate.. We can leave such cases as is, not many of them. Or introduce safe_errp_free(Error **errp), which will zero pointer after freeing.

Maybe even turning error_free() to take an Error ** ? It looks safe to zero out a dangling pointer. Of course the API change would need to be propagated to all error_* functions that explicitly call error_free().

Quoted 292 lines
Quoted 290 lines
Quoted 289 lines
include/qapi/error.h +93 -0
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
+
+/*
+ * ErrorPropagationPair
+ *
+ * [Error *local_err, Error **errp]
+ *
+ * First element is local_err, second is original errp, which is propagation
+ * target. Yes, errp has a bit another type, so it should be converted.
+ *
+ * ErrorPropagationPair may be used as errp, which points to local_err,
+ * as it's type is compatible.
+ */
+typedef Error *ErrorPropagationPair[2];
+
+static inline void error_propagation_pair_cleanup(ErrorPropagationPair *arr)
+{
+    Error *local_err = (*arr)[0];
+    Error **errp = (Error **)(*arr)[1];
+
+    if (local_err) {
+        error_propagate(errp, local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationPair,
+                                 error_propagation_pair_cleanup);
+
+/*
+ * DEF_AUTO_ERRP
+ *
+ * Define auto_errp variable, which may be used instead of errp, and
+ * *auto_errp may be safely checked to be zero or not, and may be safely
+ * used for error_append_hint(). auto_errp is automatically propagated
+ * to errp at function exit.
+ */
+#define DEF_AUTO_ERRP(auto_errp, errp) \
+    g_auto(ErrorPropagationPair) (auto_errp) = {NULL, (Error *)(errp)}
+
+
+/*
+ * Another variant:
+ *   Pros:
+ *     - normal structure instead of cheating with array
+ *     - we can directly use errp, if it's not NULL and don't point to
+ *       error_abort or error_fatal
+ *   Cons:
+ *     - we need to define two variables instead of one
+ */
+typedef struct ErrorPropagationStruct {
+    Error *local_err;
+    Error **errp;
+} ErrorPropagationStruct;
+
+static inline void error_propagation_struct_cleanup(ErrorPropagationStruct *prop)
+{
+    if (prop->local_err) {
+        error_propagate(prop->errp, prop->local_err);
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagationStruct,
+                                 error_propagation_struct_cleanup);
+
+#define DEF_AUTO_ERRP_V2(auto_errp, errp) \
+    g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+    Error **auto_errp = \
+        ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) ? \
+        &__auto_errp_prop.local_err : \
+        (errp);
+
+/*
+ * Third variant:
+ *   Pros:
+ *     - simpler movement for functions which don't have local_err yet
+ *       the only thing to do is to call one macro at function start.
+ *       This extremely simplifies Greg's series
+ *   Cons:
+ *     - looks like errp shadowing.. Still seems safe.
+ *     - must be after all definitions of local variables and before any
+ *       code.
+ *     - like v2, several statements in one open macro
+ */
+#define MAKE_ERRP_SAFE(errp) \
+g_auto(ErrorPropagationStruct) (__auto_errp_prop) = {.errp = (errp)}; \
+if ((errp) == NULL || *(errp) == error_abort || *(errp) == error_fatal) { \
+    (errp) = &__auto_errp_prop.local_err; \
+}
+
+
  /*
   * Special error destination to abort on error.
   * See error_setg() and error_propagate() for details.
block.c +29 -34
index 5944124845..5253663329 100644
@@ -1275,12 +1275,11 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
                              const char *node_name, QDict *options,
                              int open_flags, Error **errp)
  {
-    Error *local_err = NULL;
+    DEF_AUTO_ERRP_V2(auto_errp, errp);
      int i, ret;
 
-    bdrv_assign_node_name(bs, node_name, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_assign_node_name(bs, node_name, auto_errp);
+    if (*auto_errp) {
          return -EINVAL;
      }
 
@@ -1290,20 +1289,21 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
 
      if (drv->bdrv_file_open) {
          assert(!drv->bdrv_needs_filename || bs->filename[0]);
-        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_file_open(bs, options, open_flags, auto_errp);
      } else if (drv->bdrv_open) {
-        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
+        ret = drv->bdrv_open(bs, options, open_flags, auto_errp);
      } else {
          ret = 0;
      }
 
      if (ret < 0) {
-        if (local_err) {
-            error_propagate(errp, local_err);
-        } else if (bs->filename[0]) {
-            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
-        } else {
-            error_setg_errno(errp, -ret, "Could not open image");
+        if (!*auto_errp) {
+            if (bs->filename[0]) {
+                error_setg_errno(errp, -ret, "Could not open '%s'",
+                                 bs->filename);
+            } else {
+                error_setg_errno(errp, -ret, "Could not open image");
+            }
          }
          goto open_failed;
      }
@@ -1314,9 +1314,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
          return ret;
      }
 
-    bdrv_refresh_limits(bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_refresh_limits(bs, auto_errp);
+    if (*auto_errp) {
          return -EINVAL;
      }
 
@@ -4238,17 +4237,17 @@ out:
  void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
                   Error **errp)
  {
-    Error *local_err = NULL;
+    g_auto(ErrorPropagator) prop = {.errp = errp};
 
-    bdrv_set_backing_hd(bs_new, bs_top, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    errp = &prop.local_err;
+
+    bdrv_set_backing_hd(bs_new, bs_top, errp);
+    if (*errp) {
          goto out;
      }
 
-    bdrv_replace_node(bs_top, bs_new, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    bdrv_replace_node(bs_top, bs_new, errp);
+    if (*errp) {
          bdrv_set_backing_hd(bs_new, NULL, &error_abort);
          goto out;
      }
@@ -5309,9 +5308,9 @@ void bdrv_init_with_whitelist(void)
  static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
                                                    Error **errp)
  {
+    DEF_AUTO_ERRP(auto_errp, errp);
      BdrvChild *child, *parent;
      uint64_t perm, shared_perm;
-    Error *local_err = NULL;
      int ret;
      BdrvDirtyBitmap *bm;
 
@@ -5324,9 +5323,8 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
      }
 
      QLIST_FOREACH(child, &bs->children, next) {
-        bdrv_co_invalidate_cache(child->bs, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        bdrv_co_invalidate_cache(child->bs, auto_errp);
+        if (*auto_errp) {
              return;
          }
      }
@@ -5346,19 +5344,17 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
       */
      bs->open_flags &= ~BDRV_O_INACTIVE;
      bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
-    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err);
+    ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, auto_errp);
      if (ret < 0) {
          bs->open_flags |= BDRV_O_INACTIVE;
-        error_propagate(errp, local_err);
          return;
      }
      bdrv_set_perm(bs, perm, shared_perm);
 
      if (bs->drv->bdrv_co_invalidate_cache) {
-        bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
-        if (local_err) {
+        bs->drv->bdrv_co_invalidate_cache(bs, auto_errp);
+        if (*auto_errp) {
              bs->open_flags |= BDRV_O_INACTIVE;
-            error_propagate(errp, local_err);
              return;
          }
      }
@@ -5378,10 +5374,9 @@ static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
 
      QLIST_FOREACH(parent, &bs->parents, next_parent) {
          if (parent->role->activate) {
-            parent->role->activate(parent, &local_err);
-            if (local_err) {
+            parent->role->activate(parent, auto_errp);
+            if (*auto_errp) {
                  bs->open_flags |= BDRV_O_INACTIVE;
-                error_propagate(errp, local_err);
                  return;
              }
          }
block/backup.c +6 -2
index 89f7f89200..462dea4fbb 100644
@@ -583,6 +583,10 @@ static const BlockJobDriver backup_job_driver = {
  static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                                               Error **errp)
  {
+    /*
+     * Example of using DEF_AUTO_ERRP to make error_append_hint safe
+     */
+    DEF_AUTO_ERRP(auto_errp, errp);
      int ret;
      BlockDriverInfo bdi;
 
@@ -602,10 +606,10 @@ static int64_t backup_calculate_cluster_size(BlockDriverState *target,
                      BACKUP_CLUSTER_SIZE_DEFAULT);
          return BACKUP_CLUSTER_SIZE_DEFAULT;
      } else if (ret < 0 && !target->backing) {
-        error_setg_errno(errp, -ret,
+        error_setg_errno(auto_errp, -ret,
              "Couldn't determine the cluster size of the target image, "
              "which has no backing file");
-        error_append_hint(errp,
+        error_append_hint(auto_errp,
              "Aborting, since this may create an unusable destination image\n");
          return ret;
      } else if (ret < 0 && target->backing) {
block/gluster.c +7 -0
index 64028b2cba..799a2dbeca 100644
@@ -695,6 +695,13 @@ static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
                                QDict *options, Error **errp)
  {
      int ret;
+    /*
+     * Example of using MAKE_ERRP_SAFE to make error_append_hint safe. We
+     * only need to add one macro call. Note, it must be placed exactly after
+     * all local variables defenition.
+     */
+    MAKE_ERRP_SAFE(errp);
+
      if (filename) {
          ret = qemu_gluster_parse_uri(gconf, filename);
          if (ret < 0) {
#44 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a proposal (three of them, actually) of auto propagation for local_err, to not call error_propagate on every exit point, when we deal with local_err.

Quoted 9 lines

It also may help make Greg's series[1] about error_append_hint smaller.

See definitions and examples below.

I'm cc-ing to this RFC everyone from series[1] CC list, as if we like it, the idea will touch same code (and may be more).

[1]: https://lists.gnu.org/archive/html/qemu-devel/2019-09/msg03449.html

OK, seems a kind of consensus is here, and looks like

#define MAKE_ERRP_SAFE() \
g_auto(ErrorPropagationStruct) __auto_errp_prop = {.errp = errp}; \

Error **__local_errp_unused __attribute__ ((unused)) = \
     (errp = (errp == NULL || *errp == error_fatal ? \
              &__auto_errp_prop.local_error : errp))

[I also suggest to call it ERRP_FUNCTION_BEGIN, in case we'll enhance it in future (for example bring call stack into Error)]

And MAKE_ERRP_SAFE assumed to be used in all errp-functions.

Now, there are still several things to discuss:

1. Some functions want to do error_free(local_err), error_report_err(local_err), or like this, when they decide that error should not be propagated.

What to do with them? I suggest to make corresponding functions

error_free_errp, error_report_errp, warn_report_ferrp, etc, with Error **errp parameter, which calls corresponding Error* function and then set pointer to 0. Then our propagation cleanup will do nothing, as desired.

2. Some functions tends to error_free(*errp) or error_report_err(*errp). So, they don't use errp to return error, but instead they want to handle errp somehow:
vnc_client_io_error
- is used only in two places to trace errp, so it may be inlined

hmp_handle_error
- is a wrapper used in more than 80 places, to do error_reportf_err(*errp, "Error: ");
(hmm, bad that it doesn't set *errp to ZERO after it)

what do do with it? inline too?

or, maybe rename errp parameter to "Error **filled_errp", to not match our criteria?

(api function error_free_or_abort is with same behavior)

Just bugs:

3. Wow, fit_load_fdt() just error_free(*errp) in wrong way! It must set then *errp = NULL, but it doesn't do it. And qmp_query_cpu_def() is correct example of this thing - these functions definitely wants error_free_errp function. But qmp_query_cpu_def() incorrectly dereference errp (it may be NULL!)

4. A lot of functions in target/s390x/cpu_models.c just dereference errp to check error also:
build_guest_fsinfo_for_virtual_device
legacy_acpi_cpu_plug_cb
sclp_events_bus_realize
memory_device_get_free_addr
ipmi_isa_realize
isa_ipmi_bt_realize
legacy_acpi_cpu_plug_cb

5. file_ram_alloc has funny patter to check error:

     if (mem_prealloc) {
         os_mem_prealloc(fd, area, memory, ms->smp.cpus, errp);
         if (errp && *errp) {
             qemu_ram_munmap(fd, area, memory);
             return NULL;
         }
     }

Seems nothing interesting more. I used this coccinelle script @@
identifier fn;
@@

fn(..., Error **errp)

  {
      <...
*    *errp
      ...>

}

to search by this commend:

git grep -l 'Error \*\*errp' | xargs grep -l '[^*]\*errp' | xargs spatch --sp-file script

Signature · 2 lines
Best regards,
Vladimir