Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: Boris Burkov <boris@bur.io>,
	linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH 3/4] btrfs: scrub: handle bwlimit below 512KiB
Date: Thu, 26 Nov 2026 10:15:45 +1030	[thread overview]
Message-ID: <ebf4531e-584f-4d5f-beac-e81133907863@suse.com> (raw)
In-Reply-To: <dae911ee8e0d50741fd4d92b7df57e0ebc2707f4.1795642270.git.boris@bur.io>



在 2026/11/26 08:04, 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.

For the bandwidth limit, I doubt if it's really practical to set it so low.
One 1GiB bg will take over 30min to scrub, which doesn't sound that sane.

> 
> 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.

I agree the old sleep behavior is not that good for high limits, 
especially considering how fast modern storage can be.

> 
> Now that we might sleep for a rather long time rather than at most 1s,
> although the sleeps are cancellable, also add a wakeup/cancel path from
> changing the limit again. If the user tunes the limit higher, they
> should just start running rather than needing to cancel.
> 
> Signed-off-by: Boris Burkov <boris@bur.io>
> ---
>   fs/btrfs/scrub.c | 71 +++++++++++++++++++-----------------------------
>   fs/btrfs/sysfs.c |  2 ++
>   2 files changed, 30 insertions(+), 43 deletions(-)
> 
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index a976897cea8d..d68e8d2f766f 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;
> @@ -451,16 +453,19 @@ static void scrub_blocked_if_needed(struct scrub_ctx *sctx)
>   /*
>    * Pause the scrub, and sleep for the throttle duration. Pausing allows the
>    * commit to proceed unblocked.
> - * Scrub cancel interrupts the sleep.
> + * Scrub cancel or a change of the limit the sleep was computed under
> + * interrupts the sleep.
>    */
> -static void scrub_throttle_sleep(struct scrub_ctx *sctx, ktime_t timeout)
> +static void scrub_throttle_sleep(struct scrub_ctx *sctx, struct btrfs_device *device,
> +				 u64 bwlimit, 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),
> +					   atomic_read(&sctx->cancel_req) ||
> +					   READ_ONCE(device->scrub_speed_max) != bwlimit,

One question, do we really need to bother the scrub_speed_max change?

I think this is a little overengineered, and Sashiko is already report 
low priority problem related to this part.

Thanks,
Qu

>   					   timeout);
>   	scrub_pause_off(fs_info);
>   }
> @@ -514,7 +519,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 +1414,34 @@ 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))
> +	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, device, bwlimit, ahead);
> +		/* Woken by a limit change: the debt was accrued at the old limit. */
> +		if (READ_ONCE(device->scrub_speed_max) != bwlimit) {
> +			sctx->throttle_next = ktime_get();
>   			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;
> +	/* (bytes * (nsec / sec)) / (bytes / sec) ==> nsec */
> +	sctx->throttle_next = ktime_add_ns(sctx->throttle_next,
> +				div64_u64((u64)bio_size * NSEC_PER_SEC, bwlimit));
>   }
>   
>   /*
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 89b0a4ef4063..4c7f486c2924 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -1928,6 +1928,8 @@ static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
>   	if (*endptr != 0)
>   		return -EINVAL;
>   	WRITE_ONCE(device->scrub_speed_max, limit);
> +	/* A throttled scrub sleeps on this under the old limit. */
> +	wake_up(&device->fs_info->scrub_pause_wait);
>   	return len;
>   }
>   BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,


  reply	other threads:[~2026-11-25 23:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:34 [PATCH 0/4] btrfs: scrub: support low bwlimits Boris Burkov
2026-11-25 21:34 ` [PATCH 1/4] btrfs: scrub: release the commit root paths before pausing Boris Burkov
2026-11-25 23:12   ` Qu Wenruo
2026-11-25 21:34 ` [PATCH 2/4] btrfs: scrub: cancellable pause during throttle sleep Boris Burkov
2026-11-25 23:29   ` Qu Wenruo
2026-11-25 21:34 ` [PATCH 3/4] btrfs: scrub: handle bwlimit below 512KiB Boris Burkov
2026-11-25 23:45   ` Qu Wenruo [this message]
2026-11-26  0:53     ` Boris Burkov
2026-11-26  3:43       ` Qu Wenruo
2026-11-25 21:34 ` [PATCH 4/4] btrfs: scrub: size read bios to the bwlimit 10ms burst Boris Burkov

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=ebf4531e-584f-4d5f-beac-e81133907863@suse.com \
    --to=wqu@suse.com \
    --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