History

sched_ext: Skip past-sched_ext_dead() tasks in scx_task_iter_next_locked()

scx_task_iter's cgroup-scoped mode can return tasks whose sched_ext_dead() has already completed: cgroup_task_dead() removes from cset->tasks after sched_ext_dead() in finish_task_switch() and is irq-work deferred on PREEMPT_RT. The global mode is fine - sched_ext_dead() removes from scx_tasks via list_del_init() first. Callers (sub-sched enable prep/abort/apply, scx_sub_disable(), scx_fail_parent()) assume returned tasks are still on @sch and trip WARN_ON_ONCE() or operate on torn-down state otherwise. Set %SCX_TASK_OFF_TASKS in sched_ext_dead() under @p's rq lock and have scx_task_iter_next_locked() skip flagged tasks under the same lock. Setter and reader serialize on the per-task rq lock - no race.

Tejun Heo committed 6 months ago

Commit ff9eda41 parent 60f21a22 files · +26 −8Browse files

Unified · Split

2 files
  1. include/linux/sched/ext.h +1 −0
  2. kernel/sched/ext.c +25 −8
include/linux/sched/ext.h+1−0
@@ -101,6 +101,7 @@ enum scx_ent_flags { · 100 unmodified lines
101101 SCX_TASK_DEQD_FOR_SLEEP = 1 << 3, /* last dequeue was for SLEEP */
102102 SCX_TASK_SUB_INIT = 1 << 4, /* task being initialized for a sub sched */
103103 SCX_TASK_IMMED = 1 << 5, /* task is on local DSQ with %SCX_ENQ_IMMED */
104+ SCX_TASK_OFF_TASKS = 1 << 6, /* removed from scx_tasks by sched_ext_dead() */
104105
105106 /*
106107 * Bits 8 and 9 are used to carry task state:
kernel/sched/ext.c+25−8
@@ -928,16 +928,27 @@ static struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter) · 927 unmodified lines
928928 *
929929 * Test for idle_sched_class as only init_tasks are on it.
930930 */
931− if (p->sched_class != &idle_sched_class)
932− break;
933− }
934− if (!p)
935− return NULL;
931+ if (p->sched_class == &idle_sched_class)
932+ continue;
936933
937− iter->rq = task_rq_lock(p, &iter->rf);
938− iter->locked_task = p;
934+ iter->rq = task_rq_lock(p, &iter->rf);
935+ iter->locked_task = p;
939936
940− return p;
937+ /*
938+ * cgroup_task_dead() removes the dead tasks from cset->tasks
939+ * after sched_ext_dead() and cgroup iteration may see tasks
940+ * which already finished sched_ext_dead(). %SCX_TASK_OFF_TASKS
941+ * is set by sched_ext_dead() under @p's rq lock. Test it to
942+ * avoid visiting tasks which are already dead from SCX POV.
943+ */
944+ if (p->scx.flags & SCX_TASK_OFF_TASKS) {
945+ __scx_task_iter_rq_unlock(iter);
946+ continue;
947+ }
948+
949+ return p;
950+ }
951+ return NULL;
941952 }
942953
943954 /**
@@ -3850,6 +3861,11 @@ void sched_ext_dead(struct task_struct *p) · 2906 unmodified lines
38503861 /*
38513862 * @p is off scx_tasks and wholly ours. scx_root_enable()'s READY ->
38523863 * ENABLED transitions can't race us. Disable ops for @p.
3864+ *
3865+ * %SCX_TASK_OFF_TASKS synchronizes against cgroup task iteration - see
3866+ * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup
3867+ * iteration is only used from sub-sched paths, which require root
3868+ * enabled. Root enable transitions every live task to at least READY.
38533869 */
38543870 if (scx_get_task_state(p) != SCX_TASK_NONE) {
38553871 struct rq_flags rf;
@@ -3857,6 +3873,7 @@ void sched_ext_dead(struct task_struct *p) · 1 unmodified line
38573873
38583874 rq = task_rq_lock(p, &rf);
38593875 scx_disable_and_exit_task(scx_task_sched(p), p);
3876+ p->scx.flags |= SCX_TASK_OFF_TASKS;
38603877 task_rq_unlock(rq, p, &rf);
38613878 }
38623879 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t