Skip to contentSkip to conversation

KVM: PPC: Book3S HV: Make secondary threads more robust against stray IPIs

PATCH New Paul Mackerras 1 participant 1 message Feb 3, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/4 KVM: PPC: Book3S HV: Make secondary threads more robust against stray IPIs

Currently on POWER7, if we are running the guest on a core and we don't need all the hardware threads, we do nothing to ensure that the unused threads aren't executing in the kernel (other than checking that they are offline). We just assume they're napping and we don't do anything to stop them trying to enter the kernel while the guest is running. This means that a stray IPI can wake up the hardware thread and it will then try to enter the kernel, but since the core is in guest context, it will execute code from the guest in hypervisor mode once it turns the MMU on, which tends to lead to crashes or hangs in the host.

This fixes the problem by adding two new one-byte flags in the kvmppc_host_state structure in the PACA which are used to interlock between the primary thread and the unused secondary threads when entering the guest. With these flags, the primary thread can ensure that the unused secondaries are not already in kernel mode (i.e. handling a stray IPI) and then indicate that they should not try to enter the kernel if they do get woken for any reason. Instead they will go into KVM code, find that there is no vcpu to run, acknowledge and clear the IPI and go back to nap mode.

---
 arch/powerpc/include/asm/kvm_book3s_asm.h |    8 +++
 arch/powerpc/kernel/asm-offsets.c         |    2 +
 arch/powerpc/kernel/exceptions-64s.S      |   12 ++--
 arch/powerpc/kernel/idle_power7.S         |    7 ++
 arch/powerpc/kvm/book3s_hv.c              |   49 ++++++++++++++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   |   92 +++++++++++++++++------------
 6 files changed, 124 insertions(+), 46 deletions(-)
arch/powerpc/include/asm/kvm_book3s_asm.h +8 -0
index 1f2f5b6..88609b2 100644
@@ -79,6 +79,9 @@ struct kvmppc_host_state {
 	u8 napping;
 
 #ifdef CONFIG_KVM_BOOK3S_64_HV
+	u8 hwthread_req;
+	u8 hwthread_state;
+
 	struct kvm_vcpu *kvm_vcpu;
 	struct kvmppc_vcore *kvm_vcore;
 	unsigned long xics_phys;
@@ -122,4 +125,9 @@ struct kvmppc_book3s_shadow_vcpu {
 
 #endif /*__ASSEMBLY__ */
 
+/* Values for kvm_state */
+#define KVM_HWTHREAD_IN_KERNEL	0
+#define KVM_HWTHREAD_IN_NAP	1
+#define KVM_HWTHREAD_IN_KVM	2
+
 #endif /* __ASM_KVM_BOOK3S_ASM_H__ */
arch/powerpc/kernel/asm-offsets.c +2 -0
index 8e0db0b..1914818 100644
@@ -549,6 +549,8 @@ int main(void)
 	HSTATE_FIELD(HSTATE_IN_GUEST, in_guest);
 	HSTATE_FIELD(HSTATE_RESTORE_HID5, restore_hid5);
 	HSTATE_FIELD(HSTATE_NAPPING, napping);
+	HSTATE_FIELD(HSTATE_HWTHREAD_REQ, hwthread_req);
+	HSTATE_FIELD(HSTATE_HWTHREAD_STATE, hwthread_state);
 
 #ifdef CONFIG_KVM_BOOK3S_64_HV
 	HSTATE_FIELD(HSTATE_KVM_VCPU, kvm_vcpu);
arch/powerpc/kernel/exceptions-64s.S +7 -5
index ca9b733..1726a48 100644
@@ -62,11 +62,13 @@ BEGIN_FTR_SECTION
 	GET_PACA(r13)
 
 #ifdef CONFIG_KVM_BOOK3S_64_HV
-	lbz	r0,PACAPROCSTART(r13)
-	cmpwi	r0,0x80
-	bne	1f
-	li	r0,1
-	stb	r0,PACAPROCSTART(r13)
+	li	r0,KVM_HWTHREAD_IN_KERNEL
+	stb	r0,HSTATE_HWTHREAD_STATE(r13)
+	/* Order setting hwthread_state vs. testing hwthread_req */
+	sync
+	lbz	r0,HSTATE_HWTHREAD_REQ(r13)
+	cmpwi	r0,0
+	beq	1f
 	b	kvm_start_guest
 1:
 #endif
arch/powerpc/kernel/idle_power7.S +7 -0
index fcdff19..95eb8c5 100644
@@ -15,6 +15,7 @@
 #include <asm/ppc_asm.h>
 #include <asm/asm-offsets.h>
 #include <asm/ppc-opcode.h>
+#include <asm/kvm_book3s_asm.h>
 
 #undef DEBUG
 
@@ -64,6 +65,12 @@ _GLOBAL(power7_idle)
 	std	r9,_MSR(r1)
 	std	r1,PACAR1(r13)
 
+#ifdef CONFIG_KVM_BOOK3S_64_HV
+	/* Tell KVM we're napping */
+	li	r4,KVM_HWTHREAD_IN_NAP
+	stb	r4,HSTATE_HWTHREAD_STATE(r13)
+#endif
+
 	/* Magic NAP mode enter sequence */
 	std	r0,0(r1)
 	ptesync
arch/powerpc/kvm/book3s_hv.c +46 -3

79 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_rmhandlers.S +54 -38

138 lines not on this page · raw message

Signature · 1 line
1.7.8.3