[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250319124055.6951aeca@pumpkin>
Date: Wed, 19 Mar 2025 12:40:55 +0000
From: David Laight <david.laight.linux@...il.com>
To: David Sterba <dsterba@...e.cz>
Cc: Arnd Bergmann <arnd@...nel.org>, Chris Mason <clm@...com>, Josef Bacik
<josef@...icpanda.com>, David Sterba <dsterba@...e.com>, Qu Wenruo
<wqu@...e.com>, Arnd Bergmann <arnd@...db.de>, Johannes Thumshirn
<johannes.thumshirn@....com>, Filipe Manana <fdmanana@...e.com>, Li Zetao
<lizetao1@...wei.com>, linux-btrfs@...r.kernel.org,
linux-kernel@...r.kernel.org, akpm@...ux-foundation.org
Subject: Re: [PATCH] btrfs: fix signedness issue in min()
On Mon, 17 Mar 2025 20:26:39 +0100
David Sterba <dsterba@...e.cz> wrote:
> On Mon, Mar 17, 2025 at 02:16:37PM +0000, David Laight wrote:
> > On Fri, 14 Mar 2025 16:54:41 +0100
> > Arnd Bergmann <arnd@...nel.org> wrote:
> >
> > > From: Arnd Bergmann <arnd@...db.de>
> > >
> > > Comparing a u64 to an loff_t causes a warning in min()
> > >
> > > fs/btrfs/extent_io.c: In function 'extent_write_locked_range':
> > > include/linux/compiler_types.h:557:45: error: call to '__compiletime_assert_588' declared with attribute error: min(folio_pos(folio) + folio_size(folio) - 1, end) signedness error
> > > fs/btrfs/extent_io.c:2472:27: note: in expansion of macro 'min'
> > > 2472 | cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
> > > | ^~~
> > >
> > > Use min_t() instead.
> >
> > It would be slightly better to use min_unsigned() since, regardless of the types
> > involved, it can't discard significant bits.
> >
> > OTOH the real problem here is that both folio_pos() and folio_size() return signed types.
>
> folio_size() returns size_t:
>
> static inline size_t folio_size(const struct folio *folio)
> {
> return PAGE_SIZE << folio_order(folio);
> }
>
> Otherwise the min_t with force u64 is ok and lots of min() use (in
> btrfs) was converted to the typed variant in case the types don't match.
That is just broken.
min_t(u64, x, y) is just min((u64)x, (u64)y) and you wouldn't do the
same casts anywhere else unless you really had to.
So you really shouldn't use min_t() unless there is no other way around the problem.
Ok (u64) are unlikely to be a problem, but there are plenty of places where
(u8) get used and can (and actually has) discard significant bits and cause bugs.
David
Powered by blists - more mailing lists