[PATCH v2 1/3] mm: pagewalk: simplify hugepage boundary calculation using min()

Sahil Chandna posted 3 patches 1 week, 1 day ago
There is a newer version of this series
[PATCH v2 1/3] mm: pagewalk: simplify hugepage boundary calculation using min()
Posted by Sahil Chandna 1 week, 1 day ago
Use the kernel-provided min() macro to compute the minimum value instead
of an explicit ternary expression. This improves readability and matches
common kernel style. Also change the return hugepage boundary expression
by using bitmask for better clarity.

This was flagged by Coccinelle
(misc/minmax.cocci) as an opportunity to use min().

Found by: make coccicheck MODE=report M=mm/
No functional change intended.

Suggested-by: Matthew Wilcox <willy@infradead.org>
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Sahil Chandna <chandna.sahil@gmail.com>
---
changes since v1:
- Updated boundary calculation of huge page range for better clarity.
- No Functional changes intended.
link to v1: https://lore.kernel.org/all/ff24b3c573b766b187b55a38497fbad0a319b9f8.1763227530.git.chandna.sahil@gmail.com/
---
 mm/pagewalk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mm/pagewalk.c b/mm/pagewalk.c
index 9f91cf85a5be..1d38763a502a 100644
--- a/mm/pagewalk.c
+++ b/mm/pagewalk.c
@@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
 static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
 				       unsigned long end)
 {
-	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
-	return boundary < end ? boundary : end;
+	return min((addr | ~huge_page_mask(h)) + 1, end);
 }
 
 static int walk_hugetlb_range(unsigned long addr, unsigned long end,
-- 
2.50.1
Re: [PATCH v2 1/3] mm: pagewalk: simplify hugepage boundary calculation using min()
Posted by David Hildenbrand (Red Hat) 1 week ago
On 11/23/25 04:04, Sahil Chandna wrote:
> Use the kernel-provided min() macro to compute the minimum value instead
> of an explicit ternary expression. This improves readability and matches
> common kernel style. Also change the return hugepage boundary expression
> by using bitmask for better clarity.
> 
> This was flagged by Coccinelle
> (misc/minmax.cocci) as an opportunity to use min().
> 
> Found by: make coccicheck MODE=report M=mm/
> No functional change intended.
> 
> Suggested-by: Matthew Wilcox <willy@infradead.org>
> Suggested-by: David Laight <david.laight.linux@gmail.com>
> Signed-off-by: Sahil Chandna <chandna.sahil@gmail.com>
> ---
> changes since v1:
> - Updated boundary calculation of huge page range for better clarity.
> - No Functional changes intended.
> link to v1: https://lore.kernel.org/all/ff24b3c573b766b187b55a38497fbad0a319b9f8.1763227530.git.chandna.sahil@gmail.com/
> ---
>   mm/pagewalk.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/mm/pagewalk.c b/mm/pagewalk.c
> index 9f91cf85a5be..1d38763a502a 100644
> --- a/mm/pagewalk.c
> +++ b/mm/pagewalk.c
> @@ -312,8 +312,7 @@ static int walk_pgd_range(unsigned long addr, unsigned long end,
>   static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
>   				       unsigned long end)
>   {
> -	unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
> -	return boundary < end ? boundary : end;
> +	return min((addr | ~huge_page_mask(h)) + 1, end);

I know Willy suggested that, but I wonder if we could simplify that to 
something a bit easier to digest:

	return min(ALIGN(addr, huge_page_size(h)), end);

At least I can make sense of that a bit faster.

(I don't think we're interested in micro-optimizations in this code here 
in particular :) )

-- 
Cheers

David