Skip to contentSkip to conversation

KVM/hostmem: Support init-shared guest-memfd as VM backends

PATCH Superseded Peter Xu 2 participants 25 messages Nov 19, 2025 – Dec 15, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author
#23 lore raw

PATCH v2 8/9 tests/migration-test: Support guest-memfd init shared mem type

In reply to Peter Xu

Support the guest-memfd type when the fd has init share enabled. It means the gmemfd can be used similarly to memfd.

---
 tests/qtest/migration/framework.h |  4 +++
 tests/qtest/migration/framework.c | 60 +++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
tests/qtest/migration/framework.h +4 -0
index ed85ed502d..b4c5edcad3 100644
@@ -34,6 +34,10 @@ typedef enum {
      * but only anonymously allocated.
      */
     MEM_TYPE_MEMFD,
+    /*
+     * Use guest-memfd, shared mappings.
+     */
+    MEM_TYPE_GUEST_MEMFD,
     MEM_TYPE_NUM,
 } MemType;
 
tests/qtest/migration/framework.c +60 -0

86 lines not on this page · raw message

Signature · 1 line
2.50.1
author
#7 lore raw

PATCH v2 3/9 memory: Rename RAM_GUEST_MEMFD to RAM_GUEST_MEMFD_PRIVATE

In reply to Peter Xu

This name is too generic, and can conflict with in-place guest-memfd support. Add a _PRIVATE suffix to show what it really means: it is always silently using an internal guest-memfd to back a shared host backend, rather than used in-place.

This paves way for in-place guest-memfd, which means we can have a ramblock that allocates pages completely from guest-memfd (private or shared).

---
 include/system/memory.h   | 8 ++++----
 include/system/ram_addr.h | 2 +-
 backends/hostmem-file.c   | 2 +-
 backends/hostmem-memfd.c  | 2 +-
 backends/hostmem-ram.c    | 2 +-
 backends/hostmem-shm.c    | 2 +-
 system/memory.c           | 3 ++-
 system/physmem.c          | 8 ++++----
 8 files changed, 15 insertions(+), 14 deletions(-)
include/system/memory.h +4 -4
index 3bd5ffa5e0..2c1a5e06b4 100644
@@ -263,7 +263,7 @@ typedef struct IOMMUTLBEvent {
 #define RAM_READONLY_FD (1 << 11)
 
 /* RAM can be private that has kvm guest memfd backend */
-#define RAM_GUEST_MEMFD   (1 << 12)
+#define RAM_GUEST_MEMFD_PRIVATE   (1 << 12)
 
 /*
  * In RAMBlock creation functions, if MAP_SHARED is 0 in the flags parameter,
@@ -1401,7 +1401,7 @@ bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
  *        must be unique within any device
  * @size: size of the region.
  * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE,
- *             RAM_GUEST_MEMFD.
+ *             RAM_GUEST_MEMFD_PRIVATE.
  * @errp: pointer to Error*, to store an error if it happens.
  *
  * Note that this function does not do anything to cause the data in the
@@ -1463,7 +1463,7 @@ bool memory_region_init_resizeable_ram(MemoryRegion *mr,
  *         (getpagesize()) will be used.
  * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *             RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
- *             RAM_READONLY_FD, RAM_GUEST_MEMFD
+ *             RAM_READONLY_FD, RAM_GUEST_MEMFD_PRIVATE
  * @path: the path in which to allocate the RAM.
  * @offset: offset within the file referenced by path
  * @errp: pointer to Error*, to store an error if it happens.
@@ -1493,7 +1493,7 @@ bool memory_region_init_ram_from_file(MemoryRegion *mr,
  * @size: size of the region.
  * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *             RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
- *             RAM_READONLY_FD, RAM_GUEST_MEMFD
+ *             RAM_READONLY_FD, RAM_GUEST_MEMFD_PRIVATE
  * @fd: the fd to mmap.
  * @offset: offset within the file referenced by fd
  * @errp: pointer to Error*, to store an error if it happens.
include/system/ram_addr.h +1 -1
index 683485980c..930d3824d7 100644
@@ -92,7 +92,7 @@ static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
  *  @resized: callback after calls to qemu_ram_resize
  *  @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *              RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
- *              RAM_READONLY_FD, RAM_GUEST_MEMFD
+ *              RAM_READONLY_FD, RAM_GUEST_MEMFD_PRIVATE
  *  @mem_path or @fd: specify the backing file or device
  *  @offset: Offset into target file
  *  @grow: extend file if necessary (but an empty file is always extended).
backends/hostmem-file.c +1 -1
index 8e3219c061..1f20cd8fd6 100644
@@ -86,7 +86,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     ram_flags |= fb->readonly ? RAM_READONLY_FD : 0;
     ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
+    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
     ram_flags |= RAM_NAMED_FILE;
     return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
backends/hostmem-memfd.c +1 -1
index 923239f9cf..3f3e485709 100644
@@ -60,7 +60,7 @@ have_fd:
     backend->aligned = true;
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
+    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
     return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
                                           backend->size, ram_flags, fd, 0, errp);
 }
backends/hostmem-ram.c +1 -1
index 062b1abb11..96ad29112d 100644
@@ -30,7 +30,7 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
+    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
     return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend),
                                                   name, backend->size,
                                                   ram_flags, errp);
backends/hostmem-shm.c +1 -1
index 806e2670e0..e86fb2e0aa 100644
@@ -54,7 +54,7 @@ have_fd:
     /* Let's do the same as memory-backend-ram,share=on would do. */
     ram_flags = RAM_SHARED;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
+    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
 
     return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
                                               backend_name, backend->size,
system/memory.c +2 -1
index 8b84661ae3..81b7bff42d 100644
@@ -3755,7 +3755,8 @@ bool memory_region_init_ram_guest_memfd(MemoryRegion *mr,
     DeviceState *owner_dev;
 
     if (!memory_region_init_ram_flags_nomigrate(mr, owner, name, size,
-                                                RAM_GUEST_MEMFD, errp)) {
+                                                RAM_GUEST_MEMFD_PRIVATE,
+                                                errp)) {
         return false;
     }
     /* This will assert if owner is neither NULL nor a DeviceState.
system/physmem.c +4 -4
index 3555d2f6f7..25c800c9d3 100644
@@ -2203,7 +2203,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         }
     }
 
-    if (new_block->flags & RAM_GUEST_MEMFD) {
+    if (new_block->flags & RAM_GUEST_MEMFD_PRIVATE) {
         int ret;
 
         if (!kvm_enabled()) {
@@ -2341,7 +2341,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size,
     /* Just support these ram flags by now. */
     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
                           RAM_PROTECTED | RAM_NAMED_FILE | RAM_READONLY |
-                          RAM_READONLY_FD | RAM_GUEST_MEMFD |
+                          RAM_READONLY_FD | RAM_GUEST_MEMFD_PRIVATE |
                           RAM_RESIZEABLE)) == 0);
     assert(max_size >= size);
 
@@ -2498,7 +2498,7 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
     ram_flags &= ~RAM_PRIVATE;
 
     assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC |
-                          RAM_NORESERVE | RAM_GUEST_MEMFD)) == 0);
+                          RAM_NORESERVE | RAM_GUEST_MEMFD_PRIVATE)) == 0);
     assert(!host ^ (ram_flags & RAM_PREALLOC));
     assert(max_size >= size);
 
