From nobody Sat Nov 23 17:40:17 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 From nobody Sat Nov 23 17:40:17 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=1729789092; cv=none; d=zohomail.com; s=zohoarc; b=EZYD1zVZb7vzI63Tfws/xjWCxbUBJlR7ij8MV9hLuwq4ISPoCoCD3DoiywpeLwhV6BS/DQ6eW02QgHCAfZWMzSgJuxq0OmCtoP+gfJqkJdYsq5I3EE8Z12B8pSWbJY/eKXppeTGJRP6Bx3m9Gwtxqrt0nDNH6ot391QsqYkzMEw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729789092; 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=c0kLRIPNRsyiUEIuptSmNc8rruH765h3xOHpyL+r4uw=; b=KjFK7sLxcY4pnRjz5/JtpFv+9Tfq4pzA4GVkexJxDJPmwpKqtsLZm3V7x7Xr4EO6aBp9/Dy7DG4FD35JqwDlr5Vk63a0Kd8cx5Z/NV0dzl4DpZqgT+jUmI8rDzjloeOuiGYJODYBXyD0GRiF4XAtAwvIUiJ1C4lMu13lrVs0Vl4= 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 1729789092974743.7774983525834; Thu, 24 Oct 2024 09:58:12 -0700 (PDT) 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 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 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6cdf0589e8aso15307156d6.3 for ; 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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:32 -0700 (PDT) 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= X-MC-Unique: fAtS33IKPi63denm6uL7yw-1 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) 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 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 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: 1729789094511116600 Content-Type: text/plain; charset="utf-8" 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 v= IOMMUs for x86 yet. To: qemu-system-x86_64: -device intel-iommu: Class 'intel-iommu' only support= s 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" =20 void x86_iommu_iec_register_notifier(X86IOMMUState *iommu, iec_notify_fn fn, void *data) @@ -133,10 +134,19 @@ static Property x86_iommu_properties[] =3D { DEFINE_PROP_END_OF_LIST(), }; =20 +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 =3D DEVICE_CLASS(klass); + SingletonClass *singleton =3D SINGLETON_CLASS(klass); + dc->realize =3D x86_iommu_realize; + singleton->get_instance =3D x86_iommu_get_instance; + device_class_set_props(dc, x86_iommu_properties); } =20 @@ -152,6 +162,10 @@ static const TypeInfo x86_iommu_info =3D { .class_init =3D x86_iommu_class_init, .class_size =3D sizeof(X86IOMMUClass), .abstract =3D true, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_SINGLETON }, + { } + } }; =20 static void x86_iommu_register_types(void) --=20 2.45.0 From nobody Sat Nov 23 17:40:17 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=1729789092; cv=none; d=zohomail.com; s=zohoarc; b=FDNq0qmIfbq9WsWftJm3kfMtbL1nN6Lr3BtvAhdwk70Kaq5ZkmbhwDyExC1jxf1MoBAWGd16VbeLkmCHZ0Z7qjLR3zyhBeOoxNFUynzDSZpF8WZunUuDzSOv8rWI9edGy4grGe1Xp75w25DcfVozR60bANOhsXHmWX2babo08nc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729789092; 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=FhBOPpVeD7iY6TuhvhvNePic6DBQmZNqw1nKL+DgVvE=; b=nyhirtej2TgUEB2vtfi0O8H6J25tHVwacPDyLH9KsvegohVOq9zHsD5TrDmZy8sQd/RLflOS3nR7Stl1b2vjPy2A7R6zBFknUGnv61szSNrWgJBOoCL46B9W0VZLAmcD22N63p8QnMiOwTcRxDh/rSpwRF5rDZ8FeouIC/bOZ0c= 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 1729789092974125.98576664838527; Thu, 24 Oct 2024 09:58:12 -0700 (PDT) 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 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 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6cbd12b8b60so48383456d6.0 for ; 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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 09:56:34 -0700 (PDT) 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= X-MC-Unique: ZnWsICb5NdCLim05iF6yNQ-1 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) 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 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 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: 1729789094489116600 Content-Type: text/plain; charset="utf-8" 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); } =20 +static Object* migration_get_instance(Error **errp) +{ + return OBJECT(current_migration); +} + static void migration_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); + SingletonClass *singleton =3D SINGLETON_CLASS(klass); =20 dc->user_creatable =3D false; + singleton->get_instance =3D migration_get_instance; + device_class_set_props(dc, migration_properties); } =20 @@ -3932,6 +3941,10 @@ static const TypeInfo migration_type =3D { .instance_size =3D sizeof(MigrationState), .instance_init =3D migration_instance_init, .instance_finalize =3D migration_instance_finalize, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_SINGLETON }, + { } + } }; =20 static void register_migration_types(void) --=20 2.45.0 From nobody Sat Nov 23 17:40:17 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=1729789121; cv=none; d=zohomail.com; s=zohoarc; b=Ufpzn/SJpbdL/xtKFNDIwK/fg9tmMM2oN8y4kJ+zymLh/zkIG8XUX4C2oapo8z5rBRlaYqerdKqUgPBYb0tT8S0wEmZcBxhLI1r9ySp6bFJdXib1d3+O8JmYAGYoyor6L5edFC1ooxJtLARTtafsbHi0pycLMEWL7K0jVvZh6Uo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729789121; 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=3zbHXAQw1fGMrmr4E8SGLR2X4v65QDTd+QipXl5kc8s=; b=Lpkc9lUEOtFphYqnaJbRfL+JDrt5nbwv66DGvBRMJLYMSOExVAdL0N/XcW0uKXy8fBSVzjluvnrgC9XbB3Y5NT6KDJgYWoqxwXoaeukTwmPKf02vk27Em2rBjgrFLL21H3AWixt4sNG6Ja4F76G8MZ/kGuIEGgb5CTUEXYPpEc8= 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 1729789121027758.3652239123829; Thu, 24 Oct 2024 09:58:41 -0700 (PDT) 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 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 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6cbeb6075d9so24144256d6.1 for ; Thu, 24 Oct 2024 09:56:39 -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) 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= X-MC-Unique: s3bjuv3CNLWI8lnBgtWfDA-1 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) 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 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 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.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1729789122656116600 Content-Type: text/plain; charset="utf-8" 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, RunSta= te state) =20 void migration_object_init(void) { - /* This can only be called once. */ - assert(!current_migration); - current_migration =3D 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); =20 /* * 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 =3D NULL; } =20 static void migration_instance_init(Object *obj) { MigrationState *ms =3D MIGRATION_OBJ(obj); =20 + /* + * 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 =3D ms; + ms->state =3D MIGRATION_STATUS_NONE; ms->mbps =3D -1; ms->pages_per_second =3D -1; --=20 2.45.0