Skip to content

scx_layered: Don't strand a task being enqueued to a draining LLC - #3976

Merged
kkdwvd merged 1 commit into
sched-ext:mainfrom
kkdwvd:layered-stall-fix
Nov 10, 2026
Merged

kkdwvd merged 1 commit into
sched-ext:mainfrom
kkdwvd:layered-stall-fix

Conversation

@kkdwvd

@kkdwvd kkdwvd commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Chuxiao Han (@CxhAkatsukI) is using Verus to support writing imperative scheduler code, and automate proofs of various scheduling properties (fairness, liveness, work conservation, etc.). While proving starvation freedom for the drain protocol logic, a bug came up where a task can be left stranded in a DSQ. The violation was proven using Verus and then verified using a reproducer. See the commit log for details.

The Verus model used in the discovery is available here.

A layer's DSQ on an LLC where the layer has no CPUs is drained only
while the LLC's bit is set in llcs_to_drain. layered_enqueue() sets the
bit after inserting a task when nr_llc_cpus is zero.
try_drain_layer_node_llcs() clears it when scx_bpf_dsq_nr_queued() says
the DSQ is about to be empty and sets it again if the DSQ is not empty
after consuming. Both sides rely on the inserted task being on the DSQ
before layered_enqueue() sets the bit.

However, it is not. scx_bpf_dsq_insert[_vtime]() from ops.enqueue() only
records the target DSQ, and the kernel queues the task after the
callback returns. A CPU that drains in between sees an empty DSQ on both
reads:

  layered_enqueue()                 try_drain_layer_node_llcs()
    scx_bpf_dsq_insert_vtime()
    nr_llc_cpus == 0, set bit
                                      nr_queued == 0, clear bit
                                      nothing to consume
                                      nr_queued == 0, bit stays clear
  kernel queues the task

The task is left on a DSQ that is not drained. It waits until something
unrelated sets the bit again or a slower fallback, cross-LLC migration
or antistall, picks it up. refresh_cpumasks() misses a task being
enqueued the same way when it checks whether an LLC that lost its CPUs
needs draining.

Keeping a confined layer's CPUs busy while a task keeps waking up into
the layer from an LLC the layer has no CPUs on reproduces it. Most runs
left a task unscheduled for 11 to 17 seconds until antistall ran it, or,
with --disable-antistall, ended in the watchdog aborting the scheduler
with "runnable task stall". No run did either with this change.

Count the tasks on or headed for each layer's LLC DSQ in
llc_ctx->nr_queued[], charged and uncharged along with queued_runtime[],
and base the three checks on it. The increment is an atomic op ahead of
the nr_llc_cpus test, so a task is counted before its enqueue can set
the bit and stays counted until it runs. A drainer that clears the bit
and still reads zero did so before the increment, so that enqueue sets
the bit after the clear.

The count still includes a task that was just consumed, up to its
ops.running(), so a count of one no longer means the DSQ is about to be
empty. Turn draining off when the count is zero instead, which takes one
more draining attempt than before.

Fixes: 56c1550 ("scx_layered: Implement empty LLC draining")
Reported-by: Chuxiao Han <hanchuxiao23@berkeley.edu>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
@kkdwvd
kkdwvd requested a review from htejun November 10, 2026 00:56
@kkdwvd
kkdwvd added this pull request to the merge queue Nov 10, 2026
Merged via the queue into sched-ext:main with commit b5a8841 Nov 10, 2026
13 of 14 checks passed
@kkdwvd
kkdwvd deleted the layered-stall-fix branch November 10, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants