Skip to content

Commit 01b305b

Browse files
author
Sebastian Andrzej Siewior
committed
random: avoid preempt_disable()ed section
extract_crng() will use sleeping locks while in a preempt_disable() section due to get_cpu_var(). Work around it with local_locks. Cc: [email protected] # where it applies to Signed-off-by: Sebastian Andrzej Siewior <[email protected]>
1 parent cf27163 commit 01b305b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

drivers/char/random.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
#include <linux/syscalls.h>
265265
#include <linux/completion.h>
266266
#include <linux/uuid.h>
267+
#include <linux/locallock.h>
267268
#include <crypto/chacha20.h>
268269

269270
#include <asm/processor.h>
@@ -2086,6 +2087,7 @@ static rwlock_t batched_entropy_reset_lock = __RW_LOCK_UNLOCKED(batched_entropy_
20862087
* at any point prior.
20872088
*/
20882089
static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64);
2090+
static DEFINE_LOCAL_IRQ_LOCK(batched_entropy_u64_lock);
20892091
u64 get_random_u64(void)
20902092
{
20912093
u64 ret;
@@ -2106,7 +2108,7 @@ u64 get_random_u64(void)
21062108
warn_unseeded_randomness(&previous);
21072109

21082110
use_lock = READ_ONCE(crng_init) < 2;
2109-
batch = &get_cpu_var(batched_entropy_u64);
2111+
batch = &get_locked_var(batched_entropy_u64_lock, batched_entropy_u64);
21102112
if (use_lock)
21112113
read_lock_irqsave(&batched_entropy_reset_lock, flags);
21122114
if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) {
@@ -2116,12 +2118,13 @@ u64 get_random_u64(void)
21162118
ret = batch->entropy_u64[batch->position++];
21172119
if (use_lock)
21182120
read_unlock_irqrestore(&batched_entropy_reset_lock, flags);
2119-
put_cpu_var(batched_entropy_u64);
2121+
put_locked_var(batched_entropy_u64_lock, batched_entropy_u64);
21202122
return ret;
21212123
}
21222124
EXPORT_SYMBOL(get_random_u64);
21232125

21242126
static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32);
2127+
static DEFINE_LOCAL_IRQ_LOCK(batched_entropy_u32_lock);
21252128
u32 get_random_u32(void)
21262129
{
21272130
u32 ret;
@@ -2136,7 +2139,7 @@ u32 get_random_u32(void)
21362139
warn_unseeded_randomness(&previous);
21372140

21382141
use_lock = READ_ONCE(crng_init) < 2;
2139-
batch = &get_cpu_var(batched_entropy_u32);
2142+
batch = &get_locked_var(batched_entropy_u32_lock, batched_entropy_u32);
21402143
if (use_lock)
21412144
read_lock_irqsave(&batched_entropy_reset_lock, flags);
21422145
if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) {
@@ -2146,7 +2149,7 @@ u32 get_random_u32(void)
21462149
ret = batch->entropy_u32[batch->position++];
21472150
if (use_lock)
21482151
read_unlock_irqrestore(&batched_entropy_reset_lock, flags);
2149-
put_cpu_var(batched_entropy_u32);
2152+
put_locked_var(batched_entropy_u32_lock, batched_entropy_u32);
21502153
return ret;
21512154
}
21522155
EXPORT_SYMBOL(get_random_u32);

0 commit comments

Comments
 (0)