Skip to contentSkip to conversation

cgroup: Move cgroup_task_dead() to task_struct clean up

PATCH In review Sebastian Andrzej Siewior 3 participants 5 messages Mar 11 – Mar 16

Subsystems (3)
lore
Share & tools
lore

Sebastian Andrzej Siewior

author maintainer
#0 lore raw

PATCH 1/1 cgroup: Move cgroup_task_dead() to task_struct clean up

The cgroup clean up (via cgroup_task_dead()) has been moved to finish_task_switch() so that sched_ext can observe the task attached to its cgroups one last time before the task is gone.

This clean up has been added to irq_work on PREEMPT_RT because finish_task_switch() is invoked with disabled preemption and cgroup_task_dead() needs to acquire sleeping locks.

Invoking cgroup_task_dead() in finish_task_switch() is too late and creates a small window in which the task is observed in the cgroup list after it died and this confuses systemd. To close the window, tasks which are exiting are no longer exposed to the user. Now there is no reason to invoke cgroup_task_dead() in finish_task_switch() and it can be delayed further so simplify the code.

Move cgroup_task_dead() to cgroup_task_free() which is invoked during RCU clean up of the task_struct.

Fixes 9311e6c29b34 ("cgroup: Fix sleeping from invalid context warning on PREEMPT_RT")
---
 include/linux/cgroup.h |  2 --
 include/linux/sched.h  |  3 ---
 kernel/cgroup/cgroup.c | 56 ++----------------------------------------
 kernel/sched/core.c    |  6 -----
 4 files changed, 2 insertions(+), 65 deletions(-)
