Skip to content

Commit d88ed5f

Browse files
committed
KVM: selftests: Ensure all vCPUs hit -EFAULT during initial RO stage
During the initial mprotect(RO) stage of mmu_stress_test, keep vCPUs spinning until all vCPUs have hit -EFAULT, i.e. until all vCPUs have tried to write to a read-only page. If a vCPU manages to complete an entire iteration of the loop without hitting a read-only page, *and* the vCPU observes mprotect_ro_done before starting a second iteration, then the vCPU will prematurely fall through to GUEST_SYNC(3) (on x86 and arm64) and get out of sequence. Replace the "do-while (!r)" loop around the associated _vcpu_run() with a single invocation, as barring a KVM bug, the vCPU is guaranteed to hit -EFAULT, and retrying on success is super confusion, hides KVM bugs, and complicates this fix. The do-while loop was semi-unintentionally added specifically to fudge around a KVM x86 bug, and said bug is unhittable without modifying the test to force x86 down the !(x86||arm64) path. On x86, if forced emulation is enabled, vcpu_arch_put_guest() may trigger emulation of the store to memory. Due a (very, very) longstanding bug in KVM x86's emulator, emulate writes to guest memory that fail during __kvm_write_guest_page() unconditionally return KVM_EXIT_MMIO. While that is desirable in the !memslot case, it's wrong in this case as the failure happens due to __copy_to_user() hitting a read-only page, not an emulated MMIO region. But as above, x86 only uses vcpu_arch_put_guest() if the __x86_64__ guards are clobbered to force x86 down the common path, and of course the unexpected MMIO is a KVM bug, i.e. *should* cause a test failure. Fixes: b6c304a ("KVM: selftests: Verify KVM correctly handles mprotect(PROT_READ)") Reported-by: Yan Zhao <yan.y.zhao@intel.com> Closes: https://lore.kernel.org/all/20250208105318.16861-1-yan.y.zhao@intel.com Debugged-by: Yan Zhao <yan.y.zhao@intel.com> Reviewed-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://lore.kernel.org/r/20250228230804.3845860-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 807cb9c commit d88ed5f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

tools/testing/selftests/kvm/mmu_stress_test.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ucall_common.h"
1919

2020
static bool mprotect_ro_done;
21+
static bool all_vcpus_hit_ro_fault;
2122

2223
static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
2324
{
@@ -36,9 +37,9 @@ static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
3637

3738
/*
3839
* Write to the region while mprotect(PROT_READ) is underway. Keep
39-
* looping until the memory is guaranteed to be read-only, otherwise
40-
* vCPUs may complete their writes and advance to the next stage
41-
* prematurely.
40+
* looping until the memory is guaranteed to be read-only and a fault
41+
* has occurred, otherwise vCPUs may complete their writes and advance
42+
* to the next stage prematurely.
4243
*
4344
* For architectures that support skipping the faulting instruction,
4445
* generate the store via inline assembly to ensure the exact length
@@ -56,7 +57,7 @@ static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
5657
#else
5758
vcpu_arch_put_guest(*((volatile uint64_t *)gpa), gpa);
5859
#endif
59-
} while (!READ_ONCE(mprotect_ro_done));
60+
} while (!READ_ONCE(mprotect_ro_done) || !READ_ONCE(all_vcpus_hit_ro_fault));
6061

6162
/*
6263
* Only architectures that write the entire range can explicitly sync,
@@ -81,6 +82,7 @@ struct vcpu_info {
8182

8283
static int nr_vcpus;
8384
static atomic_t rendezvous;
85+
static atomic_t nr_ro_faults;
8486

8587
static void rendezvous_with_boss(void)
8688
{
@@ -148,12 +150,16 @@ static void *vcpu_worker(void *data)
148150
* be stuck on the faulting instruction for other architectures. Go to
149151
* stage 3 without a rendezvous
150152
*/
151-
do {
152-
r = _vcpu_run(vcpu);
153-
} while (!r);
153+
r = _vcpu_run(vcpu);
154154
TEST_ASSERT(r == -1 && errno == EFAULT,
155155
"Expected EFAULT on write to RO memory, got r = %d, errno = %d", r, errno);
156156

157+
atomic_inc(&nr_ro_faults);
158+
if (atomic_read(&nr_ro_faults) == nr_vcpus) {
159+
WRITE_ONCE(all_vcpus_hit_ro_fault, true);
160+
sync_global_to_guest(vm, all_vcpus_hit_ro_fault);
161+
}
162+
157163
#if defined(__x86_64__) || defined(__aarch64__)
158164
/*
159165
* Verify *all* writes from the guest hit EFAULT due to the VMA now
@@ -378,7 +384,6 @@ int main(int argc, char *argv[])
378384
rendezvous_with_vcpus(&time_run2, "run 2");
379385

380386
mprotect(mem, slot_size, PROT_READ);
381-
usleep(10);
382387
mprotect_ro_done = true;
383388
sync_global_to_guest(vm, mprotect_ro_done);
384389

0 commit comments

Comments
 (0)