bpf-event.c 12 KB

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