[PATCH 2/6] mm: factor out generic PTE batch detection from swap_pte_batch()

From: Shivank Garg

Date: Sun Jul 12 2026 - 03:41:26 EST


Factor out the generic PTE batch detection logic from swap_pte_batch()
into softleaf_pte_batch() helper, so that it can be reused by upcoming
patch that adds restore-side batching of migration entries.

swap_pte_batch() now keeps only its swap-specific concerns and
delegates the batch detection to softleaf_pte_batch().

No functional changes intended.

Signed-off-by: Shivank Garg <shivankg@xxxxxxx>
---
mm/internal.h | 36 +++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 874be94cf257..a90721f8c687 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -505,6 +505,26 @@ static inline pte_t pte_next_swp_offset(pte_t pte)
return pte_move_swp_offset(pte, 1);
}

+static inline int softleaf_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
+{
+ pte_t expected_pte = pte_next_swp_offset(pte);
+ const pte_t *end_ptep = start_ptep + max_nr;
+ pte_t *ptep = start_ptep + 1;
+
+ VM_WARN_ON(max_nr < 1);
+
+ while (ptep < end_ptep) {
+ pte = ptep_get(ptep);
+
+ if (!pte_same(pte, expected_pte))
+ break;
+ expected_pte = pte_next_swp_offset(expected_pte);
+ ptep++;
+ }
+
+ return ptep - start_ptep;
+}
+
/**
* swap_pte_batch - detect a PTE batch for a set of contiguous swap entries
* @start_ptep: Page table pointer for the first entry.
@@ -522,23 +542,9 @@ static inline pte_t pte_next_swp_offset(pte_t pte)
*/
static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
{
- pte_t expected_pte = pte_next_swp_offset(pte);
- const pte_t *end_ptep = start_ptep + max_nr;
- pte_t *ptep = start_ptep + 1;
-
- VM_WARN_ON(max_nr < 1);
VM_WARN_ON(!softleaf_is_swap(softleaf_from_pte(pte)));

- while (ptep < end_ptep) {
- pte = ptep_get(ptep);
-
- if (!pte_same(pte, expected_pte))
- break;
- expected_pte = pte_next_swp_offset(expected_pte);
- ptep++;
- }
-
- return ptep - start_ptep;
+ return softleaf_pte_batch(start_ptep, max_nr, pte);
}
#endif /* CONFIG_MMU */


--
2.43.0