linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/page_alloc.c: Use div64_ul instead of do_div
@ 2022-01-07  2:17 cgel.zte
  2022-01-07  9:15 ` David Laight
  0 siblings, 1 reply; 2+ messages in thread
From: cgel.zte @ 2022-01-07  2:17 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Changcheng Deng, Zeal Robot

From: Changcheng Deng <deng.changcheng@zte.com.cn>

do_div() does a 64-by-32 division. Here the divisor is an unsigned long
which on some platforms is 64 bit wide. So use div64_ul instead of do_div
to avoid a possible truncation.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
---
 mm/page_alloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 8dd6399bafb5..60469b616ac1 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -8431,7 +8431,7 @@ static void __setup_per_zone_wmarks(void)
 
 		spin_lock_irqsave(&zone->lock, flags);
 		tmp = (u64)pages_min * zone_managed_pages(zone);
-		do_div(tmp, lowmem_pages);
+		tmp = div64_ul(tmp, lowmem_pages);
 		if (is_highmem(zone)) {
 			/*
 			 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
@@ -8804,7 +8804,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);
+		max = div64_ul(max, bucketsize);
 	}
 	max = min(max, 0x80000000ULL);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* RE: [PATCH] mm/page_alloc.c: Use div64_ul instead of do_div
  2022-01-07  2:17 [PATCH] mm/page_alloc.c: Use div64_ul instead of do_div cgel.zte
@ 2022-01-07  9:15 ` David Laight
  0 siblings, 0 replies; 2+ messages in thread
From: David Laight @ 2022-01-07  9:15 UTC (permalink / raw)
  To: 'cgel.zte@gmail.com', akpm
  Cc: linux-mm, linux-kernel, Changcheng Deng, Zeal Robot

From: cgel.zte@gmail.com
> Sent: 07 January 2022 02:18
> 
> do_div() does a 64-by-32 division. Here the divisor is an unsigned long
> which on some platforms is 64 bit wide. So use div64_ul instead of do_div
> to avoid a possible truncation.

You really need to check the domain of the divisor, not its type.
Just because the type is 64 bits doesn't mean that the value can be
anywhere near even 32 bits.

	David

> 
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
> ---
>  mm/page_alloc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 8dd6399bafb5..60469b616ac1 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -8431,7 +8431,7 @@ static void __setup_per_zone_wmarks(void)
> 
>  		spin_lock_irqsave(&zone->lock, flags);
>  		tmp = (u64)pages_min * zone_managed_pages(zone);
> -		do_div(tmp, lowmem_pages);
> +		tmp = div64_ul(tmp, lowmem_pages);
>  		if (is_highmem(zone)) {
>  			/*
>  			 * __GFP_HIGH and PF_MEMALLOC allocations usually don't
> @@ -8804,7 +8804,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);
> +		max = div64_ul(max, bucketsize);
>  	}
>  	max = min(max, 0x80000000ULL);
> 
> --
> 2.25.1

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-07  9:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-07  2:17 [PATCH] mm/page_alloc.c: Use div64_ul instead of do_div cgel.zte
2022-01-07  9:15 ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).