@@ -2581,7 +2581,7 @@ RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags,
                          MemoryRegion *mr, Error **errp)
 {
-    assert((ram_flags & ~(RAM_SHARED | RAM_NORESERVE | RAM_GUEST_MEMFD |
+    assert((ram_flags & ~(RAM_SHARED | RAM_NORESERVE | RAM_GUEST_MEMFD_PRIVATE |
                           RAM_PRIVATE)) == 0);
     return qemu_ram_alloc_internal(size, size, NULL, NULL, ram_flags, mr, errp);
 }
Signature · 1 line
2.50.1
author
#12 lore raw

PATCH v2 5/9 ramblock: Rename guest_memfd to guest_memfd_private

In reply to Peter Xu

Rename the field to reflect the fact that the guest_memfd in this case only backs private portion of the ramblock rather than all of it.

---
 include/system/memory.h   |  3 ++-
 include/system/ramblock.h |  7 ++++++-
 accel/kvm/kvm-all.c       |  2 +-
 system/memory.c           |  2 +-
 system/physmem.c          | 21 +++++++++++----------
 5 files changed, 21 insertions(+), 14 deletions(-)
include/system/memory.h +2 -1
index 4428701a9f..0aca241360 100644
@@ -1826,7 +1826,8 @@ bool memory_region_is_protected(MemoryRegion *mr);
  * memory_region_has_guest_memfd_private: check whether a memory region has
  *     guest_memfd associated
  *
- * Returns %true if a memory region's ram_block has valid guest_memfd assigned.
+ * Returns %true if a memory region's ram_block has guest_memfd_private
+ * assigned.
  *
  * @mr: the memory region being queried
  */
include/system/ramblock.h +6 -1
index 76694fe1b5..9ecf7f970c 100644
@@ -40,7 +40,12 @@ struct RAMBlock {
     Error *cpr_blocker;
     int fd;
     uint64_t fd_offset;
-    int guest_memfd;
+    /*
+     * When RAM_GUEST_MEMFD_PRIVATE flag is set, this ramblock can have
+     * private pages backed by guest_memfd_private specified, while shared
+     * pages are backed by the ramblock on its own.
+     */
+    int guest_memfd_private;
     RamBlockAttributes *attributes;
     size_t page_size;
     /* dirty bitmap used during migration */
accel/kvm/kvm-all.c +1 -1
index 320315f50c..5942e17f7e 100644
@@ -1603,7 +1603,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
         mem->ram_start_offset = ram_start_offset;
         mem->ram = ram;
         mem->flags = kvm_mem_flags(mr);
-        mem->guest_memfd = mr->ram_block->guest_memfd;
+        mem->guest_memfd = mr->ram_block->guest_memfd_private;
         mem->guest_memfd_offset = mem->guest_memfd >= 0 ?
                                   (uint8_t*)ram - mr->ram_block->host : 0;
 
system/memory.c +1 -1
index 15964160ee..d70968c966 100644
@@ -1899,7 +1899,7 @@ bool memory_region_is_protected(MemoryRegion *mr)
 
 bool memory_region_has_guest_memfd_private(MemoryRegion *mr)
 {
-    return mr->ram_block && mr->ram_block->guest_memfd >= 0;
+    return mr->ram_block && mr->ram_block->guest_memfd_private >= 0;
 }
 
 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
system/physmem.c +11 -10
index 25c800c9d3..d30fd690d1 100644
@@ -2219,7 +2219,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
             goto out_free;
         }
 
-        assert(new_block->guest_memfd < 0);
+        assert(new_block->guest_memfd_private < 0);
 
         ret = ram_block_coordinated_discard_require(true);
         if (ret < 0) {
@@ -2229,9 +2229,9 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
             goto out_free;
         }
 
-        new_block->guest_memfd = kvm_create_guest_memfd(new_block->max_length,
-                                                        0, errp);
-        if (new_block->guest_memfd < 0) {
+        new_block->guest_memfd_private =
+            kvm_create_guest_memfd(new_block->max_length, 0, errp);
+        if (new_block->guest_memfd_private < 0) {
             qemu_mutex_unlock_ramlist();
             goto out_free;
         }
@@ -2248,7 +2248,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
         new_block->attributes = ram_block_attributes_create(new_block);
         if (!new_block->attributes) {
             error_setg(errp, "Failed to create ram block attribute");
-            close(new_block->guest_memfd);
+            close(new_block->guest_memfd_private);
             ram_block_coordinated_discard_require(false);
             qemu_mutex_unlock_ramlist();
             goto out_free;
@@ -2385,7 +2385,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size,
     new_block->max_length = max_size;
     new_block->resized = resized;
     new_block->flags = ram_flags;
-    new_block->guest_memfd = -1;
+    new_block->guest_memfd_private = -1;
     new_block->host = file_ram_alloc(new_block, max_size, fd,
                                      file_size < offset + max_size,
                                      offset, errp);
@@ -2558,7 +2558,7 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
     new_block->used_length = size;
     new_block->max_length = max_size;
     new_block->fd = -1;
-    new_block->guest_memfd = -1;
+    new_block->guest_memfd_private = -1;
     new_block->page_size = qemu_real_host_page_size();
     new_block->host = host;
     new_block->flags = ram_flags;
@@ -2609,9 +2609,9 @@ static void reclaim_ramblock(RAMBlock *block)
         qemu_anon_ram_free(block->host, block->max_length);
     }
 
-    if (block->guest_memfd >= 0) {
+    if (block->guest_memfd_private >= 0) {
         ram_block_attributes_destroy(block->attributes);
-        close(block->guest_memfd);
+        close(block->guest_memfd_private);
         ram_block_coordinated_discard_require(false);
     }
 
@@ -4222,7 +4222,8 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t offset,
 
 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
     /* ignore fd_offset with guest_memfd */
-    ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+    ret = fallocate(rb->guest_memfd_private,
+                    FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
                     offset, length);
 
     if (ret) {
Signature · 1 line
2.50.1
author
#24 lore raw

PATCH v2 9/9 tests/migration-test: Add a precopy test for guest-memfd

In reply to Peter Xu

Add a plain tcp test for guest-memfd. Note that the test will be automatically skipped whenever not supported (e.g. qemu compiled without KVM, or host kernel doesn't support kvm, or old kernels, etc.).

---
 tests/qtest/migration/precopy-tests.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tests/qtest/migration/precopy-tests.c +12 -0
index 57ca623de5..88d2627efd 100644
@@ -215,6 +215,16 @@ static void test_precopy_tcp_plain(void)
     test_precopy_common(&args);
 }
 
+static void test_precopy_tcp_plain_gmemfd(void)
+{
+    MigrateCommon args = {
+        .listen_uri = "tcp:127.0.0.1:0",
+        .start.mem_type = MEM_TYPE_GUEST_MEMFD,
+    };
+
+    test_precopy_common(&args);
+}
+
 static void test_precopy_tcp_switchover_ack(void)
 {
     MigrateCommon args = {
@@ -1276,6 +1286,8 @@ void migration_test_add_precopy(MigrationTestEnv *env)
         return;
     }
 
+    migration_test_add("/migration/precopy/tcp/plain/guest-memfd",
+                       test_precopy_tcp_plain_gmemfd);
     migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
                        test_precopy_tcp_switchover_ack);
 
Signature · 1 line
2.50.1
author
#9 lore raw

PATCH v2 4/9 memory: Rename memory_region_has_guest_memfd() to *_private()

In reply to Peter Xu

Rename the function with "_private" suffix, to show that it returns true only if it has an internal guest-memfd to back private pages (rather than in-place guest-memfd).

---
 include/system/memory.h | 6 +++---
 accel/kvm/kvm-all.c     | 6 +++---
 system/memory.c         | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
include/system/memory.h +3 -3
index 2c1a5e06b4..4428701a9f 100644
@@ -1823,14 +1823,14 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
 bool memory_region_is_protected(MemoryRegion *mr);
 
 /**
- * memory_region_has_guest_memfd: check whether a memory region has guest_memfd
- *     associated
+ * memory_region_has_guest_memfd_private: check whether a memory region has
+ *     guest_memfd associated
  *
  * Returns %true if a memory region's ram_block has valid guest_memfd assigned.
  *
  * @mr: the memory region being queried
  */
-bool memory_region_has_guest_memfd(MemoryRegion *mr);
+bool memory_region_has_guest_memfd_private(MemoryRegion *mr);
 
 /**
  * memory_region_get_iommu: check whether a memory region is an iommu
accel/kvm/kvm-all.c +3 -3
index f477014126..320315f50c 100644
@@ -666,7 +666,7 @@ static int kvm_mem_flags(MemoryRegion *mr)
     if (readonly && kvm_readonly_mem_allowed) {
         flags |= KVM_MEM_READONLY;
     }
-    if (memory_region_has_guest_memfd(mr)) {
+    if (memory_region_has_guest_memfd_private(mr)) {
         assert(kvm_guest_memfd_supported);
         flags |= KVM_MEM_GUEST_MEMFD;
     }
@@ -1615,7 +1615,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
             abort();
         }
 
-        if (memory_region_has_guest_memfd(mr)) {
+        if (memory_region_has_guest_memfd_private(mr)) {
             err = kvm_set_memory_attributes_private(start_addr, slot_size);
             if (err) {
                 error_report("%s: failed to set memory attribute private: %s",
@@ -3101,7 +3101,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
         return ret;
     }
 
-    if (!memory_region_has_guest_memfd(mr)) {
+    if (!memory_region_has_guest_memfd_private(mr)) {
         /*
          * Because vMMIO region must be shared, guest TD may convert vMMIO
          * region to shared explicitly.  Don't complain such case.  See
system/memory.c +1 -1
index 81b7bff42d..15964160ee 100644
@@ -1897,7 +1897,7 @@ bool memory_region_is_protected(MemoryRegion *mr)
     return mr->ram && (mr->ram_block->flags & RAM_PROTECTED);
 }
 
-bool memory_region_has_guest_memfd(MemoryRegion *mr)
+bool memory_region_has_guest_memfd_private(MemoryRegion *mr)
 {
     return mr->ram_block && mr->ram_block->guest_memfd >= 0;
 }
Signature · 1 line
2.50.1
author
#14 lore raw

PATCH v2 6/9 hostmem: Rename guest_memfd to guest_memfd_private

In reply to Peter Xu

Rename the HostMemoryBackend.guest_memfd field to reflect what it really means, on whether it needs guest_memfd to back its private portion of mapping. This will help on clearance when we introduce in-place guest_memfd for hostmem.

---
 include/system/hostmem.h | 2 +-
 backends/hostmem-file.c  | 2 +-
 backends/hostmem-memfd.c | 2 +-
 backends/hostmem-ram.c   | 2 +-
 backends/hostmem-shm.c   | 2 +-
 backends/hostmem.c       | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)
include/system/hostmem.h +1 -1
index 88fa791ac7..dcbf81aeae 100644
@@ -76,7 +76,7 @@ struct HostMemoryBackend {
     uint64_t size;
     bool merge, dump, use_canonical_path;
     bool prealloc, is_mapped, share, reserve;
-    bool guest_memfd, aligned;
+    bool guest_memfd_private, aligned;
     uint32_t prealloc_threads;
     ThreadContext *prealloc_context;
     DECLARE_BITMAP(host_nodes, MAX_NODES + 1);
backends/hostmem-file.c +1 -1
index 1f20cd8fd6..0e4cfd6dc6 100644
@@ -86,7 +86,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     ram_flags |= fb->readonly ? RAM_READONLY_FD : 0;
     ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
+    ram_flags |= backend->guest_memfd_private ? RAM_GUEST_MEMFD_PRIVATE : 0;
     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
     ram_flags |= RAM_NAMED_FILE;
     return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
backends/hostmem-memfd.c +1 -1
index 3f3e485709..ea93f034e4 100644
@@ -60,7 +60,7 @@ have_fd:
     backend->aligned = true;
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
+    ram_flags |= backend->guest_memfd_private ? RAM_GUEST_MEMFD_PRIVATE : 0;
     return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
                                           backend->size, ram_flags, fd, 0, errp);
 }
backends/hostmem-ram.c +1 -1
index 96ad29112d..6a507fad77 100644
@@ -30,7 +30,7 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     name = host_memory_backend_get_name(backend);
     ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
+    ram_flags |= backend->guest_memfd_private ? RAM_GUEST_MEMFD_PRIVATE : 0;
     return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(backend),
                                                   name, backend->size,
                                                   ram_flags, errp);
backends/hostmem-shm.c +1 -1
index e86fb2e0aa..4766db6aad 100644
@@ -54,7 +54,7 @@ have_fd:
     /* Let's do the same as memory-backend-ram,share=on would do. */
     ram_flags = RAM_SHARED;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
-    ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD_PRIVATE : 0;
+    ram_flags |= backend->guest_memfd_private ? RAM_GUEST_MEMFD_PRIVATE : 0;
 
     return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
                                               backend_name, backend->size,
backends/hostmem.c +1 -1
index 35734d6f4d..70450733db 100644
@@ -288,7 +288,7 @@ static void host_memory_backend_init(Object *obj)
     /* TODO: convert access to globals to compat properties */
     backend->merge = machine_mem_merge(machine);
     backend->dump = machine_dump_guest_core(machine);
-    backend->guest_memfd = machine_require_guest_memfd(machine);
+    backend->guest_memfd_private = machine_require_guest_memfd(machine);
     backend->reserve = true;
     backend->prealloc_threads = machine->smp.cpus;
 }
Signature · 1 line
2.50.1
author
#17 lore raw

PATCH v2 7/9 hostmem: Support in-place guest memfd to back a VM

In reply to Peter Xu

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it in-place. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, in place), rather than having another backend supporting the pages converted to shared.

To use the in-place guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with in-place guest_memfd.

---
 qapi/qom.json            |  6 +++-
 backends/hostmem-memfd.c | 66 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 67 insertions(+), 5 deletions(-)
qapi/qom.json +5 -1
index 6f5c9de0f0..9ebf17bfc7 100644
@@ -763,13 +763,17 @@
 # @seal: if true, create a sealed-file, which will block further
 #     resizing of the memory (default: true)
 #
+# @guest-memfd: if true, use guest-memfd to back the memory region.
+#     (default: false, since: 11.0)
+#
 # Since: 2.12
 ##
 { 'struct': 'MemoryBackendMemfdProperties',
   'base': 'MemoryBackendProperties',
   'data': { '*hugetlb': 'bool',
             '*hugetlbsize': 'size',
-            '*seal': 'bool' },
+            '*seal': 'bool',
+            '*guest-memfd': 'bool' },
   'if': 'CONFIG_LINUX' }
 
 ##
backends/hostmem-memfd.c +62 -4

96 lines not on this page · raw message

Signature · 1 line
2.50.1
author
#1 lore raw

PATCH v2 1/9 kvm: Decouple memory attribute check from kvm_guest_memfd_supported

In reply to Peter Xu

From: Xiaoyao Li <[email protected]>

With the mmap support of guest memfd, KVM allows usersapce to create guest memfd serving as normal non-private memory for X86 DEFEAULT VM. However, KVM doesn't support private memory attriute for X86 DEFAULT VM.

Make kvm_guest_memfd_supported not rely on KVM_MEMORY_ATTRIBUTE_PRIVATE and check KVM_MEMORY_ATTRIBUTE_PRIVATE separately when the machine requires guest_memfd to serve as private memory.

This allows QMEU to create guest memfd with mmap to serve as the memory backend for X86 DEFAULT VM.

---
 include/system/kvm.h   | 1 +
 accel/kvm/kvm-all.c    | 8 ++++++--
 accel/stubs/kvm-stub.c | 5 +++++
 system/physmem.c       | 8 ++++++++
 4 files changed, 20 insertions(+), 2 deletions(-)
include/system/kvm.h +1 -0
index 8f9eecf044..b5811c90f1 100644
@@ -561,6 +561,7 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp);
 
 int kvm_set_memory_attributes_private(hwaddr start, uint64_t size);
 int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size);
+bool kvm_private_memory_attribute_supported(void);
 
 int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private);
 
