Skip to contentSkip to conversation

btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()

PATCH In review Thorsten Blum 3 participants 4 messages Sep 1, 2025 – Sep 8, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()

Replace max_t() followed by min_t() with a single clamp_t(). Manually casting 'bwlimit / (16 * 1024 * 1024)' to u32 is also redundant when using max_t(u32,,) or clamp_t(u32,,) and can be removed.

No functional changes intended.

---
 fs/btrfs/scrub.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/scrub.c +1 -2
index 6776e6ab8d10..ebfde24c0e42 100644
@@ -1369,8 +1369,7 @@ static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d
 	 * Slice is divided into intervals when the IO is submitted, adjust by
 	 * bwlimit and maximum of 64 intervals.
 	 */
-	div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
-	div = min_t(u32, 64, div);
+	div = clamp_t(u32, bwlimit / (16 * 1024 * 1024), 1, 64);
 
 	/* Start new epoch, set deadline */
 	now = ktime_get();
Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines · Thorsten Blum

Replace max_t() followed by min_t() with a single clamp_t(). Manually casting 'bwlimit / (16 * 1024 * 1024)' to u32 is also redundant when using max_t(u32,,) or clamp_t(u32,,) and can be removed.

No functional changes intended.

Added to for-next, thanks.

#2 lore raw
In reply to Thorsten Blum
Quoted 16 lines · On Mon, 1 Sep 2025 17:01:44 +0200 Thorsten Blum <[email protected]>…

Replace max_t() followed by min_t() with a single clamp_t(). Manually casting 'bwlimit / (16 * 1024 * 1024)' to u32 is also redundant when using max_t(u32,,) or clamp_t(u32,,) and can be removed.

No functional changes intended.

---
 fs/btrfs/scrub.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/scrub.c +0 -0
index 6776e6ab8d10..ebfde24c0e42 100644

fs/btrfs/scrub.c static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d · line 1372

Quoted 6 lines
fs/btrfs/scrub.c +1 -2
@@ -1369,8 +1369,7 @@ static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d
 	 * Slice is divided into intervals when the IO is submitted, adjust by
 	 * bwlimit and maximum of 64 intervals.
 	 */
-	div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
-	div = min_t(u32, 64, div);
+	div = clamp_t(u32, bwlimit / (16 * 1024 * 1024), 1, 64);

That probably ought to have a nack... although it isn't different. bwlimit is 64bit, if very big dividing by 16M will still be over 32 bits. and significant bits will be lost.

Just use clamp() without all the extra (bug introducing) (u32) casts.

David

Quoted 3 lines
fs/btrfs/scrub.c +0 -0
 	/* Start new epoch, set deadline */
 	now = ktime_get();
#3 lore raw
Quoted 30 lines · David Laight
Quoted 16 lines · On Mon, 1 Sep 2025 17:01:44 +0200 Thorsten Blum <[email protected]>…

Replace max_t() followed by min_t() with a single clamp_t(). Manually casting 'bwlimit / (16 * 1024 * 1024)' to u32 is also redundant when using max_t(u32,,) or clamp_t(u32,,) and can be removed.

No functional changes intended.

---
 fs/btrfs/scrub.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/scrub.c +0 -0
index 6776e6ab8d10..ebfde24c0e42 100644

fs/btrfs/scrub.c static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d · line 1372

Quoted 6 lines
fs/btrfs/scrub.c +1 -2
@@ -1369,8 +1369,7 @@ static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d
 	 * Slice is divided into intervals when the IO is submitted, adjust by
 	 * bwlimit and maximum of 64 intervals.
 	 */
-	div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
-	div = min_t(u32, 64, div);
+	div = clamp_t(u32, bwlimit / (16 * 1024 * 1024), 1, 64);

That probably ought to have a nack... although it isn't different. bwlimit is 64bit, if very big dividing by 16M will still be over 32 bits. and significant bits will be lost.

Quoted 1 line

Just use clamp() without all the extra (bug introducing) (u32) casts.

Clamp without type works best, thanks. The bug with large values could happen, but the input value is expected to be in gigabytes range.