Skip to contentSkip to conversation

sched_ext: Add scx_task_iter_relock() and use it in scx_root_enable_workfn()

PATCH Superseded Tejun Heo 2 participants 3 messages May 10 – May 11

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Add scx_task_iter_relock() and use it in scx_root_enable_workfn()

scx_root_enable_workfn()'s post-init block re-acquires scx_tasks_lock briefly via a scoped_guard() for the tid hash insertion. c941d7391f25 ("sched_ext: Close root-enable vs sched_ext_dead() race with SCX_TASK_INIT_BEGIN") on for-7.1-fixes adds a post-init DEAD recheck that holds the task's rq lock across the state-machine updates in the same region. A naive merge would acquire scx_tasks_lock while the rq lock is held, inverting the iter's outer/inner order (scx_tasks_lock then rq lock).

Add scx_task_iter_relock(iter, p), the counterpart to scx_task_iter_unlock(), that re-acquires scx_tasks_lock and, if @p is non-NULL, @p's rq lock. The locks are tracked in @iter so subsequent iteration releases them.

Use it in scx_root_enable_workfn()'s post-init block and drop the now-redundant scoped_guard on the hash insertion. The post-init region now runs with both scx_tasks_lock and the task's rq lock held across the state-machine updates and the hash insert.

---
Applied to sched_ext/for-7.2 to resolve the merge conflict from for-7.1-fixes.

 kernel/sched/ext.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)
kernel/sched/ext.c +29 -8
index b685f45b4fd0..d23707d88cfb 100644
@@ -902,6 +902,24 @@ static void __scx_task_iter_maybe_relock(struct scx_task_iter *iter)
 	}
 }
 
+/**
+ * scx_task_iter_relock - Re-acquire scx_tasks_lock and, optionally, @p's rq
+ * @iter: iterator to relock
+ * @p: task whose rq to lock, or %NULL for scx_tasks_lock only
+ *
+ * Counterpart to scx_task_iter_unlock(). Locking @p's rq is optional. Once
+ * re-acquired, both locks are managed by the iterator from here on.
+ */
+static void scx_task_iter_relock(struct scx_task_iter *iter,
+				 struct task_struct *p)
+{
+	__scx_task_iter_maybe_relock(iter);
+	if (p) {
+		iter->rq = task_rq_lock(p, &iter->rf);
+		iter->locked_task = p;
+	}
+}
+
 /**
  * scx_task_iter_stop - Stop a task iteration and unlock scx_tasks_lock
  * @iter: iterator to exit
@@ -7072,19 +7090,22 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 			goto err_disable_unlock_all;
 		}
 
+		/*
+		 * Hold both scx_tasks_lock and @p's rq across the state machine
+		 * updates and the tid hash insert below.
+		 */
+		scx_task_iter_relock(&sti, p);
+
 		scx_set_task_sched(p, sch);
 		scx_set_task_state(p, SCX_TASK_READY);
 
 		/*
-		 * Insert into the tid hash under scx_tasks_lock so we can't
-		 * race sched_ext_dead() and leave a stale entry for an already
-		 * exited task.
+		 * Insert into the tid hash. scx_tasks_lock is held by the iter;
+		 * list_empty() guards against sched_ext_dead() having taken @p
+		 * off the list while init ran unlocked.
 		 */
-		if (scx_tid_to_task_enabled()) {
-			guard(raw_spinlock_irq)(&scx_tasks_lock);
-			if (!list_empty(&p->scx.tasks_node))
-				scx_tid_hash_insert(p);
-		}
+		if (scx_tid_to_task_enabled() && !list_empty(&p->scx.tasks_node))
+			scx_tid_hash_insert(p);
 
 		put_task_struct(p);
 	}
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH v2 1/1 sched_ext: Add scx_task_iter_relock() and use it in scx_root_enable_workfn()

scx_root_enable_workfn()'s post-init block re-acquires scx_tasks_lock briefly via a scoped_guard() for the tid hash insertion. c941d7391f25 ("sched_ext: Close root-enable vs sched_ext_dead() race with SCX_TASK_INIT_BEGIN") on for-7.1-fixes adds a post-init DEAD recheck that holds the task's rq lock across the state-machine updates in the same region. A naive merge would acquire scx_tasks_lock while the rq lock is held, inverting the iter's outer/inner order (scx_tasks_lock then rq lock).

Add scx_task_iter_relock(iter, p), the counterpart to scx_task_iter_unlock(), that re-acquires scx_tasks_lock and, if @p is non-NULL, @p's rq lock. The locks are tracked in @iter so subsequent iteration releases them.

Use it in scx_root_enable_workfn()'s post-init block and drop the now-redundant scoped_guard on the hash insertion. The post-init region now runs with both scx_tasks_lock and the task's rq lock held across the init failure check, the state-machine updates and the hash insert.

