Skip to contentSkip to conversation

a few KVM patches

PATCH In review Nicholas Piggin 3 participants 14 messages Jan 18, 2021 – Feb 10, 2021

Subsystems (2)
lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

cover a few KVM patches

These patches are unrelated except touching some of the same code. The first two fix actual guest exploitable issues, the other two try to tidy up SLB management slightly.

Thanks,
Nick

Nicholas Piggin (4):
KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host
without mixed mode support
KVM: PPC: Book3S HV: Fix radix guest SLB side channel
KVM: PPC: Book3S HV: No need to clear radix host SLB before loading
guest
KVM: PPC: Book3S HV: Use POWER9 SLBIA IH=6 variant to clear SLB

 arch/powerpc/include/asm/kvm_book3s_asm.h |  11 --
 arch/powerpc/kernel/asm-offsets.c         |   3 -
 arch/powerpc/kvm/book3s_hv.c              |  56 ++--------
 arch/powerpc/kvm/book3s_hv_builtin.c      | 108 +-----------------
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   | 129 ++++++++++------------
 5 files changed, 70 insertions(+), 237 deletions(-)
Signature · 1 line
2.23.0
author reviewer
#1 lore raw

PATCH 1/4 KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed mode

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

---
 arch/powerpc/include/asm/kvm_book3s_asm.h |  11 ---
 arch/powerpc/kernel/asm-offsets.c         |   3 -
 arch/powerpc/kvm/book3s_hv.c              |  56 +++--------
 arch/powerpc/kvm/book3s_hv_builtin.c      | 108 +---------------------
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   |  80 ++++------------
 5 files changed, 32 insertions(+), 226 deletions(-)
arch/powerpc/include/asm/kvm_book3s_asm.h +0 -11
index 078f4648ea27..b6d31bff5209 100644
@@ -74,16 +74,6 @@ struct kvm_split_mode {
 	u8		do_nap;
 	u8		napped[MAX_SMT_THREADS];
 	struct kvmppc_vcore *vc[MAX_SUBCORES];
-	/* Bits for changing lpcr on P9 */
-	unsigned long	lpcr_req;
-	unsigned long	lpidr_req;
-	unsigned long	host_lpcr;
-	u32		do_set;
-	u32		do_restore;
-	union {
-		u32	allphases;
-		u8	phase[4];
-	} lpcr_sync;
 };
 
 /*
@@ -110,7 +100,6 @@ struct kvmppc_host_state {
 	u8 hwthread_state;
 	u8 host_ipi;
 	u8 ptid;		/* thread number within subcore when split */
-	u8 tid;			/* thread number within whole core */
 	u8 fake_suspend;
 	struct kvm_vcpu *kvm_vcpu;
 	struct kvmppc_vcore *kvm_vcore;
arch/powerpc/kernel/asm-offsets.c +0 -3
index b12d7c049bfe..489a22cf1a92 100644
@@ -668,7 +668,6 @@ int main(void)
 	HSTATE_FIELD(HSTATE_SAVED_XIRR, saved_xirr);
 	HSTATE_FIELD(HSTATE_HOST_IPI, host_ipi);
 	HSTATE_FIELD(HSTATE_PTID, ptid);
-	HSTATE_FIELD(HSTATE_TID, tid);
 	HSTATE_FIELD(HSTATE_FAKE_SUSPEND, fake_suspend);
 	HSTATE_FIELD(HSTATE_MMCR0, host_mmcr[0]);
 	HSTATE_FIELD(HSTATE_MMCR1, host_mmcr[1]);
@@ -698,8 +697,6 @@ int main(void)
 	OFFSET(KVM_SPLIT_LDBAR, kvm_split_mode, ldbar);
 	OFFSET(KVM_SPLIT_DO_NAP, kvm_split_mode, do_nap);
 	OFFSET(KVM_SPLIT_NAPPED, kvm_split_mode, napped);
-	OFFSET(KVM_SPLIT_DO_SET, kvm_split_mode, do_set);
-	OFFSET(KVM_SPLIT_DO_RESTORE, kvm_split_mode, do_restore);
 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
 
 #ifdef CONFIG_PPC_BOOK3S_64
arch/powerpc/kvm/book3s_hv.c +12 -44

132 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_builtin.c +1 -107

126 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_rmhandlers.S +19 -61

147 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

Re: [PATCH 1/4] KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed

Resending because the previous got spam-filtered:

Quoted 11 lines · Nicholas Piggin

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

With this patch in a DD2.1 machine + indep_threads_mode=N + disable_radix, QEMU aborts and dumps registers, is that intended?

Could we use the 'no_mixing_hpt_and_radix' logic in check_extension to advertise only KVM_CAP_PPC_MMU_RADIX to the guest via OV5 so it doesn't try to run hash?

For instance, if I hack QEMU's 'spapr_dt_ov5_platform_support' from OV5_MMU_BOTH to OV5_MMU_RADIX_300 then it boots succesfuly, but the guest turns into radix, due to this code in prom_init:

prom_parse_mmu_model:

case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */

	prom_debug("MMU - radix only\n");
	if (prom_radix_disable) {
		/*
		 * If we __have__ to do radix, we're better off ignoring
		 * the command line rather than not booting.
		 */
		prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
	}
	support->radix_mmu = true;
	break;

It seems we could explicitly say that the host does not support hash and that would align with the above code.

Quoted 3 lines

---

author reviewer
#3 lore raw

Re: [PATCH 1/4] KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed

Excerpts from Fabiano Rosas's message of January 19, 2021 11:46 am:

Quoted 16 lines

Resending because the previous got spam-filtered:

Quoted 11 lines · Nicholas Piggin

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

Quoted 2 lines

With this patch in a DD2.1 machine + indep_threads_mode=N + disable_radix, QEMU aborts and dumps registers, is that intended?

Yes. That configuration is hanging handling MCEs in the guest with some threads waiting forever to synchronize. Paul suggested it was never a supported configuration so we might just remove it.

Quoted 22 lines

Could we use the 'no_mixing_hpt_and_radix' logic in check_extension to advertise only KVM_CAP_PPC_MMU_RADIX to the guest via OV5 so it doesn't try to run hash?

For instance, if I hack QEMU's 'spapr_dt_ov5_platform_support' from OV5_MMU_BOTH to OV5_MMU_RADIX_300 then it boots succesfuly, but the guest turns into radix, due to this code in prom_init:

prom_parse_mmu_model:

case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */

	prom_debug("MMU - radix only\n");
	if (prom_radix_disable) {
		/*
		 * If we __have__ to do radix, we're better off ignoring
		 * the command line rather than not booting.
		 */
		prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
	}
	support->radix_mmu = true;
	break;
Quoted 2 lines

It seems we could explicitly say that the host does not support hash and that would align with the above code.

I'm not sure, sounds like you could, on the other hand these aborts seem like the prefered failure mode for these kinds of configuration issues, I don't know what the policy is, is reverting back to radix acceptable?

Thanks,
Nick

Tested-by
#4 lore raw

Re: [PATCH 1/4] KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed

Quoted 17 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 19, 2021 11:46 am:

Quoted 16 lines

Resending because the previous got spam-filtered:

Quoted 11 lines · Nicholas Piggin

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

Quoted 7 lines
Quoted 2 lines

With this patch in a DD2.1 machine + indep_threads_mode=N + disable_radix, QEMU aborts and dumps registers, is that intended?

Yes. That configuration is hanging handling MCEs in the guest with some threads waiting forever to synchronize. Paul suggested it was never a supported configuration so we might just remove it.

OK, so:

Quoted 22 lines
Quoted 22 lines

Could we use the 'no_mixing_hpt_and_radix' logic in check_extension to advertise only KVM_CAP_PPC_MMU_RADIX to the guest via OV5 so it doesn't try to run hash?

For instance, if I hack QEMU's 'spapr_dt_ov5_platform_support' from OV5_MMU_BOTH to OV5_MMU_RADIX_300 then it boots succesfuly, but the guest turns into radix, due to this code in prom_init:

prom_parse_mmu_model:

case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */

	prom_debug("MMU - radix only\n");
	if (prom_radix_disable) {
		/*
		 * If we __have__ to do radix, we're better off ignoring
		 * the command line rather than not booting.
		 */
		prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
	}
	support->radix_mmu = true;
	break;
Quoted 7 lines
Quoted 2 lines

It seems we could explicitly say that the host does not support hash and that would align with the above code.

I'm not sure, sounds like you could, on the other hand these aborts seem like the prefered failure mode for these kinds of configuration issues, I don't know what the policy is, is reverting back to radix acceptable?

Yeah, I couldn't find documentation about why we're reverting back to radix. I personally dislike it, but there is already a precedent so I'm not sure. A radix guest on a hash host does the same transparent conversion AFAICT.

But despite that, this patch removes support for hash MMU in this particular scenario. I don't see why continuing to tell the guest we support hash.

Anyway, here's a patch if you decide to go that way (tested w/ DD2.1 & 2.3 machines):

---

