Re: [PATCH 39/44] mm: use min() instead of min_t()

From: David Laight

Date: Thu Nov 20 2025 - 10:44:09 EST


On Thu, 20 Nov 2025 14:42:24 +0100
"David Hildenbrand (Red Hat)" <david@xxxxxxxxxx> wrote:

> >>
> >>>
> >>> Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
> >>> ---
> >>> mm/gup.c | 4 ++--
> >>> mm/memblock.c | 2 +-
> >>> mm/memory.c | 2 +-
> >>> mm/percpu.c | 2 +-
> >>> mm/truncate.c | 3 +--
> >>> mm/vmscan.c | 2 +-
> >>> 6 files changed, 7 insertions(+), 8 deletions(-)
> >>>
> >>> diff --git a/mm/gup.c b/mm/gup.c
> >>> index a8ba5112e4d0..55435b90dcc3 100644
> >>> --- a/mm/gup.c
> >>> +++ b/mm/gup.c
> >>> @@ -237,8 +237,8 @@ static inline struct folio *gup_folio_range_next(struct page *start,
> >>> unsigned int nr = 1;
> >>>
> >>> if (folio_test_large(folio))
> >>> - nr = min_t(unsigned int, npages - i,
> >>> - folio_nr_pages(folio) - folio_page_idx(folio, next));
> >>> + nr = min(npages - i,
> >>> + folio_nr_pages(folio) - folio_page_idx(folio, next));
> >>
> >> There's no cases where any of these would discard significant bits. But we
> >> ultimately cast to unisnged int anyway (nr) so not sure this achieves anything.
> >
> > The (implicit) cast to unsigned int is irrelevant - that happens after the min().
> > The issue is that 'npages' is 'unsigned long' so can (in theory) be larger than 4G.
> > Ok that would be a 16TB buffer, but someone must have decided that npages might
> > not fit in 32 bits otherwise they wouldn't have used 'unsigned long'.
>
> See commit fa17bcd5f65e ("mm: make folio page count functions return
> unsigned") why that function used to return "long" instead of "unsigned
> int" and how we changed it to "unsigned long".
>
> Until that function actually returns something that large might take a
> while, so no need to worry about that right now.

Except that it gives a false positive on a compile-time test that finds a
few real bugs.

I've been (slowly) fixing 'allmodconfig' and found 'goodies' like:
min_t(u32, MAX_UINT, expr)
and
min_t(u8, expr, 255)

Pretty much all the min_t(unsigned xxx) that compile when changed to min()
are safe changes and might fix an obscure bug.
Probably 99% make no difference.

So I'd like to get rid of the ones that make no difference.

David