1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <bpf/bpf.h>
5 #include <bpf/btf.h>
6 #include <bpf/libbpf.h>
7 #include <linux/btf.h>
8 #include <linux/err.h>
9 #include "bpf-event.h"
10 #include "debug.h"
11 #include "dso.h"
12 #include "symbol.h"
13 #include "machine.h"
14 #include "env.h"
15 #include "session.h"
16 #include "map.h"
17 #include "evlist.h"
18 #include "record.h"
19 #include "util/synthetic-events.h"
20
21 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
22
snprintf_hex(char * buf,size_t size,unsigned char * data,size_t len)23 static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
24 {
25 int ret = 0;
26 size_t i;
27
28 for (i = 0; i < len; i++)
29 ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
30 return ret;
31 }
32
machine__process_bpf_event_load(struct machine * machine,union perf_event * event,struct perf_sample * sample __maybe_unused)33 static int machine__process_bpf_event_load(struct machine *machine,
34 union perf_event *event,
35 struct perf_sample *sample __maybe_unused)
36 {
37 struct bpf_prog_info_linear *info_linear;
38 struct bpf_prog_info_node *info_node;
39 struct perf_env *env = machine->env;
40 int id = event->bpf.id;
41 unsigned int i;
42
43 /* perf-record, no need to handle bpf-event */
44 if (env == NULL)
45 return 0;
46
47 info_node = perf_env__find_bpf_prog_info(env, id);
48 if (!info_node)
49 return 0;
50 info_linear = info_node->info_linear;
51
52 for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
53 u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
54 u64 addr = addrs[i];
55 struct map *map;
56
57 map = map_groups__find(&machine->kmaps, addr);
58
59 if (map) {
60 map->dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
61 map->dso->bpf_prog.id = id;
62 map->dso->bpf_prog.sub_id = i;
63 map->dso->bpf_prog.env = env;
64 }
65 }
66 return 0;
67 }
68
machine__process_bpf(struct machine * machine,union perf_event * event,struct perf_sample * sample)69 int machine__process_bpf(struct machine *machine, union perf_event *event,
70 struct perf_sample *sample)
71 {
72 if (dump_trace)
73 perf_event__fprintf_bpf(event, stdout);
74
75 switch (event->bpf.type) {
76 case PERF_BPF_EVENT_PROG_LOAD:
77 return machine__process_bpf_event_load(machine, event, sample);
78
79 case PERF_BPF_EVENT_PROG_UNLOAD:
80 /*
81 * Do not free bpf_prog_info and btf of the program here,
82 * as annotation still need them. They will be freed at
83 * the end of the session.
84 */
85 break;
86 default:
87 pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
88 break;
89 }
90 return 0;
91 }
92
perf_env__fetch_btf(struct perf_env * env,u32 btf_id,struct btf * btf)93 static int perf_env__fetch_btf(struct perf_env *env,
94 u32 btf_id,
95 struct btf *btf)
96 {
97 struct btf_node *node;
98 u32 data_size;
99 const void *data;
100
101 data = btf__get_raw_data(btf, &data_size);
102
103 node = malloc(data_size + sizeof(struct btf_node));
104 if (!node)
105 return -1;
106
107 node->id = btf_id;
108 node->data_size = data_size;
109 memcpy(node->data, data, data_size);
110
111 if (!perf_env__insert_btf(env, node)) {
112 /* Insertion failed because of a duplicate. */
113 free(node);
114 return -1;
115 }
116 return 0;
117 }
118
synthesize_bpf_prog_name(char * buf,int size,struct bpf_prog_info * info,struct btf * btf,u32 sub_id)119 static int synthesize_bpf_prog_name(char *buf, int size,
120 struct bpf_prog_info *info,
121 struct btf *btf,
122 u32 sub_id)
123 {
124 u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
125 void *func_infos = (void *)(uintptr_t)(info->func_info);
126 u32 sub_prog_cnt = info->nr_jited_ksyms;
127 const struct bpf_func_info *finfo;
128 const char *short_name = NULL;
129 const struct btf_type *t;
130 int name_len;
131
132 name_len = snprintf(buf, size, "bpf_prog_");
133 name_len += snprintf_hex(buf + name_len, size - name_len,
134 prog_tags[sub_id], BPF_TAG_SIZE);
135 if (btf) {
136 finfo = func_infos + sub_id * info->func_info_rec_size;
137 t = btf__type_by_id(btf, finfo->type_id);
138 short_name = btf__name_by_offset(btf, t->name_off);
139 } else if (sub_id == 0 && sub_prog_cnt == 1) {
140 /* no subprog */
141 if (info->name[0])
142 short_name = info->name;
143 } else
144 short_name = "F";
145 if (short_name)
146 name_len += snprintf(buf + name_len, size - name_len,
147 "_%s", short_name);
148 return name_len;
149 }
150
151 /*
152 * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
153 * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
154 * one PERF_RECORD_KSYMBOL is generated for each sub program.
155 *
156 * Returns:
157 * 0 for success;
158 * -1 for failures;
159 * -2 for lack of kernel support.
160 */
perf_event__synthesize_one_bpf_prog(struct perf_session * session,perf_event__handler_t process,struct machine * machine,int fd,union perf_event * event,struct record_opts * opts)161 static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
162 perf_event__handler_t process,
163 struct machine *machine,
164 int fd,
165 union perf_event *event,
166 struct record_opts *opts)
167 {
168 struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;
169 struct perf_record_bpf_event *bpf_event = &event->bpf;
170 struct bpf_prog_info_linear *info_linear;
171 struct perf_tool *tool = session->tool;
172 struct bpf_prog_info_node *info_node;
173 struct bpf_prog_info *info;
174 struct btf *btf = NULL;
175 struct perf_env *env;
176 u32 sub_prog_cnt, i;
177 int err = 0;
178 u64 arrays;
179
180 /*
181 * for perf-record and perf-report use header.env;
182 * otherwise, use global perf_env.
183 */
184 env = session->data ? &session->header.env : &perf_env;
185
186 arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
187 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
188 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
189 arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
190 arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
191 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
192 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
193
194 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
195 if (IS_ERR_OR_NULL(info_linear)) {
196 info_linear = NULL;
197 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
198 return -1;
199 }
200
201 if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
202 pr_debug("%s: the kernel is too old, aborting\n", __func__);
203 return -2;
204 }
205
206 info = &info_linear->info;
207
208 /* number of ksyms, func_lengths, and tags should match */
209 sub_prog_cnt = info->nr_jited_ksyms;
210 if (sub_prog_cnt != info->nr_prog_tags ||
211 sub_prog_cnt != info->nr_jited_func_lens)
212 return -1;
213
214 /* check BTF func info support */
215 if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
216 /* btf func info number should be same as sub_prog_cnt */
217 if (sub_prog_cnt != info->nr_func_info) {
218 pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
219 err = -1;
220 goto out;
221 }
222 if (btf__get_from_id(info->btf_id, &btf)) {
223 pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
224 err = -1;
225 btf = NULL;
226 goto out;
227 }
228 perf_env__fetch_btf(env, info->btf_id, btf);
229 }
230
231 /* Synthesize PERF_RECORD_KSYMBOL */
232 for (i = 0; i < sub_prog_cnt; i++) {
233 __u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
234 __u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
235 int name_len;
236
237 *ksymbol_event = (struct perf_record_ksymbol) {
238 .header = {
239 .type = PERF_RECORD_KSYMBOL,
240 .size = offsetof(struct perf_record_ksymbol, name),
241 },
242 .addr = prog_addrs[i],
243 .len = prog_lens[i],
244 .ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
245 .flags = 0,
246 };
247
248 name_len = synthesize_bpf_prog_name(ksymbol_event->name,
249 KSYM_NAME_LEN, info, btf, i);
250 ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
251 sizeof(u64));
252
253 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
254 event->header.size += machine->id_hdr_size;
255 err = perf_tool__process_synth_event(tool, event,
256 machine, process);
257 }
258
259 if (!opts->no_bpf_event) {
260 /* Synthesize PERF_RECORD_BPF_EVENT */
261 *bpf_event = (struct perf_record_bpf_event) {
262 .header = {
263 .type = PERF_RECORD_BPF_EVENT,
264 .size = sizeof(struct perf_record_bpf_event),
265 },
266 .type = PERF_BPF_EVENT_PROG_LOAD,
267 .flags = 0,
268 .id = info->id,
269 };
270 memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
271 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
272 event->header.size += machine->id_hdr_size;
273
274 /* save bpf_prog_info to env */
275 info_node = malloc(sizeof(struct bpf_prog_info_node));
276 if (!info_node) {
277 err = -1;
278 goto out;
279 }
280
281 info_node->info_linear = info_linear;
282 perf_env__insert_bpf_prog_info(env, info_node);
283 info_linear = NULL;
284
285 /*
286 * process after saving bpf_prog_info to env, so that
287 * required information is ready for look up
288 */
289 err = perf_tool__process_synth_event(tool, event,
290 machine, process);
291 }
292
293 out:
294 free(info_linear);
295 free(btf);
296 return err ? -1 : 0;
297 }
298
perf_event__synthesize_bpf_events(struct perf_session * session,perf_event__handler_t process,struct machine * machine,struct record_opts * opts)299 int perf_event__synthesize_bpf_events(struct perf_session *session,
300 perf_event__handler_t process,
301 struct machine *machine,
302 struct record_opts *opts)
303 {
304 union perf_event *event;
305 __u32 id = 0;
306 int err;
307 int fd;
308
309 event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size);
310 if (!event)
311 return -1;
312 while (true) {
313 err = bpf_prog_get_next_id(id, &id);
314 if (err) {
315 if (errno == ENOENT) {
316 err = 0;
317 break;
318 }
319 pr_debug("%s: can't get next program: %s%s\n",
320 __func__, strerror(errno),
321 errno == EINVAL ? " -- kernel too old?" : "");
322 /* don't report error on old kernel or EPERM */
323 err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
324 break;
325 }
326 fd = bpf_prog_get_fd_by_id(id);
327 if (fd < 0) {
328 pr_debug("%s: failed to get fd for prog_id %u\n",
329 __func__, id);
330 continue;
331 }
332
333 err = perf_event__synthesize_one_bpf_prog(session, process,
334 machine, fd,
335 event, opts);
336 close(fd);
337 if (err) {
338 /* do not return error for old kernel */
339 if (err == -2)
340 err = 0;
341 break;
342 }
343 }
344 free(event);
345 return err;
346 }
347
perf_env__add_bpf_info(struct perf_env * env,u32 id)348 static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
349 {
350 struct bpf_prog_info_linear *info_linear;
351 struct bpf_prog_info_node *info_node;
352 struct btf *btf = NULL;
353 u64 arrays;
354 u32 btf_id;
355 int fd;
356
357 fd = bpf_prog_get_fd_by_id(id);
358 if (fd < 0)
359 return;
360
361 arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
362 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
363 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
364 arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
365 arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
366 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
367 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
368
369 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
370 if (IS_ERR_OR_NULL(info_linear)) {
371 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
372 goto out;
373 }
374
375 btf_id = info_linear->info.btf_id;
376
377 info_node = malloc(sizeof(struct bpf_prog_info_node));
378 if (info_node) {
379 info_node->info_linear = info_linear;
380 perf_env__insert_bpf_prog_info(env, info_node);
381 } else
382 free(info_linear);
383
384 if (btf_id == 0)
385 goto out;
386
387 if (btf__get_from_id(btf_id, &btf)) {
388 pr_debug("%s: failed to get BTF of id %u, aborting\n",
389 __func__, btf_id);
390 goto out;
391 }
392 perf_env__fetch_btf(env, btf_id, btf);
393
394 out:
395 free(btf);
396 close(fd);
397 }
398
bpf_event__sb_cb(union perf_event * event,void * data)399 static int bpf_event__sb_cb(union perf_event *event, void *data)
400 {
401 struct perf_env *env = data;
402
403 if (event->header.type != PERF_RECORD_BPF_EVENT)
404 return -1;
405
406 switch (event->bpf.type) {
407 case PERF_BPF_EVENT_PROG_LOAD:
408 perf_env__add_bpf_info(env, event->bpf.id);
409
410 case PERF_BPF_EVENT_PROG_UNLOAD:
411 /*
412 * Do not free bpf_prog_info and btf of the program here,
413 * as annotation still need them. They will be freed at
414 * the end of the session.
415 */
416 break;
417 default:
418 pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
419 break;
420 }
421
422 return 0;
423 }
424
evlist__add_bpf_sb_event(struct evlist * evlist,struct perf_env * env)425 int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)
426 {
427 struct perf_event_attr attr = {
428 .type = PERF_TYPE_SOFTWARE,
429 .config = PERF_COUNT_SW_DUMMY,
430 .sample_id_all = 1,
431 .watermark = 1,
432 .bpf_event = 1,
433 .size = sizeof(attr), /* to capture ABI version */
434 };
435
436 /*
437 * Older gcc versions don't support designated initializers, like above,
438 * for unnamed union members, such as the following:
439 */
440 attr.wakeup_watermark = 1;
441
442 return perf_evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);
443 }
444
__bpf_event__print_bpf_prog_info(struct bpf_prog_info * info,struct perf_env * env,FILE * fp)445 void __bpf_event__print_bpf_prog_info(struct bpf_prog_info *info,
446 struct perf_env *env,
447 FILE *fp)
448 {
449 __u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
450 __u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
451 char name[KSYM_NAME_LEN];
452 struct btf *btf = NULL;
453 u32 sub_prog_cnt, i;
454
455 sub_prog_cnt = info->nr_jited_ksyms;
456 if (sub_prog_cnt != info->nr_prog_tags ||
457 sub_prog_cnt != info->nr_jited_func_lens)
458 return;
459
460 if (info->btf_id) {
461 struct btf_node *node;
462
463 node = __perf_env__find_btf(env, info->btf_id);
464 if (node)
465 btf = btf__new((__u8 *)(node->data),
466 node->data_size);
467 }
468
469 if (sub_prog_cnt == 1) {
470 synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, 0);
471 fprintf(fp, "# bpf_prog_info %u: %s addr 0x%llx size %u\n",
472 info->id, name, prog_addrs[0], prog_lens[0]);
473 goto out;
474 }
475
476 fprintf(fp, "# bpf_prog_info %u:\n", info->id);
477 for (i = 0; i < sub_prog_cnt; i++) {
478 synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, i);
479
480 fprintf(fp, "# \tsub_prog %u: %s addr 0x%llx size %u\n",
481 i, name, prog_addrs[i], prog_lens[i]);
482 }
483 out:
484 btf__free(btf);
485 }
486