From: Jiri Olsa <olsajiri@gmail.com>
To: Quentin Monnet <quentin@isovalent.com>
Cc: "Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Song Liu" <song@kernel.org>, "Yonghong Song" <yhs@fb.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"KP Singh" <kpsingh@kernel.org>,
"Stanislav Fomichev" <sdf@google.com>,
"Hao Luo" <haoluo@google.com>,
bpf@vger.kernel.org,
"Alexander Lobakin" <aleksander.lobakin@intel.com>,
"Michal Suchánek" <msuchanek@suse.de>,
"Alexander Lobakin" <alobakin@pm.me>
Subject: Re: [PATCH bpf-next 4/4] bpftool: use a local bpf_perf_event_value to fix accessing its fields
Date: Fri, 12 May 2023 14:59:35 +0200 [thread overview]
Message-ID: <ZF44Nzzw09x2n88o@krava> (raw)
In-Reply-To: <20230512103354.48374-5-quentin@isovalent.com>
On Fri, May 12, 2023 at 11:33:54AM +0100, Quentin Monnet wrote:
> From: Alexander Lobakin <alobakin@pm.me>
>
> Fix the following error when building bpftool:
>
> CLANG profiler.bpf.o
> CLANG pid_iter.bpf.o
> skeleton/profiler.bpf.c:18:21: error: invalid application of 'sizeof' to an incomplete type 'struct bpf_perf_event_value'
> __uint(value_size, sizeof(struct bpf_perf_event_value));
> ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:13:39: note: expanded from macro '__uint'
> tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helper_defs.h:7:8: note: forward declaration of 'struct bpf_perf_event_value'
> struct bpf_perf_event_value;
> ^
>
> struct bpf_perf_event_value is being used in the kernel only when
> CONFIG_BPF_EVENTS is enabled, so it misses a BTF entry then.
hi,
when I switch off CONFIG_BPF_EVENTS the bpftool build fails for me
with missing BTF error:
GEN vmlinux.h
libbpf: failed to find '.BTF' ELF section in /home/jolsa/kernel/linux-qemu/vmlinux
Error: failed to load BTF from /home/jolsa/kernel/linux-qemu/vmlinux: No data available
make: *** [Makefile:208: vmlinux.h] Error 195
make: *** Deleting file 'vmlinux.h'
so I wonder you need to care about bpf_perf_event_value
in that case
jirka
> Define struct bpf_perf_event_value___local with the
> `preserve_access_index` attribute inside the pid_iter BPF prog to
> allow compiling on any configs. It is a full mirror of a UAPI
> structure, so is compatible both with and w/o CO-RE.
> bpf_perf_event_read_value() requires a pointer of the original type,
> so a cast is needed.
>
> Fixes: 47c09d6a9f67 ("bpftool: Introduce "prog profile" command")
> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Alexander Lobakin <alobakin@pm.me>
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> ---
> tools/bpf/bpftool/skeleton/profiler.bpf.c | 27 ++++++++++++++---------
> 1 file changed, 17 insertions(+), 10 deletions(-)
>
> diff --git a/tools/bpf/bpftool/skeleton/profiler.bpf.c b/tools/bpf/bpftool/skeleton/profiler.bpf.c
> index ce5b65e07ab1..2f80edc682f1 100644
> --- a/tools/bpf/bpftool/skeleton/profiler.bpf.c
> +++ b/tools/bpf/bpftool/skeleton/profiler.bpf.c
> @@ -4,6 +4,12 @@
> #include <bpf/bpf_helpers.h>
> #include <bpf/bpf_tracing.h>
>
> +struct bpf_perf_event_value___local {
> + __u64 counter;
> + __u64 enabled;
> + __u64 running;
> +} __attribute__((preserve_access_index));
> +
> /* map of perf event fds, num_cpu * num_metric entries */
> struct {
> __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
> @@ -15,14 +21,14 @@ struct {
> struct {
> __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
> __uint(key_size, sizeof(u32));
> - __uint(value_size, sizeof(struct bpf_perf_event_value));
> + __uint(value_size, sizeof(struct bpf_perf_event_value___local));
> } fentry_readings SEC(".maps");
>
> /* accumulated readings */
> struct {
> __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
> __uint(key_size, sizeof(u32));
> - __uint(value_size, sizeof(struct bpf_perf_event_value));
> + __uint(value_size, sizeof(struct bpf_perf_event_value___local));
> } accum_readings SEC(".maps");
>
> /* sample counts, one per cpu */
> @@ -39,7 +45,7 @@ const volatile __u32 num_metric = 1;
> SEC("fentry/XXX")
> int BPF_PROG(fentry_XXX)
> {
> - struct bpf_perf_event_value *ptrs[MAX_NUM_MATRICS];
> + struct bpf_perf_event_value___local *ptrs[MAX_NUM_MATRICS];
> u32 key = bpf_get_smp_processor_id();
> u32 i;
>
> @@ -53,10 +59,10 @@ int BPF_PROG(fentry_XXX)
> }
>
> for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) {
> - struct bpf_perf_event_value reading;
> + struct bpf_perf_event_value___local reading;
> int err;
>
> - err = bpf_perf_event_read_value(&events, key, &reading,
> + err = bpf_perf_event_read_value(&events, key, (void *)&reading,
> sizeof(reading));
> if (err)
> return 0;
> @@ -68,14 +74,14 @@ int BPF_PROG(fentry_XXX)
> }
>
> static inline void
> -fexit_update_maps(u32 id, struct bpf_perf_event_value *after)
> +fexit_update_maps(u32 id, struct bpf_perf_event_value___local *after)
> {
> - struct bpf_perf_event_value *before, diff;
> + struct bpf_perf_event_value___local *before, diff;
>
> before = bpf_map_lookup_elem(&fentry_readings, &id);
> /* only account samples with a valid fentry_reading */
> if (before && before->counter) {
> - struct bpf_perf_event_value *accum;
> + struct bpf_perf_event_value___local *accum;
>
> diff.counter = after->counter - before->counter;
> diff.enabled = after->enabled - before->enabled;
> @@ -93,7 +99,7 @@ fexit_update_maps(u32 id, struct bpf_perf_event_value *after)
> SEC("fexit/XXX")
> int BPF_PROG(fexit_XXX)
> {
> - struct bpf_perf_event_value readings[MAX_NUM_MATRICS];
> + struct bpf_perf_event_value___local readings[MAX_NUM_MATRICS];
> u32 cpu = bpf_get_smp_processor_id();
> u32 i, zero = 0;
> int err;
> @@ -102,7 +108,8 @@ int BPF_PROG(fexit_XXX)
> /* read all events before updating the maps, to reduce error */
> for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) {
> err = bpf_perf_event_read_value(&events, cpu + i * num_cpu,
> - readings + i, sizeof(*readings));
> + (void *)(readings + i),
> + sizeof(*readings));
> if (err)
> return 0;
> }
> --
> 2.34.1
>
next prev parent reply other threads:[~2023-05-12 12:59 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-12 10:33 [PATCH bpf-next 0/4] bpftool: Fix skeletons compilation for older kernels Quentin Monnet
2023-05-12 10:33 ` [PATCH bpf-next 1/4] bpftool: use a local copy of perf_event to fix accessing ::bpf_cookie Quentin Monnet
2023-05-12 10:33 ` [PATCH bpf-next 2/4] bpftool: define a local bpf_perf_link to fix accessing its fields Quentin Monnet
2023-05-12 14:47 ` Yonghong Song
2023-05-15 16:43 ` Quentin Monnet
2023-05-12 10:33 ` [PATCH bpf-next 3/4] bpftool: Use a local copy of BPF_LINK_TYPE_PERF_EVENT in pid_iter.bpf.c Quentin Monnet
2023-05-12 10:33 ` [PATCH bpf-next 4/4] bpftool: use a local bpf_perf_event_value to fix accessing its fields Quentin Monnet
2023-05-12 12:59 ` Jiri Olsa [this message]
2023-05-15 16:53 ` Quentin Monnet
2023-07-07 9:53 ` Quentin Monnet
2023-05-16 21:30 ` Andrii Nakryiko
2023-05-17 15:02 ` Quentin Monnet
2023-05-17 16:58 ` Andrii Nakryiko
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=ZF44Nzzw09x2n88o@krava \
--to=olsajiri@gmail.com \
--cc=aleksander.lobakin@intel.com \
--cc=alobakin@pm.me \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=msuchanek@suse.de \
--cc=quentin@isovalent.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yhs@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