Skip to contentSkip to conversation

powerpc rng cleanups

PATCH Superseded Jason A. Donenfeld 2 participants 5 messages Jul 1, 2022 – Jul 2, 2022

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover powerpc rng cleanups

These are two small cleanups for -next.

This is meant to be atop
https://lore.kernel.org/all/[email protected]/
which is expected to land first.

v4 fixes up an inversion of thr DR flag.

Jason A. Donenfeld (2):
powerpc/powernv: rename remaining rng powernv_ functions to pnv_ powerpc/kvm: don't crash on missing rng, and use darn

 arch/powerpc/include/asm/archrandom.h |  7 +--
 arch/powerpc/kvm/book3s_hv_builtin.c  |  7 +--
 arch/powerpc/platforms/powernv/rng.c  | 66 ++++++++++-----------------
 drivers/char/hw_random/powernv-rng.c  |  2 +-
 4 files changed, 30 insertions(+), 52 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v4 1/2 powerpc/powernv: rename remaining rng powernv_ functions to pnv_

The preferred nomenclature is pnv_, not powernv_, but rng.c used powernv_ for some reason, which isn't consistent with the rest. A recent commit added a few pnv_ functions to rng.c, making the file a bit of a mishmash. This commit just replaces the rest of them.

Cc Michael Ellerman <[email protected]>
Fixes f3eac426657 ("powerpc/powernv: wire up rng during setup_arch")
---
 arch/powerpc/include/asm/archrandom.h | 10 +++---
 arch/powerpc/kvm/book3s_hv_builtin.c  |  4 +--
 arch/powerpc/platforms/powernv/rng.c  | 44 +++++++++++++--------------
 drivers/char/hw_random/powernv-rng.c  |  2 +-
 4 files changed, 30 insertions(+), 30 deletions(-)
arch/powerpc/include/asm/archrandom.h +5 -5
index 9a53e29680f4..11d4815841ab 100644
@@ -38,12 +38,12 @@ static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 #endif /* CONFIG_ARCH_RANDOM */
 
 #ifdef CONFIG_PPC_POWERNV
-int powernv_hwrng_present(void);
-int powernv_get_random_long(unsigned long *v);
-int powernv_get_random_real_mode(unsigned long *v);
+int pnv_hwrng_present(void);
+int pnv_get_random_long(unsigned long *v);
+int pnv_get_random_real_mode(unsigned long *v);
 #else
-static inline int powernv_hwrng_present(void) { return 0; }
-static inline int powernv_get_random_real_mode(unsigned long *v) { return 0; }
+static inline int pnv_hwrng_present(void) { return 0; }
+static inline int pnv_get_random_real_mode(unsigned long *v) { return 0; }
 #endif
 
 #endif /* _ASM_POWERPC_ARCHRANDOM_H */
arch/powerpc/kvm/book3s_hv_builtin.c +2 -2
index 88a8f6473c4e..799d40c2ab4f 100644
@@ -176,13 +176,13 @@ EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);
 
 int kvmppc_hwrng_present(void)
 {
-	return powernv_hwrng_present();
+	return pnv_hwrng_present();
 }
 EXPORT_SYMBOL_GPL(kvmppc_hwrng_present);
 
 long kvmppc_rm_h_random(struct kvm_vcpu *vcpu)
 {
-	if (powernv_get_random_real_mode(&vcpu->arch.regs.gpr[4]))
+	if (pnv_get_random_real_mode(&vcpu->arch.regs.gpr[4]))
 		return H_SUCCESS;
 
 	return H_HARDWARE;
arch/powerpc/platforms/powernv/rng.c +22 -22

122 lines not on this page · raw message

drivers/char/hw_random/powernv-rng.c +1 -1
index 8da1d7917bdc..429e956f34e1 100644
@@ -23,7 +23,7 @@ static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait
 	buf = (unsigned long *)data;
 
 	for (i = 0; i < len; i++)
-		powernv_get_random_long(buf++);
+		pnv_get_random_long(buf++);
 
 	return len * sizeof(unsigned long);
 }
Signature · 1 line
2.35.1
#2 lore raw

PATCH v4 2/2 powerpc/kvm: don't crash on missing rng, and use darn

In reply to Jason A. Donenfeld

On POWER8 systems that don't have ibm,power-rng available, a guest that ignores the KVM_CAP_PPC_HWRNG flag and calls H_RANDOM anyway will dereference a NULL pointer. And on machines with darn instead of ibm,power-rng, H_RANDOM won't work at all.

This patch kills two birds with one stone, by routing H_RANDOM calls to ppc_md.get_random_seed, and doing the real mode check inside of it.

Cc Michael Ellerman <[email protected]>
Fixes e928e9cb3601 ("KVM: PPC: Book3S HV: Add fast real-mode H_RANDOM implementation.")
---
 arch/powerpc/include/asm/archrandom.h |  5 ----
 arch/powerpc/kvm/book3s_hv_builtin.c  |  7 +++---
 arch/powerpc/platforms/powernv/rng.c  | 34 +++++++--------------------
 3 files changed, 12 insertions(+), 34 deletions(-)
