Re: [PATCH v2 1/2] mm/memory: reuse 16 PTEs of an exclusive large folio on a write fault
From: David Hildenbrand (Arm)
Date: Thu Sep 24 2026 - 15:17:42 EST
On 9/19/26 09:31, Yuan-Hao Hsu wrote:
> fork() maps the anonymous pages of the parent read-only and clears
> PageAnonExclusive on them. Once the child has exec'ed or exited, the
> parent's write fault takes the reuse path of do_wp_page():
> wp_can_reuse_anon_folio() finds that all references to the folio come
> from this MM, the page is marked exclusive again and its PTE is made
> writable.
>
> For a large folio that check is about the folio and holds for every
> page of it, but only the page that faulted is marked exclusive and
> made writable. Each other page takes a write fault of its own and
> takes the large mapcount lock to find out the same thing again: 16
> faults for a 64K folio. The same THP mapped by a PMD is reused by one
> fault in do_huge_pmd_wp_page(), and do_swap_page() maps all PTEs of an
> exclusive large folio writable at once.
>
> Barry proposed reusing the whole mTHP from one fault in 2024 [1]. The
> reservations then were the latency of the individual write fault and
> how far to go around the faulting PTE: a contpte-sized block was fine,
> anything bigger not yet convincing (David's replies, linked below).
> Commit 1da190f4d0a6 ("mm: Copy-on-Write (COW) reuse support for
> PTE-mapped THP") then added the per-folio check and left faulting
> around for later.
I'm fine with the original idea of limiting this to reasonable chunk
sizes, reducing the work we do in a single page fault.
The patch needs work. I disagree with various decisions
either you or the LLM came up with like
* Uglifying do_wp_page
* Not handling unshare
* Calling wp_reuse_large_anon_folio() to do some batching to then call
wp_reuse_page in same page fault
* Batching multiple pieces in a loop
* Using can_change_pte_writable()
I tried to see how to implement it cleaner. I think we should definitely
start with: