linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
To: Song Liu <songliubraving@fb.com>
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, ast@kernel.org,
	daniel@iogearbox.net, kernel-team@fb.com, peterz@infradead.org,
	acme@redhat.com, jolsa@kernel.org, namhyung@kernel.org,
	sdf@fomichev.me
Subject: Re: [PATCH v9 perf,bpf 04/15] perf, bpf: synthesize bpf events with bpf_program__get_prog_info_linear()
Date: Wed, 13 Mar 2019 18:00:30 -0300	[thread overview]
Message-ID: <20190313210030.GA16304@kernel.org> (raw)
In-Reply-To: <20190312053051.2690567-5-songliubraving@fb.com>

Em Mon, Mar 11, 2019 at 10:30:40PM -0700, Song Liu escreveu:
> 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.
> 
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  tools/perf/util/bpf-event.c | 118 ++++++++++++------------------------
>  1 file changed, 40 insertions(+), 78 deletions(-)
> 
> diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
> index ea012b735a37..e9d9854be506 100644
> --- a/tools/perf/util/bpf-event.c
> +++ b/tools/perf/util/bpf-event.c
> @@ -3,7 +3,9 @@
>  #include <stdlib.h>
>  #include <bpf/bpf.h>
>  #include <bpf/btf.h>
> +#include <bpf/libbpf.h>
>  #include <linux/btf.h>
> +#include <linux/err.h>
>  #include "bpf-event.h"
>  #include "debug.h"
>  #include "symbol.h"
> @@ -49,99 +51,62 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
>  {
>  	struct ksymbol_event *ksymbol_event = &event->ksymbol_event;
>  	struct bpf_event *bpf_event = &event->bpf_event;
> -	u32 sub_prog_cnt, i, func_info_rec_size = 0;
> -	u8 (*prog_tags)[BPF_TAG_SIZE] = NULL;
> -	struct bpf_prog_info info = { .type = 0, };
> -	u32 info_len = sizeof(info);
> -	void *func_infos = NULL;
> -	u64 *prog_addrs = NULL;
> +	struct bpf_prog_info_linear *info_linear;
> +	struct bpf_prog_info *info;
>  	struct btf *btf = NULL;
> -	u32 *prog_lens = NULL;
>  	bool has_btf = false;
> -	char errbuf[512];
> +	u32 sub_prog_cnt, i;
>  	int err = 0;
> +	u64 arrays;
>  
> -	/* Call bpf_obj_get_info_by_fd() to get sizes of arrays */
> -	err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
> +	arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
> +	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
> +	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
> +	arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
>  
> -	if (err) {
> -		pr_debug("%s: failed to get BPF program info: %s, aborting\n",
> -			 __func__, str_error_r(errno, errbuf, sizeof(errbuf)));
> +	info_linear = bpf_program__get_prog_info_linear(fd, arrays);
> +	if (IS_ERR_OR_NULL(info_linear)) {
> +		info_linear = NULL;
> +		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
>  		return -1;
>  	}
> -	if (info_len < offsetof(struct bpf_prog_info, prog_tags)) {
> +
> +	if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
>  		pr_debug("%s: the kernel is too old, aborting\n", __func__);
>  		return -2;
>  	}
>  
> +	info = &info_linear->info;
> +
>  	/* number of ksyms, func_lengths, and tags should match */
> -	sub_prog_cnt = info.nr_jited_ksyms;
> -	if (sub_prog_cnt != info.nr_prog_tags ||
> -	    sub_prog_cnt != info.nr_jited_func_lens)
> +	sub_prog_cnt = info->nr_jited_ksyms;
> +	if (sub_prog_cnt != info->nr_prog_tags ||
> +	    sub_prog_cnt != info->nr_jited_func_lens)
>  		return -1;
>  
>  	/* check BTF func info support */
> -	if (info.btf_id && info.nr_func_info && info.func_info_rec_size) {
> +	if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
>  		/* btf func info number should be same as sub_prog_cnt */
> -		if (sub_prog_cnt != info.nr_func_info) {
> +		if (sub_prog_cnt != info->nr_func_info) {
>  			pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
> -			return -1;
> -		}
> -		if (btf__get_from_id(info.btf_id, &btf)) {
> -			pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info.btf_id);
> -			return -1;
> +			err = -1;
> +			goto out;
>  		}
> -		func_info_rec_size = info.func_info_rec_size;
> -		func_infos = calloc(sub_prog_cnt, func_info_rec_size);
> -		if (!func_infos) {
> -			pr_debug("%s: failed to allocate memory for func_infos, aborting\n", __func__);
> -			return -1;
> +		if (btf__get_from_id(info->btf_id, &btf)) {
> +			pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
> +			err = -1;
> +			btf = NULL;
> +			goto out;
>  		}
>  		has_btf = true;
>  	}
>  
> -	/*
> -	 * We need address, length, and tag for each sub program.
> -	 * Allocate memory and call bpf_obj_get_info_by_fd() again
> -	 */
> -	prog_addrs = calloc(sub_prog_cnt, sizeof(u64));
> -	if (!prog_addrs) {
> -		pr_debug("%s: failed to allocate memory for prog_addrs, aborting\n", __func__);
> -		goto out;
> -	}
> -	prog_lens = calloc(sub_prog_cnt, sizeof(u32));
> -	if (!prog_lens) {
> -		pr_debug("%s: failed to allocate memory for prog_lens, aborting\n", __func__);
> -		goto out;
> -	}
> -	prog_tags = calloc(sub_prog_cnt, BPF_TAG_SIZE);
> -	if (!prog_tags) {
> -		pr_debug("%s: failed to allocate memory for prog_tags, aborting\n", __func__);
> -		goto out;
> -	}
> -
> -	memset(&info, 0, sizeof(info));
> -	info.nr_jited_ksyms = sub_prog_cnt;
> -	info.nr_jited_func_lens = sub_prog_cnt;
> -	info.nr_prog_tags = sub_prog_cnt;
> -	info.jited_ksyms = ptr_to_u64(prog_addrs);
> -	info.jited_func_lens = ptr_to_u64(prog_lens);
> -	info.prog_tags = ptr_to_u64(prog_tags);
> -	info_len = sizeof(info);
> -	if (has_btf) {
> -		info.nr_func_info = sub_prog_cnt;
> -		info.func_info_rec_size = func_info_rec_size;
> -		info.func_info = ptr_to_u64(func_infos);
> -	}
> -
> -	err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
> -	if (err) {
> -		pr_debug("%s: failed to get BPF program info, aborting\n", __func__);
> -		goto out;
> -	}
> -
>  	/* Synthesize PERF_RECORD_KSYMBOL */
>  	for (i = 0; i < sub_prog_cnt; i++) {
> +		u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(info->prog_tags);

Need this:

-               u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(info->prog_tags);
+               u8 (*prog_tags)[BPF_TAG_SIZE] = (u8 *)(info->prog_tags);


To overcome this on debian:experimental-x-mips, i.e. Debian Experimental
cross building to MIPS 32-bit:

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);
                      ^
  CC       /tmp/build/perf/util/pmu.o
  CC       /tmp/build/perf/util/pmu-flex.o
cc1: all warnings being treated as errors


> +		__u32 *prog_lens = (__u32 *)(info->jited_func_lens);
> +		__u64 *prog_addrs = (__u64 *)(info->jited_ksyms);
> +		void *func_infos = (void *)(info->func_info);
>  		const struct bpf_func_info *finfo;
>  		const char *short_name = NULL;
>  		const struct btf_type *t;
> @@ -163,13 +128,13 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
>  					 KSYM_NAME_LEN - name_len,
>  					 prog_tags[i], BPF_TAG_SIZE);
>  		if (has_btf) {
> -			finfo = func_infos + i * info.func_info_rec_size;
> +			finfo = func_infos + i * info->func_info_rec_size;
>  			t = btf__type_by_id(btf, finfo->type_id);
>  			short_name = btf__name_by_offset(btf, t->name_off);
>  		} else if (i == 0 && sub_prog_cnt == 1) {
>  			/* no subprog */
> -			if (info.name[0])
> -				short_name = info.name;
> +			if (info->name[0])
> +				short_name = info->name;
>  		} else
>  			short_name = "F";
>  		if (short_name)
> @@ -195,9 +160,9 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
>  			},
>  			.type = PERF_BPF_EVENT_PROG_LOAD,
>  			.flags = 0,
> -			.id = info.id,
> +			.id = info->id,
>  		};
> -		memcpy(bpf_event->tag, prog_tags[i], BPF_TAG_SIZE);
> +		memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
>  		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
>  		event->header.size += machine->id_hdr_size;
>  		err = perf_tool__process_synth_event(tool, event,
> @@ -205,10 +170,7 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool,
>  	}
>  
>  out:
> -	free(prog_tags);
> -	free(prog_lens);
> -	free(prog_addrs);
> -	free(func_infos);
> +	free(info_linear);
>  	free(btf);
>  	return err ? -1 : 0;
>  }
> -- 
> 2.17.1

-- 

- Arnaldo

  reply	other threads:[~2019-03-13 21:00 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-12  5:30 [PATCH v9 perf,bpf 00/15] perf annotation of BPF programs Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 01/15] perf-record: replace option --bpf-event with --no-bpf-event Song Liu
2019-03-22 22:35   ` [tip:perf/urgent] perf record: Replace " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 02/15] bpf: libbpf: introduce bpf_program__get_prog_info_linear() Song Liu
2019-03-12 13:50   ` Arnaldo Carvalho de Melo
2019-03-22 22:36   ` [tip:perf/urgent] tools lib bpf: Introduce bpf_program__get_prog_info_linear() tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 03/15] bpf: bpftool: use bpf_program__get_prog_info_linear() in prog.c:do_dump() Song Liu
2019-03-22 22:36   ` [tip:perf/urgent] " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 04/15] perf, bpf: synthesize bpf events with bpf_program__get_prog_info_linear() Song Liu
2019-03-13 21:00   ` Arnaldo Carvalho de Melo [this message]
2019-03-13 21:08     ` Arnaldo Carvalho de Melo
2019-03-14  5:08       ` Song Liu
2019-03-14 16:45         ` Song Liu
2019-03-14 18:30           ` Arnaldo Carvalho de Melo
2019-03-22 22:37   ` [tip:perf/urgent] perf bpf: Synthesize " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 05/15] perf: change prototype of perf_event__synthesize_bpf_events() Song Liu
2019-03-22 22:37   ` [tip:perf/urgent] perf bpf: Make synthesize_bpf_events() receive perf_session pointer instead of perf_tool tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 06/15] perf, bpf: save bpf_prog_info in a rbtree in perf_env Song Liu
2019-03-12 13:10   ` Jiri Olsa
2019-03-12 14:34     ` Arnaldo Carvalho de Melo
2019-03-22 22:38   ` [tip:perf/urgent] perf bpf: Save " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 07/15] perf, bpf: save bpf_prog_info information as headers to perf.data Song Liu
2019-03-12 13:10   ` Jiri Olsa
2019-03-12 14:47     ` Arnaldo Carvalho de Melo
2019-03-13 19:05   ` Arnaldo Carvalho de Melo
2019-03-22 22:39   ` [tip:perf/urgent] perf bpf: Save " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 08/15] perf, bpf: save btf in a rbtree in perf_env Song Liu
2019-03-12 15:00   ` Arnaldo Carvalho de Melo
2019-03-22 22:39   ` [tip:perf/urgent] perf bpf: Save BTF " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 09/15] perf, bpf: save btf information as headers to perf.data Song Liu
2019-03-12 15:14   ` Arnaldo Carvalho de Melo
2019-03-12 15:16     ` Arnaldo Carvalho de Melo
2019-03-12 16:26       ` Song Liu
2019-03-12 17:05         ` Arnaldo Carvalho de Melo
2019-03-12 17:29           ` Song Liu
2019-03-15 19:06             ` Arnaldo Carvalho de Melo
2019-03-15 19:26               ` Arnaldo Carvalho de Melo
2019-03-15 19:31                 ` Song Liu
2019-03-15 19:42                 ` Arnaldo Carvalho de Melo
2019-03-22 22:40   ` [tip:perf/urgent] perf bpf: Save BTF " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 10/15] perf-top: add option --no-bpf-event Song Liu
2019-03-22 22:41   ` [tip:perf/urgent] perf top: Add " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 11/15] perf: add -lopcodes to feature-libbfd Song Liu
2019-03-22 22:41   ` [tip:perf/urgent] perf feature detection: Add " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 12/15] perf, bpf: enable annotation of bpf program Song Liu
2019-03-18 16:38   ` Arnaldo Carvalho de Melo
2019-03-18 16:43     ` Arnaldo Carvalho de Melo
2019-03-19  6:10       ` Song Liu
2019-03-19 13:37         ` Arnaldo Carvalho de Melo
2019-03-19  6:05     ` Song Liu
2019-03-19 13:37       ` Arnaldo Carvalho de Melo
2019-03-19 13:58       ` Arnaldo Carvalho de Melo
2019-03-19 14:14         ` Arnaldo Carvalho de Melo
2019-03-19 14:52           ` [WORKS!] " Arnaldo Carvalho de Melo
2019-03-19 16:51             ` Song Liu
2019-03-19 16:55               ` Arnaldo Carvalho de Melo
2019-03-19 17:07                 ` Arnaldo Carvalho de Melo
2019-03-22 22:42   ` [tip:perf/urgent] perf symbols: Introduce DSO_BINARY_TYPE__BPF_PROG_INFO tip-bot for Song Liu
2019-03-22 22:43   ` [tip:perf/urgent] perf build: Check what binutils's 'disassembler()' signature to use tip-bot for Song Liu
2019-03-22 22:44   ` [tip:perf/urgent] perf annotate: Enable annotation of BPF programs tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 13/15] perf, bpf: process PERF_BPF_EVENT_PROG_LOAD for annotation Song Liu
2019-03-22 22:43   ` [tip:perf/urgent] perf bpf: Process " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 14/15] perf: introduce side band thread Song Liu
2019-03-22 22:45   ` [tip:perf/urgent] perf evlist: Introduce " tip-bot for Song Liu
2019-03-12  5:30 ` [PATCH v9 perf,bpf 15/15] perf, bpf: save bpf_prog_info and btf of short living bpf programs Song Liu
2019-03-19 15:25   ` Arnaldo Carvalho de Melo
2019-03-22 22:45   ` [tip:perf/urgent] perf tools: Save bpf_prog_info and BTF of new BPF programs tip-bot for Song Liu
2019-03-12 13:12 ` [PATCH v9 perf,bpf 00/15] perf annotation of " Jiri Olsa
2019-03-21  9:10 ` Jiri Olsa
2019-03-21 14:12   ` Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2019-03-07 17:57 [PATCH v7 " Song Liu
2019-03-07 17:57 ` [PATCH v7 perf,bpf 01/15] perf, bpf: consider events with attr.bpf_event as side-band events Song Liu
2019-03-07 17:57 ` [PATCH v7 perf,bpf 02/15] bpf: libbpf: introduce bpf_program__get_prog_info_linear() Song Liu
2019-03-11 18:26   ` Arnaldo Carvalho de Melo
2019-03-11 20:45     ` Daniel Borkmann
2019-03-11 20:57       ` Arnaldo Carvalho de Melo
2019-03-12 11:20         ` Daniel Borkmann
2019-03-07 17:57 ` [PATCH v7 perf,bpf 03/15] bpf: bpftool: use bpf_program__get_prog_info_linear() in prog.c:do_dump() Song Liu
2019-03-07 17:57 ` [PATCH v7 perf,bpf 04/15] perf, bpf: synthesize bpf events with bpf_program__get_prog_info_linear() Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 05/15] perf: change prototype of perf_event__synthesize_bpf_events() Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 06/15] perf, bpf: save bpf_prog_info in a rbtree in perf_env Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 07/15] perf, bpf: save bpf_prog_info information as headers to perf.data Song Liu
2019-03-11 17:56   ` Jiri Olsa
2019-03-11 18:05     ` Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 08/15] perf, bpf: save btf in a rbtree in perf_env Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 09/15] perf, bpf: save btf information as headers to perf.data Song Liu
2019-03-11 17:56   ` Jiri Olsa
2019-03-07 17:58 ` [PATCH v7 perf,bpf 10/15] perf-top: add option --no-bpf-event Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 11/15] perf: add -lopcodes to feature-libbfd Song Liu
2019-03-07 23:26   ` Stanislav Fomichev
2019-03-07 23:47     ` Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 12/15] perf, bpf: enable annotation of bpf program Song Liu
2019-03-11 17:56   ` Jiri Olsa
2019-03-11 18:04     ` Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 13/15] perf, bpf: process PERF_BPF_EVENT_PROG_LOAD for annotation Song Liu
2019-03-07 17:58 ` [PATCH v7 perf,bpf 14/15] perf: introduce side band thread Song Liu
2019-03-11 17:57   ` Jiri Olsa
2019-03-07 17:58 ` [PATCH v7 perf,bpf 15/15] perf, bpf: save bpf_prog_info and btf of short living bpf programs Song Liu
2019-03-11 13:59 ` [PATCH v7 perf,bpf 00/15] perf annotation of BPF programs Jiri Olsa
2019-03-11 14:16   ` Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190313210030.GA16304@kernel.org \
    --to=arnaldo.melo@gmail.com \
    --cc=acme@redhat.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=sdf@fomichev.me \
    --cc=songliubraving@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).