tools/testing/selftests/kvm/mmu_stress_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/mmu_stress_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/kvm/mmu_stress_test.c- Extension
.c- Size
- 12029 bytes
- Lines
- 426
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdlib.hpthread.hsemaphore.hsys/types.hsignal.herrno.hlinux/bitmap.hlinux/bitops.hlinux/atomic.hlinux/sizes.hkvm_util.htest_util.hguest_modes.hprocessor.hucall_common.h
Detected Declarations
struct vcpu_infofunction guest_codefunction rendezvous_with_bossfunction assert_sync_stagefunction run_vcpufunction rendezvous_with_vcpusfunction calc_default_nr_vcpusfunction main
Annotated Snippet
struct vcpu_info {
struct kvm_vcpu *vcpu;
u64 start_gpa;
u64 end_gpa;
};
static int nr_vcpus;
static atomic_t rendezvous;
static atomic_t nr_ro_faults;
static void rendezvous_with_boss(void)
{
int orig = atomic_read(&rendezvous);
if (orig > 0) {
atomic_dec_and_test(&rendezvous);
while (atomic_read(&rendezvous) > 0)
cpu_relax();
} else {
atomic_inc(&rendezvous);
while (atomic_read(&rendezvous) < 0)
cpu_relax();
}
}
static void assert_sync_stage(struct kvm_vcpu *vcpu, int stage)
{
struct ucall uc;
TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC);
TEST_ASSERT_EQ(uc.args[1], stage);
}
static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
{
vcpu_run(vcpu);
assert_sync_stage(vcpu, stage);
}
static void *vcpu_worker(void *data)
{
struct kvm_sregs __maybe_unused sregs;
struct vcpu_info *info = data;
struct kvm_vcpu *vcpu = info->vcpu;
struct kvm_vm *vm = vcpu->vm;
int r;
vcpu_args_set(vcpu, 3, info->start_gpa, info->end_gpa, vm->page_size);
rendezvous_with_boss();
/* Stage 0, write all of guest memory. */
run_vcpu(vcpu, 0);
rendezvous_with_boss();
#ifdef __x86_64__
vcpu_sregs_get(vcpu, &sregs);
/* Toggle CR0.WP to trigger a MMU context reset. */
sregs.cr0 ^= X86_CR0_WP;
vcpu_sregs_set(vcpu, &sregs);
#endif
rendezvous_with_boss();
/* Stage 1, re-write all of guest memory. */
run_vcpu(vcpu, 1);
rendezvous_with_boss();
/* Stage 2, read all of guest memory, which is now read-only. */
run_vcpu(vcpu, 2);
/*
* Stage 3, write guest memory and verify KVM returns -EFAULT for once
* the mprotect(PROT_READ) lands. Only architectures that support
* validating *all* of guest memory sync for this stage, as vCPUs will
* be stuck on the faulting instruction for other architectures. Go to
* stage 3 without a rendezvous
*/
r = _vcpu_run(vcpu);
TEST_ASSERT(r == -1 && errno == EFAULT,
"Expected EFAULT on write to RO memory, got r = %d, errno = %d", r, errno);
atomic_inc(&nr_ro_faults);
if (atomic_read(&nr_ro_faults) == nr_vcpus) {
WRITE_ONCE(all_vcpus_hit_ro_fault, true);
sync_global_to_guest(vm, all_vcpus_hit_ro_fault);
}
#if defined(__x86_64__) || defined(__aarch64__)
/*
* Verify *all* writes from the guest hit EFAULT due to the VMA now
* being read-only. x86 and arm64 only at this time as skipping the
Annotation
- Immediate include surface: `stdio.h`, `stdlib.h`, `pthread.h`, `semaphore.h`, `sys/types.h`, `signal.h`, `errno.h`, `linux/bitmap.h`.
- Detected declarations: `struct vcpu_info`, `function guest_code`, `function rendezvous_with_boss`, `function assert_sync_stage`, `function run_vcpu`, `function rendezvous_with_vcpus`, `function calc_default_nr_vcpus`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.