Skip to contentSkip to conversation

prandom_seed_state(): Change to 32-bit seed type.

RFC PATCH In review George Spelvin 1 participant 1 message Mar 28, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 23/50 prandom_seed_state(): Change to 32-bit seed type.

It only uses 32 bits of the seed anyway, and the vast majority of callers pass in 32-bit values. The only exceptions are:
* Several self-tests used a fixed seed of 3141592653589793238ULL. Changed to 3141592653.
* arch/x86/mm/kaslr.c uses a seed from kaslr_get_random_long(). It's already well-mixed, so truncating it is no worse than the previous xor-folding.

(We could use the folded equivalent 32-bit seed for the self-tests, but we just changed the PRNG algorithm, so the self-tests will change across these commits anyway.)

Cc Daniel Borkmann <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Hannes Frederic Sowa <[email protected]>
---
 include/linux/random.h   | 9 +++------
 lib/interval_tree_test.c | 2 +-
 lib/rbtree_test.c        | 2 +-
 lib/test_bpf.c           | 2 +-
 lib/test_parman.c        | 2 +-
 5 files changed, 7 insertions(+), 10 deletions(-)
include/linux/random.h +3 -6
index 4a325f5cf298f..7956063253261 100644
@@ -210,14 +210,11 @@ void prandom_seed_early(struct rnd_state *state, u32 seed, bool);
 /**
  * prandom_seed_state - set seed for prandom_u32_state().
  * @state: pointer to state structure to receive the seed.
- * @seed: arbitrary 64-bit value to use as a seed.
- *        (only 32 bits are actually used)
+ * @seed: arbitrary 32-bit value to use as a seed.
  */
-static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
+static inline void prandom_seed_state(struct rnd_state *state, u32 seed)
 {
-	u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
-
-	prandom_seed_early(state, i, false);
+	prandom_seed_early(state, seed, false);
 }
 
 #ifdef CONFIG_ARCH_RANDOM
lib/interval_tree_test.c +1 -1
index 8c129c8c638b9..149bca1edbf0d 100644
@@ -79,7 +79,7 @@ static int interval_tree_test_init(void)
 
 	printk(KERN_ALERT "interval tree insert/remove");
 
-	prandom_seed_state(&rnd, 3141592653589793238ULL);
+	prandom_seed_state(&rnd, 3141592653);
 	init();
 
 	time1 = get_cycles();
lib/rbtree_test.c +1 -1
index 41ae3c7570d39..129b653e6d702 100644
@@ -251,7 +251,7 @@ static int __init rbtree_test_init(void)
 
 	printk(KERN_ALERT "rbtree testing");
 
-	prandom_seed_state(&rnd, 3141592653589793238ULL);
+	prandom_seed_state(&rnd, 3141592653);
 	init();
 
 	time1 = get_cycles();
lib/test_bpf.c +1 -1
index cecb230833bed..76697e24ee206 100644
@@ -134,7 +134,7 @@ static int bpf_fill_maxinsns3(struct bpf_test *self)
 	if (!insn)
 		return -ENOMEM;
 
-	prandom_seed_state(&rnd, 3141592653589793238ULL);
+	prandom_seed_state(&rnd, 3141592653);
 
 	for (i = 0; i < len - 1; i++) {
 		__u32 k = prandom_u32_state(&rnd);
lib/test_parman.c +1 -1
index 35e32243693c9..9db1fe1b45083 100644
@@ -131,7 +131,7 @@ static const struct parman_ops test_parman_lsort_ops = {
 
 static void test_parman_rnd_init(struct test_parman *test_parman)
 {
-	prandom_seed_state(&test_parman->rnd, 3141592653589793238ULL);
+	prandom_seed_state(&test_parman->rnd, 3141592653);
 }
 
 static u32 test_parman_rnd_get(struct test_parman *test_parman)
Signature · 1 line
2.26.0