Skip to contentSkip to conversation

mm/khugepaged: cleanups and scan limit fix

PATCH Superseded Shivank Garg 5 participants 16 messages Dec 24, 2025 – Jan 18

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/khugepaged: cleanups and scan limit fix

This series contains several cleanups for mm/khugepaged.c to improve code readability and type safety, and one functional fix to ensure khugepaged_scan_mm_slot() correctly accounts for small VMAs towards scan limit.

Thanks,

v2:
- Added a fix for small VMAs not being counted in the scan limit (Wei)
- Updated 'progress' to 'unsigned int' to match types
- Update return types of internal functions to use enum scan_result (Zi)
- Add void wrapper collapse_pte_mapped_thp() for external callers to avoid exposing internal enum (David)

v1:
https://lore.kernel.org/linux-mm/[email protected]

Shivank Garg (5):
mm/khugepaged: remove unnecessary goto 'skip' label mm/khugepaged: count small VMAs towards scan limit mm/khugepaged: change collapse_pte_mapped_thp() to return void mm/khugepaged: use enum scan_result for result variables and return
types
mm/khugepaged: make khugepaged_collapse_control static

 include/linux/khugepaged.h |   9 +--
 mm/khugepaged.c            | 158 ++++++++++++++++++++-----------------
 2 files changed, 88 insertions(+), 79 deletions(-)

base-commit: cd119c65a615bd7bfe8cda715a77132c8e3da067

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/5 mm/khugepaged: remove unnecessary goto 'skip' label

Replace goto skip with actual logic for better code readability.

No functional change.

---
 mm/khugepaged.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -3
index 6c8c35d3e0c9..107146f012b1 100644
@@ -2442,14 +2442,15 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 			break;
 		}
 		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
-skip:
 			progress++;
 			continue;
 		}
 		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
 		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend)
-			goto skip;
+		if (khugepaged_scan.address > hend) {
+			progress++;
+			continue;
+		}
 		if (khugepaged_scan.address < hstart)
 			khugepaged_scan.address = hstart;
 		VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH V2 1/5] mm/khugepaged: remove unnecessary goto 'skip' label

Quoted 9 lines · Shivank Garg

Replace goto skip with actual logic for better code readability.

No functional change.

---

LGTM.

Quoted 26 lines
  mm/khugepaged.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -3
index 6c8c35d3e0c9..107146f012b1 100644
@@ -2442,14 +2442,15 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  			break;
  		}
  		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
-skip:
  			progress++;
  			continue;
  		}
  		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
  		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend)
-			goto skip;
+		if (khugepaged_scan.address > hend) {
+			progress++;
+			continue;
+		}
  		if (khugepaged_scan.address < hstart)
  			khugepaged_scan.address = hstart;
  		VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
#3 lore raw

PATCH v2 2/5 mm/khugepaged: count small VMAs towards scan limit

In reply to Shivank Garg

The khugepaged_scan_mm_slot() uses a 'progress' counter to limit the amount of work performed and consists of three components:
1. Transitioning to a new mm (+1).
2. Skipping an unsuitable VMA (+1).
3. Scanning a PMD-sized range (+HPAGE_PMD_NR).

Consider a 1MB VMA sitting between two 2MB alignment boundaries:

     vma1       vma2   vma3
    +----------+------+----------+
    |2M        |1M    |2M        |
    +----------+------+----------+
               ^      ^
               start  end
               ^
          hstart,hend

In this case, for vma2:
hstart = round_up(start, HPAGE_PMD_SIZE) -> Next 2MB alignment
hend = round_down(end, HPAGE_PMD_SIZE) -> Prev 2MB alignment

Currently, since hend <= hstart, VMAs that are too small or unaligned to contain a hugepage are skipped without incrementing 'progress'. A process containing a large number of such small VMAs will unfairly consume more CPU cycles before yielding compared to a process with fewer, larger, or aligned VMAs.

Fix this by incrementing progress when the hend <= hstart condition is met.

Additionally, change 'progress' type to unsigned int to match both the 'pages' type and the function return value.

---
 mm/khugepaged.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/khugepaged.c +2 -2
index 107146f012b1..0b549c3250f9 100644
@@ -2403,7 +2403,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	struct mm_slot *slot;
 	struct mm_struct *mm;
 	struct vm_area_struct *vma;
-	int progress = 0;
+	unsigned int progress = 0;
 
 	VM_BUG_ON(!pages);
 	lockdep_assert_held(&khugepaged_mm_lock);
