[RFC PATCH 19/41] random: reintroduce arch_has_random() + arch_has_random_seed()
From: Nicolai Stange
Date: Mon Sep 21 2020 - 04:00:04 EST
A future patch will introduce support for making up for a certain amount
of lacking entropy in crng_reseed() by means of arch_get_random_long()
or arch_get_random_seed_long() respectively.
However, before even the tiniest bit of precious entropy is withdrawn from
the input_pool, it should be checked if whether the current arch even
has support for these.
Reintroduce arch_has_random() + arch_has_random_seed() and implement
them for arm64, powerpc, s390 and x86 as appropriate (yeah, I know this
should go in separate commits, but this is part of a RFC series).
Note that this more or less reverts commits
647f50d5d9d9 ("linux/random.h: Remove arch_has_random,
arch_has_random_seed")
cbac004995a0 ("powerpc: Remove arch_has_random, arch_has_random_seed")
5e054c820f59 ("s390: Remove arch_has_random, arch_has_random_seed")
5f2ed7f5b99b ("x86: Remove arch_has_random, arch_has_random_seed")
Signed-off-by: Nicolai Stange <nstange@xxxxxxx>
---
arch/arm64/include/asm/archrandom.h | 25 ++++++++++++++++++-------
arch/powerpc/include/asm/archrandom.h | 12 +++++++++++-
arch/s390/include/asm/archrandom.h | 14 ++++++++++++--
arch/x86/include/asm/archrandom.h | 18 ++++++++++++++----
include/linux/random.h | 8 ++++++++
5 files changed, 63 insertions(+), 14 deletions(-)
diff --git a/arch/arm64/include/asm/archrandom.h b/arch/arm64/include/asm/archrandom.h
index 44209f6146aa..055d18713db7 100644
--- a/arch/arm64/include/asm/archrandom.h
+++ b/arch/arm64/include/asm/archrandom.h
@@ -26,17 +26,13 @@ static inline bool __arm64_rndr(unsigned long *v)
return ok;
}
-static inline bool __must_check arch_get_random_long(unsigned long *v)
-{
- return false;
-}
-static inline bool __must_check arch_get_random_int(unsigned int *v)
+static inline bool arch_has_random(void)
{
return false;
}
-static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
+static inline bool arch_has_random_seed(void)
{
/*
* Only support the generic interface after we have detected
@@ -44,7 +40,22 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
* cpufeature code and with potential scheduling between CPUs
* with and without the feature.
*/
- if (!cpus_have_const_cap(ARM64_HAS_RNG))
+ return cpus_have_const_cap(ARM64_HAS_RNG);
+}
+
+static inline bool __must_check arch_get_random_long(unsigned long *v)
+{
+ return false;
+}
+
+static inline bool __must_check arch_get_random_int(unsigned int *v)
+{
+ return false;
+}
+
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
+{
+ if (!arch_has_random_seed())
return false;
return __arm64_rndr(v);
diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h
index 9a53e29680f4..47c2d74e7244 100644
--- a/arch/powerpc/include/asm/archrandom.h
+++ b/arch/powerpc/include/asm/archrandom.h
@@ -6,6 +6,16 @@
#include <asm/machdep.h>
+static inline bool arch_has_random(void)
+{
+ return false;
+}
+
+static inline bool arch_has_random_seed(void)
+{
+ return ppc_md.get_random_seed;
+}
+
static inline bool __must_check arch_get_random_long(unsigned long *v)
{
return false;
@@ -18,7 +28,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v)
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
{
- if (ppc_md.get_random_seed)
+ if (arch_has_random_seed())
return ppc_md.get_random_seed(v);
return false;
diff --git a/arch/s390/include/asm/archrandom.h b/arch/s390/include/asm/archrandom.h
index de61ce562052..18973845634c 100644
--- a/arch/s390/include/asm/archrandom.h
+++ b/arch/s390/include/asm/archrandom.h
@@ -21,6 +21,16 @@ extern atomic64_t s390_arch_random_counter;
bool s390_arch_random_generate(u8 *buf, unsigned int nbytes);
+static inline bool arch_has_random(void)
+{
+ return false;
+}
+
+static inline bool arch_has_random_seed(void)
+{
+ return static_branch_likely(&s390_arch_random_available);
+}
+
static inline bool __must_check arch_get_random_long(unsigned long *v)
{
return false;
@@ -33,7 +43,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v)
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
{
- if (static_branch_likely(&s390_arch_random_available)) {
+ if (arch_has_random_seed()) {
return s390_arch_random_generate((u8 *)v, sizeof(*v));
}
return false;
@@ -41,7 +51,7 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
{
- if (static_branch_likely(&s390_arch_random_available)) {
+ if (arch_has_random_seed()) {
return s390_arch_random_generate((u8 *)v, sizeof(*v));
}
return false;
diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index ebc248e49549..030f46c9e310 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -70,24 +70,34 @@ static inline bool __must_check rdseed_int(unsigned int *v)
*/
#ifdef CONFIG_ARCH_RANDOM
+static inline bool arch_has_random(void)
+{
+ return static_cpu_has(X86_FEATURE_RDRAND);
+}
+
+static inline bool arch_has_random_seed(void)
+{
+ return static_cpu_has(X86_FEATURE_RDSEED);
+}
+
static inline bool __must_check arch_get_random_long(unsigned long *v)
{
- return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false;
+ return arch_has_random() ? rdrand_long(v) : false;
}
static inline bool __must_check arch_get_random_int(unsigned int *v)
{
- return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false;
+ return arch_has_random() ? rdrand_int(v) : false;
}
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
{
- return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false;
+ return arch_has_random_seed() ? rdseed_long(v) : false;
}
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
{
- return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false;
+ return arch_has_random_seed() ? rdseed_int(v) : false;
}
extern void x86_init_rdrand(struct cpuinfo_x86 *c);
diff --git a/include/linux/random.h b/include/linux/random.h
index f45b8be3e3c4..d4653422a0c7 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -120,6 +120,14 @@ unsigned long randomize_page(unsigned long start, unsigned long range);
#ifdef CONFIG_ARCH_RANDOM
# include <asm/archrandom.h>
#else
+static inline bool arch_has_random(void)
+{
+ return false;
+}
+static inline bool arch_has_random_seed(void)
+{
+ return false;
+}
static inline bool __must_check arch_get_random_long(unsigned long *v)
{
return false;
--
2.26.2