Skip to contentSkip to conversation

Optimize mprotect() for large folios

PATCH Superseded Dev Jain 5 participants 62 messages Jun 28, 2025 – Jul 3, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Dev Jain

author
#0 lore raw

v4 cover Optimize mprotect() for large folios

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds T2: 7.9 seconds T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds T2: 2.2 seconds T3: 4.3 seconds

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

Here is the test program:

#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#define SIZE (1024*1024*1024)

unsigned long pmdsize = (1UL << 21);
unsigned long pagesize = (1UL << 12);

static void pte_map_thps(char *mem, size_t size)

{
	size_t offs;
	int ret = 0;


	/* PTE-map each THP by temporarily splitting the VMAs. */
	for (offs = 0; offs < size; offs += pmdsize) {
		ret |= madvise(mem + offs, pagesize, MADV_DONTFORK);
		ret |= madvise(mem + offs, pagesize, MADV_DOFORK);
	}

	if (ret) {
		fprintf(stderr, "ERROR: mprotect() failed\n");
		exit(1);
	}
}

int main(int argc, char *argv[])

{
	char *p;
        int ret = 0;
	p = mmap((1UL << 30), SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (p != (1UL << 30)) {
		perror("mmap");
		return 1;
	}



	memset(p, 0, SIZE);
	if (madvise(p, SIZE, MADV_NOHUGEPAGE))
		perror("madvise");
	explicit_bzero(p, SIZE);
	pte_map_thps(p, SIZE);

	for (int loops = 0; loops < 40; loops++) {
		if (mprotect(p, SIZE, PROT_READ))
			perror("mprotect"), exit(1);
		if (mprotect(p, SIZE, PROT_READ|PROT_WRITE))
			perror("mprotect"), exit(1);
		explicit_bzero(p, SIZE);
	}
}

---
The patchset is rebased onto Saturday's mm-new.

v3->v4:
- Refactor skipping logic into a new function, edit patch 1 subject to highlight it is only for MM_CP_PROT_NUMA case (David H)
- Refactor the optimization logic, add more documentation to the generic batched functions, do not add clear_flush_ptes, squash patch 4 and 5 (Ryan)

v2->v3:
- Add comments for the new APIs (Ryan, Lorenzo)
- Instead of refactoring, use a "skip_batch" label
- Move arm64 patches at the end (Ryan)
- In can_change_pte_writable(), check AnonExclusive page-by-page (David H)
- Resolve implicit declaration; tested build on x86 (Lance Yang)

v1->v2:
- Rebase onto mm-unstable (6ebffe676fcf: util_macros.h: make the header more resilient)
- Abridge the anon-exclusive condition (Lance Yang)

Dev Jain (4):
mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs
mm: Add batched versions of ptep_modify_prot_start/commit
mm: Optimize mprotect() by PTE-batching
arm64: Add batched versions of ptep_modify_prot_start/commit

 arch/arm64/include/asm/pgtable.h |  10 ++
 arch/arm64/mm/mmu.c              |  28 +++-
 include/linux/pgtable.h          |  83 +++++++++-
 mm/mprotect.c                    | 269 +++++++++++++++++++++++--------
 4 files changed, 315 insertions(+), 75 deletions(-)
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH v4 1/4 mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
 mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +87 -47
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
+
+		/* Do not skip in this case */
+		nr_ptes = 0;
+		goto out;
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}
+
 static long change_pte_range(struct mmu_gather *tlb,
 		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 	flush_tlb_batched_pending(vma->vm_mm);
 	arch_enter_lazy_mmu_mode();
 	do {
+		nr_ptes = 1;
 		oldpte = ptep_get(pte);
 		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
 			pte_t ptent;
 
 			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * pages. See similar comment in change_huge_pmd.
 			 */
 			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*
-				 * Skip scanning top tier node if normal numa
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)
 					continue;
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
 			}
 
 			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 				pages++;
 			}
 		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
 	pte_unmap_unlock(pte - 1, ptl);
 
Signature · 1 line
2.30.2
author
#15 lore raw

PATCH v4 2/4 mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

---
 include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
 mm/mprotect.c           |  4 +-
 2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +82 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
 
 /*
  * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
  */
 static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 					   unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 	__ptep_modify_prot_commit(vma, addr, ptep, pte);
 }
 #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+	pte_t pte, tmp_pte;
+
+	pte = ptep_modify_prot_start(vma, addr, ptep);
+	while (--nr) {
+		ptep++;
+		addr += PAGE_SIZE;
+		tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+		if (pte_dirty(tmp_pte))
+			pte = pte_mkdirty(pte);
+		if (pte_young(tmp_pte))
+			pte = pte_mkyoung(pte);
+	}
+	return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+		pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)
+{
+	int i;
+
+	for (i = 0; i < nr; ++i) {
+		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+		ptep++;
+		addr += PAGE_SIZE;
+		old_pte = pte_next_pfn(old_pte);
+		pte = pte_next_pfn(pte);
+	}
+}
+#endif
+
 #endif /* CONFIG_MMU */
 
 /*
mm/mprotect.c +2 -2
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
-			oldpte = ptep_modify_prot_start(vma, addr, pte);
+			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
 
 			if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 			    can_change_pte_writable(vma, addr, ptent))
 				ptent = pte_mkwrite(ptent, vma);
 
-			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
 			if (pte_needs_flush(oldpte, ptent))
 				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
 			pages++;
Signature · 1 line
2.30.2
author
#26 lore raw

PATCH v4 3/4 mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
 mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +117 -26
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
 #include "internal.h"
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};
 
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
 	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
 	/* Don't touch entries that are not even readable. */
 	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for softdirty tracking? */
 	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for uffd-wp tracking? */
 	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/*
 		 * Writable MAP_PRIVATE mapping: We can only special-case on
 		 * exclusive anonymous pages, because we know that our
 		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
 		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
 	}
 
 	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	 * FS was already notified and we can simply mark the PTE writable
 	 * just like the write-fault handler would do.
 	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))
+			break;
+		nr++;
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;
 }
 
 static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
 {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;
 
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))
 		return 1;
 
 	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
 	}
 
 skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);
 out:
 	*foliop = folio;
 	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
 		if (pte_present(oldpte)) {
 			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
 			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
 			/*
 			 * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}
 		} else if (is_swap_pte(oldpte)) {
 			swp_entry_t entry = pte_to_swp_entry(oldpte);
 			pte_t newpte;
Signature · 1 line
2.30.2
author
#51 lore raw

PATCH v4 4/4 arm64: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain

Override the generic definition of modify_prot_start_ptes() to use get_and_clear_full_ptes(). This helper does a TLBI only for the starting and ending contpte block of the range, whereas the current implementation will call ptep_get_and_clear() for every contpte block, thus doing a TLBI on every contpte block. Therefore, we have a performance win.

The arm64 definition of pte_accessible() allows us to batch in the errata specific case:

#define pte_accessible(mm, pte) \
(mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))

All ptes are obviously present in the folio batch, and they are also valid.

Override the generic definition of modify_prot_commit_ptes() to simply use set_ptes() to map the new ptes into the pagetable.

---
 arch/arm64/include/asm/pgtable.h | 10 ++++++++++
 arch/arm64/mm/mmu.c              | 28 +++++++++++++++++++++++-----
 2 files changed, 33 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable.h +10 -0
index ba63c8736666..abd2dee416b3 100644
@@ -1643,6 +1643,16 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma,
 				    unsigned long addr, pte_t *ptep,
 				    pte_t old_pte, pte_t new_pte);
 
+#define modify_prot_start_ptes modify_prot_start_ptes
+extern pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+				    unsigned long addr, pte_t *ptep,
+				    unsigned int nr);
+
+#define modify_prot_commit_ptes modify_prot_commit_ptes
+extern void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+				    pte_t *ptep, pte_t old_pte, pte_t pte,
+				    unsigned int nr);
+
 #ifdef CONFIG_ARM64_CONTPTE
 
 /*
arch/arm64/mm/mmu.c +23 -5
index 3d5fb37424ab..38325616f467 100644
@@ -26,6 +26,7 @@
 #include <linux/set_memory.h>
 #include <linux/kfence.h>
 #include <linux/pkeys.h>
+#include <linux/mm_inline.h>
 
 #include <asm/barrier.h>
 #include <asm/cputype.h>
@@ -1524,24 +1525,41 @@ static int __init prevent_bootmem_remove_init(void)
 early_initcall(prevent_bootmem_remove_init);
 #endif
 
-pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t *ptep, unsigned int nr)
 {
+	pte_t pte = get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, 0);
+
 	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
 		/*
 		 * Break-before-make (BBM) is required for all user space mappings
 		 * when the permission changes from executable to non-executable
 		 * in cases where cpu is affected with errata #2645198.
 		 */
-		if (pte_user_exec(ptep_get(ptep)))
-			return ptep_clear_flush(vma, addr, ptep);
+		if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte))
+			__flush_tlb_range(vma, addr, nr * PAGE_SIZE,
+					  PAGE_SIZE, true, 3);
 	}
-	return ptep_get_and_clear(vma->vm_mm, addr, ptep);
+
+	return pte;
+}
+
+pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+{
+	return modify_prot_start_ptes(vma, addr, ptep, 1);
+}
+
+void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t *ptep, pte_t old_pte, pte_t pte,
+			     unsigned int nr)
+{
+	set_ptes(vma->vm_mm, addr, ptep, pte, nr);
 }
 
 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
 			     pte_t old_pte, pte_t pte)
 {
-	set_pte_at(vma->vm_mm, addr, ptep, pte);
+	modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1);
 }
 
 /*
Signature · 1 line
2.30.2
author
#27 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 22 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

Quoted 3 lines

---

Forgot to add:

as this patch is almost identical to the diff Ryan had suggested.

maintainer
#53 lore raw
In reply to Dev Jain
Quoted 3 lines · On Sat, 28 Jun 2025 17:04:31 +0530 Dev Jain <[email protected]> wrote:

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

um what. Seems to claim that "selftests still compiles after I messed with stuff", which isn't very impressive ;) Please clarify?

Quoted 8 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Quoted 6 lines

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds T2: 7.9 seconds T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds T2: 2.2 seconds T3: 4.3 seconds

Well that's tasty.

Quoted 5 lines

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

author
#54 lore raw
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Sat, 28 Jun 2025 17:04:31 +0530 Dev Jain <[email protected]> wrote:

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

um what. Seems to claim that "selftests still compiles after I messed with stuff", which isn't very impressive ;) Please clarify?

Sorry I mean to say that the mm-selftests pass.

Quoted 22 lines
Quoted 9 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Quoted 6 lines

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds T2: 7.9 seconds T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds T2: 2.2 seconds T3: 4.3 seconds

Well that's tasty.

Quoted 5 lines

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

#2 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Dev Jain
Quoted 15 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
 mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +0 -0
index 88709c01177b..af10a7fbe6b8 100644

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 91

Quoted 9 lines
mm/mprotect.c +6 -0
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

The !folio check wasn't in the previous version. Why is it needed now?

Quoted 52 lines
mm/mprotect.c +52 -0
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 153

Quoted 10 lines
mm/mprotect.c +10 -0
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
+
+		/* Do not skip in this case */
+		nr_ptes = 0;
+		goto out;

This doesn't smell right... perhaps I'm not understanding the logic. Why do you return nr_ptes = 0 if you end up in this conditional, but nr_ptes = 1 if you don't take this conditional? I think you want to return nr_ptes == 0 for both cases?...

Quoted 75 lines
mm/mprotect.c +13 -39
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}
+
 static long change_pte_range(struct mmu_gather *tlb,
 		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 	flush_tlb_batched_pending(vma->vm_mm);
 	arch_enter_lazy_mmu_mode();
 	do {
+		nr_ptes = 1;
 		oldpte = ptep_get(pte);
 		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
 			pte_t ptent;
 
 			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * pages. See similar comment in change_huge_pmd.
 			 */
 			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*
-				 * Skip scanning top tier node if normal numa

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 206

Quoted 10 lines
mm/mprotect.c +5 -4
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)
 					continue;

...But now here nr_ptes == 0 for the "don't skip" case, so won't you process that PTE twice because while (pte += nr_ptes, ...) won't advance it?

Suggest forcing nr_ptes = 1 after this conditional "continue"?

Thanks,
Ryan

Quoted 15 lines
mm/mprotect.c +1 -4
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
 			}
 
 			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 				pages++;
 			}
 		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
 	pte_unmap_unlock(pte - 1, ptl);
 
author
#3 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

Quoted 19 lines · Ryan Roberts
Quoted 18 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
  mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
  1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +0 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
Quoted 7 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 91

Quoted 6 lines
mm/mprotect.c +6 -0
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

The !folio check wasn't in the previous version. Why is it needed now?

It was there, actually. After prot_numa_skip_ptes(), if the folio is still NULL, we get it using vm_normal_folio(). If this returns NULL, then mprotect_folio_pte_batch() will return 1 to say that we cannot batch.

Quoted 59 lines
Quoted 59 lines
mm/mprotect.c +58 -0
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 153

Quoted 4 lines
mm/mprotect.c +4 -0
+
+		/* Do not skip in this case */
+		nr_ptes = 0;
+		goto out;

This doesn't smell right... perhaps I'm not understanding the logic. Why do you return nr_ptes = 0 if you end up in this conditional, but nr_ptes = 1 if you don't take this conditional? I think you want to return nr_ptes == 0 for both cases?...

In the existing code, we do not skip if we take this conditional. So nr_ptes == 0 is only a hint that we don't have to skip in this case.

Quoted 89 lines
Quoted 76 lines
mm/mprotect.c +13 -39
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}
+
  static long change_pte_range(struct mmu_gather *tlb,
  		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
  		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
  	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
  	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
  	tlb_change_page_size(tlb, PAGE_SIZE);
  	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  	flush_tlb_batched_pending(vma->vm_mm);
  	arch_enter_lazy_mmu_mode();
  	do {
+		nr_ptes = 1;
  		oldpte = ptep_get(pte);
  		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
  			pte_t ptent;
 
  			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * pages. See similar comment in change_huge_pmd.
  			 */
  			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*
-				 * Skip scanning top tier node if normal numa

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 206

Quoted 10 lines
mm/mprotect.c +5 -4
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)
  					continue;

...But now here nr_ptes == 0 for the "don't skip" case, so won't you process that PTE twice because while (pte += nr_ptes, ...) won't advance it?

Quoted 1 line

Suggest forcing nr_ptes = 1 after this conditional "continue"?

nr_ptes will be forced to a non zero value through mprotect_folio_pte_batch().

Quoted 20 lines

Thanks,
Ryan

Quoted 15 lines
mm/mprotect.c +1 -4
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
  			}
 
  			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  				pages++;
  			}
  		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
  	arch_leave_lazy_mmu_mode();
  	pte_unmap_unlock(pte - 1, ptl);
 
#4 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

Quoted 102 lines · Dev Jain
Quoted 11 lines · Ryan Roberts
Quoted 10 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
  mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------

  1 file changed, 87 insertions(+), 47 deletions(-)
Quoted 15 lines
Quoted 5 lines
mm/mprotect.c +0 -0
index 88709c01177b..af10a7fbe6b8 100644

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 91

Quoted 9 lines
mm/mprotect.c +5 -0
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
       return pte_dirty(pte);
   }
   +static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+        pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

The !folio check wasn't in the previous version. Why is it needed now?

It was there, actually. After prot_numa_skip_ptes(), if the folio is still NULL, we get it using vm_normal_folio(). If this returns NULL, then mprotect_folio_pte_batch() will return 1 to say that we cannot batch.

Quoted 60 lines
Quoted 60 lines
mm/mprotect.c +58 -0
+        return 1;
+
+    return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+                   NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct
 *vma,
+        unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+        int max_nr_ptes)
+{
+    struct folio *folio = NULL;
+    int nr_ptes = 1;
+    bool toptier;
+    int nid;
+
+    /* Avoid TLB flush if possible */
+    if (pte_protnone(oldpte))
+        goto skip_batch;
+
+    folio = vm_normal_folio(vma, addr, oldpte);
+    if (!folio)
+        goto skip_batch;
+
+    if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+        goto skip_batch;
+
+    /* Also skip shared copy-on-write pages */
+    if (is_cow_mapping(vma->vm_flags) &&
+        (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+        goto skip_batch;
+
+    /*
+     * While migration can move some dirty pages,
+     * it cannot move them all from MIGRATE_ASYNC
+     * context.
+     */
+    if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+        goto skip_batch;
+
+    /*
+     * Don't mess with PTEs if page is already on the node
+     * a single-threaded process is running on.
+     */
+    nid = folio_nid(folio);
+    if (target_node == nid)
+        goto skip_batch;
+
+    toptier = node_is_toptier(nid);
+
+    /*
+     * Skip scanning top tier node if normal numa
+     * balancing is disabled
+     */
+    if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+        goto skip_batch;
+
+    if (folio_use_access_time(folio)) {
+        folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 153

Quoted 4 lines
mm/mprotect.c +4 -0
+
+        /* Do not skip in this case */
+        nr_ptes = 0;
+        goto out;

This doesn't smell right... perhaps I'm not understanding the logic. Why do you return nr_ptes = 0 if you end up in this conditional, but nr_ptes = 1 if you don't take this conditional? I think you want to return nr_ptes == 0 for both cases?...

Quoted 2 lines

In the existing code, we do not skip if we take this conditional. So nr_ptes == 0 is only a hint that we don't have to skip in this case.

We also do not skip if we do not take the conditional,right? "hint that we don't have to skip in this case"... no I think it's a "directive that we must not skip"? A hint is something that the implementation is free to ignore. But I don't think that's the case here.

What I'm saying is that I think this block should actually be:

	if (folio_use_access_time(folio))
		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));

	/* Do not skip in this case */
	nr_ptes = 0;
	goto out;
Quoted 88 lines
Quoted 88 lines
Quoted 75 lines
mm/mprotect.c +9 -0
+    }
+
+skip_batch:
+    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+    *foliop = folio;
+    return nr_ptes;
+}
+
   static long change_pte_range(struct mmu_gather *tlb,

          struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, pgprot_t newprot, unsigned long cp_flags)

mm/mprotect.c +4 -39
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
       bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
       bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
       bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+    int nr_ptes;
         tlb_change_page_size(tlb, PAGE_SIZE);
       pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
       flush_tlb_batched_pending(vma->vm_mm);
       arch_enter_lazy_mmu_mode();
       do {
+        nr_ptes = 1;
           oldpte = ptep_get(pte);
           if (pte_present(oldpte)) {
+            int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+            struct folio *folio = NULL;
               pte_t ptent;
                 /*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
                * pages. See similar comment in change_huge_pmd.
                */
               if (prot_numa) {
-                struct folio *folio;
-                int nid;
-                bool toptier;
-
-                /* Avoid TLB flush if possible */
-                if (pte_protnone(oldpte))
-                    continue;
-
-                folio = vm_normal_folio(vma, addr, oldpte);
-                if (!folio || folio_is_zone_device(folio) ||
-                    folio_test_ksm(folio))
-                    continue;
-
-                /* Also skip shared copy-on-write pages */
-                if (is_cow_mapping(vma->vm_flags) &&
-                    (folio_maybe_dma_pinned(folio) ||
-                     folio_maybe_mapped_shared(folio)))
-                    continue;
-
-                /*
-                 * While migration can move some dirty pages,
-                 * it cannot move them all from MIGRATE_ASYNC
-                 * context.
-                 */
-                if (folio_is_file_lru(folio) &&
-                    folio_test_dirty(folio))
-                    continue;
-
-                /*
-                 * Don't mess with PTEs if page is already on the node
-                 * a single-threaded process is running on.
-                 */
-                nid = folio_nid(folio);
-                if (target_node == nid)
-                    continue;
-                toptier = node_is_toptier(nid);
-
-                /*
-                 * Skip scanning top tier node if normal numa

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 206

Quoted 10 lines
mm/mprotect.c +5 -4
-                 * balancing is disabled
-                 */
-                if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-                    toptier)
+                nr_ptes = prot_numa_skip_ptes(&folio, vma,
+                                  addr, oldpte, pte,
+                                  target_node,
+                                  max_nr_ptes);
+                if (nr_ptes)
                       continue;

...But now here nr_ptes == 0 for the "don't skip" case, so won't you process that PTE twice because while (pte += nr_ptes, ...) won't advance it?

Quoted 3 lines
Quoted 1 line

Suggest forcing nr_ptes = 1 after this conditional "continue"?

nr_ptes will be forced to a non zero value through mprotect_folio_pte_batch().

But you don't call mprotect_folio_pte_batch() if you have set nr_ptes = 0; Perhaps you are referring to calling mprotect_folio_pte_batch() on the processing path in a future patch? But that means that this patch is buggy without the future patch.

Quoted 20 lines
Quoted 20 lines

Thanks,
Ryan

Quoted 14 lines
mm/mprotect.c +1 -4
-                if (folio_use_access_time(folio))
-                    folio_xchg_access_time(folio,
-                        jiffies_to_msecs(jiffies));
               }
                 oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   pages++;
               }
           }
-    } while (pte++, addr += PAGE_SIZE, addr != end);
+    } while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
       arch_leave_lazy_mmu_mode();
       pte_unmap_unlock(pte - 1, ptl);

author
#5 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

Quoted 106 lines · Ryan Roberts
Quoted 98 lines · Dev Jain
Quoted 11 lines · Ryan Roberts
Quoted 10 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
   mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
   1 file changed, 87 insertions(+), 47 deletions(-)
Quoted 15 lines
Quoted 5 lines
mm/mprotect.c +0 -0
index 88709c01177b..af10a7fbe6b8 100644

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 91

Quoted 9 lines
mm/mprotect.c +5 -0
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
       return pte_dirty(pte);
   }
   +static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+        pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

The !folio check wasn't in the previous version. Why is it needed now?

It was there, actually. After prot_numa_skip_ptes(), if the folio is still NULL, we get it using vm_normal_folio(). If this returns NULL, then mprotect_folio_pte_batch() will return 1 to say that we cannot batch.

Quoted 59 lines
Quoted 59 lines
mm/mprotect.c +58 -0
+        return 1;
+
+    return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+                   NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct
 *vma,
+        unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+        int max_nr_ptes)
+{
+    struct folio *folio = NULL;
+    int nr_ptes = 1;
+    bool toptier;
+    int nid;
+
+    /* Avoid TLB flush if possible */
+    if (pte_protnone(oldpte))
+        goto skip_batch;
+
+    folio = vm_normal_folio(vma, addr, oldpte);
+    if (!folio)
+        goto skip_batch;
+
+    if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+        goto skip_batch;
+
+    /* Also skip shared copy-on-write pages */
+    if (is_cow_mapping(vma->vm_flags) &&
+        (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+        goto skip_batch;
+
+    /*
+     * While migration can move some dirty pages,
+     * it cannot move them all from MIGRATE_ASYNC
+     * context.
+     */
+    if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+        goto skip_batch;
+
+    /*
+     * Don't mess with PTEs if page is already on the node
+     * a single-threaded process is running on.
+     */
+    nid = folio_nid(folio);
+    if (target_node == nid)
+        goto skip_batch;
+
+    toptier = node_is_toptier(nid);
+
+    /*
+     * Skip scanning top tier node if normal numa
+     * balancing is disabled
+     */
+    if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+        goto skip_batch;
+
+    if (folio_use_access_time(folio)) {
+        folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 153

Quoted 4 lines
mm/mprotect.c +4 -0
+
+        /* Do not skip in this case */
+        nr_ptes = 0;
+        goto out;

This doesn't smell right... perhaps I'm not understanding the logic. Why do you return nr_ptes = 0 if you end up in this conditional, but nr_ptes = 1 if you don't take this conditional? I think you want to return nr_ptes == 0 for both cases?...

Quoted 2 lines

In the existing code, we do not skip if we take this conditional. So nr_ptes == 0 is only a hint that we don't have to skip in this case.

We also do not skip if we do not take the conditional,right? "hint that we don't have to skip in this case"... no I think it's a "directive that we must not skip"? A hint is something that the implementation is free to ignore. But I don't think that's the case here.

Quoted 8 lines

What I'm saying is that I think this block should actually be:

	if (folio_use_access_time(folio))
		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));

	/* Do not skip in this case */
	nr_ptes = 0;
	goto out;

Ah you are right. Thanks!

Quoted 86 lines
Quoted 86 lines
Quoted 86 lines
Quoted 73 lines
mm/mprotect.c +13 -39
+    }
+
+skip_batch:
+    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+    *foliop = folio;
+    return nr_ptes;
+}
+
   static long change_pte_range(struct mmu_gather *tlb,
           struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
           unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
       bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
       bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
       bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+    int nr_ptes;
         tlb_change_page_size(tlb, PAGE_SIZE);
       pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
       flush_tlb_batched_pending(vma->vm_mm);
       arch_enter_lazy_mmu_mode();
       do {
+        nr_ptes = 1;
           oldpte = ptep_get(pte);
           if (pte_present(oldpte)) {
+            int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+            struct folio *folio = NULL;
               pte_t ptent;
                 /*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
                * pages. See similar comment in change_huge_pmd.
                */
               if (prot_numa) {
-                struct folio *folio;
-                int nid;
-                bool toptier;
-
-                /* Avoid TLB flush if possible */
-                if (pte_protnone(oldpte))
-                    continue;
-
-                folio = vm_normal_folio(vma, addr, oldpte);
-                if (!folio || folio_is_zone_device(folio) ||
-                    folio_test_ksm(folio))
-                    continue;
-
-                /* Also skip shared copy-on-write pages */
-                if (is_cow_mapping(vma->vm_flags) &&
-                    (folio_maybe_dma_pinned(folio) ||
-                     folio_maybe_mapped_shared(folio)))
-                    continue;
-
-                /*
-                 * While migration can move some dirty pages,
-                 * it cannot move them all from MIGRATE_ASYNC
-                 * context.
-                 */
-                if (folio_is_file_lru(folio) &&
-                    folio_test_dirty(folio))
-                    continue;
-
-                /*
-                 * Don't mess with PTEs if page is already on the node
-                 * a single-threaded process is running on.
-                 */
-                nid = folio_nid(folio);
-                if (target_node == nid)
-                    continue;
-                toptier = node_is_toptier(nid);
-
-                /*
-                 * Skip scanning top tier node if normal numa

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 206

Quoted 10 lines
mm/mprotect.c +5 -4
-                 * balancing is disabled
-                 */
-                if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-                    toptier)
+                nr_ptes = prot_numa_skip_ptes(&folio, vma,
+                                  addr, oldpte, pte,
+                                  target_node,
+                                  max_nr_ptes);
+                if (nr_ptes)
                       continue;

...But now here nr_ptes == 0 for the "don't skip" case, so won't you process that PTE twice because while (pte += nr_ptes, ...) won't advance it?

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

Suggest forcing nr_ptes = 1 after this conditional "continue"?

nr_ptes will be forced to a non zero value through mprotect_folio_pte_batch().

But you don't call mprotect_folio_pte_batch() if you have set nr_ptes = 0; Perhaps you are referring to calling mprotect_folio_pte_batch() on the processing path in a future patch? But that means that this patch is buggy without the future patch.

Yup it is there in the future patch. You are correct, I'll respin and force nr_ptes = 1 in this case.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

Thanks,
Ryan

Quoted 14 lines
mm/mprotect.c +1 -4
-                if (folio_use_access_time(folio))
-                    folio_xchg_access_time(folio,
-                        jiffies_to_msecs(jiffies));
               }
                 oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   pages++;
               }
           }
-    } while (pte++, addr += PAGE_SIZE, addr != end);
+    } while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
       arch_leave_lazy_mmu_mode();
       pte_unmap_unlock(pte - 1, ptl);
 
#16 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain
Quoted 15 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

---
 include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
 mm/mprotect.c           |  4 +-
 2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +0 -0
index cf1515c163e2..662f39e7475a 100644

include/linux/pgtable.h static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma, · line 1335

Quoted 6 lines
include/linux/pgtable.h +2 -1
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
 
 /*
  * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.

I find this last sentance a bit confusing. I think what you are trying to say is somehthing like:

"""
old_pte is the value returned from ptep_modify_prot_start() but may additionally have have young and/or dirty bits set where previously they were not. """

?

Quoted 21 lines
include/linux/pgtable.h +14 -0
  */
 static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 					   unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 	__ptep_modify_prot_commit(vma, addr, ptep, pte);
 }
 #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1367

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).

This last sentence is confusing... You had previous said the PFN can differ, but here you imply on a, d and sd bits are allowed to differ.

Quoted 29 lines
include/linux/pgtable.h +29 -0
+ */
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+	pte_t pte, tmp_pte;
+
+	pte = ptep_modify_prot_start(vma, addr, ptep);
+	while (--nr) {
+		ptep++;
+		addr += PAGE_SIZE;
+		tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+		if (pte_dirty(tmp_pte))
+			pte = pte_mkdirty(pte);
+		if (pte_young(tmp_pte))
+			pte = pte_mkyoung(pte);
+	}
+	return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1406

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

Same comment as for ptep_modify_prot_start().

Quoted 25 lines
include/linux/pgtable.h +17 -0
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+		pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)
+{
+	int i;
+
+	for (i = 0; i < nr; ++i) {
+		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+		ptep++;
+		addr += PAGE_SIZE;
+		old_pte = pte_next_pfn(old_pte);
+		pte = pte_next_pfn(pte);
+	}
+}
+#endif
+
 #endif /* CONFIG_MMU */
 
 /*
mm/mprotect.c +0 -0
index af10a7fbe6b8..627b0d67cc4a 100644

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 209

Quoted 5 lines
mm/mprotect.c +1 -1
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
-			oldpte = ptep_modify_prot_start(vma, addr, pte);
+			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

You're calling this with nr_ptes = 0 for the prot_numa case. But the implementation expects minimum nr_ptes == 1.

Quoted 12 lines
mm/mprotect.c +1 -1
 			ptent = pte_modify(oldpte, newprot);
 
 			if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 			    can_change_pte_writable(vma, addr, ptent))
 				ptent = pte_mkwrite(ptent, vma);
 
-			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
 			if (pte_needs_flush(oldpte, ptent))
 				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
 			pages++;
author
#17 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

Quoted 25 lines · Ryan Roberts
Quoted 15 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
  mm/mprotect.c           |  4 +-
  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +0 -0
index cf1515c163e2..662f39e7475a 100644

include/linux/pgtable.h static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma, · line 1335

Quoted 6 lines
include/linux/pgtable.h +2 -1
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
 
  /*
   * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.

I find this last sentance a bit confusing. I think what you are trying to say is somehthing like:

Quoted 4 lines

"""
old_pte is the value returned from ptep_modify_prot_start() but may additionally have have young and/or dirty bits set where previously they were not. """

Thanks.

Quoted 27 lines

?

Quoted 25 lines
include/linux/pgtable.h +18 -0
   */
  static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
  					   unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
  	__ptep_modify_prot_commit(vma, addr, ptep, pte);
  }
  #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
Quoted 8 lines

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1367

Quoted 6 lines
include/linux/pgtable.h +6 -0
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).

This last sentence is confusing... You had previous said the PFN can differ, but here you imply on a, d and sd bits are allowed to differ.

Forgot to mention the PFNs, kind of took them as implied. So mentioning the PFNs also will do or do you suggest a better wording?

Quoted 70 lines
Quoted 30 lines
include/linux/pgtable.h +29 -0
+ */
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+	pte_t pte, tmp_pte;
+
+	pte = ptep_modify_prot_start(vma, addr, ptep);
+	while (--nr) {
+		ptep++;
+		addr += PAGE_SIZE;
+		tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+		if (pte_dirty(tmp_pte))
+			pte = pte_mkdirty(pte);
+		if (pte_young(tmp_pte))
+			pte = pte_mkyoung(pte);
+	}
+	return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1406

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

Same comment as for ptep_modify_prot_start().

Quoted 28 lines
include/linux/pgtable.h +17 -0
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+		pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)
+{
+	int i;
+
+	for (i = 0; i < nr; ++i) {
+		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+		ptep++;
+		addr += PAGE_SIZE;
+		old_pte = pte_next_pfn(old_pte);
+		pte = pte_next_pfn(pte);
+	}
+}
+#endif
+
  #endif /* CONFIG_MMU */
 
  /*
mm/mprotect.c +0 -0
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
Quoted 4 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 209

Quoted 2 lines
mm/mprotect.c +1 -1
-			oldpte = ptep_modify_prot_start(vma, addr, pte);
+			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

You're calling this with nr_ptes = 0 for the prot_numa case. But the implementation expects minimum nr_ptes == 1.

This will get fixed when I force nr_ptes = 1 in the previous patch right?

Quoted 13 lines
Quoted 13 lines
mm/mprotect.c +1 -1
  			ptent = pte_modify(oldpte, newprot);
 
  			if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  			    can_change_pte_writable(vma, addr, ptent))
  				ptent = pte_mkwrite(ptent, vma);
 
-			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
  			if (pte_needs_flush(oldpte, ptent))
  				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
  			pages++;
#28 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 110 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
 mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +38 -12
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
 #include "internal.h"
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};
 
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
 	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
 	/* Don't touch entries that are not even readable. */
 	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for softdirty tracking? */
 	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for uffd-wp tracking? */
 	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/*
 		 * Writable MAP_PRIVATE mapping: We can only special-case on
 		 * exclusive anonymous pages, because we know that our
 		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
 		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
 	}
 
 	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	 * FS was already notified and we can simply mark the PTE writable
 	 * just like the write-fault handler would do.
 	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)
+{
+	struct page *page;
+	int nr = 1;
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 119

Quoted 10 lines
mm/mprotect.c +10 -0
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Quoted 20 lines
mm/mprotect.c +20 -0
+			break;
+		nr++;
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +4 -2
 }
 
 static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
 {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Can't we just pass in the flags we want?

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 149

Quoted 3 lines
mm/mprotect.c +1 -1
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Quoted 26 lines
mm/mprotect.c +6 -2
 		return 1;
 
 	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
 	}
 
 skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);
 out:
 	*foliop = folio;
 	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
 		if (pte_present(oldpte)) {
 			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
 			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
 			/*
 			 * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 8 lines
mm/mprotect.c +5 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

From the other thread, my memory is jogged that this function ignores write permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 1 line
mm/mprotect.c +0 -0
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 9 lines
mm/mprotect.c +0 -3
 			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 300

Quoted 10 lines
mm/mprotect.c +4 -6
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Why not just:

			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
				    !pte_write(ptent) &&
				    maybe_change_pte_writable(...);

?

Quoted 8 lines
mm/mprotect.c +8 -0
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 318

Quoted 10 lines
mm/mprotect.c +10 -0
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Thanks,
Ryan

Quoted 14 lines
mm/mprotect.c +11 -0
+					tlb_flush_pte_range(tlb, addr,
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}
 		} else if (is_swap_pte(oldpte)) {
 			swp_entry_t entry = pte_to_swp_entry(oldpte);
 			pte_t newpte;
#18 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain
Quoted 72 lines · Dev Jain
Quoted 25 lines · Ryan Roberts
Quoted 15 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++- mm/mprotect.c           |  4 +-

  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +0 -0
index cf1515c163e2..662f39e7475a 100644

include/linux/pgtable.h static inline pte_t ptep_modify_prot_start(struct · line 1335

Quoted 6 lines
include/linux/pgtable.h +2 -1
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct
 vm_area_struct *vma,
     /*
    * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.

I find this last sentance a bit confusing. I think what you are trying to say is somehthing like:

Quoted 4 lines

"""
old_pte is the value returned from ptep_modify_prot_start() but may additionally have have young and/or dirty bits set where previously they were not. """

Thanks.

Quoted 29 lines

?

Quoted 27 lines
include/linux/pgtable.h +3 -0
    */
   static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
                          unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct
 vm_area_struct *vma,
       __ptep_modify_prot_commit(vma, addr, ptep, pte);
   }
   #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write

transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.

Quoted 8 lines
Quoted 6 lines

+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).

This last sentence is confusing... You had previous said the PFN can differ, but here you imply on a, d and sd bits are allowed to differ.

Quoted 2 lines

Forgot to mention the PFNs, kind of took them as implied. So mentioning the PFNs also will do or do you suggest a better wording?

Perhaps:

"""
Context: The caller holds the page table lock. The PTEs map consecutive pages that belong to the same folio. All other PTE bits must be identical for all PTEs in the batch except for young and dirty bits. The PTEs are all in the same PMD.
"""

You mention the soft dirty bit not needing to be the same in your current wording, but I don't think that is correct? soft dirty needs to be the same, right?

Quoted 75 lines
Quoted 66 lines
Quoted 31 lines

+ */

include/linux/pgtable.h +28 -0
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+        unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+    pte_t pte, tmp_pte;
+
+    pte = ptep_modify_prot_start(vma, addr, ptep);
+    while (--nr) {
+        ptep++;
+        addr += PAGE_SIZE;
+        tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+        if (pte_dirty(tmp_pte))
+            pte = pte_mkdirty(pte);
+        if (pte_young(tmp_pte))
+            pte = pte_mkyoung(pte);
+    }
+    return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1406

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

Same comment as for ptep_modify_prot_start().

Quoted 23 lines

+ */

include/linux/pgtable.h +2 -0
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h +13 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;
+        addr += PAGE_SIZE;
+        old_pte = pte_next_pfn(old_pte);
+        pte = pte_next_pfn(pte);
+    }
+}
+#endif
+

  #endif /* CONFIG_MMU */
    /*

mm/mprotect.c +0 -1
index af10a7fbe6b8..627b0d67cc4a 100644
--- a/mm/mprotect.c
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 209

Quoted 6 lines

+++ b/mm/mprotect.c

mm/mprotect.c +1 -0
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
   -            oldpte = ptep_modify_prot_start(vma, addr, pte);
+            oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

You're calling this with nr_ptes = 0 for the prot_numa case. But the implementation expects minimum nr_ptes == 1.

Quoted 1 line

This will get fixed when I force nr_ptes = 1 in the previous patch right?

Yep, just pointing it out.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
mm/mprotect.c +1 -0
               ptent = pte_modify(oldpte, newprot);
                 if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   can_change_pte_writable(vma, addr, ptent))
                   ptent = pte_mkwrite(ptent, vma);
   -            ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
               if (pte_needs_flush(oldpte, ptent))
                   tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
               pages++;
Reviewed-by on v4 4/4
#52 lore raw

Re: [PATCH v4 4/4] arm64: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain
Quoted 12 lines · Dev Jain

Override the generic definition of modify_prot_start_ptes() to use get_and_clear_full_ptes(). This helper does a TLBI only for the starting and ending contpte block of the range, whereas the current implementation will call ptep_get_and_clear() for every contpte block, thus doing a TLBI on every contpte block. Therefore, we have a performance win.

The arm64 definition of pte_accessible() allows us to batch in the errata specific case:

#define pte_accessible(mm, pte) \
(mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))

Quoted 6 lines

All ptes are obviously present in the folio batch, and they are also valid.

Override the generic definition of modify_prot_commit_ptes() to simply use set_ptes() to map the new ptes into the pagetable.

Quoted 85 lines
---
 arch/arm64/include/asm/pgtable.h | 10 ++++++++++
 arch/arm64/mm/mmu.c              | 28 +++++++++++++++++++++++-----
 2 files changed, 33 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable.h +10 -0
index ba63c8736666..abd2dee416b3 100644
@@ -1643,6 +1643,16 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma,
 				    unsigned long addr, pte_t *ptep,
 				    pte_t old_pte, pte_t new_pte);
 
+#define modify_prot_start_ptes modify_prot_start_ptes
+extern pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+				    unsigned long addr, pte_t *ptep,
+				    unsigned int nr);
+
+#define modify_prot_commit_ptes modify_prot_commit_ptes
+extern void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+				    pte_t *ptep, pte_t old_pte, pte_t pte,
+				    unsigned int nr);
+
 #ifdef CONFIG_ARM64_CONTPTE
 
 /*
arch/arm64/mm/mmu.c +23 -5
index 3d5fb37424ab..38325616f467 100644
@@ -26,6 +26,7 @@
 #include <linux/set_memory.h>
 #include <linux/kfence.h>
 #include <linux/pkeys.h>
+#include <linux/mm_inline.h>
 
 #include <asm/barrier.h>
 #include <asm/cputype.h>
@@ -1524,24 +1525,41 @@ static int __init prevent_bootmem_remove_init(void)
 early_initcall(prevent_bootmem_remove_init);
 #endif
 
-pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t *ptep, unsigned int nr)
 {
+	pte_t pte = get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, 0);
+
 	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
 		/*
 		 * Break-before-make (BBM) is required for all user space mappings
 		 * when the permission changes from executable to non-executable
 		 * in cases where cpu is affected with errata #2645198.
 		 */
-		if (pte_user_exec(ptep_get(ptep)))
-			return ptep_clear_flush(vma, addr, ptep);
+		if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte))
+			__flush_tlb_range(vma, addr, nr * PAGE_SIZE,
+					  PAGE_SIZE, true, 3);
 	}
-	return ptep_get_and_clear(vma->vm_mm, addr, ptep);
+
+	return pte;
+}
+
+pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+{
+	return modify_prot_start_ptes(vma, addr, ptep, 1);
+}
+
+void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t *ptep, pte_t old_pte, pte_t pte,
+			     unsigned int nr)
+{
+	set_ptes(vma->vm_mm, addr, ptep, pte, nr);
 }
 
 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
 			     pte_t old_pte, pte_t pte)
 {
-	set_pte_at(vma->vm_mm, addr, ptep, pte);
+	modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1);
 }
 
 /*
author
#19 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Ryan Roberts
Quoted 81 lines · Ryan Roberts
Quoted 69 lines · Dev Jain
Quoted 25 lines · Ryan Roberts
Quoted 15 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

---
   include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
   mm/mprotect.c           |  4 +-
   2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +0 -0
index cf1515c163e2..662f39e7475a 100644

include/linux/pgtable.h static inline pte_t ptep_modify_prot_start(struct · line 1335

Quoted 6 lines
include/linux/pgtable.h +2 -1
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct
 vm_area_struct *vma,
     /*
    * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.

I find this last sentance a bit confusing. I think what you are trying to say is somehthing like:

Quoted 4 lines

"""
old_pte is the value returned from ptep_modify_prot_start() but may additionally have have young and/or dirty bits set where previously they were not. """

Thanks.

Quoted 29 lines

?

Quoted 27 lines
include/linux/pgtable.h +3 -0
    */
   static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
                          unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct
 vm_area_struct *vma,
       __ptep_modify_prot_commit(vma, addr, ptep, pte);
   }
   #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write

transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.

Quoted 8 lines
Quoted 6 lines

+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).

This last sentence is confusing... You had previous said the PFN can differ, but here you imply on a, d and sd bits are allowed to differ.

Quoted 2 lines

Forgot to mention the PFNs, kind of took them as implied. So mentioning the PFNs also will do or do you suggest a better wording?

Perhaps:

"""
Context: The caller holds the page table lock. The PTEs map consecutive pages that belong to the same folio. All other PTE bits must be identical for all PTEs in the batch except for young and dirty bits. The PTEs are all in the same PMD.
"""

Quoted 2 lines

You mention the soft dirty bit not needing to be the same in your current wording, but I don't think that is correct? soft dirty needs to be the same, right?

Oh god, confused this with the skipping case, you are right.

Quoted 86 lines
Quoted 73 lines
Quoted 65 lines
Quoted 30 lines

+ */

include/linux/pgtable.h +28 -0
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+        unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+    pte_t pte, tmp_pte;
+
+    pte = ptep_modify_prot_start(vma, addr, ptep);
+    while (--nr) {
+        ptep++;
+        addr += PAGE_SIZE;
+        tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+        if (pte_dirty(tmp_pte))
+            pte = pte_mkdirty(pte);
+        if (pte_young(tmp_pte))
+            pte = pte_mkyoung(pte);
+    }
+    return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1406

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

Same comment as for ptep_modify_prot_start().

Quoted 23 lines

+ */

include/linux/pgtable.h +2 -0
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h +13 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;
+        addr += PAGE_SIZE;
+        old_pte = pte_next_pfn(old_pte);
+        pte = pte_next_pfn(pte);
+    }
+}
+#endif
+
   #endif /* CONFIG_MMU */
     /*
mm/mprotect.c +0 -1
index af10a7fbe6b8..627b0d67cc4a 100644
--- a/mm/mprotect.c
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 209

Quoted 6 lines

+++ b/mm/mprotect.c

mm/mprotect.c +1 -0
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
   -            oldpte = ptep_modify_prot_start(vma, addr, pte);
+            oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

You're calling this with nr_ptes = 0 for the prot_numa case. But the implementation expects minimum nr_ptes == 1.

Quoted 1 line

This will get fixed when I force nr_ptes = 1 in the previous patch right?

Yep, just pointing it out.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/mprotect.c +1 -0
               ptent = pte_modify(oldpte, newprot);
                 if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   can_change_pte_writable(vma, addr, ptent))
                   ptent = pte_mkwrite(ptent, vma);
   -            ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
               if (pte_needs_flush(oldpte, ptent))
                   tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
               pages++;
#55 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Sat, 28 Jun 2025 17:04:31 +0530 Dev Jain <[email protected]> wrote:

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

um what.  Seems to claim that "selftests still compiles after I messed with stuff", which isn't very impressive ;)  Please clarify?

Sorry I mean to say that the mm-selftests pass.

I think you're saying you both compiled and ran the mm selftests for arm64. And additionally you compiled for x86_64? (Just trying to help clarify).

Quoted 23 lines
Quoted 23 lines
Quoted 10 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Quoted 6 lines

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds   T2: 7.9 seconds   T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds   T2: 2.2 seconds   T3: 4.3 seconds

Well that's tasty.

Quoted 5 lines

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

#57 lore raw
In reply to Dev Jain
Quoted 3 lines · Dev Jain

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

Should also be tested on x86-64 not only build tested :)

You are still not really giving details here, so same comment as your mremap() series, please explain why you're doing this, what for, what benefits you expect to achieve, where etc.

E.g. 'this is deisgned to optimise mTHP cases on arm64, we expect to see benefits on amd64 also and for intel there should be no impact'.

It's probably also worth actually going and checking to make sure that this is the case re: other arches. See below on that...

Quoted 13 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds T2: 7.9 seconds T3: 4.2 seconds

Quoted 7 lines

After the patchset:
T1: 2.1 seconds T2: 2.2 seconds T3: 4.3 seconds

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

This is nice, though order-0 is probably going to be your bread and butter no?

Having said that, mprotect() is not a hot path, this delta is small enough to quite possibly just be noise, and personally I'm not all that bothered.

But let's run this same test on x86-64 too please and get some before/after numbers just to confirm no major impact.

Thanks for including code.

Quoted 96 lines

Here is the test program:

#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#define SIZE (1024*1024*1024)

unsigned long pmdsize = (1UL << 21);
unsigned long pagesize = (1UL << 12);

static void pte_map_thps(char *mem, size_t size)

{
	size_t offs;
	int ret = 0;


	/* PTE-map each THP by temporarily splitting the VMAs. */
	for (offs = 0; offs < size; offs += pmdsize) {
		ret |= madvise(mem + offs, pagesize, MADV_DONTFORK);
		ret |= madvise(mem + offs, pagesize, MADV_DOFORK);
	}

	if (ret) {
		fprintf(stderr, "ERROR: mprotect() failed\n");
		exit(1);
	}
}

int main(int argc, char *argv[])

{
	char *p;
        int ret = 0;
	p = mmap((1UL << 30), SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (p != (1UL << 30)) {
		perror("mmap");
		return 1;
	}



	memset(p, 0, SIZE);
	if (madvise(p, SIZE, MADV_NOHUGEPAGE))
		perror("madvise");
	explicit_bzero(p, SIZE);
	pte_map_thps(p, SIZE);

	for (int loops = 0; loops < 40; loops++) {
		if (mprotect(p, SIZE, PROT_READ))
			perror("mprotect"), exit(1);
		if (mprotect(p, SIZE, PROT_READ|PROT_WRITE))
			perror("mprotect"), exit(1);
		explicit_bzero(p, SIZE);
	}
}

---
The patchset is rebased onto Saturday's mm-new.

v3->v4:
- Refactor skipping logic into a new function, edit patch 1 subject to highlight it is only for MM_CP_PROT_NUMA case (David H)
- Refactor the optimization logic, add more documentation to the generic batched functions, do not add clear_flush_ptes, squash patch 4 and 5 (Ryan)

v2->v3:
- Add comments for the new APIs (Ryan, Lorenzo)
- Instead of refactoring, use a "skip_batch" label
- Move arm64 patches at the end (Ryan)
- In can_change_pte_writable(), check AnonExclusive page-by-page (David H)
- Resolve implicit declaration; tested build on x86 (Lance Yang)

v1->v2:
- Rebase onto mm-unstable (6ebffe676fcf: util_macros.h: make the header more resilient)
- Abridge the anon-exclusive condition (Lance Yang)

Dev Jain (4):
mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs
mm: Add batched versions of ptep_modify_prot_start/commit
mm: Optimize mprotect() by PTE-batching
arm64: Add batched versions of ptep_modify_prot_start/commit

 arch/arm64/include/asm/pgtable.h |  10 ++
 arch/arm64/mm/mmu.c              |  28 +++-
 include/linux/pgtable.h          |  83 +++++++++-
 mm/mprotect.c                    | 269 +++++++++++++++++++++++--------
 4 files changed, 315 insertions(+), 75 deletions(-)

--
2.30.2

author
#29 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 114 lines · Ryan Roberts
Quoted 113 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +41 -12
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
  #include "internal.h"
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};
 
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
  	/* Don't touch entries that are not even readable. */
  	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for softdirty tracking? */
  	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for uffd-wp tracking? */
  	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	if (!(vma->vm_flags & VM_SHARED)) {
  		/*
  		 * Writable MAP_PRIVATE mapping: We can only special-case on
  		 * exclusive anonymous pages, because we know that our
  		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
  		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
  	}
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	 * FS was already notified and we can simply mark the PTE writable
  	 * just like the write-fault handler would do.
  	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 119

Quoted 7 lines
mm/mprotect.c +7 -0
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Thanks I'll drop it. I have a habit of putting brackets everywhere because debugging operator precedence bugs is a nightmare - finally the time has come to learn operator precedence!

Quoted 34 lines
Quoted 21 lines
mm/mprotect.c +20 -0
+			break;
+		nr++;
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +4 -2
  }
 
  static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
  {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Quoted 1 line

Can't we just pass in the flags we want?

Yup that is cleaner.

Quoted 6 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 149

Quoted 4 lines
mm/mprotect.c +1 -1
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Stupid me forgot to drop it from the earlier patch.

Quoted 30 lines
Quoted 30 lines
mm/mprotect.c +6 -2
  		return 1;
 
  	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
  	}
 
  skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);
  out:
  	*foliop = folio;
  	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
  		if (pte_present(oldpte)) {
  			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
  			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
  			/*
  			 * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
Quoted 10 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 5 lines
mm/mprotect.c +5 -0
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

From the other thread, my memory is jogged that this function ignores write
permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

No, we are not ignoring the write permission bit. There is no way currently to do that via folio_pte_batch. So the pte batch is either entirely writable or entirely not.

Quoted 25 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 2 lines
mm/mprotect.c +0 -0
  			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 9 lines
mm/mprotect.c +0 -3
  			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * example, if a PTE is already dirty and no other
  			 * COW or special handling is required.
  			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 300

Quoted 10 lines
mm/mprotect.c +4 -6
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Why not just:

Quoted 3 lines
			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
				    !pte_write(ptent) &&
				    maybe_change_pte_writable(...);

set_write is an int, which is supposed to span {TRI_MAYBE, TRI_FALSE, TRI_TRUE}, whereas the RHS of this statement will always return a boolean.

You proposed it like this in your diff; it took hours for my eyes to catch this : )

Quoted 15 lines

?

Quoted 12 lines
mm/mprotect.c +12 -0
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 318

Quoted 6 lines
mm/mprotect.c +6 -0
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Powerpc flushes if access bit transitions from set to unset. x86 does that for both dirty and access. Both problems are solved by modify_prot_start_ptes() which collects a/d bits, both in the generic implementation and the arm64 implementation.

Quoted 18 lines

Thanks,
Ryan

Quoted 14 lines
mm/mprotect.c +11 -0
+					tlb_flush_pte_range(tlb, addr,
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}
  		} else if (is_swap_pte(oldpte)) {
  			swp_entry_t entry = pte_to_swp_entry(oldpte);
  			pte_t newpte;
author
#56 lore raw
In reply to Ryan Roberts
Quoted 4 lines · Ryan Roberts
Quoted 3 lines · Dev Jain
Quoted 2 lines · Andrew Morton

On Sat, 28 Jun 2025 17:04:31 +0530 Dev Jain <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

um what.  Seems to claim that "selftests still compiles after I messed with stuff", which isn't very impressive ;)  Please clarify?

Sorry I mean to say that the mm-selftests pass.

I think you're saying you both compiled and ran the mm selftests for arm64. And additionally you compiled for x86_64? (Just trying to help clarify).

Yes, ran mm-selftests on arm64, and build-tested the patches for x86.

Quoted 23 lines
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Quoted 6 lines

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds   T2: 7.9 seconds   T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds   T2: 2.2 seconds   T3: 4.3 seconds

Well that's tasty.

Quoted 5 lines

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

#6 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Dev Jain
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Can we maybe just drop this for this series please?

Quoted 15 lines
---
 mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +1 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 10 lines
mm/mprotect.c +10 -0
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}

I find it really odd that you're introducing this in a seemingly unrelated change.

Also won't this conflict with David's changes?

I know you like to rush out a dozen series at once, but once again I'm asking maybe please hold off?

I seem to remember David asked you for the same thing because of this, but maybe I'm misremembering.

We have only so much review resource and adding in brand new concepts mid-way and doing things that blatantly conflict with other series really doesn't help.

Quoted 55 lines
mm/mprotect.c +55 -0
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
+
+		/* Do not skip in this case */

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 161

Quoted 10 lines
mm/mprotect.c +10 -0
+		nr_ptes = 0;
+		goto out;
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}

Yeah yuck. I don't like that we're doing all this for this edge case.

Quoted 66 lines
mm/mprotect.c +5 -38
+
 static long change_pte_range(struct mmu_gather *tlb,
 		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 	flush_tlb_batched_pending(vma->vm_mm);
 	arch_enter_lazy_mmu_mode();
 	do {
+		nr_ptes = 1;
 		oldpte = ptep_get(pte);
 		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
 			pte_t ptent;
 
 			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * pages. See similar comment in change_huge_pmd.
 			 */
 			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 205

Quoted 10 lines
mm/mprotect.c +5 -5
-				 * Skip scanning top tier node if normal numa
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)

I'm not really a fan of this being added (unless I'm missing something here) but _generally_ it's better to separate out a move and a change if you can.

Quoted 17 lines
mm/mprotect.c +1 -5
 					continue;
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
 			}
 
 			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 				pages++;
 			}
 		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
 	pte_unmap_unlock(pte - 1, ptl);
 
--
Quoted 2 lines

2.30.2

Anyway will hold off on reviewing the actual changes here until we can figure out whether this is event appropriate here.

author
#58 lore raw
In reply to Lorenzo Stoakes
Quoted 4 lines · Lorenzo Stoakes
Quoted 3 lines · Dev Jain

This patchset optimizes the mprotect() system call for large folios by PTE-batching. No issues were observed with mm-selftests, build tested on x86_64.

Quoted 8 lines

Should also be tested on x86-64 not only build tested :)

You are still not really giving details here, so same comment as your mremap() series, please explain why you're doing this, what for, what benefits you expect to achieve, where etc.

E.g. 'this is deisgned to optimise mTHP cases on arm64, we expect to see benefits on amd64 also and for intel there should be no impact'.

Okay.

Quoted 19 lines

It's probably also worth actually going and checking to make sure that this is the case re: other arches. See below on that...

Quoted 15 lines

We use the following test cases to measure performance, mprotect()'ing the mapped memory to read-only then read-write 40 times:

Test case 1: Mapping 1G of memory, touching it to get PMD-THPs, then pte-mapping those THPs
Test case 2: Mapping 1G of memory with 64K mTHPs
Test case 3: Mapping 1G of memory with 4K pages

Average execution time on arm64, Apple M3:
Before the patchset:
T1: 7.9 seconds T2: 7.9 seconds T3: 4.2 seconds

After the patchset:
T1: 2.1 seconds T2: 2.2 seconds T3: 4.3 seconds

Quoted 8 lines
Quoted 4 lines

Observing T1/T2 and T3 before the patchset, we also remove the regression introduced by ptep_get() on a contpte block. And, for large folios we get an almost 74% performance improvement, albeit the trade-off being a slight degradation in the small folio case.

This is nice, though order-0 is probably going to be your bread and butter no?

Having said that, mprotect() is not a hot path, this delta is small enough to quite possibly just be noise, and personally I'm not all that bothered.

It is only the vm_normal_folio() + folio_test_large() overhead. Trying to avoid this by the horrible maybe_contiguous_pte_pfns() I introduced somewhere else is not worth it : )

Quoted 101 lines

But let's run this same test on x86-64 too please and get some before/after numbers just to confirm no major impact.

Thanks for including code.

Quoted 95 lines

Here is the test program:

  #define _GNU_SOURCE
  #include <sys/mman.h>
  #include <stdlib.h>
  #include <string.h>
  #include <stdio.h>
  #include <unistd.h>

  #define SIZE (1024*1024*1024)

unsigned long pmdsize = (1UL << 21);
unsigned long pagesize = (1UL << 12);

static void pte_map_thps(char *mem, size_t size)

{
	size_t offs;
	int ret = 0;


	/* PTE-map each THP by temporarily splitting the VMAs. */
	for (offs = 0; offs < size; offs += pmdsize) {
		ret |= madvise(mem + offs, pagesize, MADV_DONTFORK);
		ret |= madvise(mem + offs, pagesize, MADV_DOFORK);
	}

	if (ret) {
		fprintf(stderr, "ERROR: mprotect() failed\n");
		exit(1);
	}
}

int main(int argc, char *argv[])

{
	char *p;
         int ret = 0;
	p = mmap((1UL << 30), SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (p != (1UL << 30)) {
		perror("mmap");
		return 1;
	}



	memset(p, 0, SIZE);
	if (madvise(p, SIZE, MADV_NOHUGEPAGE))
		perror("madvise");
	explicit_bzero(p, SIZE);
	pte_map_thps(p, SIZE);

	for (int loops = 0; loops < 40; loops++) {
		if (mprotect(p, SIZE, PROT_READ))
			perror("mprotect"), exit(1);
		if (mprotect(p, SIZE, PROT_READ|PROT_WRITE))
			perror("mprotect"), exit(1);
		explicit_bzero(p, SIZE);
	}
}

---
The patchset is rebased onto Saturday's mm-new.

v3->v4:
- Refactor skipping logic into a new function, edit patch 1 subject to highlight it is only for MM_CP_PROT_NUMA case (David H)
- Refactor the optimization logic, add more documentation to the generic batched functions, do not add clear_flush_ptes, squash patch 4 and 5 (Ryan)

v2->v3:
- Add comments for the new APIs (Ryan, Lorenzo)
- Instead of refactoring, use a "skip_batch" label
- Move arm64 patches at the end (Ryan)
- In can_change_pte_writable(), check AnonExclusive page-by-page (David H)
- Resolve implicit declaration; tested build on x86 (Lance Yang)

v1->v2:
- Rebase onto mm-unstable (6ebffe676fcf: util_macros.h: make the header more resilient)
- Abridge the anon-exclusive condition (Lance Yang)

Dev Jain (4):
mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs
mm: Add batched versions of ptep_modify_prot_start/commit
mm: Optimize mprotect() by PTE-batching
arm64: Add batched versions of ptep_modify_prot_start/commit

  arch/arm64/include/asm/pgtable.h |  10 ++
  arch/arm64/mm/mmu.c              |  28 +++-
  include/linux/pgtable.h          |  83 +++++++++-
  mm/mprotect.c                    | 269 +++++++++++++++++++++++--------
  4 files changed, 315 insertions(+), 75 deletions(-)

--
2.30.2

#59 lore raw
In reply to Dev Jain

To reiterate what I said on 1/4 - overall since this series conflicts with David's changes - can we hold off on any respin please until David's settles and lands in mm-new at least?

Thanks.

#7 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Lorenzo Stoakes
Quoted 6 lines · Lorenzo Stoakes
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Quoted 7 lines

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Can we maybe just drop this for this series please?

From my perspective, at least, there are no new logical changes in here vs the previous version. And I don't think the patches have been re-organised either. David (I think?) was asking for the name of the patch to be changed to include MM_CP_PROT_NUMA and also for the code to be moved out of line to it's own function. That's all that Dev has done AFAICT (although as per my review comments, the refactoring has introduced a bug).

My preference is that we should ultimately support this batching. It could be a separate series if you insist, but it's all contbuting to the same goal ultimately; making mprotect support PTE batching.

Just my 2c.

Thanks,
Ryan

Quoted 210 lines
Quoted 16 lines
---
 mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +1 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 10 lines
mm/mprotect.c +10 -0
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}

I find it really odd that you're introducing this in a seemingly unrelated change.

Also won't this conflict with David's changes?

I know you like to rush out a dozen series at once, but once again I'm asking maybe please hold off?

I seem to remember David asked you for the same thing because of this, but maybe I'm misremembering.

We have only so much review resource and adding in brand new concepts mid-way and doing things that blatantly conflict with other series really doesn't help.

Quoted 55 lines
mm/mprotect.c +55 -0
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
+
+		/* Do not skip in this case */

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 161

Quoted 10 lines
mm/mprotect.c +10 -0
+		nr_ptes = 0;
+		goto out;
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}

Yeah yuck. I don't like that we're doing all this for this edge case.

Quoted 66 lines
mm/mprotect.c +5 -38
+
 static long change_pte_range(struct mmu_gather *tlb,
 		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 	flush_tlb_batched_pending(vma->vm_mm);
 	arch_enter_lazy_mmu_mode();
 	do {
+		nr_ptes = 1;
 		oldpte = ptep_get(pte);
 		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
 			pte_t ptent;
 
 			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * pages. See similar comment in change_huge_pmd.
 			 */
 			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 205

Quoted 10 lines
mm/mprotect.c +5 -5
-				 * Skip scanning top tier node if normal numa
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)

I'm not really a fan of this being added (unless I'm missing something here) but _generally_ it's better to separate out a move and a change if you can.

Quoted 17 lines
mm/mprotect.c +1 -5
 					continue;
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
 			}
 
 			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 				pages++;
 			}
 		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
 	pte_unmap_unlock(pte - 1, ptl);
 
--
Quoted 2 lines

2.30.2

Anyway will hold off on reviewing the actual changes here until we can figure out whether this is event appropriate here.

author
#9 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Lorenzo Stoakes
Quoted 5 lines · Lorenzo Stoakes
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Quoted 7 lines

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Can we maybe just drop this for this series please?

I refactored this into a new function on David's suggestion:

https://lore.kernel.org/all/[email protected]/

Maybe you are saying, having a refactoring patch first and then the "skip a PTE batch" second, I'll be happy to do that, that would be cleaner.

Quoted 27 lines
Quoted 15 lines
---
  mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
  1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +1 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 10 lines
mm/mprotect.c +10 -0
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}

I find it really odd that you're introducing this in a seemingly unrelated change.

Quoted 1 line

Also won't this conflict with David's changes?

This series was (ofcourse, IMHO) pretty stable at v3, and there were comments coming on David's series, so I guessed that he will have to post a v2 anyways after mine gets merged. My guess could have been wrong. For the khugepaged batching series, I have sent the migration race patch separately exactly because of his series, so that in that case the rebasing burden is mine.

Quoted 3 lines

I know you like to rush out a dozen series at once, but once again I'm asking maybe please hold off?

Lorenzo : ) Except for the mremap series which you pointed out, I make it a point to never repost in the same week, unless it is an obvious single patch, and even in that case I give 2-3 days for the reviews to settle. I posted
v3 of this series more than a month ago, so it makes total sense to post this. Also I have seen many people spamming the list with the next versions on literally the same day, not that I am using this as a precedent. The mistake I made here is that on Saturday I saw David's series but then forgot that I am using the same infrastructure he is changing and went ahead posting this. I suddenly remembered this during the khugepaged series and dropped the first two patches for that.

Quoted 3 lines

I seem to remember David asked you for the same thing because of this, but maybe I'm misremembering.

I don't recollect that happening, maybe I am wrong.

Quoted 141 lines

We have only so much review resource and adding in brand new concepts mid-way and doing things that blatantly conflict with other series really doesn't help.

Quoted 55 lines
mm/mprotect.c +55 -0
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{
+	struct folio *folio = NULL;
+	int nr_ptes = 1;
+	bool toptier;
+	int nid;
+
+	/* Avoid TLB flush if possible */
+	if (pte_protnone(oldpte))
+		goto skip_batch;
+
+	folio = vm_normal_folio(vma, addr, oldpte);
+	if (!folio)
+		goto skip_batch;
+
+	if (folio_is_zone_device(folio) || folio_test_ksm(folio))
+		goto skip_batch;
+
+	/* Also skip shared copy-on-write pages */
+	if (is_cow_mapping(vma->vm_flags) &&
+	    (folio_maybe_dma_pinned(folio) || folio_maybe_mapped_shared(folio)))
+		goto skip_batch;
+
+	/*
+	 * While migration can move some dirty pages,
+	 * it cannot move them all from MIGRATE_ASYNC
+	 * context.
+	 */
+	if (folio_is_file_lru(folio) && folio_test_dirty(folio))
+		goto skip_batch;
+
+	/*
+	 * Don't mess with PTEs if page is already on the node
+	 * a single-threaded process is running on.
+	 */
+	nid = folio_nid(folio);
+	if (target_node == nid)
+		goto skip_batch;
+
+	toptier = node_is_toptier(nid);
+
+	/*
+	 * Skip scanning top tier node if normal numa
+	 * balancing is disabled
+	 */
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && toptier)
+		goto skip_batch;
+
+	if (folio_use_access_time(folio)) {
+		folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
+
+		/* Do not skip in this case */

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 161

Quoted 10 lines
mm/mprotect.c +10 -0
+		nr_ptes = 0;
+		goto out;
+	}
+
+skip_batch:
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+out:
+	*foliop = folio;
+	return nr_ptes;
+}

Yeah yuck. I don't like that we're doing all this for this edge case.

Quoted 70 lines
mm/mprotect.c +5 -42
+
  static long change_pte_range(struct mmu_gather *tlb,
  		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
  		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
@@ -94,6 +171,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
  	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
  	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
+	int nr_ptes;
 
  	tlb_change_page_size(tlb, PAGE_SIZE);
  	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
@@ -108,8 +186,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  	flush_tlb_batched_pending(vma->vm_mm);
  	arch_enter_lazy_mmu_mode();
  	do {
+		nr_ptes = 1;
  		oldpte = ptep_get(pte);
  		if (pte_present(oldpte)) {
+			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
+			struct folio *folio = NULL;
  			pte_t ptent;
 
  			/*
@@ -117,53 +198,12 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * pages. See similar comment in change_huge_pmd.
  			 */
  			if (prot_numa) {
-				struct folio *folio;
-				int nid;
-				bool toptier;
-
-				/* Avoid TLB flush if possible */
-				if (pte_protnone(oldpte))
-					continue;
-
-				folio = vm_normal_folio(vma, addr, oldpte);
-				if (!folio || folio_is_zone_device(folio) ||
-				    folio_test_ksm(folio))
-					continue;
-
-				/* Also skip shared copy-on-write pages */
-				if (is_cow_mapping(vma->vm_flags) &&
-				    (folio_maybe_dma_pinned(folio) ||
-				     folio_maybe_mapped_shared(folio)))
-					continue;
-
-				/*
-				 * While migration can move some dirty pages,
-				 * it cannot move them all from MIGRATE_ASYNC
-				 * context.
-				 */
-				if (folio_is_file_lru(folio) &&
-				    folio_test_dirty(folio))
-					continue;
-
-				/*
-				 * Don't mess with PTEs if page is already on the node
-				 * a single-threaded process is running on.
-				 */
-				nid = folio_nid(folio);
-				if (target_node == nid)
-					continue;
-				toptier = node_is_toptier(nid);
-
-				/*
-				 * Skip scanning top tier node if normal numa
-				 * balancing is disabled
-				 */
-				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 205

Quoted 6 lines
mm/mprotect.c +5 -1
-				    toptier)
+				nr_ptes = prot_numa_skip_ptes(&folio, vma,
+							      addr, oldpte, pte,
+							      target_node,
+							      max_nr_ptes);
+				if (nr_ptes)

I'm not really a fan of this being added (unless I'm missing something here) but _generally_ it's better to separate out a move and a change if you can.

Yup I'll split this patch.

Quoted 22 lines
Quoted 18 lines
mm/mprotect.c +1 -5
  					continue;
-				if (folio_use_access_time(folio))
-					folio_xchg_access_time(folio,
-						jiffies_to_msecs(jiffies));
  			}
 
  			oldpte = ptep_modify_prot_start(vma, addr, pte);
@@ -280,7 +320,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  				pages++;
  			}
  		}
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+	} while (pte += nr_ptes, addr += nr_ptes * PAGE_SIZE, addr != end);
  	arch_leave_lazy_mmu_mode();
  	pte_unmap_unlock(pte - 1, ptl);
 
--
Quoted 2 lines

2.30.2

Anyway will hold off on reviewing the actual changes here until we can figure out whether this is event appropriate here.

author
#60 lore raw
In reply to Lorenzo Stoakes
Quoted 5 lines · Lorenzo Stoakes

To reiterate what I said on 1/4 - overall since this series conflicts with David's changes - can we hold off on any respin please until David's settles and lands in mm-new at least?

Thanks.

I agree, David's series should get stable by the time I get ready to post the next version, @Andrew could you remove this from mm-new please?

author
#30 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 220 lines · Dev Jain
Quoted 116 lines · Ryan Roberts
Quoted 115 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143
+++++++++++++++++++++++++++++++++++++++++---------

  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +5 -3
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
     #include "internal.h"
   -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
-{
-    struct page *page;
+enum tristate {
+    TRI_FALSE = 0,
+    TRI_TRUE = 1,
+    TRI_MAYBE = -1,
+};

  +/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */

mm/mprotect.c +4 -0
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+                     unsigned long addr, pte_t pte,
+                     struct folio *folio)
+{

      if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-        return false;
+        return TRI_FALSE;
        /* Don't touch entries that are not even readable. */ if (pte_protnone(pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for softdirty tracking? */ if (pte_needs_soft_dirty_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for uffd-wp tracking? */
      if (userfaultfd_pte_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        if (!(vma->vm_flags & VM_SHARED)) {
          /*
           * Writable MAP_PRIVATE mapping: We can only special-case on
           * exclusive anonymous pages, because we know that our
           * write-fault handler similarly would map them writable without -         * any additional checks while holding the PT lock.
+         * any additional checks while holding the PT lock. So if the +         * folio is not anonymous, we know we cannot change pte to +         * writable. If it is anonymous then the caller must further +         * check that the page is AnonExclusive().
           */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page); +        return (!folio || folio_test_anon(folio)) ? TRI_MAYBE :
TRI_FALSE;
      }
        VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c +5 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct 
 vm_area_struct *vma, unsigned long addr,
        * FS was already notified and we can simply mark the PTE writable
        * just like the write-fault handler would do.
        */
-    return pte_dirty(pte);
+    return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the 

page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+                bool *exclusive)

mm/mprotect.c +7 -0
+{
+    struct page *page;
+    int nr = 1;
+
+    if (!folio) {
+        *exclusive = false;
+        return nr;
Quoted 8 lines
Quoted 7 lines

+    }
+
+    page = folio_page(folio, pgidx++);
+    *exclusive = PageAnonExclusive(page);
+    while (nr < max_nr) {
+        page = folio_page(folio, pgidx++);
+        if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Thanks I'll drop it. I have a habit of putting brackets everywhere because debugging operator precedence bugs is a nightmare - finally the time has come to learn operator precedence!

Quoted 35 lines
Quoted 24 lines

+            break;
+        nr++;
+    }

mm/mprotect.c +5 -0
+
+    return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned 

long addr,
+                 pte_t pte)

mm/mprotect.c +11 -0
+{
+    struct page *page;
+    int ret;
+
+    ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+    if (ret == TRI_MAYBE) {
+        page = vm_normal_page(vma, addr, pte);
+        ret = page && PageAnon(page) && PageAnonExclusive(page);
+    }
+
+    return ret;

  }
    static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

Quoted 8 lines

-        pte_t *ptep, pte_t pte, int max_nr_ptes)
+        pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
  {
-    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; +    fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; +
+    flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Quoted 1 line

Can't we just pass in the flags we want?

Yup that is cleaner.

Quoted 5 lines
Quoted 3 lines

  -    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1)) +    if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Stupid me forgot to drop it from the earlier patch.

Quoted 33 lines
Quoted 33 lines

          return 1;
        return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,

mm/mprotect.c +0 -0
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio 
 **foliop, struct vm_area_struct *vma
       }

    skip_batch:
-    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, +                       max_nr_ptes, 0);
  out:
      *foliop = folio;
      return nr_ptes;

mm/mprotect.c +6 -1
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather 
 *tlb,
           if (pte_present(oldpte)) {
               int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
               struct folio *folio = NULL;
-            pte_t ptent;
+            int sub_nr_ptes, pgidx = 0;
+            pte_t ptent, newpte;
+            bool sub_set_write;
+            int set_write;
                 /*
                * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather 
 *tlb,
                       continue;
               }
   +            if (!folio)
+                folio = vm_normal_folio(vma, addr, oldpte);
+
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather · line 276

Quoted 3 lines
mm/mprotect.c +2 -0
+            nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, 
 oldpte,
+                               max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

 From the other thread, my memory is jogged that this function ignores write permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

Quoted 3 lines

No, we are not ignoring the write permission bit. There is no way currently to do that via folio_pte_batch. So the pte batch is either entirely writable or entirely not.

Oh no. We do a pte_mkwrprotect() in pte_batch_clear_ignored(), I missed that.

Quoted 84 lines
Quoted 26 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 3 lines
mm/mprotect.c +0 -0
               oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 12 lines

              ptent = pte_modify(oldpte, newprot);
  @@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
               * example, if a PTE is already dirty and no other
               * COW or special handling is required.
               */
-            if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-                !pte_write(ptent) &&
-                can_change_pte_writable(vma, addr, ptent))
-                ptent = pte_mkwrite(ptent, vma);
-
-            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);

Quoted 8 lines

-            if (pte_needs_flush(oldpte, ptent))
-                tlb_flush_pte_range(tlb, addr, PAGE_SIZE); -            pages++;
+            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && +                    !pte_write(ptent);
+            if (set_write)
+                set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Quoted 4 lines

Why not just:
            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pte_write(ptent) && maybe_change_pte_writable(...);

set_write is an int, which is supposed to span {TRI_MAYBE, TRI_FALSE, TRI_TRUE}, whereas the RHS of this statement will always return a boolean.

You proposed it like this in your diff; it took hours for my eyes to catch this : )

Quoted 15 lines

?

Quoted 12 lines
mm/mprotect.c +12 -0
+
+            while (nr_ptes) {
+                if (set_write == TRI_MAYBE) {
+                    sub_nr_ptes = anon_exclusive_batch(folio,
+                        pgidx, nr_ptes, &sub_set_write);
+                } else {
+                    sub_nr_ptes = nr_ptes;
+                    sub_set_write = (set_write == TRI_TRUE);
+                }
+
+                if (sub_set_write)
+                    newpte = pte_mkwrite(ptent, vma);
Quoted 8 lines
Quoted 6 lines

+                else
+                    newpte = ptent;
+
+                modify_prot_commit_ptes(vma, addr, pte, oldpte, +                            newpte, sub_nr_ptes);
+                if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Powerpc flushes if access bit transitions from set to unset. x86 does that for both dirty and access. Both problems are solved by modify_prot_start_ptes() which collects a/d bits, both in the generic implementation and the arm64 implementation.

Quoted 18 lines

Thanks,
Ryan

Quoted 14 lines

+ tlb_flush_pte_range(tlb, addr,
+                        sub_nr_ptes * PAGE_SIZE);
+
+                addr += sub_nr_ptes * PAGE_SIZE;
+                pte += sub_nr_ptes;
+                oldpte = pte_advance_pfn(oldpte, sub_nr_ptes); +                ptent = pte_advance_pfn(ptent, sub_nr_ptes); +                nr_ptes -= sub_nr_ptes;
+                pages += sub_nr_ptes;
+                pgidx += sub_nr_ptes;
+            }
          } else if (is_swap_pte(oldpte)) { swp_entry_t entry = pte_to_swp_entry(oldpte); pte_t newpte;

#31 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 205 lines · Dev Jain
Quoted 107 lines · Ryan Roberts
Quoted 106 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------

  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +5 -3
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
     #include "internal.h"
   -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
-{
-    struct page *page;
+enum tristate {
+    TRI_FALSE = 0,
+    TRI_TRUE = 1,
+    TRI_MAYBE = -1,
+};

  +/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */

mm/mprotect.c +4 -0
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+                     unsigned long addr, pte_t pte,
+                     struct folio *folio)
+{

      if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-        return false;
+        return TRI_FALSE;
        /* Don't touch entries that are not even readable. */ if (pte_protnone(pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for softdirty tracking? */ if (pte_needs_soft_dirty_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for uffd-wp tracking? */
      if (userfaultfd_pte_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        if (!(vma->vm_flags & VM_SHARED)) {
          /*
           * Writable MAP_PRIVATE mapping: We can only special-case on
           * exclusive anonymous pages, because we know that our
           * write-fault handler similarly would map them writable without -         * any additional checks while holding the PT lock.
+         * any additional checks while holding the PT lock. So if the +         * folio is not anonymous, we know we cannot change pte to +         * writable. If it is anonymous then the caller must further +         * check that the page is AnonExclusive().
           */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page); +        return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE; }
        VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c +18 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
        * FS was already notified and we can simply mark the PTE writable
        * just like the write-fault handler would do.
        */
-    return pte_dirty(pte);
+    return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+                bool *exclusive)
+{
+    struct page *page;
+    int nr = 1;
+
+    if (!folio) {
+        *exclusive = false;
+        return nr;
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 119

Quoted 7 lines
mm/mprotect.c +7 -0
+    }
+
+    page = folio_page(folio, pgidx++);
+    *exclusive = PageAnonExclusive(page);
+    while (nr < max_nr) {
+        page = folio_page(folio, pgidx++);
+        if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Thanks I'll drop it. I have a habit of putting brackets everywhere because debugging operator precedence bugs is a nightmare - finally the time has come to learn operator precedence!

Quoted 33 lines
Quoted 20 lines
mm/mprotect.c +19 -0
+            break;
+        nr++;
+    }
+
+    return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+                 pte_t pte)
+{
+    struct page *page;
+    int ret;
+
+    ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+    if (ret == TRI_MAYBE) {
+        page = vm_normal_page(vma, addr, pte);
+        ret = page && PageAnon(page) && PageAnonExclusive(page);
+    }
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +5 -2
+    return ret;
   }
     static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-        pte_t *ptep, pte_t pte, int max_nr_ptes)
+        pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
   {
-    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+    fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Quoted 1 line

Can't we just pass in the flags we want?

Yup that is cleaner.

Quoted 5 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 149

Quoted 3 lines
mm/mprotect.c +1 -0
   -    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+    if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Stupid me forgot to drop it from the earlier patch.

Quoted 29 lines
Quoted 29 lines
mm/mprotect.c +0 -0
           return 1;
         return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop,

struct vm_area_struct *vma
      }
    skip_batch:
-    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes); +    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, +                       max_nr_ptes, 0);
  out:
      *foliop = folio;
      return nr_ptes;

mm/mprotect.c +5 -1
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
           if (pte_present(oldpte)) {
               int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
               struct folio *folio = NULL;
-            pte_t ptent;
+            int sub_nr_ptes, pgidx = 0;
+            pte_t ptent, newpte;
+            bool sub_set_write;
+            int set_write;
                 /*
                * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
   +            if (!folio)
+                folio = vm_normal_folio(vma, addr, oldpte);
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 3 lines
mm/mprotect.c +3 -0
+
+            nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+                               max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

 From the other thread, my memory is jogged that this function ignores write permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

Quoted 3 lines

No, we are not ignoring the write permission bit. There is no way currently to do that via folio_pte_batch. So the pte batch is either entirely writable or entirely not.

How are you enforcing that then? Surely folio_pte_batch() is the only thing looking at the individual PTEs? It's not guaranteed that just because the PTEs all belong to a single VMA that the permissions are all the same; you could have an RW private anon VMA which has been forked so all set to COW then individual PTEs have faulted and broken COW (as an example).

Quoted 29 lines
Quoted 24 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 3 lines
mm/mprotect.c +0 -0
               oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 10 lines
mm/mprotect.c +0 -0
               ptent = pte_modify(oldpte, newprot);
   @@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,

               * example, if a PTE is already dirty and no other
               * COW or special handling is required. */
-            if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && -                !pte_write(ptent) &&
-                can_change_pte_writable(vma, addr, ptent)) -                ptent = pte_mkwrite(ptent, vma);
-

Quoted 8 lines

-            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes); -            if (pte_needs_flush(oldpte, ptent))
-                tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-            pages++;
+            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && +                    !pte_write(ptent);
+            if (set_write)
+                set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Quoted 4 lines

Why not just:
            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pte_write(ptent) && maybe_change_pte_writable(...);

Quoted 4 lines

set_write is an int, which is supposed to span {TRI_MAYBE, TRI_FALSE, TRI_TRUE}, whereas the RHS of this statement will always return a boolean.

You proposed it like this in your diff; it took hours for my eyes to catch this : )

Ahh good spot! I don't really love the tristate thing, but couldn't think of anything better. So I guess it should really be:

		set_write = ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
			    !pte_write(ptent)) ? TRI_MAYBE : TRI_FALSE;
		if (set_write == TRI_MAYBE)
			set_write = maybe_change_pte_writable(...);

That would make it a bit more obvious as to what is going on for the reader?

Thanks,
Ryan

Quoted 48 lines
Quoted 16 lines

?

Quoted 12 lines
mm/mprotect.c +12 -0
+
+            while (nr_ptes) {
+                if (set_write == TRI_MAYBE) {
+                    sub_nr_ptes = anon_exclusive_batch(folio,
+                        pgidx, nr_ptes, &sub_set_write);
+                } else {
+                    sub_nr_ptes = nr_ptes;
+                    sub_set_write = (set_write == TRI_TRUE);
+                }
+
+                if (sub_set_write)
+                    newpte = pte_mkwrite(ptent, vma);
Quoted 8 lines
Quoted 6 lines

+                else
+                    newpte = ptent;
+
+                modify_prot_commit_ptes(vma, addr, pte, oldpte, +                            newpte, sub_nr_ptes);
+                if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Powerpc flushes if access bit transitions from set to unset. x86 does that for both dirty and access. Both problems are solved by modify_prot_start_ptes() which collects a/d bits, both in the generic implementation and the arm64 implementation.

Quoted 18 lines

Thanks,
Ryan

Quoted 14 lines

+                    tlb_flush_pte_range(tlb, addr,
+                        sub_nr_ptes * PAGE_SIZE);
+
+                addr += sub_nr_ptes * PAGE_SIZE;
+                pte += sub_nr_ptes;
+                oldpte = pte_advance_pfn(oldpte, sub_nr_ptes); +                ptent = pte_advance_pfn(ptent, sub_nr_ptes); +                nr_ptes -= sub_nr_ptes;
+                pages += sub_nr_ptes;
+                pgidx += sub_nr_ptes;
+            }
          } else if (is_swap_pte(oldpte)) { swp_entry_t entry = pte_to_swp_entry(oldpte); pte_t newpte;

#10 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Dev Jain
Quoted 13 lines · Dev Jain
Quoted 11 lines · Lorenzo Stoakes
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Quoted 8 lines
Quoted 1 line

Can we maybe just drop this for this series please?

I refactored this into a new function on David's suggestion:

https://lore.kernel.org/all/[email protected]/

Maybe you are saying, having a refactoring patch first and then the "skip a PTE batch" second, I'll be happy to do that, that would be cleaner.

OK apologies then, my mistake.

So essentially you were doing this explicitly for the prot numa case and it just wasn't clear in subject line before, ok.

Yes please separate the two out!

Quoted 5 lines

This series was (ofcourse, IMHO) pretty stable at v3, and there were comments coming on David's series, so I guessed that he will have to post a v2 anyways after mine gets merged. My guess could have been wrong. For the khugepaged batching series, I have sent the migration race patch separately exactly because of his series, so that in that case the rebasing burden is mine.

This stuff can be difficult to align on, but I'd suggest that when there's another series in the interim that is fundamentally changing a function signature that will make your code not compile it's probably best to hold off.

And that's why I'm suggesting this here.

Quoted 5 lines
Quoted 4 lines

I know you like to rush out a dozen series at once, but once again I'm asking maybe please hold off?

Quoted 10 lines

Lorenzo : ) Except for the mremap series which you pointed out, I make it a point to never repost in the same week, unless it is an obvious single patch, and even in that case I give 2-3 days for the reviews to settle. I posted
v3 of this series more than a month ago, so it makes total sense to post this. Also I have seen many people spamming the list with the next versions on literally the same day, not that I am using this as a precedent. The mistake I made here is that on Saturday I saw David's series but then forgot that I am using the same infrastructure he is changing and went ahead posting this. I suddenly remembered this during the khugepaged series and dropped the first two patches for that.

I'm not saying you shot this out shortly after a previous version, I'm saying you have a whole bunch of series at once (I know as I'm cc'd on them all I think :), and I'm politely asking you to maybe not send another that causes a known conflict.

Whether or not you do so is up to you, it was a request.

Quoted 6 lines
Quoted 4 lines

I seem to remember David asked you for the same thing because of this, but maybe I'm misremembering.

I don't recollect that happening, maybe I am wrong.

Yeah maybe I'm misremembering, apologies if so!

#8 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Ryan Roberts
Quoted 22 lines · Ryan Roberts
Quoted 6 lines · Lorenzo Stoakes
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Quoted 7 lines

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Can we maybe just drop this for this series please?

From my perspective, at least, there are no new logical changes in here vs the previous version. And I don't think the patches have been re-organised either. David (I think?) was asking for the name of the patch to be changed to include MM_CP_PROT_NUMA and also for the code to be moved out of line to it's own function. That's all that Dev has done AFAICT (although as per my review comments, the refactoring has introduced a bug).

Quoted 8 lines

My preference is that we should ultimately support this batching. It could be a separate series if you insist, but it's all contbuting to the same goal ultimately; making mprotect support PTE batching.

Just my 2c.

Thanks,
Ryan

Ack, was my mistake, apologies. I hadn't realised this was part of the series, I hadn't looked it for a while...

But I think it's better to have the refactor and the batch bit done separately so it's clear which is which, unless the change is so trivial as for that to be just noise.

Cheers, Lorenzo

author
#32 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 198 lines · Ryan Roberts
Quoted 197 lines · Dev Jain
Quoted 107 lines · Ryan Roberts
Quoted 106 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
   mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
   1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +40 -11
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
     #include "internal.h"
   -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
-{
-    struct page *page;
+enum tristate {
+    TRI_FALSE = 0,
+    TRI_TRUE = 1,
+    TRI_MAYBE = -1,
+};
   +/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+                     unsigned long addr, pte_t pte,
+                     struct folio *folio)
+{
       if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-        return false;
+        return TRI_FALSE;
         /* Don't touch entries that are not even readable. */
       if (pte_protnone(pte))
-        return false;
+        return TRI_FALSE;
         /* Do we need write faults for softdirty tracking? */
       if (pte_needs_soft_dirty_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
         /* Do we need write faults for uffd-wp tracking? */
       if (userfaultfd_pte_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
         if (!(vma->vm_flags & VM_SHARED)) {
           /*
            * Writable MAP_PRIVATE mapping: We can only special-case on
            * exclusive anonymous pages, because we know that our
            * write-fault handler similarly would map them writable without
-         * any additional checks while holding the PT lock.
+         * any additional checks while holding the PT lock. So if the
+         * folio is not anonymous, we know we cannot change pte to
+         * writable. If it is anonymous then the caller must further
+         * check that the page is AnonExclusive().
            */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page);
+        return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
       }
         VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
        * FS was already notified and we can simply mark the PTE writable
        * just like the write-fault handler would do.
        */
-    return pte_dirty(pte);
+    return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+                bool *exclusive)
+{
+    struct page *page;
+    int nr = 1;
+
+    if (!folio) {
+        *exclusive = false;
+        return nr;
Quoted 8 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 119

Quoted 7 lines
mm/mprotect.c +7 -0
+    }
+
+    page = folio_page(folio, pgidx++);
+    *exclusive = PageAnonExclusive(page);
+    while (nr < max_nr) {
+        page = folio_page(folio, pgidx++);
+        if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Thanks I'll drop it. I have a habit of putting brackets everywhere because debugging operator precedence bugs is a nightmare - finally the time has come to learn operator precedence!

Quoted 32 lines
Quoted 19 lines
mm/mprotect.c +19 -0
+            break;
+        nr++;
+    }
+
+    return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+                 pte_t pte)
+{
+    struct page *page;
+    int ret;
+
+    ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+    if (ret == TRI_MAYBE) {
+        page = vm_normal_page(vma, addr, pte);
+        ret = page && PageAnon(page) && PageAnonExclusive(page);
+    }
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +5 -2
+    return ret;
   }
     static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-        pte_t *ptep, pte_t pte, int max_nr_ptes)
+        pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
   {
-    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+    fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Quoted 1 line

Can't we just pass in the flags we want?

Yup that is cleaner.

Quoted 4 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 149

Quoted 2 lines
mm/mprotect.c +1 -0
   -    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+    if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Stupid me forgot to drop it from the earlier patch.

Quoted 36 lines
Quoted 24 lines
mm/mprotect.c +0 -0
           return 1;
         return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop,

struct vm_area_struct *vma
      }
    skip_batch:
-    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes); +    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, +                       max_nr_ptes, 0);
  out:
      *foliop = folio;
      return nr_ptes;

mm/mprotect.c +4 -1
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
           if (pte_present(oldpte)) {
               int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
               struct folio *folio = NULL;
-            pte_t ptent;
+            int sub_nr_ptes, pgidx = 0;
+            pte_t ptent, newpte;
+            bool sub_set_write;
+            int set_write;
                 /*
                * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 274

Quoted 7 lines
mm/mprotect.c +4 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
   +            if (!folio)
+                folio = vm_normal_folio(vma, addr, oldpte);
+
+            nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+                               max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

 From the other thread, my memory is jogged that this function ignores write
permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

Quoted 8 lines
Quoted 3 lines

No, we are not ignoring the write permission bit. There is no way currently to do that via folio_pte_batch. So the pte batch is either entirely writable or entirely not.

How are you enforcing that then? Surely folio_pte_batch() is the only thing looking at the individual PTEs? It's not guaranteed that just because the PTEs all belong to a single VMA that the permissions are all the same; you could have an RW private anon VMA which has been forked so all set to COW then individual PTEs have faulted and broken COW (as an example).

Yup I just replied in the other mail, I missed the pte_mkwrprotect() in folio_pte_batch().

Quoted 35 lines
Quoted 24 lines
Quoted 24 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 3 lines
mm/mprotect.c +0 -0
               oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 18 lines
mm/mprotect.c +4 -9
               ptent = pte_modify(oldpte, newprot);
   @@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
                * example, if a PTE is already dirty and no other
                * COW or special handling is required.
                */
-            if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-                !pte_write(ptent) &&
-                can_change_pte_writable(vma, addr, ptent))
-                ptent = pte_mkwrite(ptent, vma);
-
-            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-            if (pte_needs_flush(oldpte, ptent))
-                tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-            pages++;
+            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+                    !pte_write(ptent);
+            if (set_write)
+                set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
Quoted 8 lines
Quoted 4 lines

Why not just:
            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pte_write(ptent) && maybe_change_pte_writable(...);

set_write is an int, which is supposed to span {TRI_MAYBE, TRI_FALSE, TRI_TRUE}, whereas the RHS of this statement will always return a boolean.

You proposed it like this in your diff; it took hours for my eyes to catch this : )

Ahh good spot! I don't really love the tristate thing, but couldn't think of anything better. So I guess it should really be:

Quoted 6 lines
		set_write = ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
			    !pte_write(ptent)) ? TRI_MAYBE : TRI_FALSE;
		if (set_write == TRI_MAYBE)
			set_write = maybe_change_pte_writable(...);

That would make it a bit more obvious as to what is going on for the reader?

Nice!

Quoted 48 lines

Thanks,
Ryan

Quoted 44 lines
Quoted 14 lines

?

Quoted 12 lines
mm/mprotect.c +12 -0
+
+            while (nr_ptes) {
+                if (set_write == TRI_MAYBE) {
+                    sub_nr_ptes = anon_exclusive_batch(folio,
+                        pgidx, nr_ptes, &sub_set_write);
+                } else {
+                    sub_nr_ptes = nr_ptes;
+                    sub_set_write = (set_write == TRI_TRUE);
+                }
+
+                if (sub_set_write)
+                    newpte = pte_mkwrite(ptent, vma);
Quoted 8 lines
Quoted 6 lines

+                else
+                    newpte = ptent;
+
+                modify_prot_commit_ptes(vma, addr, pte, oldpte, +                            newpte, sub_nr_ptes);
+                if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Powerpc flushes if access bit transitions from set to unset. x86 does that for both dirty and access. Both problems are solved by modify_prot_start_ptes() which collects a/d bits, both in the generic implementation and the arm64 implementation.

Quoted 17 lines

Thanks,
Ryan

Quoted 14 lines

+                    tlb_flush_pte_range(tlb, addr,
+                        sub_nr_ptes * PAGE_SIZE);
+
+                addr += sub_nr_ptes * PAGE_SIZE;
+                pte += sub_nr_ptes;
+                oldpte = pte_advance_pfn(oldpte, sub_nr_ptes); +                ptent = pte_advance_pfn(ptent, sub_nr_ptes); +                nr_ptes -= sub_nr_ptes;
+                pages += sub_nr_ptes;
+                pgidx += sub_nr_ptes;
+            }
          } else if (is_swap_pte(oldpte)) { swp_entry_t entry = pte_to_swp_entry(oldpte); pte_t newpte;

author
#11 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Lorenzo Stoakes
Quoted 27 lines · Lorenzo Stoakes
Quoted 12 lines · Dev Jain
Quoted 11 lines · Lorenzo Stoakes
Quoted 4 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

Hmm, is this a completely new concept for this series?

Please try not to introduce brand new things to a series midway through.

This seems to be adding a whole ton of questionable logic for an edge case.

Quoted 7 lines
Quoted 1 line

Can we maybe just drop this for this series please?

I refactored this into a new function on David's suggestion:

https://lore.kernel.org/all/[email protected]/

Maybe you are saying, having a refactoring patch first and then the "skip a PTE batch" second, I'll be happy to do that, that would be cleaner.

OK apologies then, my mistake.

So essentially you were doing this explicitly for the prot numa case and it just wasn't clear in subject line before, ok.

Yes please separate the two out!

Quoted 8 lines
Quoted 5 lines

This series was (ofcourse, IMHO) pretty stable at v3, and there were comments coming on David's series, so I guessed that he will have to post a v2 anyways after mine gets merged. My guess could have been wrong. For the khugepaged batching series, I have sent the migration race patch separately exactly because of his series, so that in that case the rebasing burden is mine.

This stuff can be difficult to align on, but I'd suggest that when there's another series in the interim that is fundamentally changing a function signature that will make your code not compile it's probably best to hold off.

Good point about changing the function signature. Anyways my bad, should have been more careful, apologies.

Quoted 15 lines

And that's why I'm suggesting this here.

Quoted 12 lines
Quoted 2 lines

I know you like to rush out a dozen series at once, but once again I'm asking maybe please hold off?

Lorenzo : ) Except for the mremap series which you pointed out, I make it a point to never repost in the same week, unless it is an obvious single patch, and even in that case I give 2-3 days for the reviews to settle. I posted
v3 of this series more than a month ago, so it makes total sense to post this. Also I have seen many people spamming the list with the next versions on literally the same day, not that I am using this as a precedent. The mistake I made here is that on Saturday I saw David's series but then forgot that I am using the same infrastructure he is changing and went ahead posting this. I suddenly remembered this during the khugepaged series and dropped the first two patches for that.

Quoted 6 lines

I'm not saying you shot this out shortly after a previous version, I'm saying you have a whole bunch of series at once (I know as I'm cc'd on them all I think :), and I'm politely asking you to maybe not send another that causes a known conflict.

Whether or not you do so is up to you, it was a request.

Yup my bad for forgetting about the conflict, apologies!

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

I seem to remember David asked you for the same thing because of this, but maybe I'm misremembering.

I don't recollect that happening, maybe I am wrong.

Yeah maybe I'm misremembering, apologies if so!

#35 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 34 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
 mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +0 -0
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
 #include "internal.h"

mm/mprotect.c line 47

Quoted 10 lines
mm/mprotect.c +5 -4
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};

Yeah no, absolutely not, this is horrible, I don't want to see an arbitrary type like this added, to a random file, and I absolutely think this adds confusion and does not in any way help clarify things.

Quoted 47 lines
mm/mprotect.c +18 -7
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
 	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
 	/* Don't touch entries that are not even readable. */
 	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for softdirty tracking? */
 	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for uffd-wp tracking? */
 	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/*
 		 * Writable MAP_PRIVATE mapping: We can only special-case on
 		 * exclusive anonymous pages, because we know that our
 		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
 		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
 	}
 
 	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 6 lines
mm/mprotect.c +2 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	 * FS was already notified and we can simply mark the PTE writable
 	 * just like the write-fault handler would do.
 	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}

Yeah not a fan of this at all.

This is squashing all the logic into one place when we don't really need to.

We can separate out the shared logic and just do something like:

////// Lorenzo's suggestion //////

-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
- pte_t pte)
+static bool maybe_change_pte_writable(struct vm_area_struct *vma,
+ pte_t pte)
{
- struct page *page;
-
if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
return false;

@@ -60,16 +58,14 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
if (userfaultfd_pte_wp(vma, pte))
return false;

- if (!(vma->vm_flags & VM_SHARED)) {
- /*
- * Writable MAP_PRIVATE mapping: We can only special-case on
- * exclusive anonymous pages, because we know that our
- * write-fault handler similarly would map them writable without
- * any additional checks while holding the PT lock.
- */
- page = vm_normal_page(vma, addr, pte);
- return page && PageAnon(page) && PageAnonExclusive(page);
- }
+ return true;
+}
+
+static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
+ pte_t pte)
+{
+ if (!maybe_change_pte_writable(vma, pte))
+ return false;

VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

@@ -83,6 +79,33 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
return pte_dirty(pte);
}

+static bool can_change_private_pte_writable(struct vm_area_struct *vma,
+ unsigned long addr, pte_t pte)
+{
+ struct page *page;
+
+ if (!maybe_change_pte_writable(vma, pte))
+ return false;
+
+ /*
+ * Writable MAP_PRIVATE mapping: We can only special-case on
+ * exclusive anonymous pages, because we know that our
+ * write-fault handler similarly would map them writable without
+ * any additional checks while holding the PT lock.
+ */
+ page = vm_normal_page(vma, addr, pte);
+ return page && PageAnon(page) && PageAnonExclusive(page);
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma,
+ unsigned long addr, pte_t pte)
+{
+ if (vma->vm_flags & VM_SHARED)
+ return can_change_shared_pte_writable(vma, pte);
+
+ return can_change_private_pte_writable(vma, addr, pte);
+}
+

////// end of Lorenzo's suggestion //////

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 105

Quoted 9 lines
mm/mprotect.c +9 -0
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)

Let's generalise it to something like count_folio_fungible_pages()

or maybe count_folio_batchable_pages()?

Yes naming is hard... :P but right now it reads like this is returning a batch or doing something with a batch.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 117

Quoted 12 lines
mm/mprotect.c +12 -0
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {

The C programming language asks why you don't like using for :)

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 121

Quoted 4 lines
mm/mprotect.c +4 -0
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))
+			break;
+		nr++;

This *exclusive stuff makes me want to cry :)

Just set a local variable and hand it back at the end.

Quoted 9 lines
mm/mprotect.c +9 -0
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 140

Quoted 10 lines
mm/mprotect.c +9 -0
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;
 }

See above comments on this stuff.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 4 lines
mm/mprotect.c +1 -1
 static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Obviously David's flag changes will also alter all this.

Just add a boolean re: soft dirty.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 150

Quoted 9 lines
mm/mprotect.c +4 -2
 {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;
 
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))
 		return 1;

Why remove this last check?

mm/mprotect.c static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma · line 216

Quoted 9 lines
mm/mprotect.c +2 -1
 	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
 	}
 
 skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);

See above about flag param. If you change to boolean, please prefix this with e.g. /* set_soft_dirty= */ true or whatever the flag ends up being :)

Quoted 16 lines
mm/mprotect.c +4 -1
 out:
 	*foliop = folio;
 	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
 		if (pte_present(oldpte)) {
 			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
 			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
 			/*
 			 * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 8 lines
mm/mprotect.c +5 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

Don't we only care about S/D if pte_needs_soft_dirty_wp()?

Quoted 39 lines
mm/mprotect.c +23 -9
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Quoted 6 lines
mm/mprotect.c +0 -1
 		} else if (is_swap_pte(oldpte)) {
 			swp_entry_t entry = pte_to_swp_entry(oldpte);
 			pte_t newpte;
--

2.30.2

#20 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain
Quoted 5 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

Looks generally sensible! Some comments below.

Quoted 86 lines
---
 include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
 mm/mprotect.c           |  4 +-
 2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +65 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
 
 /*
  * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
  */
 static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 					   unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
 	__ptep_modify_prot_commit(vma, addr, ptep, pte);
 }
 #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+	pte_t pte, tmp_pte;
+
+	pte = ptep_modify_prot_start(vma, addr, ptep);
+	while (--nr) {
+		ptep++;
+		addr += PAGE_SIZE;
+		tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+		if (pte_dirty(tmp_pte))
+			pte = pte_mkdirty(pte);
+		if (pte_young(tmp_pte))
+			pte = pte_mkyoung(pte);
+	}
+	return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+		pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)
+{
+	int i;
+
+	for (i = 0; i < nr; ++i) {
+		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+		ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1417

Quoted 1 line
include/linux/pgtable.h +1 -0
+		addr += PAGE_SIZE;

Same comment here.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1418

Quoted 1 line
include/linux/pgtable.h +1 -0
+		old_pte = pte_next_pfn(old_pte);

Could be:

		old_pte = pte;

No?

Quoted 33 lines
include/linux/pgtable.h +5 -0
+		pte = pte_next_pfn(pte);
+	}
+}
+#endif
+
 #endif /* CONFIG_MMU */
 
 /*
mm/mprotect.c +2 -3
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
-			oldpte = ptep_modify_prot_start(vma, addr, pte);
+			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
 
 			if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 			    can_change_pte_writable(vma, addr, ptent))
 				ptent = pte_mkwrite(ptent, vma);
 
-			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
 			if (pte_needs_flush(oldpte, ptent))
 				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
 			pages++;
--

2.30.2

author
#21 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Lorenzo Stoakes
Quoted 106 lines · Lorenzo Stoakes
Quoted 5 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

Looks generally sensible! Some comments below.

Quoted 86 lines
---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++-
  mm/mprotect.c           |  4 +-
  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +65 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
 
  /*
   * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
   */
  static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
  					   unsigned long addr,
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,
  	__ptep_modify_prot_commit(vma, addr, ptep, pte);
  }
  #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+		unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+	pte_t pte, tmp_pte;
+
+	pte = ptep_modify_prot_start(vma, addr, ptep);
+	while (--nr) {
+		ptep++;
+		addr += PAGE_SIZE;
+		tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+		if (pte_dirty(tmp_pte))
+			pte = pte_mkdirty(pte);
+		if (pte_young(tmp_pte))
+			pte = pte_mkyoung(pte);
+	}
+	return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
+		pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)
+{
+	int i;
+
+	for (i = 0; i < nr; ++i) {
+		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+		ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

Quoted 2 lines

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1417

Quoted 1 line
include/linux/pgtable.h +1 -0
+		addr += PAGE_SIZE;

Same comment here.

Sure.

Quoted 7 lines

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1418

Quoted 2 lines
include/linux/pgtable.h +1 -0
+		old_pte = pte_next_pfn(old_pte);

Could be:

		old_pte = pte;

No?

We will need to update old_pte also since that is used by powerpc in radix__ptep_modify_prot_commit().

Quoted 34 lines
Quoted 34 lines
include/linux/pgtable.h +5 -0
+		pte = pte_next_pfn(pte);
+	}
+}
+#endif
+
  #endif /* CONFIG_MMU */
 
  /*
mm/mprotect.c +2 -3
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
-			oldpte = ptep_modify_prot_start(vma, addr, pte);
+			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
  			ptent = pte_modify(oldpte, newprot);
 
  			if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  			    can_change_pte_writable(vma, addr, ptent))
  				ptent = pte_mkwrite(ptent, vma);
 
-			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
  			if (pte_needs_flush(oldpte, ptent))
  				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
  			pages++;
--

2.30.2

author
#36 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 257 lines · Lorenzo Stoakes
Quoted 34 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +0 -0
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
  #include "internal.h"

mm/mprotect.c line 47

Quoted 10 lines
mm/mprotect.c +5 -4
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};

Yeah no, absolutely not, this is horrible, I don't want to see an arbitrary type like this added, to a random file, and I absolutely think this adds confusion and does not in any way help clarify things.

Quoted 46 lines
mm/mprotect.c +18 -7
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
  	/* Don't touch entries that are not even readable. */
  	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for softdirty tracking? */
  	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for uffd-wp tracking? */
  	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	if (!(vma->vm_flags & VM_SHARED)) {
  		/*
  		 * Writable MAP_PRIVATE mapping: We can only special-case on
  		 * exclusive anonymous pages, because we know that our
  		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
  		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
  	}
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 6 lines
mm/mprotect.c +2 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	 * FS was already notified and we can simply mark the PTE writable
  	 * just like the write-fault handler would do.
  	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}

Yeah not a fan of this at all.

This is squashing all the logic into one place when we don't really need to.

We can separate out the shared logic and just do something like:

////// Lorenzo's suggestion //////

(quoted diff) +37 -14
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
+static bool maybe_change_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
  {
-	struct page *page;
-
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
  		return false;
@@ -60,16 +58,14 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	if (userfaultfd_pte_wp(vma, pte))
  		return false;
 
-	if (!(vma->vm_flags & VM_SHARED)) {
-		/*
-		 * Writable MAP_PRIVATE mapping: We can only special-case on
-		 * exclusive anonymous pages, because we know that our
-		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
-		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
-	}
+	return true;
+}
+
+static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
+{
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
 
@@ -83,6 +79,33 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
+static bool can_change_private_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	struct page *page;
+
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
+
+	/*
+	 * Writable MAP_PRIVATE mapping: We can only special-case on
+	 * exclusive anonymous pages, because we know that our
+	 * write-fault handler similarly would map them writable without
+	 * any additional checks while holding the PT lock.
+	 */
+	page = vm_normal_page(vma, addr, pte);
+	return page && PageAnon(page) && PageAnonExclusive(page);
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	if (vma->vm_flags & VM_SHARED)
+		return can_change_shared_pte_writable(vma, pte);
+
+	return can_change_private_pte_writable(vma, addr, pte);
+}
+
 
 ////// end of Lorenzo's suggestion //////
 

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 105

Quoted 9 lines
mm/mprotect.c +9 -0
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)

Let's generalise it to something like count_folio_fungible_pages()

or maybe count_folio_batchable_pages()?

Yes naming is hard... :P but right now it reads like this is returning a batch or doing something with a batch.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 117

Quoted 12 lines
mm/mprotect.c +12 -0
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {

The C programming language asks why you don't like using for :)

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 121

Quoted 4 lines
mm/mprotect.c +4 -0
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))
+			break;
+		nr++;

This *exclusive stuff makes me want to cry :)

Just set a local variable and hand it back at the end.

Quoted 9 lines
mm/mprotect.c +9 -0
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 140

Quoted 10 lines
mm/mprotect.c +9 -0
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;
  }

See above comments on this stuff.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 3 lines
mm/mprotect.c +1 -1
  static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Obviously David's flag changes will also alter all this.

Just add a boolean re: soft dirty.

Quoted 6 lines
mm/mprotect.c +3 -1
  {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;
 
Quoted 4 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 150

Quoted 3 lines
mm/mprotect.c +1 -1
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))
  		return 1;

Why remove this last check?

Forgot to drop this check also in the last patch.

Quoted 11 lines

mm/mprotect.c static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma · line 216

Quoted 9 lines
mm/mprotect.c +2 -1
  	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
  	}
 
  skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);

See above about flag param. If you change to boolean, please prefix this with e.g. /* set_soft_dirty= */ true or whatever the flag ends up being :)

On Ryan's suggestion I'll just change this to pass the fpb_flags.

Quoted 20 lines
Quoted 20 lines
mm/mprotect.c +4 -1
  out:
  	*foliop = folio;
  	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
  		if (pte_present(oldpte)) {
  			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
  			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
  			/*
  			 * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
Quoted 6 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 5 lines
mm/mprotect.c +5 -0
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

Don't we only care about S/D if pte_needs_soft_dirty_wp()?

That's what the function will do, it will switch off FPB_IGNORE_SOFT_DIRTY from the mask, meaning that folio_pte_batch will not ignore S/D. Yeah that switch_off_flags thing is really horrible : )

Quoted 56 lines
Quoted 40 lines
mm/mprotect.c +23 -9
  			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
  			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * example, if a PTE is already dirty and no other
  			 * COW or special handling is required.
  			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

Quoted 8 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

If time permits could you take a look at the corresponding patch in v3? That was my original implementation, does that look any cleaner?

Quoted 8 lines
Quoted 8 lines
mm/mprotect.c +0 -1
  		} else if (is_swap_pte(oldpte)) {
  			swp_entry_t entry = pte_to_swp_entry(oldpte);
  			pte_t newpte;
--

2.30.2

author
#33 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 193 lines · Ryan Roberts
Quoted 110 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +38 -12
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
  #include "internal.h"
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};
 
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
  	/* Don't touch entries that are not even readable. */
  	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for softdirty tracking? */
  	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for uffd-wp tracking? */
  	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	if (!(vma->vm_flags & VM_SHARED)) {
  		/*
  		 * Writable MAP_PRIVATE mapping: We can only special-case on
  		 * exclusive anonymous pages, because we know that our
  		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
  		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
  	}
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	 * FS was already notified and we can simply mark the PTE writable
  	 * just like the write-fault handler would do.
  	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)
+{
+	struct page *page;
+	int nr = 1;
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 119

Quoted 10 lines
mm/mprotect.c +10 -0
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Quoted 20 lines
mm/mprotect.c +20 -0
+			break;
+		nr++;
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +4 -2
  }
 
  static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
  {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Can't we just pass in the flags we want?

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 149

Quoted 3 lines
mm/mprotect.c +1 -1
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Quoted 29 lines
mm/mprotect.c +6 -2
  		return 1;
 
  	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
  	}
 
  skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);
  out:
  	*foliop = folio;
  	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
  		if (pte_present(oldpte)) {
  			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
  			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
  			/*
  			 * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
Quoted 10 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 5 lines
mm/mprotect.c +5 -0
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

From the other thread, my memory is jogged that this function ignores write
permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

Can this be fixed by introducing FPB_HONOR_WRITE?

Quoted 69 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 2 lines
mm/mprotect.c +0 -0
  			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 9 lines
mm/mprotect.c +0 -3
  			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * example, if a PTE is already dirty and no other
  			 * COW or special handling is required.
  			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 300

Quoted 10 lines
mm/mprotect.c +4 -6
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Why not just:

			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
				    !pte_write(ptent) &&
				    maybe_change_pte_writable(...);

?

Quoted 8 lines
mm/mprotect.c +8 -0
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 318

Quoted 10 lines
mm/mprotect.c +10 -0
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Thanks,
Ryan

Quoted 14 lines
mm/mprotect.c +11 -0
+					tlb_flush_pte_range(tlb, addr,
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}
  		} else if (is_swap_pte(oldpte)) {
  			swp_entry_t entry = pte_to_swp_entry(oldpte);
  			pte_t newpte;
#22 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Dev Jain
Quoted 119 lines · Dev Jain
Quoted 110 lines · Lorenzo Stoakes
Quoted 5 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

Looks generally sensible! Some comments below.

Quoted 94 lines

---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++- mm/mprotect.c           |  4 +-

  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +2 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct
 vm_area_struct *vma,
 
   /*
    * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
    */
   static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,

                         unsigned long addr,

include/linux/pgtable.h +3 -0
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct
 vm_area_struct *vma,
       __ptep_modify_prot_commit(vma, addr, ptep, pte);
   }
   #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write

transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */

include/linux/pgtable.h +41 -0
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+        unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+    pte_t pte, tmp_pte;
+
+    pte = ptep_modify_prot_start(vma, addr, ptep);
+    while (--nr) {
+        ptep++;
+        addr += PAGE_SIZE;
+        tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+        if (pte_dirty(tmp_pte))
+            pte = pte_mkdirty(pte);
+        if (pte_young(tmp_pte))
+            pte = pte_mkyoung(pte);
+    }
+    return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 6 lines
include/linux/pgtable.h +6 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

Quoted 2 lines
Quoted 1 line

+        addr += PAGE_SIZE;

Same comment here.

Sure.

Quoted 2 lines
Quoted 2 lines

+        old_pte = pte_next_pfn(old_pte);

Quoted 8 lines
Quoted 5 lines

Could be:

        old_pte = pte;

No?

We will need to update old_pte also since that is used by powerpc in radix__ptep_modify_prot_commit().

I think perhaps Lorenzo has the model in his head where old_pte is the previous pte in the batch. That's not the case. old_pte is the value of the pte in the current position of the batch before any changes were made. pte is the new value for the pte. So we need to expliticly advance the PFN in both old_pte and pte each iteration round the loop.

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

+        pte = pte_next_pfn(pte);
+    }

include/linux/pgtable.h +3 -0
+}
+#endif
+

  #endif /* CONFIG_MMU */

  /*

mm/mprotect.c +2 -2
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
 
-            oldpte = ptep_modify_prot_start(vma, addr, pte);
+            oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
               ptent = pte_modify(oldpte, newprot);
 
               if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   can_change_pte_writable(vma, addr, ptent))
                   ptent = pte_mkwrite(ptent, vma);
 
-            ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
               if (pte_needs_flush(oldpte, ptent))
                   tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
               pages++;
Signature · 1 line
2.30.2
#34 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 194 lines · Dev Jain
Quoted 183 lines · Ryan Roberts
Quoted 103 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------

  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +5 -3
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
     #include "internal.h"
   -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
-{
-    struct page *page;
+enum tristate {
+    TRI_FALSE = 0,
+    TRI_TRUE = 1,
+    TRI_MAYBE = -1,
+};

  +/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */

mm/mprotect.c +4 -0
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+                     unsigned long addr, pte_t pte,
+                     struct folio *folio)
+{

      if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-        return false;
+        return TRI_FALSE;
        /* Don't touch entries that are not even readable. */ if (pte_protnone(pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for softdirty tracking? */ if (pte_needs_soft_dirty_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        /* Do we need write faults for uffd-wp tracking? */
      if (userfaultfd_pte_wp(vma, pte))
-        return false;
+        return TRI_FALSE;
        if (!(vma->vm_flags & VM_SHARED)) {
          /*
           * Writable MAP_PRIVATE mapping: We can only special-case on
           * exclusive anonymous pages, because we know that our
           * write-fault handler similarly would map them writable without -         * any additional checks while holding the PT lock.
+         * any additional checks while holding the PT lock. So if the +         * folio is not anonymous, we know we cannot change pte to +         * writable. If it is anonymous then the caller must further +         * check that the page is AnonExclusive().
           */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page); +        return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE; }
        VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c +15 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
        * FS was already notified and we can simply mark the PTE writable
        * just like the write-fault handler would do.
        */
-    return pte_dirty(pte);
+    return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+                bool *exclusive)
+{
+    struct page *page;
+    int nr = 1;
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 120

Quoted 10 lines
mm/mprotect.c +10 -0
+    if (!folio) {
+        *exclusive = false;
+        return nr;
+    }
+
+    page = folio_page(folio, pgidx++);
+    *exclusive = PageAnonExclusive(page);
+    while (nr < max_nr) {
+        page = folio_page(folio, pgidx++);
+        if ((*exclusive) != PageAnonExclusive(page))

nit: brackets not required around *exclusive.

Quoted 19 lines
mm/mprotect.c +19 -0
+            break;
+        nr++;
+    }
+
+    return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+                 pte_t pte)
+{
+    struct page *page;
+    int ret;
+
+    ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+    if (ret == TRI_MAYBE) {
+        page = vm_normal_page(vma, addr, pte);
+        ret = page && PageAnon(page) && PageAnonExclusive(page);
+    }
+

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 147

Quoted 10 lines
mm/mprotect.c +5 -2
+    return ret;
   }
     static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-        pte_t *ptep, pte_t pte, int max_nr_ptes)
+        pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
   {
-    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+    fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    flags &= ~switch_off_flags;

This is mega confusing when reading the caller. Because the caller passes FPB_IGNORE_SOFT_DIRTY and that actually means DON'T ignore soft dirty.

Can't we just pass in the flags we want?

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 149

Quoted 2 lines
mm/mprotect.c +1 -0
   -    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+    if (!folio || !folio_test_large(folio))

What's the rational for dropping the max_nr_ptes == 1 condition? If you don't need it, why did you add it in the earler patch?

Quoted 28 lines
mm/mprotect.c +0 -0
           return 1;
         return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop,

struct vm_area_struct *vma
      }
    skip_batch:
-    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes); +    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, +                       max_nr_ptes, 0);
  out:
      *foliop = folio;
      return nr_ptes;

mm/mprotect.c +5 -1
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
           if (pte_present(oldpte)) {
               int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
               struct folio *folio = NULL;
-            pte_t ptent;
+            int sub_nr_ptes, pgidx = 0;
+            pte_t ptent, newpte;
+            bool sub_set_write;
+            int set_write;
                 /*
                * Avoid trapping faults against the zero or KSM
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
   +            if (!folio)
+                folio = vm_normal_folio(vma, addr, oldpte);
Quoted 8 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 3 lines
mm/mprotect.c +3 -0
+
+            nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+                               max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

 From the other thread, my memory is jogged that this function ignores write permission bit. So I think that's opening up a bug when applied here? If the first pte is writable but the rest are not (COW), doesn't this now make them all writable? I don't *think* that's a problem for the prot_numa use, but I could be wrong.

Quoted 1 line

Can this be fixed by introducing FPB_HONOR_WRITE?

Yes I think so. Suddenly David's change looks very appealing because it's going to say there are a set of bits that are ignored by default (young, dirty, soft-dirty, write) and use FPB_HONOR_ flags to stop ignoring those bits. So we can follow that pattern for write, I guess? And this avoids mixing FPB_IGNORE_ an FPB_HONOR_ flags.

Quoted 69 lines
Quoted 69 lines

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 276

Quoted 3 lines
mm/mprotect.c +0 -0
               oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);

Even if I'm wrong about ignoring write bit being a bug, I don't think the docs for this function permit write bit to be different across the batch?

Quoted 10 lines
mm/mprotect.c +0 -0
               ptent = pte_modify(oldpte, newprot);
   @@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,

               * example, if a PTE is already dirty and no other
               * COW or special handling is required. */
-            if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && -                !pte_write(ptent) &&
-                can_change_pte_writable(vma, addr, ptent)) -                ptent = pte_mkwrite(ptent, vma);
-

Quoted 8 lines

-            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes); -            if (pte_needs_flush(oldpte, ptent))
-                tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-            pages++;
+            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && +                    !pte_write(ptent);
+            if (set_write)
+                set_write = maybe_change_pte_writable(vma, addr, ptent, folio);

Why not just:
            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pte_write(ptent) && maybe_change_pte_writable(...);

?

Quoted 8 lines
mm/mprotect.c +8 -0
+
+            while (nr_ptes) {
+                if (set_write == TRI_MAYBE) {
+                    sub_nr_ptes = anon_exclusive_batch(folio,
+                        pgidx, nr_ptes, &sub_set_write);
+                } else {
+                    sub_nr_ptes = nr_ptes;
+                    sub_set_write = (set_write == TRI_TRUE);

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 318

Quoted 10 lines
mm/mprotect.c +10 -0
+                }
+
+                if (sub_set_write)
+                    newpte = pte_mkwrite(ptent, vma);
+                else
+                    newpte = ptent;
+
+                modify_prot_commit_ptes(vma, addr, pte, oldpte,
+                            newpte, sub_nr_ptes);
+                if (pte_needs_flush(oldpte, newpte))

What did we conclude with pte_needs_flush()? I thought there was an arch where it looked dodgy calling this for just the pte at the head of the batch?

Thanks,
Ryan

Quoted 14 lines

+                    tlb_flush_pte_range(tlb, addr,
+                        sub_nr_ptes * PAGE_SIZE);
+
+                addr += sub_nr_ptes * PAGE_SIZE;
+                pte += sub_nr_ptes;
+                oldpte = pte_advance_pfn(oldpte, sub_nr_ptes); +                ptent = pte_advance_pfn(ptent, sub_nr_ptes); +                nr_ptes -= sub_nr_ptes;
+                pages += sub_nr_ptes;
+                pgidx += sub_nr_ptes;
+            }
          } else if (is_swap_pte(oldpte)) { swp_entry_t entry = pte_to_swp_entry(oldpte); pte_t newpte;

#37 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 181 lines · Lorenzo Stoakes
Quoted 34 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
 mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +0 -0
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
 #include "internal.h"

mm/mprotect.c line 47

Quoted 10 lines
mm/mprotect.c +5 -4
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};

Yeah no, absolutely not, this is horrible, I don't want to see an arbitrary type like this added, to a random file, and I absolutely think this adds confusion and does not in any way help clarify things.

Quoted 47 lines
mm/mprotect.c +18 -7
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
 	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
 	/* Don't touch entries that are not even readable. */
 	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for softdirty tracking? */
 	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	/* Do we need write faults for uffd-wp tracking? */
 	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
 	if (!(vma->vm_flags & VM_SHARED)) {
 		/*
 		 * Writable MAP_PRIVATE mapping: We can only special-case on
 		 * exclusive anonymous pages, because we know that our
 		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
 		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
 	}
 
 	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 6 lines
mm/mprotect.c +2 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	 * FS was already notified and we can simply mark the PTE writable
 	 * just like the write-fault handler would do.
 	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}

Yeah not a fan of this at all.

This is squashing all the logic into one place when we don't really need to.

We can separate out the shared logic and just do something like:

////// Lorenzo's suggestion //////

(quoted diff) +37 -14
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
+static bool maybe_change_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
 {
-	struct page *page;
-
 	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
 		return false;
@@ -60,16 +58,14 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	if (userfaultfd_pte_wp(vma, pte))
 		return false;
 
-	if (!(vma->vm_flags & VM_SHARED)) {
-		/*
-		 * Writable MAP_PRIVATE mapping: We can only special-case on
-		 * exclusive anonymous pages, because we know that our
-		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
-		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
-	}
+	return true;
+}
+
+static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
+{
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
 
 	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
 
@@ -83,6 +79,33 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static bool can_change_private_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	struct page *page;
+
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
+
+	/*
+	 * Writable MAP_PRIVATE mapping: We can only special-case on
+	 * exclusive anonymous pages, because we know that our
+	 * write-fault handler similarly would map them writable without
+	 * any additional checks while holding the PT lock.
+	 */
+	page = vm_normal_page(vma, addr, pte);
+	return page && PageAnon(page) && PageAnonExclusive(page);
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	if (vma->vm_flags & VM_SHARED)
+		return can_change_shared_pte_writable(vma, pte);
+
+	return can_change_private_pte_writable(vma, addr, pte);
+}
+
 
 ////// end of Lorenzo's suggestion //////
 
Quoted 2 lines

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

This sugestion for this part of the problem looks much cleaner!

Sorry; this whole struct tristate thing was my idea. I never really liked it but I was more focussed on trying to illustrate the big picture flow that I thought would work well with a batch and sub-batches (which it seems below that you hate... but let's talk about that down there).

Quoted 73 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 105

Quoted 10 lines
mm/mprotect.c +9 -0
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)

Let's generalise it to something like count_folio_fungible_pages()

or maybe count_folio_batchable_pages()?

Yes naming is hard... :P but right now it reads like this is returning a batch or doing something with a batch.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 117

Quoted 12 lines
mm/mprotect.c +12 -0
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {

The C programming language asks why you don't like using for :)

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 121

Quoted 4 lines
mm/mprotect.c +4 -0
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))
+			break;
+		nr++;

This *exclusive stuff makes me want to cry :)

Just set a local variable and hand it back at the end.

Quoted 9 lines
mm/mprotect.c +9 -0
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 140

Quoted 10 lines
mm/mprotect.c +9 -0
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;
 }

See above comments on this stuff.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 4 lines
mm/mprotect.c +1 -1
 static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Quoted 3 lines

Obviously David's flag changes will also alter all this.

Just add a boolean re: soft dirty.

Dev had a boolean for this in the last round. I've seen various functions expand over time with increasing numbers of bool flags. So I asked to convert to a flags parameter and just pass in the flags we need. Then it's a bit more future proof and self documenting. (For the record I dislike the "switch_off_flags" approach taken here).

Quoted 105 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 150

Quoted 10 lines
mm/mprotect.c +4 -2
 {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;
 
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))
 		return 1;

Why remove this last check?

mm/mprotect.c static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma · line 216

Quoted 9 lines
mm/mprotect.c +2 -1
 	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
 	}
 
 skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);

See above about flag param. If you change to boolean, please prefix this with e.g. /* set_soft_dirty= */ true or whatever the flag ends up being :)

Quoted 16 lines
mm/mprotect.c +4 -1
 out:
 	*foliop = folio;
 	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
 		if (pte_present(oldpte)) {
 			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
 			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
 			/*
 			 * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 8 lines
mm/mprotect.c +5 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
 					continue;
 			}
 
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

Don't we only care about S/D if pte_needs_soft_dirty_wp()?

Quoted 39 lines
mm/mprotect.c +23 -9
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

Quoted 3 lines

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

This loop approach, as written, felt more straightforward for the reader to understand (i.e. the least-worst option). Is the context approach what you are suggesting or do you have something else in mind?

Quoted 9 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Sorry I don't understand this statement - could you clarify? Especially the bit about "always ... processing folio_nr_pages(folio)"; I don't think we do. In various corner cases the size of the folio has no relationship to the way the PTEs are mapped.

Thanks,
Ryan

Quoted 8 lines
Quoted 8 lines
mm/mprotect.c +0 -1
 		} else if (is_swap_pte(oldpte)) {
 			swp_entry_t entry = pte_to_swp_entry(oldpte);
 			pte_t newpte;
--

2.30.2

#23 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Ryan Roberts
Quoted 126 lines · Ryan Roberts
Quoted 125 lines · Dev Jain
Quoted 110 lines · Lorenzo Stoakes
Quoted 5 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

Looks generally sensible! Some comments below.

Quoted 94 lines

---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++- mm/mprotect.c           |  4 +-

  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +2 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct
 vm_area_struct *vma,
 
   /*
    * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
    */
   static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,

                         unsigned long addr,

include/linux/pgtable.h +3 -0
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct
 vm_area_struct *vma,
       __ptep_modify_prot_commit(vma, addr, ptep, pte);
   }
   #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write

transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */

include/linux/pgtable.h +41 -0
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+        unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+    pte_t pte, tmp_pte;
+
+    pte = ptep_modify_prot_start(vma, addr, ptep);
+    while (--nr) {
+        ptep++;
+        addr += PAGE_SIZE;
+        tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+        if (pte_dirty(tmp_pte))
+            pte = pte_mkdirty(pte);
+        if (pte_young(tmp_pte))
+            pte = pte_mkyoung(pte);
+    }
+    return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 6 lines
include/linux/pgtable.h +6 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

Quoted 2 lines
Quoted 1 line

+        addr += PAGE_SIZE;

Same comment here.

Sure.

Quoted 7 lines
Quoted 2 lines

+        old_pte = pte_next_pfn(old_pte);

Could be:

        old_pte = pte;

No?

Quoted 8 lines
Quoted 2 lines

We will need to update old_pte also since that is used by powerpc in radix__ptep_modify_prot_commit().

I think perhaps Lorenzo has the model in his head where old_pte is the previous pte in the batch. That's not the case. old_pte is the value of the pte in the current position of the batch before any changes were made. pte is the new value for the pte. So we need to expliticly advance the PFN in both old_pte and pte each iteration round the loop.

Yeah, you're right, apologies, I'd misinterpreted.

I really, really, really hate how all this is implemented. This is obviously an mprotect() and legacy thing but it's almost designed for confusion. Not the fault of this series, and todo++ on improving mprotect as a whole (been on my list for a while...)

So we're ultimately updating ptep (this thing that we update, of course, is buried in the middle of the function invocation) in:

	ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

We are setting *ptep++ = pte essentially (roughly speaking) right?

And the arch needs to know about any bits that have changed I guess hence providing old_pte as well right?

OK so yeah, I get it now, we're not actually advancing through ptes here, we're just advancing the PFN and applying the same 'template'.

How about something like:

static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

{
	int i;

	for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

		/* Advance PFN only, set same flags. */
		old_pte = pte_next_pfn(old_pte);
		pte = pte_next_pfn(pte);
	}
}

Neatens it up a bit and makes it clear that we're effectively propagating the flags here.

Quoted 37 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

+        pte = pte_next_pfn(pte);
+    }

include/linux/pgtable.h +3 -0
+}
+#endif
+

  #endif /* CONFIG_MMU */

  /*

mm/mprotect.c +2 -3
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
 
-            oldpte = ptep_modify_prot_start(vma, addr, pte);
+            oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
               ptent = pte_modify(oldpte, newprot);
 
               if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   can_change_pte_writable(vma, addr, ptent))
                   ptent = pte_mkwrite(ptent, vma);
 
-            ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
               if (pte_needs_flush(oldpte, ptent))
                   tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
               pages++;
--

2.30.2

author
#38 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 362 lines · Ryan Roberts
Quoted 178 lines · Lorenzo Stoakes
Quoted 34 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +0 -0
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
  #include "internal.h"

mm/mprotect.c line 47

Quoted 10 lines
mm/mprotect.c +5 -4
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
-{
-	struct page *page;
+enum tristate {
+	TRI_FALSE = 0,
+	TRI_TRUE = 1,
+	TRI_MAYBE = -1,
+};

Yeah no, absolutely not, this is horrible, I don't want to see an arbitrary type like this added, to a random file, and I absolutely think this adds confusion and does not in any way help clarify things.

Quoted 46 lines
mm/mprotect.c +18 -7
+/*
+ * Returns enum tristate indicating whether the pte can be changed to writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t pte,
+				     struct folio *folio)
+{
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
-		return false;
+		return TRI_FALSE;
 
  	/* Don't touch entries that are not even readable. */
  	if (pte_protnone(pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for softdirty tracking? */
  	if (pte_needs_soft_dirty_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	/* Do we need write faults for uffd-wp tracking? */
  	if (userfaultfd_pte_wp(vma, pte))
-		return false;
+		return TRI_FALSE;
 
  	if (!(vma->vm_flags & VM_SHARED)) {
  		/*
  		 * Writable MAP_PRIVATE mapping: We can only special-case on
  		 * exclusive anonymous pages, because we know that our
  		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
+		 * any additional checks while holding the PT lock. So if the
+		 * folio is not anonymous, we know we cannot change pte to
+		 * writable. If it is anonymous then the caller must further
+		 * check that the page is AnonExclusive().
  		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
+		return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE;
  	}
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 96

Quoted 6 lines
mm/mprotect.c +2 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	 * FS was already notified and we can simply mark the PTE writable
  	 * just like the write-fault handler would do.
  	 */
-	return pte_dirty(pte);
+	return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}

Yeah not a fan of this at all.

This is squashing all the logic into one place when we don't really need to.

We can separate out the shared logic and just do something like:

////// Lorenzo's suggestion //////

(quoted diff) +37 -14
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-			     pte_t pte)
+static bool maybe_change_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
  {
-	struct page *page;
-
  	if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
  		return false;
@@ -60,16 +58,14 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	if (userfaultfd_pte_wp(vma, pte))
  		return false;
 
-	if (!(vma->vm_flags & VM_SHARED)) {
-		/*
-		 * Writable MAP_PRIVATE mapping: We can only special-case on
-		 * exclusive anonymous pages, because we know that our
-		 * write-fault handler similarly would map them writable without
-		 * any additional checks while holding the PT lock.
-		 */
-		page = vm_normal_page(vma, addr, pte);
-		return page && PageAnon(page) && PageAnonExclusive(page);
-	}
+	return true;
+}
+
+static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
+		pte_t pte)
+{
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
 
  	VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
 
@@ -83,6 +79,33 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
+static bool can_change_private_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	struct page *page;
+
+	if (!maybe_change_pte_writable(vma, pte))
+		return false;
+
+	/*
+	 * Writable MAP_PRIVATE mapping: We can only special-case on
+	 * exclusive anonymous pages, because we know that our
+	 * write-fault handler similarly would map them writable without
+	 * any additional checks while holding the PT lock.
+	 */
+	page = vm_normal_page(vma, addr, pte);
+	return page && PageAnon(page) && PageAnonExclusive(page);
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma,
+		unsigned long addr, pte_t pte)
+{
+	if (vma->vm_flags & VM_SHARED)
+		return can_change_shared_pte_writable(vma, pte);
+
+	return can_change_private_pte_writable(vma, addr, pte);
+}
+
 
 ////// end of Lorenzo's suggestion //////
 
Quoted 2 lines

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

This sugestion for this part of the problem looks much cleaner!

Sorry; this whole struct tristate thing was my idea. I never really liked it but I was more focussed on trying to illustrate the big picture flow that I thought would work well with a batch and sub-batches (which it seems below that you hate... but let's talk about that down there).

Quoted 66 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 105

Quoted 9 lines
mm/mprotect.c +9 -0
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+				bool *exclusive)

Let's generalise it to something like count_folio_fungible_pages()

or maybe count_folio_batchable_pages()?

Yes naming is hard... :P but right now it reads like this is returning a batch or doing something with a batch.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 117

Quoted 12 lines
mm/mprotect.c +12 -0
+{
+	struct page *page;
+	int nr = 1;
+
+	if (!folio) {
+		*exclusive = false;
+		return nr;
+	}
+
+	page = folio_page(folio, pgidx++);
+	*exclusive = PageAnonExclusive(page);
+	while (nr < max_nr) {

The C programming language asks why you don't like using for :)

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 121

Quoted 4 lines
mm/mprotect.c +4 -0
+		page = folio_page(folio, pgidx++);
+		if ((*exclusive) != PageAnonExclusive(page))
+			break;
+		nr++;

This *exclusive stuff makes me want to cry :)

Just set a local variable and hand it back at the end.

Quoted 9 lines
mm/mprotect.c +9 -0
+	}
+
+	return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte)
+{
+	struct page *page;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 140

Quoted 10 lines
mm/mprotect.c +9 -0
+	int ret;
+
+	ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+	if (ret == TRI_MAYBE) {
+		page = vm_normal_page(vma, addr, pte);
+		ret = page && PageAnon(page) && PageAnonExclusive(page);
+	}
+
+	return ret;
  }

See above comments on this stuff.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 3 lines
mm/mprotect.c +1 -1
  static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Quoted 3 lines

Obviously David's flag changes will also alter all this.

Just add a boolean re: soft dirty.

Dev had a boolean for this in the last round. I've seen various functions expand over time with increasing numbers of bool flags. So I asked to convert to a flags parameter and just pass in the flags we need. Then it's a bit more future proof and self documenting. (For the record I dislike the "switch_off_flags" approach taken here).

Quoted 99 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 150

Quoted 9 lines
mm/mprotect.c +4 -2
  {
-	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+	fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	flags &= ~switch_off_flags;
 
-	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+	if (!folio || !folio_test_large(folio))
  		return 1;

Why remove this last check?

mm/mprotect.c static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma · line 216

Quoted 8 lines
mm/mprotect.c +2 -1
  	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma
  	}
 
  skip_batch:
-	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes);
+	nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+					   max_nr_ptes, 0);

See above about flag param. If you change to boolean, please prefix this with e.g. /* set_soft_dirty= */ true or whatever the flag ends up being :)

Quoted 16 lines
mm/mprotect.c +4 -1
  out:
  	*foliop = folio;
  	return nr_ptes;
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
  		if (pte_present(oldpte)) {
  			int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
  			struct folio *folio = NULL;
-			pte_t ptent;
+			int sub_nr_ptes, pgidx = 0;
+			pte_t ptent, newpte;
+			bool sub_set_write;
+			int set_write;
 
  			/*
  			 * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 8 lines
mm/mprotect.c +5 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
  					continue;
  			}
 
+			if (!folio)
+				folio = vm_normal_folio(vma, addr, oldpte);
+
+			nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+							   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

Don't we only care about S/D if pte_needs_soft_dirty_wp()?

Quoted 39 lines
mm/mprotect.c +23 -9
  			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
  			ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
  			 * example, if a PTE is already dirty and no other
  			 * COW or special handling is required.
  			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

Quoted 8 lines
Quoted 3 lines

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

That patch was in our private exchange, not on the list.

Quoted 36 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

This loop approach, as written, felt more straightforward for the reader to understand (i.e. the least-worst option). Is the context approach what you are suggesting or do you have something else in mind?

Quoted 8 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Sorry I don't understand this statement - could you clarify? Especially the bit about "always ... processing folio_nr_pages(folio)"; I don't think we do. In various corner cases the size of the folio has no relationship to the way the PTEs are mapped.

Thanks,
Ryan

Quoted 7 lines
Quoted 7 lines
mm/mprotect.c +0 -1
  		} else if (is_swap_pte(oldpte)) {
  			swp_entry_t entry = pte_to_swp_entry(oldpte);
  			pte_t newpte;
--

2.30.2

#40 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 7 lines · Ryan Roberts
Quoted 5 lines

////// end of Lorenzo's suggestion //////

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

This sugestion for this part of the problem looks much cleaner!

Thanks :)

Quoted 5 lines

Sorry; this whole struct tristate thing was my idea. I never really liked it but I was more focussed on trying to illustrate the big picture flow that I thought would work well with a batch and sub-batches (which it seems below that you hate... but let's talk about that down there).

Yeah, this is fiddly stuff so I get it as a sort of psuedocode, but as code obviously I've made my feelings known haha.

It seems that we can apply the fundamental underlying idea without needing to do it this way at any rate so we should be good.

Quoted 11 lines
Quoted 11 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 4 lines

static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

mm/mprotect.c +1 -1
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Obviously David's flag changes will also alter all this.

Quoted 7 lines
Quoted 1 line

Just add a boolean re: soft dirty.

Dev had a boolean for this in the last round. I've seen various functions expand over time with increasing numbers of bool flags. So I asked to convert to a flags parameter and just pass in the flags we need. Then it's a bit more future proof and self documenting. (For the record I dislike the "switch_off_flags" approach taken here).

Yeah, but we can change this when it needs to be changed. When it comes to internal non-uAPI stuff I don't think we need to be too worried about future-proofing like this at least just yet.

Do not fear the future churn... ;)

I mean I guess David's new flags will make this less egregious anyway.

Quoted 56 lines
Quoted 52 lines
Quoted 39 lines
mm/mprotect.c +23 -9
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

Quoted 3 lines

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

But i"m also not sure we need to maintain _that_ much state?

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

Quoted 5 lines

This loop approach, as written, felt more straightforward for the reader to understand (i.e. the least-worst option). Is the context approach what you are suggesting or do you have something else in mind?

See above.

Quoted 6 lines
Quoted 6 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Quoted 8 lines
Quoted 3 lines

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Sorry I don't understand this statement - could you clarify? Especially the bit about "always ... processing folio_nr_pages(folio)"; I don't think we do. In various corner cases the size of the folio has no relationship to the way the PTEs are mapped.

Right yeah I put this badly. Obviously you can have all sorts of fun with large folios partially mapped and page-table split and etc. etc.

I should have said 'always process nr_ptes'.

The idea is to abstract this sub-batch stuff to another function, fundamentally.

#24 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Lorenzo Stoakes
Quoted 136 lines · Lorenzo Stoakes
Quoted 126 lines · Ryan Roberts
Quoted 125 lines · Dev Jain
Quoted 110 lines · Lorenzo Stoakes
Quoted 5 lines · Dev Jain

Batch ptep_modify_prot_start/commit in preparation for optimizing mprotect. Architecture can override these helpers; in case not, they are implemented as a simple loop over the corresponding single pte helpers.

Looks generally sensible! Some comments below.

Quoted 94 lines

---
  include/linux/pgtable.h | 83 ++++++++++++++++++++++++++++++++++++++++- mm/mprotect.c           |  4 +-

  2 files changed, 84 insertions(+), 3 deletions(-)
include/linux/pgtable.h +2 -1
index cf1515c163e2..662f39e7475a 100644
@@ -1331,7 +1331,8 @@ static inline pte_t ptep_modify_prot_start(struct
 vm_area_struct *vma,
 
   /*
    * Commit an update to a pte, leaving any hardware-controlled bits in
- * the PTE unmodified.
+ * the PTE unmodified. The pte may have been "upgraded" w.r.t a/d bits compared
+ * to the old_pte, as in, it may have a/d bits on which were off in old_pte.
    */
   static inline void ptep_modify_prot_commit(struct vm_area_struct *vma,

                         unsigned long addr,

include/linux/pgtable.h +3 -0
@@ -1340,6 +1341,86 @@ static inline void ptep_modify_prot_commit(struct
 vm_area_struct *vma,
       __ptep_modify_prot_commit(vma, addr, ptep, pte);
   }
   #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
+
+/**
+ * modify_prot_start_ptes - Start a pte protection read-modify-write

transaction
+ * over a batch of ptes, which protects against asynchronous hardware
+ * modifications to the ptes. The intention is not to prevent the hardware from
+ * making pte updates, but to prevent any updates it may make from being lost.
+ * Please see the comment above ptep_modify_prot_start() for full description.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte
+ * in the batch.
+ *
+ * Note that PTE bits in the PTE batch besides the PFN can differ.
+ *
+ * Context: The caller holds the page table lock.  The PTEs map consecutive
+ * pages that belong to the same folio.  The PTEs are all in the same PMD.
+ * Since the batch is determined from folio_pte_batch, the PTEs must differ
+ * only in a/d bits (and the soft dirty bit; see fpb_t flags in
+ * mprotect_folio_pte_batch()).
+ */

include/linux/pgtable.h +41 -0
+#ifndef modify_prot_start_ptes
+static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
+        unsigned long addr, pte_t *ptep, unsigned int nr)
+{
+    pte_t pte, tmp_pte;
+
+    pte = ptep_modify_prot_start(vma, addr, ptep);
+    while (--nr) {
+        ptep++;
+        addr += PAGE_SIZE;
+        tmp_pte = ptep_modify_prot_start(vma, addr, ptep);
+        if (pte_dirty(tmp_pte))
+            pte = pte_mkdirty(pte);
+        if (pte_young(tmp_pte))
+            pte = pte_mkyoung(pte);
+    }
+    return pte;
+}
+#endif
+
+/**
+ * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any
+ * hardware-controlled bits in the PTE unmodified.
+ *
+ * @vma: The virtual memory area the pages are mapped into.
+ * @addr: Address the first page is mapped at.
+ * @ptep: Page table pointer for the first entry.
+ * @old_pte: Old page table entry (for the first entry) which is now cleared.
+ * @pte: New page table entry to be set.
+ * @nr: Number of entries.
+ *
+ * May be overridden by the architecture; otherwise, implemented as a simple
+ * loop over ptep_modify_prot_commit().
+ *
+ * Context: The caller holds the page table lock. The PTEs are all in the same
+ * PMD. On exit, the set ptes in the batch map the same folio. The pte may have
+ * been "upgraded" w.r.t a/d bits compared to the old_pte, as in, it may have
+ * a/d bits on which were off in old_pte.
+ */
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 6 lines
include/linux/pgtable.h +6 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

Quoted 2 lines
Quoted 1 line

+        addr += PAGE_SIZE;

Same comment here.

Sure.

Quoted 7 lines
Quoted 2 lines

+        old_pte = pte_next_pfn(old_pte);

Could be:

        old_pte = pte;

No?

Quoted 8 lines
Quoted 2 lines

We will need to update old_pte also since that is used by powerpc in radix__ptep_modify_prot_commit().

I think perhaps Lorenzo has the model in his head where old_pte is the previous pte in the batch. That's not the case. old_pte is the value of the pte in the current position of the batch before any changes were made. pte is the new value for the pte. So we need to expliticly advance the PFN in both old_pte and pte each iteration round the loop.

Quoted 6 lines

Yeah, you're right, apologies, I'd misinterpreted.

I really, really, really hate how all this is implemented. This is obviously an mprotect() and legacy thing but it's almost designed for confusion. Not the fault of this series, and todo++ on improving mprotect as a whole (been on my list for a while...)

Agreed. I struggled for a long time with some of the pgtable helper abstractions to the arch and all the assumptions they make. But ultimately all Dev is trying to do here is make some incremental improvements, following the established patterns. Hopefully you agree that cleanups on a larger scale should be reserved for a systematic, focussed series.

Quoted 7 lines

So we're ultimately updating ptep (this thing that we update, of course, is buried in the middle of the function invocation) in:

	ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

We are setting *ptep++ = pte essentially (roughly speaking) right?

Yeah, pretty much.

The API was originally created for Xen IIRC. The problem is that the HW can update the A/D bits asynchronously if the PTE is valid (from the HW perspective) so the previous approach was to get_and_clear (atomic), modify, write. But that required 2 Xen hypervisor calls per PTE. This start/commit approach allows Xen to both avoid the get_and_clear() and batch the writes for all PTEs in a lazy mmu batch. So hypervisor calls are reduced from 2 per PTE to 1 per lazy mmu batch. TBH I'm no Xen expert; some of those details may be off, but big picture is correct.

Anyway, arm64 doesn't care about any of that, but it does override ptep_modify_prot_start() / ptep_modify_prot_commit() to implement an erratum workaround. And it can benefit substantially from batching.

Quoted 23 lines

And the arch needs to know about any bits that have changed I guess hence providing old_pte as well right?

OK so yeah, I get it now, we're not actually advancing through ptes here, we're just advancing the PFN and applying the same 'template'.

How about something like:

static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

{
	int i;

	for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

		/* Advance PFN only, set same flags. */
		old_pte = pte_next_pfn(old_pte);
		pte = pte_next_pfn(pte);
	}
}
Quoted 2 lines

Neatens it up a bit and makes it clear that we're effectively propagating the flags here.

Yes, except we don't usually refer to the non-pfn parts of a pte as "flags". We normally call them pgprot or prot. God knows why...

Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

+        pte = pte_next_pfn(pte);
+    }

include/linux/pgtable.h +3 -0
+}
+#endif
+

  #endif /* CONFIG_MMU */

  /*

mm/mprotect.c +2 -3
index af10a7fbe6b8..627b0d67cc4a 100644
@@ -206,7 +206,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
 
-            oldpte = ptep_modify_prot_start(vma, addr, pte);
+            oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
               ptent = pte_modify(oldpte, newprot);
 
               if (uffd_wp)
@@ -232,7 +232,7 @@ static long change_pte_range(struct mmu_gather *tlb,
                   can_change_pte_writable(vma, addr, ptent))
                   ptent = pte_mkwrite(ptent, vma);
 
-            ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
+            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
               if (pte_needs_flush(oldpte, ptent))
                   tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
               pages++;
--

2.30.2

#39 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Dev Jain
Quoted 373 lines · Dev Jain
Quoted 371 lines · Ryan Roberts
Quoted 182 lines · Lorenzo Stoakes
Quoted 34 lines · Dev Jain

Use folio_pte_batch to batch process a large folio. Reuse the folio from prot_numa case if possible.

For all cases other than the PageAnonExclusive case, if the case holds true for one pte in the batch, one can confirm that that case will hold true for other ptes in the batch too; for pte_needs_soft_dirty_wp(), we do not pass FPB_IGNORE_SOFT_DIRTY. modify_prot_start_ptes() collects the dirty and access bits across the batch, therefore batching across pte_dirty(): this is correct since the dirty bit on the PTE really is just an indication that the folio got written to, so even if the PTE is not actually dirty (but one of the PTEs in the batch is), the wp-fault optimization can be made.

The crux now is how to batch around the PageAnonExclusive case; we must check the corresponding condition for every single page. Therefore, from the large folio batch, we process sub batches of ptes mapping pages with the same PageAnonExclusive condition, and process that sub batch, then determine and process the next sub batch, and so on. Note that this does not cause any extra overhead; if suppose the size of the folio batch is 512, then the sub batch processing in total will take 512 iterations, which is the same as what we would have done before.

---
  mm/mprotect.c | 143 +++++++++++++++++++++++++++++++++++++++++---------

  1 file changed, 117 insertions(+), 26 deletions(-)
mm/mprotect.c +0 -0
index 627b0d67cc4a..28c7ce7728ff 100644
@@ -40,35 +40,47 @@
 
   #include "internal.h"

mm/mprotect.c line 47

Quoted 10 lines
mm/mprotect.c +5 -4
 
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
-{
-    struct page *page;
+enum tristate {
+    TRI_FALSE = 0,
+    TRI_TRUE = 1,
+    TRI_MAYBE = -1,
+};

Yeah no, absolutely not, this is horrible, I don't want to see an arbitrary type like this added, to a random file, and I absolutely think this adds confusion and does not in any way help clarify things.

Quoted 47 lines
mm/mprotect.c +2 -0
+/*
+ * Returns enum tristate indicating whether the pte can be changed to

writable.
+ * If TRI_MAYBE is returned, then the folio is anonymous and the user must
+ * additionally check PageAnonExclusive() for every page in the desired range.
+ */

mm/mprotect.c +4 -0
+static int maybe_change_pte_writable(struct vm_area_struct *vma,
+                     unsigned long addr, pte_t pte,
+                     struct folio *folio)
+{

      if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) -        return false;
+        return TRI_FALSE;

      /* Don't touch entries that are not even readable. */ if (pte_protnone(pte))
-        return false;
+        return TRI_FALSE;

      /* Do we need write faults for softdirty tracking? */ if (pte_needs_soft_dirty_wp(vma, pte))
-        return false;
+        return TRI_FALSE;

      /* Do we need write faults for uffd-wp tracking? */ if (userfaultfd_pte_wp(vma, pte))
-        return false;
+        return TRI_FALSE;

      if (!(vma->vm_flags & VM_SHARED)) {
          /*
           * Writable MAP_PRIVATE mapping: We can only special-case on
           * exclusive anonymous pages, because we know that our
           * write-fault handler similarly would map them writable without -         * any additional checks while holding the PT lock.
+         * any additional checks while holding the PT lock. So if the +         * folio is not anonymous, we know we cannot change pte to +         * writable. If it is anonymous then the caller must further +         * check that the page is AnonExclusive().
           */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page); +        return (!folio || folio_test_anon(folio)) ? TRI_MAYBE : TRI_FALSE; }

      VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, · line 97

Quoted 7 lines
mm/mprotect.c +2 -1
@@ -80,15 +92,61 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
        * FS was already notified and we can simply mark the PTE writable
        * just like the write-fault handler would do.
        */
-    return pte_dirty(pte);
+    return pte_dirty(pte) ? TRI_TRUE : TRI_FALSE;
+}

Yeah not a fan of this at all.

This is squashing all the logic into one place when we don't really need to.

We can separate out the shared logic and just do something like:

////// Lorenzo's suggestion //////

mm/mprotect.c +2 -2
-bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
-                 pte_t pte)
+static bool maybe_change_pte_writable(struct vm_area_struct *vma,
+        pte_t pte)

  {
-    struct page *page;
-
      if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) return false;

(quoted diff) +35 -10
@@ -60,16 +58,14 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
       if (userfaultfd_pte_wp(vma, pte))
           return false;
 
-    if (!(vma->vm_flags & VM_SHARED)) {
-        /*
-         * Writable MAP_PRIVATE mapping: We can only special-case on
-         * exclusive anonymous pages, because we know that our
-         * write-fault handler similarly would map them writable without
-         * any additional checks while holding the PT lock.
-         */
-        page = vm_normal_page(vma, addr, pte);
-        return page && PageAnon(page) && PageAnonExclusive(page);
-    }
+    return true;
+}
+
+static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
+        pte_t pte)
+{
+    if (!maybe_change_pte_writable(vma, pte))
+        return false;
 
       VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
@@ -83,6 +79,33 @@ bool can_change_pte_writable(struct vm_area_struct *vma,
 unsigned long addr,
       return pte_dirty(pte);
   }
 
+static bool can_change_private_pte_writable(struct vm_area_struct *vma,
+        unsigned long addr, pte_t pte)
+{
+    struct page *page;
+
+    if (!maybe_change_pte_writable(vma, pte))
+        return false;
+
+    /*
+     * Writable MAP_PRIVATE mapping: We can only special-case on
+     * exclusive anonymous pages, because we know that our
+     * write-fault handler similarly would map them writable without
+     * any additional checks while holding the PT lock.
+     */
+    page = vm_normal_page(vma, addr, pte);
+    return page && PageAnon(page) && PageAnonExclusive(page);
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma,
+        unsigned long addr, pte_t pte)
+{
+    if (vma->vm_flags & VM_SHARED)
+        return can_change_shared_pte_writable(vma, pte);
+
+    return can_change_private_pte_writable(vma, addr, pte);
+}
+
 
 ////// end of Lorenzo's suggestion //////
Quoted 2 lines

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

This sugestion for this part of the problem looks much cleaner!

Sorry; this whole struct tristate thing was my idea. I never really liked it but I was more focussed on trying to illustrate the big picture flow that I thought would work well with a batch and sub-batches (which it seems below that you hate... but let's talk about that down there).

Quoted 62 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 105

Quoted 9 lines
mm/mprotect.c +9 -0
+
+/*
+ * Returns the number of pages within the folio, starting from the page
+ * indicated by pgidx and up to pgidx + max_nr, that have the same value of
+ * PageAnonExclusive(). Must only be called for anonymous folios. Value of
+ * PageAnonExclusive() is returned in *exclusive.
+ */
+static int anon_exclusive_batch(struct folio *folio, int pgidx, int max_nr,
+                bool *exclusive)

Let's generalise it to something like count_folio_fungible_pages()

or maybe count_folio_batchable_pages()?

Yes naming is hard... :P but right now it reads like this is returning a batch or doing something with a batch.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 117

Quoted 12 lines
mm/mprotect.c +12 -0
+{
+    struct page *page;
+    int nr = 1;
+
+    if (!folio) {
+        *exclusive = false;
+        return nr;
+    }
+
+    page = folio_page(folio, pgidx++);
+    *exclusive = PageAnonExclusive(page);
+    while (nr < max_nr) {

The C programming language asks why you don't like using for :)

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 121

Quoted 4 lines
mm/mprotect.c +4 -0
+        page = folio_page(folio, pgidx++);
+        if ((*exclusive) != PageAnonExclusive(page))
+            break;
+        nr++;

This *exclusive stuff makes me want to cry :)

Just set a local variable and hand it back at the end.

Quoted 9 lines
mm/mprotect.c +9 -0
+    }
+
+    return nr;
+}
+
+bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
+                 pte_t pte)
+{
+    struct page *page;

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 140

Quoted 10 lines
mm/mprotect.c +9 -0
+    int ret;
+
+    ret = maybe_change_pte_writable(vma, addr, pte, NULL);
+    if (ret == TRI_MAYBE) {
+        page = vm_normal_page(vma, addr, pte);
+        ret = page && PageAnon(page) && PageAnonExclusive(page);
+    }
+
+    return ret;
   }

See above comments on this stuff.

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 3 lines
mm/mprotect.c +1 -1
   static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
-        pte_t *ptep, pte_t pte, int max_nr_ptes)
+        pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)
Quoted 7 lines

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Obviously David's flag changes will also alter all this.

Just add a boolean re: soft dirty.

Dev had a boolean for this in the last round. I've seen various functions expand over time with increasing numbers of bool flags. So I asked to convert to a flags parameter and just pass in the flags we need. Then it's a bit more future proof and self documenting. (For the record I dislike the "switch_off_flags" approach taken here).

Quoted 104 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 150

Quoted 9 lines
mm/mprotect.c +4 -2
   {
-    const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+    fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+    flags &= ~switch_off_flags;
 
-    if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))
+    if (!folio || !folio_test_large(folio))
           return 1;

Why remove this last check?

Quoted 9 lines
mm/mprotect.c +0 -0
       return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
@@ -154,7 +212,8 @@ static int prot_numa_skip_ptes(struct folio **foliop,

struct vm_area_struct *vma
      }

  skip_batch:
-    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, max_nr_ptes); +    nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte, +                       max_nr_ptes, 0);

See above about flag param. If you change to boolean, please prefix this with e.g. /* set_soft_dirty= */ true or whatever the flag ends up being :)

Quoted 16 lines

  out:
      *foliop = folio;
      return nr_ptes;

mm/mprotect.c +4 -1
@@ -191,7 +250,10 @@ static long change_pte_range(struct mmu_gather *tlb,
           if (pte_present(oldpte)) {
               int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
               struct folio *folio = NULL;
-            pte_t ptent;
+            int sub_nr_ptes, pgidx = 0;
+            pte_t ptent, newpte;
+            bool sub_set_write;
+            int set_write;
 
               /*
                * Avoid trapping faults against the zero or KSM

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 275

Quoted 8 lines
mm/mprotect.c +5 -0
@@ -206,6 +268,11 @@ static long change_pte_range(struct mmu_gather *tlb,
                       continue;
               }
 
+            if (!folio)
+                folio = vm_normal_folio(vma, addr, oldpte);
+
+            nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
+                               max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

Don't we only care about S/D if pte_needs_soft_dirty_wp()?

Quoted 40 lines
mm/mprotect.c +23 -9
               oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
               ptent = pte_modify(oldpte, newprot);
 
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
                * example, if a PTE is already dirty and no other
                * COW or special handling is required.
                */
-            if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-                !pte_write(ptent) &&
-                can_change_pte_writable(vma, addr, ptent))
-                ptent = pte_mkwrite(ptent, vma);
-
-            modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-            if (pte_needs_flush(oldpte, ptent))
-                tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-            pages++;
+            set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+                    !pte_write(ptent);
+            if (set_write)
+                set_write = maybe_change_pte_writable(vma, addr, ptent,
 folio);
+
+            while (nr_ptes) {
+                if (set_write == TRI_MAYBE) {
+                    sub_nr_ptes = anon_exclusive_batch(folio,
+                        pgidx, nr_ptes, &sub_set_write);
+                } else {
+                    sub_nr_ptes = nr_ptes;
+                    sub_set_write = (set_write == TRI_TRUE);
+                }
+
+                if (sub_set_write)
+                    newpte = pte_mkwrite(ptent, vma);
+                else
+                    newpte = ptent;
+
+                modify_prot_commit_ptes(vma, addr, pte, oldpte,
+                            newpte, sub_nr_ptes);
+                if (pte_needs_flush(oldpte, newpte))
+                    tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 330

Quoted 10 lines
mm/mprotect.c +10 -0
+                        sub_nr_ptes * PAGE_SIZE);
+
+                addr += sub_nr_ptes * PAGE_SIZE;
+                pte += sub_nr_ptes;
+                oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+                ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+                nr_ptes -= sub_nr_ptes;
+                pages += sub_nr_ptes;
+                pgidx += sub_nr_ptes;
+            }

I hate hate hate having this loop here, let's abstract this please.

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

Quoted 7 lines
Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

That patch was in our private exchange, not on the list.

Ahh, my bad. Well perhaps that type of approach is what Lorenzo is arguing in favour of then? Perhaps I should just shut up and get out of the way :)

Quoted 37 lines
Quoted 37 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

This loop approach, as written, felt more straightforward for the reader to understand (i.e. the least-worst option). Is the context approach what you are suggesting or do you have something else in mind?

Quoted 8 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Sorry I don't understand this statement - could you clarify? Especially the bit about "always ... processing folio_nr_pages(folio)"; I don't think we do. In various corner cases the size of the folio has no relationship to the way the PTEs are mapped.

Thanks,
Ryan

Quoted 7 lines
Quoted 7 lines
mm/mprotect.c +0 -0
           } else if (is_swap_pte(oldpte)) {
               swp_entry_t entry = pte_to_swp_entry(oldpte);

              pte_t newpte;

Signature · 1 line
2.30.2
#41 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 128 lines · Lorenzo Stoakes
Quoted 7 lines · Ryan Roberts
Quoted 5 lines

////// end of Lorenzo's suggestion //////

You can obviously modify this to change other stuff like whether you feed back the PAE or not in private case for use in your code.

This sugestion for this part of the problem looks much cleaner!

Thanks :)

Quoted 5 lines

Sorry; this whole struct tristate thing was my idea. I never really liked it but I was more focussed on trying to illustrate the big picture flow that I thought would work well with a batch and sub-batches (which it seems below that you hate... but let's talk about that down there).

Yeah, this is fiddly stuff so I get it as a sort of psuedocode, but as code obviously I've made my feelings known haha.

It seems that we can apply the fundamental underlying idea without needing to do it this way at any rate so we should be good.

Quoted 11 lines
Quoted 11 lines

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 143

Quoted 4 lines

static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,

mm/mprotect.c +1 -1
-		pte_t *ptep, pte_t pte, int max_nr_ptes)
+		pte_t *ptep, pte_t pte, int max_nr_ptes, fpb_t switch_off_flags)

This last parameter is pretty horrible. It's a negative mask so now you're passing 'ignore soft dirty' to the function meaning 'don't ignore it'. This is just planting land mines.

Obviously David's flag changes will also alter all this.

Quoted 7 lines
Quoted 1 line

Just add a boolean re: soft dirty.

Dev had a boolean for this in the last round. I've seen various functions expand over time with increasing numbers of bool flags. So I asked to convert to a flags parameter and just pass in the flags we need. Then it's a bit more future proof and self documenting. (For the record I dislike the "switch_off_flags" approach taken here).

Yeah, but we can change this when it needs to be changed. When it comes to internal non-uAPI stuff I don't think we need to be too worried about future-proofing like this at least just yet.

Do not fear the future churn... ;)

I mean I guess David's new flags will make this less egregious anyway.

Quoted 56 lines
Quoted 52 lines
Quoted 39 lines
mm/mprotect.c +23 -9
 			oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
 			ptent = pte_modify(oldpte, newprot);
@@ -227,15 +294,39 @@ static long change_pte_range(struct mmu_gather *tlb,
 			 * example, if a PTE is already dirty and no other
 			 * COW or special handling is required.
 			 */
-			if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
-			    !pte_write(ptent) &&
-			    can_change_pte_writable(vma, addr, ptent))
-				ptent = pte_mkwrite(ptent, vma);
-
-			modify_prot_commit_ptes(vma, addr, pte, oldpte, ptent, nr_ptes);
-			if (pte_needs_flush(oldpte, ptent))
-				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
-			pages++;
+			set_write = (cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
+				    !pte_write(ptent);
+			if (set_write)
+				set_write = maybe_change_pte_writable(vma, addr, ptent, folio);
+
+			while (nr_ptes) {
+				if (set_write == TRI_MAYBE) {
+					sub_nr_ptes = anon_exclusive_batch(folio,
+						pgidx, nr_ptes, &sub_set_write);
+				} else {
+					sub_nr_ptes = nr_ptes;
+					sub_set_write = (set_write == TRI_TRUE);
+				}
+
+				if (sub_set_write)
+					newpte = pte_mkwrite(ptent, vma);
+				else
+					newpte = ptent;
+
+				modify_prot_commit_ptes(vma, addr, pte, oldpte,
+							newpte, sub_nr_ptes);
+				if (pte_needs_flush(oldpte, newpte))
+					tlb_flush_pte_range(tlb, addr,

mm/mprotect.c static long change_pte_range(struct mmu_gather *tlb, · line 329

Quoted 10 lines
mm/mprotect.c +10 -0
+						sub_nr_ptes * PAGE_SIZE);
+
+				addr += sub_nr_ptes * PAGE_SIZE;
+				pte += sub_nr_ptes;
+				oldpte = pte_advance_pfn(oldpte, sub_nr_ptes);
+				ptent = pte_advance_pfn(ptent, sub_nr_ptes);
+				nr_ptes -= sub_nr_ptes;
+				pages += sub_nr_ptes;
+				pgidx += sub_nr_ptes;
+			}

I hate hate hate having this loop here, let's abstract this please.

Quoted 3 lines

I mean I think we can just use mprotect_folio_pte_batch() no? It's not abstracting much here, and we can just do all this handling there. Maybe have to pass in a bunch more params, but it saves us having to do all this.

Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

Quoted 6 lines

But i"m also not sure we need to maintain _that_ much state?

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

So effectively turn mprotect_folio_pte_batch() into an iterator; have a struct and a funtion to init the struct for the the number of ptes we want to iterate over, then a per iteration function that progressively returns batches?

Then we just have a simple loop here that gets the next batch and processes it?

Quoted 30 lines
Quoted 6 lines

This loop approach, as written, felt more straightforward for the reader to understand (i.e. the least-worst option). Is the context approach what you are suggesting or do you have something else in mind?

See above.

Quoted 6 lines
Quoted 6 lines

Alternatively, we could add a new wrapper function, but yeah definitely not this.

Also the C programming language asks... etc etc. ;)

Quoted 8 lines
Quoted 3 lines

Overall since you always end up processing folio_nr_pages(folio) you can just have the batch function or a wrapper return this and do updates as necessary here on that basis, and leave the 'sub' batching to that function.

Sorry I don't understand this statement - could you clarify? Especially the bit about "always ... processing folio_nr_pages(folio)"; I don't think we do. In various corner cases the size of the folio has no relationship to the way the PTEs are mapped.

Right yeah I put this badly. Obviously you can have all sorts of fun with large folios partially mapped and page-table split and etc. etc.

I should have said 'always process nr_ptes'.

The idea is to abstract this sub-batch stuff to another function, fundamentally.

#25 lore raw

Re: [PATCH v4 2/4] mm: Add batched versions of ptep_modify_prot_start/commit

In reply to Ryan Roberts
Quoted 41 lines · Ryan Roberts
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
Quoted 12 lines

include/linux/pgtable.h static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, · line 1416

Quoted 10 lines
(quoted diff) +2 -0
+#ifndef modify_prot_commit_ptes
+static inline void modify_prot_commit_ptes(struct vm_area_struct *vma,

unsigned long addr,
+        pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

include/linux/pgtable.h +6 -0
+{
+    int i;
+
+    for (i = 0; i < nr; ++i) {
+        ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);
+        ptep++;

Weird place to put this increment, maybe just stick it in the for loop.

Quoted 2 lines
Quoted 1 line

+        addr += PAGE_SIZE;

Same comment here.

Sure.

Quoted 7 lines
Quoted 2 lines

+        old_pte = pte_next_pfn(old_pte);

Could be:

        old_pte = pte;

No?

Quoted 8 lines
Quoted 2 lines

We will need to update old_pte also since that is used by powerpc in radix__ptep_modify_prot_commit().

I think perhaps Lorenzo has the model in his head where old_pte is the previous pte in the batch. That's not the case. old_pte is the value of the pte in the current position of the batch before any changes were made. pte is the new value for the pte. So we need to expliticly advance the PFN in both old_pte and pte each iteration round the loop.

Quoted 6 lines

Yeah, you're right, apologies, I'd misinterpreted.

I really, really, really hate how all this is implemented. This is obviously an mprotect() and legacy thing but it's almost designed for confusion. Not the fault of this series, and todo++ on improving mprotect as a whole (been on my list for a while...)

Quoted 5 lines

Agreed. I struggled for a long time with some of the pgtable helper abstractions to the arch and all the assumptions they make. But ultimately all Dev is trying to do here is make some incremental improvements, following the established patterns. Hopefully you agree that cleanups on a larger scale should be reserved for a systematic, focussed series.

Totally agree, when I mention my distaste for existing logic, see those as asides, I'm not asking the series to be blocked until that's fixed :)

I'm happy for us to take Dev's changes obviously once review issues are resolved.

I think my suggestion below helps get us to a good compromise (modulo mm's beautifully overloaded/confusing use of terminology :>)

Quoted 11 lines
Quoted 8 lines

So we're ultimately updating ptep (this thing that we update, of course, is buried in the middle of the function invocation) in:

	ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

We are setting *ptep++ = pte essentially (roughly speaking) right?

Yeah, pretty much.

Quoted 8 lines

The API was originally created for Xen IIRC. The problem is that the HW can update the A/D bits asynchronously if the PTE is valid (from the HW perspective) so the previous approach was to get_and_clear (atomic), modify, write. But that required 2 Xen hypervisor calls per PTE. This start/commit approach allows Xen to both avoid the get_and_clear() and batch the writes for all PTEs in a lazy mmu batch. So hypervisor calls are reduced from 2 per PTE to 1 per lazy mmu batch. TBH I'm no Xen expert; some of those details may be off, but big picture is correct.

Yeah, here we go again with some horror show stuff on Xen's behalf. I've played Half Life, so I already know to fear Xen ;)

I believe David has _thoughts_ on this :)

Again this is aside stuff.

Quoted 4 lines

Anyway, arm64 doesn't care about any of that, but it does override ptep_modify_prot_start() / ptep_modify_prot_commit() to implement an erratum workaround. And it can benefit substantially from batching.

Ack. And of course, the batching part is why we're all here!

Quoted 24 lines
Quoted 24 lines

And the arch needs to know about any bits that have changed I guess hence providing old_pte as well right?

OK so yeah, I get it now, we're not actually advancing through ptes here, we're just advancing the PFN and applying the same 'template'.

How about something like:

static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr)

{
	int i;

	for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
		ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte);

		/* Advance PFN only, set same flags. */
		old_pte = pte_next_pfn(old_pte);
		pte = pte_next_pfn(pte);
	}
}
Quoted 5 lines
Quoted 2 lines

Neatens it up a bit and makes it clear that we're effectively propagating the flags here.

Yes, except we don't usually refer to the non-pfn parts of a pte as "flags". We normally call them pgprot or prot. God knows why...

Ah of course we love to do this kind of thing to oursevles :>)

Dev - suggestion above then but s/flags/prot/.

#42 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Ryan Roberts
Quoted 24 lines · Ryan Roberts
Quoted 24 lines
Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

But i"m also not sure we need to maintain _that_ much state?

Quoted 8 lines
Quoted 4 lines

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

So effectively turn mprotect_folio_pte_batch() into an iterator; have a struct and a funtion to init the struct for the the number of ptes we want to iterate over, then a per iteration function that progressively returns batches?

Is that really necessary though?

Idea is that mprotect_folio_pte_batch() returns the nr_ptes _taking into account the PAE stuff_.

Would this break anything?

We might need to pass a flag to say 'don't account for this' for prot numa case.

Quoted 2 lines

Then we just have a simple loop here that gets the next batch and processes it?

#43 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

Quoted 30 lines · Lorenzo Stoakes
Quoted 29 lines · Ryan Roberts
Quoted 28 lines
Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

But i"m also not sure we need to maintain _that_ much state?

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

Quoted 8 lines
Quoted 3 lines

So effectively turn mprotect_folio_pte_batch() into an iterator; have a struct and a funtion to init the struct for the the number of ptes we want to iterate over, then a per iteration function that progressively returns batches?

Is that really necessary though?

Idea is that mprotect_folio_pte_batch() returns the nr_ptes _taking into account the PAE stuff_.

The issue is the efficiency. Assuming we want to keep the PTE scan contained within the core folio_pte_batch() function and we _don't_ want to add PAE awareness to that function, then we have 2 separate, independent loops; one for PTE scanning and the other for PAE scanning. If the first loop scans through ans returns 512, but then the PAE scan returns 1, we return 1. If we don't remember for the next time that we already determined we have a PTE batch of 512 (now
511) then we will rescan the 511 PTEs and potentially return 1 again due to PAE. Then 510, then 509...

That feels inefficient to me. So I'd much rather just remember that we have a batch of 512, then split into sub batches as needed for PAE compliance. Then we only scan each PTE once and each struct page once.

But to achieve this, we either need to merge the 2 loops or we need to carry state across function calls (i.e. like an iterator).

Quoted 2 lines

Would this break anything?

It's not about breaking anything, it's about scanning efficiently. Perhaps you don't think it's worth worrying about in practice?

Quoted 2 lines

We might need to pass a flag to say 'don't account for this' for prot numa case.

Yep, another bool ;-)

Quoted 3 lines
Quoted 3 lines

Then we just have a simple loop here that gets the next batch and processes it?

#44 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

Quoted 40 lines · Ryan Roberts
Quoted 30 lines · Lorenzo Stoakes
Quoted 29 lines · Ryan Roberts
Quoted 28 lines
Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

But i"m also not sure we need to maintain _that_ much state?

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

Quoted 8 lines
Quoted 3 lines

So effectively turn mprotect_folio_pte_batch() into an iterator; have a struct and a funtion to init the struct for the the number of ptes we want to iterate over, then a per iteration function that progressively returns batches?

Is that really necessary though?

Idea is that mprotect_folio_pte_batch() returns the nr_ptes _taking into account the PAE stuff_.

Quoted 8 lines

The issue is the efficiency. Assuming we want to keep the PTE scan contained within the core folio_pte_batch() function and we _don't_ want to add PAE awareness to that function, then we have 2 separate, independent loops; one for PTE scanning and the other for PAE scanning. If the first loop scans through ans returns 512, but then the PAE scan returns 1, we return 1. If we don't remember for the next time that we already determined we have a PTE batch of 512 (now
511) then we will rescan the 511 PTEs and potentially return 1 again due to PAE. Then 510, then 509...

Hm really?

The idea is mprotect_folio_pte_batch() would look ahead and determine the PAE/non-PAE sub-batch and return this nr_pages. It'd check 'this page is PAE, so when's the next page that is not/hit max_nr_pages?'

So that'd be 1 in the first case.

Then you loop around and go again, and this time it'd check 'this page is !PAE, so when's the next page that is/hit max_nr_pages?' and then it'd return 511.

A better example I think is e.g. if we had, for the sake argument, it return 16, 16, 480.

Then we scan ahead 16, set nr_ptes = 16, process 16 PTEs. Then the same again, then the same again only for 480 PTEs.

Each time we set nr_ptes = the sub-batch size.

So I don't think we'll see O(n^2) here?

It would be like:

	do {
		/* now returns sub-batch count */
		nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
				   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

		... rest of logic remains roughly the same ...
	} while (...);

I may be being naive here in some way?

Quoted 5 lines

That feels inefficient to me. So I'd much rather just remember that we have a batch of 512, then split into sub batches as needed for PAE compliance. Then we only scan each PTE once and each struct page once.

Quoted 8 lines

But to achieve this, we either need to merge the 2 loops or we need to carry state across function calls (i.e. like an iterator).

Quoted 2 lines

Would this break anything?

It's not about breaking anything, it's about scanning efficiently. Perhaps you don't think it's worth worrying about in practice?

The question re: breaking was - if we re-do things like getting oldpte, ptent, etc. on each sub-batch does _that_ break anything?

The current implementation is not acceptable on the basis of adding a horrible level of complexity. That function is already terrible, and adding an inner loop for this batch special casing with _sub batches_ to account for PAE- nobody is going to understand what's going on.

	do {
		if (...) {
			while (...) {
				help!!!

We can do better, and I'm going to go further and say - this series _has_ to do better, because I can't accept that, however we do it.

I want the efficiency gainz as much as you guys but I"m convinced we can do it without causing eye bleeding confusion...

Quoted 5 lines
Quoted 3 lines

We might need to pass a flag to say 'don't account for this' for prot numa case.

Yep, another bool ;-)

Yeah... but we can't sensibly add a flag for this so the flag idea doesn't fly for that either... :>)

I mean I don't think we actually need that flag, let it skip the sub-batch size then check again. Now that, I reckon, is a small overhead.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Then we just have a simple loop here that gets the next batch and processes it?

Another advantage of doing things this way is we can actually add a comment explaining the sub-batch size.

Right now there's just absolutely _nothing_ and it's entirely unclear what on earth is going on.

#45 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

Quoted 81 lines · Lorenzo Stoakes
Quoted 40 lines · Ryan Roberts
Quoted 30 lines · Lorenzo Stoakes
Quoted 29 lines · Ryan Roberts
Quoted 28 lines
Quoted 5 lines

In an ideal world we would flatten and just have mprotect_folio_pte_batch() return the batch size considering all the relevant PTE bits AND the AnonExclusive bit on the pages. IIRC one of Dev's earlier versions modified the core folio_pte_batch() function to also look at the AnonExclusive bit, but I really disliked changing that core function (I think others did too?).

Yeah let's not change the core function.

My suggestion is to have mprotect_folio_pte_batch() do this.

Quoted 8 lines

So barring that approach, we are really only left with the batch and sub-batch approach - although, yes, it could be abstracted more. We could maintain a context struct that persists across all calls to mprotect_folio_pte_batch() and it can use that to keep it's state to remember if we are in the middle of a sub-batch and decide either to call folio_pte_batch() to get a new batch, or call anon_exclusive_batch() to get the next sub-batch within the current batch. But that started to feel overly abstracted to me.

Having this nested batch/sub-batch loop really feels worse. You just get lost in the complexity here very easily.

But i"m also not sure we need to maintain _that_ much state?

We're already looping over all of the PTEs here, so abstracting _the entire loop_ and all the sub-batch stuff to another function, that is mprotect_folio_pte_batch() I think sensibly, so it handles this for you makes a ton of sense.

Quoted 8 lines
Quoted 3 lines

So effectively turn mprotect_folio_pte_batch() into an iterator; have a struct and a funtion to init the struct for the the number of ptes we want to iterate over, then a per iteration function that progressively returns batches?

Is that really necessary though?

Idea is that mprotect_folio_pte_batch() returns the nr_ptes _taking into account the PAE stuff_.

Quoted 8 lines

The issue is the efficiency. Assuming we want to keep the PTE scan contained within the core folio_pte_batch() function and we _don't_ want to add PAE awareness to that function, then we have 2 separate, independent loops; one for PTE scanning and the other for PAE scanning. If the first loop scans through ans returns 512, but then the PAE scan returns 1, we return 1. If we don't remember for the next time that we already determined we have a PTE batch of 512 (now
511) then we will rescan the 511 PTEs and potentially return 1 again due to PAE. Then 510, then 509...

Hm really?

The idea is mprotect_folio_pte_batch() would look ahead and determine the PAE/non-PAE sub-batch and return this nr_pages. It'd check 'this page is PAE, so when's the next page that is not/hit max_nr_pages?'

So that'd be 1 in the first case.

Then you loop around and go again, and this time it'd check 'this page is !PAE, so when's the next page that is/hit max_nr_pages?' and then it'd return 511.

A better example I think is e.g. if we had, for the sake argument, it return 16, 16, 480.

Then we scan ahead 16, set nr_ptes = 16, process 16 PTEs. Then the same again, then the same again only for 480 PTEs.

Each time we set nr_ptes = the sub-batch size.

So I don't think we'll see O(n^2) here?

It would be like:

	do {
		/* now returns sub-batch count */
		nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
				   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);

		... rest of logic remains roughly the same ...
	} while (...);
Quoted 1 line

I may be being naive here in some way?

I believe so, yes. But usually it's me that ends up being wrong. Let me try to explain my point and we shall see...

There are 2 separate requirements that need to be met for a batch to be assembled:

- All PTEs have to map consecutive pages from the same folio, all with the same pgprots (except a/d/sd).
- If anon, all of the mapped pages must have the same PAE value.

The first requirement is managed by scanning forward through PTEs until it hits the first PTE that is non-conformant (or hits max_nr). Currently implemented by folio_pte_batch().

The second requirement is managed by scanning through the struct pages, checking PAE (or hits max_nr).

The final batch is determined according to the smaller of the 2 batches determined using both these checks.

So, assuming we don't want to fold both of those into the same loop (which would enable checking the PTE and PAE in lock-step, mprotect_folio_pte_batch() needs to call both folio_pte_batch() and loop over the pages looking at PAE in order to decide where the batch boundary is.

If we want it to be stateless, then if it scans the PTEs first and that batch is larger than the batch determined for the subsequent PAE scan, we return the smaller and next time it is called it will rescan those excess PTEs. The same logic applies in reverse if you scan PAE first.

If we make it stateless, it can remember "I've already scanned PTEs and the PTE batch ends at X. So now I just need to iterate through that to create sub-batches taking PAE into account".

Quoted 30 lines
Quoted 6 lines

That feels inefficient to me. So I'd much rather just remember that we have a batch of 512, then split into sub batches as needed for PAE compliance. Then we only scan each PTE once and each struct page once.

Quoted 8 lines

But to achieve this, we either need to merge the 2 loops or we need to carry state across function calls (i.e. like an iterator).

Quoted 2 lines

Would this break anything?

It's not about breaking anything, it's about scanning efficiently. Perhaps you don't think it's worth worrying about in practice?

The question re: breaking was - if we re-do things like getting oldpte, ptent, etc. on each sub-batch does _that_ break anything?

The current implementation is not acceptable on the basis of adding a horrible level of complexity. That function is already terrible, and adding an inner loop for this batch special casing with _sub batches_ to account for PAE- nobody is going to understand what's going on.

	do {
		if (...) {
			while (...) {
				help!!!
Quoted 5 lines

We can do better, and I'm going to go further and say - this series _has_ to do better, because I can't accept that, however we do it.

I want the efficiency gainz as much as you guys but I"m convinced we can do it without causing eye bleeding confusion...

That's completely reasonable - we will get there! I'm very happy for this to be refactored into help function(s) to make it more accessible.

I'm just saying that fundamentally, we either need to flatten this to a single loop so that the PTE and PAE can be assessed in lock-step and we never over-scan. Or we need to keep some state to remember when we have already scanned for a PTE batch and are currently working our way through that chunking it into sub-batches based on PAE. I don't think we should entertain a stateless two-loop solution.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

We might need to pass a flag to say 'don't account for this' for prot numa case.

Quoted 7 lines
Quoted 1 line

Yep, another bool ;-)

Yeah... but we can't sensibly add a flag for this so the flag idea doesn't fly for that either... :>)

I mean I don't think we actually need that flag, let it skip the sub-batch size then check again. Now that, I reckon, is a small overhead.

Yeah, agreed. That's probably fine in practice.

Quoted 12 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Then we just have a simple loop here that gets the next batch and processes it?

Another advantage of doing things this way is we can actually add a comment explaining the sub-batch size.

Right now there's just absolutely _nothing_ and it's entirely unclear what on earth is going on.

#46 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

Quoted 37 lines · Ryan Roberts
Quoted 37 lines
Quoted 8 lines

The issue is the efficiency. Assuming we want to keep the PTE scan contained within the core folio_pte_batch() function and we _don't_ want to add PAE awareness to that function, then we have 2 separate, independent loops; one for PTE scanning and the other for PAE scanning. If the first loop scans through ans returns 512, but then the PAE scan returns 1, we return 1. If we don't remember for the next time that we already determined we have a PTE batch of 512 (now
511) then we will rescan the 511 PTEs and potentially return 1 again due to PAE. Then 510, then 509...

Hm really?

The idea is mprotect_folio_pte_batch() would look ahead and determine the PAE/non-PAE sub-batch and return this nr_pages. It'd check 'this page is PAE, so when's the next page that is not/hit max_nr_pages?'

So that'd be 1 in the first case.

Then you loop around and go again, and this time it'd check 'this page is !PAE, so when's the next page that is/hit max_nr_pages?' and then it'd return 511.

A better example I think is e.g. if we had, for the sake argument, it return 16, 16, 480.

Then we scan ahead 16, set nr_ptes = 16, process 16 PTEs. Then the same again, then the same again only for 480 PTEs.

Each time we set nr_ptes = the sub-batch size.

So I don't think we'll see O(n^2) here?

It would be like:

	do {
		/* now returns sub-batch count */
		nr_ptes = mprotect_folio_pte_batch(folio, addr, pte, oldpte,
				   max_nr_ptes, FPB_IGNORE_SOFT_DIRTY);
Quoted 7 lines
Quoted 4 lines
		... rest of logic remains roughly the same ...
	} while (...);

I may be being naive here in some way?

I believe so, yes. But usually it's me that ends up being wrong. Let me try to explain my point and we shall see...

Haha no, being embarrassingly wrong is my speciality. Don't take that from me ;)

But in this case I think you're right here actually and I've underestimated the complication here.

Quoted 10 lines

There are 2 separate requirements that need to be met for a batch to be assembled:

- All PTEs have to map consecutive pages from the same folio, all with the same pgprots (except a/d/sd).
- If anon, all of the mapped pages must have the same PAE value.

The first requirement is managed by scanning forward through PTEs until it hits the first PTE that is non-conformant (or hits max_nr). Currently implemented by folio_pte_batch().

OK so I think this is the crux. The folio_pte_batch() is naive to the PAE thing, and so we end up having to rescan.

Quoted 12 lines

The second requirement is managed by scanning through the struct pages, checking PAE (or hits max_nr).

The final batch is determined according to the smaller of the 2 batches determined using both these checks.

So, assuming we don't want to fold both of those into the same loop (which would enable checking the PTE and PAE in lock-step, mprotect_folio_pte_batch() needs to call both folio_pte_batch() and loop over the pages looking at PAE in order to decide where the batch boundary is.

Quoted 8 lines

If we want it to be stateless, then if it scans the PTEs first and that batch is larger than the batch determined for the subsequent PAE scan, we return the smaller and next time it is called it will rescan those excess PTEs. The same logic applies in reverse if you scan PAE first.

If we make it stateless, it can remember "I've already scanned PTEs and the PTE batch ends at X. So now I just need to iterate through that to create sub-batches taking PAE into account".

Right yeah. Statelessness is not crucial here and doesn't seem workable then.

Quoted 11 lines
Quoted 11 lines

The current implementation is not acceptable on the basis of adding a horrible level of complexity. That function is already terrible, and adding an inner loop for this batch special casing with _sub batches_ to account for PAE- nobody is going to understand what's going on.

	do {
		if (...) {
			while (...) {
				help!!!
Quoted 8 lines
Quoted 5 lines

We can do better, and I'm going to go further and say - this series _has_ to do better, because I can't accept that, however we do it.

I want the efficiency gainz as much as you guys but I"m convinced we can do it without causing eye bleeding confusion...

That's completely reasonable - we will get there! I'm very happy for this to be refactored into help function(s) to make it more accessible.

We'll definitely get there :)

Quoted 7 lines

I'm just saying that fundamentally, we either need to flatten this to a single loop so that the PTE and PAE can be assessed in lock-step and we never over-scan. Or we need to keep some state to remember when we have already scanned for a PTE batch and are currently working our way through that chunking it into sub-batches based on PAE. I don't think we should entertain a stateless two-loop solution.

Yes.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

We might need to pass a flag to say 'don't account for this' for prot numa case.

Yep, another bool ;-)

Quoted 7 lines
Quoted 5 lines

Yeah... but we can't sensibly add a flag for this so the flag idea doesn't fly for that either... :>)

I mean I don't think we actually need that flag, let it skip the sub-batch size then check again. Now that, I reckon, is a small overhead.

Yeah, agreed. That's probably fine in practice.

Ack.

Let me fiddle with this code and see if I can suggest something sensible.

#12 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Dev Jain
Quoted 24 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
 mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +6 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 	return pte_dirty(pte);
 }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 101

Quoted 10 lines
mm/mprotect.c +10 -0
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{

While it's nice to separate this out, it's not so nice to pass folio as a pointer to a pointer like this and maybe or maybe not set it.

Just get the folio before you call this... you'll need it either way.

I'll wait until you separate it all out before reviewing next revision as a bit tricky as-is.

Thanks!

#47 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes

Let me fiddle with this code and see if I can suggest something sensible.

OK this is _even more fiddly_ than I thought.

Dev - feel free to do a respin (once David's stuff's landed in mm-new, which I think maybe it has now?), and we can maybe discuss a v6 with everything else in place if this is still problematic.

Ryan mentioned an iterator solution, which actually now seems sensible here, if fiddly. Please do try to attack that in v5 to see if something's workable there.

Why are computers so complicated...

author
#13 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Lorenzo Stoakes
Quoted 38 lines · Lorenzo Stoakes
Quoted 24 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
  mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
  1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +6 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 101

Quoted 10 lines
mm/mprotect.c +10 -0
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{

While it's nice to separate this out, it's not so nice to pass folio as a pointer to a pointer like this and maybe or maybe not set it.

Quoted 1 line

Just get the folio before you call this... you'll need it either way.

I did that on David's suggestion:

https://lore.kernel.org/all/[email protected]/

We were trying to reuse the folio if available from prot_numa_skip_ptes, to avoid using vm_normal_folio() again. Not sure if avoiding vm_normal_folio is worth the complexity.

Quoted 5 lines

I'll wait until you separate it all out before reviewing next revision as a bit tricky as-is.

Thanks!

author
#48 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 4 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes

Let me fiddle with this code and see if I can suggest something sensible.

OK this is _even more fiddly_ than I thought.

Quoted 8 lines

Dev - feel free to do a respin (once David's stuff's landed in mm-new, which I think maybe it has now?), and we can maybe discuss a v6 with everything else in place if this is still problematic.

Ryan mentioned an iterator solution, which actually now seems sensible here, if fiddly. Please do try to attack that in v5 to see if something's workable there.

Why are computers so complicated...

Sure! I'll be out next week so the mm list can take a breather from my patches : ) My brain exploded trying to understand your and Ryan's implementation conversation, will read it all and try to come up with something nice.

#49 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

Quoted 10 lines · Dev Jain
Quoted 8 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes

Let me fiddle with this code and see if I can suggest something sensible.

OK this is _even more fiddly_ than I thought.

Dev - feel free to do a respin (once David's stuff's landed in mm-new, which I think maybe it has now?), and we can maybe discuss a v6 with everything else in place if this is still problematic.

Quoted 9 lines
Quoted 4 lines

Ryan mentioned an iterator solution, which actually now seems sensible here, if fiddly. Please do try to attack that in v5 to see if something's workable there.

Why are computers so complicated...

Sure! I'll be out next week so the mm list can take a breather from my patches : ) My brain exploded trying to understand your and Ryan's implementation conversation, will read it all and try to come up with something nice.

No worries, it's actually genuinely annoyingly tricky this... :) we may need a few more iterations to get there.

Really the underlying issue (other than inherent complexity) is the poor mprotect implementation in the first instance that makes the complexity here less easy to integrate.

#14 lore raw

Re: [PATCH v4 1/4] mm: Optimize mprotect() for MM_CP_PROT_NUMA by batch-skipping PTEs

In reply to Dev Jain
Quoted 42 lines · Dev Jain
Quoted 38 lines · Lorenzo Stoakes
Quoted 24 lines · Dev Jain

In case of prot_numa, there are various cases in which we can skip to the next iteration. Since the skip condition is based on the folio and not the PTEs, we can skip a PTE batch. Additionally refactor all of this into a new function to clean up the existing code.

---
  mm/mprotect.c | 134 ++++++++++++++++++++++++++++++++------------------
  1 file changed, 87 insertions(+), 47 deletions(-)
mm/mprotect.c +6 -0
index 88709c01177b..af10a7fbe6b8 100644
@@ -83,6 +83,83 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
  	return pte_dirty(pte);
  }
 
+static int mprotect_folio_pte_batch(struct folio *folio, unsigned long addr,
+		pte_t *ptep, pte_t pte, int max_nr_ptes)
+{
+	const fpb_t flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
+
+	if (!folio || !folio_test_large(folio) || (max_nr_ptes == 1))

mm/mprotect.c bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, · line 101

Quoted 10 lines
mm/mprotect.c +10 -0
+		return 1;
+
+	return folio_pte_batch(folio, addr, ptep, pte, max_nr_ptes, flags,
+			       NULL, NULL, NULL);
+}
+
+static int prot_numa_skip_ptes(struct folio **foliop, struct vm_area_struct *vma,
+		unsigned long addr, pte_t oldpte, pte_t *pte, int target_node,
+		int max_nr_ptes)
+{

While it's nice to separate this out, it's not so nice to pass folio as a pointer to a pointer like this and maybe or maybe not set it.

Quoted 1 line

Just get the folio before you call this... you'll need it either way.

Quoted 7 lines

I did that on David's suggestion:

https://lore.kernel.org/all/[email protected]/

We were trying to reuse the folio if available from prot_numa_skip_ptes, to avoid using vm_normal_folio() again. Not sure if avoiding vm_normal_folio is worth the complexity.

Well, do you need to? You're doing vm_normal_folio() in both cases, why not just put the vm_normal_folio() lookup in change_pte_range() before invoking this function, then reuse the folio in the loop?

Oh right, I guess David was concerned about not needing to look it up in the pte_protnone(oldpte) case?

I'm not sure that's worth it honestly. If we do _really_ want to do this, then at least put the param last

#50 lore raw

Re: [PATCH v4 3/4] mm: Optimize mprotect() by PTE-batching

In reply to Lorenzo Stoakes
Quoted 21 lines · Lorenzo Stoakes
Quoted 10 lines · Dev Jain
Quoted 8 lines · Lorenzo Stoakes
Quoted 1 line · Lorenzo Stoakes

Let me fiddle with this code and see if I can suggest something sensible.

OK this is _even more fiddly_ than I thought.

Dev - feel free to do a respin (once David's stuff's landed in mm-new, which I think maybe it has now?), and we can maybe discuss a v6 with everything else in place if this is still problematic.

Quoted 9 lines
Quoted 4 lines

Ryan mentioned an iterator solution, which actually now seems sensible here, if fiddly. Please do try to attack that in v5 to see if something's workable there.

Why are computers so complicated...

Sure! I'll be out next week so the mm list can take a breather from my patches : ) My brain exploded trying to understand your and Ryan's implementation conversation, will read it all and try to come up with something nice.

Quoted 7 lines

No worries, it's actually genuinely annoyingly tricky this... :) we may need a few more iterations to get there.

Really the underlying issue (other than inherent complexity) is the poor mprotect implementation in the first instance that makes the complexity here less easy to integrate.

I haven't really scanned all the discussions yet, just something to keep in mind:

In the common case, all PAE values will match. This is the case to optimize for.

Having some PAE values not match is the corner case, that can be left suboptimal if it makes the code nicer.

Signature · 3 lines
Cheers,

David / dhildenb