1 From 30def93565e5ba08676aa2b9083f253fc586dbed Mon Sep 17 00:00:00 2001
2 From: Johannes Weiner <hannes@cmpxchg.org>
3 Date: Thu, 29 Jul 2021 14:53:44 -0700
4 Subject: mm: memcontrol: fix blocking rstat function called from atomic cgroup1 thresholding code
6 From: Johannes Weiner <hannes@cmpxchg.org>
8 commit 30def93565e5ba08676aa2b9083f253fc586dbed upstream.
10 Dan Carpenter reports:
12 The patch 2d146aa3aa84: "mm: memcontrol: switch to rstat" from Apr
13 29, 2021, leads to the following static checker warning:
15 kernel/cgroup/rstat.c:200 cgroup_rstat_flush()
16 warn: sleeping in atomic context
19 3572 static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
21 3574 unsigned long val;
23 3576 if (mem_cgroup_is_root(memcg)) {
24 3577 cgroup_rstat_flush(memcg->css.cgroup);
25 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 This is from static analysis and potentially a false positive. The
28 problem is that mem_cgroup_usage() is called from __mem_cgroup_threshold()
29 which holds an rcu_read_lock(). And the cgroup_rstat_flush() function
32 3578 val = memcg_page_state(memcg, NR_FILE_PAGES) +
33 3579 memcg_page_state(memcg, NR_ANON_MAPPED);
35 3581 val += memcg_page_state(memcg, MEMCG_SWAP);
38 3584 val = page_counter_read(&memcg->memory);
40 3586 val = page_counter_read(&memcg->memsw);
45 __mem_cgroup_threshold() indeed holds the rcu lock. In addition, the
46 thresholding code is invoked during stat changes, and those contexts
47 have irqs disabled as well. If the lock breaking occurs inside the
48 flush function, it will result in a sleep from an atomic context.
50 Use the irqsafe flushing variant in mem_cgroup_usage() to fix this.
52 Link: https://lkml.kernel.org/r/20210726150019.251820-1-hannes@cmpxchg.org
53 Fixes: 2d146aa3aa84 ("mm: memcontrol: switch to rstat")
54 Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
55 Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
56 Acked-by: Chris Down <chris@chrisdown.name>
57 Reviewed-by: Rik van Riel <riel@surriel.com>
58 Acked-by: Michal Hocko <mhocko@suse.com>
59 Reviewed-by: Shakeel Butt <shakeelb@google.com>
60 Cc: <stable@vger.kernel.org>
61 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
62 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
63 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
65 mm/memcontrol.c | 3 ++-
66 1 file changed, 2 insertions(+), 1 deletion(-)
70 @@ -3394,7 +3394,8 @@ static unsigned long mem_cgroup_usage(st
73 if (mem_cgroup_is_root(memcg)) {
74 - cgroup_rstat_flush(memcg->css.cgroup);
75 + /* mem_cgroup_threshold() calls here from irqsafe context */
76 + cgroup_rstat_flush_irqsafe(memcg->css.cgroup);
77 val = memcg_page_state(memcg, NR_FILE_PAGES) +
78 memcg_page_state(memcg, NR_ANON_MAPPED);