arch/powerpc/include/asm/kvm_ppc.h +1 -0
index 0a056c64c317b..53743555676d6 100644
@@ -314,6 +314,7 @@ struct kvmppc_ops {
 			      int size);
 	int (*enable_svm)(struct kvm *kvm);
 	int (*svm_off)(struct kvm *kvm);
+	bool (*hash_possible)(void);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +10 -0
index 6f612d240392f..2d1e8aba22b85 100644
@@ -5599,6 +5599,15 @@ static int kvmhv_svm_off(struct kvm *kvm)
 	return ret;
 }
 
+static bool kvmppc_hash_possible(void)
+{
+	if (radix_enabled() && no_mixing_hpt_and_radix)
+		return false;
+
+	return cpu_has_feature(CPU_FTR_ARCH_300) &&
+		cpu_has_feature(CPU_FTR_HVMODE);
+}
+
 static struct kvmppc_ops kvm_ops_hv = {
 	.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
 	.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5642,6 +5651,7 @@ static struct kvmppc_ops kvm_ops_hv = {
 	.store_to_eaddr = kvmhv_store_to_eaddr,
 	.enable_svm = kvmhv_enable_svm,
 	.svm_off = kvmhv_svm_off,
+	.hash_possible = kvmppc_hash_possible,
 };
 
 static int kvm_init_subcore_bitmap(void)
arch/powerpc/kvm/powerpc.c +1 -2
index cf52d26f49cd7..99ced6c570e74 100644
@@ -611,8 +611,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = !!(hv_enabled && radix_enabled());
 		break;
 	case KVM_CAP_PPC_MMU_HASH_V3:
-		r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
-		       cpu_has_feature(CPU_FTR_HVMODE));
+		r = !!(hv_enabled && kvmppc_hv_ops->hash_possible());
 		break;
 	case KVM_CAP_PPC_NESTED_HV:
 		r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&

---

https://github.com/farosas/linux/commit/0ae6c65ba9592c23215899c473acf392bd6e36d5.patch

Quoted 2 lines

Thanks,
Nick

author reviewer
#5 lore raw

Re: [PATCH 1/4] KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed

Excerpts from Fabiano Rosas's message of January 20, 2021 7:07 am:

Quoted 66 lines
Quoted 17 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 19, 2021 11:46 am:

Quoted 16 lines

Resending because the previous got spam-filtered:

Quoted 11 lines · Nicholas Piggin

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

Quoted 7 lines
Quoted 2 lines

With this patch in a DD2.1 machine + indep_threads_mode=N + disable_radix, QEMU aborts and dumps registers, is that intended?

Yes. That configuration is hanging handling MCEs in the guest with some threads waiting forever to synchronize. Paul suggested it was never a supported configuration so we might just remove it.

OK, so:

Quoted 22 lines
Quoted 22 lines

Could we use the 'no_mixing_hpt_and_radix' logic in check_extension to advertise only KVM_CAP_PPC_MMU_RADIX to the guest via OV5 so it doesn't try to run hash?

For instance, if I hack QEMU's 'spapr_dt_ov5_platform_support' from OV5_MMU_BOTH to OV5_MMU_RADIX_300 then it boots succesfuly, but the guest turns into radix, due to this code in prom_init:

prom_parse_mmu_model:

case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */

	prom_debug("MMU - radix only\n");
	if (prom_radix_disable) {
		/*
		 * If we __have__ to do radix, we're better off ignoring
		 * the command line rather than not booting.
		 */
		prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
	}
	support->radix_mmu = true;
	break;
Quoted 7 lines
Quoted 2 lines

It seems we could explicitly say that the host does not support hash and that would align with the above code.

I'm not sure, sounds like you could, on the other hand these aborts seem like the prefered failure mode for these kinds of configuration issues, I don't know what the policy is, is reverting back to radix acceptable?

Yeah, I couldn't find documentation about why we're reverting back to radix. I personally dislike it, but there is already a precedent so I'm not sure. A radix guest on a hash host does the same transparent conversion AFAICT.

Quoted 6 lines

But despite that, this patch removes support for hash MMU in this particular scenario. I don't see why continuing to tell the guest we support hash.

Anyway, here's a patch if you decide to go that way (tested w/ DD2.1 & 2.3 machines):

Thanks, I don't mind it, have to see if the maintainer will take it :)

You could add a small changelog / SOB and I could putit after my patch?

Quoted 20 lines

---

arch/powerpc/include/asm/kvm_ppc.h +1 -0
index 0a056c64c317b..53743555676d6 100644
@@ -314,6 +314,7 @@ struct kvmppc_ops {
 			      int size);
 	int (*enable_svm)(struct kvm *kvm);
 	int (*svm_off)(struct kvm *kvm);
+	bool (*hash_possible)(void);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +0 -0
index 6f612d240392f..2d1e8aba22b85 100644
@@ -5599,6 +5599,15 @@ static int kvmhv_svm_off(struct kvm *kvm)
 	return ret;

arch/powerpc/kvm/book3s_hv.c static int kvmhv_svm_off(struct kvm *kvm) · line 5609

Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +8 -0
 }
 
+static bool kvmppc_hash_possible(void)
+{
+	if (radix_enabled() && no_mixing_hpt_and_radix)
+		return false;
+
+	return cpu_has_feature(CPU_FTR_ARCH_300) &&
+		cpu_has_feature(CPU_FTR_HVMODE);
+}

Just be careful, it's hash_v3 specifically. Either make this return true for arch < 300 add the ARCH_300 check in the ioctl, or rename to include v3.

arch/powerpc/kvm/book3s_hv.c static struct kvmppc_ops kvm_ops_hv = { · line 5656

Quoted 11 lines
arch/powerpc/kvm/book3s_hv.c +2 -0
+
 static struct kvmppc_ops kvm_ops_hv = {
 	.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
 	.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5642,6 +5651,7 @@ static struct kvmppc_ops kvm_ops_hv = {
 	.store_to_eaddr = kvmhv_store_to_eaddr,
 	.enable_svm = kvmhv_enable_svm,
 	.svm_off = kvmhv_svm_off,
+	.hash_possible = kvmppc_hash_possible,
 };
 

How about adding an op which can check extensions? It could return false if it wasn't checked and so default to the generic checks in kvm_vm_ioctl_check_extension, and take a pointer to 'r' to set.

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +0 -0
 static int kvm_init_subcore_bitmap(void)
arch/powerpc/kvm/powerpc.c +0 -0
index cf52d26f49cd7..99ced6c570e74 100644

arch/powerpc/kvm/powerpc.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 617

Quoted 9 lines
arch/powerpc/kvm/powerpc.c +1 -2
@@ -611,8 +611,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = !!(hv_enabled && radix_enabled());
 		break;
 	case KVM_CAP_PPC_MMU_HASH_V3:
-		r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
-		       cpu_has_feature(CPU_FTR_HVMODE));
+		r = !!(hv_enabled && kvmppc_hv_ops->hash_possible());
 		break;
 	case KVM_CAP_PPC_NESTED_HV:
 		r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&

Thanks,
Nick

#6 lore raw

Re: [PATCH 1/4] KVM: PPC: Book3S HV: Remove support for running HPT guest on RPT host without mixed

Quoted 71 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 20, 2021 7:07 am:

Quoted 70 lines
Quoted 17 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 19, 2021 11:46 am:

Quoted 16 lines

Resending because the previous got spam-filtered:

Quoted 11 lines · Nicholas Piggin

This reverts much of commit c01015091a770 ("KVM: PPC: Book3S HV: Run HPT guests on POWER9 radix hosts"), which was required to run HPT guests on RPT hosts on early POWER9 CPUs without support for "mixed mode", which meant the host could not run with MMU on while guests were running.

This code has some corner case bugs, e.g., when the guest hits a machine check or HMI the primary locks up waiting for secondaries to switch LPCR to host, which they never do. This could all be fixed in software, but most CPUs in production have mixed mode support, and those that don't are believed to be all in installations that don't use this capability. So simplify things and remove support.

Quoted 7 lines
Quoted 2 lines

With this patch in a DD2.1 machine + indep_threads_mode=N + disable_radix, QEMU aborts and dumps registers, is that intended?

Yes. That configuration is hanging handling MCEs in the guest with some threads waiting forever to synchronize. Paul suggested it was never a supported configuration so we might just remove it.

OK, so:

Quoted 22 lines
Quoted 22 lines

Could we use the 'no_mixing_hpt_and_radix' logic in check_extension to advertise only KVM_CAP_PPC_MMU_RADIX to the guest via OV5 so it doesn't try to run hash?

For instance, if I hack QEMU's 'spapr_dt_ov5_platform_support' from OV5_MMU_BOTH to OV5_MMU_RADIX_300 then it boots succesfuly, but the guest turns into radix, due to this code in prom_init:

prom_parse_mmu_model:

case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */

	prom_debug("MMU - radix only\n");
	if (prom_radix_disable) {
		/*
		 * If we __have__ to do radix, we're better off ignoring
		 * the command line rather than not booting.
		 */
		prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
	}
	support->radix_mmu = true;
	break;
Quoted 7 lines
Quoted 2 lines

It seems we could explicitly say that the host does not support hash and that would align with the above code.

I'm not sure, sounds like you could, on the other hand these aborts seem like the prefered failure mode for these kinds of configuration issues, I don't know what the policy is, is reverting back to radix acceptable?

Yeah, I couldn't find documentation about why we're reverting back to radix. I personally dislike it, but there is already a precedent so I'm not sure. A radix guest on a hash host does the same transparent conversion AFAICT.

But despite that, this patch removes support for hash MMU in this particular scenario. I don't see why continuing to tell the guest we support hash.

Quoted 7 lines
Quoted 2 lines

Anyway, here's a patch if you decide to go that way (tested w/ DD2.1 & 2.3 machines):

Thanks, I don't mind it, have to see if the maintainer will take it :)

You could add a small changelog / SOB and I could putit after my patch?

Sure, I'll reply to this thread with a proper patch.

Quoted 35 lines
Quoted 20 lines

---

arch/powerpc/include/asm/kvm_ppc.h +1 -0
index 0a056c64c317b..53743555676d6 100644
@@ -314,6 +314,7 @@ struct kvmppc_ops {
 			      int size);
 	int (*enable_svm)(struct kvm *kvm);
 	int (*svm_off)(struct kvm *kvm);
+	bool (*hash_possible)(void);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +0 -0
index 6f612d240392f..2d1e8aba22b85 100644
@@ -5599,6 +5599,15 @@ static int kvmhv_svm_off(struct kvm *kvm)
 	return ret;

arch/powerpc/kvm/book3s_hv.c static int kvmhv_svm_off(struct kvm *kvm) · line 5609

Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +8 -0
 }
 
