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

From: Pedro Falcato

Date: Tue Apr 07 2026 - 10:08:53 EST


On Tue, Apr 07, 2026 at 02:31:29PM +0200, Vlastimil Babka (SUSE) wrote:
> 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? :)

These kinds of tricks are pretty ingrained across kernel code, e.g:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/include/asm/cmpxchg.h#n65

and similar. In these cases if it fails to do constant propagation you'll
get an actual build error. In our case you just get a regression back :)

--
Pedro