Skip to content

Commit a0b09f2

Browse files
committed
Merge tag 'random-6.0-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random
Pull random number generator updates from Jason Donenfeld: "Though there's been a decent amount of RNG-related development during this last cycle, not all of it is coming through this tree, as this cycle saw a shift toward tackling early boot time seeding issues, which took place in other trees as well. Here's a summary of the various patches: - The CONFIG_ARCH_RANDOM .config option and the "nordrand" boot option have been removed, as they overlapped with the more widely supported and more sensible options, CONFIG_RANDOM_TRUST_CPU and "random.trust_cpu". This change allowed simplifying a bit of arch code. - x86's RDRAND boot time test has been made a bit more robust, with RDRAND disabled if it's clearly producing bogus results. This would be a tip.git commit, technically, but I took it through random.git to avoid a large merge conflict. - The RNG has long since mixed in a timestamp very early in boot, on the premise that a computer that does the same things, but does so starting at different points in wall time, could be made to still produce a different RNG state. Unfortunately, the clock isn't set early in boot on all systems, so now we mix in that timestamp when the time is actually set. - User Mode Linux now uses the host OS's getrandom() syscall to generate a bootloader RNG seed and later on treats getrandom() as the platform's RDRAND-like faculty. - The arch_get_random_{seed_,}_long() family of functions is now arch_get_random_{seed_,}_longs(), which enables certain platforms, such as s390, to exploit considerable performance advantages from requesting multiple CPU random numbers at once, while at the same time compiling down to the same code as before on platforms like x86. - A small cleanup changing a cmpxchg() into a try_cmpxchg(), from Uros. - A comment spelling fix" More info about other random number changes that come in through various architecture trees in the full commentary in the pull request: https://lore.kernel.org/all/20220731232428.2219258-1-Jason@zx2c4.com/ * tag 'random-6.0-rc1-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random: random: correct spelling of "overwrites" random: handle archrandom with multiple longs um: seed rng using host OS rng random: use try_cmpxchg in _credit_init_bits timekeeping: contribute wall clock to rng on time change x86/rdrand: Remove "nordrand" flag in favor of "random.trust_cpu" random: remove CONFIG_ARCH_RANDOM
2 parents 569bede + 7f637be commit a0b09f2

34 files changed

Lines changed: 204 additions & 300 deletions

File tree

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3741,11 +3741,6 @@
37413741
noreplace-smp [X86-32,SMP] Don't replace SMP instructions
37423742
with UP alternatives
37433743

3744-
nordrand [X86] Disable kernel use of the RDRAND and
3745-
RDSEED instructions even if they are supported
3746-
by the processor. RDRAND and RDSEED are still
3747-
available to user space applications.
3748-
37493744
noresume [SWSUSP] Disables resume and restores original swap
37503745
space.
37513746

arch/arm/include/asm/archrandom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ static inline bool __init smccc_probe_trng(void)
77
return false;
88
}
99

10+
#include <asm-generic/archrandom.h>
11+
1012
#endif /* _ASM_ARCHRANDOM_H */

arch/arm64/Kconfig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,14 +1895,6 @@ config ARM64_E0PD
18951895

18961896
This option enables E0PD for TTBR1 where available.
18971897

1898-
config ARCH_RANDOM
1899-
bool "Enable support for random number generation"
1900-
default y
1901-
help
1902-
Random number generation (part of the ARMv8.5 Extensions)
1903-
provides a high bandwidth, cryptographically secure
1904-
hardware random number generator.
1905-
19061898
config ARM64_AS_HAS_MTE
19071899
# Initial support for MTE went in binutils 2.32.0, checked with
19081900
# ".arch armv8.5-a+memtag" below. However, this was incomplete

arch/arm64/include/asm/archrandom.h

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#ifndef _ASM_ARCHRANDOM_H
33
#define _ASM_ARCHRANDOM_H
44

5-
#ifdef CONFIG_ARCH_RANDOM
6-
75
#include <linux/arm-smccc.h>
86
#include <linux/bug.h>
97
#include <linux/kernel.h>
@@ -60,35 +58,23 @@ static inline bool __arm64_rndrrs(unsigned long *v)
6058
return ok;
6159
}
6260

63-
static inline bool __must_check arch_get_random_long(unsigned long *v)
61+
static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
6462
{
6563
/*
6664
* Only support the generic interface after we have detected
6765
* the system wide capability, avoiding complexity with the
6866
* cpufeature code and with potential scheduling between CPUs
6967
* with and without the feature.
7068
*/
71-
if (cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndr(v))
72-
return true;
73-
return false;
74-
}
75-
76-
static inline bool __must_check arch_get_random_int(unsigned int *v)
77-
{
78-
if (cpus_have_const_cap(ARM64_HAS_RNG)) {
79-
unsigned long val;
80-
81-
if (__arm64_rndr(&val)) {
82-
*v = val;
83-
return true;
84-
}
85-
}
86-
return false;
69+
if (max_longs && cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndr(v))
70+
return 1;
71+
return 0;
8772
}
8873

89-
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
74+
static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
9075
{
91-
struct arm_smccc_res res;
76+
if (!max_longs)
77+
return 0;
9278

9379
/*
9480
* We prefer the SMCCC call, since its semantics (return actual
@@ -97,10 +83,23 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
9783
* (the output of a pseudo RNG freshly seeded by a TRNG).
9884
*/
9985
if (smccc_trng_available) {
100-
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res);
86+
struct arm_smccc_res res;
87+
88+
max_longs = min_t(size_t, 3, max_longs);
89+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, max_longs * 64, &res);
10190
if ((int)res.a0 >= 0) {
102-
*v = res.a3;
103-
return true;
91+
switch (max_longs) {
92+
case 3:
93+
*v++ = res.a1;
94+
fallthrough;
95+
case 2:
96+
*v++ = res.a2;
97+
fallthrough;
98+
case 1:
99+
*v++ = res.a3;
100+
break;
101+
}
102+
return max_longs;
104103
}
105104
}
106105

@@ -110,32 +109,9 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
110109
* enough to implement this API if no other entropy source exists.
111110
*/
112111
if (cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndrrs(v))
113-
return true;
112+
return 1;
114113

115-
return false;
116-
}
117-
118-
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
119-
{
120-
struct arm_smccc_res res;
121-
unsigned long val;
122-
123-
if (smccc_trng_available) {
124-
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 32, &res);
125-
if ((int)res.a0 >= 0) {
126-
*v = res.a3 & GENMASK(31, 0);
127-
return true;
128-
}
129-
}
130-
131-
if (cpus_have_const_cap(ARM64_HAS_RNG)) {
132-
if (__arm64_rndrrs(&val)) {
133-
*v = val;
134-
return true;
135-
}
136-
}
137-
138-
return false;
114+
return 0;
139115
}
140116

141117
static inline bool __init __early_cpu_has_rndr(void)
@@ -145,34 +121,40 @@ static inline bool __init __early_cpu_has_rndr(void)
145121
return (ftr >> ID_AA64ISAR0_EL1_RNDR_SHIFT) & 0xf;
146122
}
147123

148-
static inline bool __init __must_check
149-
arch_get_random_seed_long_early(unsigned long *v)
124+
static inline size_t __init __must_check
125+
arch_get_random_seed_longs_early(unsigned long *v, size_t max_longs)
150126
{
151127
WARN_ON(system_state != SYSTEM_BOOTING);
152128

129+
if (!max_longs)
130+
return 0;
131+
153132
if (smccc_trng_available) {
154133
struct arm_smccc_res res;
155134

156-
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res);
135+
max_longs = min_t(size_t, 3, max_longs);
136+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, max_longs * 64, &res);
157137
if ((int)res.a0 >= 0) {
158-
*v = res.a3;
159-
return true;
138+
switch (max_longs) {
139+
case 3:
140+
*v++ = res.a1;
141+
fallthrough;
142+
case 2:
143+
*v++ = res.a2;
144+
fallthrough;
145+
case 1:
146+
*v++ = res.a3;
147+
break;
148+
}
149+
return max_longs;
160150
}
161151
}
162152

163153
if (__early_cpu_has_rndr() && __arm64_rndr(v))
164-
return true;
165-
166-
return false;
167-
}
168-
#define arch_get_random_seed_long_early arch_get_random_seed_long_early
154+
return 1;
169155

170-
#else /* !CONFIG_ARCH_RANDOM */
171-
172-
static inline bool __init smccc_probe_trng(void)
173-
{
174-
return false;
156+
return 0;
175157
}
158+
#define arch_get_random_seed_longs_early arch_get_random_seed_longs_early
176159

177-
#endif /* CONFIG_ARCH_RANDOM */
178160
#endif /* _ASM_ARCHRANDOM_H */

arch/arm64/kernel/cpufeature.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,6 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
25022502
.cpu_enable = cpu_enable_e0pd,
25032503
},
25042504
#endif
2505-
#ifdef CONFIG_ARCH_RANDOM
25062505
{
25072506
.desc = "Random Number Generator",
25082507
.capability = ARM64_HAS_RNG,
@@ -2514,7 +2513,6 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
25142513
.sign = FTR_UNSIGNED,
25152514
.min_field_value = 1,
25162515
},
2517-
#endif
25182516
#ifdef CONFIG_ARM64_BTI
25192517
{
25202518
.desc = "Branch Target Identification",

arch/powerpc/Kconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,6 @@ config PHYSICAL_START
12571257
default "0x00000000"
12581258
endif
12591259

1260-
config ARCH_RANDOM
1261-
def_bool n
1262-
12631260
config PPC_LIB_RHEAP
12641261
bool
12651262

arch/powerpc/include/asm/archrandom.h

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,19 @@
22
#ifndef _ASM_POWERPC_ARCHRANDOM_H
33
#define _ASM_POWERPC_ARCHRANDOM_H
44

5-
#ifdef CONFIG_ARCH_RANDOM
6-
75
#include <asm/machdep.h>
86

9-
static inline bool __must_check arch_get_random_long(unsigned long *v)
10-
{
11-
return false;
12-
}
13-
14-
static inline bool __must_check arch_get_random_int(unsigned int *v)
15-
{
16-
return false;
17-
}
18-
19-
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
7+
static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
208
{
21-
if (ppc_md.get_random_seed)
22-
return ppc_md.get_random_seed(v);
23-
24-
return false;
9+
return 0;
2510
}
2611

27-
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
12+
static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
2813
{
29-
unsigned long val;
30-
bool rc;
31-
32-
rc = arch_get_random_seed_long(&val);
33-
if (rc)
34-
*v = val;
35-
36-
return rc;
14+
if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed(v))
15+
return 1;
16+
return 0;
3717
}
38-
#endif /* CONFIG_ARCH_RANDOM */
3918

4019
#ifdef CONFIG_PPC_POWERNV
4120
int powernv_hwrng_present(void);

arch/powerpc/include/asm/machdep.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ struct machdep_calls {
200200
ssize_t (*cpu_release)(const char *, size_t);
201201
#endif
202202

203-
#ifdef CONFIG_ARCH_RANDOM
204203
int (*get_random_seed)(unsigned long *v);
205-
#endif
206204
};
207205

208206
extern void e500_idle(void);

arch/powerpc/kvm/book3s_hv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
12071207
break;
12081208
#endif
12091209
case H_RANDOM:
1210-
if (!arch_get_random_seed_long(&vcpu->arch.regs.gpr[4]))
1210+
if (!arch_get_random_seed_longs(&vcpu->arch.regs.gpr[4], 1))
12111211
ret = H_HARDWARE;
12121212
break;
12131213
case H_RPT_INVALIDATE:

arch/powerpc/platforms/microwatt/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ config PPC_MICROWATT
66
select PPC_ICS_NATIVE
77
select PPC_ICP_NATIVE
88
select PPC_UDBG_16550
9-
select ARCH_RANDOM
109
help
1110
This option enables support for FPGA-based Microwatt implementations.
1211

0 commit comments

Comments
 (0)