accel/kvm/kvm-all.c +6 -2
index f9254ae654..96c194ce54 100644
@@ -1501,6 +1501,11 @@ int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size)
     return kvm_set_memory_attributes(start, size, 0);
 }
 
+bool kvm_private_memory_attribute_supported(void)
+{
+    return !!(kvm_supported_memory_attributes & KVM_MEMORY_ATTRIBUTE_PRIVATE);
+}
+
 /* Called with KVMMemoryListener.slots_lock held */
 static void kvm_set_phys_mem(KVMMemoryListener *kml,
                              MemoryRegionSection *section, bool add)
@@ -2781,8 +2786,7 @@ static int kvm_init(AccelState *as, MachineState *ms)
     kvm_supported_memory_attributes = kvm_vm_check_extension(s, KVM_CAP_MEMORY_ATTRIBUTES);
     kvm_guest_memfd_supported =
         kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
-        kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2) &&
-        (kvm_supported_memory_attributes & KVM_MEMORY_ATTRIBUTE_PRIVATE);
+        kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
     kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
 
     if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {
accel/stubs/kvm-stub.c +5 -0
index 68cd33ba97..73f04eb589 100644
@@ -125,3 +125,8 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
 {
     return -ENOSYS;
 }
+
+bool kvm_private_memory_attribute_supported(void)
+{
+    return false;
+}
system/physmem.c +8 -0
index c9869e4049..3555d2f6f7 100644
@@ -2211,6 +2211,14 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
                        object_get_typename(OBJECT(current_machine->cgs)));
             goto out_free;
         }
+
+        if (!kvm_private_memory_attribute_supported()) {
+            error_setg(errp, "cannot set up private guest memory for %s: "
+                       " KVM does not support private memory attribute",
+                       object_get_typename(OBJECT(current_machine->cgs)));
+            goto out_free;
+        }
+
         assert(new_block->guest_memfd < 0);
 
         ret = ram_block_coordinated_discard_require(true);
Signature · 1 line
2.50.1
author
#2 lore raw

PATCH v2 2/9 kvm: Detect guest-memfd flags supported

In reply to Peter Xu

Detect supported guest-memfd flags by the current kernel, and reject creations of guest-memfd using invalid flags. When the cap isn't available, then no flag is supported.

---
 accel/kvm/kvm-all.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
accel/kvm/kvm-all.c +11 -0
index 96c194ce54..f477014126 100644
@@ -108,6 +108,7 @@ static int kvm_sstep_flags;
 static bool kvm_immediate_exit;
 static uint64_t kvm_supported_memory_attributes;
 static bool kvm_guest_memfd_supported;
+static uint64_t kvm_guest_memfd_flags_supported;
 static hwaddr kvm_max_slot_size = ~0;
 
 static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
     kvm_guest_memfd_supported =
         kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
         kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
+
+    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
+    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;
+
     kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
 
     if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {
@@ -4492,6 +4497,12 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
         return -1;
     }
 
+    if (flags & ~kvm_guest_memfd_flags_supported) {
+        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,
+                   flags & ~kvm_guest_memfd_flags_supported);
+        return -1;
+    }
+
     fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
     if (fd < 0) {
         error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
Signature · 1 line
2.50.1

Peter Xu

author
#0 lore raw

v2 cover KVM/hostmem: Support init-shared guest-memfd as VM backends

NOTE: The test cases of this series needs a dependency small series:

Based-on: <[email protected]>

[PATCH 0/4] tests/migration-test: Introduce mem_type
https://lore.kernel.org/r/[email protected]

v2 changelog:
- Linux-header update patch dropped because a similar version has landed
- Replaced old patch 2 with a similar but better one from Xiaoyao Li
- Target this series for QEMU 11.0 (hence, update in qapi/ "Since:")
- Added a qtest covering guest-memfd boot and basic precopy migration
- Fix checkpatch issues

=========8<===========

This series allows QEMU to consume init-shared guest-memfd to be a common memory backend. Before this series, guest-memfd was only used in CoCo and the fds will be created implicitly whenever CoCo environment is detected. When used in init-shared mode, the guest-memfd will be specified in the command lines directly just like other types of memory backends.

In the current patchset, I reused the memory-backend-memfd object, rather than creating a new type of object. After all, guest-memfd (at least from userspace POV) works similarly like a memfd, except that it was tailored for VM's use case.

This approach so far also does not involve gmem bindings to KVM instances, hence it is not prone to issues when the same chunk of RAM will be attached to more than one KVM memslots.

Now, instead of using a normal memfd backend using:

-object memory-backend-memfd,id=ID,size=SIZE,share=on

One can also boot a VM with guest-memfd:

-object memory-backend-memfd,id=ID,size=SIZE,share=on,guest-memfd=on

The init-shared guest-memfd relies on almost the latest linux, as the mmap() support just landed v6.18-rc2. When run it on an older qemu, we'll see errors like:

qemu-system-x86_64: KVM does not support guest_memfd

One thing to mention is live migration is by default supported, however postcopy is still currently not supported. The postcopy support will have some kernel dependency work to be merged in Linux first.

Thanks,

Peter Xu (8):
kvm: Detect guest-memfd flags supported
memory: Rename RAM_GUEST_MEMFD to RAM_GUEST_MEMFD_PRIVATE
memory: Rename memory_region_has_guest_memfd() to *_private()
ramblock: Rename guest_memfd to guest_memfd_private
hostmem: Rename guest_memfd to guest_memfd_private
hostmem: Support in-place guest memfd to back a VM tests/migration-test: Support guest-memfd init shared mem type tests/migration-test: Add a precopy test for guest-memfd

Xiaoyao Li (1):
kvm: Decouple memory attribute check from kvm_guest_memfd_supported

 qapi/qom.json                         |  6 ++-
 include/system/hostmem.h              |  2 +-
 include/system/kvm.h                  |  1 +
 include/system/memory.h               | 17 +++----
 include/system/ram_addr.h             |  2 +-
 include/system/ramblock.h             |  7 ++-
 tests/qtest/migration/framework.h     |  4 ++
 accel/kvm/kvm-all.c                   | 27 ++++++++---
 accel/stubs/kvm-stub.c                |  5 ++
 backends/hostmem-file.c               |  2 +-
 backends/hostmem-memfd.c              | 68 +++++++++++++++++++++++++--
 backends/hostmem-ram.c                |  2 +-
 backends/hostmem-shm.c                |  2 +-
 backends/hostmem.c                    |  2 +-
 system/memory.c                       |  7 +--
 system/physmem.c                      | 37 +++++++++------
 tests/qtest/migration/framework.c     | 60 +++++++++++++++++++++++
 tests/qtest/migration/precopy-tests.c | 12 +++++
 18 files changed, 219 insertions(+), 44 deletions(-)
Signature · 1 line
2.50.1
Reviewed-by on v2 2/9
#3 lore raw

Re: [PATCH v2 2/9] kvm: Detect guest-memfd flags supported

In reply to Peter Xu
Quoted 5 lines · Peter Xu

Detect supported guest-memfd flags by the current kernel, and reject creations of guest-memfd using invalid flags. When the cap isn't available, then no flag is supported.

Quoted 40 lines
---
  accel/kvm/kvm-all.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
accel/kvm/kvm-all.c +11 -0
index 96c194ce54..f477014126 100644
@@ -108,6 +108,7 @@ static int kvm_sstep_flags;
  static bool kvm_immediate_exit;
  static uint64_t kvm_supported_memory_attributes;
  static bool kvm_guest_memfd_supported;
+static uint64_t kvm_guest_memfd_flags_supported;
  static hwaddr kvm_max_slot_size = ~0;
 
  static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
      kvm_guest_memfd_supported =
          kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
          kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
+
+    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
+    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;
+
      kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
 
      if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {
@@ -4492,6 +4497,12 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
          return -1;
      }
 
+    if (flags & ~kvm_guest_memfd_flags_supported) {
+        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,
+                   flags & ~kvm_guest_memfd_flags_supported);
+        return -1;
+    }
+
      fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
      if (fd < 0) {
          error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
Reviewed-by on v2 3/9
#8 lore raw

Re: [PATCH v2 3/9] memory: Rename RAM_GUEST_MEMFD to RAM_GUEST_MEMFD_PRIVATE

In reply to Peter Xu
Quoted 9 lines · Peter Xu

This name is too generic, and can conflict with in-place guest-memfd support. Add a _PRIVATE suffix to show what it really means: it is always silently using an internal guest-memfd to back a shared host backend, rather than used in-place.

This paves way for in-place guest-memfd, which means we can have a ramblock that allocates pages completely from guest-memfd (private or shared).

Reviewed-by on v2 4/9
#10 lore raw

Re: [PATCH v2 4/9] memory: Rename memory_region_has_guest_memfd() to *_private()

In reply to Peter Xu
Quoted 16 lines · Peter Xu

Rename the function with "_private" suffix, to show that it returns true only if it has an internal guest-memfd to back private pages (rather than in-place guest-memfd).

---
  include/system/memory.h | 6 +++---
  accel/kvm/kvm-all.c     | 6 +++---
  system/memory.c         | 2 +-
  3 files changed, 7 insertions(+), 7 deletions(-)
include/system/memory.h +0 -0
index 2c1a5e06b4..4428701a9f 100644

include/system/memory.h static inline bool memory_region_is_romd(MemoryRegion *mr) · line 1827

Quoted 7 lines
include/system/memory.h +2 -2
@@ -1823,14 +1823,14 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  bool memory_region_is_protected(MemoryRegion *mr);
 
  /**
- * memory_region_has_guest_memfd: check whether a memory region has guest_memfd
- *     associated
+ * memory_region_has_guest_memfd_private: check whether a memory region has
+ *     guest_memfd associated

Nit: maybe change it to "guest_memfd_private associated", and maybe put this patch after patch 5?

Otherwise,

Quoted 54 lines
include/system/memory.h +1 -1
   *
   * Returns %true if a memory region's ram_block has valid guest_memfd assigned.
   *
   * @mr: the memory region being queried
   */
-bool memory_region_has_guest_memfd(MemoryRegion *mr);
+bool memory_region_has_guest_memfd_private(MemoryRegion *mr);
 
  /**
   * memory_region_get_iommu: check whether a memory region is an iommu
accel/kvm/kvm-all.c +3 -3
index f477014126..320315f50c 100644
@@ -666,7 +666,7 @@ static int kvm_mem_flags(MemoryRegion *mr)
      if (readonly && kvm_readonly_mem_allowed) {
          flags |= KVM_MEM_READONLY;
      }
-    if (memory_region_has_guest_memfd(mr)) {
+    if (memory_region_has_guest_memfd_private(mr)) {
          assert(kvm_guest_memfd_supported);
          flags |= KVM_MEM_GUEST_MEMFD;
      }
@@ -1615,7 +1615,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
              abort();
          }
 
-        if (memory_region_has_guest_memfd(mr)) {
+        if (memory_region_has_guest_memfd_private(mr)) {
              err = kvm_set_memory_attributes_private(start_addr, slot_size);
              if (err) {
                  error_report("%s: failed to set memory attribute private: %s",
@@ -3101,7 +3101,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
          return ret;
      }
 
-    if (!memory_region_has_guest_memfd(mr)) {
+    if (!memory_region_has_guest_memfd_private(mr)) {
          /*
           * Because vMMIO region must be shared, guest TD may convert vMMIO
           * region to shared explicitly.  Don't complain such case.  See
system/memory.c +1 -1
index 81b7bff42d..15964160ee 100644
@@ -1897,7 +1897,7 @@ bool memory_region_is_protected(MemoryRegion *mr)
      return mr->ram && (mr->ram_block->flags & RAM_PROTECTED);
  }
 
-bool memory_region_has_guest_memfd(MemoryRegion *mr)
+bool memory_region_has_guest_memfd_private(MemoryRegion *mr)
  {
      return mr->ram_block && mr->ram_block->guest_memfd >= 0;
  }
Reviewed-by on v2 6/9
#15 lore raw

Re: [PATCH v2 6/9] hostmem: Rename guest_memfd to guest_memfd_private

In reply to Peter Xu
Quoted 6 lines · Peter Xu

Rename the HostMemoryBackend.guest_memfd field to reflect what it really means, on whether it needs guest_memfd to back its private portion of mapping. This will help on clearance when we introduce in-place guest_memfd for hostmem.

<...>

backends/hostmem.c static void host_memory_backend_init(Object *obj) · line 291

Quoted 10 lines
backends/hostmem.c +1 -1
index 35734d6f4d..70450733db 100644
@@ -288,7 +288,7 @@ static void host_memory_backend_init(Object *obj)
      /* TODO: convert access to globals to compat properties */
      backend->merge = machine_mem_merge(machine);
      backend->dump = machine_dump_guest_core(machine);
-    backend->guest_memfd = machine_require_guest_memfd(machine);
+    backend->guest_memfd_private = machine_require_guest_memfd(machine);

btw, how about a separate patch to rename

machine_require_guest_memfd() to machine_require_guest_memfd_private()?

and another patch to rename memory_region_init_ram_guest_memfd()?

Quoted 3 lines
backends/hostmem.c +0 -0
      backend->reserve = true;
      backend->prealloc_threads = machine->smp.cpus;
  }
Reviewed-by on v2 7/9
#18 lore raw

Re: [PATCH v2 7/9] hostmem: Support in-place guest memfd to back a VM

In reply to Peter Xu
Quoted 10 lines · Peter Xu

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it in-place. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, in place), rather than having another backend supporting the pages converted to shared.

Quoted 7 lines

To use the in-place guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with in-place guest_memfd.

overall looks good to me except a few comments below,

Quoted 63 lines
---
  qapi/qom.json            |  6 +++-
  backends/hostmem-memfd.c | 66 +++++++++++++++++++++++++++++++++++++---
  2 files changed, 67 insertions(+), 5 deletions(-)
qapi/qom.json +5 -1
index 6f5c9de0f0..9ebf17bfc7 100644
@@ -763,13 +763,17 @@
  # @seal: if true, create a sealed-file, which will block further
  #     resizing of the memory (default: true)
  #
+# @guest-memfd: if true, use guest-memfd to back the memory region.
+#     (default: false, since: 11.0)
+#
  # Since: 2.12
  ##
  { 'struct': 'MemoryBackendMemfdProperties',
    'base': 'MemoryBackendProperties',
    'data': { '*hugetlb': 'bool',
              '*hugetlbsize': 'size',
-            '*seal': 'bool' },
+            '*seal': 'bool',
+            '*guest-memfd': 'bool' },
    'if': 'CONFIG_LINUX' }
 
  ##
backends/hostmem-memfd.c +9 -4
index ea93f034e4..1fa16c1e1d 100644
@@ -18,6 +18,8 @@
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "migration/cpr.h"
+#include "system/kvm.h"
+#include <linux/kvm.h>
 
  OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
 
@@ -28,6 +30,13 @@ struct HostMemoryBackendMemfd {
      bool hugetlb;
      uint64_t hugetlbsize;
      bool seal;
+    /*
+     * NOTE: this differs from HostMemoryBackend's guest_memfd_private,
+     * which represents a internally private guest-memfd that only backs
+     * private pages.  Instead, this flag marks the memory backend will
+     * 100% use the guest-memfd pages in-place.
+     */
+    bool guest_memfd;
  };
 
  static bool
@@ -47,10 +56,40 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
          goto have_fd;
      }
 
-    fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
-                           m->hugetlb, m->hugetlbsize, m->seal ?
-                           F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
-                           errp);

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 68

Quoted 10 lines
backends/hostmem-memfd.c +10 -0
+    if (m->guest_memfd) {
+        /* User choose to use in-place guest-memfd to back the VM.. */
+        if (!backend->share) {
+            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            return false;
+        }
+
+        /*
+         * This is the request to have a guest-memfd to back private pages.
+         * In-place guest-memfd doesn't work like that.  Disable it for now

This seems not correct to me. I think in-place guest-memfd can work with guest_memfd_private. The former serves as shared memory and referenced by the userspace_addr while the latter serves as private memory referenced by the fd of guest_memfd.

While the argument of "disable it for now to make it simple" does make sense to me.

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 76

Quoted 8 lines
backends/hostmem-memfd.c +8 -0
+         * to make it simple, so that each memory backend can only have
+         * guest-memfd either as private, or fully shared.
+         */
+        if (backend->guest_memfd_private) {
+            error_setg(errp, "In-place guest-memfd cannot be used with another "
+                       "private guest-memfd");
+            return false;
+        }

please add the following check as I commented in v1:

	if (!kvm_enabled()) {
		error_setg(errp, "in-place guest-memfd requires KVM");
		return false;
	}

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 85

Quoted 8 lines
backends/hostmem-memfd.c +8 -0
+        /* TODO: add huge page support */
+        fd = kvm_create_guest_memfd(backend->size,
+                                    GUEST_MEMFD_FLAG_MMAP |
+                                    GUEST_MEMFD_FLAG_INIT_SHARED,
+                                    errp);
+        if (fd < 0) {
+            return false;
+        }

how about just removing the fd check here because ...

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 95

Quoted 10 lines
backends/hostmem-memfd.c +7 -0
+    } else {
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
+                               m->hugetlb, m->hugetlbsize, m->seal ?
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
+                               errp);
+    }
+
      if (fd == -1) {
          return false;
      }

