Skip to contentSkip to conversation

mm/khugepaged: use [pmd|pte]_addr for better reading

PATCH In review Wei Yang 4 participants 13 messages Sep 20, 2025 – Sep 22, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm/khugepaged: use [pmd|pte]_addr for better reading

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
 mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +22 -21
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
 }
 
 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
 					pte_t *pte,
 					struct collapse_control *cc,
 					struct list_head *compound_pagelist)
 {
 	struct page *page = NULL;
 	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
 	pte_t *_pte;
 	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
 
 	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
 		pte_t pteval = ptep_get(_pte);
 		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
 			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
 			result = SCAN_PTE_UFFD_WP;
 			goto out;
 		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
 		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
 			result = SCAN_PAGE_NULL;
 			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
 		 */
 		if (cc->is_khugepaged &&
 		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
 			referenced++;
 	}
 
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
  */
 static int __collapse_huge_page_swapin(struct mm_struct *mm,
 				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
 				       int referenced)
 {
 	int swapped_in = 0;
 	vm_fault_t ret = 0;
-	unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
+	unsigned long pte_addr, end = pmd_addr + (HPAGE_PMD_NR * PAGE_SIZE);
 	int result;
 	pte_t *pte = NULL;
 	spinlock_t *ptl;
 
-	for (address = haddr; address < end; address += PAGE_SIZE) {
+	for (pte_addr = pmd_addr; pte_addr < end; pte_addr += PAGE_SIZE) {
 		struct vm_fault vmf = {
 			.vma = vma,
-			.address = address,
-			.pgoff = linear_page_index(vma, address),
+			.address = pte_addr,
+			.pgoff = linear_page_index(vma, pte_addr),
 			.flags = FAULT_FLAG_ALLOW_RETRY,
 			.pmd = pmd,
 		};
@@ -1009,7 +1010,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
 			 * Here the ptl is only used to check pte_same() in
 			 * do_swap_page(), so readonly version is enough.
 			 */
-			pte = pte_offset_map_ro_nolock(mm, pmd, address, &ptl);
+			pte = pte_offset_map_ro_nolock(mm, pmd, pte_addr, &ptl);
 			if (!pte) {
 				mmap_read_unlock(mm);
 				result = SCAN_PMD_NULL;
@@ -1252,7 +1253,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 
 static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 				   struct vm_area_struct *vma,
-				   unsigned long address, bool *mmap_locked,
+				   unsigned long pmd_addr, bool *mmap_locked,
 				   struct collapse_control *cc)
 {
 	pmd_t *pmd;
@@ -1261,26 +1262,26 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 	int none_or_zero = 0, shared = 0;
 	struct page *page = NULL;
 	struct folio *folio = NULL;
-	unsigned long _address;
+	unsigned long pte_addr;
 	spinlock_t *ptl;
 	int node = NUMA_NO_NODE, unmapped = 0;
 
-	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
+	VM_BUG_ON(pmd_addr & ~HPAGE_PMD_MASK);
 
-	result = find_pmd_or_thp_or_none(mm, address, &pmd);
+	result = find_pmd_or_thp_or_none(mm, pmd_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
 
 	memset(cc->node_load, 0, sizeof(cc->node_load));
 	nodes_clear(cc->alloc_nmask);
-	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
+	pte = pte_offset_map_lock(mm, pmd, pmd_addr, &ptl);
 	if (!pte) {
 		result = SCAN_PMD_NULL;
 		goto out;
 	}
 
-	for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, _address += PAGE_SIZE) {
+	for (pte_addr = pmd_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
+	     _pte++, pte_addr += PAGE_SIZE) {
 		pte_t pteval = ptep_get(_pte);
 		if (is_swap_pte(pteval)) {
 			++unmapped;
@@ -1328,7 +1329,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 			goto out_unmap;
 		}
 
-		page = vm_normal_page(vma, _address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
 		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
 			result = SCAN_PAGE_NULL;
 			goto out_unmap;
@@ -1397,7 +1398,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 		if (cc->is_khugepaged &&
 		    (pte_young(pteval) || folio_test_young(folio) ||
 		     folio_test_referenced(folio) ||
-		     mmu_notifier_test_young(vma->vm_mm, _address)))
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
 			referenced++;
 	}
 	if (cc->is_khugepaged &&
@@ -1410,7 +1411,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 out_unmap:
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
-		result = collapse_huge_page(mm, address, referenced,
+		result = collapse_huge_page(mm, pmd_addr, referenced,
 					    unmapped, cc);
 		/* collapse_huge_page will return with the mmap_lock released */
 		*mmap_locked = false;
Signature · 1 line
2.34.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

This renaming makes the code much easier to follow, but just some minor style nits below :)

Quoted 13 lines
---
  mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +0 -1
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
  }
 
  static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,

mm/khugepaged.c static void release_pte_pages(pte_t *pte, pte_t *_pte, · line 549

Quoted 10 lines
mm/khugepaged.c +2 -0
+					unsigned long pmd_addr,
  					pte_t *pte,
  					struct collapse_control *cc,
  					struct list_head *compound_pagelist)
  {
  	struct page *page = NULL;
  	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
  	pte_t *_pte;
  	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;

Nit: could we refactor this block into the "reverse christmas tree" style?

Quoted 32 lines
mm/khugepaged.c +4 -5
  	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
  		pte_t pteval = ptep_get(_pte);
  		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  			result = SCAN_PTE_UFFD_WP;
  			goto out;
  		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
  		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
  			result = SCAN_PAGE_NULL;
  			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  		 */
  		if (cc->is_khugepaged &&
  		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
  			referenced++;
  	}
 
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
   */
  static int __collapse_huge_page_swapin(struct mm_struct *mm,
  				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 997

Quoted 10 lines
mm/khugepaged.c +2 -1
+				       unsigned long pmd_addr, pmd_t *pmd,
  				       int referenced)
  {
  	int swapped_in = 0;
  	vm_fault_t ret = 0;
-	unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
+	unsigned long pte_addr, end = pmd_addr + (HPAGE_PMD_NR * PAGE_SIZE);
  	int result;
  	pte_t *pte = NULL;
  	spinlock_t *ptl;

Same nit as above.

Quoted 31 lines
mm/khugepaged.c +5 -5
-	for (address = haddr; address < end; address += PAGE_SIZE) {
+	for (pte_addr = pmd_addr; pte_addr < end; pte_addr += PAGE_SIZE) {
  		struct vm_fault vmf = {
  			.vma = vma,
-			.address = address,
-			.pgoff = linear_page_index(vma, address),
+			.address = pte_addr,
+			.pgoff = linear_page_index(vma, pte_addr),
  			.flags = FAULT_FLAG_ALLOW_RETRY,
  			.pmd = pmd,
  		};
@@ -1009,7 +1010,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
  			 * Here the ptl is only used to check pte_same() in
  			 * do_swap_page(), so readonly version is enough.
  			 */
-			pte = pte_offset_map_ro_nolock(mm, pmd, address, &ptl);
+			pte = pte_offset_map_ro_nolock(mm, pmd, pte_addr, &ptl);
  			if (!pte) {
  				mmap_read_unlock(mm);
  				result = SCAN_PMD_NULL;
@@ -1252,7 +1253,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 
  static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  				   struct vm_area_struct *vma,
-				   unsigned long address, bool *mmap_locked,
+				   unsigned long pmd_addr, bool *mmap_locked,
  				   struct collapse_control *cc)
  {
  	pmd_t *pmd;

mm/khugepaged.c static int hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1267

Quoted 7 lines
mm/khugepaged.c +1 -1
@@ -1261,26 +1262,26 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  	int none_or_zero = 0, shared = 0;
  	struct page *page = NULL;
  	struct folio *folio = NULL;
-	unsigned long _address;
+	unsigned long pte_addr;
  	spinlock_t *ptl;
  	int node = NUMA_NO_NODE, unmapped = 0;

Ditto.

Quoted 52 lines
mm/khugepaged.c +8 -8
-	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
+	VM_BUG_ON(pmd_addr & ~HPAGE_PMD_MASK);
 
-	result = find_pmd_or_thp_or_none(mm, address, &pmd);
+	result = find_pmd_or_thp_or_none(mm, pmd_addr, &pmd);
  	if (result != SCAN_SUCCEED)
  		goto out;
 
  	memset(cc->node_load, 0, sizeof(cc->node_load));
  	nodes_clear(cc->alloc_nmask);
-	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
+	pte = pte_offset_map_lock(mm, pmd, pmd_addr, &ptl);
  	if (!pte) {
  		result = SCAN_PMD_NULL;
  		goto out;
  	}
 
-	for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, _address += PAGE_SIZE) {
+	for (pte_addr = pmd_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
+	     _pte++, pte_addr += PAGE_SIZE) {
  		pte_t pteval = ptep_get(_pte);
  		if (is_swap_pte(pteval)) {
  			++unmapped;
@@ -1328,7 +1329,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  			goto out_unmap;
  		}
 
-		page = vm_normal_page(vma, _address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
  		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
  			result = SCAN_PAGE_NULL;
  			goto out_unmap;
@@ -1397,7 +1398,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  		if (cc->is_khugepaged &&
  		    (pte_young(pteval) || folio_test_young(folio) ||
  		     folio_test_referenced(folio) ||
-		     mmu_notifier_test_young(vma->vm_mm, _address)))
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
  			referenced++;
  	}
  	if (cc->is_khugepaged &&
@@ -1410,7 +1411,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  out_unmap:
  	pte_unmap_unlock(pte, ptl);
  	if (result == SCAN_SUCCEED) {
-		result = collapse_huge_page(mm, address, referenced,
+		result = collapse_huge_page(mm, pmd_addr, referenced,
  					    unmapped, cc);
  		/* collapse_huge_page will return with the mmap_lock released */
  		*mmap_locked = false;
author
#2 lore raw
Quoted 35 lines · Lance Yang
Quoted 11 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

This renaming makes the code much easier to follow, but just some minor style nits below :)

Quoted 16 lines
---
  mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +1 -1
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
  }
  static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
  					pte_t *pte,
  					struct collapse_control *cc,
  					struct list_head *compound_pagelist)
Quoted 9 lines

mm/khugepaged.c static void release_pte_pages(pte_t *pte, pte_t *_pte, · line 549

Quoted 6 lines
mm/khugepaged.c +1 -0
  {
  	struct page *page = NULL;
  	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
  	pte_t *_pte;
  	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;

Nit: could we refactor this block into the "reverse christmas tree" style?

You mean sth like this?

   	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
   	struct page *page = NULL;
   	struct folio *folio = NULL;
  	unsigned long pte_addr = pmd_addr;
   	pte_t *_pte;
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#3 lore raw
Quoted 46 lines · Wei Yang
Quoted 35 lines · Lance Yang
Quoted 11 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

This renaming makes the code much easier to follow, but just some minor style nits below :)

Quoted 16 lines
---
   mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
   1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +1 -1
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
   }
   static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
   					pte_t *pte,
   					struct collapse_control *cc,
   					struct list_head *compound_pagelist)
Quoted 9 lines

mm/khugepaged.c static void release_pte_pages(pte_t *pte, pte_t *_pte, · line 549

Quoted 6 lines
mm/khugepaged.c +1 -0
   {
   	struct page *page = NULL;
   	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
   	pte_t *_pte;
   	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;

Nit: could we refactor this block into the "reverse christmas tree" style?

Quoted 7 lines

You mean sth like this?

    	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
    	struct page *page = NULL;
    	struct folio *folio = NULL;
   	unsigned long pte_addr = pmd_addr;
    	pte_t *_pte;

That's getting closer. The formatting would be something like this:

	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
	unsigned long pte_addr = pmd_addr;
	struct folio *folio = NULL;
	struct page *page = NULL;
	pte_t *_pte;

Feel free to change it :)

reviewer
#4 lore raw
In reply to Wei Yang
Quoted 62 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
  mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +6 -5
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
  }
 
  static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
  					pte_t *pte,
  					struct collapse_control *cc,
  					struct list_head *compound_pagelist)
  {
  	struct page *page = NULL;
  	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
  	pte_t *_pte;
  	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
 
  	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
  		pte_t pteval = ptep_get(_pte);
  		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  			result = SCAN_PTE_UFFD_WP;
  			goto out;
  		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
  		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
  			result = SCAN_PAGE_NULL;
  			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  		 */
  		if (cc->is_khugepaged &&
  		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
  			referenced++;
  	}
 

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 990

Quoted 6 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
   */
  static int __collapse_huge_page_swapin(struct mm_struct *mm,
  				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
  				       int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

Quoted 13 lines
mm/khugepaged.c +1 -1
  			if (!pte) {
  				mmap_read_unlock(mm);
  				result = SCAN_PMD_NULL;
@@ -1252,7 +1253,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 
  static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  				   struct vm_area_struct *vma,
-				   unsigned long address, bool *mmap_locked,
+				   unsigned long pmd_addr, bool *mmap_locked,
  				   struct collapse_control *cc)
  {
  	pmd_t *pmd;

mm/khugepaged.c static int hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1265

Quoted 5 lines
mm/khugepaged.c +1 -1
@@ -1261,26 +1262,26 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  	int none_or_zero = 0, shared = 0;
  	struct page *page = NULL;
  	struct folio *folio = NULL;
-	unsigned long _address;
+	unsigned long pte_addr;

Here we can change the address parameter of hpage_collapse_scan_pmd to haddr and _address to addr. And so on.

Quoted 54 lines
mm/khugepaged.c +8 -8
  	spinlock_t *ptl;
  	int node = NUMA_NO_NODE, unmapped = 0;
 
-	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
+	VM_BUG_ON(pmd_addr & ~HPAGE_PMD_MASK);
 
-	result = find_pmd_or_thp_or_none(mm, address, &pmd);
+	result = find_pmd_or_thp_or_none(mm, pmd_addr, &pmd);
  	if (result != SCAN_SUCCEED)
  		goto out;
 
  	memset(cc->node_load, 0, sizeof(cc->node_load));
  	nodes_clear(cc->alloc_nmask);
-	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
+	pte = pte_offset_map_lock(mm, pmd, pmd_addr, &ptl);
  	if (!pte) {
  		result = SCAN_PMD_NULL;
  		goto out;
  	}
 
-	for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, _address += PAGE_SIZE) {
+	for (pte_addr = pmd_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
+	     _pte++, pte_addr += PAGE_SIZE) {
  		pte_t pteval = ptep_get(_pte);
  		if (is_swap_pte(pteval)) {
  			++unmapped;
@@ -1328,7 +1329,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  			goto out_unmap;
  		}
 
-		page = vm_normal_page(vma, _address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
  		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
  			result = SCAN_PAGE_NULL;
  			goto out_unmap;
@@ -1397,7 +1398,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  		if (cc->is_khugepaged &&
  		    (pte_young(pteval) || folio_test_young(folio) ||
  		     folio_test_referenced(folio) ||
-		     mmu_notifier_test_young(vma->vm_mm, _address)))
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
  			referenced++;
  	}
  	if (cc->is_khugepaged &&
@@ -1410,7 +1411,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
  out_unmap:
  	pte_unmap_unlock(pte, ptl);
  	if (result == SCAN_SUCCEED) {
-		result = collapse_huge_page(mm, address, referenced,
+		result = collapse_huge_page(mm, pmd_addr, referenced,
  					    unmapped, cc);
  		/* collapse_huge_page will return with the mmap_lock released */
  		*mmap_locked = false;
author
#5 lore raw
Quoted 71 lines · Dev Jain
Quoted 59 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
  mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +6 -5
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
  }
  static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
  					pte_t *pte,
  					struct collapse_control *cc,
  					struct list_head *compound_pagelist)
  {
  	struct page *page = NULL;
  	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
  	pte_t *_pte;
  	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
  	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
  		pte_t pteval = ptep_get(_pte);
  		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  			result = SCAN_PTE_UFFD_WP;
  			goto out;
  		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
  		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
  			result = SCAN_PAGE_NULL;
  			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  		 */
  		if (cc->is_khugepaged &&
  		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
  			referenced++;
  	}

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 990

Quoted 6 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
   */
  static int __collapse_huge_page_swapin(struct mm_struct *mm,
  				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
  				       int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Quoted 5 lines

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

So your suggestion is

    pmd_addr -> haddr
    pte_addr -> address

right?

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#6 lore raw
Quoted 75 lines · Wei Yang
Quoted 69 lines · Dev Jain
Quoted 59 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
   mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
   1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +6 -5
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
   }
   static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
   					pte_t *pte,
   					struct collapse_control *cc,
   					struct list_head *compound_pagelist)
   {
   	struct page *page = NULL;
   	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
   	pte_t *_pte;
   	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
   	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
   		pte_t pteval = ptep_get(_pte);
   		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
   			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
   			result = SCAN_PTE_UFFD_WP;
   			goto out;
   		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
   		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
   			result = SCAN_PAGE_NULL;
   			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
   		 */
   		if (cc->is_khugepaged &&
   		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
   			referenced++;
   	}

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 990

Quoted 6 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
    */
   static int __collapse_huge_page_swapin(struct mm_struct *mm,
   				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
   				       int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Quoted 5 lines

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

Quoted 4 lines

So your suggestion is

     pmd_addr -> haddr
     pte_addr -> address

pmd_addr -> haddr
pte_addr -> addr

Quoted 3 lines

right?

author
#7 lore raw
Quoted 77 lines · Dev Jain
Quoted 75 lines · Wei Yang
Quoted 74 lines · Dev Jain
Quoted 59 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
   mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
   1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +6 -5
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
   }
   static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
   					pte_t *pte,
   					struct collapse_control *cc,
   					struct list_head *compound_pagelist)
   {
   	struct page *page = NULL;
   	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
   	pte_t *_pte;
   	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
   	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
   		pte_t pteval = ptep_get(_pte);
   		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
   			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
   			result = SCAN_PTE_UFFD_WP;
   			goto out;
   		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
   		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
   			result = SCAN_PAGE_NULL;
   			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
   		 */
   		if (cc->is_khugepaged &&
   		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
   			referenced++;
   	}

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 990

Quoted 6 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
    */
   static int __collapse_huge_page_swapin(struct mm_struct *mm,
   				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
   				       int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

Quoted 8 lines
Quoted 4 lines

So your suggestion is

     pmd_addr -> haddr
     pte_addr -> address

pmd_addr -> haddr
pte_addr -> addr

Take another look into the code.

The change touch three functions:

__collapse_huge_page_isolate()
__collapse_huge_page_swapin()
hpage_collapse_scan_pmd()

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

Is this ok for you?

Signature · 2 lines
Wei Yang
Help you, Help me
#8 lore raw
Quoted 89 lines · Wei Yang
Quoted 77 lines · Dev Jain
Quoted 75 lines · Wei Yang
Quoted 74 lines · Dev Jain
Quoted 59 lines · Wei Yang

When collapse a pmd, there are two address in use:

* address points to the start of pmd
* address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
    mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------
    1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +6 -5
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
    }
    static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long address,
+					unsigned long pmd_addr,
    					pte_t *pte,
    					struct collapse_control *cc,
    					struct list_head *compound_pagelist)
    {
    	struct page *page = NULL;
    	struct folio *folio = NULL;
+	unsigned long pte_addr = pmd_addr;
    	pte_t *_pte;
    	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
    	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-	     _pte++, address += PAGE_SIZE) {
+	     _pte++, pte_addr += PAGE_SIZE) {
    		pte_t pteval = ptep_get(_pte);
    		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
    			++none_or_zero;
@@ -570,7 +571,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
    			result = SCAN_PTE_UFFD_WP;
    			goto out;
    		}
-		page = vm_normal_page(vma, address, pteval);
+		page = vm_normal_page(vma, pte_addr, pteval);
    		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
    			result = SCAN_PAGE_NULL;
    			goto out;
@@ -655,8 +656,8 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
    		 */
    		if (cc->is_khugepaged &&
    		    (pte_young(pteval) || folio_test_young(folio) ||
-		     folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-								     address)))
+		     folio_test_referenced(folio) ||
+		     mmu_notifier_test_young(vma->vm_mm, pte_addr)))
    			referenced++;
    	}

mm/khugepaged.c static int check_pmd_still_valid(struct mm_struct *mm, · line 990

Quoted 6 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct mm_struct *mm,
     */
    static int __collapse_huge_page_swapin(struct mm_struct *mm,
    				       struct vm_area_struct *vma,
-				       unsigned long haddr, pmd_t *pmd,
+				       unsigned long pmd_addr, pmd_t *pmd,
    				       int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

Quoted 8 lines
Quoted 4 lines

So your suggestion is

      pmd_addr -> haddr
      pte_addr -> address

pmd_addr -> haddr
pte_addr -> addr

Take another look into the code.

Quoted 8 lines

The change touch three functions:

__collapse_huge_page_isolate()
__collapse_huge_page_swapin()
hpage_collapse_scan_pmd()

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

haddr vs. addr is just nasty.

Can we call the aligned one "aligned_addr" or something like that?

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#9 lore raw
Quoted 101 lines · David Hildenbrand
Quoted 100 lines · Wei Yang
Quoted 82 lines · Dev Jain
Quoted 80 lines · Wei Yang
Quoted 79 lines · Dev Jain
Quoted 63 lines · Wei Yang

When collapse a pmd, there are two address in use:

     * address points to the start of pmd
     * address points to each individual page

Current naming is not easy to distinguish these two and error prone.

Name the first one to pmd_addr and second one to pte_addr.

---
    mm/khugepaged.c | 43 ++++++++++++++++++++++---------------------

    1 file changed, 22 insertions(+), 21 deletions(-)
mm/khugepaged.c +4 -3
index 4c957ce788d1..6d03072c1a92 100644
@@ -537,18 +537,19 @@ static void release_pte_pages(pte_t *pte, 
 pte_t *_pte,
     }
     static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-                    unsigned long address,
+                    unsigned long pmd_addr,
                         pte_t *pte,
                         struct collapse_control *cc,
                         struct list_head *compound_pagelist)
     {
         struct page *page = NULL;
         struct folio *folio = NULL;
+    unsigned long pte_addr = pmd_addr;
         pte_t *_pte;
         int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
         for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-         _pte++, address += PAGE_SIZE) {
+         _pte++, pte_addr += PAGE_SIZE) {
             pte_t pteval = ptep_get(_pte);
             if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
                 ++none_or_zero;
@@ -570,7 +571,7 @@ static int 
 __collapse_huge_page_isolate(struct vm_area_struct *vma,
                 result = SCAN_PTE_UFFD_WP;
                 goto out;
             }
-        page = vm_normal_page(vma, address, pteval);
+        page = vm_normal_page(vma, pte_addr, pteval);
             if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
                 result = SCAN_PAGE_NULL;

                goto out;

mm/khugepaged.c +0 -0
@@ -655,8 +656,8 @@ static int 
 __collapse_huge_page_isolate(struct vm_area_struct *vma,
              */

            if (cc->is_khugepaged && (pte_young(pteval) || folio_test_young(folio) || -             folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm,
-                                     address)))
+             folio_test_referenced(folio) ||
+             mmu_notifier_test_young(vma->vm_mm, pte_addr))) referenced++;
        }

mm/khugepaged.c static int check_pmd_still_valid(struct · line 991

Quoted 7 lines
mm/khugepaged.c +1 -1
@@ -985,21 +986,21 @@ static int check_pmd_still_valid(struct 
 mm_struct *mm,
      */
     static int __collapse_huge_page_swapin(struct mm_struct *mm,
                            struct vm_area_struct *vma,
-                       unsigned long haddr, pmd_t *pmd,
+                       unsigned long pmd_addr, pmd_t *pmd,
                            int referenced)

Will this be a problem when mTHP collapse is in? You may have the starting address lying in the PTE table.

Personally "haddr" is pretty clear to me - I read it as "huge-aligned addr". I will vote for naming the starting addresses everywhere as "haddr", and use addr as the loop iterator. This is a short name and haddr will imply that it is aligned to the huge order we are collapsing for.

Quoted 8 lines
Quoted 4 lines

So your suggestion is

      pmd_addr -> haddr
      pte_addr -> address

pmd_addr -> haddr
pte_addr -> addr

Take another look into the code.

The change touch three functions:

   __collapse_huge_page_isolate()
   __collapse_huge_page_swapin()
   hpage_collapse_scan_pmd()

Quoted 6 lines
Quoted 2 lines

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

haddr vs. addr is just nasty.

Can we call the aligned one "aligned_addr" or something like that?

This works. Let us use aligned_addr/addr everywhere then.

author
#10 lore raw

On Mon, Sep 22, 2025 at 01:56:54PM +0530, Dev Jain wrote:
[...]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Take another look into the code.

The change touch three functions:

   __collapse_huge_page_isolate()
   __collapse_huge_page_swapin()
   hpage_collapse_scan_pmd()

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

haddr vs. addr is just nasty.

Can we call the aligned one "aligned_addr" or something like that?

This works. Let us use aligned_addr/addr everywhere then.

Including hpage_collapse_scan_pmd()?

Signature · 2 lines
Wei Yang
Help you, Help me
#11 lore raw
Quoted 15 lines · Wei Yang

On Mon, Sep 22, 2025 at 01:56:54PM +0530, Dev Jain wrote:
[...]

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 1 line

Take another look into the code.

The change touch three functions:

   __collapse_huge_page_isolate()
   __collapse_huge_page_swapin()
   hpage_collapse_scan_pmd()

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

haddr vs. addr is just nasty.

Can we call the aligned one "aligned_addr" or something like that?

This works. Let us use aligned_addr/addr everywhere then.

Including hpage_collapse_scan_pmd()?

Yes. Anything is better than what we have right now :)

If we just want to express "this is the start of the new THP", simply "start_addr" might also do?

Signature · 3 lines
Cheers

David / dhildenb
author
#12 lore raw
Quoted 23 lines · David Hildenbrand
Quoted 22 lines · Wei Yang

On Mon, Sep 22, 2025 at 01:56:54PM +0530, Dev Jain wrote:
[...]

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Take another look into the code.

The change touch three functions:

   __collapse_huge_page_isolate()
   __collapse_huge_page_swapin()
   hpage_collapse_scan_pmd()

Quoted 6 lines
Quoted 2 lines

Use pmd_addr/pte_addr look reasonable for hpage_collapse_scan_pmd(). And haddr/addr would be suitable for the other two.

haddr vs. addr is just nasty.

Can we call the aligned one "aligned_addr" or something like that?

This works. Let us use aligned_addr/addr everywhere then.

Quoted 7 lines
Quoted 1 line

Including hpage_collapse_scan_pmd()?

Yes. Anything is better than what we have right now :)

If we just want to express "this is the start of the new THP", simply "start_addr" might also do?

OK, start_addr/addr looks good to me.

Thanks

Quoted 4 lines
Signature · 3 lines
Cheers

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me