Skip to content

Commit 77bcd9e

Browse files
committed
KVM: Add dedicated arch hook for querying if vCPU was preempted in-kernel
Plumb in a dedicated hook for querying whether or not a vCPU was preempted in-kernel. Unlike literally every other architecture, x86's VMX can check if a vCPU is in kernel context if and only if the vCPU is loaded on the current pCPU. x86's kvm_arch_vcpu_in_kernel() works around the limitation by querying kvm_get_running_vcpu() and redirecting to vcpu->arch.preempted_in_kernel as needed. But that's unnecessary, confusing, and fragile, e.g. x86 has had at least one bug where KVM incorrectly used a stale preempted_in_kernel. No functional change intended. Reviewed-by: Yuan Yao <yuan.yao@intel.com> Link: https://lore.kernel.org/r/20240110003938.490206-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent fc3c941 commit 77bcd9e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

arch/x86/kvm/x86.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13054,6 +13054,11 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
1305413054
return false;
1305513055
}
1305613056

13057+
bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu)
13058+
{
13059+
return kvm_arch_vcpu_in_kernel(vcpu);
13060+
}
13061+
1305713062
bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
1305813063
{
1305913064
if (READ_ONCE(vcpu->arch.pv.pv_unhalted))

include/linux/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,7 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);
15051505
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
15061506
bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu);
15071507
bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);
1508+
bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu);
15081509
int kvm_arch_post_init_vm(struct kvm *kvm);
15091510
void kvm_arch_pre_destroy_vm(struct kvm *kvm);
15101511
int kvm_arch_create_vm_debugfs(struct kvm *kvm);

virt/kvm/kvm_main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4042,6 +4042,18 @@ static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu)
40424042
return false;
40434043
}
40444044

4045+
/*
4046+
* By default, simply query the target vCPU's current mode when checking if a
4047+
* vCPU was preempted in kernel mode. All architectures except x86 (or more
4048+
* specifical, except VMX) allow querying whether or not a vCPU is in kernel
4049+
* mode even if the vCPU is NOT loaded, i.e. using kvm_arch_vcpu_in_kernel()
4050+
* directly for cross-vCPU checks is functionally correct and accurate.
4051+
*/
4052+
bool __weak kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu)
4053+
{
4054+
return kvm_arch_vcpu_in_kernel(vcpu);
4055+
}
4056+
40454057
bool __weak kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu)
40464058
{
40474059
return false;
@@ -4080,7 +4092,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
40804092
continue;
40814093
if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
40824094
!kvm_arch_dy_has_pending_interrupt(vcpu) &&
4083-
!kvm_arch_vcpu_in_kernel(vcpu))
4095+
!kvm_arch_vcpu_preempted_in_kernel(vcpu))
40844096
continue;
40854097
if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
40864098
continue;

0 commit comments

Comments
 (0)