Add guest_memfd support for arm64
Fuad Tabba
reviewercover Add guest_memfd support for arm64
Hi folks,
Due to popular demand, i.e., Alexandru asking me about it, I repolished my guest_memfd support series. The one I sent years ago was stale and over-engineered, so this is a fresh start instead.
This series adds support for backing guest RAM with guest_memfd on arm64, for non-protected VMs under both regular KVM and pKVM.
The first two patches are standalone fixes in the code this series builds on: an uninitialized return value in kvm__for_each_mem_bank(), and doubled newlines in die() messages. The next two switch memory registration to kvm_userspace_memory_region2 unconditionally and add guest_memfd plumbing to kvm__register_mem().
A new --guest-memfd option creates guest RAM from a guest_memfd with mmap support (GUEST_MEMFD_FLAG_MMAP and GUEST_MEMFD_FLAG_INIT_SHARED, Linux 6.18+), maps it to provide the userspace mapping, and registers it with KVM_SET_USER_MEMORY_REGION2.
Protected VMs need in-place shared/private conversion of guest_memfd, which is still in progress in the kernel [1], so --protected rejects the option.
Based on kvmtool master (ca0ddafa941d).
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (5):
Initialize the return value in kvm__for_each_mem_bank() Remove newline from end of die() aborts
Use kvm_userspace_memory_region2 for all memory registration Add guest_memfd support to kvm__register_mem()
arm64: Add --guest-memfd option to back guest RAM with guest_memfd
arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++------ hw/cfi_flash.c | 2 +- include/kvm/kvm.h | 16 +++++++++-- include/kvm/util.h | 1 + kvm.c | 26 +++++++++++------ util/util.c | 44 +++++++++++++++++++++++++---- 7 files changed, 106 insertions(+), 26 deletions(-)
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 1/5 Initialize the return value in kvm__for_each_mem_bank()
If none of the bank types match, the function would return an uninitialized value.
--- kvm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kvm.c +3 -2
@@ -383,14 +383,15 @@ u64 host_to_guest_flat(struct kvm *kvm, void *ptr)* their type. * * If one call to @fun returns a non-zero value, stop iterating and return the -* value. Otherwise, return zero. +* value. If none of the bank types match, return -ENODEV. Otherwise, return +* zero. */ int kvm__for_each_mem_bank (struct kvm *kvm,enum kvm_mem_type type,int (*fun)(struct kvm *kvm,struct kvm_mem_bank *bank,void *data),void *data) { -int ret;struct kvm_mem_bank *bank; +int ret = -ENODEV ;list_for_each_entry (bank, &kvm->mem_banks, list) {if (type !=KVM_MEM_TYPE_ALL && !(bank->type & type))
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 2/5 Remove newline from end of die() aborts
In reply to Fuad Tabbadie() already adds the newline at the end of the string.
--- util/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
util/util.c +5 -5
@@ -128,14 +128,14 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)unsigned long blk_size;if (statfs (htlbfs_path, &sfs) <0 ) -die ("Can't stat %s\n" , htlbfs_path); +die ("Can't stat %s" , htlbfs_path);if ((unsigned int )sfs.f_type !=HUGETLBFS_MAGIC ) -die ("%s is not hugetlbfs!\n" , htlbfs_path); +die ("%s is not hugetlbfs!" , htlbfs_path); blk_size = (unsigned long )sfs.f_bsize;if (sfs.f_bsize ==0 || blk_size > size) { -die ("Can't use hugetlbfs pagesize %ld for mem size %lld\n" , +die ("Can't use hugetlbfs pagesize %ld for mem size %lld" , blk_size, (unsigned long long )size); }
@@ -144,10 +144,10 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)snprintf (mpath,PATH_MAX ,"%s/kvmtoolXXXXXX" , htlbfs_path); fd =mkstemp (mpath);if (fd <0 ) -die ("Can't open %s for hugetlbfs map\n" , mpath); +die ("Can't open %s for hugetlbfs map" , mpath);unlink (mpath);if (ftruncate (fd, size) <0 ) -die ("Can't ftruncate for mem mapping size %lld\n" , +die ("Can't ftruncate for mem mapping size %lld" , (unsigned long long )size); addr =mmap (NULL , size,PROT_RW ,MAP_PRIVATE , fd,0 );close (fd);
Signature · 1 line
2.39.5
Re: [PATCH kvmtool v1 2/5] Remove newline from end of die() aborts
Quoted 3 lines · Fuad Tabba
die()already adds the newline at the end of the string.Signed-off-by Fuad Tabba <[email protected]>
While you are at it, there are plenty more die() with \n, worth fixing them too ?
$ git grep "die(" | grep "\\\n" | grep -v util
arm64/kvm-cpu.c: die("32bit guests are not
supported\n");
arm64/kvm-cpu.c: die("What's that mode???\n");
builtin-run.c: die("Unable to resolve program %s:
%s\n", src, strerror(errno));
builtin-run.c: die("Pathname too long: %s -> %s\n",
src, resolved_path);
include/kvm/fdt.h: die("Error creating device tree:
%s: %s\n", \
kvm-cpu.c: die("Pause signaled for already paused
CPU\n");
kvm.c: die("kvm__arch_setup_firmware() failed with
error %d\n", ret);
powerpc/kvm.c: die("Couldn't map %lld bytes for RAM (%d)\n",
powerpc/kvm.c: die("Can't allocate %d bytes for HPT\n",
(1<<HPT_ORDER));
powerpc/kvm.c: die("initrd too big to contain
in guest RAM.\n");
powerpc/kvm.c: die("Couldn't create RTAS FDT properties\n");
powerpc/spapr_hvcons.c: die("term_putc_iov error %d!\n",
errno);
powerpc/spapr_pci.c: die("error making bus subnode, %s\n",
fdt_strerror(bus_off));
powerpc/spapr_pci.c: die("error making node subnode,
%s\n", fdt_strerror(bus_off));
powerpc/xics.c: die("Invalid server number for cpuid
%ld\n", vcpu->cpu_id);
riscv/fdt.c: die("Invalid CPU type %s\n", arg);
riscv/fdt.c: die("No way to generate IRQCHIP FDT node\n");
riscv/irq.c: die("No IRQCHIP found\n");
term.c: die("term_poll_thread_loop: error polling device fds %d\n", errno);
term.c: die("Unable to create console input poll thread\n");
virtio/mmio.c: die("Unable to generate device tree nodes without
libfdt\n");
virtio/vhost.c: die("Unable to start vhost polling thread\n");
x86/boot.c: die("firmware image %s is too big to fit in
memory (%Lu KB).\n", firmware_filename, (u64)(st.st_size / 1024));Quoted 29 lines
--- util/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)util/util.c +3
-3@@ -128,14 +128,14 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)unsigned long blk_size;if (statfs (htlbfs_path, &sfs) <0 ) -die ("Can't stat %s\n" , htlbfs_path); +die ("Can't stat %s" , htlbfs_path);if ((unsigned int )sfs.f_type !=HUGETLBFS_MAGIC ) -die ("%s is not hugetlbfs!\n" , htlbfs_path); +die ("%s is not hugetlbfs!" , htlbfs_path); blk_size = (unsigned long )sfs.f_bsize;if (sfs.f_bsize ==0 || blk_size > size) { -die ("Can't use hugetlbfs pagesize %ld for mem size %lld\n" , +die ("Can't use hugetlbfs pagesize %ld for mem size %lld" , blk_size, (unsigned long long )size); }@@ -144,10 +144,10 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)snprintf (mpath,PATH_MAX ,"%s/kvmtoolXXXXXX" , htlbfs_path); fd =mkstemp (mpath);
util/util.c void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) · line 153
Quoted 10 lines
util/util.c +2
-2if (fd <0 ) -die ("Can't open %s for hugetlbfs map\n" , mpath); +die ("Can't open %s for hugetlbfs map" , mpath);unlink (mpath);if (ftruncate (fd, size) <0 ) -die ("Can't ftruncate for mem mapping size %lld\n" , +die ("Can't ftruncate for mem mapping size %lld" , (unsigned long long )size); addr =mmap (NULL , size,PROT_RW ,MAP_PRIVATE , fd,0 );close (fd);
For this one :
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 2/5] Remove newline from end of die() aborts
Quoted 8 lines · Suzuki K Poulose
Quoted 3 lines · Fuad Tabba
die()already adds the newline at the end of the string.Signed-off-by Fuad Tabba <[email protected]>While you are at it, there are plenty more
die()with \n, worth fixing them too ?
I fixed the ones relevant to this series. I'm happy to do the rest, if Will doesn't mind the churn. One commit or multiple ones?
Thanks for the review!
Cheers,
/fuad
Quoted 85 lines
$ git grep "die(" | grep "\\\n" | grep -v util arm64/kvm-cpu.c: die("32bit guests are not supported\n"); arm64/kvm-cpu.c: die("What's that mode???\n"); builtin-run.c: die("Unable to resolve program %s: %s\n", src, strerror(errno)); builtin-run.c: die("Pathname too long: %s -> %s\n", src, resolved_path); include/kvm/fdt.h: die("Error creating device tree: %s: %s\n", \ kvm-cpu.c: die("Pause signaled for already paused CPU\n"); kvm.c: die("kvm__arch_setup_firmware() failed with error %d\n", ret); powerpc/kvm.c: die("Couldn't map %lld bytes for RAM (%d)\n", powerpc/kvm.c: die("Can't allocate %d bytes for HPT\n", (1<<HPT_ORDER)); powerpc/kvm.c: die("initrd too big to contain in guest RAM.\n"); powerpc/kvm.c: die("Couldn't create RTAS FDT properties\n"); powerpc/spapr_hvcons.c: die("term_putc_iov error %d!\n", errno); powerpc/spapr_pci.c: die("error making bus subnode, %s\n", fdt_strerror(bus_off)); powerpc/spapr_pci.c: die("error making node subnode, %s\n", fdt_strerror(bus_off)); powerpc/xics.c: die("Invalid server number for cpuid %ld\n", vcpu->cpu_id); riscv/fdt.c: die("Invalid CPU type %s\n", arg); riscv/fdt.c: die("No way to generate IRQCHIP FDT node\n"); riscv/irq.c: die("No IRQCHIP found\n"); term.c: die("term_poll_thread_loop: error polling device fds %d\n", errno); term.c: die("Unable to create console input poll thread\n"); virtio/mmio.c: die("Unable to generate device tree nodes without libfdt\n"); virtio/vhost.c: die("Unable to start vhost polling thread\n"); x86/boot.c: die("firmware image %s is too big to fit in memory (%Lu KB).\n", firmware_filename, (u64)(st.st_size / 1024));Quoted 29 lines
--- util/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)util/util.c +3
-3@@ -128,14 +128,14 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)unsigned long blk_size;if (statfs (htlbfs_path, &sfs) <0 ) -die ("Can't stat %s\n" , htlbfs_path); +die ("Can't stat %s" , htlbfs_path);if ((unsigned int )sfs.f_type !=HUGETLBFS_MAGIC ) -die ("%s is not hugetlbfs!\n" , htlbfs_path); +die ("%s is not hugetlbfs!" , htlbfs_path); blk_size = (unsigned long )sfs.f_bsize;if (sfs.f_bsize ==0 || blk_size > size) { -die ("Can't use hugetlbfs pagesize %ld for mem size %lld\n" , +die ("Can't use hugetlbfs pagesize %ld for mem size %lld" , blk_size, (unsigned long long )size); }@@ -144,10 +144,10 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size)snprintf (mpath,PATH_MAX ,"%s/kvmtoolXXXXXX" , htlbfs_path); fd =mkstemp (mpath);util/util.c void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) · line 153
Quoted 10 lines
util/util.c +2
-2if (fd <0 ) -die ("Can't open %s for hugetlbfs map\n" , mpath); +die ("Can't open %s for hugetlbfs map" , mpath);unlink (mpath);if (ftruncate (fd, size) <0 ) -die ("Can't ftruncate for mem mapping size %lld\n" , +die ("Can't ftruncate for mem mapping size %lld" , (unsigned long long )size); addr =mmap (NULL , size,PROT_RW ,MAP_PRIVATE , fd,0 );close (fd);For this one :
Reviewed-by Suzuki K Poulose <[email protected]>IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerPATCH 3/5 Use kvm_userspace_memory_region2 for all memory registration
In reply to Fuad TabbaSwitch kvm__register_mem() and kvm__destroy_mem() from kvm_userspace_memory_region to kvm_userspace_memory_region2 and KVM_SET_USER_MEMORY_REGION2 unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.
This prepares for adding guest_memfd support.
--- kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kvm.c +5 -6
@@ -189,7 +189,7 @@ core_exit(kvm__exit);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) { -struct kvm_userspace_memory_region mem; +struct kvm_userspace_memory_region2 mem;struct kvm_mem_bank *bank;int ret;
@@ -213,14 +213,14 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region ) { + mem = (struct kvm_userspace_memory_region2 ) { .slot = bank->slot, .guest_phys_addr = guest_phys, .memory_size =0 , .userspace_addr = (unsigned long )userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;
@@ -239,7 +239,6 @@ out:int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type) { -struct kvm_userspace_memory_region mem;struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;struct list_head *prev_entry;
@@ -320,7 +319,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |=KVM_MEM_READONLY ;if (type !=KVM_MEM_TYPE_RESERVED ) { - mem = (struct kvm_userspace_memory_region ) { +struct kvm_userspace_memory_region2 mem = { .slot = slot, .flags = flags, .guest_phys_addr = guest_phys,
@@ -328,7 +327,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;
Signature · 1 line
2.39.5
Suzuki K Poulose
reviewerRe: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
Hi Fuad
Quoted 5 lines · Fuad Tabba
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.
Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
Cheers
Suzuki
Quoted 56 lines
This prepares for adding
guest_memfdsupport.Signed-off-by Fuad Tabba <[email protected]>--- kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kvm.c +4
-5@@ -189,7 +189,7 @@ core_exit(kvm__exit);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) { -struct kvm_userspace_memory_region mem; +struct kvm_userspace_memory_region2 mem;struct kvm_mem_bank *bank;int ret;@@ -213,14 +213,14 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region ) { + mem = (struct kvm_userspace_memory_region2 ) { .slot = bank->slot, .guest_phys_addr = guest_phys, .memory_size =0 , .userspace_addr = (unsigned long )userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;@@ -239,7 +239,6 @@ out:int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type) { -struct kvm_userspace_memory_region mem;struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;struct list_head *prev_entry;@@ -320,7 +319,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |=KVM_MEM_READONLY ;if (type !=KVM_MEM_TYPE_RESERVED ) { - mem = (struct kvm_userspace_memory_region ) { +struct kvm_userspace_memory_region2 mem = { .slot = slot, .flags = flags, .guest_phys_addr = guest_phys,
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 333
Quoted 8 lines
kvm.c +1
-1@@ -328,7 +327,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
Hi Suzuki,
Quoted 4 lines · Suzuki K Poulose
Hi Fuad
On 06/07/2026 09:35, Fuad Tabba wrote:
Quoted 8 lines
Quoted 5 lines
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
KVM_SET_USER_MEMORY_REGION2 goes back to 6.8, so this would break kernels older than that. I can gate it so that REGION2 is only used when --guest-memfd is requested, keeping REGION for the default path. But like you said, do others care for kernels that old?
Cheers,
/fuad
Quoted 71 lines
Cheers
SuzukiQuoted 56 lines
This prepares for adding
guest_memfdsupport.Signed-off-by Fuad Tabba <[email protected]>--- kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kvm.c +4
-5@@ -189,7 +189,7 @@ core_exit(kvm__exit);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) { -struct kvm_userspace_memory_region mem; +struct kvm_userspace_memory_region2 mem;struct kvm_mem_bank *bank;int ret;@@ -213,14 +213,14 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region ) { + mem = (struct kvm_userspace_memory_region2 ) { .slot = bank->slot, .guest_phys_addr = guest_phys, .memory_size =0 , .userspace_addr = (unsigned long )userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;@@ -239,7 +239,6 @@ out:int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type) { -struct kvm_userspace_memory_region mem;struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;struct list_head *prev_entry;@@ -320,7 +319,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |=KVM_MEM_READONLY ;if (type !=KVM_MEM_TYPE_RESERVED ) { - mem = (struct kvm_userspace_memory_region ) { +struct kvm_userspace_memory_region2 mem = { .slot = slot, .flags = flags, .guest_phys_addr = guest_phys,kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 333
Quoted 8 lines
kvm.c +1
-1@@ -328,7 +327,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
Quoted 13 lines · Fuad Tabba
Hi Suzuki,
Quoted 10 lines · Suzuki K Poulose
Hi Fuad
Quoted 5 lines · Fuad Tabba
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.
Quoted 7 lines
Quoted 2 lines
Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
KVM_SET_USER_MEMORY_REGION2goes back to 6.8, so this would break kernels older than that. I can gate it so that REGION2 is only used when --guest-memfd is requested, keeping REGION for the default path. But like you said, do others care for kernels that old?
v6.8 isn't that old, please don't break it ;)
Will
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
Quoted 18 lines · Will Deacon
Quoted 16 lines · Fuad Tabba
Hi Suzuki,
Quoted 12 lines · Suzuki K Poulose
Hi Fuad
Quoted 5 lines · Fuad Tabba
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
Quoted 6 lines
Quoted 4 lines
KVM_SET_USER_MEMORY_REGION2goes back to 6.8, so this would break kernels older than that. I can gate it so that REGION2 is only used when --guest-memfd is requested, keeping REGION for the default path. But like you said, do others care for kernels that old?v6.8 isn't that old, please don't break it ;)
Sure. I'll fix that in V2 :)
Cheers,
/fuad
Quoted 2 lines
Will
Re: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
In reply to Suzuki K PouloseHi Fuad, Suzuki,
Quoted 11 lines · Suzuki K Poulose
Hi Fuad
Quoted 5 lines · Fuad Tabba
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
I haven't had the time to review the series, but I do care about this change, I find myself from time to time testing old kernels.
I think the two ioctls can coexist without much maintenance burden. For example, the usage of KVM_SET_USER_MEMORY_REGION can be kept as-is and KVM_SET_USER_MEMORY_REGION2 can be used only for guest_memfd in kvm__register_mem().
Thoughts?
Thanks,
Alex
Quoted 65 lines
Cheers
SuzukiQuoted 59 lines
This prepares for adding
guest_memfdsupport.Signed-off-by Fuad Tabba <[email protected]>--- kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kvm.c +5
-6@@ -189,7 +189,7 @@ core_exit(kvm__exit);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) { -struct kvm_userspace_memory_region mem; +struct kvm_userspace_memory_region2 mem;struct kvm_mem_bank *bank;int ret;@@ -213,14 +213,14 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region ) { + mem = (struct kvm_userspace_memory_region2 ) { .slot = bank->slot, .guest_phys_addr = guest_phys, .memory_size =0 , .userspace_addr = (unsigned long )userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;@@ -239,7 +239,6 @@ out:int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type) { -struct kvm_userspace_memory_region mem;struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;struct list_head *prev_entry;@@ -320,7 +319,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |=KVM_MEM_READONLY ;if (type !=KVM_MEM_TYPE_RESERVED ) { - mem = (struct kvm_userspace_memory_region ) { +struct kvm_userspace_memory_region2 mem = { .slot = slot, .flags = flags, .guest_phys_addr = guest_phys,@@ -328,7 +327,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 3/5] Use kvm_userspace_memory_region2 for all memory registration
Quoted 19 lines · Alexandru Elisei
Hi Fuad, Suzuki,
Quoted 11 lines · Suzuki K Poulose
Hi Fuad
Quoted 5 lines · Fuad Tabba
Switch
kvm__register_mem()andkvm__destroy_mem()fromkvm_userspace_memory_regiontokvm_userspace_memory_region2andKVM_SET_USER_MEMORY_REGION2unconditionally. The v2 struct is a superset; when the extra fields are zero, the ioctl behaves identically to v1.Doesn't this break using this on older KVM implementations ? Not that I care, but do others care ?
I haven't had the time to review the series, but I do care about this change, I find myself from time to time testing old kernels.
Quoted 6 lines
I think the two ioctls can coexist without much maintenance burden. For example, the usage of
KVM_SET_USER_MEMORY_REGIONcan be kept as-is andKVM_SET_USER_MEMORY_REGION2can be used only forguest_memfdinkvm__register_mem().Thoughts?
Already did that. I'll wait for more reviews before posting V2 with this change.
Cheers,
/fuad
Quoted 69 lines
Thanks,
AlexQuoted 65 lines
Cheers
SuzukiQuoted 59 lines
This prepares for adding
guest_memfdsupport.Signed-off-by Fuad Tabba <[email protected]>--- kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kvm.c +5
-6@@ -189,7 +189,7 @@ core_exit(kvm__exit);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) { -struct kvm_userspace_memory_region mem; +struct kvm_userspace_memory_region2 mem;struct kvm_mem_bank *bank;int ret;@@ -213,14 +213,14 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region ) { + mem = (struct kvm_userspace_memory_region2 ) { .slot = bank->slot, .guest_phys_addr = guest_phys, .memory_size =0 , .userspace_addr = (unsigned long )userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;@@ -239,7 +239,6 @@ out:int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr,enum kvm_mem_type type) { -struct kvm_userspace_memory_region mem;struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;struct list_head *prev_entry;@@ -320,7 +319,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |=KVM_MEM_READONLY ;if (type !=KVM_MEM_TYPE_RESERVED ) { - mem = (struct kvm_userspace_memory_region ) { +struct kvm_userspace_memory_region2 mem = { .slot = slot, .flags = flags, .guest_phys_addr = guest_phys,@@ -328,7 +327,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; - ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno; goto out;
Fuad Tabba
reviewerPATCH 4/5 Add guest_memfd support to kvm__register_mem()
In reply to Fuad TabbaAdd a guest_memfd file descriptor and offset to kvm__register_mem(). When guest_memfd >= 0, set KVM_MEM_GUEST_MEMFD and populate the guest_memfd fields, binding the memslot to the file. This prepares for backing guest RAM with guest_memfd on arm64.
--- hw/cfi_flash.c | 2 +- include/kvm/kvm.h | 15 +++++++++++++-- kvm.c | 9 ++++++++- 3 files changed, 22 insertions(+), 4 deletions(-)
hw/cfi_flash.c +1 -1
@@ -451,7 +451,7 @@ static int map_flash_memory(struct kvm *kvm, struct cfi_flash_device *sfdev)int ret; ret =kvm__register_mem (kvm, sfdev->base_addr, sfdev->size, - sfdev->flash_memory, + sfdev->flash_memory,-1 ,0 ,KVM_MEM_TYPE_RAM |KVM_MEM_TYPE_READONLY );if (!ret) sfdev->is_mapped = true;
include/kvm/kvm.h +13 -2
@@ -128,11 +128,22 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction,bool kvm__emulate_mmio (struct kvm_cpu *vcpu,u64 phys_addr,u8 *data,u32 len,u8 is_write);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr);int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr, +int guest_memfd,u64 guest_memfd_offset,enum kvm_mem_type type);static inline int kvm__register_ram (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) {return kvm__register_mem (kvm, guest_phys, size, userspace_addr, +-1 ,0 ,KVM_MEM_TYPE_RAM ); +} + +static inline int kvm__register_ram_guest_memfd (struct kvm *kvm,u64 guest_phys, +u64 size,void *userspace_addr, +int guest_memfd, +u64 guest_memfd_offset) +{ +return kvm__register_mem (kvm, guest_phys, size, userspace_addr, + guest_memfd, guest_memfd_offset,KVM_MEM_TYPE_RAM ); }
@@ -140,13 +151,13 @@ static inline int kvm__register_dev_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr) { return kvm__register_mem(kvm, guest_phys, size, userspace_addr, -KVM_MEM_TYPE_DEVICE ); + -1, 0, KVM_MEM_TYPE_DEVICE); }static inline int kvm__reserve_mem (struct kvm *kvm,u64 guest_phys,u64 size) {return kvm__register_mem (kvm, guest_phys, size,NULL , -KVM_MEM_TYPE_RESERVED ); +-1 ,0 ,KVM_MEM_TYPE_RESERVED ); } int __must_check kvm__register_iotrap(struct kvm *kvm, u64 phys_addr, u64 len,
kvm.c +8 -1
@@ -237,7 +237,8 @@ out: }int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size, -void *userspace_addr,enum kvm_mem_type type) +void *userspace_addr,int guest_memfd, +u64 guest_memfd_offset,enum kvm_mem_type type) {struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;
@@ -327,6 +328,12 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; +if (guest_memfd >=0 ) { + mem.flags |=KVM_MEM_GUEST_MEMFD ; + mem.guest_memfd = guest_memfd; + mem.guest_memfd_offset = guest_memfd_offset; + } + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno;
Signature · 1 line
2.39.5
Re: [PATCH kvmtool v1 4/5] Add guest_memfd support to kvm__register_mem()
Quoted 6 lines · Fuad Tabba
Add a
guest_memfdfile descriptor and offset tokvm__register_mem(). Whenguest_memfd>= 0, setKVM_MEM_GUEST_MEMFDand populate theguest_memfdfields, binding the memslot to the file. This prepares for backing guest RAM withguest_memfdon arm64.Signed-off-by Fuad Tabba <[email protected]>
Looks good to me
Quoted 79 lines
--- hw/cfi_flash.c | 2 +- include/kvm/kvm.h | 15 +++++++++++++-- kvm.c | 9 ++++++++- 3 files changed, 22 insertions(+), 4 deletions(-)hw/cfi_flash.c +1
-1@@ -451,7 +451,7 @@ static int map_flash_memory(struct kvm *kvm, struct cfi_flash_device *sfdev)int ret; ret =kvm__register_mem (kvm, sfdev->base_addr, sfdev->size, - sfdev->flash_memory, + sfdev->flash_memory,-1 ,0 ,KVM_MEM_TYPE_RAM |KVM_MEM_TYPE_READONLY );if (!ret) sfdev->is_mapped = true;include/kvm/kvm.h +13
-2@@ -128,11 +128,22 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction,bool kvm__emulate_mmio (struct kvm_cpu *vcpu,u64 phys_addr,u8 *data,u32 len,u8 is_write);int kvm__destroy_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr);int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr, +int guest_memfd,u64 guest_memfd_offset,enum kvm_mem_type type);static inline int kvm__register_ram (struct kvm *kvm,u64 guest_phys,u64 size,void *userspace_addr) {return kvm__register_mem (kvm, guest_phys, size, userspace_addr, +-1 ,0 ,KVM_MEM_TYPE_RAM ); +} + +static inline int kvm__register_ram_guest_memfd (struct kvm *kvm,u64 guest_phys, +u64 size,void *userspace_addr, +int guest_memfd, +u64 guest_memfd_offset) +{ +return kvm__register_mem (kvm, guest_phys, size, userspace_addr, + guest_memfd, guest_memfd_offset,KVM_MEM_TYPE_RAM ); }@@ -140,13 +151,13 @@ static inline int kvm__register_dev_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr) { return kvm__register_mem(kvm, guest_phys, size, userspace_addr, -KVM_MEM_TYPE_DEVICE ); + -1, 0, KVM_MEM_TYPE_DEVICE); }static inline int kvm__reserve_mem (struct kvm *kvm,u64 guest_phys,u64 size) {return kvm__register_mem (kvm, guest_phys, size,NULL , -KVM_MEM_TYPE_RESERVED ); +-1 ,0 ,KVM_MEM_TYPE_RESERVED ); } int __must_check kvm__register_iotrap(struct kvm *kvm, u64 phys_addr, u64 len,kvm.c +2
-1@@ -237,7 +237,8 @@ out: }int kvm__register_mem (struct kvm *kvm,u64 guest_phys,u64 size, -void *userspace_addr,enum kvm_mem_type type) +void *userspace_addr,int guest_memfd, +u64 guest_memfd_offset,enum kvm_mem_type type) {struct kvm_mem_bank *merged =NULL ;struct kvm_mem_bank *bank;@@ -327,6 +328,12 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, };
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 339
Quoted 10 lines
kvm.c +6
-0+if (guest_memfd >=0 ) { + mem.flags |=KVM_MEM_GUEST_MEMFD ; + mem.guest_memfd = guest_memfd; + mem.guest_memfd_offset = guest_memfd_offset; + } + ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno;
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Suzuki K Poulose
reviewerRe: [PATCH kvmtool v1 4/5] Add guest_memfd support to kvm__register_mem()
Quoted 11 lines · Suzuki K Poulose
Quoted 6 lines · Fuad Tabba
Add a
guest_memfdfile descriptor and offset tokvm__register_mem(). Whenguest_memfd>= 0, setKVM_MEM_GUEST_MEMFDand populate theguest_memfdfields, binding the memslot to the file. This prepares for backing guest RAM withguest_memfdon arm64.Signed-off-by Fuad Tabba <[email protected]>Looks good to me
Reviewed-by Suzuki K Poulose <[email protected]>
Having another look... Thought of adding my comment, no changes to the above.
Quoted 8 lines
Quoted 8 lines
int kvm__register_mem(struct kvm *kvm, u64
guest_phys, u64 size,kvm.c +2
-1- void *userspace_addr, enum kvm_mem_type type) + void *userspace_addr, int guest_memfd, + u64 guest_memfd_offset, enum kvm_mem_type type) { struct kvm_mem_bank *merged = NULL;struct kvm_mem_bank *bank;@@ -327,6 +328,12 @@ int kvm__register_mem(struct kvm *kvm, u64
Quoted 8 lines
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 335
Quoted 8 lines
kvm.c +5
-0guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; +if (guest_memfd >=0 ) { + mem.flags |=KVM_MEM_GUEST_MEMFD ; + mem.guest_memfd = guest_memfd; + mem.guest_memfd_offset = guest_memfd_offset; + }
It may be worth capturing the fd and the offset in the mem_bank here, but agree that can be added on the need basis and there are no users of it for now, without conversion support.
Suzuki
Quoted 5 lines
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 339
Quoted 4 lines
kvm.c +1
-0+ ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno;
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 4/5] Add guest_memfd support to kvm__register_mem()
Quoted 10 lines · Suzuki K Poulose
Quoted 6 lines · Suzuki K Poulose
Quoted 5 lines · Fuad Tabba
Add a
guest_memfdfile descriptor and offset tokvm__register_mem(). Whenguest_memfd>= 0, setKVM_MEM_GUEST_MEMFDand populate theguest_memfdfields, binding the memslot to the file. This prepares for backing guest RAM withguest_memfdon arm64.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Fuad Tabba <[email protected]>Looks good to me
Reviewed-by Suzuki K Poulose <[email protected]>Having another look... Thought of adding my comment, no changes to the above.
Thanks!
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
int kvm__register_mem(struct kvm *kvm, u64
guest_phys, u64 size,kvm.c +3
-1- void *userspace_addr, enum kvm_mem_type type) + void *userspace_addr, int guest_memfd, + u64 guest_memfd_offset, enum kvm_mem_type type) { struct kvm_mem_bank *merged = NULL;struct kvm_mem_bank *bank;@@ -327,6 +328,12 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, .userspace_addr = (unsigned long)userspace_addr, }; +if (guest_memfd >=0 ) {
Quoted 8 lines
Quoted 4 lines
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 335
Quoted 4 lines
kvm.c +4
-0+ mem.flags |=KVM_MEM_GUEST_MEMFD ; + mem.guest_memfd = guest_memfd; + mem.guest_memfd_offset = guest_memfd_offset; + }It may be worth capturing the fd and the offset in the
mem_bankhere, but agree that can be added on the need basis and there are no users of it for now, without conversion support.
Didn't want to over-engineer this. We'll add it when we need it, which is soon I hope :)
Cheers,
/fuad
Quoted 11 lines
Suzuki
Quoted 5 lines
kvm.c int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, · line 339
Quoted 4 lines
kvm.c +1
-0+ ret =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION2 , &mem);if (ret <0 ) { ret = -errno;IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Suzuki K Poulose
reviewerRe: [PATCH kvmtool v1 4/5] Add guest_memfd support to kvm__register_mem()
Quoted 10 lines · Fuad Tabba
Quoted 9 lines · Suzuki K Poulose
Quoted 7 lines
Quoted 6 lines
+
ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION2, &mem); if (ret < 0) { ret = -errno;IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Sorry about this^, hopefully I have fixed this now :-(
Suzuki
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerPATCH 5/5 arm64: Add --guest-memfd option to back guest RAM with guest_memfd
In reply to Fuad TabbaBack guest RAM with a guest_memfd created with GUEST_MEMFD_FLAG_MMAP and GUEST_MEMFD_FLAG_INIT_SHARED, mmapped MAP_SHARED to provide the userspace mapping.
Protected VMs need in-place shared/private conversion of guest_memfd, which the kernel does not support yet, so reject the combination of --protected and --guest-memfd.
--- arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++++++------ include/kvm/kvm.h | 1 + include/kvm/util.h | 1 + kvm.c | 1 + util/util.c | 34 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-)
arm64/include/kvm/kvm-config-arch.h +4 -1
@@ -20,6 +20,7 @@ struct kvm_config_arch {bool no_pvtime;bool psci;bool protected; +bool guest_memfd; };int irqchip_parser (const struct option *opt,const char *arg,int unset);
@@ -73,6 +74,8 @@ int sve_vl_parser(const struct option *opt, const char *arg, int unset);OPT_BOOLEAN ('\0' ,"e2h0" , &(cfg)->e2h0, \"Create guest without VHE support" ), \OPT_BOOLEAN ('\0' ,"protected" , &(cfg)->protected, \ - "Create a protected VM when pKVM is enabled"), +"Create a protected VM when pKVM is enabled" ), \ +OPT_BOOLEAN ('\0' ,"guest-memfd" , &(cfg)->guest_memfd, \ + "Use guest_memfd to back guest RAM"), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */
arm64/kvm.c +30 -8
@@ -49,9 +49,14 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.ram_alloc_size = kvm->ram_size;if (!kvm->cfg.hugetlbfs_path) kvm->arch.ram_alloc_size +=SZ_2M ; - kvm->arch.ram_alloc_start =mmap_anon_or_hugetlbfs (kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + +if (kvm->cfg.arch.guest_memfd) + kvm->arch.ram_alloc_start = +mmap_guest_memfd (kvm, kvm->arch.ram_alloc_size); +else + kvm->arch.ram_alloc_start = +mmap_anon_or_hugetlbfs (kvm, kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size);if (kvm->arch.ram_alloc_start ==MAP_FAILED ) die("Failed to map %lld bytes for guest memory (%d)",
@@ -60,17 +65,25 @@ void kvm__init_ram(struct kvm *kvm) kvm->ram_start = (void *)ALIGN ((unsigned long )kvm->arch.ram_alloc_start,SZ_2M ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_MERGEABLE ); +if (!kvm->cfg.arch.guest_memfd) { +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_MERGEABLE ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_HUGEPAGE ); +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_HUGEPAGE ); + } phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err =kvm__register_ram (kvm, phys_start, phys_size, host_mem); +if (kvm->cfg.arch.guest_memfd) + err =kvm__register_ram_guest_memfd (kvm, phys_start, + phys_size, host_mem, + kvm->ram_guest_memfd, + host_mem - kvm->arch.ram_alloc_start); +else + err =kvm__register_ram (kvm, phys_start, phys_size, host_mem);if (err)die ("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]" , phys_size, phys_start, err);
@@ -468,6 +481,15 @@ void kvm__arch_validate_cfg(struct kvm *kvm)if (kvm->cfg.arch.e2h0 && !kvm->cfg.arch.nested_virt)pr_warning ("--e2h0 requires --nested, ignoring" ); +if (kvm->cfg.arch.guest_memfd) { +/* Requires in-place shared/private conversion of guest_memfd. */ +if (kvm->cfg.arch.protected) +die ("Protected VMs do not yet support guest_memfd" ); + +if (kvm->cfg.hugetlbfs_path) +die ("guest_memfd cannot be backed by hugetlbfs" ); + } +if (kvm->cfg.arch.protected) {if (kvm->cfg.ram_size && kvm->cfg.ram_size <DMA_MEM_REGION_SIZE ) {
include/kvm/kvm.h +1 -0
@@ -90,6 +90,7 @@ struct kvm {u64 ram_size;/* Guest memory size, in bytes */ void *ram_start;u64 ram_pagesize; +int ram_guest_memfd;/* guest_memfd backing RAM, -1 if none */ struct mutex mem_banks_lock;struct list_head mem_banks;
include/kvm/util.h +1 -0
@@ -148,5 +148,6 @@ static inline int pow2_size(unsigned long x)struct kvm ;void *mmap_hugetlbfs (struct kvm *kvm,const char *htlbfs_path,u64 size);void *mmap_anon_or_hugetlbfs (struct kvm *kvm,const char *hugetlbfs_path,u64 size); +void *mmap_guest_memfd (struct kvm *kvm,u64 size);#endif /* KVM__UTIL_H */
kvm.c +1 -0
@@ -162,6 +162,7 @@ struct kvm *kvm__new(void)mutex_init (&kvm->mem_banks_lock); kvm->sys_fd =-1 ; kvm->vm_fd =-1 ; + kvm->ram_guest_memfd =-1 ;#ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t )PTHREAD_RWLOCK_INITIALIZER ;
util/util.c +34 -0
@@ -6,7 +6,9 @@#include "kvm/util.h" #include <kvm/kvm.h> +#include <linux/kvm.h> #include <linux/magic.h> /* For HUGETLBFS_MAGIC */ +#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/statfs.h>
@@ -169,3 +171,35 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 sireturn mmap (NULL , size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); } } + +/* +* Map guest RAM backed by a guest_memfd. The file descriptor must stay open +* for the lifetime of the VM: closing it invalidates the memslot bindings. +*/ +void *mmap_guest_memfd (struct kvm *kvm,u64 size) +{ +u64 flags =GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + }; +void *addr; +int ret; + + ret =ioctl (kvm->vm_fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 || ((u64 )ret & flags) != flags) +die ("kernel does not support guest_memfd with mmap" ); + + ret =ioctl (kvm->vm_fd,KVM_CREATE_GUEST_MEMFD , &gmem); +if (ret <0 ) +die_perror ("KVM_CREATE_GUEST_MEMFD" ); + + addr =mmap (NULL , size,PROT_RW ,MAP_SHARED , ret,0 ); +if (addr ==MAP_FAILED ) +die_perror ("guest_memfd mmap" ); + + kvm->ram_guest_memfd = ret; + kvm->ram_pagesize =getpagesize (); + +return addr; +}
Signature · 1 line
2.39.5
Suzuki K Poulose
reviewerRe: [PATCH kvmtool v1 5/5] arm64: Add --guest-memfd option to back guest RAM with guest_memfd
Quoted 170 lines · Fuad Tabba
Back guest RAM with a
guest_memfdcreated withGUEST_MEMFD_FLAG_MMAPandGUEST_MEMFD_FLAG_INIT_SHARED, mmappedMAP_SHAREDto provide the userspace mapping.Protected VMs need in-place shared/private conversion of
guest_memfd, which the kernel does not support yet, so reject the combination of --protected and --guest-memfd.Signed-off-by Fuad Tabba <[email protected]>--- arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++++++------ include/kvm/kvm.h | 1 + include/kvm/util.h | 1 + kvm.c | 1 + util/util.c | 34 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-)arm64/include/kvm/kvm-config-arch.h +4
-1@@ -20,6 +20,7 @@ struct kvm_config_arch {bool no_pvtime;bool psci;bool protected; +bool guest_memfd; };int irqchip_parser (const struct option *opt,const char *arg,int unset);@@ -73,6 +74,8 @@ int sve_vl_parser(const struct option *opt, const char *arg, int unset);OPT_BOOLEAN ('\0' ,"e2h0" , &(cfg)->e2h0, \"Create guest without VHE support" ), \OPT_BOOLEAN ('\0' ,"protected" , &(cfg)->protected, \ - "Create a protected VM when pKVM is enabled"), +"Create a protected VM when pKVM is enabled" ), \ +OPT_BOOLEAN ('\0' ,"guest-memfd" , &(cfg)->guest_memfd, \ + "Use guest_memfd to back guest RAM"), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */arm64/kvm.c +30
-8@@ -49,9 +49,14 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.ram_alloc_size = kvm->ram_size;if (!kvm->cfg.hugetlbfs_path) kvm->arch.ram_alloc_size +=SZ_2M ; - kvm->arch.ram_alloc_start =mmap_anon_or_hugetlbfs (kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + +if (kvm->cfg.arch.guest_memfd) + kvm->arch.ram_alloc_start = +mmap_guest_memfd (kvm, kvm->arch.ram_alloc_size); +else + kvm->arch.ram_alloc_start = +mmap_anon_or_hugetlbfs (kvm, kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size);if (kvm->arch.ram_alloc_start ==MAP_FAILED ) die("Failed to map %lld bytes for guest memory (%d)",@@ -60,17 +65,25 @@ void kvm__init_ram(struct kvm *kvm) kvm->ram_start = (void *)ALIGN ((unsigned long )kvm->arch.ram_alloc_start,SZ_2M ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_MERGEABLE ); +if (!kvm->cfg.arch.guest_memfd) { +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_MERGEABLE ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_HUGEPAGE ); +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_HUGEPAGE ); + } phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err =kvm__register_ram (kvm, phys_start, phys_size, host_mem); +if (kvm->cfg.arch.guest_memfd) + err =kvm__register_ram_guest_memfd (kvm, phys_start, + phys_size, host_mem, + kvm->ram_guest_memfd, + host_mem - kvm->arch.ram_alloc_start); +else + err =kvm__register_ram (kvm, phys_start, phys_size, host_mem);if (err)die ("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]" , phys_size, phys_start, err);@@ -468,6 +481,15 @@ void kvm__arch_validate_cfg(struct kvm *kvm)if (kvm->cfg.arch.e2h0 && !kvm->cfg.arch.nested_virt)pr_warning ("--e2h0 requires --nested, ignoring" ); +if (kvm->cfg.arch.guest_memfd) { +/* Requires in-place shared/private conversion of guest_memfd. */ +if (kvm->cfg.arch.protected) +die ("Protected VMs do not yet support guest_memfd" ); + +if (kvm->cfg.hugetlbfs_path) +die ("guest_memfd cannot be backed by hugetlbfs" ); + } +if (kvm->cfg.arch.protected) {if (kvm->cfg.ram_size && kvm->cfg.ram_size <DMA_MEM_REGION_SIZE ) {include/kvm/kvm.h +1
-0@@ -90,6 +90,7 @@ struct kvm {u64 ram_size;/* Guest memory size, in bytes */ void *ram_start;u64 ram_pagesize; +int ram_guest_memfd;/* guest_memfd backing RAM, -1 if none */ struct mutex mem_banks_lock;struct list_head mem_banks;include/kvm/util.h +1
-0@@ -148,5 +148,6 @@ static inline int pow2_size(unsigned long x)struct kvm ;void *mmap_hugetlbfs (struct kvm *kvm,const char *htlbfs_path,u64 size);void *mmap_anon_or_hugetlbfs (struct kvm *kvm,const char *hugetlbfs_path,u64 size); +void *mmap_guest_memfd (struct kvm *kvm,u64 size);#endif /* KVM__UTIL_H */ kvm.c +1
-0@@ -162,6 +162,7 @@ struct kvm *kvm__new(void)mutex_init (&kvm->mem_banks_lock); kvm->sys_fd =-1 ; kvm->vm_fd =-1 ; + kvm->ram_guest_memfd =-1 ;#ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t )PTHREAD_RWLOCK_INITIALIZER ;util/util.c +10
-0@@ -6,7 +6,9 @@#include "kvm/util.h" #include <kvm/kvm.h> +#include <linux/kvm.h> #include <linux/magic.h> /* For HUGETLBFS_MAGIC */ +#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/statfs.h> @@ -169,3 +171,35 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 sireturn mmap (NULL , size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); } } + +/* +* Map guest RAM backed by a guest_memfd. The file descriptor must stay open +* for the lifetime of the VM: closing it invalidates the memslot bindings. +*/ +void *mmap_guest_memfd (struct kvm *kvm,u64 size) +{ +u64 flags =GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ;
util/util.c void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si · line 191
Quoted 10 lines
util/util.c +10
-0+struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + }; +void *addr; +int ret; + + ret =ioctl (kvm->vm_fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 || ((u64 )ret & flags) != flags) +die ("kernel does not support guest_memfd with mmap" );
Minor nit: "VM type doesn't support..", to be precise ?
Suzuki
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 5/5] arm64: Add --guest-memfd option to back guest RAM with guest_memfd
Quoted 176 lines · Suzuki K Poulose
Quoted 174 lines · Fuad Tabba
Back guest RAM with a
guest_memfdcreated withGUEST_MEMFD_FLAG_MMAPandGUEST_MEMFD_FLAG_INIT_SHARED, mmappedMAP_SHAREDto provide the userspace mapping.Protected VMs need in-place shared/private conversion of
guest_memfd, which the kernel does not support yet, so reject the combination of --protected and --guest-memfd.Signed-off-by Fuad Tabba <[email protected]>--- arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++++++------ include/kvm/kvm.h | 1 + include/kvm/util.h | 1 + kvm.c | 1 + util/util.c | 34 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-)arm64/include/kvm/kvm-config-arch.h +4
-1@@ -20,6 +20,7 @@ struct kvm_config_arch {bool no_pvtime;bool psci;bool protected; +bool guest_memfd; };int irqchip_parser (const struct option *opt,const char *arg,int unset);@@ -73,6 +74,8 @@ int sve_vl_parser(const struct option *opt, const char *arg, int unset);OPT_BOOLEAN ('\0' ,"e2h0" , &(cfg)->e2h0, \"Create guest without VHE support" ), \OPT_BOOLEAN ('\0' ,"protected" , &(cfg)->protected, \ - "Create a protected VM when pKVM is enabled"), +"Create a protected VM when pKVM is enabled" ), \ +OPT_BOOLEAN ('\0' ,"guest-memfd" , &(cfg)->guest_memfd, \ + "Use guest_memfd to back guest RAM"), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */arm64/kvm.c +30
-8@@ -49,9 +49,14 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.ram_alloc_size = kvm->ram_size;if (!kvm->cfg.hugetlbfs_path) kvm->arch.ram_alloc_size +=SZ_2M ; - kvm->arch.ram_alloc_start =mmap_anon_or_hugetlbfs (kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + +if (kvm->cfg.arch.guest_memfd) + kvm->arch.ram_alloc_start = +mmap_guest_memfd (kvm, kvm->arch.ram_alloc_size); +else + kvm->arch.ram_alloc_start = +mmap_anon_or_hugetlbfs (kvm, kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size);if (kvm->arch.ram_alloc_start ==MAP_FAILED ) die("Failed to map %lld bytes for guest memory (%d)",@@ -60,17 +65,25 @@ void kvm__init_ram(struct kvm *kvm) kvm->ram_start = (void *)ALIGN ((unsigned long )kvm->arch.ram_alloc_start,SZ_2M ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_MERGEABLE ); +if (!kvm->cfg.arch.guest_memfd) { +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_MERGEABLE ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_HUGEPAGE ); +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_HUGEPAGE ); + } phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err =kvm__register_ram (kvm, phys_start, phys_size, host_mem); +if (kvm->cfg.arch.guest_memfd) + err =kvm__register_ram_guest_memfd (kvm, phys_start, + phys_size, host_mem, + kvm->ram_guest_memfd, + host_mem - kvm->arch.ram_alloc_start); +else + err =kvm__register_ram (kvm, phys_start, phys_size, host_mem);if (err)die ("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]" , phys_size, phys_start, err);@@ -468,6 +481,15 @@ void kvm__arch_validate_cfg(struct kvm *kvm)if (kvm->cfg.arch.e2h0 && !kvm->cfg.arch.nested_virt)pr_warning ("--e2h0 requires --nested, ignoring" ); +if (kvm->cfg.arch.guest_memfd) { +/* Requires in-place shared/private conversion of guest_memfd. */ +if (kvm->cfg.arch.protected) +die ("Protected VMs do not yet support guest_memfd" ); + +if (kvm->cfg.hugetlbfs_path) +die ("guest_memfd cannot be backed by hugetlbfs" ); + } +if (kvm->cfg.arch.protected) {if (kvm->cfg.ram_size && kvm->cfg.ram_size <DMA_MEM_REGION_SIZE ) {include/kvm/kvm.h +1
-0@@ -90,6 +90,7 @@ struct kvm {u64 ram_size;/* Guest memory size, in bytes */ void *ram_start;u64 ram_pagesize; +int ram_guest_memfd;/* guest_memfd backing RAM, -1 if none */ struct mutex mem_banks_lock;struct list_head mem_banks;include/kvm/util.h +1
-0@@ -148,5 +148,6 @@ static inline int pow2_size(unsigned long x)struct kvm ;void *mmap_hugetlbfs (struct kvm *kvm,const char *htlbfs_path,u64 size);void *mmap_anon_or_hugetlbfs (struct kvm *kvm,const char *hugetlbfs_path,u64 size); +void *mmap_guest_memfd (struct kvm *kvm,u64 size);#endif /* KVM__UTIL_H */ kvm.c +1
-0@@ -162,6 +162,7 @@ struct kvm *kvm__new(void)mutex_init (&kvm->mem_banks_lock); kvm->sys_fd =-1 ; kvm->vm_fd =-1 ; + kvm->ram_guest_memfd =-1 ;#ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t )PTHREAD_RWLOCK_INITIALIZER ;util/util.c +14
-0@@ -6,7 +6,9 @@#include "kvm/util.h" #include <kvm/kvm.h> +#include <linux/kvm.h> #include <linux/magic.h> /* For HUGETLBFS_MAGIC */ +#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/statfs.h> @@ -169,3 +171,35 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 sireturn mmap (NULL , size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); } } + +/* +* Map guest RAM backed by a guest_memfd. The file descriptor must stay open +* for the lifetime of the VM: closing it invalidates the memslot bindings. +*/ +void *mmap_guest_memfd (struct kvm *kvm,u64 size) +{ +u64 flags =GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + };
Quoted 8 lines
util/util.c void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si · line 191
Quoted 6 lines
util/util.c +6
-0+void *addr; +int ret; + + ret =ioctl (kvm->vm_fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 || ((u64 )ret & flags) != flags) +die ("kernel does not support guest_memfd with mmap" );Minor nit: "VM type doesn't support..", to be precise ?
Done,
/fuad
Quoted 3 lines
Suzuki
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [PATCH kvmtool v1 5/5] arm64: Add --guest-memfd option to back guest RAM with guest_memfd
In reply to Fuad TabbaHi Fuad,
Looks good me, two small things: it might be useful for debugging to first check that KVM supports KVM_CAP_GUEST_MEMFD before checking KVM_CAP_GUEST_MEMFD_FLAGS, and the file descriptor could be closed after it is unmapped.
Thanks,
Alex
Quoted 197 lines · Fuad Tabba
Back guest RAM with a
guest_memfdcreated withGUEST_MEMFD_FLAG_MMAPandGUEST_MEMFD_FLAG_INIT_SHARED, mmappedMAP_SHAREDto provide the userspace mapping.Protected VMs need in-place shared/private conversion of
guest_memfd, which the kernel does not support yet, so reject the combination of --protected and --guest-memfd.Signed-off-by Fuad Tabba <[email protected]>--- arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++++++------ include/kvm/kvm.h | 1 + include/kvm/util.h | 1 + kvm.c | 1 + util/util.c | 34 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-)arm64/include/kvm/kvm-config-arch.h +4
-1@@ -20,6 +20,7 @@ struct kvm_config_arch {bool no_pvtime;bool psci;bool protected; +bool guest_memfd; };int irqchip_parser (const struct option *opt,const char *arg,int unset);@@ -73,6 +74,8 @@ int sve_vl_parser(const struct option *opt, const char *arg, int unset);OPT_BOOLEAN ('\0' ,"e2h0" , &(cfg)->e2h0, \"Create guest without VHE support" ), \OPT_BOOLEAN ('\0' ,"protected" , &(cfg)->protected, \ - "Create a protected VM when pKVM is enabled"), +"Create a protected VM when pKVM is enabled" ), \ +OPT_BOOLEAN ('\0' ,"guest-memfd" , &(cfg)->guest_memfd, \ + "Use guest_memfd to back guest RAM"), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */arm64/kvm.c +30
-8@@ -49,9 +49,14 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.ram_alloc_size = kvm->ram_size;if (!kvm->cfg.hugetlbfs_path) kvm->arch.ram_alloc_size +=SZ_2M ; - kvm->arch.ram_alloc_start =mmap_anon_or_hugetlbfs (kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + +if (kvm->cfg.arch.guest_memfd) + kvm->arch.ram_alloc_start = +mmap_guest_memfd (kvm, kvm->arch.ram_alloc_size); +else + kvm->arch.ram_alloc_start = +mmap_anon_or_hugetlbfs (kvm, kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size);if (kvm->arch.ram_alloc_start ==MAP_FAILED ) die("Failed to map %lld bytes for guest memory (%d)",@@ -60,17 +65,25 @@ void kvm__init_ram(struct kvm *kvm) kvm->ram_start = (void *)ALIGN ((unsigned long )kvm->arch.ram_alloc_start,SZ_2M ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_MERGEABLE ); +if (!kvm->cfg.arch.guest_memfd) { +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_MERGEABLE ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_HUGEPAGE ); +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_HUGEPAGE ); + } phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err =kvm__register_ram (kvm, phys_start, phys_size, host_mem); +if (kvm->cfg.arch.guest_memfd) + err =kvm__register_ram_guest_memfd (kvm, phys_start, + phys_size, host_mem, + kvm->ram_guest_memfd, + host_mem - kvm->arch.ram_alloc_start); +else + err =kvm__register_ram (kvm, phys_start, phys_size, host_mem);if (err)die ("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]" , phys_size, phys_start, err);@@ -468,6 +481,15 @@ void kvm__arch_validate_cfg(struct kvm *kvm)if (kvm->cfg.arch.e2h0 && !kvm->cfg.arch.nested_virt)pr_warning ("--e2h0 requires --nested, ignoring" ); +if (kvm->cfg.arch.guest_memfd) { +/* Requires in-place shared/private conversion of guest_memfd. */ +if (kvm->cfg.arch.protected) +die ("Protected VMs do not yet support guest_memfd" ); + +if (kvm->cfg.hugetlbfs_path) +die ("guest_memfd cannot be backed by hugetlbfs" ); + } +if (kvm->cfg.arch.protected) {if (kvm->cfg.ram_size && kvm->cfg.ram_size <DMA_MEM_REGION_SIZE ) {include/kvm/kvm.h +1
-0@@ -90,6 +90,7 @@ struct kvm {u64 ram_size;/* Guest memory size, in bytes */ void *ram_start;u64 ram_pagesize; +int ram_guest_memfd;/* guest_memfd backing RAM, -1 if none */ struct mutex mem_banks_lock;struct list_head mem_banks;include/kvm/util.h +1
-0@@ -148,5 +148,6 @@ static inline int pow2_size(unsigned long x)struct kvm ;void *mmap_hugetlbfs (struct kvm *kvm,const char *htlbfs_path,u64 size);void *mmap_anon_or_hugetlbfs (struct kvm *kvm,const char *hugetlbfs_path,u64 size); +void *mmap_guest_memfd (struct kvm *kvm,u64 size);#endif /* KVM__UTIL_H */ kvm.c +1
-0@@ -162,6 +162,7 @@ struct kvm *kvm__new(void)mutex_init (&kvm->mem_banks_lock); kvm->sys_fd =-1 ; kvm->vm_fd =-1 ; + kvm->ram_guest_memfd =-1 ;#ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t )PTHREAD_RWLOCK_INITIALIZER ;util/util.c +34
-0@@ -6,7 +6,9 @@#include "kvm/util.h" #include <kvm/kvm.h> +#include <linux/kvm.h> #include <linux/magic.h> /* For HUGETLBFS_MAGIC */ +#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/statfs.h> @@ -169,3 +171,35 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 sireturn mmap (NULL , size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); } } + +/* +* Map guest RAM backed by a guest_memfd. The file descriptor must stay open +* for the lifetime of the VM: closing it invalidates the memslot bindings. +*/ +void *mmap_guest_memfd (struct kvm *kvm,u64 size) +{ +u64 flags =GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + }; +void *addr; +int ret; + + ret =ioctl (kvm->vm_fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 || ((u64 )ret & flags) != flags) +die ("kernel does not support guest_memfd with mmap" ); + + ret =ioctl (kvm->vm_fd,KVM_CREATE_GUEST_MEMFD , &gmem); +if (ret <0 ) +die_perror ("KVM_CREATE_GUEST_MEMFD" ); + + addr =mmap (NULL , size,PROT_RW ,MAP_SHARED , ret,0 ); +if (addr ==MAP_FAILED ) +die_perror ("guest_memfd mmap" ); + + kvm->ram_guest_memfd = ret; + kvm->ram_pagesize =getpagesize (); + +return addr; +}Signature · 1 line
2.39.5
Fuad Tabba
reviewerRe: [PATCH kvmtool v1 5/5] arm64: Add --guest-memfd option to back guest RAM with guest_memfd
Quoted 7 lines · Alexandru Elisei
Hi Fuad,
Looks good me, two small things: it might be useful for debugging to first check that KVM supports
KVM_CAP_GUEST_MEMFDbefore checkingKVM_CAP_GUEST_MEMFD_FLAGS, and the file descriptor could be closed after it is unmapped.
Good points, will fix both in v2.
Thanks,
/fuad
Quoted 202 lines
Thanks,
AlexQuoted 197 lines · Fuad Tabba
Back guest RAM with a
guest_memfdcreated withGUEST_MEMFD_FLAG_MMAPandGUEST_MEMFD_FLAG_INIT_SHARED, mmappedMAP_SHAREDto provide the userspace mapping.Protected VMs need in-place shared/private conversion of
guest_memfd, which the kernel does not support yet, so reject the combination of --protected and --guest-memfd.Signed-off-by Fuad Tabba <[email protected]>--- arm64/include/kvm/kvm-config-arch.h | 5 +++- arm64/kvm.c | 38 +++++++++++++++++++++++------ include/kvm/kvm.h | 1 + include/kvm/util.h | 1 + kvm.c | 1 + util/util.c | 34 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-)arm64/include/kvm/kvm-config-arch.h +4
-1@@ -20,6 +20,7 @@ struct kvm_config_arch {bool no_pvtime;bool psci;bool protected; +bool guest_memfd; };int irqchip_parser (const struct option *opt,const char *arg,int unset);@@ -73,6 +74,8 @@ int sve_vl_parser(const struct option *opt, const char *arg, int unset);OPT_BOOLEAN ('\0' ,"e2h0" , &(cfg)->e2h0, \"Create guest without VHE support" ), \OPT_BOOLEAN ('\0' ,"protected" , &(cfg)->protected, \ - "Create a protected VM when pKVM is enabled"), +"Create a protected VM when pKVM is enabled" ), \ +OPT_BOOLEAN ('\0' ,"guest-memfd" , &(cfg)->guest_memfd, \ + "Use guest_memfd to back guest RAM"), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */arm64/kvm.c +30
-8@@ -49,9 +49,14 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.ram_alloc_size = kvm->ram_size;if (!kvm->cfg.hugetlbfs_path) kvm->arch.ram_alloc_size +=SZ_2M ; - kvm->arch.ram_alloc_start =mmap_anon_or_hugetlbfs (kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + +if (kvm->cfg.arch.guest_memfd) + kvm->arch.ram_alloc_start = +mmap_guest_memfd (kvm, kvm->arch.ram_alloc_size); +else + kvm->arch.ram_alloc_start = +mmap_anon_or_hugetlbfs (kvm, kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size);if (kvm->arch.ram_alloc_start ==MAP_FAILED ) die("Failed to map %lld bytes for guest memory (%d)",@@ -60,17 +65,25 @@ void kvm__init_ram(struct kvm *kvm) kvm->ram_start = (void *)ALIGN ((unsigned long )kvm->arch.ram_alloc_start,SZ_2M ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_MERGEABLE ); +if (!kvm->cfg.arch.guest_memfd) { +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_MERGEABLE ); -madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, -MADV_HUGEPAGE ); +madvise (kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, +MADV_HUGEPAGE ); + } phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err =kvm__register_ram (kvm, phys_start, phys_size, host_mem); +if (kvm->cfg.arch.guest_memfd) + err =kvm__register_ram_guest_memfd (kvm, phys_start, + phys_size, host_mem, + kvm->ram_guest_memfd, + host_mem - kvm->arch.ram_alloc_start); +else + err =kvm__register_ram (kvm, phys_start, phys_size, host_mem);if (err)die ("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]" , phys_size, phys_start, err);@@ -468,6 +481,15 @@ void kvm__arch_validate_cfg(struct kvm *kvm)if (kvm->cfg.arch.e2h0 && !kvm->cfg.arch.nested_virt)pr_warning ("--e2h0 requires --nested, ignoring" ); +if (kvm->cfg.arch.guest_memfd) { +/* Requires in-place shared/private conversion of guest_memfd. */ +if (kvm->cfg.arch.protected) +die ("Protected VMs do not yet support guest_memfd" ); + +if (kvm->cfg.hugetlbfs_path) +die ("guest_memfd cannot be backed by hugetlbfs" ); + } +if (kvm->cfg.arch.protected) {if (kvm->cfg.ram_size && kvm->cfg.ram_size <DMA_MEM_REGION_SIZE ) {include/kvm/kvm.h +1
-0@@ -90,6 +90,7 @@ struct kvm {u64 ram_size;/* Guest memory size, in bytes */ void *ram_start;u64 ram_pagesize; +int ram_guest_memfd;/* guest_memfd backing RAM, -1 if none */ struct mutex mem_banks_lock;struct list_head mem_banks;include/kvm/util.h +1
-0@@ -148,5 +148,6 @@ static inline int pow2_size(unsigned long x)struct kvm ;void *mmap_hugetlbfs (struct kvm *kvm,const char *htlbfs_path,u64 size);void *mmap_anon_or_hugetlbfs (struct kvm *kvm,const char *hugetlbfs_path,u64 size); +void *mmap_guest_memfd (struct kvm *kvm,u64 size);#endif /* KVM__UTIL_H */ kvm.c +1
-0@@ -162,6 +162,7 @@ struct kvm *kvm__new(void)mutex_init (&kvm->mem_banks_lock); kvm->sys_fd =-1 ; kvm->vm_fd =-1 ; + kvm->ram_guest_memfd =-1 ;#ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t )PTHREAD_RWLOCK_INITIALIZER ;util/util.c +34
-1@@ -6,7 +6,9 @@#include "kvm/util.h" #include <kvm/kvm.h> +#include <linux/kvm.h> #include <linux/magic.h> /* For HUGETLBFS_MAGIC */ +#include <sys/ioctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/statfs.h> @@ -169,3 +171,35 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 sireturn mmap (NULL , size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); } } + +/* +* Map guest RAM backed by a guest_memfd. The file descriptor must stay open +* for the lifetime of the VM: closing it invalidates the memslot bindings. +*/ +void *mmap_guest_memfd (struct kvm *kvm,u64 size) +{ +u64 flags =GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + }; +void *addr; +int ret; + + ret =ioctl (kvm->vm_fd,KVM_CHECK_EXTENSION ,KVM_CAP_GUEST_MEMFD_FLAGS ); +if (ret <0 || ((u64 )ret & flags) != flags) +die ("kernel does not support guest_memfd with mmap" ); + + ret =ioctl (kvm->vm_fd,KVM_CREATE_GUEST_MEMFD , &gmem); +if (ret <0 ) +die_perror ("KVM_CREATE_GUEST_MEMFD" ); + + addr =mmap (NULL , size,PROT_RW ,MAP_SHARED , ret,0 ); +if (addr ==MAP_FAILED ) +die_perror ("guest_memfd mmap" ); + + kvm->ram_guest_memfd = ret; + kvm->ram_pagesize =getpagesize (); + +return addr; +} --2.39.5