+static bool kvmppc_hash_possible(void)
+{
+	if (radix_enabled() && no_mixing_hpt_and_radix)
+		return false;
+
+	return cpu_has_feature(CPU_FTR_ARCH_300) &&
+		cpu_has_feature(CPU_FTR_HVMODE);
+}

Just be careful, it's hash_v3 specifically. Either make this return true for arch < 300 add the ARCH_300 check in the ioctl, or rename to include v3.

Quoted 16 lines

arch/powerpc/kvm/book3s_hv.c static struct kvmppc_ops kvm_ops_hv = { · line 5656

Quoted 11 lines
arch/powerpc/kvm/book3s_hv.c +2 -0
+
 static struct kvmppc_ops kvm_ops_hv = {
 	.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
 	.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5642,6 +5651,7 @@ static struct kvmppc_ops kvm_ops_hv = {
 	.store_to_eaddr = kvmhv_store_to_eaddr,
 	.enable_svm = kvmhv_enable_svm,
 	.svm_off = kvmhv_svm_off,
+	.hash_possible = kvmppc_hash_possible,
 };
 

How about adding an op which can check extensions? It could return false if it wasn't checked and so default to the generic checks in kvm_vm_ioctl_check_extension, and take a pointer to 'r' to set.

I'm not sure I get the part about "return false if it wasn't checked". Do you mean like this?

static bool kvmhv_check_extension(long ext, int *r)

{
	switch (ext) {
	case KVM_CAP_PPC_MMU_HASH_V3:
		r = kvmppc_hash_v3_possible();
		break;
	default:
		return false;
	}
	return true;
}

And then we could move all of the #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE cases into it and early in kvm_vm_ioctl_check_extension have something like:

if (hv_enabled && kvmppc_hv_ops->check_extension(ext, &r))

	return r;
Quoted 18 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +0 -0
 static int kvm_init_subcore_bitmap(void)
arch/powerpc/kvm/powerpc.c +0 -0
index cf52d26f49cd7..99ced6c570e74 100644

arch/powerpc/kvm/powerpc.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 617

Quoted 9 lines
arch/powerpc/kvm/powerpc.c +1 -2
@@ -611,8 +611,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = !!(hv_enabled && radix_enabled());
 		break;
 	case KVM_CAP_PPC_MMU_HASH_V3:
-		r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
-		       cpu_has_feature(CPU_FTR_HVMODE));
+		r = !!(hv_enabled && kvmppc_hv_ops->hash_possible());
 		break;
 	case KVM_CAP_PPC_NESTED_HV:
 		r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&

Thanks,
Nick

#7 lore raw

PATCH 1/1 KVM: PPC: Don't always report hash MMU capability for P9 < DD2.2

In reply to Nicholas Piggin

These machines don't support running both MMU types at the same time, so remove the KVM_CAP_PPC_MMU_HASH_V3 capability when the host is using Radix MMU.

---
 arch/powerpc/include/asm/kvm_ppc.h |  1 +
 arch/powerpc/kvm/book3s_hv.c       | 10 ++++++++++
 arch/powerpc/kvm/powerpc.c         |  3 +--
 3 files changed, 12 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -0
index 0a056c64c317..b36abc89baf3 100644
@@ -314,6 +314,7 @@ struct kvmppc_ops {
 			      int size);
 	int (*enable_svm)(struct kvm *kvm);
 	int (*svm_off)(struct kvm *kvm);
+	bool (*hash_v3_possible)(void);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/kvm/book3s_hv.c +10 -0
index 6f612d240392..d20c0682cae5 100644
@@ -5599,6 +5599,15 @@ static int kvmhv_svm_off(struct kvm *kvm)
 	return ret;
 }
 
