]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
random: use lockless method of accessing and updating f->reg_idx
authorTheodore Ts'o <tytso@mit.edu>
Wed, 7 Jun 2017 23:01:32 +0000 (19:01 -0400)
committerSasha Levin <alexander.levin@microsoft.com>
Wed, 23 May 2018 01:36:30 +0000 (21:36 -0400)
[ Upstream commit 92e75428ffc90e2a0321062379f883f3671cfebe ]

Linus pointed out that there is a much more efficient way of avoiding
the problem that we were trying to address in commit 9dfa7bba35ac0:
"fix race in drivers/char/random.c:get_reg()".

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
drivers/char/random.c

index 73049abf4898acc3e4dbcc9eddd47564a5a35bd1..6fa0efa48cc510ba3cc25e1d3f0c70b246dcd522 100644 (file)
@@ -863,15 +863,15 @@ static void add_interrupt_bench(cycles_t start)
 static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
 {
        __u32 *ptr = (__u32 *) regs;
-       unsigned long flags;
+       unsigned int idx;
 
        if (regs == NULL)
                return 0;
-       local_irq_save(flags);
-       if (f->reg_idx >= sizeof(struct pt_regs) / sizeof(__u32))
-               f->reg_idx = 0;
-       ptr += f->reg_idx++;
-       local_irq_restore(flags);
+       idx = READ_ONCE(f->reg_idx);
+       if (idx >= sizeof(struct pt_regs) / sizeof(__u32))
+               idx = 0;
+       ptr += idx++;
+       WRITE_ONCE(f->reg_idx, idx);
        return *ptr;
 }