... the existing check can work for the guest memfd as well.

Quoted 33 lines
backends/hostmem-memfd.c +19 -0
@@ -65,6 +104,18 @@ have_fd:
                                            backend->size, ram_flags, fd, 0, errp);
  }
 
+static bool
+memfd_backend_get_guest_memfd(Object *o, Error **errp)
+{
+    return MEMORY_BACKEND_MEMFD(o)->guest_memfd;
+}
+
+static void
+memfd_backend_set_guest_memfd(Object *o, bool value, Error **errp)
+{
+    MEMORY_BACKEND_MEMFD(o)->guest_memfd = value;
+}
+
  static bool
  memfd_backend_get_hugetlb(Object *o, Error **errp)
  {
@@ -152,6 +203,13 @@ memfd_backend_class_init(ObjectClass *oc, const void *data)
          object_class_property_set_description(oc, "hugetlbsize",
                                                "Huge pages size (ex: 2M, 1G)");
      }
+
+    object_class_property_add_bool(oc, "guest-memfd",
+                                   memfd_backend_get_guest_memfd,
+                                   memfd_backend_set_guest_memfd);
+    object_class_property_set_description(oc, "guest-memfd",
+                                          "Use guest memfd");
+
      object_class_property_add_bool(oc, "seal",
                                     memfd_backend_get_seal,
                                     memfd_backend_set_seal);
author
#11 lore raw

Re: [PATCH v2 4/9] memory: Rename memory_region_has_guest_memfd() to *_private()

In reply to Xiaoyao Li
Quoted 4 lines · Xiaoyao Li
Quoted 3 lines · Peter Xu

Rename the function with "_private" suffix, to show that it returns true only if it has an internal guest-memfd to back private pages (rather than in-place guest-memfd).

PS: I forgot to update here, I'll use "fully shared" to replace "in-place".

Quoted 8 lines
Quoted 8 lines
---
  include/system/memory.h | 6 +++---
  accel/kvm/kvm-all.c     | 6 +++---
  system/memory.c         | 2 +-
  3 files changed, 7 insertions(+), 7 deletions(-)
Quoted 14 lines

include/system/memory.h static inline bool memory_region_is_romd(MemoryRegion *mr) · line 1826

Quoted 11 lines
include/system/memory.h +2 -2
index 2c1a5e06b4..4428701a9f 100644
@@ -1823,14 +1823,14 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
  bool memory_region_is_protected(MemoryRegion *mr);
  /**
- * memory_region_has_guest_memfd: check whether a memory region has guest_memfd
- *     associated
+ * memory_region_has_guest_memfd_private: check whether a memory region has
+ *     guest_memfd associated

Nit: maybe change it to "guest_memfd_private associated", and maybe put this patch after patch 5?

Agree, though maybe I should do this change in the other ramblock patch (and move that one before this)?

Quoted 4 lines

Otherwise,

Thanks,

Signature · 1 line
Peter Xu
author
#16 lore raw

Re: [PATCH v2 6/9] hostmem: Rename guest_memfd to guest_memfd_private

In reply to Xiaoyao Li
Quoted 25 lines · Xiaoyao Li
Quoted 6 lines · Peter Xu

Rename the HostMemoryBackend.guest_memfd field to reflect what it really means, on whether it needs guest_memfd to back its private portion of mapping. This will help on clearance when we introduce in-place guest_memfd for hostmem.

<...>

backends/hostmem.c static void host_memory_backend_init(Object *obj) · line 291

Quoted 10 lines
backends/hostmem.c +1 -1
index 35734d6f4d..70450733db 100644
@@ -288,7 +288,7 @@ static void host_memory_backend_init(Object *obj)
      /* TODO: convert access to globals to compat properties */
      backend->merge = machine_mem_merge(machine);
      backend->dump = machine_dump_guest_core(machine);
-    backend->guest_memfd = machine_require_guest_memfd(machine);
+    backend->guest_memfd_private = machine_require_guest_memfd(machine);

btw, how about a separate patch to rename

Quoted 3 lines

machine_require_guest_memfd() to machine_require_guest_memfd_private()?

and another patch to rename memory_region_init_ram_guest_memfd()?

Sounds all reasonable, will do.

Thanks,

Signature · 1 line
Peter Xu
author
#19 lore raw

Re: [PATCH v2 7/9] hostmem: Support in-place guest memfd to back a VM

In reply to Xiaoyao Li
Quoted 97 lines · Xiaoyao Li
Quoted 10 lines · Peter Xu

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it in-place. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, in place), rather than having another backend supporting the pages converted to shared.

Quoted 7 lines

To use the in-place guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with in-place guest_memfd.

overall looks good to me except a few comments below,

Quoted 58 lines
---
  qapi/qom.json            |  6 +++-
  backends/hostmem-memfd.c | 66 +++++++++++++++++++++++++++++++++++++---
  2 files changed, 67 insertions(+), 5 deletions(-)
qapi/qom.json +5 -1
index 6f5c9de0f0..9ebf17bfc7 100644
@@ -763,13 +763,17 @@
  # @seal: if true, create a sealed-file, which will block further
  #     resizing of the memory (default: true)
  #
+# @guest-memfd: if true, use guest-memfd to back the memory region.
+#     (default: false, since: 11.0)
+#
  # Since: 2.12
  ##
  { 'struct': 'MemoryBackendMemfdProperties',
    'base': 'MemoryBackendProperties',
    'data': { '*hugetlb': 'bool',
              '*hugetlbsize': 'size',
-            '*seal': 'bool' },
+            '*seal': 'bool',
+            '*guest-memfd': 'bool' },
    'if': 'CONFIG_LINUX' }
  ##
backends/hostmem-memfd.c +9 -4
index ea93f034e4..1fa16c1e1d 100644
@@ -18,6 +18,8 @@
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "migration/cpr.h"
+#include "system/kvm.h"
+#include <linux/kvm.h>
  OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
@@ -28,6 +30,13 @@ struct HostMemoryBackendMemfd {
      bool hugetlb;
      uint64_t hugetlbsize;
      bool seal;
+    /*
+     * NOTE: this differs from HostMemoryBackend's guest_memfd_private,
+     * which represents a internally private guest-memfd that only backs
+     * private pages.  Instead, this flag marks the memory backend will
+     * 100% use the guest-memfd pages in-place.
+     */
+    bool guest_memfd;
  };
  static bool
@@ -47,10 +56,40 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
          goto have_fd;
      }
-    fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
-                           m->hugetlb, m->hugetlbsize, m->seal ?
-                           F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
-                           errp);

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 67

Quoted 10 lines
backends/hostmem-memfd.c +10 -0
+    if (m->guest_memfd) {
+        /* User choose to use in-place guest-memfd to back the VM.. */
+        if (!backend->share) {
+            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            return false;
+        }
+
+        /*
+         * This is the request to have a guest-memfd to back private pages.
+         * In-place guest-memfd doesn't work like that.  Disable it for now

This seems not correct to me. I think in-place guest-memfd can work with guest_memfd_private. The former serves as shared memory and referenced by the userspace_addr while the latter serves as private memory referenced by the fd of guest_memfd.

Quoted 2 lines

While the argument of "disable it for now to make it simple" does make sense to me.

Oops, I forgot to touch up quite a few places that kept mentioning in-place, sorry.

I'll squash this diff into this patch when repost:

backends/hostmem-memfd.c +4 -4
index 1fa16c1e1d..e9e288651e 100644
@@ -57,16 +57,16 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     }
 
     if (m->guest_memfd) {
-        /* User choose to use in-place guest-memfd to back the VM.. */
+        /* User choose to use fully shared guest-memfd to back the VM.. */
         if (!backend->share) {
-            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            error_setg(errp, "Guest-memfd=on must be used with share=on");
             return false;
         }
 
         /*
          * This is the request to have a guest-memfd to back private pages.
-         * In-place guest-memfd doesn't work like that.  Disable it for now
-         * to make it simple, so that each memory backend can only have
+         * Fully shared guest-memfd doesn't work like that.  Disable it for
+         * now to make it simple, so that each memory backend can only have
          * guest-memfd either as private, or fully shared.
          */
         if (backend->guest_memfd_private) {

I'll also fix the commit message on in-place, now the one to be reposted:

hostmem: Support fully shared guest memfd to back a VM

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it fully shared. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, fully shared), rather than having another backend supporting the pages converted to shared.

To use the fully shared guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with fully shared guest_memfd.

I'll not take your R-b as of now, please check and ack again if you see fit after reading.

Quoted 10 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 75

Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+         * to make it simple, so that each memory backend can only have
+         * guest-memfd either as private, or fully shared.
+         */
+        if (backend->guest_memfd_private) {
+            error_setg(errp, "In-place guest-memfd cannot be used with another "
+                       "private guest-memfd");
+            return false;
+        }
Quoted 6 lines

please add the following check as I commented in v1:

	if (!kvm_enabled()) {
		error_setg(errp, "in-place guest-memfd requires KVM");
		return false;
	}

IMHO it's redundant to set here, when kvm not enabled, kvm_create_guest_memfd() should be a stub.

However indeed I found the stub didn't set an error, so how about add one trivial patch to add a verbal error for it instead?

commit aeeaba6dfc68a1c89af90c12f36cb8fe48faecfd

Author: Peter Xu <[email protected]>
Date:   Thu Dec 11 11:19:44 2025 -0500

kvm/stub: Provide explicit error for kvm_create_guest_memfd()

So that there will be a verbal string returned when kvm not enabled.

---
 accel/stubs/kvm-stub.c | 1 +
 1 file changed, 1 insertion(+)
accel/stubs/kvm-stub.c +1 -0
index 73f04eb589..01b1d6285e 100644
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
 
 int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
 {
+    error_setg(errp, "KVM is not enabled");
     return -ENOSYS;
 }
 

IIUC it'll achieve the same goal with better layering.

Quoted 11 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 83

Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+        /* TODO: add huge page support */
+        fd = kvm_create_guest_memfd(backend->size,
+                                    GUEST_MEMFD_FLAG_MMAP |
+                                    GUEST_MEMFD_FLAG_INIT_SHARED,
+                                    errp);
+        if (fd < 0) {
+            return false;
+        }

how about just removing the fd check here because ...

We needed it because at least the stub returns -ENOSYS..

I can remove it, but I'll need to change below to "fd<0" check. That I can do.

Thanks,

Quoted 47 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 93

Quoted 11 lines
backends/hostmem-memfd.c +7 -0
+    } else {
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
+                               m->hugetlb, m->hugetlbsize, m->seal ?
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
+                               errp);
+    }
+
      if (fd == -1) {
          return false;
      }