@@ -2447,7 +2447,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 		}
 		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
 		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend) {
+		if (khugepaged_scan.address > hend || hend <= hstart) {
 			progress++;
 			continue;
 		}
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH V2 2/5] mm/khugepaged: count small VMAs towards scan limit

Quoted 53 lines · Shivank Garg

The khugepaged_scan_mm_slot() uses a 'progress' counter to limit the amount of work performed and consists of three components:
1. Transitioning to a new mm (+1).
2. Skipping an unsuitable VMA (+1).
3. Scanning a PMD-sized range (+HPAGE_PMD_NR).

Consider a 1MB VMA sitting between two 2MB alignment boundaries:

      vma1       vma2   vma3
     +----------+------+----------+
     |2M        |1M    |2M        |
     +----------+------+----------+
                ^      ^
                start  end
                ^
           hstart,hend

In this case, for vma2:

   hstart = round_up(start, HPAGE_PMD_SIZE)  -> Next 2MB alignment
   hend   = round_down(end, HPAGE_PMD_SIZE) -> Prev 2MB alignment

Currently, since hend <= hstart, VMAs that are too small or unaligned to contain a hugepage are skipped without incrementing 'progress'. A process containing a large number of such small VMAs will unfairly consume more CPU cycles before yielding compared to a process with fewer, larger, or aligned VMAs.

Fix this by incrementing progress when the hend <= hstart condition is met.

Additionally, change 'progress' type to unsigned int to match both the 'pages' type and the function return value.

---
  mm/khugepaged.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/khugepaged.c +1 -1
index 107146f012b1..0b549c3250f9 100644
@@ -2403,7 +2403,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  	struct mm_slot *slot;
  	struct mm_struct *mm;
  	struct vm_area_struct *vma;
-	int progress = 0;
+	unsigned int progress = 0;
 
  	VM_BUG_ON(!pages);
  	lockdep_assert_held(&khugepaged_mm_lock);

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2450

Quoted 4 lines
mm/khugepaged.c +0 -1
@@ -2447,7 +2447,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  		}
  		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
  		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend) {

Maybe add a short comment explaining why we increment progress for small VMAs ;)

Something like this:

		/* Count small VMAs that can't hold a hugepage towards scan limit */

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2453

Quoted 4 lines
mm/khugepaged.c +1 -0
+		if (khugepaged_scan.address > hend || hend <= hstart) {
  			progress++;
  			continue;
  		}

Otherwise, looks good to me.

Reviewed-by
#5 lore raw

Re: [PATCH V2 2/5] mm/khugepaged: count small VMAs towards scan limit

Quoted 6 lines · Lance Yang
Quoted 3 lines · Shivank Garg

The khugepaged_scan_mm_slot() uses a 'progress' counter to limit the amount of work performed and consists of three components:
1. Transitioning to a new mm (+1).

Hmm... maybe not only a new mm, but also we start another scan from last mm.

Since default khugepaged_pages_to_scan is 8 PMD, it looks very possible.

Quoted 60 lines
Quoted 49 lines

2. Skipping an unsuitable VMA (+1).
3. Scanning a PMD-sized range (+HPAGE_PMD_NR).

Consider a 1MB VMA sitting between two 2MB alignment boundaries:

      vma1       vma2   vma3
     +----------+------+----------+
     |2M        |1M    |2M        |
     +----------+------+----------+
                ^      ^
                start  end
                ^
           hstart,hend

In this case, for vma2:

   hstart = round_up(start, HPAGE_PMD_SIZE)  -> Next 2MB alignment
   hend   = round_down(end, HPAGE_PMD_SIZE) -> Prev 2MB alignment

Currently, since hend <= hstart, VMAs that are too small or unaligned to contain a hugepage are skipped without incrementing 'progress'. A process containing a large number of such small VMAs will unfairly consume more CPU cycles before yielding compared to a process with fewer, larger, or aligned VMAs.

Fix this by incrementing progress when the hend <= hstart condition is met.

Additionally, change 'progress' type to unsigned int to match both the 'pages' type and the function return value.

---
  mm/khugepaged.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/khugepaged.c +1 -1
index 107146f012b1..0b549c3250f9 100644
@@ -2403,7 +2403,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  	struct mm_slot *slot;
  	struct mm_struct *mm;
  	struct vm_area_struct *vma;
-	int progress = 0;
+	unsigned int progress = 0;
  	VM_BUG_ON(!pages);
  	lockdep_assert_held(&khugepaged_mm_lock);

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2450

