[PATCH 2/2] mm: memcontrol: find the OOM group of a process where its threads are
From: Zhan Xusheng
Date: Mon Nov 23 2026 - 22:36:13 EST
From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
When a cgroup.procs migration leaves an exited leader behind, a
CSS_TASK_ITER_PROCS walk visits the leader at the first live thread of
its process. A memcg OOM walk of the cgroup the threads were moved to
can then return that leader as the victim. mem_cgroup_get_oom_group()
takes the memcg from the leader, which is still the old one, so
memory.oom.group of the cgroup the process is in has no effect. With
memory.oom.group set in dst and a second process in dst, an OOM in dst
killed only the moved process.
Add task_procs_css(), which gives the css of the process a PROCS walk
returned, and use it to look up the memcg.
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
include/linux/cgroup.h | 18 ++++++++++++++++++
kernel/cgroup/cgroup.c | 11 +++++++++++
mm/memcontrol.c | 2 +-
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 5dfa915a630e..cb05808f2172 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -548,6 +548,24 @@ static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
return task_css_check(task, subsys_id, false);
}
+struct css_set *task_procs_css_set(struct task_struct *task);
+
+/**
+ * task_procs_css - css of the process a CSS_TASK_ITER_PROCS walk returned
+ * @task: the task returned by the walk
+ * @subsys_id: the subsystem ID of interest
+ *
+ * The walk can return an exiting thread group leader that a cgroup.procs
+ * migration left behind for where the rest of its process is. Use this
+ * instead of task_css() to find the css of the process. Must be called
+ * under rcu_read_lock().
+ */
+static inline struct cgroup_subsys_state *task_procs_css(struct task_struct *task,
+ int subsys_id)
+{
+ return task_procs_css_set(task)->subsys[subsys_id];
+}
+
/**
* task_get_css - find and get the css for (task, subsys)
* @task: the target task
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index ab442f94e29d..0be7f96ce074 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5237,6 +5237,17 @@ static bool procs_stand_in(struct task_struct *task)
return leader != task && procs_live_thread(leader) == task;
}
+/**
+ * task_procs_css_set - css_set of the process a PROCS walk returned
+ * @task: the task returned by the walk
+ *
+ * See task_procs_css().
+ */
+struct css_set *task_procs_css_set(struct task_struct *task)
+{
+ return task_css_set(procs_live_thread(task));
+}
+
static void css_task_iter_advance(struct css_task_iter *it)
{
struct task_struct *task;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1271d390b617..5704e4415a16 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2003,7 +2003,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
rcu_read_lock();
- memcg = mem_cgroup_from_task(victim);
+ memcg = mem_cgroup_from_css(task_procs_css(victim, memory_cgrp_id));
if (mem_cgroup_is_root(memcg))
goto out;
--
2.43.0