... the existing check can work for the guest memfd as well.

Quoted 32 lines
backends/hostmem-memfd.c +19 -0
@@ -65,6 +104,18 @@ have_fd:
                                            backend->size, ram_flags, fd, 0, errp);
  }
+static bool
+memfd_backend_get_guest_memfd(Object *o, Error **errp)
+{
+    return MEMORY_BACKEND_MEMFD(o)->guest_memfd;
+}
+
+static void
+memfd_backend_set_guest_memfd(Object *o, bool value, Error **errp)
+{
+    MEMORY_BACKEND_MEMFD(o)->guest_memfd = value;
+}
+
  static bool
  memfd_backend_get_hugetlb(Object *o, Error **errp)
  {
@@ -152,6 +203,13 @@ memfd_backend_class_init(ObjectClass *oc, const void *data)
          object_class_property_set_description(oc, "hugetlbsize",
                                                "Huge pages size (ex: 2M, 1G)");
      }
+
+    object_class_property_add_bool(oc, "guest-memfd",
+                                   memfd_backend_get_guest_memfd,
+                                   memfd_backend_set_guest_memfd);
+    object_class_property_set_description(oc, "guest-memfd",
+                                          "Use guest memfd");
+
      object_class_property_add_bool(oc, "seal",
                                     memfd_backend_get_seal,
                                     memfd_backend_set_seal);
Signature · 1 line
Peter Xu
#20 lore raw

Re: [PATCH v2 7/9] hostmem: Support in-place guest memfd to back a VM

Quoted 111 lines · Peter Xu
Quoted 97 lines · Xiaoyao Li
Quoted 10 lines · Peter Xu

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it in-place. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, in place), rather than having another backend supporting the pages converted to shared.

Quoted 7 lines

To use the in-place guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with in-place guest_memfd.

overall looks good to me except a few comments below,

Quoted 58 lines
---
   qapi/qom.json            |  6 +++-
   backends/hostmem-memfd.c | 66 +++++++++++++++++++++++++++++++++++++---
   2 files changed, 67 insertions(+), 5 deletions(-)
qapi/qom.json +5 -1
index 6f5c9de0f0..9ebf17bfc7 100644
@@ -763,13 +763,17 @@
   # @seal: if true, create a sealed-file, which will block further
   #     resizing of the memory (default: true)
   #
+# @guest-memfd: if true, use guest-memfd to back the memory region.
+#     (default: false, since: 11.0)
+#
   # Since: 2.12
   ##
   { 'struct': 'MemoryBackendMemfdProperties',
     'base': 'MemoryBackendProperties',
     'data': { '*hugetlb': 'bool',
               '*hugetlbsize': 'size',
-            '*seal': 'bool' },
+            '*seal': 'bool',
+            '*guest-memfd': 'bool' },
     'if': 'CONFIG_LINUX' }
   ##
backends/hostmem-memfd.c +9 -4
index ea93f034e4..1fa16c1e1d 100644
@@ -18,6 +18,8 @@
   #include "qapi/error.h"
   #include "qom/object.h"
   #include "migration/cpr.h"
+#include "system/kvm.h"
+#include <linux/kvm.h>
   OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
@@ -28,6 +30,13 @@ struct HostMemoryBackendMemfd {
       bool hugetlb;
       uint64_t hugetlbsize;
       bool seal;
+    /*
+     * NOTE: this differs from HostMemoryBackend's guest_memfd_private,
+     * which represents a internally private guest-memfd that only backs
+     * private pages.  Instead, this flag marks the memory backend will
+     * 100% use the guest-memfd pages in-place.
+     */
+    bool guest_memfd;
   };
   static bool
@@ -47,10 +56,40 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
           goto have_fd;
       }
-    fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
-                           m->hugetlb, m->hugetlbsize, m->seal ?
-                           F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
-                           errp);

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 67

Quoted 10 lines
backends/hostmem-memfd.c +10 -0
+    if (m->guest_memfd) {
+        /* User choose to use in-place guest-memfd to back the VM.. */
+        if (!backend->share) {
+            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            return false;
+        }
+
+        /*
+         * This is the request to have a guest-memfd to back private pages.
+         * In-place guest-memfd doesn't work like that.  Disable it for now

This seems not correct to me. I think in-place guest-memfd can work with guest_memfd_private. The former serves as shared memory and referenced by the userspace_addr while the latter serves as private memory referenced by the fd of guest_memfd.

Quoted 2 lines

While the argument of "disable it for now to make it simple" does make sense to me.

Oops, I forgot to touch up quite a few places that kept mentioning in-place, sorry.

I'll squash this diff into this patch when repost:

backends/hostmem-memfd.c +0 -0
index 1fa16c1e1d..e9e288651e 100644

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 62

Quoted 8 lines
backends/hostmem-memfd.c +2 -2
@@ -57,16 +57,16 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      }
 
      if (m->guest_memfd) {
-        /* User choose to use in-place guest-memfd to back the VM.. */
+        /* User choose to use fully shared guest-memfd to back the VM.. */
          if (!backend->share) {
-            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            error_setg(errp, "Guest-memfd=on must be used with share=on");

lower-case the guest-memfd? since it's the name of the property?

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 72

Quoted 12 lines
backends/hostmem-memfd.c +2 -2
              return false;
          }
 
          /*
           * This is the request to have a guest-memfd to back private pages.
-         * In-place guest-memfd doesn't work like that.  Disable it for now
-         * to make it simple, so that each memory backend can only have
+         * Fully shared guest-memfd doesn't work like that.  Disable it for
+         * now to make it simple, so that each memory backend can only have
           * guest-memfd either as private, or fully shared.
           */
          if (backend->guest_memfd_private) {

After rethinking on it, I think we just remove the check. There is nothing from QEMU side to prevent such usage. It is KVM currently that doesn't support INIT_SHARED guest-memfd for confidential VMs.

If we change to KVM to allow it:

arch/x86/kvm/x86.c +2 -1
index 8bb8830561d4..a658b636a9cf 100644
@@ -13957,7 +13957,8 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
  */
 bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
 {
-       return !kvm_arch_has_private_mem(kvm);
+       return true;
+
 

and remove this check in QEMU, it can actually boot a TDX guest with fully-shared guest-memfd back'ed as shared part.

Quoted 10 lines

I'll also fix the commit message on in-place, now the one to be reposted:

hostmem: Support fully shared guest memfd to back a VM

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it fully shared. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

Quoted 3 lines

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, fully shared), rather than having another backend supporting the pages converted to shared.

As above, I think what the patch achieves is to enable guest-memfd (with MMAP and INIT_SHARED) to back shared memory. And it is not conflicted with using another guest-memfd to back private memory.

Quoted 21 lines

To use the fully shared guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with fully shared guest_memfd.

I'll not take your R-b as of now, please check and ack again if you see fit after reading.

Quoted 12 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 75

Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+         * to make it simple, so that each memory backend can only have
+         * guest-memfd either as private, or fully shared.
+         */
+        if (backend->guest_memfd_private) {
+            error_setg(errp, "In-place guest-memfd cannot be used with another "
+                       "private guest-memfd");
+            return false;
+        }

please add the following check as I commented in v1:

Quoted 7 lines
Quoted 4 lines
	if (!kvm_enabled()) {
		error_setg(errp, "in-place guest-memfd requires KVM");
		return false;
	}

IMHO it's redundant to set here, when kvm not enabled, kvm_create_guest_memfd() should be a stub.

No. The KVM stub is for the case where KVM is disable at compile time.

The kvm_enabled() check here is for the case where users use different accelerators other than KVM, e.g., -accel tcg.

Quoted 40 lines

However indeed I found the stub didn't set an error, so how about add one trivial patch to add a verbal error for it instead?

commit aeeaba6dfc68a1c89af90c12f36cb8fe48faecfd

Author: Peter Xu <[email protected]>
Date:   Thu Dec 11 11:19:44 2025 -0500

kvm/stub: Provide explicit error for kvm_create_guest_memfd()

So that there will be a verbal string returned when kvm not enabled.

---
  accel/stubs/kvm-stub.c | 1 +
  1 file changed, 1 insertion(+)
accel/stubs/kvm-stub.c +1 -0
index 73f04eb589..01b1d6285e 100644
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
 
  int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
  {
+    error_setg(errp, "KVM is not enabled");
      return -ENOSYS;
  }
 

IIUC it'll achieve the same goal with better layering.

Quoted 10 lines
Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+        /* TODO: add huge page support */
+        fd = kvm_create_guest_memfd(backend->size,
+                                    GUEST_MEMFD_FLAG_MMAP |
+                                    GUEST_MEMFD_FLAG_INIT_SHARED,
+                                    errp);
+        if (fd < 0) {
+            return false;
+        }
Quoted 6 lines
Quoted 1 line

how about just removing the fd check here because ...

We needed it because at least the stub returns -ENOSYS..

I can remove it, but I'll need to change below to "fd<0" check. That I can do.

yeah, change to "fd < 0" looks better.

Quoted 16 lines

Thanks,

Quoted 14 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 93

Quoted 11 lines
backends/hostmem-memfd.c +7 -0
+    } else {
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
+                               m->hugetlb, m->hugetlbsize, m->seal ?
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
+                               errp);
+    }
+
       if (fd == -1) {
           return false;
       }

