From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796336103; b=cGjkC7Vik2gFe1XO+5pZumxDiAUzHXKxokUcdP7NAQ1LwGV9FaUALRg6awAtWw+iaxXt Qb8J1rLYPrI0NcHwSI4ZVdf8LMRRzfl+daPMIpLNyiddoVBGLiLKzMk/EU7hB4sXsf/QL owlYtg2IVrdWL8F98QUXObXajBkXrcXG1rfafxzx3T4IIxZA5Q1Ps88TgasI3xZEnzNtq UFEX1176N0AiwwjZDa7OjC6BvI5MMExmWYlusPIe5M2ZQIAKf+963tOtmeMXSE6gcHcz2 F5+8mfrb76XgvFM4/msEh7EX8tNzks2g7DTsr/7R5IhNU636KcsOKFaQ44DjHn+wkFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796336103; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=KK8bGuZlVUjrTdnM0LaR9YKqgkqqCPxiX+z3HPfsoA4=; b=H8cLaIUZH6IajrTnhILgxbx2EGKQE7+J93tE7OmCx9KjJsUS1wls3lOCzwvv5oxAiQP2 U1maXk+EPCRILk0HD/p+sAvds3aBh4SH6t917ZqN1eZv3fVyNt8WjD5aTqNdnId9h9y7t pSDPzFR6X0KcMxumn5PTQ2x9eSnUu+BkvnoSdYb/hbXSJflMLFA9YrulycOT6Dlyy921M lgbXdaq2dIozVSD3p5uMOyuZOOmjaUr9P9WEmp+9NAxWXDeqVvNHEYYVwEMA/BmowjyCs YwT9Lra5jBRuPbf9hAZUKv6RfmNQbtEPrxmeHlQ8fqQ7d5G8pJYFkH7eEPEdeR8ka8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=U0JmeyuN; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A735A606A32F for ; Thu, 3 Dec 2026 22:15:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A735A606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=U0JmeyuN Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1b5d5fad69bso413175c88.1 for ; Thu, 03 Dec 2026 14:15:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796336102; x=1796940902; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=KK8bGuZlVUjrTdnM0LaR9YKqgkqqCPxiX+z3HPfsoA4=; b=U0JmeyuN22fozTkmChZyOVtISPPdLo+BSdiOScTX96Vx0JnjSlRA6ZfzklU9rqgPGz SP83iGU6O15T/gDYOilvluXfiNEdO+QlL+fXPmM3n0JbeQ9FdmazzY8nbO7+gFJhcgp4 R7pzQcukfJb0OyMh42qF2AKUV5IWuMFkGW8AA45+ZQINSPbLdEGy3wb9ucSLElEMK/5y BAJMl++xKGMVPXKmRC6DBoAwYLxK/iWn+zWQMMjB/4nu8nrpIKIbHTZ6ygTd9nU40jJG 5E/W4KtzHr5SY+fObh8WTMDI1cLWB0bv3CaUk+W1zTz9ZwftOECni1ZrcjgwzU7Ne2OW Z9vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796336102; x=1796940902; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KK8bGuZlVUjrTdnM0LaR9YKqgkqqCPxiX+z3HPfsoA4=; b=kwWJSyWvn9uXaG2tZwK/L0oqOHRv6fE9s8Pckn1pTY4ee0FPp7XMMphYppl5l2KQAR v0UFdM+oEXXSeHiZROqd4CoOr8QOdwpDWPpup45VI0RBGzRTN8EZu6Tm2mARp9Z27pzi lMhOvgPNih/fnkauQdfu+5uwWy1MfiH6vdcY8/GQvSJnKOJKwHLLpPGVdl6cO9UMVN3R 6mDPZWsXFzgGqPPLTwzeaOQ2NDNAkpbIsJsX2ifVpXzQwOlRaIpdxpFgq9gmufeXat9O Lf9hRiQxkpTjt1aOgX8H+8mlaBvic66Euojnsr3R+pEq2Wpj8qHg76Acs0KemiWg3vsz xP5A== X-Forwarded-Encrypted: i=1; AL+hErGlfB+bLa8siJitEII8EA3D5ydsjQBSRJrxj36Qloj7GAOcrybwsvlXHaADYZ3KTqS9XSxGL0aJLBl5XSdkGg9t@vger.kernel.org X-Gm-Message-State: APuU4Z+4D6oAIKLzLwbB4zFXOVRSrirG2hQ+gLFRbl1fbZwUn6xKaPEp 8LqPps25A59MUpYfgknpQAgVagm5jKQ7FesI+VXom9fuI0rNXVYZLYBRT2UW/PNiDE/wqY+R4zm Qh5GXAMQDAg== X-Received: from dly1-n2.prod.google.com ([2002:a05:701b:2041:20b0:1b5:717d:2654]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701a:c9d9:10b0:1b5:5fe5:78d1 with SMTP id a92af1059eb24-1b5d70c58dbmr216267c88.11.1796336101369; Thu, 03 Dec 2026 14:15:01 -0800 (PST) Date: Thu, 3 Dec 2026 14:14:59 -0800 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203221459.1567050-1-irogers@google.com> Subject: [PATCH] perf build: Fix bpf-gcc skeleton codegen and CO-RE relocations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Suchit Karunakaran , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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. Fixes: 63bdd143c589 ("perf build: Support building BPF skeletons with bpf-gcc") Signed-off-by: Ian Rogers --- 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(-) diff --git a/tools/perf/bpf_skel.mak b/tools/perf/bpf_skel.mak index 7b7edf506509..400e84c4fd34 100644 --- a/tools/perf/bpf_skel.mak +++ b/tools/perf/bpf_skel.mak @@ -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 - > 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 diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index 0d9c6f55050e..d4c4ddf0f979 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,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; diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c index e5666d4c1722..9ddac8e5cd78 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,44 @@ 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 src_new = { + .val = src.val, + }; +#ifdef __clang__ 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; - - return 0; + if (!__builtin_preserve_field_info(data->mem_hops, BPF_FIELD_EXISTS)) + return 0; +#endif + 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