Skip to content

Commit

Permalink
Merge tag 'random-6.0-rc1-for-linus' of git://git.kernel.org/pub/scm/…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
torvalds committed Aug 3, 2022
2 parents 569bede + 7f637be commit a0b09f2
Show file tree
Hide file tree
Showing 34 changed files with 204 additions and 300 deletions.
5 changes: 0 additions & 5 deletions Documentation/admin-guide/kernel-parameters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3741,11 +3741,6 @@
noreplace-smp [X86-32,SMP] Don't replace SMP instructions
with UP alternatives

nordrand [X86] Disable kernel use of the RDRAND and
RDSEED instructions even if they are supported
by the processor. RDRAND and RDSEED are still
available to user space applications.

noresume [SWSUSP] Disables resume and restores original swap
space.

Expand Down
2 changes: 2 additions & 0 deletions arch/arm/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ static inline bool __init smccc_probe_trng(void)
return false;
}

#include <asm-generic/archrandom.h>

#endif /* _ASM_ARCHRANDOM_H */
8 changes: 0 additions & 8 deletions arch/arm64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1895,14 +1895,6 @@ config ARM64_E0PD

This option enables E0PD for TTBR1 where available.

config ARCH_RANDOM
bool "Enable support for random number generation"
default y
help
Random number generation (part of the ARMv8.5 Extensions)
provides a high bandwidth, cryptographically secure
hardware random number generator.

config ARM64_AS_HAS_MTE
# Initial support for MTE went in binutils 2.32.0, checked with
# ".arch armv8.5-a+memtag" below. However, this was incomplete
Expand Down
112 changes: 47 additions & 65 deletions arch/arm64/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#ifndef _ASM_ARCHRANDOM_H
#define _ASM_ARCHRANDOM_H

#ifdef CONFIG_ARCH_RANDOM

#include <linux/arm-smccc.h>
#include <linux/bug.h>
#include <linux/kernel.h>
Expand Down Expand Up @@ -60,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;
}

static inline bool __must_check arch_get_random_int(unsigned int *v)
{
if (cpus_have_const_cap(ARM64_HAS_RNG)) {
unsigned long val;

if (__arm64_rndr(&val)) {
*v = val;
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_seed_long(unsigned long *v)
static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
{
struct arm_smccc_res res;
if (!max_longs)
return 0;

/*
* We prefer the SMCCC call, since its semantics (return actual
Expand All @@ -97,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 @@ -110,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 @@ -145,34 +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 false;
}
#define arch_get_random_seed_long_early arch_get_random_seed_long_early
return 1;

#else /* !CONFIG_ARCH_RANDOM */

static inline bool __init smccc_probe_trng(void)
{
return false;
return 0;
}
#define arch_get_random_seed_longs_early arch_get_random_seed_longs_early

#endif /* CONFIG_ARCH_RANDOM */
#endif /* _ASM_ARCHRANDOM_H */
2 changes: 0 additions & 2 deletions arch/arm64/kernel/cpufeature.c
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,6 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
.cpu_enable = cpu_enable_e0pd,
},
#endif
#ifdef CONFIG_ARCH_RANDOM
{
.desc = "Random Number Generator",
.capability = ARM64_HAS_RNG,
Expand All @@ -2514,7 +2513,6 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
.sign = FTR_UNSIGNED,
.min_field_value = 1,
},
#endif
#ifdef CONFIG_ARM64_BTI
{
.desc = "Branch Target Identification",
Expand Down
3 changes: 0 additions & 3 deletions arch/powerpc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1257,9 +1257,6 @@ config PHYSICAL_START
default "0x00000000"
endif

config ARCH_RANDOM
def_bool n

config PPC_LIB_RHEAP
bool

Expand Down
33 changes: 6 additions & 27 deletions arch/powerpc/include/asm/archrandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@
#ifndef _ASM_POWERPC_ARCHRANDOM_H
#define _ASM_POWERPC_ARCHRANDOM_H

#ifdef CONFIG_ARCH_RANDOM

#include <asm/machdep.h>

static inline bool __must_check arch_get_random_long(unsigned long *v)
{
return false;
}

static inline bool __must_check arch_get_random_int(unsigned int *v)
{
return false;
}

static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
{
if (ppc_md.get_random_seed)
return ppc_md.get_random_seed(v);

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_longs(unsigned long *v, size_t max_longs)
{
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;
}
#endif /* CONFIG_ARCH_RANDOM */

#ifdef CONFIG_PPC_POWERNV
int powernv_hwrng_present(void);
Expand Down
2 changes: 0 additions & 2 deletions arch/powerpc/include/asm/machdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ struct machdep_calls {
ssize_t (*cpu_release)(const char *, size_t);
#endif

#ifdef CONFIG_ARCH_RANDOM
int (*get_random_seed)(unsigned long *v);
#endif
};

extern void e500_idle(void);
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
1 change: 0 additions & 1 deletion arch/powerpc/platforms/microwatt/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ config PPC_MICROWATT
select PPC_ICS_NATIVE
select PPC_ICP_NATIVE
select PPC_UDBG_16550
select ARCH_RANDOM
help
This option enables support for FPGA-based Microwatt implementations.

1 change: 0 additions & 1 deletion arch/powerpc/platforms/powernv/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ config PPC_POWERNV
select EPAPR_BOOT
select PPC_INDIRECT_PIO
select PPC_UDBG_16550
select ARCH_RANDOM
select CPU_FREQ
select PPC_DOORBELL
select MMU_NOTIFIER
Expand Down
1 change: 0 additions & 1 deletion arch/powerpc/platforms/pseries/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ config PPC_PSERIES
select PPC_UDBG_16550
select PPC_DOORBELL
select HOTPLUG_CPU
select ARCH_RANDOM
select FORCE_SMP
select SWIOTLB
default y
Expand Down
15 changes: 0 additions & 15 deletions arch/s390/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -508,21 +508,6 @@ config KEXEC_SIG
verification for the corresponding kernel image type being
loaded in order for this to work.

config ARCH_RANDOM
def_bool y
prompt "s390 architectural random number generation API"
help
Enable the s390 architectural random number generation API
to provide random data for all consumers within the Linux
kernel.

When enabled the arch_random_* functions declared in linux/random.h
are implemented. The implementation is based on the s390 CPACF
instruction subfunction TRNG which provides a real true random
number generator.

If unsure, say Y.

config KERNEL_NOBP
def_bool n
prompt "Enable modified branch prediction for the kernel by default"
Expand Down
1 change: 0 additions & 1 deletion arch/s390/configs/zfcpdump_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ CONFIG_TUNE_ZEC12=y
# CONFIG_COMPAT is not set
CONFIG_NR_CPUS=2
CONFIG_HZ_100=y
# CONFIG_ARCH_RANDOM is not set
# CONFIG_RELOCATABLE is not set
# CONFIG_CHSC_SCH is not set
# CONFIG_SCM_BUS is not set
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ obj-$(CONFIG_CRYPTO_CHACHA_S390) += chacha_s390.o
obj-$(CONFIG_S390_PRNG) += prng.o
obj-$(CONFIG_CRYPTO_GHASH_S390) += ghash_s390.o
obj-$(CONFIG_CRYPTO_CRC32_S390) += crc32-vx_s390.o
obj-$(CONFIG_ARCH_RANDOM) += arch_random.o
obj-y += arch_random.o

crc32-vx_s390-y := crc32-vx.o crc32le-vx.o crc32be-vx.o
chacha_s390-y := chacha-glue.o chacha-s390.o
Loading

0 comments on commit a0b09f2

Please sign in to comment.