Skip to contentSkip to conversation

unify PMD scan results and remove redundant cleanup

PATCH Applied Wei Yang 5 participants 13 messages Nov 14, 2025 – Nov 17, 2025

Subsystems (3)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

v2 cover unify PMD scan results and remove redundant cleanup

This small series addresses two minor cleanup opportunities in the hugepage collapse logic.

The initial motivation arose during a code review of madvise_collapse(), where it was noted that the function was missing a handler for SCAN_PMD_NONE. This oversight exposed the inconsistent handling of SCAN_PMD_NULL and SCAN_PMD_NONE.

Since both scan results are functionally identical (they indicate the absence of a PTE table), the primary patch unifies them into a single, clearer identifier, SCAN_NO_PTE_TABLE.

The series also takes the opportunity to remove a redundant clearing of the struct collapse_control.

---
v1: http://lkml.kernel.org/r/[email protected]

Wei Yang (3):
mm/khugepaged: remove redundant clearing of struct collapse_control mm/khugepaged: continue to collapse on SCAN_PMD_NONE mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into
SCAN_NO_PTE_TABLE

 include/trace/events/huge_memory.h |  3 +--
 mm/khugepaged.c                    | 24 ++++++++++--------------
 2 files changed, 11 insertions(+), 16 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/3 mm/khugepaged: remove redundant clearing of struct collapse_control

The structure struct collapse_control is being unnecessarily cleared twice during the huge page collapse process.

Both hpage_collapse_scan_file() and hpage_collapse_scan_pmd() currently perform a clear operation on this structure.

Remove the redundant clear operation.

---
 mm/khugepaged.c | 2 --
 1 file changed, 2 deletions(-)
mm/khugepaged.c +0 -2
index 7e8cb181d5bd..1fc8986a28b3 100644
@@ -2803,8 +2803,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);
 		}
 		mmap_assert_locked(mm);
-		memset(cc->node_load, 0, sizeof(cc->node_load));
-		nodes_clear(cc->alloc_nmask);
 		if (!vma_is_anonymous(vma)) {
 			struct file *file = get_file(vma->vm_file);
 			pgoff_t pgoff = linear_page_index(vma, addr);
Signature · 1 line
2.34.1
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [Patch v2 1/3] mm/khugepaged: remove redundant clearing of struct collapse_control

Quoted 18 lines · Wei Yang

The structure struct collapse_control is being unnecessarily cleared twice during the huge page collapse process.

Both hpage_collapse_scan_file() and hpage_collapse_scan_pmd() currently perform a clear operation on this structure.

Remove the redundant clear operation.

---
  mm/khugepaged.c | 2 --
  1 file changed, 2 deletions(-)
mm/khugepaged.c +0 -0
index 7e8cb181d5bd..1fc8986a28b3 100644

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2808

Quoted 8 lines
mm/khugepaged.c +0 -2
@@ -2803,8 +2803,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
  			hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);
  		}
  		mmap_assert_locked(mm);
-		memset(cc->node_load, 0, sizeof(cc->node_load));
-		nodes_clear(cc->alloc_nmask);
  		if (!vma_is_anonymous(vma)) {
  			struct file *file = get_file(vma->vm_file);
  			pgoff_t pgoff = linear_page_index(vma, addr);
maintainer Acked-by on v2 1/3
#3 lore raw

Re: [Patch v2 1/3] mm/khugepaged: remove redundant clearing of struct collapse_control

In reply to Wei Yang
Quoted 9 lines · Wei Yang

The structure struct collapse_control is being unnecessarily cleared twice during the huge page collapse process.

Both hpage_collapse_scan_file() and hpage_collapse_scan_pmd() currently perform a clear operation on this structure.

Remove the redundant clear operation.

Right, and the structure does not seem to get used in between. Thanks!

Signature · 3 lines
Cheers

David
reviewer Reviewed-by on v2 1/3
#4 lore raw

Re: [Patch v2 1/3] mm/khugepaged: remove redundant clearing of struct collapse_control

In reply to Wei Yang
Quoted 10 lines · Wei Yang

The structure struct collapse_control is being unnecessarily cleared twice during the huge page collapse process.

Both hpage_collapse_scan_file() and hpage_collapse_scan_pmd() currently perform a clear operation on this structure.

Remove the redundant clear operation.

---

LGTM.

Reviewed-by on v2 1/3
#5 lore raw

Re: [Patch v2 1/3] mm/khugepaged: remove redundant clearing of struct collapse_control

In reply to Wei Yang
Quoted 8 lines · Wei Yang

The structure struct collapse_control is being unnecessarily cleared twice during the huge page collapse process.

Both hpage_collapse_scan_file() and hpage_collapse_scan_pmd() currently perform a clear operation on this structure.

Remove the redundant clear operation.

Nice! The more we can simplify/unify madvise_collapse/collapse the better :)

