Re: mm/mprotect: testing a zero-offset fast path

From: lin2530632123

Date: Tue Dec 01 2026 - 09:16:38 EST


Hi Pedro,

Both tests ran on the same x86-64 machine (i7-12700KF), with kernels
built using GCC 15.2.0. For the follow-up, I used the same v7.3
kernel for both settings. At zero offset, the kernel made the two
calls as usual in one setting and skipped them in the other via a
switch. In a separate check, both settings handled the same requests
and number of pages.

For this follow-up, I used the same 128 KiB libMicro test. Skipping
the calls was faster in all eight pairs of runs without probes.
The time per request went from 2.465 to 2.373 us, a saving of about
92 ns. Even with the code layout fixed, the saving was close to the
original 80-85 ns.

The first test compared two separately built kernels. I checked
the machine code: the stock build inlined change_pte_range() into
change_protection(), but still emitted two calls to pte_advance_pfn().
With the guard, those calls were skipped at zero offset, and GCC
also emitted change_pte_range() as a separate function. I repeated
the test using one kernel because the original comparison also
changed the code layout.

Thanks,
Chengfeng


On Mon, 30 Nov 2026 18:48:21 +0000, Pedro Falcato <pfalcato@xxxxxxx> wrote:
> On Mon, Nov 30, 2026 at 05:21:47PM +0800, 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.
> >
> > 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.
>
> This doesn't match my expectations. What architecture is this? pte_advance_pfn()
> is a fairly simple function with (AIUI) a fairly simple, predictable branch.
>
> What's the difference in codegen? Could it be some artifact of code alignment?
>
> --
> Pedro