[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAADnVQJKguqUD0bYh=tHnrGXOUOMrSk9_72HDFVcuMsv7=S_nQ@mail.gmail.com>
Date: Fri, 30 Jan 2026 12:23:44 -0800
From: Alexei Starovoitov <alexei.starovoitov@...il.com>
To: Changwoo Min <changwoo@...lia.com>
Cc: Alexei Starovoitov <ast@...nel.org>, Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, Shuah Khan <shuah@...nel.org>, kernel-dev@...lia.com,
bpf <bpf@...r.kernel.org>, sched-ext@...ts.linux.dev,
LKML <linux-kernel@...r.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@...r.kernel.org>
Subject: Re: [PATCH v2] selftests/bpf: Make x86 preempt_count access
compatible across v6.14+
On Thu, Jan 29, 2026 at 6:19 PM Changwoo Min <changwoo@...lia.com> wrote:
>
> Recent x86 kernels export __preempt_count as a ksym, while some old kernels
> between v6.1 and v6.14 expose the preemption counter via
> pcpu_hot.preempt_count. The existing selftest helper unconditionally
> dereferenced __preempt_count, which breaks BPF program loading on such old
> kernels.
>
> Make the x86 preemption count lookup version-agnostic by:
> - Marking __preempt_count and pcpu_hot as weak ksyms.
> - Introducing a BTF-described pcpu_hot___local layout with
> preserve_access_index.
> - Selecting the appropriate access path at runtime using ksym availability
> and bpf_ksym_exists() and bpf_core_field_exists().
>
> This allows a single BPF binary to run correctly across kernel versions
> (e.g., v6.18 vs. v6.13) without relying on compile-time version checks.
>
> Signed-off-by: Changwoo Min <changwoo@...lia.com>
> ---
> .../testing/selftests/bpf/bpf_experimental.h | 21 +++++++++++++++++--
> 1 file changed, 19 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
> index a39576c8ba04..acabf869d2c4 100644
> --- a/tools/testing/selftests/bpf/bpf_experimental.h
> +++ b/tools/testing/selftests/bpf/bpf_experimental.h
> @@ -614,7 +614,13 @@ extern int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
>
> extern bool CONFIG_PREEMPT_RT __kconfig __weak;
> #ifdef bpf_target_x86
> -extern const int __preempt_count __ksym;
> +extern const int __preempt_count __ksym __weak;
> +
> +struct pcpu_hot___local {
> + int preempt_count;
> +} __attribute__((preserve_access_index));
> +
> +extern struct pcpu_hot___local pcpu_hot __ksym __weak;
> #endif
>
> struct task_struct___preempt_rt {
> @@ -624,7 +630,18 @@ struct task_struct___preempt_rt {
> static inline int get_preempt_count(void)
> {
> #if defined(bpf_target_x86)
> - return *(int *) bpf_this_cpu_ptr(&__preempt_count);
> + /* By default, read the per-CPU __preempt_count. */
> + if (bpf_ksym_exists(&__preempt_count))
> + return *(int *) bpf_this_cpu_ptr(&__preempt_count);
> +
> + /* If __preempt_count does not exist, try to read preempt_count under
Please use proper kernel comment style in the future.
We don't use old networking style in bpf anymore.
I fixed it up while applying.
Also use [PATCH bpf-next] in the subject to help CI identify the tree.
Powered by blists - more mailing lists