* [PATCH v2 01/11] timekeeping: add accessor for raw clock
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 02/11] timekeeping: use ktime_read_raw_clock() for random_get_entropy() if no get_cycles() Jason A. Donenfeld
` (9 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
This 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.
It's defined in linux/timex.h rather than linux/timekeeping.h, because
the latter cannot be included easily from asm/ headers, and generally
shouldn't be used outside of this rather narrow purpose. It's a ktime
function, but it's not the usual ktime API.
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/linux/timex.h | 2 ++
kernel/time/timekeeping.c | 8 ++++++++
2 files changed, 10 insertions(+)
diff --git a/include/linux/timex.h b/include/linux/timex.h
index 5745c90c8800..56502b338287 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -62,6 +62,8 @@
#include <linux/types.h>
#include <linux/param.h>
+extern u64 ktime_read_raw_clock(void);
+
#include <asm/timex.h>
#ifndef random_get_entropy
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index dcdcb85121e4..0d04d2e8b94b 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -939,6 +939,14 @@ ktime_t ktime_get_raw(void)
}
EXPORT_SYMBOL_GPL(ktime_get_raw);
+/**
+ * ktime_read_raw_clock - Returns the raw clock source value
+ */
+u64 ktime_read_raw_clock(void)
+{
+ return tk_clock_read(&tk_core.timekeeper.tkr_mono);
+}
+
/**
* ktime_get_ts64 - get the monotonic clock in timespec64 format
* @ts: pointer to timespec variable
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 02/11] timekeeping: use ktime_read_raw_clock() for random_get_entropy() if no get_cycles()
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 01/11] timekeeping: add accessor for raw clock Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero Jason A. Donenfeld
` (8 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 ktime_read_raw_clock()
would be preferable, because that always needs to return _something_,
even falling back to jiffies eventually. It's not as though
ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/linux/timex.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/include/linux/timex.h b/include/linux/timex.h
index 56502b338287..4050f68c34cb 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -76,8 +76,14 @@ extern u64 ktime_read_raw_clock(void);
*
* 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 sched_clock().
*/
+#ifdef get_cycles
#define random_get_entropy() ((unsigned long)get_cycles())
+#else
+#define random_get_entropy() ((unsigned long)ktime_read_raw_clock())
+#endif
#endif
/*
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 01/11] timekeeping: add accessor for raw clock Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 02/11] timekeeping: use ktime_read_raw_clock() for random_get_entropy() if no get_cycles() Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-11 8:18 ` Thomas Gleixner
2022-04-10 21:49 ` [PATCH v2 04/11] riscv: " Jason A. Donenfeld
` (7 subsequent siblings)
10 siblings, 1 reply; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/m68k/include/asm/timex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/m68k/include/asm/timex.h b/arch/m68k/include/asm/timex.h
index 6a21d9358280..5351b10e1b18 100644
--- a/arch/m68k/include/asm/timex.h
+++ b/arch/m68k/include/asm/timex.h
@@ -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 ktime_read_raw_clock();
}
#define random_get_entropy random_get_entropy
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 ` [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero Jason A. Donenfeld
@ 2022-04-11 8:18 ` Thomas Gleixner
2022-04-11 22:07 ` Jason A. Donenfeld
0 siblings, 1 reply; 14+ messages in thread
From: Thomas Gleixner @ 2022-04-11 8:18 UTC (permalink / raw)
To: Jason A. Donenfeld, linux-kernel, linux-crypto, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
On Sun, Apr 10 2022 at 23:49, Jason A. Donenfeld wrote:
> 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 calling ktime_read_raw_clock() would be preferable,
> because that always needs to return _something_, even falling back to
> jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> arch/m68k/include/asm/timex.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/m68k/include/asm/timex.h b/arch/m68k/include/asm/timex.h
> index 6a21d9358280..5351b10e1b18 100644
> --- a/arch/m68k/include/asm/timex.h
> +++ b/arch/m68k/include/asm/timex.h
> @@ -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 ktime_read_raw_clock();
I'd rather do something like this in a common header:
unsigned long random_get_entropy_fallback(void);
and use random_get_entropy_fallback() in the architecture specific
files.
That way you can encapsulate the fallback implementation in the random
code and if it turns out that ktime_read_raw_clock() is a stupid idea or
someone has a better idea then you have to change exactly one place and
not patch the whole tree again.
Thanks,
tglx
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-11 8:18 ` Thomas Gleixner
@ 2022-04-11 22:07 ` Jason A. Donenfeld
0 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-11 22:07 UTC (permalink / raw)
To: Thomas Gleixner
Cc: LKML, Linux Crypto Mailing List, Arnd Bergmann, Theodore Ts'o,
Dominik Brodowski, Russell King, Catalin Marinas, Will Deacon,
Geert Uytterhoeven, Thomas Bogendoerfer, Paul Walmsley,
Palmer Dabbelt, Albert Ou, David S . Miller, Richard Weinberger,
Anton Ivanov, Johannes Berg, Ingo Molnar, Borislav Petkov,
Dave Hansen, H . Peter Anvin, Chris Zankel, Max Filippov,
John Stultz, Stephen Boyd, Dinh Nguyen, linux-arm-kernel,
linux-m68k, open list:BROADCOM NVRAM DRIVER, linux-riscv,
sparclinux, linux-um, X86 ML, linux-xtensa
Hi Thomas,
On Mon, Apr 11, 2022 at 10:18 AM Thomas Gleixner <tglx@linutronix.de> wrote:
> > diff --git a/arch/m68k/include/asm/timex.h b/arch/m68k/include/asm/timex.h
> > index 6a21d9358280..5351b10e1b18 100644
> > --- a/arch/m68k/include/asm/timex.h
> > +++ b/arch/m68k/include/asm/timex.h
> > @@ -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 ktime_read_raw_clock();
>
> I'd rather do something like this in a common header:
>
> unsigned long random_get_entropy_fallback(void);
>
> and use random_get_entropy_fallback() in the architecture specific
> files.
>
> That way you can encapsulate the fallback implementation in the random
> code and if it turns out that ktime_read_raw_clock() is a stupid idea or
> someone has a better idea then you have to change exactly one place and
> not patch the whole tree again.
Absolutely. That's a good idea. I'll do that for v3.
Jason
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v2 04/11] riscv: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (2 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 05/11] mips: " Jason A. Donenfeld
` (6 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/riscv/include/asm/timex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h
index 507cae273bc6..b320afede88a 100644
--- a/arch/riscv/include/asm/timex.h
+++ b/arch/riscv/include/asm/timex.h
@@ -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 ktime_read_raw_clock();
return get_cycles();
}
#define random_get_entropy() random_get_entropy()
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 05/11] mips: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (3 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 04/11] riscv: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 06/11] arm: " Jason A. Donenfeld
` (5 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/mips/include/asm/timex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
index b05bb70a2e46..fa6a5ca20b46 100644
--- a/arch/mips/include/asm/timex.h
+++ b/arch/mips/include/asm/timex.h
@@ -94,7 +94,7 @@ static inline unsigned long random_get_entropy(void)
else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A))
return read_c0_random();
else
- return 0; /* no usable register */
+ return ktime_read_raw_clock(); /* no usable register */
}
#define random_get_entropy random_get_entropy
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 06/11] arm: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (4 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 05/11] mips: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 07/11] nios2: " Jason A. Donenfeld
` (4 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Russell King <linux@armlinux.org.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/arm/include/asm/timex.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/arm/include/asm/timex.h b/arch/arm/include/asm/timex.h
index 7c3b3671d6c2..d0b32ce87254 100644
--- a/arch/arm/include/asm/timex.h
+++ b/arch/arm/include/asm/timex.h
@@ -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() ?: ktime_read_raw_clock()))
#endif
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 07/11] nios2: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (5 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 06/11] arm: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 08/11] x86: " Jason A. Donenfeld
` (3 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/nios2/include/asm/timex.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/arch/nios2/include/asm/timex.h b/arch/nios2/include/asm/timex.h
index a769f871b28d..3c74d3e337a0 100644
--- a/arch/nios2/include/asm/timex.h
+++ b/arch/nios2/include/asm/timex.h
@@ -9,4 +9,6 @@ typedef unsigned long cycles_t;
extern cycles_t get_cycles(void);
+#define random_get_entropy() ((unsigned long)(get_cycles() ?: ktime_read_raw_clock()))
+
#endif
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 08/11] x86: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (6 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 07/11] nios2: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 09/11] um: " Jason A. Donenfeld
` (2 subsequent siblings)
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: x86@kernel.org
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/x86/include/asm/tsc.h | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h
index 01a300a9700b..dad3027bf6a2 100644
--- a/arch/x86/include/asm/tsc.h
+++ b/arch/x86/include/asm/tsc.h
@@ -28,6 +28,16 @@ static inline cycles_t get_cycles(void)
return rdtsc();
}
+static inline unsigned long random_get_entropy(void)
+{
+#ifndef CONFIG_X86_TSC
+ if (!boot_cpu_has(X86_FEATURE_TSC))
+ return ktime_read_raw_clock();
+#endif
+ return rdtsc();
+}
+#define random_get_entropy random_get_entropy
+
extern struct system_counterval_t convert_art_to_tsc(u64 art);
extern struct system_counterval_t convert_art_ns_to_tsc(u64 art_ns);
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 09/11] um: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (7 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 08/11] x86: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 10/11] sparc: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 11/11] xtensa: " Jason A. Donenfeld
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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.
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Richard Weinberger <richard@nod.at>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/um/include/asm/timex.h | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/arch/um/include/asm/timex.h b/arch/um/include/asm/timex.h
index e392a9a5bc9b..9f27176adb26 100644
--- a/arch/um/include/asm/timex.h
+++ b/arch/um/include/asm/timex.h
@@ -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
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 10/11] sparc: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (8 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 09/11] um: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 11/11] xtensa: " Jason A. Donenfeld
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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.
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/sparc/include/asm/timex_32.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/arch/sparc/include/asm/timex_32.h b/arch/sparc/include/asm/timex_32.h
index 542915b46209..f86326a6f89e 100644
--- a/arch/sparc/include/asm/timex_32.h
+++ b/arch/sparc/include/asm/timex_32.h
@@ -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
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v2 11/11] xtensa: use ktime_read_raw_clock() for random_get_entropy() instead of zero
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
` (9 preceding siblings ...)
2022-04-10 21:49 ` [PATCH v2 10/11] sparc: " Jason A. Donenfeld
@ 2022-04-10 21:49 ` Jason A. Donenfeld
10 siblings, 0 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
To: linux-kernel, linux-crypto, tglx, arnd
Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
linux-riscv, sparclinux, linux-um, x86, linux-xtensa
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 calling ktime_read_raw_clock() would be preferable,
because that always needs to return _something_, even falling back to
jiffies eventually. It's not as though ktime_read_raw_clock() 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.
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/xtensa/include/asm/timex.h | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/arch/xtensa/include/asm/timex.h b/arch/xtensa/include/asm/timex.h
index 233ec75e60c6..3f2462f2d027 100644
--- a/arch/xtensa/include/asm/timex.h
+++ b/arch/xtensa/include/asm/timex.h
@@ -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 */
--
2.35.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 14+ messages in thread