mm: memcg: switch to css_tryget() in get_mem_cgroup_from_mm()

This change “mm: memcg: switch to css_tryget() in get_mem_cgroup_from_mm()” in Linux kernel is authored by Roman Gushchin <guro [at]> on Fri Nov 15 17:34:43 2019 -0800.

mm: memcg: switch to css_tryget() in get_mem_cgroup_from_mm()

We've encountered a rcu stall in get_mem_cgroup_from_mm():

  rcu: INFO: rcu_sched self-detected stall on CPU
  rcu: 33-....: (21000 ticks this GP) idle=6c6/1/0x4000000000000002 softirq=35441/35441 fqs=5017
  (t=21031 jiffies g=324821 q=95837) NMI backtrace for cpu 33
  RIP: 0010:get_mem_cgroup_from_mm+0x2f/0x90

The problem is caused by an exiting task which is associated with an
offline memcg.  We're iterating over and over in the do {} while
(!css_tryget_online()) loop, but obviously the memcg won't become online
and the exiting task won't be migrated to a live memcg.

Let's fix it by switching from css_tryget_online() to css_tryget().

As css_tryget_online() cannot guarantee that the memcg won't go offline,
the check is usually useless, except some rare cases when for example it
determines if something should be presented to a user.

A similar problem is described by commit 18fa84a2db0e ("cgroup: Use
css_tryget() instead of css_tryget_online() in task_get_css()").


: The bug aside, it doesn't matter whether the cgroup is online for the
: callers.  It used to matter when offlining needed to evacuate all charges
: from the memcg, and so needed to prevent new ones from showing up, but we
: don't care now.

Signed-off-by: Roman Gushchin <>
Acked-by: Johannes Weiner <>
Acked-by: Tejun Heo <>
Reviewed-by: Shakeel Butt <>
Cc: Michal Hocko <>
Cc: Michal Koutn <>
Cc: <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit 00d484f.

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 37592dd..46ad252 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -960,7 +960,7 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
 			if (unlikely(!memcg))
 				memcg = root_mem_cgroup;
-	} while (!css_tryget_online(&memcg->css));
+	} while (!css_tryget(&memcg->css));
 	return memcg;

The commit for this change in Linux stable tree is 00d484f (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *