From: David Laight <david.laight.linux@gmail.com>
To: Eric Biggers <ebiggers@kernel.org>
Cc: "David Hildenbrand (Red Hat)" <david@kernel.org>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Christoph Lameter <cl@gentwo.org>,
Dennis Zhou <dennis@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Mike Rapoport <rppt@kernel.org>, Tejun Heo <tj@kernel.org>,
Yuanchu Xie <yuanchu@google.com>
Subject: Re: [PATCH 39/44] mm: use min() instead of min_t()
Date: Fri, 21 Nov 2025 09:15:04 +0000 [thread overview]
Message-ID: <20251121091504.41ada607@pumpkin> (raw)
In-Reply-To: <20251120234522.GB3532564@google.com>
On Thu, 20 Nov 2025 23:45:22 +0000
Eric Biggers <ebiggers@kernel.org> wrote:
> On Thu, Nov 20, 2025 at 09:59:46AM +0000, David Laight wrote:
> > On Thu, 20 Nov 2025 10:20:41 +0100
> > "David Hildenbrand (Red Hat)" <david@kernel.org> wrote:
> >
> > > On 11/19/25 23:41, david.laight.linux@gmail.com wrote:
> > > > From: David Laight <david.laight.linux@gmail.com>
> > > >
> > > > min_t(unsigned int, a, b) casts an 'unsigned long' to 'unsigned int'.
> > > > Use min(a, b) instead as it promotes any 'unsigned int' to 'unsigned long'
> > > > and so cannot discard significant bits.
> > >
> > > I thought using min() was frowned upon and we were supposed to use
> > > min_t() instead to make it clear which type we want to use.
> >
> > I'm not sure that was ever true.
> > min_t() is just an accident waiting to happen.
> > (and I found a few of them, the worst are in sched/fair.c)
> >
> > Most of the min_t() are there because of the rather overzealous type
> > check that used to be in min().
> > But even then it would really be better to explicitly cast one of the
> > parameters to min(), so min_t(T, a, b) => min(a, (T)b).
> > Then it becomes rather more obvious that min_t(u8, x->m_u8, expr)
> > is going mask off the high bits of 'expr'.
> >
> > > Do I misremember or have things changed?
> > >
> > > Wasn't there a checkpatch warning that states exactly that?
> >
> > There is one that suggests min_t() - it ought to be nuked.
> > The real fix is to backtrack the types so there isn't an error.
> > min_t() ought to be a 'last resort' and a single cast is better.
> >
> > With the relaxed checks in min() most of the min_t() can just
> > be replaced by min(), even this is ok:
> > int len = fun();
> > if (len < 0)
> > return;
> > count = min(len, sizeof(T));
> >
> > I did look at the history of min() and min_t().
> > IIRC some of the networking code had a real function min() with
> > 'unsigned int' arguments.
> > This was moved to a common header, changed to a #define and had
> > a type added - so min(T, a, b).
> > Pretty much immediately that was renamed min_t() and min() added
> > that accepted any type - but checked the types of 'a' and 'b'
> > exactly matched.
> > Code was then changed (over the years) to use min(), but in many
> > cases the types didn't quite match - so min_t() was used a lot.
> >
> > I keep spotting new commits that pass too small a type to min_t().
> > So this is the start of a '5 year' campaign to nuke min_t() (et al).
>
> Yes, checkpatch suggests min_t() or max_t() if you cast an argument to
> min() or max(). Grep for "typecasts on min/max could be min_t/max_t" in
> scripts/checkpatch.pl.
IMHO that is a really bad suggestion (and always has been).
In reality min(a, (T)b) is less likely to be buggy than min_t(T, a, b).
Someone will notice that (u16)long_var is likely to be buggy but min_t()
is expected to 'do something magic'.
There are a log of examples of 'T_var = min_t(T, T_var, b)' which really
needed (typeof (b))T_var rather than (T)b
and T_var = min_t(T, a, b) which just doesn't need a cast at all.
>
> And historically you could not pass different types to min() and max(),
> which is why people use min_t() and max_t(). It looks like you fixed
> that a couple years ago in
> https://lore.kernel.org/all/b97faef60ad24922b530241c5d7c933c@AcuMS.aculab.com/,
> which is great!
I wrote that, and then Linus redid it to avoid some very long lines
from nested expansion (with some tree-wide patches that only he could do).
> It just takes some time for the whole community to get
> the message. Also, it seems that checkpatch is in need of an update.
>
> Doing these conversions looks good to me, but unfortunately this is
> probably the type of thing that shouldn't be a single kernel-wide patch
> series. They should be sent out per-subsystem.
In effect it is a list of separate patches, one per subsystem.
They just have a common 0/n wrapper.
I wanted to link them together, I guess I could have put a bit more
text in the common commit message I pasted into all the commits.
I didn't post the change to minmax.h (apart from a summary in 0/44)
because I hadn't even tried to build a 32bit kernel nevery mind
an allmodconfig or allyesconfig one.
I spent all yesterday trying to build allyesconfig...
David
>
> I suggest also putting a sentence in the commit message that mentions
> that min() and max() have been updated to accept arguments with
> different types. (Seeing as historically that wasn't true.) I suggest
> also being extra clear about when each change is a cleanup vs a fix.
>
> - Eric
next prev parent reply other threads:[~2025-11-21 9:15 UTC|newest]
Thread overview: 109+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-19 22:40 [PATCH 00/44] Change a lot of min_t() that might mask high bits david.laight.linux
2025-11-19 22:40 ` [PATCH 01/44] x86/asm/bitops: Change the return type of variable__ffs() to unsigned int david.laight.linux
2025-11-20 15:54 ` Yury Norov
2025-11-20 18:29 ` David Laight
2025-11-20 18:33 ` Yury Norov
2025-11-20 21:18 ` David Laight
2025-11-24 14:58 ` Yury Norov
2025-11-24 18:18 ` David Laight
2025-11-19 22:40 ` [PATCH 02/44] ext4: Fix saturation of 64bit inode times for old filesystems david.laight.linux
2025-11-19 22:40 ` [PATCH 03/44] perf: Fix branch stack callchain limit david.laight.linux
2025-11-26 5:25 ` Mi, Dapeng
2025-11-19 22:41 ` [PATCH 04/44] io_uring/net: Change some dubious min_t() david.laight.linux
2025-11-20 14:48 ` Jens Axboe
2025-11-20 15:48 ` David Laight
2025-11-20 15:53 ` Jens Axboe
2025-11-22 11:31 ` David Laight
2025-11-19 22:41 ` [PATCH 05/44] ipc/msg: Fix saturation of percpu counts in msgctl_info() david.laight.linux
2025-11-19 22:41 ` [PATCH 06/44] bpf: Verifier, remove some unusual uses of min_t() and max_t() david.laight.linux
2025-11-21 21:40 ` Alexei Starovoitov
2025-11-21 22:21 ` David Laight
2025-11-23 16:39 ` Alexei Starovoitov
2025-11-23 18:07 ` David Laight
2025-11-23 19:20 ` Alexei Starovoitov
2025-11-23 23:03 ` David Laight
2025-11-19 22:41 ` [PATCH 07/44] net/core/flow_dissector: Fix cap of __skb_flow_dissect() return value david.laight.linux
2025-11-19 22:41 ` [PATCH 08/44] net: ethtool: Use min3() instead of nested min_t(u16,...) david.laight.linux
2025-11-19 22:41 ` [PATCH 09/44] ipv6: __ip6_append_data() don't abuse max_t() casts david.laight.linux
2025-11-20 0:32 ` bot+bpf-ci
2025-11-20 11:16 ` David Laight
2025-11-20 13:50 ` Chris Mason
2025-11-19 22:41 ` [PATCH 10/44] x86/crypto: ctr_crypt() use min() instead of min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 11/44] arch/x96/kvm: " david.laight.linux
2025-11-19 22:41 ` [PATCH 12/44] block: " david.laight.linux
2025-11-20 14:44 ` Jens Axboe
2025-11-19 22:41 ` [PATCH 13/44] drivers/acpi: " david.laight.linux
2025-11-24 19:48 ` Rafael J. Wysocki
2025-11-19 22:41 ` [PATCH 14/44] drivers/char/hw_random: use min3() instead of nested min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 15/44] drivers/char/tpm: use min() instead of min_t() david.laight.linux
2025-11-21 20:11 ` Jarkko Sakkinen
2025-11-19 22:41 ` [PATCH 16/44] drivers/crypto/ccp: " david.laight.linux
2025-11-19 22:41 ` [PATCH 17/44] drivers/cxl: " david.laight.linux
2025-11-19 23:50 ` Dave Jiang
2025-11-19 22:41 ` [PATCH 18/44] drivers/gpio: " david.laight.linux
2025-11-20 8:01 ` Andy Shevchenko
2025-11-20 9:37 ` David Laight
2025-11-25 9:55 ` Andy Shevchenko
2025-11-19 22:41 ` [PATCH 19/44] drivers/gpu/drm/amd: " david.laight.linux
2025-11-19 22:41 ` [PATCH 20/44] drivers/i2c/busses: " david.laight.linux
2026-01-20 13:19 ` Andi Shyti
2025-11-19 22:41 ` [PATCH 21/44] drivers/net/ethernet/realtek: " david.laight.linux
2025-11-19 22:41 ` [PATCH 22/44] drivers/nvme: " david.laight.linux
2025-11-19 22:41 ` [PATCH 23/44] arch/x86/mm: " david.laight.linux
2025-11-19 22:41 ` [PATCH 24/44] drivers/nvmem: " david.laight.linux
2025-11-19 22:41 ` [PATCH 25/44] drivers/pci: " david.laight.linux
2025-11-24 21:04 ` Bjorn Helgaas
2025-11-24 21:42 ` David Laight
2025-11-19 22:41 ` [PATCH 26/44] drivers/scsi: " david.laight.linux
2025-11-19 23:09 ` Bart Van Assche
2025-11-20 18:44 ` David Laight
2025-11-22 21:50 ` David Laight
2025-11-19 22:41 ` [PATCH 27/44] drivers/tty/vt: use umin() instead of min_t(u16, ...) for row/col limits david.laight.linux
2025-11-20 7:23 ` Jiri Slaby
2025-11-19 22:41 ` [PATCH 28/44] drivers/usb/storage: use min() instead of min_t() david.laight.linux
2025-11-20 2:59 ` Alan Stern
2025-11-20 9:18 ` David Laight
2025-11-20 14:39 ` [usb-storage] " Alan Stern
2025-11-19 22:41 ` [PATCH 29/44] drivers/xen: " david.laight.linux
2025-11-20 8:13 ` Jürgen Groß
2025-11-19 22:41 ` [PATCH 30/44] fs: use min() or umin() " david.laight.linux
2025-11-25 9:06 ` Christian Brauner
2026-01-12 21:51 ` Brian Masney
2026-01-13 9:42 ` David Laight
2026-01-13 16:56 ` Mark Brown
2026-01-13 18:33 ` David Laight
2026-01-13 19:10 ` Mark Brown
2026-01-13 19:24 ` David Laight
2025-11-19 22:41 ` [PATCH 31/44] block: bvec.h: use min() " david.laight.linux
2025-11-19 22:41 ` [PATCH 32/44] nodemask: " david.laight.linux
2025-11-20 15:19 ` Yury Norov
2025-11-19 22:41 ` [PATCH 33/44] ipc: " david.laight.linux
2025-11-19 22:41 ` [PATCH 34/44] bpf: " david.laight.linux
2025-11-19 22:41 ` [PATCH 35/44] " david.laight.linux
2025-11-19 22:41 ` [PATCH 36/44] lib/bucket_locks: " david.laight.linux
2025-11-19 22:41 ` [PATCH 37/44] lib/crypto/mpi: " david.laight.linux
2025-11-19 22:41 ` [PATCH 38/44] lib/dynamic_queue_limits: use max() instead of max_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 39/44] mm: use min() instead of min_t() david.laight.linux
2025-11-20 9:20 ` David Hildenbrand (Red Hat)
2025-11-20 9:59 ` David Laight
2025-11-20 23:45 ` Eric Biggers
2025-11-21 8:27 ` David Hildenbrand (Red Hat)
2025-11-21 9:15 ` David Laight [this message]
2025-11-20 10:36 ` Lorenzo Stoakes
2025-11-20 12:09 ` Lorenzo Stoakes
2025-11-20 12:55 ` David Laight
2025-11-20 13:42 ` David Hildenbrand (Red Hat)
2025-11-20 15:44 ` David Laight
2025-11-21 8:24 ` David Hildenbrand (Red Hat)
2025-11-19 22:41 ` [PATCH 40/44] net: Don't pass bitfields to max_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 41/44] net/core: Change loop conditions so min() can be used david.laight.linux
2025-11-19 22:41 ` [PATCH 42/44] net: use min() instead of min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 43/44] net/netlink: Use umin() to avoid min_t(int, ...) discarding high bits david.laight.linux
2025-11-19 22:41 ` [PATCH 44/44] net/mptcp: Change some dubious min_t(int, ...) to min() david.laight.linux
2025-12-18 17:33 ` Matthieu Baerts
2025-12-18 20:15 ` David Laight
2025-12-19 10:48 ` Matthieu Baerts
2025-11-20 1:47 ` [PATCH 00/44] Change a lot of min_t() that might mask high bits Jakub Kicinski
2025-11-20 9:38 ` Lorenzo Stoakes
2025-11-20 14:52 ` (subset) " Jens Axboe
2025-11-24 9:49 ` Herbert Xu
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=20251121091504.41ada607@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=cl@gentwo.org \
--cc=david@kernel.org \
--cc=dennis@kernel.org \
--cc=ebiggers@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rppt@kernel.org \
--cc=tj@kernel.org \
--cc=willy@infradead.org \
--cc=yuanchu@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox