Skip to content

Commit 6cd8824

Browse files
committed
KVM: x86: do not report a vCPU as preempted outside instruction boundaries
If a vCPU is outside guest mode and is scheduled out, it might be in the process of making a memory access. A problem occurs if another vCPU uses the PV TLB flush feature during the period when the vCPU is scheduled out, and a virtual address has already been translated but has not yet been accessed, because this is equivalent to using a stale TLB entry. To avoid this, only report a vCPU as preempted if sure that the guest is at an instruction boundary. A rescheduling request will be delivered to the host physical CPU as an external interrupt, so for simplicity consider any vmexit *not* instruction boundary except for external interrupts. It would in principle be okay to report the vCPU as preempted also if it is sleeping in kvm_vcpu_block(): a TLB flush IPI will incur the vmentry/vmexit overhead unnecessarily, and optimistic spinning is also unlikely to succeed. However, leave it for later because right now kvm_vcpu_check_block() is doing memory accesses. Even though the TLB flush issue only applies to virtual memory address, it's very much preferrable to be conservative. Reported-by: Jann Horn <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 54aa83c commit 6cd8824

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ struct kvm_vcpu_arch {
653653
u64 ia32_misc_enable_msr;
654654
u64 smbase;
655655
u64 smi_count;
656+
bool at_instruction_boundary;
656657
bool tpr_access_reporting;
657658
bool xsaves_enabled;
658659
bool xfd_no_write_intercept;
@@ -1300,6 +1301,8 @@ struct kvm_vcpu_stat {
13001301
u64 nested_run;
13011302
u64 directed_yield_attempted;
13021303
u64 directed_yield_successful;
1304+
u64 preemption_reported;
1305+
u64 preemption_other;
13031306
u64 guest_mode;
13041307
};
13051308

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,6 +4263,8 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
42634263

42644264
static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
42654265
{
4266+
if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
4267+
vcpu->arch.at_instruction_boundary = true;
42664268
}
42674269

42684270
static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6547,6 +6547,7 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
65476547
return;
65486548

65496549
handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc));
6550+
vcpu->arch.at_instruction_boundary = true;
65506551
}
65516552

65526553
static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)

arch/x86/kvm/x86.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
296296
STATS_DESC_COUNTER(VCPU, nested_run),
297297
STATS_DESC_COUNTER(VCPU, directed_yield_attempted),
298298
STATS_DESC_COUNTER(VCPU, directed_yield_successful),
299+
STATS_DESC_COUNTER(VCPU, preemption_reported),
300+
STATS_DESC_COUNTER(VCPU, preemption_other),
299301
STATS_DESC_ICOUNTER(VCPU, guest_mode)
300302
};
301303

@@ -4625,6 +4627,19 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
46254627
struct kvm_memslots *slots;
46264628
static const u8 preempted = KVM_VCPU_PREEMPTED;
46274629

4630+
/*
4631+
* The vCPU can be marked preempted if and only if the VM-Exit was on
4632+
* an instruction boundary and will not trigger guest emulation of any
4633+
* kind (see vcpu_run). Vendor specific code controls (conservatively)
4634+
* when this is true, for example allowing the vCPU to be marked
4635+
* preempted if and only if the VM-Exit was due to a host interrupt.
4636+
*/
4637+
if (!vcpu->arch.at_instruction_boundary) {
4638+
vcpu->stat.preemption_other++;
4639+
return;
4640+
}
4641+
4642+
vcpu->stat.preemption_reported++;
46284643
if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
46294644
return;
46304645

@@ -10424,6 +10439,13 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
1042410439
vcpu->arch.l1tf_flush_l1d = true;
1042510440

1042610441
for (;;) {
10442+
/*
10443+
* If another guest vCPU requests a PV TLB flush in the middle
10444+
* of instruction emulation, the rest of the emulation could
10445+
* use a stale page translation. Assume that any code after
10446+
* this point can start executing an instruction.
10447+
*/
10448+
vcpu->arch.at_instruction_boundary = false;
1042710449
if (kvm_vcpu_running(vcpu)) {
1042810450
r = vcpu_enter_guest(vcpu);
1042910451
} else {

0 commit comments

Comments
 (0)