Skip to content

Commit 80b7859

Browse files
committed
KVM: selftests: Add a read-only mprotect() phase to mmu_stress_test
Add a third phase of mmu_stress_test to verify that mprotect()ing guest memory to make it read-only doesn't cause explosions, e.g. to verify KVM correctly handles the resulting mmu_notifier invalidations. Reviewed-by: James Houghton <jthoughton@google.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Link: https://lore.kernel.org/r/20241128005547.4077116-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 82b542e commit 80b7859

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tools/testing/selftests/kvm/mmu_stress_test.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
2828
GUEST_SYNC(i);
2929
}
3030

31+
for (gpa = start_gpa; gpa < end_gpa; gpa += stride)
32+
*((volatile uint64_t *)gpa);
33+
GUEST_SYNC(2);
34+
3135
GUEST_ASSERT(0);
3236
}
3337

@@ -95,6 +99,10 @@ static void *vcpu_worker(void *data)
9599
run_vcpu(vcpu, 1);
96100
rendezvous_with_boss();
97101

102+
/* Stage 2, read all of guest memory, which is now read-only. */
103+
run_vcpu(vcpu, 2);
104+
rendezvous_with_boss();
105+
98106
return NULL;
99107
}
100108

@@ -175,7 +183,7 @@ int main(int argc, char *argv[])
175183
const uint64_t start_gpa = SZ_4G;
176184
const int first_slot = 1;
177185

178-
struct timespec time_start, time_run1, time_reset, time_run2;
186+
struct timespec time_start, time_run1, time_reset, time_run2, time_ro;
179187
uint64_t max_gpa, gpa, slot_size, max_mem, i;
180188
int max_slots, slot, opt, fd;
181189
bool hugepages = false;
@@ -279,14 +287,20 @@ int main(int argc, char *argv[])
279287
rendezvous_with_vcpus(&time_reset, "reset");
280288
rendezvous_with_vcpus(&time_run2, "run 2");
281289

290+
mprotect(mem, slot_size, PROT_READ);
291+
rendezvous_with_vcpus(&time_ro, "mprotect RO");
292+
293+
time_ro = timespec_sub(time_ro, time_run2);
282294
time_run2 = timespec_sub(time_run2, time_reset);
283-
time_reset = timespec_sub(time_reset, time_run1);
295+
time_reset = timespec_sub(time_reset, time_run1);
284296
time_run1 = timespec_sub(time_run1, time_start);
285297

286-
pr_info("run1 = %ld.%.9lds, reset = %ld.%.9lds, run2 = %ld.%.9lds\n",
298+
pr_info("run1 = %ld.%.9lds, reset = %ld.%.9lds, run2 = %ld.%.9lds, "
299+
"ro = %ld.%.9lds\n",
287300
time_run1.tv_sec, time_run1.tv_nsec,
288301
time_reset.tv_sec, time_reset.tv_nsec,
289-
time_run2.tv_sec, time_run2.tv_nsec);
302+
time_run2.tv_sec, time_run2.tv_nsec,
303+
time_ro.tv_sec, time_ro.tv_nsec);
290304

291305
/*
292306
* Delete even numbered slots (arbitrary) and unmap the first half of

0 commit comments

Comments
 (0)