LGTM!

Quoted 22 lines
---
 mm/khugepaged.c | 2 --
 1 file changed, 2 deletions(-)
mm/khugepaged.c +0 -3
index 7e8cb181d5bd..1fc8986a28b3 100644
@@ -2803,8 +2803,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                        hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);
                }
                mmap_assert_locked(mm);
-               memset(cc->node_load, 0, sizeof(cc->node_load));
-               nodes_clear(cc->alloc_nmask);
                if (!vma_is_anonymous(vma)) {
                        struct file *file = get_file(vma->vm_file);
                        pgoff_t pgoff = linear_page_index(vma, addr);
--

2.34.1

author
#6 lore raw

PATCH v2 2/3 mm/khugepaged: continue to collapse on SCAN_PMD_NONE

In reply to Wei Yang

SCAN_PMD_NONE means current pmd is empty, but we can still continue collapse next pmd range.

---
 mm/khugepaged.c | 1 +
 1 file changed, 1 insertion(+)
mm/khugepaged.c +1 -0
index 1fc8986a28b3..2ee5048b764e 100644
@@ -2833,6 +2833,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			goto handle_result;
 		/* Whitelisted set of results where continuing OK */
 		case SCAN_PMD_NULL:
+		case SCAN_PMD_NONE:
 		case SCAN_PTE_NON_PRESENT:
 		case SCAN_PTE_UFFD_WP:
 		case SCAN_LACK_REFERENCED_PAGE:
Signature · 1 line
2.34.1
maintainer Acked-by on v2 2/3
#7 lore raw

Re: [Patch v2 2/3] mm/khugepaged: continue to collapse on SCAN_PMD_NONE

Quoted 8 lines · Wei Yang

SCAN_PMD_NONE means current pmd is empty, but we can still continue collapse next pmd range.

---

Signature · 3 lines
Cheers

David
author
#8 lore raw

PATCH v2 3/3 mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into SCAN_NO_PTE_TABLE

In reply to Wei Yang

The current hugepage collapse scan results include two separate values, SCAN_PMD_NONE and SCAN_PMD_NULL, which are handled identically by the consuming code.

To reduce confusion and improve long-term maintenance, this commit merges these two functionally equivalent states into a single, clearer
identifier: SCAN_NO_PTE_TABLE

---
 include/trace/events/huge_memory.h |  3 +--
 mm/khugepaged.c                    | 23 ++++++++++-------------
 2 files changed, 11 insertions(+), 15 deletions(-)
include/trace/events/huge_memory.h +1 -2
index dd94d14a2427..4cde53b45a85 100644
@@ -10,8 +10,7 @@
 #define SCAN_STATUS							\
 	EM( SCAN_FAIL,			"failed")			\
 	EM( SCAN_SUCCEED,		"succeeded")			\
-	EM( SCAN_PMD_NULL,		"pmd_null")			\
-	EM( SCAN_PMD_NONE,		"pmd_none")			\
+	EM( SCAN_NO_PTE_TABLE,		"no_pte_table")			\
 	EM( SCAN_PMD_MAPPED,		"page_pmd_mapped")		\
 	EM( SCAN_EXCEED_NONE_PTE,	"exceed_none_pte")		\
 	EM( SCAN_EXCEED_SWAP_PTE,	"exceed_swap_pte")		\
mm/khugepaged.c +10 -13
index 2ee5048b764e..40f9d5939aa5 100644
@@ -30,8 +30,7 @@
 enum scan_result {
 	SCAN_FAIL,
 	SCAN_SUCCEED,
-	SCAN_PMD_NULL,
-	SCAN_PMD_NONE,
+	SCAN_NO_PTE_TABLE,
 	SCAN_PMD_MAPPED,
 	SCAN_EXCEED_NONE_PTE,
 	SCAN_EXCEED_SWAP_PTE,
@@ -934,7 +933,7 @@ static inline int check_pmd_state(pmd_t *pmd)
 	pmd_t pmde = pmdp_get_lockless(pmd);
 
 	if (pmd_none(pmde))
-		return SCAN_PMD_NONE;
+		return SCAN_NO_PTE_TABLE;
 
 	/*
 	 * The folio may be under migration when khugepaged is trying to
@@ -944,11 +943,11 @@ static inline int check_pmd_state(pmd_t *pmd)
 	if (pmd_is_migration_entry(pmde))
 		return SCAN_PMD_MAPPED;
 	if (!pmd_present(pmde))
-		return SCAN_PMD_NULL;
+		return SCAN_NO_PTE_TABLE;
 	if (pmd_trans_huge(pmde))
 		return SCAN_PMD_MAPPED;
 	if (pmd_bad(pmde))
-		return SCAN_PMD_NULL;
+		return SCAN_NO_PTE_TABLE;
 	return SCAN_SUCCEED;
 }
 
@@ -958,7 +957,7 @@ static int find_pmd_or_thp_or_none(struct mm_struct *mm,
 {
 	*pmd = mm_find_pmd(mm, address);
 	if (!*pmd)
-		return SCAN_PMD_NULL;
+		return SCAN_NO_PTE_TABLE;
 
 	return check_pmd_state(*pmd);
 }
@@ -1013,7 +1012,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
 			pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
 			if (!pte) {
 				mmap_read_unlock(mm);
-				result = SCAN_PMD_NULL;
+				result = SCAN_NO_PTE_TABLE;
 				goto out;
 			}
 		}
@@ -1187,7 +1186,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 						      &compound_pagelist);
 		spin_unlock(pte_ptl);
 	} else {
-		result = SCAN_PMD_NULL;
+		result = SCAN_NO_PTE_TABLE;
 	}
 
 	if (unlikely(result != SCAN_SUCCEED)) {
@@ -1270,7 +1269,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 	nodes_clear(cc->alloc_nmask);
 	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
 	if (!pte) {
-		result = SCAN_PMD_NULL;
+		result = SCAN_NO_PTE_TABLE;
 		goto out;
 	}
 
@@ -1544,8 +1543,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	switch (result) {
 	case SCAN_SUCCEED:
 		break;
-	case SCAN_PMD_NULL:
-	case SCAN_PMD_NONE:
+	case SCAN_NO_PTE_TABLE:
 		/*
 		 * All pte entries have been removed and pmd cleared.
 		 * Skip all the pte checks and just update the pmd mapping.
@@ -2832,8 +2830,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_read_unlock(mm);
 			goto handle_result;
 		/* Whitelisted set of results where continuing OK */
-		case SCAN_PMD_NULL:
-		case SCAN_PMD_NONE:
+		case SCAN_NO_PTE_TABLE:
 		case SCAN_PTE_NON_PRESENT:
 		case SCAN_PTE_UFFD_WP:
 		case SCAN_LACK_REFERENCED_PAGE:
Signature · 1 line
2.34.1
reviewer Reviewed-by on v2 3/3
#9 lore raw

Re: [Patch v2 3/3] mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into SCAN_NO_PTE_TABLE

Quoted 4 lines · Wei Yang

The current hugepage collapse scan results include two separate values, SCAN_PMD_NONE and SCAN_PMD_NULL, which are handled identically by the consuming code.

Quoted 8 lines

To reduce confusion and improve long-term maintenance, this commit merges these two functionally equivalent states into a single, clearer
identifier: SCAN_NO_PTE_TABLE

---

maintainer Acked-by on v2 3/3
#10 lore raw

Re: [Patch v2 3/3] mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into SCAN_NO_PTE_TABLE

In reply to Wei Yang
Quoted 11 lines · Wei Yang

The current hugepage collapse scan results include two separate values, SCAN_PMD_NONE and SCAN_PMD_NULL, which are handled identically by the consuming code.

To reduce confusion and improve long-term maintenance, this commit merges these two functionally equivalent states into a single, clearer
identifier: SCAN_NO_PTE_TABLE

---

Signature · 3 lines
Cheers

David
reviewer Reviewed-by on v2 3/3
#11 lore raw

Re: [Patch v2 3/3] mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into SCAN_NO_PTE_TABLE

In reply to Wei Yang
Quoted 11 lines · Wei Yang

The current hugepage collapse scan results include two separate values, SCAN_PMD_NONE and SCAN_PMD_NULL, which are handled identically by the consuming code.

To reduce confusion and improve long-term maintenance, this commit merges these two functionally equivalent states into a single, clearer
identifier: SCAN_NO_PTE_TABLE

---

Looks cleaner. Thanks.

Reviewed-by on v2 3/3
#12 lore raw

Re: [Patch v2 3/3] mm/khugepaged: unify SCAN_PMD_NONE and SCAN_PMD_NULL into SCAN_NO_PTE_TABLE

In reply to Wei Yang
Quoted 8 lines · Wei Yang

The current hugepage collapse scan results include two separate values, SCAN_PMD_NONE and SCAN_PMD_NULL, which are handled identically by the consuming code.

To reduce confusion and improve long-term maintenance, this commit merges these two functionally equivalent states into a single, clearer
identifier: SCAN_NO_PTE_TABLE

Nice cleanup!

Quoted 118 lines
---
 include/trace/events/huge_memory.h |  3 +--
 mm/khugepaged.c                    | 23 ++++++++++-------------
 2 files changed, 11 insertions(+), 15 deletions(-)
include/trace/events/huge_memory.h +1 -2
index dd94d14a2427..4cde53b45a85 100644
@@ -10,8 +10,7 @@
 #define SCAN_STATUS                                                    \
        EM( SCAN_FAIL,                  "failed")                       \
        EM( SCAN_SUCCEED,               "succeeded")                    \
-       EM( SCAN_PMD_NULL,              "pmd_null")                     \
-       EM( SCAN_PMD_NONE,              "pmd_none")                     \
+       EM( SCAN_NO_PTE_TABLE,          "no_pte_table")                 \
        EM( SCAN_PMD_MAPPED,            "page_pmd_mapped")              \
        EM( SCAN_EXCEED_NONE_PTE,       "exceed_none_pte")              \
        EM( SCAN_EXCEED_SWAP_PTE,       "exceed_swap_pte")              \
mm/khugepaged.c +10 -14
index 2ee5048b764e..40f9d5939aa5 100644
@@ -30,8 +30,7 @@
 enum scan_result {
        SCAN_FAIL,
        SCAN_SUCCEED,
-       SCAN_PMD_NULL,
-       SCAN_PMD_NONE,
+       SCAN_NO_PTE_TABLE,
        SCAN_PMD_MAPPED,
        SCAN_EXCEED_NONE_PTE,
        SCAN_EXCEED_SWAP_PTE,
@@ -934,7 +933,7 @@ static inline int check_pmd_state(pmd_t *pmd)
        pmd_t pmde = pmdp_get_lockless(pmd);
 
        if (pmd_none(pmde))
-               return SCAN_PMD_NONE;
+               return SCAN_NO_PTE_TABLE;
 
        /*
         * The folio may be under migration when khugepaged is trying to
@@ -944,11 +943,11 @@ static inline int check_pmd_state(pmd_t *pmd)
        if (pmd_is_migration_entry(pmde))
                return SCAN_PMD_MAPPED;
        if (!pmd_present(pmde))
-               return SCAN_PMD_NULL;
+               return SCAN_NO_PTE_TABLE;
        if (pmd_trans_huge(pmde))
                return SCAN_PMD_MAPPED;
        if (pmd_bad(pmde))
-               return SCAN_PMD_NULL;
+               return SCAN_NO_PTE_TABLE;
        return SCAN_SUCCEED;
 }
 
@@ -958,7 +957,7 @@ static int find_pmd_or_thp_or_none(struct mm_struct *mm,
 {
        *pmd = mm_find_pmd(mm, address);
        if (!*pmd)
-               return SCAN_PMD_NULL;
+               return SCAN_NO_PTE_TABLE;
 
        return check_pmd_state(*pmd);
 }
@@ -1013,7 +1012,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
                        pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
                        if (!pte) {
                                mmap_read_unlock(mm);
-                               result = SCAN_PMD_NULL;
+                               result = SCAN_NO_PTE_TABLE;
                                goto out;
                        }
                }
@@ -1187,7 +1186,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
                                                      &compound_pagelist);
                spin_unlock(pte_ptl);
        } else {
-               result = SCAN_PMD_NULL;
+               result = SCAN_NO_PTE_TABLE;
        }
 
        if (unlikely(result != SCAN_SUCCEED)) {
@@ -1270,7 +1269,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
        nodes_clear(cc->alloc_nmask);
        pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
        if (!pte) {
-               result = SCAN_PMD_NULL;
+               result = SCAN_NO_PTE_TABLE;
                goto out;
        }
 
@@ -1544,8 +1543,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
        switch (result) {
        case SCAN_SUCCEED:
                break;
-       case SCAN_PMD_NULL:
-       case SCAN_PMD_NONE:
+       case SCAN_NO_PTE_TABLE:
                /*
                 * All pte entries have been removed and pmd cleared.
                 * Skip all the pte checks and just update the pmd mapping.
@@ -2832,8 +2830,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                        mmap_read_unlock(mm);
                        goto handle_result;
                /* Whitelisted set of results where continuing OK */
-               case SCAN_PMD_NULL:
-               case SCAN_PMD_NONE:
+               case SCAN_NO_PTE_TABLE:
                case SCAN_PTE_NON_PRESENT:
                case SCAN_PTE_UFFD_WP:
                case SCAN_LACK_REFERENCED_PAGE:
--

2.34.1