linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 00/17] archs/random: fallback to best raw ktime when no cycle counter
@ 2022-04-23 21:26 Jason A. Donenfeld
  2022-04-23 21:26 ` [PATCH v6 01/17] ia64: define get_cycles macro for arch-override Jason A. Donenfeld
                   ` (16 more replies)
  0 siblings, 17 replies; 60+ messages in thread
From: Jason A. Donenfeld @ 2022-04-23 21:26 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, Jonas Bonn, Stefan Kristiansson, Stafford Horne,
	Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle, Helge Deller,
	Richard Henderson, Ivan Kokshaysky, Matt Turner,
	Michael Ellerman, Benjamin Herrenschmidt, Paul Mackerras,
	linux-arm-kernel, linux-m68k, linux-mips, linux-riscv,
	sparclinux, linux-um, x86, linux-xtensa, openrisc, linux-ia64,
	linux-s390, linux-parisc, linuxppc-dev

[Preface for v6: if you're an arch maintainer, a simple Acked-by would
 be appreciated if this looks okay.]

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, OpenRISC, 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 v5->v6:
- Use cpu_feature_enabled() instead of boot_cpu_has() on x86.
- OpenRISC support.
- Define missing `#define get_cycles get_cycles` on various platforms.

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: Jonas Bonn <jonas@southpole.se>
Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
Cc: Stafford Horne <shorne@gmail.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.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
Cc: openrisc@lists.librecores.org
Cc: linux-ia64@vger.kernel.org
Cc: linux-s390@vger.kernel.org
Cc: linux-parisc@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org

Jason A. Donenfeld (17):
  ia64: define get_cycles macro for arch-override
  s390: define get_cycles macro for arch-override
  parisc: define get_cycles macro for arch-override
  alpha: define get_cycles macro for arch-override
  powerpc: define get_cycles macro for arch-override
  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
  openrisc: 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/alpha/include/asm/timex.h    |  1 +
 arch/arm/include/asm/timex.h      |  1 +
 arch/ia64/include/asm/timex.h     |  1 +
 arch/m68k/include/asm/timex.h     |  2 +-
 arch/mips/include/asm/timex.h     | 17 +++---
 arch/nios2/include/asm/timex.h    |  3 ++
 arch/openrisc/include/asm/timex.h |  3 ++
 arch/parisc/include/asm/timex.h   |  3 +-
 arch/powerpc/include/asm/timex.h  |  1 +
 arch/riscv/include/asm/timex.h    |  2 +-
 arch/s390/include/asm/timex.h     |  1 +
 arch/sparc/include/asm/timex_32.h |  4 +-
 arch/um/include/asm/timex.h       |  9 +---
 arch/x86/include/asm/timex.h      | 10 ++++
 arch/x86/include/asm/tsc.h        |  4 +-
 arch/xtensa/include/asm/timex.h   |  6 +--
 drivers/char/random.c             | 89 ++++++++++---------------------
 include/linux/timex.h             |  8 +++
 kernel/time/timekeeping.c         | 10 ++++
 19 files changed, 87 insertions(+), 88 deletions(-)

-- 
2.35.1


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

end of thread, other threads:[~2022-09-25 14:17 UTC | newest]

Thread overview: 60+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-23 21:26 [PATCH v6 00/17] archs/random: fallback to best raw ktime when no cycle counter Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 01/17] ia64: define get_cycles macro for arch-override Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 02/17] s390: " Jason A. Donenfeld
2022-04-25  9:43   ` Heiko Carstens
2022-04-25  9:48     ` Jason A. Donenfeld
2022-04-25 10:21       ` Heiko Carstens
2022-04-23 21:26 ` [PATCH v6 03/17] parisc: " Jason A. Donenfeld
2022-04-24 18:35   ` Helge Deller
2022-04-23 21:26 ` [PATCH v6 04/17] alpha: " Jason A. Donenfeld
2022-04-26 20:31   ` Matt Turner
2022-04-23 21:26 ` [PATCH v6 05/17] powerpc: " Jason A. Donenfeld
2022-04-28  5:19   ` Michael Ellerman
2022-04-23 21:26 ` [PATCH v6 06/17] timekeeping: add raw clock fallback for random_get_entropy() Jason A. Donenfeld
2022-04-25 12:37   ` Thomas Gleixner
2022-04-25 13:22     ` Jason A. Donenfeld
2022-05-02  9:37       ` Thomas Gleixner
2022-05-05  0:19         ` [PATCH v7] timekeeping: Add " Jason A. Donenfeld
2022-05-05  0:29           ` Jason A. Donenfeld
2022-05-06  3:20           ` [timekeeping] 3aeaac747d: PANIC:early_exception kernel test robot
2022-05-06  7:59             ` Thomas Gleixner
2022-05-06 10:12               ` Jason A. Donenfeld
2022-05-06 16:01                 ` Thomas Gleixner
2022-05-06 10:21         ` [PATCH v8] timekeeping: Add raw clock fallback for random_get_entropy() Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 07/17] m68k: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 08/17] riscv: " Jason A. Donenfeld
2022-04-25 14:55   ` Palmer Dabbelt
2022-04-25 15:02     ` Jason A. Donenfeld
2022-04-25 15:20       ` Palmer Dabbelt
2022-04-23 21:26 ` [PATCH v6 09/17] mips: use fallback for random_get_entropy() instead of just c0 random Jason A. Donenfeld
2022-04-26 20:28   ` Maciej W. Rozycki
2022-04-27  1:29     ` Jason A. Donenfeld
2022-06-24 13:04       ` Jason A. Donenfeld
2022-06-24 13:25         ` Maciej W. Rozycki
2022-09-25 10:00           ` Jason A. Donenfeld
2022-09-25 14:17             ` Maciej W. Rozycki
2022-04-23 21:26 ` [PATCH v6 10/17] arm: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-25 14:30   ` Russell King (Oracle)
2022-04-23 21:26 ` [PATCH v6 11/17] openrisc: " Jason A. Donenfeld
2022-04-27 21:42   ` Stafford Horne
2022-04-27 23:26     ` Jason A. Donenfeld
2022-04-29  0:16   ` [PATCH v7 11/17] openrisc: account for 0 starting value in random_get_entropy() Jason A. Donenfeld
2022-04-30  1:19     ` Stafford Horne
2022-04-30  1:29       ` Jason A. Donenfeld
2022-04-30 22:11         ` Stafford Horne
2022-04-30 22:34           ` Jason A. Donenfeld
2022-04-30 22:44             ` Stafford Horne
2022-04-23 21:26 ` [PATCH v6 12/17] nios2: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-05-23 13:56   ` Dinh Nguyen
2022-04-23 21:26 ` [PATCH v6 13/17] x86: " Jason A. Donenfeld
2022-04-25 12:35   ` Thomas Gleixner
2022-04-25 13:41     ` Jason A. Donenfeld
2022-04-25 13:51       ` Jason A. Donenfeld
2022-04-25 17:03       ` Thomas Gleixner
2022-04-25 17:24         ` Jason A. Donenfeld
2022-04-26  8:33           ` [PATCH v7 13/17] x86/asm: " Jason A. Donenfeld
2022-05-02  9:40             ` Thomas Gleixner
2022-04-23 21:26 ` [PATCH v6 14/17] um: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 15/17] sparc: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 16/17] xtensa: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 17/17] random: insist on random_get_entropy() existing in order to simplify Jason A. Donenfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).