Skip to content

Commit ae895cb

Browse files
mmhalsean-jc
authored andcommitted
KVM: selftests: Extend x86's sync_regs_test to check for CR4 races
Attempt to modify vcpu->run->s.regs _after_ the sanity checks performed by KVM_CAP_SYNC_REGS's arch/x86/kvm/x86.c:sync_regs(). This can lead to some nonsensical vCPU states accompanied by kernel splats, e.g. disabling PAE while long mode is enabled makes KVM all kinds of confused: WARNING: CPU: 0 PID: 1142 at arch/x86/kvm/mmu/paging_tmpl.h:358 paging32_walk_addr_generic+0x431/0x8f0 [kvm] arch/x86/kvm/mmu/paging_tmpl.h: KVM_BUG_ON(is_long_mode(vcpu) && !is_pae(vcpu), vcpu->kvm) Signed-off-by: Michal Luczaj <mhal@rbox.co> Link: https://lore.kernel.org/r/20230728001606.2275586-3-mhal@rbox.co [sean: see link] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 0d03377 commit ae895cb

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tools/testing/selftests/kvm/x86_64/sync_regs_test.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717
#include <sys/ioctl.h>
18+
#include <pthread.h>
1819

1920
#include "test_util.h"
2021
#include "kvm_util.h"
@@ -80,6 +81,75 @@ static void compare_vcpu_events(struct kvm_vcpu_events *left,
8081
#define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
8182
#define INVALID_SYNC_FIELD 0x80000000
8283

84+
/*
85+
* Toggle CR4.PAE while KVM is processing SREGS, EFER.LME=1 with CR4.PAE=0 is
86+
* illegal, and KVM's MMU heavily relies on vCPU state being valid.
87+
*/
88+
static noinline void *race_sregs_cr4(void *arg)
89+
{
90+
struct kvm_run *run = (struct kvm_run *)arg;
91+
__u64 *cr4 = &run->s.regs.sregs.cr4;
92+
__u64 pae_enabled = *cr4;
93+
__u64 pae_disabled = *cr4 & ~X86_CR4_PAE;
94+
95+
for (;;) {
96+
WRITE_ONCE(run->kvm_dirty_regs, KVM_SYNC_X86_SREGS);
97+
WRITE_ONCE(*cr4, pae_enabled);
98+
asm volatile(".rept 512\n\t"
99+
"nop\n\t"
100+
".endr");
101+
WRITE_ONCE(*cr4, pae_disabled);
102+
103+
pthread_testcancel();
104+
}
105+
106+
return NULL;
107+
}
108+
109+
static void race_sync_regs(void *racer)
110+
{
111+
const time_t TIMEOUT = 2; /* seconds, roughly */
112+
struct kvm_translation tr;
113+
struct kvm_vcpu *vcpu;
114+
struct kvm_run *run;
115+
struct kvm_vm *vm;
116+
pthread_t thread;
117+
time_t t;
118+
119+
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
120+
run = vcpu->run;
121+
122+
run->kvm_valid_regs = KVM_SYNC_X86_SREGS;
123+
vcpu_run(vcpu);
124+
run->kvm_valid_regs = 0;
125+
126+
/*
127+
* Selftests run 64-bit guests by default, both EFER.LME and CR4.PAE
128+
* should already be set in guest state.
129+
*/
130+
TEST_ASSERT((run->s.regs.sregs.cr4 & X86_CR4_PAE) &&
131+
(run->s.regs.sregs.efer & EFER_LME),
132+
"vCPU should be in long mode, CR4.PAE=%d, EFER.LME=%d",
133+
!!(run->s.regs.sregs.cr4 & X86_CR4_PAE),
134+
!!(run->s.regs.sregs.efer & EFER_LME));
135+
136+
ASSERT_EQ(pthread_create(&thread, NULL, racer, (void *)run), 0);
137+
138+
for (t = time(NULL) + TIMEOUT; time(NULL) < t;) {
139+
__vcpu_run(vcpu);
140+
141+
if (racer == race_sregs_cr4) {
142+
tr = (struct kvm_translation) { .linear_address = 0 };
143+
__vcpu_ioctl(vcpu, KVM_TRANSLATE, &tr);
144+
}
145+
}
146+
147+
ASSERT_EQ(pthread_cancel(thread), 0);
148+
ASSERT_EQ(pthread_join(thread, NULL), 0);
149+
150+
kvm_vm_free(vm);
151+
}
152+
83153
int main(int argc, char *argv[])
84154
{
85155
struct kvm_vcpu *vcpu;
@@ -218,5 +288,7 @@ int main(int argc, char *argv[])
218288

219289
kvm_vm_free(vm);
220290

291+
race_sync_regs(race_sregs_cr4);
292+
221293
return 0;
222294
}

0 commit comments

Comments
 (0)