syzbot
KCSAN: data-race in percpu_counter_add_batch / try_to_unmap_one
Status:
moderation: reported on 2026/11/18 06:16
Subsystems:
mm
Labels:
prio:low
race:benign
[Documentation on labels]
Reported-by: syzbot+9194be7e22e1abaaa239@syzkaller.appspotmail.com
First crash: 21h59m, last: 8h15m
✨ AI-generated root cause summary:
A KCSAN data race occurs between a marked lockless read in percpu_counter_read_positive() and an unmarked locked update in percpu_counter_add_batch(). When update_hiwater_rss() reads mm->rss_stat via percpu_counter_read_positive(), it executes READ_ONCE(fbc->count) without holding fbc->lock. Concurrently, a page fault slow path in percpu_counter_add_batch() updates fbc->count via a plain read-modify-write (fbc->count += count + amount) under fbc->lock. Under the Linux Kernel Memory Model and KCSAN (with CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC disabled), pairing an unannotated lockless read with a plain locked write constitutes a data race.
▶
▼
✨ Full AI root cause analysis
Root Cause Analysis
1. Functions, Structs, and Fields Involved
- Data Object:
mm->rss_stat[MM_FILEPAGES].count (s64 count within struct percpu_counter, defined in include/linux/percpu_counter.h:24 and embedded in struct mm_struct at include/linux/mm_types.h:1353).
- Writer:
percpu_counter_add_batch() (lib/percpu_counter.c:107), called via:
filemap_map_pages() mm/filemap.c:4022
-> add_mm_counter() include/linux/mm.h:3687
-> percpu_counter_add() include/linux/percpu_counter.h:71
-> percpu_counter_add_batch() lib/percpu_counter.c:107
- Reader:
percpu_counter_read_positive() (include/linux/percpu_counter.h:121), called via:
try_to_unmap_one() mm/rmap.c:2360
-> update_hiwater_rss() include/linux/mm.h:3747
-> get_mm_rss() include/linux/mm.h:3723
-> get_mm_counter() include/linux/mm.h:3675
-> percpu_counter_read_positive() include/linux/percpu_counter.h:121
2. Trigger Mechanism and Concurrency Violation
-
Slow-path counter update in percpu_counter_add_batch():
During fault-around in filemap_map_pages(), task 3805 updates the process RSS counter via add_mm_counter(). When the per-CPU batch overflows (abs(count + amount) >= batch), it enters the slow path:
raw_spin_lock_irqsave(&fbc->lock, flags);
count = __this_cpu_read(*fbc->counters);
fbc->count += count + amount;
__this_cpu_sub(*fbc->counters, count);
raw_spin_unlock_irqrestore(&fbc->lock, flags);
While this read-modify-write is serialized against other writers by fbc->lock, the assignment to fbc->count is a plain, unmarked C write (fbc->count += count + amount;).
-
Lockless statistical read in percpu_counter_read_positive():
Concurrently, task 3804 is running folio unmapping / page reclaim in try_to_unmap_one(). Before decrementing RSS, it calls update_hiwater_rss(), which samples the current RSS usage via get_mm_rss() -> percpu_counter_read_positive(&mm->rss_stat[member]).
percpu_counter_read_positive() is designed for fast, lockless approximate reads and does not acquire fbc->lock:
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{
/* Prevent reloads of fbc->count */
s64 ret = READ_ONCE(fbc->count);
if (ret >= 0)
return ret;
return 0;
}
-
Violation of LKMM / KCSAN rules:
Under the Linux Kernel Memory Model (LKMM) and with # CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is not set, KCSAN sets watchpoints on plain writes. Pairing an unannotated lockless reader (READ_ONCE(fbc->count)) with a plain write (fbc->count += ...) triggers a data-race report because compilers are theoretically allowed to tear or refetch plain memory accesses.
While commit 8535c259c8fb previously addressed this identical scenario in __percpu_counter_limited_add() by annotating the lockless read as data_race(READ_ONCE(fbc->count)) (noting that READ_ONCE() alone still triggers KCSAN against plain writes), percpu_counter_read_positive() in include/linux/percpu_counter.h still used plain READ_ONCE(fbc->count) without data_race(...). Conversely, the locked updates to fbc->count in lib/percpu_counter.c remain plain writes rather than WRITE_ONCE() or data_race().
Sample crash report:
* Struck through repros no longer work on HEAD.