Skip to contentSkip to conversation

btrfs: scrub: support low bwlimits

PATCH New Boris Burkov 1 participant 5 messages Dec 1 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v2 cover btrfs: scrub: support low bwlimits

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(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH v2 1/4 btrfs: scrub: release the commit root paths before pausing

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")
---
 fs/btrfs/scrub.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
fs/btrfs/scrub.c +16 -8
index c8da03e14094..1a721623813f 100644
@@ -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.
Signature · 1 line
2.56.0
#2 lore raw

PATCH v2 2/4 btrfs: scrub: cancellable pause during throttle sleep

In reply to Boris Burkov

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.

---
 fs/btrfs/scrub.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +21 -6
index 1a721623813f..a976897cea8d 100644
@@ -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,
Signature · 1 line
2.56.0
#3 lore raw

PATCH v2 3/4 btrfs: scrub: handle bwlimit below 512KiB

In reply to Boris Burkov

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.

---
 fs/btrfs/scrub.c | 61 +++++++++++++++---------------------------------
 1 file changed, 19 insertions(+), 42 deletions(-)
fs/btrfs/scrub.c +19 -42
index a976897cea8d..ab3a67149415 100644
@@ -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));
 }
 
 /*
Signature · 1 line
2.56.0
#4 lore raw

PATCH v2 4/4 btrfs: scrub: throttle per stripe instead of per stripe group

In reply to Boris Burkov

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.

---
 fs/btrfs/scrub.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/scrub.c +1 -3
index ab3a67149415..17e7dad4a624 100644
@@ -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);
Signature · 1 line
2.56.0