Skip to content

Commit a742258

Browse files
liu-song-6acmel
authored andcommitted
perf bpf: Synthesize bpf events with bpf_program__get_prog_info_linear()
With bpf_program__get_prog_info_linear, we can simplify the logic that synthesizes bpf events. This patch doesn't change the behavior of the code. Commiter notes: Needed this (for all four variables), suggested by Song, to overcome build failure on debian experimental cross building to MIPS 32-bit: - u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(info->prog_tags); + u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags); util/bpf-event.c: In function 'perf_event__synthesize_one_bpf_prog': util/bpf-event.c:143:35: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(info->prog_tags); ^ util/bpf-event.c:144:22: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] __u32 *prog_lens = (__u32 *)(info->jited_func_lens); ^ util/bpf-event.c:145:23: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] __u64 *prog_addrs = (__u64 *)(info->jited_ksyms); ^ util/bpf-event.c:146:22: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] void *func_infos = (void *)(info->func_info); ^ cc1: all warnings being treated as errors Signed-off-by: Song Liu <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: [email protected] Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stanislav Fomichev <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent cae73f2 commit a742258

File tree

1 file changed

+40
-78
lines changed

1 file changed

+40
-78
lines changed

tools/perf/util/bpf-event.c

Lines changed: 40 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <stdlib.h>
44
#include <bpf/bpf.h>
55
#include <bpf/btf.h>
6+
#include <bpf/libbpf.h>
67
#include <linux/btf.h>
8+
#include <linux/err.h>
79
#include "bpf-event.h"
810
#include "debug.h"
911
#include "symbol.h"
@@ -49,99 +51,62 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
4951
{
5052
struct ksymbol_event *ksymbol_event = &event->ksymbol_event;
5153
struct bpf_event *bpf_event = &event->bpf_event;
52-
u32 sub_prog_cnt, i, func_info_rec_size = 0;
53-
u8 (*prog_tags)[BPF_TAG_SIZE] = NULL;
54-
struct bpf_prog_info info = { .type = 0, };
55-
u32 info_len = sizeof(info);
56-
void *func_infos = NULL;
57-
u64 *prog_addrs = NULL;
54+
struct bpf_prog_info_linear *info_linear;
55+
struct bpf_prog_info *info;
5856
struct btf *btf = NULL;
59-
u32 *prog_lens = NULL;
6057
bool has_btf = false;
61-
char errbuf[512];
58+
u32 sub_prog_cnt, i;
6259
int err = 0;
60+
u64 arrays;
6361

64-
/* Call bpf_obj_get_info_by_fd() to get sizes of arrays */
65-
err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
62+
arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
63+
arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
64+
arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
65+
arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
6666

67-
if (err) {
68-
pr_debug("%s: failed to get BPF program info: %s, aborting\n",
69-
__func__, str_error_r(errno, errbuf, sizeof(errbuf)));
67+
info_linear = bpf_program__get_prog_info_linear(fd, arrays);
68+
if (IS_ERR_OR_NULL(info_linear)) {
69+
info_linear = NULL;
70+
pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
7071
return -1;
7172
}
72-
if (info_len < offsetof(struct bpf_prog_info, prog_tags)) {
73+
74+
if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
7375
pr_debug("%s: the kernel is too old, aborting\n", __func__);
7476
return -2;
7577
}
7678

79+
info = &info_linear->info;
80+
7781
/* number of ksyms, func_lengths, and tags should match */
78-
sub_prog_cnt = info.nr_jited_ksyms;
79-
if (sub_prog_cnt != info.nr_prog_tags ||
80-
sub_prog_cnt != info.nr_jited_func_lens)
82+
sub_prog_cnt = info->nr_jited_ksyms;
83+
if (sub_prog_cnt != info->nr_prog_tags ||
84+
sub_prog_cnt != info->nr_jited_func_lens)
8185
return -1;
8286

8387
/* check BTF func info support */
84-
if (info.btf_id && info.nr_func_info && info.func_info_rec_size) {
88+
if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
8589
/* btf func info number should be same as sub_prog_cnt */
86-
if (sub_prog_cnt != info.nr_func_info) {
90+
if (sub_prog_cnt != info->nr_func_info) {
8791
pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
88-
return -1;
89-
}
90-
if (btf__get_from_id(info.btf_id, &btf)) {
91-
pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info.btf_id);
92-
return -1;
92+
err = -1;
93+
goto out;
9394
}
94-
func_info_rec_size = info.func_info_rec_size;
95-
func_infos = calloc(sub_prog_cnt, func_info_rec_size);
96-
if (!func_infos) {
97-
pr_debug("%s: failed to allocate memory for func_infos, aborting\n", __func__);
98-
return -1;
95+
if (btf__get_from_id(info->btf_id, &btf)) {
96+
pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
97+
err = -1;
98+
btf = NULL;
99+
goto out;
99100
}
100101
has_btf = true;
101102
}
102103

103-
/*
104-
* We need address, length, and tag for each sub program.
105-
* Allocate memory and call bpf_obj_get_info_by_fd() again
106-
*/
107-
prog_addrs = calloc(sub_prog_cnt, sizeof(u64));
108-
if (!prog_addrs) {
109-
pr_debug("%s: failed to allocate memory for prog_addrs, aborting\n", __func__);
110-
goto out;
111-
}
112-
prog_lens = calloc(sub_prog_cnt, sizeof(u32));
113-
if (!prog_lens) {
114-
pr_debug("%s: failed to allocate memory for prog_lens, aborting\n", __func__);
115-
goto out;
116-
}
117-
prog_tags = calloc(sub_prog_cnt, BPF_TAG_SIZE);
118-
if (!prog_tags) {
119-
pr_debug("%s: failed to allocate memory for prog_tags, aborting\n", __func__);
120-
goto out;
121-
}
122-
123-
memset(&info, 0, sizeof(info));
124-
info.nr_jited_ksyms = sub_prog_cnt;
125-
info.nr_jited_func_lens = sub_prog_cnt;
126-
info.nr_prog_tags = sub_prog_cnt;
127-
info.jited_ksyms = ptr_to_u64(prog_addrs);
128-
info.jited_func_lens = ptr_to_u64(prog_lens);
129-
info.prog_tags = ptr_to_u64(prog_tags);
130-
info_len = sizeof(info);
131-
if (has_btf) {
132-
info.nr_func_info = sub_prog_cnt;
133-
info.func_info_rec_size = func_info_rec_size;
134-
info.func_info = ptr_to_u64(func_infos);
135-
}
136-
137-
err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
138-
if (err) {
139-
pr_debug("%s: failed to get BPF program info, aborting\n", __func__);
140-
goto out;
141-
}
142-
143104
/* Synthesize PERF_RECORD_KSYMBOL */
144105
for (i = 0; i < sub_prog_cnt; i++) {
106+
u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
107+
__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
108+
__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
109+
void *func_infos = (void *)(uintptr_t)(info->func_info);
145110
const struct bpf_func_info *finfo;
146111
const char *short_name = NULL;
147112
const struct btf_type *t;
@@ -163,13 +128,13 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
163128
KSYM_NAME_LEN - name_len,
164129
prog_tags[i], BPF_TAG_SIZE);
165130
if (has_btf) {
166-
finfo = func_infos + i * info.func_info_rec_size;
131+
finfo = func_infos + i * info->func_info_rec_size;
167132
t = btf__type_by_id(btf, finfo->type_id);
168133
short_name = btf__name_by_offset(btf, t->name_off);
169134
} else if (i == 0 && sub_prog_cnt == 1) {
170135
/* no subprog */
171-
if (info.name[0])
172-
short_name = info.name;
136+
if (info->name[0])
137+
short_name = info->name;
173138
} else
174139
short_name = "F";
175140
if (short_name)
@@ -195,20 +160,17 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
195160
},
196161
.type = PERF_BPF_EVENT_PROG_LOAD,
197162
.flags = 0,
198-
.id = info.id,
163+
.id = info->id,
199164
};
200-
memcpy(bpf_event->tag, prog_tags[i], BPF_TAG_SIZE);
165+
memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
201166
memset((void *)event + event->header.size, 0, machine->id_hdr_size);
202167
event->header.size += machine->id_hdr_size;
203168
err = perf_tool__process_synth_event(tool, event,
204169
machine, process);
205170
}
206171

207172
out:
208-
free(prog_tags);
209-
free(prog_lens);
210-
free(prog_addrs);
211-
free(func_infos);
173+
free(info_linear);
212174
free(btf);
213175
return err ? -1 : 0;
214176
}

0 commit comments

Comments
 (0)