selftests: KVM: selftests for fd-based private memory
v3 cover selftests: KVM: selftests for fd-based private memory
This v3 series implements selftests targeting the feature floated by Chao via:
https://lore.kernel.org/linux-mm/[email protected]/T/
Below changes aim to test the fd based approach for guest private memory in context of normal (non-confidential) VMs executing on non-confidential platforms.
private_mem_test.c file adds selftest to access private memory from the guest via private/shared accesses and checking if the contents can be leaked to/accessed by vmm via shared memory view before/after conversions.
Updates in V3:
1) Series is based on v7 series from Chao
2) Changes are introduced in KVM to help execute private mem selftests
3) Selftests are executing from private memory
4) Test implementation is simplified to contain implicit/explicit memory conversion paths according to feedback from Sean.
5) Addressed comments from Sean and Shuah.
This series has dependency on following patches:
1) V7 series patches from Chao mentioned above.
2) https://lore.kernel.org/lkml/[email protected]/T/#u
- Series posted by Peter containing patches from Michael and Sean.
Github link for the patches posted as part of this series:
https://github.com/vishals4gh/linux/commits/priv_memfd_selftests_rfc_v3
Vishal Annapurve (6):
kvm: x86: Add support for testing private memory
selftests: kvm: Add support for private memory
selftests: kvm: ucall: Allow querying ucall pool gpa
selftests: kvm: x86: Execute hypercall as per the cpu
selftests: kvm: x86: Execute VMs with private memory
sefltests: kvm: x86: Add selftest for private memory
arch/x86/include/uapi/asm/kvm_para.h | 2 + arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 19 ++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/x86.c | 67 +++- include/linux/kvm_host.h | 12 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 2 + .../selftests/kvm/include/kvm_util_base.h | 12 +- .../selftests/kvm/include/ucall_common.h | 2 + .../kvm/include/x86_64/private_mem.h | 51 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 40 ++- .../testing/selftests/kvm/lib/ucall_common.c | 12 + .../selftests/kvm/lib/x86_64/private_mem.c | 297 ++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 15 +- .../selftests/kvm/x86_64/private_mem_test.c | 262 +++++++++++++++ virt/kvm/Kconfig | 9 + virt/kvm/kvm_main.c | 90 +++++- 18 files changed, 887 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/private_mem.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/private_mem.c create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_test.c
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
RFC PATCH v3 1/6 kvm: x86: Add support for testing private memory
Introduce HAVE_KVM_PRIVATE_MEM_TESTING config to be able to test fd based approach to support private memory with non-confidential selftest VMs. To support this testing few important aspects need to be considered from the perspective of selftests -
* Non-confidential VM should be able to boot from private memory:
ENCRYPT_REG_REGION is modified to support populating initial private memory contents.
* Non-confidential VM needs to convey its access type to KVM:
Introduce HAVE_KVM_GUEST_PRIVATE_ACCESS_TRACKING to allow guest to convey the gpa ranges that need to be considered as accessed via private/shared access.
memslots are modified to carry a shared bitmap that will be toggled when guest issues HC_MAP_GPA_RANGE hypercall with newly introduced attributes.
* arch_private_mem_supported and kvm_is_fault_private are added/updated to allow private memory logic to work with non-confidential vm selftests.
--- arch/x86/include/uapi/asm/kvm_para.h | 2 + arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 19 ++++++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/x86.c | 67 ++++++++++++++++++++- include/linux/kvm_host.h | 12 ++++ virt/kvm/Kconfig | 9 +++ virt/kvm/kvm_main.c | 90 +++++++++++++++++++++++++++- 8 files changed, 198 insertions(+), 4 deletions(-)
arch/x86/include/uapi/asm/kvm_para.h +2 -0
@@ -102,6 +102,8 @@ struct kvm_clock_pairing {#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M (1 << 0)#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G (1 << 1)#define KVM_MAP_GPA_RANGE_ENC_STAT (n) (n << 4) +#define KVM_MARK_GPA_RANGE_ENC_ACCESS (1 << 8) +#define KVM_CLR_GPA_RANGE_ENC_ACCESS (1 << 9)#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(1)#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(0)
arch/x86/kvm/Kconfig +1 -0
@@ -49,6 +49,7 @@ config KVM select INTERVAL_TREE select HAVE_KVM_PM_NOTIFIER if PM select HAVE_KVM_PRIVATE_MEM if X86_64 + select HAVE_KVM_PRIVATE_MEM_TESTING if HAVE_KVM_PRIVATE_MEM select MEMFILE_NOTIFIER if HAVE_KVM_PRIVATE_MEM select XARRAY_MULTI if HAVE_KVM_PRIVATE_MEM help
arch/x86/kvm/mmu/mmu.c +19 -0
@@ -4236,6 +4236,24 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,mmu_updating_retry_gfn (vcpu->kvm, mmu_seq, fault->gfn); } +static bool kvm_is_fault_private (struct kvm *kvm,struct kvm_page_fault *fault) +{ +struct kvm_memory_slot *slot = fault->slot; +bool is_private; + +if (!kvm_slot_can_be_private (slot)) +return false; + +mutex_lock (&kvm->slots_lock); +if (slot->shared_bitmap) + is_private = !test_bit (fault->gfn - slot->base_gfn, slot->shared_bitmap); +else + is_private = false; +mutex_unlock (&kvm->slots_lock); + +return is_private; +} +static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);
@@ -4245,6 +4263,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->gfn = fault->addr >>PAGE_SHIFT ; fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); + fault->is_private =kvm_is_fault_private (vcpu->kvm, fault);if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ;
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -188,7 +188,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp; -const bool is_private; +bool is_private;const bool nx_huge_page_workaround_enabled;/*
arch/x86/kvm/x86.c +65 -2
94 lines not on this page · raw message
include/linux/kvm_host.h +12 -0
@@ -328,6 +328,9 @@ struct kvm_vcpu {u64 requests;unsigned long guest_debug; +uint64_t priv_gfn; +uint64_t priv_pages; +struct mutex mutex;struct kvm_run *run;
@@ -576,6 +579,7 @@ struct kvm_memory_slot {gfn_t base_gfn;unsigned long npages;unsigned long *dirty_bitmap; +unsigned long *shared_bitmap;struct kvm_arch_memory_slot arch;unsigned long userspace_addr;u32 flags;
@@ -602,6 +606,11 @@ static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslreturn ALIGN (memslot->npages,BITS_PER_LONG ) /8 ; } +static inline unsigned long kvm_shared_bitmap_bytes (struct kvm_memory_slot *memslot) +{ +return ALIGN (memslot->npages,BITS_PER_LONG ) /8 ; +} +static inline unsigned long *kvm_second_dirty_bitmap (struct kvm_memory_slot *memslot) {unsigned long len =kvm_dirty_bitmap_bytes (memslot);
@@ -799,6 +808,9 @@ struct kvm {u32 dirty_ring_size;bool vm_bugged;bool vm_dead; +#ifdef CONFIG_HAVE_KVM_PRIVATE_MEM_TESTING +bool vm_entry_attempted; +#endif #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier;
virt/kvm/Kconfig +9 -0
@@ -75,3 +75,12 @@ config HAVE_KVM_PM_NOTIFIER config HAVE_KVM_PRIVATE_MEM bool + +config HAVE_KVM_PRIVATE_MEM_TESTING + bool + select HAVE_KVM_GUEST_PRIVATE_ACCESS_TRACKING + depends on HAVE_KVM_PRIVATE_MEM + +config HAVE_KVM_GUEST_PRIVATE_ACCESS_TRACKING + bool + depends on HAVE_KVM_PRIVATE_MEM
virt/kvm/kvm_main.c +89 -1
140 lines not on this page · raw message
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
RFC PATCH v3 2/6 selftests: kvm: Add support for private memory
In reply to Vishal AnnapurveAdd support for registering private memory with kvm using KVM_SET_USER_MEMORY_REGION ioctl.
Helper function to query extended userspace mem region is introduced to allow memory conversion.
--- .../selftests/kvm/include/kvm_util_base.h | 12 +++++- tools/testing/selftests/kvm/lib/kvm_util.c | 40 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +10 -2
@@ -30,7 +30,10 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { -struct kvm_userspace_memory_region region; +union { +struct kvm_userspace_memory_region region; +struct kvm_userspace_memory_region_ext region_ext; + };struct sparsebit *unused_phy_pages;struct sparsebit *encrypted_phy_pages;int fd;
@@ -200,7 +203,7 @@ static inline bool kvm_has_cap(long cap)#define kvm_do_ioctl (fd, cmd, arg) \ ({ \ - static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) == _IOC_SIZE(cmd), ""); \ + static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) >= _IOC_SIZE(cmd), ""); \ ioctl(fd, cmd, arg); \ })
@@ -388,6 +391,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,void vm_mem_region_set_flags (struct kvm_vm *vm,uint32_t slot,uint32_t flags);void vm_mem_region_move (struct kvm_vm *vm,uint32_t slot,uint64_t new_gpa);void vm_mem_region_delete (struct kvm_vm *vm,uint32_t slot); +struct kvm_vcpu *__vm_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id);vm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);
@@ -717,6 +721,10 @@ struct kvm_userspace_memory_region * kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end); +struct kvm_userspace_memory_region_ext * +kvm_userspace_memory_region_ext_find (struct kvm_vm *vm,uint64_t start, +uint64_t end); +#define sync_global_to_guest (vm, g) ({ \ typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ memcpy(_p, &(g), sizeof(g)); \
tools/testing/selftests/kvm/lib/kvm_util.c +39 -1
@@ -17,6 +17,7 @@#include <unistd.h> #include <linux/kernel.h> +#define MFD_INACCESSIBLE 0x0008U#define KVM_UTIL_MIN_PFN 2static int vcpu_mmap_sz (void );
@@ -466,6 +467,35 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,return ®ion->region; } +/* +* KVM Userspace Memory Region Ext Find +* +* Input Args: +* vm - Virtual Machine +* start - Starting VM physical address +* end - Ending VM physical address, inclusive. +* +* Output Args: None +* +* Return: +* Pointer to overlapping ext region, NULL if no such region. +* +* Public interface to userspace_mem_region_find. Allows tests to look up +* the memslot datastructure for a given range of guest physical memory. +*/ +struct kvm_userspace_memory_region_ext * +kvm_userspace_memory_region_ext_find (struct kvm_vm *vm,uint64_t start, +uint64_t end) +{ +struct userspace_mem_region *region; + + region =userspace_mem_region_find (vm, start, end); +if (!region) +return NULL ; + +return ®ion->region_ext; +} +/* * VM VCPU Remove *
@@ -764,6 +794,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,struct userspace_mem_region *region;size_t backing_src_pagesz =get_backing_src_pagesz (src_type);size_t alignment; +int priv_memfd =-1 ;TEST_ASSERT (vm_adjust_num_guest_pages (vm->mode, npages) == npages, "Number of guest pages is not compatible with the host. "
@@ -869,6 +900,11 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, vm_mem_backing_src_alias(src_type)->name); } +if (flags &KVM_MEM_PRIVATE ) { + priv_memfd =memfd_create ("vm_private_mem_" ,MFD_INACCESSIBLE ); +TEST_ASSERT (priv_memfd !=-1 ,"Failed to create private memfd" ); + } + region->unused_phy_pages =sparsebit_alloc (); region->encrypted_phy_pages =sparsebit_alloc (); sparsebit_set_num(region->unused_phy_pages,
@@ -878,7 +914,9 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->region.guest_phys_addr = guest_paddr; region->region.memory_size = npages * vm->page_size; region->region.userspace_addr = (uintptr_t ) region->host_mem; - ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region); + region->region_ext.private_fd = priv_memfd; + region->region_ext.private_offset =0 ; + ret =ioctl (vm->fd,KVM_SET_USER_MEMORY_REGION , ®ion->region_ext);TEST_ASSERT (ret ==0 ,"KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n"
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
RFC PATCH v3 3/6 selftests: kvm: ucall: Allow querying ucall pool gpa
In reply to Vishal AnnapurveAdd a helper to query guest physical address for ucall pool so that guest can mark the page as accessed shared or private.
--- tools/testing/selftests/kvm/include/ucall_common.h | 2 ++ tools/testing/selftests/kvm/lib/ucall_common.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +2 -0
@@ -31,6 +31,8 @@ void ucall_arch_uninit(struct kvm_vm *vm);void ucall_arch_do_ucall (vm_vaddr_t uc);void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu); +vm_paddr_t get_ucall_pool_paddr (void ); +void ucall (uint64_t cmd,int nargs, ...);uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc);
tools/testing/selftests/kvm/lib/ucall_common.c +12 -0
@@ -11,6 +11,7 @@ struct ucall_header {static bool use_ucall_pool;static struct ucall_header *ucall_pool; +static vm_paddr_t ucall_page_paddr;void ucall_init (struct kvm_vm *vm,void *arg) {
@@ -35,7 +36,10 @@ void ucall_init(struct kvm_vm *vm, void *arg) } ucall_pool = (struct ucall_header *)vaddr; + ucall_page_paddr =addr_gva2gpa (vm, vaddr);sync_global_to_guest (vm, ucall_pool); +sync_global_to_guest (vm, ucall_page_paddr); +printf ("ucall_page_paddr 0x%lx\n" , ucall_page_paddr); out:ucall_arch_init (vm, arg);
@@ -54,6 +58,14 @@ void ucall_uninit(struct kvm_vm *vm)ucall_arch_uninit (vm); } +vm_paddr_t get_ucall_pool_paddr (void ) +{ +if (!use_ucall_pool) +return 0 ; + +return ucall_page_paddr; +} +static struct ucall *ucall_alloc (void ) {struct ucall *uc =NULL ;
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
maintainerRe: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 5 lines · Vishal Annapurve
Add a helper to query guest physical address for ucall pool so that guest can mark the page as accessed shared or private.
Signed-off-by Vishal Annapurve <[email protected]>---
This should be handled by the SEV series[*]. Can you provide feedback on that series if having a generic way to map the ucall address as shared won't work?
Re: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · Vishal Annapurve
Add a helper to query guest physical address for ucall pool so that guest can mark the page as accessed shared or private.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Vishal Annapurve <[email protected]>---
This should be handled by the SEV series[*]. Can you provide feedback on that series if having a generic way to map the ucall address as shared won't work?
Based on the SEV series you referred to, selftests are capable of accessing ucall pool memory by having encryption bit cleared (as set by guest pagetables) as allowed by generic API vm_vaddr_alloc_shared. This change is needed in the context of fd based private memory where guest (specifically non-confidential/sev guests) code in the selftests will have to explicitly indicate that ucall pool address range will be accessed by guest as shared.
Sean Christopherson
maintainerRe: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 14 lines · Vishal Annapurve
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · Vishal Annapurve
Add a helper to query guest physical address for ucall pool so that guest can mark the page as accessed shared or private.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Vishal Annapurve <[email protected]>---
This should be handled by the SEV series[*]. Can you provide feedback on that series if having a generic way to map the ucall address as shared won't work?
Quoted 7 lines
Based on the SEV series you referred to, selftests are capable of accessing ucall pool memory by having encryption bit cleared (as set by guest pagetables) as allowed by generic API
vm_vaddr_alloc_shared. This change is needed in the context of fd based private memory where guest (specifically non-confidential/sev guests) code in the selftests will have to explicitly indicate that ucall pool address range will be accessed by guest as shared.
Ah, right, the conversion needs an explicit hypercall, which gets downright annoying because auto-converting shared pages would effectivfely require injecting code into the start of every guest.
Ha! I think we got too fancy. This is purely for testing UPM, not any kind of trust model, i.e. there's no need for KVM to treat userspace as untrusted. Rather than jump through hoops just to let the guest dictate private vs. shared, simply "trust" userspace when determining whether a page should be mapped private. Then the selftests can invoke the repurposed KVM_MEMORY_ENCRYPT_(UN)REG_REGION ioctls as appropriate when allocating/remapping guest private memory.
E.g. on top of UPM v8, I think the test hook boils down to:
arch/x86/kvm/mmu/mmu.c +3 -0
@@ -4279,6 +4279,9 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->gfn = fault->addr >>PAGE_SHIFT ; fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); + fault->is_private =IS_ENABLED (CONFIG_KVM_PRIVATE_MEM_TESTING ) && +kvm_slot_can_be_private (fault->slot) && +kvm_mem_is_private (vcpu->kvm, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ;
virt/kvm/kvm_main.c +1 -1
@@ -1653,7 +1653,7 @@ static void kvm_replace_memslot(struct kvm *kvm,bool __weak kvm_arch_has_private_mem (struct kvm *kvm) { -return false; +return IS_ENABLED (CONFIG_KVM_PRIVATE_MEM_TESTING ); } static int check_memory_region_flags(struct kvm *kvm,
Re: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 24 lines · Sean Christopherson
Quoted 14 lines · Vishal Annapurve
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · Vishal Annapurve
Add a helper to query guest physical address for ucall pool so that guest can mark the page as accessed shared or private.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Vishal Annapurve <[email protected]>---
This should be handled by the SEV series[*]. Can you provide feedback on that series if having a generic way to map the ucall address as shared won't work?
Quoted 7 lines
Based on the SEV series you referred to, selftests are capable of accessing ucall pool memory by having encryption bit cleared (as set by guest pagetables) as allowed by generic API
vm_vaddr_alloc_shared. This change is needed in the context of fd based private memory where guest (specifically non-confidential/sev guests) code in the selftests will have to explicitly indicate that ucall pool address range will be accessed by guest as shared.
Quoted 4 lines
Ah, right, the conversion needs an explicit hypercall, which gets downright annoying because auto-converting shared pages would effectivfely require injecting code into the start of every guest.
Ack.
Quoted 28 lines
Ha! I think we got too fancy. This is purely for testing UPM, not any kind of trust model, i.e. there's no need for KVM to treat userspace as untrusted. Rather than jump through hoops just to let the guest dictate private vs. shared, simply "trust" userspace when determining whether a page should be mapped private. Then the selftests can invoke the repurposed KVM_MEMORY_ENCRYPT_(UN)REG_REGION ioctls as appropriate when allocating/remapping guest private memory.
E.g. on top of UPM v8, I think the test hook boils down to:
arch/x86/kvm/mmu/mmu.c +3
-0@@ -4279,6 +4279,9 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->gfn = fault->addr >>PAGE_SHIFT ; fault->slot =kvm_vcpu_gfn_to_memslot (vcpu, fault->gfn); + fault->is_private =IS_ENABLED (CONFIG_KVM_PRIVATE_MEM_TESTING ) && +kvm_slot_can_be_private (fault->slot) && +kvm_mem_is_private (vcpu->kvm, fault->gfn);if (page_fault_handle_page_track (vcpu, fault))return RET_PF_EMULATE ;virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static void kvm_replace_memslot(struct kvm *kvm, · line 1659
Quoted 8 lines
virt/kvm/kvm_main.c +1
-1@@ -1653,7 +1653,7 @@ static void kvm_replace_memslot(struct kvm *kvm,bool __weak kvm_arch_has_private_mem (struct kvm *kvm) { -return false; +return IS_ENABLED (CONFIG_KVM_PRIVATE_MEM_TESTING ); } static int check_memory_region_flags(struct kvm *kvm,
This is much sleeker and will avoid hacking KVM for testing. Only caveat here is that these tests will not be able to exercise implicit conversion path if we go this route.
Sean Christopherson
maintainerRe: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 3 lines · Vishal Annapurve
This is much sleeker and will avoid hacking KVM for testing. Only caveat here is that these tests will not be able to exercise implicit conversion path if we go this route.
Yeah, I think that's a perfectly fine tradeoff. Implicit conversion isn't strictly a UPM feature, e.g. if TDX and SNP "architecturally" disallowed implicit conversions, then KVM wouldn't need to support implicit conversions at all, i.e. that testing can be punted to SNP and/or TDX selftests.
Re: [RFC V3 PATCH 3/6] selftests: kvm: ucall: Allow querying ucall pool gpa
Quoted 10 lines · Sean Christopherson
Quoted 3 lines · Vishal Annapurve
This is much sleeker and will avoid hacking KVM for testing. Only caveat here is that these tests will not be able to exercise implicit conversion path if we go this route.
Yeah, I think that's a perfectly fine tradeoff. Implicit conversion isn't strictly a UPM feature, e.g. if TDX and SNP "architecturally" disallowed implicit conversions, then KVM wouldn't need to support implicit conversions at all, i.e. that testing can be punted to SNP and/or TDX selftests.
Ack. Will address this feedback in the next series.
RFC PATCH v3 4/6 selftests: kvm: x86: Execute hypercall as per the cpu
In reply to Vishal AnnapurveAdd support for executing vmmcall/vmcall instruction on amd/intel cpus. In general kvm patches the instruction according to the cpu implementation at runtime. While executing selftest vms from private memory KVM will not be able to update the private memory of the guest.
Hypercall parameters are fixed to explicitly populate hypercall number in eax. Otherwise inlined function calls to kvm_hypercall would call vmmcall/vmcall instruction without updating eax with hypercall number.
--- .../testing/selftests/kvm/lib/x86_64/processor.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +13 -2
@@ -1254,10 +1254,21 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3) {uint64_t r; +static bool is_cpu_checked; +static bool is_cpu_amd; - asmvolatile ("vmcall" +if (!is_cpu_checked) + is_cpu_amd =is_amd_cpu (); + +if (is_cpu_amd) { + asmvolatile ("vmmcall" + :"=a" (r) + :"a" (nr),"b" (a0),"c" (a1),"d" (a2),"S" (a3)); + }else { + asmvolatile ("vmcall" :"=a" (r) - :"b" (a0),"c" (a1),"d" (a2),"S" (a3)); + :"a" (nr),"b" (a0),"c" (a1),"d" (a2),"S" (a3)); + }return r; }
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
maintainerRe: [RFC V3 PATCH 4/6] selftests: kvm: x86: Execute hypercall as per the cpu
Quoted 8 lines · Vishal Annapurve
Add support for executing vmmcall/vmcall instruction on amd/intel cpus. In general kvm patches the instruction according to the cpu implementation at runtime. While executing selftest vms from private memory KVM will not be able to update the private memory of the guest.
Hypercall parameters are fixed to explicitly populate hypercall number in eax. Otherwise inlined function calls to
kvm_hypercallwould call vmmcall/vmcall instruction without updating eax with hypercall number.
Can you send a seperate non-RFC series to clean up the selftests mess? kvm_hypercall() isn't the only culprit.
git grep \"vmcall | wc -l
16
I'm pretty sure things work only because of KVM's dubious behavior of patching VMCALL/VMMCALL by default.
Note, svm_vmcall_test.c intentionally uses the wrong instructions and shouldn't be converted. Actually, we can and should just delete that test, it's superseded by the wonderfully named fix_hypercall_test.
Quoted 10 lines
Signed-off-by Vishal Annapurve <[email protected]>--- .../testing/selftests/kvm/lib/x86_64/processor.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0
tools/testing/selftests/kvm/lib/x86_64/processor.c uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, · line 1261
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +4
-1@@ -1254,10 +1254,21 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3) {uint64_t r; +static bool is_cpu_checked; +static bool is_cpu_amd; - asm volatile("vmcall" +if (!is_cpu_checked) + is_cpu_amd =is_amd_cpu ();
This can be done using a single int, e.g.
static bool is_cpu_amd = -1; if (is_cpu_amd < 0) is_cpu_amd = is_amd_cpu();
Although... what if we declare main() in lib/kvm_util.c (or maybe a dedicated file?) and rename all tests to use __main()? Then add an arch hook to do global initialization and avoid the "did we check CPUID?!?!?" altogether.
That would allow us to dedup all of the hilarious copy paste:
/* Tell stdout not to buffer its content */ setbuf(stdout, NULL);
and we could turn is_amd_cpu() and is_intel_cpu() into bools.
E.g.
int main(int argc, char *argv[])
{
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
kvm_arch_main();
return __main(argc, argv);
}void kvm_arch_main(void)
{
is_cpu_amd = cpu_vendor_string_is("AuthenticAMD");
is_cpu_intel = cpu_vendor_string_is("AuthenticAMD");
}And then we just need macro magic to emit the right VMCALL/VMMCALL instruction.
Re: [RFC V3 PATCH 4/6] selftests: kvm: x86: Execute hypercall as per the cpu
Quoted 74 lines · Sean Christopherson
Quoted 8 lines · Vishal Annapurve
Add support for executing vmmcall/vmcall instruction on amd/intel cpus. In general kvm patches the instruction according to the cpu implementation at runtime. While executing selftest vms from private memory KVM will not be able to update the private memory of the guest.
Hypercall parameters are fixed to explicitly populate hypercall number in eax. Otherwise inlined function calls to
kvm_hypercallwould call vmmcall/vmcall instruction without updating eax with hypercall number.Can you send a seperate non-RFC series to clean up the selftests mess?
kvm_hypercall()isn't the only culprit.git grep \"vmcall | wc -l
16I'm pretty sure things work only because of KVM's dubious behavior of patching VMCALL/VMMCALL by default.
Note,
svm_vmcall_test.cintentionally uses the wrong instructions and shouldn't be converted. Actually, we can and should just delete that test, it's superseded by the wonderfully namedfix_hypercall_test.Quoted 10 lines
Signed-off-by Vishal Annapurve <[email protected]>--- .../testing/selftests/kvm/lib/x86_64/processor.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0tools/testing/selftests/kvm/lib/x86_64/processor.c uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, · line 1261
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +4
-1@@ -1254,10 +1254,21 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3) {uint64_t r; +static bool is_cpu_checked; +static bool is_cpu_amd; - asm volatile("vmcall" +if (!is_cpu_checked) + is_cpu_amd =is_amd_cpu ();This can be done using a single int, e.g.
static bool is_cpu_amd = -1; if (is_cpu_amd < 0) is_cpu_amd = is_amd_cpu();Although... what if we declare
main()inlib/kvm_util.c(or maybe a dedicated file?) and rename all tests to use__main()? Then add an arch hook to do global initialization and avoid the "did we check CPUID?!?!?" altogether.That would allow us to dedup all of the hilarious copy paste:
/* Tell stdout not to buffer its content */ setbuf(stdout, NULL);and we could turn
is_amd_cpu()andis_intel_cpu()into bools.E.g.
int
main(int argc, char *argv[]){ /* Tell stdout not to buffer its content */ setbuf(stdout, NULL); kvm_arch_main(); return __main(argc, argv); }
Quoted 8 lines
void
kvm_arch_main(void){ is_cpu_amd = cpu_vendor_string_is("AuthenticAMD"); is_cpu_intel = cpu_vendor_string_is("AuthenticAMD"); }And then we just need macro magic to emit the right VMCALL/VMMCALL instruction.
Thanks Sean for the feedback here. I have posted a separate series addressing your comments:
https://lore.kernel.org/all/[email protected]/T/
Regards,
Vishal
RFC PATCH v3 5/6 selftests: kvm: x86: Execute VMs with private memory
In reply to Vishal AnnapurveIntroduce a set of APIs to execute VM with private memslots.
Host userspace APIs for:
1) Setting up and executing VM having private memslots
2) Backing/unbacking guest private memory
Guest APIs for:
1) Changing memory access type and memory type
2) Setting ucall page access type as shared
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/include/x86_64/private_mem.h | 51 +++ .../selftests/kvm/lib/x86_64/private_mem.c | 297 ++++++++++++++++++ 3 files changed, 349 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/private_mem.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/private_mem.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -51,6 +51,7 @@ LIBKVM += lib/ucall_common.c LIBKVM_x86_64 += lib/x86_64/apic.c LIBKVM_x86_64 += lib/x86_64/handlers.S LIBKVM_x86_64 += lib/x86_64/perf_test_util.c +LIBKVM_x86_64 += lib/x86_64/private_mem.c LIBKVM_x86_64 += lib/x86_64/processor.c LIBKVM_x86_64 += lib/x86_64/svm.c LIBKVM_x86_64 += lib/x86_64/ucall.c
tools/testing/selftests/kvm/include/x86_64/private_mem.h new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +297 -0
297 lines not on this page · raw message
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
maintainerRe: [RFC V3 PATCH 5/6] selftests: kvm: x86: Execute VMs with private memory
Quoted 9 lines · Vishal Annapurve
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +9
-0+/* +* Execute KVM hypercall to change memory access type for a given gpa range. +* +* Input Args: +* type - memory conversion type TO_SHARED/TO_PRIVATE +* gpa - starting gpa address +* size - size of the range starting from gpa for which memory access needs +* to be changed +*
tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 44
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +10
-0+* Output Args: None +* +* Return: None +* +* Function called by guest logic in selftests to update the memory access type +* for a given gpa range. This API is useful in exercising implicit conversion +* path. +*/ +void guest_update_mem_access (enum mem_conversion_type type,uint64_t gpa, +uint64_t size)
Provide wrappers to self-document what's going on, then the massive block comments go away. And the guts of this and guest_update_mem_map() are nearly identical.
Hmm, and we probably want to make it possible to do negative testing.
Then the one-off enums for TO_PRIVATE and whatnot go way too.
Review
Quoted 2 lines
(quoted diff) +2
-0+{ + int ret = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> MIN_PAGE_SHIFT,
Needs an assert that @size is page aligned. And since these are x86-64 specific, just use PAGE_SHIFT. Huh, IS_ALIGNED() doesn't exist in selftests. That should be added, either by pulling in align.h or by adding the generic macros to kvm_util_base.h.
And then x86-64's processor.h can defined IS_PAGE_ALIGNED().
E.g.
static inline void __kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size,
uint64_t flags)
{
return = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> PAGE_SHIFT, flags, 0);
}static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size,
uint64_t flags)
{
int ret;
GUEST_ASSERT_2(IS_PAGE_ALIGNED(gpa) && IS_PAGE_ALIGNED(size), gpa, size);
ret = __kvm_hypercall_map_gpa_range(gpa, size, flags);
GUEST_ASSERT_1(!ret, ret);
}static inline kvm_hypercall_map_shared(uint64_t gpa, uint64_t size)
{
kvm_hypercall_map_gpa_range(gpa, size, KVM_CLR_GPA_RANGE_ENC_ACCESS);
}static inline kvm_hypercall_map_private(uint64_t gpa, uint64_t size)
{
kvm_hypercall_map_gpa_range(gpa, size, KVM_MARK_GPA_RANGE_ENC_ACCESS);
}tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 155
Quoted 2 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +2
-0+static void handle_vm_exit_map_gpa_hypercall (struct kvm_vm *vm, +volatile struct kvm_run *run)
Pass in @vcpu, not a vm+run.
Quoted 11 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +11
-0+{ +uint64_t gpa, npages, attrs, size; + +TEST_ASSERT (run->hypercall.nr ==KVM_HC_MAP_GPA_RANGE , +"Unhandled Hypercall %lld\n" , run->hypercall.nr); + gpa = run->hypercall.args[0 ]; + npages = run->hypercall.args[1 ]; + size = npages <<MIN_PAGE_SHIFT ; + attrs = run->hypercall.args[2 ]; +pr_info ("Explicit conversion off 0x%lx size 0x%lx to %s\n" , gpa, size, + (attrs &KVM_MAP_GPA_RANGE_ENCRYPTED ) ?"private" :"shared" );
tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 176
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +10
-0+ +if (attrs &KVM_MAP_GPA_RANGE_ENCRYPTED ) +vm_update_private_mem (vm, gpa, size,ALLOCATE_MEM ); +else +vm_update_private_mem (vm, gpa, size,UNBACK_MEM ); + + run->hypercall.ret =0 ; +} + +static void handle_vm_exit_memory_error (struct kvm_vm *vm,volatile struct kvm_run *run)
Same here, take a @vcpu.
Re: [RFC V3 PATCH 5/6] selftests: kvm: x86: Execute VMs with private memory
Quoted 88 lines · Sean Christopherson
Quoted 9 lines · Vishal Annapurve
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +9
-0+/* +* Execute KVM hypercall to change memory access type for a given gpa range. +* +* Input Args: +* type - memory conversion type TO_SHARED/TO_PRIVATE +* gpa - starting gpa address +* size - size of the range starting from gpa for which memory access needs +* to be changed +* tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 44
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +10
-0+* Output Args: None +* +* Return: None +* +* Function called by guest logic in selftests to update the memory access type +* for a given gpa range. This API is useful in exercising implicit conversion +* path. +*/ +void guest_update_mem_access (enum mem_conversion_type type,uint64_t gpa, +uint64_t size)Provide wrappers to self-document what's going on, then the massive block comments go away. And the guts of this and
guest_update_mem_map()are nearly identical.Hmm, and we probably want to make it possible to do negative testing.
Then the one-off enums for
TO_PRIVATEand whatnot go way too.Review
Quoted 2 lines
(quoted diff) +2
-0+{ + int ret = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> MIN_PAGE_SHIFT,Needs an assert that @size is page aligned. And since these are x86-64 specific, just use
PAGE_SHIFT. Huh,IS_ALIGNED()doesn't exist in selftests. That should be added, either by pulling inalign.hor by adding the generic macros tokvm_util_base.h.And then x86-64's
processor.hcan definedIS_PAGE_ALIGNED().E.g.
static inline void __kvm_hypercall_map_gpa_range(uint64_t gpa,
uint64_tsize,uint64_t flags) { return = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> PAGE_SHIFT, flags, 0); }static inline void kvm_hypercall_map_gpa_range(uint64_t gpa,
uint64_tsize,uint64_t flags) { int ret; GUEST_ASSERT_2(IS_PAGE_ALIGNED(gpa) && IS_PAGE_ALIGNED(size), gpa, size); ret = __kvm_hypercall_map_gpa_range(gpa, size, flags); GUEST_ASSERT_1(!ret, ret); }static inline
kvm_hypercall_map_shared(uint64_t gpa, uint64_t size){ kvm_hypercall_map_gpa_range(gpa, size, KVM_CLR_GPA_RANGE_ENC_ACCESS); }static inline
kvm_hypercall_map_private(uint64_t gpa, uint64_t size){ kvm_hypercall_map_gpa_range(gpa, size, KVM_MARK_GPA_RANGE_ENC_ACCESS); }tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 155
Quoted 2 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +2
-0+static void handle_vm_exit_map_gpa_hypercall (struct kvm_vm *vm, +volatile struct kvm_run *run)Pass in @vcpu, not a vm+run.
Quoted 15 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +15
-0+{ +uint64_t gpa, npages, attrs, size; + +TEST_ASSERT (run->hypercall.nr ==KVM_HC_MAP_GPA_RANGE , +"Unhandled Hypercall %lld\n" , run->hypercall.nr); + gpa = run->hypercall.args[0 ]; + npages = run->hypercall.args[1 ]; + size = npages <<MIN_PAGE_SHIFT ; + attrs = run->hypercall.args[2 ]; +pr_info ("Explicit conversion off 0x%lx size 0x%lx to %s\n" , gpa, size, + (attrs &KVM_MAP_GPA_RANGE_ENCRYPTED ) ?"private" :"shared" ); + +if (attrs &KVM_MAP_GPA_RANGE_ENCRYPTED ) +vm_update_private_mem (vm, gpa, size,ALLOCATE_MEM ); +else
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c line 176
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86_64/private_mem.c new file +6
-0+vm_update_private_mem (vm, gpa, size,UNBACK_MEM ); + + run->hypercall.ret =0 ; +} + +static void handle_vm_exit_memory_error (struct kvm_vm *vm,volatile struct kvm_run *run)Same here, take a @vcpu.
Ack. Will address these comments in the next series.
RFC PATCH v3 6/6 sefltests: kvm: x86: Add selftest for private memory
In reply to Vishal AnnapurveAdd a selftest to exercise implicit/explicit conversion functionality within KVM and verify:
1) Shared memory is visible to host userspace after conversion
2) Private memory is not visible to host userspace before/after conversion
3) Host userspace and guest can communicate over shared memory
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/private_mem_test.c | 262 ++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -31,6 +31,7 @@ /x86_64/monitor_mwait_test /x86_64/nx_huge_pages_test /x86_64/platform_info_test +/x86_64/private_mem_test /x86_64/pmu_event_filter_test /x86_64/set_boot_cpu_id /x86_64/set_sregs_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -94,6 +94,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_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_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/smm_test
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +262 -0
262 lines not on this page · raw message
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
maintainerRe: [RFC V3 PATCH 6/6] sefltests: kvm: x86: Add selftest for private memory
Quoted 1 line · Vishal Annapurve
+static bool
verify_mem_contents(void *mem, uint32_t size, uint8_t pat)
As per feedback in v1[*], spell out "pattern".
[*] https://lore.kernel.org/all/[email protected]
Quoted 15 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +15
-0+{ +uint8_t *buf = (uint8_t *)mem; + +for (uint32_t i =0 ; i < size; i++) { +if (buf[i] != pat) +return false; + } + +return true; +} + +/* +* Add custom implementation for memset to avoid using standard/builtin memset +* which may use features like SSE/GOT that don't work with guest vm execution +* within selftests.
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 68
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +10
-0+*/ +void *memset (void *mem,int byte,size_t size) +{ +uint8_t *buf = (uint8_t *)mem; + +for (uint32_t i =0 ; i < size; i++) + buf[i] = byte; + +return buf; +}
memset(), memcpy(), and memcmp() are safe to use as of commit 6b6f71484bf4 ("KVM:
selftests: Implement memcmp(), memcpy(), and memset() for guest use").
Note the "fun" with gcc "optimizing" into infinite recursion... :-)
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 81
Quoted 13 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +13
-0+ +static void populate_test_area (void *test_area_base,uint64_t pat) +{ +memset (test_area_base, pat,TEST_AREA_SIZE ); +} + +static void populate_guest_test_mem (void *guest_test_mem,uint64_t pat) +{ +memset (guest_test_mem, pat,GUEST_TEST_MEM_SIZE ); +} + +static bool verify_test_area (void *test_area_base,uint64_t area_pat, +uint64_t guest_pat)
Again, avoid "pat".
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 89
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +8
-0+{ +void *test_area1_base = test_area_base; +uint64_t test_area1_size =GUEST_TEST_MEM_OFFSET ; +void *guest_test_mem = test_area_base + test_area1_size; +uint64_t guest_test_size =GUEST_TEST_MEM_SIZE ; +void *test_area2_base = guest_test_mem + guest_test_size; +uint64_t test_area2_size = (TEST_AREA_SIZE - (GUEST_TEST_MEM_OFFSET + +GUEST_TEST_MEM_SIZE ));
This is all amazingly hard to read. AFAICT, the local variables are largely useless. Actually, why even take in @test_area_base, isn't it hardcoded to TEST_AREA_GPA? Then everything except the pattern can be hardcoded.
Quoted 4 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +4
-0+return (verify_mem_contents (test_area1_base, test_area1_size, area_pat) && +verify_mem_contents (guest_test_mem, guest_test_size, guest_pat) && +verify_mem_contents (test_area2_base, test_area2_size, area_pat)); +}
Re: [RFC V3 PATCH 6/6] sefltests: kvm: x86: Add selftest for private memory
Quoted 28 lines · Sean Christopherson
Quoted 1 line · Vishal Annapurve
+static bool
verify_mem_contents(void *mem, uint32_t size, uint8_t pat)As per feedback in v1[*], spell out "pattern".
[*] https://lore.kernel.org/all/[email protected]
Quoted 20 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +20
-0+{ +uint8_t *buf = (uint8_t *)mem; + +for (uint32_t i =0 ; i < size; i++) { +if (buf[i] != pat) +return false; + } + +return true; +} + +/* +* Add custom implementation for memset to avoid using standard/builtin memset +* which may use features like SSE/GOT that don't work with guest vm execution +* within selftests. +*/ +void *memset (void *mem,int byte,size_t size) +{ +uint8_t *buf = (uint8_t *)mem; +
Quoted 9 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 68
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +5
-0+for (uint32_t i =0 ; i < size; i++) + buf[i] = byte; + +return buf; +}
memset(),memcpy(), andmemcmp()are safe to use as of commit6b6f71484bf4("KVM:
selftests: Implementmemcmp(),memcpy(), andmemset()for guest use").
This is much better. It made less sense to add a custom memset for a single selftest.
Quoted 27 lines
Note the "fun" with gcc "optimizing" into infinite recursion... :-)
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 81
Quoted 13 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +13
-0+ +static void populate_test_area (void *test_area_base,uint64_t pat) +{ +memset (test_area_base, pat,TEST_AREA_SIZE ); +} + +static void populate_guest_test_mem (void *guest_test_mem,uint64_t pat) +{ +memset (guest_test_mem, pat,GUEST_TEST_MEM_SIZE ); +} + +static bool verify_test_area (void *test_area_base,uint64_t area_pat, +uint64_t guest_pat)Again, avoid "pat".
tools/testing/selftests/kvm/x86_64/private_mem_test.c line 89
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +8
-0+{ +void *test_area1_base = test_area_base; +uint64_t test_area1_size =GUEST_TEST_MEM_OFFSET ; +void *guest_test_mem = test_area_base + test_area1_size; +uint64_t guest_test_size =GUEST_TEST_MEM_SIZE ; +void *test_area2_base = guest_test_mem + guest_test_size; +uint64_t test_area2_size = (TEST_AREA_SIZE - (GUEST_TEST_MEM_OFFSET + +GUEST_TEST_MEM_SIZE ));
Quoted 8 lines
This is all amazingly hard to read. AFAICT, the local variables are largely useless. Actually, why even take in @test_area_base, isn't it hardcoded to
TEST_AREA_GPA? Then everything except the pattern can be hardcoded.Quoted 4 lines
tools/testing/selftests/kvm/x86_64/private_mem_test.c new file +4
-0+return (verify_mem_contents (test_area1_base, test_area1_size, area_pat) && +verify_mem_contents (guest_test_mem, guest_test_size, guest_pat) && +verify_mem_contents (test_area2_base, test_area2_size, area_pat)); +}
Ack. Will address these comments in the next series.