Skip to content

Commit 0fdb596

Browse files
leitaogregkh
authored andcommitted
memcg: fix data-race KCSAN bug in rstats
commit 78ec6f9 upstream. A data-race issue in memcg rstat occurs when two distinct code paths access the same 4-byte region concurrently. KCSAN detection triggers the following BUG as a result. BUG: KCSAN: data-race in __count_memcg_events / mem_cgroup_css_rstat_flush write to 0xffffe8ffff98e300 of 4 bytes by task 5274 on cpu 17: mem_cgroup_css_rstat_flush (mm/memcontrol.c:5850) cgroup_rstat_flush_locked (kernel/cgroup/rstat.c:243 (discriminator 7)) cgroup_rstat_flush (./include/linux/spinlock.h:401 kernel/cgroup/rstat.c:278) mem_cgroup_flush_stats.part.0 (mm/memcontrol.c:767) memory_numa_stat_show (mm/memcontrol.c:6911) <snip> read to 0xffffe8ffff98e300 of 4 bytes by task 410848 on cpu 27: __count_memcg_events (mm/memcontrol.c:725 mm/memcontrol.c:962) count_memcg_event_mm.part.0 (./include/linux/memcontrol.h:1097 ./include/linux/memcontrol.h:1120) handle_mm_fault (mm/memory.c:5483 mm/memory.c:5622) <snip> value changed: 0x00000029 -> 0x00000000 The race occurs because two code paths access the same "stats_updates" location. Although "stats_updates" is a per-CPU variable, it is remotely accessed by another CPU at cgroup_rstat_flush_locked()->mem_cgroup_css_rstat_flush(), leading to the data race mentioned. Considering that memcg_rstat_updated() is in the hot code path, adding a lock to protect it may not be desirable, especially since this variable pertains solely to statistics. Therefore, annotating accesses to stats_updates with READ/WRITE_ONCE() can prevent KCSAN splats and potential partial reads/writes. Link: https://lkml.kernel.org/r/20240424125940.2410718-1-leitao@debian.org Fixes: 9cee7e8 ("mm: memcg: optimize parent iteration in memcg_rstat_updated()") Signed-off-by: Breno Leitao <leitao@debian.org> Suggested-by: Shakeel Butt <shakeel.butt@linux.dev> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Shakeel Butt <shakeel.butt@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 541e85e commit 0fdb596

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

mm/memcontrol.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,25 +710,27 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
710710
{
711711
struct memcg_vmstats_percpu *statc;
712712
int cpu = smp_processor_id();
713+
unsigned int stats_updates;
713714

714715
if (!val)
715716
return;
716717

717718
cgroup_rstat_updated(memcg->css.cgroup, cpu);
718719
statc = this_cpu_ptr(memcg->vmstats_percpu);
719720
for (; statc; statc = statc->parent) {
720-
statc->stats_updates += abs(val);
721-
if (statc->stats_updates < MEMCG_CHARGE_BATCH)
721+
stats_updates = READ_ONCE(statc->stats_updates) + abs(val);
722+
WRITE_ONCE(statc->stats_updates, stats_updates);
723+
if (stats_updates < MEMCG_CHARGE_BATCH)
722724
continue;
723725

724726
/*
725727
* If @memcg is already flush-able, increasing stats_updates is
726728
* redundant. Avoid the overhead of the atomic update.
727729
*/
728730
if (!memcg_vmstats_needs_flush(statc->vmstats))
729-
atomic64_add(statc->stats_updates,
731+
atomic64_add(stats_updates,
730732
&statc->vmstats->stats_updates);
731-
statc->stats_updates = 0;
733+
WRITE_ONCE(statc->stats_updates, 0);
732734
}
733735
}
734736

@@ -5690,7 +5692,7 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
56905692
}
56915693
}
56925694
}
5693-
statc->stats_updates = 0;
5695+
WRITE_ONCE(statc->stats_updates, 0);
56945696
/* We are in a per-cpu loop here, only do the atomic write once */
56955697
if (atomic64_read(&memcg->vmstats->stats_updates))
56965698
atomic64_set(&memcg->vmstats->stats_updates, 0);

0 commit comments

Comments
 (0)