[PATCH 3/3] random: use trylock in irq handler rather than spinning

From: Jason A. Donenfeld
Date: Sat Feb 12 2022 - 18:10:50 EST


crng_pre_init_inject() (and prior crng_fast_load()) uses a trylock when
in fast mode, so that it never contends. We should be doing the same
when grabbing a spinlock for mixing into the entropy pool. So switch to
doing that before calling the underscored _mix_pool_bytes().

Cc: Dominik Brodowski <linux@xxxxxxxxxxxxxxxxxxxx>
Cc: Theodore Ts'o <tytso@xxxxxxx>
Signed-off-by: Jason A. Donenfeld <Jason@xxxxxxxxx>
---
drivers/char/random.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 9a8e1bb9845d..ca224c3f2561 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1286,13 +1286,10 @@ void add_interrupt_randomness(int irq)
atomic_set(&fast_pool->count, 0);
fast_pool->last = now;

- /*
- * Technically this call means that we're using a spinlock_t
- * in the IRQ handler, which isn't terrific for PREEMPT_RT.
- * However, this only happens during boot, and then never
- * again, so we live with it.
- */
- mix_pool_bytes(&fast_pool->pool, sizeof(fast_pool->pool));
+ if (spin_trylock(&input_pool.lock)) {
+ _mix_pool_bytes(&fast_pool->pool, sizeof(fast_pool->pool));
+ spin_unlock(&input_pool.lock);
+ }
}
return;
}
--
2.35.0