From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
To: Boris Burkov <boris@bur.io>
Cc: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH v2 0/4] btrfs: scrub: support low bwlimits
Date: Fri, 4 Dec 2026 01:27:20 -0500 [thread overview]
Message-ID: <axJdSAXfqATtejwq@hungrycats.org> (raw)
In-Reply-To: <e1054d9ed9329c82ddf8466ad29af01e95c146dc.1796087540.git.boris@bur.io>
On Mon, Nov 30, 2026 at 05:14:32PM -0800, Boris Burkov wrote:
> Improve the scrub throttling semantics by implementing it as a cancel
> aware sleep wrapped in a pause.
TL;DR there is a pre-existing scrub/commit/discard race that this patch
series makes worse, resulting in device replace failures and spurious
scrub errors. My LLM agent found it when checking this patch against
fixes for one of the existing bugs, then discovered the other issues
while testing this patch.
This patch puts the throttle sleep in a pause, so a commit can run while
stripes are queued, and the throttle holds the remaining reads back long
enough for the commit to discard their blocks.
Scrub cancel doesn't interrupt the pause, so under steady commits a cancel
can be held off for minutes. Making the pause return on cancel fixes that,
but the scrub then submits the rest of its queued reads while a commit
may still be running, which can hit the same read-after-discard failure.
Test failures hit 60-80% with a 32 MiB/s limit--not even particularly slow.
discard=async lowers the failure rate, but it's still not zero.
[curated LLM analysis follows]
The pause lets a transaction commit run while the scrub still holds stripes
it filled from the old commit root that it has not read yet.
scrub_throttle_dev_io() is called from submit_initial_group_read(),
which is reached from queue_scrub_stripe() and flush_scrub_stripes().
With patch 4/4 the sleep happens between the stripes of a group:
the stripes before it are submitted, and the ones after it were
filled by scrub_find_fill_first_stripe() from the commit root but are
not submitted yet. 1/4 drops the paths at the pause, but not those
stripes. During the sleep, a commit can switch the commit roots and
run btrfs_finish_extent_commit(), which unpins the extents freed in that
transaction. The block group is read-only, so the space is not reused,
but it is discarded.
With discard=sync, that discard happens before btrfs_scrub_continue(),
so the scrub reads zeros for the stripes of the group that come after
the sleep. On for-next 4e4ea25ed954 vs 1382d88402c0 (patch 1/4 only),
discard=sync, 32MiB/s limit, files deleted ahead of the scrub: scrub
failed 6/10 and replace 8/10 test runs on the whole series, 0/10 with
patch 1/4 only; 0/10 with no limit or nodiscard.
The failures are what you'd expect from reading zeros: "has bad bytenr,
has 0" on tree blocks, data csum errors, uncorrectable errors on a
healthy filesystem, and dev-replace canceled with -EIO on the first
bad metadata sector. A bpftrace probe on the discard and the reads
put every logged bad block inside a range discarded while the reading
thread was paused in scrub_throttle_sleep(), and read 0.4-23.5ms after
the discard. None of them fell in a commit at the existing pause point
in scrub_simple_mirror().
With discard=async it is rarer, but it does not go away. Async discard
only puts partly used block groups on the discard list if they hold
only data, so the exposure is data blocks, not metadata. do_trimming()
discards free space in read-only block groups too, and a block group on
the list is trimmed when its discard_eligible_time comes up. That is
now + BTRFS_DISCARD_DELAY (120s) when the group joins the list, and again
when a discard pass over it finishes. So whether 120s protects the
scrub depends on when the group last joined the list, not on when this
commit freed the extents. If the deadline is already partly used up,
or a pass is running on that group, the extents freed by this commit
can be trimmed well under 120s later, or within about a second.
The throttle is what makes the scrub slow. The stripes after the
sleep are read one throttle interval apart, and each interval is
another pause and another commit. The last stripe of a group is read
up to 7 * 64KiB / limit after it was filled, counted from the commit
root it was filled from. Patch 3/4 allows limits below 512KiB/s with
no floor. So at about 3.7KiB/s that is past a fresh 120s deadline,
and with a deadline that is already partly used up it takes much less.
Anything else that holds the scrub thread up adds to that. In a test
with discard=async, a block group queued before the scrub reached it,
and the limit dropped to 64KiB/s on data, the whole series failed 1 of 8
runs (data csum errors, uncorrectable). The bad sectors were trimmed at
the group's first eligible pass, about 120s after it was queued, while
the scrub was in the throttle sleep, and read 8s later.
The async tests also showed a second problem. When commits run back to
back (file deletion plus a sync every 50ms, about 6 commits per second),
both scrub threads can stop reading for minutes: 2 of 8 runs in the
first test, and all 5 runs in a test that cancels a scrub making no
progress for 20s, with stalls of 60-205s. For the whole time they were
in __scrub_blocked_if_needed() called from scrub_throttle_sleep(), i.e.
the scrub_pause_off() at the end of the sleep, waiting for scrub_pause_req
to drop to zero. None of the commits had to wait for the scrub, because
it was already counted as paused.
__scrub_blocked_if_needed() does not check the cancel flags, so a cancel
issued during a stall took effect only when the stall ended, 42-188s
late. On patch 1/4 only, every cancel returned within a second. So
under steady commit load, the paused sleep can starve the scrub and
delay the cancel this patch is meant to speed up.
The same stale-stripe window exists without this series at the existing
pause point in scrub_simple_mirror(), where stripes are queued but
not read. I reproduced that on v7.3.3 with discard=sync and fixed it
in our tree by finishing the queued stripes before pausing. That fix
does not carry over here, because this sleep is reached from inside the
submission and the flush. A possible solution is to move the sleep out
of the submission path, to the top of the scrub_simple_mirror() loop:
- charge each stripe against the limit when it is submitted, but never sleep
there;
- at the top of the loop, sleep without pausing, and wake on a cancel or on
scrub_pause_req. btrfs_scrub_pause() would need a wake_up() on
scrub_pause_wait for that;
- when woken by a pause request, finish the queued stripes, drop the paths,
and then pause.
A commit then waits only for the reads already queued (which are
submitted at device speed because nothing sleeps in the submission),
and not for the throttle sleep. That keeps the goal of this patch,
and the scrub is not counted as paused while it sleeps, so commits
cannot pass it indefinitely. To keep patch 4/4's per-stripe pacing,
submit each stripe as soon as it is queued when the device has a limit.
Separately, __scrub_blocked_if_needed() should return on a cancel. The
agent tested that on top of the whole series: it wakes on scrub_cancel_req
or the sctx's cancel_req and returns. In 19 stalls like the ones above,
both scrub threads returned 10-26ms after the cancel, against 42-188s
on the series alone. But the scrub then leaves the pause while the
commit that kept it there may still be running, and it went on to
submit the rest of the group, 4-16 stripes per run, all filled before
that commit. Those reads can report errors that are not on the disk.
So the canceled scrub also has to drop the stripes it has not submitted:
complete them without reading, reporting, writing them to a replace
target, or advancing last_physical.
With both changes, no read was submitted after the cancel in 10 runs.
next prev parent reply other threads:[~2026-12-04 6:27 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 1:14 [PATCH v2 0/4] btrfs: scrub: support low bwlimits Boris Burkov
2026-12-01 1:14 ` [PATCH v2 1/4] btrfs: scrub: release the commit root paths before pausing Boris Burkov
2026-12-01 1:14 ` [PATCH v2 2/4] btrfs: scrub: cancellable pause during throttle sleep Boris Burkov
2026-12-04 6:27 ` Zygo Blaxell [this message]
2026-12-01 1:14 ` [PATCH v2 3/4] btrfs: scrub: handle bwlimit below 512KiB Boris Burkov
2026-12-01 1:14 ` [PATCH v2 4/4] btrfs: scrub: throttle per stripe instead of per stripe group Boris Burkov
2026-12-01 21:17 ` [PATCH v2 0/4] btrfs: scrub: support low bwlimits Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axJdSAXfqATtejwq@hungrycats.org \
--to=ce3g8jdj@umail.furryterror.org \
--cc=boris@bur.io \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox