[PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment

Ye Liu posted 1 patch 2 months, 1 week ago
mm/khugepaged.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Ye Liu 2 months, 1 week ago
From: Ye Liu <liuye@kylinos.cn>

PMD alignment in khugepaged is currently implemented using a mix of
rounding helpers and open-coded bitmask operations.

Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
alignment, matching the preferred style for address and size handling.

No functional change intended.

Signed-off-by: Ye Liu <liuye@kylinos.cn>

Changes in v2:
- Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
- Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
  consistent within khugepaged.
- Update the changelog accordingly.
- Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
---
 mm/khugepaged.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 1e2bff40d014..1b1d1a881ec3 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2518,8 +2518,8 @@ static void collapse_scan_mm_slot(unsigned int progress_max,
 			cc->progress++;
 			continue;
 		}
-		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
-		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
+		hstart = ALIGN(vma->vm_start, HPAGE_PMD_SIZE);
+		hend = ALIGN_DOWN(vma->vm_end, HPAGE_PMD_SIZE);
 		if (khugepaged_scan.address > hend) {
 			cc->progress++;
 			continue;
@@ -2835,8 +2835,8 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	mmgrab(mm);
 	lru_add_drain_all();
 
-	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
-	hend = end & HPAGE_PMD_MASK;
+	hstart = ALIGN(start, HPAGE_PMD_SIZE);
+	hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
 
 	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
 		enum scan_result result = SCAN_FAIL;
-- 
2.43.0
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by SeongJae Park 2 months ago
On Thu,  9 Apr 2026 09:43:22 +0800 Ye Liu <ye.liu@linux.dev> wrote:

> From: Ye Liu <liuye@kylinos.cn>
> 
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
> 
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
> 
> No functional change intended.
> 
> Signed-off-by: Ye Liu <liuye@kylinos.cn>

Reviewed-by: SeongJae Park <sj@kernel.org>

> 
> Changes in v2:
> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>   consistent within khugepaged.
> - Update the changelog accordingly.
> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/

Nit.  Changelog should go to the commentary area [1].

[1] https://docs.kernel.org/process/submitting-patches.html#commentary


Thanks,
SJ

[...]
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Ye Liu 2 months ago

在 2026/4/10 08:18, SeongJae Park 写道:
> On Thu,  9 Apr 2026 09:43:22 +0800 Ye Liu <ye.liu@linux.dev> wrote:
> 
>> From: Ye Liu <liuye@kylinos.cn>
>>
>> PMD alignment in khugepaged is currently implemented using a mix of
>> rounding helpers and open-coded bitmask operations.
>>
>> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
>> alignment, matching the preferred style for address and size handling.
>>
>> No functional change intended.
>>
>> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> 
> Reviewed-by: SeongJae Park <sj@kernel.org>
> 
>>
>> Changes in v2:
>> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
>> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>>   consistent within khugepaged.
>> - Update the changelog accordingly.
>> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
> 
> Nit.  Changelog should go to the commentary area [1].
> 
> [1] https://docs.kernel.org/process/submitting-patches.html#commentary
> 
> 

Hi SJ,                                                                      
                                                                            
Thanks for the review, the Reviewed-by, and the helpful note.               
                                                                            
Got it. I'll keep the changelog in the commentary area for future revisions.


> Thanks,
> SJ
> 
> [...]

-- 
Thanks,
Ye Liu

Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Dev Jain 2 months ago

On 09/04/26 7:13 am, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
> 
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
> 
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
> 
> No functional change intended.
> 
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> 
> Changes in v2:
> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>   consistent within khugepaged.
> - Update the changelog accordingly.
> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
> ---

Reviewed-by: Dev Jain <dev.jain@arm.com>
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by David Hildenbrand (Arm) 2 months ago
On 4/9/26 03:43, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
> 
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
> 
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
> 
> No functional change intended.
> 
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> 
> Changes in v2:
> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>   consistent within khugepaged.
> - Update the changelog accordingly.
> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
> ---

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Lorenzo Stoakes 2 months ago
On Thu, Apr 09, 2026 at 09:43:22AM +0800, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
>
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
>
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
>
> No functional change intended.
>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <ljs@kernel.org>

>
> Changes in v2:
> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>   consistent within khugepaged.
> - Update the changelog accordingly.
> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
> ---
>  mm/khugepaged.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 1e2bff40d014..1b1d1a881ec3 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -2518,8 +2518,8 @@ static void collapse_scan_mm_slot(unsigned int progress_max,
>  			cc->progress++;
>  			continue;
>  		}
> -		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
> -		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
> +		hstart = ALIGN(vma->vm_start, HPAGE_PMD_SIZE);
> +		hend = ALIGN_DOWN(vma->vm_end, HPAGE_PMD_SIZE);
>  		if (khugepaged_scan.address > hend) {
>  			cc->progress++;
>  			continue;
> @@ -2835,8 +2835,8 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
>  	mmgrab(mm);
>  	lru_add_drain_all();
>
> -	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
> -	hend = end & HPAGE_PMD_MASK;
> +	hstart = ALIGN(start, HPAGE_PMD_SIZE);
> +	hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
>
>  	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
>  		enum scan_result result = SCAN_FAIL;
> --
> 2.43.0
>
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Lance Yang 2 months, 1 week ago
On Thu, Apr 09, 2026 at 09:43:22AM +0800, Ye Liu wrote:
>From: Ye Liu <liuye@kylinos.cn>
>
>PMD alignment in khugepaged is currently implemented using a mix of
>rounding helpers and open-coded bitmask operations.
>
>Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
>alignment, matching the preferred style for address and size handling.

One more spot in madvise_collapse():

hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);

Maybe switch that one to ALIGN_DOWN() as well.

And there is also in try_collapse_pte_mapped_thp():

unsigned long haddr = addr & HPAGE_PMD_MASK;

Just a thought :)
Lance
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Lorenzo Stoakes 2 months ago
On Thu, Apr 09, 2026 at 11:12:42AM +0800, Lance Yang wrote:
>
> On Thu, Apr 09, 2026 at 09:43:22AM +0800, Ye Liu wrote:
> >From: Ye Liu <liuye@kylinos.cn>
> >
> >PMD alignment in khugepaged is currently implemented using a mix of
> >rounding helpers and open-coded bitmask operations.
> >
> >Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> >alignment, matching the preferred style for address and size handling.
>
> One more spot in madvise_collapse():
>
> hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);
>
> Maybe switch that one to ALIGN_DOWN() as well.

Sorry to bikeshed but I'm not sure if:

hend = min(hend, ALIGN_DOWN(vma->vm_end, HPAGE_PMD_SIZE));

Really adds any clarity there, possibly better to just leave that one as-is.

>
> And there is also in try_collapse_pte_mapped_thp():
>
> unsigned long haddr = addr & HPAGE_PMD_MASK;

This one's fine though, be good to also const-ify haddr and end there.

>
> Just a thought :)
> Lance

Cheers, Lorenzo
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Barry Song 2 months, 1 week ago
On Thu, Apr 9, 2026 at 9:43 AM Ye Liu <ye.liu@linux.dev> wrote:
>
> From: Ye Liu <liuye@kylinos.cn>
>
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
>
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
>
> No functional change intended.
>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>

Reviewed-by: Barry Song <baohua@kernel.org>

Best Regards
Barry
Re: [PATCH v2] mm/khugepaged: use ALIGN helpers for PMD alignment
Posted by Zi Yan 2 months, 1 week ago
On 8 Apr 2026, at 21:43, Ye Liu wrote:

> From: Ye Liu <liuye@kylinos.cn>
>
> PMD alignment in khugepaged is currently implemented using a mix of
> rounding helpers and open-coded bitmask operations.
>
> Use ALIGN() and ALIGN_DOWN() consistently for PMD-sized address range
> alignment, matching the preferred style for address and size handling.
>
> No functional change intended.
>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
>
> Changes in v2:
> - Switch to ALIGN()/ALIGN_DOWN() per David's suggestion.
> - Also convert collapse_scan_mm_slot() to keep PMD alignment helpers
>   consistent within khugepaged.
> - Update the changelog accordingly.
> - Link to v1:https://lore.kernel.org/all/20260408093534.2373007-1-ye.liu@linux.dev/
> ---
>  mm/khugepaged.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
LGTM.

Reviewed-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi