Re: [PATCH mm-unstable v2 1/5] mm: consolidate anonymous folio PTE mapping into helpers

From: Lance Yang

Date: Sat Feb 28 2026 - 02:57:47 EST




On 2026/2/26 09:29, Nico Pache wrote:
The anonymous page fault handler in do_anonymous_page() open-codes the
sequence to map a newly allocated anonymous folio at the PTE level:
- construct the PTE entry
- add rmap
- add to LRU
- set the PTEs
- update the MMU cache.

Introduce a two helpers to consolidate this duplicated logic, mirroring the
existing map_anon_folio_pmd_nopf() pattern for PMD-level mappings:

map_anon_folio_pte_nopf(): constructs the PTE entry, takes folio
references, adds anon rmap and LRU. This function also handles the
uffd_wp that can occur in the pf variant.

map_anon_folio_pte_pf(): extends the nopf variant to handle MM_ANONPAGES
counter updates, and mTHP fault allocation statistics for the page fault
path.

The zero-page read path in do_anonymous_page() is also untangled from the
shared setpte label, since it does not allocate a folio and should not
share the same mapping sequence as the write path. Make nr_pages = 1
rather than relying on the variable. This makes it more clear that we
are operating on the zero page only.

This refactoring will also help reduce code duplication between mm/memory.c
and mm/khugepaged.c, and provides a clean API for PTE-level anonymous folio
mapping that can be reused by future callers.

Signed-off-by: Nico Pache <npache@xxxxxxxxxx>
---

The refactoring looks good to me.

With the change David suggested (removing nr_pages parameter from
map_anon_folio_pte_pf() and using folio_order() instead internally),

Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>