Skip to contentSkip to conversation

khugepaged: convert redundant check to WARN_ON

PATCH In review Dev Jain 5 participants 6 messages Feb 19 – Feb 24

Subsystems (2)
lore
Share & tools
lore

Dev Jain

author reviewer
#0 lore raw

PATCH 1/1 khugepaged: convert redundant check to WARN_ON

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

---
Based on mm-unstable (d9982f38eb6e). mm-selftests pass.

 mm/khugepaged.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/khugepaged.c +6 -4
index fa1e57fd2c469..f27cbb4d1f62c 100644
@@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		 * we locked the first folio, then a THP might be there already.
 		 * This will be discovered on the first iteration.
 		 */
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);
+
 			/* Maybe PMD-mapped */
 			result = SCAN_PTE_MAPPED_HUGEPAGE;
 			goto out_unlock;
@@ -2329,8 +2330,9 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			continue;
 		}
 
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);
+
 			/* Maybe PMD-mapped */
 			result = SCAN_PTE_MAPPED_HUGEPAGE;
 			/*
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 20 lines · On Thu, 19 Feb 2026 11:18:27 +0530 Dev Jain <[email protected]> wrote:

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

...

mm/khugepaged.c static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, · line 2004

Quoted 7 lines
mm/khugepaged.c +2 -2
@@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		 * we locked the first folio, then a THP might be there already.
 		 * This will be discovered on the first iteration.
 		 */
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);

It's a bad sad to remove unneeded code by retaining that code and adding even more code.

Perhaps add a comment reminding us to remove this altogether at a later date?

author reviewer
#2 lore raw
Quoted 32 lines · Andrew Morton
Quoted 20 lines · On Thu, 19 Feb 2026 11:18:27 +0530 Dev Jain <[email protected]> wrote:

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

...

mm/khugepaged.c static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, · line 2004

Quoted 7 lines
mm/khugepaged.c +2 -2
@@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		 * we locked the first folio, then a THP might be there already.
 		 * This will be discovered on the first iteration.
 		 */
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);

It's a bad sad to remove unneeded code by retaining that code and adding even more code.

Quoted 2 lines

Perhaps add a comment reminding us to remove this altogether at a later date?

But then shouldn't I remove it just now :) try_collapse_pte_mapped_thp is anways going to bail out if such a bug occurs, so nothing goes wrong. In short this check is actually useless, apart from the fact that this lets us spot a bug (which I doubt won't be figured out by other code paths already).

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Quoted 6 lines

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

---

Make sense to me. Personally, I’d like to keep this VM_WARN_ON() to catch unexpected behavior.

Quoted 33 lines

Based on mm-unstable (d9982f38eb6e). mm-selftests pass.

  mm/khugepaged.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
mm/khugepaged.c +6 -4
index fa1e57fd2c469..f27cbb4d1f62c 100644
@@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
  		 * we locked the first folio, then a THP might be there already.
  		 * This will be discovered on the first iteration.
  		 */
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);
+
  			/* Maybe PMD-mapped */
  			result = SCAN_PTE_MAPPED_HUGEPAGE;
  			goto out_unlock;
@@ -2329,8 +2330,9 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
  			continue;
  		}
 
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);
+
  			/* Maybe PMD-mapped */
  			result = SCAN_PTE_MAPPED_HUGEPAGE;
  			/*
reviewer Reviewed-by
#5 lore raw
Quoted 14 lines · Baolin Wang
Quoted 11 lines · Dev Jain

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Quoted 8 lines
Quoted 6 lines

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

---

Make sense to me. Personally, I’d like to keep this VM_WARN_ON() to catch unexpected behavior.

+1

#3 lore raw
In reply to Dev Jain
Quoted 34 lines · Dev Jain
Quoted 32 lines · Andrew Morton
Quoted 20 lines · On Thu, 19 Feb 2026 11:18:27 +0530 Dev Jain <[email protected]> wrote:

Claim: folio_order(folio) == HPAGE_PMD_ORDER => folio->index == start.

Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that
start <= folio->index < start + HPAGE_PMD_NR ... (i)
A folio is always naturally aligned in the pagecache, therefore
folio_order == HPAGE_PMD_ORDER => IS_ALIGNED(folio->index, HPAGE_PMD_NR) == true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order,
=> IS_ALIGNED(start, HPAGE_PMD_NR) == true ... (iii)

Combining (i), (ii) and (iii), the claim is proven.

Therefore, convert this to a VM_WARN_ON.

...

mm/khugepaged.c static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, · line 2004

Quoted 7 lines
mm/khugepaged.c +2 -2
@@ -2000,8 +2000,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		 * we locked the first folio, then a THP might be there already.
 		 * This will be discovered on the first iteration.
 		 */
-		if (folio_order(folio) == HPAGE_PMD_ORDER &&
-		    folio->index == start) {
+		if (folio_order(folio) == HPAGE_PMD_ORDER) {
+			VM_WARN_ON(folio->index != start);

It's a bad sad to remove unneeded code by retaining that code and adding even more code.

Quoted 4 lines
Quoted 2 lines

Perhaps add a comment reminding us to remove this altogether at a later date?

But then shouldn't I remove it just now :)

I'd say, remove it directly. Even if "folio->index != start", both functions should do the right thing.

While at it, we could heavily simplify the comments to "If there is already a PMD-sized THP, we can only try collapsing the PTE table".

Or sth, like that.

Signature · 3 lines
Cheers,

David