v2: Move scx_task_iter_relock() earlier to ease the for-7.1-fixes merge.

---
Applied to sched_ext/for-7.2 to resolve the merge conflict from for-7.1-fixes.

 kernel/sched/ext.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
kernel/sched/ext.c +26 -8
index b685f45b4fd0..fbdc1819d4cb 100644
@@ -902,6 +902,24 @@ static void __scx_task_iter_maybe_relock(struct scx_task_iter *iter)
 	}
 }
 
+/**
+ * scx_task_iter_relock - Re-acquire scx_tasks_lock and, optionally, @p's rq
+ * @iter: iterator to relock
+ * @p: task whose rq to lock, or %NULL for scx_tasks_lock only
+ *
+ * Counterpart to scx_task_iter_unlock(). Locking @p's rq is optional. Once
+ * re-acquired, both locks are managed by the iterator from here on.
+ */
+static void scx_task_iter_relock(struct scx_task_iter *iter,
+				 struct task_struct *p)
+{
+	__scx_task_iter_maybe_relock(iter);
+	if (p) {
+		iter->rq = task_rq_lock(p, &iter->rf);
+		iter->locked_task = p;
+	}
+}
+
 /**
  * scx_task_iter_stop - Stop a task iteration and unlock scx_tasks_lock
  * @iter: iterator to exit
@@ -7064,6 +7082,9 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 		scx_task_iter_unlock(&sti);
 
 		ret = scx_init_task(sch, p, false);
+
+		scx_task_iter_relock(&sti, p);
+
 		if (ret) {
 			put_task_struct(p);
 			scx_task_iter_stop(&sti);
@@ -7076,15 +7097,12 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 		scx_set_task_state(p, SCX_TASK_READY);
 
 		/*
-		 * Insert into the tid hash under scx_tasks_lock so we can't
-		 * race sched_ext_dead() and leave a stale entry for an already
-		 * exited task.
+		 * Insert into the tid hash. scx_tasks_lock is held by the iter;
+		 * list_empty() guards against sched_ext_dead() having taken @p
+		 * off the list while init ran unlocked.
 		 */
-		if (scx_tid_to_task_enabled()) {
-			guard(raw_spinlock_irq)(&scx_tasks_lock);
-			if (!list_empty(&p->scx.tasks_node))
-				scx_tid_hash_insert(p);
-		}
+		if (scx_tid_to_task_enabled() && !list_empty(&p->scx.tasks_node))
+			scx_tid_hash_insert(p);
 
 		put_task_struct(p);
 	}
Signature · 1 line
2.54.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Dereferencing `p->comm` and `p->pid` in `scx_error()` after dropping the task reference and…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Dereferencing p->comm and p->pid in scx_error() after dropping the task reference and releasing locks causes a Use-After-Free. --

commit 3693e9669076c05d1b5519c1c28733cc325c69fb
Author: Tejun Heo <[email protected]>
sched_ext: Add scx_task_iter_relock() and use it in scx_root_enable_workfn()

This commit introduces scx_task_iter_relock() to re-acquire scx_tasks_lock and optionally the task's runqueue lock. It applies this new function in scx_root_enable_workfn() to properly manage locking during task initialization and hash insertion.

Quoted 4 lines
kernel/sched/ext.c +0 -0
index 1d7f206941f0c..2238bd005dc7b 100644

[ ... ]

kernel/sched/ext.c static void scx_root_enable_workfn(struct kthread_work *work) · line 7161

Quoted 10 lines
kernel/sched/ext.c +3 -0
@@ -7135,6 +7153,9 @@ static void scx_root_enable_workfn(struct kthread_work *work)
 		scx_task_iter_unlock(&sti);
 
 		ret = scx_init_task(sch, p, false);
+
+		scx_task_iter_relock(&sti, p);
+
 		if (ret) {
 			put_task_struct(p);
 			scx_task_iter_stop(&sti);

This regression wasn't introduced by this patch, but existed previously. Can this cause a use-after-free when scx_error() is called?

Looking at the error path just below this diff chunk:

    if (ret) {
        put_task_struct(p);
        scx_task_iter_stop(&sti);
        scx_error(sch, "ops.init_task() failed (%d) for %s[%d]",
                  ret, p->comm, p->pid);

If put_task_struct() drops the last reference to the task, and scx_task_iter_stop() drops the iterator locks (which unconditionally re-enables local interrupts via raw_spin_unlock_irq()), could an RCU grace period expire and physically free the task struct before scx_error() accesses p->comm and p->pid?

Would it be safer to move the put_task_struct() call until after the scx_error() formatting completes?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1