Quoted 4 lines
mm/khugepaged.c +0 -1
@@ -2447,7 +2447,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  		}
  		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
  		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend) {

Maybe add a short comment explaining why we increment progress for small VMAs ;)

Something like this:

		/* Count small VMAs that can't hold a hugepage towards scan limit */
Quoted 9 lines

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2453

Quoted 4 lines
mm/khugepaged.c +1 -0
+		if (khugepaged_scan.address > hend || hend <= hstart) {
  			progress++;
  			continue;
  		}

Otherwise, looks good to me.

The code change LGTM.

Signature · 2 lines
Wei Yang
Help you, Help me
author Reviewed-by Wei Yang Reviewed-by Lance Yang
#6 lore raw

Re: [PATCH V2 2/5] mm/khugepaged: count small VMAs towards scan limit

Quoted 12 lines · Wei Yang
Quoted 6 lines · Lance Yang
Quoted 3 lines · Shivank Garg

The khugepaged_scan_mm_slot() uses a 'progress' counter to limit the amount of work performed and consists of three components:
1. Transitioning to a new mm (+1).

Hmm... maybe not only a new mm, but also we start another scan from last mm.

Since default khugepaged_pages_to_scan is 8 PMD, it looks very possible.

It makes sense, will correct this.

Quoted 57 lines
Quoted 57 lines
Quoted 49 lines

2. Skipping an unsuitable VMA (+1).
3. Scanning a PMD-sized range (+HPAGE_PMD_NR).

Consider a 1MB VMA sitting between two 2MB alignment boundaries:

      vma1       vma2   vma3
     +----------+------+----------+
     |2M        |1M    |2M        |
     +----------+------+----------+
                ^      ^
                start  end
                ^
           hstart,hend

In this case, for vma2:

   hstart = round_up(start, HPAGE_PMD_SIZE)  -> Next 2MB alignment
   hend   = round_down(end, HPAGE_PMD_SIZE) -> Prev 2MB alignment

Currently, since hend <= hstart, VMAs that are too small or unaligned to contain a hugepage are skipped without incrementing 'progress'. A process containing a large number of such small VMAs will unfairly consume more CPU cycles before yielding compared to a process with fewer, larger, or aligned VMAs.

Fix this by incrementing progress when the hend <= hstart condition is met.

Additionally, change 'progress' type to unsigned int to match both the 'pages' type and the function return value.

---
  mm/khugepaged.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/khugepaged.c +1 -1
index 107146f012b1..0b549c3250f9 100644
@@ -2403,7 +2403,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  	struct mm_slot *slot;
  	struct mm_struct *mm;
  	struct vm_area_struct *vma;
-	int progress = 0;
+	unsigned int progress = 0;
  	VM_BUG_ON(!pages);
  	lockdep_assert_held(&khugepaged_mm_lock);

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2450

Quoted 4 lines
mm/khugepaged.c +0 -1
@@ -2447,7 +2447,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  		}
  		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
  		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend) {

Maybe add a short comment explaining why we increment progress for small VMAs ;)

Quoted 3 lines
Quoted 3 lines

Something like this:

		/* Count small VMAs that can't hold a hugepage towards scan limit */

I'll add explanation.

Quoted 7 lines
Quoted 7 lines

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2453

Quoted 4 lines
mm/khugepaged.c +1 -0
+		if (khugepaged_scan.address > hend || hend <= hstart) {
  			progress++;
  			continue;
  		}

Otherwise, looks good to me.

Quoted 7 lines
Quoted 2 lines

The code change LGTM.

Thanks Lance and Wei. I have made suggested changes.

From d464604c09cef70f0f2aa0f9607a977b4bcd7081 Mon Sep 17 00:00:00 2001
From: Shivank Garg <[email protected]>
Date: Wed, 17 Dec 2025 19:43:26 +0000
Subject: [PATCH V2 2/5] mm/khugepaged: count small VMAs towards scan limit

The khugepaged_scan_mm_slot() uses a 'progress' counter to limit the amount of work performed and consists of three components:
1. Starting/resuming scan of mm slot (+1).
2. Skipping an unsuitable VMA (+1).
3. Scanning a PMD-sized range (+HPAGE_PMD_NR).

Consider a 1MB VMA sitting between two 2MB alignment boundaries:

     vma1       vma2   vma3
    +----------+------+----------+
    |2M        |1M    |2M        |
    +----------+------+----------+
               ^      ^
               start  end
               ^
          hstart,hend

In this case, for vma2:
hstart = round_up(start, HPAGE_PMD_SIZE) -> Next 2MB alignment
hend = round_down(end, HPAGE_PMD_SIZE) -> Prev 2MB alignment

