sched_ext: Backport two fixes dropped from 7.3.7-rc1
Subsystems (2)
cover sched_ext: Backport two fixes dropped from 7.3.7-rc1
These are 7.3 backports of two sched_ext fixes dropped from 7.3.7-rc2 because their rc1 versions failed to build [1][2].
- Patch1: factor scx_reset_task_fields() out of scx_disable_task() instead of taking a298fd5f4ee8.
- Patch2: drop the __SCX_DSQ_ITER_HAS_REENQ context line, so
b3768d058c98, which adds a new kfunc, isn't needed.
Build tested on 7.3.7-rc2 with CONFIG_SCHED_CLASS_EXT=y.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
Kang Chen (1):
sched_ext: make DSQ iterator stay drained after NULL
Rahad Bhuiya (1):
sched_ext: Reset task fields on exit of an SCX_TASK_READY task
kernel/sched/ext/ext.c | 57 +++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 18 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/2 sched_ext: Reset task fields on exit of an SCX_TASK_READY task
From: Rahad Bhuiya <[email protected]>
commit 564405c424f49d70b13debe7fe5b550234ad6b84 upstream.
Tasks in SCX_TASK_READY are attached to a scheduler but outside the ext class, such as RT/DL tasks, or non-SCHED_EXT tasks under a partial switch. The scheduler's BPF code can still update their dsq_vtime and slice (for instance, via ops.cgroup_move() as done by scx_flatcg).
Because __scx_disable_and_exit_task() currently breaks out of the state switch for SCX_TASK_READY without calling scx_reset_task_fields(), these fields are not cleared when the task leaves the scheduler. If the task is subsequently moved to another cgroup/sub-scheduler and later switches to SCHED_NORMAL, the new scheduler's ops.enable() sees stale dsq_vtime from the previous scheduler's clock.
Call scx_reset_task_fields() in the SCX_TASK_READY case of __scx_disable_and_exit_task() to ensure SCX-managed fields are properly reset when leaving the scheduler.
[ chengyang: 7.3 lacks scx_reset_task_fields(), so factor it out of
scx_disable_task() ]
--- kernel/sched/ext/ext.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-)
kernel/sched/ext/ext.c +24 -16
@@ -4159,6 +4159,27 @@ void scx_enable_task(struct scx_sched *sch, struct task_struct *p)scx_set_task_state (p,SCX_TASK_ENABLED ); } +/* +* Reset the SCX-managed fields when @p leaves the BPF scheduler's control. +*/ +static void scx_reset_task_fields (struct rq *rq,struct task_struct *p) +{ +lockdep_assert_rq_held (rq); + + p->scx.dsq_vtime =0 ; +scx_task_slice_ended (rq, p); +scx_set_task_slice (p,0 ); + p->scx.reenq_cnt =0 ; + +/* +* If @p is runnable but not running, its wait started at runnable_at, +* which ops_dequeue() didn't reset. Leaving the scheduler ends the +* wait. Reset runnable_at the next time @p is enqueued on SCX, so that +* the watchdog doesn't count the time @p spends elsewhere. +*/ + p->scx.flags |=SCX_TASK_RESET_RUNNABLE_AT ; +} +static void scx_disable_task (struct scx_sched *sch,struct task_struct *p) {struct rq *rq =task_rq (p);
@@ -4172,22 +4193,8 @@ static void scx_disable_task(struct scx_sched *sch, struct task_struct *p)SCX_CALL_OP_TASK (sch, disable, rq, p);scx_set_task_state (p,SCX_TASK_READY ); -/* -* Reset the SCX-managed fields when @p leaves the BPF scheduler's -* control, after ops.disable() has observed their final values. -*/ - p->scx.dsq_vtime =0 ; -scx_task_slice_ended (rq, p); -scx_set_task_slice (p,0 ); - p->scx.reenq_cnt =0 ; - -/* -* If @p is runnable but not running, its wait started at runnable_at, -* which ops_dequeue() didn't reset. Leaving @sch ends the wait. Reset -* runnable_at the next time @p is enqueued on SCX, so that the watchdog -* doesn't count the time @p spends elsewhere. -*/ - p->scx.flags |=SCX_TASK_RESET_RUNNABLE_AT ; +/* after ops.disable() has observed the final values */ +scx_reset_task_fields (rq, p);/* * Verify the task is not in BPF scheduler's custody. If flag
@@ -4213,6 +4220,7 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p) args.cancelled = true;break ;case SCX_TASK_READY : +scx_reset_task_fields (task_rq (p), p);break ;case SCX_TASK_ENABLED :scx_disable_task (sch, p);
Signature · 1 line
2.56.0
PATCH 2/2 sched_ext: make DSQ iterator stay drained after NULL
In reply to Cheng-Yang ChouFrom: Kang Chen <[email protected]>
commit fcbff7b56a881bbe041b6c5ba12831a3c1ac1bb4 upstream.
nldsq_cursor_next_task() unlinks the iteration cursor when it reaches the end of the DSQ. A later call sees list_empty(&cursor->node), starts from the DSQ head again, and returns the first task of the same iteration.
The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted DSQ iteration therefore hands the BPF program a task pointer while the verifier believes R0 == 0.
Remember the drained state in the cursor flags and return NULL on every later next() call. The cursor has spare flag bits and the BPF-visible iterator layout is unchanged.
--- kernel/sched/ext/ext.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
kernel/sched/ext/ext.c +15 -2
@@ -492,11 +492,13 @@ enum scx_dsq_iter_flags {/* iterate in the reverse dispatch order */ SCX_DSQ_ITER_REV =1U <<16 , + __SCX_DSQ_ITER_DRAINED =1U <<28 , __SCX_DSQ_ITER_HAS_SLICE =1U <<30 , __SCX_DSQ_ITER_HAS_VTIME =1U <<31 , __SCX_DSQ_ITER_USER_FLAGS =SCX_DSQ_ITER_REV , __SCX_DSQ_ITER_ALL_FLAGS = __SCX_DSQ_ITER_USER_FLAGS | + __SCX_DSQ_ITER_DRAINED | __SCX_DSQ_ITER_HAS_SLICE | __SCX_DSQ_ITER_HAS_VTIME, };
@@ -10255,13 +10257,24 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, __bpf_kfunc struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) {struct bpf_iter_scx_dsq_kern *kit = (void *)it; +struct task_struct *p; -if (!kit->dsq) +if (!kit->dsq || (kit->cursor.flags & __SCX_DSQ_ITER_DRAINED))return NULL ;guard (raw_spinlock_irqsave)(&kit->dsq->lock); -return nldsq_cursor_next_task (&kit->cursor, kit->dsq); + p =nldsq_cursor_next_task (&kit->cursor, kit->dsq); +/* +* nldsq_cursor_next_task() unlinks the cursor and starts from the DSQ +* head when it is called again. The verifier relies on an iterator +* which returned NULL to keep returning NULL (see +* process_iter_next_call()), so remember the drained state. +*/ +if (!p) + kit->cursor.flags |= __SCX_DSQ_ITER_DRAINED; + +return p; }/**
Signature · 1 line
2.56.0
Re: [PATCH 7.3.y 2/2] sched_ext: make DSQ iterator stay drained after NULL
Thanks for fixing the 7.3 backport and cleanly factoring out scx_reset_task_fields().
Quoted 77 lines · Cheng-Yang Chou
From: Kang Chen <[email protected]>
commit
fcbff7b56a881bbe041b6c5ba12831a3c1ac1bb4upstream.
nldsq_cursor_next_task()unlinks the iteration cursor when it reaches the end of the DSQ. A later call seeslist_empty(&cursor->node), starts from the DSQ head again, and returns the first task of the same iteration.The verifier relies on the iterator contract that once
next()returns NULL it keeps returning NULL:process_iter_next_call()marks the iterator DRAINED and treats the result of laternext()calls as the constant zero. A restarted DSQ iteration therefore hands the BPF program a task pointer while the verifier believes R0 == 0.Remember the drained state in the cursor flags and return NULL on every later
next()call. The cursor has spare flag bits and the BPF-visible iterator layout is unchanged.Fixes 650ba21b131e ("sched_ext: Implement DSQ iterator")Signed-off-by Kang Chen <[email protected]>Signed-off-by Tejun Heo <[email protected]>[ chengyang: drop __SCX_DSQ_ITER_HAS_REENQ, which 7.3 lacks ]Signed-off-by Cheng-Yang Chou <[email protected]>--- kernel/sched/ext/ext.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)kernel/sched/ext/ext.c +15
-3@@ -492,11 +492,13 @@ enum scx_dsq_iter_flags {/* iterate in the reverse dispatch order */ SCX_DSQ_ITER_REV =1U <<16 , + __SCX_DSQ_ITER_DRAINED =1U <<28 , __SCX_DSQ_ITER_HAS_SLICE =1U <<30 , __SCX_DSQ_ITER_HAS_VTIME =1U <<31 , __SCX_DSQ_ITER_USER_FLAGS =SCX_DSQ_ITER_REV , __SCX_DSQ_ITER_ALL_FLAGS = __SCX_DSQ_ITER_USER_FLAGS | + __SCX_DSQ_ITER_DRAINED | __SCX_DSQ_ITER_HAS_SLICE | __SCX_DSQ_ITER_HAS_VTIME, };@@ -10255,13 +10257,24 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, __bpf_kfunc struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) {struct bpf_iter_scx_dsq_kern *kit = (void *)it; +struct task_struct *p; -if (!kit->dsq) +if (!kit->dsq || (kit->cursor.flags & __SCX_DSQ_ITER_DRAINED))return NULL ;guard (raw_spinlock_irqsave)(&kit->dsq->lock); -return nldsq_cursor_next_task (&kit->cursor, kit->dsq); + p =nldsq_cursor_next_task (&kit->cursor, kit->dsq); +/* +* nldsq_cursor_next_task() unlinks the cursor and starts from the DSQ +* head when it is called again. The verifier relies on an iterator +* which returned NULL to keep returning NULL (see +* process_iter_next_call()), so remember the drained state. +*/ +if (!p) + kit->cursor.flags |= __SCX_DSQ_ITER_DRAINED; + +return p; }/** -- 2.56.0