All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: dwarves@vger.kernel.org, bpf@vger.kernel.org
Cc: acme@kernel.org, alan.maguire@oracle.com, ast@kernel.org,
	andrii@kernel.org, eddyz87@gmail.com, mykolal@fb.com,
	kernel-team@meta.com
Subject: [PATCH dwarves v4 4/6] btf_encoder: emit type tags for bpf_arena pointers
Date: Fri, 28 Feb 2025 11:46:52 -0800	[thread overview]
Message-ID: <20250228194654.1022535-5-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20250228194654.1022535-1-ihor.solodrai@linux.dev>

When adding a kfunc prototype to BTF, check for the flags indicating
bpf_arena pointers and emit a type tag encoding
__attribute__((address_space(1))) for them. This also requires
updating BTF type ids in the btf_encoder_func_state, which is done as
a side effect in the tagging functions.

This feature depends on recent update in libbpf, supporting arbitrarty
attribute encoding [1].

[1] https://lore.kernel.org/bpf/20250130201239.1429648-1-ihor.solodrai@linux.dev/

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
---
 btf_encoder.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 dwarves.h     |  1 +
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index 12a040f..991aba6 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -41,7 +41,13 @@
 #define BTF_SET8_KFUNCS		(1 << 0)
 #define BTF_KFUNC_TYPE_TAG	"bpf_kfunc"
 #define BTF_FASTCALL_TAG       "bpf_fastcall"
-#define KF_FASTCALL            (1 << 12)
+#define BPF_ARENA_ATTR         "address_space(1)"
+
+/* kfunc flags, see include/linux/btf.h in the kernel source */
+#define KF_FASTCALL   (1 << 12)
+#define KF_ARENA_RET  (1 << 13)
+#define KF_ARENA_ARG1 (1 << 14)
+#define KF_ARENA_ARG2 (1 << 15)
 
 struct btf_id_and_flag {
 	uint32_t id;
@@ -718,6 +724,81 @@ static int32_t btf_encoder__tag_type(struct btf_encoder *encoder, uint32_t tag_t
 	return encoder->type_id_off + tag_type;
 }
 
+static int btf__tag_bpf_arena_ptr(struct btf *btf, int ptr_id)
+{
+	const struct btf_type *ptr;
+	int tagged_type_id;
+
+	ptr = btf__type_by_id(btf, ptr_id);
+	if (!btf_is_ptr(ptr))
+		return -EINVAL;
+
+	tagged_type_id = btf__add_type_attr(btf, BPF_ARENA_ATTR, ptr->type);
+	if (tagged_type_id < 0)
+		return tagged_type_id;
+
+	return btf__add_ptr(btf, tagged_type_id);
+}
+
+static int btf__tag_bpf_arena_arg(struct btf *btf, struct btf_encoder_func_state *state, int idx)
+{
+	int id;
+
+	if (state->nr_parms <= idx)
+		return -EINVAL;
+
+	id = btf__tag_bpf_arena_ptr(btf, state->parms[idx].type_id);
+	if (id < 0) {
+		btf__log_err(btf, BTF_KIND_TYPE_TAG, BPF_ARENA_ATTR, true, id,
+			"Error adding BPF_ARENA_ATTR for an argument of kfunc '%s'", state->elf->name);
+		return id;
+	}
+	state->parms[idx].type_id = id;
+
+	return id;
+}
+
+static int btf__add_bpf_arena_type_tags(struct btf *btf, struct btf_encoder_func_state *state)
+{
+	uint32_t flags = state->elf->kfunc_flags;
+	int ret_type_id;
+	int err;
+
+	if (!btf__add_type_attr) {
+		fprintf(stderr, "btf__add_type_attr is not available, is libbpf < 1.6?\n");
+		return -ENOTSUP;
+	}
+
+	if (KF_ARENA_RET & flags) {
+		ret_type_id = btf__tag_bpf_arena_ptr(btf, state->ret_type_id);
+		if (ret_type_id < 0) {
+			btf__log_err(btf, BTF_KIND_TYPE_TAG, BPF_ARENA_ATTR, true, ret_type_id,
+				"Error adding BPF_ARENA_ATTR for return type of kfunc '%s'", state->elf->name);
+			return ret_type_id;
+		}
+		state->ret_type_id = ret_type_id;
+	}
+
+	if (KF_ARENA_ARG1 & flags) {
+		err = btf__tag_bpf_arena_arg(btf, state, 0);
+		if (err < 0)
+			return err;
+	}
+
+	if (KF_ARENA_ARG2 & flags) {
+		err = btf__tag_bpf_arena_arg(btf, state, 1);
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
+}
+
+static inline bool is_kfunc_state(struct btf_encoder_func_state *state)
+{
+	return state && state->elf && state->elf->kfunc;
+}
+
 static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct ftype *ftype,
 					   struct btf_encoder_func_state *state)
 {
@@ -731,6 +812,10 @@ static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct f
 
 	assert(ftype != NULL || state != NULL);
 
+	if (is_kfunc_state(state) && encoder->tag_kfuncs)
+		if (btf__add_bpf_arena_type_tags(encoder->btf, state) < 0)
+			return -1;
+
 	/* add btf_type for func_proto */
 	if (ftype) {
 		btf = encoder->btf;
diff --git a/dwarves.h b/dwarves.h
index ab32204..4202dfb 100644
--- a/dwarves.h
+++ b/dwarves.h
@@ -52,6 +52,7 @@ enum load_steal_kind {
 struct btf;
 __weak extern int btf__add_enum64(struct btf *btf, const char *name, __u32 byte_sz, bool is_signed);
 __weak extern int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value);
+__weak extern int btf__add_type_attr(struct btf *btf, const char *value, int ref_type_id);
 __weak extern int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf, struct btf **new_split_btf);
 
 /*
-- 
2.48.1


  parent reply	other threads:[~2025-02-28 19:47 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-28 19:46 [PATCH dwarves v4 0/6] btf_encoder: emit type tags for bpf_arena pointers Ihor Solodrai
2025-02-28 19:46 ` [PATCH dwarves v4 1/6] btf_encoder: refactor btf_encoder__tag_kfuncs() Ihor Solodrai
2025-02-28 19:46 ` [PATCH dwarves v4 2/6] btf_encoder: use __weak declarations of version-dependent libbpf API Ihor Solodrai
2025-02-28 19:53   ` Ihor Solodrai
2025-03-06 17:14     ` Alan Maguire
2025-02-28 19:46 ` [PATCH dwarves v4 3/6] pahole: sync with libbpf mainline Ihor Solodrai
2025-02-28 19:46 ` Ihor Solodrai [this message]
2025-02-28 19:46 ` [PATCH dwarves v4 5/6] pahole: introduce --btf_feature=attributes Ihor Solodrai
2025-02-28 19:46 ` [PATCH dwarves v4 6/6] man-pages: describe attributes and remove reproducible_build Ihor Solodrai
2025-03-20 16:32 ` [PATCH dwarves v4 0/6] btf_encoder: emit type tags for bpf_arena pointers Ihor Solodrai
2025-03-20 20:34   ` Alan Maguire
2025-03-23 11:11     ` Alan Maguire
2025-03-24 18:07       ` Ihor Solodrai
2025-03-24 18:47         ` Ihor Solodrai
2025-03-25  9:59           ` Alan Maguire
2025-03-26 17:41             ` Ihor Solodrai
2025-03-27  8:22               ` Alan Maguire
2025-03-27 15:33                 ` Ihor Solodrai
2025-03-27 15:40                   ` Alan Maguire

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=20250228194654.1022535-5-ihor.solodrai@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=acme@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=mykolal@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.