From nobody Wed Nov 27 11:50:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1699365094; cv=none; d=zohomail.com; s=zohoarc; b=m0zpBw9i8pyc1sRoet+tf2NXjLTZPNslhmwQu5Vi4armRuUlub40ij+JQsNV34XJRjC/udEf217TlW+HaWwlR1Vi4NPaE9XqV6I8v33NdjXI+pfc04e0J6kayAE4qNU/OSMoD0pSih/H8aHgfkJek1mmKWMOFMXqrLwjnetZMaA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699365094; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sSv2i15i579/KCuDTINwxGQbK6Z9nfCzTYTS3zqF80o=; b=PXqXqhxzbQlgR3Qi+qPcp16laGfphVUJyM5Pxn+FQY8KgaMm1ohwlKU5uOGXsBRH+SLpM5gdql0skSpxDPYbYrzgCLJJFp9j8lm6mDwEre4wp0bi1zxtJjnJFDCJG4jIlO3OVcIfBLJ+oyIRepWc2Gv6xvW8g2b/1SDRZelT1OM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1699365094255419.4982504601736; Tue, 7 Nov 2023 05:51:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0MSA-00086j-1H; Tue, 07 Nov 2023 08:49:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r0MS6-0007yh-5h for qemu-devel@nongnu.org; Tue, 07 Nov 2023 08:49:11 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r0MRy-0007Nj-Ja for qemu-devel@nongnu.org; Tue, 07 Nov 2023 08:49:06 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-625-yxCgfQ26MVaUd7_pfQQADg-1; Tue, 07 Nov 2023 08:48:54 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1569B811001 for ; Tue, 7 Nov 2023 13:48:54 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 932BA1C060AE; Tue, 7 Nov 2023 13:48:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699364941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sSv2i15i579/KCuDTINwxGQbK6Z9nfCzTYTS3zqF80o=; b=Yj4uWUkSuNxO0tU+SlSyWsjmWRyM6SdehvbuwMrBPo0oW9j3bO8n2C4XJwCFpC1NsdyHdI DdkLoH2z6rY2OwTzhpCplyPBxhx8TWHg2XOv41f5ba42YyTyOtHTEC+12xhBF58YWjCL1w i8S+Rx+JiMAYgt+fs7WTGEkpRR8/vFo= X-MC-Unique: yxCgfQ26MVaUd7_pfQQADg-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 11/11] qdev: Rework array properties based on list visitor Date: Tue, 7 Nov 2023 14:48:28 +0100 Message-ID: <20231107134828.35909-12-kwolf@redhat.com> In-Reply-To: <20231107134828.35909-1-kwolf@redhat.com> References: <20231107134828.35909-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1699365096311100006 Content-Type: text/plain; charset="utf-8" Until now, array properties are actually implemented with a hack that uses multiple properties on the QOM level: a static "foo-len" property and after it is set, dynamically created "foo[i]" properties. In external interfaces (-device on the command line and device_add in QMP), this interface was broken by commit f3558b1b ('qdev: Base object creation on QDict rather than QemuOpts') because QDicts are unordered and therefore it could happen that QEMU tried to set the indexed properties before setting the length, which fails and effectively makes array properties inaccessible. In particular, this affects the 'ports' property of the 'rocker' device, which used to be configured like this: -device rocker,len-ports=3D2,ports[0]=3Ddev0,ports[1]=3Ddev1 This patch reworks the external interface so that instead of using a separate top-level property for the length and for each element, we use a single true array property that accepts a list value. In the external interfaces, this is naturally expressed as a JSON list and makes array properties accessible again. The new syntax looks like this: -device '{"driver":"rocker","ports":["dev0","dev1"]}' Creating an array property on the command line without using JSON format is currently not possible. This could be fixed by switching from QemuOpts to a keyval parser, which however requires consideration of the compatibility implications. All internal users of devices with array properties go through qdev_prop_set_array() at this point, so updating it takes care of all of them. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1090 Fixes: f3558b1b763683bb877f7dd5b282469cdadc65c3 Signed-off-by: Kevin Wolf Message-ID: <20231030142658.182193-13-kwolf@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Kevin Wolf --- include/hw/qdev-properties.h | 57 +++++---- hw/core/qdev-properties.c | 224 ++++++++++++++++++++++------------- 2 files changed, 181 insertions(+), 100 deletions(-) diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h index 7fa2fdb7c9..256d9e5024 100644 --- a/include/hw/qdev-properties.h +++ b/include/hw/qdev-properties.h @@ -2,6 +2,7 @@ #define QEMU_QDEV_PROPERTIES_H =20 #include "hw/qdev-core.h" +#include "qapi/visitor.h" =20 /** * Property: @@ -61,7 +62,7 @@ extern const PropertyInfo qdev_prop_size; extern const PropertyInfo qdev_prop_string; extern const PropertyInfo qdev_prop_on_off_auto; extern const PropertyInfo qdev_prop_size32; -extern const PropertyInfo qdev_prop_arraylen; +extern const PropertyInfo qdev_prop_array; extern const PropertyInfo qdev_prop_link; =20 #define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) { \ @@ -115,8 +116,6 @@ extern const PropertyInfo qdev_prop_link; .bitmask =3D (_bitmask), \ .set_default =3D false) =20 -#define PROP_ARRAY_LEN_PREFIX "len-" - /** * DEFINE_PROP_ARRAY: * @_name: name of the array @@ -127,28 +126,46 @@ extern const PropertyInfo qdev_prop_link; * @_arrayprop: PropertyInfo defining what property the array elements have * @_arraytype: C type of the array elements * - * Define device properties for a variable-length array _name. A - * static property "len-arrayname" is defined. When the device creator - * sets this property to the desired length of array, further dynamic - * properties "arrayname[0]", "arrayname[1]", ... are defined so the - * device creator can set the array element values. Setting the - * "len-arrayname" property more than once is an error. + * Define device properties for a variable-length array _name. The array = is + * represented as a list in the visitor interface. + * + * @_arraytype is required to be movable with memcpy() and to have an alig= nment + * such that it can be stored at GenericList.padding. * - * When the array length is set, the @_field member of the device + * When the array property is set, the @_field member of the device * struct is set to the array length, and @_arrayfield is set to point - * to (zero-initialised) memory allocated for the array. For a zero - * length array, @_field will be set to 0 and @_arrayfield to NULL. + * to the memory allocated for the array. + * * It is the responsibility of the device deinit code to free the * @_arrayfield memory. */ -#define DEFINE_PROP_ARRAY(_name, _state, _field, \ - _arrayfield, _arrayprop, _arraytype) \ - DEFINE_PROP((PROP_ARRAY_LEN_PREFIX _name), \ - _state, _field, qdev_prop_arraylen, uint32_t, \ - .set_default =3D true, \ - .defval.u =3D 0, \ - .arrayinfo =3D &(_arrayprop), \ - .arrayfieldsize =3D sizeof(_arraytype), \ +#define DEFINE_PROP_ARRAY(_name, _state, _field, \ + _arrayfield, _arrayprop, _arraytype) \ + DEFINE_PROP(_name, _state, _field, qdev_prop_array, uint32_t, \ + .set_default =3D true, \ + .defval.u =3D 0, \ + .arrayinfo =3D &(_arrayprop), \ + /* \ + * set_prop_array() temporarily stores elements at \ + * GenericList.padding. Make sure that this has the \ + * right alignment for @_arraytype. \ + * \ + * Hack: In this place, neither static assertions work \ + * nor is a statement expression allowed. This \ + * abomination of an expression works because inside \ + * the declaration of a dummy struct, static assertions \ + * are possible. Using the comma operator causes \ + * warnings about an unused value and casting to void \ + * makes the expression not constant in gcc, so instead \ + * of ignoring the first part, make it evaluate to 0 \ + * and add it to the actual result. \ + */ \ + .arrayfieldsize =3D (!sizeof(struct { \ + QEMU_BUILD_BUG_ON( \ + !QEMU_IS_ALIGNED(sizeof(GenericList), \ + __alignof__(_arraytype))); \ + int dummy; \ + }) + sizeof(_arraytype)), \ .arrayoffset =3D offsetof(_state, _arrayfield)) =20 #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) \ diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 950ef48e01..4f3e1152be 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -546,98 +546,174 @@ const PropertyInfo qdev_prop_size32 =3D { =20 /* --- support for array properties --- */ =20 -/* Used as an opaque for the object properties we add for each - * array element. Note that the struct Property must be first - * in the struct so that a pointer to this works as the opaque - * for the underlying element's property hooks as well as for - * our own release callback. +/* + * Given an array property @parent_prop in @obj, return a Property for a + * specific element of the array. Arrays are backed by an uint32_t length = field + * and an element array. @elem points at an element in this element array. */ -typedef struct { - struct Property prop; - char *propname; - ObjectPropertyRelease *release; -} ArrayElementProperty; - -/* object property release callback for array element properties: - * we call the underlying element's property release hook, and - * then free the memory we allocated when we added the property. +static Property array_elem_prop(Object *obj, Property *parent_prop, + const char *name, char *elem) +{ + return (Property) { + .info =3D parent_prop->arrayinfo, + .name =3D name, + /* + * This ugly piece of pointer arithmetic sets up the offset so + * that when the underlying release hook calls qdev_get_prop_ptr + * they get the right answer despite the array element not actually + * being inside the device struct. + */ + .offset =3D (uintptr_t)elem - (uintptr_t)obj, + }; +} + +/* + * Object property release callback for array properties: We call the + * underlying element's property release hook for each element. + * + * Note that it is the responsibility of the individual device's deinit + * to free the array proper. */ -static void array_element_release(Object *obj, const char *name, void *opa= que) +static void release_prop_array(Object *obj, const char *name, void *opaque) { - ArrayElementProperty *p =3D opaque; - if (p->release) { - p->release(obj, name, opaque); + Property *prop =3D opaque; + uint32_t *alenptr =3D object_field_prop_ptr(obj, prop); + void **arrayptr =3D (void *)obj + prop->arrayoffset; + char *elem =3D *arrayptr; + int i; + + if (!prop->arrayinfo->release) { + return; + } + + for (i =3D 0; i < *alenptr; i++) { + Property elem_prop =3D array_elem_prop(obj, prop, name, elem); + prop->arrayinfo->release(obj, NULL, &elem_prop); + elem +=3D prop->arrayfieldsize; } - g_free(p->propname); - g_free(p); } =20 -static void set_prop_arraylen(Object *obj, Visitor *v, const char *name, - void *opaque, Error **errp) +/* + * Setter for an array property. This sets both the array length (which + * is technically the property field in the object) and the array itself + * (a pointer to which is stored in the additional field described by + * prop->arrayoffset). + */ +static void set_prop_array(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) { - /* Setter for the property which defines the length of a - * variable-sized property array. As well as actually setting the - * array-length field in the device struct, we have to create the - * array itself and dynamically add the corresponding properties. - */ + ERRP_GUARD(); Property *prop =3D opaque; uint32_t *alenptr =3D object_field_prop_ptr(obj, prop); void **arrayptr =3D (void *)obj + prop->arrayoffset; - void *eltptr; - const char *arrayname; - int i; + GenericList *list, *elem, *next; + const size_t list_elem_size =3D sizeof(*list) + prop->arrayfieldsize; + char *elemptr; + bool ok =3D true; =20 if (*alenptr) { error_setg(errp, "array size property %s may not be set more than = once", name); return; } - if (!visit_type_uint32(v, name, alenptr, errp)) { + + if (!visit_start_list(v, name, &list, list_elem_size, errp)) { return; } - if (!*alenptr) { + + /* Read the whole input into a temporary list */ + elem =3D list; + while (elem) { + Property elem_prop =3D array_elem_prop(obj, prop, name, elem->padd= ing); + prop->arrayinfo->set(obj, v, NULL, &elem_prop, errp); + if (*errp) { + ok =3D false; + goto out_obj; + } + if (*alenptr =3D=3D INT_MAX) { + error_setg(errp, "array is too big"); + return; + } + (*alenptr)++; + elem =3D visit_next_list(v, elem, list_elem_size); + } + + ok =3D visit_check_list(v, errp); +out_obj: + visit_end_list(v, (void**) &list); + + if (!ok) { + for (elem =3D list; elem; elem =3D next) { + Property elem_prop =3D array_elem_prop(obj, prop, name, + elem->padding); + if (prop->arrayinfo->release) { + prop->arrayinfo->release(obj, NULL, &elem_prop); + } + next =3D elem->next; + g_free(elem); + } return; } =20 - /* DEFINE_PROP_ARRAY guarantees that name should start with this prefi= x; - * strip it off so we can get the name of the array itself. + /* + * Now that we know how big the array has to be, move the data over to= a + * linear array and free the temporary list. */ - assert(strncmp(name, PROP_ARRAY_LEN_PREFIX, - strlen(PROP_ARRAY_LEN_PREFIX)) =3D=3D 0); - arrayname =3D name + strlen(PROP_ARRAY_LEN_PREFIX); + *arrayptr =3D g_malloc_n(*alenptr, prop->arrayfieldsize); + elemptr =3D *arrayptr; + for (elem =3D list; elem; elem =3D next) { + memcpy(elemptr, elem->padding, prop->arrayfieldsize); + elemptr +=3D prop->arrayfieldsize; + next =3D elem->next; + g_free(elem); + } +} =20 - /* Note that it is the responsibility of the individual device's deinit - * to free the array proper. - */ - *arrayptr =3D eltptr =3D g_malloc0(*alenptr * prop->arrayfieldsize); - for (i =3D 0; i < *alenptr; i++, eltptr +=3D prop->arrayfieldsize) { - char *propname =3D g_strdup_printf("%s[%d]", arrayname, i); - ArrayElementProperty *arrayprop =3D g_new0(ArrayElementProperty, 1= ); - arrayprop->release =3D prop->arrayinfo->release; - arrayprop->propname =3D propname; - arrayprop->prop.info =3D prop->arrayinfo; - arrayprop->prop.name =3D propname; - /* This ugly piece of pointer arithmetic sets up the offset so - * that when the underlying get/set hooks call qdev_get_prop_ptr - * they get the right answer despite the array element not actually - * being inside the device struct. - */ - arrayprop->prop.offset =3D eltptr - (void *)obj; - assert(object_field_prop_ptr(obj, &arrayprop->prop) =3D=3D eltptr); - object_property_add(obj, propname, - arrayprop->prop.info->name, - field_prop_getter(arrayprop->prop.info), - field_prop_setter(arrayprop->prop.info), - array_element_release, - arrayprop); +static void get_prop_array(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + ERRP_GUARD(); + Property *prop =3D opaque; + uint32_t *alenptr =3D object_field_prop_ptr(obj, prop); + void **arrayptr =3D (void *)obj + prop->arrayoffset; + char *elem =3D *arrayptr; + GenericList *list; + const size_t list_elem_size =3D sizeof(*list) + prop->arrayfieldsize; + int i; + bool ok; + + if (!visit_start_list(v, name, &list, list_elem_size, errp)) { + return; } + + for (i =3D 0; i < *alenptr; i++) { + Property elem_prop =3D array_elem_prop(obj, prop, name, elem); + prop->arrayinfo->get(obj, v, NULL, &elem_prop, errp); + if (*errp) { + goto out_obj; + } + elem +=3D prop->arrayfieldsize; + } + + /* visit_check_list() can only fail for input visitors */ + ok =3D visit_check_list(v, errp); + assert(ok); + +out_obj: + visit_end_list(v, (void**) &list); } =20 -const PropertyInfo qdev_prop_arraylen =3D { - .name =3D "uint32", - .get =3D get_uint32, - .set =3D set_prop_arraylen, - .set_default_value =3D qdev_propinfo_set_default_value_uint, +static void default_prop_array(ObjectProperty *op, const Property *prop) +{ + object_property_set_default_list(op); +} + +const PropertyInfo qdev_prop_array =3D { + .name =3D "list", + .get =3D get_prop_array, + .set =3D set_prop_array, + .release =3D release_prop_array, + .set_default_value =3D default_prop_array, }; =20 /* --- public helpers --- */ @@ -743,20 +819,8 @@ void qdev_prop_set_enum(DeviceState *dev, const char *= name, int value) =20 void qdev_prop_set_array(DeviceState *dev, const char *name, QList *values) { - const QListEntry *entry; - g_autofree char *prop_len =3D g_strdup_printf("len-%s", name); - uint32_t i =3D 0; - - object_property_set_int(OBJECT(dev), prop_len, qlist_size(values), - &error_abort); - - QLIST_FOREACH_ENTRY(values, entry) { - g_autofree char *prop_idx =3D g_strdup_printf("%s[%u]", name, i); - object_property_set_qobject(OBJECT(dev), prop_idx, entry->value, - &error_abort); - i++; - } - + object_property_set_qobject(OBJECT(dev), name, QOBJECT(values), + &error_abort); qobject_unref(values); } =20 --=20 2.41.0