Skip to content

Commit

Permalink
random: handle archrandom with multiple longs
Browse files Browse the repository at this point in the history
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 one and three
longs. On s390, the CPACF TRNG interface can return arbitrary amounts,
with four 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.

Acked-by: Heiko Carstens <[email protected]>
Acked-by: Catalin Marinas <[email protected]>
Acked-by: Mark Rutland <[email protected]>
Acked-by: Michael Ellerman <[email protected]>
Acked-by: Borislav Petkov <[email protected]>
Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed Jul 25, 2022
1 parent 0b9ba61 commit d349ab9
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 188 deletions.
102 changes: 47 additions & 55 deletions arch/arm64/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,23 @@ 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
* the system wide capability, avoiding complexity with the
* 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
Expand All @@ -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;
}
}

Expand All @@ -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)
Expand All @@ -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 */
2 changes: 1 addition & 1 deletion arch/arm64/kernel/kaslr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 6 additions & 24 deletions arch/powerpc/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/kvm/book3s_hv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 7 additions & 22 deletions arch/s390/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
21 changes: 8 additions & 13 deletions arch/um/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 4 additions & 37 deletions arch/x86/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/espfix_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d349ab9

Please sign in to comment.