Skip to contentSkip to conversation

PPC Book 3S HV-mode KVM updates for 3.15

PATCH In review Paul Mackerras 4 participants 14 messages Mar 24, 2014 – Mar 28, 2014

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

cover PPC Book 3S HV-mode KVM updates for 3.15

This series of patches fixes some bugs in HV-mode KVM for PowerPC Book 3S and finishes off adding the support for POWER8. Patches 2 and 3 are the two patches from the series I posted in January that Alex Graf didn't apply at that stage. I have updated them according to his review comments. The last patch is also POWER8-related, adding code to save and restore more of the host state of the PMU. (We context-switch the PMU between host and guest since the guest can access the PMU directly.) The remaining patches fix bugs that have been found over the last few months of testing.

This patch series is based on the merge of the "queue" branch of the kvm tree with the "kvm-ppc-queue" branch of Alex Graf's tree, though I expect they would apply cleanly against the kvm tree "queue" branch also.

I would like these to go into 3.15. Scott, please ack.

Paul.

---
[PATCH 1/8] KVM: PPC: Book3S HV: Fix KVM hang with CONFIG_KVM_XICS=n [PATCH 2/8] KVM: PPC: Book3S HV: Add transactional memory support [PATCH 3/8] KVM: PPC: Book3S HV: Add get/set_one_reg for new TM state [PATCH 4/8] KVM: PPC: Book3S: Trim top 4 bits of physical address in [PATCH 5/8] KVM: PPC: Book3S HV: Return ENODEV error rather than EIO [PATCH 6/8] KVM: PPC: Book3S HV: Don't use kvm_memslots() in real [PATCH 7/8] KVM: PPC: Book3S HV: Fix decrementer timeouts with [PATCH 8/8] KVM: PPC: Book3S HV: Save/restore host PMU registers that

 arch/powerpc/include/asm/kvm_book3s_64.h  |  12 ++
 arch/powerpc/include/asm/kvm_book3s_asm.h |   2 +-
 arch/powerpc/include/asm/tm.h             |   4 +
 arch/powerpc/kvm/book3s_64_mmu_hv.c       |   9 +-
 arch/powerpc/kvm/book3s_hv.c              | 153 +++++++++++++++++++++-----
 arch/powerpc/kvm/book3s_hv_interrupts.S   |  22 ++++
 arch/powerpc/kvm/book3s_hv_rm_mmu.c       |   6 +-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   | 177 +++++++++++++++++++++++++-----
 arch/powerpc/kvm/book3s_rtas.c            |   7 +-
 9 files changed, 329 insertions(+), 63 deletions(-)
#1 lore raw

PATCH 1/8 KVM: PPC: Book3S HV: Fix KVM hang with CONFIG_KVM_XICS=n

From: Anton Blanchard <[email protected]>

I noticed KVM is broken when KVM in-kernel XICS emulation (CONFIG_KVM_XICS) is disabled.

The problem was introduced in 48eaef05 (KVM: PPC: Book3S HV: use xics_wake_cpu only when defined). It used CONFIG_KVM_XICS to wrap xics_wake_cpu, where CONFIG_PPC_ICP_NATIVE should have been used.

---
 arch/powerpc/kvm/book3s_hv.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/powerpc/kvm/book3s_hv.c +1 -3
index 3b498d9..e0a535c 100644
@@ -86,7 +86,7 @@ static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
 
 	/* CPU points to the first thread of the core */
 	if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
-#ifdef CONFIG_KVM_XICS
+#ifdef CONFIG_PPC_ICP_NATIVE
 		int real_cpu = cpu + vcpu->arch.ptid;
 		if (paca[real_cpu].kvm_hstate.xics_phys)
 			xics_wake_cpu(real_cpu);
@@ -1360,9 +1360,7 @@ static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
 	smp_wmb();
 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
 	if (cpu != smp_processor_id()) {
-#ifdef CONFIG_KVM_XICS
 		xics_wake_cpu(cpu);
-#endif
 		if (vcpu->arch.ptid)
 			++vc->n_woken;
 	}
Signature · 1 line
1.9.rc1
#2 lore raw

PATCH 2/8 KVM: PPC: Book3S HV: Add transactional memory support

In reply to Paul Mackerras

From: Michael Neuling <[email protected]>

This adds saving of the transactional memory (TM) checkpointed state on guest entry and exit. We only do this if we see that the guest has an active transaction.

