From nobody Sat Nov 23 19:58:54 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=1729789056; cv=none; d=zohomail.com; s=zohoarc; b=EnaPFfE/ZdhTCHi0fwCkcl/i86W4IV2KUcyMHJyaii9F+SmenSJvjyx8bcqfvw4COh3AdGIcijt4AcwtY+AMBvO6hlz5BktbfX/Y7U40uVOKYGiYzopjpw5gKRX7aMouPhaSNQ94yNgR1E243M7zduoGWsd4kEfgycN8NRP5LaQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729789056; 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=4bMrWu6ahu03dVa1Nj/jzCJZxr91okbKkHKlA528y4A=; b=VZiEUmQ5zg/J8UqV1jEDkIRmQflXrUWzngzXrdqwYjrwPl/Usa29c9sUHCJ5/4LCsv4H3R83G5VE2sq/wOfy/MNDApe4XA3Q7ZJZuTK6AlcozfskWE1+XT2gj5UAC2DWO3aIXI+d4I/4Y4eL+0y0E7QO0HXG+7r3BHVB4SvsdFc= 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 1729789056289591.9789455034874; Thu, 24 Oct 2024 09:57:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t4193-0003w8-5n; Thu, 24 Oct 2024 12:57:10 -0400 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 1t418Y-0003tq-Vm for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t418W-0002wO-A6 for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:37 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-270-Oh-9LA8yPLqGwszaBFln7g-1; Thu, 24 Oct 2024 12:56:33 -0400 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6ce30559894so13964146d6.3 for ; Thu, 24 Oct 2024 09:56:33 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6ce008aff31sm51871076d6.12.2024.10.24.09.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729788994; 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=4bMrWu6ahu03dVa1Nj/jzCJZxr91okbKkHKlA528y4A=; b=OrO0cHPqVM94vNtjuAEBHFavHoylBbxGmHKTXKX/jYMaa9yalX81sKAWdrCGDUNJiaaqDH SrKN1puoC++4z8g7+hCGCC+S1f4KJ+jXDsaO2KUQ0ag8y1eDJ+bSpS1eml2QxCTKBsNGyT LJtOHtz6QZb4+Oyjdq3wI+OyYdLvvr4= X-MC-Unique: Oh-9LA8yPLqGwszaBFln7g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729788992; x=1730393792; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4bMrWu6ahu03dVa1Nj/jzCJZxr91okbKkHKlA528y4A=; b=CjQtq+zv0ZhvGSwoE6auuBZdlxV9xPPRXPF5RenfGOEtAdzU7hNhnGBC3Xoy8utWFh rKWfjoHDNjWBsMKT2/zLjJBgdaKDaPr35o7q2INwNKcFo4VaYa/RUp4gjNgEVSWqKrse 1qJPMoMWUfjr7yzCjotpDNz7k+jA2ZaWEDrvaF1//lDfQHqtlATU8o6sc5LXNY7uI0XL B178UYRa9WRBKgYTalWIr7pkU76M/VFS227XKeTQkglMq3VvPT3YQ64TDR94XbGJ6Fau 0ioypfAYOMHzx6QVYawTCmEac3WLmzyNYL0Qi3jIFG9wIVUYhZkawLT6+AsITr5ZKQcX EWdQ== X-Gm-Message-State: AOJu0YzZzxfeNgOoHDNOMfpF8LmXLcf0dz6MlsqObAxa7f8o/SuyT022 ALnpVPxCkkWYJMT3oiOz2arUX+WBq8McurxdhPVqPLyJjFteLjYNmgHaUHwcP88aH14SQ7PLTEv NbkewcEIRG34RfzCrx3UB7SvTvLPVWvWWqBxhTHnYPtDIifJTwQvCn+vxhXeEle5E9+9oPSZ4Kx EA6aImcrO+2XOJ8pSxc4GImwbzyByeX6ri8Q== X-Received: by 2002:a05:6214:3985:b0:6cb:e4d3:10a0 with SMTP id 6a1803df08f44-6ce342f3d8emr75447226d6.48.1729788992005; Thu, 24 Oct 2024 09:56:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf1VpAhGoD2rZyxmYQA3w3jyukDSTjdLMRt5w8yisKLcg8DBqkDDjYiV2dzwF3CNZ8AZ2Zfg== X-Received: by 2002:a05:6214:3985:b0:6cb:e4d3:10a0 with SMTP id 6a1803df08f44-6ce342f3d8emr75446786d6.48.1729788991457; Thu, 24 Oct 2024 09:56:31 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: peterx@redhat.com, Fabiano Rosas , Igor Mammedov , Juraj Marcin , "Michael S . Tsirkin" , "Dr . David Alan Gilbert" , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Markus Armbruster , Eduardo Habkost , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Alex Williamson , Paolo Bonzini , Peter Maydell Subject: [PATCH 1/4] qom: TYPE_SINGLETON interface Date: Thu, 24 Oct 2024 12:56:24 -0400 Message-ID: <20241024165627.1372621-2-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024165627.1372621-1-peterx@redhat.com> References: <20241024165627.1372621-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1729789058388116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Xu --- include/qom/object_interfaces.h | 47 +++++++++++++++++++++++++++++++++ qom/object.c | 3 +++ qom/object_interfaces.c | 24 +++++++++++++++++ qom/qom-qmp-cmds.c | 22 ++++++++++++--- system/qdev-monitor.c | 7 +++++ 5 files changed, 100 insertions(+), 3 deletions(-) diff --git a/include/qom/object_interfaces.h b/include/qom/object_interface= s.h index 02b11a7ef0..9b2cc0e554 100644 --- a/include/qom/object_interfaces.h +++ b/include/qom/object_interfaces.h @@ -177,4 +177,51 @@ bool user_creatable_del(const char *id, Error **errp); */ void user_creatable_cleanup(void); =20 +#define TYPE_SINGLETON "singleton" + +typedef struct SingletonClass SingletonClass; +DECLARE_CLASS_CHECKERS(SingletonClass, SINGLETON, TYPE_SINGLETON) + +/** + * SingletonClass: + * + * @parent_class: the base class + * @get_instance: fetch the singleton instance if it is created, + * NULL otherwise. + * + * Singleton class describes the type of object classes that can only + * provide one instance for the whole lifecycle of QEMU. It will fail the + * operation if one attemps to create more than one instance. + * + * One can fetch the single object using class's get_instance() callback if + * it was created before. This can be useful for operations like QMP + * qom-list-properties, where dynamically creating an object might not be + * feasible. + */ +struct SingletonClass { + /* */ + InterfaceClass parent_class; + /* */ + Object *(*get_instance)(Error **errp); +}; + +/** + * object_class_is_singleton: + * + * @class: the class to detect singleton + * + * Returns: true if it's a singleton class, false otherwise. + */ +bool object_class_is_singleton(ObjectClass *class); + +/** + * singleton_get_instance: + * + * @class: the class to fetch singleton instance + * + * Returns: the object* if the class is a singleton class and the singleton + * object is created, NULL otherwise. + */ +Object *singleton_get_instance(ObjectClass *class); + #endif diff --git a/qom/object.c b/qom/object.c index 11424cf471..ded299ae1a 100644 --- a/qom/object.c +++ b/qom/object.c @@ -553,6 +553,9 @@ static void object_initialize_with_type(Object *obj, si= ze_t size, TypeImpl *type g_assert(type->abstract =3D=3D false); g_assert(size >=3D type->instance_size); =20 + /* Singleton class can only create one object */ + g_assert(!singleton_get_instance(type->class)); + memset(obj, 0, type->instance_size); obj->class =3D type->class; object_ref(obj); diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c index e0833c8bfe..6766060d0a 100644 --- a/qom/object_interfaces.c +++ b/qom/object_interfaces.c @@ -354,6 +354,23 @@ void user_creatable_cleanup(void) object_unparent(object_get_objects_root()); } =20 +bool object_class_is_singleton(ObjectClass *class) +{ + return !!object_class_dynamic_cast(class, TYPE_SINGLETON); +} + +Object *singleton_get_instance(ObjectClass *class) +{ + SingletonClass *singleton =3D + (SingletonClass *)object_class_dynamic_cast(class, TYPE_SINGLETON); + + if (!singleton) { + return NULL; + } + + return singleton->get_instance(&error_abort); +} + static void register_types(void) { static const TypeInfo uc_interface_info =3D { @@ -362,7 +379,14 @@ static void register_types(void) .class_size =3D sizeof(UserCreatableClass), }; =20 + static const TypeInfo singleton_interface_info =3D { + .name =3D TYPE_SINGLETON, + .parent =3D TYPE_INTERFACE, + .class_size =3D sizeof(SingletonClass), + }; + type_register_static(&uc_interface_info); + type_register_static(&singleton_interface_info); } =20 type_init(register_types) diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c index e91a235347..ecc1cf781c 100644 --- a/qom/qom-qmp-cmds.c +++ b/qom/qom-qmp-cmds.c @@ -126,6 +126,7 @@ ObjectPropertyInfoList *qmp_device_list_properties(cons= t char *typename, ObjectProperty *prop; ObjectPropertyIterator iter; ObjectPropertyInfoList *prop_list =3D NULL; + bool create; =20 klass =3D module_object_class_by_name(typename); if (klass =3D=3D NULL) { @@ -141,7 +142,15 @@ ObjectPropertyInfoList *qmp_device_list_properties(con= st char *typename, return NULL; } =20 - obj =3D object_new(typename); + /* Avoid creating multiple instances if the class is a singleton */ + create =3D !object_class_is_singleton(klass) || + !singleton_get_instance(klass); + + if (create) { + obj =3D object_new(typename); + } else { + obj =3D singleton_get_instance(klass); + } =20 object_property_iter_init(&iter, obj); while ((prop =3D object_property_iter_next(&iter))) { @@ -172,7 +181,9 @@ ObjectPropertyInfoList *qmp_device_list_properties(cons= t char *typename, QAPI_LIST_PREPEND(prop_list, info); } =20 - object_unref(obj); + if (create) { + object_unref(obj); + } =20 return prop_list; } @@ -199,7 +210,12 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const = char *typename, return NULL; } =20 - if (object_class_is_abstract(klass)) { + /* + * Abstract classes are not for instantiations, meanwhile avoid + * creating temporary singleton objects because it can cause conflicts + * if there's already one created. + */ + if (object_class_is_abstract(klass) || object_class_is_singleton(klass= )) { object_class_property_iter_init(&iter, klass); } else { obj =3D object_new(typename); diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c index 44994ea0e1..1310f35c9f 100644 --- a/system/qdev-monitor.c +++ b/system/qdev-monitor.c @@ -36,6 +36,7 @@ #include "qemu/option.h" #include "qemu/qemu-print.h" #include "qemu/option_int.h" +#include "qom/object_interfaces.h" #include "sysemu/block-backend.h" #include "migration/misc.h" #include "qemu/cutils.h" @@ -643,6 +644,12 @@ DeviceState *qdev_device_add_from_qdict(const QDict *o= pts, return NULL; } =20 + if (singleton_get_instance(OBJECT_CLASS(dc))) { + error_setg(errp, "Class '%s' only supports one instance", + driver); + return NULL; + } + /* find bus */ path =3D qdict_get_try_str(opts, "bus"); if (path !=3D NULL) { --=20 2.45.0