Repository navigation
scx_layered: Don't strand a task being enqueued to a draining LLC - #3976
Merged
Merged
Conversation
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>
htejun
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.