It also adds emulation of the TM state changes when delivering IRQs into the guest. According to the architecture, if we are transactional when an IRQ occurs, the TM state is changed to suspended, otherwise it's left unchanged.

---
 arch/powerpc/include/asm/tm.h           |   4 +
 arch/powerpc/kvm/book3s_64_mmu_hv.c     |   9 +-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 157 ++++++++++++++++++++++++++------
 3 files changed, 140 insertions(+), 30 deletions(-)
arch/powerpc/include/asm/tm.h +4 -0
index 0c9f8b7..c22d704 100644
@@ -7,6 +7,8 @@
 
 #include <uapi/asm/tm.h>
 
+#ifndef __ASSEMBLY__
+
 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
 extern void do_load_up_transact_fpu(struct thread_struct *thread);
 extern void do_load_up_transact_altivec(struct thread_struct *thread);
@@ -21,3 +23,5 @@ extern void tm_recheckpoint(struct thread_struct *thread,
 extern void tm_abort(uint8_t cause);
 extern void tm_save_sprs(struct thread_struct *thread);
 extern void tm_restore_sprs(struct thread_struct *thread);
+
+#endif /* __ASSEMBLY__ */
arch/powerpc/kvm/book3s_64_mmu_hv.c +8 -1
index 303ece7..fb25ebc 100644
@@ -262,7 +262,14 @@ int kvmppc_mmu_hv_init(void)
 
 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
 {
-	kvmppc_set_msr(vcpu, vcpu->arch.intr_msr);
+	unsigned long msr = vcpu->arch.intr_msr;
+
+	/* If transactional, change to suspend mode on IRQ delivery */
+	if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
+		msr |= MSR_TS_S;
+	else
+		msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
+	kvmppc_set_msr(vcpu, msr);
 }
 
 /*
arch/powerpc/kvm/book3s_hv_rmhandlers.S +128 -29

226 lines not on this page · raw message

Signature · 1 line
1.9.rc1
#3 lore raw

PATCH 3/8 KVM: PPC: Book3S HV: Add get/set_one_reg for new TM state

In reply to Paul Mackerras

From: Michael Neuling <[email protected]>

This adds code to get/set_one_reg to read and write the new transactional memory (TM) state.

---
 arch/powerpc/kvm/book3s_hv.c | 147 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 125 insertions(+), 22 deletions(-)
arch/powerpc/kvm/book3s_hv.c +125 -22
index e0a535c..a6d8f01 100644
@@ -879,17 +879,6 @@ static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
 	case KVM_REG_PPC_IAMR:
 		*val = get_reg_val(id, vcpu->arch.iamr);
 		break;
-#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
-	case KVM_REG_PPC_TFHAR:
-		*val = get_reg_val(id, vcpu->arch.tfhar);
-		break;
-	case KVM_REG_PPC_TFIAR:
-		*val = get_reg_val(id, vcpu->arch.tfiar);
-		break;
-	case KVM_REG_PPC_TEXASR:
-		*val = get_reg_val(id, vcpu->arch.texasr);
-		break;
-#endif
 	case KVM_REG_PPC_FSCR:
 		*val = get_reg_val(id, vcpu->arch.fscr);
 		break;
@@ -970,6 +959,69 @@ static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
 	case KVM_REG_PPC_PPR:
 		*val = get_reg_val(id, vcpu->arch.ppr);
 		break;
+#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
+	case KVM_REG_PPC_TFHAR:
+		*val = get_reg_val(id, vcpu->arch.tfhar);
+		break;
+	case KVM_REG_PPC_TFIAR:
+		*val = get_reg_val(id, vcpu->arch.tfiar);
+		break;
+	case KVM_REG_PPC_TEXASR:
+		*val = get_reg_val(id, vcpu->arch.texasr);
+		break;
+	case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
+		i = id - KVM_REG_PPC_TM_GPR0;
+		*val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
+		break;
+	case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
+	{
+		int j;
+		i = id - KVM_REG_PPC_TM_VSR0;
+		if (i < 32)
+			for (j = 0; j < TS_FPRWIDTH; j++)
+				val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
+		else {
+			if (cpu_has_feature(CPU_FTR_ALTIVEC))
+				val->vval = vcpu->arch.vr_tm.vr[i-32];
+			else
+				r = -ENXIO;
+		}
+		break;
+	}
+	case KVM_REG_PPC_TM_CR:
+		*val = get_reg_val(id, vcpu->arch.cr_tm);
+		break;
+	case KVM_REG_PPC_TM_LR:
+		*val = get_reg_val(id, vcpu->arch.lr_tm);
+		break;
+	case KVM_REG_PPC_TM_CTR:
+		*val = get_reg_val(id, vcpu->arch.ctr_tm);
+		break;
+	case KVM_REG_PPC_TM_FPSCR:
+		*val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
+		break;
+	case KVM_REG_PPC_TM_AMR:
+		*val = get_reg_val(id, vcpu->arch.amr_tm);
+		break;
+	case KVM_REG_PPC_TM_PPR:
+		*val = get_reg_val(id, vcpu->arch.ppr_tm);
+		break;
+	case KVM_REG_PPC_TM_VRSAVE:
+		*val = get_reg_val(id, vcpu->arch.vrsave_tm);
+		break;
+	case KVM_REG_PPC_TM_VSCR:
+		if (cpu_has_feature(CPU_FTR_ALTIVEC))
+			*val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
+		else
+			r = -ENXIO;
+		break;
+	case KVM_REG_PPC_TM_DSCR:
+		*val = get_reg_val(id, vcpu->arch.dscr_tm);
+		break;
+	case KVM_REG_PPC_TM_TAR:
+		*val = get_reg_val(id, vcpu->arch.tar_tm);
+		break;
+#endif
 	case KVM_REG_PPC_ARCH_COMPAT:
 		*val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
 		break;
@@ -1039,17 +1091,6 @@ static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
 	case KVM_REG_PPC_IAMR:
 		vcpu->arch.iamr = set_reg_val(id, *val);
 		break;
-#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
-	case KVM_REG_PPC_TFHAR:
-		vcpu->arch.tfhar = set_reg_val(id, *val);
-		break;
-	case KVM_REG_PPC_TFIAR:
-		vcpu->arch.tfiar = set_reg_val(id, *val);
-		break;
-	case KVM_REG_PPC_TEXASR:
-		vcpu->arch.texasr = set_reg_val(id, *val);
-		break;
-#endif
 	case KVM_REG_PPC_FSCR:
 		vcpu->arch.fscr = set_reg_val(id, *val);
 		break;
@@ -1144,6 +1185,68 @@ static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
 	case KVM_REG_PPC_PPR:
 		vcpu->arch.ppr = set_reg_val(id, *val);
 		break;
+#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
+	case KVM_REG_PPC_TFHAR:
+		vcpu->arch.tfhar = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TFIAR:
+		vcpu->arch.tfiar = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TEXASR:
+		vcpu->arch.texasr = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
+		i = id - KVM_REG_PPC_TM_GPR0;
+		vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
+	{
+		int j;
+		i = id - KVM_REG_PPC_TM_VSR0;
+		if (i < 32)
+			for (j = 0; j < TS_FPRWIDTH; j++)
+				vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
+		else
+			if (cpu_has_feature(CPU_FTR_ALTIVEC))
+				vcpu->arch.vr_tm.vr[i-32] = val->vval;
+			else
+				r = -ENXIO;
+		break;
+	}
+	case KVM_REG_PPC_TM_CR:
+		vcpu->arch.cr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_LR:
+		vcpu->arch.lr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_CTR:
+		vcpu->arch.ctr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_FPSCR:
+		vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_AMR:
+		vcpu->arch.amr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_PPR:
+		vcpu->arch.ppr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_VRSAVE:
+		vcpu->arch.vrsave_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_VSCR:
+		if (cpu_has_feature(CPU_FTR_ALTIVEC))
+			vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
+		else
+			r = - ENXIO;
+		break;
+	case KVM_REG_PPC_TM_DSCR:
+		vcpu->arch.dscr_tm = set_reg_val(id, *val);
+		break;
+	case KVM_REG_PPC_TM_TAR:
+		vcpu->arch.tar_tm = set_reg_val(id, *val);
+		break;
+#endif
 	case KVM_REG_PPC_ARCH_COMPAT:
 		r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
 		break;
Signature · 1 line
1.9.rc1
#4 lore raw

PATCH 1/2 KVM: PPC: Book3S HV: Make TM avoid program check

Currently using kvmppc_set_one_reg() a transaction could be setup without TEXASR Failure Summary (FS) not set. When this is switched back in by the host, this will result in a TM Bad Thing (ie 0x700 program check) when the trechkpt is run.

This avoids this by always setting the TEXASR FS when there is an active transaction being started.

This patch is on top of Paulus' recent KVM TM patch set.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +8 -0
index 217a22e..01d5701 100644
@@ -639,6 +639,14 @@ END_FTR_SECTION_IFCLR(CPU_FTR_TM)
 	rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
 	beq	1f	/* TM not active in guest */
 
+	/* Make sure the failure summary is set, otherwise we'll program check
+	 * when we trechkpt.  It's possible that this might have been not set
+	 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
+	 * host.
+	 */
+	oris	r7, r7, (TEXASR_FS)@h
+	mtspr	SPRN_TEXASR, r7
+
 	/*
 	 * We need to load up the checkpointed state for the guest.
 	 * We need to do this early as it will blow away any GPRs, VSRs and
Signature · 1 line
1.8.3.2
#5 lore raw

PATCH 2/2 KVM: PPC: Book3S HV: Add branch label

This branch label is over a large section so let's give it a real name.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +3 -3
index 01d5701..832750d 100644
@@ -612,7 +612,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 
 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
 BEGIN_FTR_SECTION
-	b	1f
+	b	skip_tm
 END_FTR_SECTION_IFCLR(CPU_FTR_TM)
 
 	/* Turn on TM/FP/VSX/VMX so we can restore them. */
@@ -637,7 +637,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_TM)
 
 	ld	r5, VCPU_MSR(r4)
 	rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
-	beq	1f	/* TM not active in guest */
+	beq	skip_tm	/* TM not active in guest */
 
 	/* Make sure the failure summary is set, otherwise we'll program check
 	 * when we trechkpt.  It's possible that this might have been not set
@@ -717,7 +717,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_TM)
 	/* Set the MSR RI since we have our registers back. */
 	li	r5, MSR_RI
 	mtmsrd	r5, 1
-1:
+skip_tm:
 #endif
 
 	/* Load guest PMU registers */
Signature · 1 line
1.8.3.2
#6 lore raw

Re: [PATCH 1/2] KVM: PPC: Book3S HV: Make TM avoid program check

In reply to Michael Neuling
Quoted 9 lines · Michael Neuling

Currently using kvmppc_set_one_reg() a transaction could be setup without TEXASR Failure Summary (FS) not set. When this is switched back in by the host, this will result in a TM Bad Thing (ie 0x700 program check) when the trechkpt is run.

This avoids this by always setting the TEXASR FS when there is an active transaction being started.

This patch is on top of Paulus' recent KVM TM patch set.

Thanks, Mikey.

Do you mind if I fold these two patches into patch 2/8 of the set I posted?

Paul.

maintainer
#7 lore raw

Re: [PATCH 1/2] KVM: PPC: Book3S HV: Make TM avoid program check

