linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter
@ 2022-04-10 21:49 Jason A. Donenfeld
  2022-04-10 21:49 ` [PATCH v2 01/11] timekeeping: add accessor for raw clock Jason A. Donenfeld
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Jason A. Donenfeld @ 2022-04-10 21:49 UTC (permalink / raw)
  To: linux-kernel, linux-crypto, tglx, arnd
  Cc: Jason A. Donenfeld, Theodore Ts'o, Dominik Brodowski,
	Russell King, Catalin Marinas, Will Deacon, Geert Uytterhoeven,
	Thomas Bogendoerfer, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	David S . Miller, Richard Weinberger, Anton Ivanov, Johannes Berg,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
	Chris Zankel, Max Filippov, John Stultz, Stephen Boyd,
	Dinh Nguyen, linux-arm-kernel, linux-m68k, linux-mips,
	linux-riscv, sparclinux, linux-um, x86, linux-xtensa

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 CP0 random register, which isn't 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 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: John Stultz <john.stultz@linaro.org>
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 accessor for raw clock
  timekeeping: use ktime_read_raw_clock() for random_get_entropy() if no
    get_cycles()
  m68k: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  riscv: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  mips: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  arm: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  nios2: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  x86: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  um: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  sparc: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero
  xtensa: use ktime_read_raw_clock() for random_get_entropy() instead of
    zero

 arch/arm/include/asm/timex.h      |  1 +
 arch/m68k/include/asm/timex.h     |  2 +-
 arch/mips/include/asm/timex.h     |  2 +-
 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/tsc.h        | 10 ++++++++++
 arch/xtensa/include/asm/timex.h   |  6 ++----
 include/linux/timex.h             |  8 ++++++++
 kernel/time/timekeeping.c         |  8 ++++++++
 11 files changed, 37 insertions(+), 17 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] 14+ messages in thread

end of thread, other threads:[~2022-04-11 22:09 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-10 21:49 [PATCH v2 00/11] archs/random: fallback to using ktime_read_raw_clock() if no cycle counter Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 01/11] timekeeping: add accessor for raw clock Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 02/11] timekeeping: use ktime_read_raw_clock() for random_get_entropy() if no get_cycles() Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 03/11] m68k: use ktime_read_raw_clock() for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-11  8:18   ` Thomas Gleixner
2022-04-11 22:07     ` Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 04/11] riscv: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 05/11] mips: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 06/11] arm: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 07/11] nios2: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 08/11] x86: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 09/11] um: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 10/11] sparc: " Jason A. Donenfeld
2022-04-10 21:49 ` [PATCH v2 11/11] xtensa: " Jason A. Donenfeld

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