Currently, since hend <= hstart, VMAs that are too small or unaligned to contain a hugepage are skipped without incrementing 'progress'. A process containing a large number of such small VMAs will unfairly consume more CPU cycles before yielding compared to a process with fewer, larger, or aligned VMAs.

Fix this by incrementing progress when the hend <= hstart condition is met.

Additionally, change 'progress' to unsigned int. This matches both the 'pages' type and the function return value.

---
 mm/khugepaged.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/khugepaged.c +3 -2
index 107146f012b1..155281c49169 100644
@@ -2403,7 +2403,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	struct mm_slot *slot;
 	struct mm_struct *mm;
 	struct vm_area_struct *vma;
-	int progress = 0;
+	unsigned int progress = 0;
 
 	VM_BUG_ON(!pages);
 	lockdep_assert_held(&khugepaged_mm_lock);
@@ -2447,7 +2447,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 		}
 		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
 		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
-		if (khugepaged_scan.address > hend) {
+		if (khugepaged_scan.address > hend || hend <= hstart) {
+			/* VMA already scanned or too small/unaligned for hugepage. */
 			progress++;
 			continue;
 		}
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 3/5 mm/khugepaged: change collapse_pte_mapped_thp() to return void

In reply to Shivank Garg

The only external caller of collapse_pte_mapped_thp() is uprobe, which ignores the return value. Change the external API to return void to simplify the interface.

Introduce try_collapse_pte_mapped_thp() for internal use that preserves the return value. This prepares for future patch that will convert the return type to use enum scan_result.

---
 include/linux/khugepaged.h |  9 ++++-----
 mm/khugepaged.c            | 40 ++++++++++++++++++++++----------------
 2 files changed, 27 insertions(+), 22 deletions(-)
include/linux/khugepaged.h +4 -5
index eb1946a70cff..37b992b22bba 100644
@@ -17,8 +17,8 @@ extern void khugepaged_enter_vma(struct vm_area_struct *vma,
 				 vm_flags_t vm_flags);
 extern void khugepaged_min_free_kbytes_update(void);
 extern bool current_is_khugepaged(void);
-extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
-				   bool install_pmd);
+extern void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+				    bool install_pmd);
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
@@ -42,10 +42,9 @@ static inline void khugepaged_enter_vma(struct vm_area_struct *vma,
 					vm_flags_t vm_flags)
 {
 }
-static inline int collapse_pte_mapped_thp(struct mm_struct *mm,
-					  unsigned long addr, bool install_pmd)
+static inline void collapse_pte_mapped_thp(struct mm_struct *mm,
+					   unsigned long addr, bool install_pmd)
 {
-	return 0;
 }
 
 static inline void khugepaged_min_free_kbytes_update(void)
mm/khugepaged.c +23 -17
index 0b549c3250f9..04ff0730c9a1 100644
@@ -1477,20 +1477,8 @@ static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
 	return SCAN_SUCCEED;
 }
 
