All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Lobakin <alobakin@pm.me>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Alexander Lobakin <alobakin@pm.me>,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
	Song Liu <songliubraving@fb.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	bpf@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 bpf 03/11] bpftool: use a local bpf_perf_event_value to fix accessing its fields
Date: Thu, 21 Apr 2022 00:39:04 +0000	[thread overview]
Message-ID: <20220421003152.339542-4-alobakin@pm.me> (raw)
In-Reply-To: <20220421003152.339542-1-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.
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>
---
 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.36.0



  parent reply	other threads:[~2022-04-21  0:39 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21  0:38 [PATCH v2 bpf 00/11] bpf: random unpopular userspace fixes (32 bit et al) Alexander Lobakin
2022-04-21  0:38 ` [PATCH v2 bpf 01/11] bpftool: use a local copy of perf_event to fix accessing ::bpf_cookie Alexander Lobakin
2022-04-21  0:38 ` [PATCH v2 bpf 02/11] bpftool: define a local bpf_perf_link to fix accessing its fields Alexander Lobakin
2023-04-14  9:54   ` Michal Suchánek
2023-04-14 15:18     ` Alexander Lobakin
2023-04-14 16:28       ` Michal Suchánek
2023-04-20 23:07         ` Andrii Nakryiko
2023-04-21  7:39           ` Michal Suchánek
2023-05-03 23:43             ` Quentin Monnet
2023-05-03 23:52               ` Andrii Nakryiko
2023-05-04  8:18               ` Michal Suchánek
2023-05-04 16:48                 ` Yonghong Song
2022-04-21  0:39 ` Alexander Lobakin [this message]
2023-06-06 21:02   ` [PATCH v2 bpf 03/11] bpftool: use a local bpf_perf_event_value " Nick Desaulniers
2023-06-07 10:42     ` Quentin Monnet
2023-06-07 16:18     ` Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 04/11] bpftool: fix fcntl.h include Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 05/11] samples/bpf: add 'asm/mach-generic' include path for every MIPS Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 06/11] samples/bpf: use host bpftool to generate vmlinux.h, not target Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 07/11] samples/bpf: fix uin64_t format literals Alexander Lobakin
2022-04-21  7:46   ` David Laight
2022-04-21 22:55     ` Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 08/11] samples/bpf: fix false-positive right-shift underflow warnings Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 09/11] samples/bpf: fix include order for non-Glibc environments Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 10/11] samples/bpf: fix -Wsequence-point Alexander Lobakin
2022-04-21  0:39 ` [PATCH v2 bpf 11/11] samples/bpf: xdpsock: fix -Wmaybe-uninitialized Alexander Lobakin
2022-04-21  0:40 ` [PATCH v2 bpf 00/11] bpf: random unpopular userspace fixes (32 bit et al) Alexei Starovoitov
2022-04-21 10:52   ` Toke Høiland-Jørgensen
2022-04-21 22:39   ` Alexander Lobakin
2022-04-21 23:17     ` Toke Høiland-Jørgensen
2022-05-03 21:17   ` Alexander Lobakin
2022-05-04 11:34     ` Toke Høiland-Jørgensen

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=20220421003152.339542-4-alobakin@pm.me \
    --to=alobakin@pm.me \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maciej.fijalkowski@intel.com \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.