KVM: selftests: Fix nx_huge_pages_test when TDP is disabled
v2 cover KVM: selftests: Fix nx_huge_pages_test when TDP is disabled
This builds on the v1 fix for nx_huge_pages_test for TDP-disabled hosts. Originally this was just 1 patch but now it is 3 to add the necessary infrastructure to check if TDP is enabled or not so that we can conditionally use 4KiB or 2MiB mappings in the test.
Sean, I opted not to refactor virt_map() to take nr_bytes since that will take a bit more work than I have cycles for at the moment and I want to get this fix in. That being said, I coded up the new virt_map_level() using nr_bytes so that there will be less to clean up in the future.
v2:
- 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 helper to read boolean module parameters
KVM: selftests: Fix nx_huge_pages_test on TDP-disabled hosts
.../testing/selftests/kvm/include/test_util.h | 7 +++- .../selftests/kvm/include/x86_64/processor.h | 4 ++ tools/testing/selftests/kvm/lib/test_util.c | 31 ++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 40 +++++++++++++------ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 19 ++++++++- 5 files changed, 85 insertions(+), 16 deletions(-)
base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
prerequisite-patch-id: 2e3661ba8856c29b769499bac525b6943d9284b8
Signature · 1 line
2.37.3.998.g577e59143f-goog
PATCH v2 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.37.3.998.g577e59143f-goog
PATCH v2 2/3 KVM: selftests: Add helper to read boolean module parameters
In reply to David MatlackAdd a common helper function for reading boolean module parameters and use it in vm_is_unrestricted_guest() to check the value of kvm_intel.unrestricted_guest.
Note that vm_is_unrestricted_guest() will now fail with a TEST_ASSERT() if called on AMD instead of just returning false. However this should not cause any functional change since all of the callers of vm_is_unrestricted_guest() first check is_intel_cpu().
No functional change intended.
--- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 31 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 13 +------- 3 files changed, 33 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +1 -0
@@ -108,6 +108,7 @@ struct vm_mem_backing_src_alias {#define MIN_RUN_DELAY_NS 200000UL +bool get_module_param_bool (const char *module_name,const char *param);bool thp_configured (void );size_t get_trans_hugepagesz (void );size_t get_def_hugetlb_pagesz (void );
tools/testing/selftests/kvm/lib/test_util.c +31 -0
@@ -9,6 +9,7 @@#include <ctype.h> #include <limits.h> #include <stdlib.h> +#include <stdio.h> #include <time.h> #include <sys/stat.h> #include <sys/syscall.h>
@@ -114,6 +115,36 @@ void print_skip(const char *fmt, ...)puts (", skipping test" ); } +bool get_module_param_bool (const char *module_name,const char *param) +{ +const int path_size =1024 ; +char path[path_size]; +char value; +FILE *f; +int r; + + 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); + + f =fopen (path,"r" ); +TEST_ASSERT (f,"fopen(%s) failed" , path); + + r =fread (&value,1 ,1 , f); +TEST_ASSERT (r ==1 ,"fread(%s) failed" , path); + + r =fclose (f); +TEST_ASSERT (!r,"fclose(%s) failed" , path); + +if (value =='Y' ) +return true; +else if (value =='N' ) +return false; + +TEST_FAIL ("Unrecognized value: %c" , value); +} +bool thp_configured (void ) {int ret;
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_module_param_bool ("kvm_intel" ,"unrestricted_guest" ); }
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerRe: [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters
Quoted 8 lines · David Matlack
tools/testing/selftests/kvm/lib/test_util.c +4
-0@@ -114,6 +115,36 @@ void print_skip(const char *fmt, ...)puts (", skipping test" ); } +bool get_module_param_bool (const char *module_name,const char *param) +{ +const int path_size =1024 ; +char path[path_size];
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 131
Quoted 10 lines
tools/testing/selftests/kvm/lib/test_util.c +10
-0+char value; +FILE *f; +int r; + + 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); + + f =fopen (path,"r" );
Any particular reason for using fopen()? Oh, because that's what the existing code does. More below.
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 132
Quoted 1 line
tools/testing/selftests/kvm/lib/test_util.c +1
-0+TEST_ASSERT (f,"fopen(%s) failed" , path);
I don't actually care myself, but for consistency this should probably be a skip condition. The easiest thing would be to use open_path_or_exit().
At that point, assuming read() instead of fread() does the right thin, that seems like the easiest solution.
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 145
Quoted 1 line
tools/testing/selftests/kvm/lib/test_util.c +1
-0+TEST_FAIL ("Unrecognized value: %c" , value);
Maybe be slightly more verbose? E.g.
TEST_FAIL("Unrecognized value '%c' for boolean module param", value);Quoted 20 lines
tools/testing/selftests/kvm/lib/test_util.c +2
-0+} +bool thp_configured (void ) {int ret;tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-4@@ -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 ());
tools/testing/selftests/kvm/lib/x86_64/processor.c unsigned long vm_compute_max_gfn(struct kvm_vm *vm) · line 1301
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-8- 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_module_param_bool ("kvm_intel" ,"unrestricted_guest" );
Since there are only three possible modules, what about providing wrappers to handle "kvm", "kvm_amd", and "kvm_intel"? I'm guessing we'll end up with wrappers for each param we care about, but one fewer strings to get right would be nice.
Re: [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters
Quoted 9 lines · Sean Christopherson
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 120
Quoted 7 lines · David Matlack
tools/testing/selftests/kvm/lib/test_util.c +3
-0@@ -114,6 +115,36 @@ void print_skip(const char *fmt, ...)puts (", skipping test" ); } +bool get_module_param_bool (const char *module_name,const char *param) +{ +const int path_size =1024 ;
(Unrelated to your review but I noticed 1024 is overkill. e.g. "/sys/module/kvm_intel/parameters/error_on_inconsistent_vmcs_config" is only 67 characters. I will reduce this in v3.)
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/kvm/lib/test_util.c +6
-0+char path[path_size]; +char value; +FILE *f; +int r; + + r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
Quoted 8 lines
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 131
Quoted 5 lines
tools/testing/selftests/kvm/lib/test_util.c +5
-0+ module_name, param); +TEST_ASSERT (r < path_size, +"Failed to construct sysfs path in %d bytes." , path_size); + + f =fopen (path,"r" );Any particular reason for using
fopen()? Oh, because that's what the existing code does. More below.
Heh, yes. I write C code to do file I/O about once every 2 years so I always need to reference existing code :)
Quoted 8 lines
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 132
Quoted 2 lines
tools/testing/selftests/kvm/lib/test_util.c +1
-0+TEST_ASSERT (f,"fopen(%s) failed" , path);I don't actually care myself, but for consistency this should probably be a skip condition. The easiest thing would be to use
open_path_or_exit().At that point, assuming
read()instead offread()does the right thin, that seems like the easiest solution.
Fine by me!
Quoted 6 lines
tools/testing/selftests/kvm/lib/test_util.c void print_skip(const char *fmt, ...) · line 145
Quoted 2 lines
tools/testing/selftests/kvm/lib/test_util.c +1
-0+TEST_FAIL ("Unrecognized value: %c" , value);Maybe be slightly more verbose? E.g.
TEST_FAIL("Unrecognized value '%c' for boolean module param", value);
Will do.
Quoted 22 lines
Quoted 22 lines
tools/testing/selftests/kvm/lib/test_util.c +2
-0+} +bool thp_configured (void ) {int ret;tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-4@@ -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 ());
Quoted 13 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c unsigned long vm_compute_max_gfn(struct kvm_vm *vm) · line 1301
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-8- 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_module_param_bool ("kvm_intel" ,"unrestricted_guest" );Since there are only three possible modules, what about providing wrappers to handle "kvm", "
kvm_amd", and "kvm_intel"? I'm guessing we'll end up with wrappers for each param we care about, but one fewer strings to get right would be nice.
Will do.
PATCH v2 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_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_tdp_enabled (void ) +{ +if (is_amd_cpu ()) +return get_module_param_bool ("kvm_amd" ,"npt" ); +else +return get_module_param_bool ("kvm_intel" ,"ept" ); +} +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_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.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerRe: [PATCH v2 3/3] KVM: selftests: Fix nx_huge_pages_test on TDP-disabled hosts
tools/testing/selftests/kvm/include/x86_64/processor.h static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val) · line 828
Quoted 9 lines · David Matlack
tools/testing/selftests/kvm/include/x86_64/processor.h +1
-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_tdp_enabled (void );
Uber nit, maybe kvm_is_tdp_enabled()?