arch/powerpc/include/asm/archrandom.h +0 -5
index 11d4815841ab..3af27bb84a3d 100644
@@ -38,12 +38,7 @@ static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 #endif /* CONFIG_ARCH_RANDOM */
 
 #ifdef CONFIG_PPC_POWERNV
-int pnv_hwrng_present(void);
 int pnv_get_random_long(unsigned long *v);
-int pnv_get_random_real_mode(unsigned long *v);
-#else
-static inline int pnv_hwrng_present(void) { return 0; }
-static inline int pnv_get_random_real_mode(unsigned long *v) { return 0; }
 #endif
 
 #endif /* _ASM_POWERPC_ARCHRANDOM_H */
arch/powerpc/kvm/book3s_hv_builtin.c +4 -3
index 799d40c2ab4f..3abaef5f9ac2 100644
@@ -19,7 +19,7 @@
 #include <asm/interrupt.h>
 #include <asm/kvm_ppc.h>
 #include <asm/kvm_book3s.h>
-#include <asm/archrandom.h>
+#include <asm/machdep.h>
 #include <asm/xics.h>
 #include <asm/xive.h>
 #include <asm/dbell.h>
@@ -176,13 +176,14 @@ EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);
 
 int kvmppc_hwrng_present(void)
 {
-	return pnv_hwrng_present();
+	return ppc_md.get_random_seed != NULL;
 }
 EXPORT_SYMBOL_GPL(kvmppc_hwrng_present);
 
 long kvmppc_rm_h_random(struct kvm_vcpu *vcpu)
 {
-	if (pnv_get_random_real_mode(&vcpu->arch.regs.gpr[4]))
+	if (ppc_md.get_random_seed &&
+	    ppc_md.get_random_seed(&vcpu->arch.regs.gpr[4]))
 		return H_SUCCESS;
 
 	return H_HARDWARE;
arch/powerpc/platforms/powernv/rng.c +8 -26
index 386b44660e76..985e200183eb 100644
@@ -29,15 +29,6 @@ struct pnv_rng {
 
 static DEFINE_PER_CPU(struct pnv_rng *, pnv_rng);
 
-int pnv_hwrng_present(void)
-{
-	struct pnv_rng *rng;
-
-	rng = get_cpu_var(pnv_rng);
-	put_cpu_var(rng);
-	return rng != NULL;
-}
-
 static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
 {
 	unsigned long parity;
@@ -58,17 +49,6 @@ static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
 	return val;
 }
 
-int pnv_get_random_real_mode(unsigned long *v)
-{
-	struct pnv_rng *rng;
-
-	rng = raw_cpu_read(pnv_rng);
-
-	*v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
-
-	return 1;
-}
-
 static int pnv_get_random_darn(unsigned long *v)
 {
 	unsigned long val;
@@ -105,12 +85,14 @@ int pnv_get_random_long(unsigned long *v)
 {
 	struct pnv_rng *rng;
 
-	rng = get_cpu_var(pnv_rng);
-
-	*v = rng_whiten(rng, in_be64(rng->regs));
-
-	put_cpu_var(rng);
-
+	if (mfmsr() & MSR_DR) {
+		rng = get_cpu_var(pnv_rng);
+		*v = rng_whiten(rng, in_be64(rng->regs));
+		put_cpu_var(rng);
+	} else {
+		rng = raw_cpu_read(pnv_rng);
+		*v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
+	}
 	return 1;
 }
 EXPORT_SYMBOL_GPL(pnv_get_random_long);
Signature · 1 line
2.35.1
#3 lore raw
In reply to Jason A. Donenfeld
Quoted 10 lines · Jason A. Donenfeld

These are two small cleanups for -next.

This is meant to be atop
https://lore.kernel.org/all/[email protected]/
which is expected to land first.

Quoted 6 lines

v4 fixes up an inversion of thr DR flag.

Jason A. Donenfeld (2):
powerpc/powernv: rename remaining rng powernv_ functions to pnv_ powerpc/kvm: don't crash on missing rng, and use darn

Boot tested this series and can successfully boot a Power8 server.

fwiw Tested-by: Sachin Sant <[email protected]>

- Sachin

#4 lore raw
Quoted 10 lines · Sachin Sant
Quoted 10 lines · Jason A. Donenfeld

These are two small cleanups for -next.

This is meant to be atop
https://lore.kernel.org/all/[email protected]/
which is expected to land first.

v4 fixes up an inversion of thr DR flag.

Quoted 8 lines
Quoted 4 lines

Jason A. Donenfeld (2):
powerpc/powernv: rename remaining rng powernv_ functions to pnv_ powerpc/kvm: don't crash on missing rng, and use darn

Boot tested this series and can successfully boot a Power8 server.

fwiw Tested-by: Sachin Sant <[email protected]>

Oh good. 4th time's a charm then. I had assumed this would be for 5.20, but maybe it'll hit 5.19-rcx instead.

Jason