KVM/hostmem: Support init-shared guest-memfd as VM backends
v4 cover KVM/hostmem: Support init-shared guest-memfd as VM backends
v1: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]/
v4:
- Picked up v3 series from Peter Xu and rebased on 11.1.0
- Collected pending Reviewed-by's
- Per-patch changes:
[01/12] Fix blank space (Xiaoyao)
Fix commit message typos
[02/12] Drop duplicated error-handling for capability checks [05/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [07/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [08/12] Disallow 'seal' and 'hugetlb' options based on guest-memfd=on
Fix minor typo in comment
[11/12] Fix up typo in g_test_skip() reason
Pending: Move kvm_guest_memfd_init_shared_supported() to
migration-util.c (Fabiano)
This patchset is also available at:
https://github.com/amdese/qemu/commits/gmem-shared-mem-v4
and is based on top of v11.1.0
OVERVIEW
========
(cover letter shamelessly adapted from Peter's prior postings)
Recent kernels allow guest_memfd to be initialized with an 'init-shared' flag that will default to allocating normal/non-private memory that can be used to back non-confidential VMs.
This allows QEMU to make use of these init-shared guest_memfd instances via a common memory backend that's usable for either provide a common memory backend.
On the QEMU side, before this series, guest_memfd was only used for private guest memory (and thus only applied to confidential VMs), and the guest_memfd FDs would be created implicitly whenever a confidential environment was detected/specified.
With this series, users can now explicitly configure QEMU to use guest_memfd for non-private memory; thus, it can be used for non-confidential VMs. It also has implications for confidential VMs, since with this series an init-shared guest_memfd instance can now be specified for the shared memory while the internally-allocated guest_memfd continues to be used for private memory. This same infrastructure will also be used as the base for enabling in-place conversion for confidential VMs, where these separate shared/private paths will be modified to act on the same underlying guest_memfd instance and use a unified pool of shared/private memory.
IMPLEMENTATION
==============
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. While there is potential that new guest_memfd features may eventually necessitate introducing a dedicated guest_memfd memory backend object, for now the memory-backend-memfd object is a good fit for the current feature set.
This will also make it easier when in-place conversion comes around, since confidential VMs typically already use memory-backend-memfd for their shared memory, so by also making using that approach to specify the guest_memfd backend for in-place conversion the command-line syntax remains similar, and even allow choosing between memfd vs. guest_memfd to be handled automatically based on whether or not we're dealing with a Confidential VM with in-place conversion enabled.
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 a recent kernel (6.18+). When run it on an older qemu, you'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 (11):
kvm: Detect guest-memfd flags supported
kvm: Provide explicit error for kvm_create_guest_memfd()
ramblock: Rename guest_memfd to guest_memfd_private
memory: Rename RAM_GUEST_MEMFD to RAM_GUEST_MEMFD_PRIVATE
memory: Rename memory_region_has_guest_memfd() to *_private()
hostmem: Rename guest_memfd to guest_memfd_private
hostmem: Support fully shared guest memfd to back a VM
machine: Rename machine_require_guest_memfd() to *_private()
memory: Rename memory_region_init_ram_guest_memfd() to *_private() 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/hw/boards.h | 2 +- include/system/hostmem.h | 2 +- include/system/kvm.h | 1 + include/system/memory.h | 27 ++++++------ include/system/ram_addr.h | 2 +- include/system/ramblock.h | 7 +++- tests/qtest/migration/framework.h | 4 ++ accel/kvm/kvm-all.c | 33 ++++++++++++--- accel/stubs/kvm-stub.c | 6 +++ backends/hostmem-file.c | 2 +- backends/hostmem-memfd.c | 55 +++++++++++++++++++++--- backends/hostmem-ram.c | 2 +- backends/hostmem-shm.c | 2 +- backends/hostmem.c | 2 +- backends/igvm.c | 4 +- hw/core/machine.c | 2 +- hw/i386/pc.c | 6 +-- hw/i386/pc_sysfw.c | 8 ++-- hw/i386/x86-common.c | 8 ++-- system/memory.c | 17 ++++---- system/physmem.c | 37 ++++++++++------- target/i386/kvm/kvm.c | 3 +- tests/qtest/migration/framework.c | 60 +++++++++++++++++++++++++++ tests/qtest/migration/precopy-tests.c | 12 ++++++ 25 files changed, 239 insertions(+), 71 deletions(-)
Signature · 1 line
2.50.1
PATCH v4 1/12 kvm: Decouple memory attribute check from kvm_guest_memfd_supported
From: Xiaoyao Li <[email protected]>
With the mmap support of guest memfd, KVM allows userspace to create guest memfd serving as normal non-private memory for X86 DEFAULT VM. However, KVM doesn't support private memory attribute 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 QEMU to create guest memfd with mmap to serve as the memory backend for X86 DEFAULT VM.
--- accel/kvm/kvm-all.c | 8 ++++++-- accel/stubs/kvm-stub.c | 5 +++++ include/system/kvm.h | 1 + system/physmem.c | 8 ++++++++ 4 files changed, 20 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +6 -2
@@ -1627,6 +1627,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)
@@ -3058,8 +3063,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
@@ -143,3 +143,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; +}
include/system/kvm.h +1 -0
@@ -551,6 +551,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);
system/physmem.c +8 -0
@@ -2192,6 +2192,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.43.0
PATCH v4 2/12 kvm: Detect guest-memfd flags supported
In reply to Michael RothFrom: Peter Xu <[email protected]>
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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
accel/kvm/kvm-all.c +10 -0
@@ -107,6 +107,7 @@ bool kvm_pre_fault_memory_supported;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_capabilities[] = {
@@ -3065,6 +3066,8 @@ static int kvm_init(AccelState *as, MachineState *ms)kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD ) &&kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 ); kvm_pre_fault_memory_supported =kvm_vm_check_extension (s,KVM_CAP_PRE_FAULT_MEMORY ); + kvm_guest_memfd_flags_supported = +kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD_FLAGS );if (s->kernel_irqchip_split ==ON_OFF_AUTO_AUTO ) { s->kernel_irqchip_split = mc->default_kernel_irqchip_split ?ON_OFF_AUTO_ON :ON_OFF_AUTO_OFF ;
@@ -4760,6 +4763,13 @@ 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,"Current KVM instance 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.43.0
PATCH v4 3/12 kvm: Provide explicit error for kvm_create_guest_memfd()
In reply to Michael RothFrom: Peter Xu <[email protected]>
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
@@ -4758,6 +4758,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
@@ -141,6 +141,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 ; }
Signature · 1 line
2.43.0
Re: [PATCH v4 03/12] kvm: Provide explicit error for kvm_create_guest_memfd()
Hi Peter, Michael,
Quoted 20 lines · Michael Roth
From: Peter Xu <[email protected]>
So that there will be a verbal string returned when kvm not enabled, or kvm not compiled.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Reviewed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 1 + 2 files changed, 6 insertions(+)accel/kvm/kvm-all.c +0
-0
accel/kvm/kvm-all.c int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp) · line 4763
Quoted 6 lines
accel/kvm/kvm-all.c +3
-0@@ -4758,6 +4758,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 ;
This doesn't sound right withing a KVM-specific method. We want to assert() here.
The call in ram_block_add() is already protected:
2147 static void ram_block_add(RAMBlock *new_block, Error **errp) 2148 {
...
2185 if (new_block->flags & RAM_GUEST_MEMFD) {
2188 if (!kvm_enabled()) {
2189 error_setg(errp, "cannot set up private guest memory for %s: KVM required",
2190 object_get_typename(OBJECT(current_machine->cgs)));
2191 goto out_free;
2192 }
...
2203 new_block->guest_memfd = kvm_create_guest_memfd(new_block->max_length,
2204 0, errp);
The other one is:
2823 int ram_block_rebind(Error **errp)
2824 {
...
2829 RAMBLOCK_FOREACH(block) {
...
2834 block->guest_memfd = kvm_create_guest_memfd(block->max_length,
2835 0, errp);
which is only called from KVM:
2776 static int kvm_reset_vmfd(MachineState *ms)
2777 {
...
2827 /* rebind memory to new vm fd */
2828 ret = ram_block_rebind(&err);
So maybe what we want is:
-- >8 --
system/physmem.c +2 -0
@@ -2824,6 +2824,8 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +assert (kvm_enabled ());/* Only supported by KVM so far */ +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) {
---
Or less aggressive:
-- >8 --
system/physmem.c +5 -0
@@ -2824,6 +2824,11 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +if (!kvm_enabled ()) { +error_setg (errp,"guest-memfd requires KVM accelerator" ); +return -1 ; + } +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) {
---
WDYT?
Quoted 17 lines
accel/kvm/kvm-all.c +2
-0+ } +if (!kvm_guest_memfd_supported) {error_setg (errp,"KVM does not support guest_memfd" );return -1 ;accel/stubs/kvm-stub.c +1
-0@@ -141,6 +141,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 ; }
Re: [PATCH v4 03/12] kvm: Provide explicit error for kvm_create_guest_memfd()
Quoted 107 lines · Philippe Mathieu-Daudé
Hi Peter, Michael,
Quoted 20 lines · Michael Roth
From: Peter Xu <[email protected]>
So that there will be a verbal string returned when kvm not enabled, or kvm not compiled.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Reviewed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 1 + 2 files changed, 6 insertions(+)accel/kvm/kvm-all.c +0
-0accel/kvm/kvm-all.c int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp) · line 4762
Quoted 5 lines
accel/kvm/kvm-all.c +3
-0@@ -4758,6 +4758,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 ;This doesn't sound right withing a KVM-specific method. We want to
assert()here.The call in
ram_block_add()is already protected:2147 static void
ram_block_add(RAMBlock *new_block, Error **errp)2148 {
...
2185 if (new_block->flags&RAM_GUEST_MEMFD) {
2188 if (!kvm_enabled()) {
2189 error_setg(errp, "cannot set up private guest memory for
%s: KVM required",
2190 object_get_typename(OBJECT(current_machine->cgs)));
2191 gotoout_free;
2192 }
...
2203new_block->guest_memfd=
kvm_create_guest_memfd(new_block->max_length,
2204 0, errp);The other one is:
2823 int
ram_block_rebind(Error **errp)
2824 {
...
2829RAMBLOCK_FOREACH(block){
...
2834block->guest_memfd=
kvm_create_guest_memfd(block->max_length,
2835 0, errp);which is only called from KVM:
2776 static int
kvm_reset_vmfd(MachineState *ms)
2777 {
...
2827 /* rebind memory to new vm fd */
2828 ret =ram_block_rebind(&err);So maybe what we want is:
-- >8 --
system/physmem.c +2
-1@@ -2824,6 +2824,8 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +assert (kvm_enabled ());/* Only supported by KVM so far */ +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) { ---Or less aggressive:
-- >8 --
system/physmem.c +5
-1@@ -2824,6 +2824,11 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +if (!kvm_enabled ()) { +error_setg (errp,"guest-memfd requires KVM accelerator" ); +return -1 ; + } +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) { ---
Quoted 1 line
WDYT?
Fine by me.
IMHO it's normally more of an issue the other way round, if we used an assert() where we should use error_setg() (hence, user triggerable assert()s). Here we expect it to never happen, so either way should not happen..
If so, we could also assert() in ram_block_rebind(), as it's only used in kvm_reset_vmfd() only, so I don't see how it can be reached if KVM is not enabled first..
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 03/12] kvm: Provide explicit error for kvm_create_guest_memfd()
Quoted 63 lines · Peter Xu
Quoted 58 lines · Philippe Mathieu-Daudé
Hi Peter, Michael,
Quoted 10 lines · Michael Roth
From: Peter Xu <[email protected]>
So that there will be a verbal string returned when kvm not enabled, or kvm not compiled.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Reviewed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>Quoted 4 lines
--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 1 + 2 files changed, 6 insertions(+)So maybe what we want is:
-- >8 --
system/physmem.c +2
-1@@ -2824,6 +2824,8 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +assert (kvm_enabled ());/* Only supported by KVM so far */ +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) { ---Or less aggressive:
-- >8 --
system/physmem.c +5
-1@@ -2824,6 +2824,11 @@ int ram_block_rebind(Error **errp) {RAMBlock *block; +if (!kvm_enabled ()) { +error_setg (errp,"guest-memfd requires KVM accelerator" ); +return -1 ; + } +qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) { ---Quoted 1 line
WDYT?
Fine by me.
Quoted 8 lines
IMHO it's normally more of an issue the other way round, if we used an
assert()where we should useerror_setg()(hence, user triggerable assert()s). Here we expect it to never happen, so either way should not happen..If so, we could also
assert()inram_block_rebind(), as it's only used inkvm_reset_vmfd()only, so I don't see how it can be reached if KVM is not enabled first..
I'd rather avoid kvm_enabled() checks in kvm-specific API (accel/kvm/).
If ram_block_rebind() is KVM-specific, why expose it as RAMBlock API?
Re: [PATCH v4 03/12] kvm: Provide explicit error for kvm_create_guest_memfd()
Quoted 3 lines · Philippe Mathieu-Daudé
I'd rather avoid
kvm_enabled()checks in kvm-specific API (accel/kvm/).If
ram_block_rebind()is KVM-specific, why expose it as RAMBlock API?
I only started to remember why this patch existed.. after this series applied, kvm_create_guest_memfd() can be invoked from hostmem code, so we need this patch to fail on old kernels that do not support init shared (old kernel case, user triggerable).
Signature · 1 line
Peter Xu
Re: [PATCH v4 03/12] kvm: Provide explicit error for kvm_create_guest_memfd()
Quoted 9 lines · Peter Xu
Quoted 3 lines · Philippe Mathieu-Daudé
I'd rather avoid
kvm_enabled()checks in kvm-specific API (accel/kvm/).If
ram_block_rebind()is KVM-specific, why expose it as RAMBlock API?I only started to remember why this patch existed.. after this series applied,
kvm_create_guest_memfd()can be invoked from hostmem code, so we need this patch to fail on old kernels that do not support init shared (old kernel case, user triggerable).
OK, then please add a comment in the code, otherwise after also forgetting the why I'd be tempted to remove it, the pattern is odd :o)
PATCH v4 4/12 ramblock: Rename guest_memfd to guest_memfd_private
In reply to Michael RothFrom: Peter Xu <[email protected]>
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.
--- accel/kvm/kvm-all.c | 2 +- include/system/memory.h | 7 ++++--- include/system/ramblock.h | 7 ++++++- system/memory.c | 2 +- system/physmem.c | 33 +++++++++++++++++---------------- 5 files changed, 29 insertions(+), 22 deletions(-)
accel/kvm/kvm-all.c +1 -1
@@ -1729,7 +1729,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 ;
include/system/memory.h +4 -3
@@ -1507,10 +1507,11 @@ bool memory_region_skip_iommu_map(const MemoryRegion *mr);void memory_region_set_skip_iommu_map (MemoryRegion *mr,bool skip);/** -* memory_region_has_guest_memfd: check whether a memory region has guest_memfd -* associated +* memory_region_has_guest_memfd: check whether a memory region has +* guest_memfd_private 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 valid guest_memfd_private +* assigned. * * @mr: the memory region being queried */
include/system/ramblock.h +6 -1
@@ -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 */
system/memory.c +1 -1
@@ -1843,7 +1843,7 @@ void memory_region_set_skip_iommu_map(MemoryRegion *mr, bool skip)bool memory_region_has_guest_memfd (const 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 (const MemoryRegion *mr)
system/physmem.c +17 -16
@@ -2200,7 +2200,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 ) {
@@ -2210,9 +2210,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; }
@@ -2229,7 +2229,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;
@@ -2365,7 +2365,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);
@@ -2538,7 +2538,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;
@@ -2589,8 +2589,8 @@ static void reclaim_ramblock(RAMBlock *block)qemu_anon_ram_free (block->host, block->max_length); } -if (block->guest_memfd >=0 ) { -close (block->guest_memfd); +if (block->guest_memfd_private >=0 ) { +close (block->guest_memfd_private);ram_block_coordinated_discard_require (false); }
@@ -2838,12 +2838,12 @@ int ram_block_rebind(Error **errp)RAMBLOCK_FOREACH (block) {if (block->flags &RAM_GUEST_MEMFD ) { -if (block->guest_memfd >=0 ) { -close (block->guest_memfd); +if (block->guest_memfd_private >=0 ) { +close (block->guest_memfd_private); } - block->guest_memfd =kvm_create_guest_memfd (block->max_length, -0 , errp); -if (block->guest_memfd <0 ) { + block->guest_memfd_private =kvm_create_guest_memfd ( + block->max_length,0 , errp); +if (block->guest_memfd_private <0 ) {qemu_mutex_unlock_ramlist ();return -1 ; }
@@ -4227,7 +4227,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t offset, size_t length)return ret; } -if (rb->guest_memfd >=0 ) { +if (rb->guest_memfd_private >=0 ) { ret =ram_block_discard_guest_memfd_range (rb, offset, length); }
@@ -4241,7 +4241,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.43.0
PATCH v4 5/12 memory: Rename RAM_GUEST_MEMFD to RAM_GUEST_MEMFD_PRIVATE
In reply to Michael RothFrom: Peter Xu <[email protected]>
This name is too generic, and can conflict with support for using guest-memfd for shared memory. Add a _PRIVATE suffix to show what it really means: it is using guest_memfd specifically for private memory; whether or not guest_memfd is being used for shared memory is something to be configured/determined separately.
This also paves way for in-place guest-memfd, which means we can have a ramblock that allocates pages completely from guest-memfd (private or shared).
--- backends/hostmem-file.c | 2 +- backends/hostmem-memfd.c | 2 +- backends/hostmem-ram.c | 2 +- backends/hostmem-shm.c | 2 +- include/system/memory.h | 8 ++++---- include/system/ramblock.h | 2 +- system/memory.c | 2 +- system/physmem.c | 10 +++++----- 8 files changed, 15 insertions(+), 15 deletions(-)
backends/hostmem-file.c +1 -1
@@ -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
@@ -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
@@ -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
@@ -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,
include/system/memory.h +4 -4
@@ -262,7 +262,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,
@@ -1119,7 +1119,7 @@ void memory_region_init_io(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
@@ -1181,7 +1181,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.
@@ -1212,7 +1212,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/ramblock.h +1 -1
@@ -251,7 +251,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).
system/memory.c +1 -1
@@ -3660,7 +3660,7 @@ bool memory_region_init_ram_guest_memfd(MemoryRegion *mr, Object *owner, Error **errp) {if (!memory_region_init_ram_flags_nomigrate (mr, owner, name, size, -RAM_GUEST_MEMFD , errp)) { +RAM_GUEST_MEMFD_PRIVATE , errp)) {return false; }memory_region_register_ram (mr, owner);
system/physmem.c +5 -5
@@ -2184,7 +2184,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 ()) {
@@ -2321,7 +2321,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);
@@ -2478,7 +2478,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);
@@ -2561,7 +2561,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); }
@@ -2837,7 +2837,7 @@ int ram_block_rebind(Error **errp)qemu_mutex_lock_ramlist ();RAMBLOCK_FOREACH (block) { -if (block->flags &RAM_GUEST_MEMFD ) { +if (block->flags &RAM_GUEST_MEMFD_PRIVATE ) {if (block->guest_memfd_private >=0 ) {close (block->guest_memfd_private); }
Signature · 1 line
2.43.0
PATCH v4 6/12 memory: Rename memory_region_has_guest_memfd() to *_private()
In reply to Michael RothFrom: Peter Xu <[email protected]>
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 fully shared guest-memfd).
--- accel/kvm/kvm-all.c | 6 +++--- include/system/memory.h | 4 ++-- system/memory.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
accel/kvm/kvm-all.c +3 -3
@@ -792,7 +792,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 ; }
@@ -1741,7 +1741,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",
@@ -3372,7 +3372,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
include/system/memory.h +2 -2
@@ -1507,7 +1507,7 @@ bool memory_region_skip_iommu_map(const MemoryRegion *mr);void memory_region_set_skip_iommu_map (MemoryRegion *mr,bool skip);/** -* memory_region_has_guest_memfd: check whether a memory region has +* memory_region_has_guest_memfd_private: check whether a memory region has * guest_memfd_private associated * * Returns %true if a memory region's ram_block has valid guest_memfd_private
@@ -1515,7 +1515,7 @@ void memory_region_set_skip_iommu_map(MemoryRegion *mr, bool skip);* * @mr: the memory region being queried */ -bool memory_region_has_guest_memfd (const MemoryRegion *mr); +bool memory_region_has_guest_memfd_private (const MemoryRegion *mr);/** * memory_region_get_iommu: check whether a memory region is an iommu
system/memory.c +1 -1
@@ -1841,7 +1841,7 @@ void memory_region_set_skip_iommu_map(MemoryRegion *mr, bool skip) mr->ram_device_skip_iommu_map = skip; } -bool memory_region_has_guest_memfd (const MemoryRegion *mr) +bool memory_region_has_guest_memfd_private (const MemoryRegion *mr) {return mr->ram_block && mr->ram_block->guest_memfd_private >=0 ; }
Signature · 1 line
2.43.0
PATCH v4 7/12 hostmem: Rename guest_memfd to guest_memfd_private
In reply to Michael RothFrom: Peter Xu <[email protected]>
Rename the HostMemoryBackend.guest_memfd field to reflect what it really
means: whether it needs guest_memfd to back its private guest memory. This will help avoid conflicts when we introduce supported for using guest_memfd for shared guest memory via hostmem.
--- backends/hostmem-file.c | 2 +- backends/hostmem-memfd.c | 2 +- backends/hostmem-ram.c | 2 +- backends/hostmem-shm.c | 2 +- backends/hostmem.c | 2 +- include/system/hostmem.h | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-)
backends/hostmem-file.c +1 -1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -290,7 +290,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; }
include/system/hostmem.h +1 -1
@@ -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 );
Signature · 1 line
2.43.0
PATCH v4 8/12 hostmem: Support fully shared guest memfd to back a VM
In reply to Michael RothFrom: Peter Xu <[email protected]>
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).
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.
PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)
backends/hostmem-memfd.c +51 -5
@@ -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 an 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,11 +56,29 @@ 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); -if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" ); + } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);
@@ -65,6 +92,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 +191,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);
qapi/qom.json +5 -1
@@ -771,13 +771,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.2) +# # Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', - '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' } ##
Signature · 1 line
2.43.0
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 64 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)backends/hostmem-memfd.c +9
-4@@ -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 an 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,11 +56,29 @@ 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 +9
-1-if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" );
Reporting an error without returning false like the other cases.
Quoted 61 lines
backends/hostmem-memfd.c +33
-0+ } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);@@ -65,6 +92,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 +191,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);qapi/qom.json +3
-0@@ -771,13 +771,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.2) +#
qapi/qom.json line 785
Quoted 10 lines
qapi/qom.json +2
-1# Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', - '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }
We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 145 lines · Daniel P. Berrangé
Quoted 64 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)backends/hostmem-memfd.c +9
-4@@ -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 an 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,11 +56,29 @@ 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 +9
-1-if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" );Reporting an error without returning false like the other cases.
Quoted 67 lines
backends/hostmem-memfd.c +33
-0+ } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);@@ -65,6 +92,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 +191,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);qapi/qom.json +3
-0@@ -771,13 +771,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.2) +# # Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size',
Quoted 8 lines
qapi/qom.json line 785
Quoted 4 lines
qapi/qom.json +2
-1- '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
gmemfd should be able to use all ultimately.
For seal, IMHO it's already implied, kind of forced seal=on but it doesn't matter, gmemfd was introduced with sealing, at least what QEMU implies with "F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL". So IMHO we could ignore what user selected and assume it's ON.
For hugetlb, we will support hugetlb (and allow specify hugetlb size) for gmem in the future I believe. It's only that this is done one step at a time so we haven't supported it yet, while the kernel support is still in progress.
Quoted 4 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
Quoted 8 lines
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Please check if you agree after reading above. We can definitely still rethink the interfacing.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 155 lines · Peter Xu
Quoted 145 lines · Daniel P. Berrangé
Quoted 64 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)backends/hostmem-memfd.c +9
-4@@ -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 an 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,11 +56,29 @@ 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 +9
-1-if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" );Reporting an error without returning false like the other cases.
Quoted 67 lines
backends/hostmem-memfd.c +33
-0+ } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);@@ -65,6 +92,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 +191,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);qapi/qom.json +3
-0@@ -771,13 +771,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.2) +# # Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size',Quoted 8 lines
qapi/qom.json line 785
Quoted 4 lines
qapi/qom.json +2
-1- '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
Quoted 6 lines
gmemfd should be able to use all ultimately.
For seal, IMHO it's already implied, kind of forced seal=on but it doesn't matter, gmemfd was introduced with sealing, at least what QEMU implies with "
F_SEAL_GROW|F_SEAL_SHRINK|F_SEAL_SEAL". So IMHO we could ignore what user selected and assume it's ON.
Then we should not have a 'seal' property defined for guest memfd at all. Defining a property and then ignoring it, or only ever allowing 1 value to be set is a design mistake. The property should not exist if it can't ever be changed by the user/app.
Quoted 4 lines
For hugetlb, we will support hugetlb (and allow specify hugetlb size) for gmem in the future I believe. It's only that this is done one step at a time so we haven't supported it yet, while the kernel support is still in progress.
The problem with this idea is that it makes it impossible for a mgmt app to know if hugetlb is supported or not, as QEMU will always report it supported against memory-backend-memfd.
Having a memory-backend-guest-memfd ensures the public interface matches what is actually implemented/permitted for guest memfd.
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 212 lines · Daniel P. Berrangé
Quoted 155 lines · Peter Xu
Quoted 145 lines · Daniel P. Berrangé
Quoted 64 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)backends/hostmem-memfd.c +9
-4@@ -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 an 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,11 +56,29 @@ 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 +9
-1-if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" );Reporting an error without returning false like the other cases.
Quoted 67 lines
backends/hostmem-memfd.c +33
-0+ } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);@@ -65,6 +92,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 +191,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);qapi/qom.json +3
-0@@ -771,13 +771,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.2) +# # Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size',Quoted 8 lines
qapi/qom.json line 785
Quoted 4 lines
qapi/qom.json +2
-1- '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
Quoted 6 lines
gmemfd should be able to use all ultimately.
For seal, IMHO it's already implied, kind of forced seal=on but it doesn't matter, gmemfd was introduced with sealing, at least what QEMU implies with "
F_SEAL_GROW|F_SEAL_SHRINK|F_SEAL_SEAL". So IMHO we could ignore what user selected and assume it's ON.Then we should not have a 'seal' property defined for guest memfd at all. Defining a property and then ignoring it, or only ever allowing 1 value to be set is a design mistake. The property should not exist if it can't ever be changed by the user/app.
Quoted 4 lines
For hugetlb, we will support hugetlb (and allow specify hugetlb size) for gmem in the future I believe. It's only that this is done one step at a time so we haven't supported it yet, while the kernel support is still in progress.
The problem with this idea is that it makes it impossible for a mgmt app to know if hugetlb is supported or not, as QEMU will always report it supported against memory-backend-memfd.
Having a memory-backend-guest-memfd ensures the public interface matches what is actually implemented/permitted for guest memfd.
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Quoted 8 lines
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
The idea of that, at least in my mind.. is an user shouldn't need to worry about differences of guest-memfd and memfd, QEMU should just do it for the users, based on the machine configurations. Memfd is a concept more widely spread, the hope is anyone using guest-memfd should simply treat it as one memfd, no matter it is shared, in-place converted, two-layer-backed, or whatever is happening underneath.
Now we do create guest-memfd via a separate ioctl, what if we can create it via memfd_create() syscall too? Then do we need to do the separation from QEMU layer?
IMHO that is now an ioctl is not required; it really can be part of memfd_create() syscall, it's just easier to manage, e.g. it's completely KVM alone, and it also has attached to the KVM instance. The idea is still similar, and we can see that from possibly shared properties here on huge pages and so. I don't treat seal= a block just to introduce a new object for that, and I expect as gmemfd evolves it will gradually get most features memfd supports.. like folio migration and so.. but I could be wrong.
IMHO one major question to ask is, is it more convenient for libvirt to have that new object? Please keep in mind that after we introduce this as a new object, we may start to introduce even more *-guest-memfd in the future, we roughly talked about DAX in the previous discussion. My goal is to make it most convenient for either user or libvirt to maintain the cmdlines for QEMU, but if you think that makes libvirt live harder instead, I've no strong feeling to go back to what Michael initially suggested.
For example, if libvirt wants to detect hugetlb supports for an object, it can still pass in the parameters and test-boot a QEMU and then IIUC it'll still correctly capture an error for gmemfd case. It might be that I didn't really get what will make libvirt complex by reusing the object, but I'll definitely follow your judgement on that.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 32 lines · Peter Xu
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Peter Xu
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)
snip
Quoted 48 lines
Quoted 32 lines
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Quoted 8 lines
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
The idea of that, at least in my mind.. is an user shouldn't need to worry about differences of guest-memfd and memfd, QEMU should just do it for the users, based on the machine configurations. Memfd is a concept more widely spread, the hope is anyone using guest-memfd should simply treat it as one memfd, no matter it is shared, in-place converted, two-layer-backed, or whatever is happening underneath.
Quoted 3 lines
Now we do create guest-memfd via a separate ioctl, what if we can create it via
memfd_create()syscall too? Then do we need to do the separation from QEMU layer?
If QEMU automatically did "the right thing" choosing between traditional memfd and guest-memfd, then I wouldn't have even started this thread :-)
The "guest-memfd=on|off" is the trigger that made me think the design was wrong from a public interface POV, as that explicitly says that the memfd vs guest-memfd distinction is not automatic - it requires the mgmt app to understand it and choose between them.
Quoted 9 lines
IMHO that is now an ioctl is not required; it really can be part of
memfd_create()syscall, it's just easier to manage, e.g. it's completely KVM alone, and it also has attached to the KVM instance. The idea is still similar, and we can see that from possibly shared properties here on huge pages and so. I don't treat seal= a block just to introduce a new object for that, and I expect as gmemfd evolves it will gradually get most features memfd supports.. like folio migration and so.. but I could be wrong.
Quoted 7 lines
IMHO one major question to ask is, is it more convenient for libvirt to have that new object? Please keep in mind that after we introduce this as a new object, we may start to introduce even more *-guest-memfd in the future, we roughly talked about DAX in the previous discussion. My goal is to make it most convenient for either user or libvirt to maintain the cmdlines for QEMU, but if you think that makes libvirt live harder instead, I've no strong feeling to go back to what Michael initially suggested.
It is no more or less difficult for libvirt to use different object types vs using different guest-memfd=bool values.
What makes a difference for libvirt is understanding whether QEMU implements a given feature or not.
If we have the situation with
memory-backend-memfd,hugetlb=bool,guest-memfd=bool
with this series, IIUC, libvirt can introspect to see the new guest-memfd property, but it has no way of knowing that it can't use the hugetlb proeprty when guest-memfd=on
If the next QEMU release now permits hugetlb=on at the same time as guest-memfd=on, then libvirt has no way to know the restriction was relaxed.
The QAPI introspection data for memory-backend-memfd is identical in both cases.
To deal with this, you need to introduce a workaround to QAPI by declaring a feature flag like
features: ['hugetlb-with-guest-memfd-works']
that libvirt can probe for to determine the functional improvement in QEMU.
By comparison, if we introduce a memory-backend-guest-memfd object today, then it will omit the hugetlb property entirely.
If the next QEMU release adds a hugetlb property to the memory-backend-guest-memfd object, this change is now visible in the introspection data, and thus libvirt knows that combo is possible with QEMU
(yes, there is the issue that this might have kernel dependancy that is not visible from QEMU's introspection, so not perfect, but at least libvirt can determine what QEMU is capable of)
Quoted 5 lines
For example, if libvirt wants to detect hugetlb supports for an object, it can still pass in the parameters and test-boot a QEMU and then IIUC it'll still correctly capture an error for gmemfd case. It might be that I didn't really get what will make libvirt complex by reusing the object, but I'll definitely follow your judgement on that.
Trying parameters to see if they fail and then boot again with different options is not a viable approach. That is an indication that QEMU's design and/or introspection is flawed.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 145 lines · Daniel P. Berrangé
Quoted 32 lines · Peter Xu
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Peter Xu
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)snip
Quoted 48 lines
Quoted 32 lines
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Quoted 8 lines
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
The idea of that, at least in my mind.. is an user shouldn't need to worry about differences of guest-memfd and memfd, QEMU should just do it for the users, based on the machine configurations. Memfd is a concept more widely spread, the hope is anyone using guest-memfd should simply treat it as one memfd, no matter it is shared, in-place converted, two-layer-backed, or whatever is happening underneath.
Quoted 3 lines
Now we do create guest-memfd via a separate ioctl, what if we can create it via
memfd_create()syscall too? Then do we need to do the separation from QEMU layer?If QEMU automatically did "the right thing" choosing between traditional memfd and guest-memfd, then I wouldn't have even started this thread :-)
The "guest-memfd=on|off" is the trigger that made me think the design was wrong from a public interface POV, as that explicitly says that the memfd vs guest-memfd distinction is not automatic - it requires the mgmt app to understand it and choose between them.
Quoted 9 lines
IMHO that is now an ioctl is not required; it really can be part of
memfd_create()syscall, it's just easier to manage, e.g. it's completely KVM alone, and it also has attached to the KVM instance. The idea is still similar, and we can see that from possibly shared properties here on huge pages and so. I don't treat seal= a block just to introduce a new object for that, and I expect as gmemfd evolves it will gradually get most features memfd supports.. like folio migration and so.. but I could be wrong.Quoted 7 lines
IMHO one major question to ask is, is it more convenient for libvirt to have that new object? Please keep in mind that after we introduce this as a new object, we may start to introduce even more *-guest-memfd in the future, we roughly talked about DAX in the previous discussion. My goal is to make it most convenient for either user or libvirt to maintain the cmdlines for QEMU, but if you think that makes libvirt live harder instead, I've no strong feeling to go back to what Michael initially suggested.
It is no more or less difficult for libvirt to use different object types vs using different guest-memfd=bool values.
What makes a difference for libvirt is understanding whether QEMU implements a given feature or not.
If we have the situation with
memory-backend-memfd,hugetlb=bool,guest-memfd=bool
with this series, IIUC, libvirt can introspect to see the new guest-memfd property, but it has no way of knowing that it can't use the hugetlb proeprty when guest-memfd=on
If the next QEMU release now permits hugetlb=on at the same time as guest-memfd=on, then libvirt has no way to know the restriction was relaxed.
The QAPI introspection data for memory-backend-memfd is identical in both cases.
To deal with this, you need to introduce a workaround to QAPI by declaring a feature flag like
features: ['hugetlb-with-guest-memfd-works']
that libvirt can probe for to determine the functional improvement in QEMU.
By comparison, if we introduce a memory-backend-guest-memfd object today, then it will omit the hugetlb property entirely.
Quoted 8 lines
If the next QEMU release adds a hugetlb property to the memory-backend-guest-memfd object, this change is now visible in the introspection data, and thus libvirt knows that combo is possible with QEMU
(yes, there is the issue that this might have kernel dependancy that is not visible from QEMU's introspection, so not perfect, but at least libvirt can determine what QEMU is capable of)
Yes. Due to that, I was expecting Libvirt will always need to query host capabilities on its own. In this case Libvirt, if preferred, should also be able to probe standalone with KVM_CAP_GUEST_MEMFD_FLAGS.
It's also more involved for sure when hugetlb is involved, if it will share the same hugetlb reservation with hugetlbfs, it'll also somehow need to make sure there're available pages later for allocations. Also IIUC not all default users are accessible to huge pages..
From QEMU's POV, personally I still prefer sticking with the memfd object with guest-memfd=on, then expose the "features" in QAPI, that seems cleaner to me.
Quoted 11 lines
Quoted 7 lines
For example, if libvirt wants to detect hugetlb supports for an object, it can still pass in the parameters and test-boot a QEMU and then IIUC it'll still correctly capture an error for gmemfd case. It might be that I didn't really get what will make libvirt complex by reusing the object, but I'll definitely follow your judgement on that.
Trying parameters to see if they fail and then boot again with different options is not a viable approach. That is an indication that QEMU's design and/or introspection is flawed.
It'll be the same when introducing the new -guest-memfd object per above complications, IMHO. I'm not sure how hugetlbfs pages were managed now with Libvirt, maybe that can help us understand how guest-memfd (when hugetlb pages will be supported) will affect Libvirt's mgmt. For now, it doesn't seem to me that the new object will help in anyway, if we have QEMU's "features" option.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 151 lines · Peter Xu
Quoted 150 lines · Daniel P. Berrangé
Quoted 32 lines · Peter Xu
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Peter Xu
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)snip
Quoted 48 lines
Quoted 32 lines
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Quoted 8 lines
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
The idea of that, at least in my mind.. is an user shouldn't need to worry about differences of guest-memfd and memfd, QEMU should just do it for the users, based on the machine configurations. Memfd is a concept more widely spread, the hope is anyone using guest-memfd should simply treat it as one memfd, no matter it is shared, in-place converted, two-layer-backed, or whatever is happening underneath.
Quoted 3 lines
Now we do create guest-memfd via a separate ioctl, what if we can create it via
memfd_create()syscall too? Then do we need to do the separation from QEMU layer?If QEMU automatically did "the right thing" choosing between traditional memfd and guest-memfd, then I wouldn't have even started this thread :-)
The "guest-memfd=on|off" is the trigger that made me think the design was wrong from a public interface POV, as that explicitly says that the memfd vs guest-memfd distinction is not automatic - it requires the mgmt app to understand it and choose between them.
Quoted 9 lines
IMHO that is now an ioctl is not required; it really can be part of
memfd_create()syscall, it's just easier to manage, e.g. it's completely KVM alone, and it also has attached to the KVM instance. The idea is still similar, and we can see that from possibly shared properties here on huge pages and so. I don't treat seal= a block just to introduce a new object for that, and I expect as gmemfd evolves it will gradually get most features memfd supports.. like folio migration and so.. but I could be wrong.Quoted 7 lines
IMHO one major question to ask is, is it more convenient for libvirt to have that new object? Please keep in mind that after we introduce this as a new object, we may start to introduce even more *-guest-memfd in the future, we roughly talked about DAX in the previous discussion. My goal is to make it most convenient for either user or libvirt to maintain the cmdlines for QEMU, but if you think that makes libvirt live harder instead, I've no strong feeling to go back to what Michael initially suggested.
It is no more or less difficult for libvirt to use different object types vs using different guest-memfd=bool values.
What makes a difference for libvirt is understanding whether QEMU implements a given feature or not.
If we have the situation with
memory-backend-memfd,hugetlb=bool,guest-memfd=bool
with this series, IIUC, libvirt can introspect to see the new guest-memfd property, but it has no way of knowing that it can't use the hugetlb proeprty when guest-memfd=on
If the next QEMU release now permits hugetlb=on at the same time as guest-memfd=on, then libvirt has no way to know the restriction was relaxed.
The QAPI introspection data for memory-backend-memfd is identical in both cases.
To deal with this, you need to introduce a workaround to QAPI by declaring a feature flag like
features: ['hugetlb-with-guest-memfd-works']
that libvirt can probe for to determine the functional improvement in QEMU.
By comparison, if we introduce a memory-backend-guest-memfd object today, then it will omit the hugetlb property entirely.
If the next QEMU release adds a hugetlb property to the memory-backend-guest-memfd object, this change is now visible in the introspection data, and thus libvirt knows that combo is possible with QEMU
Quoted 7 lines
Quoted 3 lines
(yes, there is the issue that this might have kernel dependancy that is not visible from QEMU's introspection, so not perfect, but at least libvirt can determine what QEMU is capable of)
Yes. Due to that, I was expecting Libvirt will always need to query host capabilities on its own. In this case Libvirt, if preferred, should also be able to probe standalone with
KVM_CAP_GUEST_MEMFD_FLAGS.
Yah some generic routine to probe for guest_memfd flags seems unavoidable, and likely that probe will need to be per-VM-type (e.g. confidential vs. non-confidential) to be reliable.
Quoted 22 lines
It's also more involved for sure when hugetlb is involved, if it will share the same hugetlb reservation with hugetlbfs, it'll also somehow need to make sure there're available pages later for allocations. Also IIUC not all default users are accessible to huge pages..
From QEMU's POV, personally I still prefer sticking with the memfd object with guest-memfd=on, then expose the "features" in QAPI, that seems cleaner to me.
Quoted 11 lines
Quoted 7 lines
For example, if libvirt wants to detect hugetlb supports for an object, it can still pass in the parameters and test-boot a QEMU and then IIUC it'll still correctly capture an error for gmemfd case. It might be that I didn't really get what will make libvirt complex by reusing the object, but I'll definitely follow your judgement on that.
Trying parameters to see if they fail and then boot again with different options is not a viable approach. That is an indication that QEMU's design and/or introspection is flawed.
Quoted 6 lines
It'll be the same when introducing the new -guest-memfd object per above complications, IMHO. I'm not sure how hugetlbfs pages were managed now with Libvirt, maybe that can help us understand how guest-memfd (when hugetlb pages will be supported) will affect Libvirt's mgmt. For now, it doesn't seem to me that the new object will help in anyway, if we have QEMU's "features" option.
It seemed like the guest_memfd support for directmap removal[1] might be a good test for how this all look like once new features start coming along for confidential vs. non-confidential so I've been trying to get that up and running to see.
If we keep everything in memory-backend-memfd, there's a little bit of awkwardness, but the following schema should cover discoverability of QEMU support at least, and then libvirt/management can branch into whatever routine is needed for probing actual kernel support:
diff --git a/qapi/qom.json b/qapi/qom.json
index ee981fc44c..4777f09ad1 100644
--- a/qapi/qom.json
+++ b/qapi/qom.json
@@ -774,6 +774,19 @@
# @guest-memfd: if true, use guest-memfd to back the memory region.
# (default: false, since: 11.2)
#
+# @no-directmap: if true, enable support for unmapping backend memory
+# from the kernel directmap to better isolate it from
+# host activity. This option is only available if a
+# corresponding Features value advertises support for
+# the intended use-case.
+# (default: false, since: 11.2)
+#
+# Features:
+#
+# @no-directmap-for-guest-memfd: If present (and if the hypervisor
+# supports the feature), the corresponding option is available if
+# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,8 +794,10 @@
'data': { '*hugetlb': 'bool',
'*hugetlbsize': 'size',
'*seal': 'bool', - '*guest-memfd': 'bool' },
- 'if': 'CONFIG_LINUX' }
+ '*guest-memfd': 'bool',
+ '*no-directmap': 'bool' },
+ 'if': 'CONFIG_LINUX',
+ 'features': ['no-directmap-for-guest-memfd'] }
One notable thing there is the current code only enables it for non-confidential VM types, so that's where the KVM_CAP_* checks would need to come into play to distinguish between what's supported for specific VM types.
On a side note:
Ideally we'd be able to do something like the below so we can have one 'supported-for-guest-memfd' features that could be associated with multiple options, e.g.:
+# Features:
+#
+# @supported-for-guest-memfd: If present (and if the hypervisor +# supports the feature), the corresponding option is available if +# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,7 +794,9 @@
'data': { '*hugetlb': 'bool',
'*hugetlbsize': 'size',
'*seal': 'bool',
- '*guest-memfd': 'bool' },
+ '*guest-memfd': 'bool',
+ { 'name': 'no-directmap',+ 'features': ['supported-for-guest-memfd'] } },
But it doesn't seem like QAPI is wired up for that. I guess we could add that as part of enabling directmap support if it seems useful enough but the above should work as well.
Thanks,
Mike
[1] https://lore.kernel.org/kvm/[email protected]/
Quoted 6 lines
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 9 lines · Michael Roth
It seemed like the
guest_memfdsupport for directmap removal[1] might be a good test for how this all look like once new features start coming along for confidential vs. non-confidential so I've been trying to get that up and running to see.If we keep everything in memory-backend-memfd, there's a little bit of awkwardness, but the following schema should cover discoverability of QEMU support at least, and then libvirt/management can branch into whatever routine is needed for probing actual kernel support:
Isn't that secretmem for generic memfd case? I'm not sure if it'll ever need to be supported by QEMU at all, but it sounds like exactly that for normal memfds..
Quoted 10 lines
diff --git
a/qapi/qom.jsonb/qapi/qom.json
indexee981fc44c..4777f09ad1100644
---a/qapi/qom.json
+++b/qapi/qom.json
@@ -774,6 +774,19 @@
# @guest-memfd: if true, use guest-memfd to back the memory region.
# (default: false, since: 11.2)
#
+# @no-directmap: if true, enable support for unmapping backend memory
Quoted 5 lines
+# from the kernel directmap to better isolate it from +# host activity. This option is only available if a +# corresponding Features value advertises support for +# the intended use-case. +# (default: false, since: 11.2)
Shall we name the property to avoid "no-" prefixes ("kernel-map=on/off", default on)? I used "kernel" instead of "direct" because any mmap() from user can also be described, more or less.. to be direct.
Quoted 11 lines
+#
+# Features:
+#
+# @no-directmap-for-guest-memfd: If present (and if the hypervisor +# supports the feature), the corresponding option is available if +# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,8 +794,10 @@
Quoted 8 lines
'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', '*seal': 'bool',- '*guest-memfd': 'bool' },
- 'if': 'CONFIG_LINUX' }
+ '*guest-memfd': 'bool',
+ '*no-directmap': 'bool' },
+ 'if': 'CONFIG_LINUX',
Nitpic: we could drop CONFIG_LINUX IMHO in QAPI if it'll fail properly on non-linux in impl anyway.
Quoted 30 lines
+ 'features': ['no-directmap-for-guest-memfd'] }
One notable thing there is the current code only enables it for non-confidential VM types, so that's where the KVM_CAP_* checks would need to come into play to distinguish between what's supported for specific VM types.
On a side note:
Ideally we'd be able to do something like the below so we can have one 'supported-for-guest-memfd' features that could be associated with multiple options, e.g.:
+# Features:
+#
+# @supported-for-guest-memfd: If present (and if the hypervisor +# supports the feature), the corresponding option is available if +# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,7 +794,9 @@'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', '*seal': 'bool', - '*guest-memfd': 'bool' }, + '*guest-memfd': 'bool', + { 'name': 'no-directmap',
Quoted 5 lines
+ 'features': ['supported-for-guest-memfd'] } },
But it doesn't seem like QAPI is wired up for that. I guess we could add that as part of enabling directmap support if it seems useful enough but the above should work as well.
Maybe we can still just fail it for normal memfds for now, considering secretmem or anything like that might still be supported someday. In general, for any memfd property (guest-memfd or memfd), if it conceptrally apply to all (my bet is here..) then IMHO we can keep them around at least from the interface level, and just fail them when we don't have support for one of the two.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 6 lines · Peter Xu
Quoted 5 lines · Michael Roth
It seemed like the
guest_memfdsupport for directmap removal[1] might be a good test for how this all look like once new features start coming along for confidential vs. non-confidential so I've been trying to get that up and running to see.
Quoted 8 lines
Quoted 4 lines
If we keep everything in memory-backend-memfd, there's a little bit of awkwardness, but the following schema should cover discoverability of QEMU support at least, and then libvirt/management can branch into whatever routine is needed for probing actual kernel support:
Isn't that secretmem for generic memfd case? I'm not sure if it'll ever need to be supported by QEMU at all, but it sounds like exactly that for normal memfds..
Hmm, I did intend to leave open the option of using this for other use-cases that came along, but I didn't realize secretmem already does kernel directmap removal. That's an interesting thing to consider...
We couldn't just flip memfd_secret on under the covers since it has other side-effects, so you'd likely need something like secret-memfd=on which starts to look a lot like guest-memfd=on.
So the schema would be something like:
{ 'struct': 'MemoryBackendMemfdProperties',
'data': { '*hugetlb': 'bool',
'*hugetlbsize': 'size',
'*seal': 'bool',
'*guest-memfd': 'bool',
'*no-directmap': 'bool' },
'features': ['no-directmap-for-guest-memfd',
'no-directmap-for-secret-memfd'] }with nearly identifical wording except s/guest/secret/. Not sure how pretty that all is, but hopefully that's some indication that this is a reasonable design choice at least.
Quoted 17 lines
Quoted 11 lines
diff --git
a/qapi/qom.jsonb/qapi/qom.json
indexee981fc44c..4777f09ad1100644
---a/qapi/qom.json
+++b/qapi/qom.json
@@ -774,6 +774,19 @@
# @guest-memfd: if true, use guest-memfd to back the memory region.
# (default: false, since: 11.2)
#
+# @no-directmap: if true, enable support for unmapping backend memoryQuoted 5 lines
+# from the kernel directmap to better isolate it from +# host activity. This option is only available if a +# corresponding Features value advertises support for +# the intended use-case. +# (default: false, since: 11.2)
Quoted 3 lines
Shall we name the property to avoid "no-" prefixes ("kernel-map=on/off", default on)? I used "kernel" instead of "direct" because any
mmap()from user can also be described, more or less.. to be direct.
Makes sense, I guess we don't need to reach a consensus on this detail here, but kernel-map or maybe kernel-isolation seem like reasonable choices.
Quoted 21 lines
Quoted 12 lines
+#
+# Features:
+#
+# @no-directmap-for-guest-memfd: If present (and if the hypervisor +# supports the feature), the corresponding option is available if +# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,8 +794,10 @@Quoted 8 lines
'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', '*seal': 'bool',- '*guest-memfd': 'bool' },
- 'if': 'CONFIG_LINUX' }
+ '*guest-memfd': 'bool',
+ '*no-directmap': 'bool' },
+ 'if': 'CONFIG_LINUX',
Quoted 2 lines
Nitpic: we could drop
CONFIG_LINUXIMHO in QAPI if it'll fail properly on non-linux in impl anyway.
That was already the case before this series so I guess I'd leave that up to others to decide. One point on this though:
On the introspection side, if we don't actually support anything beyond linux, it probaby doesn't hurt to continue to gate on CONFIG_LINUX just in case some funky business needs to happen to switch it on for another OS (e.g. if certain options become inapplicable to that use-case/etc. and dropping 'CONFIG_LINUX' involves adding it back in more granularly on a per-option basis).
Quoted 37 lines
Quoted 31 lines
+ 'features': ['no-directmap-for-guest-memfd'] }
One notable thing there is the current code only enables it for non-confidential VM types, so that's where the KVM_CAP_* checks would need to come into play to distinguish between what's supported for specific VM types.
On a side note:
Ideally we'd be able to do something like the below so we can have one 'supported-for-guest-memfd' features that could be associated with multiple options, e.g.:
+# Features:
+#
+# @supported-for-guest-memfd: If present (and if the hypervisor +# supports the feature), the corresponding option is available if +# @guest-memfd is true. (since 11.2)
+#
# Since: 2.12
##
{ 'struct': 'MemoryBackendMemfdProperties',
@@ -781,7 +794,9 @@'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', '*seal': 'bool', - '*guest-memfd': 'bool' }, + '*guest-memfd': 'bool', + { 'name': 'no-directmap',Quoted 5 lines
+ 'features': ['supported-for-guest-memfd'] } },
But it doesn't seem like QAPI is wired up for that. I guess we could add that as part of enabling directmap support if it seems useful enough but the above should work as well.
Quoted 6 lines
Maybe we can still just fail it for normal memfds for now, considering secretmem or anything like that might still be supported someday. In general, for any memfd property (guest-memfd or memfd), if it conceptrally apply to all (my bet is here..) then IMHO we can keep them around at least from the interface level, and just fail them when we don't have support for one of the two.
It would already fail for non-guest-memfd, but it doesn't seem like a bad idea to keep the option documented/introspected as guest-memfd-only until someone explicitly adds a Feature to advertise support for additional use-cases.
I think it could also be done how you propose though and still have all the introspection support in place. We would just document 'no-directmap' (or 'kernel-map'/etc) as if it was generally available, but update the Feature to be 'supported-for-guest-memfd-only' and turn it into a quirk. If we do one day generally enable the option, then we drop the 'supported-for-guest-memfd-only' Feature/quirk and everything is nice and clean from that point forward.
The downside is maybe your bet doesn't pan out and the feature doesn't get generally enabled, but instead only additionally gets enabled for, say, secretmem use-cases, and now we have both 'supported-for-guest-memfd-only' and 'supported-for-secret-mem-only' and the wording/semantics of the 2 becomes awkward. And maybe that secretmem functionality isn't something we can switch on automatically because of too many side-effects (as speculated above), so it becomes another one of these options that depends on another option being set in order to actually work. In that case it might make sense to detail these additional requirements via a new feature flag instead of needing to sort of redefine things a bit.
That's why I basically documented it to be 'don't introspect this option based on it's existence, introspect based on specific Features that indicate support for using it in conjunction with that use-case'. It's a bit more awkward upfront, but seems to avoid some potential quagmires.
But I think we'd survive with either approach, so happy to take whatever direction we end up going once that series comes around. It's nice to know that either option is available/workable though.
Thanks!
-Mike
Quoted 6 lines
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
In reply to Daniel P. BerrangéQuoted 156 lines · Daniel P. Berrangé
Quoted 155 lines · Peter Xu
Quoted 153 lines · Daniel P. Berrangé
Quoted 64 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)backends/hostmem-memfd.c +9
-4@@ -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 an 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,11 +56,29 @@ 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 +9
-1-if (fd ==-1 ) { +if (m->guest_memfd) { +if (!backend->share) { +error_setg (errp,"guest-memfd=on must be used with share=on" ); +return false; + }else if (m->seal) { +error_setg (errp,"guest-memfd=on must be used with seal=off" ); +return false; + }else if (m->hugetlb) { +error_setg (errp,"guest-memfd=on must be used with hugetlb=off" );Reporting an error without returning false like the other cases.
Quoted 61 lines
backends/hostmem-memfd.c +33
-0+ } + + fd =kvm_create_guest_memfd (backend->size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED , + errp); + }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 <0 ) {return false; }cpr_save_fd (name,0 , fd);@@ -65,6 +92,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 +191,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);qapi/qom.json +3
-0@@ -771,13 +771,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.2) +#qapi/qom.json line 785
Quoted 10 lines
qapi/qom.json +2
-1# Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', - '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
Quoted 6 lines
Quoted 6 lines
gmemfd should be able to use all ultimately.
For seal, IMHO it's already implied, kind of forced seal=on but it doesn't matter, gmemfd was introduced with sealing, at least what QEMU implies with "
F_SEAL_GROW|F_SEAL_SHRINK|F_SEAL_SEAL". So IMHO we could ignore what user selected and assume it's ON.
The naming threw me off, but looking at the actual schema description for 'seal', it basically implies 'fixed-size=on', which guest_memfd does enforce. So, aside from the question of whether to reuse the memfd backend, I do think it's more correct to imply seal=on for guest_memfd.
Quoted 5 lines
Then we should not have a 'seal' property defined for guest memfd at all. Defining a property and then ignoring it, or only ever allowing 1 value to be set is a design mistake. The property should not exist if it can't ever be changed by the user/app.
We have a couple examples of what seems like something similar with memory-backend-file,rom=on,readonly=off and memory-backend-*,prealloc=on,reserve=off. Granted, those seem a little more obvious to recognize as mutually-exclusive, but as far as the discussion around libvirt/mgmt/introspection: are there mechanisms in place already to handle cases like that? Or are these special-cases that would be problematic to try to handle similarly?
Regarding hugepage options, there will undoubtedly be kernels that support guest_memfd but not hugetlb, so even if we introduce memory-backend-guest-memfd now so that we can add options when/where it makes sense, libvirt/mgmt. would still need to eventually handle a 'hugetlb' option existing, but not necessarilly implying that the guest can actually use them. It seems like the issue exists in either case.
FWIW, I do anticipate that we will need something like memory-backend-guest-memfd for some of the use-cases coming down the pipeline, but for some stuff like CXL/HBM memory support where folks are talking about stuff like custom guest_memfd allocators or special-purpose NUMA nodes I'm not reasonably confident that memory-backend-guest-memfd won't itself end up being too generic of a construct and be immediately relegated to only handling the exact same set of options as memory-backend-memfd (give or take a 'seal').
Peter's suggested approach allows us to assume less about how things will eventually look by reusing existing options/command-lines and handling things underneath the covers for the more basic use-cases in the meantime. Once we hit cases that clearly have no business in memory-backend-memfd, we won't really have lost anything as far as our options to introduce memory-backend-guest-memfd at that point or maybe some subclass or something else entirely.
So that's sort of my angle here, but despite the walls of text I don't have a strong opinion one way or the other. But I do want to make sure we don't make things harder on libvirt/mgmt than necessary.
Quoted 41 lines
Quoted 5 lines
For hugetlb, we will support hugetlb (and allow specify hugetlb size) for gmem in the future I believe. It's only that this is done one step at a time so we haven't supported it yet, while the kernel support is still in progress.
The problem with this idea is that it makes it impossible for a mgmt app to know if hugetlb is supported or not, as QEMU will always report it supported against memory-backend-memfd.
Having a memory-backend-guest-memfd ensures the public interface matches what is actually implemented/permitted for guest memfd.
Quoted 22 lines
Quoted 11 lines
"memory-backend-memfd,guest-memfd=on|off" is switching between two separate implementations of the class.
This whole thing is just shouting "use a different class".
There is no meaningful sharing of code here, and the sharing of the public interface is offering apps no value as the impl prevents them from choosing the value of the properties - they have to be set of certain values which are not introspectable.
Please introduce a "memory-backend-guest-memfd" backend instead.
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Quoted 3 lines
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
I think your point still stands, but the current CoCo support doesn't rely on memory-backend-memfd, the private guest_memfd instance is handled by QEMU completely separately as a function of whether or not we are running a CoCo VM. The backends are only for shared memory, which aren't relevant to guest_memfd (without in-place conversion support), so users can select whatever they'd like.
However, backends like memory-backend-file tend to get used for special cases like persisting memory, where there is a high chance of surprises for users trying to make use of these sorts of things for a CoCo VM. That's why memory-backend-memfd is the configuration we normally suggest for CoCo VMs, but it's not required, since nothing stops users from persisting shared memory ranges if that's actually their intent.
The need to enforce the backend more explicitly becomes much more apparent once in-place conversion however, where even shared memory goes through guest-memfd and so the backend necessarily needs to be guest_memfd-aware. The in-place conversion series tracks this via a RAMBlock flag that can be set by whatever backends we decide would be appropriate for managing guest_memfd instances based on these discussions.
So, at least for users following the recommended configuration:
qemu -object sev-snp-guest,...
-object memory-backend-memfd,...
they can then switch on in-place conversion via, e.g.:
qemu -object sev-snp-guest,...,convert-in-place=on
-object memory-backend-memfd,...
and we can flip guest_memfd=on automatically underneath the covers so that both shared/private memory go through guest_memfd as convert-in-place would necessarily imply.
It's not necessary that this happen automatically, but from a user perspective it seems clean, and aligns with the above goals of trying to defer the introduction of new backends/options until we have a better idea of what the relevant uAPI bits are going to look like.
Thanks,
Mike
Quoted 17 lines
Given that we need to be able to control memfd vs guest-memfd for the non-Coco case, it is still worth introducing the new object class today.
Even if the two classes shared all their properties (which they don't given the comment about 'seal' being always on), then a "foo=on|off" that toggles two separate impls is still creating a pair of sub-classes by the backdoor.
With regards,
DanielSignature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 32 lines · Michael Roth
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Peter Xu
Quoted 29 lines · Daniel P. Berrangé
Quoted 28 lines · Michael Roth
From: Peter Xu <[email protected]>
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).
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.PS: there's a trivial touch-up on fd<0 check, because the stub to create guest-memfd may return negative but not -1.
Signed-off-by Peter Xu <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>Signed-off-by Michael Roth <[email protected]>--- backends/hostmem-memfd.c | 56 ++++++++++++++++++++++++++++++++++++---- qapi/qom.json | 6 ++++- 2 files changed, 56 insertions(+), 6 deletions(-)
snip
Quoted 45 lines
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 12 lines
qapi/qom.json +3
-0@@ -771,13 +771,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.2) +#qapi/qom.json line 786
Quoted 10 lines
qapi/qom.json +2
-1# Since: 2.12 ## { 'struct': 'MemoryBackendMemfdProperties', 'base': 'MemoryBackendProperties', 'data': { '*hugetlb': 'bool', '*hugetlbsize': 'size', - '*seal': 'bool' }, + '*seal': 'bool', + '*guest-memfd': 'bool' }, 'if': 'CONFIG_LINUX' }We're reusing the 'memory-backend-memfd' class, and then at runtime refusing allow the user to control any of properties in MemoryBackendProperties.
Quoted 6 lines
Quoted 6 lines
gmemfd should be able to use all ultimately.
For seal, IMHO it's already implied, kind of forced seal=on but it doesn't matter, gmemfd was introduced with sealing, at least what QEMU implies with "
F_SEAL_GROW|F_SEAL_SHRINK|F_SEAL_SEAL". So IMHO we could ignore what user selected and assume it's ON.The naming threw me off, but looking at the actual schema description for 'seal', it basically implies 'fixed-size=on', which
guest_memfddoes enforce. So, aside from the question of whether to reuse the memfd backend, I do think it's more correct to imply seal=on forguest_memfd.Quoted 5 lines
Then we should not have a 'seal' property defined for guest memfd at all. Defining a property and then ignoring it, or only ever allowing 1 value to be set is a design mistake. The property should not exist if it can't ever be changed by the user/app.
Quoted 7 lines
We have a couple examples of what seems like something similar with memory-backend-file,rom=on,readonly=off and memory-backend-*,prealloc=on,reserve=off. Granted, those seem a little more obvious to recognize as mutually-exclusive, but as far as the discussion around libvirt/mgmt/introspection: are there mechanisms in place already to handle cases like that? Or are these special-cases that would be problematic to try to handle similarly?
The difference only matters if what QEMU supports changes over time. eg if current QEMU required 'reserve=off', but later changed to allow either reserve=on or reserve=off, as libvirt can't introspect the built-in runtime restriction.
Quoted 6 lines
Regarding hugepage options, there will undoubtedly be kernels that support
guest_memfdbut not hugetlb, so even if we introduce memory-backend-guest-memfd now so that we can add options when/where it makes sense, libvirt/mgmt. would still need to eventually handle a 'hugetlb' option existing, but not necessarilly implying that the guest can actually use them. It seems like the issue exists in either case.
Yes, the varying kernel dependancy makes the QEMU introspection less valuable than it would otherwise be :-(
Quoted 9 lines
FWIW, I do anticipate that we will need something like memory-backend-guest-memfd for some of the use-cases coming down the pipeline, but for some stuff like CXL/HBM memory support where folks are talking about stuff like custom
guest_memfdallocators or special-purpose NUMA nodes I'm not reasonably confident that memory-backend-guest-memfd won't itself end up being too generic of a construct and be immediately relegated to only handling the exact same set of options as memory-backend-memfd (give or take a 'seal').
Quoted 7 lines
Peter's suggested approach allows us to assume less about how things will eventually look by reusing existing options/command-lines and handling things underneath the covers for the more basic use-cases in the meantime. Once we hit cases that clearly have no business in memory-backend-memfd, we won't really have lost anything as far as our options to introduce memory-backend-guest-memfd at that point or maybe some subclass or something else entirely.
Hmm, if we think that even a separate memory-backend-guest-memfd is not going to suitable / sufficiently flexible for future enhancements, that reduces its value somewhat :-(
Quoted 20 lines
Quoted 16 lines
Quoted 10 lines
This is indeed what Michael used to suggest, and we were discussing in previous version on which is better,
https://lore.kernel.org/r/rjqfiwh57gip3u3psqg33jhmo7ixaj2qwzupc7zdk7f3d26qnu@tglactz67ogk
The hope is this is also easier for either libvirt or most users, but please correct me if it's not the case, especially for libvirt. The plan is when CoCo flags are provided, all things will automatically switch to a CoCo-friendly implementation within QEMU.
Quoted 5 lines
It also means here the guest-memfd= parameter shouldn't be needed in real CoCo contexts because they'll simply be implied (no cmdline change needed for the same "-object memory-backend-memfd" one used to use without CoCo). It's only needed for only special use of guest-memfd, in this case init-shared is the special case where CoCo doesn't use.
Quoted 3 lines
Reading all this, IMHO reusing memory-backend-memfd for the current Coco support was a design mistake, it should have have a memory-backend-guest-memfd object from the start.
Quoted 6 lines
I think your point still stands, but the current CoCo support doesn't rely on memory-backend-memfd, the private
guest_memfdinstance is handled by QEMU completely separately as a function of whether or not we are running a CoCo VM. The backends are only for shared memory, which aren't relevant toguest_memfd(without in-place conversion support), so users can select whatever they'd like.
Ah, I missed that the guest_memfd stuff for Coco is magically handled with QEMU able to "do the right thing" there.
Quoted 20 lines
However, backends like memory-backend-file tend to get used for special cases like persisting memory, where there is a high chance of surprises for users trying to make use of these sorts of things for a CoCo VM. That's why memory-backend-memfd is the configuration we normally suggest for CoCo VMs, but it's not required, since nothing stops users from persisting shared memory ranges if that's actually their intent.
The need to enforce the backend more explicitly becomes much more apparent once in-place conversion however, where even shared memory goes through guest-memfd and so the backend necessarily needs to be guest_memfd-aware. The in-place conversion series tracks this via a RAMBlock flag that can be set by whatever backends we decide would be appropriate for managing
guest_memfdinstances based on these discussions.So, at least for users following the recommended configuration:
qemu -object sev-snp-guest,...
-object memory-backend-memfd,...
Quoted 8 lines
they can then switch on in-place conversion via, e.g.:
qemu -object sev-snp-guest,...,convert-in-place=on
-object memory-backend-memfd,...and we can flip guest_memfd=on automatically underneath the covers so that both shared/private memory go through
guest_memfdas convert-in-place would necessarily imply.
Yes, if we think we can make QEMU "do the right thing" internally that makes a separate object much less desirable. That would be saying that we have a tri-state, guest_memfd=auto|on|off, and such tri-state's would not invite a separate sub-class design. The need for "auto" rather forces the approach you have in this patch.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Re: [PATCH v4 08/12] hostmem: Support fully shared guest memfd to back a VM
Quoted 6 lines · Daniel P. Berrangé
Yes, if we think we can make QEMU "do the right thing" internally that makes a separate object much less desirable. That would be saying that we have a tri-state, guest_memfd=auto|on|off, and such tri-state's would not invite a separate sub-class design. The need for "auto" rather forces the approach you have in this patch.
Just to mention, one option here is we don't introduce "auto" at all, guest-memfd=on could explicitly mean the init full shared mode too.
IOW, when used in a CoCo context, memfd objects will be normally silently created with guest-memfds, like what we used to silently create guest-memfd 2nd layer without any cmdlines, but this time when in-place convertable it'll be the solo backend. User doesn't even need to know.
Then, when guest-memfd=on is specified in such a CoCo context, it should be able to make this specific chunk of memory "init-shared", while the rest could still be in-place convertable. This may make the cmdline slightly more flexible to allow init shared also in CoCo. But I confess I don't know if that would be useful ultimately, and I didn't think all things through.
Thanks,
Signature · 1 line
Peter Xu
PATCH v4 9/12 machine: Rename machine_require_guest_memfd() to *_private()
In reply to Michael RothFrom: Peter Xu <[email protected]>
Differenciate it from fully shared guest-memfd use cases.
When at it, add proper brackets in kvm_handle_hc_map_gpa_range() otherwise checkpatch may complain.
--- backends/hostmem.c | 2 +- hw/core/machine.c | 2 +- hw/i386/pc.c | 2 +- hw/i386/pc_sysfw.c | 4 ++-- hw/i386/x86-common.c | 4 ++-- include/hw/core/boards.h | 2 +- target/i386/kvm/kvm.c | 3 ++- 7 files changed, 10 insertions(+), 9 deletions(-)
backends/hostmem.c +1 -1
@@ -290,7 +290,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_private =machine_require_guest_memfd (machine); + backend->guest_memfd_private =machine_require_guest_memfd_private (machine); backend->reserve = true; backend->prealloc_threads = machine->smp.cpus; }
hw/core/machine.c +1 -1
@@ -1330,7 +1330,7 @@ bool machine_mem_merge(MachineState *machine)return machine->mem_merge; } -bool machine_require_guest_memfd (MachineState *machine) +bool machine_require_guest_memfd_private (MachineState *machine) {return machine->cgs && machine->cgs->require_guest_memfd; }
hw/i386/pc.c +1 -1
@@ -870,7 +870,7 @@ void pc_memory_init(PCMachineState *pcms,if (!is_tdx_vm ()) { option_rom_mr =g_malloc (sizeof (*option_rom_mr)); -if (machine_require_guest_memfd (machine)) { +if (machine_require_guest_memfd_private (machine)) {memory_region_init_ram_guest_memfd (option_rom_mr,NULL ,"pc.rom" ,PC_ROM_SIZE , &error_fatal); }else {
hw/i386/pc_sysfw.c +2 -2
@@ -51,7 +51,7 @@ static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios,/* map the last 128KB of the BIOS in ISA space */ isa_bios_size =MIN (flash_size,128 * KiB); -if (machine_require_guest_memfd (MACHINE (pcms))) { +if (machine_require_guest_memfd_private (MACHINE (pcms))) {memory_region_init_ram_guest_memfd (isa_bios,NULL ,"isa-bios" , isa_bios_size, &error_fatal); }else {
@@ -70,7 +70,7 @@ static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios, ((uint8_t *)flash_ptr) + (flash_size - isa_bios_size), isa_bios_size); -if (!machine_require_guest_memfd (current_machine)) { +if (!machine_require_guest_memfd_private (current_machine)) {memory_region_set_readonly (isa_bios, true); } }
hw/i386/x86-common.c +2 -2
@@ -1036,7 +1036,7 @@ static void load_bios_from_file(X86MachineState *x86ms, const char *bios_name,ssize_t ret;/* BIOS load */ -if (machine_require_guest_memfd (MACHINE (x86ms))) { +if (machine_require_guest_memfd_private (MACHINE (x86ms))) {memory_region_init_ram_guest_memfd (&x86ms->bios,NULL ,"pc.bios" , bios_size, &error_fatal);if (is_tdx_vm ()) {
@@ -1106,7 +1106,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware, bios_size =get_bios_size (x86ms, bios_name, filename);load_bios_from_file (x86ms, bios_name, filename, bios_size, isapc_ram_fw); -if (!machine_require_guest_memfd (MACHINE (x86ms))) { +if (!machine_require_guest_memfd_private (MACHINE (x86ms))) {/* map the last 128KB of the BIOS in ISA space */ x86_isa_bios_init (&x86ms->isa_bios, rom_memory, &x86ms->bios, !isapc_ram_fw);
include/hw/core/boards.h +1 -1
@@ -43,7 +43,7 @@ bool machine_usb(MachineState *machine);int machine_phandle_start (MachineState *machine);bool machine_dump_guest_core (MachineState *machine);bool machine_mem_merge (MachineState *machine); -bool machine_require_guest_memfd (MachineState *machine); +bool machine_require_guest_memfd_private (MachineState *machine);HotpluggableCPUList *machine_query_hotpluggable_cpus (MachineState *machine); void machine_set_cpu_numa_node(MachineState *machine, const CpuInstanceProperties *props,
target/i386/kvm/kvm.c +2 -1
@@ -6488,8 +6488,9 @@ static int kvm_handle_hc_map_gpa_range(X86CPU *cpu, struct kvm_run *run)uint64_t gpa, size, attributes;int ret; -if (!machine_require_guest_memfd (current_machine)) +if (!machine_require_guest_memfd_private (current_machine)) {return -EINVAL ; + } gpa = run->hypercall.args[0 ]; size = run->hypercall.args[1 ] *TARGET_PAGE_SIZE ;
Signature · 1 line
2.43.0
PATCH v4 10/12 memory: Rename memory_region_init_ram_guest_memfd() to *_private()
In reply to Michael RothFrom: Peter Xu <[email protected]>
Differenciate it from fully shared guest-memfd use cases.
--- backends/igvm.c | 4 ++-- hw/i386/pc.c | 4 ++-- hw/i386/pc_sysfw.c | 4 ++-- hw/i386/x86-common.c | 4 ++-- include/system/memory.h | 10 +++++----- system/memory.c | 8 +++++--- 6 files changed, 18 insertions(+), 16 deletions(-)
backends/igvm.c +2 -2
@@ -256,8 +256,8 @@ static void *qigvm_prepare_memory(QIgvm *ctx, uint64_t addr, uint64_t size, igvm_pages =g_new0 (MemoryRegion,1 );if (ctx->machine_state->cgs && ctx->machine_state->cgs->require_guest_memfd) { -if (!memory_region_init_ram_guest_memfd (igvm_pages,NULL , - region_name, size, errp)) { +if (!memory_region_init_ram_guest_memfd_private ( + igvm_pages,NULL , region_name, size, errp)) {g_free (igvm_pages);return NULL ; }
hw/i386/pc.c +2 -2
@@ -871,8 +871,8 @@ void pc_memory_init(PCMachineState *pcms,if (!is_tdx_vm ()) { option_rom_mr =g_malloc (sizeof (*option_rom_mr));if (machine_require_guest_memfd_private (machine)) { -memory_region_init_ram_guest_memfd (option_rom_mr,NULL ,"pc.rom" , -PC_ROM_SIZE , &error_fatal); +memory_region_init_ram_guest_memfd_private ( + option_rom_mr,NULL ,"pc.rom" ,PC_ROM_SIZE , &error_fatal); }else {memory_region_init_ram (option_rom_mr,NULL ,"pc.rom" ,PC_ROM_SIZE , &error_fatal);
hw/i386/pc_sysfw.c +2 -2
@@ -52,8 +52,8 @@ static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios,/* map the last 128KB of the BIOS in ISA space */ isa_bios_size =MIN (flash_size,128 * KiB);if (machine_require_guest_memfd_private (MACHINE (pcms))) { -memory_region_init_ram_guest_memfd (isa_bios,NULL ,"isa-bios" , - isa_bios_size, &error_fatal); +memory_region_init_ram_guest_memfd_private ( + isa_bios,NULL ,"isa-bios" , isa_bios_size, &error_fatal); }else {memory_region_init_ram (isa_bios,NULL ,"isa-bios" , isa_bios_size, &error_fatal);
hw/i386/x86-common.c +2 -2
@@ -1037,8 +1037,8 @@ static void load_bios_from_file(X86MachineState *x86ms, const char *bios_name,/* BIOS load */ if (machine_require_guest_memfd_private (MACHINE (x86ms))) { -memory_region_init_ram_guest_memfd (&x86ms->bios,NULL ,"pc.bios" , - bios_size, &error_fatal); +memory_region_init_ram_guest_memfd_private ( + &x86ms->bios,NULL ,"pc.bios" , bios_size, &error_fatal);if (is_tdx_vm ()) {tdx_set_tdvf_region (&x86ms->bios); }
include/system/memory.h +5 -5
@@ -1358,11 +1358,11 @@ bool memory_region_init_ram(MemoryRegion *mr,uint64_t size, Error **errp); -bool memory_region_init_ram_guest_memfd (MemoryRegion *mr, -Object *owner, -const char *name, -uint64_t size, -Error **errp); +bool memory_region_init_ram_guest_memfd_private (MemoryRegion *mr, +Object *owner, +const char *name, +uint64_t size, +Error **errp);/** * memory_region_init_rom: Initialize a ROM memory region.
system/memory.c +5 -3
@@ -3655,9 +3655,11 @@ bool memory_region_init_ram(MemoryRegion *mr, Object *owner,return true; } -bool memory_region_init_ram_guest_memfd (MemoryRegion *mr,Object *owner, -const char *name,uint64_t size, -Error **errp) +bool memory_region_init_ram_guest_memfd_private (MemoryRegion *mr, +Object *owner, +const char *name, +uint64_t size, +Error **errp) {if (!memory_region_init_ram_flags_nomigrate (mr, owner, name, size,RAM_GUEST_MEMFD_PRIVATE , errp)) {
Signature · 1 line
2.43.0
PATCH v4 11/12 tests/migration-test: Support guest-memfd init shared mem type
In reply to Michael RothFrom: Peter Xu <[email protected]>
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.c | 60 +++++++++++++++++++++++++++++++ tests/qtest/migration/framework.h | 4 +++ 2 files changed, 64 insertions(+)
tests/qtest/migration/framework.c +60 -0
@@ -26,6 +26,10 @@#include "qemu/range.h" #include "qemu/sockets.h" +#ifdef CONFIG_LINUX +#include <linux/kvm.h> +#include <sys/ioctl.h> +#endif #define QEMU_VM_FILE_MAGIC 0x5145564d#define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
@@ -296,6 +300,9 @@ static char *migrate_mem_type_get_opts(MemType type, const char *memory_size)case MEM_TYPE_MEMFD : backend =g_strdup ("-object memory-backend-memfd" );break ; +case MEM_TYPE_GUEST_MEMFD : + backend =g_strdup ("-object memory-backend-memfd,guest-memfd=on" ); +break ;default :g_assert_not_reached ();break ;
@@ -444,8 +451,55 @@ int migrate_args(char **from, char **to, MigrateStart *args)return 0 ; } +static bool kvm_guest_memfd_init_shared_supported (const char **reason) +{ +assert (*reason ==NULL ); + +#ifdef CONFIG_LINUX +int ret, fd =-1 ; + +if (!migration_get_env ()->has_kvm) { + *reason ="KVM is not enabled in the current QEMU build" ; + goto out; + } + + fd =open ("/dev/kvm" ,O_RDWR ); +if (fd <0 ) { + *reason ="KVM module isn't available or missing permission" ; + goto out; + } + + ret =ioctl (fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD ); +if (!ret) { + *reason ="KVM module doesn't support guest-memfd" ; + goto out; + } + + ret =ioctl (fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 ) { + *reason ="KVM doesn't support KVM_CAP_GUEST_MEMFD_FLAGS" ; + goto out; + } + +if (!(ret &GUEST_MEMFD_FLAG_INIT_SHARED )) { + *reason ="KVM doesn't support GUEST_MEMFD_FLAG_INIT_SHARED" ; + goto out; + } +out: +if (fd >=0 ) { +close (fd); + } +#else + *reason ="KVM not supported on non-Linux OS" ; +#endif + +return !*reason; +} +static bool migrate_mem_type_prepare (MemType type) { +const char *reason =NULL ; +switch (type) {case MEM_TYPE_SHMEM :if (!g_file_test ("/dev/shm" ,G_FILE_TEST_IS_DIR )) {
@@ -453,6 +507,12 @@ static bool migrate_mem_type_prepare(MemType type)return false; }break ; +case MEM_TYPE_GUEST_MEMFD : +if (!kvm_guest_memfd_init_shared_supported (&reason)) { +g_test_skip (reason); +return false; + } +break ;default :break ; }
tests/qtest/migration/framework.h +4 -0
@@ -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;
Signature · 1 line
2.43.0
PATCH v4 12/12 tests/migration-test: Add a precopy test for guest-memfd
In reply to Michael RothFrom: Peter Xu <[email protected]>
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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
tests/qtest/migration/precopy-tests.c +10 -0
@@ -182,6 +182,14 @@ static void test_precopy_tcp_plain(char *name, MigrateCommon *args)test_precopy_common (args); } +static void test_precopy_tcp_plain_gmemfd (char *name,MigrateCommon *args) +{ + args->uri ="tcp:127.0.0.1:0" ; + args->start.mem_type =MEM_TYPE_GUEST_MEMFD ; + +test_precopy_common (args); +} +static void test_precopy_tcp_switchover_ack (char *name,MigrateCommon *args) {/*
@@ -1108,6 +1116,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.43.0
Quoted 18 lines · Michael Roth
v1: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]/
v4:
- Picked up v3 series from Peter Xu and rebased on 11.1.0
- Collected pending Reviewed-by's
- Per-patch changes:
[01/12] Fix blank space (Xiaoyao)
Fix commit message typos
[02/12] Drop duplicated error-handling for capability checks [05/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [07/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [08/12] Disallow 'seal' and 'hugetlb' options based on guest-memfd=on
Fix minor typo in comment
[11/12] Fix up typo ing_test_skip()reason
Pending: Movekvm_guest_memfd_init_shared_supported()to
migration-util.c(Fabiano)
Quoted 5 lines
This patchset is also available at:
https://github.com/amdese/qemu/commits/gmem-shared-mem-v4
and is based on top of v11.1.0
Should I just pick this up? I'll touch up the commit log that Phil requested. Objections welcomed before next week.
Signature · 1 line
Peter Xu
Quoted 19 lines · Peter Xu
Quoted 18 lines · Michael Roth
v1: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]/
v4:
- Picked up v3 series from Peter Xu and rebased on 11.1.0
- Collected pending Reviewed-by's
- Per-patch changes:
[01/12] Fix blank space (Xiaoyao)
Fix commit message typos
[02/12] Drop duplicated error-handling for capability checks [05/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [07/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [08/12] Disallow 'seal' and 'hugetlb' options based on guest-memfd=on
Fix minor typo in comment
[11/12] Fix up typo ing_test_skip()reason
Pending: Movekvm_guest_memfd_init_shared_supported()to
migration-util.c(Fabiano)
Quoted 8 lines
Quoted 5 lines
This patchset is also available at:
https://github.com/amdese/qemu/commits/gmem-shared-mem-v4
and is based on top of v11.1.0
Should I just pick this up? I'll touch up the commit log that Phil requested. Objections welcomed before next week.
Hi Peter,
No objection here, although I'll be posting new version of the in-place conversion support later this week (~Wed-ish) and was planning to an updated version of this series roughly the same time so which ever way is best for you.
If you do post one though there's also the missing 'return false' and dropping the seal=off enforcement that Daniel noticed before we started talking about introspection fun.
Thanks!
-Mike
Quoted 4 lines
Signature · 2 lines
-- Peter Xu
Quoted 22 lines · Michael Roth
Quoted 21 lines · Peter Xu
Quoted 20 lines · Michael Roth
v1: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]/
v4:
- Picked up v3 series from Peter Xu and rebased on 11.1.0
- Collected pending Reviewed-by's
- Per-patch changes:
[01/12] Fix blank space (Xiaoyao)
Fix commit message typos
[02/12] Drop duplicated error-handling for capability checks [05/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [07/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [08/12] Disallow 'seal' and 'hugetlb' options based on guest-memfd=on
Fix minor typo in comment
[11/12] Fix up typo ing_test_skip()reason
Pending: Movekvm_guest_memfd_init_shared_supported()to
migration-util.c(Fabiano)This patchset is also available at:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
https://github.com/amdese/qemu/commits/gmem-shared-mem-v4
and is based on top of v11.1.0
Should I just pick this up? I'll touch up the commit log that Phil requested. Objections welcomed before next week.
Hi Peter,
Hi, Michael,
Quoted 9 lines
No objection here, although I'll be posting new version of the in-place conversion support later this week (~Wed-ish) and was planning to an updated version of this series roughly the same time so which ever way is best for you.
If you do post one though there's also the missing 'return false' and dropping the seal=off enforcement that Daniel noticed before we started talking about introspection fun.
I didn't mean to repost this series, I meant to pick it up for a pull. Sorry if it was confusing..
I want to make sure balls are rolling. From our side, we weren't rush on this one to land, as we'll need to wait for 1G to settle anyway, but if we have others (like your in-place series upcoming) pending, I think we should just land it sooner.
You're right, we need to fix things, I noticed it right after I sent the email and then I found CI / unit test failing.. but I haven't got a chance to say.. If you repost please check migration-test with --full, otherwise the gmemfd test will be skipped.
I have two fixups queued locally, I'll attach it here, please use it whatever way you like, I'll wait for your repost. NOTE: I touched up the hugetlb failure message when I fixed it up myself, but since you'll repost you can decide; please just treat it as a comment then, I just think the new err message is less strong because we will support it.
Thanks,
===8<===
From 0a0dd0f808d4f668ac099945ab35a981e1a07bfb Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Fri, 21 Aug 2026 12:16:15 -0400
Subject: [PATCH 1/2] fixup! hostmem: Support fully shared guest memfd to back
a VM
--- backends/hostmem-memfd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
backends/hostmem-memfd.c +6 -4
@@ -57,14 +57,16 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) }if (m->guest_memfd) { +/* +* NOTE: guest-memfd ignores seal=on/off because it always +* implicitly seals the FD by definition. +*/ if (!backend->share) {error_setg (errp,"guest-memfd=on must be used with share=on" );return false; - }else if (m->seal) { -error_setg (errp,"guest-memfd=on must be used with seal=off" ); -return false; }else if (m->hugetlb) { -error_setg (errp,"guest-memfd=on must be used with hugetlb=off" ); +error_setg (errp,"guest-memfd=on doesn't support hugetlb=on yet" ); +return false; } fd = kvm_create_guest_memfd(backend->size,
Signature · 14 lines
2.54.0 From 66ac7b3f2268430106bfe8034ef8460215e9db91 Mon Sep 17 00:00:00 2001 From: Peter Xu <[email protected]> Date: Fri, 21 Aug 2026 13:00:09 -0400 Subject: [PATCH 2/2] fixup! tests/migration-test: Add a precopy test for guest-memfd Signed-off-by: Peter Xu <[email protected]> --- tests/qtest/migration/precopy-tests.c | 1 - 1 file changed, 1 deletion(-)
tests/qtest/migration/precopy-tests.c +0 -1
@@ -184,7 +184,6 @@ static void test_precopy_tcp_plain(char *name, MigrateCommon *args)static void test_precopy_tcp_plain_gmemfd (char *name,MigrateCommon *args) { - args->uri ="tcp:127.0.0.1:0" ; args->start.mem_type =MEM_TYPE_GUEST_MEMFD ;test_precopy_common (args);
Signature · 5 lines
2.54.0 -- Peter Xu
Quoted 40 lines · Peter Xu
Quoted 22 lines · Michael Roth
Quoted 21 lines · Peter Xu
Quoted 20 lines · Michael Roth
v1: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]/
v4:
- Picked up v3 series from Peter Xu and rebased on 11.1.0
- Collected pending Reviewed-by's
- Per-patch changes:
[01/12] Fix blank space (Xiaoyao)
Fix commit message typos
[02/12] Drop duplicated error-handling for capability checks [05/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [07/12] Fix up commit message, disambigurate "in-place" terminology (Xiaoyao) [08/12] Disallow 'seal' and 'hugetlb' options based on guest-memfd=on
Fix minor typo in comment
[11/12] Fix up typo ing_test_skip()reason
Pending: Movekvm_guest_memfd_init_shared_supported()to
migration-util.c(Fabiano)This patchset is also available at:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
https://github.com/amdese/qemu/commits/gmem-shared-mem-v4
and is based on top of v11.1.0
Should I just pick this up? I'll touch up the commit log that Phil requested. Objections welcomed before next week.
Hi Peter,
Hi, Michael,
Quoted 6 lines
No objection here, although I'll be posting new version of the in-place conversion support later this week (~Wed-ish) and was planning to an updated version of this series roughly the same time so which ever way is best for you.
Quoted 6 lines
Quoted 3 lines
If you do post one though there's also the missing 'return false' and dropping the seal=off enforcement that Daniel noticed before we started talking about introspection fun.
I didn't mean to repost this series, I meant to pick it up for a pull. Sorry if it was confusing..
Ahh ok :) No problem!
Quoted 5 lines
I want to make sure balls are rolling. From our side, we weren't rush on this one to land, as we'll need to wait for 1G to settle anyway, but if we have others (like your in-place series upcoming) pending, I think we should just land it sooner.
Agreed, LGTM!
Quoted 5 lines
You're right, we need to fix things, I noticed it right after I sent the email and then I found CI / unit test failing.. but I haven't got a chance to say.. If you repost please check migration-test with --full, otherwise the gmemfd test will be skipped.
Ok, I had seen migration-test passing but I didn't run it with full so I'll look into what's going on there.
Quoted 6 lines
I have two fixups queued locally, I'll attach it here, please use it whatever way you like, I'll wait for your repost. NOTE: I touched up the hugetlb failure message when I fixed it up myself, but since you'll repost you can decide; please just treat it as a comment then, I just think the new err message is less strong because we will support it.
Ok, I'll get these rolled in as well.
Thanks!
-Mike
Quoted 77 lines
Thanks,
===8<===
From
0a0dd0f808d4f668ac099945ab35a981e1a07bfbMon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Fri, 21 Aug 2026 12:16:15 -0400
Subject: [PATCH 1/2] fixup! hostmem: Support fully shared guest memfd to back
a VMSigned-off-by Peter Xu <[email protected]>--- backends/hostmem-memfd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)backends/hostmem-memfd.c +6
-4@@ -57,14 +57,16 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) }if (m->guest_memfd) { +/* +* NOTE: guest-memfd ignores seal=on/off because it always +* implicitly seals the FD by definition. +*/ if (!backend->share) {error_setg (errp,"guest-memfd=on must be used with share=on" );return false; - }else if (m->seal) { -error_setg (errp,"guest-memfd=on must be used with seal=off" ); -return false; }else if (m->hugetlb) { -error_setg (errp,"guest-memfd=on must be used with hugetlb=off" ); +error_setg (errp,"guest-memfd=on doesn't support hugetlb=on yet" ); +return false; } fd = kvm_create_guest_memfd(backend->size,Signature · 14 lines
2.54.0 From 66ac7b3f2268430106bfe8034ef8460215e9db91 Mon Sep 17 00:00:00 2001 From: Peter Xu <[email protected]> Date: Fri, 21 Aug 2026 13:00:09 -0400 Subject: [PATCH 2/2] fixup! tests/migration-test: Add a precopy test for guest-memfd Signed-off-by: Peter Xu <[email protected]> --- tests/qtest/migration/precopy-tests.c | 1 - 1 file changed, 1 deletion(-)tests/qtest/migration/precopy-tests.c +0
-1@@ -184,7 +184,6 @@ static void test_precopy_tcp_plain(char *name, MigrateCommon *args)static void test_precopy_tcp_plain_gmemfd (char *name,MigrateCommon *args) { - args->uri ="tcp:127.0.0.1:0" ; args->start.mem_type =MEM_TYPE_GUEST_MEMFD ;test_precopy_common (args);Signature · 5 lines
2.54.0 -- Peter Xu