random: handle archrandom in plural words
Jason A. Donenfeld
maintainerPATCH 1/1 random: handle archrandom in plural words
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the os_getrandom() interface can return arbitrary amounts.
So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the requested number of words. Additionally, all existing callers pass in a constant words parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 25 ++----- arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 14 +--- include/linux/random.h | 12 +-- 11 files changed, 111 insertions(+), 185 deletions(-)
arch/arm64/include/asm/archrandom.h +47 -55
@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {/* * Only support the generic interface after we have detected
@@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (words &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!words) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual
@@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }
@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )
@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!words) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_words_early arch_get_random_seed_words_early#endif /* _ASM_ARCHRANDOM_H */
arch/arm64/kernel/kaslr.c +1 -1
@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_words_early (&raw,1 )) seed ^= raw;if (!seed) {
arch/powerpc/include/asm/archrandom.h +6 -24
@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (words && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_words (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :
arch/s390/include/asm/archrandom.h +5 -20
@@ -18,31 +18,16 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; +return 0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); +cpacf_trng (NULL ,0 , (u8 *)v, words *sizeof (*v)); +atomic64_add (words *sizeof (*v), &s390_arch_random_counter);return true; }return false;
arch/um/include/asm/archrandom.h +7 -14
@@ -7,24 +7,17 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); +ssize_t ret =os_getrandom (v, words *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif
arch/x86/include/asm/archrandom.h +4 -37
62 lines not on this page · raw message
arch/x86/kernel/espfix_64.c +1 -1
@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_words (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;
drivers/char/random.c +31 -14
65 lines not on this page · raw message
include/asm-generic/archrandom.h +2 -12
@@ -2,22 +2,12 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {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) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {return false; }
include/linux/random.h +6 -6
@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_words_early +static inline size_t __init arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_words (v, words); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_words_early +static inline bool __init arch_get_random_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_words (v, words); }#endif
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerQuoted 305 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the requested number of words. Additionally, all existing callers pass in a constant words parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
Cc Will Deacon <[email protected]>Cc Michael Ellerman <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Harald Freudenberger <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 25 ++----- arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 14 +--- include/linux/random.h | 12 +-- 11 files changed, 111 insertions(+), 185 deletions(-)arch/arm64/include/asm/archrandom.h +47
-55@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {/* * Only support the generic interface after we have detected @@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (words &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!words) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual @@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!words) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_words_early arch_get_random_seed_words_early#endif /* _ASM_ARCHRANDOM_H */ arch/arm64/kernel/kaslr.c +1
-1@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_words_early (&raw,1 )) seed ^= raw;if (!seed) {arch/powerpc/include/asm/archrandom.h +6
-24@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (words && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV arch/powerpc/kvm/book3s_hv.c +1
-1@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_words (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :arch/s390/include/asm/archrandom.h +2
-17@@ -18,31 +18,16 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; +return 0 ; }
arch/s390/include/asm/archrandom.h line 31
Quoted 10 lines
arch/s390/include/asm/archrandom.h +3
-3-static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); +cpacf_trng (NULL ,0 , (u8 *)v, words *sizeof (*v)); +atomic64_add (words *sizeof (*v), &s390_arch_random_counter);return true;
return words;
arch/s390/include/asm/archrandom.h line 33
Quoted 2 lines
arch/s390/include/asm/archrandom.h +0
-0}return false;
return 0;
Quoted 6 lines
arch/um/include/asm/archrandom.h +0
-0@@ -7,24 +7,17 @@/* This is from <os.h>, but better not to #include that in a global header here. */
arch/um/include/asm/archrandom.h line 15
Quoted 10 lines
arch/um/include/asm/archrandom.h +5
-2ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); +ssize_t ret =os_getrandom (v, words *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret;
return ret / sizeof(*v);
Quoted 177 lines
arch/um/include/asm/archrandom.h +2
-12} -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif arch/x86/include/asm/archrandom.h +4
-37@@ -31,20 +31,6 @@ static inline bool __must_check rdrand_long(unsigned long *v)return false; } -static inline bool __must_check rdrand_int (unsigned int *v) -{ -bool ok; -unsigned int retry =RDRAND_RETRY_LOOPS ; -do { - asm volatile("rdrand %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); -if (ok) -return true; - }while (--retry); -return false; -} -static inline bool __must_check rdseed_long (unsigned long *v) {bool ok;@@ -54,38 +40,19 @@ static inline bool __must_check rdseed_long(unsigned long *v)return ok; } -static inline bool __must_check rdseed_int (unsigned int *v) -{ -bool ok; - asm volatile("rdseed %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); - return ok; -} -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked. */ -static inline bool __must_check arch_get_random_long (unsigned long *v) -{ -return static_cpu_has (X86_FEATURE_RDRAND ) ?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; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ; }#ifndef CONFIG_UML arch/x86/kernel/espfix_64.c +1
-1@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_words (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;drivers/char/random.c +31
-14@@ -596,12 +596,20 @@ static void extract_entropy(void *buf, size_t len)unsigned long rdseed[32 /sizeof (long )];size_t counter; } block; -size_t i; +size_t i, words; -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); +for (i =0 ; i <ARRAY_SIZE (block.rdseed);) { + words =arch_get_random_seed_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + words =arch_get_random_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + block.rdseed[i++] =random_get_entropy (); }spin_lock_irqsave (&input_pool.lock, flags);@@ -776,22 +784,31 @@ static struct notifier_block pm_notifier = { .notifier_call = random_pm_notificaint __init random_init (const char *command_line) {ktime_t now =ktime_get_real (); -unsigned int i, arch_bits; -unsigned long entropy; +size_t i, words, arch_bits; +unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )];#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;_mix_pool_bytes (compiletime_seed,sizeof (compiletime_seed));#endif -for (i =0 , arch_bits =BLAKE2S_BLOCK_SIZE *8 ; - i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (entropy)) { -if (!arch_get_random_seed_long_early (&entropy) && - !arch_get_random_long_early (&entropy)) { - entropy =random_get_entropy (); - arch_bits -=sizeof (entropy) *8 ; +for (i =0 , arch_bits =sizeof (entropy) *8 ; i <ARRAY_SIZE (entropy);) { + words =arch_get_random_seed_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; } -_mix_pool_bytes (&entropy,sizeof (entropy)); + words =arch_get_random_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; + } + entropy[0 ] =random_get_entropy (); +_mix_pool_bytes (entropy,sizeof (*entropy)); + arch_bits -=sizeof (*entropy) *8 ; + ++i; }_mix_pool_bytes (&now,sizeof (now));_mix_pool_bytes (utsname (),sizeof (*(utsname ())));include/asm-generic/archrandom.h +0
-0
include/asm-generic/archrandom.h line 7
Quoted 7 lines
include/asm-generic/archrandom.h +1
-1@@ -2,22 +2,12 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {return false;
return 0;
Quoted 6 lines
include/asm-generic/archrandom.h +0
-4} -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return false; -}
include/asm-generic/archrandom.h line 12
Quoted 10 lines
include/asm-generic/archrandom.h +1
-7- -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {return false;
return 0;
Quoted 34 lines
include/asm-generic/archrandom.h +0
-0}include/linux/random.h +6
-6@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_words_early +static inline size_t __init arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_words (v, words); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_words_early +static inline bool __init arch_get_random_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_words (v, words); }#endif Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v2 1/1 random: handle archrandom in plural words
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the os_getrandom() interface can return arbitrary amounts.
So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the requested number of words. Additionally, all existing callers pass in a constant words parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)
arch/arm64/include/asm/archrandom.h +47 -55
@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {/* * Only support the generic interface after we have detected
@@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (words &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!words) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual
@@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }
@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )
@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!words) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_words_early arch_get_random_seed_words_early#endif /* _ASM_ARCHRANDOM_H */
arch/arm64/kernel/kaslr.c +1 -1
@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_words_early (&raw,1 )) seed ^= raw;if (!seed) {
arch/powerpc/include/asm/archrandom.h +6 -24
@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (words && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_words (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :
arch/s390/include/asm/archrandom.h +7 -22
@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, words *sizeof (*v)); +atomic64_add (words *sizeof (*v), &s390_arch_random_counter); +return words; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */
arch/um/include/asm/archrandom.h +8 -13
@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; + ret =os_getrandom (v, words *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; +return 0 ; }#endif
arch/x86/include/asm/archrandom.h +4 -37
62 lines not on this page · raw message
arch/x86/kernel/espfix_64.c +1 -1
@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_words (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;
drivers/char/random.c +31 -14
65 lines not on this page · raw message
include/asm-generic/archrandom.h +4 -14
@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif
include/linux/random.h +6 -6
@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_words_early +static inline size_t __init arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_words (v, words); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_words_early +static inline bool __init arch_get_random_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_words (v, words); }#endif
Signature · 1 line
2.35.1
Quoted 5 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
Quoted 8 lines
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
On powerpc a word is 32-bits and a doubleword is 64-bits (at least according to the ISA). I think that's also true on other 64-bit architectures.
You could avoid any confusion by defining the API in terms of "longs" rather than "words".
But that's just a comment, see what others think.
Quoted 2 lines
arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +-
cheers
Quoted 11 lines · Michael Ellerman
Quoted 10 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.
Quoted 7 lines
Quoted 3 lines
So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
On powerpc a word is 32-bits and a doubleword is 64-bits (at least according to the ISA). I think that's also true on other 64-bit architectures.
IIRC, this is (or was) not the case on Alpha, where word was defined as 16 bits. All assembly mnemonics had w for 16 bits, l for 32 bits, and q for 64 bits.
Blame the PDP-11...
Gabriel
Quoted 4 lines
You could avoid any confusion by defining the API in terms of "longs" rather than "words".
Quoted 8 lines
But that's just a comment, see what others think.
Quoted 2 lines
arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +-cheers
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Michael Ellerman
reviewerQuoted 16 lines · Gabriel Paubert
Quoted 15 lines · Michael Ellerman
Quoted 13 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Quoted 7 lines
Quoted 3 lines
On powerpc a word is 32-bits and a doubleword is 64-bits (at least according to the ISA). I think that's also true on other 64-bit architectures.
IIRC, this is (or was) not the case on Alpha, where word was defined as 16 bits. All assembly mnemonics had w for 16 bits, l for 32 bits, and q for 64 bits.
Yeah I should have said on *some* other 64-bit arches.
Seems to be a common feature/hack on arches that have evolved over time, or been inspired by earlier arches.
The latest Power ISA has octwords :)
cheers
Mark Rutland
reviewerQuoted 52 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the requested number of words. Additionally, all existing callers pass in a constant words parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
Cc Will Deacon <[email protected]>Cc Michael Ellerman <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Harald Freudenberger <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)arch/arm64/include/asm/archrandom.h +0
-0
arch/arm64/include/asm/archrandom.h static inline bool __arm64_rndrrs(unsigned long *v) · line 61
Quoted 5 lines
arch/arm64/include/asm/archrandom.h +1
-1@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words)
As others have suggested, could we please s/words/longs/? That's a smaller change from the existing name, and avoids amibugity were "word" has an architecture-specific meaning. e.g. the ARM ARM (ARM DDI 0487G.b) defines:
Word
A 32-bit data item. Words are normally word-aligned in Arm systems.
Quoted 7 lines
arch/arm64/include/asm/archrandom.h +0
-0{/* * Only support the generic interface after we have detected @@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_long(unsigned long *v)* cpufeature code and with potential scheduling between CPUs * with and without the feature. */
arch/arm64/include/asm/archrandom.h static inline bool __must_check arch_get_random_long(unsigned long *v) · line 74
Quoted 10 lines
arch/arm64/include/asm/archrandom.h +4
-4-if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) -return true; -return false; +if (words &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words)
Likewise, s/words/longs/ woudl be clearer.
Quoted 136 lines
arch/arm64/include/asm/archrandom.h +42
-50{ -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!words) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual @@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!words) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_words_early arch_get_random_seed_words_early#endif /* _ASM_ARCHRANDOM_H */ arch/arm64/kernel/kaslr.c +0
-0
arch/arm64/kernel/kaslr.c u64 __init kaslr_early_init(void) · line 112
Quoted 8 lines
arch/arm64/kernel/kaslr.c +1
-1@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_words_early (&raw,1 )) seed ^= raw;if (!seed) {
FWIW, other than the naming nits, the arm64 bits look fine to me.
Mark.
Quoted 359 lines
arch/powerpc/include/asm/archrandom.h +6
-24@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (words && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV arch/powerpc/kvm/book3s_hv.c +1
-1@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_words (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :arch/s390/include/asm/archrandom.h +7
-22@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, words *sizeof (*v)); +atomic64_add (words *sizeof (*v), &s390_arch_random_counter); +return words; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */ arch/um/include/asm/archrandom.h +8
-13@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; + ret =os_getrandom (v, words *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; +return 0 ; }#endif arch/x86/include/asm/archrandom.h +4
-37@@ -31,20 +31,6 @@ static inline bool __must_check rdrand_long(unsigned long *v)return false; } -static inline bool __must_check rdrand_int (unsigned int *v) -{ -bool ok; -unsigned int retry =RDRAND_RETRY_LOOPS ; -do { - asm volatile("rdrand %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); -if (ok) -return true; - }while (--retry); -return false; -} -static inline bool __must_check rdseed_long (unsigned long *v) {bool ok;@@ -54,38 +40,19 @@ static inline bool __must_check rdseed_long(unsigned long *v)return ok; } -static inline bool __must_check rdseed_int (unsigned int *v) -{ -bool ok; - asm volatile("rdseed %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); - return ok; -} -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked. */ -static inline bool __must_check arch_get_random_long (unsigned long *v) -{ -return static_cpu_has (X86_FEATURE_RDRAND ) ?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; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ; }#ifndef CONFIG_UML arch/x86/kernel/espfix_64.c +1
-1@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_words (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;drivers/char/random.c +31
-14@@ -596,12 +596,20 @@ static void extract_entropy(void *buf, size_t len)unsigned long rdseed[32 /sizeof (long )];size_t counter; } block; -size_t i; +size_t i, words; -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); +for (i =0 ; i <ARRAY_SIZE (block.rdseed);) { + words =arch_get_random_seed_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + words =arch_get_random_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + block.rdseed[i++] =random_get_entropy (); }spin_lock_irqsave (&input_pool.lock, flags);@@ -776,22 +784,31 @@ static struct notifier_block pm_notifier = { .notifier_call = random_pm_notificaint __init random_init (const char *command_line) {ktime_t now =ktime_get_real (); -unsigned int i, arch_bits; -unsigned long entropy; +size_t i, words, arch_bits; +unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )];#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;_mix_pool_bytes (compiletime_seed,sizeof (compiletime_seed));#endif -for (i =0 , arch_bits =BLAKE2S_BLOCK_SIZE *8 ; - i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (entropy)) { -if (!arch_get_random_seed_long_early (&entropy) && - !arch_get_random_long_early (&entropy)) { - entropy =random_get_entropy (); - arch_bits -=sizeof (entropy) *8 ; +for (i =0 , arch_bits =sizeof (entropy) *8 ; i <ARRAY_SIZE (entropy);) { + words =arch_get_random_seed_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; } -_mix_pool_bytes (&entropy,sizeof (entropy)); + words =arch_get_random_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; + } + entropy[0 ] =random_get_entropy (); +_mix_pool_bytes (entropy,sizeof (*entropy)); + arch_bits -=sizeof (*entropy) *8 ; + ++i; }_mix_pool_bytes (&now,sizeof (now));_mix_pool_bytes (utsname (),sizeof (*(utsname ())));include/asm-generic/archrandom.h +4
-14@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif include/linux/random.h +6
-6@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_words_early +static inline size_t __init arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_words (v, words); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_words_early +static inline bool __init arch_get_random_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_words (v, words); }#endif Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerHi Mark,
Quoted 6 lines · Mark Rutland
Review
Quoted 2 lines
(quoted diff) +1
-1-static inline bool __must_check arch_get_random_long(unsigned long *v) +static inline size_t __must_check arch_get_random_words(unsigned long *v, size_t words)As others have suggested, could we please s/words/longs/? That's a smaller change from the existing name, and avoids amibugity were "word" has an architecture-specific meaning. e.g. the ARM ARM (ARM DDI 0487G.b) defines:
At first I had thought, "erm, I like 'words'" but decided to wait it out, and now it's two people with the same probably good objection to 'words', so I'll send a v+1 with 'longs' instead. Thanks for your feedback. Patch incoming.
Quoted 1 line
FWIW, other than the naming nits, the arm64 bits look fine to me.
Oh good. Can you send an Acked-by for v+1? Coming up shortly.
Jason
Jason A. Donenfeld
maintainerPATCH v3 1/1 random: handle archrandom with multiple longs
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the os_getrandom() interface can return arbitrary amounts.
So change the api signature to take a "max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant max_longs parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)
arch/arm64/include/asm/archrandom.h +47 -55
@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {/* * Only support the generic interface after we have detected
@@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (max_longs &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!max_longs) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual
@@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }
@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )
@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!max_longs) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_longs_early arch_get_random_seed_longs_early#endif /* _ASM_ARCHRANDOM_H */
arch/arm64/kernel/kaslr.c +1 -1
@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_longs_early (&raw,1 )) seed ^= raw;if (!seed) {
arch/powerpc/include/asm/archrandom.h +6 -24
@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_longs (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :
arch/s390/include/asm/archrandom.h +7 -22
@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, max_longs *sizeof (*v)); +atomic64_add (max_longs *sizeof (*v), &s390_arch_random_counter); +return max_longs; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */
arch/um/include/asm/archrandom.h +8 -13
@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; + ret =os_getrandom (v, max_longs *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; }#endif
arch/x86/include/asm/archrandom.h +4 -37
62 lines not on this page · raw message
arch/x86/kernel/espfix_64.c +1 -1
@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_longs (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;
drivers/char/random.c +31 -14
65 lines not on this page · raw message
include/asm-generic/archrandom.h +4 -14
@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif
include/linux/random.h +6 -6
@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_longs_early +static inline size_t __init arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_longs (v, max_longs); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_longs_early +static inline bool __init arch_get_random_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_longs (v, max_longs); }#endif
Signature · 1 line
2.35.1
Re: [PATCH v3] random: handle archrandom with multiple longs
Quoted 22 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
Quoted 12 lines
Cc Will Deacon <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Mark Rutland <[email protected]>Cc Harald Freudenberger <[email protected]>Acked-by Michael Ellerman <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
The arm64 bits looks good to me, so FWIW:
Mark.
Quoted 544 lines
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)arch/arm64/include/asm/archrandom.h +47
-55@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {/* * Only support the generic interface after we have detected @@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (max_longs &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!max_longs) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual @@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!max_longs) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_longs_early arch_get_random_seed_longs_early#endif /* _ASM_ARCHRANDOM_H */ arch/arm64/kernel/kaslr.c +1
-1@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_longs_early (&raw,1 )) seed ^= raw;if (!seed) {arch/powerpc/include/asm/archrandom.h +6
-24@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV arch/powerpc/kvm/book3s_hv.c +1
-1@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_longs (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :arch/s390/include/asm/archrandom.h +7
-22@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, max_longs *sizeof (*v)); +atomic64_add (max_longs *sizeof (*v), &s390_arch_random_counter); +return max_longs; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */ arch/um/include/asm/archrandom.h +8
-13@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; + ret =os_getrandom (v, max_longs *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; }#endif arch/x86/include/asm/archrandom.h +4
-37@@ -31,20 +31,6 @@ static inline bool __must_check rdrand_long(unsigned long *v)return false; } -static inline bool __must_check rdrand_int (unsigned int *v) -{ -bool ok; -unsigned int retry =RDRAND_RETRY_LOOPS ; -do { - asm volatile("rdrand %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); -if (ok) -return true; - }while (--retry); -return false; -} -static inline bool __must_check rdseed_long (unsigned long *v) {bool ok;@@ -54,38 +40,19 @@ static inline bool __must_check rdseed_long(unsigned long *v)return ok; } -static inline bool __must_check rdseed_int (unsigned int *v) -{ -bool ok; - asm volatile("rdseed %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); - return ok; -} -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked. */ -static inline bool __must_check arch_get_random_long (unsigned long *v) -{ -return static_cpu_has (X86_FEATURE_RDRAND ) ?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; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; +return max_longs &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; +return max_longs &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ; }#ifndef CONFIG_UML arch/x86/kernel/espfix_64.c +1
-1@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_longs (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;drivers/char/random.c +31
-14@@ -596,12 +596,20 @@ static void extract_entropy(void *buf, size_t len)unsigned long rdseed[32 /sizeof (long )];size_t counter; } block; -size_t i; +size_t i, longs; -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); +for (i =0 ; i <ARRAY_SIZE (block.rdseed);) { + longs =arch_get_random_seed_longs (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (longs) { + i += longs; +continue ; + } + longs =arch_get_random_longs (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (longs) { + i += longs; +continue ; + } + block.rdseed[i++] =random_get_entropy (); }spin_lock_irqsave (&input_pool.lock, flags);@@ -776,22 +784,31 @@ static struct notifier_block pm_notifier = { .notifier_call = random_pm_notificaint __init random_init (const char *command_line) {ktime_t now =ktime_get_real (); -unsigned int i, arch_bits; -unsigned long entropy; +size_t i, longs, arch_bits; +unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )];#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;_mix_pool_bytes (compiletime_seed,sizeof (compiletime_seed));#endif -for (i =0 , arch_bits =BLAKE2S_BLOCK_SIZE *8 ; - i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (entropy)) { -if (!arch_get_random_seed_long_early (&entropy) && - !arch_get_random_long_early (&entropy)) { - entropy =random_get_entropy (); - arch_bits -=sizeof (entropy) *8 ; +for (i =0 , arch_bits =sizeof (entropy) *8 ; i <ARRAY_SIZE (entropy);) { + longs =arch_get_random_seed_longs (entropy,ARRAY_SIZE (entropy) - i); +if (longs) { +_mix_pool_bytes (entropy,sizeof (*entropy) * longs); + i += longs; +continue ; } -_mix_pool_bytes (&entropy,sizeof (entropy)); + longs =arch_get_random_longs (entropy,ARRAY_SIZE (entropy) - i); +if (longs) { +_mix_pool_bytes (entropy,sizeof (*entropy) * longs); + i += longs; +continue ; + } + entropy[0 ] =random_get_entropy (); +_mix_pool_bytes (entropy,sizeof (*entropy)); + arch_bits -=sizeof (*entropy) *8 ; + ++i; }_mix_pool_bytes (&now,sizeof (now));_mix_pool_bytes (utsname (),sizeof (*(utsname ())));include/asm-generic/archrandom.h +4
-14@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif include/linux/random.h +6
-6@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_longs_early +static inline size_t __init arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_longs (v, max_longs); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_longs_early +static inline bool __init arch_get_random_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_longs (v, max_longs); }#endif Signature · 1 line
2.35.1
Re: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldQuoted 22 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
Quoted 12 lines
Cc Will Deacon <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Mark Rutland <[email protected]>Cc Harald Freudenberger <[email protected]>Acked-by Michael Ellerman <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
For arm64:
Re: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldQuoted 34 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.Cc Will Deacon <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Mark Rutland <[email protected]>Cc Harald Freudenberger <[email protected]>Acked-by Michael Ellerman <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Quoted 13 lines
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)
For s390:
Jason A. Donenfeld
maintainerRe: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldHey Borislav (or other x86ers),
Quoted 15 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.
Quoted 7 lines
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
This patch now has acks from Heiko, Catalin, Mark, and Michael, covering the arm, ppc, and s390 changes. The changes to s390 and arm were non-trivial so those acks were quite meaningful. On x86 and ppc, the code compiles down to basically the same assembly and the change really doesn't matter at all. Nonetheless, I thought I should give you a final poke in case you want to ack or nack this, lest I step on the tip.git of your shoes.
Jason
Re: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldQuoted 4 lines · Jason A. Donenfeld
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter.
Hmm, maybe this has come up already but it reads weird.
If I have a function arch_get_random_longs(), I'd expect it to give me the number of longs I requested or say, error.
Why do the callers need to loop?
If I have to loop, I'd call the "get me one long" function and loop N times.
Signature · 6 lines
Regards/Gruss,
Boris.
SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)Jason A. Donenfeld
maintainerRe: [PATCH v3] random: handle archrandom with multiple longs
Hi Boris,
Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Jason A. Donenfeld
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter.Hmm, maybe this has come up already but it reads weird.
Quoted 7 lines
If I have a function
arch_get_random_longs(), I'd expect it to give me the number of longs I requested or say, error.Why do the callers need to loop?
If I have to loop, I'd call the "get me one long" function and loop N times.
Answered partially in the commit message you quoted and partially here:
https://lore.kernel.org/lkml/[email protected]/
Note that arch_get_random_longs() is not a general purpose function. For that there used to be get_random_bytes_arch(), but that no longer exists as people shouldn't be using this stuff directly. arch_get_random_longs() is a special purpose function mainly intended for use by the RNG itself.
More directly, the reason we don't want to error is because the use case has fallbacks meant to handle errors. The cascade looks like this (quoting from the other email):
unsigned long array[whatever];
for (i = 0; i < ARRAY_SIZE(array);) {
longs = arch_get_random_seed_longs(&array[i], ARRAY_SIZE(array) - i);
if (longs) {
i += longs;
continue;
}
longs = arch_get_random_longs(&array[i], ARRAY_SIZE(array) - i);
if (longs) {
i += longs;
continue;
}
array[i++] = random_get_entropy();
}It tries to get the best that it can as much as it can, but isn't going to block or do anything too nuts for that.
Anyway, from an x86 perspective, I can't imagine you object to this change, right? Codegen is the same.
Jason
RE: [PATCH v3] random: handle archrandom with multiple longs
...
Quoted 19 lines
More directly, the reason we don't want to error is because the use case has fallbacks meant to handle errors. The cascade looks like this (quoting from the other email):
unsigned long array[whatever]; for (i = 0; i < ARRAY_SIZE(array);) { longs = arch_get_random_seed_longs(&array[i], ARRAY_SIZE(array) - i); if (longs) { i += longs; continue; } longs = arch_get_random_longs(&array[i], ARRAY_SIZE(array) - i); if (longs) { i += longs; continue; } array[i++] = random_get_entropy(); }
Quoted 2 lines
It tries to get the best that it can as much as it can, but isn't going to block or do anything too nuts for that.
Do you really want to retry the earlier calls that returned no data?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Jason A. Donenfeld
maintainerRe: [PATCH v3] random: handle archrandom with multiple longs
Quoted 21 lines · David Laight
...
Quoted 19 lines
More directly, the reason we don't want to error is because the use case has fallbacks meant to handle errors. The cascade looks like this (quoting from the other email):
unsigned long array[whatever]; for (i = 0; i < ARRAY_SIZE(array);) { longs = arch_get_random_seed_longs(&array[i], ARRAY_SIZE(array) - i); if (longs) { i += longs; continue; } longs = arch_get_random_longs(&array[i], ARRAY_SIZE(array) - i); if (longs) { i += longs; continue; } array[i++] = random_get_entropy(); }
Quoted 4 lines
Quoted 2 lines
It tries to get the best that it can as much as it can, but isn't going to block or do anything too nuts for that.
Do you really want to retry the earlier calls that returned no data?
Does the above code do that?
Jason A. Donenfeld
maintainerPATCH 1/1 random: discourage use of archrandom outside of rng
In reply to Jason A. DonenfeldBorislav pointed out during the review of "random: handle archrandom with multiple longs" that people might actually use this function, which might not be good because the function has surprising semantics. This of course was also the case before that patch, and indeed RDSEED-like functions across architectures often behave surprisingly, failing often. While random.c has been written specifically to work with that behavior, not much else is well equipped for that.
So add a comment suggesting that this is not for general consumption. Fortunately, nobody uses this for general consumption anyway, and people who try quickly find themselves in trouble. But adding this comment out of an abundance of caution was nonetheless suggested, and it at least means there will be easier justification for cleaning up potential misuses of the function later.
--- arch/arm64/include/asm/archrandom.h | 13 +++++++++++++ arch/powerpc/include/asm/archrandom.h | 8 ++++++++ arch/s390/include/asm/archrandom.h | 8 ++++++++ arch/um/include/asm/archrandom.h | 8 ++++++++ arch/x86/include/asm/archrandom.h | 9 ++++++--- include/asm-generic/archrandom.h | 8 ++++++++ 6 files changed, 51 insertions(+), 3 deletions(-)
arch/arm64/include/asm/archrandom.h +13 -0
@@ -58,6 +58,10 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {/*
@@ -71,6 +75,10 @@ static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_treturn 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (!max_longs)
@@ -121,6 +129,11 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } + +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __init __must_check arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {
arch/powerpc/include/asm/archrandom.h +8 -0
@@ -4,11 +4,19 @@#include <asm/machdep.h> +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed (v))
arch/s390/include/asm/archrandom.h +8 -0
@@ -18,11 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (static_branch_likely (&s390_arch_random_available)) {
arch/um/include/asm/archrandom.h +8 -0
@@ -7,6 +7,10 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {ssize_t ret;
@@ -17,6 +21,10 @@ static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_treturn ret /sizeof (*v); } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return 0 ;
arch/x86/include/asm/archrandom.h +6 -3
@@ -41,15 +41,18 @@ static inline bool __must_check rdseed_long(unsigned long *v) }/* -* These are the generic interfaces; they must not be declared if the -* stubs in <linux/random.h> are to be invoked. +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. */ -static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return max_longs &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return max_longs &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ;
include/asm-generic/archrandom.h +8 -0
@@ -2,11 +2,19 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return 0 ;
Signature · 1 line
2.35.1
Re: [PATCH] random: discourage use of archrandom outside of rng
Quoted 23 lines · Jason A. Donenfeld
Borislav pointed out during the review of "random: handle archrandom with multiple longs" that people might actually use this function, which might not be good because the function has surprising semantics. This of course was also the case before that patch, and indeed RDSEED-like functions across architectures often behave surprisingly, failing often. While
random.chas been written specifically to work with that behavior, not much else is well equipped for that.So add a comment suggesting that this is not for general consumption. Fortunately, nobody uses this for general consumption anyway, and people who try quickly find themselves in trouble. But adding this comment out of an abundance of caution was nonetheless suggested, and it at least means there will be easier justification for cleaning up potential misuses of the function later.
Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Catalin Marinas <[email protected]>Cc Mark Rutland <[email protected]>Cc Michael Ellerman <[email protected]>Cc Johannes Berg <[email protected]>Suggested-by Borislav Petkov <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Quoted 8 lines
--- arch/arm64/include/asm/archrandom.h | 13 +++++++++++++ arch/powerpc/include/asm/archrandom.h | 8 ++++++++ arch/s390/include/asm/archrandom.h | 8 ++++++++ arch/um/include/asm/archrandom.h | 8 ++++++++ arch/x86/include/asm/archrandom.h | 9 ++++++--- include/asm-generic/archrandom.h | 8 ++++++++ 6 files changed, 51 insertions(+), 3 deletions(-)
Thx.
Signature · 6 lines
Regards/Gruss,
Boris.
SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)Re: [PATCH] random: discourage use of archrandom outside of rng
In reply to Jason A. DonenfeldQuoted 23 lines · Jason A. Donenfeld
Borislav pointed out during the review of "random: handle archrandom with multiple longs" that people might actually use this function, which might not be good because the function has surprising semantics. This of course was also the case before that patch, and indeed RDSEED-like functions across architectures often behave surprisingly, failing often. While
random.chas been written specifically to work with that behavior, not much else is well equipped for that.So add a comment suggesting that this is not for general consumption. Fortunately, nobody uses this for general consumption anyway, and people who try quickly find themselves in trouble. But adding this comment out of an abundance of caution was nonetheless suggested, and it at least means there will be easier justification for cleaning up potential misuses of the function later.
Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Catalin Marinas <[email protected]>Cc Mark Rutland <[email protected]>Cc Michael Ellerman <[email protected]>Cc Johannes Berg <[email protected]>Suggested-by Borislav Petkov <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Quoted 8 lines
--- arch/arm64/include/asm/archrandom.h | 13 +++++++++++++ arch/powerpc/include/asm/archrandom.h | 8 ++++++++ arch/s390/include/asm/archrandom.h | 8 ++++++++ arch/um/include/asm/archrandom.h | 8 ++++++++ arch/x86/include/asm/archrandom.h | 9 ++++++--- include/asm-generic/archrandom.h | 8 ++++++++ 6 files changed, 51 insertions(+), 3 deletions(-)
For s390:
Re: [PATCH] random: discourage use of archrandom outside of rng
In reply to Jason A. DonenfeldQuoted 15 lines · Jason A. Donenfeld
Borislav pointed out during the review of "random: handle archrandom with multiple longs" that people might actually use this function, which might not be good because the function has surprising semantics. This of course was also the case before that patch, and indeed RDSEED-like functions across architectures often behave surprisingly, failing often. While
random.chas been written specifically to work with that behavior, not much else is well equipped for that.So add a comment suggesting that this is not for general consumption. Fortunately, nobody uses this for general consumption anyway, and people who try quickly find themselves in trouble. But adding this comment out of an abundance of caution was nonetheless suggested, and it at least means there will be easier justification for cleaning up potential misuses of the function later.
Quoted 8 lines
Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Catalin Marinas <[email protected]>Cc Mark Rutland <[email protected]>Cc Michael Ellerman <[email protected]>Cc Johannes Berg <[email protected]>Suggested-by Borislav Petkov <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
FWIW:
Mark.
Quoted 174 lines
--- arch/arm64/include/asm/archrandom.h | 13 +++++++++++++ arch/powerpc/include/asm/archrandom.h | 8 ++++++++ arch/s390/include/asm/archrandom.h | 8 ++++++++ arch/um/include/asm/archrandom.h | 8 ++++++++ arch/x86/include/asm/archrandom.h | 9 ++++++--- include/asm-generic/archrandom.h | 8 ++++++++ 6 files changed, 51 insertions(+), 3 deletions(-)arch/arm64/include/asm/archrandom.h +13
-0@@ -58,6 +58,10 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {/* @@ -71,6 +75,10 @@ static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_treturn 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (!max_longs)@@ -121,6 +129,11 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } + +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __init __must_check arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {arch/powerpc/include/asm/archrandom.h +8
-0@@ -4,11 +4,19 @@#include <asm/machdep.h> +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed (v))arch/s390/include/asm/archrandom.h +8
-0@@ -18,11 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (static_branch_likely (&s390_arch_random_available)) {arch/um/include/asm/archrandom.h +8
-0@@ -7,6 +7,10 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {ssize_t ret;@@ -17,6 +21,10 @@ static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_treturn ret /sizeof (*v); } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return 0 ;arch/x86/include/asm/archrandom.h +6
-3@@ -41,15 +41,18 @@ static inline bool __must_check rdseed_long(unsigned long *v) }/* -* These are the generic interfaces; they must not be declared if the -* stubs in <linux/random.h> are to be invoked. +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. */ -static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return max_longs &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return max_longs &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ;include/asm-generic/archrandom.h +8
-0@@ -2,11 +2,19 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {return 0 ; } +/* +* This should only be used by drivers/char/random.c. Other drivers *must* +* use get_random_bytes() instead. +*/ static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {return 0 ;Signature · 1 line
2.35.1
Re: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldQuoted 34 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.Cc Will Deacon <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Mark Rutland <[email protected]>Cc Harald Freudenberger <[email protected]>Acked-by Michael Ellerman <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
Quoted 13 lines
--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)
Thx.
Signature · 6 lines
Regards/Gruss,
Boris.
SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)Re: [PATCH v3] random: handle archrandom with multiple longs
In reply to Jason A. DonenfeldQuoted 623 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
On arm64, the SMCCC TRNG interface can return between 1 and 3 longs. On s390, the CPACF TRNG interface can return arbitrary amounts, with 32 longs having the same cost as one. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "
max_longs" parameter designating the maximum number of longs requested, and then return the number of longs generated.Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the maximum number of longs. Additionally, all existing callers pass in a constant
max_longsparameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.Cc Will Deacon <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Mark Rutland <[email protected]>Cc Harald Freudenberger <[email protected]>Acked-by Michael Ellerman <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)arch/arm64/include/asm/archrandom.h +1
-1@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long(unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) {/* arch/arm64/include/asm/archrandom.h +24
-46* Only support the generic interface after we have detected @@ -66,27 +66,15 @@ static inline bool __must_checkarch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (max_longs &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int(unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!max_longs) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual @@ -95,10 +83,23 @@ static inline bool __must_checkarch_get_random_seed_long(unsigned long *v) * (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }@@ -108,32 +109,9 @@ static inline bool __must_checkarch_get_random_seed_long(unsigned long *v) * enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool
__init__early_cpu_has_rndr(void)arch/arm64/include/asm/archrandom.h +21
-8@@ -143,26 +121,40 @@ static inline bool __init__early_cpu_has_rndr (void ) return (ftr >> ID_AA64ISAR0_EL1_RNDR_SHIFT) & 0xf; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!max_longs) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + max_longs =min_t (size_t,3 , max_longs); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , max_longs *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (max_longs) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return max_longs; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early
arch_get_random_seed_long_early
+#definearch_get_random_seed_longs_earlyarch_get_random_seed_longs_early#endif /*
_ASM_ARCHRANDOM_H*/arch/arm64/kernel/kaslr.c +1
-1@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_longs_early (&raw,1 )) seed ^= raw;if (!seed) {arch/powerpc/include/asm/archrandom.h +6
-24@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long(unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int(unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { - return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV arch/powerpc/kvm/book3s_hv.c +1
-1@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_longs (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;arch/powerpc/kvm/book3s_hv.c +0
-0case H_RPT_INVALIDATE :arch/s390/include/asm/archrandom.h +7
-22@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long(unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int(unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) {if (static_branch_likely (&s390_arch_random_available)) { - cpacf_trng(NULL, 0, (u8 *)v, sizeof(*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, max_longs *sizeof (*v)); +atomic64_add (max_longs *sizeof (*v), &s390_arch_random_counter); +return max_longs; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */ arch/um/include/asm/archrandom.h +0
-0@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */
ssize_tos_getrandom(void *buf, size_t len, unsigned int flags);arch/um/include/asm/archrandom.h +1
-1-static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs(unsigned long*v,
size_tmax_longs)
{arch/um/include/asm/archrandom.h +1
-8-return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long(unsignedlong *v)
arch/um/include/asm/archrandom.h +4
-3-{ -return false; + ret =os_getrandom (v, max_longs *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int(unsigned int*v)
+static inlinesize_t__must_checkarch_get_random_seed_longs(unsigned long *v, size_t max_longs)
{arch/um/include/asm/archrandom.h +1
-1- return false; + return 0; }#endif arch/x86/include/asm/archrandom.h +0
-14@@ -31,20 +31,6 @@ static inline bool __must_check rdrand_long(unsigned long *v)return false; } -static inline bool __must_check rdrand_int (unsigned int *v) -{ -bool ok; -unsigned int retry =RDRAND_RETRY_LOOPS ; -do { - asm volatile("rdrand %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); -if (ok) -return true; - }while (--retry); -return false; -} -static inline bool __must_check rdseed_long (unsigned long *v) {arch/x86/include/asm/archrandom.h +3
-22bool ok;@@ -54,38 +40,19 @@ static inline bool __must_check rdseed_long(unsigned long *v) return ok; } -static inline bool __must_check rdseed_int (unsigned int *v) -{ -bool ok; - asm volatile("rdseed %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); - return ok; -} -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked. */ -static inline bool __must_check arch_get_random_long (unsigned long *v) -{ -return static_cpu_has (X86_FEATURE_RDRAND ) ?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; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; +return max_longs &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs(unsignedlong *v,
size_tmax_longs)
{arch/x86/include/asm/archrandom.h +1
-1-return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; + return max_longs && static_cpu_has(X86_FEATURE_RDSEED) &&
rdseed_long(v)? 1 : 0;
}#ifndef
CONFIG_UMLarch/x86/kernel/espfix_64.c +1
-1@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_longs (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;drivers/char/random.c +13
-5@@ -596,12 +596,20 @@ static void extract_entropy(void *buf, size_t len)unsigned long rdseed[32 /sizeof (long )];size_t counter; } block; -size_t i; +size_t i, longs; -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); +for (i =0 ; i <ARRAY_SIZE (block.rdseed);) { + longs =arch_get_random_seed_longs (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (longs) { + i += longs; +continue ; + } + longs =arch_get_random_longs (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (longs) { + i += longs; +continue ; + } + block.rdseed[i++] =random_get_entropy ();drivers/char/random.c +0
-0}spin_lock_irqsave (&input_pool.lock, flags);@@ -776,22 +784,31 @@ static struct notifier_block pm_notifier = {.notifier_call =
random_pm_notifica
int__initrandom_init(const char *command_line)
{drivers/char/random.c +2
-2ktime_t now =ktime_get_real (); -unsigned int i, arch_bits; -unsigned long entropy; +size_t i, longs, arch_bits; +unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )];#if defined(LATENT_ENTROPY_PLUGIN )static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE ]__initconst
__latent_entropy;drivers/char/random.c +1
-6_mix_pool_bytes (compiletime_seed,sizeof (compiletime_seed));#endif -for (i =0 , arch_bits =BLAKE2S_BLOCK_SIZE *8 ; - i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (entropy)) { -if (!arch_get_random_seed_long_early (&entropy) && - !arch_get_random_long_early (&entropy)) { - entropy =random_get_entropy (); - arch_bits -=sizeof (entropy) *8 ; + for (i = 0, arch_bits = sizeof(entropy) * 8; i <
ARRAY_SIZE(entropy);) {drivers/char/random.c +1
-0+ longs = arch_get_random_seed_longs(entropy, ARRAY_SIZE(entropy) -i);
drivers/char/random.c +14
-1+if (longs) { +_mix_pool_bytes (entropy,sizeof (*entropy) * longs); + i += longs; +continue ; } -_mix_pool_bytes (&entropy,sizeof (entropy)); + longs =arch_get_random_longs (entropy,ARRAY_SIZE (entropy) - i); +if (longs) { +_mix_pool_bytes (entropy,sizeof (*entropy) * longs); + i += longs; +continue ; + } + entropy[0 ] =random_get_entropy (); +_mix_pool_bytes (entropy,sizeof (*entropy)); + arch_bits -=sizeof (*entropy) *8 ; + ++i; }_mix_pool_bytes (&now,sizeof (now));_mix_pool_bytes (utsname (),sizeof (*(utsname ())));include/asm-generic/archrandom.h +4
-14@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long(unsigned long *v) +static inline size_t __must_check arch_get_random_longs (unsigned long *v,size_t max_longs) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int(unsigned int *v) +static inline size_t __must_check arch_get_random_seed_longs (unsigned long *v,size_t max_longs) { - return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; + return 0; }#endif include/linux/random.h +6
-6@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early(unsigned long *v) +#ifndef arch_get_random_seed_longs_early +static inline size_t __init arch_get_random_seed_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_longs (v, max_longs); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early(unsigned long *v) +#ifndef arch_get_random_longs_early +static inline bool __init arch_get_random_longs_early (unsigned long *v,size_t max_longs) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_longs (v, max_longs);
Quoted 2 lines
}
#endif
Nice interface :-)
Here is my
but please keep in mind that your patch does not apply cleanly as there is my other patch918e75f77af7 ("s390/archrandom: prevent CPACF trng invocations in interrupt context") in between.
Thanks for your work
Harald
Jason A. Donenfeld
maintainerRe: [PATCH v3] random: handle archrandom with multiple longs
Hi Harald,
Quoted 4 lines · Harald Freudenberger
Nice interface :-)
Here is my
You're too late; I already sent in my pull request. But that's okay; I received Heiko's ack on this for s390.
Quoted 5 lines
but please keep in mind that your patch does not apply cleanly as there is my other patch
918e75f77af7("s390/archrandom: prevent CPACF trng invocations in interrupt context")
in between.
Yea, this was a late addition to 5.19, and my random tree was based on an earlier rc of 5.19. That kind of thing happens and is fine; Stephen caught it when doing the next merge and carries the merge conflict resolution there. I mentioned it to Linus in my pull, so he'll be able to work it out.
Jason
Holger Dengler
maintainerHi Jason,
Quoted 5 lines · Jason A. Donenfeld
The archrandom interface was originally designed for x86, which supplies RDRAND/RDSEED for receiving random words into registers, resulting in one function to generate an int and another to generate a long. However, other architectures don't follow this.
Quoted 8 lines
On arm64, the SMCCC TRNG interface can return between 1 and 3 words. On s390, the CPACF TRNG interface can return between 1 and 32 words for the same cost as for one word. On UML, the
os_getrandom()interface can return arbitrary amounts.So change the api signature to take a "words" parameter designating the maximum number of words requested, and then return the number of words generated.
Why not changing the API to take bytes instead of words? Sure, at the moment it looks like all platforms with TRNG support are able to deliver at least one word, but bytes would be more flexible.
Quoted 560 lines
Since callers need to check this return value and loop anyway, each arch implementation does not bother implementing its own loop to try again to fill the requested number of words. Additionally, all existing callers pass in a constant words parameter. Taken together, these two things mean that the codegen doesn't really change much for one-word-at-a-time platforms, while performance is greatly improved on platforms such as s390.
Cc Will Deacon <[email protected]>Cc Michael Ellerman <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Thomas Gleixner <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Catalin Marinas <[email protected]>Cc Borislav Petkov <[email protected]>Cc Heiko Carstens <[email protected]>Cc Johannes Berg <[email protected]>Cc Harald Freudenberger <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/arm64/include/asm/archrandom.h | 102 ++++++++++++-------------- arch/arm64/kernel/kaslr.c | 2 +- arch/powerpc/include/asm/archrandom.h | 30 ++------ arch/powerpc/kvm/book3s_hv.c | 2 +- arch/s390/include/asm/archrandom.h | 29 ++------ arch/um/include/asm/archrandom.h | 21 ++---- arch/x86/include/asm/archrandom.h | 41 +---------- arch/x86/kernel/espfix_64.c | 2 +- drivers/char/random.c | 45 ++++++++---- include/asm-generic/archrandom.h | 18 +---- include/linux/random.h | 12 +-- 11 files changed, 116 insertions(+), 188 deletions(-)arch/arm64/include/asm/archrandom.h +47
-55@@ -58,7 +58,7 @@ static inline bool __arm64_rndrrs(unsigned long *v)return ok; } -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) {/* * Only support the generic interface after we have detected @@ -66,27 +66,15 @@ static inline bool __must_check arch_get_random_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 ) &&__arm64_rndr (v)) -return true; -return false; +if (words &&cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndr (v)) +return 1 ; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -unsigned long val; - -if (__arm64_rndr (&val)) { - *v = val; -return true; - } - } -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -struct arm_smccc_res res; +if (!words) +return 0 ;/* * We prefer the SMCCC call, since its semantics (return actual @@ -95,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* (the output of a pseudo RNG freshly seeded by a TRNG). */ if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); +struct arm_smccc_res res; + + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }@@ -108,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)* enough to implement this API if no other entropy source exists. */ if (cpus_have_const_cap (ARM64_HAS_RNG ) &&__arm64_rndrrs (v)) -return true; +return 1 ; -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -struct arm_smccc_res res; -unsigned long val; - -if (smccc_trng_available) { -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,32 , &res); -if ((int )res.a0 >=0 ) { - *v = res.a3 &GENMASK (31 ,0 ); -return true; - } - } - -if (cpus_have_const_cap (ARM64_HAS_RNG )) { -if (__arm64_rndrrs (&val)) { - *v = val; -return true; - } - } - -return false; +return 0 ; }static inline bool __init __early_cpu_has_rndr (void )@@ -143,26 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)return (ftr >>ID_AA64ISAR0_EL1_RNDR_SHIFT ) &0xf ; } -static inline bool __init __must_check -arch_get_random_seed_long_early (unsigned long *v) +static inline size_t __init __must_check +arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); +if (!words) +return 0 ; +if (smccc_trng_available) {struct arm_smccc_res res; -arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 ,64 , &res); + words =min_t (size_t,3 , words); +arm_smccc_1_1_invoke (ARM_SMCCC_TRNG_RND64 , words *64 , &res);if ((int )res.a0 >=0 ) { - *v = res.a3; -return true; +switch (words) { +case 3 : + *v++ = res.a1; + fallthrough; +case 2 : + *v++ = res.a2; + fallthrough; +case 1 : + *v++ = res.a3; +break ; + } +return words; } }if (__early_cpu_has_rndr () &&__arm64_rndr (v)) -return true; +return 1 ; -return false; +return 0 ; } -#define arch_get_random_seed_long_early arch_get_random_seed_long_early +#define arch_get_random_seed_words_early arch_get_random_seed_words_early#endif /* _ASM_ARCHRANDOM_H */ arch/arm64/kernel/kaslr.c +1
-1@@ -106,7 +106,7 @@ u64 __init kaslr_early_init(void)* and supported. */ -if (arch_get_random_seed_long_early (&raw)) +if (arch_get_random_seed_words_early (&raw,1 )) seed ^= raw;if (!seed) {arch/powerpc/include/asm/archrandom.h +6
-24@@ -4,34 +4,16 @@#include <asm/machdep.h> -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -if (ppc_md.get_random_seed) -return ppc_md.get_random_seed (v); - -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -unsigned long val; -bool rc; - - rc =arch_get_random_seed_long (&val); -if (rc) - *v = val; - -return rc; +if (words && ppc_md.get_random_seed && ppc_md.get_random_seed (v)) +return 1 ; +return 0 ; }#ifdef CONFIG_PPC_POWERNV arch/powerpc/kvm/book3s_hv.c +1
-1@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)break ;#endif case H_RANDOM : -if (!arch_get_random_seed_long (&vcpu->arch.regs.gpr[4 ])) +if (!arch_get_random_seed_words (&vcpu->arch.regs.gpr[4 ],1 )) ret =H_HARDWARE ;break ;case H_RPT_INVALIDATE :arch/s390/include/asm/archrandom.h +7
-22@@ -18,34 +18,19 @@DECLARE_STATIC_KEY_FALSE (s390_arch_random_available);extern atomic64_t s390_arch_random_counter; -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -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 (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; - } -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) {if (static_branch_likely (&s390_arch_random_available)) { -cpacf_trng (NULL ,0 , (u8 *)v,sizeof (*v)); -atomic64_add (sizeof (*v), &s390_arch_random_counter); -return true; +cpacf_trng (NULL ,0 , (u8 *)v, words *sizeof (*v)); +atomic64_add (words *sizeof (*v), &s390_arch_random_counter); +return words; } -return false; +return 0 ; }#endif /* _ASM_S390_ARCHRANDOM_H */ arch/um/include/asm/archrandom.h +8
-13@@ -7,24 +7,19 @@/* This is from <os.h>, but better not to #include that in a global header here. */ ssize_t os_getrandom (void *buf,size_t len,unsigned int flags); -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} +ssize_t ret; -static inline bool __must_check arch_get_random_int (unsigned int *v) -{ -return os_getrandom (v,sizeof (*v),0 ) ==sizeof (*v); -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; + ret =os_getrandom (v, words *sizeof (*v),0 ); +if (ret <0 ) +return 0 ; +return ret /sizeof (*v); } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; +return 0 ; }#endif arch/x86/include/asm/archrandom.h +4
-37@@ -31,20 +31,6 @@ static inline bool __must_check rdrand_long(unsigned long *v)return false; } -static inline bool __must_check rdrand_int (unsigned int *v) -{ -bool ok; -unsigned int retry =RDRAND_RETRY_LOOPS ; -do { - asm volatile("rdrand %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); -if (ok) -return true; - }while (--retry); -return false; -} -static inline bool __must_check rdseed_long (unsigned long *v) {bool ok;@@ -54,38 +40,19 @@ static inline bool __must_check rdseed_long(unsigned long *v)return ok; } -static inline bool __must_check rdseed_int (unsigned int *v) -{ -bool ok; - asm volatile("rdseed %[out]" - CC_SET(c) - : CC_OUT(c) (ok), [out] "=r" (*v)); - return ok; -} -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked. */ -static inline bool __must_check arch_get_random_long (unsigned long *v) -{ -return static_cpu_has (X86_FEATURE_RDRAND ) ?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; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDRAND ) &&rdrand_long (v) ?1 :0 ; } -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; +return words &&static_cpu_has (X86_FEATURE_RDSEED ) &&rdseed_long (v) ?1 :0 ; }#ifndef CONFIG_UML arch/x86/kernel/espfix_64.c +1
-1@@ -100,7 +100,7 @@ static void init_espfix_random(void)* This is run before the entropy pools are initialized, * but this is hopefully better than nothing. */ -if (!arch_get_random_long (&rand)) { +if (!arch_get_random_words (&rand,1 )) {/* The constant is an arbitrary large prime */ rand =rdtsc (); rand *=0xc345c6b72fd16123UL ;drivers/char/random.c +31
-14@@ -596,12 +596,20 @@ static void extract_entropy(void *buf, size_t len)unsigned long rdseed[32 /sizeof (long )];size_t counter; } block; -size_t i; +size_t i, words; -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); +for (i =0 ; i <ARRAY_SIZE (block.rdseed);) { + words =arch_get_random_seed_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + words =arch_get_random_words (&block.rdseed[i],ARRAY_SIZE (block.rdseed) - i); +if (words) { + i += words; +continue ; + } + block.rdseed[i++] =random_get_entropy (); }spin_lock_irqsave (&input_pool.lock, flags);@@ -776,22 +784,31 @@ static struct notifier_block pm_notifier = { .notifier_call = random_pm_notificaint __init random_init (const char *command_line) {ktime_t now =ktime_get_real (); -unsigned int i, arch_bits; -unsigned long entropy; +size_t i, words, arch_bits; +unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )];#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;_mix_pool_bytes (compiletime_seed,sizeof (compiletime_seed));#endif -for (i =0 , arch_bits =BLAKE2S_BLOCK_SIZE *8 ; - i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (entropy)) { -if (!arch_get_random_seed_long_early (&entropy) && - !arch_get_random_long_early (&entropy)) { - entropy =random_get_entropy (); - arch_bits -=sizeof (entropy) *8 ; +for (i =0 , arch_bits =sizeof (entropy) *8 ; i <ARRAY_SIZE (entropy);) { + words =arch_get_random_seed_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; } -_mix_pool_bytes (&entropy,sizeof (entropy)); + words =arch_get_random_words (entropy,ARRAY_SIZE (entropy) - i); +if (words) { +_mix_pool_bytes (entropy,sizeof (*entropy) * words); + i += words; +continue ; + } + entropy[0 ] =random_get_entropy (); +_mix_pool_bytes (entropy,sizeof (*entropy)); + arch_bits -=sizeof (*entropy) *8 ; + ++i; }_mix_pool_bytes (&now,sizeof (now));_mix_pool_bytes (utsname (),sizeof (*(utsname ())));include/asm-generic/archrandom.h +4
-14@@ -2,24 +2,14 @@#ifndef __ASM_GENERIC_ARCHRANDOM_H__#define __ASM_GENERIC_ARCHRANDOM_H__ -static inline bool __must_check arch_get_random_long (unsigned long *v) +static inline size_t __must_check arch_get_random_words (unsigned long *v,size_t words) { -return false; +return 0 ; } -static inline bool __must_check arch_get_random_int (unsigned int *v) +static inline size_t __must_check arch_get_random_seed_words (unsigned long *v,size_t words) { -return false; -} - -static inline bool __must_check arch_get_random_seed_long (unsigned long *v) -{ -return false; -} - -static inline bool __must_check arch_get_random_seed_int (unsigned int *v) -{ -return false; +return 0 ; }#endif include/linux/random.h +6
-6@@ -112,19 +112,19 @@ declare_get_random_var_wait(long, unsigned long)* Called from the boot CPU during startup; not valid to call once * secondary CPUs are up and preemption is possible. */ -#ifndef arch_get_random_seed_long_early -static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +#ifndef arch_get_random_seed_words_early +static inline size_t __init arch_get_random_seed_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_seed_long (v); +return arch_get_random_seed_words (v, words); }#endif -#ifndef arch_get_random_long_early -static inline bool __init arch_get_random_long_early (unsigned long *v) +#ifndef arch_get_random_words_early +static inline bool __init arch_get_random_words_early (unsigned long *v,size_t words) {WARN_ON (system_state !=SYSTEM_BOOTING ); -return arch_get_random_long (v); +return arch_get_random_words (v, words); }#endif
Signature · 5 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler -- IBM Systems, Linux on IBM Z Development [email protected]
Jason A. Donenfeld
maintainerHi Holger,
Quoted 3 lines · Holger Dengler
Why not changing the API to take bytes instead of words? Sure, at the moment it looks like all platforms with TRNG support are able to deliver at least one word, but bytes would be more flexible.
The idea is to strike a sweet spot between capabilities. S390x is fine with byte-level granularity up to arbitrary lengths, while x86 is best with word-level granularity of length 1. The happy intersection between the two is just word-level granularity of arbitrary length. Yes we _could_ introduce a lot of code complexity by cascading the x86 case down into smaller and smaller registers, ignoring the fact that it's no longer efficient below 32- or 64-bit registers depending on vendor. But then we're relying on the inliner to remove all of that extra code, since all callers actually only ever want 32 or 64 bytes. Why bloat for nothing? The beauty of this approach is that it translates very naturally over all the various quirks of architectures without having to have a lot of coupling code.
The other reason is that it's simply not necessary. The primary use for this in random.c is to fill a 32- or 64-*byte* block with "some stuff", preferring RDSEED, then RDRAND, and finally falling back to RDTSC. These correspond with arch_get_random_seed_longs(), arch_get_random_longs(), and random_get_entropy() (which is usually get_cycles() underneath), respectively. With the cycle counter being (at least) ~word-sized on all platforms, keeping the granularity of the arch_get_random_*_longs() functions the same lets us fill these with a basic cascade that doesn't require a lot of code:
unsigned long array[whatever];
for (i = 0; i < ARRAY_SIZE(array);) {
longs = arch_get_random_seed_longs(&array[i], ARRAY_SIZE(array) - i);
if (longs) {
i += longs;
continue;
}
longs = arch_get_random_longs(&array[i], ARRAY_SIZE(array) - i);
if (longs) {
i += longs;
continue;
}
array[i++] = random_get_entropy();
}By using a word as the underlying unit, the above cascade generates optimal code on basically all archrandom platforms, no matter what their byte-vs-word or one-vs-three-vs-many semantics are.
That's a bit long winded, but hopefully that gives a bit of insight on why going from _long -> _longs is so "lazy" looking.
Jason
Holger Dengler
maintainerHi Jason,
Quoted 5 lines · Jason A. Donenfeld
Quoted 3 lines · Holger Dengler
Why not changing the API to take bytes instead of words? Sure, at the moment it looks like all platforms with TRNG support are able to deliver at least one word, but bytes would be more flexible.
Quoted 12 lines
The idea is to strike a sweet spot between capabilities. S390x is fine with byte-level granularity up to arbitrary lengths, while x86 is best with word-level granularity of length 1. The happy intersection between the two is just word-level granularity of arbitrary length. Yes we _could_ introduce a lot of code complexity by cascading the x86 case down into smaller and smaller registers, ignoring the fact that it's no longer efficient below 32- or 64-bit registers depending on vendor. But then we're relying on the inliner to remove all of that extra code, since all callers actually only ever want 32 or 64 bytes. Why bloat for nothing? The beauty of this approach is that it translates very naturally over all the various quirks of architectures without having to have a lot of coupling code.
You're absolutely right. Your solution addresses all needs of current architectures. My proposal was just meant as preparation for the case, that new (smaller) architectures may come up in the future with a TRNG support, but with other granularity. But anyhow: we can handle it as soon as it happens, fine with me.
Signature · 5 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler -- IBM Systems, Linux on IBM Z Development [email protected]