Skip to content

Commit

Permalink
include/linux/percpu_counter.h: race in uniprocessor percpu_counter_a…
Browse files Browse the repository at this point in the history
…dd()

The percpu interface is supposed to be preempt and irq safe.

But:
The uniprocessor implementation of percpu_counter_add() is not irq safe:
if an interrupt happens during the +=, then the result is undefined.

Therefore: switch from preempt_disable() to local_irq_save().
This prevents interrupts from interrupting the +=, and as a side effect
prevents preemption.

Link: https://lkml.kernel.org/r/20221216150441.200533-2-manfred@colorfullife.com
Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
Cc: "Sun, Jiebin" <jiebin.sun@intel.com>
Cc: <1vier1@web.de>
Cc: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
Manfred Spraul authored and Andrew Morton committed Jan 20, 2023
1 parent a58fc1e commit 1590827
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions include/linux/percpu_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
static inline void
percpu_counter_add(struct percpu_counter *fbc, s64 amount)
{
preempt_disable();
unsigned long flags;

local_irq_save(flags);
fbc->count += amount;
preempt_enable();
local_irq_restore(flags);
}

/* non-SMP percpu_counter_add_local is the same with percpu_counter_add */
Expand Down

0 comments on commit 1590827

Please sign in to comment.