KVM: guest_memfd() and per-page attributes
Subsystems (19)
Sean Christopherson
maintainerPATCH v13 29/35 KVM: selftests: Introduce VM "shape" to allow tests to specify the VM type
In reply to Sean ChristophersonAdd a "vm_shape" structure to encapsulate the selftests-defined "mode", along with the KVM-defined "type" for use when creating a new VM. "mode" tracks physical and virtual address properties, as well as the preferred backing memory type, while "type" corresponds to the VM type.
Taking the VM type will allow adding tests for KVM_CREATE_GUEST_MEMFD, a.k.a. guest private memory, without needing an entirely separate set of helpers. Guest private memory is effectively usable only by confidential VM types, and it's expected that x86 will double down and require unique VM types for TDX and SNP guests.
--- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../selftests/kvm/include/kvm_util_base.h | 54 +++++++++++++++---- .../selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 43 +++++++-------- tools/testing/selftests/kvm/lib/memstress.c | 3 +- .../kvm/x86_64/ucna_injection_test.c | 2 +- 6 files changed, 72 insertions(+), 34 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -699,7 +699,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, struct kvm_vcpu **vcpu,pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); - vm =__vm_create (mode,1 , extra_mem_pages); + vm =__vm_create (VM_SHAPE (mode),1 , extra_mem_pages);log_mode_create_vm_done (vm); *vcpu =vm_vcpu_add (vm,0 , guest_code);
tools/testing/selftests/kvm/include/kvm_util_base.h +45 -9
101 lines not on this page · raw message
tools/testing/selftests/kvm/kvm_page_table_test.c +1 -1
@@ -254,7 +254,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)/* Create a VM with enough guest pages */ guest_num_pages = test_mem_size / guest_page_size; - vm =__vm_create_with_vcpus (mode, nr_vcpus, guest_num_pages, + vm =__vm_create_with_vcpus (VM_SHAPE (mode), nr_vcpus, guest_num_pages, guest_code, test_args.vcpus);/* Align down GPA of the testing memslot */
tools/testing/selftests/kvm/lib/kvm_util.c +22 -21
107 lines not on this page · raw message
tools/testing/selftests/kvm/lib/memstress.c +2 -1
@@ -168,7 +168,8 @@ struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,* The memory is also added to memslot 0, but that's a benign side * effect as KVM allows aliasing HVAs in meslots. */ - vm =__vm_create_with_vcpus (mode, nr_vcpus, slot0_pages + guest_num_pages, + vm =__vm_create_with_vcpus (VM_SHAPE (mode), nr_vcpus, + slot0_pages + guest_num_pages, memstress_guest_code, vcpus); args->vm = vm;
tools/testing/selftests/kvm/x86_64/ucna_injection_test.c +1 -1
@@ -271,7 +271,7 @@ int main(int argc, char *argv[])kvm_check_cap (KVM_CAP_MCE ); - vm =__vm_create (VM_MODE_DEFAULT ,3 ,0 ); + vm =__vm_create (VM_SHAPE_DEFAULT ,3 ,0 );kvm_ioctl (vm->kvm_fd,KVM_X86_GET_MCE_CAP_SUPPORTED , &supported_mcg_caps);
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 30/35 KVM: selftests: Add GUEST_SYNC[1-6] macros for synchronizing more data
In reply to Sean ChristophersonAdd GUEST_SYNC[1-6]() so that tests can pass the maximum amount of information supported via ucall(), without needing to resort to shared memory.
--- tools/testing/selftests/kvm/include/ucall_common.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +11 -0
@@ -52,6 +52,17 @@ int ucall_nr_pages_required(uint64_t page_size);#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)#define GUEST_SYNC (stage) ucall(UCALL_SYNC, 2, "hello", stage) +#define GUEST_SYNC1 (arg0) ucall(UCALL_SYNC, 1, arg0) +#define GUEST_SYNC2 (arg0, arg1) ucall(UCALL_SYNC, 2, arg0, arg1) +#define GUEST_SYNC3 (arg0, arg1, arg2) \ + ucall(UCALL_SYNC, 3, arg0, arg1, arg2) +#define GUEST_SYNC4 (arg0, arg1, arg2, arg3) \ + ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3) +#define GUEST_SYNC5 (arg0, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4) +#define GUEST_SYNC6 (arg0, arg1, arg2, arg3, arg4, arg5) \ + ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5) + #define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)#define GUEST_DONE () ucall(UCALL_DONE, 0)
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 31/35 KVM: selftests: Add x86-only selftest for private memory conversions
In reply to Sean ChristophersonFrom: Vishal Annapurve <[email protected]>
Add a selftest to exercise implicit/explicit conversion functionality within KVM and verify:
- Shared memory is visible to host userspace
- Private memory is not visible to host userspace
- Host userspace and guest can communicate over shared memory
- Data in shared backing is preserved across conversions (test's host userspace doesn't free the data)
- Private memory is bound to the lifetime of the VM
Ideally, KVM's selftests infrastructure would be reworked to allow backing a single region of guest memory with multiple memslots for _all_ backing types and shapes, i.e. ideally the code for using a single backing fd across multiple memslots would work for "regular" memory as well. But sadly, support for KVM_CREATE_GUEST_MEMFD has languished for far too long, and overhauling selftests' memslots infrastructure would likely open a can of worms, i.e. delay things even further.
In addition to the more obvious tests, verify that PUNCH_HOLE actually frees memory. Directly verifying that KVM frees memory is impractical, if it's even possible, so instead indirectly verify memory is freed by asserting that the guest reads zeroes after a PUNCH_HOLE. E.g. if KVM zaps SPTEs but doesn't actually punch a hole in the inode, the subsequent read will still see the previous value. And obviously punching a hole shouldn't cause explosions.
Let the user specify the number of memslots in the private mem conversion test, i.e. don't require the number of memslots to be '1' or "nr_vcpus". Creating more memslots than vCPUs is particularly interesting, e.g. it can result in a single KVM_SET_MEMORY_ATTRIBUTES spanning multiple memslots. To keep the math reasonable, align each vCPU's chunk to at least 2MiB (the size is 2MiB+4KiB), and require the total size to be cleanly divisible by the number of memslots. The goal is to be able to validate that KVM plays nice with multiple memslots, being able to create a truly arbitrary number of memslots doesn't add meaningful value, i.e. isn't worth the cost.
Intentionally don't take a requirement on KVM_CAP_GUEST_MEMFD, KVM_CAP_MEMORY_FAULT_INFO, KVM_MEMORY_ATTRIBUTE_PRIVATE, etc., as it's a KVM bug to advertise KVM_X86_SW_PROTECTED_VM without its prerequisites.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_conversions_test.c | 487 ++++++++++++++++++ 2 files changed, 488 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -81,6 +81,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c new file +487 -0
487 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 32/35 KVM: selftests: Add KVM_SET_USER_MEMORY_REGION2 helper
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Add helpers to invoke KVM_SET_USER_MEMORY_REGION2 directly so that tests can validate of features that are unique to "version 2" of "set user memory region", e.g. do negative testing on gmem_fd and gmem_offset.
Provide a raw version as well as an assert-success version to reduce the amount of boilerplate code need for basic usage.
--- .../selftests/kvm/include/kvm_util_base.h | 7 +++++ tools/testing/selftests/kvm/lib/kvm_util.c | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +7 -0
@@ -522,6 +522,13 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva);int __vm_set_user_memory_region (struct kvm_vm *vm,uint32_t slot,uint32_t flags,uint64_t gpa,uint64_t size,void *hva); +void vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot,uint32_t flags, +uint64_t gpa,uint64_t size,void *hva, +uint32_t guest_memfd,uint64_t guest_memfd_offset); +int __vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot,uint32_t flags, +uint64_t gpa,uint64_t size,void *hva, +uint32_t guest_memfd,uint64_t guest_memfd_offset); + void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages,
tools/testing/selftests/kvm/lib/kvm_util.c +29 -0
@@ -873,6 +873,35 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, errno, strerror(errno)); } +int __vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot,uint32_t flags, +uint64_t gpa,uint64_t size,void *hva, +uint32_t guest_memfd,uint64_t guest_memfd_offset) +{ +struct kvm_userspace_memory_region2 region = { + .slot = slot, + .flags = flags, + .guest_phys_addr = gpa, + .memory_size = size, + .userspace_addr = (uintptr_t )hva, + .guest_memfd = guest_memfd, + .guest_memfd_offset = guest_memfd_offset, + }; + +return ioctl (vm->fd,KVM_SET_USER_MEMORY_REGION2 , ®ion); +} + +void vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot,uint32_t flags, +uint64_t gpa,uint64_t size,void *hva, +uint32_t guest_memfd,uint64_t guest_memfd_offset) +{ +int ret =__vm_set_user_memory_region2 (vm, slot, flags, gpa, size, hva, + guest_memfd, guest_memfd_offset); + +TEST_ASSERT (!ret,"KVM_SET_USER_MEMORY_REGION2 failed, errno = %d (%s)" , + errno,strerror (errno)); +} + +/* FIXME: This thing needs to be ripped apart and rewritten. */ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages,
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 33/35 KVM: selftests: Expand set_memory_region_test to validate guest_memfd()
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Expand set_memory_region_test to exercise various positive and negative testcases for private memory.
- Non-guest_memfd() file descriptor for private memory
- guest_memfd() from different VM
- Overlapping bindings
- Unaligned bindings
--- .../selftests/kvm/include/kvm_util_base.h | 10 ++ .../selftests/kvm/set_memory_region_test.c | 100 ++++++++++++++++++ 2 files changed, 110 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +10 -0
@@ -819,6 +819,16 @@ static inline struct kvm_vm *vm_create_barebones(void)return ____vm_create (VM_SHAPE_DEFAULT ); } +static inline struct kvm_vm *vm_create_barebones_protected_vm (void ) +{ +const struct vm_shape shape = { + .mode =VM_MODE_DEFAULT , + .type =KVM_X86_SW_PROTECTED_VM , + }; + +return ____vm_create (shape); +} +static inline struct kvm_vm *vm_create (uint32_t nr_runnable_vcpus) {return __vm_create (VM_SHAPE_DEFAULT , nr_runnable_vcpus,0 );
tools/testing/selftests/kvm/set_memory_region_test.c +100 -0
112 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 34/35 KVM: selftests: Add basic selftest for guest_memfd()
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Add a selftest to verify the basic functionality of guest_memfd():
+ file descriptor created with the guest_memfd() ioctl does not allow read/write/mmap operations
+ file size and block size as returned from fstat are as expected
+ fallocate on the fd checks that offset/length on fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned
+ invalid inputs (misaligned size, invalid flags) are rejected
+ file size and inode are unique (the innocuous-sounding anon_inode_getfile() backs all files with a single inode...)
--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 221 ++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -124,6 +124,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test +TEST_GEN_PROGS_x86_64 += guest_memfd_test TEST_GEN_PROGS_x86_64 += guest_print_test TEST_GEN_PROGS_x86_64 += hardware_disable_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/guest_memfd_test.c new file +221 -0
221 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Sean Christopherson
maintainerPATCH v13 35/35 KVM: selftests: Test KVM exit behavior for private memory/access
In reply to Sean ChristophersonFrom: Ackerley Tng <[email protected]>
"Testing private access when memslot gets deleted" tests the behavior of KVM when a private memslot gets deleted while the VM is using the private memslot. When KVM looks up the deleted (slot = NULL) memslot, KVM should exit to userspace with KVM_EXIT_MEMORY_FAULT.
In the second test, upon a private access to non-private memslot, KVM should also exit to userspace with KVM_EXIT_MEMORY_FAULT.
Intentionally don't take a requirement on KVM_CAP_GUEST_MEMFD, KVM_CAP_MEMORY_FAULT_INFO, KVM_MEMORY_ATTRIBUTE_PRIVATE, etc., as it's a KVM bug to advertise KVM_X86_SW_PROTECTED_VM without its prerequisites.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -82,6 +82,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Paolo Bonzini
maintainerQuoted 9 lines · Sean Christopherson
Non-KVM people, please take a gander at two small-ish patches buried in the middle of this series:
fs: Export
anon_inode_getfile_secure()for use by KVM
mm: AddAS_UNMOVABLEto mark mapping as completely unmovableOur plan/hope is to take this through the KVM tree for 6.8, reviews (and acks!) would be much appreciated. Note, adding
AS_UNMOVABLEisn't strictly required as it's "just" an optimization, but we'd prefer to have it in place straightaway.
Reporting what I wrote in the other thread, for wider distribution:
I'm going to wait a couple days more for reviews to come in, post a v14 myself, and apply the series to kvm/next as soon as Linus merges the 6.7 changes. The series will be based on the 6.7 tags/for-linus, and when 6.7-rc1 comes up, I'll do this to straighten the history:
git checkout kvm/next
git tag -s -f kvm-gmem HEAD
git reset --hard v6.7-rc1
git merge tags/kvm-gmem
# fix conflict with Christian Brauner's VFS series git commit
git push kvm
6.8 is not going to be out for four months, and I'm pretty sure that anything that would be discovered within "a few weeks" can also be applied on top, and the heaviness of a 35-patch series will outweigh any imperfections by a long margin.
(Full disclosure: this is _also_ because I want to apply this series to the RHEL kernel, and Red Hat has a high level of disdain for non-upstream patches. But it's mostly because I want all dependencies to be able to move on and be developed on top of stock kvm/next).
Paolo