Skip to content

Commit 1f5829e

Browse files
solbjornqmonnet
authored andcommitted
bpftool: use a local copy of perf_event to fix accessing :: Bpf_cookie
When CONFIG_PERF_EVENTS is not set, struct perf_event remains empty. However, the structure is being used by bpftool indirectly via BTF. This leads to: skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event' return BPF_CORE_READ(event, bpf_cookie); ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~ ... skeleton/pid_iter.bpf.c:49:9: error: returning 'void' from a function with incompatible result type '__u64' (aka 'unsigned long long') return BPF_CORE_READ(event, bpf_cookie); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tools and samples can't use any CONFIG_ definitions, so the fields used there should always be present. Define struct perf_event___local with the `preserve_access_index` attribute inside the pid_iter BPF prog to allow compiling on any configs. CO-RE will substitute it with the real struct perf_event accesses later on. Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexander Lobakin <[email protected]> Signed-off-by: Quentin Monnet <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3073a60 commit 1f5829e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/skeleton/pid_iter.bpf.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ enum bpf_obj_type {
1515
BPF_OBJ_BTF,
1616
};
1717

18+
struct perf_event___local {
19+
u64 bpf_cookie;
20+
} __attribute__((preserve_access_index));
21+
1822
extern const void bpf_link_fops __ksym;
1923
extern const void bpf_map_fops __ksym;
2024
extern const void bpf_prog_fops __ksym;
@@ -41,8 +45,8 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
4145
/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
4246
static __u64 get_bpf_cookie(struct bpf_link *link)
4347
{
48+
struct perf_event___local *event;
4449
struct bpf_perf_link *perf_link;
45-
struct perf_event *event;
4650

4751
perf_link = container_of(link, struct bpf_perf_link, link);
4852
event = BPF_CORE_READ(perf_link, perf_file, private_data);

0 commit comments

Comments
 (0)