From nobody Wed Oct 29 22:59:59 2025 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 1525712947530579.6530638440595; Mon, 7 May 2018 10:09:07 -0700 (PDT) Received: from localhost ([::1]:47260 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFjdK-0001tT-Bw for importer@patchew.org; Mon, 07 May 2018 13:09:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38542) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFjY8-0006WO-NZ for qemu-devel@nongnu.org; Mon, 07 May 2018 13:03:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fFjY7-0007gg-Fe for qemu-devel@nongnu.org; Mon, 07 May 2018 13:03:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50596) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fFjY7-0007gH-76 for qemu-devel@nongnu.org; Mon, 07 May 2018 13:03:43 -0400 Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6A01B30F8D96; Mon, 7 May 2018 17:03:42 +0000 (UTC) Received: from localhost (ovpn-116-33.gru2.redhat.com [10.97.116.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF72731320A2; Mon, 7 May 2018 17:03:41 +0000 (UTC) From: Eduardo Habkost To: Peter Maydell , qemu-devel@nongnu.org Date: Mon, 7 May 2018 14:03:09 -0300 Message-Id: <20180507170315.11497-7-ehabkost@redhat.com> In-Reply-To: <20180507170315.11497-1-ehabkost@redhat.com> References: <20180507170315.11497-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 07 May 2018 17:03:42 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 06/12] pc-dimm: factor out capacity and slot checks into MemoryDevice 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: David Hildenbrand 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" From: David Hildenbrand Move the checks into memory_device_get_free_addr(). This will check before doing any calculations if we have KVM/vhost slots left and if the total region size would be exceeded. Of course, while at it, make it independent of pc-dimm code. Signed-off-by: David Hildenbrand Message-Id: <20180423165126.15441-7-david@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Eduardo Habkost --- include/hw/mem/pc-dimm.h | 1 - hw/mem/memory-device.c | 51 ++++++++++++++++++++++++++++++++++++++++ hw/mem/pc-dimm.c | 60 --------------------------------------------= ---- 3 files changed, 51 insertions(+), 61 deletions(-) diff --git a/include/hw/mem/pc-dimm.h b/include/hw/mem/pc-dimm.h index e37fb5d5db..627c8601d9 100644 --- a/include/hw/mem/pc-dimm.h +++ b/include/hw/mem/pc-dimm.h @@ -78,7 +78,6 @@ typedef struct PCDIMMDeviceClass { =20 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp); =20 -uint64_t pc_existing_dimms_capacity(Error **errp); void pc_dimm_memory_plug(DeviceState *dev, MachineState *machine, uint64_t align, Error **errp); void pc_dimm_memory_unplug(DeviceState *dev, MachineState *machine); diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index a2cb85462f..8535ddcb14 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -15,6 +15,8 @@ #include "qapi/error.h" #include "hw/boards.h" #include "qemu/range.h" +#include "hw/virtio/vhost.h" +#include "sysemu/kvm.h" =20 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b) { @@ -48,6 +50,50 @@ static int memory_device_build_list(Object *obj, void *o= paque) 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); + const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(obj); + + if (dev->realized) { + *size +=3D mdc->get_region_size(md); + } + } + + object_child_foreach(obj, memory_device_used_region_size, opaque); + return 0; +} + +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) @@ -73,6 +119,11 @@ uint64_t memory_device_get_free_addr(MachineState *ms, = const uint64_t *hint, 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) { + return 0; + } + if (hint && QEMU_ALIGN_UP(*hint, align) !=3D *hint) { error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", align); diff --git a/hw/mem/pc-dimm.c b/hw/mem/pc-dimm.c index 9b70174fd9..8aa2d36ce9 100644 --- a/hw/mem/pc-dimm.c +++ b/hw/mem/pc-dimm.c @@ -25,9 +25,7 @@ #include "qapi/error.h" #include "qapi/visitor.h" #include "sysemu/numa.h" -#include "sysemu/kvm.h" #include "trace.h" -#include "hw/virtio/vhost.h" =20 typedef struct pc_dimms_capacity { uint64_t size; @@ -43,7 +41,6 @@ void pc_dimm_memory_plug(DeviceState *dev, MachineState *= machine, PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(dimm); MemoryRegion *vmstate_mr =3D ddc->get_vmstate_memory_region(dimm); Error *local_err =3D NULL; - uint64_t existing_dimms_capacity =3D 0; MemoryRegion *mr; uint64_t addr; =20 @@ -64,20 +61,6 @@ void pc_dimm_memory_plug(DeviceState *dev, MachineState = *machine, goto out; } =20 - existing_dimms_capacity =3D pc_existing_dimms_capacity(&local_err); - if (local_err) { - goto out; - } - - if (existing_dimms_capacity + memory_region_size(mr) > - machine->maxram_size - machine->ram_size) { - error_setg(&local_err, "not enough space, currently 0x%" PRIx64 - " in use of total hot pluggable 0x" RAM_ADDR_FMT, - existing_dimms_capacity, - machine->maxram_size - machine->ram_size); - goto out; - } - object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_= err); if (local_err) { goto out; @@ -100,17 +83,6 @@ void pc_dimm_memory_plug(DeviceState *dev, MachineState= *machine, } trace_mhp_pc_dimm_assigned_slot(slot); =20 - if (kvm_enabled() && !kvm_has_free_slot(machine)) { - error_setg(&local_err, "hypervisor has no free memory slots left"); - goto out; - } - - if (!vhost_has_free_slot()) { - error_setg(&local_err, "a used vhost backend has no free" - " memory slots left"); - goto out; - } - memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); vmstate_register_ram(vmstate_mr, dev); =20 @@ -129,38 +101,6 @@ void pc_dimm_memory_unplug(DeviceState *dev, MachineSt= ate *machine) vmstate_unregister_ram(vmstate_mr, dev); } =20 -static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) -{ - pc_dimms_capacity *cap =3D opaque; - uint64_t *size =3D &cap->size; - - if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { - DeviceState *dev =3D DEVICE(obj); - - if (dev->realized) { - (*size) +=3D object_property_get_uint(obj, PC_DIMM_SIZE_PROP, - cap->errp); - } - - if (cap->errp && *cap->errp) { - return 1; - } - } - object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque); - return 0; -} - -uint64_t pc_existing_dimms_capacity(Error **errp) -{ - pc_dimms_capacity cap; - - cap.size =3D 0; - cap.errp =3D errp; - - pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap); - return cap.size; -} - static int pc_dimm_slot2bitmap(Object *obj, void *opaque) { unsigned long *bitmap =3D opaque; --=20 2.14.3