Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: reinterpret async discard iops_limit=0 as no delay

Currently, a limit of 0 results in a hard coded metering over 6 hours.
Since the default is a set limit, I suspect no one truly depends on this
rather arbitrary setting. Repurpose it for an arguably more useful
"unlimited" mode, where the delay is 0.

Note that if block groups are too new, or go fully empty, there is still
a delay associated with those conditions. Those delays implement
heuristics for not trimming a region we are relatively likely to fully
overwrite soon.

CC: stable@vger.kernel.org # 6.2+
Reviewed-by: Neal Gompa <neal@gompa.dev>
Signed-off-by: Boris Burkov <boris@bur.io>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>

authored by

Boris Burkov and committed by
David Sterba
f263a7c3 cfe3445a

+13 -8
+13 -8
fs/btrfs/discard.c
··· 56 56 #define BTRFS_DISCARD_DELAY (120ULL * NSEC_PER_SEC) 57 57 #define BTRFS_DISCARD_UNUSED_DELAY (10ULL * NSEC_PER_SEC) 58 58 59 - /* Target completion latency of discarding all discardable extents */ 60 - #define BTRFS_DISCARD_TARGET_MSEC (6 * 60 * 60UL * MSEC_PER_SEC) 61 59 #define BTRFS_DISCARD_MIN_DELAY_MSEC (1UL) 62 60 #define BTRFS_DISCARD_MAX_DELAY_MSEC (1000UL) 63 61 #define BTRFS_DISCARD_MAX_IOPS (1000U) ··· 575 577 s32 discardable_extents; 576 578 s64 discardable_bytes; 577 579 u32 iops_limit; 580 + unsigned long min_delay = BTRFS_DISCARD_MIN_DELAY_MSEC; 578 581 unsigned long delay; 579 582 580 583 discardable_extents = atomic_read(&discard_ctl->discardable_extents); ··· 606 607 } 607 608 608 609 iops_limit = READ_ONCE(discard_ctl->iops_limit); 609 - if (iops_limit) 610 - delay = MSEC_PER_SEC / iops_limit; 611 - else 612 - delay = BTRFS_DISCARD_TARGET_MSEC / discardable_extents; 613 610 614 - delay = clamp(delay, BTRFS_DISCARD_MIN_DELAY_MSEC, 615 - BTRFS_DISCARD_MAX_DELAY_MSEC); 611 + if (iops_limit) { 612 + delay = MSEC_PER_SEC / iops_limit; 613 + } else { 614 + /* 615 + * Unset iops_limit means go as fast as possible, so allow a 616 + * delay of 0. 617 + */ 618 + delay = 0; 619 + min_delay = 0; 620 + } 621 + 622 + delay = clamp(delay, min_delay, BTRFS_DISCARD_MAX_DELAY_MSEC); 616 623 discard_ctl->delay_ms = delay; 617 624 618 625 spin_unlock(&discard_ctl->lock);