As mentioned in previous patches, we want to call visit_end_struct() functions unconditionally, so that visitors can release resources tied up since the matching visit_start_struct() without also having to worry about error priority if more than one error occurs.
Even though error_propagate() can be safely used to ignore a second error during cleanup caused by a first error, it is simpler if the cleanup cannot set an error, and we instead split the task of checking that an input visitor has no unvisited input as a new function visit_check_struct(), called only if all prior steps are successful. Signed-off-by: Eric Blake <ebl...@redhat.com> --- v6: new patch, revised version of RFC based on discussion of v5 7/46 --- hmp.c | 8 +++----- hw/ppc/spapr_drc.c | 3 ++- hw/virtio/virtio-balloon.c | 12 ++++++------ include/qapi/visitor-impl.h | 4 +++- include/qapi/visitor.h | 12 ++++++++++-- qapi/opts-visitor.c | 17 +++++++++++++++-- qapi/qapi-dealloc-visitor.c | 2 +- qapi/qapi-visit-core.c | 11 +++++++++-- qapi/qmp-input-visitor.c | 34 +++++++++++++++++++--------------- qapi/qmp-output-visitor.c | 2 +- qom/object.c | 5 ++--- scripts/qapi-event.py | 3 ++- scripts/qapi-visit.py | 9 ++++----- vl.c | 9 ++++----- 14 files changed, 81 insertions(+), 50 deletions(-) diff --git a/hmp.c b/hmp.c index ec1d682..b09f78b 100644 --- a/hmp.c +++ b/hmp.c @@ -1663,7 +1663,6 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict) void hmp_object_add(Monitor *mon, const QDict *qdict) { Error *err = NULL; - Error *err_end = NULL; QemuOpts *opts; char *type = NULL; char *id = NULL; @@ -1696,13 +1695,12 @@ void hmp_object_add(Monitor *mon, const QDict *qdict) if (err) { goto out_end; } - + visit_check_struct(v, &err); out_end: - visit_end_struct(v, &err_end); - if (!err && !err_end) { + visit_end_struct(v); + if (!err) { object_add(type, id, pdict, v, &err); } - error_propagate(&err, err_end); out_clean: opts_visitor_cleanup(ov); diff --git a/hw/ppc/spapr_drc.c b/hw/ppc/spapr_drc.c index 03a1879..5ff270f 100644 --- a/hw/ppc/spapr_drc.c +++ b/hw/ppc/spapr_drc.c @@ -281,7 +281,8 @@ static void prop_get_fdt(Object *obj, Visitor *v, void *opaque, case FDT_END_NODE: /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */ g_assert(fdt_depth > 0); - visit_end_struct(v, &error_abort); + visit_check_struct(v, &error_abort); + visit_end_struct(v); fdt_depth--; break; case FDT_PROP: { diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 1ce987a..531913b 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -136,15 +136,15 @@ static void balloon_stats_get_all(Object *obj, struct Visitor *v, goto out_nested; } } + visit_check_struct(v, &err); out_nested: - error_propagate(errp, err); - err = NULL; - visit_end_struct(v, &err); + visit_end_struct(v); + if (!err) { + visit_check_struct(v, &err); + } out_end: - error_propagate(errp, err); - err = NULL; - visit_end_struct(v, &err); + visit_end_struct(v); out: error_propagate(errp, err); } diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index 018f419..5350bdf 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -28,8 +28,10 @@ struct Visitor * currently visit structs). */ void (*start_struct)(Visitor *v, void **obj, const char *kind, const char *name, size_t size, Error **errp); + /* May be NULL; most useful for input visitors. */ + void (*check_struct)(Visitor *v, Error **errp); /* Must be provided if start_struct is present. */ - void (*end_struct)(Visitor *v, Error **errp); + void (*end_struct)(Visitor *v); /* May be NULL; most useful for input visitors. */ void (*start_implicit_struct)(Visitor *v, void **obj, size_t size, diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index cc1ff6d..f9ea325 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -57,11 +57,19 @@ typedef struct GenericList void visit_start_struct(Visitor *v, void **obj, const char *kind, const char *name, size_t size, Error **errp); /** + * Prepare for completing a struct visit. + * Should be called prior to visit_end_struct() if all other intermediate + * visit steps were successful, to allow the caller one last chance to + * report errors such as remaining data that was not consumed by the visit. + */ +void visit_check_struct(Visitor *v, Error **errp); +/** * Complete a struct visit started earlier. * Must be called after any successful use of visit_start_struct(), - * even if intermediate processing was skipped due to errors. + * even if intermediate processing was skipped due to errors, to allow + * the backend to release any resources. */ -void visit_end_struct(Visitor *v, Error **errp); +void visit_end_struct(Visitor *v); /** * Prepare to visit an implicit struct. diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c index 46dd9fe..db5fc1a 100644 --- a/qapi/opts-visitor.c +++ b/qapi/opts-visitor.c @@ -151,13 +151,13 @@ opts_start_struct(Visitor *v, void **obj, const char *kind, static void -opts_end_struct(Visitor *v, Error **errp) +opts_check_struct(Visitor *v, Error **errp) { OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); GHashTableIter iter; GQueue *any; - if (--ov->depth > 0) { + if (ov->depth > 0) { return; } @@ -169,6 +169,18 @@ opts_end_struct(Visitor *v, Error **errp) first = g_queue_peek_head(any); error_setg(errp, QERR_INVALID_PARAMETER, first->name); } +} + + +static void +opts_end_struct(Visitor *v) +{ + OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + + if (--ov->depth > 0) { + return; + } + g_hash_table_destroy(ov->unprocessed_opts); ov->unprocessed_opts = NULL; if (ov->fake_id_opt) { @@ -500,6 +512,7 @@ opts_visitor_new(const QemuOpts *opts) ov = g_malloc0(sizeof *ov); ov->visitor.start_struct = &opts_start_struct; + ov->visitor.check_struct = &opts_check_struct; ov->visitor.end_struct = &opts_end_struct; ov->visitor.start_list = &opts_start_list; diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c index 8b489a4..14dc7c3 100644 --- a/qapi/qapi-dealloc-visitor.c +++ b/qapi/qapi-dealloc-visitor.c @@ -67,7 +67,7 @@ static void qapi_dealloc_start_struct(Visitor *v, void **obj, const char *kind, qapi_dealloc_push(qov, obj); } -static void qapi_dealloc_end_struct(Visitor *v, Error **errp) +static void qapi_dealloc_end_struct(Visitor *v) { QapiDeallocVisitor *qov = to_qov(v); void **obj = qapi_dealloc_pop(qov); diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index a193a04..00f446e 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -23,9 +23,16 @@ void visit_start_struct(Visitor *v, void **obj, const char *kind, v->start_struct(v, obj, kind, name, size, errp); } -void visit_end_struct(Visitor *v, Error **errp) +void visit_check_struct(Visitor *v, Error **errp) { - v->end_struct(v, errp); + if (v->check_struct) { + v->check_struct(v, errp); + } +} + +void visit_end_struct(Visitor *v) +{ + v->end_struct(v); } void visit_start_implicit_struct(Visitor *v, void **obj, size_t size, diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 9dbe025..0270d25 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -89,8 +89,10 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp) } -static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp) +static void qmp_input_check_struct(Visitor *v, Error **errp) { + QmpInputVisitor *qiv = to_qiv(v); + assert(qiv->nb_stack > 0); if (qiv->strict) { @@ -103,6 +105,19 @@ static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp) if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) { error_setg(errp, QERR_QMP_EXTRA_MEMBER, key); } + } + } +} + +static void qmp_input_pop(Visitor *v) +{ + QmpInputVisitor *qiv = to_qiv(v); + + assert(qiv->nb_stack > 0); + + if (qiv->strict) { + GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h; + if (top_ht) { g_hash_table_unref(top_ht); } } @@ -134,12 +149,6 @@ static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind, } } -static void qmp_input_end_struct(Visitor *v, Error **errp) -{ - QmpInputVisitor *qiv = to_qiv(v); - - qmp_input_pop(qiv, errp); -} static void qmp_input_start_implicit_struct(Visitor *v, void **obj, size_t size, Error **errp) @@ -193,12 +202,6 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list, return entry; } -static void qmp_input_end_list(Visitor *v) -{ - QmpInputVisitor *qiv = to_qiv(v); - - qmp_input_pop(qiv, &error_abort); -} static void qmp_input_get_next_type(Visitor *v, QType *type, bool promote_int, const char *name, Error **errp) @@ -341,11 +344,12 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj) v = g_malloc0(sizeof(*v)); v->visitor.start_struct = qmp_input_start_struct; - v->visitor.end_struct = qmp_input_end_struct; + v->visitor.check_struct = qmp_input_check_struct; + v->visitor.end_struct = qmp_input_pop; v->visitor.start_implicit_struct = qmp_input_start_implicit_struct; v->visitor.start_list = qmp_input_start_list; v->visitor.next_list = qmp_input_next_list; - v->visitor.end_list = qmp_input_end_list; + v->visitor.end_list = qmp_input_pop; v->visitor.type_enum = input_type_enum; v->visitor.type_int64 = qmp_input_type_int64; v->visitor.type_uint64 = qmp_input_type_uint64; diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c index 8035649..af1e01b 100644 --- a/qapi/qmp-output-visitor.c +++ b/qapi/qmp-output-visitor.c @@ -122,7 +122,7 @@ static void qmp_output_start_struct(Visitor *v, void **obj, const char *kind, qmp_output_push(qov, dict); } -static void qmp_output_end_struct(Visitor *v, Error **errp) +static void qmp_output_end_struct(Visitor *v) { QmpOutputVisitor *qov = to_qov(v); qmp_output_pop(qov); diff --git a/qom/object.c b/qom/object.c index d751569..3d5c9d7 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1904,10 +1904,9 @@ static void property_get_tm(Object *obj, Visitor *v, void *opaque, if (err) { goto out_end; } + visit_check_struct(v, &err); out_end: - error_propagate(errp, err); - err = NULL; - visit_end_struct(v, errp); + visit_end_struct(v); out: error_propagate(errp, err); diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py index 7ee74a5..68d59e1 100644 --- a/scripts/qapi-event.py +++ b/scripts/qapi-event.py @@ -74,8 +74,9 @@ def gen_event_send(name, arg_type): ret += gen_visit_fields(arg_type.members, need_cast=True, label='out_obj') ret += mcgen(''' + visit_check_struct(v, &err); out_obj: - visit_end_struct(v, &err); + visit_end_struct(v); if (err) { goto out; } diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 43233d6..c28208e 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -255,8 +255,7 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error visit_type_%(c_name)s_fields(v, %(cast)sobj, &err); ''', c_name=type_name, cast=cast) - if variants: - ret += gen_err_check(label='out_obj') + ret += gen_err_check(label='out_obj') if variants: ret += mcgen(''' @@ -293,12 +292,12 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error abort(); } ''') + ret += gen_err_check(label='out_obj') ret += mcgen(''' + visit_check_struct(v, &err); out_obj: - error_propagate(errp, err); - err = NULL; - visit_end_struct(v, &err); + visit_end_struct(v); out: error_propagate(errp, err); } diff --git a/vl.c b/vl.c index 4e69815..32e4300 100644 --- a/vl.c +++ b/vl.c @@ -2828,7 +2828,6 @@ static bool object_create_delayed(const char *type) static int object_create(void *opaque, QemuOpts *opts, Error **errp) { Error *err = NULL; - Error *err_end = NULL; char *type = NULL; char *id = NULL; OptsVisitor *ov; @@ -2848,7 +2847,7 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp) qdict_del(pdict, "qom-type"); visit_type_str(v, &type, "qom-type", &err); if (err) { - goto out; + goto out_end; } if (!type_predicate(type)) { goto out_end; @@ -2859,10 +2858,10 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp) if (err) { goto out_end; } - + visit_check_struct(v, &err); out_end: - visit_end_struct(v, &err_end); - if (!err && !err_end) { + visit_end_struct(v); + if (!err) { object_add(type, id, pdict, v, &err); } -- 2.4.3