-/**
- * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
- * address haddr.
- *
- * @mm: process address space where collapse happens
- * @addr: THP collapse address
- * @install_pmd: If a huge PMD should be installed
- *
- * This function checks whether all the PTEs in the PMD are pointing to the
- * right THP. If so, retract the page table so the THP can refault in with
- * as pmd-mapped. Possibly install a huge PMD mapping the THP.
- */
-int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
-			    bool install_pmd)
+static int try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+				       bool install_pmd)
 {
 	int nr_mapped_ptes = 0, result = SCAN_FAIL;
 	unsigned int nr_batch_ptes;
@@ -1711,6 +1699,24 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
+/**
+ * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
+ * address haddr.
+ *
+ * @mm: process address space where collapse happens
+ * @addr: THP collapse address
+ * @install_pmd: If a huge PMD should be installed
+ *
+ * This function checks whether all the PTEs in the PMD are pointing to the
+ * right THP. If so, retract the page table so the THP can refault in with
+ * as pmd-mapped. Possibly install a huge PMD mapping the THP.
+ */
+void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+			     bool install_pmd)
+{
+	try_collapse_pte_mapped_thp(mm, addr, install_pmd);
+}
+
 /* Can we retract page tables for this file-backed VMA? */
 static bool file_backed_vma_is_retractable(struct vm_area_struct *vma)
 {
@@ -2227,7 +2233,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
 
 	/*
 	 * Remove pte page tables, so we can re-fault the page as huge.
-	 * If MADV_COLLAPSE, adjust result to call collapse_pte_mapped_thp().
+	 * If MADV_COLLAPSE, adjust result to call try_collapse_pte_mapped_thp().
 	 */
 	retract_page_tables(mapping, start);
 	if (cc && !cc->is_khugepaged)
@@ -2479,7 +2485,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 					mmap_read_lock(mm);
 					if (hpage_collapse_test_exit_or_disable(mm))
 						goto breakouterloop;
-					*result = collapse_pte_mapped_thp(mm,
+					*result = try_collapse_pte_mapped_thp(mm,
 						khugepaged_scan.address, false);
 					if (*result == SCAN_PMD_MAPPED)
 						*result = SCAN_SUCCEED;
@@ -2869,7 +2875,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 		case SCAN_PTE_MAPPED_HUGEPAGE:
 			BUG_ON(mmap_locked);
 			mmap_read_lock(mm);
-			result = collapse_pte_mapped_thp(mm, addr, true);
+			result = try_collapse_pte_mapped_thp(mm, addr, true);
 			mmap_read_unlock(mm);
 			goto handle_result;
 		/* Whitelisted set of results where continuing OK */
Signature · 1 line
2.43.0
reviewer Acked-by on v2 3/5
#8 lore raw

Re: [PATCH V2 3/5] mm/khugepaged: change collapse_pte_mapped_thp() to return void

Quoted 11 lines · Shivank Garg

The only external caller of collapse_pte_mapped_thp() is uprobe, which ignores the return value. Change the external API to return void to simplify the interface.

Introduce try_collapse_pte_mapped_thp() for internal use that preserves the return value. This prepares for future patch that will convert the return type to use enum scan_result.

---

Looks good overall, thanks!

reviewer Reviewed-by on v2 3/5
#9 lore raw

Re: [PATCH V2 3/5] mm/khugepaged: change collapse_pte_mapped_thp() to return void

In reply to Shivank Garg
Quoted 8 lines · Shivank Garg

The only external caller of collapse_pte_mapped_thp() is uprobe, which ignores the return value. Change the external API to return void to simplify the interface.

Introduce try_collapse_pte_mapped_thp() for internal use that preserves the return value. This prepares for future patch that will convert the return type to use enum scan_result.

Quoted 7 lines
---
 include/linux/khugepaged.h |  9 ++++-----
 mm/khugepaged.c            | 40 ++++++++++++++++++++++----------------
 2 files changed, 27 insertions(+), 22 deletions(-)

LGTM.

--
Best Regards,
Yan, Zi

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

Re: [PATCH V2 3/5] mm/khugepaged: change collapse_pte_mapped_thp() to return void

In reply to Shivank Garg
Quoted 20 lines · Shivank Garg

The only external caller of collapse_pte_mapped_thp() is uprobe, which ignores the return value. Change the external API to return void to simplify the interface.

Introduce try_collapse_pte_mapped_thp() for internal use that preserves the return value. This prepares for future patch that will convert the return type to use enum scan_result.

---
  include/linux/khugepaged.h |  9 ++++-----
  mm/khugepaged.c            | 40 ++++++++++++++++++++++----------------
  2 files changed, 27 insertions(+), 22 deletions(-)
include/linux/khugepaged.h +0 -0
index eb1946a70cff..37b992b22bba 100644

include/linux/khugepaged.h extern void khugepaged_enter_vma(struct vm_area_struct *vma, · line 21

Quoted 7 lines
include/linux/khugepaged.h +2 -2
@@ -17,8 +17,8 @@ extern void khugepaged_enter_vma(struct vm_area_struct *vma,
  				 vm_flags_t vm_flags);
  extern void khugepaged_min_free_kbytes_update(void);
  extern bool current_is_khugepaged(void);
-extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
-				   bool install_pmd);
+extern void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+				    bool install_pmd);

Sorry for late review.

We remove the "extern" whenever we touch functions.

Nit: two tab indent on second parameter line. Same applies for the other code you touch below (if you already adjust that line, just indent with two tabs).

Signature · 3 lines
Cheers

David
#11 lore raw

Re: [PATCH V2 3/5] mm/khugepaged: change collapse_pte_mapped_thp() to return void

Quoted 32 lines · David Hildenbrand (Red Hat)
Quoted 20 lines · Shivank Garg

The only external caller of collapse_pte_mapped_thp() is uprobe, which ignores the return value. Change the external API to return void to simplify the interface.

Introduce try_collapse_pte_mapped_thp() for internal use that preserves the return value. This prepares for future patch that will convert the return type to use enum scan_result.

---
  include/linux/khugepaged.h |  9 ++++-----
  mm/khugepaged.c            | 40 ++++++++++++++++++++++----------------

  2 files changed, 27 insertions(+), 22 deletions(-)
include/linux/khugepaged.h +0 -0
index eb1946a70cff..37b992b22bba 100644

include/linux/khugepaged.h extern void khugepaged_enter_vma(struct vm_area_struct *vma, · line 21

Quoted 7 lines
include/linux/khugepaged.h +2 -2
@@ -17,8 +17,8 @@ extern void khugepaged_enter_vma(struct vm_area_struct *vma,
                    vm_flags_t vm_flags);
   extern void khugepaged_min_free_kbytes_update(void);
   extern bool current_is_khugepaged(void);
-extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
-                   bool install_pmd);
+extern void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+                    bool install_pmd);

