sched/ext: Avoid null ptr traversal when ->put_prev_task() is called with NULL next
Subsystems (2)
RFC PATCH 1/1 sched/ext: Avoid null ptr traversal when ->put_prev_task() is called with NULL next
Early when trying to get sched_ext and proxy-exe working together, I kept tripping over NULL ptr in put_prev_task_scx() on the line:
if (sched_class_above(&ext_sched_class, next->sched_class)) {
Which was due to put_prev_task() passes a NULL next, calling:
prev->sched_class->put_prev_task(rq, prev, NULL);
put_prev_task_scx() already guards for a NULL next in the switch_class case, but doesn't seem to have a guard for sched_class_above() check.
I can't say I understand why this doesn't trip usually without proxy-exec. And in newer kernels there are way fewer put_prev_task(), and I can't easily reproduce the issue now even with proxy-exec.
But we still have one put_prev_task() call left in core.c that seems like it could trip this, so I wanted to send this out for consideration.
---
Cc: Joel Fernandes <[email protected]>
Cc: Qais Yousef <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>
Cc: Vincent Guittot <[email protected]>
Cc: Dietmar Eggemann <[email protected]>
Cc: Valentin Schneider <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Ben Segall <[email protected]>
Cc: Zimuzo Ezeozue <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: "Paul E. McKenney" <[email protected]>
Cc: Metin Kaya <[email protected]>
Cc: Xuewen Yan <[email protected]>
Cc: K Prateek Nayak <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Daniel Lezcano <[email protected]>
Cc: Suleiman Souhlal <[email protected]>
Cc: kuyo chang <[email protected]>
Cc: hupu <[email protected]>
Cc: Tejun Heo <[email protected]>
Cc: David Vernet <[email protected]>
Cc: Andrea Righi <[email protected]>
Cc: Changwoo Min <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- kernel/sched/ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c +1 -1
@@ -2402,7 +2402,7 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,* ops.enqueue() that @p is the only one available for this cpu, * which should trigger an explicit follow-up scheduling event. */ -if (sched_class_above (&ext_sched_class, next->sched_class)) { +if (next &&sched_class_above (&ext_sched_class, next->sched_class)) {WARN_ON_ONCE (!(sch->ops.flags &SCX_OPS_ENQ_LAST ));do_enqueue_task (rq, p,SCX_ENQ_LAST ,-1 ); }else {
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Quoted 16 lines · John Stultz
Early when trying to get
sched_extand proxy-exe working together, I kept tripping over NULL ptr input_prev_task_scx()on the line:
if (sched_class_above(&ext_sched_class, next->sched_class)) {Which was due to
put_prev_task()passes a NULL next, calling:
prev->sched_class->put_prev_task(rq, prev, NULL);
put_prev_task_scx()already guards for a NULL next in theswitch_classcase, but doesn't seem to have a guard forsched_class_above()check.I can't say I understand why this doesn't trip usually without proxy-exec. And in newer kernels there are way fewer
put_prev_task(), and I can't easily reproduce the issue now even with proxy-exec.
Quoted 5 lines
But we still have one
put_prev_task()call left incore.cthat seems like it could trip this, so I wanted to send this out for consideration.Signed-off-by John Stultz <[email protected]>
This looks like a valid fix to me. If the task changes any sched property while it's running, we go through sched_change_begin() which calls put_prev_task() that always passes NULL as the next parameter:
static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
{
WARN_ON_ONCE(rq->donor != prev);
prev->sched_class->put_prev_task(rq, prev, NULL);
}This should be the code path(s) to trigger the bug:
sys_setpriority() / sched_setaffinity() / sched_setscheduler()
- set_user_nice() / __sched_setaffinity() / __sched_setscheduler()
- scoped_guard(sched_change, p, DEQUEUE_SAVE)
- sched_change_begin(p, DEQUEUE_SAVE)
- if (ctx->running)
put_prev_task(rq, p)
- prev->sched_class->put_prev_task(rq, prev, NULL)
- put_prev_task_scx(rq, prev, NULL)
- if (sched_class_above(&ext_sched_class, next->sched_class)) ^^^^ NULL dereference
Thanks,
-Andrea
Quoted 51 lines
---
Cc: Joel Fernandes <[email protected]>
Cc: Qais Yousef <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>
Cc: Vincent Guittot <[email protected]>
Cc: Dietmar Eggemann <[email protected]>
Cc: Valentin Schneider <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Ben Segall <[email protected]>
Cc: Zimuzo Ezeozue <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: "Paul E. McKenney" <[email protected]>
Cc: Metin Kaya <[email protected]>
Cc: Xuewen Yan <[email protected]>
Cc: K Prateek Nayak <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Daniel Lezcano <[email protected]>
Cc: Suleiman Souhlal <[email protected]>
Cc: kuyo chang <[email protected]>
Cc: hupu <[email protected]>
Cc: Tejun Heo <[email protected]>
Cc: David Vernet <[email protected]>
Cc: Andrea Righi <[email protected]>
Cc: Changwoo Min <[email protected]>
Cc: [email protected]
Cc: [email protected]--- kernel/sched/ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/ext.c +1
-1@@ -2402,7 +2402,7 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,* ops.enqueue() that @p is the only one available for this cpu, * which should trigger an explicit follow-up scheduling event. */ -if (sched_class_above (&ext_sched_class, next->sched_class)) { +if (next &&sched_class_above (&ext_sched_class, next->sched_class)) {WARN_ON_ONCE (!(sch->ops.flags &SCX_OPS_ENQ_LAST ));do_enqueue_task (rq, p,SCX_ENQ_LAST ,-1 ); }else {Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Hello John,
Quoted 4 lines · John Stultz
I can't say I understand why this doesn't trip usually without proxy-exec. And in newer kernels there are way fewer
put_prev_task(), and I can't easily reproduce the issue now even with proxy-exec.
That's probably because put_prev_task_scx() with next == NULL is always preceded by a dequeue, clearing SCX_TASK_QUEUED from p->scx.flags, so we don't reach the problematic sched_class_above() check because it only happens when the flag is set.
Quoted 3 lines
But we still have one
put_prev_task()call left incore.cthat seems like it could trip this, so I wanted to send this out for consideration.
I'm assuming you're referring to the one in sched_change_begin(). It looks like it's impossible for an outside observer holding a CPU's rq lock to observe a task that is running on that CPU and isn't queued, i.e. 'running' implies 'queued' (I'm new to the scheduler so I may be wrong here). That would explain why dequeue_task() is always called before put_prev_task(). Does proxy execution break that assumption?
Best,
Kuba
Quoted 3 lines · Kuba Piecuch
It looks like it's impossible for an outside observer holding a CPU's rq lock to observe a task that is running on that CPU and isn't queued, i.e. 'running' implies 'queued' (I'm new to the scheduler so I may be wrong here).
A task that blocks in __schedule() can drop the rq lock while picking the next task, which is after try_to_block_task() dequeues prev. So it's very much possible for a task on another CPU to grab the rq lock and observe prev as dequeued but still running.
Quoted 9 lines · Kuba Piecuch
Quoted 3 lines · Kuba Piecuch
It looks like it's impossible for an outside observer holding a CPU's rq lock to observe a task that is running on that CPU and isn't queued, i.e. 'running' implies 'queued' (I'm new to the scheduler so I may be wrong here).
A task that blocks in
__schedule()can drop the rq lock while picking the next task, which is aftertry_to_block_task()dequeues prev. So it's very much possible for a task on another CPU to grab the rq lock and observe prev as dequeued but still running.
Even with that, I'm not convinced that it's possible to do a NULL deref with the current code.
In order for sched_change_begin() to do the NULL deref in put_prev_task_scx(), we would need to have:
* rq->donor == p (for sched_change_begin() to call put_prev_task())
* p->on_rq != TASK_ON_RQ_QUEUED
(for sched_change_begin() to not call dequeue_task() beforehand)
* p->scx.flags & SCX_TASK_QUEUED
(for put_prev_task_scx() to enter the branch with the @next deref)
From a brief survey of the code, __assuming proxy execution is disabled__, I don't think it's possible for a remote task holding @rq's lock to observe the second and third condition to be true.
Every time p->on_rq is changed away from TASK_ON_RQ_QUEUED, it happens under the rq lock and is paired with a dequeue (see block_task(), deactivate_task()). dequeue_task_scx() always clears SCX_TASK_QUEUED from p->scx.flags.
Every time SCX_TASK_QUEUED is set in p->scx.flags (i.e. enqueue_task_scx() is called), it happens under the rq lock and is either gated by p->on_rq == TASK_ON_RQ_QUEUED (see ttwu_runnable(), sched_change_end()) or is paired with p->on_rq being set to TASK_ON_RQ_QUEUED (see activate_task()). It also happens in proxy_tag_curr(), which is a no-op if proxy execution is disabled. Even when it's enabled, proxy_tag_curr() does a dequeue-enqueue cycle while holding the rq lock, which doesn't look dangerous.
I'm not trying to say that we shouldn't add a NULL check, all this is just for my own understanding.
Applied to sched_ext/for-6.19-fixes with a note.
Thanks.
--
tejun