Skip to contentSkip to conversation

mm/khugepaged: use ALIGN helpers for PMD alignment

PATCH Applied Ye Liu 8 participants 10 messages Apr 9 – Apr 10

Subsystems (2)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

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

From: Ye Liu <[email protected]>

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.

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/[email protected]/

---
 mm/khugepaged.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/khugepaged.c +4 -4
index 1e2bff40d014..1b1d1a881ec3 100644
@@ -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;
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 18 lines · Ye Liu

From: Ye Liu <[email protected]>

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.

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/[email protected]/

Quoted 4 lines
---
 mm/khugepaged.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

LGTM.

Best Regards,
Yan, Zi

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Ye Liu
Quoted 6 lines · Ye Liu

From: Ye Liu <[email protected]>

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

Quoted 6 lines

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.

Best Regards
Barry

reviewer
#3 lore raw
In reply to Ye Liu
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

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

Reviewed-by on v2 1/1
#5 lore raw
In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

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.

LGTM, so:

Quoted 40 lines

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/[email protected]/

---
 mm/khugepaged.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/khugepaged.c +4 -5
index 1e2bff40d014..1b1d1a881ec3 100644
@@ -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

#4 lore raw
In reply to Lance Yang
Quoted 7 lines · Lance Yang
Quoted 5 lines · Ye Liu

From: Ye Liu <[email protected]>

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

Quoted 8 lines
Quoted 2 lines

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.

Quoted 4 lines

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.

Quoted 3 lines

Just a thought :)
Lance

Cheers, Lorenzo

maintainer Acked-by on v2 1/1
#6 lore raw
In reply to Ye Liu
Quoted 12 lines · Ye Liu

From: Ye Liu <[email protected]>

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.

Quoted 7 lines

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/[email protected]/
---

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 1/1
#7 lore raw
In reply to Ye Liu
Quoted 12 lines · Ye Liu

From: Ye Liu <[email protected]>

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.

Quoted 7 lines

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/[email protected]/
---

Reviewed-by on v2 1/1
#8 lore raw
In reply to Ye Liu
Quoted 11 lines · On Thu, 9 Apr 2026 09:43:22 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

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.

Quoted 7 lines

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/[email protected]/

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

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

Thanks,
SJ

[...]

author
#9 lore raw
Quoted 24 lines · SeongJae Park
Quoted 11 lines · On Thu, 9 Apr 2026 09:43:22 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

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.

Quoted 7 lines

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/[email protected]/

Quoted 5 lines

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.

Quoted 4 lines

Thanks,
SJ

[...]

Signature · 2 lines
Thanks,
Ye Liu