linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org
Cc: nathan@kernel.org, "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Arnd Bergmann <arnd@arndb.de>, Theodore Ts'o <tytso@mit.edu>
Subject: [PATCH v7] timekeeping: Add raw clock fallback for random_get_entropy()
Date: Thu,  5 May 2022 02:29:10 +0200	[thread overview]
Message-ID: <20220505001924.176087-1-Jason@zx2c4.com> (raw)
Message-ID: <20220505002910.IAcnpEOE2zR2ibERl4Lh3Y_PMmtb0Rf43lVevgztJiM@z> (raw)
In-Reply-To: <87y1zkmg0l.ffs@tglx>

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.

Finally, since random_get_entropy_fallback() is used during extremely
early boot when randomizing freelists in mm_init(), it can be called
before timekeeping has been initialized. In that case there really is
nothing we can do; jiffies hasn't even started ticking yet. So just give
up and return 0.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Theodore Ts'o <tytso@mit.edu>
---
Changes v6->v7:
- Return 0 if we're called before timekeeping has been initialized,
  reported by Nathan.

 include/linux/timex.h     |  8 ++++++++
 kernel/time/timekeeping.c | 12 ++++++++++++
 2 files changed, 20 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..1d1302ca8d36 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,17 @@ 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)
+{
+	if (unlikely(!tk_core.timekeeper.tkr_mono.clock))
+		return 0;
+	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


  reply	other threads:[~2022-05-05  0:29 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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         ` Jason A. Donenfeld [this message]
2022-05-05  0:29           ` [PATCH v7] timekeeping: Add " 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220505001924.176087-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=arnd@arndb.de \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).