Il 28/03/2014 12:08, Paul Mackerras ha scritto:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Currently using kvmppc_set_one_reg() a transaction could be setup without TEXASR Failure Summary (FS) not set. When this is switched back in by the host, this will result in a TM Bad Thing (ie 0x700 program check) when the trechkpt is run.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

This avoids this by always setting the TEXASR FS when there is an active transaction being started.

This patch is on top of Paulus' recent KVM TM patch set.

Thanks, Mikey.

Do you mind if I fold these two patches into patch 2/8 of the set I posted?

In either case, am I right that this doesn't include the patches in kvm-ppc-queue? I'm waiting for the pull request. :)

Paolo

#8 lore raw

PATCH 4/8 KVM: PPC: Book3S: Trim top 4 bits of physical address in RTAS code

In reply to Paul Mackerras

The in-kernel emulation of RTAS functions needs to read the argument buffer from guest memory in order to find out what function is being requested. The guest supplies the guest physical address of the buffer, and on a real system the code that reads that buffer would run in guest real mode. In guest real mode, the processor ignores the top 4 bits of the address specified in load and store instructions. In order to emulate that behaviour correctly, we need to mask off those bits before calling kvm_read_guest() or kvm_write_guest(). This adds that masking.

---
 arch/powerpc/kvm/book3s_rtas.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/kvm/book3s_rtas.c +5 -2
index cf95cde..7a05315 100644
@@ -213,8 +213,11 @@ int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
 	gpa_t args_phys;
 	int rc;
 
