KVM: selftests: Fix nx_huge_pages_test when TDP is disabled
v3 cover KVM: selftests: Fix nx_huge_pages_test when TDP is disabled
Fix nx_huge_pages_test when run on TDP-disabled hosts by mapping the test region with huge pages in the VM so that KVM can shadow it with huge pages.
Patches 1 and 2 are precursor patches to add the necessary infrastructure to check if TDP is enabled or not.
v3:
- Skip the selftest if a module param file cannot be opened [Sean]
- Add wrappers for reading kvm_intel and kvm_amd params [Sean]
- Small renames and error reporting cleanups [Sean]
- Decrease sysfs path array from 1024 to 128 bytes [me]
v2: https://lore.kernel.org/kvm/[email protected]/
- Still use 4K mappins on TDP-enabled hosts [Sean]
- Generalize virt_map_2m() to virt_map_level() [me]
- Pass nr_bytes instead of nr_pages to virt_map_level() [Sean]
v1: https://lore.kernel.org/kvm/[email protected]/
David Matlack (3):
KVM: selftests: Tell the compiler that code after TEST_FAIL() is
unreachable
KVM: selftests: Add helpers to read kvm_{intel,amd} boolean module
parameters
KVM: selftests: Fix nx_huge_pages_test on TDP-disabled hosts
.../selftests/kvm/include/kvm_util_base.h | 4 ++ .../testing/selftests/kvm/include/test_util.h | 6 ++- .../selftests/kvm/include/x86_64/processor.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 39 ++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 40 +++++++++++++------ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 19 ++++++++- 6 files changed, 96 insertions(+), 16 deletions(-)
base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
prerequisite-patch-id: 2e3661ba8856c29b769499bac525b6943d9284b8
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
PATCH v3 1/3 KVM: selftests: Tell the compiler that code after TEST_FAIL() is unreachable
Add __builtin_unreachable() to TEST_FAIL() so that the compiler knows that any code after a TEST_FAIL() is unreachable.
--- tools/testing/selftests/kvm/include/test_util.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +4 -2
@@ -63,8 +63,10 @@ void test_assert(bool exp, const char *exp_str,#a , #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \ } while (0) -#define TEST_FAIL (fmt, ...) \ - TEST_ASSERT(false, fmt, ##__VA_ARGS__) +#define TEST_FAIL (fmt, ...) do { \ + TEST_ASSERT(false, fmt, ##__VA_ARGS__); \ + __builtin_unreachable(); \ +} while (0)size_t parse_size (const char *size);
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
PATCH v3 2/3 KVM: selftests: Add helpers to read kvm_{intel,amd} boolean module parameters
In reply to David MatlackAdd helper functions for reading the value of kvm_intel and kvm_amd boolean module parameters. Use the kvm_intel variant in vm_is_unrestricted_guest() to simplify the check for kvm_intel.unrestricted_guest.
No functional change intended.
--- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 39 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 13 +------ 3 files changed, 44 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +4 -0
@@ -175,6 +175,10 @@ extern const struct vm_guest_mode_params vm_guest_mode_params[];int open_path_or_exit (const char *path,int flags);int open_kvm_dev_path_or_exit (void ); + +bool get_kvm_intel_param_bool (const char *param); +bool get_kvm_amd_param_bool (const char *param); +unsigned int kvm_check_cap (long cap);static inline bool kvm_has_cap (long cap)
tools/testing/selftests/kvm/lib/kvm_util.c +39 -0
@@ -50,6 +50,45 @@ int open_kvm_dev_path_or_exit(void)return _open_kvm_dev_path_or_exit (O_RDONLY ); } +static bool get_module_param_bool (const char *module_name,const char *param) +{ +const int path_size =128 ; +char path[path_size]; +char value; +ssize_t r; +int fd; + + r =snprintf (path, path_size,"/sys/module/%s/parameters/%s" , + module_name, param); +TEST_ASSERT (r < path_size, +"Failed to construct sysfs path in %d bytes." , path_size); + + fd =open_path_or_exit (path,O_RDONLY ); + + r =read (fd, &value,1 ); +TEST_ASSERT (r ==1 ,"read(%s) failed" , path); + + r =close (fd); +TEST_ASSERT (!r,"close(%s) failed" , path); + +if (value =='Y' ) +return true; +else if (value =='N' ) +return false; + +TEST_FAIL ("Unrecognized value '%c' for boolean module param" , value); +} + +bool get_kvm_intel_param_bool (const char *param) +{ +return get_module_param_bool ("kvm_intel" , param); +} + +bool get_kvm_amd_param_bool (const char *param) +{ +return get_module_param_bool ("kvm_amd" , param); +} +/* * Capability *
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -12
@@ -1294,20 +1294,9 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)/* Returns true if kvm_intel was loaded with unrestricted_guest=1. */ bool vm_is_unrestricted_guest (struct kvm_vm *vm) { -char val ='N' ; -size_t count; -FILE *f; -/* Ensure that a KVM vendor-specific module is loaded. */ if (vm ==NULL )close (open_kvm_dev_path_or_exit ()); - f =fopen ("/sys/module/kvm_intel/parameters/unrestricted_guest" ,"r" ); -if (f) { - count =fread (&val,sizeof (char ),1 , f); -TEST_ASSERT (count ==1 ,"Unable to read from param file." ); -fclose (f); - } - -return val =='Y' ; +return get_kvm_intel_param_bool ("unrestricted_guest" ); }
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
PATCH v3 3/3 KVM: selftests: Fix nx_huge_pages_test on TDP-disabled hosts
In reply to David MatlackMap the test's huge page region with 2MiB virtual mappings when TDP is disabled so that KVM can shadow the region with huge pages. This fixes nx_huge_pages_test on hosts where TDP hardware support is disabled.
Purposely do not skip this test on TDP-disabled hosts. While we don't care about NX Huge Pages on TDP-disabled hosts from a security perspective, KVM does support it, and so we should test it.
For TDP-enabled hosts, continue mapping the region with 4KiB pages to ensure that KVM can map it with huge pages irrespective of the guest mappings.
--- .../selftests/kvm/include/x86_64/processor.h | 4 +++ .../selftests/kvm/lib/x86_64/processor.c | 27 +++++++++++++++++++ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 19 +++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -0
@@ -825,6 +825,8 @@ static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val)return kvm_asm_safe ("wrmsr" ,"a" (val &-1u ),"d" (val >>32 ),"c" (msr)); } +bool kvm_is_tdp_enabled (void ); +uint64_t vm_get_page_table_entry (struct kvm_vm *vm,struct kvm_vcpu *vcpu,uint64_t vaddr); void vm_set_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
@@ -855,6 +857,8 @@ enum pg_level {#define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G)void __virt_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr,int level); +void virt_map_level (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr, +uint64_t nr_bytes,int level);/* * Basic CPU control in CR0
tools/testing/selftests/kvm/lib/x86_64/processor.c +27 -0
@@ -111,6 +111,14 @@ static void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent) } } +bool kvm_is_tdp_enabled (void ) +{ +if (is_intel_cpu ()) +return get_kvm_intel_param_bool ("ept" ); +else +return get_kvm_amd_param_bool ("npt" ); +} +void virt_arch_pgd_alloc (struct kvm_vm *vm) { TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
@@ -214,6 +222,25 @@ void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)__virt_pg_map (vm, vaddr, paddr,PG_LEVEL_4K ); } +void virt_map_level (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr, +uint64_t nr_bytes,int level) +{ +uint64_t pg_size =PG_LEVEL_SIZE (level); +uint64_t nr_pages = nr_bytes / pg_size; +int i; + +TEST_ASSERT (nr_bytes % pg_size ==0 , +"Region size not aligned: nr_bytes: 0x%lx, page size: 0x%lx" , + nr_bytes, pg_size); + +for (i =0 ; i < nr_pages; i++) { +__virt_pg_map (vm, vaddr, paddr, level); + + vaddr += pg_size; + paddr += pg_size; + } +} +static uint64_t *_vm_get_page_table_entry (struct kvm_vm *vm,struct kvm_vcpu *vcpu,uint64_t vaddr)
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c +17 -2
@@ -112,6 +112,7 @@ void run_test(int reclaim_period_ms, bool disable_nx_huge_pages, {struct kvm_vcpu *vcpu;struct kvm_vm *vm; +uint64_t nr_bytes;void *hva;int r;
@@ -141,10 +142,24 @@ void run_test(int reclaim_period_ms, bool disable_nx_huge_pages,HPAGE_GPA ,HPAGE_SLOT , HPAGE_SLOT_NPAGES, 0); -virt_map (vm,HPAGE_GVA ,HPAGE_GPA ,HPAGE_SLOT_NPAGES ); + nr_bytes =HPAGE_SLOT_NPAGES * vm->page_size; + +/* +* Ensure that KVM can map HPAGE_SLOT with huge pages by mapping the +* region into the guest with 2MiB pages whenever TDP is disabled (i.e. +* whenever KVM is shadowing the guest page tables). +* +* When TDP is enabled, KVM should be able to map HPAGE_SLOT with huge +* pages irrespective of the guest page size, so map with 4KiB pages +* to test that that is the case. +*/ +if (kvm_is_tdp_enabled ()) +virt_map_level (vm,HPAGE_GVA ,HPAGE_GPA , nr_bytes,PG_LEVEL_4K ); +else +virt_map_level (vm,HPAGE_GVA ,HPAGE_GPA , nr_bytes,PG_LEVEL_2M ); hva =addr_gpa2hva (vm,HPAGE_GPA ); -memset (hva,RETURN_OPCODE ,HPAGE_SLOT_NPAGES *PAGE_SIZE ); +memset (hva,RETURN_OPCODE , nr_bytes);check_2m_page_count (vm,0 );check_split_count (vm,0 );
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Quoted 6 lines · David Matlack
Fix
nx_huge_pages_testwhen run on TDP-disabled hosts by mapping the test region with huge pages in the VM so that KVM can shadow it with huge pages.Patches 1 and 2 are precursor patches to add the necessary infrastructure to check if TDP is enabled or not.
Queued, thanks. For now I placed them in kvm/master, but I think they will not be in 6.0.
Paolo
Quoted 31 lines
v3:
- Skip the selftest if a module param file cannot be opened [Sean]
- Add wrappers for readingkvm_intelandkvm_amdparams [Sean]
- Small renames and error reporting cleanups [Sean]
- Decrease sysfs path array from 1024 to 128 bytes [me]v2: https://lore.kernel.org/kvm/[email protected]/
- Still use 4K mappins on TDP-enabled hosts [Sean]
- Generalizevirt_map_2m()tovirt_map_level()[me]
- Passnr_bytesinstead ofnr_pagestovirt_map_level()[Sean]v1: https://lore.kernel.org/kvm/[email protected]/
David Matlack (3):
KVM: selftests: Tell the compiler that code afterTEST_FAIL()is
unreachable
KVM: selftests: Add helpers to read kvm_{intel,amd} boolean module
parameters
KVM: selftests: Fixnx_huge_pages_teston TDP-disabled hosts.../selftests/kvm/include/kvm_util_base.h | 4 ++ .../testing/selftests/kvm/include/test_util.h | 6 ++- .../selftests/kvm/include/x86_64/processor.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 39 ++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 40 +++++++++++++------ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 19 ++++++++- 6 files changed, 96 insertions(+), 16 deletions(-)base-commit:
372d07084593dc7a399bf9bee815711b1fb1bcf2
prerequisite-patch-id:2e3661ba8856c29b769499bac525b6943d9284b8