[PATCH 1/3] mm/memory: install new PTEs first in insert_page_into_pte_locked()
From: Mike Rapoport (Microsoft)
Date: Tue Nov 17 2026 - 10:59:32 EST
insert_page_into_pte_locked() first handles non-empty PTEs and only then
deals with installing a new PTE.
Upcoming fixes need to handle non-empty but non-present entries, like PTE
markers, differently from present mappings and this requires restructuring
of insert_page_into_pte_locked() to avoid extra branching and nesting.
Install a new PTE first and return early, and then handle the existing
mappings.
No functional change.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
mm/memory.c | 65 ++++++++++++++++++++++++++++++-------------------------------
1 file changed, 32 insertions(+), 33 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index fe72e66c9c2e..f927f2241e40 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2453,44 +2453,43 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
struct folio *folio = page_folio(page);
pte_t pteval = ptep_get(pte);
- if (!pte_none(pteval)) {
- if (!mkwrite)
- return -EBUSY;
-
- /*
- * For read faults on private mappings the PFN passed in may
- * not match the PFN we have mapped if the mapped PFN is a
- * writeable COW page. In the mkwrite case we are creating a
- * writable PTE for a shared mapping and we expect the PFNs to
- * match. If they don't match, we are likely racing with block
- * allocation and mapping invalidation.
- */
- if (pte_pfn(pteval) != page_to_pfn(page)) {
- WARN_ON_ONCE(!is_zero_pfn(pte_pfn(pteval)));
- return -EFAULT;
+ if (pte_none(pteval)) {
+ pteval = mk_pte(page, prot);
+ if (unlikely(is_zero_folio(folio))) {
+ pteval = pte_mkspecial(pteval);
+ } else {
+ folio_get(folio);
+ pteval = mk_pte(page, prot);
+ if (mkwrite) {
+ pteval = pte_mkyoung(pteval);
+ pteval = maybe_mkwrite(pte_mkdirty(pteval), vma);
+ }
+ inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
+ folio_add_file_rmap_pte(folio, page, vma);
}
- pteval = maybe_mkwrite(pteval, vma);
- pteval = pte_mkyoung(pteval);
- if (ptep_set_access_flags(vma, addr, pte, pteval, 1))
- update_mmu_cache(vma, addr, pte);
+ set_pte_at(vma->vm_mm, addr, pte, pteval);
return 0;
}
- /* Ok, finally just insert the thing.. */
- pteval = mk_pte(page, prot);
- if (unlikely(is_zero_folio(folio))) {
- pteval = pte_mkspecial(pteval);
- } else {
- folio_get(folio);
- pteval = mk_pte(page, prot);
- if (mkwrite) {
- pteval = pte_mkyoung(pteval);
- pteval = maybe_mkwrite(pte_mkdirty(pteval), vma);
- }
- inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
- folio_add_file_rmap_pte(folio, page, vma);
+ if (!mkwrite)
+ return -EBUSY;
+
+ /*
+ * For read faults on private mappings the PFN passed in may
+ * not match the PFN we have mapped if the mapped PFN is a
+ * writeable COW page. In the mkwrite case we are creating a
+ * writable PTE for a shared mapping and we expect the PFNs to
+ * match. If they don't match, we are likely racing with block
+ * allocation and mapping invalidation.
+ */
+ if (pte_pfn(pteval) != page_to_pfn(page)) {
+ WARN_ON_ONCE(!is_zero_pfn(pte_pfn(pteval)));
+ return -EFAULT;
}
- set_pte_at(vma->vm_mm, addr, pte, pteval);
+ pteval = maybe_mkwrite(pteval, vma);
+ pteval = pte_mkyoung(pteval);
+ if (ptep_set_access_flags(vma, addr, pte, pteval, 1))
+ update_mmu_cache(vma, addr, pte);
return 0;
}
--
2.53.0