Skip to contentSkip to conversation

sched_ext: Call ops.sub_attach() only while the cgroup is online

PATCH Applied Tejun Heo 2 participants 3 messages Oct 29 – Oct 30

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Call ops.sub_attach() only while the cgroup is online

The parent's ops.sub_attach() runs before the enable work checks that the cgroup is still online, so the parent can get an attach for a cgroup whose rmdir already returned. That complicates parents where cgroup cleanup and sub-scheduler attach race.

Check CSS_ONLINE and call ops.sub_attach() under cgroup_lock(), so that the parent sees an attach only for a cgroup that is online.

Fixes ebeca1f930ea ("sched_ext: Introduce cgroup sub-sched support")
---
 kernel/sched/ext/internal.h |  4 ++++
 kernel/sched/ext/sub.c      | 13 +++++++++++++
 2 files changed, 17 insertions(+)
kernel/sched/ext/internal.h +4 -0
index ff0a3d508533..58652370da5a 100644
@@ -869,6 +869,10 @@ struct sched_ext_ops {
 	 * @sub_attach: Attach a sub-scheduler
 	 * @args: argument container, see the struct definition
 	 *
+	 * The sub-scheduler's cgroup is online for the whole call. If it goes
+	 * offline after the sub-scheduler is accepted, ops.sub_detach()
+	 * follows.
+	 *
 	 * Return 0 to accept the sub-scheduler. -errno to reject.
 	 */
 	s32 (*sub_attach)(struct scx_sub_attach_args *args);
kernel/sched/ext/sub.c +13 -0
index b5d92d89629b..6bc6b93ca63a 100644
@@ -2340,8 +2340,21 @@ void scx_sub_enable_workfn(struct kthread_work *work)
 		.cgroup_path = sch->cgrp_path,
 	};
 
+	/*
+	 * The cgroup stays online across ops.sub_attach(), so the parent never
+	 * sees an attach for a cgroup that rmdir already removed.
+	 */
+	cgroup_lock();
+	if (!(cgrp->self.flags & CSS_ONLINE)) {
+		cgroup_unlock();
+		scx_error(sch, "cgroup is not online");
+		ret = -ENODEV;
+		goto err_disable;
+	}
 	ret = SCX_CALL_OP_RET(parent, sub_attach, NULL,
 			      &sub_attach_args);
+	cgroup_unlock();
+
 	if (ret) {
 		ret = scx_ops_sanitize_err(sch, "sub_attach", ret);
 		scx_error(sch, "parent rejected (%d)", ret);
Signature · 1 line
2.56.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Tejun Heo

The parent's ops.sub_attach() runs before the enable work checks that the cgroup is still online, so the parent can get an attach for a cgroup whose rmdir already returned. That complicates parents where cgroup cleanup and sub-scheduler attach race.

Check CSS_ONLINE and call ops.sub_attach() under cgroup_lock(), so that the parent sees an attach only for a cgroup that is online.

Fixes ebeca1f930ea ("sched_ext: Introduce cgroup sub-sched support")

Thanks,
-Andrea

Quoted 49 lines
---
 kernel/sched/ext/internal.h |  4 ++++
 kernel/sched/ext/sub.c      | 13 +++++++++++++
 2 files changed, 17 insertions(+)
kernel/sched/ext/internal.h +4 -0
index ff0a3d508533..58652370da5a 100644
@@ -869,6 +869,10 @@ struct sched_ext_ops {
 	 * @sub_attach: Attach a sub-scheduler
 	 * @args: argument container, see the struct definition
 	 *
+	 * The sub-scheduler's cgroup is online for the whole call. If it goes
+	 * offline after the sub-scheduler is accepted, ops.sub_detach()
+	 * follows.
+	 *
 	 * Return 0 to accept the sub-scheduler. -errno to reject.
 	 */
 	s32 (*sub_attach)(struct scx_sub_attach_args *args);
kernel/sched/ext/sub.c +13 -0
index b5d92d89629b..6bc6b93ca63a 100644
@@ -2340,8 +2340,21 @@ void scx_sub_enable_workfn(struct kthread_work *work)
 		.cgroup_path = sch->cgrp_path,
 	};
 
+	/*
+	 * The cgroup stays online across ops.sub_attach(), so the parent never
+	 * sees an attach for a cgroup that rmdir already removed.
+	 */
+	cgroup_lock();
+	if (!(cgrp->self.flags & CSS_ONLINE)) {
+		cgroup_unlock();
+		scx_error(sch, "cgroup is not online");
+		ret = -ENODEV;
+		goto err_disable;
+	}
 	ret = SCX_CALL_OP_RET(parent, sub_attach, NULL,
 			      &sub_attach_args);
+	cgroup_unlock();
+
 	if (ret) {
 		ret = scx_ops_sanitize_err(sch, "sub_attach", ret);
 		scx_error(sch, "parent rejected (%d)", ret);
Signature · 1 line
2.56.0