* [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()
@ 2025-09-01 15:01 Thorsten Blum
2025-09-01 19:42 ` David Sterba
2025-09-06 11:24 ` David Laight
0 siblings, 2 replies; 4+ messages in thread
From: Thorsten Blum @ 2025-09-01 15:01 UTC (permalink / raw)
To: Chris Mason, Josef Bacik, David Sterba
Cc: Thorsten Blum, linux-btrfs, linux-kernel
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.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
fs/btrfs/scrub.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 6776e6ab8d10..ebfde24c0e42 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -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();
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()
2025-09-01 15:01 [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io() Thorsten Blum
@ 2025-09-01 19:42 ` David Sterba
2025-09-06 11:24 ` David Laight
1 sibling, 0 replies; 4+ messages in thread
From: David Sterba @ 2025-09-01 19:42 UTC (permalink / raw)
To: Thorsten Blum
Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel
On Mon, Sep 01, 2025 at 05:01:44PM +0200, Thorsten Blum wrote:
> 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.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Added to for-next, thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()
2025-09-01 15:01 [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io() Thorsten Blum
2025-09-01 19:42 ` David Sterba
@ 2025-09-06 11:24 ` David Laight
2025-09-08 18:36 ` David Sterba
1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2025-09-06 11:24 UTC (permalink / raw)
To: Thorsten Blum
Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel
On Mon, 1 Sep 2025 17:01:44 +0200
Thorsten Blum <thorsten.blum@linux.dev> wrote:
> 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.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> fs/btrfs/scrub.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index 6776e6ab8d10..ebfde24c0e42 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -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
>
> /* Start new epoch, set deadline */
> now = ktime_get();
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io()
2025-09-06 11:24 ` David Laight
@ 2025-09-08 18:36 ` David Sterba
0 siblings, 0 replies; 4+ messages in thread
From: David Sterba @ 2025-09-08 18:36 UTC (permalink / raw)
To: David Laight
Cc: Thorsten Blum, Chris Mason, Josef Bacik, David Sterba,
linux-btrfs, linux-kernel
On Sat, Sep 06, 2025 at 12:24:58PM +0100, David Laight wrote:
> On Mon, 1 Sep 2025 17:01:44 +0200
> Thorsten Blum <thorsten.blum@linux.dev> wrote:
>
> > 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.
> >
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > ---
> > fs/btrfs/scrub.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> > index 6776e6ab8d10..ebfde24c0e42 100644
> > --- a/fs/btrfs/scrub.c
> > +++ b/fs/btrfs/scrub.c
> > @@ -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.
Clamp without type works best, thanks. The bug with large values could
happen, but the input value is expected to be in gigabytes range.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-08 18:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-01 15:01 [PATCH] btrfs: scrub: replace max_t()/min_t() with clamp_t() in scrub_throttle_dev_io() Thorsten Blum
2025-09-01 19:42 ` David Sterba
2025-09-06 11:24 ` David Laight
2025-09-08 18:36 ` David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox