Skip to content

Commit

Permalink
random: do not use batches when !crng_ready()
Browse files Browse the repository at this point in the history
commit cbe89e5 upstream.

It's too hard to keep the batches synchronized, and pointless anyway,
since in !crng_ready(), we're updating the base_crng key really often,
where batching only hurts. So instead, if the crng isn't ready, just
call into get_random_bytes(). At this stage nothing is performance
critical anyhow.

Cc: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
zx2c4 authored and gregkh committed May 30, 2022
1 parent f4c98fe commit 273aebb
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,8 @@ static void crng_pre_init_inject(const void *input, size_t len, bool account)

if (account) {
crng_init_cnt += min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt);
if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
++base_crng.generation;
if (crng_init_cnt >= CRNG_INIT_CNT_THRESH)
crng_init = 1;
}
}

spin_unlock_irqrestore(&base_crng.lock, flags);
Expand Down Expand Up @@ -626,6 +624,11 @@ u64 get_random_u64(void)

warn_unseeded_randomness(&previous);

if (!crng_ready()) {
_get_random_bytes(&ret, sizeof(ret));
return ret;
}

local_lock_irqsave(&batched_entropy_u64.lock, flags);
batch = raw_cpu_ptr(&batched_entropy_u64);

Expand Down Expand Up @@ -660,6 +663,11 @@ u32 get_random_u32(void)

warn_unseeded_randomness(&previous);

if (!crng_ready()) {
_get_random_bytes(&ret, sizeof(ret));
return ret;
}

local_lock_irqsave(&batched_entropy_u32.lock, flags);
batch = raw_cpu_ptr(&batched_entropy_u32);

Expand Down

0 comments on commit 273aebb

Please sign in to comment.