mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: David Hildenbrand <david@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	 Rik van Riel <riel@surriel.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	 Vlastimil Babka <vbabka@kernel.org>,
	Harry Yoo <harry@kernel.org>,  Jann Horn <jannh@google.com>,
	Lance Yang <lance.yang@linux.dev>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@infradead.org>,
	Kiryl Shutsemau <kas@kernel.org>,
	 linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 Qiliang Yuan <qiliang.yuan@linux.dev>
Subject: [PATCH] mm/rmap: flush the TLB once per mapping in page_vma_mkclean_one()
Date: Mon, 23 Nov 2026 05:41:38 -0500	[thread overview]
Message-ID: <20261123-feat-mm-mkclean-batch-flush-v1-1-7e998ad13251@linux.dev> (raw)

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>


             reply	other threads:[~2026-11-23 10:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 10:41 Qiliang Yuan [this message]
2026-11-23 11:53 ` Lorenzo Stoakes (ARM)
2026-11-23 12:05   ` Qiliang Yuan

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=20261123-feat-mm-mkclean-batch-flush-v1-1-7e998ad13251@linux.dev \
    --to=qiliang.yuan@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=harry@kernel.org \
    --cc=jannh@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=riel@surriel.com \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.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®