include/linux/cgroup.h +0 -2
index bc892e3b37eea..4068035176c41 100644
@@ -138,7 +138,6 @@ extern void cgroup_cancel_fork(struct task_struct *p,
 extern void cgroup_post_fork(struct task_struct *p,
 			     struct kernel_clone_args *kargs);
 void cgroup_task_exit(struct task_struct *p);
-void cgroup_task_dead(struct task_struct *p);
 void cgroup_task_release(struct task_struct *p);
 void cgroup_task_free(struct task_struct *p);
 
@@ -682,7 +681,6 @@ static inline void cgroup_cancel_fork(struct task_struct *p,
 static inline void cgroup_post_fork(struct task_struct *p,
 				    struct kernel_clone_args *kargs) {}
 static inline void cgroup_task_exit(struct task_struct *p) {}
-static inline void cgroup_task_dead(struct task_struct *p) {}
 static inline void cgroup_task_release(struct task_struct *p) {}
 static inline void cgroup_task_free(struct task_struct *p) {}
 
include/linux/sched.h +0 -3
index a7b4a980eb2f0..375d0c958081d 100644
@@ -1321,9 +1321,6 @@ struct task_struct {
 	struct css_set __rcu		*cgroups;
 	/* cg_list protected by css_set_lock and tsk->alloc_lock: */
 	struct list_head		cg_list;
-#ifdef CONFIG_PREEMPT_RT
-	struct llist_node		cg_dead_lnode;
-#endif	/* CONFIG_PREEMPT_RT */
 #endif	/* CONFIG_CGROUPS */
 #ifdef CONFIG_X86_CPU_RESCTRL
 	u32				closid;
kernel/cgroup/cgroup.c +2 -54

86 lines not on this page · raw message

kernel/sched/core.c +0 -6
index b7f77c165a6e0..0e67f6db3204a 100644
@@ -5181,13 +5181,7 @@ static struct rq *finish_task_switch(struct task_struct *prev)
 		if (prev->sched_class->task_dead)
 			prev->sched_class->task_dead(prev);
 
-		/*
-		 * sched_ext_dead() must come before cgroup_task_dead() to
-		 * prevent cgroups from being removed while its member tasks are
-		 * visible to SCX schedulers.
-		 */
 		sched_ext_dead(prev);
-		cgroup_task_dead(prev);
 
 		/* Task is done with its stack. */
 		put_task_stack(prev);
Signature · 1 line
2.53.0
maintainer
#1 lore raw

Hello.

kernel/cgroup/cgroup.c void cgroup_task_free(struct task_struct *task) · line 7037

Quoted 8 lines · Sebastian Andrzej Siewior
kernel/cgroup/cgroup.c +1 -0
@@ -7084,6 +7031,7 @@ void cgroup_task_free(struct task_struct *task)
 {
 	struct css_set *cset = task_css_set(task);
 
+	cgroup_task_dead(task);
 	if (!list_empty(&task->cg_list)) {
 		spin_lock_irq(&css_set_lock);
 		css_set_skip_task_iters(task_css_set(task), task);

Erm, isn't this way too late?
I see that cset->dying_tasks is appended in do_cgroup_task_dead() (which I was used to find in cgroup_exit()).

(Also, whole cgroup_task_dead() becomes single use thing so it could be open-coded in the place where it belongs.)

Thanks,
Michal

author maintainer
#2 lore raw
Quoted 7 lines · Michal Koutný

Hello.

Quoted 4 lines · Sebastian Andrzej Siewior
kernel/cgroup/cgroup.c +0 -0
@@ -7084,6 +7031,7 @@ void cgroup_task_free(struct task_struct *task)
 {
 	struct css_set *cset = task_css_set(task);
 
Quoted 6 lines

kernel/cgroup/cgroup.c void cgroup_task_free(struct task_struct *task) · line 7037

Quoted 4 lines
kernel/cgroup/cgroup.c +1 -0
+	cgroup_task_dead(task);
 	if (!list_empty(&task->cg_list)) {
 		spin_lock_irq(&css_set_lock);
 		css_set_skip_task_iters(task_css_set(task), task);

Erm, isn't this way too late?

My understanding is that the point is that the cgroup must not be exposed to userland once the task is about to die. This has been moved after the fine schedule() which is too late because the parent is signaled before that and the removal happens after that.
So now we hide the tasks in the iterator once the task is PF_EXITING.

Quoted 2 lines

I see that cset->dying_tasks is appended in do_cgroup_task_dead() (which I was used to find in cgroup_exit()).

So this could be probably removed later on if this is the only purpose.

Quoted 2 lines

(Also, whole cgroup_task_dead() becomes single use thing so it could be open-coded in the place where it belongs.)

If this what we want, I could inline it there.

Quoted 2 lines

Thanks,
Michal

Sebastian

maintainer
#3 lore raw

Hello,

Quoted 15 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Michal Koutný

Hello.

Quoted 4 lines · Sebastian Andrzej Siewior
kernel/cgroup/cgroup.c +0 -0
@@ -7084,6 +7031,7 @@ void cgroup_task_free(struct task_struct *task)
 {
 	struct css_set *cset = task_css_set(task);
 
Quoted 6 lines

kernel/cgroup/cgroup.c void cgroup_task_free(struct task_struct *task) · line 7037

Quoted 4 lines
kernel/cgroup/cgroup.c +1 -0
+	cgroup_task_dead(task);
 	if (!list_empty(&task->cg_list)) {
 		spin_lock_irq(&css_set_lock);
 		css_set_skip_task_iters(task_css_set(task), task);

Erm, isn't this way too late?

Quoted 5 lines

My understanding is that the point is that the cgroup must not be exposed to userland once the task is about to die. This has been moved after the fine schedule() which is too late because the parent is signaled before that and the removal happens after that.
So now we hide the tasks in the iterator once the task is PF_EXITING.

Yeah, I think it's too late. That's where we tell the cgroup is becoming empty among other things. cgroup_task_free() waits for all task refcnts to drop which can take any amount of time. You can also imagine situations where e.g. userspace pins a specific task through pidfd unitl the cgroup it belongs becomes empty, which would have worked before but now would deadlock.

Thanks.

Signature · 1 line
tejun
author maintainer
#4 lore raw
Quoted 1 line · Tejun Heo

Hello,

Hi,

Quoted 6 lines

Yeah, I think it's too late. That's where we tell the cgroup is becoming empty among other things. cgroup_task_free() waits for all task refcnts to drop which can take any amount of time. You can also imagine situations where e.g. userspace pins a specific task through pidfd unitl the cgroup it belongs becomes empty, which would have worked before but now would deadlock.

I see. What about moving it to delayed_put_task_struct() such as the change on top below? It also RCU and only acquired by bpf_task_acquire(). Is this also a concern or would that work?

include/linux/cgroup.h +2 -0
@@ -138,6 +138,7 @@ extern void cgroup_cancel_fork(struct task_struct *p,
 extern void cgroup_post_fork(struct task_struct *p,
 			     struct kernel_clone_args *kargs);
 void cgroup_task_exit(struct task_struct *p);
+void cgroup_task_dead(struct task_struct *p);
 void cgroup_task_release(struct task_struct *p);
 void cgroup_task_free(struct task_struct *p);
 
@@ -681,6 +682,7 @@ static inline void cgroup_cancel_fork(struct task_struct *p,
 static inline void cgroup_post_fork(struct task_struct *p,
 				    struct kernel_clone_args *kargs) {}
 static inline void cgroup_task_exit(struct task_struct *p) {}
+static inline void cgroup_task_dead(struct task_struct *p) {}
 static inline void cgroup_task_release(struct task_struct *p) {}
 static inline void cgroup_task_free(struct task_struct *p) {}
 
kernel/cgroup/cgroup.c +1 -2
index 684b773cd5cb4..28cdb8d3210e7 100644
@@ -6991,7 +6991,7 @@ void cgroup_task_exit(struct task_struct *tsk)
 	} while_each_subsys_mask();
 }
 
-static void cgroup_task_dead(struct task_struct *tsk)
+void cgroup_task_dead(struct task_struct *tsk)
 {
 	struct css_set *cset;
 	unsigned long flags;
@@ -7031,7 +7031,6 @@ void cgroup_task_free(struct task_struct *task)
 {
 	struct css_set *cset = task_css_set(task);
 
-	cgroup_task_dead(task);
 	if (!list_empty(&task->cg_list)) {
 		spin_lock_irq(&css_set_lock);
 		css_set_skip_task_iters(task_css_set(task), task);
kernel/exit.c +1 -0
index ede3117fa7d41..7a94425122238 100644
@@ -228,6 +228,7 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
 	rethook_flush_task(tsk);
 	perf_event_delayed_put(tsk);
 	trace_sched_process_free(tsk);
+	cgroup_task_dead(tsk);
 	put_task_struct(tsk);
 }
 
Quoted 1 line

Thanks.

Sebastian