mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Yuan-Hao Hsu <aa9736195201@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>,
	liam@infradead.org, Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Barry Song <baohua@kernel.org>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/memory: reuse the whole exclusive large folio on a write fault
Date: Fri, 18 Sep 2026 14:14:11 +0200	[thread overview]
Message-ID: <b1ba175c-ceb2-4e00-b233-d51dd8a2f4cb@kernel.org> (raw)
In-Reply-To: <20260918064238.868-1-aa9736195201@gmail.com>

On 9/18/26 08:42, 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, a
> write fault of the parent ends up in the reuse path of do_wp_page():
> wp_can_reuse_anon_folio() finds that all references to the folio come
> from mappings in 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 what it finds holds
> for every page of it.  Still only the page that faulted is marked
> exclusive and only its PTE becomes writable, so each of the other pages
> takes a write fault of its own, and each of those takes the large
> mapcount lock to find out the same thing again: 16 faults for a 64K
> folio, 512 for a 2M THP that is mapped by PTEs.  The same THP mapped by
> a PMD is reused by one fault in do_huge_pmd_wp_page(), do_swap_page()
> maps all PTEs of an exclusive large folio writable at once, and
> numa_rebuild_large_mapping() upgrades all PTEs of the folio from one
> hinting fault.
> 
> Commit 1da190f4d0a6 ("mm: Copy-on-Write (COW) reuse support for
> PTE-mapped THP") left this for later because faulting around might
> increase the COW latency.  Numbers for that are below.
> 
> When a large folio has been found exclusive, walk the part of it that
> this page table maps inside the VMA.  PTEs that still map it read-only
> are batched with folio_pte_batch_flags(), their pages are marked
> exclusive, and where can_change_pte_writable() agrees the batch is made
> writable with modify_prot_start_ptes()/modify_prot_commit_ptes(), the
> way mprotect() does it.  That leaves NUMA hinting and uffd-wp PTEs
> alone, keeps soft-dirty tracking exact, and on arm64 writes a contpte
> block back as a whole where ptep_set_access_flags() on a single PTE has
> to unfold it.  Pages that cannot be made writable are marked exclusive
> all the same, so their own fault skips the folio check.  The PTE that
> faulted is in one of the batches and is then completed by
> wp_page_reuse() as before.  Small folios, PMD-mapped THPs, unsharing
> faults and the copy path are not changed.
> 
> x86-64, i7-12700KF, 256 MiB of anonymous memory, fork(), the child
> exits, then the parent stores to the memory.  Medians of 15 runs, two
> boots of each kernel, taken alternately:
> 
>                           v7.3-rc3+           patched
>   one byte per page, a clock_gettime() between the stores
>     write faults
>       4K pages            65,601              65,601
>       64K mTHP            65,601               4,161
>       1M mTHP             65,601                 321
>       2M THP, PTE-mapped  65,601                 193
>       2M THP, PMD-mapped     193                 193
>     time (ms)
>       4K pages            29.7 / 30.1         30.0 / 31.1
>       64K mTHP            29.4 / 29.7          5.5 /  5.6
>       1M mTHP             29.3 / 29.8          3.9 /  3.9
>       2M THP, PTE-mapped  29.9 / 29.2          3.8 /  3.9
>       2M THP, PMD-mapped   2.5 /  2.5          2.5 /  2.6
>   memset() of all of it (ms)
>       4K pages            57.2 / 56.5         56.9 / 57.1
>       64K mTHP            56.5 / 56.3         39.0 / 38.7
>       2M THP, PTE-mapped  58.6 / 56.5         36.8 / 37.7
>       2M THP, PMD-mapped  37.5 / 35.9         35.9 / 36.5
>   8 threads, one byte per page, random order (ms)
>       64K mTHP             6.1 /  6.2          0.9 /  1.0
>       2M THP, PTE-mapped   6.0 /  6.7          0.7 /  0.7
> 
> The latency of the one fault that now does the work for the folio,
> measured as the time of the store that takes it, against 420 ns for a
> reuse fault today (medians, ns):
> 
>                           reuse fault   fault that     COW fault that
>                           (patched)     allocated it   copies 4K
>       64K mTHP                   730          4,600          1,500
>       1M mTHP                  5,500         63,000          1,500
>       2M THP, PTE-mapped      10,100        126,000          1,500
> 
> That is 14 to 20 ns per PTE.  Builds that differ only by NOPs in front
> of the new function take either 10,100 or 7,500 ns for the 2M folio,
> with a period of 32 bytes: it is the loop of modify_prot_commit_ptes()
> that changes speed with its address.
> 
> Capping the walk to the 16 PTEs around the fault instead was measured as
> well: it takes 5.4 ms where the above takes 3.9 ms on 1M and 2M folios,
> it is slower than today when only one page per 64K is written (3.1 ms
> against 1.9 ms, the whole folio takes 1.5 ms), and it is only ahead when
> no more than one page per 2M is ever written (0.1 ms against 1.3 ms for
> the 256 MiB).
> 
> Redis 7.0.15 with 1.3 M keys of 512 bytes on 64K mTHP, BGSAVE and then
> 1,000,000 SETs: the faults of redis-server during the SETs go from
> 262,100 to 18,500, its CPU time from 1.48-1.53 s to 1.25-1.33 s, and
> redis-benchmark reports 742,000 to 794,000 requests per second instead
> of 652,000 to 658,000.  With THP off all three stay where they were.
> 
> arm64 was only run under QEMU, for the counters and with DEBUG_VM and
> PAGE_TABLE_CHECK: the faults are the same as above, and with 64K folios
> the first pass after fork() unfolds every contpte block today (512
> contpte_convert() calls for 512 blocks, and nothing folds them again)
> while none is unfolded with this patch.
> 
> What does not get faster on x86 are stores to pages that this CPU still
> has a read-only TLB entry for.  The fault makes the PTEs writable but,
> like mprotect(), does not flush, so such a page still takes a fault, a
> spurious one that costs about the same as the reuse fault it replaces.
> That happens to pages that were read since fork(): reading the 16 pages
> of every 64K folio before storing to them takes 65,500 faults and 28 ms
> before and after.  And it happens in a loop that does nothing but store
> one byte to every page in ascending order, which is what the reuse-byte
> mode of David's pte-mapped-folio-benchmarks does (120 ms before and
> after for 1 GiB of 64K folios; 2M folios: 119 ms to 18 ms; the reuse
> mode, a memset(), goes from 232 ms to 157 ms with 64K folios): while the
> first store of a folio is faulting, the CPU has already run the next
> stores speculatively and has filled the TLB with the read-only
> translations of their pages.  Counting with kprobes, such a run enters
> handle_mm_fault() 135,687 times and do_wp_page() 8,457 times; with an
> LFENCE after every store the faults are 4,100 instead of 65,400 and the
> loop takes 4 ms instead of 29 ms.  The PMD-mapped case, which this patch
> does not touch, shows the same: 8,800 faults for 128 THPs, 133 with the
> LFENCE.
> 
> With a flush_tlb_local() in the new function, as an experiment, the
> ascending loop takes 4.1 ms instead of 28 ms on 64K folios, the
> read-then-store loop 5.3 ms instead of 28 ms and the memset() 21 ms
> instead of 38 ms, for a fault of 870 instead of 730 ns and 11% more time
> for the pass with the clock_gettime().  Generic code has no way to ask
> x86 for a flush that stays on this CPU, so that is left for later.
> 
> Assisted-by: LLM sparse


My review backlog is large enough for me to just go through this wall of text.

There were previous discussions on this, in particular around how much we should
actually try operating around the target PTE.

How did you use the LLM for coming up with this patch + description?

-- 
Cheers,

David

  reply	other threads:[~2026-09-18 12:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18  6:42 Yuan-Hao Hsu
2026-09-18 12:14 ` David Hildenbrand (Arm) [this message]
2026-09-18 18:28   ` Yuan-Hao Hsu
2026-09-18 23:48     ` Barry Song
2026-09-18 13:54 ` Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b1ba175c-ceb2-4e00-b233-d51dd8a2f4cb@kernel.org \
    --to=david@kernel.org \
    --cc=aa9736195201@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®