Re: mm/mprotect: testing a zero-offset fast path
From: lin2530632123
Date: Tue Dec 01 2026 - 09:18:04 EST
Hi Dev,
Sorry I wasn't clear. The earlier reports were about the shared-dirty
PTE workload. This time I was trying to understand part of the slowdown
in libMicro, not report a new regression. I don't have evidence that
this slowdown affects an application workload. The zero-offset check
helped in that test, so I shared the result for discussion.
The same change slowed down a follow-up test with mostly nonzero
offsets. That's why I sent the follow-up, and I won't pursue the change.
Thanks,
Chengfeng
On Tue, 01 Dec 2026 10:14:09 +0530, Dev Jain <dev.jain@xxxxxxx> wrote:
> On 30/11/26 2:51 pm, Chengfeng Lin wrote:
> > Hi Dev, Pedro and all,
> >
> > On bare-metal x86 (i7-12700KF), I tested mprotect() with the libMicro
> > benchmark used by MMTests. Before timing, it writes to a private
> > /dev/zero mapping to populate its pages. It then changes permissions
> > between PROT_NONE and PROT_READ | PROT_WRITE in 128 KiB requests.
> > In a direct-parent / cac1db8c3aad / direct-parent comparison, requests
> > took 8.1-8.3% longer on the commit than on either parent run. I saw this
> > both with and without reading each page before removing access.
> >
> > To isolate one cost, I kept batching enabled on v7.3 and skipped just the
> > two pte_advance_pfn() calls in prot_commit_flush_ptes() when idx is zero.
> > At offset zero the subrange starts at the first PTE, so neither the old
> > nor the new PTE needs its page frame number adjusted. Nonzero offsets,
> > protection commits and TLB flush decisions are unchanged.
>
> I really don't understand what your objective here is, you have sent two
> reports in the past as well:
>
> https://lore.kernel.org/all/662955ba.f499.19e3b2cf478.Coremail.23020251154299@xxxxxxxxxxxxxx/
>
> https://lore.kernel.org/all/7c9eeb21.1c04f.19efaacbb2f.Coremail.chengfenglin@xxxxxxxxxxxxxx/
>
> If there is something we are genuinely missing and is a *real* problem,
> please explain in brief. It is really difficult to read walls upon walls of
> text, even more so with all the LLM stuff we are getting, and that I am
> busy with off-list work so haven't had the time to look at the list
> as much as I would want to.
>
>
> >
> > Means of nine uninstrumented runs per arm:
> >
> > stock before guard only stock return
> > libMicro with read-touch,
> > us per 128 KiB request 2.462113 2.377241 2.456938
> > Separate 16-PTE fixture,
> > ns per NONE + RW round trip 2669.41 2591.66 2676.70
> >
> > The first row averages libMicro's default usecs/call medians from all
> > nine runs per arm. libMicro filters batches within each run. With -w,
> > the pages are written before timing. With -t, reading each page before
> > removing access is part of the timed work. The result is time per
> > 128 KiB benchmark request, not syscall-only latency.
> >
> > Request time fell by 3.45% / 3.24% (about 80-85 ns) versus the before/return
> > baselines. In separate observations, all 8,192 batch queries returned one
> > page and all 16,384 PTE advances had offset zero.
> >
> > The second row uses another private 128 KiB anonymous mapping, with
> > 64 KiB multi-size THPs, pinned to CPU 4 and with no page access between
> > the two calls. I separately verified 16-PTE batches in both directions.
> > Time per complete round trip fell by 2.91% / 3.18%. These reductions are
> > for the two tested workloads on v7.3.
> >
> > For the native workload I used gormanm/libMicro at
> > 976d502afaa2d9b919564d7fa8a08fec286dfc89. The single worker was allowed
> > to run on CPUs 0-19:
> >
> > taskset -c 0-19 ./mprotect -E -C 0 -D 30000 -B 10000 -L -S -W \
> > -N mprot_tw128k -l 128k -w -t -f /dev/zero
> >
> > The v7.3 comparison used this diff:
> >
> > --- a/mm/mprotect.c
> > +++ b/mm/mprotect.c
> > @@ -127,8 +127,10 @@ static __always_inline void
> > prot_commit_flush_ptes(struct vm_area_struct *vma,
> > */
> > addr += idx * PAGE_SIZE;
> > ptep += idx;
> > - oldpte = pte_advance_pfn(oldpte, idx);
> > - ptent = pte_advance_pfn(ptent, idx);
> > + if (idx) {
> > + oldpte = pte_advance_pfn(oldpte, idx);
> > + ptent = pte_advance_pfn(ptent, idx);
> > + }
> >
> > if (set_write)
> > ptent = pte_mkwrite(ptent, vma);
> >
> > I checked mainline v7.4-rc5 (fe6ff8d5d675): both advances are still
> > unconditional. I haven't measured that revision. I'd appreciate your
> > thoughts on whether this small change looks useful.
> >
> > Thanks,
> > Chengfeng