Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Restore assert for non-nested VMs in access tracking test

Restore the assert (on x86-64) that <10% of pages are still idle when NOT
running as a nested VM in the access tracking test. The original assert
was converted to a "warning" to avoid false failures when running the
test in a VM, but the non-nested case does not suffer from the same
"infinite TLB size" issue.

Using the HYPERVISOR flag isn't infallible as VMMs aren't strictly
required to enumerate the "feature" in CPUID, but practically speaking
anyone that is running KVM selftests in VMs is going to be using a VMM
and hypervisor that sets the HYPERVISOR flag.

Cc: David Matlack <dmatlack@google.com>
Reviewed-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221129175300.4052283-3-seanjc@google.com

+13 -5
+12 -5
tools/testing/selftests/kvm/access_tracking_perf_test.c
··· 46 46 #include "test_util.h" 47 47 #include "memstress.h" 48 48 #include "guest_modes.h" 49 + #include "processor.h" 49 50 50 51 /* Global variable used to synchronize all of the vCPU threads. */ 51 52 static int iteration; ··· 181 180 * access tracking but low enough as to not make the test too brittle 182 181 * over time and across architectures. 183 182 * 184 - * Note that when run in nested virtualization, this check will trigger 185 - * much more frequently because TLB size is unlimited and since no flush 186 - * happens, much more pages are cached there and guest won't see the 187 - * "idle" bit cleared. 183 + * When running the guest as a nested VM, "warn" instead of asserting 184 + * as the TLB size is effectively unlimited and the KVM doesn't 185 + * explicitly flush the TLB when aging SPTEs. As a result, more pages 186 + * are cached and the guest won't see the "idle" bit cleared. 188 187 */ 189 - if (still_idle >= pages / 10) 188 + if (still_idle >= pages / 10) { 189 + #ifdef __x86_64__ 190 + TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR), 191 + "vCPU%d: Too many pages still idle (%lu out of %lu)", 192 + vcpu_idx, still_idle, pages); 193 + #endif 190 194 printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), " 191 195 "this will affect performance results.\n", 192 196 vcpu_idx, still_idle, pages); 197 + } 193 198 194 199 close(page_idle_fd); 195 200 close(pagemap_fd);
+1
tools/testing/selftests/kvm/include/x86_64/processor.h
··· 94 94 #define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26) 95 95 #define X86_FEATURE_OSXSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 27) 96 96 #define X86_FEATURE_RDRAND KVM_X86_CPU_FEATURE(0x1, 0, ECX, 30) 97 + #define X86_FEATURE_HYPERVISOR KVM_X86_CPU_FEATURE(0x1, 0, ECX, 31) 97 98 #define X86_FEATURE_PAE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 6) 98 99 #define X86_FEATURE_MCE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 7) 99 100 #define X86_FEATURE_APIC KVM_X86_CPU_FEATURE(0x1, 0, EDX, 9)