* [PATCH v3] perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations
@ 2026-12-06 1:00 Ian Rogers
2026-12-06 1:14 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2026-12-06 1:00 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
James Clark, Jiayuan Chen, Ihor Solodrai, bpf, linux-perf-users,
linux-kernel, bpf, Ian Rogers
When building perf with CC=gcc and bpf-gcc installed, commit
63bdd143c589 ("perf build: Support building BPF skeletons with bpf-gcc")
selects bpf-gcc -O2 by default for tools/perf BPF skeletons. Several
BPF skeletons fail to compile with GEN_VMLINUX_H=1 under bpf-gcc >= 15
or fail at runtime during libbpf relocation or kernel BPF verification:
1. -fivopts strength-reduces bounded array loops over BPF map values
and stack arrays (such as sample_filter.bpf.c and bperf_cgroup.bpf.c)
into PTR_TO_MAP_VALUE / PTR_TO_STACK end-pointer comparisons. In
kernel/bpf/verifier.c, is_branch_taken() returns unknown for
non-packet pointer comparisons, causing the verifier to explore one
iteration past the array end and reject the program with -EACCES.
Pass -fno-ivopts so bpf-gcc preserves scalar loop induction variables.
2. -fipa-sra and -fpartial-inlining outline cold tails of static BPF
functions into .isra/.part subprogram clones (such as
task_traced.part.0 in augmented_raw_syscalls.bpf.c and do_exit.part.0
in syscall_summary.bpf.c) without .BTF.ext func_info entries, causing
libbpf call relocation to fail with -ENOENT. Pass -fno-ipa-sra and
-fno-partial-inlining.
3. -ftree-loop-im and -fmove-loop-invariants treat CO-RE LDIMM64
relocation builtins as pure loop-invariant constants and hoist them
out of bpf_core_field_exists() conditional blocks into enclosing loop
preheaders (such as cgrp_old->ancestor_ids in bperf_cgroup.bpf.c).
When the guarded field is absent on the running kernel, libbpf
poisons the hoisted LDIMM64 with call 195896080 on an unconditionally
reachable path. Pass -fno-tree-loop-im and -fno-move-loop-invariants.
4. In sample_filter.bpf.c, accessing members inside the anonymous inner
structs of union perf_sample_weight and union perf_mem_data_src
through preserve_access_index kctx->data causes bpf-gcc to emit CO-RE
relocations rooted at anonymous struct types, which libbpf rejects
with -EINVAL. Read the named union words (.full and .val) into local
union variables before extracting the subfields, and drop the
redundant __builtin_preserve_field_info(data->mem_hops,
BPF_FIELD_EXISTS) check (mem_hops landed in v5.16, whereas
perf_sample_filter() already requires data->sample_flags from v6.1
and bpf_cast_to_kern_ctx() from v6.2).
5. In lock_contention.bpf.c, collect_zone_lock() and collect_lock_syms()
use C offsetof() on preserve_access_index structs (pglist_data, zone,
rq___new, and rq___old), which bpf-gcc evaluates to compile-time
offsets without emitting CO-RE field offset relocations (computing
offset 0 for struct rq___new.__lock). Use the 1-argument
bpf_core_field_offset(ptr->field) and bpf_core_field_exists(ptr->field)
forms via typed NULL pointers so bpf-gcc emits BTF CO-RE relocations.
6. -fif-conversion, -fif-conversion2, -fssa-phiopt, -fschedule-insns,
and -fschedule-insns2 convert pointer NULL checks into branchless
(-ptr | ptr) >> 63 pointer ALU (rejected with "pointer |= pointer
prohibited") or schedule non-trapping pointer offsets (such as
rq + lock_off in collect_lock_syms) ahead of their NULL or bounds
checks (rejected with "pointer arithmetic on ptr_or_null_
prohibited"). Disable those passes for bpf-gcc builds.
7. GCC 15 and newer default to C23 (-std=gnu23), where bool, true, and
false are keywords. When building perf with GEN_VMLINUX_H=1 and
bpf-gcc >= 15, vmlinux.h dumped from kernel BTF emits
'enum { false = 0, true = 1 };' and 'typedef _Bool bool;', causing
compilation to fail. Pass -std=gnu11 when invoking $(BPF_CC) in
tools/perf/bpf_skel.mak (before $(BPF_OPTIONS) and $(EXTRA_BPF_FLAGS)
so callers can still override it), matching the kernel build and
tools/testing/selftests/bpf/Makefile.buildvars.
Fixes: 63bdd143c589 ("perf build: Support building BPF skeletons with bpf-gcc")
Signed-off-by: Ian Rogers <irogers@google.com>
---
Changes in v3:
- Fold in -std=gnu11 for $(BPF_CC) in tools/perf/bpf_skel.mak to fix
GEN_VMLINUX_H=1 builds with bpf-gcc >= 15 (which defaults to C23 where
bool/true/false are keywords).
- Clarify item 1 to cite both map-value (sample_filter.bpf.c) and stack
(bperf_cgroup.bpf.c) bounded array loops (Sashiko).
- Cc Jiayuan Chen, Ihor Solodrai, and bpf@gcc.gnu.org from the v2
discussion.
- v2: https://lore.kernel.org/linux-perf-users/20261204011619.1955782-1-irogers@google.com/
Changes in v2:
- Drop the redundant __builtin_preserve_field_info(data->mem_hops,
BPF_FIELD_EXISTS) check for both compilers instead of guarding it with
#ifdef __clang__, and document why in the commit message (Sashiko).
- Drop the redundant barrier_var(rq) in collect_lock_syms(), already
handled by -fno-schedule-insns{,2} (Sashiko).
- v1: https://lore.kernel.org/linux-perf-users/20261203221459.1567050-1-irogers@google.com/
tools/perf/bpf_skel.mak | 22 ++++++++-
.../perf/util/bpf_skel/lock_contention.bpf.c | 14 ++++--
tools/perf/util/bpf_skel/sample_filter.bpf.c | 49 +++++++++++--------
3 files changed, 58 insertions(+), 27 deletions(-)
diff --git a/tools/perf/bpf_skel.mak b/tools/perf/bpf_skel.mak
index 7b7edf506509..ea158fb47b84 100644
--- a/tools/perf/bpf_skel.mak
+++ b/tools/perf/bpf_skel.mak
@@ -32,7 +32,25 @@ ifeq ($(CONFIG_PERF_BPF_SKEL_BPF_GCC),y)
QUIET_BPF_CC = $(QUIET_BPF_GCC)
IS_LITTLE_ENDIAN := $(shell $(CC) -dM -E - </dev/null | \
grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
- BPF_OPTIONS = -Wall -mcpu=v3 -fno-ident -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes \
+ # Disable GCC passes that emit BPF code rejected by libbpf or the verifier:
+ # - -fno-ivopts: keep scalar loop counters instead of pointer-equality
+ # latches (PTR_TO_MAP_VALUE / PTR_TO_STACK) that is_branch_taken() cannot
+ # bound.
+ # - -fno-ipa-sra -fno-partial-inlining: avoid .isra/.part subprogram clones
+ # that omit .BTF.ext func_info records.
+ # - -fno-tree-loop-im -fno-move-loop-invariants: keep CO-RE relocations
+ # inside bpf_core_field_exists() guards instead of hoisting them into loop
+ # preheaders where libbpf poisons missing-field relocations.
+ # - -fno-if-conversion -fno-if-conversion2 -fno-ssa-phiopt
+ # -fno-schedule-insns -fno-schedule-insns2: prevent converting pointer
+ # NULL checks into branchless (-ptr | ptr) >> 63 pointer ALU or hoisting
+ # pointer arithmetic above NULL / bounds checks.
+ BPF_OPTIONS = -Wall -mcpu=v3 -fno-ident \
+ -fno-ivopts -fno-ipa-sra -fno-partial-inlining \
+ -fno-tree-loop-im -fno-move-loop-invariants \
+ -fno-if-conversion -fno-if-conversion2 -fno-ssa-phiopt \
+ -fno-schedule-insns -fno-schedule-insns2 \
+ -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes \
-D__TARGET_ARCH_$(SRCARCH) $(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
BPF_SYS_INCLUDES = $(call get_sys_includes,$(CC),)
else
@@ -98,7 +116,7 @@ endif
$(SKEL_TMP_OUT)/%.bpf.o: $(srctree)/tools/perf/$(dir)/bpf_skel/%.bpf.c $(LIBBPF) $(VMLINUX_H) $(OUTPUT)PERF-VERSION-FILE util/bpf_skel/perf_version.h
$(call rule_mkdir)
$(QUIET_BPF_CC)
- $(Q)$(BPF_CC) -g -O2 \
+ $(Q)$(BPF_CC) -g -O2 -std=gnu11 \
$(BPF_OPTIONS) $(EXTRA_BPF_FLAGS) $(BPF_INCLUDE) $(TOOLS_UAPI_INCLUDE) \
-include $(OUTPUT)PERF-VERSION-FILE -include util/bpf_skel/perf_version.h \
-fms-extensions \
diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c
index 0d9c6f55050e..03456e292f31 100644
--- a/tools/perf/util/bpf_skel/lock_contention.bpf.c
+++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c
@@ -925,12 +925,14 @@ struct rq___new {
static void collect_zone_lock(void)
{
+ struct pglist_data *pgd_null = NULL;
+ struct zone *zone_null = NULL;
__u64 nr_zones, zone_off;
__u64 lock_addr, lock_off;
__u32 lock_flag = LOCK_CLASS_ZONE_LOCK;
- zone_off = offsetof(struct pglist_data, node_zones);
- lock_off = offsetof(struct zone, lock);
+ zone_off = bpf_core_field_offset(pgd_null->node_zones);
+ lock_off = bpf_core_field_offset(zone_null->lock);
if (contig_page_data_addr) {
struct pglist_data *contig_page_data;
@@ -979,13 +981,15 @@ static void collect_zone_lock(void)
SEC("raw_tp/bpf_test_finish")
int BPF_PROG(collect_lock_syms)
{
+ struct rq___new *rq_new = NULL;
+ struct rq___old *rq_old = NULL;
__u64 lock_addr, lock_off;
__u32 lock_flag;
- if (bpf_core_field_exists(struct rq___new, __lock))
- lock_off = offsetof(struct rq___new, __lock);
+ if (bpf_core_field_exists(rq_new->__lock))
+ lock_off = bpf_core_field_offset(rq_new->__lock);
else
- lock_off = offsetof(struct rq___old, lock);
+ lock_off = bpf_core_field_offset(rq_old->lock);
for (int i = 0; i < MAX_CPUS; i++) {
struct rq *rq = bpf_per_cpu_ptr(&runqueues, i);
diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c
index e5666d4c1722..cc77eb20503e 100644
--- a/tools/perf/util/bpf_skel/sample_filter.bpf.c
+++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c
@@ -124,14 +124,19 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
return kctx->data->period;
case PBF_TERM_TRANSACTION:
return kctx->data->txn;
- case PBF_TERM_WEIGHT_STRUCT:
+ case PBF_TERM_WEIGHT_STRUCT: {
+ union perf_sample_weight weight = {
+ .full = kctx->data->weight.full,
+ };
+
if (entry->part == 1)
- return kctx->data->weight.var1_dw;
+ return weight.var1_dw;
if (entry->part == 2)
- return kctx->data->weight.var2_w;
+ return weight.var2_w;
if (entry->part == 3)
- return kctx->data->weight.var3_w;
- /* fall through */
+ return weight.var3_w;
+ return weight.full;
+ }
case PBF_TERM_WEIGHT:
return kctx->data->weight.full;
case PBF_TERM_PHYS_ADDR:
@@ -142,35 +147,39 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
return kctx->data->code_page_size;
case PBF_TERM_DATA_PAGE_SIZE:
return kctx->data->data_page_size;
- case PBF_TERM_DATA_SRC:
+ case PBF_TERM_DATA_SRC: {
+ union perf_mem_data_src src = {
+ .val = kctx->data->data_src.val,
+ };
+
if (entry->part == 1)
- return kctx->data->data_src.mem_op;
+ return src.mem_op;
if (entry->part == 2)
- return kctx->data->data_src.mem_lvl_num;
+ return src.mem_lvl_num;
if (entry->part == 3) {
- __u32 snoop = kctx->data->data_src.mem_snoop;
- __u32 snoopx = kctx->data->data_src.mem_snoopx;
+ __u32 snoop = src.mem_snoop;
+ __u32 snoopx = src.mem_snoopx;
return (snoopx << 5) | snoop;
}
if (entry->part == 4)
- return kctx->data->data_src.mem_remote;
+ return src.mem_remote;
if (entry->part == 5)
- return kctx->data->data_src.mem_lock;
+ return src.mem_lock;
if (entry->part == 6)
- return kctx->data->data_src.mem_dtlb;
+ return src.mem_dtlb;
if (entry->part == 7)
- return kctx->data->data_src.mem_blk;
+ return src.mem_blk;
if (entry->part == 8) {
- union perf_mem_data_src___new *data = (void *)&kctx->data->data_src;
-
- if (__builtin_preserve_field_info(data->mem_hops, BPF_FIELD_EXISTS))
- return data->mem_hops;
+ union perf_mem_data_src___new src_new = {
+ .val = src.val,
+ };
- return 0;
+ return src_new.mem_hops;
}
/* return the whole word */
- return kctx->data->data_src.val;
+ return src.val;
+ }
case PBF_TERM_UID:
return bpf_get_current_uid_gid() & 0xFFFFFFFF;
case PBF_TERM_GID:
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v3] perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations
2026-12-06 1:00 [PATCH v3] perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations Ian Rogers
@ 2026-12-06 1:14 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06 1:14 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] perf: unresolved maintainer objection to ten bpf-gcc workaround
flags
--
commit 01cc9ea53169f37881bab27710483ce98ca56d3e
Author: Ian Rogers <irogers@google.com>
perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations
This commit disables ten GCC optimization passes and adds -std=gnu11 for
bpf-gcc builds in tools/perf/bpf_skel.mak, and updates lock_contention.bpf.c
and sample_filter.bpf.c to emit CO-RE relocations accepted by libbpf and the
kernel verifier.
> diff --git a/tools/perf/bpf_skel.mak b/tools/perf/bpf_skel.mak
> index 7b7edf5065091..ea158fb47b848 100644
> --- a/tools/perf/bpf_skel.mak
> +++ b/tools/perf/bpf_skel.mak
> @@ -32,7 +32,25 @@ ifeq ($(CONFIG_PERF_BPF_SKEL_BPF_GCC),y)
> QUIET_BPF_CC = $(QUIET_BPF_GCC)
> IS_LITTLE_ENDIAN := $(shell $(CC) -dM -E - </dev/null | \
> grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
> - BPF_OPTIONS = -Wall -mcpu=v3 -fno-ident -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes \
> + # Disable GCC passes that emit BPF code rejected by libbpf or the verifier:
> + # - -fno-ivopts: keep scalar loop counters instead of pointer-equality
> + # latches (PTR_TO_MAP_VALUE / PTR_TO_STACK) that is_branch_taken() cannot
> + # bound.
> + # - -fno-ipa-sra -fno-partial-inlining: avoid .isra/.part subprogram clones
> + # that omit .BTF.ext func_info records.
> + # - -fno-tree-loop-im -fno-move-loop-invariants: keep CO-RE relocations
> + # inside bpf_core_field_exists() guards instead of hoisting them into loop
> + # preheaders where libbpf poisons missing-field relocations.
> + # - -fno-if-conversion -fno-if-conversion2 -fno-ssa-phiopt
> + # -fno-schedule-insns -fno-schedule-insns2: prevent converting pointer
> + # NULL checks into branchless (-ptr | ptr) >> 63 pointer ALU or hoisting
> + # pointer arithmetic above NULL / bounds checks.
> + BPF_OPTIONS = -Wall -mcpu=v3 -fno-ident \
> + -fno-ivopts -fno-ipa-sra -fno-partial-inlining \
> + -fno-tree-loop-im -fno-move-loop-invariants \
> + -fno-if-conversion -fno-if-conversion2 -fno-ssa-phiopt \
> + -fno-schedule-insns -fno-schedule-insns2 \
> + -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes \
> -D__TARGET_ARCH_$(SRCARCH) $(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
> BPF_SYS_INCLUDES = $(call get_sys_includes,$(CC),)
> else
[Severity: Low]
As reported by Namhyung Kim on v2 [1], carrying ten GCC optimization-pass
disabling flags in BPF_OPTIONS raises maintainability concerns while
tools/perf/Makefile.config still keeps bpf-gcc preferred by default on GCC
builds (prefer_bpf_gcc := $(CC_NO_CLANG)).
Unlike test_progs-bpf_gcc in tools/testing/selftests/bpf/Makefile, which
treats bpf-gcc as an optional flavor without global pass-disabling flags,
this disables ivopts, ipa-sra, partial-inlining, loop invariant motion,
if-conversion, phiopt, and instruction scheduling for all perf skeletons.
Should bpf-gcc be made opt-in until GCC fixes these codegen issues instead
of carrying these flags?
[1] https://lore.kernel.org/r/axO9dKDMajQkON36@google.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261206010021.2830750-1-irogers@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 1:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 1:00 [PATCH v3] perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations Ian Rogers
2026-12-06 1:14 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox