Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

include/linux/percpu_counter.h: race in uniprocessor percpu_counter_add()

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>

authored by

Manfred Spraul and committed by
Andrew Morton
88ad32a7 805afd83

+4 -2
+4 -2
include/linux/percpu_counter.h
··· 152 152 static inline void 153 153 percpu_counter_add(struct percpu_counter *fbc, s64 amount) 154 154 { 155 - preempt_disable(); 155 + unsigned long flags; 156 + 157 + local_irq_save(flags); 156 158 fbc->count += amount; 157 - preempt_enable(); 159 + local_irq_restore(flags); 158 160 } 159 161 160 162 /* non-SMP percpu_counter_add_local is the same with percpu_counter_add */