perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations
Subsystems (2)
Ian Rogers
reviewerPATCH 1/1 perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations
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 compile with bpf-gcc -O2 yet fail at runtime during libbpf relocation or kernel BPF verification:
1. -fivopts strength-reduces bounded array loops over BPF map values (such as bperf_cgroup.bpf.c) into PTR_TO_MAP_VALUE != PTR_TO_MAP_VALUE 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 map value 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.
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.
--- tools/perf/bpf_skel.mak | 19 ++++++- .../perf/util/bpf_skel/lock_contention.bpf.c | 15 ++++-- tools/perf/util/bpf_skel/sample_filter.bpf.c | 52 ++++++++++++------- 3 files changed, 61 insertions(+), 25 deletions(-)
tools/perf/bpf_skel.mak +18 -1
@@ -32,7 +32,24 @@ 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 PTR_TO_MAP_VALUE + # pointer-equality latches 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
tools/perf/util/bpf_skel/lock_contention.bpf.c +10 -5
@@ -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,19 +981,22 @@ 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);if (rq ==NULL )break ; +barrier_var (rq); lock_addr = (__u64 )(void *)rq + lock_off; lock_flag =LOCK_CLASS_RQLOCK ;
tools/perf/util/bpf_skel/sample_filter.bpf.c +33 -19
82 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
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 fortools/perfBPF skeletons. Several BPF skeletons compile with bpf-gcc -O2 yet fail at runtime during libbpf relocation or kernel BPF verification:1. -fivopts strength-reduces bounded array loops over BPF map values (such as
bperf_cgroup.bpf.c) intoPTR_TO_MAP_VALUE!=PTR_TO_MAP_VALUEend-pointer comparisons. Inkernel/bpf/verifier.c,is_branch_taken()returns unknown for non-packet pointer comparisons, causing the verifier to explore one iteration past the map value end and reject the program with-EACCES. Pass -fno-ivopts so bpf-gcc preserves scalar loop induction variables.
Quoted 7 lines
2. -fipa-sra and -fpartial-inlining outline cold tails of static BPF [ ... ]
checks (rejected with "pointer arithmetic onptr_or_null_prohibited"). Disable those passes for bpf-gcc builds.Fixes 63bdd143c589 ("perf build: Support building BPF skeletons with bpf-gcc")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1