git: dd9548db7a72 - stable/12 - kern: random: collect ~16x less from fast-entropy sources

From: David E. O'Brien <obrien_at_FreeBSD.org>
Date: Fri, 18 Feb 2022 04:54:54 UTC
The branch stable/12 has been updated by obrien:

URL: https://cgit.FreeBSD.org/src/commit/?id=dd9548db7a72644b5ddd822a40162c3b5a1476d1

commit dd9548db7a72644b5ddd822a40162c3b5a1476d1
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2021-09-20 05:46:21 +0000
Commit:     David E. O'Brien <obrien@FreeBSD.org>
CommitDate: 2022-02-18 04:09:47 +0000

    kern: random: collect ~16x less from fast-entropy sources
    
    Previously, we were collecting at a base rate of:
    
    64 bits x 32 pools x 10 Hz = 2.5 kB/s
    
    This change drops it to closer to 64-ish bits per pool per second, to
    work a little better with entropy providers in virtualized environments
    without compromising the security goals of Fortuna.
    
    (cherry picked from commit 5e79bba562bc303eed669dbd0d391b6c6a9c289b)
    (cherry picked from commit 06248c82158019f30a18494e7ce91ac5f9524452)
---
 sys/dev/random/random_harvestq.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c
index 035b5c028fb6..d98730d0054f 100644
--- a/sys/dev/random/random_harvestq.c
+++ b/sys/dev/random/random_harvestq.c
@@ -64,6 +64,13 @@ __FBSDID("$FreeBSD$");
 #include <dev/random/randomdev.h>
 #include <dev/random/random_harvestq.h>
 
+/*
+ * Note that random_sources_feed() will also use this to try and split up
+ * entropy into a subset of pools per iteration with the goal of feeding
+ * HARVESTSIZE into every pool at least once per second.
+ */
+#define	RANDOM_KTHREAD_HZ	10
+
 static void random_kthread(void);
 static void random_sources_feed(void);
 
@@ -192,7 +199,8 @@ random_kthread(void)
 			}
 		}
 		/* XXX: FIX!! This is a *great* place to pass hardware/live entropy to random(9) */
-		tsleep_sbt(&harvest_context.hc_kthread_proc, 0, "-", SBT_1S/10, 0, C_PREL(1));
+		tsleep_sbt(&harvest_context.hc_kthread_proc, 0, "-",
+		    SBT_1S/RANDOM_KTHREAD_HZ, 0, C_PREL(1));
 	}
 	random_kthread_control = -1;
 	wakeup(&harvest_context.hc_kthread_proc);
@@ -213,7 +221,7 @@ random_sources_feed(void)
 {
 	uint32_t entropy[HARVESTSIZE];
 	struct random_sources *rrs;
-	u_int i, n, local_read_rate;
+	u_int i, n, local_read_rate, npools;
 
 	/*
 	 * Step over all of live entropy sources, and feed their output
@@ -229,8 +237,21 @@ random_sources_feed(void)
 	local_read_rate = MAX(local_read_rate, 1);
 	/* But not exceeding RANDOM_KEYSIZE_WORDS */
 	local_read_rate = MIN(local_read_rate, RANDOM_KEYSIZE_WORDS);
+
+	/*
+	 * Evenly-ish distribute pool population across the second based on how
+	 * frequently random_kthread iterates.
+	 *
+	 * For Fortuna, the math currently works out as such:
+	 * 64 bits * 4 pools = 256 bits per iteration
+	 * 256 bits * 10 Hz = 2560 bits per second, 320 B/s
+	 *
+	 */
+	npools = howmany(p_random_alg_context->ra_poolcount * local_read_rate,
+	    RANDOM_KTHREAD_HZ);
+
 	LIST_FOREACH(rrs, &source_list, rrs_entries) {
-		for (i = 0; i < p_random_alg_context->ra_poolcount*local_read_rate; i++) {
+		for (i = 0; i < npools; i++) {
 			n = rrs->rrs_source->rs_read(entropy, sizeof(entropy));
 			KASSERT((n <= sizeof(entropy)), ("%s: rs_read returned too much data (%u > %zu)", __func__, n, sizeof(entropy)));
 			/* It would appear that in some circumstances (e.g. virtualisation),