archs/random: fallback to best raw ktime when no cycle counter
Subsystems (12)
Jason A. Donenfeld
maintainerv5 cover archs/random: fallback to best raw ktime when no cycle counter
Hi folks,
The RNG uses a function called random_get_entropy() basically anytime that it needs to timestamp an event. For example, an interrupt comes in, and we mix a random_get_entropy() into the entropy pool somehow. Somebody mashes their keyboard or moves their mouse around? We mix a random_get_entropy() into the entropy pool. It's one of the main varieties of input.
Unfortunately, it's always 0 on a few platforms. The RNG has accumulated various hacks to deal with this, but in general it's not great. Surely we can do better than 0. In fact, *anything* that's not the same exact value all the time would be better than 0. Even a counter that increments once per hour would be better than 0! I think you get the idea.
On most platforms, random_get_entropy() is aliased to get_cycles(), which makes sense for platforms where get_cycles() is defined. RDTSC, for example, has all the characteristics we care about for this
function: it's fast to acquire (i.e. acceptable in an irq handler), pretty high precision, available, forms a 2-monotone distribution, etc. But for platforms without that, what is the next best thing?
Sometimes the next best thing is architecture-defined. For example, really old MIPS has the C0 random register, which isn't quite a cycle counter, but is at least something. However, some platforms don't even have an architecture-defined fallback.
Fortunately, the timekeeping subsystem has already solved this problem of trying to determine what the least bad clock is on constrained systems, falling back to jiffies in the worst case. By exporting the raw clock, we can get a decent fallback function for when there's no cycle counter or architecture-specific function.
This series makes the RNG more useful on: m68k, RISC-V, MIPS, ARM32, NIOS II, SPARC32, Xtensa, and Usermode Linux. Previously these platforms would, in certain circumstances, but out of luck with regards to having any type of event timestamping source in the RNG.
Finally, note that this series isn't about "jitter entropy" or other ways of initializing the RNG. That's a different topic for a different thread. Please don't let this discussion veer off into that. Here, I'm just trying to find a good fallback counter/timer for platforms without get_cycles(), a question with limited scope.
If this (or a future revision) looks good to you all and receives the requisite acks, my plan was to take these through the random.git tree for 5.19, so that I can then build on top of it.
Thanks,
Jason
Changes v4->v5:
- Do not prototype symbol with 'extern', according to style guide.
- On MIPS, combine random_get_entropy_fallback() with the c0 random register in a way that matches the format of the c0 random value, so that we get the best of a high precision cycle counter and of larger period timer, joined together. As a result, Thomas Bogendoerfer's ack on v4 of patch 4 has been dropped, since this is a substantial change.
Changes v3->v4:
- Use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL.
Changes v2->v3:
- Name the fallback function random_get_entropy_fallback(), so that it can be changed out as needed.
- Include header with prototype in timekeeping.c to avoid compiler warning.
- Export fallback function symbol.
Changes v1->v2:
- Use ktime_read_raw_clock() instead of sched_clock(), per Thomas' suggestion.
- Drop arm64 change.
- Cleanup header inclusion ordering problem.
Jason A. Donenfeld (11):
timekeeping: add raw clock fallback for random_get_entropy()
m68k: use fallback for random_get_entropy() instead of zero
riscv: use fallback for random_get_entropy() instead of zero
mips: use fallback for random_get_entropy() instead of just c0 random
arm: use fallback for random_get_entropy() instead of zero
nios2: use fallback for random_get_entropy() instead of zero
x86: use fallback for random_get_entropy() instead of zero
um: use fallback for random_get_entropy() instead of zero
sparc: use fallback for random_get_entropy() instead of zero
xtensa: use fallback for random_get_entropy() instead of zero
random: insist on random_get_entropy() existing in order to simplify
arch/arm/include/asm/timex.h | 1 + arch/m68k/include/asm/timex.h | 2 +- arch/mips/include/asm/timex.h | 16 +++--- arch/nios2/include/asm/timex.h | 2 + arch/riscv/include/asm/timex.h | 2 +- arch/sparc/include/asm/timex_32.h | 4 +- arch/um/include/asm/timex.h | 9 +--- arch/x86/include/asm/timex.h | 10 ++++ arch/xtensa/include/asm/timex.h | 6 +-- drivers/char/random.c | 89 ++++++++++--------------------- include/linux/timex.h | 8 +++ kernel/time/timekeeping.c | 10 ++++ 12 files changed, 74 insertions(+), 85 deletions(-)
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 1/11 timekeeping: add raw clock fallback for random_get_entropy()
The addition of random_get_entropy_fallback() provides access to whichever time source has the highest frequency, which is useful for gathering entropy on platforms without available cycle counters. It's not necessarily as good as being able to quickly access a cycle counter that the CPU has, but it's still something, even when it falls back to being jiffies-based.
In the event that a given arch does not define get_cycles(), falling back to the get_cycles() default implementation that returns 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
--- include/linux/timex.h | 8 ++++++++ kernel/time/timekeeping.c | 10 ++++++++++ 2 files changed, 18 insertions(+)
include/linux/timex.h +8 -0
@@ -62,6 +62,8 @@#include <linux/types.h> #include <linux/param.h> +unsigned long random_get_entropy_fallback (void ); +#include <asm/timex.h> #ifndef random_get_entropy
@@ -74,8 +76,14 @@* * By default we use get_cycles() for this purpose, but individual * architectures may override this in their asm/timex.h header file. +* If a given arch does not have get_cycles(), then we fallback to +* using random_get_entropy_fallback(). */ +#ifdef get_cycles#define random_get_entropy () ((unsigned long)get_cycles()) +#else +#define random_get_entropy () random_get_entropy_fallback() +#endif #endif /*
kernel/time/timekeeping.c +10 -0
@@ -17,6 +17,7 @@#include <linux/clocksource.h> #include <linux/jiffies.h> #include <linux/time.h> +#include <linux/timex.h> #include <linux/tick.h> #include <linux/stop_machine.h> #include <linux/pvclock_gtod.h>
@@ -2380,6 +2381,15 @@ static int timekeeping_validate_timex(const struct __kernel_timex *txc)return 0 ; } +/** +* random_get_entropy_fallback - Returns the raw clock source value, +* used by random.c for platforms with no valid random_get_entropy(). +*/ +unsigned long random_get_entropy_fallback (void ) +{ +return tk_clock_read (&tk_core.timekeeper.tkr_mono); +} +EXPORT_SYMBOL_GPL (random_get_entropy_fallback);/** * do_adjtimex() - Accessor function to NTP __do_adjtimex function
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 2/11 m68k: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
--- arch/m68k/include/asm/timex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/m68k/include/asm/timex.h +1 -1
@@ -35,7 +35,7 @@ static inline unsigned long random_get_entropy(void) {if (mach_random_get_entropy)return mach_random_get_entropy (); -return 0 ; +return random_get_entropy_fallback (); }#define random_get_entropy random_get_entropy
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 3/11 riscv: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
--- arch/riscv/include/asm/timex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/timex.h +1 -1
@@ -41,7 +41,7 @@ static inline u32 get_cycles_hi(void)static inline unsigned long random_get_entropy (void ) {if (unlikely (clint_time_val ==NULL )) -return 0 ; +return random_get_entropy_fallback ();return get_cycles (); }#define random_get_entropy () random_get_entropy()
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 4/11 mips: use fallback for random_get_entropy() instead of just c0 random
In reply to Jason A. DonenfeldFor situations in which we don't have a c0 counter register available, we've been falling back to reading the c0 "random" register, which is usually bounded by the amount of TLB entries and changes every other cycle or so. This means it wraps extremely often. We can do better by combining this fast-changing counter with a potentially slower-changing counter from random_get_entropy_fallback() in the more significant bits. This commit combines the two, taking into account that the changing bits are in a different bit position depending on the CPU model. In addition, we previously were falling back to 0 for ancient CPUs that Linux does not support anyway; remove that dead path entirely.
---
ThomasB - I dropped your Ack from v4, because this is pretty different from v4 now.
Maciej - you mentioned you had a test rig. Think you could provide a "Tested-by" if this approach works?
arch/mips/include/asm/timex.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
arch/mips/include/asm/timex.h +7 -9
@@ -80,21 +80,19 @@ static inline cycles_t get_cycles(void)/* * Like get_cycles - but where c0_count is not available we desperately * use c0_random in an attempt to get at least a little bit of entropy. -* -* R6000 and R6000A neither have a count register nor a random register. -* That leaves no entropy source in the CPU itself. */ static inline unsigned long random_get_entropy (void ) { -unsigned int prid =read_c0_prid (); -unsigned int imp = prid &PRID_IMP_MASK ; +unsigned int c0_random; -if (can_use_mips_counter (prid)) +if (can_use_mips_counter (read_c0_prid ()))return read_c0_count (); -else if (likely (imp !=PRID_IMP_R6000 && imp !=PRID_IMP_R6000A )) -return read_c0_random (); + +if (cpu_has_3kex) + c0_random = (read_c0_random () >>8 ) &0x3f ;else -return 0 ;/* no usable register */ + c0_random =read_c0_random () &0x3f ; +return (random_get_entropy_fallback () <<6 ) | (0x3f - c0_random); }#define random_get_entropy random_get_entropy
Signature · 1 line
2.35.1
Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
Quoted 10 lines · Jason A. Donenfeld
For situations in which we don't have a c0 counter register available, we've been falling back to reading the c0 "random" register, which is usually bounded by the amount of TLB entries and changes every other cycle or so. This means it wraps extremely often. We can do better by combining this fast-changing counter with a potentially slower-changing counter from
random_get_entropy_fallback()in the more significant bits. This commit combines the two, taking into account that the changing bits are in a different bit position depending on the CPU model. In addition, we previously were falling back to 0 for ancient CPUs that Linux does not support anyway; remove that dead path entirely.
I've pushed the algorithm through testing with a number of suitable
systems:
- an R2000A and an R3000A with no timer of any kind, only jiffies,
- an R3400 with a chipset timer only,
- an R4400SC with a usable buggy CP0 counter and a chipset timer,
- a 5Kc with a good CP0 counter only,
with no obvious issues spotted. Thank you for working on this!
Maciej
Jason A. Donenfeld
maintainerRe: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
Hi Maciej,
Quoted 21 lines · Maciej W. Rozycki
Quoted 10 lines · Jason A. Donenfeld
For situations in which we don't have a c0 counter register available, we've been falling back to reading the c0 "random" register, which is usually bounded by the amount of TLB entries and changes every other cycle or so. This means it wraps extremely often. We can do better by combining this fast-changing counter with a potentially slower-changing counter from
random_get_entropy_fallback()in the more significant bits. This commit combines the two, taking into account that the changing bits are in a different bit position depending on the CPU model. In addition, we previously were falling back to 0 for ancient CPUs that Linux does not support anyway; remove that dead path entirely.Tested-by Maciej W. Rozycki <[email protected]>I've pushed the algorithm through testing with a number of suitable
systems:- an R2000A and an R3000A with no timer of any kind, only jiffies,
Quoted 7 lines
- an R3400 with a chipset timer only,
- an R4400SC with a usable buggy CP0 counter and a chipset timer,
- a 5Kc with a good CP0 counter only,
with no obvious issues spotted. Thank you for working on this!
Thanks for all the testing!
ThomasB - I think maybe you can re-"Acked-by" this now if you're on board with the strategy here?
Jason
Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
In reply to Jason A. DonenfeldQuoted 61 lines · Jason A. Donenfeld
For situations in which we don't have a c0 counter register available, we've been falling back to reading the c0 "random" register, which is usually bounded by the amount of TLB entries and changes every other cycle or so. This means it wraps extremely often. We can do better by combining this fast-changing counter with a potentially slower-changing counter from
random_get_entropy_fallback()in the more significant bits. This commit combines the two, taking into account that the changing bits are in a different bit position depending on the CPU model. In addition, we previously were falling back to 0 for ancient CPUs that Linux does not support anyway; remove that dead path entirely.Cc Thomas Gleixner <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Thomas Bogendoerfer <[email protected]>Cc Maciej W. Rozycki <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>---
ThomasB - I dropped your Ack from v4, because this is pretty different from v4 now.Maciej - you mentioned you had a test rig. Think you could provide a "Tested-by" if this approach works?
arch/mips/include/asm/timex.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)arch/mips/include/asm/timex.h +7
-9@@ -80,21 +80,19 @@ static inline cycles_t get_cycles(void)/* * Like get_cycles - but where c0_count is not available we desperately * use c0_random in an attempt to get at least a little bit of entropy. -* -* R6000 and R6000A neither have a count register nor a random register. -* That leaves no entropy source in the CPU itself. */ static inline unsigned long random_get_entropy (void ) { -unsigned int prid =read_c0_prid (); -unsigned int imp = prid &PRID_IMP_MASK ; +unsigned int c0_random; -if (can_use_mips_counter (prid)) +if (can_use_mips_counter (read_c0_prid ()))return read_c0_count (); -else if (likely (imp !=PRID_IMP_R6000 && imp !=PRID_IMP_R6000A )) -return read_c0_random (); + +if (cpu_has_3kex) + c0_random = (read_c0_random () >>8 ) &0x3f ;else -return 0 ;/* no usable register */ + c0_random =read_c0_random () &0x3f ; +return (random_get_entropy_fallback () <<6 ) | (0x3f - c0_random); }#define random_get_entropy random_get_entropySignature · 1 line
2.35.1
Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]
Jason A. Donenfeld
maintainerPATCH v5 5/11 arm: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
--- arch/arm/include/asm/timex.h | 1 + 1 file changed, 1 insertion(+)
arch/arm/include/asm/timex.h +1 -0
@@ -11,5 +11,6 @@typedef unsigned long cycles_t ;#define get_cycles () ({ cycles_t c; read_current_timer(&c) ? 0 : c; }) +#define random_get_entropy () (((unsigned long)get_cycles()) ?: random_get_entropy_fallback())#endif
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 6/11 nios2: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
--- arch/nios2/include/asm/timex.h | 2 ++ 1 file changed, 2 insertions(+)
arch/nios2/include/asm/timex.h +2 -0
@@ -9,4 +9,6 @@ typedef unsigned long cycles_t;extern cycles_t get_cycles (void ); +#define random_get_entropy () (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) +#endif
Signature · 1 line
2.35.1
Re: [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
Quoted 22 lines · Jason A. Donenfeld
In the event that
random_get_entropy()can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least callingrandom_get_entropy_fallback()would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as thoughrandom_get_entropy_fallback()is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.Cc Thomas Gleixner <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Dinh Nguyen <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/nios2/include/asm/timex.h | 2 ++ 1 file changed, 2 insertions(+)arch/nios2/include/asm/timex.h +0
-0
arch/nios2/include/asm/timex.h typedef unsigned long cycles_t; · line 14
Quoted 6 lines
arch/nios2/include/asm/timex.h +2
-0@@ -9,4 +9,6 @@ typedef unsigned long cycles_t;extern cycles_t get_cycles (void ); +#define random_get_entropy () (((unsigned long)get_cycles()) ?: random_get_entropy_fallback()) +#endif
Jason A. Donenfeld
maintainerPATCH v5 7/11 x86: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
If CONFIG_X86_TSC=n, then it's possible that we're running on a 486 with no RDTSC, so we only need the fallback code for that case.
--- arch/x86/include/asm/timex.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/include/asm/timex.h +10 -0
@@ -5,6 +5,16 @@#include <asm/processor.h> #include <asm/tsc.h> +static inline unsigned long random_get_entropy (void ) +{ +#ifndef CONFIG_X86_TSC +if (!boot_cpu_has (X86_FEATURE_TSC )) +return random_get_entropy_fallback (); +#endif +return rdtsc (); +} +#define random_get_entropy random_get_entropy +/* Assume we use the PIT time source for the clock tick */ #define CLOCK_TICK_RATE PIT_TICK_RATE
Signature · 1 line
2.35.1
Borislav Petkov
maintainerRe: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
Quoted 25 lines · Jason A. Donenfeld
In the event that
random_get_entropy()can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least callingrandom_get_entropy_fallback()would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as thoughrandom_get_entropy_fallback()is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.If
CONFIG_X86_TSC=n, then it's possible that we're running on a 486 with no RDTSC, so we only need the fallback code for that case.Cc Thomas Gleixner <[email protected]>Cc Arnd Bergmann <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/x86/include/asm/timex.h | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/include/asm/timex.h +0
-0
arch/x86/include/asm/timex.h line 11
Quoted 7 lines
arch/x86/include/asm/timex.h +4
-0@@ -5,6 +5,16 @@#include <asm/processor.h> #include <asm/tsc.h> +static inline unsigned long random_get_entropy (void ) +{ +#ifndef CONFIG_X86_TSC +if (!boot_cpu_has (X86_FEATURE_TSC ))
cpu_feature_enabled() pls.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJason A. Donenfeld
maintainerRe: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
Hi Borislav,
Quoted 6 lines · Borislav Petkov
arch/x86/include/asm/timex.h line 11
Quoted 4 lines
arch/x86/include/asm/timex.h +4
-0+static inline unsigned long random_get_entropy (void ) +{ +#ifndef CONFIG_X86_TSC +if (!boot_cpu_has (X86_FEATURE_TSC ))
cpu_feature_enabled()pls.
This function began as a carbon copy of get_cycles(), which reads:
static inline cycles_t get_cycles(void)
{
#ifndef CONFIG_X86_TSC
if (!boot_cpu_has(X86_FEATURE_TSC))
return 0;#endif
return rdtsc(); }
As you see, random_get_entropy() is the same function, but with that zero replaced with the fallback. (Using the fallback in get_cycles() wouldn't be appropriate.)
So, your options are:
a) We keep this patch as-is, using boot_cpu_has(); or
b) I make an unrelated change inside of this same patch to change get_cycles() to use cpu_feature_enabled() (in addition to the new random_get_entropy()).
I think I prefer doing (a), and leaving (b) for another time when you or another x86 maintainer can do so. But I'll do whichever you say. Which would you like?
Jason
Borislav Petkov
maintainerRe: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
Quoted 3 lines · Jason A. Donenfeld
I think I prefer doing (a), and leaving (b) for another time when you or another x86 maintainer can do so. But I'll do whichever you say. Which would you like?
We are switching all feature checks to cpu_feature_enabled() so you might as well do the new preferred way of checking when adding a new function and so that we have one less place to touch. Which is appreciated. :)
Thx!
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJason A. Donenfeld
maintainerRe: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
Hi Borislav,
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Jason A. Donenfeld
I think I prefer doing (a), and leaving (b) for another time when you or another x86 maintainer can do so. But I'll do whichever you say. Which would you like?
We are switching all feature checks to
cpu_feature_enabled()so you might as well do the new preferred way of checking when adding a new function and so that we have one less place to touch. Which is appreciated. :)
Okay, no problem. I'll make the change there and get_cycles() for v+1 of the patchset.
Jason
Jason A. Donenfeld
maintainerPATCH v5 8/11 um: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
--- arch/um/include/asm/timex.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
arch/um/include/asm/timex.h +2 -7
@@ -2,13 +2,8 @@#ifndef __UM_TIMEX_H#define __UM_TIMEX_H -typedef unsigned long cycles_t ; - -static inline cycles_t get_cycles (void ) -{ -return 0 ; -} -#define CLOCK_TICK_RATE (HZ) +#include <asm-generic/timex.h> +#endif
Signature · 1 line
2.35.1
Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
Quoted 9 lines · Jason A. Donenfeld
In the event that
random_get_entropy()can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least callingrandom_get_entropy_fallback()would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as thoughrandom_get_entropy_fallback()is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
Quoted 3 lines
This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
LGTM, actually better than before, though not even sure any drivers in ARCH=um have interrupts that say they can be used for this.
I assume you're going to take this through the random tree?
johannes
Jason A. Donenfeld
maintainerRe: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
Hi Johannes,
Quoted 15 lines · Johannes Berg
Quoted 9 lines · Jason A. Donenfeld
In the event that
random_get_entropy()can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least callingrandom_get_entropy_fallback()would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as thoughrandom_get_entropy_fallback()is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.Quoted 3 lines
This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
Quoted 6 lines
LGTM, actually better than before, though not even sure any drivers in ARCH=um have interrupts that say they can be used for this.
Acked-by Johannes Berg <[email protected]>I assume you're going to take this through the random tree?
Right, that's the plan (as mentioned in the cover letter). Changes to random.c will depend on these patches being there.
Jason
Jason A. Donenfeld
maintainerPATCH v5 9/11 sparc: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
--- arch/sparc/include/asm/timex_32.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/sparc/include/asm/timex_32.h +1 -3
@@ -9,8 +9,6 @@#define CLOCK_TICK_RATE 1193180/* Underlying HZ */ -/* XXX Maybe do something better at some point... -DaveM */ -typedef unsigned long cycles_t ; -#define get_cycles () (0) +#include <asm-generic/timex.h> #endif
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerPATCH v5 10/11 xtensa: use fallback for random_get_entropy() instead of zero
In reply to Jason A. DonenfeldIn the event that random_get_entropy() can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least calling random_get_entropy_fallback() would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as though random_get_entropy_fallback() is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.
This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
--- arch/xtensa/include/asm/timex.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/xtensa/include/asm/timex.h +2 -4
@@ -29,10 +29,6 @@extern unsigned long ccount_freq; -typedef unsigned long long cycles_t ; - -#define get_cycles () (0) -void local_timer_setup (unsigned cpu);/*
@@ -59,4 +55,6 @@ static inline void set_linux_timer (unsigned long ccompare) xtensa_set_sr(ccompare, SREG_CCOMPARE + LINUX_TIMER); } +#include <asm-generic/timex.h> +#endif /* _XTENSA_TIMEX_H */
Signature · 1 line
2.35.1
Re: [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
Quoted 14 lines · Jason A. Donenfeld
In the event that
random_get_entropy()can't access a cycle counter or similar, falling back to returning 0 is really not the best we can do. Instead, at least callingrandom_get_entropy_fallback()would be preferable, because that always needs to return _something_, even falling back to jiffies eventually. It's not as thoughrandom_get_entropy_fallback()is super high precision or guaranteed to be entropic, but basically anything that's not zero all the time is better than returning zero all the time.This is accomplished by just including the asm-generic code like on other architectures, which means we can get rid of the empty stub function here.
Quoted 7 lines
Cc Thomas Gleixner <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Max Filippov <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>--- arch/xtensa/include/asm/timex.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
Signature · 2 lines
Thanks. -- Max
Jason A. Donenfeld
maintainerPATCH v5 11/11 random: insist on random_get_entropy() existing in order to simplify
In reply to Jason A. DonenfeldAll platforms are now guaranteed to provide some value for random_get_entropy(). In case some bug leads to this not being so, we print a warning, because that indicates that something is really very wrong (and likely other things are impacted too). This should never be hit, but it's a good and cheap way of finding out if something ever is problematic.
Since we now have viable fallback code for random_get_entropy() on all platforms, which is, in the worst case, not worse than jiffies, we can count on getting the best possible value out of it. That means there's no longer a use for using jiffies as entropy input. It also means we no longer have a reason for doing the round-robin register flow in the IRQ handler, which was always of fairly dubious value.
Instead we can greatly simplify the IRQ handler inputs and also unify the construction between 64-bits and 32-bits. We now collect the cycle counter and the return address, since those are the two things that matter. Because the return address and the irq number are likely related, to the extent we mix in the irq number, we can just xor it into the top unchanging bytes of the return address, rather than the bottom changing bytes of the cycle counter as before. Then, we can do a fixed 2 rounds of SipHash/HSipHash. Finally, we use the same construction of hashing only half of the [H]SipHash state on 32-bit and 64-bit. We're not actually discarding any entropy, since that entropy is carried through until the next time. And more importantly, it lets us do the same sponge-like construction everywhere.
--- drivers/char/random.c | 89 ++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 60 deletions(-)
drivers/char/random.c +29 -60
@@ -1012,6 +1012,9 @@ int __init rand_initialize(void) urandom_warning.interval =0 ; unseeded_warning.interval =0 ; } + +WARN (!random_get_entropy (),"Missing cycle counter and fallback timer; RNG " +"entropy collection will consequently suffer." );return 0 ; }
@@ -1025,15 +1028,14 @@ int __init rand_initialize(void)*/ void add_device_randomness (const void *buf,size_t size) { -unsigned long cycles =random_get_entropy (); -unsigned long flags, now = jiffies; +unsigned long entropy =random_get_entropy (); +unsigned long flags;if (crng_init ==0 && size)crng_pre_init_inject (buf, size, false);spin_lock_irqsave (&input_pool.lock, flags); -_mix_pool_bytes (&cycles,sizeof (cycles)); -_mix_pool_bytes (&now,sizeof (now)); +_mix_pool_bytes (&entropy,sizeof (entropy));_mix_pool_bytes (buf, size);spin_unlock_irqrestore (&input_pool.lock, flags); }
@@ -1056,12 +1058,11 @@ struct timer_rand_state {*/ static void add_timer_randomness (struct timer_rand_state *state,unsigned int num) { -unsigned long cycles =random_get_entropy (), now = jiffies, flags; +unsigned long entropy =random_get_entropy (), now = jiffies, flags;long delta, delta2, delta3;spin_lock_irqsave (&input_pool.lock, flags); -_mix_pool_bytes (&cycles,sizeof (cycles)); -_mix_pool_bytes (&now,sizeof (now)); +_mix_pool_bytes (&entropy,sizeof (entropy));_mix_pool_bytes (&num,sizeof (num));spin_unlock_irqrestore (&input_pool.lock, flags);
@@ -1223,7 +1224,6 @@ struct fast_pool {unsigned long pool[4 ];unsigned long last;unsigned int count; -u16 reg_idx; }; static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = {
@@ -1241,13 +1241,13 @@ static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = {* This is [Half]SipHash-1-x, starting from an empty key. Because * the key is fixed, it assumes that its inputs are non-malicious, * and therefore this has no security on its own. s represents the -* 128 or 256-bit SipHash state, while v represents a 128-bit input. +* four-word SipHash state, while v represents a two-word input. */ -static void fast_mix (unsigned long s[4 ],const unsigned long *v) +static void fast_mix (unsigned long s[4 ],const unsigned long v[2 ]) {size_t i; -for (i =0 ; i <16 /sizeof (long ); ++i) { +for (i =0 ; i <2 ; ++i) { s[3 ] ^= v[i];#ifdef CONFIG_64BIT s[0 ] += s[1 ]; s[1 ] =rol64 (s[1 ],13 ); s[1 ] ^= s[0 ]; s[0 ] =rol64 (s[0 ],32 );
@@ -1287,33 +1287,17 @@ int random_online_cpu(unsigned int cpu) }#endif -static unsigned long get_reg (struct fast_pool *f,struct pt_regs *regs) -{ -unsigned long *ptr = (unsigned long *)regs; -unsigned int idx; - -if (regs ==NULL ) -return 0 ; - idx =READ_ONCE (f->reg_idx); -if (idx >=sizeof (struct pt_regs ) /sizeof (unsigned long )) - idx =0 ; - ptr += idx++; -WRITE_ONCE (f->reg_idx, idx); -return *ptr; -} -static void mix_interrupt_randomness (struct work_struct *work) { struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix);/* -* The size of the copied stack pool is explicitly 16 bytes so that we -* tax mix_pool_byte()'s compression function the same amount on all -* platforms. This means on 64-bit we copy half the pool into this, -* while on 32-bit we copy all of it. The entropy is supposed to be -* sufficiently dispersed between bits that in the sponge-like -* half case, on average we don't wind up "losing" some. +* The size of the copied stack pool is explicitly 2 longs so that we +* only ever ingest half of the siphash output each time, retaining +* the other half as the next "key" that carries over. The entropy is +* supposed to be sufficiently dispersed between bits so on average +* we don't wind up "losing" some. */ -u8 pool[16 ]; +unsigned long pool[2 ];/* Check to see if we're running on the wrong CPU due to hotplug. */ local_irq_disable ();
@@ -1345,36 +1329,21 @@ static void mix_interrupt_randomness(struct work_struct *work)void add_interrupt_randomness (int irq) {enum {MIX_INFLIGHT =1U <<31 }; -unsigned long cycles =random_get_entropy (), now = jiffies; +unsigned long cycles =random_get_entropy ();struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);struct pt_regs *regs =get_irq_regs ();unsigned int new_count; -union { -u32 u32[4 ]; -u64 u64[2 ]; -unsigned long longs[16 /sizeof (long )]; - } irq_data; - -if (cycles ==0 ) - cycles =get_reg (fast_pool, regs); - -if (sizeof (unsigned long ) ==8 ) { - irq_data.u64[0 ] = cycles ^rol64 (now,32 ) ^ irq; - irq_data.u64[1 ] = regs ?instruction_pointer (regs) : _RET_IP_; - }else { - irq_data.u32[0 ] = cycles ^ irq; - irq_data.u32[1 ] = now; - irq_data.u32[2 ] = regs ?instruction_pointer (regs) : _RET_IP_; - irq_data.u32[3 ] =get_reg (fast_pool, regs); - } -fast_mix (fast_pool->pool, irq_data.longs); +fast_mix (fast_pool->pool, (unsigned long [2 ]){ + (regs ?instruction_pointer (regs) : _RET_IP_) ^swab (irq), + cycles + }); new_count = ++fast_pool->count;if (new_count &MIX_INFLIGHT )return ; -if (new_count <64 && (!time_after (now, fast_pool->last +HZ ) || +if (new_count <64 && (!time_is_before_jiffies (fast_pool->last +HZ ) ||unlikely (crng_init ==0 )))return ;
@@ -1410,28 +1379,28 @@ static void entropy_timer(struct timer_list *t)static void try_to_generate_entropy (void ) {struct { -unsigned long cycles; +unsigned long entropy;struct timer_list timer; } stack; - stack.cycles =random_get_entropy (); + stack.entropy =random_get_entropy ();/* Slow counter - or none. Don't even bother */ -if (stack.cycles ==random_get_entropy ()) +if (stack.entropy ==random_get_entropy ())return ;timer_setup_on_stack (&stack.timer, entropy_timer,0 );while (!crng_ready () && !signal_pending (current)) {if (!timer_pending (&stack.timer))mod_timer (&stack.timer, jiffies +1 ); -mix_pool_bytes (&stack.cycles,sizeof (stack.cycles)); +mix_pool_bytes (&stack.entropy,sizeof (stack.entropy));schedule (); - stack.cycles =random_get_entropy (); + stack.entropy =random_get_entropy (); }del_timer_sync (&stack.timer);destroy_timer_on_stack (&stack.timer); -mix_pool_bytes (&stack.cycles,sizeof (stack.cycles)); +mix_pool_bytes (&stack.entropy,sizeof (stack.entropy)); }
Signature · 1 line
2.35.1
Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
Quoted 4 lines · Jason A. Donenfeld
All platforms are now guaranteed to provide some value for
random_get_entropy(). In case some bug leads to this not being so, we print a warning, ...
Would it make sense to test at compile time? If there is no hardware RNG nor a cycle counter, then the kernel should be compiled with the gcc latent entropy plugin. Generate a warning suggesting that, or even an error insisting on it.
Jason A. Donenfeld
maintainerRe: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
Hi Sandy,
Quoted 10 lines · Sandy Harris
Quoted 4 lines · Jason A. Donenfeld
All platforms are now guaranteed to provide some value for
random_get_entropy(). In case some bug leads to this not being so, we print a warning, ...Would it make sense to test at compile time? If there is no hardware RNG nor a cycle counter, then the kernel should be compiled with the gcc latent entropy plugin. Generate a warning suggesting that, or even an error insisting on it.
Unfortunately, as a last ditch warning safeguard against bugs, I don't think that's something we can determine at build time. A lot of this machinery is dynamic. Fortunately a single check at init time brings with it zero appreciable overhead.
Jason