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

Vlastimil Babka (SUSE)
2026-04-07T12:31:37Z

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@kernel.org>
> Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
> Tested-by: Luke Yang <luyang@redhat.com>
> Signed-off-by: Pedro Falcato <pfalcato@suse.de>

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

Reviewed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>

> + 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)) {
> /*