All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 00/11] archs/random: fallback to best raw ktime when no cycle counter
@ 2022-04-19 11:16 ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: David S. Miller <davem@davemloft.net>
Cc: Richard Weinberger <richard@nod.at>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Chris Zankel <chris@zankel.net>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Stephen Boyd <sboyd@kernel.org>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-mips@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: sparclinux@vger.kernel.org
Cc: linux-um@lists.infradead.org
Cc: x86@kernel.org
Cc: linux-xtensa@linux-xtensa.org

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(-)

-- 
2.35.1


^ permalink raw reply	[flat|nested] 75+ messages in thread

* [PATCH v5 00/11] archs/random: fallback to best raw ktime when no cycle counter
@ 2022-04-19 11:16 ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: David S. Miller <davem@davemloft.net>
Cc: Richard Weinberger <richard@nod.at>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Chris Zankel <chris@zankel.net>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Stephen Boyd <sboyd@kernel.org>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-mips@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: sparclinux@vger.kernel.org
Cc: linux-um@lists.infradead.org
Cc: x86@kernel.org
Cc: linux-xtensa@linux-xtensa.org

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(-)

-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* [PATCH v5 00/11] archs/random: fallback to best raw ktime when no cycle counter
@ 2022-04-19 11:16 ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: David S. Miller <davem@davemloft.net>
Cc: Richard Weinberger <richard@nod.at>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Chris Zankel <chris@zankel.net>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Stephen Boyd <sboyd@kernel.org>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-mips@vger.kernel.org
Cc: linux-riscv@lists.infradead.org
Cc: sparclinux@vger.kernel.org
Cc: linux-um@lists.infradead.org
Cc: x86@kernel.org
Cc: linux-xtensa@linux-xtensa.org

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(-)

-- 
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	[flat|nested] 75+ messages in thread

* [PATCH v5 01/11] timekeeping: add raw clock fallback for random_get_entropy()
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

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     |  8 ++++++++
 kernel/time/timekeeping.c | 10 ++++++++++
 2 files changed, 18 insertions(+)

