From: Marc Zyngier <maz@kernel.org>
To: Puranjay Mohan <puranjay@kernel.org>, Will Deacon <will@kernel.org>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
Xu Kuohai <xukuohai@huaweicloud.com>,
Vadim Fedorenko <vadim.fedorenko@linux.dev>,
Catalin Marinas <catalin.marinas@arm.com>,
kernel-team@meta.com
Subject: Re: [PATCH bpf-next v13 6/6] bpf, arm64: Add JIT support for cpu time counter kfuncs
Date: Mon, 20 Apr 2026 11:44:10 +0100 [thread overview]
Message-ID: <86pl3t29ol.wl-maz@kernel.org> (raw)
In-Reply-To: <aeX9Alsyk1Cb2QLG@willie-the-truck>
On Mon, 20 Apr 2026 11:16:34 +0100,
Will Deacon <will@kernel.org> wrote:
>
> [+ Marc]
>
> On Sat, Apr 18, 2026 at 06:16:04AM -0700, Puranjay Mohan wrote:
> > Add ARM64 JIT inlining for bpf_get_cpu_time_counter() and
> > bpf_cpu_time_counter_to_ns() kfuncs.
> >
> > bpf_get_cpu_time_counter() is JIT-inlined as:
> >
> > ISB // serialize instruction stream
> > MRS Xn, CNTVCT_EL0 // read architected timer counter
> >
> > The ISB before the MRS is required for ordering, matching the kernel's
> > arch_timer_read_cntvct_el0() implementation.
>
> Careful here: this will _not_ order counter accesses against normal
> memory barriers (e.g. smp_mb(), acquire/release). If you need that, then
> you need something like arch_counter_enforce_ordering(), which we do
> have in __arch_counter_get_cntvct().
>
> Furthermore, using the virtual counter may expose you to situations
> where a guest value for CNTVOFF is installed (e.g. during the VCPU run
> loop). Given all the contexts in which BPF can run, this worries me a
> little as you might end up seeing a non-monotonic view of time between
> BPF programs.
Yup, and this will catch you out in subtle ways depending on whether
the host runs at EL2 or not.
The kernel (mostly) avoids this pitfall by picking the physical
counter for sched_clock() when running at EL2, so that it isn't
directly affected by CNTVOFF_EL2.
I suspect that you'll need something similar if you insist on a
globally monotonic view of the counter.
>
> > On newer CPUs it will be JITed to:
> >
> > MRS Xn, CNTVCTSS_EL0 // self-synchronized (ISB not needed)
> >
> > bpf_cpu_time_counter_to_ns() is JIT-inlined using mult/shift constants
> > computed at JIT time from the architected timer frequency (CNTFRQ_EL0):
And what happens when CNTFRQ_EL0 is not populated, as it is the case
on a lot of stupidly integrated HW? The kernel has a fallback, but you
don't seem to make use of it here.
> >
> > MOV Xtmp, #mult // load conversion multiplier
> > MUL Xn, Xarg, Xtmp // delta_ticks * mult
> > LSR Xn, Xn, #shift // >> shift = nanoseconds
> >
> > On systems with a 1GHz counter (e.g., Neoverse-V2), mult=1 and shift=0,
> > so the conversion collapses to a single MOV (identity).
>
> Do you have any performance numbers to show that this is worthwhile
> compared to calling a helper wrapping __arch_counter_get_cntvct().
>
> > Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
> > ---
> > arch/arm64/include/asm/insn.h | 2 +
> > arch/arm64/net/bpf_jit.h | 4 ++
> > arch/arm64/net/bpf_jit_comp.c | 54 +++++++++++++++++++
> > .../selftests/bpf/progs/verifier_cpu_cycles.c | 50 ++++++++++++++++-
> > 4 files changed, 109 insertions(+), 1 deletion(-)
>
> Aren't you conveniently ignoring CPU errata here? I suspect this needs
> to be predicated on the absence of those, in a similar way to how the
> vDSO deals with the 'vsdo_clockmode'.
And I'm afraid this still is the common case...
M.
--
Without deviation from the norm, progress is not possible.
prev parent reply other threads:[~2026-04-20 10:44 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-18 13:15 [PATCH bpf-next v13 0/6] bpf: add cpu time counter kfuncs Puranjay Mohan
2026-04-18 13:15 ` [PATCH bpf-next v13 1/6] bpf: adjust BPF JIT dependency to BPF_SYSCALL Puranjay Mohan
2026-04-18 13:46 ` sashiko-bot
2026-04-18 13:16 ` [PATCH bpf-next v13 2/6] bpf: add bpf_get_cpu_time_counter kfunc Puranjay Mohan
2026-04-18 14:24 ` sashiko-bot
2026-04-18 13:16 ` [PATCH bpf-next v13 3/6] bpf: add bpf_cpu_time_counter_to_ns kfunc Puranjay Mohan
2026-04-18 14:03 ` bot+bpf-ci
2026-04-18 14:54 ` sashiko-bot
2026-04-18 13:16 ` [PATCH bpf-next v13 4/6] selftests/bpf: add selftest to check bpf_get_cpu_time_counter jit Puranjay Mohan
2026-04-18 15:08 ` sashiko-bot
2026-04-18 13:16 ` [PATCH bpf-next v13 5/6] selftests/bpf: add usage example for cpu time counter kfuncs Puranjay Mohan
2026-04-18 15:17 ` sashiko-bot
2026-04-18 13:16 ` [PATCH bpf-next v13 6/6] bpf, arm64: Add JIT support " Puranjay Mohan
2026-04-18 14:03 ` bot+bpf-ci
2026-04-18 16:06 ` sashiko-bot
2026-04-20 4:03 ` Xu Kuohai
2026-04-20 9:45 ` Puranjay Mohan
2026-04-20 10:16 ` Will Deacon
2026-04-20 10:44 ` Marc Zyngier [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86pl3t29ol.wl-maz@kernel.org \
--to=maz@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=catalin.marinas@arm.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=puranjay@kernel.org \
--cc=vadim.fedorenko@linux.dev \
--cc=will@kernel.org \
--cc=xukuohai@huaweicloud.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox