Skip to contentSkip to conversation

sched_ext: Avoid unnecessary IMMED reenqueues on idle CPUs

PATCH Applied Andrea Righi 2 participants 2 messages Oct 29

Subsystems (2)
lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH v2 1/1 sched_ext: Avoid unnecessary IMMED reenqueues on idle CPUs

When two IMMED tasks wake on an idle CPU, the kernel can unnecessarily send both back to BPF instead of keeping the first task ready to run.

The first wakeup inserts a task into the local DSQ and raises rq->next_class to ext before the CPU leaves idle. The second insertion queues a reenqueue scan. rq_is_open() sees next_class == ext and reports the CPU unavailable, although it is still idle. The scan then reenqueues the head along with the task behind it, losing a placement that could have been honored on the next pick.

Report the rq as open while the CPU is idle, even if a local insertion has already raised next_class to ext. This keeps the local DSQ head through the reenqueue scan while IMMED tasks behind it are returned to BPF.

A local reproducer runs in a four-vCPU virtme-ng guest: two futex waiters pinned to CPU 0 are woken together from CPU 2, and the BPF scheduler inserts their wakeups into the local DSQ with SCX_ENQ_IMMED. Across 10000 pairs, kernel instrumentation counts 9776 unnecessary head reenqueues while the CPU is idle and next_class is ext. Keeping the idle CPU open eliminates these head reenqueues.

Fixes 98d709cba319 ("sched_ext: Implement SCX_ENQ_IMMED")

---
Changes in v2:
- Remove the core-scheduling cookie check (Tejun Heo).
- Remove the independent skipped-head bookkeeping fix (Tejun Heo).
- Link to v1:
https://lore.kernel.org/r/[email protected]

 kernel/sched/ext/ext.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/ext/ext.c +8 -0
index 1b098728676f3..8862e81b63157 100644
@@ -461,6 +461,14 @@ static bool rq_is_open(struct rq *rq, u64 enq_flags)
 	 * @rq is either picking, in transition to, or running an SCX task.
 	 */
 
+	/*
+	 * A local insertion can raise next_class to ext while the donor is
+	 * still idle. Keep the rq open so that the local DSQ head is not
+	 * unnecessarily reenqueued before the CPU can pick it.
+	 */
+	if (rq->donor == rq->idle)
+		return true;
+
 	/*
 	 * If we're in the dispatch path holding rq lock, $donor may or may not
 	 * be ready depending on whether the on-going dispatch decides to extend
Signature · 1 line
2.56.0
reviewer Applied
#1 lore raw

Applied to sched_ext/for-7.4-fixes.

Thanks.

--
tejun