From nobody Mon Feb 9 01:19:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1525363042167472.12632427715573; Thu, 3 May 2018 08:57:22 -0700 (PDT) Received: from localhost ([::1]:57402 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEGbb-0006N8-0P for importer@patchew.org; Thu, 03 May 2018 11:57:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50419) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEGUd-0000s1-MK for qemu-devel@nongnu.org; Thu, 03 May 2018 11:50:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fEGUc-0003nK-Kw for qemu-devel@nongnu.org; Thu, 03 May 2018 11:50:03 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:60494 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fEGUc-0003mr-GN; Thu, 03 May 2018 11:50:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 06E5A406F88F; Thu, 3 May 2018 15:50:02 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-201.ams2.redhat.com [10.36.117.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id CCA2E215CDAE; Thu, 3 May 2018 15:49:59 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Thu, 3 May 2018 17:49:35 +0200 Message-Id: <20180503154936.18946-8-david@redhat.com> In-Reply-To: <20180503154936.18946-1-david@redhat.com> References: <20180503154936.18946-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 03 May 2018 15:50:02 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 03 May 2018 15:50:02 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v1 7/8] memory-device: factor out pre-assign into default resource handler X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Markus Armbruster , Alexander Graf , qemu-s390x@nongnu.org, qemu-ppc@nongnu.org, Paolo Bonzini , Marcel Apfelbaum , Igor Mammedov , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's move all pre-assign checks we can do without the device being realized into the default resource handler. Signed-off-by: David Hildenbrand --- hw/core/machine.c | 17 ++++++++++ hw/mem/memory-device.c | 72 +++++++++++++++++++-------------------= ---- include/hw/mem/memory-device.h | 2 ++ 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/hw/core/machine.c b/hw/core/machine.c index a41068410c..e763501c66 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -21,6 +21,7 @@ #include "qemu/error-report.h" #include "qemu/cutils.h" #include "sysemu/qtest.h" +#include "hw/mem/memory-device.h" =20 static char *machine_get_accel(Object *obj, Error **errp) { @@ -520,6 +521,9 @@ void machine_set_cpu_numa_node(MachineState *machine, static ResourceHandler *machine_get_resource_handler(MachineState *machine, const DeviceState *de= v) { + if (object_dynamic_cast(OBJECT(dev), TYPE_MEMORY_DEVICE)) { + return RESOURCE_HANDLER(machine); + } return NULL; } =20 @@ -527,6 +531,19 @@ static void machine_resource_handler_pre_assign(Resour= ceHandler *rh, const DeviceState *dev, Error **errp) { + MachineState *ms =3D MACHINE(rh); + Error *local_err =3D NULL; + + if (object_dynamic_cast(OBJECT(dev), TYPE_MEMORY_DEVICE)) { + const MemoryDeviceState *md =3D MEMORY_DEVICE(dev); + + memory_device_pre_assign(ms, md, &local_err); + if (local_err) { + goto out; + } + } +out: + error_propagate(errp, local_err); } =20 static void machine_resource_handler_assign(ResourceHandler *rh, diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 3e04f3954e..aa04e0c962 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -68,59 +68,27 @@ static int memory_device_used_region_size(Object *obj, = void *opaque) return 0; } =20 -static void memory_device_check_addable(MachineState *ms, uint64_t size, - Error **errp) -{ - uint64_t used_region_size =3D 0; - - /* we will need a new memory slot for kvm and vhost */ - if (kvm_enabled() && !kvm_has_free_slot(ms)) { - error_setg(errp, "hypervisor has no free memory slots left"); - return; - } - if (!vhost_has_free_slot()) { - error_setg(errp, "a used vhost backend has no free memory slots le= ft"); - return; - } - - /* will we exceed the total amount of memory specified */ - memory_device_used_region_size(OBJECT(ms), &used_region_size); - if (used_region_size + size > ms->maxram_size - ms->ram_size) { - error_setg(errp, "not enough space, currently 0x%" PRIx64 - " in use of total hot pluggable 0x" RAM_ADDR_FMT, - used_region_size, ms->maxram_size - ms->ram_size); - return; - } - -} - uint64_t memory_device_get_free_addr(MachineState *ms, const uint64_t *hin= t, uint64_t align, uint64_t size, Error **errp) { uint64_t address_space_start, address_space_end; + uint64_t used_region_size =3D 0; GSList *list =3D NULL, *item; uint64_t new_addr =3D 0; =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; - } address_space_start =3D ms->device_memory->base; address_space_end =3D address_space_start + memory_region_size(&ms->device_memory->mr); g_assert(QEMU_ALIGN_UP(address_space_start, align) =3D=3D address_spac= e_start); g_assert(address_space_end >=3D address_space_start); =20 - memory_device_check_addable(ms, size, errp); - if (*errp) { + /* will we exceed the total amount of memory specified */ + memory_device_used_region_size(OBJECT(ms), &used_region_size); + if (used_region_size + size > ms->maxram_size - ms->ram_size) { + error_setg(errp, "not enough space, currently 0x%" PRIx64 + " in use of total hot pluggable 0x" RAM_ADDR_FMT, + used_region_size, ms->maxram_size - ms->ram_size); return 0; } =20 @@ -243,6 +211,32 @@ uint64_t get_plugged_memory_size(void) return size; } =20 +void memory_device_pre_assign(MachineState *ms, + const MemoryDeviceState *md, Error **errp) +{ + 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; + } + + /* we will need a new memory slot for kvm and vhost */ + if (kvm_enabled() && !kvm_has_free_slot(ms)) { + error_setg(errp, "hypervisor has no free memory slots left"); + return; + } + if (!vhost_has_free_slot()) { + error_setg(errp, "a used vhost backend has no free memory slots le= ft"); + return; + } +} + void memory_device_plug_region(MachineState *ms, MemoryRegion *mr, uint64_t addr) { diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index e10f2e854a..edbd964ac7 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -47,6 +47,8 @@ typedef struct MemoryDeviceClass { =20 MemoryDeviceInfoList *qmp_memory_device_list(void); uint64_t get_plugged_memory_size(void); +void memory_device_pre_assign(MachineState *ms, + const MemoryDeviceState *md, Error **errp); uint64_t memory_device_get_free_addr(MachineState *ms, const uint64_t *hin= t, uint64_t align, uint64_t size, Error **errp); --=20 2.14.3