diff --git a/include/linux/timex.h b/include/linux/timex.h
index 5745c90c8800..3871b06bd302 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -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
 
 /*
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index dcdcb85121e4..7cd2ec239cae 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 01/11] timekeeping: add raw clock fallback for random_get_entropy()
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

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     |  8 ++++++++
 kernel/time/timekeeping.c | 10 ++++++++++
 2 files changed, 18 insertions(+)

diff --git a/include/linux/timex.h b/include/linux/timex.h
index 5745c90c8800..3871b06bd302 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -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
 
 /*
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index dcdcb85121e4..7cd2ec239cae 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 01/11] timekeeping: add raw clock fallback for random_get_entropy()
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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.

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     |  8 ++++++++
 kernel/time/timekeeping.c | 10 ++++++++++
 2 files changed, 18 insertions(+)

diff --git a/include/linux/timex.h b/include/linux/timex.h
index 5745c90c8800..3871b06bd302 100644
--- a/include/linux/timex.h
+++ b/include/linux/timex.h
@@ -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
 
 /*
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index dcdcb85121e4..7cd2ec239cae 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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
-- 
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] 75+ messages in thread

* [PATCH v5 02/11] m68k: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Acked-by: 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..f4a7a340f4ca 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 random_get_entropy_fallback();
 }
 #define random_get_entropy	random_get_entropy
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 02/11] m68k: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Acked-by: 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..f4a7a340f4ca 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 random_get_entropy_fallback();
 }
 #define random_get_entropy	random_get_entropy
 
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 02/11] m68k: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Acked-by: 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..f4a7a340f4ca 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 random_get_entropy_fallback();
 }
 #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] 75+ messages in thread

* [PATCH v5 03/11] riscv: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d6a7428f6248 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 random_get_entropy_fallback();
 	return get_cycles();
 }
 #define random_get_entropy()	random_get_entropy()
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 03/11] riscv: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d6a7428f6248 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 random_get_entropy_fallback();
 	return get_cycles();
 }
 #define random_get_entropy()	random_get_entropy()
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 03/11] riscv: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d6a7428f6248 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 random_get_entropy_fallback();
 	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] 75+ messages in thread

* [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Maciej W. Rozycki <macro@orcam.me.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
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(-)

diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
index b05bb70a2e46..e3f5460a923b 100644
--- a/arch/mips/include/asm/timex.h
+++ b/arch/mips/include/asm/timex.h
@@ -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
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Maciej W. Rozycki <macro@orcam.me.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
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(-)

diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
index b05bb70a2e46..e3f5460a923b 100644
--- a/arch/mips/include/asm/timex.h
+++ b/arch/mips/include/asm/timex.h
@@ -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
 
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Maciej W. Rozycki <macro@orcam.me.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
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(-)

diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
index b05bb70a2e46..e3f5460a923b 100644
--- a/arch/mips/include/asm/timex.h
+++ b/arch/mips/include/asm/timex.h
@@ -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
 
-- 
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] 75+ messages in thread

* [PATCH v5 05/11] arm: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..6d1337c169cd 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()) ?: random_get_entropy_fallback())
 
 #endif
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 05/11] arm: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..6d1337c169cd 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()) ?: random_get_entropy_fallback())
 
 #endif
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 05/11] arm: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..6d1337c169cd 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()) ?: random_get_entropy_fallback())
 
 #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] 75+ messages in thread

* [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
+
 #endif
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
+
 #endif
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
+
 #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] 75+ messages in thread

* [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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/timex.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
index a4a8b1b16c0c..fac180359693 100644
--- a/arch/x86/include/asm/timex.h
+++ b/arch/x86/include/asm/timex.h
@@ -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
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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/timex.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
index a4a8b1b16c0c..fac180359693 100644
--- a/arch/x86/include/asm/timex.h
+++ b/arch/x86/include/asm/timex.h
@@ -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
 
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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/timex.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
index a4a8b1b16c0c..fac180359693 100644
--- a/arch/x86/include/asm/timex.h
+++ b/arch/x86/include/asm/timex.h
@@ -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
 
-- 
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] 75+ messages in thread

* [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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] 75+ messages in thread

* [PATCH v5 09/11] sparc: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 09/11] sparc: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 09/11] sparc: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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] 75+ messages in thread

* [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, 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 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.

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] 75+ messages in thread

* [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
  2022-04-19 11:16 ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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, 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.

Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 89 ++++++++++++++-----------------------------
 1 file changed, 29 insertions(+), 60 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 4c9adb4f3d5d..bf89c6f27a19 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -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));
 }
 
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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, 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.

Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 89 ++++++++++++++-----------------------------
 1 file changed, 29 insertions(+), 60 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 4c9adb4f3d5d..bf89c6f27a19 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -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));
 }
 
 
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 75+ messages in thread

* [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-19 11:16   ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:16 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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, 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.

Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 89 ++++++++++++++-----------------------------
 1 file changed, 29 insertions(+), 60 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 4c9adb4f3d5d..bf89c6f27a19 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -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));
 }
 
 
-- 
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] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-19 11:33     ` Johannes Berg
  -1 siblings, 0 replies; 75+ messages in thread
From: Johannes Berg @ 2022-04-19 11:33 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H . Peter Anvin, Chris Zankel,
	Max Filippov, Stephen Boyd, Dinh Nguyen, linux-arm-kernel,
	linux-m68k, linux-mips, linux-riscv, sparclinux, linux-um, x86,
	linux-xtensa

On Tue, 2022-04-19 at 13:16 +0200, 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 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.


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 <johannes@sipsolutions.net>

I assume you're going to take this through the random tree?

johannes

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:33     ` Johannes Berg
  0 siblings, 0 replies; 75+ messages in thread
From: Johannes Berg @ 2022-04-19 11:33 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H . Peter Anvin, Chris Zankel,
	Max Filippov, Stephen Boyd, Dinh Nguyen, linux-arm-kernel,
	linux-m68k, linux-mips, linux-riscv, sparclinux, linux-um, x86,
	linux-xtensa

On Tue, 2022-04-19 at 13:16 +0200, 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 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.


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 <johannes@sipsolutions.net>

I assume you're going to take this through the random tree?

johannes

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:33     ` Johannes Berg
  0 siblings, 0 replies; 75+ messages in thread
From: Johannes Berg @ 2022-04-19 11:33 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H . Peter Anvin, Chris Zankel,
	Max Filippov, Stephen Boyd, Dinh Nguyen, linux-arm-kernel,
	linux-m68k, linux-mips, linux-riscv, sparclinux, linux-um, x86,
	linux-xtensa

On Tue, 2022-04-19 at 13:16 +0200, 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 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.


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 <johannes@sipsolutions.net>

I assume you're going to take this through the random tree?

johannes

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:33     ` Johannes Berg
  (?)
@ 2022-04-19 11:37       ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:37 UTC (permalink / raw)
  To: Johannes Berg
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Johannes,

On Tue, Apr 19, 2022 at 01:33:21PM +0200, Johannes Berg wrote:
> On Tue, 2022-04-19 at 13:16 +0200, 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 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.
> 
> 
> 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 <johannes@sipsolutions.net>
> 
> 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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:37       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:37 UTC (permalink / raw)
  To: Johannes Berg
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Johannes,

On Tue, Apr 19, 2022 at 01:33:21PM +0200, Johannes Berg wrote:
> On Tue, 2022-04-19 at 13:16 +0200, 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 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.
> 
> 
> 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 <johannes@sipsolutions.net>
> 
> 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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 08/11] um: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 11:37       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 11:37 UTC (permalink / raw)
  To: Johannes Berg
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Johannes,

On Tue, Apr 19, 2022 at 01:33:21PM +0200, Johannes Berg wrote:
> On Tue, 2022-04-19 at 13:16 +0200, 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 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.
> 
> 
> 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 <johannes@sipsolutions.net>
> 
> 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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-19 18:16     ` Borislav Petkov
  -1 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:16 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, Apr 19, 2022 at 01:16:46PM +0200, 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 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.
> 
> 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/timex.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
> index a4a8b1b16c0c..fac180359693 100644
> --- a/arch/x86/include/asm/timex.h
> +++ b/arch/x86/include/asm/timex.h
> @@ -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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:16     ` Borislav Petkov
  0 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:16 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, Apr 19, 2022 at 01:16:46PM +0200, 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 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.
> 
> 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/timex.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
> index a4a8b1b16c0c..fac180359693 100644
> --- a/arch/x86/include/asm/timex.h
> +++ b/arch/x86/include/asm/timex.h
> @@ -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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:16     ` Borislav Petkov
  0 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:16 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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, Dave Hansen,
	H . Peter Anvin, Chris Zankel, Max Filippov, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, Apr 19, 2022 at 01:16:46PM +0200, 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 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.
> 
> 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/timex.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/arch/x86/include/asm/timex.h b/arch/x86/include/asm/timex.h
> index a4a8b1b16c0c..fac180359693 100644
> --- a/arch/x86/include/asm/timex.h
> +++ b/arch/x86/include/asm/timex.h
> @@ -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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
  2022-04-19 18:16     ` Borislav Petkov
  (?)
@ 2022-04-19 18:38       ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 18:38 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:16 PM Borislav Petkov <bp@alien8.de> wrote:
> > +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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:38       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 18:38 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:16 PM Borislav Petkov <bp@alien8.de> wrote:
> > +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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:38       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 18:38 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:16 PM Borislav Petkov <bp@alien8.de> wrote:
> > +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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
  2022-04-19 18:38       ` Jason A. Donenfeld
  (?)
@ 2022-04-19 18:59         ` Borislav Petkov
  -1 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:59 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> 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!

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:59         ` Borislav Petkov
  0 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:59 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> 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!

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 18:59         ` Borislav Petkov
  0 siblings, 0 replies; 75+ messages in thread
From: Borislav Petkov @ 2022-04-19 18:59 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> 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!

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
  2022-04-19 18:59         ` Borislav Petkov
  (?)
@ 2022-04-19 19:00           ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 19:00 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:59 PM Borislav Petkov <bp@alien8.de> wrote:
>
> On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> > 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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 19:00           ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 19:00 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:59 PM Borislav Petkov <bp@alien8.de> wrote:
>
> On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> > 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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 07/11] x86: use fallback for random_get_entropy() instead of zero
@ 2022-04-19 19:00           ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-19 19:00 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, linux-m68k, open list:BROADCOM NVRAM DRIVER,
	linux-riscv, sparclinux, linux-um, X86 ML, linux-xtensa

Hi Borislav,

On Tue, Apr 19, 2022 at 8:59 PM Borislav Petkov <bp@alien8.de> wrote:
>
> On Tue, Apr 19, 2022 at 08:38:41PM +0200, Jason A. Donenfeld wrote:
> > 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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-21  8:00     ` Max Filippov
  -1 siblings, 0 replies; 75+ messages in thread
From: Max Filippov @ 2022-04-21  8:00 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, linux-crypto, Thomas Gleixner, 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, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, open list:M68K ARCHITECTURE, linux-mips,
	linux-riscv, open list:SPARC + UltraSPAR...,
	linux-um, maintainer:X86 ARCHITECTURE...,
	open list:TENSILICA XTENSA PORT (xtensa)

On Tue, Apr 19, 2022 at 4:18 AM Jason A. Donenfeld <Jason@zx2c4.com> 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 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.
>
> 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(-)

Acked-by: Max Filippov <jcmvbkbc@gmail.com>

-- 
Thanks.
-- Max

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
@ 2022-04-21  8:00     ` Max Filippov
  0 siblings, 0 replies; 75+ messages in thread
From: Max Filippov @ 2022-04-21  8:00 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, linux-crypto, Thomas Gleixner, 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, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, open list:M68K ARCHITECTURE, linux-mips,
	linux-riscv, open list:SPARC + UltraSPAR...,
	linux-um, maintainer:X86 ARCHITECTURE...,
	open list:TENSILICA XTENSA PORT (xtensa)

On Tue, Apr 19, 2022 at 4:18 AM Jason A. Donenfeld <Jason@zx2c4.com> 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 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.
>
> 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(-)

Acked-by: Max Filippov <jcmvbkbc@gmail.com>

-- 
Thanks.
-- Max

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 10/11] xtensa: use fallback for random_get_entropy() instead of zero
@ 2022-04-21  8:00     ` Max Filippov
  0 siblings, 0 replies; 75+ messages in thread
From: Max Filippov @ 2022-04-21  8:00 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, linux-crypto, Thomas Gleixner, 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, Stephen Boyd, Dinh Nguyen,
	linux-arm-kernel, open list:M68K ARCHITECTURE, linux-mips,
	linux-riscv, open list:SPARC + UltraSPAR...,
	linux-um, maintainer:X86 ARCHITECTURE...,
	open list:TENSILICA XTENSA PORT (xtensa)

On Tue, Apr 19, 2022 at 4:18 AM Jason A. Donenfeld <Jason@zx2c4.com> 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 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.
>
> 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(-)

Acked-by: Max Filippov <jcmvbkbc@gmail.com>

-- 
Thanks.
-- Max

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-21 19:25     ` Maciej W. Rozycki
  -1 siblings, 0 replies; 75+ messages in thread
From: Maciej W. Rozycki @ 2022-04-21 19:25 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	linux-mips, linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:

> 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 <macro@orcam.me.uk>

 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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 19:25     ` Maciej W. Rozycki
  0 siblings, 0 replies; 75+ messages in thread
From: Maciej W. Rozycki @ 2022-04-21 19:25 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	linux-mips, linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:

> 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 <macro@orcam.me.uk>

 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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 19:25     ` Maciej W. Rozycki
  0 siblings, 0 replies; 75+ messages in thread
From: Maciej W. Rozycki @ 2022-04-21 19:25 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	linux-mips, linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:

> 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 <macro@orcam.me.uk>

 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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
  2022-04-21 19:25     ` Maciej W. Rozycki
  (?)
@ 2022-04-21 20:01       ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-21 20:01 UTC (permalink / raw)
  To: Maciej W. Rozycki, Thomas Bogendoerfer
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, Arnd Bergmann,
	Theodore Ts'o, Dominik Brodowski, Russell King,
	Catalin Marinas, Will Deacon, Geert Uytterhoeven, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	open list:BROADCOM NVRAM DRIVER, linux-riscv, sparclinux,
	linux-um, X86 ML, linux-xtensa

Hi Maciej,

On Thu, Apr 21, 2022 at 9:25 PM Maciej W. Rozycki <macro@orcam.me.uk> wrote:
>
> On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:
>
> > 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 <macro@orcam.me.uk>
>
>  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!

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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 20:01       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-21 20:01 UTC (permalink / raw)
  To: Maciej W. Rozycki, Thomas Bogendoerfer
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, Arnd Bergmann,
	Theodore Ts'o, Dominik Brodowski, Russell King,
	Catalin Marinas, Will Deacon, Geert Uytterhoeven, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	open list:BROADCOM NVRAM DRIVER, linux-riscv, sparclinux,
	linux-um, X86 ML, linux-xtensa

Hi Maciej,

On Thu, Apr 21, 2022 at 9:25 PM Maciej W. Rozycki <macro@orcam.me.uk> wrote:
>
> On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:
>
> > 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 <macro@orcam.me.uk>
>
>  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!

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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 20:01       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-21 20:01 UTC (permalink / raw)
  To: Maciej W. Rozycki, Thomas Bogendoerfer
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, Arnd Bergmann,
	Theodore Ts'o, Dominik Brodowski, Russell King,
	Catalin Marinas, Will Deacon, Geert Uytterhoeven, 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,
	Stephen Boyd, Dinh Nguyen, linux-arm-kernel, linux-m68k,
	open list:BROADCOM NVRAM DRIVER, linux-riscv, sparclinux,
	linux-um, X86 ML, linux-xtensa

Hi Maciej,

On Thu, Apr 21, 2022 at 9:25 PM Maciej W. Rozycki <macro@orcam.me.uk> wrote:
>
> On Tue, 19 Apr 2022, Jason A. Donenfeld wrote:
>
> > 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 <macro@orcam.me.uk>
>
>  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!

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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-21 21:05     ` Thomas Bogendoerfer
  -1 siblings, 0 replies; 75+ messages in thread
From: Thomas Bogendoerfer @ 2022-04-21 21:05 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, Theodore Ts'o,
	Dominik Brodowski, Russell King, Catalin Marinas, Will Deacon,
	Geert Uytterhoeven, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

On Tue, Apr 19, 2022 at 01:16:43PM +0200, Jason A. Donenfeld wrote:
> 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 <tglx@linutronix.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
> Cc: Maciej W. Rozycki <macro@orcam.me.uk>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> 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(-)
> 
> diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
> index b05bb70a2e46..e3f5460a923b 100644
> --- a/arch/mips/include/asm/timex.h
> +++ b/arch/mips/include/asm/timex.h
> @@ -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
>  
> -- 
> 2.35.1

Acked-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 21:05     ` Thomas Bogendoerfer
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Bogendoerfer @ 2022-04-21 21:05 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, Theodore Ts'o,
	Dominik Brodowski, Russell King, Catalin Marinas, Will Deacon,
	Geert Uytterhoeven, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

On Tue, Apr 19, 2022 at 01:16:43PM +0200, Jason A. Donenfeld wrote:
> 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 <tglx@linutronix.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
> Cc: Maciej W. Rozycki <macro@orcam.me.uk>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> 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(-)
> 
> diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
> index b05bb70a2e46..e3f5460a923b 100644
> --- a/arch/mips/include/asm/timex.h
> +++ b/arch/mips/include/asm/timex.h
> @@ -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
>  
> -- 
> 2.35.1

Acked-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random
@ 2022-04-21 21:05     ` Thomas Bogendoerfer
  0 siblings, 0 replies; 75+ messages in thread
From: Thomas Bogendoerfer @ 2022-04-21 21:05 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: linux-kernel, linux-crypto, tglx, arnd, Theodore Ts'o,
	Dominik Brodowski, Russell King, Catalin Marinas, Will Deacon,
	Geert Uytterhoeven, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa,
	Maciej W . Rozycki

On Tue, Apr 19, 2022 at 01:16:43PM +0200, Jason A. Donenfeld wrote:
> 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 <tglx@linutronix.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
> Cc: Maciej W. Rozycki <macro@orcam.me.uk>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> 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(-)
> 
> diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h
> index b05bb70a2e46..e3f5460a923b 100644
> --- a/arch/mips/include/asm/timex.h
> +++ b/arch/mips/include/asm/timex.h
> @@ -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
>  
> -- 
> 2.35.1

Acked-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-04-23  2:24     ` Sandy Harris
  -1 siblings, 0 replies; 75+ messages in thread
From: Sandy Harris @ 2022-04-23  2:24 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> 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.

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-23  2:24     ` Sandy Harris
  0 siblings, 0 replies; 75+ messages in thread
From: Sandy Harris @ 2022-04-23  2:24 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> 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.

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-23  2:24     ` Sandy Harris
  0 siblings, 0 replies; 75+ messages in thread
From: Sandy Harris @ 2022-04-23  2:24 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> 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.

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
  2022-04-23  2:24     ` Sandy Harris
  (?)
@ 2022-04-23 10:00       ` Jason A. Donenfeld
  -1 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-23 10:00 UTC (permalink / raw)
  To: Sandy Harris
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Sandy,

On Sat, Apr 23, 2022 at 10:24:07AM +0800, Sandy Harris wrote:
> On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
> >
> > 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

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-23 10:00       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-23 10:00 UTC (permalink / raw)
  To: Sandy Harris
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Sandy,

On Sat, Apr 23, 2022 at 10:24:07AM +0800, Sandy Harris wrote:
> On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
> >
> > 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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify
@ 2022-04-23 10:00       ` Jason A. Donenfeld
  0 siblings, 0 replies; 75+ messages in thread
From: Jason A. Donenfeld @ 2022-04-23 10:00 UTC (permalink / raw)
  To: Sandy Harris
  Cc: LKML, Linux Crypto Mailing List, Thomas Gleixner, 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, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

Hi Sandy,

On Sat, Apr 23, 2022 at 10:24:07AM +0800, Sandy Harris wrote:
> On Sat, Apr 23, 2022 at 6:37 AM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
> >
> > 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

_______________________________________________
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] 75+ messages in thread

* Re: [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
  2022-04-19 11:16   ` Jason A. Donenfeld
  (?)
@ 2022-05-02 21:01     ` Dinh Nguyen
  -1 siblings, 0 replies; 75+ messages in thread
From: Dinh Nguyen @ 2022-05-02 21:01 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Stephen Boyd,
	linux-arm-kernel, linux-m68k, linux-mips, linux-riscv,
	sparclinux, linux-um, x86, linux-xtensa



On 4/19/22 06:16, 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 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.
> 
> 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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
> +
>   #endif

Acked-by: Dinh Nguyen <dinguyen@kernel.org>

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
@ 2022-05-02 21:01     ` Dinh Nguyen
  0 siblings, 0 replies; 75+ messages in thread
From: Dinh Nguyen @ 2022-05-02 21:01 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Stephen Boyd,
	linux-arm-kernel, linux-m68k, linux-mips, linux-riscv,
	sparclinux, linux-um, x86, linux-xtensa



On 4/19/22 06:16, 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 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.
> 
> 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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
> +
>   #endif

Acked-by: Dinh Nguyen <dinguyen@kernel.org>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 75+ messages in thread

* Re: [PATCH v5 06/11] nios2: use fallback for random_get_entropy() instead of zero
@ 2022-05-02 21:01     ` Dinh Nguyen
  0 siblings, 0 replies; 75+ messages in thread
From: Dinh Nguyen @ 2022-05-02 21:01 UTC (permalink / raw)
  To: Jason A. Donenfeld, linux-kernel, linux-crypto, tglx, arnd
  Cc: 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, Stephen Boyd,
	linux-arm-kernel, linux-m68k, linux-mips, linux-riscv,
	sparclinux, linux-um, x86, linux-xtensa



On 4/19/22 06:16, 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 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.
> 
> 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..d9a3f426cdda 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()) ?: random_get_entropy_fallback())
> +
>   #endif

Acked-by: Dinh Nguyen <dinguyen@kernel.org>

_______________________________________________
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] 75+ messages in thread

end of thread, other threads:[~2022-05-02 21:02 UTC | newest]

Thread overview: 75+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-19 11:16 [PATCH v5 00/11] archs/random: fallback to best raw ktime when no cycle counter Jason A. Donenfeld
2022-04-19 11:16 ` Jason A. Donenfeld
2022-04-19 11:16 ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 01/11] timekeeping: add raw clock fallback for random_get_entropy() Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 02/11] m68k: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 03/11] riscv: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-21 19:25   ` Maciej W. Rozycki
2022-04-21 19:25     ` Maciej W. Rozycki
2022-04-21 19:25     ` Maciej W. Rozycki
2022-04-21 20:01     ` Jason A. Donenfeld
2022-04-21 20:01       ` Jason A. Donenfeld
2022-04-21 20:01       ` Jason A. Donenfeld
2022-04-21 21:05   ` Thomas Bogendoerfer
2022-04-21 21:05     ` Thomas Bogendoerfer
2022-04-21 21:05     ` Thomas Bogendoerfer
2022-04-19 11:16 ` [PATCH v5 05/11] arm: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 06/11] nios2: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-05-02 21:01   ` Dinh Nguyen
2022-05-02 21:01     ` Dinh Nguyen
2022-05-02 21:01     ` Dinh Nguyen
2022-04-19 11:16 ` [PATCH v5 07/11] x86: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 18:16   ` Borislav Petkov
2022-04-19 18:16     ` Borislav Petkov
2022-04-19 18:16     ` Borislav Petkov
2022-04-19 18:38     ` Jason A. Donenfeld
2022-04-19 18:38       ` Jason A. Donenfeld
2022-04-19 18:38       ` Jason A. Donenfeld
2022-04-19 18:59       ` Borislav Petkov
2022-04-19 18:59         ` Borislav Petkov
2022-04-19 18:59         ` Borislav Petkov
2022-04-19 19:00         ` Jason A. Donenfeld
2022-04-19 19:00           ` Jason A. Donenfeld
2022-04-19 19:00           ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 08/11] um: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:33   ` Johannes Berg
2022-04-19 11:33     ` Johannes Berg
2022-04-19 11:33     ` Johannes Berg
2022-04-19 11:37     ` Jason A. Donenfeld
2022-04-19 11:37       ` Jason A. Donenfeld
2022-04-19 11:37       ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 09/11] sparc: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16 ` [PATCH v5 10/11] xtensa: " Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-21  8:00   ` Max Filippov
2022-04-21  8:00     ` Max Filippov
2022-04-21  8:00     ` Max Filippov
2022-04-19 11:16 ` [PATCH v5 11/11] random: insist on random_get_entropy() existing in order to simplify Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-19 11:16   ` Jason A. Donenfeld
2022-04-23  2:24   ` Sandy Harris
2022-04-23  2:24     ` Sandy Harris
2022-04-23  2:24     ` Sandy Harris
2022-04-23 10:00     ` Jason A. Donenfeld
2022-04-23 10:00       ` Jason A. Donenfeld
2022-04-23 10:00       ` Jason A. Donenfeld

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.