From nobody Tue Feb 10 23:12:23 2026 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=1649955323; cv=none; d=zohomail.com; s=zohoarc; b=a33vm6/LD5t9AmhjD8JTUwX6nqkXNDIxAkOOR6ghR0paU/cmnlYncQT7nwyJ2hTWPfag/6AR2v3I6fDpwwBY7rGyDKr2gSn79fIzyHFRPhLsyj2jfgJKPuyPtzGPcE+9uhVAELt/rPd5YyhoyffTpE4wc/FOBHQ2LTIrbcVRTgg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1649955323; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hjzHbo/x3OYy4t2hYx0utDmUA9Po6wcLkDqSpAtS58s=; b=afJH9Scb+FJBfzuT+jAAvSqCfPgqNHI+BQsyusWoPxuEnvmXsn3kpQIvAwU6H1eLURSHt2YqIFn2+LKDBHxLKJrBhzy+hUSyzXOtp1FwaSs88RzaglDAAIW6mjOaUr1ijfQkWCEhWFAH3BhpDZVTTFgYQk/FPJH6PraDarAAszk= 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 1649955323317632.6914982983519; Thu, 14 Apr 2022 09:55:23 -0700 (PDT) Received: from localhost ([::1]:34400 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nf2kb-0005Te-Tb for importer@patchew.org; Thu, 14 Apr 2022 12:55:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40850) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nf2iU-0002he-74 for qemu-devel@nongnu.org; Thu, 14 Apr 2022 12:53:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:38296) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nf2iO-000482-AY for qemu-devel@nongnu.org; Thu, 14 Apr 2022 12:53:07 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-461-XIbm9pjuM1KXtjPNJwLExQ-1; Thu, 14 Apr 2022 12:53:02 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 10CF43838C8B for ; Thu, 14 Apr 2022 16:53:02 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id F234E14583C1 for ; Thu, 14 Apr 2022 16:53:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649955183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hjzHbo/x3OYy4t2hYx0utDmUA9Po6wcLkDqSpAtS58s=; b=H8P0hB+ORaGabL1tAFHuagU3+fhdV1GajREzG+jCt6RNaT9fZpvkOJXvNI3PROawNETEe9 eGy2a43hZbigLhHChd5YbjlyBkzKJO5EsRq5xCdJmWOKUFohc6/dsVhwbB4meSReozfxdP IJS07RnC6wh6ZALAdAVN2NkavmpSy/c= X-MC-Unique: XIbm9pjuM1KXtjPNJwLExQ-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH for-7.1 4/5] machine: make memory-backend a link property Date: Thu, 14 Apr 2022 12:52:59 -0400 Message-Id: <20220414165300.555321-5-pbonzini@redhat.com> In-Reply-To: <20220414165300.555321-1-pbonzini@redhat.com> References: <20220414165300.555321-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1649955325469100007 Content-Type: text/plain; charset="utf-8" Handle HostMemoryBackend creation and setting of ms->ram entirely in machine_run_board_init. Signed-off-by: Paolo Bonzini --- hw/core/machine.c | 70 ++++++++++++++++++++++++++++++--------------- hw/core/numa.c | 2 +- hw/sparc/sun4m.c | 5 ++-- include/hw/boards.h | 4 +-- softmmu/vl.c | 62 ++++++++++++++------------------------- 5 files changed, 74 insertions(+), 69 deletions(-) diff --git a/hw/core/machine.c b/hw/core/machine.c index d5cfffe8a0..3dca927e3f 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -36,6 +36,7 @@ #include "exec/confidential-guest-support.h" #include "hw/virtio/virtio.h" #include "hw/virtio/virtio-pci.h" +#include "qom/object_interfaces.h" =20 GlobalProperty hw_compat_6_2[] =3D { { "PIIX4_PM", "x-not-migrate-acpi-index", "on"}, @@ -650,21 +651,6 @@ bool device_type_is_dynamic_sysbus(MachineClass *mc, c= onst char *type) return allowed; } =20 -static char *machine_get_memdev(Object *obj, Error **errp) -{ - MachineState *ms =3D MACHINE(obj); - - return g_strdup(ms->ram_memdev_id); -} - -static void machine_set_memdev(Object *obj, const char *value, Error **err= p) -{ - MachineState *ms =3D MACHINE(obj); - - g_free(ms->ram_memdev_id); - ms->ram_memdev_id =3D g_strdup(value); -} - HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) { int i; @@ -1007,8 +993,9 @@ static void machine_class_init(ObjectClass *oc, void *= data) object_class_property_set_description(oc, "memory-encryption", "Set memory encryption object to use"); =20 - object_class_property_add_str(oc, "memory-backend", - machine_get_memdev, machine_set_memdev); + object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKE= ND, + offsetof(MachineState, memdev), object_= property_allow_set_link, + OBJ_PROP_LINK_STRONG); object_class_property_set_description(oc, "memory-backend", "Set RAM backend" "Valid value is ID of hostmem ba= sed backend"); @@ -1252,7 +1239,40 @@ MemoryRegion *machine_consume_memdev(MachineState *m= achine, return ret; } =20 -void machine_run_board_init(MachineState *machine) +static bool create_default_memdev(MachineState *ms, const char *path, Erro= r **errp) +{ + Object *obj; + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + bool r =3D false; + + obj =3D object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKE= ND_RAM); + if (path) { + if (!object_property_set_str(obj, "mem-path", path, errp)) { + goto out; + } + } + if (!object_property_set_int(obj, "size", ms->ram_size, errp)) { + goto out; + } + object_property_add_child(object_get_objects_root(), mc->default_ram_i= d, + obj); + /* Ensure backend's memory region name is equal to mc->default_ram_id = */ + if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-= id", + false, errp)) { + goto out; + } + if (!user_creatable_complete(USER_CREATABLE(obj), errp)) { + goto out; + } + r =3D object_property_set_link(OBJECT(ms), "memory-backend", obj, errp= ); + +out: + object_unref(obj); + return r; +} + + +void machine_run_board_init(MachineState *machine, const char *mem_path, E= rror **errp) { MachineClass *machine_class =3D MACHINE_GET_CLASS(machine); ObjectClass *oc =3D object_class_by_name(machine->cpu_type); @@ -1263,11 +1283,11 @@ void machine_run_board_init(MachineState *machine) clock values from the log. */ replay_checkpoint(CHECKPOINT_INIT); =20 - if (machine->ram_memdev_id) { - Object *o; - o =3D object_resolve_path_type(machine->ram_memdev_id, - TYPE_MEMORY_BACKEND, NULL); - machine->ram =3D machine_consume_memdev(machine, MEMORY_BACKEND(o)= ); + if (machine_class->default_ram_id && machine->ram_size && + numa_uses_legacy_mem() && !machine->memdev) { + if (!create_default_memdev(current_machine, mem_path, errp)) { + return; + } } =20 if (machine->numa_state) { @@ -1277,6 +1297,10 @@ void machine_run_board_init(MachineState *machine) } } =20 + if (!machine->ram && machine->memdev) { + machine->ram =3D machine_consume_memdev(machine, machine->memdev); + } + /* If the machine supports the valid_cpu_types check and the user * specified a CPU with -cpu check here that the user CPU is supported. */ diff --git a/hw/core/numa.c b/hw/core/numa.c index 1aa05dcf42..26d8e5f616 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -695,7 +695,7 @@ void numa_complete_configuration(MachineState *ms) } =20 if (!numa_uses_legacy_mem() && mc->default_ram_id) { - if (ms->ram_memdev_id) { + if (ms->memdev) { error_report("'-machine memory-backend' and '-numa memdev'" " properties are mutually exclusive"); exit(1); diff --git a/hw/sparc/sun4m.c b/hw/sparc/sun4m.c index fb58e19e4b..98a94a87d5 100644 --- a/hw/sparc/sun4m.c +++ b/hw/sparc/sun4m.c @@ -832,8 +832,7 @@ static void sun4m_hw_init(MachineState *machine) SysBusDevice *s; unsigned int smp_cpus =3D machine->smp.cpus; unsigned int max_cpus =3D machine->smp.max_cpus; - Object *ram_memdev =3D object_resolve_path_type(machine->ram_memdev_id, - TYPE_MEMORY_BACKEND, NUL= L); + HostMemoryBackend *ram_memdev =3D machine->memdev; NICInfo *nd =3D &nd_table[0]; =20 if (machine->ram_size > hwdef->max_mem) { @@ -853,7 +852,7 @@ static void sun4m_hw_init(MachineState *machine) =20 /* Create and map RAM frontend */ dev =3D qdev_new("memory"); - object_property_set_link(OBJECT(dev), "memdev", ram_memdev, &error_fat= al); + object_property_set_link(OBJECT(dev), "memdev", OBJECT(ram_memdev), &e= rror_fatal); sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0); =20 diff --git a/include/hw/boards.h b/include/hw/boards.h index 8994f6c93b..3390de80ff 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -25,7 +25,7 @@ OBJECT_DECLARE_TYPE(MachineState, MachineClass, MACHINE) =20 extern MachineState *current_machine; =20 -void machine_run_board_init(MachineState *machine); +void machine_run_board_init(MachineState *machine, const char *mem_path, E= rror **errp); bool machine_usb(MachineState *machine); int machine_phandle_start(MachineState *machine); bool machine_dump_guest_core(MachineState *machine); @@ -339,7 +339,7 @@ struct MachineState { bool suppress_vmdesc; bool enable_graphics; ConfidentialGuestSupport *cgs; - char *ram_memdev_id; + HostMemoryBackend *memdev; /* * convenience alias to ram_memdev_id backend memory region * or to numa container memory region diff --git a/softmmu/vl.c b/softmmu/vl.c index c908c3af95..03494760d5 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -157,6 +157,7 @@ static const char *incoming; static const char *loadvm; static const char *accelerators; static bool have_custom_ram_size; +static const char *ram_memdev_id; static QDict *machine_opts_dict; static QTAILQ_HEAD(, ObjectOption) object_opts =3D QTAILQ_HEAD_INITIALIZER= (object_opts); static QTAILQ_HEAD(, DeviceOption) device_opts =3D QTAILQ_HEAD_INITIALIZER= (device_opts); @@ -1762,6 +1763,19 @@ static void qemu_apply_legacy_machine_options(QDict = *qdict) qdict_del(qdict, "kernel-irqchip"); } =20 + value =3D qdict_get_try_str(qdict, "memory-backend"); + if (value) { + if (mem_path) { + error_report("'-mem-path' can't be used together with" + "'-machine memory-backend'"); + exit(EXIT_FAILURE); + } + + /* Resolved later. */ + ram_memdev_id =3D g_strdup(value); + qdict_del(qdict, "memory-backend"); + } + prop =3D qdict_get(qdict, "memory"); if (prop) { have_custom_ram_size =3D @@ -1997,29 +2011,25 @@ static void qemu_create_late_backends(void) =20 static void qemu_resolve_machine_memdev(void) { - if (current_machine->ram_memdev_id) { + if (ram_memdev_id) { Object *backend; ram_addr_t backend_size; =20 - backend =3D object_resolve_path_type(current_machine->ram_memdev_i= d, + backend =3D object_resolve_path_type(ram_memdev_id, TYPE_MEMORY_BACKEND, NULL); if (!backend) { - error_report("Memory backend '%s' not found", - current_machine->ram_memdev_id); + error_report("Memory backend '%s' not found", ram_memdev_id); exit(EXIT_FAILURE); } backend_size =3D object_property_get_uint(backend, "size", &error= _abort); if (have_custom_ram_size && backend_size !=3D current_machine->ram= _size) { - error_report("Size specified by -m option must match size = of " - "explicitly specified 'memory-backend' proper= ty"); - exit(EXIT_FAILURE); - } - if (mem_path) { - error_report("'-mem-path' can't be used together with" - "'-machine memory-backend'"); + error_report("Size specified by -m option must match size of " + "explicitly specified 'memory-backend' property"); exit(EXIT_FAILURE); } current_machine->ram_size =3D backend_size; + object_property_set_link(OBJECT(current_machine), + "memory-backend", backend, &error_fatal); } =20 if (!xen_enabled()) { @@ -2370,27 +2380,6 @@ static void configure_accelerators(const char *progn= ame) } } =20 -static void create_default_memdev(MachineState *ms, const char *path) -{ - Object *obj; - MachineClass *mc =3D MACHINE_GET_CLASS(ms); - - obj =3D object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKE= ND_RAM); - if (path) { - object_property_set_str(obj, "mem-path", path, &error_fatal); - } - object_property_set_int(obj, "size", ms->ram_size, &error_fatal); - object_property_add_child(object_get_objects_root(), mc->default_ram_i= d, - obj); - /* Ensure backend's memory region name is equal to mc->default_ram_id = */ - object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id", - false, &error_fatal); - user_creatable_complete(USER_CREATABLE(obj), &error_fatal); - object_unref(obj); - object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_= id, - &error_fatal); -} - static void qemu_validate_options(const QDict *machine_opts) { const char *kernel_filename =3D qdict_get_try_str(machine_opts, "kerne= l"); @@ -2578,18 +2567,11 @@ static void qemu_init_displays(void) =20 static void qemu_init_board(void) { - MachineClass *machine_class =3D MACHINE_GET_CLASS(current_machine); - - if (machine_class->default_ram_id && current_machine->ram_size && - numa_uses_legacy_mem() && !current_machine->ram_memdev_id) { - create_default_memdev(current_machine, mem_path); - } - /* process plugin before CPUs are created, but once -smp has been pars= ed */ qemu_plugin_load_list(&plugin_list, &error_fatal); =20 /* From here on we enter MACHINE_PHASE_INITIALIZED. */ - machine_run_board_init(current_machine); + machine_run_board_init(current_machine, mem_path, &error_fatal); =20 drive_check_orphaned(); =20 --=20 2.31.1