sched_ext: Fix proxy execution balance callbacks
Subsystems (2)
Andrea Righi
reviewerPATCH 1/1 sched_ext: Fix proxy execution balance callbacks
This series addresses the three follow-ups you reported after applying "sched_ext: Queue the kick_sync wait from set_next_task_scx()" [1].
The first patch prevents queue_balance_callback() from requeueing a callback that is already the sole list entry. With proxy execution, SNT_REPICK followed by SNT_PICK can otherwise queue the kick_sync callback twice, linking it to itself and running the wait twice.
The second patch routes dispatch-time deferred actions through irq_work when proxy execution is enabled. Proxy resolution can zap balance callbacks and then select idle or another scheduling class, leaving no SCX dispatch to restore the discarded deferred callback.
The third patch removes the now-unreachable foreign CPU check from the kick_sync wait callback.
[1] https://lore.kernel.org/r/[email protected]
Andrea Righi (3):
sched/core: Avoid requeueing a singleton balance callback
sched_ext: Use irq_work for deferred actions with proxy execution
sched_ext: Remove the foreign CPU check from the kick_sync wait
kernel/sched/ext/ext.c | 18 +++++------------- kernel/sched/sched.h | 3 ++- 2 files changed, 7 insertions(+), 14 deletions(-)
Andrea Righi
reviewerPATCH 1/3 sched/core: Avoid requeueing a singleton balance callback
With proxy execution, __schedule() can call set_next_task_scx() with SNT_REPICK and then SNT_PICK for the same donor. Both calls queue the kick_sync balance callback while kick_sync_pending remains set.
When the callback is the only list entry, its next pointer is NULL, so the existing duplicate check misses it. Queueing it again links it to itself and causes the wait to run twice.
Also check whether the callback is already the head of the list to reject the second insertion.
--- kernel/sched/sched.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/sched.h +2 -1
@@ -2175,7 +2175,8 @@ queue_balance_callback(struct rq *rq,* balance_push() is active, see the comment with * balance_push_callback. */ -if (unlikely (head->next || rq->balance_callback == &balance_push_callback)) +if (unlikely (head->next || rq->balance_callback == head || + rq->balance_callback == &balance_push_callback))return ; head->func = func;
Signature · 1 line
2.56.0
Re: [PATCH 1/3] sched/core: Avoid requeueing a singleton balance callback
Quoted 8 lines · Andrea Righi
With proxy execution,
__schedule()can callset_next_task_scx()withSNT_REPICKand thenSNT_PICKfor the same donor. Both calls queue thekick_syncbalance callback whilekick_sync_pendingremains set.When the callback is the only list entry, its next pointer is NULL, so the existing duplicate check misses it. Queueing it again links it to itself and causes the wait to run twice.
Quoted 7 lines
Also check whether the callback is already the head of the list to reject the second insertion.
Fixes e2859566ffbc ("sched_ext: Queue the kick_sync wait from set_next_task_scx()")Reported-by Tejun Heo <[email protected]>Signed-off-by Andrea Righi <[email protected]>
Can you resend this to Peter? Please feel free to add
Thanks.
Signature · 1 line
tejun
Tejun Heo
reviewerRe: [PATCH 1/3] sched/core: Avoid requeueing a singleton balance callback
In reply to Andrea RighiHello, Andrea.
Quoted 3 lines · Andrea Righi
When the callback is the only list entry, its next pointer is NULL, so the existing duplicate check misses it. Queueing it again links it to itself and causes the wait to run twice.
The last entry of a longer list has the same problem. Its next is NULL and it isn't rq->balance_callback, so it can be queued again:
1. A is queued on an empty list. A->next is NULL.
2. B is queued. The list is B -> A.
3. A is queued again. A->next = B and the list becomes A -> B -> A.
do_balance_callbacks() then runs A twice. This doesn't affect kick_sync_bal_cb, as nothing gets queued between the SNT_REPICK and SNT_PICK calls.
Thanks.
--
tejun
Andrea Righi
reviewerRe: [PATCH 1/3] sched/core: Avoid requeueing a singleton balance callback
Hi Tejun,
Quoted 10 lines · Tejun Heo
Hello, Andrea.
Quoted 3 lines · Andrea Righi
When the callback is the only list entry, its next pointer is NULL, so the existing duplicate check misses it. Queueing it again links it to itself and causes the wait to run twice.
The last entry of a longer list has the same problem. Its next is NULL and it isn't
rq->balance_callback, so it can be queued again:
Quoted 7 lines
1. A is queued on an empty list.
A->nextis NULL.
2. B is queued. The list is B -> A.
3. A is queued again.A->next= B and the list becomes A -> B -> A.
do_balance_callbacks()then runs A twice. This doesn't affectkick_sync_bal_cb, as nothing gets queued between theSNT_REPICKandSNT_PICKcalls.
Ah yes, thanks for pointing that out. Agreed, the head check only covers the singleton case, but the tail-entry case remains.
I'll keep this fix scoped to the reported issue and address the duplicate detection for a separate follow-up with Peter.
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 2/3 sched_ext: Use irq_work for deferred actions with proxy execution
In reply to Andrea Righimaybe_queue_balance_callback() clears SCX_RQ_BAL_CB_PENDING when it queues the deferred callback. Proxy resolution can then zap the callback without running it, leaving the deferred actions unscheduled until a later wakeup or deferred request.
Use the existing irq_work fallback for dispatch-time deferred requests when proxy execution is enabled. This keeps the work scheduled even if proxy resolution retries the pick or falls back to idle, without relying on another SCX dispatch to restore a discarded callback.
--- kernel/sched/ext/ext.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/sched/ext/ext.c +5 -4
@@ -1129,9 +1129,10 @@ static void schedule_deferred_locked(struct rq *rq)return ;/* -* If in dispatch, the balance callbacks will be called before rq lock -* is released. Schedule one. -* +* If in dispatch without proxy execution, the balance callbacks will +* be called before rq lock is released. Schedule one. With proxy +* execution, proxy resolution can zap the callbacks without another +* SCX dispatch to requeue them, so use irq_work instead. * * We can't directly insert the callback into the * rq's list: The call can drop its lock and make the pending balance
@@ -1139,7 +1140,7 @@ static void schedule_deferred_locked(struct rq *rq)* * Just let dispatch_one() know that it must do it itself. */ -if (rq->scx.flags &SCX_RQ_IN_DISPATCH ) { +if ((rq->scx.flags &SCX_RQ_IN_DISPATCH ) && !sched_proxy_exec ()) { rq->scx.flags |=SCX_RQ_BAL_CB_PENDING ;return ; }
Signature · 1 line
2.56.0
Re: [PATCH 2/3] sched_ext: Use irq_work for deferred actions with proxy execution
Applied to sched_ext/for-7.4-fixes.
Maybe this can be optimized further in the future, but we should probably improve the balance_callback mechanism rather than trying to work around it from the ext side.
Thanks.
--
tejun
Andrea Righi
reviewerPATCH 3/3 sched_ext: Remove the foreign CPU check from the kick_sync wait
In reply to Andrea RighiThe kick_sync balance callback is now queued from set_next_task_scx() after the core-wide selection has finished. Proxy resolution zaps the callbacks before dropping the rq lock, so a foreign CPU cannot acquire the lock and flush this callback while it is queued.
Remove the unreachable foreign CPU check and its obsolete comment.
--- kernel/sched/ext/ext.c | 9 --------- 1 file changed, 9 deletions(-)
kernel/sched/ext/ext.c +0 -9
@@ -3733,15 +3733,6 @@ static void kick_sync_wait_bal_cb(struct rq *rq)bool waited;s32 cpu; -/* -* This callback is queued once the pick is final and flushed within -* @rq's own scheduling pass. If a lock taker on a foreign CPU ever -* flushes it, the snapshots are unrelated to that CPU. Don't get in the -* way and leave the wait pending for @rq's next pick. -*/ -if (unlikely (cpu_of (rq) !=smp_processor_id ())) -return ; -/* * Clear only when the wait actually runs. The callback can be dropped * without running (e.g., proxy exec zaps the balance callbacks and
Signature · 1 line
2.56.0
Re: [PATCH 3/3] sched_ext: Remove the foreign CPU check from the kick_sync wait
Applied to sched_ext/for-7.5.
Thanks.
--
tejun