[PATCH 4/4] mm/mprotect: special-case small folios when applying write permissions

Pedro Falcato
2026-03-19T18:31:33Z

The common order-0 case is important enough to want its own branch, and
avoids the hairy, large loop logic that the CPU does not seem to handle
particularly well.

Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
mm/mprotect.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index aa845f5bf14d..23a741f9a4c8 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -170,6 +170,13 @@ static __always_inline void commit_anon_folio_batch(struct vm_area_struct *vma,
int sub_batch_idx = 0;
int len;

+ /* Optimize for the common order-0 case. */
+ if (likely(nr_ptes == 1)) {
+ prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, 1,
+ 0, PageAnonExclusive(first_page), tlb);
+ return;
+ }
+
while (nr_ptes) {
expected_anon_exclusive = PageAnonExclusive(first_page + sub_batch_idx);
len = page_anon_exclusive_sub_batch(sub_batch_idx, nr_ptes,
--
2.53.0