All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: qemu-devel@nongnu.org
Cc: "David Hildenbrand" <david@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Xiao Guangrong" <xiaoguangrong.eric@gmail.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Michal Privoznik" <mprivozn@redhat.com>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"Gavin Shan" <gshan@redhat.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Maciej S . Szmigiero" <mail@maciej.szmigiero.name>,
	kvm@vger.kernel.org,
	"Maciej S . Szmigiero" <maciej.szmigiero@oracle.com>
Subject: [PATCH v4 06/18] memory-device: Support memory devices with multiple memslots
Date: Tue, 26 Sep 2023 20:57:26 +0200	[thread overview]
Message-ID: <20230926185738.277351-7-david@redhat.com> (raw)
In-Reply-To: <20230926185738.277351-1-david@redhat.com>

We want to support memory devices that have a memory region container as
device memory region that maps multiple RAM memory regions. Let's start
by supporting memory devices that statically map multiple RAM memory
regions and, thereby, consume multiple memslots.

We already have one device that uses a container as device memory region:
NVDIMMs. However, a NVDIMM always ends up consuming exactly one memslot.

Let's add support for that by asking the memory device via a new
callback how many memslots it requires.

Reviewed-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 hw/mem/memory-device.c         | 27 +++++++++++++++++++--------
 include/hw/mem/memory-device.h | 18 ++++++++++++++++++
 2 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c
index e09960744d..0eec0872a9 100644
--- a/hw/mem/memory-device.c
+++ b/hw/mem/memory-device.c
@@ -52,19 +52,30 @@ static int memory_device_build_list(Object *obj, void *opaque)
     return 0;
 }
 
-static void memory_device_check_addable(MachineState *ms, MemoryRegion *mr,
-                                        Error **errp)
+static unsigned int memory_device_get_memslots(MemoryDeviceState *md)
+{
+    const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
+
+    if (mdc->get_memslots) {
+        return mdc->get_memslots(md);
+    }
+    return 1;
+}
+
+static void memory_device_check_addable(MachineState *ms, MemoryDeviceState *md,
+                                        MemoryRegion *mr, Error **errp)
 {
     const uint64_t used_region_size = ms->device_memory->used_region_size;
     const uint64_t size = memory_region_size(mr);
+    const unsigned int required_memslots = memory_device_get_memslots(md);
 
-    /* we will need a new memory slot for kvm and vhost */
-    if (kvm_enabled() && !kvm_get_free_memslots()) {
-        error_setg(errp, "hypervisor has no free memory slots left");
+    /* we will need memory slots for kvm and vhost */
+    if (kvm_enabled() && kvm_get_free_memslots() < required_memslots) {
+        error_setg(errp, "hypervisor has not enough free memory slots left");
         return;
     }
-    if (!vhost_get_free_memslots()) {
-        error_setg(errp, "a used vhost backend has no free memory slots left");
+    if (vhost_get_free_memslots() < required_memslots) {
+        error_setg(errp, "a used vhost backend has not enough free memory slots left");
         return;
     }
 
@@ -233,7 +244,7 @@ void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
         goto out;
     }
 
-    memory_device_check_addable(ms, mr, &local_err);
+    memory_device_check_addable(ms, md, mr, &local_err);
     if (local_err) {
         goto out;
     }
diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h
index 48d2611fc5..b51a579fb9 100644
--- a/include/hw/mem/memory-device.h
+++ b/include/hw/mem/memory-device.h
@@ -41,6 +41,11 @@ typedef struct MemoryDeviceState MemoryDeviceState;
  * successive memory regions are used, a covering memory region has to
  * be provided. Scattered memory regions are not supported for single
  * devices.
+ *
+ * The device memory region returned via @get_memory_region may either be a
+ * single RAM memory region or a memory region container with subregions
+ * that are RAM memory regions or aliases to RAM memory regions. Other
+ * memory regions or subregions are not supported.
  */
 struct MemoryDeviceClass {
     /* private */
@@ -88,6 +93,19 @@ struct MemoryDeviceClass {
      */
     MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
 
+    /*
+     * Optional for memory devices that require only a single memslot,
+     * required for all other memory devices: Return the number of memslots
+     * (distinct RAM memory regions in the device memory region) that are
+     * required by the device.
+     *
+     * If this function is not implemented, the assumption is "1".
+     *
+     * Called when (un)plugging the memory device, to check if the requirements
+     * can be satisfied, and to do proper accounting.
+     */
+    unsigned int (*get_memslots)(MemoryDeviceState *md);
+
     /*
      * Optional: Return the desired minimum alignment of the device in guest
      * physical address space. The final alignment is computed based on this
-- 
2.41.0


  parent reply	other threads:[~2023-09-26 18:59 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-26 18:57 [PATCH v4 00/18] virtio-mem: Expose device memory through multiple memslots David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 01/18] vhost: Rework memslot filtering and fix "used_memslot" tracking David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 02/18] vhost: Remove vhost_backend_can_merge() callback David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 03/18] softmmu/physmem: Fixup qemu_ram_block_from_host() documentation David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 04/18] kvm: Return number of free memslots David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 05/18] vhost: " David Hildenbrand
2023-09-26 18:57 ` David Hildenbrand [this message]
2023-09-26 18:57 ` [PATCH v4 07/18] stubs: Rename qmp_memory_device.c to memory_device.c David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 08/18] memory-device: Track required and actually used memslots in DeviceMemoryState David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 09/18] memory-device,vhost: Support memory devices that dynamically consume memslots David Hildenbrand
2023-09-26 18:57   ` [PATCH v4 09/18] memory-device, vhost: " David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 10/18] kvm: Add stub for kvm_get_max_memslots() David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 11/18] vhost: Add vhost_get_max_memslots() David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 12/18] memory-device,vhost: Support automatic decision on the number of memslots David Hildenbrand
2023-09-26 18:57   ` [PATCH v4 12/18] memory-device, vhost: " David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 13/18] memory: Clarify mapping requirements for RamDiscardManager David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 14/18] virtio-mem: Pass non-const VirtIOMEM via virtio_mem_range_cb David Hildenbrand
2023-09-28 17:41   ` Maciej S. Szmigiero
2023-09-26 18:57 ` [PATCH v4 15/18] virtio-mem: Update state to match bitmap as soon as it's been migrated David Hildenbrand
2023-09-30 15:50   ` Maciej S. Szmigiero
2023-09-26 18:57 ` [PATCH v4 16/18] virtio-mem: Expose device memory dynamically via multiple memslots if enabled David Hildenbrand
2023-09-30 17:31   ` Maciej S. Szmigiero
2023-10-02  8:57     ` David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 17/18] memory,vhost: Allow for marking memory device memory regions unmergeable David Hildenbrand
2023-09-26 18:57   ` [PATCH v4 17/18] memory, vhost: " David Hildenbrand
2023-09-26 18:57 ` [PATCH v4 18/18] virtio-mem: Mark memslot alias " David Hildenbrand
2023-10-02  8:58 ` [PATCH v4 00/18] virtio-mem: Expose device memory through multiple memslots David Hildenbrand
2023-10-03 13:39 ` Michael S. Tsirkin
2023-10-06  9:29   ` David Hildenbrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230926185738.277351-7-david@redhat.com \
    --to=david@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=berrange@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=gshan@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=maciej.szmigiero@oracle.com \
    --cc=mail@maciej.szmigiero.name \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mprivozn@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=wangyanan55@huawei.com \
    --cc=xiaoguangrong.eric@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.