+static bool kvmppc_hash_v3_possible(void)
+{
+	if (radix_enabled() && no_mixing_hpt_and_radix)
+		return false;
+
+	return cpu_has_feature(CPU_FTR_ARCH_300) &&
+		cpu_has_feature(CPU_FTR_HVMODE);
+}
+
 static struct kvmppc_ops kvm_ops_hv = {
 	.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
 	.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5642,6 +5651,7 @@ static struct kvmppc_ops kvm_ops_hv = {
 	.store_to_eaddr = kvmhv_store_to_eaddr,
 	.enable_svm = kvmhv_enable_svm,
 	.svm_off = kvmhv_svm_off,
+	.hash_v3_possible = kvmppc_hash_v3_possible,
 };
 
 static int kvm_init_subcore_bitmap(void)
arch/powerpc/kvm/powerpc.c +1 -2
index cf52d26f49cd..b9fb2f20f879 100644
@@ -611,8 +611,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = !!(hv_enabled && radix_enabled());
 		break;
 	case KVM_CAP_PPC_MMU_HASH_V3:
-		r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
-		       cpu_has_feature(CPU_FTR_HVMODE));
+		r = !!(hv_enabled && kvmppc_hv_ops->hash_v3_possible());
 		break;
 	case KVM_CAP_PPC_NESTED_HV:
 		r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
Signature · 1 line
2.29.2
author reviewer
#8 lore raw

PATCH 2/4 KVM: PPC: Book3S HV: Fix radix guest SLB side channel

In reply to Nicholas Piggin

The slbmte instruction is legal in radix mode, including radix guest mode. This means radix guests can load the SLB with arbitrary data.

KVM host does not clear the SLB when exiting a guest if it was a radix guest, which would allow a rogue radix guest to use the SLB as a side channel to communicate with other guests.

Fix this by ensuring the SLB is cleared when coming out of a radix guest. Only the first 4 entries are a concern, because radix guests always run with LPCR[UPRT]=1, which limits the reach of slbmte. slbia is not used (except in a non-performance-critical path) because it can clear cached translations.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 39 ++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +31 -8
index d5a9b57ec129..0e1f5bf168a1 100644
@@ -1157,6 +1157,20 @@ EXPORT_SYMBOL_GPL(__kvmhv_vcpu_entry_p9)
 	mr	r4, r3
 	b	fast_guest_entry_c
 guest_exit_short_path:
+	/*
+	 * Malicious or buggy radix guests may have inserted SLB entries
+	 * (only 0..3 because radix always runs with UPRT=1), so these must
+	 * be cleared here to avoid side-channels. slbmte is used rather
+	 * than slbia, as it won't clear cached translations.
+	 */
+	li	r0,0
+	slbmte	r0,r0
+	li	r4,1
+	slbmte	r0,r4
+	li	r4,2
+	slbmte	r0,r4
+	li	r4,3
+	slbmte	r0,r4
 
 	li	r0, KVM_GUEST_MODE_NONE
 	stb	r0, HSTATE_IN_GUEST(r13)
@@ -1469,7 +1483,7 @@ guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
 	lbz	r0, KVM_RADIX(r5)
 	li	r5, 0
 	cmpwi	r0, 0
-	bne	3f			/* for radix, save 0 entries */
+	bne	0f			/* for radix, save 0 entries */
 	lwz	r0,VCPU_SLB_NR(r9)	/* number of entries in SLB */
 	mtctr	r0
 	li	r6,0
@@ -1490,12 +1504,9 @@ guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
 	slbmte	r0,r0
 	slbia
 	ptesync
-3:	stw	r5,VCPU_SLB_MAX(r9)
+	stw	r5,VCPU_SLB_MAX(r9)
 
 	/* load host SLB entries */
-BEGIN_MMU_FTR_SECTION
-	b	0f
-END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 	ld	r8,PACA_SLBSHADOWPTR(r13)
 
 	.rept	SLB_NUM_BOLTED
@@ -1508,7 +1519,17 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 	slbmte	r6,r5
 1:	addi	r8,r8,16
 	.endr
-0:
+	b	guest_bypass
+
+0:	/* Sanitise radix guest SLB, see guest_exit_short_path comment. */
+	li	r0,0
+	slbmte	r0,r0
+	li	r4,1
+	slbmte	r0,r4
+	li	r4,2
+	slbmte	r0,r4
+	li	r4,3
+	slbmte	r0,r4
 
 guest_bypass:
 	stw	r12, STACK_SLOT_TRAP(r1)
@@ -3302,12 +3323,14 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
 	mtspr	SPRN_CIABR, r0
 	mtspr	SPRN_DAWRX0, r0
 
+	/* Clear hash and radix guest SLB, see guest_exit_short_path comment. */
+	slbmte	r0, r0
+	slbia
+
 BEGIN_MMU_FTR_SECTION
 	b	4f
 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 
-	slbmte	r0, r0
-	slbia
 	ptesync
 	ld	r8, PACA_SLBSHADOWPTR(r13)
 	.rept	SLB_NUM_BOLTED
Signature · 1 line
2.23.0
Reviewed-by on v1 2/4
#9 lore raw

Re: [PATCH 2/4] KVM: PPC: Book3S HV: Fix radix guest SLB side channel

Quoted 7 lines · Nicholas Piggin

The slbmte instruction is legal in radix mode, including radix guest mode. This means radix guests can load the SLB with arbitrary data.

KVM host does not clear the SLB when exiting a guest if it was a radix guest, which would allow a rogue radix guest to use the SLB as a side channel to communicate with other guests.

Quoted 7 lines

Fix this by ensuring the SLB is cleared when coming out of a radix guest. Only the first 4 entries are a concern, because radix guests always run with LPCR[UPRT]=1, which limits the reach of slbmte. slbia is not used (except in a non-performance-critical path) because it can clear cached translations.

Quoted 88 lines
---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 39 ++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +31 -8
index d5a9b57ec129..0e1f5bf168a1 100644
@@ -1157,6 +1157,20 @@ EXPORT_SYMBOL_GPL(__kvmhv_vcpu_entry_p9)
 	mr	r4, r3
 	b	fast_guest_entry_c
 guest_exit_short_path:
+	/*
+	 * Malicious or buggy radix guests may have inserted SLB entries
+	 * (only 0..3 because radix always runs with UPRT=1), so these must
+	 * be cleared here to avoid side-channels. slbmte is used rather
+	 * than slbia, as it won't clear cached translations.
+	 */
+	li	r0,0
+	slbmte	r0,r0
+	li	r4,1
+	slbmte	r0,r4
+	li	r4,2
+	slbmte	r0,r4
+	li	r4,3
+	slbmte	r0,r4
 
 	li	r0, KVM_GUEST_MODE_NONE
 	stb	r0, HSTATE_IN_GUEST(r13)
@@ -1469,7 +1483,7 @@ guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
 	lbz	r0, KVM_RADIX(r5)
 	li	r5, 0
 	cmpwi	r0, 0
-	bne	3f			/* for radix, save 0 entries */
+	bne	0f			/* for radix, save 0 entries */
 	lwz	r0,VCPU_SLB_NR(r9)	/* number of entries in SLB */
 	mtctr	r0
 	li	r6,0
@@ -1490,12 +1504,9 @@ guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
 	slbmte	r0,r0
 	slbia
 	ptesync
-3:	stw	r5,VCPU_SLB_MAX(r9)
+	stw	r5,VCPU_SLB_MAX(r9)
 
 	/* load host SLB entries */
-BEGIN_MMU_FTR_SECTION
-	b	0f
-END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 	ld	r8,PACA_SLBSHADOWPTR(r13)
 
 	.rept	SLB_NUM_BOLTED
@@ -1508,7 +1519,17 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 	slbmte	r6,r5
 1:	addi	r8,r8,16
 	.endr
-0:
+	b	guest_bypass
+
+0:	/* Sanitise radix guest SLB, see guest_exit_short_path comment. */
+	li	r0,0
+	slbmte	r0,r0
+	li	r4,1
+	slbmte	r0,r4
+	li	r4,2
+	slbmte	r0,r4
+	li	r4,3
+	slbmte	r0,r4
 
 guest_bypass:
 	stw	r12, STACK_SLOT_TRAP(r1)
@@ -3302,12 +3323,14 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
 	mtspr	SPRN_CIABR, r0
 	mtspr	SPRN_DAWRX0, r0
 