-	/* r4 contains the guest physical address of the RTAS args */
-	args_phys = kvmppc_get_gpr(vcpu, 4);
+	/*
+	 * r4 contains the guest physical address of the RTAS args
+	 * Mask off the top 4 bits since this is a guest real address
+	 */
+	args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
 
 	rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
 	if (rc)
Signature · 1 line
1.9.rc1
#9 lore raw

PATCH 5/8 KVM: PPC: Book3S HV: Return ENODEV error rather than EIO

In reply to Paul Mackerras

If an attempt is made to load the kvm-hv module on a machine which doesn't have hypervisor mode available, return an ENODEV error, which is the conventional thing to return to indicate that this module is not applicable to the hardware of the current machine, rather than EIO, which causes a warning to be printed.

(cherry picked from commit a41cf3b2d791478f239c434917dffe9d1fe362c3)
---
 arch/powerpc/kvm/book3s_hv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +1 -1
index a6d8f01..8227dba 100644
@@ -2467,7 +2467,7 @@ static int kvmppc_book3s_init_hv(void)
 	 */
 	r = kvmppc_core_check_processor_compat_hv();
 	if (r < 0)
-		return r;
+		return -ENODEV;
 
 	kvm_ops_hv.owner = THIS_MODULE;
 	kvmppc_hv_ops = &kvm_ops_hv;
Signature · 1 line
1.9.rc1
#10 lore raw

PATCH 6/8 KVM: PPC: Book3S HV: Don't use kvm_memslots() in real mode

In reply to Paul Mackerras

With HV KVM, some high-frequency hypercalls such as H_ENTER are handled in real mode, and need to access the memslots array for the guest. Accessing the memslots array is safe, because we hold the SRCU read lock for the whole time that a guest vcpu is running. However, the checks that kvm_memslots() does when lockdep is enabled are potentially unsafe in real mode, when only the linear mapping is available. Furthermore, kvm_memslots() can be called from a secondary CPU thread, which is an offline CPU from the point of view of the host kernel, and is not running the task which holds the SRCU read lock.

To avoid false positives in the checks in kvm_memslots(), and to avoid possible side effects from doing the checks in real mode, this replaces kvm_memslots() with kvm_memslots_raw() in all the places that execute in real mode. kvm_memslots_raw() is a new function that is like kvm_memslots() but uses rcu_dereference_raw_notrace() instead of kvm_dereference_check().

---
 arch/powerpc/include/asm/kvm_book3s_64.h | 12 ++++++++++++
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      |  6 +++---
 2 files changed, 15 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +12 -0
index bf0fa8b..51388be 100644
@@ -289,6 +289,18 @@ static inline void note_hpte_modification(struct kvm *kvm,
 	if (atomic_read(&kvm->arch.hpte_mod_interest))
 		rev->guest_rpte |= HPTE_GR_MODIFIED;
 }
+
+/*
+ * Like kvm_memslots(), but for use in real mode when we can't do
+ * any RCU stuff (since the secondary threads are offline from the
+ * kernel's point of view), and we can't print anything.
+ * Thus we use rcu_dereference_raw() rather than rcu_dereference_check().
+ */
+static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
+{
+	return rcu_dereference_raw_notrace(kvm->memslots);
+}
+
 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
 
 #endif /* __ASM_KVM_BOOK3S_64_H__ */
