* [PATCH v2 1/4] btrfs: scrub: release the commit root paths before pausing
2026-12-01 1:14 [PATCH v2 0/4] btrfs: scrub: support low bwlimits Boris Burkov
@ 2026-12-01 1:14 ` Boris Burkov
2026-12-01 1:14 ` [PATCH v2 2/4] btrfs: scrub: cancellable pause during throttle sleep Boris Burkov
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Boris Burkov @ 2026-12-01 1:14 UTC (permalink / raw)
To: linux-btrfs, kernel-team
Since commit 1dc4888e725d ("btrfs: scrub: avoid unnecessary extent tree
search preparing stripes") scrub keeps sctx->extent_path and
sctx->csum_path across the stripes of a block group, so the paths are
also held across the implementation of scrub pause in scrub_simple_mirror()
and scrub_raid56_parity_stripe().
Both use searches that set search_commit_root and skip_locking to true.
Meanwhile, the transaction critical section is exactly what issues a pause.
So if we populate sctx->extent_path/csum_path with nodes from a commit root,
then pause, then complete a commit critical section, those nodes refer
to the previous commit root, by construction.
These nodes could have been cowed and thus unpinned by the conclusion
of the transaction. It is generally not safe to refer to nodes from old
commit roots.
Therefore, we must drop these paths across pauses to ensure that after a
pause we do a new search in the current commit root. Otherwise we would
need to block the commit on the scrub which is obviously unacceptable.
This issue becomes particularly nasty with dev-replace scrubs which can
then try to use an invalid reallocated eb to populate the new device.
Fixes: 1dc4888e725d ("btrfs: scrub: avoid unnecessary extent tree search preparing stripes")
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
---
fs/btrfs/scrub.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index c8da03e14094..1a721623813f 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -418,8 +418,16 @@ static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
}
}
-static void scrub_pause_on(struct btrfs_fs_info *fs_info)
+static void scrub_pause_on(struct scrub_ctx *sctx)
{
+ struct btrfs_fs_info *fs_info = sctx->fs_info;
+
+ /*
+ * These paths are populated from the commit root without using
+ * commit_root_sem so they are not valid across pauses.
+ */
+ btrfs_release_path(&sctx->extent_path);
+ btrfs_release_path(&sctx->csum_path);
atomic_inc(&fs_info->scrubs_paused);
wake_up(&fs_info->scrub_pause_wait);
}
@@ -434,10 +442,10 @@ static void scrub_pause_off(struct btrfs_fs_info *fs_info)
wake_up(&fs_info->scrub_pause_wait);
}
-static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
+static void scrub_blocked_if_needed(struct scrub_ctx *sctx)
{
- scrub_pause_on(fs_info);
- scrub_pause_off(fs_info);
+ scrub_pause_on(sctx);
+ scrub_pause_off(sctx->fs_info);
}
static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
@@ -2204,7 +2212,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
return ret;
if (atomic_read(&fs_info->scrub_pause_req))
- scrub_blocked_if_needed(fs_info);
+ scrub_blocked_if_needed(sctx);
spin_lock(&bg->lock);
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
@@ -2350,7 +2358,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,
break;
if (atomic_read(&fs_info->scrub_pause_req))
- scrub_blocked_if_needed(fs_info);
+ scrub_blocked_if_needed(sctx);
spin_lock(&bg->lock);
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
@@ -2492,7 +2500,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
/* Extent_path should be released by now. */
ASSERT(sctx->extent_path.nodes[0] == NULL);
- scrub_blocked_if_needed(fs_info);
+ scrub_blocked_if_needed(sctx);
if (sctx->is_dev_replace &&
btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
@@ -2837,7 +2845,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
* -> btrfs_commit_transaction()
* -> btrfs_scrub_pause()
*/
- scrub_pause_on(fs_info);
+ scrub_pause_on(sctx);
/*
* Don't do chunk preallocation for scrub.
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/4] btrfs: scrub: cancellable pause during throttle sleep
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 ` Boris Burkov
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
3 siblings, 0 replies; 5+ messages in thread
From: Boris Burkov @ 2026-12-01 1:14 UTC (permalink / raw)
To: linux-btrfs, kernel-team
scrub_throttle_dev_io() sleeps with schedule_timeout_interruptible()
without pausing the scrub, while a transaction cannot enter a critical
section without pausing scrub. Thus, a transaction commit gets blocked
on a scrub throttle, which is completely backwards.
Furthermore, this sleep ignores attempts to cancel the scrub.
Improve the scrub throttling semantics by implementing it as a cancel
aware sleep wrapped in a pause.
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
---
fs/btrfs/scrub.c | 27 +++++++++++++++++++++------
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 1a721623813f..a976897cea8d 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -448,6 +448,23 @@ static void scrub_blocked_if_needed(struct scrub_ctx *sctx)
scrub_pause_off(sctx->fs_info);
}
+/*
+ * Pause the scrub, and sleep for the throttle duration. Pausing allows the
+ * commit to proceed unblocked.
+ * Scrub cancel interrupts the sleep.
+ */
+static void scrub_throttle_sleep(struct scrub_ctx *sctx, ktime_t timeout)
+{
+ struct btrfs_fs_info *fs_info = sctx->fs_info;
+
+ scrub_pause_on(sctx);
+ wait_event_interruptible_hrtimeout(fs_info->scrub_pause_wait,
+ atomic_read(&fs_info->scrub_cancel_req) ||
+ atomic_read(&sctx->cancel_req),
+ timeout);
+ scrub_pause_off(fs_info);
+}
+
static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
{
int i;
@@ -1435,12 +1452,8 @@ static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d
delta = 0;
}
- if (delta) {
- long timeout;
-
- timeout = div_u64(delta * HZ, 1000);
- schedule_timeout_interruptible(timeout);
- }
+ if (delta)
+ scrub_throttle_sleep(sctx, ms_to_ktime(delta));
/* Next call will start the deadline period */
sctx->throttle_deadline = 0;
@@ -3334,6 +3347,7 @@ int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
}
atomic_inc(&fs_info->scrub_cancel_req);
+ wake_up(&fs_info->scrub_pause_wait);
while (atomic_read(&fs_info->scrubs_running)) {
mutex_unlock(&fs_info->scrub_lock);
wait_event(fs_info->scrub_pause_wait,
@@ -3358,6 +3372,7 @@ int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
return -ENOTCONN;
}
atomic_inc(&sctx->cancel_req);
+ wake_up(&fs_info->scrub_pause_wait);
while (dev->scrub_ctx) {
mutex_unlock(&fs_info->scrub_lock);
wait_event(fs_info->scrub_pause_wait,
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 3/4] btrfs: scrub: handle bwlimit below 512KiB
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-01 1:14 ` Boris Burkov
2026-12-01 1:14 ` [PATCH v2 4/4] btrfs: scrub: throttle per stripe instead of per stripe group Boris Burkov
3 siblings, 0 replies; 5+ messages in thread
From: Boris Burkov @ 2026-12-01 1:14 UTC (permalink / raw)
To: linux-btrfs, kernel-team
scrub_throttle_dev_io() divides a time slice into up to 64 intervals
with the computation
div = clamp(bwlimit / (16 * 1024 * 1024), 1, 64);
So at bwlimit below 16MiB/s, the time slice is unitary and above 1GiB/s
the slice is split into 64 intervals.
Each interval represents an opportunity to schedule IO and we set a
throttle deadline to now + time_slice / div. time_slice is 1000ms or 1s.
At each interval we count bio_size against bwlimit / div, or the portion
of the per second bwlimit for that interval. If it is under, we send the
bio with no throttling. Otherwise, we throttle till the end of the
interval then send the bio.
This ensures that we have a hard cap of 64 sleeps in a second which
ensures good throughput for typical unbounded or high limit scrubs.
But because bio_size is always 512KiB and we send at each interval, the
practical minimum limit is 512KiB/s, as we will send at least 512KiB
every second (the longest possible interval when div = 1).
To support lower limits without ruining throughput at higher limits,
change to a model which can sleep for an arbitrary duration to properly
amortize the data sent and acheive an average IO demand below bwlimit
for bwlimit < 512KiB/s.
However, at higher limits, because the unit of sending is still 512KiB,
sleeping between every IO for a naively calculated limit would be far
too many sleeps and adds unacceptable sleeping overhead, ruining our
ability to even submit at those limits. Therefore, we need to allow a
"burst duration" as a floor for the sleeping duration. 10ms is a
reasonable value: it is a similar number of sleeps at high limits (100
vs 64 per second) and is a responsive duration relatively commensurate
in scale with disk latencies and intuitively seems like it would not
be "too bursty" at any speed.
Signed-off-by: Boris Burkov <boris@bur.io>
---
fs/btrfs/scrub.c | 61 +++++++++++++++---------------------------------
1 file changed, 19 insertions(+), 42 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index a976897cea8d..ab3a67149415 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -57,6 +57,9 @@ struct scrub_ctx;
#define SCRUB_TOTAL_STRIPES (SCRUB_GROUPS_PER_SCTX * SCRUB_STRIPES_PER_GROUP)
+/* Length of a burst of throttled scrub IO, see scrub_throttle_dev_io(). */
+#define SCRUB_THROTTLE_BURST_MS 10
+
/* Represent one sector and its needed info to verify the content. */
struct scrub_sector_verification {
union {
@@ -195,9 +198,8 @@ struct scrub_ctx {
atomic_t cancel_req;
int readonly;
- /* State of IO submission throttling affecting the associated device */
- ktime_t throttle_deadline;
- u64 throttle_sent;
+ /* Earliest time the next read may be submitted under scrub_speed_max. */
+ ktime_t throttle_next;
bool is_dev_replace;
u64 write_pointer;
@@ -514,7 +516,7 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
atomic_set(&sctx->cancel_req, 0);
spin_lock_init(&sctx->stat_lock);
- sctx->throttle_deadline = 0;
+ sctx->throttle_next = 0;
mutex_init(&sctx->wr_lock);
if (is_dev_replace) {
@@ -1409,54 +1411,29 @@ static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *str
}
/*
- * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
- * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
+ * Throttling of IO submission, bandwidth-limit based. Limit can be set via
+ * /sys/fs/UUID/devinfo/devid/scrub_speed_max.
*/
static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *device,
unsigned int bio_size)
{
- const int time_slice = 1000;
- s64 delta;
+ const u64 bwlimit = READ_ONCE(device->scrub_speed_max);
ktime_t now;
- u32 div;
- u64 bwlimit;
+ ktime_t ahead;
- bwlimit = READ_ONCE(device->scrub_speed_max);
if (bwlimit == 0)
return;
- /*
- * Slice is divided into intervals when the IO is submitted, adjust by
- * bwlimit and maximum of 64 intervals.
- */
- div = clamp(bwlimit / (16 * 1024 * 1024), 1, 64);
-
- /* Start new epoch, set deadline */
now = ktime_get();
- if (sctx->throttle_deadline == 0) {
- sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
- sctx->throttle_sent = 0;
- }
-
- /* Still in the time to send? */
- if (ktime_before(now, sctx->throttle_deadline)) {
- /* If current bio is within the limit, send it */
- sctx->throttle_sent += bio_size;
- if (sctx->throttle_sent <= div_u64(bwlimit, div))
- return;
-
- /* We're over the limit, sleep until the rest of the slice */
- delta = ktime_ms_delta(sctx->throttle_deadline, now);
- } else {
- /* New request after deadline, start new epoch */
- delta = 0;
- }
-
- if (delta)
- scrub_throttle_sleep(sctx, ms_to_ktime(delta));
-
- /* Next call will start the deadline period */
- sctx->throttle_deadline = 0;
+ ahead = ktime_sub(sctx->throttle_next, now);
+ if (ahead < 0) {
+ sctx->throttle_next = now;
+ } else if (ahead > SCRUB_THROTTLE_BURST_MS * NSEC_PER_MSEC) {
+ scrub_throttle_sleep(sctx, ahead);
+ }
+ /* (bytes * (nsec / sec)) / (bytes / sec) ==> nsec */
+ sctx->throttle_next = ktime_add_ns(sctx->throttle_next,
+ div64_u64((u64)bio_size * NSEC_PER_SEC, bwlimit));
}
/*
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 4/4] btrfs: scrub: throttle per stripe instead of per stripe group
2026-12-01 1:14 [PATCH v2 0/4] btrfs: scrub: support low bwlimits Boris Burkov
` (2 preceding siblings ...)
2026-12-01 1:14 ` [PATCH v2 3/4] btrfs: scrub: handle bwlimit below 512KiB Boris Burkov
@ 2026-12-01 1:14 ` Boris Burkov
3 siblings, 0 replies; 5+ messages in thread
From: Boris Burkov @ 2026-12-01 1:14 UTC (permalink / raw)
To: linux-btrfs, kernel-team
submit_initial_group_read() charges a whole group of
SCRUB_STRIPES_PER_GROUP stripes to the bwlimit at once and then submits
all of them, so the smallest unit of throttled read IO is 512KiB. With
a low limit that is a long burst followed by a long sleep: at 32KiB/s
it is 512KiB every 16 seconds. This is unnecessarily bursty even if we
do maintain the correct bandwidth on average.
Since the real unit of scrub IO is the stripe of 64k, throttle in
between stripes, rather than stripe groups. The block layer plug is
still in place for situations where the throttle does not kick in and we
don't sleep between stripes.
Signed-off-by: Boris Burkov <boris@bur.io>
---
fs/btrfs/scrub.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index ab3a67149415..17e7dad4a624 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -1962,15 +1962,13 @@ static void submit_initial_group_read(struct scrub_ctx *sctx,
ASSERT(first_slot < SCRUB_TOTAL_STRIPES);
ASSERT(first_slot + nr_stripes <= SCRUB_TOTAL_STRIPES);
- scrub_throttle_dev_io(sctx, sctx->stripes[0].dev,
- btrfs_stripe_nr_to_offset(nr_stripes,
- sctx->stripes[0].stripe_len));
blk_start_plug(&plug);
for (int i = 0; i < nr_stripes; i++) {
struct scrub_stripe *stripe = &sctx->stripes[first_slot + i];
/* Those stripes should be initialized. */
ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
+ scrub_throttle_dev_io(sctx, stripe->dev, stripe->stripe_len);
scrub_submit_initial_read(sctx, stripe);
}
blk_finish_plug(&plug);
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread