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

From: Vlastimil Babka (SUSE)

Date: Tue Apr 07 2026 - 08:33:15 EST


On 4/2/26 16:16, 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.
>
> While at it, encourage the compiler to inline batch PTE logic and resolve
> constant branches by adding __always_inline strategically.
>
> Suggested-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@xxxxxxxxxx>
> Tested-by: Luke Yang <luyang@xxxxxxxxxx>
> Signed-off-by: Pedro Falcato <pfalcato@xxxxxxx>

Can't say I'm thrilled about these hacks, but seems to work, so

Reviewed-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>


> + if (likely(nr_ptes == 1)) {
> + change_present_ptes(tlb, vma, addr, pte, 1,
> + end, newprot, folio, page, cp_flags);
> + } else {
> + change_present_ptes(tlb, vma, addr, pte,
> + nr_ptes, end, newprot, folio, page,
> + cp_flags);
> + }

I wonder if there's anything about this trick that ensures the compilers
will not eventually stop compiling it the way you intend. Have you talked to
compiler people? :)

> +
> pages += nr_ptes;
> } else if (pte_none(oldpte)) {
> /*