From nobody Sat May 18 09:01:37 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=1684868002; cv=none; d=zohomail.com; s=zohoarc; b=Bk6ULsDK7e+PQHPts4Brt2WdusjIp3TA5R7qWEVM/4ITLgZRNLwuDyiVJgSTwIJXNetWUSuQcRE3ZR9wd/9++DA+7ZYEHcFDH3iVXiDprVmKKmevzAQwxCVR9wporc90sl6nazDLaGcTrP6tfgos5ENjHDMpBA8qAcWhKE83vbs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684868002; h=Content-Transfer-Encoding:Cc: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=2cRfgMgmo16t8N/GUHnwSkZs9pSLVamwaBvt1cmft3w=; b=kyaJJ2gBuUBaaoqLIsOakXLCFfd0k4/V9p1qWRvVAd4RI3SM72iFolRQpGW7lKli+ecH3798GVPKYwg33XUqPvqQ878lhPbxc0aJ3INsGwHO9ZE2Escgoo7laNLNdkmT/cAB0ojgxP0jg7mKONrVYVZuoIkDTv+W6NOiVzOIGOc= 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 1684868002989771.9667889233585; Tue, 23 May 2023 11:53:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q1X76-0007Hn-FP; Tue, 23 May 2023 14:52:04 -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 1q1X73-0007Gs-Br for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:03 -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 1q1X71-0000cr-UA for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:01 -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-385-lfi196cxPQOFU29eavavVw-1; Tue, 23 May 2023 14:51:55 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85CF12932484; Tue, 23 May 2023 18:51:54 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.194.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6472040CFD45; Tue, 23 May 2023 18:51:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684867919; 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=2cRfgMgmo16t8N/GUHnwSkZs9pSLVamwaBvt1cmft3w=; b=ANNAWxOec9HKtFCe47z3LETMsHvvv1I8QDhaLp6sFBausU3b4+tvYLBkMKYSdYw46lzPpj lcf5xnUxYYRJNLxvEGygwe32v10LgSf9yHDAtfjkovgqe3ONgFLx3zFnqFIW8Laj/o2hoy fpl4Q34spRabtGRNP2SnPilYTpAgqN4= X-MC-Unique: lfi196cxPQOFU29eavavVw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-ppc@nongnu.org, David Hildenbrand , Igor Mammedov , Xiao Guangrong , Peter Maydell , "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Xiaojuan Yang , Song Gao , Daniel Henrique Barboza , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , David Gibson , Greg Kurz , Harsh Prateek Bora , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang Subject: [PATCH v1 1/3] memory-device: Refactor memory_device_pre_plug() Date: Tue, 23 May 2023 20:51:42 +0200 Message-Id: <20230523185144.533592-2-david@redhat.com> In-Reply-To: <20230523185144.533592-1-david@redhat.com> References: <20230523185144.533592-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684868003376100005 Content-Type: text/plain; charset="utf-8" Let's move memory_device_check_addable() and basic checks out of memory_device_get_free_addr() directly into memory_device_pre_plug(). Separating basic checks from address assignment is cleaner and prepares for further changes. Signed-off-by: David Hildenbrand --- hw/mem/memory-device.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 1636db9679..6c025b02c1 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -68,9 +68,10 @@ static int memory_device_used_region_size(Object *obj, v= oid *opaque) return 0; } =20 -static void memory_device_check_addable(MachineState *ms, uint64_t size, +static void memory_device_check_addable(MachineState *ms, MemoryRegion *mr, Error **errp) { + const uint64_t size =3D memory_region_size(mr); uint64_t used_region_size =3D 0; =20 /* we will need a new memory slot for kvm and vhost */ @@ -100,21 +101,9 @@ static uint64_t memory_device_get_free_addr(MachineSta= te *ms, uint64_t align, uint64_t size, Error **errp) { - Error *err =3D NULL; GSList *list =3D NULL, *item; Range as, new =3D range_empty; =20 - if (!ms->device_memory) { - error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " - "supported by the machine"); - return 0; - } - - if (!memory_region_size(&ms->device_memory->mr)) { - error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " - "enabled, please specify the maxmem option"); - return 0; - } range_init_nofail(&as, ms->device_memory->base, memory_region_size(&ms->device_memory->mr)); =20 @@ -126,12 +115,6 @@ static uint64_t memory_device_get_free_addr(MachineSta= te *ms, align); } =20 - memory_device_check_addable(ms, size, &err); - if (err) { - error_propagate(errp, err); - return 0; - } - if (hint && !QEMU_IS_ALIGNED(*hint, align)) { error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", align); @@ -255,11 +238,28 @@ void memory_device_pre_plug(MemoryDeviceState *md, Ma= chineState *ms, uint64_t addr, align =3D 0; MemoryRegion *mr; =20 + if (!ms->device_memory) { + error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " + "supported by the machine"); + return; + } + + if (!memory_region_size(&ms->device_memory->mr)) { + error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " + "enabled, please specify the maxmem option"); + return; + } + mr =3D mdc->get_memory_region(md, &local_err); if (local_err) { goto out; } =20 + memory_device_check_addable(ms, mr, &local_err); + if (local_err) { + goto out; + } + if (legacy_align) { align =3D *legacy_align; } else { --=20 2.40.1 From nobody Sat May 18 09:01:37 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=1684868003; cv=none; d=zohomail.com; s=zohoarc; b=iN+vgDmjhSJ7Izm+pnN+ZQq8OIV/t7EUvHcXYaGyhsPGNPJAg5QZYzeG5RJwHnm9br4v0GWNTyyGCD+kVnj+yjfr/MqYTv0lY8HmBvF/ILSMUyyUPF+ms7Xq13SCAQ+39H9TA4opGeoz4CwiNAHgrsuFlNBh6JvHX3aOe4NdRo4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684868003; h=Content-Transfer-Encoding:Cc: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=tqgoMx3VGq/IcEUJhCLlD0ORcUiqe8OqGgyCdOS0Zyo=; b=d5S4302NGzesCj/gcYdz66jpx4T5bn0vfm1j08D79daoOUeIuwysPIfNu64g+S4rb06wAGirxTI5fCZq9VQAFMAqeOD0hAZMtIyRxarXQvIojWSfIz8/oGBE5WnXppFTjaG6u+YjaEgk4hgEe2s+8XXnsF2fZOzz7GHO2qHg/gY= 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 1684868003608610.7858286515755; Tue, 23 May 2023 11:53:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q1X7A-0007RL-QS; Tue, 23 May 2023 14:52:08 -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 1q1X78-0007JA-L2 for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:06 -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 1q1X76-0000dZ-6m for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:06 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-182-as8nrjWUM-mq_PVVRPnJVw-1; Tue, 23 May 2023 14:51:59 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4AF2685A5BE; Tue, 23 May 2023 18:51:58 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.194.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id C10BB40CFD45; Tue, 23 May 2023 18:51:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684867922; 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=tqgoMx3VGq/IcEUJhCLlD0ORcUiqe8OqGgyCdOS0Zyo=; b=AukMM1j+TcdsYoy1M4fh0s/+Cfamg17aJZvOYfFAxXw2RGH2lTvHSLkZyVIlRWIErOfxDw NRDNWu6v238j4QHBJTx/rfLQFecInMZfXODqBhVXjhI7a9FbvO2RZZp0DRngt7RQ8lm57y RFReGFH1fqa8g1G/1R1ABjlR48Q2lkc= X-MC-Unique: as8nrjWUM-mq_PVVRPnJVw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-ppc@nongnu.org, David Hildenbrand , Igor Mammedov , Xiao Guangrong , Peter Maydell , "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Xiaojuan Yang , Song Gao , Daniel Henrique Barboza , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , David Gibson , Greg Kurz , Harsh Prateek Bora , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang Subject: [PATCH v1 2/3] memory-device: Factor out device memory initialization into memory_devices_init() Date: Tue, 23 May 2023 20:51:43 +0200 Message-Id: <20230523185144.533592-3-david@redhat.com> In-Reply-To: <20230523185144.533592-1-david@redhat.com> References: <20230523185144.533592-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: 1684868005580100015 Content-Type: text/plain; charset="utf-8" Let's factor the common setup out, to prepare for further changes. On arm64, we'll add the subregion to system RAM now earlier -- which shouldn't matter, because the system RAM memory region should already be alive at that point. Signed-off-by: David Hildenbrand Tested-by: Song Gao --- hw/arm/virt.c | 9 +-------- hw/i386/pc.c | 17 ++++++----------- hw/loongarch/virt.c | 14 ++++---------- hw/mem/memory-device.c | 20 ++++++++++++++++++++ hw/ppc/spapr.c | 15 ++++++--------- include/hw/mem/memory-device.h | 2 ++ 6 files changed, 39 insertions(+), 38 deletions(-) diff --git a/hw/arm/virt.c b/hw/arm/virt.c index b99ae18501..284f45d998 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -1816,10 +1816,7 @@ static void virt_set_memmap(VirtMachineState *vms, i= nt pa_bits) virt_set_high_memmap(vms, base, pa_bits); =20 if (device_memory_size > 0) { - ms->device_memory =3D g_malloc0(sizeof(*ms->device_memory)); - ms->device_memory->base =3D device_memory_base; - memory_region_init(&ms->device_memory->mr, OBJECT(vms), - "device-memory", device_memory_size); + memory_devices_init(ms, device_memory_base, device_memory_size); } } =20 @@ -2260,10 +2257,6 @@ static void machvirt_init(MachineState *machine) =20 memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, machine->ram); - if (machine->device_memory) { - memory_region_add_subregion(sysmem, machine->device_memory->base, - &machine->device_memory->mr); - } =20 virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem); =20 diff --git a/hw/i386/pc.c b/hw/i386/pc.c index bb62c994fa..9d215df92e 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1039,13 +1039,11 @@ void pc_memory_init(PCMachineState *pcms, exit(EXIT_FAILURE); } =20 - /* always allocate the device memory information */ - machine->device_memory =3D g_malloc0(sizeof(*machine->device_memory)); - /* initialize device memory address space */ if (pcmc->has_reserved_memory && (machine->ram_size < machine->maxram_size)) { ram_addr_t device_mem_size; + hwaddr device_mem_base; =20 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) { error_report("unsupported amount of memory slots: %"PRIu64, @@ -1060,19 +1058,16 @@ void pc_memory_init(PCMachineState *pcms, exit(EXIT_FAILURE); } =20 - pc_get_device_memory_range(pcms, &machine->device_memory->base, &d= evice_mem_size); + pc_get_device_memory_range(pcms, &device_mem_base, &device_mem_siz= e); =20 - if ((machine->device_memory->base + device_mem_size) < - device_mem_size) { + if (device_mem_base + device_mem_size < device_mem_size) { error_report("unsupported amount of maximum memory: " RAM_ADDR= _FMT, machine->maxram_size); exit(EXIT_FAILURE); } - - memory_region_init(&machine->device_memory->mr, OBJECT(pcms), - "device-memory", device_mem_size); - memory_region_add_subregion(system_memory, machine->device_memory-= >base, - &machine->device_memory->mr); + memory_devices_init(machine, device_mem_base, device_mem_size); + } else { + memory_devices_init(machine, 0, 0); } =20 if (pcms->cxl_devices_state.is_enabled) { diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c index 2b7588e32a..2ccc90feb4 100644 --- a/hw/loongarch/virt.c +++ b/hw/loongarch/virt.c @@ -45,7 +45,7 @@ #include "sysemu/block-backend.h" #include "hw/block/flash.h" #include "qemu/error-report.h" - +#include "hw/mem/memory-device.h" =20 static void virt_flash_create(LoongArchMachineState *lams) { @@ -804,8 +804,8 @@ static void loongarch_init(MachineState *machine) =20 /* initialize device memory address space */ if (machine->ram_size < machine->maxram_size) { - machine->device_memory =3D g_malloc0(sizeof(*machine->device_memor= y)); ram_addr_t device_mem_size =3D machine->maxram_size - machine->ram= _size; + hwaddr device_mem_base; =20 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) { error_report("unsupported amount of memory slots: %"PRIu64, @@ -820,14 +820,8 @@ static void loongarch_init(MachineState *machine) exit(EXIT_FAILURE); } /* device memory base is the top of high memory address. */ - machine->device_memory->base =3D 0x90000000 + highram_size; - machine->device_memory->base =3D - ROUND_UP(machine->device_memory->base, 1 * GiB); - - memory_region_init(&machine->device_memory->mr, OBJECT(lams), - "device-memory", device_mem_size); - memory_region_add_subregion(address_space_mem, machine->device_mem= ory->base, - &machine->device_memory->mr); + device_mem_base =3D ROUND_UP(0x90000000 + highram_size, 1 * GiB); + memory_devices_init(machine, device_mem_base, device_mem_size); } =20 /* Add isa io region */ diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 6c025b02c1..d99ceb621a 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -17,6 +17,7 @@ #include "qemu/range.h" #include "hw/virtio/vhost.h" #include "sysemu/kvm.h" +#include "exec/address-spaces.h" #include "trace.h" =20 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b) @@ -333,6 +334,25 @@ uint64_t memory_device_get_region_size(const MemoryDev= iceState *md, return memory_region_size(mr); } =20 +void memory_devices_init(MachineState *ms, hwaddr base, uint64_t size) +{ + g_assert(!ms->device_memory); + ms->device_memory =3D g_new0(DeviceMemoryState, 1); + ms->device_memory->base =3D base; + + /* + * See memory_device_get_free_addr(): An empty device memory region + * means "this machine supports memory devices, but they are not enabl= ed". + */ + if (size > 0) { + memory_region_init(&ms->device_memory->mr, OBJECT(ms), "device-mem= ory", + size); + memory_region_add_subregion(get_system_memory(), + ms->device_memory->base, + &ms->device_memory->mr); + } +} + static const TypeInfo memory_device_info =3D { .name =3D TYPE_MEMORY_DEVICE, .parent =3D TYPE_INTERFACE, diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 1baea16c96..d66dc00ea5 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -2866,12 +2866,11 @@ static void spapr_machine_init(MachineState *machin= e) /* map RAM */ memory_region_add_subregion(sysmem, 0, machine->ram); =20 - /* always allocate the device memory information */ - machine->device_memory =3D g_malloc0(sizeof(*machine->device_memory)); - /* initialize hotplug memory address space */ if (machine->ram_size < machine->maxram_size) { ram_addr_t device_mem_size =3D machine->maxram_size - machine->ram= _size; + hwaddr device_mem_base; + /* * Limit the number of hotpluggable memory slots to half the number * slots that KVM supports, leaving the other half for PCI and oth= er @@ -2890,12 +2889,10 @@ static void spapr_machine_init(MachineState *machin= e) exit(1); } =20 - machine->device_memory->base =3D ROUND_UP(machine->ram_size, - SPAPR_DEVICE_MEM_ALIGN); - memory_region_init(&machine->device_memory->mr, OBJECT(spapr), - "device-memory", device_mem_size); - memory_region_add_subregion(sysmem, machine->device_memory->base, - &machine->device_memory->mr); + device_mem_base =3D ROUND_UP(machine->ram_size, SPAPR_DEVICE_MEM_A= LIGN); + memory_devices_init(machine, device_mem_base, device_mem_size); + } else { + memory_devices_init(machine, 0, 0); } =20 if (smc->dr_lmb_enabled) { diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index 48d2611fc5..6e8a10e2f5 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -16,6 +16,7 @@ #include "hw/qdev-core.h" #include "qapi/qapi-types-machine.h" #include "qom/object.h" +#include "exec/hwaddr.h" =20 #define TYPE_MEMORY_DEVICE "memory-device" =20 @@ -113,5 +114,6 @@ void memory_device_plug(MemoryDeviceState *md, MachineS= tate *ms); void memory_device_unplug(MemoryDeviceState *md, MachineState *ms); uint64_t memory_device_get_region_size(const MemoryDeviceState *md, Error **errp); +void memory_devices_init(MachineState *ms, hwaddr base, uint64_t size); =20 #endif --=20 2.40.1 From nobody Sat May 18 09:01:37 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=1684868000; cv=none; d=zohomail.com; s=zohoarc; b=asw2sBGgLjQgZ5aytFM1MBxE7rwerrNmup//WasVLDaSahlg/po1u8H/C3b9MggT1TRHK8fmmtAgebrs8bbfkMdBQ62HvXfsRVF32LT4S3OGEnWsul10azhopW9zJvFdNOmY5B5l9TFrtY0zmCzO02KmXd1MbAnP0dA+PoezthY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684868000; h=Content-Transfer-Encoding:Cc: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=jtf3t5gWtbo1UMsiBtlI3po1F4gSF64L6rEUf8Zg8FE=; b=XMsQXVFpr3RAB39Vx9wIBkgjV7iA2KLj6YvxaYZ4iQ7Rv+edDNlVife6fq6vu5IkYFkd34fBLRGKMBhNtcshM8wx2eDsMh4vWev6vFLrkPQXaxjArvx9DhvCOqNU936vWSWbVf/TxuD4Kci6Ws+Y3+dje7IJdLwqhG304hnfpx0= 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 1684868000599868.7556137187934; Tue, 23 May 2023 11:53:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q1X7K-0007VW-F0; Tue, 23 May 2023 14:52:19 -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 1q1X7F-0007SH-M0 for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:15 -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 1q1X7A-0000fl-8l for qemu-devel@nongnu.org; Tue, 23 May 2023 14:52:09 -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-459-o9cyEnWjO_K-INe167Me5w-1; Tue, 23 May 2023 14:52:03 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AD22D1C07827; Tue, 23 May 2023 18:52:01 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.194.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 921D840CFD45; Tue, 23 May 2023 18:51:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684867926; 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=jtf3t5gWtbo1UMsiBtlI3po1F4gSF64L6rEUf8Zg8FE=; b=cML+KVw6S26vlot+FxZP/0NuRIeUbeyEy6YfAeAdJLsj4QdAXQNEWAefT3Pw4E+HKm4XRs pRWojpFdkNaNSGEXEqefA3wu402gRnNC2sfINRCYxWoP3SODZfKVPc5cHIFIVLegmiVQ4O TssZ80L5kTMyzPrPYRQof6BuJcSU/Zk= X-MC-Unique: o9cyEnWjO_K-INe167Me5w-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-ppc@nongnu.org, David Hildenbrand , Igor Mammedov , Xiao Guangrong , Peter Maydell , "Michael S. Tsirkin" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Xiaojuan Yang , Song Gao , Daniel Henrique Barboza , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , David Gibson , Greg Kurz , Harsh Prateek Bora , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang Subject: [PATCH v1 3/3] memory-device: Track used region size in DeviceMemoryState Date: Tue, 23 May 2023 20:51:44 +0200 Message-Id: <20230523185144.533592-4-david@redhat.com> In-Reply-To: <20230523185144.533592-1-david@redhat.com> References: <20230523185144.533592-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: 1684868002412100003 Content-Type: text/plain; charset="utf-8" Let's avoid iterating over all devices and simply track it in the DeviceMemoryState. Signed-off-by: David Hildenbrand --- hw/mem/memory-device.c | 22 +++------------------- include/hw/boards.h | 2 ++ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index d99ceb621a..675ceeff55 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -52,28 +52,11 @@ static int memory_device_build_list(Object *obj, void *= opaque) return 0; } =20 -static int memory_device_used_region_size(Object *obj, void *opaque) -{ - uint64_t *size =3D opaque; - - if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { - const DeviceState *dev =3D DEVICE(obj); - const MemoryDeviceState *md =3D MEMORY_DEVICE(obj); - - if (dev->realized) { - *size +=3D memory_device_get_region_size(md, &error_abort); - } - } - - object_child_foreach(obj, memory_device_used_region_size, opaque); - return 0; -} - static void memory_device_check_addable(MachineState *ms, MemoryRegion *mr, Error **errp) { + const uint64_t used_region_size =3D ms->device_memory->used_region_siz= e; const uint64_t size =3D memory_region_size(mr); - uint64_t used_region_size =3D 0; =20 /* we will need a new memory slot for kvm and vhost */ if (kvm_enabled() && !kvm_has_free_slot(ms)) { @@ -86,7 +69,6 @@ static void memory_device_check_addable(MachineState *ms,= MemoryRegion *mr, } =20 /* will we exceed the total amount of memory specified */ - memory_device_used_region_size(OBJECT(ms), &used_region_size); if (used_region_size + size < used_region_size || used_region_size + size > ms->maxram_size - ms->ram_size) { error_setg(errp, "not enough space, currently 0x%" PRIx64 @@ -297,6 +279,7 @@ void memory_device_plug(MemoryDeviceState *md, MachineS= tate *ms) mr =3D mdc->get_memory_region(md, &error_abort); g_assert(ms->device_memory); =20 + ms->device_memory->used_region_size +=3D memory_region_size(mr); memory_region_add_subregion(&ms->device_memory->mr, addr - ms->device_memory->base, mr); trace_memory_device_plug(DEVICE(md)->id ? DEVICE(md)->id : "", addr); @@ -315,6 +298,7 @@ void memory_device_unplug(MemoryDeviceState *md, Machin= eState *ms) g_assert(ms->device_memory); =20 memory_region_del_subregion(&ms->device_memory->mr, mr); + ms->device_memory->used_region_size -=3D memory_region_size(mr); trace_memory_device_unplug(DEVICE(md)->id ? DEVICE(md)->id : "", mdc->get_addr(md)); } diff --git a/include/hw/boards.h b/include/hw/boards.h index a385010909..9d48c73edb 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -294,11 +294,13 @@ struct MachineClass { * address space for memory devices starts * @mr: address space container for memory devices * @dimm_size: the sum of plugged DIMMs' sizes + * @used_region_size: the part of @mr already used by memory devices */ typedef struct DeviceMemoryState { hwaddr base; MemoryRegion mr; uint64_t dimm_size; + uint64_t used_region_size; } DeviceMemoryState; =20 /** --=20 2.40.1