Skip to content

Commit 92e7542

Browse files
committed
random: use lockless method of accessing and updating f->reg_idx
Linus pointed out that there is a much more efficient way of avoiding the problem that we were trying to address in commit 9dfa7bb: "fix race in drivers/char/random.c:get_reg()". Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 9dfa7bb commit 92e7542

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

drivers/char/random.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,15 +1097,15 @@ static void add_interrupt_bench(cycles_t start)
10971097
static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
10981098
{
10991099
__u32 *ptr = (__u32 *) regs;
1100-
unsigned long flags;
1100+
unsigned int idx;
11011101

11021102
if (regs == NULL)
11031103
return 0;
1104-
local_irq_save(flags);
1105-
if (f->reg_idx >= sizeof(struct pt_regs) / sizeof(__u32))
1106-
f->reg_idx = 0;
1107-
ptr += f->reg_idx++;
1108-
local_irq_restore(flags);
1104+
idx = READ_ONCE(f->reg_idx);
1105+
if (idx >= sizeof(struct pt_regs) / sizeof(__u32))
1106+
idx = 0;
1107+
ptr += idx++;
1108+
WRITE_ONCE(f->reg_idx, idx);
11091109
return *ptr;
11101110
}
11111111

0 commit comments

Comments
 (0)