cgroup: list processes whose leader has exited where their threads are
Subsystems (6)
cover cgroup: list processes whose leader has exited where their threads are
From: Zhan Xusheng <[email protected]>
This implements Tejun's suggestion in [1]. Patch 1 makes PROCS walks visit an exited leader left behind by a cgroup.procs migration at the first live thread of its process. Patch 2 makes the memcg OOM group lookup do the same.
Unlike [1], the leader does not record the destination css_set at migration time. The walk looks up the css_set of the first live thread instead, because a recorded css_set goes stale when the threads move without a cgroup.procs write. With a version that recorded it, writing "+pids" to the parent's cgroup.subtree_control moved the threads to a new css_set, and dst's cgroup.procs was empty again.
/proc/$PID/cgroup still shows the leader's cgroup, so for such a process it now differs from the cgroup whose cgroup.procs lists it. /proc/$PID/task/$TID/cgroup of a live thread shows the new cgroup, as before.
Tested in a 2-CPU QEMU guest with KASAN and lockdep on for-7.4-fixes acec9637e03c. The process has an exited leader and two live threads, and one thread ID is written to dst/cgroup.procs:
base 1/2 1/2+2/2
dst/cgroup.procs empty pid pid
OOM in dst no kill killed killed
OOM in src moved not killed not killed
process
killed
OOM in dst with oom.group and other moved both
another process in dst process process killed
killed killed
test_kill, 5 runs 5 failures pass passtest_core, test_freezer, test_pids and test_memcontrol give the same results as base. Patch 2 also applies to mm-hotfixes-unstable and mm-new.
[1] https://lore.kernel.org/all/[email protected]/
Zhan Xusheng (2):
cgroup: list a process whose leader has exited where its threads are
mm: memcontrol: find the OOM group of a process where its threads are
include/linux/cgroup.h | 18 ++++++ include/linux/sched.h | 2 + kernel/cgroup/cgroup.c | 66 +++++++++++++++++++++- mm/memcontrol.c | 2 +- tools/testing/selftests/cgroup/test_kill.c | 16 ++++-- 5 files changed, 96 insertions(+), 8 deletions(-)
base-commit: acec9637e03ca54bd3c3a0c1a6ad5c6dfa07c0df
Signature · 1 line
2.43.0
PATCH 1/2 cgroup: list a process whose leader has exited where its threads are
From: Zhan Xusheng <[email protected]>
A cgroup.procs migration skips exiting tasks. If the thread group leader of a process has exited, writing the ID of one of its threads to cgroup.procs moves the live threads and leaves the zombie leader behind. CSS_TASK_ITER_PROCS walks only visit leaders, so the process stays in the old cgroup's cgroup.procs and is missing from the new one's.
With a process whose main thread has called pthread_exit() and whose other threads allocate memory:
# echo $TID > dst/cgroup.procs # cat src/cgroup.procs $PID # cat dst/cgroup.procs #
When dst reaches memory.max, the memcg OOM killer finds no process in dst and kills nothing: 5453 oom events and no oom_kill in 10 seconds. An OOM in src picks the moved process instead.
Keep the leader on its css_set, and make PROCS walks visit it at the first live thread of the process when that thread is on another css_set. Update test_cgkill_exited_leader_migrated, which expected dst's cgroup.procs to be empty.
--- include/linux/sched.h | 2 + kernel/cgroup/cgroup.c | 55 +++++++++++++++++++++- tools/testing/selftests/cgroup/test_kill.c | 16 +++++-- 3 files changed, 66 insertions(+), 7 deletions(-)
include/linux/sched.h +2 -0
@@ -1352,6 +1352,8 @@ struct task_struct {struct css_set __rcu *cgroups;/* cg_list protected by css_set_lock and tsk->alloc_lock: */ struct list_head cg_list; +/* exiting leader left behind by a cgroup.procs migration */ +bool cg_leader_left;#ifdef CONFIG_PREEMPT_RT struct llist_node cg_dead_lnode;#endif /* CONFIG_PREEMPT_RT */
kernel/cgroup/cgroup.c +53 -2
85 lines not on this page · raw message
tools/testing/selftests/cgroup/test_kill.c +11 -5
@@ -406,15 +406,16 @@ static int test_cgkill_exited_leader(const char *root) }/* -* Test that cgroup.kill kills the threads of a process that were migrated -* to the cgroup after their thread group leader had exited, and so are -* not listed in its cgroup.procs. +* Test that a process whose thread group leader has exited is listed in the +* cgroup.procs of the cgroup its threads were migrated to, and that +* cgroup.kill there kills it. */ static int test_cgkill_exited_leader_migrated (const char *root) {char *parent =NULL , *src =NULL , *dst =NULL ;int ret =KSFT_FAIL ;pid_t pid = -ESRCH ; +char buf[32 ]; parent =cg_name (root,"cg_test_exited_leader_migrated" );if (!parent)
@@ -438,12 +439,17 @@ static int test_cgkill_exited_leader_migrated(const char *root)if (wait_for_zombie_leader (pid)) goto cleanup; -/* The exiting leader stays behind, only the live thread is moved. */ +/* +* Only the live thread is moved, but the process is listed where the +* thread is. +*/ if (cg_enter (dst, pid)) goto cleanup; +snprintf (buf,sizeof (buf),"%d\n" , pid);if (cg_read_strcmp (dst,"cgroup.events" ,"populated 1\n" ) || -cg_read_strcmp (dst,"cgroup.procs" ,"" )) +cg_read_strcmp (dst,"cgroup.procs" , buf) || +cg_read_strcmp (src,"cgroup.procs" ,"" )) goto cleanup;if (cg_write (dst,"cgroup.kill" ,"1" ))
Signature · 1 line
2.43.0
PATCH 2/2 mm: memcontrol: find the OOM group of a process where its threads are
In reply to Zhan XushengFrom: Zhan Xusheng <[email protected]>
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.
--- include/linux/cgroup.h | 18 ++++++++++++++++++ kernel/cgroup/cgroup.c | 11 +++++++++++ mm/memcontrol.c | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-)
include/linux/cgroup.h +18 -0
@@ -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
kernel/cgroup/cgroup.c +11 -0
@@ -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;
mm/memcontrol.c +1 -1
@@ -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;
Signature · 1 line
2.43.0