All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly
@ 2017-07-31  0:41 Paul Mackerras
  2017-07-31  0:42 ` [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit Paul Mackerras
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Paul Mackerras @ 2017-07-31  0:41 UTC (permalink / raw)
  To: stable

commit ca8efa1df1d15a1795a2da57f9f6aada6ed6b946 upstream.

This adds code to save the values of three SPRs (special-purpose
registers) used by userspace to control event-based branches (EBBs),
which are essentially interrupts that get delivered directly to
userspace.  These registers are loaded up with guest values when
entering the guest, and their values are saved when exiting the
guest, but we were not saving the host values and restoring them
before going back to userspace.

On POWER8 this would only affect userspace programs which explicitly
request the use of EBBs and also use the KVM_RUN ioctl, since the
only source of EBBs on POWER8 is the PMU, and there is an explicit
enable bit in the PMU registers (and those PMU registers do get
properly context-switched between host and guest).  On POWER9 there
is provision for externally-generated EBBs, and these are not subject
to the control in the PMU registers.

Since these registers only affect userspace, we can save them when
we first come in from userspace and restore them before returning to
userspace, rather than saving/restoring the host values on every
guest entry/exit.  Similarly, we don't need to worry about their
values on offline secondary threads since they execute in the context
of the idle task, which never executes in userspace.

Fixes: b005255e12a3 ("KVM: PPC: Book3S HV: Context-switch new POWER8 SPRs", 2014-01-08)
Cc: stable@vger.kernel.org # v3.14+
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
---
 arch/powerpc/kvm/book3s_hv.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index 396dc44e783b..fb847d9be9ae 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -2687,6 +2687,7 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 {
 	int r;
 	int srcu_idx;
+	unsigned long ebb_regs[3] = {};	/* shut up GCC */
 
 	if (!vcpu->arch.sane) {
 		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
@@ -2736,6 +2737,14 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	flush_fp_to_thread(current);
 	flush_altivec_to_thread(current);
 	flush_vsx_to_thread(current);
+
+	/* Save userspace EBB register values */
+	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
+		ebb_regs[0] = mfspr(SPRN_EBBHR);
+		ebb_regs[1] = mfspr(SPRN_EBBRR);
+		ebb_regs[2] = mfspr(SPRN_BESCR);
+	}
+
 	vcpu->arch.wqp = &vcpu->arch.vcore->wq;
 	vcpu->arch.pgdir = current->mm->pgd;
 	vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
@@ -2757,6 +2766,13 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 		}
 	} while (is_kvmppc_resume_guest(r));
 
+	/* Restore userspace EBB register values */
+	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
+		mtspr(SPRN_EBBHR, ebb_regs[0]);
+		mtspr(SPRN_EBBRR, ebb_regs[1]);
+		mtspr(SPRN_BESCR, ebb_regs[2]);
+	}
+
  out:
 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
 	atomic_dec(&vcpu->kvm->arch.vcpus_running);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit
  2017-07-31  0:41 [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Paul Mackerras
@ 2017-07-31  0:42 ` Paul Mackerras
  2017-08-03 19:19   ` Greg KH
  2017-07-31  0:43 ` [PATCH 3/4 v4.4.y] KVM: PPC: Book3S HV: Reload HTM registers explicitly Paul Mackerras
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Paul Mackerras @ 2017-07-31  0:42 UTC (permalink / raw)
  To: stable

commit 4c3bb4ccd074e1a0552078c0bf94c662367a1658 upstream.

This restores several special-purpose registers (SPRs) to sane values
on guest exit that were missed before.

TAR and VRSAVE are readable and writable by userspace, and we need to
save and restore them to prevent the guest from potentially affecting
userspace execution (not that TAR or VRSAVE are used by any known
program that run uses the KVM_RUN ioctl).  We save/restore these
in kvmppc_vcpu_run_hv() rather than on every guest entry/exit.

FSCR affects userspace execution in that it can prohibit access to
certain facilities by userspace.  We save/restore it like we do
for TAR and VRSAVE.

PSPB is normally 0.  We restore it to 0 on guest exit to prevent
userspace taking advantage of the guest having set it non-zero
(which would allow userspace to set its SMT priority to high).

