Skip to content

Commit 1872b3b

Browse files
leitaoakpm00
authored andcommitted
mm: memcg: use READ_ONCE()/WRITE_ONCE() to access stock->nr_pages
A memcg pointer in the per-cpu stock can be accessed by drain_all_stock() and consume_stock() in parallel, causing a potential race, which is believed to e harmless. KCSAN shows this data-race clearly in the splat below: BUG: KCSAN: data-race in drain_all_stock.part.0 / try_charge_memcg write to 0xffff88903f8b0788 of 4 bytes by task 35901 on cpu 2: try_charge_memcg (mm/memcontrol.c:2323 mm/memcontrol.c:2746) __mem_cgroup_charge (mm/memcontrol.c:7287 mm/memcontrol.c:7301) do_anonymous_page (mm/memory.c:1054 mm/memory.c:4375 mm/memory.c:4433) __handle_mm_fault (mm/memory.c:3878 mm/memory.c:5300 mm/memory.c:5441) handle_mm_fault (mm/memory.c:5606) do_user_addr_fault (arch/x86/mm/fault.c:1363) exc_page_fault (./arch/x86/include/asm/irqflags.h:37 ./arch/x86/include/asm/irqflags.h:72 arch/x86/mm/fault.c:1513 arch/x86/mm/fault.c:1563) asm_exc_page_fault (./arch/x86/include/asm/idtentry.h:623) read to 0xffff88903f8b0788 of 4 bytes by task 287 on cpu 27: drain_all_stock.part.0 (mm/memcontrol.c:2433) mem_cgroup_css_offline (mm/memcontrol.c:5398 mm/memcontrol.c:5687) css_killed_work_fn (kernel/cgroup/cgroup.c:5521 kernel/cgroup/cgroup.c:5794) process_one_work (kernel/workqueue.c:3254) worker_thread (kernel/workqueue.c:3329 kernel/workqueue.c:3416) kthread (kernel/kthread.c:388) ret_from_fork (arch/x86/kernel/process.c:147) ret_from_fork_asm (arch/x86/entry/entry_64.S:257) value changed: 0x00000014 -> 0x00000013 This happens because drain_all_stock() is reading stock->nr_pages, while consume_stock() might be updating the same address, causing a potential data-race. Make the shared addresses bulletproof regarding to reads and writes, similarly to what stock->cached_objcg and stock->cached. Annotate all accesses to stock->nr_pages with READ_ONCE()/WRITE_ONCE(). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Breno Leitao <[email protected]> Acked-by: Shakeel Butt <[email protected]> Reviewed-by: Roman Gushchin <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3a5a8d3 commit 1872b3b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

mm/memcontrol.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,7 @@ static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
24682468
static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
24692469
{
24702470
struct memcg_stock_pcp *stock;
2471+
unsigned int stock_pages;
24712472
unsigned long flags;
24722473
bool ret = false;
24732474

@@ -2477,8 +2478,9 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
24772478
local_lock_irqsave(&memcg_stock.stock_lock, flags);
24782479

24792480
stock = this_cpu_ptr(&memcg_stock);
2480-
if (memcg == READ_ONCE(stock->cached) && stock->nr_pages >= nr_pages) {
2481-
stock->nr_pages -= nr_pages;
2481+
stock_pages = READ_ONCE(stock->nr_pages);
2482+
if (memcg == READ_ONCE(stock->cached) && stock_pages >= nr_pages) {
2483+
WRITE_ONCE(stock->nr_pages, stock_pages - nr_pages);
24822484
ret = true;
24832485
}
24842486

@@ -2492,16 +2494,18 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
24922494
*/
24932495
static void drain_stock(struct memcg_stock_pcp *stock)
24942496
{
2497+
unsigned int stock_pages = READ_ONCE(stock->nr_pages);
24952498
struct mem_cgroup *old = READ_ONCE(stock->cached);
24962499

24972500
if (!old)
24982501
return;
24992502

2500-
if (stock->nr_pages) {
2501-
page_counter_uncharge(&old->memory, stock->nr_pages);
2503+
if (stock_pages) {
2504+
page_counter_uncharge(&old->memory, stock_pages);
25022505
if (do_memsw_account())
2503-
page_counter_uncharge(&old->memsw, stock->nr_pages);
2504-
stock->nr_pages = 0;
2506+
page_counter_uncharge(&old->memsw, stock_pages);
2507+
2508+
WRITE_ONCE(stock->nr_pages, 0);
25052509
}
25062510

25072511
css_put(&old->css);
@@ -2537,16 +2541,18 @@ static void drain_local_stock(struct work_struct *dummy)
25372541
static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
25382542
{
25392543
struct memcg_stock_pcp *stock;
2544+
unsigned int stock_pages;
25402545

25412546
stock = this_cpu_ptr(&memcg_stock);
25422547
if (READ_ONCE(stock->cached) != memcg) { /* reset if necessary */
25432548
drain_stock(stock);
25442549
css_get(&memcg->css);
25452550
WRITE_ONCE(stock->cached, memcg);
25462551
}
2547-
stock->nr_pages += nr_pages;
2552+
stock_pages = READ_ONCE(stock->nr_pages) + nr_pages;
2553+
WRITE_ONCE(stock->nr_pages, stock_pages);
25482554

2549-
if (stock->nr_pages > MEMCG_CHARGE_BATCH)
2555+
if (stock_pages > MEMCG_CHARGE_BATCH)
25502556
drain_stock(stock);
25512557
}
25522558

@@ -2585,7 +2591,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
25852591

25862592
rcu_read_lock();
25872593
memcg = READ_ONCE(stock->cached);
2588-
if (memcg && stock->nr_pages &&
2594+
if (memcg && READ_ONCE(stock->nr_pages) &&
25892595
mem_cgroup_is_descendant(memcg, root_memcg))
25902596
flush = true;
25912597
else if (obj_stock_flush_required(stock, root_memcg))

0 commit comments

Comments
 (0)