Skip to content

Commit 59b5694

Browse files
bebarinoherbertx
authored andcommitted
random: Use wait_event_freezable() in add_hwgenerator_randomness()
Sebastian reports that after commit ff29629 ("random: Support freezable kthreads in add_hwgenerator_randomness()") we can call might_sleep() when the task state is TASK_INTERRUPTIBLE (state=1). This leads to the following warning. do not call blocking ops when !TASK_RUNNING; state=1 set at [<00000000349d1489>] prepare_to_wait_event+0x5a/0x180 WARNING: CPU: 0 PID: 828 at kernel/sched/core.c:6741 __might_sleep+0x6f/0x80 Modules linked in: CPU: 0 PID: 828 Comm: hwrng Not tainted 5.3.0-rc7-next-20190903+ #46 RIP: 0010:__might_sleep+0x6f/0x80 Call Trace: kthread_freezable_should_stop+0x1b/0x60 add_hwgenerator_randomness+0xdd/0x130 hwrng_fillfn+0xbf/0x120 kthread+0x10c/0x140 ret_from_fork+0x27/0x50 We shouldn't call kthread_freezable_should_stop() from deep within the wait_event code because the task state is still set as TASK_INTERRUPTIBLE instead of TASK_RUNNING and kthread_freezable_should_stop() will try to call into the freezer with the task in the wrong state. Use wait_event_freezable() instead so that it calls schedule() in the right place and tries to enter the freezer when the task state is TASK_RUNNING instead. Reported-by: Sebastian Andrzej Siewior <[email protected]> Tested-by: Sebastian Andrzej Siewior <[email protected]> Cc: Keerthy <[email protected]> Fixes: ff29629 ("random: Support freezable kthreads in add_hwgenerator_randomness()") Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 347bce3 commit 59b5694

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

drivers/char/random.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@
327327
#include <linux/percpu.h>
328328
#include <linux/cryptohash.h>
329329
#include <linux/fips.h>
330+
#include <linux/freezer.h>
330331
#include <linux/ptrace.h>
331332
#include <linux/workqueue.h>
332333
#include <linux/irq.h>
@@ -2429,7 +2430,6 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
24292430
size_t entropy)
24302431
{
24312432
struct entropy_store *poolp = &input_pool;
2432-
bool frozen = false;
24332433

24342434
if (unlikely(crng_init == 0)) {
24352435
crng_fast_load(buffer, count);
@@ -2440,12 +2440,10 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
24402440
* We'll be woken up again once below random_write_wakeup_thresh,
24412441
* or when the calling thread is about to terminate.
24422442
*/
2443-
wait_event_interruptible(random_write_wait,
2444-
kthread_freezable_should_stop(&frozen) ||
2443+
wait_event_freezable(random_write_wait,
2444+
kthread_should_stop() ||
24452445
ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits);
2446-
if (!frozen) {
2447-
mix_pool_bytes(poolp, buffer, count);
2448-
credit_entropy_bits(poolp, entropy);
2449-
}
2446+
mix_pool_bytes(poolp, buffer, count);
2447+
credit_entropy_bits(poolp, entropy);
24502448
}
24512449
EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);

0 commit comments

Comments
 (0)