... the existing check can work for the guest memfd as well.

#4 lore raw

Re: [PATCH v2 2/9] kvm: Detect guest-memfd flags supported

In reply to Peter Xu
Quoted 33 lines · Peter Xu

Detect supported guest-memfd flags by the current kernel, and reject creations of guest-memfd using invalid flags. When the cap isn't available, then no flag is supported.

---
  accel/kvm/kvm-all.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 96c194ce54..f477014126 100644
@@ -108,6 +108,7 @@ static int kvm_sstep_flags;
  static bool kvm_immediate_exit;
  static uint64_t kvm_supported_memory_attributes;
  static bool kvm_guest_memfd_supported;
+static uint64_t kvm_guest_memfd_flags_supported;
  static hwaddr kvm_max_slot_size = ~0;
 
  static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
      kvm_guest_memfd_supported =
          kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
          kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
+
+    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
+    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;
+
      kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
 
      if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {

accel/kvm/kvm-all.c int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp) · line 4501

Quoted 5 lines
accel/kvm/kvm-all.c +2 -0
@@ -4492,6 +4497,12 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
          return -1;
      }
 
+    if (flags & ~kvm_guest_memfd_flags_supported) {
+        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,

I'm thinking if need to add "for current VM" like:

KVM does not support guest-memfd flag: xxx for current VM

because kvm_guest_memfd_flags_supported is got from VM-scope CAP and varies for different VM types.

Quoted 7 lines
accel/kvm/kvm-all.c +4 -0
+                   flags & ~kvm_guest_memfd_flags_supported);
+        return -1;
+    }
+
      fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
      if (fd < 0) {
          error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
author
#5 lore raw

Re: [PATCH v2 2/9] kvm: Detect guest-memfd flags supported

Quoted 39 lines · Xiaoyao Li
Quoted 31 lines · Peter Xu

Detect supported guest-memfd flags by the current kernel, and reject creations of guest-memfd using invalid flags. When the cap isn't available, then no flag is supported.

---
  accel/kvm/kvm-all.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 96c194ce54..f477014126 100644
@@ -108,6 +108,7 @@ static int kvm_sstep_flags;
  static bool kvm_immediate_exit;
  static uint64_t kvm_supported_memory_attributes;
  static bool kvm_guest_memfd_supported;
+static uint64_t kvm_guest_memfd_flags_supported;
  static hwaddr kvm_max_slot_size = ~0;
  static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
      kvm_guest_memfd_supported =
          kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
          kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
+
+    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
+    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;
+
      kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
      if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {

accel/kvm/kvm-all.c int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp) · line 4500

Quoted 4 lines
accel/kvm/kvm-all.c +2 -0
@@ -4492,6 +4497,12 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
          return -1;
      }
+    if (flags & ~kvm_guest_memfd_flags_supported) {
+        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,

I'm thinking if need to add "for current VM" like:

Quoted 4 lines

KVM does not support guest-memfd flag: xxx for current VM

because kvm_guest_memfd_flags_supported is got from VM-scope CAP and varies for different VM types.

Sure I can amend it. Though I plan to change the sentence slightly:

     if (flags & ~kvm_guest_memfd_flags_supported) {
-        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,
+        error_setg(errp, "Current KVM instance does not support "
+                   "guest-memfd flag: 0x%"PRIx64,
                    flags & ~kvm_guest_memfd_flags_supported);
         return -1;
     }

Thanks,

Quoted 9 lines
Quoted 8 lines
accel/kvm/kvm-all.c +4 -0
+                   flags & ~kvm_guest_memfd_flags_supported);
+        return -1;
+    }
+
      fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
      if (fd < 0) {
          error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
Signature · 1 line
Peter Xu
author
#21 lore raw

Re: [PATCH v2 7/9] hostmem: Support in-place guest memfd to back a VM

In reply to Xiaoyao Li
Quoted 107 lines · Xiaoyao Li
Quoted 106 lines · Peter Xu
Quoted 97 lines · Xiaoyao Li
Quoted 10 lines · Peter Xu

Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it in-place. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.

This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, in place), rather than having another backend supporting the pages converted to shared.

Quoted 7 lines

To use the in-place guest-memfd, one can add a memfd object with:

-object memory-backend-memfd,guest-memfd=on,share=on

Note that share=on is required with in-place guest_memfd.

overall looks good to me except a few comments below,

Quoted 58 lines
---
   qapi/qom.json            |  6 +++-
   backends/hostmem-memfd.c | 66 +++++++++++++++++++++++++++++++++++++---
   2 files changed, 67 insertions(+), 5 deletions(-)
qapi/qom.json +5 -1
index 6f5c9de0f0..9ebf17bfc7 100644
@@ -763,13 +763,17 @@
   # @seal: if true, create a sealed-file, which will block further
   #     resizing of the memory (default: true)
   #
+# @guest-memfd: if true, use guest-memfd to back the memory region.
+#     (default: false, since: 11.0)
+#
   # Since: 2.12
   ##
   { 'struct': 'MemoryBackendMemfdProperties',
     'base': 'MemoryBackendProperties',
     'data': { '*hugetlb': 'bool',
               '*hugetlbsize': 'size',
-            '*seal': 'bool' },
+            '*seal': 'bool',
+            '*guest-memfd': 'bool' },
     'if': 'CONFIG_LINUX' }
   ##
backends/hostmem-memfd.c +9 -4
index ea93f034e4..1fa16c1e1d 100644
@@ -18,6 +18,8 @@
   #include "qapi/error.h"
   #include "qom/object.h"
   #include "migration/cpr.h"
+#include "system/kvm.h"
+#include <linux/kvm.h>
   OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD)
@@ -28,6 +30,13 @@ struct HostMemoryBackendMemfd {
       bool hugetlb;
       uint64_t hugetlbsize;
       bool seal;
+    /*
+     * NOTE: this differs from HostMemoryBackend's guest_memfd_private,
+     * which represents a internally private guest-memfd that only backs
+     * private pages.  Instead, this flag marks the memory backend will
+     * 100% use the guest-memfd pages in-place.
+     */
+    bool guest_memfd;
   };
   static bool
@@ -47,10 +56,40 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
           goto have_fd;
       }
-    fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
-                           m->hugetlb, m->hugetlbsize, m->seal ?
-                           F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
-                           errp);

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 67

