* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 9:21 mm/mprotect: testing a zero-offset fast path Chengfeng Lin
@ 2026-11-30 15:41 ` lin2530632123
2026-11-30 16:08 ` Lorenzo Stoakes (ARM)
2026-11-30 18:48 ` Pedro Falcato
2 siblings, 0 replies; 7+ messages in thread
From: lin2530632123 @ 2026-11-30 15:41 UTC (permalink / raw)
To: Dev Jain
Cc: Pedro Falcato, Andrew Morton, linux-mm, Liam R. Howlett,
Lorenzo Stoakes, Vlastimil Babka, Jann Horn, linux-kernel
Hi Dev, Pedro and all,
I tested the same zero-offset guard with a workload dominated by
nonzero offsets. On the same i7-12700KF and v7.3 kernels, restoring
write permission took 2.20-2.45% longer with the guard.
The test uses a 64 KiB private anonymous folio with alternate 4 KiB
pages still shared with a child after fork. I timed only the call that
restores write permission. Returning the mapping to read-only prepares
the next iteration and is outside that timing. Separate probes showed
that both builds performed the same conversions at offsets 1 through
15, while the guard skipped the two zero-offset conversions per call.
Mean ns per mprotect(PROT_READ | PROT_WRITE), nine uninstrumented runs
per arm:
v7.3 stock before 1297.47
v7.3 with guard 1329.25
v7.3 stock return 1300.63
That is about 29-32 ns extra per call. The two stock means differed by
3.16 ns (0.24%). This measures the effect of the whole source change,
not the cost of the branch in isolation.
The earlier zero-offset measurements still stand for those workloads,
but this result shows the other side of the trade-off. I don't think
the available measurements justify pursuing the guard as-is.
Thanks,
Chengfeng
On Mon, 30 Nov 2026 17:21:47 +0800, Chengfeng Lin
<lin2530632123@gmail.com> 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.
>
> 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
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 9:21 mm/mprotect: testing a zero-offset fast path Chengfeng Lin
2026-11-30 15:41 ` lin2530632123
@ 2026-11-30 16:08 ` Lorenzo Stoakes (ARM)
2026-11-30 16:44 ` Chengfeng Lin
2026-11-30 18:48 ` Pedro Falcato
2 siblings, 1 reply; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 16:08 UTC (permalink / raw)
To: Chengfeng Lin
Cc: Dev Jain, Pedro Falcato, Andrew Morton, linux-mm,
Liam R. Howlett, Vlastimil Babka, Jann Horn, linux-kernel
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.
>
> 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.
Is this an RFC or a proposed patch or what is it?
I'm a little confused as to what real-world this impacts, and why we would want
it? It seems like a microbenchmark-based thing that just adds complexity no?
>
> Thanks,
> Chengfeng
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 16:08 ` Lorenzo Stoakes (ARM)
@ 2026-11-30 16:44 ` Chengfeng Lin
2026-11-30 16:48 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 7+ messages in thread
From: Chengfeng Lin @ 2026-11-30 16:44 UTC (permalink / raw)
To: Lorenzo Stoakes
Cc: Dev Jain, Pedro Falcato, Andrew Morton, linux-mm,
Liam R. Howlett, Vlastimil Babka, Jann Horn, linux-kernel
Hi Lorenzo,
Thanks for taking a look. I meant to share a microbenchmark result,
not submit an RFC or patch. Sorry I didn’t make that clear.
I don’t have evidence that this matters in an application workload,
and the follow-up test actually showed a slowdown when most offsets
were nonzero. I agree that these results don’t justify adding the
branch, so I won’t pursue the change.
Best,
Chengfeng
On Mon, 30 Nov 2026 16:08:46 +0000, "Lorenzo Stoakes (ARM)"
<ljs@kernel.org> 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.
> >
> > 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.
>
> Is this an RFC or a proposed patch or what is it?
>
> I'm a little confused as to what real-world this impacts, and why we would want
> it? It seems like a microbenchmark-based thing that just adds complexity no?
>
> >
> > Thanks,
> > Chengfeng
>
> --
> Cheers, Lorenzo
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 16:44 ` Chengfeng Lin
@ 2026-11-30 16:48 ` Lorenzo Stoakes (ARM)
2026-11-30 17:13 ` Chengfeng Lin
0 siblings, 1 reply; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 16:48 UTC (permalink / raw)
To: Chengfeng Lin
Cc: Dev Jain, Pedro Falcato, Andrew Morton, linux-mm,
Liam R. Howlett, Vlastimil Babka, Jann Horn, linux-kernel
On Mon, Nov 30, 2026 at 08:44:45AM -0800, Chengfeng Lin wrote:
> Hi Lorenzo,
>
> Thanks for taking a look. I meant to share a microbenchmark result,
> not submit an RFC or patch. Sorry I didn’t make that clear.
>
> I don’t have evidence that this matters in an application workload,
> and the follow-up test actually showed a slowdown when most offsets
> were nonzero. I agree that these results don’t justify adding the
> branch, so I won’t pursue the change.
Ack OK well I don't want to scare away reasonable discussion either :P
Probably the easiest way to clarify is to prefix [DISCUSSION] to the thread
but I don't think we really have a great convention for this.
Always with this stuff it's a bit of a 'why are we doing X' vs. benefit
trade-off.
But do welcome feedback + discussion, sorry didn't mean to dissuade that :)
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 16:48 ` Lorenzo Stoakes (ARM)
@ 2026-11-30 17:13 ` Chengfeng Lin
0 siblings, 0 replies; 7+ messages in thread
From: Chengfeng Lin @ 2026-11-30 17:13 UTC (permalink / raw)
To: Lorenzo Stoakes
Cc: Dev Jain, Pedro Falcato, Andrew Morton, linux-mm,
Liam R. Howlett, Vlastimil Babka, Jann Horn, linux-kernel
Hi Lorenzo,
No worries at all, and thanks for the note. The [DISCUSSION] prefix is
a good suggestion and would have made my intent clearer. I'll use it
for similar threads in the future.
I appreciate the feedback and the discussion.
Best,
Chengfeng
On Mon, 30 Nov 2026 16:48:33 +0000, "Lorenzo Stoakes (ARM)"
<ljs@kernel.org> wrote:
> On Mon, Nov 30, 2026 at 08:44:45AM -0800, Chengfeng Lin wrote:
> > Hi Lorenzo,
> >
> > Thanks for taking a look. I meant to share a microbenchmark result,
> > not submit an RFC or patch. Sorry I didn’t make that clear.
> >
> > I don’t have evidence that this matters in an application workload,
> > and the follow-up test actually showed a slowdown when most offsets
> > were nonzero. I agree that these results don’t justify adding the
> > branch, so I won’t pursue the change.
>
> Ack OK well I don't want to scare away reasonable discussion either :P
>
> Probably the easiest way to clarify is to prefix [DISCUSSION] to the thread
> but I don't think we really have a great convention for this.
>
> Always with this stuff it's a bit of a 'why are we doing X' vs. benefit
> trade-off.
>
> But do welcome feedback + discussion, sorry didn't mean to dissuade that :)
>
> --
> Cheers, Lorenzo
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: mm/mprotect: testing a zero-offset fast path
2026-11-30 9:21 mm/mprotect: testing a zero-offset fast path Chengfeng Lin
2026-11-30 15:41 ` lin2530632123
2026-11-30 16:08 ` Lorenzo Stoakes (ARM)
@ 2026-11-30 18:48 ` Pedro Falcato
2 siblings, 0 replies; 7+ messages in thread
From: Pedro Falcato @ 2026-11-30 18:48 UTC (permalink / raw)
To: Chengfeng Lin
Cc: Dev Jain, Andrew Morton, linux-mm, Liam R. Howlett,
Lorenzo Stoakes, Vlastimil Babka, Jann Horn, linux-kernel
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
^ permalink raw reply [flat|nested] 7+ messages in thread