Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/4]  btrfs: scrub: support low bwlimits
@ 2026-12-01  1:14 Boris Burkov
  2026-12-01  1:14 ` [PATCH v2 1/4] btrfs: scrub: release the commit root paths before pausing Boris Burkov
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Boris Burkov @ 2026-12-01  1:14 UTC (permalink / raw)
  To: linux-btrfs, kernel-team

Scrub currently bottoms out at 512KiB/s bwlimit, and below that the logic
doesn't make much sense. But if we support it naively, we ruin high speed
scrubbing and add unacceptably long uninterruptible sleeps.

Therefore, to support arbitrarily slow scrubs, we must also spruce up some of
the sleeping / pausing / cancelling logic.

Changelog:
v2:
- simplify the last patch which was splitting bios below the
  scrub stripe level. Just throttle per 64k stripe and avoid issues with bitmap
  concurrency.
- remove the bwlimit changed via sysfs wakeup logic which was buggy
  and over-engineered.


Boris Burkov (4):
  btrfs: scrub: release the commit root paths before pausing
  btrfs: scrub: cancellable pause during throttle sleep
  btrfs: scrub: handle bwlimit below 512KiB
  btrfs: scrub: throttle per stripe instead of per stripe group

 fs/btrfs/scrub.c | 108 +++++++++++++++++++++++------------------------
 1 file changed, 53 insertions(+), 55 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ 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] 7+ 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-04  6:27   ` [PATCH v2 0/4] btrfs: scrub: support low bwlimits Zygo Blaxell
  2026-12-01  1:14 ` [PATCH v2 3/4] btrfs: scrub: handle bwlimit below 512KiB Boris Burkov
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 7+ 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] 7+ 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
  2026-12-01 21:17 ` [PATCH v2 0/4] btrfs: scrub: support low bwlimits Qu Wenruo
  4 siblings, 0 replies; 7+ 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] 7+ 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
  2026-12-01 21:17 ` [PATCH v2 0/4] btrfs: scrub: support low bwlimits Qu Wenruo
  4 siblings, 0 replies; 7+ 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] 7+ messages in thread

* Re: [PATCH v2 0/4] btrfs: scrub: support low bwlimits
  2026-12-01  1:14 [PATCH v2 0/4] btrfs: scrub: support low bwlimits Boris Burkov
                   ` (3 preceding siblings ...)
  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 ` Qu Wenruo
  4 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2026-12-01 21:17 UTC (permalink / raw)
  To: Boris Burkov, linux-btrfs, kernel-team



在 2026/12/1 11:44, Boris Burkov 写道:
> Scrub currently bottoms out at 512KiB/s bwlimit, and below that the logic
> doesn't make much sense. But if we support it naively, we ruin high speed
> scrubbing and add unacceptably long uninterruptible sleeps.
> 
> Therefore, to support arbitrarily slow scrubs, we must also spruce up some of
> the sleeping / pausing / cancelling logic.
> 
> Changelog:
> v2:
> - simplify the last patch which was splitting bios below the
>    scrub stripe level. Just throttle per 64k stripe and avoid issues with bitmap
>    concurrency.
> - remove the bwlimit changed via sysfs wakeup logic which was buggy
>    and over-engineered.
> 

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> 
> Boris Burkov (4):
>    btrfs: scrub: release the commit root paths before pausing
>    btrfs: scrub: cancellable pause during throttle sleep
>    btrfs: scrub: handle bwlimit below 512KiB
>    btrfs: scrub: throttle per stripe instead of per stripe group
> 
>   fs/btrfs/scrub.c | 108 +++++++++++++++++++++++------------------------
>   1 file changed, 53 insertions(+), 55 deletions(-)
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/4]  btrfs: scrub: support low bwlimits
  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
  0 siblings, 0 replies; 7+ messages in thread
From: Zygo Blaxell @ 2026-12-04  6:27 UTC (permalink / raw)
  To: Boris Burkov; +Cc: linux-btrfs, kernel-team

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-04  6:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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   ` [PATCH v2 0/4] btrfs: scrub: support low bwlimits Zygo Blaxell
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox