error: auto propagated local_err part I
Subsystems (2)
v10 cover error: auto propagated local_err part I
v10: (based-on "[PATCH 0/3] Minor error handling cleanups" including my 4/3 in it) 02: Change some comments.
Do not chain check1 and check2 rules to rule1 to cover move unusual cases to warn about.
Add positions to check1 rule.
Move check1 and check2 above rule1, otherwise our ___ hack will break check2 rule.
03: add hunk, due to rebasing and
09: rebased. Change is trivial, I keep r-b.
v10 is available at
https://src.openvz.org/scm/~vsementsov/qemu.git #tag up-auto-local-err-partI-v10
v9 is available at
https://src.openvz.org/scm/~vsementsov/qemu.git #tag up-auto-local-err-partI-v9
In these series, there is no commit-per-subsystem script, each generated commit is generated in separate.
Still, generating commands are very similar, and looks like
sed -n '/^<Subsystem name>$/,/^$/{s/^F: //p}' MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80Note, that in each generated commit, generation command is the only text, indented by 8 spaces in 'git log -1' output, so, to regenerate all commits (for example, after rebase, or change in coccinelle script), you may use the following command:
git rebase -x "sh -c \"git show --pretty= --name-only | xargs git checkout HEAD^ -- ; git reset; git log -1 | grep '^ ' | sh\"" HEAD~7
Which will start automated interactive rebase for generated patches, which will stop if generated patch changed
(you may do git commit --amend to apply updated generated changes).
Note:
git show --pretty= --name-only - lists files, changed in HEAD
git log -1 | grep '^ ' | sh - rerun generation command of HEAD
Check for compilation of changed .c files
git rebase -x "sh -c \"git show --pretty= --name-only | sed -n 's/\.c$/.o/p' | xargs make -j9\"" HEAD~7
Vladimir Sementsov-Ogievskiy (9):
error: auto propagated local_err
scripts: Coccinelle script to use ERRP_AUTO_PROPAGATE() SD (Secure Card): introduce ERRP_AUTO_PROPAGATE
pflash: introduce ERRP_AUTO_PROPAGATE
fw_cfg: introduce ERRP_AUTO_PROPAGATE
virtio-9p: introduce ERRP_AUTO_PROPAGATE
TPM: introduce ERRP_AUTO_PROPAGATE
nbd: introduce ERRP_AUTO_PROPAGATE
xen: introduce ERRP_AUTO_PROPAGATE
scripts/coccinelle/auto-propagated-errp.cocci | 336 ++++++++++++++++++ include/block/nbd.h | 1 + include/qapi/error.h | 208 +++++++++-- block/nbd.c | 21 +- hw/9pfs/9p-local.c | 12 +- hw/9pfs/9p.c | 1 + hw/block/dataplane/xen-block.c | 17 +- hw/block/pflash_cfi01.c | 7 +- hw/block/pflash_cfi02.c | 7 +- hw/block/xen-block.c | 122 +++---- hw/nvram/fw_cfg.c | 14 +- hw/pci-host/xen_igd_pt.c | 7 +- hw/sd/sdhci-pci.c | 7 +- hw/sd/sdhci.c | 21 +- hw/sd/ssi-sd.c | 16 +- hw/tpm/tpm_util.c | 7 +- hw/xen/xen-backend.c | 7 +- hw/xen/xen-bus.c | 92 ++--- hw/xen/xen-host-pci-device.c | 27 +- hw/xen/xen_pt.c | 25 +- hw/xen/xen_pt_config_init.c | 20 +- nbd/client.c | 5 + nbd/server.c | 5 + tpm.c | 7 +- MAINTAINERS | 1 + 25 files changed, 715 insertions(+), 278 deletions(-) create mode 100644 scripts/coccinelle/auto-propagated-errp.cocci
Signature · 7 lines
2.21.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v10 1/9 error: auto propagated local_err
Introduce a new ERRP_AUTO_PROPAGATE macro, to be used at start of functions with an errp OUT parameter.
It has three goals:
1. Fix issue with error_fatal and error_prepend/error_append_hint: user can't see this additional information, because exit() happens in error_setg earlier than information is added. [Reported by Greg Kurz]
2. Fix issue with error_abort and error_propagate: when we wrap error_abort by local_err+error_propagate, the resulting coredump will refer to error_propagate and not to the place where error happened. (the macro itself doesn't fix the issue, but it allows us to [3.] drop the local_err+error_propagate pattern, which will definitely fix the issue) [Reported by Kevin Wolf]
3. Drop local_err+error_propagate pattern, which is used to workaround void functions with errp parameter, when caller wants to know resulting status. (Note: actually these functions could be merely updated to return int error code).
To achieve these goals, later patches will add invocations of this macro at the start of functions with either use error_prepend/error_append_hint (solving 1) or which use local_err+error_propagate to check errors, switching those functions to use *errp instead (solving 2 and 3).
---
include/qapi/error.h | 205 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 173 insertions(+), 32 deletions(-)
include/qapi/error.h +173 -32
@@ -15,6 +15,8 @@/* * Error reporting system loosely patterned after Glib's GError. * +* = Deal with Error object = +* * Create an error: * error_setg(&err, "situation normal, all fouled up"); *
@@ -47,28 +49,91 @@* reporting it (primarily useful in testsuites): * error_free_or_abort(&err); * -* Pass an existing error to the caller: -* error_propagate(errp, err); -* where Error **errp is a parameter, by convention the last one. +* = Deal with Error ** function parameter = * -* Pass an existing error to the caller with the message modified: -* error_propagate_prepend(errp, err); +* A function may use the error system to return errors. In this case, the +* function defines an Error **errp parameter, by convention the last one (with +* exceptions for functions using ... or va_list). * -* Avoid -* error_propagate(errp, err); -* error_prepend(errp, "Could not frobnicate '%s': ", name); -* because this fails to prepend when @errp is &error_fatal. +* The caller may then pass in the following errp values: * -* Create a new error and pass it to the caller: +* 1. &error_abort +* Any error will result in abort(). +* 2. &error_fatal +* Any error will result in exit() with a non-zero status. +* 3. NULL +* No error reporting through errp parameter. +* 4. The address of a NULL-initialized Error *err +* Any error will populate errp with an error object. +* +* The following rules then implement the correct semantics desired by the +* caller. +* +* Create a new error to pass to the caller: * error_setg(errp, "situation normal, all fouled up"); * -* Call a function and receive an error from it: +* Calling another errp-based function: +* f(..., errp); +* +* == Checking success of subcall == +* +* If a function returns a value indicating an error in addition to setting +* errp (which is recommended), then you don't need any additional code, just +* do: +* +* int ret = f(..., errp); +* if (ret < 0) { +* ... handle error ... +* return ret; +* } +* +* If a function returns nothing (not recommended for new code), the only way +* to check success is by consulting errp; doing this safely requires the use +* of the ERRP_AUTO_PROPAGATE macro, like this: +* +* int our_func(..., Error **errp) { +* ERRP_AUTO_PROPAGATE(); +* ... +* subcall(..., errp); +* if (*errp) { +* ... +* return -EINVAL; +* } +* ... +* } +* +* ERRP_AUTO_PROPAGATE takes care of wrapping the original errp as needed, so +* that the rest of the function can directly use errp (including +* dereferencing), where any errors will then be propagated on to the original +* errp when leaving the function. +* +* In some cases, we need to check result of subcall, but do not want to +* propagate the Error object to our caller. In such cases we don't need +* ERRP_AUTO_PROPAGATE, but just a local Error object: +* +* Receive an error and not pass it: * Error *err = NULL; -* foo(arg, &err); +* subcall(arg, &err); * if (err) { * handle the error... +* error_free(err); * } * +* Note that older code that did not use ERRP_AUTO_PROPAGATE would instead need +* a local Error * variable and the use of error_propagate() to properly handle +* all possible caller values of errp. Now this is DEPRECATED* (see below). +* +* Note that any function that wants to modify an error object, such as by +* calling error_append_hint or error_prepend, must use ERRP_AUTO_PROPAGATE, in +* order for a caller's use of &error_fatal to see the additional information. +* +* In rare cases, we need to pass existing Error object to the caller by hand: +* error_propagate(errp, err); +* +* Pass an existing error to the caller with the message modified: +* error_propagate_prepend(errp, err); +* +* * Call a function ignoring errors: * foo(arg, NULL); *
@@ -78,26 +143,6 @@* Call a function treating errors as fatal: * foo(arg, &error_fatal); * -* Receive an error and pass it on to the caller: -* Error *err = NULL; -* foo(arg, &err); -* if (err) { -* handle the error... -* error_propagate(errp, err); -* } -* where Error **errp is a parameter, by convention the last one. -* -* Do *not* "optimize" this to -* foo(arg, errp); -* if (*errp) { // WRONG! -* handle the error... -* } -* because errp may be NULL! -* -* But when all you do with the error is pass it on, please use -* foo(arg, errp); -* for readability. -* * Receive and accumulate multiple errors (first one wins): * Error *err = NULL, *local_err = NULL; * foo(arg, &err);
@@ -114,6 +159,61 @@* handle the error... * } * because this may pass a non-null err to bar(). +* +* DEPRECATED* +* +* The following pattern of receiving, checking, and then forwarding an error +* to the caller by hand is now deprecated: +* +* Error *err = NULL; +* foo(arg, &err); +* if (err) { +* handle the error... +* error_propagate(errp, err); +* } +* +* Instead, use ERRP_AUTO_PROPAGATE macro. +* +* The old pattern is deprecated because of two things: +* +* 1. Issue with error_abort and error_propagate: when we wrap error_abort by +* local_err+error_propagate, the resulting coredump will refer to +* error_propagate and not to the place where error happened. +* +* 2. A lot of extra code of the same pattern +* +* How to update old code to use ERRP_AUTO_PROPAGATE? +* +* All you need is to add ERRP_AUTO_PROPAGATE() invocation at function start, +* than you may safely dereference errp to check errors and do not need any +* additional local Error variables or calls to error_propagate(). +* +* Example: +* +* old code +* +* void fn(..., Error **errp) { +* Error *err = NULL; +* foo(arg, &err); +* if (err) { +* handle the error... +* error_propagate(errp, err); +* return; +* } +* ... +* } +* +* updated code +* +* void fn(..., Error **errp) { +* ERRP_AUTO_PROPAGATE(); +* foo(arg, errp); +* if (*errp) { +* handle the error... +* return; +* } +* ... +* } */ #ifndef ERROR_H
@@ -322,6 +422,47 @@ void error_set_internal(Error **errp, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(6, 7); +typedef struct ErrorPropagator { +Error *local_err; +Error **errp; +}ErrorPropagator ; + +static inline void error_propagator_cleanup (ErrorPropagator *prop) +{ +error_propagate (prop->errp, prop->local_err); +} + +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (ErrorPropagator, error_propagator_cleanup); + +/* +* ERRP_AUTO_PROPAGATE +* +* This macro exists to assist with proper error handling in a function which +* uses an Error **errp parameter. It must be used as the first line of a +* function which modifies an error (with error_prepend, error_append_hint, or +* similar) or which wants to dereference *errp. It is still safe (but +* useless) to use in other functions. +* +* If errp is NULL or points to error_fatal, it is rewritten to point to a +* local Error object, which will be automatically propagated to the original +* errp on function exit (see error_propagator_cleanup). +* +* After invocation of this macro it is always safe to dereference errp +* (as it's not NULL anymore) and to add information by error_prepend or +* error_append_hint (as, if it was error_fatal, we swapped it with a +* local_error to be propagated on cleanup). +* +* Note: we don't wrap the error_abort case, as we want resulting coredump +* to point to the place where the error happened, not to error_propagate. +*/ +#define ERRP_AUTO_PROPAGATE () \ + g_auto(ErrorPropagator) _auto_errp_prop = {.errp = errp}; \ + do { \ + if (!errp || errp == &error_fatal) { \ + errp = &_auto_errp_prop.local_err; \ + } \ + } while (0) +/* * Special error destination to abort on error. * See error_setg() and error_propagate() for details.
Signature · 7 lines
2.21.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v10 1/9] error: auto propagated local_err
Quoted 9 lines · On Tue, 17 Mar 2020 18:16:17 +0300 Vladimir Sementsov-Ogievskiy…
Introduce a new
ERRP_AUTO_PROPAGATEmacro, to be used at start of functions with an errp OUT parameter.It has three goals:
1. Fix issue with
error_fataland error_prepend/error_append_hint: user can't see this additional information, becauseexit()happens inerror_setgearlier than information is added. [Reported by Greg Kurz]
I have more of these coming and I'd really like to use ERRP_AUTO_PROPAGATE.
It seems we have a consensus on the macro itself but this series is gated by the conversion of the existing code base.
What about merging this patch separately so that people can start using it at least ?
Quoted 300 lines
2. Fix issue with
error_abortanderror_propagate: when we wraperror_abortby local_err+error_propagate, the resulting coredump will refer toerror_propagateand not to the place where error happened. (the macro itself doesn't fix the issue, but it allows us to [3.] drop the local_err+error_propagate pattern, which will definitely fix the issue) [Reported by Kevin Wolf]3. Drop local_err+error_propagate pattern, which is used to workaround void functions with errp parameter, when caller wants to know resulting status. (Note: actually these functions could be merely updated to return int error code).
To achieve these goals, later patches will add invocations of this macro at the start of functions with either use error_prepend/error_append_hint (solving 1) or which use local_err+error_propagate to check errors, switching those functions to use *errp instead (solving 2 and 3).
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Reviewed-by Paul Durrant <[email protected]>Reviewed-by Greg Kurz <[email protected]>Reviewed-by Eric Blake <[email protected]>---
Cc Eric Blake <[email protected]>Cc Kevin Wolf <[email protected]>Cc Max Reitz <[email protected]>Cc Greg Kurz <[email protected]>Cc Christian Schoenebeck <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Anthony Perard <[email protected]>Cc Paul Durrant <[email protected]>Cc "Philippe Mathieu-Daudé" <[email protected]>Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Stefan Berger <[email protected]>Cc Markus Armbruster <[email protected]>Cc Michael Roth <[email protected]>include/qapi/error.h | 205 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 173 insertions(+), 32 deletions(-)include/qapi/error.h +173
-32@@ -15,6 +15,8 @@/* * Error reporting system loosely patterned after Glib's GError. * +* = Deal with Error object = +* * Create an error: * error_setg(&err, "situation normal, all fouled up"); * @@ -47,28 +49,91 @@* reporting it (primarily useful in testsuites): * error_free_or_abort(&err); * -* Pass an existing error to the caller: -* error_propagate(errp, err); -* where Error **errp is a parameter, by convention the last one. +* = Deal with Error ** function parameter = * -* Pass an existing error to the caller with the message modified: -* error_propagate_prepend(errp, err); +* A function may use the error system to return errors. In this case, the +* function defines an Error **errp parameter, by convention the last one (with +* exceptions for functions using ... or va_list). * -* Avoid -* error_propagate(errp, err); -* error_prepend(errp, "Could not frobnicate '%s': ", name); -* because this fails to prepend when @errp is &error_fatal. +* The caller may then pass in the following errp values: * -* Create a new error and pass it to the caller: +* 1. &error_abort +* Any error will result in abort(). +* 2. &error_fatal +* Any error will result in exit() with a non-zero status. +* 3. NULL +* No error reporting through errp parameter. +* 4. The address of a NULL-initialized Error *err +* Any error will populate errp with an error object. +* +* The following rules then implement the correct semantics desired by the +* caller. +* +* Create a new error to pass to the caller: * error_setg(errp, "situation normal, all fouled up"); * -* Call a function and receive an error from it: +* Calling another errp-based function: +* f(..., errp); +* +* == Checking success of subcall == +* +* If a function returns a value indicating an error in addition to setting +* errp (which is recommended), then you don't need any additional code, just +* do: +* +* int ret = f(..., errp); +* if (ret < 0) { +* ... handle error ... +* return ret; +* } +* +* If a function returns nothing (not recommended for new code), the only way +* to check success is by consulting errp; doing this safely requires the use +* of the ERRP_AUTO_PROPAGATE macro, like this: +* +* int our_func(..., Error **errp) { +* ERRP_AUTO_PROPAGATE(); +* ... +* subcall(..., errp); +* if (*errp) { +* ... +* return -EINVAL; +* } +* ... +* } +* +* ERRP_AUTO_PROPAGATE takes care of wrapping the original errp as needed, so +* that the rest of the function can directly use errp (including +* dereferencing), where any errors will then be propagated on to the original +* errp when leaving the function. +* +* In some cases, we need to check result of subcall, but do not want to +* propagate the Error object to our caller. In such cases we don't need +* ERRP_AUTO_PROPAGATE, but just a local Error object: +* +* Receive an error and not pass it: * Error *err = NULL; -* foo(arg, &err); +* subcall(arg, &err); * if (err) { * handle the error... +* error_free(err); * } * +* Note that older code that did not use ERRP_AUTO_PROPAGATE would instead need +* a local Error * variable and the use of error_propagate() to properly handle +* all possible caller values of errp. Now this is DEPRECATED* (see below). +* +* Note that any function that wants to modify an error object, such as by +* calling error_append_hint or error_prepend, must use ERRP_AUTO_PROPAGATE, in +* order for a caller's use of &error_fatal to see the additional information. +* +* In rare cases, we need to pass existing Error object to the caller by hand: +* error_propagate(errp, err); +* +* Pass an existing error to the caller with the message modified: +* error_propagate_prepend(errp, err); +* +* * Call a function ignoring errors: * foo(arg, NULL); * @@ -78,26 +143,6 @@* Call a function treating errors as fatal: * foo(arg, &error_fatal); * -* Receive an error and pass it on to the caller: -* Error *err = NULL; -* foo(arg, &err); -* if (err) { -* handle the error... -* error_propagate(errp, err); -* } -* where Error **errp is a parameter, by convention the last one. -* -* Do *not* "optimize" this to -* foo(arg, errp); -* if (*errp) { // WRONG! -* handle the error... -* } -* because errp may be NULL! -* -* But when all you do with the error is pass it on, please use -* foo(arg, errp); -* for readability. -* * Receive and accumulate multiple errors (first one wins): * Error *err = NULL, *local_err = NULL; * foo(arg, &err); @@ -114,6 +159,61 @@* handle the error... * } * because this may pass a non-null err to bar(). +* +* DEPRECATED* +* +* The following pattern of receiving, checking, and then forwarding an error +* to the caller by hand is now deprecated: +* +* Error *err = NULL; +* foo(arg, &err); +* if (err) { +* handle the error... +* error_propagate(errp, err); +* } +* +* Instead, use ERRP_AUTO_PROPAGATE macro. +* +* The old pattern is deprecated because of two things: +* +* 1. Issue with error_abort and error_propagate: when we wrap error_abort by +* local_err+error_propagate, the resulting coredump will refer to +* error_propagate and not to the place where error happened. +* +* 2. A lot of extra code of the same pattern +* +* How to update old code to use ERRP_AUTO_PROPAGATE? +* +* All you need is to add ERRP_AUTO_PROPAGATE() invocation at function start, +* than you may safely dereference errp to check errors and do not need any +* additional local Error variables or calls to error_propagate(). +* +* Example: +* +* old code +* +* void fn(..., Error **errp) { +* Error *err = NULL; +* foo(arg, &err); +* if (err) { +* handle the error... +* error_propagate(errp, err); +* return; +* } +* ... +* } +* +* updated code +* +* void fn(..., Error **errp) { +* ERRP_AUTO_PROPAGATE(); +* foo(arg, errp); +* if (*errp) { +* handle the error... +* return; +* } +* ... +* } */ #ifndef ERROR_H @@ -322,6 +422,47 @@ void error_set_internal(Error **errp, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(6, 7); +typedef struct ErrorPropagator { +Error *local_err; +Error **errp; +}ErrorPropagator ; + +static inline void error_propagator_cleanup (ErrorPropagator *prop) +{ +error_propagate (prop->errp, prop->local_err); +} + +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (ErrorPropagator, error_propagator_cleanup); + +/* +* ERRP_AUTO_PROPAGATE +* +* This macro exists to assist with proper error handling in a function which +* uses an Error **errp parameter. It must be used as the first line of a +* function which modifies an error (with error_prepend, error_append_hint, or +* similar) or which wants to dereference *errp. It is still safe (but +* useless) to use in other functions. +* +* If errp is NULL or points to error_fatal, it is rewritten to point to a +* local Error object, which will be automatically propagated to the original +* errp on function exit (see error_propagator_cleanup). +* +* After invocation of this macro it is always safe to dereference errp +* (as it's not NULL anymore) and to add information by error_prepend or +* error_append_hint (as, if it was error_fatal, we swapped it with a +* local_error to be propagated on cleanup). +* +* Note: we don't wrap the error_abort case, as we want resulting coredump +* to point to the place where the error happened, not to error_propagate. +*/ +#define ERRP_AUTO_PROPAGATE () \ + g_auto(ErrorPropagator) _auto_errp_prop = {.errp = errp}; \ + do { \ + if (!errp || errp == &error_fatal) { \ + errp = &_auto_errp_prop.local_err; \ + } \ + } while (0) +/* * Special error destination to abort on error. * See error_setg() and error_propagate() for details.
Re: [PATCH v10 1/9] error: auto propagated local_err
Quoted 13 lines · Greg Kurz
Quoted 9 lines · On Tue, 17 Mar 2020 18:16:17 +0300 Vladimir Sementsov-Ogievskiy…
Introduce a new
ERRP_AUTO_PROPAGATEmacro, to be used at start of functions with an errp OUT parameter.It has three goals:
1. Fix issue with
error_fataland error_prepend/error_append_hint: user can't see this additional information, becauseexit()happens inerror_setgearlier than information is added. [Reported by Greg Kurz]
Quoted 7 lines
I have more of these coming and I'd really like to use
ERRP_AUTO_PROPAGATE.It seems we have a consensus on the macro itself but this series is gated by the conversion of the existing code base.
What about merging this patch separately so that people can start using it at least ?
Please give me a few more days to finish the work I feel should go in before the conversion. With any luck, Vladimir can then rebase / recreate the conversion easily, and you can finally use the macro for your own work.
Re: [PATCH v10 1/9] error: auto propagated local_err
Quoted 20 lines · On Mon, 15 Jun 2020 07:21:03 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 18 lines · Greg Kurz
Quoted 9 lines · On Tue, 17 Mar 2020 18:16:17 +0300 Vladimir Sementsov-Ogievskiy…
Introduce a new
ERRP_AUTO_PROPAGATEmacro, to be used at start of functions with an errp OUT parameter.It has three goals:
1. Fix issue with
error_fataland error_prepend/error_append_hint: user can't see this additional information, becauseexit()happens inerror_setgearlier than information is added. [Reported by Greg Kurz]I have more of these coming and I'd really like to use
ERRP_AUTO_PROPAGATE.It seems we have a consensus on the macro itself but this series is gated by the conversion of the existing code base.
Quoted 8 lines
Quoted 2 lines
What about merging this patch separately so that people can start using it at least ?
Please give me a few more days to finish the work I feel should go in before the conversion. With any luck, Vladimir can then rebase / recreate the conversion easily, and you can finally use the macro for your own work.
Sure. Thanks.
Re: [PATCH v10 1/9] error: auto propagated local_err
Quoted 26 lines · Greg Kurz
Quoted 23 lines · On Mon, 15 Jun 2020 07:21:03 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 20 lines · Greg Kurz
Quoted 9 lines · On Tue, 17 Mar 2020 18:16:17 +0300 Vladimir Sementsov-Ogievskiy…
Introduce a new
ERRP_AUTO_PROPAGATEmacro, to be used at start of functions with an errp OUT parameter.It has three goals:
1. Fix issue with
error_fataland error_prepend/error_append_hint: user can't see this additional information, becauseexit()happens inerror_setgearlier than information is added. [Reported by Greg Kurz]I have more of these coming and I'd really like to use
ERRP_AUTO_PROPAGATE.It seems we have a consensus on the macro itself but this series is gated by the conversion of the existing code base.
What about merging this patch separately so that people can start using it at least ?
Quoted 7 lines
Quoted 5 lines
Please give me a few more days to finish the work I feel should go in before the conversion. With any luck, Vladimir can then rebase / recreate the conversion easily, and you can finally use the macro for your own work.
Sure. Thanks.
Just posted "[PATCH 00/46] Less clumsy error checking". The sheer size of the thing and the length of its dependency chain explains why it took me so long. I feel bad about delaying you all the same. Apologies!
I hope we can converge quickly enough to get Vladimir's work on top ready in time for the soft freeze.
Re: [PATCH v10 1/9] error: auto propagated local_err
Quoted 34 lines · On Wed, 24 Jun 2020 18:53:05 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 32 lines · Greg Kurz
Quoted 28 lines · On Mon, 15 Jun 2020 07:21:03 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 13 lines · Greg Kurz
Quoted 9 lines · On Tue, 17 Mar 2020 18:16:17 +0300 Vladimir Sementsov-Ogievskiy…
Introduce a new
ERRP_AUTO_PROPAGATEmacro, to be used at start of functions with an errp OUT parameter.It has three goals:
1. Fix issue with
error_fataland error_prepend/error_append_hint: user can't see this additional information, becauseexit()happens inerror_setgearlier than information is added. [Reported by Greg Kurz]Quoted 7 lines
I have more of these coming and I'd really like to use
ERRP_AUTO_PROPAGATE.It seems we have a consensus on the macro itself but this series is gated by the conversion of the existing code base.
What about merging this patch separately so that people can start using it at least ?
Please give me a few more days to finish the work I feel should go in before the conversion. With any luck, Vladimir can then rebase / recreate the conversion easily, and you can finally use the macro for your own work.
Quoted 6 lines
Quoted 1 line
Sure. Thanks.
Just posted "[PATCH 00/46] Less clumsy error checking". The sheer size of the thing and the length of its dependency chain explains why it took me so long. I feel bad about delaying you all the same. Apologies!
No problem. This series of yours is impressive. Putting an end to the highjacking of the Error ** argument is really a beneficial move.
Quoted 3 lines
I hope we can converge quickly enough to get Vladimir's work on top ready in time for the soft freeze.
I'll find some cycles for reviewing.
Cheers,
--
Greg
PATCH v10 2/9 scripts: Coccinelle script to use ERRP_AUTO_PROPAGATE()
In reply to Vladimir Sementsov-OgievskiyScript adds ERRP_AUTO_PROPAGATE macro invocation where appropriate and does corresponding changes in code (look for details in include/qapi/error.h)
Usage example:
spatch --sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h --in-place --no-show-diff \
--max-width 80 FILES...
---
scripts/coccinelle/auto-propagated-errp.cocci | 336 ++++++++++++++++++ include/qapi/error.h | 3 + MAINTAINERS | 1 + 3 files changed, 340 insertions(+) create mode 100644 scripts/coccinelle/auto-propagated-errp.cocci
scripts/coccinelle/auto-propagated-errp.cocci new file +336 -0
@@ -0,0 +1,336 @@ +// Use ERRP_AUTO_PROPAGATE (see include/qapi/error.h) +// +// Copyright (c) 2020 Virtuozzo International GmbH. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// <http://www.gnu.org/licenses/>. +// +// Usage example: +// spatch --sp-file scripts/coccinelle/auto-propagated-errp.cocci \ +// --macro-file scripts/cocci-macro-file.h --in-place \ +// --no-show-diff --max-width 80 FILES... +// +// Note: --max-width 80 is needed because coccinelle default is less +// than 80, and without this parameter coccinelle may reindent some +// lines which fit into 80 characters but not to coccinelle default, +// which in turn produces extra patch hunks for no reason. + +// Switch unusual Error ** parameter names to errp +// (this is necessary to use ERRP_AUTO_PROPAGATE). +// +// Disable optional_qualifier to skip functions with +// "Error *const *errp" parameter. +// +// Skip functions with "assert(_errp && *_errp)" statement, because +// that signals unusual semantics, and the parameter name may well +// serve a purpose. (like nbd_iter_channel_error()). +// +// Skip util/error.c to not touch, for example, error_propagate() and +// error_propagate_prepend(). +@ depends on !(file in "util/error.c") disable optional_qualifier@ +identifier fn; +identifier _errp != errp; +@@ + + fn(..., +- Error **_errp ++ Error **errp + ,...) + { +( + ... when != assert(_errp && *_errp) +& + <... +- _errp ++ errp + ...> +) + } + +// Add invocation of ERRP_AUTO_PROPAGATE to errp-functions where +// necessary +// +// Note, that without "when any" the final "..." does not mach +// something matched by previous pattern, i.e. the rule will not match +// double error_prepend in control flow like in +// vfio_set_irq_signaling(). +// +// Note, "exists" says that we want apply rule even if it does not +// match on all possible control flows (otherwise, it will not match +// standard pattern when error_propagate() call is in if branch). +@ disable optional_qualifier exists@ +identifier fn, local_err; +symbol errp; +@@ + + fn(..., Error **errp, ...) + { ++ ERRP_AUTO_PROPAGATE(); + ... when != ERRP_AUTO_PROPAGATE(); +( +( + error_append_hint(errp, ...); +| + error_prepend(errp, ...); +| + error_vprepend(errp, ...); +) + ... when any +| + Error *local_err = NULL; + ... +( + error_propagate_prepend(errp, local_err, ...); +| + error_propagate(errp, local_err); +) + ... +) + } + +// Warn when several Error * definitions are in the control flow. +// This rule is not chained to rule1 and less restrictive, to cover more +// functions to warn (even those we are not going to convert). +// +// Note, that even with one (or zero) Error * definition in the each +// control flow we may have several (in total) Error * definitions in +// the function. This case deserves attention too, but I don't see +// simple way to match with help of coccinelle. +@check1 disable optional_qualifier exists@ +identifier fn, _errp, local_err, local_err2; +position p1, p2; +@@ + + fn(..., Error **_errp, ...) + { + ... + Error *local_err = NULL;@p1 + ... when any + Error *local_err2 = NULL;@p2 + ... when any + } + +@ script:python @ +fn << check1.fn; +p1 << check1.p1; +p2 << check1.p2; +@@ + +print('Warning: function {} has several definitions of ' + 'Error * local variable: at {}:{} and then at {}:{}'.format( + fn, p1[0].file, p1[0].line, p2[0].file, p2[0].line)) + +// Warn when several propagations are in the control flow. +@check2 disable optional_qualifier exists@ +identifier fn, _errp; +position p1, p2; +@@ + + fn(..., Error **_errp, ...) + { + ... +( + error_propagate_prepend(_errp, ...);@p1 +| + error_propagate(_errp, ...);@p1 +) + ... +( + error_propagate_prepend(_errp, ...);@p2 +| + error_propagate(_errp, ...);@p2 +) + ... when any + } + +@ script:python @ +fn << check2.fn; +p1 << check2.p1; +p2 << check2.p2; +@@ + +print('Warning: function {} propagates to errp several times in ' + 'one control flow: at {}:{} and then at {}:{}'.format( + fn, p1[0].file, p1[0].line, p2[0].file, p2[0].line)) + +// Match functions with propagation of local error to errp. +// We want to refer these functions in several following rules, but I +// don't know a proper way to inherit a function, not just its name +// (to not match another functions with same name in following rules). +// Not-proper way is as follows: rename errp parameter in functions +// header and match it in following rules. Rename it back after all +// transformations. +// +// The common case is a single definition of local_err with at most one +// error_propagate_prepend() or error_propagate() on each control-flow +// path. Functions with multiple definitions or propagates we want to +// examine manually. Later rules emit warnings to guide us to them. +// +// Note that we match not only this "common case", but any function, +// which has the "common cose" on at least one control-flow path. +@rule1 disable optional_qualifier exists@ +identifier fn, local_err; +symbol errp; +@@ + + fn(..., Error ** +- errp ++ ____ + , ...) + { + ... + Error *local_err = NULL; + ... +( + error_propagate_prepend(errp, local_err, ...); +| + error_propagate(errp, local_err); +) + ... + } + +// Convert special case with goto separately. +// I tried merging this into the following rule the obvious way, but +// it made Coccinelle hang on block.c +// +// Note interesting thing: if we don't do it here, and try to fixup +// "out: }" things later after all transformations (the rule will be +// the same, just without error_propagate() call), coccinelle fails to +// match this "out: }". +@ disable optional_qualifier@ +identifier rule1.fn, rule1.local_err, out; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- goto out; ++ return; + ...> +- out: +- error_propagate(errp, local_err); + } + +// Convert most of local_err related stuff. +// +// Note, that we inherit rule1.fn and rule1.local_err names, not +// objects themselves. We may match something not related to the +// pattern matched by rule1. For example, local_err may be defined with +// the same name in different blocks inside one function, and in one +// block follow the propagation pattern and in other block doesn't. +// +// Note also that errp-cleaning functions +// error_free_errp +// error_report_errp +// error_reportf_errp +// warn_report_errp +// warn_reportf_errp +// are not yet implemented. They must call corresponding Error* - +// freeing function and then set *errp to NULL, to avoid further +// propagation to original errp (consider ERRP_AUTO_PROPAGATE in use). +// For example, error_free_errp may look like this: +// +// void error_free_errp(Error **errp) +// { +// error_free(*errp); +// *errp = NULL; +// } +@ disable optional_qualifier exists@ +identifier rule1.fn, rule1.local_err; +expression list args; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +( +- Error *local_err = NULL; +| + +// Convert error clearing functions +( +- error_free(local_err); ++ error_free_errp(errp); +| +- error_report_err(local_err); ++ error_report_errp(errp); +| +- error_reportf_err(local_err, args); ++ error_reportf_errp(errp, args); +| +- warn_report_err(local_err); ++ warn_report_errp(errp); +| +- warn_reportf_err(local_err, args); ++ warn_reportf_errp(errp, args); +) +?- local_err = NULL; + +| +- error_propagate_prepend(errp, local_err, args); ++ error_prepend(errp, args); +| +- error_propagate(errp, local_err); +| +- &local_err ++ errp +) + ...> + } + +// Convert remaining local_err usage. For example, different kinds of +// error checking in if conditionals. We can't merge this into +// previous hunk, as this conflicts with other substitutions in it (at +// least with "- local_err = NULL"). +@ disable optional_qualifier@ +identifier rule1.fn, rule1.local_err; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- local_err ++ *errp + ...> + } + +// Always use the same pattern for checking error +@ disable optional_qualifier@ +identifier rule1.fn; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- *errp != NULL ++ *errp + ...> + } + +// Revert temporary ___ identifier. +@ disable optional_qualifier@ +identifier rule1.fn; +@@ + + fn(..., Error ** +- ____ ++ errp + , ...) + { + ... + }
include/qapi/error.h +3 -0
@@ -214,6 +214,9 @@* } * ... * } +* +* For mass-conversion use script +* scripts/coccinelle/auto-propagated-errp.cocci */ #ifndef ERROR_H
MAINTAINERS +1 -0
@@ -2016,6 +2016,7 @@ F: include/qemu/error-report.h F: qapi/error.json F: util/error.c F: util/qemu-error.c +F: scripts/coccinelle/*err*.cocci GDB stub M: Alex Bennée <[email protected]>
Signature · 7 lines
2.21.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v10 2/9] scripts: Coccinelle script to use ERRP_AUTO_PROPAGATE()
Quoted 211 lines · Vladimir Sementsov-Ogievskiy
Script adds
ERRP_AUTO_PROPAGATEmacro invocation where appropriate and does corresponding changes in code (look for details ininclude/qapi/error.h)Usage example:
spatch --sp-filescripts/coccinelle/auto-propagated-errp.cocci\
--macro-filescripts/cocci-macro-file.h--in-place --no-show-diff \
--max-width 80 FILES...Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
Cc Eric Blake <[email protected]>Cc Kevin Wolf <[email protected]>Cc Max Reitz <[email protected]>Cc Greg Kurz <[email protected]>Cc Christian Schoenebeck <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Anthony Perard <[email protected]>Cc Paul Durrant <[email protected]>Cc "Philippe Mathieu-Daudé" <[email protected]>Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Stefan Berger <[email protected]>Cc Markus Armbruster <[email protected]>Cc Michael Roth <[email protected]>scripts/coccinelle/auto-propagated-errp.cocci | 336 ++++++++++++++++++ include/qapi/error.h | 3 + MAINTAINERS | 1 + 3 files changed, 340 insertions(+) create mode 100644 scripts/coccinelle/auto-propagated-errp.cocciscripts/coccinelle/auto-propagated-errp.cocci new file +168
-0@@ -0,0 +1,336 @@ +// Use ERRP_AUTO_PROPAGATE (see include/qapi/error.h) +// +// Copyright (c) 2020 Virtuozzo International GmbH. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// <http://www.gnu.org/licenses/>. +// +// Usage example: +// spatch --sp-file scripts/coccinelle/auto-propagated-errp.cocci \ +// --macro-file scripts/cocci-macro-file.h --in-place \ +// --no-show-diff --max-width 80 FILES... +// +// Note: --max-width 80 is needed because coccinelle default is less +// than 80, and without this parameter coccinelle may reindent some +// lines which fit into 80 characters but not to coccinelle default, +// which in turn produces extra patch hunks for no reason. + +// Switch unusual Error ** parameter names to errp +// (this is necessary to use ERRP_AUTO_PROPAGATE). +// +// Disable optional_qualifier to skip functions with +// "Error *const *errp" parameter. +// +// Skip functions with "assert(_errp && *_errp)" statement, because +// that signals unusual semantics, and the parameter name may well +// serve a purpose. (like nbd_iter_channel_error()). +// +// Skip util/error.c to not touch, for example, error_propagate() and +// error_propagate_prepend(). +@ depends on !(file in "util/error.c") disable optional_qualifier@ +identifier fn; +identifier _errp != errp; +@@ + + fn(..., +- Error **_errp ++ Error **errp + ,...) + { +( + ... when != assert(_errp && *_errp) +& + <... +- _errp ++ errp + ...> +) + } + +// Add invocation of ERRP_AUTO_PROPAGATE to errp-functions where +// necessary +// +// Note, that without "when any" the final "..." does not mach +// something matched by previous pattern, i.e. the rule will not match +// double error_prepend in control flow like in +// vfio_set_irq_signaling(). +// +// Note, "exists" says that we want apply rule even if it does not +// match on all possible control flows (otherwise, it will not match +// standard pattern when error_propagate() call is in if branch). +@ disable optional_qualifier exists@ +identifier fn, local_err; +symbol errp; +@@ + + fn(..., Error **errp, ...) + { ++ ERRP_AUTO_PROPAGATE(); + ... when != ERRP_AUTO_PROPAGATE(); +( +( + error_append_hint(errp, ...); +| + error_prepend(errp, ...); +| + error_vprepend(errp, ...); +) + ... when any +| + Error *local_err = NULL; + ... +( + error_propagate_prepend(errp, local_err, ...); +| + error_propagate(errp, local_err); +) + ... +) + } + +// Warn when several Error * definitions are in the control flow. +// This rule is not chained to rule1 and less restrictive, to cover more +// functions to warn (even those we are not going to convert). +// +// Note, that even with one (or zero) Error * definition in the each +// control flow we may have several (in total) Error * definitions in +// the function. This case deserves attention too, but I don't see +// simple way to match with help of coccinelle. +@check1 disable optional_qualifier exists@ +identifier fn, _errp, local_err, local_err2; +position p1, p2; +@@ + + fn(..., Error **_errp, ...) + { + ... + Error *local_err = NULL;@p1 + ... when any + Error *local_err2 = NULL;@p2 + ... when any + } + +@ script:python @ +fn << check1.fn; +p1 << check1.p1; +p2 << check1.p2; +@@ + +print('Warning: function {} has several definitions of ' + 'Error * local variable: at {}:{} and then at {}:{}'.format( + fn, p1[0].file, p1[0].line, p2[0].file, p2[0].line)) + +// Warn when several propagations are in the control flow. +@check2 disable optional_qualifier exists@ +identifier fn, _errp; +position p1, p2; +@@ + + fn(..., Error **_errp, ...) + { + ... +( + error_propagate_prepend(_errp, ...);@p1 +| + error_propagate(_errp, ...);@p1 +) + ... +( + error_propagate_prepend(_errp, ...);@p2 +| + error_propagate(_errp, ...);@p2 +) + ... when any + } + +@ script:python @ +fn << check2.fn; +p1 << check2.p1; +p2 << check2.p2; +@@ + +print('Warning: function {} propagates to errp several times in ' + 'one control flow: at {}:{} and then at {}:{}'.format( + fn, p1[0].file, p1[0].line, p2[0].file, p2[0].line)) + +// Match functions with propagation of local error to errp. +// We want to refer these functions in several following rules, but I
scripts/coccinelle/auto-propagated-errp.cocci line 178
Quoted 10 lines
scripts/coccinelle/auto-propagated-errp.cocci new file +10
-0+// don't know a proper way to inherit a function, not just its name +// (to not match another functions with same name in following rules). +// Not-proper way is as follows: rename errp parameter in functions +// header and match it in following rules. Rename it back after all +// transformations. +// +// The common case is a single definition of local_err with at most one +// error_propagate_prepend() or error_propagate() on each control-flow +// path. Functions with multiple definitions or propagates we want to +// examine manually. Later rules emit warnings to guide us to them.
"Later rules" is no longer correct. Suggest "Rules check1 and check2".
scripts/coccinelle/auto-propagated-errp.cocci line 181
Quoted 3 lines
scripts/coccinelle/auto-propagated-errp.cocci new file +3
-0+// +// Note that we match not only this "common case", but any function, +// which has the "common cose" on at least one control-flow path.
I appreciate this note.
Typo: s/cose/case/
Quoted 174 lines
scripts/coccinelle/auto-propagated-errp.cocci new file +155
-0+@rule1 disable optional_qualifier exists@ +identifier fn, local_err; +symbol errp; +@@ + + fn(..., Error ** +- errp ++ ____ + , ...) + { + ... + Error *local_err = NULL; + ... +( + error_propagate_prepend(errp, local_err, ...); +| + error_propagate(errp, local_err); +) + ... + } + +// Convert special case with goto separately. +// I tried merging this into the following rule the obvious way, but +// it made Coccinelle hang on block.c +// +// Note interesting thing: if we don't do it here, and try to fixup +// "out: }" things later after all transformations (the rule will be +// the same, just without error_propagate() call), coccinelle fails to +// match this "out: }". +@ disable optional_qualifier@ +identifier rule1.fn, rule1.local_err, out; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- goto out; ++ return; + ...> +- out: +- error_propagate(errp, local_err); + } + +// Convert most of local_err related stuff. +// +// Note, that we inherit rule1.fn and rule1.local_err names, not +// objects themselves. We may match something not related to the +// pattern matched by rule1. For example, local_err may be defined with +// the same name in different blocks inside one function, and in one +// block follow the propagation pattern and in other block doesn't. +// +// Note also that errp-cleaning functions +// error_free_errp +// error_report_errp +// error_reportf_errp +// warn_report_errp +// warn_reportf_errp +// are not yet implemented. They must call corresponding Error* - +// freeing function and then set *errp to NULL, to avoid further +// propagation to original errp (consider ERRP_AUTO_PROPAGATE in use). +// For example, error_free_errp may look like this: +// +// void error_free_errp(Error **errp) +// { +// error_free(*errp); +// *errp = NULL; +// } +@ disable optional_qualifier exists@ +identifier rule1.fn, rule1.local_err; +expression list args; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +( +- Error *local_err = NULL; +| + +// Convert error clearing functions +( +- error_free(local_err); ++ error_free_errp(errp); +| +- error_report_err(local_err); ++ error_report_errp(errp); +| +- error_reportf_err(local_err, args); ++ error_reportf_errp(errp, args); +| +- warn_report_err(local_err); ++ warn_report_errp(errp); +| +- warn_reportf_err(local_err, args); ++ warn_reportf_errp(errp, args); +) +?- local_err = NULL; + +| +- error_propagate_prepend(errp, local_err, args); ++ error_prepend(errp, args); +| +- error_propagate(errp, local_err); +| +- &local_err ++ errp +) + ...> + } + +// Convert remaining local_err usage. For example, different kinds of +// error checking in if conditionals. We can't merge this into +// previous hunk, as this conflicts with other substitutions in it (at +// least with "- local_err = NULL"). +@ disable optional_qualifier@ +identifier rule1.fn, rule1.local_err; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- local_err ++ *errp + ...> + } + +// Always use the same pattern for checking error +@ disable optional_qualifier@ +identifier rule1.fn; +symbol errp; +@@ + + fn(..., Error ** ____, ...) + { + <... +- *errp != NULL ++ *errp + ...> + } + +// Revert temporary ___ identifier. +@ disable optional_qualifier@ +identifier rule1.fn; +@@ + + fn(..., Error ** +- ____ ++ errp + , ...) + { + ... + }include/qapi/error.h +3
-0@@ -214,6 +214,9 @@* } * ... * } +* +* For mass-conversion use script +* scripts/coccinelle/auto-propagated-errp.cocci */ #ifndef ERROR_H MAINTAINERS +0
-0
MAINTAINERS F: include/qemu/error-report.h · line 2022
Quoted 7 lines
MAINTAINERS +1
-0@@ -2016,6 +2016,7 @@ F: include/qemu/error-report.h F: qapi/error.json F: util/error.c F: util/qemu-error.c +F: scripts/coccinelle/*err*.cocci GDB stub M: Alex Bennée <[email protected]>
With the minor comment issues addressed:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v10 3/9 SD (Secure Card): introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^SD (Secure Card)$/,/^$/{s/^F: //p}' \
MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/sd/sdhci-pci.c | 7 +++---- hw/sd/sdhci.c | 21 +++++++++------------ hw/sd/ssi-sd.c | 16 ++++++++-------- 3 files changed, 20 insertions(+), 24 deletions(-)
hw/sd/sdhci-pci.c +3 -4
@@ -29,13 +29,12 @@ static Property sdhci_pci_properties[] = {static void sdhci_pci_realize (PCIDevice *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();SDHCIState *s =PCI_SDHCI (dev); -Error *local_err =NULL ;sdhci_initfn (s); -sdhci_common_realize (s, &local_err); -if (local_err) { -error_propagate (errp, local_err); +sdhci_common_realize (s, errp); +if (*errp) {return ; }
hw/sd/sdhci.c +9 -12
@@ -1288,7 +1288,7 @@ static const MemoryRegionOps sdhci_mmio_ops = {static void sdhci_init_readonly_registers (SDHCIState *s,Error **errp) { -Error *local_err =NULL ; +ERRP_AUTO_PROPAGATE ();switch (s->sd_spec_version) { case 2 ... 3:
@@ -1299,9 +1299,8 @@ static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp) } s->version = (SDHC_HCVER_VENDOR <<8 ) | (s->sd_spec_version -1 ); -sdhci_check_capareg (s, &local_err); -if (local_err) { -error_propagate (errp, local_err); +sdhci_check_capareg (s, errp); +if (*errp) {return ; } }
@@ -1332,11 +1331,10 @@ void sdhci_uninitfn(SDHCIState *s)void sdhci_common_realize (SDHCIState *s,Error **errp) { -Error *local_err =NULL ; +ERRP_AUTO_PROPAGATE (); -sdhci_init_readonly_registers (s, &local_err); -if (local_err) { -error_propagate (errp, local_err); +sdhci_init_readonly_registers (s, errp); +if (*errp) {return ; } s->buf_maxsz =sdhci_get_fifolen (s);
@@ -1456,13 +1454,12 @@ static void sdhci_sysbus_finalize(Object *obj)static void sdhci_sysbus_realize (DeviceState *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();SDHCIState *s =SYSBUS_SDHCI (dev);SysBusDevice *sbd =SYS_BUS_DEVICE (dev); -Error *local_err =NULL ; -sdhci_common_realize (s, &local_err); -if (local_err) { -error_propagate (errp, local_err); +sdhci_common_realize (s, errp); +if (*errp) {return ; }
hw/sd/ssi-sd.c +8 -8
@@ -241,10 +241,10 @@ static const VMStateDescription vmstate_ssi_sd = {static void ssi_sd_realize (SSISlave *d,Error **errp) { +ERRP_AUTO_PROPAGATE ();ssi_sd_state *s =FROM_SSI_SLAVE (ssi_sd_state, d);DeviceState *carddev;DriveInfo *dinfo; -Error *err =NULL ;qbus_create_inplace (&s->sdbus,sizeof (s->sdbus),TYPE_SD_BUS ,DEVICE (d),"sd-bus" );
@@ -254,26 +254,26 @@ static void ssi_sd_realize(SSISlave *d, Error **errp) dinfo =drive_get_next (IF_SD ); carddev =qdev_create (BUS (&s->sdbus),TYPE_SD_CARD );if (dinfo) { -qdev_prop_set_drive (carddev,"drive" ,blk_by_legacy_dinfo (dinfo), &err); -if (err) { +qdev_prop_set_drive (carddev,"drive" ,blk_by_legacy_dinfo (dinfo), errp); +if (*errp) { goto fail; } } -object_property_set_bool (OBJECT (carddev), true,"spi" , &err); -if (err) { +object_property_set_bool (OBJECT (carddev), true,"spi" , errp); +if (*errp) { goto fail; } -object_property_set_bool (OBJECT (carddev), true,"realized" , &err); -if (err) { +object_property_set_bool (OBJECT (carddev), true,"realized" , errp); +if (*errp) { goto fail; }return ; fail: -error_propagate_prepend (errp, err,"failed to init SD card: " ); +error_prepend (errp,"failed to init SD card: " ); }static void ssi_sd_reset (DeviceState *dev)
Signature · 1 line
2.21.0
PATCH v10 4/9 pflash: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^Parallel NOR Flash devices$/,/^$/{s/^F: //p}' \
MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/block/pflash_cfi01.c | 7 +++---- hw/block/pflash_cfi02.c | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-)
hw/block/pflash_cfi01.c +3 -4
@@ -700,12 +700,12 @@ static const MemoryRegionOps pflash_cfi01_ops = {static void pflash_cfi01_realize (DeviceState *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();PFlashCFI01 *pfl =PFLASH_CFI01 (dev);uint64_t total_len;int ret;uint64_t blocks_per_device, sector_len_per_device, device_len;int num_devices; -Error *local_err =NULL ;if (pfl->sector_len ==0 ) {error_setg (errp,"attribute \"sector-length\" not specified or zero." );
@@ -739,9 +739,8 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) &pfl->mem,OBJECT (dev), &pflash_cfi01_ops, pfl, - pfl->name, total_len, &local_err); -if (local_err) { -error_propagate (errp, local_err); + pfl->name, total_len, errp); +if (*errp) {return ; }
hw/block/pflash_cfi02.c +3 -4
@@ -718,9 +718,9 @@ static const MemoryRegionOps pflash_cfi02_ops = {static void pflash_cfi02_realize (DeviceState *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();PFlashCFI02 *pfl =PFLASH_CFI02 (dev);int ret; -Error *local_err =NULL ;if (pfl->uniform_sector_len ==0 && pfl->sector_len[0 ] ==0 ) {error_setg (errp,"attribute \"sector-length\" not specified or zero." );
@@ -786,9 +786,8 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)memory_region_init_rom_device (&pfl->orig_mem,OBJECT (pfl), &pflash_cfi02_ops, pfl, pfl->name, - pfl->chip_len, &local_err); -if (local_err) { -error_propagate (errp, local_err); + pfl->chip_len, errp); +if (*errp) {return ; }
Signature · 1 line
2.21.0
PATCH v10 5/9 fw_cfg: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^Firmware configuration (fw_cfg)$/,/^$/{s/^F: //p}' \
MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/nvram/fw_cfg.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
hw/nvram/fw_cfg.c +6 -8
@@ -1142,12 +1142,11 @@ static Property fw_cfg_io_properties[] = {static void fw_cfg_io_realize (DeviceState *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();FWCfgIoState *s =FW_CFG_IO (dev); -Error *local_err =NULL ; -fw_cfg_file_slots_allocate (FW_CFG (s), &local_err); -if (local_err) { -error_propagate (errp, local_err); +fw_cfg_file_slots_allocate (FW_CFG (s), errp); +if (*errp) {return ; }
@@ -1193,14 +1192,13 @@ static Property fw_cfg_mem_properties[] = {static void fw_cfg_mem_realize (DeviceState *dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();FWCfgMemState *s =FW_CFG_MEM (dev);SysBusDevice *sbd =SYS_BUS_DEVICE (dev);const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops; -Error *local_err =NULL ; -fw_cfg_file_slots_allocate (FW_CFG (s), &local_err); -if (local_err) { -error_propagate (errp, local_err); +fw_cfg_file_slots_allocate (FW_CFG (s), errp); +if (*errp) {return ; }
Signature · 1 line
2.21.0
PATCH v10 6/9 virtio-9p: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^virtio-9p$/,/^$/{s/^F: //p}' MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/9pfs/9p-local.c | 12 +++++------- hw/9pfs/9p.c | 1 + 2 files changed, 6 insertions(+), 7 deletions(-)
hw/9pfs/9p-local.c +5 -7
@@ -1479,10 +1479,10 @@ static void error_append_security_model_hint(Error *const *errp)static int local_parse_opts (QemuOpts *opts,FsDriverEntry *fse,Error **errp) { +ERRP_AUTO_PROPAGATE ();const char *sec_model =qemu_opt_get (opts,"security_model" );const char *path =qemu_opt_get (opts,"path" );const char *multidevs =qemu_opt_get (opts,"multidevs" ); -Error *local_err =NULL ;if (!sec_model) {error_setg (errp,"security_model property not set" );
@@ -1516,11 +1516,10 @@ static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp) fse->export_flags &= ~V9FS_FORBID_MULTIDEVS ; fse->export_flags &= ~V9FS_REMAP_INODES ; }else { -error_setg (&local_err,"invalid multidevs property '%s'" , +error_setg (errp,"invalid multidevs property '%s'" , multidevs); -error_append_hint (&local_err,"Valid options are: multidevs=" +error_append_hint (errp,"Valid options are: multidevs=" "[remap|forbid|warn]\n" ); -error_propagate (errp, local_err);return -1 ; } }
@@ -1530,9 +1529,8 @@ static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)return -1 ; } -if (fsdev_throttle_parse_opts (opts, &fse->fst, &local_err)) { -error_propagate_prepend (errp, local_err, -"invalid throttle configuration: " ); +if (fsdev_throttle_parse_opts (opts, &fse->fst, errp)) { +error_prepend (errp,"invalid throttle configuration: " );return -1 ; }
hw/9pfs/9p.c +1 -0
@@ -4023,6 +4023,7 @@ void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)int v9fs_device_realize_common (V9fsState *s,const V9fsTransport *t,Error **errp) { +ERRP_AUTO_PROPAGATE ();int i, len;struct stat stat;FsDriverEntry *fse;
Signature · 1 line
2.21.0
PATCH v10 7/9 TPM: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^TPM$/,/^$/{s/^F: //p}' MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/tpm/tpm_util.c | 7 +++---- tpm.c | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-)
hw/tpm/tpm_util.c +3 -4
@@ -47,8 +47,8 @@ static void get_tpm(Object *obj, Visitor *v, const char *name, void *opaque,static void set_tpm (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) { +ERRP_AUTO_PROPAGATE ();DeviceState *dev =DEVICE (obj); -Error *local_err =NULL ;Property *prop = opaque;TPMBackend *s, **be =qdev_get_prop_ptr (dev, prop);char *str;
@@ -58,9 +58,8 @@ static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,return ; } -visit_type_str (v, name, &str, &local_err); -if (local_err) { -error_propagate (errp, local_err); +visit_type_str (v, name, &str, errp); +if (*errp) {return ; }
tpm.c +3 -4
@@ -81,11 +81,11 @@ TPMBackend *qemu_find_tpm_be(const char *id)static int tpm_init_tpmdev (void *dummy,QemuOpts *opts,Error **errp) { +ERRP_AUTO_PROPAGATE ();const char *value;const char *id;const TPMBackendClass *be;TPMBackend *drv; -Error *local_err =NULL ;int i;if (!QLIST_EMPTY (&tpm_backends)) {
@@ -116,9 +116,8 @@ static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) }/* validate backend specific opts */ -qemu_opts_validate (opts, be->opts, &local_err); -if (local_err) { -error_propagate (errp, local_err); +qemu_opts_validate (opts, be->opts, errp); +if (*errp) {return 1 ; }
Signature · 1 line
2.21.0
PATCH v10 8/9 nbd: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^Network Block Device (NBD)$/,/^$/{s/^F: //p}' \
MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- include/block/nbd.h | 1 + block/nbd.c | 21 +++++++++------------ nbd/client.c | 5 +++++ nbd/server.c | 5 +++++ 4 files changed, 20 insertions(+), 12 deletions(-)
include/block/nbd.h +1 -0
@@ -361,6 +361,7 @@ void nbd_server_start_options(NbdServerOptions *arg, Error **errp);static inline int nbd_read (QIOChannel *ioc,void *buffer,size_t size,const char *desc,Error **errp) { +ERRP_AUTO_PROPAGATE ();int ret =qio_channel_read_all (ioc, buffer, size, errp) <0 ? -EIO :0 ;if (ret <0 ) {
block/nbd.c +9 -12
@@ -1410,16 +1410,15 @@ static void nbd_client_close(BlockDriverState *bs)static QIOChannelSocket *nbd_establish_connection (SocketAddress *saddr,Error **errp) { +ERRP_AUTO_PROPAGATE ();QIOChannelSocket *sioc; -Error *local_err =NULL ; sioc =qio_channel_socket_new ();qio_channel_set_name (QIO_CHANNEL (sioc),"nbd-client" ); -qio_channel_socket_connect_sync (sioc, saddr, &local_err); -if (local_err) { +qio_channel_socket_connect_sync (sioc, saddr, errp); +if (*errp) {object_unref (OBJECT (sioc)); -error_propagate (errp, local_err);return NULL ; }
@@ -1725,10 +1724,10 @@ static bool nbd_process_legacy_socket_options(QDict *output_options,static SocketAddress *nbd_config (BDRVNBDState *s,QDict *options,Error **errp) { +ERRP_AUTO_PROPAGATE ();SocketAddress *saddr =NULL ;QDict *addr =NULL ;Visitor *iv =NULL ; -Error *local_err =NULL ;qdict_extract_subqdict (options, &addr,"server." );if (!qdict_size (addr)) {
@@ -1741,9 +1740,8 @@ static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, goto done; } -visit_type_SocketAddress (iv,NULL , &saddr, &local_err); -if (local_err) { -error_propagate (errp, local_err); +visit_type_SocketAddress (iv,NULL , &saddr, errp); +if (*errp) { goto done; }
@@ -1836,15 +1834,14 @@ static QemuOptsList nbd_runtime_opts = {static int nbd_process_options (BlockDriverState *bs,QDict *options,Error **errp) { +ERRP_AUTO_PROPAGATE ();BDRVNBDState *s = bs->opaque;QemuOpts *opts; -Error *local_err =NULL ;int ret = -EINVAL ; opts =qemu_opts_create (&nbd_runtime_opts,NULL ,0 , &error_abort); -qemu_opts_absorb_qdict (opts, options, &local_err); -if (local_err) { -error_propagate (errp, local_err); +qemu_opts_absorb_qdict (opts, options, errp); +if (*errp) { goto error; }
nbd/client.c +5 -0
@@ -68,6 +68,7 @@ static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt, uint32_t len, const char *data, Error **errp) { +ERRP_AUTO_PROPAGATE ();NBDOption req;QEMU_BUILD_BUG_ON (sizeof (req) !=16 );
@@ -153,6 +154,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,static int nbd_handle_reply_err (QIOChannel *ioc,NBDOptionReply *reply,bool strict,Error **errp) { +ERRP_AUTO_PROPAGATE (); g_autofree char *msg = NULL;if (!(reply->type & (1 <<31 ))) {
@@ -337,6 +339,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,static int nbd_opt_info_or_go (QIOChannel *ioc,uint32_t opt,NBDExportInfo *info,Error **errp) { +ERRP_AUTO_PROPAGATE ();NBDOptionReply reply;uint32_t len =strlen (info->name);uint16_t type;
@@ -882,6 +885,7 @@ static int nbd_start_negotiate(AioContext *aio_context, QIOChannel *ioc, bool structured_reply, bool *zeroes, Error **errp) { +ERRP_AUTO_PROPAGATE ();uint64_t magic;trace_nbd_start_negotiate (tlscreds, hostname ? hostname :"<null>" );
@@ -1017,6 +1021,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QIOChannel *ioc, const char *hostname, QIOChannel **outioc, NBDExportInfo *info, Error **errp) { +ERRP_AUTO_PROPAGATE ();int result;bool zeroes;bool base_allocation = info->base_allocation;
nbd/server.c +5 -0
@@ -211,6 +211,7 @@ static int GCC_FMT_ATTR(4, 0) nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type, Error **errp, const char *fmt, va_list va) { +ERRP_AUTO_PROPAGATE (); g_autofree char *msg = NULL;int ret;size_t len;
@@ -369,6 +370,7 @@ static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,static int nbd_negotiate_send_rep_list (NBDClient *client,NBDExport *exp,Error **errp) { +ERRP_AUTO_PROPAGATE ();size_t name_len, desc_len;uint32_t len;const char *name = exp->name ? exp->name :"" ;
@@ -432,6 +434,7 @@ static void nbd_check_meta_export(NBDClient *client)static int nbd_negotiate_handle_export_name (NBDClient *client,bool no_zeroes,Error **errp) { +ERRP_AUTO_PROPAGATE (); g_autofree char *name = NULL;char buf[NBD_REPLY_EXPORT_NAME_SIZE ] ="" ;size_t len;
@@ -1272,6 +1275,7 @@ static int nbd_negotiate_options(NBDClient *client, Error **errp)*/ static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp) { +ERRP_AUTO_PROPAGATE ();char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE ] ="" ;int ret;
@@ -1646,6 +1650,7 @@ void nbd_export_close(NBDExport *exp)void nbd_export_remove (NBDExport *exp,NbdServerRemoveMode mode,Error **errp) { +ERRP_AUTO_PROPAGATE ();if (mode ==NBD_SERVER_REMOVE_MODE_HARD ||QTAILQ_EMPTY (&exp->clients)) {nbd_export_close (exp);return ;
Signature · 1 line
2.21.0
PATCH v10 9/9 xen: introduce ERRP_AUTO_PROPAGATE
In reply to Vladimir Sementsov-OgievskiyIf we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix such cases.
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate)
This commit is generated by command
sed -n '/^X86 Xen CPUs$/,/^$/{s/^F: //p}' MAINTAINERS | \
xargs git ls-files | grep '\.[hc]$' | \
xargs spatch \
--sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h \
--in-place --no-show-diff --max-width 80--- hw/block/dataplane/xen-block.c | 17 ++--- hw/block/xen-block.c | 122 ++++++++++++++------------------- hw/pci-host/xen_igd_pt.c | 7 +- hw/xen/xen-backend.c | 7 +- hw/xen/xen-bus.c | 92 +++++++++++-------------- hw/xen/xen-host-pci-device.c | 27 ++++---- hw/xen/xen_pt.c | 25 +++---- hw/xen/xen_pt_config_init.c | 20 +++--- 8 files changed, 138 insertions(+), 179 deletions(-)
hw/block/dataplane/xen-block.c +7 -10
@@ -739,8 +739,8 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane,unsigned int protocol, Error **errp) { +ERRP_AUTO_PROPAGATE ();XenDevice *xendev = dataplane->xendev; -Error *local_err =NULL ;unsigned int ring_size;unsigned int i;
@@ -776,9 +776,8 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane, }xen_device_set_max_grant_refs (xendev, dataplane->nr_ring_ref, - &local_err); -if (local_err) { -error_propagate (errp, local_err); + errp); +if (*errp) { goto stop; }
@@ -786,9 +785,8 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane, dataplane->ring_ref, dataplane->nr_ring_ref,PROT_READ |PROT_WRITE , - &local_err); -if (local_err) { -error_propagate (errp, local_err); + errp); +if (*errp) { goto stop; }
@@ -821,9 +819,8 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane, dataplane->event_channel =xen_device_bind_event_channel (xendev, event_channel, xen_block_dataplane_event, dataplane, - &local_err); -if (local_err) { -error_propagate (errp, local_err); + errp); +if (*errp) { goto stop; }
hw/block/xen-block.c +52 -70
309 lines not on this page · raw message
hw/pci-host/xen_igd_pt.c +3 -4
@@ -79,17 +79,16 @@ static void host_pci_config_read(int pos, int len, uint32_t *val, Error **errp)static void igd_pt_i440fx_realize (PCIDevice *pci_dev,Error **errp) { +ERRP_AUTO_PROPAGATE ();uint32_t val =0 ;size_t i;int pos, len; -Error *local_err =NULL ;for (i =0 ; i <ARRAY_SIZE (igd_host_bridge_infos); i++) { pos = igd_host_bridge_infos[i].offset; len = igd_host_bridge_infos[i].len; -host_pci_config_read (pos, len, &val, &local_err); -if (local_err) { -error_propagate (errp, local_err); +host_pci_config_read (pos, len, &val, errp); +if (*errp) {return ; }pci_default_write_config (pci_dev, pos, val, len);
hw/xen/xen-backend.c +3 -4
@@ -98,9 +98,9 @@ static void xen_backend_list_remove(XenBackendInstance *backend)void xen_backend_device_create (XenBus *xenbus,const char *type,const char *name,QDict *opts,Error **errp) { +ERRP_AUTO_PROPAGATE ();const XenBackendImpl *impl =xen_backend_table_lookup (type);XenBackendInstance *backend; -Error *local_error =NULL ;if (!impl) {return ;
@@ -110,9 +110,8 @@ void xen_backend_device_create(XenBus *xenbus, const char *type, backend->xenbus = xenbus; backend->name =g_strdup (name); - impl->create (backend, opts, &local_error); -if (local_error) { -error_propagate (errp, local_error); + impl->create (backend, opts, errp); +if (*errp) {g_free (backend->name);g_free (backend);return ;
hw/xen/xen-bus.c +39 -53
219 lines not on this page · raw message
hw/xen/xen-host-pci-device.c +13 -14
@@ -333,8 +333,8 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, uint8_t bus, uint8_t dev, uint8_t func, Error **errp) { +ERRP_AUTO_PROPAGATE ();unsigned int v; -Error *err =NULL ; d->config_fd =-1 ; d->domain = domain;
@@ -342,36 +342,36 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, d->dev = dev; d->func = func; -xen_host_pci_config_open (d, &err); -if (err) { +xen_host_pci_config_open (d, errp); +if (*errp) { goto error; } -xen_host_pci_get_resource (d, &err); -if (err) { +xen_host_pci_get_resource (d, errp); +if (*errp) { goto error; } -xen_host_pci_get_hex_value (d,"vendor" , &v, &err); -if (err) { +xen_host_pci_get_hex_value (d,"vendor" , &v, errp); +if (*errp) { goto error; } d->vendor_id = v; -xen_host_pci_get_hex_value (d,"device" , &v, &err); -if (err) { +xen_host_pci_get_hex_value (d,"device" , &v, errp); +if (*errp) { goto error; } d->device_id = v; -xen_host_pci_get_dec_value (d,"irq" , &v, &err); -if (err) { +xen_host_pci_get_dec_value (d,"irq" , &v, errp); +if (*errp) { goto error; } d->irq = v; -xen_host_pci_get_hex_value (d,"class" , &v, &err); -if (err) { +xen_host_pci_get_hex_value (d,"class" , &v, errp); +if (*errp) { goto error; } d->class_code = v;
@@ -381,7 +381,6 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,return ; error: -error_propagate (errp, err);if (d->config_fd >=0 ) {close (d->config_fd);
hw/xen/xen_pt.c +11 -14
@@ -767,12 +767,12 @@ static void xen_pt_destroy(PCIDevice *d) {static void xen_pt_realize (PCIDevice *d,Error **errp) { +ERRP_AUTO_PROPAGATE ();XenPCIPassthroughState *s =XEN_PT_DEVICE (d);int i, rc =0 ;uint8_t machine_irq =0 , scratch;uint16_t cmd =0 ;int pirq =XEN_PT_UNASSIGNED_PIRQ ; -Error *err =NULL ;/* register real device */ XEN_PT_LOG(d, "Assigning real physical device %02x:%02x.%d"
@@ -783,10 +783,9 @@ static void xen_pt_realize(PCIDevice *d, Error **errp)xen_host_pci_device_get (&s->real_device, s->hostaddr.domain, s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function, - &err); -if (err) { -error_append_hint (&err,"Failed to \"open\" the real pci device" ); -error_propagate (errp, err); + errp); +if (*errp) { +error_append_hint (errp,"Failed to \"open\" the real pci device" );return ; }
@@ -813,11 +812,10 @@ static void xen_pt_realize(PCIDevice *d, Error **errp)return ; } -xen_pt_setup_vga (s, &s->real_device, &err); -if (err) { -error_append_hint (&err,"Setup VGA BIOS of passthrough" -" GFX failed" ); -error_propagate (errp, err); +xen_pt_setup_vga (s, &s->real_device, errp); +if (*errp) { +error_append_hint (errp,"Setup VGA BIOS of passthrough" +" GFX failed" );xen_host_pci_device_put (&s->real_device);return ; }
@@ -830,10 +828,9 @@ static void xen_pt_realize(PCIDevice *d, Error **errp)xen_pt_register_regions (s, &cmd);/* reinitialize each config register to be emulated */ -xen_pt_config_init (s, &err); -if (err) { -error_append_hint (&err,"PCI Config space initialisation failed" ); -error_propagate (errp, err); +xen_pt_config_init (s, errp); +if (*errp) { +error_append_hint (errp,"PCI Config space initialisation failed" ); rc =-1 ; goto err_out; }
hw/xen/xen_pt_config_init.c +10 -10
@@ -2008,8 +2008,8 @@ static void xen_pt_config_reg_init(XenPCIPassthroughState *s,void xen_pt_config_init (XenPCIPassthroughState *s,Error **errp) { +ERRP_AUTO_PROPAGATE ();int i, rc; -Error *err =NULL ;QLIST_INIT (&s->reg_grps);
@@ -2052,10 +2052,9 @@ void xen_pt_config_init(XenPCIPassthroughState *s, Error **errp) reg_grp_offset, ®_grp_entry->size);if (rc <0 ) { -error_setg (&err,"Failed to initialize %d/%zu, type = 0x%x," +error_setg (errp,"Failed to initialize %d/%zu, type = 0x%x," " rc: %d" , i,ARRAY_SIZE (xen_pt_emu_reg_grps), xen_pt_emu_reg_grps[i].grp_type, rc); -error_propagate (errp, err);xen_pt_config_delete (s);return ; }
@@ -2068,13 +2067,14 @@ void xen_pt_config_init(XenPCIPassthroughState *s, Error **errp)/* initialize capability register */ for (j =0 ; regs->size !=0 ; j++, regs++) { -xen_pt_config_reg_init (s, reg_grp_entry, regs, &err); -if (err) { -error_append_hint (&err,"Failed to init register %d" -" offsets 0x%x in grp_type = 0x%x (%d/%zu)" , j, - regs->offset, xen_pt_emu_reg_grps[i].grp_type, - i,ARRAY_SIZE (xen_pt_emu_reg_grps)); -error_propagate (errp, err); +xen_pt_config_reg_init (s, reg_grp_entry, regs, errp); +if (*errp) { +error_append_hint (errp,"Failed to init register %d" +" offsets 0x%x in grp_type = 0x%x (%d/%zu)" , + j, + regs->offset, + xen_pt_emu_reg_grps[i].grp_type, + i,ARRAY_SIZE (xen_pt_emu_reg_grps));xen_pt_config_delete (s);return ; }
Signature · 7 lines
2.21.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel