RE: [PATCH v1] mm:Using div64_ul replaces do_div() function

From: David Laight
Date: Tue Jun 20 2023 - 06:41:53 EST


From: Andrew Morton
> Sent: 16 June 2023 20:52
>
> On Thu, 15 Jun 2023 15:51:14 +0800 Wang Ming <machel@xxxxxxxx> wrote:
>
> > Fix the following coccicheck warning:
> >
> > mm/mm_init.c:2492:2-8: WARNING: do_div() does a
> > 64-by-32 division, please consider using div64_ul instead.
>
> I guess coccicheck was fooled by the incorrect comment over do_div().
> do_div() returns an unsigned long.

IIRC do_div() does an in-place update, but div64_ul() returns the result.
So the 'fixed' code is entirely broken.

The coccicheck message should really be 'check the domain/type of the divisor'.

> alloc_large_system_hash() should be using a u64 for `max'. Nobody uses
> unsigned long long any more.

Probably makes no difference.
The issue is with 'buckesize' - probably 'long' so 64bit on 64bit systems.

I guess 'max' can overflow 'long' on 32bit PAE.
Although using min(nr_all_pages, 0x80000000UL >> PAGE_SHIFT >> 4)
and doing everything as 'unsigned long' might be better.

David

>
> > --- a/mm/mm_init.c
> > +++ b/mm/mm_init.c
> > @@ -2508,7 +2508,7 @@ void *__init alloc_large_system_hash(const char *tablename,
> > /* limit allocation size to 1/16 total memory by default */
> > if (max == 0) {
> > max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
> > - do_div(max, bucketsize);
> > + div64_ul(max, bucketsize);
> > }
> > max = min(max, 0x80000000ULL);

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)