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

Lorenzo Stoakes (Oracle)
2026-03-19T19:17:40Z

On Thu, Mar 19, 2026 at 06:31:08PM +0000, Pedro Falcato wrote:
> 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.
>

I think it'd be good to get a sense per-commit what the perf impact is.

> Signed-off-by: Pedro Falcato <pfalcato@suse.de>

I am iffy on the likely() in the same way I am always iffy on non-profile backed
likely()/unlikely() but this change seems sensible enough so:

Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>

> ---
> 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)) {

I mean I we're likely()'ing here without data to back it, but I guess I believe
this one :)

> + 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
>

Cheers, Lorenzo