KVM: Ensure WAIT requests actually wait
Sean Christopherson
maintainercover KVM: Ensure WAIT requests actually wait
Fix a bug in where kvm_make_request_and_kick() won't actually wait if the kick races without another non-waiting kick. The issue is that all of the conditional kvm_arch_vcpu_should_kick() implementations are missing this code:
if (req & KVM_REQUEST_WAIT) return mode != OUTSIDE_GUEST_MODE;
Thankfully, kvm_request_needs_ipi() does the right thing, so this doesn't affect kvm_make_all_cpus_request() or kvm_make_vcpus_request_mask(), i.e. it's limited to x86 SNP's used of kvm_make_request_and_kick().
To fix, address this TODO:
/* TODO: merge with kvm_arch_vcpu_should_kick */
and purge kvm_arch_vcpu_should_kick(). As usual, PPC makes that difficult because it has wonky code that is poorly documented. For reasons unknown, kvm_vcpu_kick() unconditionally returns true on (most flavors of) PPC. To preserve that behavior, add a compatibility wrapper in PPC, along with a @force param to __kvm_vcpu_kick().
This series would ideally go into 7.4, even though it's obviously not small, as guest_memfd in-place conversion massively widens the window where a missed kick+wait can lead to the host trying to reclaim an in-use VMSA page, which is generally fatal to the host.
Sean Christopherson (7):
KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
KVM: Add a @force param to __kvm_vcpu_kick() for PPC backwards
compatibility
KVM: PPC: Explicitly force IPIs when directly kicking vCPUs
KVM: MIPS: Rework kvm_arch_vcpu_should_kick() to use standard
implementation
KVM: Move kvm_arch_vcpu_should_kick() to common code as
kvm_vcpu_should_kick()
KVM: Plumb the full request into __kvm_vcpu_kick()
KVM: Drop kvm_vcpu_should_kick() and use kvm_request_needs_ipi()
arch/arm64/kvm/arm.c | 5 ----- arch/loongarch/kvm/vcpu.c | 5 ----- arch/mips/kvm/mips.c | 5 ----- arch/powerpc/include/asm/kvm_ppc.h | 13 ++++++++++++- arch/powerpc/kvm/book3s.c | 4 ++-- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/booke.c | 6 +++--- arch/powerpc/kvm/e500_emulate.c | 2 +- arch/powerpc/kvm/powerpc.c | 7 +------ arch/riscv/kvm/vcpu.c | 5 ----- arch/s390/kvm/s390/s390.c | 7 ------- arch/x86/kvm/x86.c | 5 ----- include/linux/kvm_host.h | 7 +++---- virt/kvm/kvm_main.c | 28 ++++++++++++++++------------ 14 files changed, 39 insertions(+), 62 deletions(-)
base-commit: 7e60162b16f9da0d2fa83da5c1723b6853a0ac1d
Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerPATCH 1/7 KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
Drop s390's kvm_arch_vcpu_should_kick() stub as the one and only call site is wrapped with "#ifndef CONFIG_S390".
No functional change intended.
--- arch/s390/kvm/s390/s390.c | 7 ------- 1 file changed, 7 deletions(-)
arch/s390/kvm/s390/s390.c +0 -7
@@ -3922,13 +3922,6 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)return false; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -/* kvm common code refers to this, but never calls it */ -BUG (); -return 0 ; -} -static int kvm_arch_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) {
Signature · 1 line
2.56.0.313.g2479322e68-goog
Steffen Eiden
reviewerRe: [PATCH 1/7] KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
Quoted 2 lines · Sean Christopherson
Drop s390's
kvm_arch_vcpu_should_kick()stub as the one and only call site is wrapped with "#ifndefCONFIG_S390".
In the somehow near future I'll remove those #ifdefs and use the kick stuff for kvm_arm64. The native s390 KVM then must at least keep an (unused) symbol at hand. It seems that the compiler could not optimize those calls away. So let's keep it there so I do not have to re-add it soon.
Steffen
Quoted 29 lines
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/s390/kvm/s390/s390.c | 7 ------- 1 file changed, 7 deletions(-)arch/s390/kvm/s390/s390.c +0
-7@@ -3922,13 +3922,6 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)return false; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -/* kvm common code refers to this, but never calls it */ -BUG (); -return 0 ; -} -static int kvm_arch_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) {Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerRe: [PATCH 1/7] KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
Quoted 9 lines · Steffen Eiden
Quoted 2 lines · Sean Christopherson
Drop s390's
kvm_arch_vcpu_should_kick()stub as the one and only call site is wrapped with "#ifndefCONFIG_S390".In the somehow near future I'll remove those #ifdefs and use the kick stuff for
kvm_arm64. The native s390 KVM then must at least keep an (unused) symbol at hand. It seems that the compiler could not optimize those calls away. So let's keep it there so I do not have to re-add it soon.
Please read the rest of the series. kvm_arch_vcpu_should_kick() goes away entirely.
Steffen Eiden
reviewerRe: [PATCH 1/7] KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden
Quoted 2 lines · Sean Christopherson
Drop s390's
kvm_arch_vcpu_should_kick()stub as the one and only call site is wrapped with "#ifndefCONFIG_S390".
Quoted 8 lines
Quoted 5 lines
In the somehow near future I'll remove those #ifdefs and use the kick stuff for
kvm_arm64. The native s390 KVM then must at least keep an (unused) symbol at hand. It seems that the compiler could not optimize those calls away. So let's keep it there so I do not have to re-add it soon.Please read the rest of the series.
kvm_arch_vcpu_should_kick()goes away entirely.
Sorry for that. This patch just jumped to my attention and I had no time yet to read the rest.
Steffen
Sean Christopherson
maintainerPATCH 2/7 KVM: Add a @force param to __kvm_vcpu_kick() for PPC backwards compatibility
In reply to Sean ChristophersonPlumb in a @force param to __kvm_vcpu_kick() to force an IPI without checking kvm_arch_vcpu_should_kick(). The param will be used to drop kvm_arch_vcpu_should_kick() in favor of kvm_request_needs_ipi() without regressing PPC, which for unknown reasons unconditionally blasts an IPI in a variety of kvm_vcpu_kick() flows.
No functional change intended (nothing yet passes in @force=true).
--- include/linux/kvm_host.h | 6 +++--- virt/kvm/kvm_main.c | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-)
include/linux/kvm_host.h +3 -3
@@ -1570,11 +1570,11 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);bool kvm_vcpu_wake_up (struct kvm_vcpu *vcpu);#ifndef CONFIG_S390 -void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait); +void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait,bool force);static inline void kvm_vcpu_kick (struct kvm_vcpu *vcpu) { -__kvm_vcpu_kick (vcpu, false); +__kvm_vcpu_kick (vcpu, false, false); }#endif
@@ -2315,7 +2315,7 @@ static __always_inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)static inline void kvm_make_request_and_kick (int req,struct kvm_vcpu *vcpu) {kvm_make_request (req, vcpu); -__kvm_vcpu_kick (vcpu, req &KVM_REQUEST_WAIT ); +__kvm_vcpu_kick (vcpu, req &KVM_REQUEST_WAIT , false); }#endif
virt/kvm/kvm_main.c +7 -3
@@ -3908,7 +3908,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_wake_up);/* * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. */ -void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait) +void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait,bool force) {int me, cpu;
@@ -3933,9 +3933,13 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait)* after kvm_arch_vcpu_should_kick(), which could result in sending an * IPI to the previous pCPU. But, that's ok because the purpose of the * IPI is to force the vCPU to leave IN_GUEST_MODE, and migrating the -* vCPU also requires it to leave IN_GUEST_MODE. +* vCPU also requires it to leave IN_GUEST_MODE +* +* Note #2, do NOT check if an IPI is needed if @force is true, as the +* check also modifies vcpu->mode and KVM PPC needs to avoid that side +* effect for unknown historical reasons. */ -if (kvm_arch_vcpu_should_kick (vcpu)) { +if (force ||kvm_arch_vcpu_should_kick (vcpu)) { cpu =READ_ONCE (vcpu->cpu);if (cpu != me && (unsigned int )cpu < nr_cpu_ids &&cpu_online (cpu)) {/*
Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerPATCH 3/7 KVM: PPC: Explicitly force IPIs when directly kicking vCPUs
In reply to Sean ChristophersonExplicitly force an IPI when invoking kvm_vcpu_kick() directly instead of relying on kvm_arch_vcpu_should_kick() to unconditionally return 1/true. This will allow dropping kvm_arch_vcpu_should_kick() in favor of kvm_request_needs_ipi().
No functional change intended (1 || 1 == true).
--- arch/powerpc/include/asm/kvm_ppc.h | 13 ++++++++++++- arch/powerpc/kvm/book3s.c | 4 ++-- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/booke.c | 6 +++--- arch/powerpc/kvm/e500_emulate.c | 2 +- arch/powerpc/kvm/powerpc.c | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +12 -1
@@ -30,6 +30,17 @@#endif #include <asm/inst.h> +/* +* TODO: For unknown reasons, KVM PPC has historically ignored vcpu->mode when +* determining if kicking a vCPU needs an IPI. Either document why that +* behavior is necessary, or drop this helper and the @force param from +* __kvm_vcpu_kick(). +*/ +static inline void kvmppc_vcpu_kick (struct kvm_vcpu *vcpu) +{ +__kvm_vcpu_kick (vcpu, false, true); +} +/* * KVMPPC_INST_SW_BREAKPOINT is debug Instruction * for supporting software breakpoint.
@@ -598,7 +609,7 @@ static inline void kvmppc_clear_host_ipi(int cpu)static inline void kvmppc_fast_vcpu_kick (struct kvm_vcpu *vcpu) { -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu); }static inline bool kvm_hv_mode_active (void ) {return false; }
arch/powerpc/kvm/book3s.c +2 -2
@@ -260,7 +260,7 @@ void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,* external interrupts for the guest just before entering * the guest. That is OK even if userspace did do a * KVM_INTERRUPT_SET on a pseries guest vcpu, because the -* caller (kvm_vcpu_ioctl_interrupt) does a kvm_vcpu_kick() +* caller (kvm_vcpu_ioctl_interrupt) does a kvmppc_vcpu_kick() * which ends up doing a smp_send_reschedule(), which will * pull the guest all the way out to the host, meaning that * we will call kvmppc_core_prepare_to_enter() before entering
@@ -827,7 +827,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,void kvmppc_decrementer_func (struct kvm_vcpu *vcpu) {kvmppc_core_queue_dec (vcpu); -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu); }int kvmppc_core_vcpu_create (struct kvm_vcpu *vcpu)
arch/powerpc/kvm/book3s_pr.c +1 -1
@@ -2066,7 +2066,7 @@ static struct kvmppc_ops kvm_ops_pr = { .emulate_op = kvmppc_core_emulate_op_pr, .emulate_mtspr = kvmppc_core_emulate_mtspr_pr, .emulate_mfspr = kvmppc_core_emulate_mfspr_pr, - .fast_vcpu_kick = kvm_vcpu_kick, + .fast_vcpu_kick = kvmppc_vcpu_kick, .arch_vm_ioctl = kvm_arch_vm_ioctl_pr, #ifdef CONFIG_PPC_BOOK3S_64 .hcall_implemented = kvmppc_hcall_impl_pr,
arch/powerpc/kvm/booke.c +3 -3
@@ -650,7 +650,7 @@ static void kvmppc_watchdog_func(struct timer_list *t)if (new_tsr &TSR_WIS ) {smp_wmb ();kvm_make_request (KVM_REQ_PENDING_TIMER , vcpu); -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu); }/*
@@ -661,7 +661,7 @@ static void kvmppc_watchdog_func(struct timer_list *t) vcpu->arch.watchdog_enabled) {smp_wmb ();kvm_make_request (KVM_REQ_WATCHDOG , vcpu); -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu); }/*
@@ -1872,7 +1872,7 @@ void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)set_bits (tsr_bits, &vcpu->arch.tsr);smp_wmb ();kvm_make_request (KVM_REQ_PENDING_TIMER , vcpu); -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu); }void kvmppc_clr_tsr_bits (struct kvm_vcpu *vcpu,u32 tsr_bits)
arch/powerpc/kvm/e500_emulate.c +1 -1
@@ -75,7 +75,7 @@ static int kvmppc_e500_emul_msgsnd(struct kvm_vcpu *vcpu, int rb)int cpir = cvcpu->arch.shared->pir;if ((param &PPC_DBELL_MSG_BRDCAST ) || (cpir == pir)) {set_bit (prio, &cvcpu->arch.pending_exceptions); -kvm_vcpu_kick (cvcpu); +kvmppc_vcpu_kick (cvcpu); } }
arch/powerpc/kvm/powerpc.c +1 -1
@@ -1903,7 +1903,7 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)kvmppc_core_queue_external (vcpu, irq); -kvm_vcpu_kick (vcpu); +kvmppc_vcpu_kick (vcpu);return 0 ; }
Signature · 1 line
2.56.0.313.g2479322e68-goog
Re: [PATCH 3/7] KVM: PPC: Explicitly force IPIs when directly kicking vCPUs
Quoted 26 lines · Sean Christopherson
Explicitly force an IPI when invoking
kvm_vcpu_kick()directly instead of relying onkvm_arch_vcpu_should_kick()to unconditionally return 1/true. This will allow droppingkvm_arch_vcpu_should_kick()in favor ofkvm_request_needs_ipi().No functional change intended (1 || 1 == true).
Signed-off-by Sean Christopherson <[email protected]>--- arch/powerpc/include/asm/kvm_ppc.h | 13 ++++++++++++- arch/powerpc/kvm/book3s.c | 4 ++-- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/booke.c | 6 +++--- arch/powerpc/kvm/e500_emulate.c | 2 +- arch/powerpc/kvm/powerpc.c | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-)arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -30,6 +30,17 @@#endif #include <asm/inst.h> +/*
arch/powerpc/include/asm/kvm_ppc.h line 43
Quoted 10 lines
arch/powerpc/include/asm/kvm_ppc.h +10
-0+* TODO: For unknown reasons, KVM PPC has historically ignored vcpu->mode when +* determining if kicking a vCPU needs an IPI. Either document why that +* behavior is necessary, or drop this helper and the @force param from +* __kvm_vcpu_kick(). +*/ +static inline void kvmppc_vcpu_kick (struct kvm_vcpu *vcpu) +{ +__kvm_vcpu_kick (vcpu, false, true); +} +
AFAICS, there is no reason for PPC to always return 1. I was looking at
the old patches on mailing list and found this - https://lore.kernel.org/kvm/[email protected]
``` How about we return 1 for kvm_arch_vcpu_in_guest_mode() on !x86 always, just like it's done today basically? Then we can worry about needless IPIs later and don't regress from the respective current kick implementations. ```
However, I'll do some testing and get back by mid next week to make sure there are no suprises.
Thanks,
Gautam
Sean Christopherson
maintainerRe: [PATCH 3/7] KVM: PPC: Explicitly force IPIs when directly kicking vCPUs
Quoted 41 lines · Gautam Menghani
Quoted 26 lines · Sean Christopherson
Explicitly force an IPI when invoking
kvm_vcpu_kick()directly instead of relying onkvm_arch_vcpu_should_kick()to unconditionally return 1/true. This will allow droppingkvm_arch_vcpu_should_kick()in favor ofkvm_request_needs_ipi().No functional change intended (1 || 1 == true).
Signed-off-by Sean Christopherson <[email protected]>--- arch/powerpc/include/asm/kvm_ppc.h | 13 ++++++++++++- arch/powerpc/kvm/book3s.c | 4 ++-- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/booke.c | 6 +++--- arch/powerpc/kvm/e500_emulate.c | 2 +- arch/powerpc/kvm/powerpc.c | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-)arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -30,6 +30,17 @@#endif #include <asm/inst.h> +/* arch/powerpc/include/asm/kvm_ppc.h line 43
Quoted 10 lines
arch/powerpc/include/asm/kvm_ppc.h +10
-0+* TODO: For unknown reasons, KVM PPC has historically ignored vcpu->mode when +* determining if kicking a vCPU needs an IPI. Either document why that +* behavior is necessary, or drop this helper and the @force param from +* __kvm_vcpu_kick(). +*/ +static inline void kvmppc_vcpu_kick (struct kvm_vcpu *vcpu) +{ +__kvm_vcpu_kick (vcpu, false, true); +} +AFAICS, there is no reason for PPC to always return 1. I was looking at
the old patches on mailing list and found this - https://lore.kernel.org/kvm/[email protected]
Quoted 5 lines
``` How about we return 1 for kvm_arch_vcpu_in_guest_mode() on !x86 always, just like it's done today basically? Then we can worry about needless IPIs later and don't regress from the respective current kick implementations. ```
FWIW, I found that as well, but there are a metric ton of kvm_vcpu_kick() calls that have been added since then, and they scared me.
Quoted 2 lines
However, I'll do some testing and get back by mid next week to make sure there are no suprises.
Thanks! If we can drop the PPC behavior, that would be wonderful! Though if we do that, it should be for 7.5 in a follow-up (this series really should go into 7.4 due to the SNP issue).
Gautam Menghani
Reviewed-byRe: [PATCH 3/7] KVM: PPC: Explicitly force IPIs when directly kicking vCPUs
Quoted 51 lines · Sean Christopherson
Quoted 41 lines · Gautam Menghani
Quoted 26 lines · Sean Christopherson
Explicitly force an IPI when invoking
kvm_vcpu_kick()directly instead of relying onkvm_arch_vcpu_should_kick()to unconditionally return 1/true. This will allow droppingkvm_arch_vcpu_should_kick()in favor ofkvm_request_needs_ipi().No functional change intended (1 || 1 == true).
Signed-off-by Sean Christopherson <[email protected]>--- arch/powerpc/include/asm/kvm_ppc.h | 13 ++++++++++++- arch/powerpc/kvm/book3s.c | 4 ++-- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/booke.c | 6 +++--- arch/powerpc/kvm/e500_emulate.c | 2 +- arch/powerpc/kvm/powerpc.c | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-)arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -30,6 +30,17 @@#endif #include <asm/inst.h> +/* arch/powerpc/include/asm/kvm_ppc.h line 43
Quoted 10 lines
arch/powerpc/include/asm/kvm_ppc.h +10
-0+* TODO: For unknown reasons, KVM PPC has historically ignored vcpu->mode when +* determining if kicking a vCPU needs an IPI. Either document why that +* behavior is necessary, or drop this helper and the @force param from +* __kvm_vcpu_kick(). +*/ +static inline void kvmppc_vcpu_kick (struct kvm_vcpu *vcpu) +{ +__kvm_vcpu_kick (vcpu, false, true); +} +AFAICS, there is no reason for PPC to always return 1. I was looking at
the old patches on mailing list and found this - https://lore.kernel.org/kvm/[email protected]Quoted 5 lines
``` How about we return 1 for kvm_arch_vcpu_in_guest_mode() on !x86 always, just like it's done today basically? Then we can worry about needless IPIs later and don't regress from the respective current kick implementations. ```FWIW, I found that as well, but there are a metric ton of
kvm_vcpu_kick()calls that have been added since then, and they scared me.
Quoted 6 lines
Quoted 2 lines
However, I'll do some testing and get back by mid next week to make sure there are no suprises.
Thanks! If we can drop the PPC behavior, that would be wonderful! Though if we do that, it should be for 7.5 in a follow-up (this series really should go into 7.4 due to the SNP issue).
Yeah I looked into it and realized that the newer variants of KVM on PPC are not even using vcpu->mode at all. So that has to be fixed from our end.
For this series,
Sean Christopherson
maintainerPATCH 4/7 KVM: MIPS: Rework kvm_arch_vcpu_should_kick() to use standard implementation
In reply to Sean ChristophersonRework MIPS' kvm_arch_vcpu_should_kick() to use the "standard" arch implementation so that the arch hook can be dropped (MIPS is the only arch other than PPC that implements a non-standard hook, and PPC uses the recently added @force param when invoking __kvm_vcpu_kick() instead of relying on kvm_arch_vcpu_should_kick()).
Convert MIPS to the standard implementation even though the code should be unreachable (neither kvm_vcpu_kick() nor kvm_make_request_and_kick() are used by MIPS or common KVM), so that there is a bisection point if the code is somehow reachable.
No functional change intended (because the code is currently unreachable).
--- arch/mips/kvm/mips.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/kvm/mips.c +1 -1
@@ -122,7 +122,7 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) { -return 1 ; +return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; }int kvm_arch_enable_virtualization_cpu (void )
Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerPATCH 5/7 KVM: Move kvm_arch_vcpu_should_kick() to common code as kvm_vcpu_should_kick()
In reply to Sean ChristophersonDedup kvm_arch_vcpu_should_kick() now that all architectures except for PPC have identical implementations. PPC's implementation is unreachable as PPC always invokes __kvm_vcpu_kick() with @force=true.
No functional change intended.
--- arch/arm64/kvm/arm.c | 5 ----- arch/loongarch/kvm/vcpu.c | 5 ----- arch/mips/kvm/mips.c | 5 ----- arch/powerpc/kvm/powerpc.c | 5 ----- arch/riscv/kvm/vcpu.c | 5 ----- arch/x86/kvm/x86.c | 5 ----- include/linux/kvm_host.h | 1 - virt/kvm/kvm_main.c | 11 ++++++++--- 8 files changed, 8 insertions(+), 34 deletions(-)
arch/arm64/kvm/arm.c +0 -5
@@ -128,11 +128,6 @@ bool is_kvm_arm_initialised(void)return kvm_arm_initialised; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {
arch/loongarch/kvm/vcpu.c +0 -5
@@ -379,11 +379,6 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_RUNNABLE ; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu) {unsigned long val;
arch/mips/kvm/mips.c +0 -5
@@ -120,11 +120,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)return false; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -int kvm_arch_enable_virtualization_cpu (void ) {return kvm_mips_callbacks->enable_virtualization_cpu ();
arch/powerpc/kvm/powerpc.c +0 -5
@@ -62,11 +62,6 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)return false; } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return 1 ; -} -/* * Common checks before entering the guest world. Call with interrupts * enabled.
arch/riscv/kvm/vcpu.c +0 -5
@@ -239,11 +239,6 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) !kvm_riscv_vcpu_stopped(vcpu) && !vcpu->arch.pause); } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu) {return (vcpu->arch.guest_context.sstatus &SR_SPP ) ? true : false;
arch/x86/kvm/x86.c +0 -5
@@ -10472,11 +10472,6 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)return kvm_rip_read (vcpu); } -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -static inline u32 kvm_async_pf_hash_fn (gfn_t gfn) {BUILD_BUG_ON (!is_power_of_2 (ASYNC_PF_PER_VCPU ));
include/linux/kvm_host.h +0 -1
@@ -1694,7 +1694,6 @@ void kvm_arch_disable_virtualization_cpu(void);bool kvm_vcpu_has_events (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu); -int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);bool kvm_arch_dy_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_dy_has_pending_interrupt (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_preempted_in_kernel (struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +8 -3
@@ -191,7 +191,7 @@ void vcpu_put(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (vcpu_put); -/* TODO: merge with kvm_arch_vcpu_should_kick */ +/* TODO: merge with kvm_vcpu_should_kick */ static bool kvm_request_needs_ipi (struct kvm_vcpu *vcpu,unsigned req) {int mode =kvm_vcpu_exiting_guest_mode (vcpu);
@@ -209,6 +209,11 @@ static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)return mode ==IN_GUEST_MODE ; } +static int kvm_vcpu_should_kick (struct kvm_vcpu *vcpu) +{ +return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; +} +static void ack_kick (void *_completed) { }
@@ -3930,7 +3935,7 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait, bool force)/* * Note, the vCPU could get migrated to a different pCPU at any point -* after kvm_arch_vcpu_should_kick(), which could result in sending an +* after kvm_vcpu_should_kick(), which could result in sending an * IPI to the previous pCPU. But, that's ok because the purpose of the * IPI is to force the vCPU to leave IN_GUEST_MODE, and migrating the * vCPU also requires it to leave IN_GUEST_MODE
@@ -3939,7 +3944,7 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait, bool force)* check also modifies vcpu->mode and KVM PPC needs to avoid that side * effect for unknown historical reasons. */ -if (force ||kvm_arch_vcpu_should_kick (vcpu)) { +if (force ||kvm_vcpu_should_kick (vcpu)) { cpu =READ_ONCE (vcpu->cpu);if (cpu != me && (unsigned int )cpu < nr_cpu_ids &&cpu_online (cpu)) {/*
Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerPATCH 6/7 KVM: Plumb the full request into __kvm_vcpu_kick()
In reply to Sean ChristophersonPlumb the full request instead of just a "wait" boolean into __kvm_vcpu_kick() so that it can use kvm_request_needs_ipi() instead of the flawed kvm_vcpu_should_kick().
No functional change intended.
--- arch/powerpc/include/asm/kvm_ppc.h | 2 +- include/linux/kvm_host.h | 6 +++--- virt/kvm/kvm_main.c | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -1
@@ -38,7 +38,7 @@*/ static inline void kvmppc_vcpu_kick (struct kvm_vcpu *vcpu) { -__kvm_vcpu_kick (vcpu, false, true); +__kvm_vcpu_kick (vcpu,0 , true); }/*
include/linux/kvm_host.h +3 -3
@@ -1570,11 +1570,11 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);bool kvm_vcpu_wake_up (struct kvm_vcpu *vcpu);#ifndef CONFIG_S390 -void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait,bool force); +void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,int request,bool force);static inline void kvm_vcpu_kick (struct kvm_vcpu *vcpu) { -__kvm_vcpu_kick (vcpu, false, false); +__kvm_vcpu_kick (vcpu,0 , false); }#endif
@@ -2314,7 +2314,7 @@ static __always_inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)static inline void kvm_make_request_and_kick (int req,struct kvm_vcpu *vcpu) {kvm_make_request (req, vcpu); -__kvm_vcpu_kick (vcpu, req &KVM_REQUEST_WAIT , false); +__kvm_vcpu_kick (vcpu, req, false); }#endif
virt/kvm/kvm_main.c +9 -8
@@ -3913,7 +3913,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_wake_up);/* * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. */ -void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,bool wait,bool force) +void __kvm_vcpu_kick (struct kvm_vcpu *vcpu,int req,bool force) {int me, cpu;
@@ -3948,14 +3948,15 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait, bool force) cpu =READ_ONCE (vcpu->cpu);if (cpu != me && (unsigned int )cpu < nr_cpu_ids &&cpu_online (cpu)) {/* -* Use a reschedule IPI to kick the vCPU if the caller -* doesn't need to wait for a response, as KVM allows -* kicking vCPUs while IRQs are disabled, but using the -* SMP function call framework with IRQs disabled can -* deadlock due to taking cross-CPU locks. +* Use a reschedule IPI to kick the vCPU if the request +* doesn't need to wait for a response (or there is no +* request), as KVM allows kicking vCPUs while IRQs are +* disabled, but using the SMP function call framework +* with IRQs disabled can deadlock due to taking cross- +* CPU locks. */ -if (wait) -smp_call_function_single (cpu, ack_kick,NULL , wait); +if (req &KVM_REQUEST_WAIT ) +smp_call_function_single (cpu, ack_kick,NULL , true);else smp_send_reschedule (cpu); }
Signature · 1 line
2.56.0.313.g2479322e68-goog
Sean Christopherson
maintainerPATCH 7/7 KVM: Drop kvm_vcpu_should_kick() and use kvm_request_needs_ipi()
In reply to Sean ChristophersonDrop kvm_vcpu_should_kick() in favor of kvm_request_needs_ipi() to fix a bug where KVM will fail to wait for a vCPU to exit guest mode if the vCPU was already marked as EXITING_GUEST_MODE by a concurrent non-waiting kick. Because kvm_vcpu_should_kick() (was kvm_arch_vcpu_should_kick()) only waits (and sends an IPI to do so) if the target vCPU is fully IN_GUEST_MODE, the sending task will proceed even though it hasn't actually guaranteed the vCPU as fully exited the guest.
The flaw is most problematic for SNP VMSA invalidation with in-place gmem conversion, as KVM invokes kvm_gmem_invalidate_start() => rmp_make_shared() (via kvm_gmem_make_shared() in quick succession when in-place conversion is enabled. I.e. if the target has been marked as EXITING_GUEST_MODE but has not yet received the other kicker's IPI, then updating the RMP will fail as the VMSA is still in-use and subsequent accesses to the page will encounter unexpected RMP #PFs.
Note, the bug exists and affects VMSA invalidation even without in-place conversion, but is significantly more difficult to hit as PUNCH_HOLE and .release() have to wade through a lot more code and several locks to get from invalidation to kvm_gmem_free_folio().
Note #2, currently only x86 uses kvm_make_request_and_kick(), i.e. this is a glorified nop for all other architectures as the KVM_REQUEST_WAIT path is never taken.
--- virt/kvm/kvm_main.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
virt/kvm/kvm_main.c +2 -8
@@ -191,7 +191,6 @@ void vcpu_put(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (vcpu_put); -/* TODO: merge with kvm_vcpu_should_kick */ static bool kvm_request_needs_ipi (struct kvm_vcpu *vcpu,unsigned req) {int mode =kvm_vcpu_exiting_guest_mode (vcpu);
@@ -209,11 +208,6 @@ static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)return mode ==IN_GUEST_MODE ; } -static int kvm_vcpu_should_kick (struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; -} -static void ack_kick (void *_completed) { }
@@ -3935,7 +3929,7 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, int req, bool force)/* * Note, the vCPU could get migrated to a different pCPU at any point -* after kvm_vcpu_should_kick(), which could result in sending an +* after kvm_request_needs_ipi(), which could result in sending an * IPI to the previous pCPU. But, that's ok because the purpose of the * IPI is to force the vCPU to leave IN_GUEST_MODE, and migrating the * vCPU also requires it to leave IN_GUEST_MODE
@@ -3944,7 +3938,7 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, int req, bool force)* check also modifies vcpu->mode and KVM PPC needs to avoid that side * effect for unknown historical reasons. */ -if (force ||kvm_vcpu_should_kick (vcpu)) { +if (force ||kvm_request_needs_ipi (vcpu, req)) { cpu =READ_ONCE (vcpu->cpu);if (cpu != me && (unsigned int )cpu < nr_cpu_ids &&cpu_online (cpu)) {/*
Signature · 1 line
2.56.0.313.g2479322e68-goog
Quoted 8 lines · Sean Christopherson
Fix a bug in where
kvm_make_request_and_kick()won't actually wait if the kick races without another non-waiting kick. The issue is that all of the conditionalkvm_arch_vcpu_should_kick()implementations are missing this code:if (req & KVM_REQUEST_WAIT) return mode != OUTSIDE_GUEST_MODE;[...]
Applied to kvm-x86 fixes, thanks!
[1/7] KVM: s390: Drop unused and unreachable kvm_arch_vcpu_should_kick()
https://github.com/kvm-x86/linux/commit/20cf5e54a1bc [2/7] KVM: Add a @force param to __kvm_vcpu_kick() for PPC backwards compatibility https://github.com/kvm-x86/linux/commit/0ee75a9dee9c [3/7] KVM: PPC: Explicitly force IPIs when directly kicking vCPUs https://github.com/kvm-x86/linux/commit/2530231b023e [4/7] KVM: MIPS: Rework kvm_arch_vcpu_should_kick() to use standard implementation https://github.com/kvm-x86/linux/commit/a8dabc687ddb [5/7] KVM: Move kvm_arch_vcpu_should_kick() to common code as kvm_vcpu_should_kick() https://github.com/kvm-x86/linux/commit/7e52636d9c37 [6/7] KVM: Plumb the full request into __kvm_vcpu_kick() https://github.com/kvm-x86/linux/commit/5c430e211f4d [7/7] KVM: Drop kvm_vcpu_should_kick() and use kvm_request_needs_ipi() https://github.com/kvm-x86/linux/commit/fd3dd4d9c186