Skip to content

Commit 8e62bf2

Browse files
dwmw2sean-jc
authored andcommitted
KVM: x86/xen: inject vCPU upcall vector when local APIC is enabled
Linux guests since commit b1c3497 ("x86/xen: Add support for HVMOP_set_evtchn_upcall_vector") in v6.0 onwards will use the per-vCPU upcall vector when it's advertised in the Xen CPUID leaves. This upcall is injected through the guest's local APIC as an MSI, unlike the older system vector which was merely injected by the hypervisor any time the CPU was able to receive an interrupt and the upcall_pending flags is set in its vcpu_info. Effectively, that makes the per-CPU upcall edge triggered instead of level triggered, which results in the upcall being lost if the MSI is delivered when the local APIC is *disabled*. Xen checks the vcpu_info->evtchn_upcall_pending flag when the local APIC for a vCPU is software enabled (in fact, on any write to the SPIV register which doesn't disable the APIC). Do the same in KVM since KVM doesn't provide a way for userspace to intervene and trap accesses to the SPIV register of a local APIC emulated by KVM. Fixes: fde0451 ("KVM: x86/xen: Support per-vCPU event channel upcall via local APIC") Signed-off-by: David Woodhouse <dwmw@amazon.co.uk> Reviewed-by: Paul Durrant <paul@xen.org> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20240227115648.3104-3-dwmw2@infradead.org Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 451a707 commit 8e62bf2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

arch/x86/kvm/lapic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "ioapic.h"
4242
#include "trace.h"
4343
#include "x86.h"
44+
#include "xen.h"
4445
#include "cpuid.h"
4546
#include "hyperv.h"
4647
#include "smm.h"
@@ -499,8 +500,10 @@ static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
499500
}
500501

501502
/* Check if there are APF page ready requests pending */
502-
if (enabled)
503+
if (enabled) {
503504
kvm_make_request(KVM_REQ_APF_READY, apic->vcpu);
505+
kvm_xen_sw_enable_lapic(apic->vcpu);
506+
}
504507
}
505508

506509
static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)

arch/x86/kvm/xen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void kvm_xen_update_runstate(struct kvm_vcpu *v, int state)
567567
kvm_xen_update_runstate_guest(v, state == RUNSTATE_runnable);
568568
}
569569

570-
static void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v)
570+
void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v)
571571
{
572572
struct kvm_lapic_irq irq = { };
573573
int r;

arch/x86/kvm/xen.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern struct static_key_false_deferred kvm_xen_enabled;
1818

1919
int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
2020
void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu);
21+
void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *vcpu);
2122
int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
2223
int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
2324
int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
@@ -36,6 +37,19 @@ int kvm_xen_setup_evtchn(struct kvm *kvm,
3637
const struct kvm_irq_routing_entry *ue);
3738
void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu);
3839

40+
static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
41+
{
42+
/*
43+
* The local APIC is being enabled. If the per-vCPU upcall vector is
44+
* set and the vCPU's evtchn_upcall_pending flag is set, inject the
45+
* interrupt.
46+
*/
47+
if (static_branch_unlikely(&kvm_xen_enabled.key) &&
48+
vcpu->arch.xen.vcpu_info_cache.active &&
49+
vcpu->arch.xen.upcall_vector && __kvm_xen_has_interrupt(vcpu))
50+
kvm_xen_inject_vcpu_vector(vcpu);
51+
}
52+
3953
static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
4054
{
4155
return static_branch_unlikely(&kvm_xen_enabled.key) &&
@@ -101,6 +115,10 @@ static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
101115
{
102116
}
103117

118+
static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
119+
{
120+
}
121+
104122
static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
105123
{
106124
return false;

0 commit comments

Comments
 (0)