Sorry for late review.

Quoted 7 lines

We remove the "extern" whenever we touch functions.

Nit: two tab indent on second parameter line. Same applies for the other code you touch below (if you already adjust that line, just indent with two tabs).

Thank you for the review.
I've always been unsure about when to use two-tab indent versus aligning with the opening bracket. I'll follow the two-tab indent convention going forward.

I have incorporated these reviews in V3 here:
https://lore.kernel.org/linux-mm/[email protected]

Best Regards,
Shivank

#12 lore raw

PATCH v2 4/5 mm/khugepaged: use enum scan_result for result variables and return types

In reply to Shivank Garg

Convert result variables and return types from int to enum scan_result throughout khugepaged code. This improves type safety and code clarity by making the intent explicit.

No functional change.

---
 mm/khugepaged.c | 111 +++++++++++++++++++++++++-----------------------
 1 file changed, 57 insertions(+), 54 deletions(-)
mm/khugepaged.c +57 -54
index 04ff0730c9a1..6892b23d6fc4 100644
@@ -537,17 +537,18 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
 	}
 }
 
-static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long start_addr,
-					pte_t *pte,
-					struct collapse_control *cc,
-					struct list_head *compound_pagelist)
+static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
+						     unsigned long start_addr,
+						     pte_t *pte,
+						     struct collapse_control *cc,
+						     struct list_head *compound_pagelist)
 {
 	struct page *page = NULL;
 	struct folio *folio = NULL;
 	unsigned long addr = start_addr;
 	pte_t *_pte;
-	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
+	int none_or_zero = 0, shared = 0, referenced = 0;
+	enum scan_result result = SCAN_FAIL;
 
 	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
 	     _pte++, addr += PAGE_SIZE) {
@@ -780,13 +781,13 @@ static void __collapse_huge_page_copy_failed(pte_t *pte,
  * @ptl: lock on raw pages' PTEs
  * @compound_pagelist: list that stores compound pages
  */
-static int __collapse_huge_page_copy(pte_t *pte, struct folio *folio,
+static enum scan_result __collapse_huge_page_copy(pte_t *pte, struct folio *folio,
 		pmd_t *pmd, pmd_t orig_pmd, struct vm_area_struct *vma,
 		unsigned long address, spinlock_t *ptl,
 		struct list_head *compound_pagelist)
 {
 	unsigned int i;
-	int result = SCAN_SUCCEED;
+	enum scan_result result = SCAN_SUCCEED;
 
 	/*
 	 * Copying pages' contents is subject to memory poison at any iteration.
@@ -898,10 +899,9 @@ static int hpage_collapse_find_target_node(struct collapse_control *cc)
  * Returns enum scan_result value.
  */
 
-static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
-				   bool expect_anon,
-				   struct vm_area_struct **vmap,
-				   struct collapse_control *cc)
+static enum scan_result hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
+						bool expect_anon, struct vm_area_struct **vmap,
+						struct collapse_control *cc)
 {
 	struct vm_area_struct *vma;
 	enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
@@ -930,7 +930,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
 	return SCAN_SUCCEED;
 }
 
-static inline int check_pmd_state(pmd_t *pmd)
+static inline enum scan_result check_pmd_state(pmd_t *pmd)
 {
 	pmd_t pmde = pmdp_get_lockless(pmd);
 
@@ -953,9 +953,9 @@ static inline int check_pmd_state(pmd_t *pmd)
 	return SCAN_SUCCEED;
 }
 
-static int find_pmd_or_thp_or_none(struct mm_struct *mm,
-				   unsigned long address,
-				   pmd_t **pmd)
+static enum scan_result find_pmd_or_thp_or_none(struct mm_struct *mm,
+						unsigned long address,
+						pmd_t **pmd)
 {
 	*pmd = mm_find_pmd(mm, address);
 	if (!*pmd)
@@ -964,12 +964,12 @@ static int find_pmd_or_thp_or_none(struct mm_struct *mm,
 	return check_pmd_state(*pmd);
 }
 
-static int check_pmd_still_valid(struct mm_struct *mm,
-				 unsigned long address,
-				 pmd_t *pmd)
+static enum scan_result check_pmd_still_valid(struct mm_struct *mm,
+					      unsigned long address,
+					      pmd_t *pmd)
 {
 	pmd_t *new_pmd;
-	int result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
+	enum scan_result result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
 
 	if (result != SCAN_SUCCEED)
 		return result;
@@ -985,15 +985,15 @@ static int check_pmd_still_valid(struct mm_struct *mm,
  * Called and returns without pte mapped or spinlocks held.
  * Returns result: if not SCAN_SUCCEED, mmap_lock has been released.
  */
-static int __collapse_huge_page_swapin(struct mm_struct *mm,
-				       struct vm_area_struct *vma,
-				       unsigned long start_addr, pmd_t *pmd,
-				       int referenced)
+static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,
+						    struct vm_area_struct *vma,
+						    unsigned long start_addr, pmd_t *pmd,
+						    int referenced)
 {
 	int swapped_in = 0;
 	vm_fault_t ret = 0;
 	unsigned long addr, end = start_addr + (HPAGE_PMD_NR * PAGE_SIZE);
-	int result;
+	enum scan_result result;
 	pte_t *pte = NULL;
 	spinlock_t *ptl;
 
@@ -1062,8 +1062,8 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
 	return result;
 }
 
-static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
-			      struct collapse_control *cc)
+static enum scan_result alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
+					   struct collapse_control *cc)
 {
 	gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() :
 		     GFP_TRANSHUGE);
@@ -1090,9 +1090,9 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm,
 	return SCAN_SUCCEED;
 }
 
-static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
-			      int referenced, int unmapped,
-			      struct collapse_control *cc)
+static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long address,
+					   int referenced, int unmapped,
+					   struct collapse_control *cc)
 {
 	LIST_HEAD(compound_pagelist);
 	pmd_t *pmd, _pmd;
@@ -1100,7 +1100,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 	pgtable_t pgtable;
 	struct folio *folio;
 	spinlock_t *pmd_ptl, *pte_ptl;
-	int result = SCAN_FAIL;
+	enum scan_result result = SCAN_FAIL;
 	struct vm_area_struct *vma;
 	struct mmu_notifier_range range;
 
@@ -1246,15 +1246,15 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 	return result;
 }
 
