[PATCH 5/8] mm/internal: add set_softleaf_ptes

From: Dev Jain

Date: Thu Jul 23 2026 - 03:10:46 EST


Currently we have a helper called set_ptes() which is used to set
consecutive present ptes in the pgtables. To do the same operation but
to set "consecutive" nonpresent (softleaf) ptes, add set_softleaf_ptes().

The softleaves which have a notion of consecutivity is swap softleaf,
and those grouped by softeaf_has_pfn(). The latter is trivial; future
code can convert present ptes pointing to the same large folio to
swap softleaves with consecutive offsets using this helper.

The other case is softleaf markers. They do not have a notion of a swap
offset or PFN, so future code can use set_softleaf_ptes() to store
multiple (same) markers on the ptes.

Signed-off-by: Dev Jain <dev.jain@xxxxxxx>
---
mm/internal.h | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

diff --git a/mm/internal.h b/mm/internal.h
index ce52188a8a65d..3fc1c1c776ca3 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -514,6 +514,39 @@ static inline pte_t pte_next_softleaf_offset(pte_t pte)
return pte_move_softleaf_offset(pte, 1);
}

+/**
+ * set_softleaf_ptes - Set consecutive softleaf PTEs.
+ * @mm: Address space the PTEs belong to.
+ * @addr: Address of the first PTE.
+ * @ptep: Page table pointer for the first PTE.
+ * @pte: PTE to set for the first entry.
+ * @nr: Number of PTEs to set.
+ *
+ * Install @nr softleaf PTEs, advancing @pte when its softleaf entry
+ * represents consecutive offsets. Swap entries advance through swap offsets,
+ * PFN softleaf entries advance through PFNs (encoded by swap offset), and
+ * marker entries are repeated unchanged.
+ */
+static inline void set_softleaf_ptes(struct mm_struct *mm, unsigned long addr,
+ pte_t *ptep, pte_t pte, unsigned long nr)
+{
+ softleaf_t entry;
+ bool advance;
+
+ entry = softleaf_from_pte(pte);
+ advance = softleaf_is_swap(entry) || softleaf_has_pfn(entry);
+
+ for (;;) {
+ set_pte_at(mm, addr, ptep, pte);
+ if (--nr == 0)
+ break;
+ if (advance)
+ pte = pte_next_softleaf_offset(pte);
+ ptep++;
+ addr += PAGE_SIZE;
+ }
+}
+
/**
* swap_pte_batch - detect a PTE batch for a set of contiguous swap entries
* @start_ptep: Page table pointer for the first entry.
--
2.43.0