+	/* Clear hash and radix guest SLB, see guest_exit_short_path comment. */
+	slbmte	r0, r0
+	slbia
+
 BEGIN_MMU_FTR_SECTION
 	b	4f
 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
 
-	slbmte	r0, r0
-	slbia
 	ptesync
 	ld	r8, PACA_SLBSHADOWPTR(r13)
 	.rept	SLB_NUM_BOLTED
#10 lore raw

Re: [PATCH 2/4] KVM: PPC: Book3S HV: Fix radix guest SLB side channel

In reply to Nicholas Piggin
Quoted 6 lines · Nicholas Piggin

The slbmte instruction is legal in radix mode, including radix guest mode. This means radix guests can load the SLB with arbitrary data.

KVM host does not clear the SLB when exiting a guest if it was a radix guest, which would allow a rogue radix guest to use the SLB as a side channel to communicate with other guests.

No, because the code currently clears the SLB when entering a radix guest, which you remove in the next patch. I'm OK with moving the SLB clearing from guest entry to guest exit, I guess, but I don't see that you are in fact fixing anything by doing so.

Paul.

author reviewer
#11 lore raw

Re: [PATCH 2/4] KVM: PPC: Book3S HV: Fix radix guest SLB side channel

Excerpts from Paul Mackerras's message of February 10, 2021 11:28 am:

Quoted 10 lines · Nicholas Piggin
Quoted 6 lines

The slbmte instruction is legal in radix mode, including radix guest mode. This means radix guests can load the SLB with arbitrary data.

KVM host does not clear the SLB when exiting a guest if it was a radix guest, which would allow a rogue radix guest to use the SLB as a side channel to communicate with other guests.

No, because the code currently clears the SLB when entering a radix guest,

Not AFAIKS.

Quoted 1 line

which you remove in the next patch.

The next patch avoids clearing host SLB entries when a hash guest is entered from a radix host, it doesn't apply to radix guests. Not sure where the changelog for it went but it should have "HPT guests" in the title at least, I guess.

Quoted 3 lines

I'm OK with moving the SLB
clearing from guest entry to guest exit, I guess, but I don't see that you are in fact fixing anything by doing so.

I can set slb entries in a radix guest in simulator and observe they stay around over host<->guest transitions, and they don't after this patch.

Thanks,
Nick

author reviewer
#12 lore raw

PATCH 3/4 KVM: PPC: Book3S HV: No need to clear radix host SLB before loading guest

In reply to Nicholas Piggin
---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +5 -1
index 0e1f5bf168a1..9f0fdbae4b44 100644
@@ -888,15 +888,19 @@ ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
 	cmpdi	r3, 512		/* 1 microsecond */
 	blt	hdec_soon
 
-	/* For hash guest, clear out and reload the SLB */
 	ld	r6, VCPU_KVM(r4)
 	lbz	r0, KVM_RADIX(r6)
 	cmpwi	r0, 0
 	bne	9f
+
+	/* For hash guest, clear out and reload the SLB */
+BEGIN_MMU_FTR_SECTION
+	/* Radix host won't have populated the SLB, so no need to clear */
 	li	r6, 0
 	slbmte	r6, r6
 	slbia
 	ptesync
+END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)
 
 	/* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
 	lwz	r5,VCPU_SLB_MAX(r4)
Signature · 1 line
2.23.0
author reviewer
#13 lore raw

PATCH 4/4 KVM: PPC: Book3S HV: Use POWER9 SLBIA IH=6 variant to clear SLB

In reply to Nicholas Piggin

IH=6 may preserve hypervisor real-mode ERAT entries and is the recommended SLBIA hint for switching partitions.

---
 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 9f0fdbae4b44..8cf1f69f442e 100644
@@ -898,7 +898,7 @@ BEGIN_MMU_FTR_SECTION
 	/* Radix host won't have populated the SLB, so no need to clear */
 	li	r6, 0
 	slbmte	r6, r6
-	slbia
+	PPC_SLBIA(6)
 	ptesync
 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)
 
@@ -1506,7 +1506,7 @@ guest_exit_cont:		/* r9 = vcpu, r12 = trap, r13 = paca */
 	/* Finally clear out the SLB */
 	li	r0,0
 	slbmte	r0,r0
-	slbia
+	PPC_SLBIA(6)
 	ptesync
 	stw	r5,VCPU_SLB_MAX(r9)
 
@@ -3329,7 +3329,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
 
 	/* Clear hash and radix guest SLB, see guest_exit_short_path comment. */
 	slbmte	r0, r0
-	slbia
+	PPC_SLBIA(6)
 
 BEGIN_MMU_FTR_SECTION
 	b	4f
Signature · 1 line
2.23.0