-static int hpage_collapse_scan_pmd(struct mm_struct *mm,
-				   struct vm_area_struct *vma,
-				   unsigned long start_addr, bool *mmap_locked,
-				   struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
+						struct vm_area_struct *vma,
+						unsigned long start_addr, bool *mmap_locked,
+						struct collapse_control *cc)
 {
 	pmd_t *pmd;
 	pte_t *pte, *_pte;
-	int result = SCAN_FAIL, referenced = 0;
-	int none_or_zero = 0, shared = 0;
+	int none_or_zero = 0, shared = 0, referenced = 0;
+	enum scan_result result = SCAN_FAIL;
 	struct page *page = NULL;
 	struct folio *folio = NULL;
 	unsigned long addr;
@@ -1441,8 +1441,8 @@ static void collect_mm_slot(struct mm_slot *slot)
 }
 
 /* folio must be locked, and mmap_lock must be held */
-static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
-			pmd_t *pmdp, struct folio *folio, struct page *page)
+static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
+				     pmd_t *pmdp, struct folio *folio, struct page *page)
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct vm_fault vmf = {
@@ -1477,10 +1477,11 @@ static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
 	return SCAN_SUCCEED;
 }
 
-static int try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
-				       bool install_pmd)
+static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
+						    bool install_pmd)
 {
-	int nr_mapped_ptes = 0, result = SCAN_FAIL;
+	enum scan_result result = SCAN_FAIL;
+	int nr_mapped_ptes = 0;
 	unsigned int nr_batch_ptes;
 	struct mmu_notifier_range range;
 	bool notified = false;
@@ -1862,9 +1863,9 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
  *    + unlock old pages
  *    + unlock and free huge page;
  */
-static int collapse_file(struct mm_struct *mm, unsigned long addr,
-			 struct file *file, pgoff_t start,
-			 struct collapse_control *cc)
+static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
+				      struct file *file, pgoff_t start,
+				      struct collapse_control *cc)
 {
 	struct address_space *mapping = file->f_mapping;
 	struct page *dst;
@@ -1872,7 +1873,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
 	pgoff_t index = 0, end = start + HPAGE_PMD_NR;
 	LIST_HEAD(pagelist);
 	XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
-	int nr_none = 0, result = SCAN_SUCCEED;
+	enum scan_result result = SCAN_SUCCEED;
+	int nr_none = 0;
 	bool is_shmem = shmem_file(file);
 
 	VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -2293,16 +2295,16 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-				    struct file *file, pgoff_t start,
-				    struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
+						 struct file *file, pgoff_t start,
+						 struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;
 	XA_STATE(xas, &mapping->i_pages, start);
 	int present, swap;
 	int node = NUMA_NO_NODE;
-	int result = SCAN_SUCCEED;
+	enum scan_result result = SCAN_SUCCEED;
 
 	present = 0;
 	swap = 0;
@@ -2400,7 +2402,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
+static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result *result,
 					    struct collapse_control *cc)
 	__releases(&khugepaged_mm_lock)
 	__acquires(&khugepaged_mm_lock)
