Skip to content

Commit f5bda35

Browse files
committed
random: use static branch for crng_ready()
Since crng_ready() is only false briefly during initialization and then forever after becomes true, we don't need to evaluate it after, making it a prime candidate for a static branch. One complication, however, is that it changes state in a particular call to credit_init_bits(), which might be made from atomic context, which means we must kick off a workqueue to change the static key. Further complicating things, credit_init_bits() may be called sufficiently early on in system initialization such that system_wq is NULL. Fortunately, there exists the nice function execute_in_process_context(), which will immediately execute the function if !in_interrupt(), and otherwise defer it to a workqueue. During early init, before workqueues are available, in_interrupt() is always false, because interrupts haven't even been enabled yet, which means the function in that case executes immediately. Later on, after workqueues are available, in_interrupt() might be true, but in that case, the work is queued in system_wq and all goes well. Cc: Theodore Ts'o <[email protected]> Cc: Sultan Alsawaf <[email protected]> Reviewed-by: Dominik Brodowski <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 12e45a2 commit f5bda35

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

drivers/char/random.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ static enum {
7878
CRNG_EMPTY = 0, /* Little to no entropy collected */
7979
CRNG_EARLY = 1, /* At least POOL_EARLY_BITS collected */
8080
CRNG_READY = 2 /* Fully initialized with POOL_READY_BITS collected */
81-
} crng_init = CRNG_EMPTY;
82-
#define crng_ready() (likely(crng_init >= CRNG_READY))
81+
} crng_init __read_mostly = CRNG_EMPTY;
82+
static DEFINE_STATIC_KEY_FALSE(crng_is_ready);
83+
#define crng_ready() (static_branch_likely(&crng_is_ready) || crng_init >= CRNG_READY)
8384
/* Various types of waiters for crng_init->CRNG_READY transition. */
8485
static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
8586
static struct fasync_struct *fasync;
@@ -109,6 +110,11 @@ bool rng_is_initialized(void)
109110
}
110111
EXPORT_SYMBOL(rng_is_initialized);
111112

113+
static void crng_set_ready(struct work_struct *work)
114+
{
115+
static_branch_enable(&crng_is_ready);
116+
}
117+
112118
/* Used by wait_for_random_bytes(), and considered an entropy collector, below. */
113119
static void try_to_generate_entropy(void);
114120

@@ -268,7 +274,7 @@ static void crng_reseed(void)
268274
++next_gen;
269275
WRITE_ONCE(base_crng.generation, next_gen);
270276
WRITE_ONCE(base_crng.birth, jiffies);
271-
if (!crng_ready())
277+
if (!static_branch_likely(&crng_is_ready))
272278
crng_init = CRNG_READY;
273279
spin_unlock_irqrestore(&base_crng.lock, flags);
274280
memzero_explicit(key, sizeof(key));
@@ -786,6 +792,7 @@ static void extract_entropy(void *buf, size_t nbytes)
786792

787793
static void credit_init_bits(size_t nbits)
788794
{
795+
static struct execute_work set_ready;
789796
unsigned int new, orig, add;
790797
unsigned long flags;
791798

@@ -801,6 +808,7 @@ static void credit_init_bits(size_t nbits)
801808

802809
if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) {
803810
crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */
811+
execute_in_process_context(crng_set_ready, &set_ready);
804812
process_random_ready_list();
805813
wake_up_interruptible(&crng_init_wait);
806814
kill_fasync(&fasync, SIGIO, POLL_IN);
@@ -1396,7 +1404,7 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int,
13961404
if (count > INT_MAX)
13971405
count = INT_MAX;
13981406

1399-
if (!(flags & GRND_INSECURE) && !crng_ready()) {
1407+
if (!crng_ready() && !(flags & GRND_INSECURE)) {
14001408
int ret;
14011409

14021410
if (flags & GRND_NONBLOCK)

0 commit comments

Comments
 (0)