From patchwork Thu Oct 24 16:56:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849421 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BBBB8CE8E94 for ; Thu, 24 Oct 2024 16:57:35 +0000 (UTC) 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 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= 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 X-MC-Unique: Oh-9LA8yPLqGwszaBFln7g-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6ce30559894so13964146d6.3 for ; Thu, 24 Oct 2024 09:56:33 -0700 (PDT) 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) 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) 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_Le_Goat?= =?utf-8?q?er?= , Markus Armbruster , Eduardo Habkost , =?utf-8?q?Daniel_P_=2E_Berrang=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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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_interfaces.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); +#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, size_t size, TypeImpl *type g_assert(type->abstract == false); g_assert(size >= type->instance_size); + /* Singleton class can only create one object */ + g_assert(!singleton_get_instance(type->class)); + memset(obj, 0, type->instance_size); obj->class = 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()); } +bool object_class_is_singleton(ObjectClass *class) +{ + return !!object_class_dynamic_cast(class, TYPE_SINGLETON); +} + +Object *singleton_get_instance(ObjectClass *class) +{ + SingletonClass *singleton = + (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 = { @@ -362,7 +379,14 @@ static void register_types(void) .class_size = sizeof(UserCreatableClass), }; + static const TypeInfo singleton_interface_info = { + .name = TYPE_SINGLETON, + .parent = TYPE_INTERFACE, + .class_size = sizeof(SingletonClass), + }; + type_register_static(&uc_interface_info); + type_register_static(&singleton_interface_info); } 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(const char *typename, ObjectProperty *prop; ObjectPropertyIterator iter; ObjectPropertyInfoList *prop_list = NULL; + bool create; klass = module_object_class_by_name(typename); if (klass == NULL) { @@ -141,7 +142,15 @@ ObjectPropertyInfoList *qmp_device_list_properties(const char *typename, return NULL; } - obj = object_new(typename); + /* Avoid creating multiple instances if the class is a singleton */ + create = !object_class_is_singleton(klass) || + !singleton_get_instance(klass); + + if (create) { + obj = object_new(typename); + } else { + obj = singleton_get_instance(klass); + } object_property_iter_init(&iter, obj); while ((prop = object_property_iter_next(&iter))) { @@ -172,7 +181,9 @@ ObjectPropertyInfoList *qmp_device_list_properties(const char *typename, QAPI_LIST_PREPEND(prop_list, info); } - object_unref(obj); + if (create) { + object_unref(obj); + } return prop_list; } @@ -199,7 +210,12 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename, return NULL; } - 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 = 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 *opts, return NULL; } + if (singleton_get_instance(OBJECT_CLASS(dc))) { + error_setg(errp, "Class '%s' only supports one instance", + driver); + return NULL; + } + /* find bus */ path = qdict_get_try_str(opts, "bus"); if (path != NULL) { From patchwork Thu Oct 24 16:56:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849422 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B4E0FCE8E96 for ; Thu, 24 Oct 2024 16:58:12 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t4196-0003wR-In; Thu, 24 Oct 2024 12:57:14 -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 1t418a-0003tx-Sl for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:42 -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 1t418Y-0002we-JN for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729788996; 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=c0kLRIPNRsyiUEIuptSmNc8rruH765h3xOHpyL+r4uw=; b=SFztNE60EY86SVUKL/uWH5gxEz4x8TpJNJfJQ/snsg+LcPvKqdOOyzupxU332s7RoevEov FlKj60pmLwkX4M7yK6PcrZmZmbPc6aUO8FxmIHwIOddpX8hyEO0K/yyHin0vEoHUDiNcH/ M1xp6XtwJwt+lmHMYLSvBev+IbOSQOA= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-46-fAtS33IKPi63denm6uL7yw-1; Thu, 24 Oct 2024 12:56:35 -0400 X-MC-Unique: fAtS33IKPi63denm6uL7yw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6cdf0589e8aso15307156d6.3 for ; Thu, 24 Oct 2024 09:56:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729788994; x=1730393794; 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=c0kLRIPNRsyiUEIuptSmNc8rruH765h3xOHpyL+r4uw=; b=n0uPClROqKuXG7wr4SP5xdFg1oO15GmwS3bnQqNEPqeYYqC9YY23/WRwUbI0vobCIR AYTYLzXn3laZz5quxVVcmNY2N0Rwqx+j9SJsVmbSgr6l7aKPXZ/5ontUJlU6i98g1zNk CuqEZ0pLM86iwYo1jcVzP5aCuDVpVDrAPaWD2wxhIKBxZI8kBBetmF0+94Fs6DZWwDZ6 GtESv211L6XejDB6COxCi14cKZefDPFQhRd8AV5GUCifLCaZev64hb6H9olAawUIHnad d5mj4R7wM91C4576JaGdCDiM6BZgIxU15WLGRn3kmFpr82GVhBEtu0OiKnoNPecqKalB mW1A== X-Gm-Message-State: AOJu0YxQ/c7+vupOr9SeEdh97rCDB/J4lgjARWMUMKs0uCYeLCclTz8O 0icwn9GTGzbgOnsCh8XjM29pwJq/eJw/G/HypW687Kus2mduaOltbwYyReqnGG+PJm3p6t3585z +YFZEzOVMF1sxjs/VJgVOE5FgxUt0czTXDcpBpq0g8r28g+NimfEkgjDXYCisb0OgPn40crY5Ol +HMtMgVWHev+YqUevDpXq4DrYgQKIX4qhd3A== X-Received: by 2002:a05:6214:4b08:b0:6ce:d6c:499b with SMTP id 6a1803df08f44-6ce3427b2d2mr79501936d6.42.1729788994212; Thu, 24 Oct 2024 09:56:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG4neWev4q6Z4s9HREWkITl0xNMDbmLGQIm86O/0DXT0Uyol03xhdKJKN0gtBVqo6enK+iUNQ== X-Received: by 2002:a05:6214:4b08:b0:6ce:d6c:499b with SMTP id 6a1803df08f44-6ce3427b2d2mr79501606d6.42.1729788993831; 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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:32 -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_Le_Goat?= =?utf-8?q?er?= , Markus Armbruster , Eduardo Habkost , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Alex Williamson , Paolo Bonzini , Peter Maydell Subject: [PATCH 2/4] x86/iommu: Make x86-iommu a singleton object Date: Thu, 24 Oct 2024 12:56:25 -0400 Message-ID: <20241024165627.1372621-3-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X86 IOMMUs cannot be created more than one on a system yet. Make it a singleton so it guards the system from accidentally create yet another IOMMU object when one already presents. Now if someone tries to create more than one, e.g., via: ./qemu -M q35 -device intel-iommu -device intel-iommu The error will change from: qemu-system-x86_64: -device intel-iommu: QEMU does not support multiple vIOMMUs for x86 yet. To: qemu-system-x86_64: -device intel-iommu: Class 'intel-iommu' only supports one instance Unfortunately, yet we can't remove the singleton check in the machine hook (pc_machine_device_pre_plug_cb), because there can also be virtio-iommu involved, which doesn't share a common parent class yet. But with this, it should be closer to reach that goal to check singleton by QOM one day. Signed-off-by: Peter Xu --- hw/i386/x86-iommu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hw/i386/x86-iommu.c b/hw/i386/x86-iommu.c index 60af896225..4bfeb08705 100644 --- a/hw/i386/x86-iommu.c +++ b/hw/i386/x86-iommu.c @@ -26,6 +26,7 @@ #include "qemu/error-report.h" #include "trace.h" #include "sysemu/kvm.h" +#include "qom/object_interfaces.h" void x86_iommu_iec_register_notifier(X86IOMMUState *iommu, iec_notify_fn fn, void *data) @@ -133,10 +134,19 @@ static Property x86_iommu_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +static Object *x86_iommu_get_instance(Error **errp) +{ + return OBJECT(x86_iommu_get_default()); +} + static void x86_iommu_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); + SingletonClass *singleton = SINGLETON_CLASS(klass); + dc->realize = x86_iommu_realize; + singleton->get_instance = x86_iommu_get_instance; + device_class_set_props(dc, x86_iommu_properties); } @@ -152,6 +162,10 @@ static const TypeInfo x86_iommu_info = { .class_init = x86_iommu_class_init, .class_size = sizeof(X86IOMMUClass), .abstract = true, + .interfaces = (InterfaceInfo[]) { + { TYPE_SINGLETON }, + { } + } }; static void x86_iommu_register_types(void) From patchwork Thu Oct 24 16:56:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849423 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9365CE8E97 for ; Thu, 24 Oct 2024 16:58:12 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t419O-0003zg-Sv; Thu, 24 Oct 2024 12:57:33 -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 1t418b-0003u0-FS for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:42 -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 1t418Z-0002x3-Il for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729788998; 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=FhBOPpVeD7iY6TuhvhvNePic6DBQmZNqw1nKL+DgVvE=; b=Cu6C4CJk7qZoW/Fq+eQYjMlul75DwdFq/nIgmqftx1RTnoVsckdsXjUPmhSCO77kTKWOi5 fil6DxuzxJzIegySpTSumdTFqvsonR3GHH+M5f9g+pwess8Jcy2E/ilTqLB8XqKWhAlQl+ X1qha0yH1OZ6eLsUnWpvaztTM6vk75Y= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-154-ZnWsICb5NdCLim05iF6yNQ-1; Thu, 24 Oct 2024 12:56:37 -0400 X-MC-Unique: ZnWsICb5NdCLim05iF6yNQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6cbd12b8b60so48383456d6.0 for ; Thu, 24 Oct 2024 09:56:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729788996; x=1730393796; 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=FhBOPpVeD7iY6TuhvhvNePic6DBQmZNqw1nKL+DgVvE=; b=Cj8PxsuCgkDvN7pQQipFW19BzIa1PdEI/9g4DoQOQxB/UIJyDKmVBWomnrsmrmnwoK vYi28HVx6gE1kEh8injgF/vWUnj3Vd/4mD4dz//9cHrt8i5aZlZ9yk1sfCfCAhWE7Sl6 xxaElUOklXUDH0YXFQbbU6WVR8b/tnJhG9GIlo+1o0q1I+ESpLBWZC67QZRg31DzgkcA kB6rQ3sGXzHf/RWm5Ywq3bxDTZlhEVXm3b8GxHJe1mw3TK5Az9ZA4WxG4/KQPOu/Rji7 F5cJPMpFYxKb20kte4dojqDNb+KThSk5zVt4Je8ClceB8mm3U9zYp/RX6G+xM8WRhl/2 B4Cw== X-Gm-Message-State: AOJu0YwKSRuccNE+DuC9Uea+RraQKCBGhMAyfvhi0D0blzc6TPrYOqSm lhoUzANYKzB6sRo5Bu7yDjfv7Ad1CeTMA5tPV7fS6YQ2jHERJadriB1rag7FTdv6qo1/qnm4DsX NhgZymY1HhYw0oNMZ9QhqfoS48OemIFFpsb4ozYbpODkry7pE2nIaYMjpd4TO1huC284Zx7yukz 0wVdjXBiKthZliAxZGdHod9djA0H2a1Et6lw== X-Received: by 2002:a05:6214:449c:b0:6cb:f077:f2f7 with SMTP id 6a1803df08f44-6d090405805mr38226036d6.25.1729788996255; Thu, 24 Oct 2024 09:56:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEILwM8gPQuuRVNIvfejpUixJwOf0AOTdAVbVgV+KVZO5jm/Vc3z51bcXVSlC8K0c8vKLYi5w== X-Received: by 2002:a05:6214:449c:b0:6cb:f077:f2f7 with SMTP id 6a1803df08f44-6d090405805mr38225646d6.25.1729788995902; Thu, 24 Oct 2024 09:56:35 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:34 -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_Le_Goat?= =?utf-8?q?er?= , Markus Armbruster , Eduardo Habkost , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Alex Williamson , Paolo Bonzini , Peter Maydell Subject: [PATCH 3/4] migration: Make migration object a singleton object Date: Thu, 24 Oct 2024 12:56:26 -0400 Message-ID: <20241024165627.1372621-4-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This makes the migration object a singleton unit. After this, we can do something slightly tricky later on with the guarantee that nobody will be able to create the object twice. Signed-off-by: Peter Xu --- migration/migration.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/migration/migration.c b/migration/migration.c index bcb735869b..1b5285af95 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -45,6 +45,7 @@ #include "qapi/qmp/qerror.h" #include "qapi/qmp/qnull.h" #include "qemu/rcu.h" +#include "qom/object_interfaces.h" #include "postcopy-ram.h" #include "qemu/thread.h" #include "trace.h" @@ -3855,11 +3856,19 @@ fail: migrate_fd_cleanup(s); } +static Object* migration_get_instance(Error **errp) +{ + return OBJECT(current_migration); +} + static void migration_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); + SingletonClass *singleton = SINGLETON_CLASS(klass); dc->user_creatable = false; + singleton->get_instance = migration_get_instance; + device_class_set_props(dc, migration_properties); } @@ -3932,6 +3941,10 @@ static const TypeInfo migration_type = { .instance_size = sizeof(MigrationState), .instance_init = migration_instance_init, .instance_finalize = migration_instance_finalize, + .interfaces = (InterfaceInfo[]) { + { TYPE_SINGLETON }, + { } + } }; static void register_migration_types(void) From patchwork Thu Oct 24 16:56:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849424 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DA612CE8E99 for ; Thu, 24 Oct 2024 16:58:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t419R-00040E-En; Thu, 24 Oct 2024 12:57:33 -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 1t418i-0003ur-Ao for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:53 -0400 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 1t418d-0002xw-Km for qemu-devel@nongnu.org; Thu, 24 Oct 2024 12:56:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729789002; 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=3zbHXAQw1fGMrmr4E8SGLR2X4v65QDTd+QipXl5kc8s=; b=NCW1YsKM65k+ehHrVkgTPzekEXBAsjC5lBhprKKQ7U39BSuDcyZ7l6WC7o0KkIl33lyn7C dpYxH9zjXt6JfXa7yT32wzx6nk9DUk5MDZr63XEDgFAa8YF6+u6RSoxk1naU+QMCQY6qzD RauOzjftkiKi1o+S0TWk7rwtfvRji2I= 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-79-s3bjuv3CNLWI8lnBgtWfDA-1; Thu, 24 Oct 2024 12:56:39 -0400 X-MC-Unique: s3bjuv3CNLWI8lnBgtWfDA-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6cbeb6075d9so24144256d6.1 for ; Thu, 24 Oct 2024 09:56:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729788998; x=1730393798; 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=3zbHXAQw1fGMrmr4E8SGLR2X4v65QDTd+QipXl5kc8s=; b=PHgFmCOr8v0qOH8gUuuU0g4GyFp8wn6RimMDmnn/B1+bGSu4Twa2VcnfqzmRaOFkKL kYP5QkQoz0KQZ7tX1Qvk08JZwW74lvaJ4zY2zyMwXzoslydiCni0x4HscYwZK+ANF7h5 ZhvQKKj5cQ80tTOXQwg62+DErP5T3nibKHeO/YXOJK2M8bb/bV7Imqf4ejdIxDZDK/PT E2xUc/Q6vXG9LACVfY6jlvGN0s8udHzNvCIzBwvRQVqw0lu6iI8qrxl3Hfk7QsJxcLV4 aNj1ixASH7VPoLLi+aUeXQW6mOl5D1bBcyMg54cWZPcYWaJZpaH8ujSAbUWfmyAzMttZ bLsA== X-Gm-Message-State: AOJu0Yyq87CvFVUQs90nFgCsDKD7/VJzKNIoucGjAWfhGpRkP7LdCMUl MF4CS197mohPGrAviIRHAu9YO5Py5tQXElTYcl78xWfVTWNyMGRMEwZgfhLj7vz8tghHb29Y2IL WUrrVwY5IP1cZWxCtgIyR4CACSy81/VxOgvUUiddyunCehqccR4+BXk8sqWnEe/qBWYzJnsOhb4 ATKpgCagapoDgOS5vWaD/nRFk3+WHYVJtRSw== X-Received: by 2002:a05:6214:2d43:b0:6cb:e9da:bd4 with SMTP id 6a1803df08f44-6d07a5fff7cmr43867806d6.5.1729788998172; Thu, 24 Oct 2024 09:56:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7koPn1D5NP5mkRs5TPr43VbcvjnUva8aXN51jCV6jnfnoJHJjFTcvoeya0Hfi/Yh13b7daw== X-Received: by 2002:a05:6214:2d43:b0:6cb:e9da:bd4 with SMTP id 6a1803df08f44-6d07a5fff7cmr43867386d6.5.1729788997806; Thu, 24 Oct 2024 09:56:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:36 -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_Le_Goat?= =?utf-8?q?er?= , Markus Armbruster , Eduardo Habkost , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Alex Williamson , Paolo Bonzini , Peter Maydell Subject: [PATCH 4/4] migration: Reset current_migration properly Date: Thu, 24 Oct 2024 12:56:27 -0400 Message-ID: <20241024165627.1372621-5-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 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 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_H2=-1.697, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org current_migration is never reset, even if the migration object is freed already. It means anyone references that can trigger UAF and it'll be hard to debug. Properly clear the pointer now, so far the only way to do is via finalize() as we know there's only one instance of it, meanwhile QEMU won't know who holds the refcount, so it can't reset the variable manually but only in finalize(). To make it more readable, also initialize the variable in the instance_init() so it's very well paired at least. Signed-off-by: Peter Xu --- migration/migration.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 1b5285af95..74812ca785 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -233,9 +233,11 @@ static int migration_stop_vm(MigrationState *s, RunState state) void migration_object_init(void) { - /* This can only be called once. */ - assert(!current_migration); - current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); + /* This creates the singleton migration object */ + object_new(TYPE_MIGRATION); + + /* This should be set now when initialize the singleton object */ + assert(current_migration); /* * Init the migrate incoming object as well no matter whether @@ -3886,12 +3888,27 @@ static void migration_instance_finalize(Object *obj) qemu_sem_destroy(&ms->rp_state.rp_pong_acks); qemu_sem_destroy(&ms->postcopy_qemufile_src_sem); error_free(ms->error); + + /* + * We know we only have one intance of migration, and when reaching + * here it means migration object is gone. Clear the global reference + * to reflect that. + */ + current_migration = NULL; } static void migration_instance_init(Object *obj) { MigrationState *ms = MIGRATION_OBJ(obj); + /* + * There can only be one migration object globally. Keep a record of + * the pointer in current_migration, which will be reset after the + * object finalize(). + */ + assert(!current_migration); + current_migration = ms; + ms->state = MIGRATION_STATUS_NONE; ms->mbps = -1; ms->pages_per_second = -1;