Skip to contentSkip to conversation

memcg: Don't loop on css_tryget_online() failure

PATCH In review Tejun Heo 2 participants 3 messages May 29, 2019 – Jun 5, 2019

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 memcg: Don't loop on css_tryget_online() failure

A PF_EXITING task may stay associated with an offline css. get_mem_cgroup_from_mm() may deadlock if mm->owner is in such state. All similar logics in memcg are falling back to root memcg on tryget_online failure and get_mem_cgroup_from_mm() can do the same.

A similar failure existed for task_get_css() and could be triggered through BSD process accounting racing against memcg offlining. See 18fa84a2db0e ("cgroup: Use css_tryget() instead of css_tryget_online() in task_get_css()") for details.

---
 mm/memcontrol.c |   24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
mm/memcontrol.c +10 -14
index e50a2db5b4ff..be1fa89db198 100644
@@ -918,23 +918,19 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
 
 	if (mem_cgroup_disabled())
 		return NULL;
+	/*
+	 * Page cache insertions can happen without an actual mm context,
+	 * e.g. during disk probing on boot, loopback IO, acct() writes.
+	 */
+	if (unlikely(!mm))
+		return root_mem_cgroup;
 
 	rcu_read_lock();
-	do {
-		/*
-		 * Page cache insertions can happen withou an
-		 * actual mm context, e.g. during disk probing
-		 * on boot, loopback IO, acct() writes etc.
-		 */
-		if (unlikely(!mm))
-			memcg = root_mem_cgroup;
-		else {
-			memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
-			if (unlikely(!memcg))
-				memcg = root_mem_cgroup;
-		}
-	} while (!css_tryget_online(&memcg->css));
+	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
+	if (!css_tryget_online(&memcg->css))
+		memcg = root_mem_cgroup;
 	rcu_read_unlock();
+
 	return memcg;
 }
 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Tejun Heo

A PF_EXITING task may stay associated with an offline css. get_mem_cgroup_from_mm() may deadlock if mm->owner is in such state. All similar logics in memcg are falling back to root memcg on tryget_online failure and get_mem_cgroup_from_mm() can do the same.

A similar failure existed for task_get_css() and could be triggered through BSD process accounting racing against memcg offlining. See 18fa84a2db0e ("cgroup: Use css_tryget() instead of css_tryget_online() in task_get_css()") for details.

Do we need to mark this patch for stable or this is too unlikely to happen?

Thanks!

Quoted 42 lines
---
 mm/memcontrol.c |   24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
mm/memcontrol.c +10 -14
index e50a2db5b4ff..be1fa89db198 100644
@@ -918,23 +918,19 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
 
 	if (mem_cgroup_disabled())
 		return NULL;
+	/*
+	 * Page cache insertions can happen without an actual mm context,
+	 * e.g. during disk probing on boot, loopback IO, acct() writes.
+	 */
+	if (unlikely(!mm))
+		return root_mem_cgroup;
 
 	rcu_read_lock();
-	do {
-		/*
-		 * Page cache insertions can happen withou an
-		 * actual mm context, e.g. during disk probing
-		 * on boot, loopback IO, acct() writes etc.
-		 */
-		if (unlikely(!mm))
-			memcg = root_mem_cgroup;
-		else {
-			memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
-			if (unlikely(!memcg))
-				memcg = root_mem_cgroup;
-		}
-	} while (!css_tryget_online(&memcg->css));
+	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
+	if (!css_tryget_online(&memcg->css))
+		memcg = root_mem_cgroup;
 	rcu_read_unlock();
+
 	return memcg;
 }
 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#2 lore raw
Quoted 11 lines · Michal Hocko
Quoted 10 lines · Tejun Heo

A PF_EXITING task may stay associated with an offline css. get_mem_cgroup_from_mm() may deadlock if mm->owner is in such state. All similar logics in memcg are falling back to root memcg on tryget_online failure and get_mem_cgroup_from_mm() can do the same.

A similar failure existed for task_get_css() and could be triggered through BSD process accounting racing against memcg offlining. See 18fa84a2db0e ("cgroup: Use css_tryget() instead of css_tryget_online() in task_get_css()") for details.

Quoted 4 lines
Quoted 1 line

Do we need to mark this patch for stable or this is too unlikely to happen?

This one's a lot less likely than the one in task_get_css() which already is pretty low frequency. I don't think it warrants -stable tagging.

Thanks.

Signature · 1 line
tejun