arch/powerpc/kvm/book3s_hv_rm_mmu.c +3 -3
index 37fb3ca..1d6c56a 100644
@@ -111,7 +111,7 @@ static void remove_revmap_chain(struct kvm *kvm, long pte_index,
 	rcbits = hpte_r & (HPTE_R_R | HPTE_R_C);
 	ptel = rev->guest_rpte |= rcbits;
 	gfn = hpte_rpn(ptel, hpte_page_size(hpte_v, ptel));
-	memslot = __gfn_to_memslot(kvm_memslots(kvm), gfn);
+	memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
 	if (!memslot)
 		return;
 
@@ -192,7 +192,7 @@ long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
 	/* Find the memslot (if any) for this address */
 	gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
 	gfn = gpa >> PAGE_SHIFT;
-	memslot = __gfn_to_memslot(kvm_memslots(kvm), gfn);
+	memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
 	pa = 0;
 	is_io = ~0ul;
 	rmap = NULL;
@@ -670,7 +670,7 @@ long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
 
 			psize = hpte_page_size(v, r);
 			gfn = ((r & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
-			memslot = __gfn_to_memslot(kvm_memslots(kvm), gfn);
+			memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
 			if (memslot) {
 				hva = __gfn_to_hva_memslot(memslot, gfn);
 				pte = lookup_linux_pte_and_update(pgdir, hva,
Signature · 1 line
1.9.rc1
#11 lore raw

PATCH 7/8 KVM: PPC: Book3S HV: Fix decrementer timeouts with non-zero TB offset

In reply to Paul Mackerras

Commit c7699822bc21 ("KVM: PPC: Book3S HV: Make physical thread 0 do the MMU switching") reordered the guest entry/exit code so that most of the guest register save/restore code happened in guest MMU context. A side effect of that is that the timebase still contains the guest timebase value at the point where we compute and use vcpu->arch.dec_expires, and therefore that is now a guest timebase value rather than a host timebase value. That in turn means that the timeouts computed in kvmppc_set_timer() are wrong if the timebase offset for the guest is non-zero. The consequence of that is things such as "sleep 1" in a guest after migration may sleep for much longer than they should.

This fixes the problem by converting between guest and host timebase values as necessary, by adding or subtracting the timebase offset. This also fixes an incorrect comment.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +9 -1
index 6c8dca7..0e6d9e5 100644
@@ -833,6 +833,10 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 	 * Set the decrementer to the guest decrementer.
 	 */
 	ld	r8,VCPU_DEC_EXPIRES(r4)
+	/* r8 is a host timebase value here, convert to guest TB */
+	ld	r5,HSTATE_KVM_VCORE(r13)
+	ld	r6,VCORE_TB_OFFSET(r5)
+	add	r8,r8,r6
 	mftb	r7
 	subf	r3,r7,r8
 	mtspr	SPRN_DEC,r3
@@ -1196,6 +1200,10 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
 	mftb	r6
 	extsw	r5,r5
 	add	r5,r5,r6
+	/* r5 is a guest timebase value here, convert to host TB */
+	ld	r3,HSTATE_KVM_VCORE(r13)
+	ld	r4,VCORE_TB_OFFSET(r3)
+	subf	r5,r4,r5
 	std	r5,VCPU_DEC_EXPIRES(r9)
 
 BEGIN_FTR_SECTION
@@ -1471,7 +1479,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	ld	r8,VCORE_TB_OFFSET(r5)
 	cmpdi	r8,0
 	beq	17f
-	mftb	r6			/* current host timebase */
+	mftb	r6			/* current guest timebase */
 	subf	r8,r8,r6
 	mtspr	SPRN_TBU40,r8		/* update upper 40 bits */
 	mftb	r7			/* check if lower 24 bits overflowed */
Signature · 1 line
1.9.rc1
#12 lore raw

PATCH 8/8 KVM: PPC: Book3S HV: Save/restore host PMU registers that are new in POWER8

In reply to Paul Mackerras

Currently we save the host PMU configuration, counter values, etc., when entering a guest, and restore it on return from the guest. (We have to do this because the guest has control of the PMU while it is executing.) However, we missed saving/restoring the SIAR and SDAR registers, as well as the registers which are new on POWER8, namely SIER and MMCR2.

This adds code to save the values of these registers when entering the guest and restore them on exit. This also works around the bug in POWER8 where setting PMAE with a counter already negative doesn't generate an interrupt.

---
 arch/powerpc/include/asm/kvm_book3s_asm.h |  2 +-
 arch/powerpc/kvm/book3s_hv_interrupts.S   | 22 ++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   | 10 ++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_book3s_asm.h +1 -1
index f3a91dc..821725c 100644
@@ -94,7 +94,7 @@ struct kvmppc_host_state {
 	unsigned long xics_phys;
 	u32 saved_xirr;
 	u64 dabr;
-	u64 host_mmcr[3];
+	u64 host_mmcr[7];	/* MMCR 0,1,A, SIAR, SDAR, MMCR2, SIER */
 	u32 host_pmc[8];
 	u64 host_purr;
 	u64 host_spurr;
arch/powerpc/kvm/book3s_hv_interrupts.S +22 -0
index e873796..e18e3cf 100644
@@ -71,6 +71,14 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 	mtmsrd  r10,1
 
 	/* Save host PMU registers */
+BEGIN_FTR_SECTION
+	/* Work around P8 PMAE bug */
+	li	r3, -1
+	clrrdi	r3, r3, 10
+	mfspr	r8, SPRN_MMCR2
+	mtspr	SPRN_MMCR2, r3		/* freeze all counters using MMCR2 */
+	isync
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	li	r3, 1
 	sldi	r3, r3, 31		/* MMCR0_FC (freeze counters) bit */
 	mfspr	r7, SPRN_MMCR0		/* save MMCR0 */
@@ -87,9 +95,18 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
 	cmpwi	r5, 0
 	beq	31f			/* skip if not */
 	mfspr	r5, SPRN_MMCR1
+	mfspr	r9, SPRN_SIAR
+	mfspr	r10, SPRN_SDAR
 	std	r7, HSTATE_MMCR(r13)
 	std	r5, HSTATE_MMCR + 8(r13)
 	std	r6, HSTATE_MMCR + 16(r13)
+	std	r9, HSTATE_MMCR + 24(r13)
+	std	r10, HSTATE_MMCR + 32(r13)
+BEGIN_FTR_SECTION
+	mfspr	r9, SPRN_SIER
+	std	r8, HSTATE_MMCR + 40(r13)
+	std	r9, HSTATE_MMCR + 48(r13)
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	mfspr	r3, SPRN_PMC1
 	mfspr	r5, SPRN_PMC2
 	mfspr	r6, SPRN_PMC3
@@ -110,6 +127,11 @@ BEGIN_FTR_SECTION
 	stw	r10, HSTATE_PMC + 24(r13)
 	stw	r11, HSTATE_PMC + 28(r13)
 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
+BEGIN_FTR_SECTION
+	mfspr	r9, SPRN_SIER
+	std	r8, HSTATE_MMCR + 40(r13)
+	std	r9, HSTATE_MMCR + 48(r13)
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 31:
 
 	/*
arch/powerpc/kvm/book3s_hv_rmhandlers.S +10 -0
index 0e6d9e5..9be5b3a 100644
@@ -109,8 +109,18 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
 	ld	r3, HSTATE_MMCR(r13)
 	ld	r4, HSTATE_MMCR + 8(r13)
 	ld	r5, HSTATE_MMCR + 16(r13)
+	ld	r6, HSTATE_MMCR + 24(r13)
+	ld	r7, HSTATE_MMCR + 32(r13)
 	mtspr	SPRN_MMCR1, r4
 	mtspr	SPRN_MMCRA, r5
+	mtspr	SPRN_SIAR, r6
+	mtspr	SPRN_SDAR, r7
+BEGIN_FTR_SECTION
+	ld	r8, HSTATE_MMCR + 40(r13)
+	ld	r9, HSTATE_MMCR + 48(r13)
+	mtspr	SPRN_MMCR2, r8
+	mtspr	SPRN_SIER, r9
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	mtspr	SPRN_MMCR0, r3
 	isync
 23:
Signature · 1 line
1.9.rc1
Acked-by on the v1 series
#13 lore raw
In reply to Paul Mackerras
Quoted 19 lines · Paul Mackerras

This series of patches fixes some bugs in HV-mode KVM for PowerPC Book 3S and finishes off adding the support for POWER8. Patches 2 and 3 are the two patches from the series I posted in January that Alex Graf didn't apply at that stage. I have updated them according to his review comments. The last patch is also POWER8-related, adding code to save and restore more of the host state of the PMU. (We context-switch the PMU between host and guest since the guest can access the PMU directly.) The remaining patches fix bugs that have been found over the last few months of testing.

This patch series is based on the merge of the "queue" branch of the kvm tree with the "kvm-ppc-queue" branch of Alex Graf's tree, though I expect they would apply cleanly against the kvm tree "queue" branch also.

I would like these to go into 3.15. Scott, please ack.

Paul.

Quoted 9 lines

---
[PATCH 1/8] KVM: PPC: Book3S HV: Fix KVM hang with CONFIG_KVM_XICS=n [PATCH 2/8] KVM: PPC: Book3S HV: Add transactional memory support [PATCH 3/8] KVM: PPC: Book3S HV: Add get/set_one_reg for new TM state [PATCH 4/8] KVM: PPC: Book3S: Trim top 4 bits of physical address in [PATCH 5/8] KVM: PPC: Book3S HV: Return ENODEV error rather than EIO [PATCH 6/8] KVM: PPC: Book3S HV: Don't use kvm_memslots() in real [PATCH 7/8] KVM: PPC: Book3S HV: Fix decrementer timeouts with [PATCH 8/8] KVM: PPC: Book3S HV: Save/restore host PMU registers that

-Scott