@@ -2561,7 +2563,7 @@ static void khugepaged_do_scan(struct collapse_control *cc)
 	unsigned int progress = 0, pass_through_head = 0;
 	unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
 	bool wait = true;
-	int result = SCAN_SUCCEED;
+	enum scan_result result = SCAN_SUCCEED;
 
 	lru_add_drain_all();
 
@@ -2774,7 +2776,8 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	struct collapse_control *cc;
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long hstart, hend, addr;
-	int thps = 0, last_fail = SCAN_FAIL;
+	enum scan_result last_fail = SCAN_FAIL;
+	int thps = 0;
 	bool mmap_locked = true;
 
 	BUG_ON(vma->vm_start > start);
@@ -2796,7 +2799,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 
 	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
 		bool retried = false;
-		int result = SCAN_FAIL;
+		enum scan_result result = SCAN_FAIL;
 
 		if (!mmap_locked) {
 retry:
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 4/5
#13 lore raw

Re: [PATCH V2 4/5] mm/khugepaged: use enum scan_result for result variables and return types

Quoted 11 lines · Shivank Garg

Convert result variables and return types from int to enum scan_result throughout khugepaged code. This improves type safety and code clarity by making the intent explicit.

No functional change.

---
 mm/khugepaged.c | 111 +++++++++++++++++++++++++-----------------------
 1 file changed, 57 insertions(+), 54 deletions(-)

--
Best Regards,
Yan, Zi

#14 lore raw

Re: [PATCH V2 4/5] mm/khugepaged: use enum scan_result for result variables and return types

In reply to Shivank Garg
Quoted 19 lines · Shivank Garg

Convert result variables and return types from int to enum scan_result throughout khugepaged code. This improves type safety and code clarity by making the intent explicit.

No functional change.

---
  mm/khugepaged.c | 111 +++++++++++++++++++++++++-----------------------
  1 file changed, 57 insertions(+), 54 deletions(-)
mm/khugepaged.c +0 -0
index 04ff0730c9a1..6892b23d6fc4 100644
@@ -537,17 +537,18 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
  	}
  }
 

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

Quoted 10 lines
mm/khugepaged.c +5 -5
-static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
-					unsigned long start_addr,
-					pte_t *pte,
-					struct collapse_control *cc,
-					struct list_head *compound_pagelist)
+static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
+						     unsigned long start_addr,
+						     pte_t *pte,
+						     struct collapse_control *cc,
+						     struct list_head *compound_pagelist)

Another reason to use to tabs to indent.

(Again, applies to all cases where you touch the lines either way :) )

Signature · 3 lines
Cheers

David
#15 lore raw

PATCH v2 5/5 mm/khugepaged: make khugepaged_collapse_control static

In reply to Shivank Garg

The global variable 'khugepaged_collapse_control' is not used outside of mm/khugepaged.c. Make it static to limit its scope.

---
 mm/khugepaged.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/khugepaged.c +1 -1
index 6892b23d6fc4..4df480a87a74 100644
@@ -829,7 +829,7 @@ static void khugepaged_alloc_sleep(void)
 	remove_wait_queue(&khugepaged_wait, &wait);
 }
 
-struct collapse_control khugepaged_collapse_control = {
+static struct collapse_control khugepaged_collapse_control = {
 	.is_khugepaged = true,
 };
 
Signature · 1 line
2.43.0