Skip to contentSkip to conversation

sched_ext: Fix proxy execution balance callbacks

PATCH New Andrea Righi 2 participants 9 messages Oct 20 – Oct 21

Subsystems (2)
lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH 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(-)
author reviewer
#1 lore raw

PATCH 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.

Fixes e2859566ffbc ("sched_ext: Queue the kick_sync wait from set_next_task_scx()")
---
 kernel/sched/sched.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/sched.h +2 -1
index 9e7993256c817..628765910934a 100644
@@ -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
reviewer Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] sched/core: Avoid requeueing a singleton balance callback

Quoted 8 lines · Andrea Righi

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.

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()")

Can you resend this to Peter? Please feel free to add

Thanks.

Signature · 1 line
tejun
reviewer
#3 lore raw

Re: [PATCH 1/3] sched/core: Avoid requeueing a singleton balance callback

In reply to Andrea Righi

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:

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

author reviewer
#4 lore raw

Re: [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->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.

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

author reviewer
#5 lore raw

PATCH 2/3 sched_ext: Use irq_work for deferred actions with proxy execution

In reply to Andrea Righi

maybe_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.

Fixes a8ad873113d3 ("sched_ext: defer queue_balance_callback() until after ops.dispatch")
---
 kernel/sched/ext/ext.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/sched/ext/ext.c +5 -4
index 5efc3ad1b1198..4b7d2602b4d40 100644
@@ -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
reviewer Applied
#6 lore raw

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

author reviewer
#7 lore raw

PATCH 3/3 sched_ext: Remove the foreign CPU check from the kick_sync wait

In reply to Andrea Righi

The 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
index 4b7d2602b4d40..a9b26fa431bf3 100644
@@ -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
reviewer Applied
#8 lore raw

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