From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Borislav Petkov <bp@alien8.de>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Thomas Gleixner <tglx@linutronix.de>,
Yonghong Song <yonghong.song@linux.dev>,
Mykola Lysenko <mykolal@fb.com>,
x86@kernel.org, bpf@vger.kernel.org,
Martin KaFai Lau <martin.lau@linux.dev>
Subject: Re: [PATCH bpf-next v7 2/4] bpf: add bpf_cpu_cycles_to_ns helper
Date: Wed, 20 Nov 2024 05:39:43 -0800 [thread overview]
Message-ID: <47d9fb73-f665-4566-bf3e-e016469ea3e3@linux.dev> (raw)
In-Reply-To: <20241120084943.GB19989@noisy.programming.kicks-ass.net>
On 20/11/2024 00:49, Peter Zijlstra wrote:
> On Tue, Nov 19, 2024 at 06:38:51AM -0800, Vadim Fedorenko wrote:
>> On 19/11/2024 03:28, Peter Zijlstra wrote:
>>> On Mon, Nov 18, 2024 at 10:52:43AM -0800, Vadim Fedorenko wrote:
>>>
>>>> + if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
>>>> + imm32 == BPF_CALL_IMM(bpf_cpu_cycles_to_ns) &&
>>>> + cpu_feature_enabled(X86_FEATURE_CONSTANT_TSC)) {
>>>> + u32 mult, shift;
>>>> +
>>>> + clocks_calc_mult_shift(&mult, &shift, tsc_khz, USEC_PER_SEC, 0);
>>>> + /* imul RAX, RDI, mult */
>>>> + maybe_emit_mod(&prog, BPF_REG_1, BPF_REG_0, true);
>>>> + EMIT2_off32(0x69, add_2reg(0xC0, BPF_REG_1, BPF_REG_0),
>>>> + mult);
>>>> +
>>>> + /* shr RAX, shift (which is less than 64) */
>>>> + maybe_emit_1mod(&prog, BPF_REG_0, true);
>>>> + EMIT3(0xC1, add_1reg(0xE8, BPF_REG_0), shift);
>>>> +
>>>> + break;
>>>> + }
>>>
>>> This is ludicrously horrible. Why are you using your own mult/shift and
>>> not offset here instead of using the one from either sched_clock or
>>> clocksource_tsc ?
>>
>> With X86_FEATURE_CONSTANT_TSC, tsc_khz is actually constant after
>> switching from tsc_early. And the very same call to
>> clocks_calc_mult_shift() is used to create clocksource_tsc mult and
>> shift constants. Unfortunately, clocksources don't have proper API to
>> get the underlying info, that's why I have to calculate shift and mult
>> values on my own.
>
> There is cyc2ns_read_begin() / cyc2ns_read_end(), and you can use the
> VDSO thing you do below.
Looks like I missed arch-specific implementation. Thanks, I'll use it in
the next version.
>>> And being totally inconsistent with your own alternative implementation
>>> which uses the VDSO, which in turn uses clocksource_tsc:
>>
>> With what I said above it is consistent with clocksource_tsc.
>>
>>>
>>>> +__bpf_kfunc u64 bpf_cpu_cycles_to_ns(u64 cycles)
>>>> +{
>>>> + const struct vdso_data *vd = __arch_get_k_vdso_data();
>>>> +
>>>> + vd = &vd[CS_RAW];
>>>> + /* kfunc implementation does less manipulations than vDSO
>>>> + * implementation. BPF use-case assumes two measurements are close
>>>> + * in time and can simplify the logic.
>>>> + */
>>>> + return mul_u64_u32_shr(cycles, vd->mult, vd->shift);
>>>> +}
>>>
>>> Also, if I'm not mistaken, the above is broken, you really should add
>>> the offset, without it I don't think we guarantee the result is
>>> monotonic.
>>
>> Not quite sure how constant offset can affect monotonic guarantee of
>> cycles, given that the main use case will be to calculate ns out of
>> small deltas?
>
> Well, when I read this patch I didn't know, because your changelogs
> don't mention anything at all.
Fair, I'll improve commit message in v8, thanks.
next prev parent reply other threads:[~2024-11-20 13:39 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-18 18:52 [PATCH bpf-next v7 0/4] bpf: add cpu cycles kfuncss Vadim Fedorenko
2024-11-18 18:52 ` [PATCH bpf-next v7 1/4] bpf: add bpf_get_cpu_cycles kfunc Vadim Fedorenko
2024-11-19 11:18 ` Peter Zijlstra
2024-11-19 14:29 ` Vadim Fedorenko
2024-11-19 16:17 ` Peter Zijlstra
2024-11-19 18:03 ` Vadim Fedorenko
2024-11-19 19:16 ` Andrii Nakryiko
2024-11-19 19:27 ` Vadim Fedorenko
2024-11-18 18:52 ` [PATCH bpf-next v7 2/4] bpf: add bpf_cpu_cycles_to_ns helper Vadim Fedorenko
2024-11-19 11:28 ` Peter Zijlstra
2024-11-19 14:38 ` Vadim Fedorenko
2024-11-20 8:49 ` Peter Zijlstra
2024-11-20 13:39 ` Vadim Fedorenko [this message]
2024-11-18 18:52 ` [PATCH bpf-next v7 3/4] selftests/bpf: add selftest to check rdtsc jit Vadim Fedorenko
2024-11-18 18:52 ` [PATCH bpf-next v7 4/4] selftests/bpf: add usage example for cpu cycles kfuncs Vadim Fedorenko
2024-11-19 11:47 ` Peter Zijlstra
2024-11-19 14:45 ` Vadim Fedorenko
2024-11-20 8:51 ` Peter Zijlstra
2024-11-20 17:19 ` Vadim Fedorenko
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=47d9fb73-f665-4566-bf3e-e016469ea3e3@linux.dev \
--to=vadim.fedorenko@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=yonghong.song@linux.dev \
/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