UAMOR is normally 0.  We restore it to 0 on guest exit to prevent
the AMR from being used as a covert channel between userspace
processes, since the AMR is not context-switched at present.

[paulus@ozlabs.org - removed IAMR bits that are only needed on POWER9;
 adjusted FSCR save/restore for lack of fscr field in thread_struct.]

Fixes: b005255e12a3 ("KVM: PPC: Book3S HV: Context-switch new POWER8 SPRs", 2014-01-08)
Cc: stable@vger.kernel.org # v3.14+
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
---
 arch/powerpc/kvm/book3s_hv.c            | 13 +++++++++++--
 arch/powerpc/kvm/book3s_hv_rmhandlers.S |  2 ++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index fb847d9be9ae..b5eeaa1824df 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -2688,6 +2688,9 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	int r;
 	int srcu_idx;
 	unsigned long ebb_regs[3] = {};	/* shut up GCC */
+	unsigned long user_tar = 0;
+	unsigned long proc_fscr = 0;
+	unsigned int user_vrsave;
 
 	if (!vcpu->arch.sane) {
 		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
@@ -2738,12 +2741,15 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	flush_altivec_to_thread(current);
 	flush_vsx_to_thread(current);
 
-	/* Save userspace EBB register values */
+	/* Save userspace EBB and other register values */
 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
 		ebb_regs[0] = mfspr(SPRN_EBBHR);
 		ebb_regs[1] = mfspr(SPRN_EBBRR);
 		ebb_regs[2] = mfspr(SPRN_BESCR);
+		user_tar = mfspr(SPRN_TAR);
+		proc_fscr = mfspr(SPRN_FSCR);
 	}
+	user_vrsave = mfspr(SPRN_VRSAVE);
 
 	vcpu->arch.wqp = &vcpu->arch.vcore->wq;
 	vcpu->arch.pgdir = current->mm->pgd;
@@ -2766,12 +2772,15 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 		}
 	} while (is_kvmppc_resume_guest(r));
 
-	/* Restore userspace EBB register values */
+	/* Restore userspace EBB and other register values */
 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
 		mtspr(SPRN_EBBHR, ebb_regs[0]);
 		mtspr(SPRN_EBBRR, ebb_regs[1]);
 		mtspr(SPRN_BESCR, ebb_regs[2]);
+		mtspr(SPRN_TAR, user_tar);
+		mtspr(SPRN_FSCR, proc_fscr);
 	}
+	mtspr(SPRN_VRSAVE, user_vrsave);
 
  out:
 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index 1a743f87b37d..2094fd55ba7f 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -1268,6 +1268,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 	mtspr	SPRN_IAMR, r0
 	mtspr	SPRN_CIABR, r0
 	mtspr	SPRN_DAWRX, r0
+	mtspr	SPRN_PSPB, r0
 	mtspr	SPRN_TCSCR, r0
 	mtspr	SPRN_WORT, r0
 	/* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
@@ -1283,6 +1284,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 	std	r6,VCPU_UAMOR(r9)
 	li	r6,0
 	mtspr	SPRN_AMR,r6
+	mtspr	SPRN_UAMOR, r6
 
 	/* Switch DSCR back to host value */
 	mfspr	r8, SPRN_DSCR
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/4 v4.4.y] KVM: PPC: Book3S HV: Reload HTM registers explicitly
  2017-07-31  0:41 [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Paul Mackerras
  2017-07-31  0:42 ` [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit Paul Mackerras
@ 2017-07-31  0:43 ` Paul Mackerras
  2017-08-03 19:21   ` Patch "KVM: PPC: Book3S HV: Reload HTM registers explicitly" has been added to the 4.4-stable tree gregkh
  2017-07-31  0:44 ` [PATCH 4/4 v4.4.y] KVM: PPC: Book3S HV: Save/restore host values of debug registers Paul Mackerras
  2017-08-03 19:19 ` [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Greg KH
  3 siblings, 1 reply; 9+ messages in thread
From: Paul Mackerras @ 2017-07-31  0:43 UTC (permalink / raw)
  To: stable

Commit 46a704f8409f ("KVM: PPC: Book3S HV: Preserve userspace HTM
state properly", 2017-06-15) added code which assumes that the kernel
is able to handle a TM (transactional memory) unavailable interrupt
from userspace by reloading the TM-related registers and enabling TM
for the process.  That ability was added in the 4.9 kernel; earlier
kernel versions simply panic on getting the TM unavailable interrupt.

Since commit 46a704f8409f has been backported to the 4.4 stable tree
as commit 824b9506e4f2, 4.4.75 and subsequent versions are vulnerable
to a userspace-triggerable panic.

This patch fixes the problem by explicitly reloading the TM-related
registers before returning to userspace, rather than disabling TM
for the process.

Commit 46a704f8409f also failed to enable TM for the kernel, leading
to a TM unavailable interrupt in the kernel, causing an oops.  This
fixes that problem too, by enabling TM before accessing the TM
registers.  That problem is fixed upstream by the patch "KVM: PPC:
Book3S HV: Enable TM before accessing TM registers".

Fixes: 824b9506e4f2 ("KVM: PPC: Book3S HV: Preserve userspace HTM state properly")
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
---
 arch/powerpc/kvm/book3s_hv.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index b5eeaa1824df..428563b195c3 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -2711,10 +2711,11 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 			run->fail_entry.hardware_entry_failure_reason = 0;
 			return -EINVAL;
 		}
+		/* Enable TM so we can read the TM SPRs */
+		mtmsr(mfmsr() | MSR_TM);
 		current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
 		current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
 		current->thread.tm_texasr = mfspr(SPRN_TEXASR);
-		current->thread.regs->msr &= ~MSR_TM;
 	}
 #endif
 
@@ -2782,6 +2783,19 @@ static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	}
 	mtspr(SPRN_VRSAVE, user_vrsave);
 
+	/*
+	 * Since we don't do lazy TM reload, we need to reload
+	 * the TM registers here.
+	 */
+#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
+	if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
+	    (current->thread.regs->msr & MSR_TM)) {
+		mtspr(SPRN_TFHAR, current->thread.tm_tfhar);
+		mtspr(SPRN_TFIAR, current->thread.tm_tfiar);
+		mtspr(SPRN_TEXASR, current->thread.tm_texasr);
+	}
+#endif
+
  out:
 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
 	atomic_dec(&vcpu->kvm->arch.vcpus_running);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/4 v4.4.y] KVM: PPC: Book3S HV: Save/restore host values of debug registers
  2017-07-31  0:41 [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Paul Mackerras
  2017-07-31  0:42 ` [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit Paul Mackerras
  2017-07-31  0:43 ` [PATCH 3/4 v4.4.y] KVM: PPC: Book3S HV: Reload HTM registers explicitly Paul Mackerras
@ 2017-07-31  0:44 ` Paul Mackerras
  2017-08-03 19:19 ` [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Greg KH
  3 siblings, 0 replies; 9+ messages in thread
From: Paul Mackerras @ 2017-07-31  0:44 UTC (permalink / raw)
  To: stable

commit 7ceaa6dcd8c6f59588428cec37f3c8093dd1011f upstream.

At present, HV KVM on POWER8 and POWER9 machines loses any instruction
or data breakpoint set in the host whenever a guest is run.
Instruction breakpoints are currently only used by xmon, but ptrace
and the perf_event subsystem can set data breakpoints as well as xmon.

To fix this, we save the host values of the debug registers (CIABR,
DAWR and DAWRX) before entering the guest and restore them on exit.

[paulus@ozlabs.org - Adjusted stack offsets since we aren't saving
 POWER9-specific registers.]

Fixes: b005255e12a3 ("KVM: PPC: Book3S HV: Context-switch new POWER8 SPRs", 2014-01-08)
Cc: stable@vger.kernel.org # v3.14+
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 39 +++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index 2094fd55ba7f..ffab9269bfe4 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -36,6 +36,13 @@
 #define NAPPING_CEDE	1
 #define NAPPING_NOVCPU	2
 
+/* Stack frame offsets for kvmppc_hv_entry */
+#define SFS			112
+#define STACK_SLOT_TRAP		(SFS-4)
+#define STACK_SLOT_CIABR	(SFS-16)
+#define STACK_SLOT_DAWR		(SFS-24)
+#define STACK_SLOT_DAWRX	(SFS-32)
+
 /*
  * Call kvmppc_hv_entry in real mode.
  * Must be called with interrupts hard-disabled.
@@ -274,10 +281,10 @@ kvm_novcpu_exit:
 	bl	kvmhv_accumulate_time
 #endif
 13:	mr	r3, r12
-	stw	r12, 112-4(r1)
+	stw	r12, STACK_SLOT_TRAP(r1)
 	bl	kvmhv_commence_exit
 	nop
-	lwz	r12, 112-4(r1)
+	lwz	r12, STACK_SLOT_TRAP(r1)
 	b	kvmhv_switch_to_host
 
 /*
@@ -489,7 +496,7 @@ kvmppc_hv_entry:
 	 */
 	mflr	r0
 	std	r0, PPC_LR_STKOFF(r1)
-	stdu	r1, -112(r1)
+	stdu	r1, -SFS(r1)
 
 	/* Save R1 in the PACA */
 	std	r1, HSTATE_HOST_R1(r13)
@@ -643,6 +650,16 @@ kvmppc_got_guest:
 	mtspr	SPRN_PURR,r7
 	mtspr	SPRN_SPURR,r8
 
+	/* Save host values of some registers */
+BEGIN_FTR_SECTION
+	mfspr	r5, SPRN_CIABR
+	mfspr	r6, SPRN_DAWR
+	mfspr	r7, SPRN_DAWRX
+	std	r5, STACK_SLOT_CIABR(r1)
+	std	r6, STACK_SLOT_DAWR(r1)
+	std	r7, STACK_SLOT_DAWRX(r1)
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
+
 BEGIN_FTR_SECTION
 	/* Set partition DABR */
 	/* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
@@ -1266,8 +1283,6 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
 	 */
 	li	r0, 0
 	mtspr	SPRN_IAMR, r0
-	mtspr	SPRN_CIABR, r0
-	mtspr	SPRN_DAWRX, r0
 	mtspr	SPRN_PSPB, r0
 	mtspr	SPRN_TCSCR, r0
 	mtspr	SPRN_WORT, r0
@@ -1426,6 +1441,16 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	slbia
 	ptesync
 
+	/* Restore host values of some registers */
+BEGIN_FTR_SECTION
+	ld	r5, STACK_SLOT_CIABR(r1)
+	ld	r6, STACK_SLOT_DAWR(r1)
+	ld	r7, STACK_SLOT_DAWRX(r1)
+	mtspr	SPRN_CIABR, r5
+	mtspr	SPRN_DAWR, r6
+	mtspr	SPRN_DAWRX, r7
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
+
 	/*
 	 * POWER7/POWER8 guest -> host partition switch code.
 	 * We don't have to lock against tlbies but we do
@@ -1535,8 +1560,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 	li	r0, KVM_GUEST_MODE_NONE
 	stb	r0, HSTATE_IN_GUEST(r13)
 
-	ld	r0, 112+PPC_LR_STKOFF(r1)
-	addi	r1, r1, 112
+	ld	r0, SFS+PPC_LR_STKOFF(r1)
+	addi	r1, r1, SFS
 	mtlr	r0
 	blr
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly
  2017-07-31  0:41 [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Paul Mackerras
                   ` (2 preceding siblings ...)
  2017-07-31  0:44 ` [PATCH 4/4 v4.4.y] KVM: PPC: Book3S HV: Save/restore host values of debug registers Paul Mackerras
@ 2017-08-03 19:19 ` Greg KH
  2017-08-04  4:27   ` Paul Mackerras
  3 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2017-08-03 19:19 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: stable

On Mon, Jul 31, 2017 at 10:41:54AM +1000, Paul Mackerras wrote:
> commit ca8efa1df1d15a1795a2da57f9f6aada6ed6b946 upstream.
> 
> This adds code to save the values of three SPRs (special-purpose
> registers) used by userspace to control event-based branches (EBBs),
> which are essentially interrupts that get delivered directly to
> userspace.  These registers are loaded up with guest values when
> entering the guest, and their values are saved when exiting the
> guest, but we were not saving the host values and restoring them
> before going back to userspace.

I already took this patch, right?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit
  2017-07-31  0:42 ` [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit Paul Mackerras
@ 2017-08-03 19:19   ` Greg KH
  2017-08-04  4:41     ` Paul Mackerras
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2017-08-03 19:19 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: stable

On Mon, Jul 31, 2017 at 10:42:41AM +1000, Paul Mackerras wrote:
> commit 4c3bb4ccd074e1a0552078c0bf94c662367a1658 upstream.
> 
> This restores several special-purpose registers (SPRs) to sane values
> on guest exit that were missed before.
> 
> TAR and VRSAVE are readable and writable by userspace, and we need to
> save and restore them to prevent the guest from potentially affecting
> userspace execution (not that TAR or VRSAVE are used by any known
> program that run uses the KVM_RUN ioctl).  We save/restore these
> in kvmppc_vcpu_run_hv() rather than on every guest entry/exit.

I also already have this one, right?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Patch "KVM: PPC: Book3S HV: Reload HTM registers explicitly" has been added to the 4.4-stable tree
  2017-07-31  0:43 ` [PATCH 3/4 v4.4.y] KVM: PPC: Book3S HV: Reload HTM registers explicitly Paul Mackerras
@ 2017-08-03 19:21   ` gregkh
  0 siblings, 0 replies; 9+ messages in thread
From: gregkh @ 2017-08-03 19:21 UTC (permalink / raw)
  To: paulus, gregkh; +Cc: stable, stable-commits


This is a note to let you know that I've just added the patch titled

    KVM: PPC: Book3S HV: Reload HTM registers explicitly

to the 4.4-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     kvm-ppc-book3s-hv-reload-htm-registers-explicitly.patch
and it can be found in the queue-4.4 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@vger.kernel.org> know about it.


>From paulus@ozlabs.org  Thu Aug  3 12:20:26 2017
From: Paul Mackerras <paulus@ozlabs.org>
Date: Mon, 31 Jul 2017 10:43:37 +1000
Subject: KVM: PPC: Book3S HV: Reload HTM registers explicitly
To: stable@vger.kernel.org
Message-ID: <20170731004337.girl42lozwohoobi@oak.ozlabs.ibm.com>
Content-Disposition: inline

From: Paul Mackerras <paulus@ozlabs.org>

Commit 46a704f8409f ("KVM: PPC: Book3S HV: Preserve userspace HTM
state properly", 2017-06-15) added code which assumes that the kernel
is able to handle a TM (transactional memory) unavailable interrupt
from userspace by reloading the TM-related registers and enabling TM
for the process.  That ability was added in the 4.9 kernel; earlier
kernel versions simply panic on getting the TM unavailable interrupt.

Since commit 46a704f8409f has been backported to the 4.4 stable tree
as commit 824b9506e4f2, 4.4.75 and subsequent versions are vulnerable
to a userspace-triggerable panic.

This patch fixes the problem by explicitly reloading the TM-related
registers before returning to userspace, rather than disabling TM
for the process.

Commit 46a704f8409f also failed to enable TM for the kernel, leading
to a TM unavailable interrupt in the kernel, causing an oops.  This
fixes that problem too, by enabling TM before accessing the TM
registers.  That problem is fixed upstream by the patch "KVM: PPC:
Book3S HV: Enable TM before accessing TM registers".

Fixes: 824b9506e4f2 ("KVM: PPC: Book3S HV: Preserve userspace HTM state properly")
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 arch/powerpc/kvm/book3s_hv.c |   16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -2711,10 +2711,11 @@ static int kvmppc_vcpu_run_hv(struct kvm
 			run->fail_entry.hardware_entry_failure_reason = 0;
 			return -EINVAL;
 		}
+		/* Enable TM so we can read the TM SPRs */
+		mtmsr(mfmsr() | MSR_TM);
 		current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
 		current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
 		current->thread.tm_texasr = mfspr(SPRN_TEXASR);
-		current->thread.regs->msr &= ~MSR_TM;
 	}
 #endif
 
@@ -2782,6 +2783,19 @@ static int kvmppc_vcpu_run_hv(struct kvm
 	}
 	mtspr(SPRN_VRSAVE, user_vrsave);
 
+	/*
+	 * Since we don't do lazy TM reload, we need to reload
+	 * the TM registers here.
+	 */
+#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
+	if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
+	    (current->thread.regs->msr & MSR_TM)) {
+		mtspr(SPRN_TFHAR, current->thread.tm_tfhar);
+		mtspr(SPRN_TFIAR, current->thread.tm_tfiar);
+		mtspr(SPRN_TEXASR, current->thread.tm_texasr);
+	}
+#endif
+
  out:
 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
 	atomic_dec(&vcpu->kvm->arch.vcpus_running);


Patches currently in stable-queue which might be from paulus@ozlabs.org are

queue-4.4/kvm-ppc-book3s-hv-save-restore-host-values-of-debug-registers.patch
queue-4.4/kvm-ppc-book3s-hv-context-switch-ebb-registers-properly.patch
queue-4.4/kvm-ppc-book3s-hv-restore-critical-sprs-to-host-values-on-guest-exit.patch
queue-4.4/kvm-ppc-book3s-hv-reload-htm-registers-explicitly.patch

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly
  2017-08-03 19:19 ` [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Greg KH
@ 2017-08-04  4:27   ` Paul Mackerras
  0 siblings, 0 replies; 9+ messages in thread
From: Paul Mackerras @ 2017-08-04  4:27 UTC (permalink / raw)
  To: Greg KH; +Cc: stable

On Thu, Aug 03, 2017 at 12:19:30PM -0700, Greg KH wrote:
> On Mon, Jul 31, 2017 at 10:41:54AM +1000, Paul Mackerras wrote:
> > commit ca8efa1df1d15a1795a2da57f9f6aada6ed6b946 upstream.
> > 
> > This adds code to save the values of three SPRs (special-purpose
> > registers) used by userspace to control event-based branches (EBBs),
> > which are essentially interrupts that get delivered directly to
> > userspace.  These registers are loaded up with guest values when
> > entering the guest, and their values are saved when exiting the
> > guest, but we were not saving the host values and restoring them
> > before going back to userspace.
> 
> I already took this patch, right?

Yes, you did.  Thanks.

Paul.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit
  2017-08-03 19:19   ` Greg KH
@ 2017-08-04  4:41     ` Paul Mackerras
  0 siblings, 0 replies; 9+ messages in thread
From: Paul Mackerras @ 2017-08-04  4:41 UTC (permalink / raw)
  To: Greg KH; +Cc: stable

On Thu, Aug 03, 2017 at 12:19:49PM -0700, Greg KH wrote:
> On Mon, Jul 31, 2017 at 10:42:41AM +1000, Paul Mackerras wrote:
> > commit 4c3bb4ccd074e1a0552078c0bf94c662367a1658 upstream.
> > 
> > This restores several special-purpose registers (SPRs) to sane values
> > on guest exit that were missed before.
> > 
> > TAR and VRSAVE are readable and writable by userspace, and we need to
> > save and restore them to prevent the guest from potentially affecting
> > userspace execution (not that TAR or VRSAVE are used by any known
> > program that run uses the KVM_RUN ioctl).  We save/restore these
> > in kvmppc_vcpu_run_hv() rather than on every guest entry/exit.
> 
> I also already have this one, right?
> 

Right.

Paul.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-08-04  4:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-31  0:41 [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Paul Mackerras
2017-07-31  0:42 ` [PATCH 2/4 v4.4.y] KVM: PPC: Book3S HV: Restore critical SPRs to host values on guest exit Paul Mackerras
2017-08-03 19:19   ` Greg KH
2017-08-04  4:41     ` Paul Mackerras
2017-07-31  0:43 ` [PATCH 3/4 v4.4.y] KVM: PPC: Book3S HV: Reload HTM registers explicitly Paul Mackerras
2017-08-03 19:21   ` Patch "KVM: PPC: Book3S HV: Reload HTM registers explicitly" has been added to the 4.4-stable tree gregkh
2017-07-31  0:44 ` [PATCH 4/4 v4.4.y] KVM: PPC: Book3S HV: Save/restore host values of debug registers Paul Mackerras
2017-08-03 19:19 ` [PATCH 1/4 v4.4.y] KVM: PPC: Book3S HV: Context-switch EBB registers properly Greg KH
2017-08-04  4:27   ` Paul Mackerras

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.