Quoted 10 lines
backends/hostmem-memfd.c +10 -0
+    if (m->guest_memfd) {
+        /* User choose to use in-place guest-memfd to back the VM.. */
+        if (!backend->share) {
+            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            return false;
+        }
+
+        /*
+         * This is the request to have a guest-memfd to back private pages.
+         * In-place guest-memfd doesn't work like that.  Disable it for now

This seems not correct to me. I think in-place guest-memfd can work with guest_memfd_private. The former serves as shared memory and referenced by the userspace_addr while the latter serves as private memory referenced by the fd of guest_memfd.

Quoted 2 lines

While the argument of "disable it for now to make it simple" does make sense to me.

Oops, I forgot to touch up quite a few places that kept mentioning in-place, sorry.

I'll squash this diff into this patch when repost:

Quoted 14 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 61

Quoted 12 lines
backends/hostmem-memfd.c +2 -2
index 1fa16c1e1d..e9e288651e 100644
@@ -57,16 +57,16 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
      }
      if (m->guest_memfd) {
-        /* User choose to use in-place guest-memfd to back the VM.. */
+        /* User choose to use fully shared guest-memfd to back the VM.. */
          if (!backend->share) {
-            error_setg(errp, "In-place guest-memfd must be used with share=on");
+            error_setg(errp, "Guest-memfd=on must be used with share=on");

lower-case the guest-memfd? since it's the name of the property?

Sure.

Quoted 31 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 70

Quoted 12 lines
backends/hostmem-memfd.c +2 -2
              return false;
          }
          /*
           * This is the request to have a guest-memfd to back private pages.
-         * In-place guest-memfd doesn't work like that.  Disable it for now
-         * to make it simple, so that each memory backend can only have
+         * Fully shared guest-memfd doesn't work like that.  Disable it for
+         * now to make it simple, so that each memory backend can only have
           * guest-memfd either as private, or fully shared.
           */
          if (backend->guest_memfd_private) {

After rethinking on it, I think we just remove the check. There is nothing from QEMU side to prevent such usage. It is KVM currently that doesn't support INIT_SHARED guest-memfd for confidential VMs.

If we change to KVM to allow it:

arch/x86/kvm/x86.c +2 -1
index 8bb8830561d4..a658b636a9cf 100644
@@ -13957,7 +13957,8 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
  */
 bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
 {
-       return !kvm_arch_has_private_mem(kvm);
+       return true;
+
 
Quoted 2 lines

and remove this check in QEMU, it can actually boot a TDX guest with fully-shared guest-memfd back'ed as shared part.

Yes, logically I also don't see an issue with it. I wanted to add more restrictions because that's not an expected way of consuming fully shared.

But I don't see it a must indeed. In practise, IIUC it's not triggerable anyway with the current KVM (as IIUC the flags will report less when CoCo is on..). Let me drop it when repost.

Quoted 13 lines
Quoted 12 lines

I'll also fix the commit message on in-place, now the one to be reposted:

hostmem: Support fully shared guest memfd to back a VM
Host backends supports guest-memfd now by detecting whether it's a confidential VM. There's no way to choose it yet from the memory level to use it fully shared. If we use guest-memfd, it so far always implies we need two layers of memory backends, while the guest-memfd only provides the private set of pages.
This patch introduces a way so that QEMU can consume guest memfd as the only source of memory to back the object (aka, fully shared), rather than having another backend supporting the pages converted to shared.

Quoted 3 lines

As above, I think what the patch achieves is to enable guest-memfd (with MMAP and INIT_SHARED) to back shared memory. And it is not conflicted with using another guest-memfd to back private memory.

Sure.

Quoted 25 lines
Quoted 25 lines

To use the fully shared guest-memfd, one can add a memfd object with:
-object memory-backend-memfd,guest-memfd=on,share=on
Note that share=on is required with fully shared guest_memfd.

I'll not take your R-b as of now, please check and ack again if you see fit after reading.

Quoted 16 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 75

Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+         * to make it simple, so that each memory backend can only have
+         * guest-memfd either as private, or fully shared.
+         */
+        if (backend->guest_memfd_private) {
+            error_setg(errp, "In-place guest-memfd cannot be used with another "
+                       "private guest-memfd");
+            return false;
+        }

please add the following check as I commented in v1:

	if (!kvm_enabled()) {
		error_setg(errp, "in-place guest-memfd requires KVM");
		return false;
	}
Quoted 7 lines
Quoted 2 lines

IMHO it's redundant to set here, when kvm not enabled, kvm_create_guest_memfd() should be a stub.

No. The KVM stub is for the case where KVM is disable at compile time.

The kvm_enabled() check here is for the case where users use different accelerators other than KVM, e.g., -accel tcg.

I thought the kvm-compiled case is already covered, at least the flags will be 0 here:

    if (!kvm_guest_memfd_supported) {
        error_setg(errp, "KVM does not support guest_memfd");
        return -1;
    }

So I can change below stub patch into this one, would it look better (so that we'll provide explicit errors for all cases)?

===8<===
commit 70012ceb70d3ffe624db33a8aeaaec581c7b4ccd

Author: Peter Xu <[email protected]>
Date:   Thu Dec 11 11:19:44 2025 -0500

kvm: Provide explicit error for kvm_create_guest_memfd()

So that there will be a verbal string returned when kvm not enabled, or kvm not compiled.

---
 accel/kvm/kvm-all.c    | 5 +++++
 accel/stubs/kvm-stub.c | 1 +
 2 files changed, 6 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 68d57c1af0..c32fbcf9cc 100644
@@ -4492,6 +4492,11 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
         .flags = flags,
     };
 
+    if (!kvm_enabled()) {
+        error_setg(errp, "guest-memfd requires KVM accelerator");
+        return -1;
+    }
+
     if (!kvm_guest_memfd_supported) {
         error_setg(errp, "KVM does not support guest_memfd");
         return -1;
accel/stubs/kvm-stub.c +1 -0
index 73f04eb589..01b1d6285e 100644
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
 
 int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
 {
+    error_setg(errp, "KVM is not enabled");
     return -ENOSYS;
 }

===8<===

Thanks,

Quoted 64 lines
Quoted 38 lines

However indeed I found the stub didn't set an error, so how about add one trivial patch to add a verbal error for it instead?

commit aeeaba6dfc68a1c89af90c12f36cb8fe48faecfd

Author: Peter Xu <[email protected]>
Date:   Thu Dec 11 11:19:44 2025 -0500

kvm/stub: Provide explicit error for kvm_create_guest_memfd() So that there will be a verbal string returned when kvm not enabled.

---
  accel/stubs/kvm-stub.c | 1 +
  1 file changed, 1 insertion(+)
accel/stubs/kvm-stub.c +1 -0
index 73f04eb589..01b1d6285e 100644
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
  int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
  {
+    error_setg(errp, "KVM is not enabled");
      return -ENOSYS;
  }
 

IIUC it'll achieve the same goal with better layering.

Quoted 10 lines
Quoted 9 lines
backends/hostmem-memfd.c +8 -0
+        /* TODO: add huge page support */
+        fd = kvm_create_guest_memfd(backend->size,
+                                    GUEST_MEMFD_FLAG_MMAP |
+                                    GUEST_MEMFD_FLAG_INIT_SHARED,
+                                    errp);
+        if (fd < 0) {
+            return false;
+        }
Quoted 6 lines
Quoted 1 line

how about just removing the fd check here because ...

We needed it because at least the stub returns -ENOSYS..

I can remove it, but I'll need to change below to "fd<0" check. That I can do.

yeah, change to "fd < 0" looks better.

Quoted 16 lines

Thanks,

Quoted 14 lines

backends/hostmem-memfd.c memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) · line 93

Quoted 11 lines
backends/hostmem-memfd.c +7 -0
+    } else {
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size,
+                               m->hugetlb, m->hugetlbsize, m->seal ?
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0,
+                               errp);
+    }
+
       if (fd == -1) {
           return false;
       }

... the existing check can work for the guest memfd as well.

Signature · 1 line
Peter Xu
#6 lore raw

Re: [PATCH v2 2/9] kvm: Detect guest-memfd flags supported

In reply to Peter Xu
Quoted 48 lines · Peter Xu
Quoted 39 lines · Xiaoyao Li
Quoted 31 lines · Peter Xu

Detect supported guest-memfd flags by the current kernel, and reject creations of guest-memfd using invalid flags. When the cap isn't available, then no flag is supported.

---
   accel/kvm/kvm-all.c | 11 +++++++++++
   1 file changed, 11 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 96c194ce54..f477014126 100644
@@ -108,6 +108,7 @@ static int kvm_sstep_flags;
   static bool kvm_immediate_exit;
   static uint64_t kvm_supported_memory_attributes;
   static bool kvm_guest_memfd_supported;
+static uint64_t kvm_guest_memfd_flags_supported;
   static hwaddr kvm_max_slot_size = ~0;
   static const KVMCapabilityInfo kvm_required_capabilites[] = {
@@ -2787,6 +2788,10 @@ static int kvm_init(AccelState *as, MachineState *ms)
       kvm_guest_memfd_supported =
           kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD) &&
           kvm_vm_check_extension(s, KVM_CAP_USER_MEMORY2);
+
+    ret = kvm_vm_check_extension(s, KVM_CAP_GUEST_MEMFD_FLAGS);
+    kvm_guest_memfd_flags_supported = ret > 0 ? ret : 0;
+
       kvm_pre_fault_memory_supported = kvm_vm_check_extension(s, KVM_CAP_PRE_FAULT_MEMORY);
       if (s->kernel_irqchip_split == ON_OFF_AUTO_AUTO) {

accel/kvm/kvm-all.c int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp) · line 4500

Quoted 4 lines
accel/kvm/kvm-all.c +2 -0
@@ -4492,6 +4497,12 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
           return -1;
       }
+    if (flags & ~kvm_guest_memfd_flags_supported) {
+        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,

I'm thinking if need to add "for current VM" like:

Quoted 4 lines

KVM does not support guest-memfd flag: xxx for current VM

because kvm_guest_memfd_flags_supported is got from VM-scope CAP and varies for different VM types.

Sure I can amend it. Though I plan to change the sentence slightly:

      if (flags & ~kvm_guest_memfd_flags_supported) {

Review

Quoted 6 lines
(quoted diff) +2 -1
-        error_setg(errp, "KVM does not support guest-memfd flag: 0x%"PRIx64,
+        error_setg(errp, "Current KVM instance does not support "
+                   "guest-memfd flag: 0x%"PRIx64,
                     flags & ~kvm_guest_memfd_flags_supported);
          return -1;
      }

Works for me.

Quoted 12 lines

Thanks,

Quoted 9 lines
Quoted 8 lines
accel/kvm/kvm-all.c +4 -0
+                   flags & ~kvm_guest_memfd_flags_supported);
+        return -1;
+    }
+
       fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
       if (fd < 0) {
           error_setg_errno(errp, errno, "Error creating KVM guest_memfd");
#22 lore raw

Re: [PATCH v2 7/9] hostmem: Support in-place guest memfd to back a VM

In reply to Peter Xu
Quoted 15 lines · Peter Xu
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

please add the following check as I commented in v1:

	if (!kvm_enabled()) {
		error_setg(errp, "in-place guest-memfd requires KVM");
		return false;
	}
Quoted 6 lines
Quoted 2 lines

IMHO it's redundant to set here, when kvm not enabled, kvm_create_guest_memfd() should be a stub.

No. The KVM stub is for the case where KVM is disable at compile time.

The kvm_enabled() check here is for the case where users use different accelerators other than KVM, e.g., -accel tcg.

I thought the kvm-compiled case is already covered, at least the flags will be 0 here:

Quoted 7 lines
     if (!kvm_guest_memfd_supported) {
         error_setg(errp, "KVM does not support guest_memfd");
         return -1;
     }

So I can change below stub patch into this one, would it look better (so that we'll provide explicit errors for all cases)?

It looks good to me.

Quoted 46 lines

===8<===
commit 70012ceb70d3ffe624db33a8aeaaec581c7b4ccd

Author: Peter Xu<[email protected]>
Date:   Thu Dec 11 11:19:44 2025 -0500

kvm: Provide explicit error for kvm_create_guest_memfd()

So that there will be a verbal string returned when kvm not enabled, or kvm not compiled.

---
  accel/kvm/kvm-all.c    | 5 +++++
  accel/stubs/kvm-stub.c | 1 +
  2 files changed, 6 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 68d57c1af0..c32fbcf9cc 100644
@@ -4492,6 +4492,11 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
          .flags = flags,
      };
 
+    if (!kvm_enabled()) {
+        error_setg(errp, "guest-memfd requires KVM accelerator");
+        return -1;
+    }
+
      if (!kvm_guest_memfd_supported) {
          error_setg(errp, "KVM does not support guest_memfd");
          return -1;
accel/stubs/kvm-stub.c +1 -0
index 73f04eb589..01b1d6285e 100644
@@ -123,6 +123,7 @@ bool kvm_hwpoisoned_mem(void)
 
  int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)
  {
+    error_setg(errp, "KVM is not enabled");
      return -ENOSYS;
  }

===8<===

Thanks,