Skip to content

Commit 1876dd6

Browse files
committed
KVM: x86: Add fastpath handling of HLT VM-Exits
Add a fastpath for HLT VM-Exits by immediately re-entering the guest if it has a pending wake event. When virtual interrupt delivery is enabled, i.e. when KVM doesn't need to manually inject interrupts, this allows KVM to stay in the fastpath run loop when a vIRQ arrives between the guest doing CLI and STI;HLT. Without AMD's Idle HLT-intercept support, the CPU generates a HLT VM-Exit even though KVM will immediately resume the guest. Note, on bare metal, it's relatively uncommon for a modern guest kernel to actually trigger this scenario, as the window between the guest checking for a wake event and committing to HLT is quite small. But in a nested environment, the timings change significantly, e.g. rudimentary testing showed that ~50% of HLT exits where HLT-polling was successful would be serviced by this fastpath, i.e. ~50% of the time that a nested vCPU gets a wake event before KVM schedules out the vCPU, the wake event was pending even before the VM-Exit. Link: https://lore.kernel.org/all/20240528041926.3989-3-manali.shukla@amd.com Link: https://lore.kernel.org/r/20240802195120.325560-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 70cdd23 commit 1876dd6

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,12 +4147,21 @@ static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
41474147

41484148
static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
41494149
{
4150+
struct vcpu_svm *svm = to_svm(vcpu);
4151+
41504152
if (is_guest_mode(vcpu))
41514153
return EXIT_FASTPATH_NONE;
41524154

4153-
if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
4154-
to_svm(vcpu)->vmcb->control.exit_info_1)
4155+
switch (svm->vmcb->control.exit_code) {
4156+
case SVM_EXIT_MSR:
4157+
if (!svm->vmcb->control.exit_info_1)
4158+
break;
41554159
return handle_fastpath_set_msr_irqoff(vcpu);
4160+
case SVM_EXIT_HLT:
4161+
return handle_fastpath_hlt(vcpu);
4162+
default:
4163+
break;
4164+
}
41564165

41574166
return EXIT_FASTPATH_NONE;
41584167
}

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7265,6 +7265,8 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu,
72657265
return handle_fastpath_set_msr_irqoff(vcpu);
72667266
case EXIT_REASON_PREEMPTION_TIMER:
72677267
return handle_fastpath_preemption_timer(vcpu, force_immediate_exit);
7268+
case EXIT_REASON_HLT:
7269+
return handle_fastpath_hlt(vcpu);
72687270
default:
72697271
return EXIT_FASTPATH_NONE;
72707272
}

arch/x86/kvm/x86.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11363,7 +11363,10 @@ static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason)
1136311363
*/
1136411364
++vcpu->stat.halt_exits;
1136511365
if (lapic_in_kernel(vcpu)) {
11366-
vcpu->arch.mp_state = state;
11366+
if (kvm_vcpu_has_events(vcpu))
11367+
vcpu->arch.pv.pv_unhalted = false;
11368+
else
11369+
vcpu->arch.mp_state = state;
1136711370
return 1;
1136811371
} else {
1136911372
vcpu->run->exit_reason = reason;
@@ -11388,6 +11391,24 @@ int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1138811391
}
1138911392
EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1139011393

11394+
fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu)
11395+
{
11396+
int ret;
11397+
11398+
kvm_vcpu_srcu_read_lock(vcpu);
11399+
ret = kvm_emulate_halt(vcpu);
11400+
kvm_vcpu_srcu_read_unlock(vcpu);
11401+
11402+
if (!ret)
11403+
return EXIT_FASTPATH_EXIT_USERSPACE;
11404+
11405+
if (kvm_vcpu_running(vcpu))
11406+
return EXIT_FASTPATH_REENTER_GUEST;
11407+
11408+
return EXIT_FASTPATH_EXIT_HANDLED;
11409+
}
11410+
EXPORT_SYMBOL_GPL(handle_fastpath_hlt);
11411+
1139111412
int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu)
1139211413
{
1139311414
int ret = kvm_skip_emulated_instruction(vcpu);

arch/x86/kvm/x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
334334
int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
335335
int emulation_type, void *insn, int insn_len);
336336
fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
337+
fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu);
337338

338339
extern struct kvm_caps kvm_caps;
339340
extern struct kvm_host_values kvm_host;

0 commit comments

Comments
 (0)