* [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
@ 2026-11-23 10:41 Qiliang Yuan
2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
2026-11-24 10:41 ` sashiko-bot
0 siblings, 2 replies; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-23 10:41 UTC (permalink / raw)
To: David Hildenbrand, Lorenzo Stoakes, Rik van Riel, Liam R. Howlett,
Vlastimil Babka, Harry Yoo, Jann Horn, Lance Yang, Andrew Morton
Cc: Matthew Wilcox, Kiryl Shutsemau, linux-mm, linux-kernel,
Qiliang Yuan
Writeback calls folio_mkclean() on every folio it writes, which
write-protects and cleans each PTE that maps the folio, so that the
next store through the mapping faults and redirties it.
page_vma_mkclean_one() does this with ptep_clear_flush(), one TLB flush
per PTE. A large folio mapped by PTEs thus costs as many flushes as it
has pages, each an IPI to the other CPUs the mm runs on. Writing an
8 GiB file through a shared mapping on xfs flushes the TLB 2,097,152
times for 65,575 folios, and the writer spends about half of its time
in the IPIs and flushes.
Write-protect and clean the PTEs with ptep_modify_prot_start() and
ptep_modify_prot_commit(), as change_protection() does, and flush the
range they cover once after the walk. The flush still happens before
folio_mkclean() returns: stores through a stale TLB entry complete
before writeback reads the folio, and later stores fault.
Writing an 8 GiB checkpoint to brd through a shared mapping, one file
per writer, then msync(), median of 6 runs:
fs writers before (s) after (s) change
xfs 1 5.57 2.98 -46%
xfs 8 5.37 2.10 -61%
ext4 1 7.88 3.90 -50%
ext4 8 5.10 2.02 -60%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
mm/rmap.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/mm/rmap.c b/mm/rmap.c
index 65646ed6fc453..4c3d9cb47e550 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1127,6 +1127,7 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
struct vm_area_struct *vma = pvmw->vma;
struct mmu_notifier_range range;
unsigned long address = pvmw->address;
+ unsigned long flush_start = ULONG_MAX, flush_end = 0;
/*
* We have to assume the worse case ie pmd for invalidation. Note that
@@ -1156,10 +1157,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
continue;
flush_cache_page(vma, address, pte_pfn(entry));
- entry = ptep_clear_flush(vma, address, pte);
- entry = pte_wrprotect(entry);
- entry = pte_mkclean(entry);
- set_pte_at(vma->vm_mm, address, pte, entry);
+ entry = ptep_modify_prot_start(vma, address, pte);
+ ptep_modify_prot_commit(vma, address, pte, entry,
+ pte_mkclean(pte_wrprotect(entry)));
+ flush_start = min(flush_start, address);
+ flush_end = max(flush_end, address + PAGE_SIZE);
ret = 1;
} else {
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -1193,6 +1195,12 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
cleaned++;
}
+ /*
+ * Flush the write-protected PTEs once rather than one by one, before
+ * the caller writes the folio back, as change_protection() does.
+ */
+ if (flush_end)
+ flush_tlb_range(vma, flush_start, flush_end);
mmu_notifier_invalidate_range_end(&range);
return cleaned;
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261123-feat-mm-mkclean-batch-flush-686d20219b52
Best regards,
--
Qiliang Yuan <qiliang.yuan@linux.dev>
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
2026-11-23 10:41 [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one() Qiliang Yuan
@ 2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
2026-11-23 12:05 ` Qiliang Yuan
2026-11-24 10:41 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-23 11:53 UTC (permalink / raw)
To: Qiliang Yuan
Cc: David Hildenbrand, Rik van Riel, Liam R. Howlett, Vlastimil Babka,
Harry Yoo, Jann Horn, Lance Yang, Andrew Morton, Matthew Wilcox,
Kiryl Shutsemau, linux-mm, linux-kernel
On Mon, Nov 23, 2026 at 05:41:38AM -0500, Qiliang Yuan wrote:
> Writeback calls folio_mkclean() on every folio it writes, which
> write-protects and cleans each PTE that maps the folio, so that the
> next store through the mapping faults and redirties it.
>
> page_vma_mkclean_one() does this with ptep_clear_flush(), one TLB flush
> per PTE. A large folio mapped by PTEs thus costs as many flushes as it
> has pages, each an IPI to the other CPUs the mm runs on. Writing an
> 8 GiB file through a shared mapping on xfs flushes the TLB 2,097,152
> times for 65,575 folios, and the writer spends about half of its time
> in the IPIs and flushes.
>
> Write-protect and clean the PTEs with ptep_modify_prot_start() and
> ptep_modify_prot_commit(), as change_protection() does, and flush the
> range they cover once after the walk. The flush still happens before
> folio_mkclean() returns: stores through a stale TLB entry complete
> before writeback reads the folio, and later stores fault.
>
> Writing an 8 GiB checkpoint to brd through a shared mapping, one file
> per writer, then msync(), median of 6 runs:
>
> fs writers before (s) after (s) change
> xfs 1 5.57 2.98 -46%
> xfs 8 5.37 2.10 -61%
> ext4 1 7.88 3.90 -50%
> ext4 8 5.10 2.02 -60%
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
You are flooding the kernel with patches, 6 series across 4
subsystems over 19 hrs:
https://lore.kernel.org/all/?q=f%3Aqiliang.yuan%40linux.dev
Please don't do that :)
Given the volume both recently and in the past I assume you are using an LLM to
do this, so could you confirm your understanding of the problem (as well as
those of patches sent to other subsystems) and what real-world task you were
doing to identify this issue?
See https://docs.kernel.org/process/generated-content.html especially the
paragraphs at the end.
In general a core team member tends to take over stuff like this.
> ---
> mm/rmap.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 65646ed6fc453..4c3d9cb47e550 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -1127,6 +1127,7 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> struct vm_area_struct *vma = pvmw->vma;
> struct mmu_notifier_range range;
> unsigned long address = pvmw->address;
> + unsigned long flush_start = ULONG_MAX, flush_end = 0;
>
> /*
> * We have to assume the worse case ie pmd for invalidation. Note that
> @@ -1156,10 +1157,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> continue;
>
> flush_cache_page(vma, address, pte_pfn(entry));
> - entry = ptep_clear_flush(vma, address, pte);
> - entry = pte_wrprotect(entry);
> - entry = pte_mkclean(entry);
> - set_pte_at(vma->vm_mm, address, pte, entry);
> + entry = ptep_modify_prot_start(vma, address, pte);
> + ptep_modify_prot_commit(vma, address, pte, entry,
> + pte_mkclean(pte_wrprotect(entry)));
> + flush_start = min(flush_start, address);
> + flush_end = max(flush_end, address + PAGE_SIZE);
> ret = 1;
> } else {
> #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> @@ -1193,6 +1195,12 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> cleaned++;
> }
>
> + /*
> + * Flush the write-protected PTEs once rather than one by one, before
> + * the caller writes the folio back, as change_protection() does.
> + */
> + if (flush_end)
> + flush_tlb_range(vma, flush_start, flush_end);
> mmu_notifier_invalidate_range_end(&range);
>
> return cleaned;
>
> ---
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
> change-id: 20261123-feat-mm-mkclean-batch-flush-686d20219b52
>
> Best regards,
> --
> Qiliang Yuan <qiliang.yuan@linux.dev>
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
@ 2026-11-23 12:05 ` Qiliang Yuan
2026-11-23 15:21 ` Matthew Wilcox
0 siblings, 1 reply; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-23 12:05 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Qiliang Yuan, David Hildenbrand, Rik van Riel, Liam R. Howlett,
Vlastimil Babka, Harry Yoo, Jann Horn, Lance Yang, Andrew Morton,
Matthew Wilcox, Kiryl Shutsemau, linux-mm, linux-kernel
On Mon, Nov 23, 2026 at 11:53:50AM +0000, Lorenzo Stoakes wrote:
> You are flooding the kernel with patches, 6 series across 4
> subsystems over 19 hrs:
[...]
> Given the volume both recently and in the past I assume you are using an LLM to
> do this, so could you confirm your understanding of the problem (as well as
> those of patches sent to other subsystems) and what real-world task you were
> doing to identify this issue?
Yes, I use an AI assistant. What it changed for me is mostly breadth: I
used to work in depth on one area of the kernel, and with the assistant
I can bring the same kind of algorithmic work to other paths and measure
it there. The patches stay small, a few to a few dozen lines each. I
test every change myself, with before/after measurements and the
relevant selftests, and debug kernels where it touches locking or
memory ordering, and I review the code line by line before sending it.
I don't send anything I don't understand, and I can explain every one
of them.
For this one, I was looking at how AI training jobs save checkpoints.
Writing a checkpoint through a shared mapping was 3-4x slower than
write(), and perf showed the writer spending about half of its time in
IPIs. Counting the callers of flush_tlb_mm_range() showed writeback's
folio_mkclean() flushing once per PTE in page_vma_mkclean_one(), so a
large folio mapped by PTEs costs one flush per page. The patch
write-protects the PTEs the way change_protection() does and flushes
the range once, before folio_mkclean() returns: stores through a stale
TLB entry land before writeback reads the folio, and later stores fault
and redirty it. Besides the timings in the changelog, I checked this
with writers storing through the mapping while another thread loops
fsync(), then compared the file with memory, on xfs and ext4 with 1 to
16 writers and on a KASAN/lockdep/DEBUG_VM kernel: no mismatch and no
warning.
I'd like to understand the concern better: is it the review load from
the number of postings, or something in these patches you think I may
have missed? If it is the former, I can space them out.
Thanks,
Qiliang
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
2026-11-23 12:05 ` Qiliang Yuan
@ 2026-11-23 15:21 ` Matthew Wilcox
0 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2026-11-23 15:21 UTC (permalink / raw)
To: Qiliang Yuan
Cc: Lorenzo Stoakes (ARM), David Hildenbrand, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, Andrew Morton, Kiryl Shutsemau, linux-mm,
linux-kernel
On Mon, Nov 23, 2026 at 07:05:14AM -0500, Qiliang Yuan wrote:
> For this one, I was looking at how AI training jobs save checkpoints.
> Writing a checkpoint through a shared mapping was 3-4x slower than
> write()
Shared writable mmaps are a terrible idea. It doesn't surpise me that
lazy AI people do this, but your efforts would be better spent fixing
userspace to Not Do That.
https://numi.st/post/2024/mmap/
https://db.cs.cmu.edu/papers/2022/cidr2022-p13-crotty.pdf
(and many, many more articles along these lines)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
2026-11-23 10:41 [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one() Qiliang Yuan
2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
@ 2026-11-24 10:41 ` sashiko-bot
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-24 10:41 UTC (permalink / raw)
To: Qiliang Yuan, David Hildenbrand, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, Andrew Morton
Cc: Matthew Wilcox, Kiryl Shutsemau, linux-mm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm/rmap: ptep_modify_prot_commit() fails to clear PTE dirty bit
on Xen PV
- [High] mm/rmap: missing tlb_flush_pending tracking in
page_vma_mkclean_one()
--
commit 5d94fc7a1fe6fb48da4ff29187b2f51e4ede88a8
Author: Qiliang Yuan <qiliang.yuan@linux.dev>
mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
This commit replaces per-PTE ptep_clear_flush() calls in
page_vma_mkclean_one() with ptep_modify_prot_start() and
ptep_modify_prot_commit(), batching the TLB invalidation into a single
flush_tlb_range() call after walking the mapping.
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 65646ed6fc453..4c3d9cb47e550 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -1127,6 +1127,7 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> struct vm_area_struct *vma = pvmw->vma;
> struct mmu_notifier_range range;
> unsigned long address = pvmw->address;
> + unsigned long flush_start = ULONG_MAX, flush_end = 0;
>
> /*
> * We have to assume the worse case ie pmd for invalidation. Note that
> @@ -1156,10 +1157,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> continue;
>
> flush_cache_page(vma, address, pte_pfn(entry));
> - entry = ptep_clear_flush(vma, address, pte);
> - entry = pte_wrprotect(entry);
> - entry = pte_mkclean(entry);
> - set_pte_at(vma->vm_mm, address, pte, entry);
> + entry = ptep_modify_prot_start(vma, address, pte);
> + ptep_modify_prot_commit(vma, address, pte, entry,
> + pte_mkclean(pte_wrprotect(entry)));
> + flush_start = min(flush_start, address);
> + flush_end = max(flush_end, address + PAGE_SIZE);
> ret = 1;
> } else {
> #ifdef CONFIG_TRANSPARENT_HUGEPAGE
[Severity: High]
Does ptep_modify_prot_commit() clear the PTE dirty bit on Xen PV guests?
Per include/linux/pgtable.h, ptep_modify_prot_commit() updates a PTE while
leaving hardware-controlled bits unmodified. On x86 Xen PV,
xen_ptep_modify_prot_start() returns *ptep without clearing it in the page
table, and xen_ptep_modify_prot_commit() updates the entry with
MMU_PT_UPDATE_PRESERVE_AD:
arch/x86/xen/mmu_pv.c:xen_ptep_modify_prot_commit() {
...
u.ptr = virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
u.val = pte_val_ma(pte);
xen_extend_mmu_update(&u);
...
}
MMU_PT_UPDATE_PRESERVE_AD sets *ptr = val | (*ptr & (A | D)), which ORs the
existing _PAGE_DIRTY bit in *ptep back into the committed PTE and undoes
pte_mkclean(). Leaving _PAGE_DIRTY set on a read-only shared PTE allows
can_change_shared_pte_writable() during mprotect() or NUMA balancing faults
to make the PTE writable again without a write fault or ->page_mkwrite()
notification, and on x86 clearing _PAGE_RW while preserving _PAGE_DIRTY
produces a shadow-stack PTE encoding (pte_shstk()).
Clearing the PTE first with ptep_get_and_clear() (or
clear_young_dirty_ptes()) before installing the clean, write-protected
entry would avoid preserving the dirty bit.
> @@ -1193,6 +1195,12 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
> cleaned++;
> }
>
> + /*
> + * Flush the write-protected PTEs once rather than one by one, before
> + * the caller writes the folio back, as change_protection() does.
> + */
> + if (flush_end)
> + flush_tlb_range(vma, flush_start, flush_end);
> mmu_notifier_invalidate_range_end(&range);
>
> return cleaned;
[Severity: High]
Can deferring flush_tlb_range() past pvmw->ptl unlock without
inc_tlb_flush_pending() allow concurrent page-table walks to skip their TLB
flush?
page_vma_mapped_walk() unlocks pvmw->ptl when crossing a PMD boundary and
when finishing the walk, before flush_tlb_range() runs. Because
folio_mkclean() only holds i_mmap_lock_read(), a concurrent mprotect() or
folio_mkclean() on the same VMA can acquire the PTL in that window:
CPU0 CPU1
page_vma_mkclean_one()
page_vma_mapped_walk()
spin_lock(pvmw->ptl)
ptep_modify_prot_start()
ptep_modify_prot_commit()
/* PTE is now clean & read-only */
page_vma_mapped_walk()
spin_unlock(pvmw->ptl)
change_protection()
tlb_gather_mmu()
inc_tlb_flush_pending(mm)
spin_lock(ptl)
/* sees PTE already RO/clean;
* pte_needs_flush() is false */
spin_unlock(ptl)
tlb_finish_mmu()
mm_tlb_flush_nested(mm) == false
/* skips TLB flush */
/* returns to userspace with
* stale writable TLB entry */
flush_tlb_range()
Conversely, if another thread has already marked the PTE clean and
read-only under PTL and released PTL with its deferred TLB flush still in
flight, page_vma_mkclean_one() sees !pte_dirty(entry) && !pte_write(entry),
leaves flush_end at 0, and returns to writeback without waiting for or
performing a TLB flush.
Bracketing the walk with inc_tlb_flush_pending(vma->vm_mm) and
dec_tlb_flush_pending(vma->vm_mm), and flushing the full walk range when
mm_tlb_flush_nested(vma->vm_mm) is true (as wp_clean_pre_vma() and
wp_clean_post_vma() do), would close both races.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-feat-mm-mkclean-batch-flush-v1-1-7e998ad13251@linux.dev?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 10:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 10:41 [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one() Qiliang Yuan
2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
2026-11-23 12:05 ` Qiliang Yuan
2026-11-23 15:21 ` Matthew Wilcox
2026-11-24 10:41 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox