All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: David Sterba <dsterba@suse.cz>
Cc: Arnd Bergmann <arnd@kernel.org>, Chris Mason <clm@fb.com>,
	Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>, Qu Wenruo <wqu@suse.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Filipe Manana <fdmanana@suse.com>, Li Zetao <lizetao1@huawei.com>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	akpm@linux-foundation.org
Subject: Re: [PATCH] btrfs: fix signedness issue in min()
Date: Wed, 19 Mar 2025 12:40:55 +0000	[thread overview]
Message-ID: <20250319124055.6951aeca@pumpkin> (raw)
In-Reply-To: <20250317192638.GA32661@twin.jikos.cz>

On Mon, 17 Mar 2025 20:26:39 +0100
David Sterba <dsterba@suse.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@kernel.org> wrote:
> >   
> > > From: Arnd Bergmann <arnd@arndb.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



      reply	other threads:[~2025-03-19 12:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-14 15:54 [PATCH] btrfs: fix signedness issue in min() Arnd Bergmann
2025-03-14 18:26 ` David Sterba
2025-03-17 14:16 ` David Laight
2025-03-17 19:26   ` David Sterba
2025-03-19 12:40     ` David Laight [this message]

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=20250319124055.6951aeca@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=arnd@kernel.org \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=dsterba@suse.cz \
    --cc=fdmanana@suse.com \
    --cc=johannes.thumshirn@wdc.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizetao1@huawei.com \
    --cc=wqu@suse.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.