All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: dwarves@vger.kernel.org, alan.maguire@oracle.com,
	acme@kernel.org, eddyz87@gmail.com
Cc: bpf@vger.kernel.org, andrii@kernel.org, ast@kernel.org,
	kernel-team@meta.com
Subject: [PATCH dwarves v4 3/3] btf_encoder: Factor out BPF kfunc emission
Date: Wed,  5 Nov 2025 17:28:35 -0800	[thread overview]
Message-ID: <20251106012835.260373-4-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20251106012835.260373-1-ihor.solodrai@linux.dev>

Generating BTF for BPF kernel functions requires special
handling. Consolidate this behavior into btf_encoder__add_bpf_kfunc(),
which uses simplified btf_encoder_add_func().

No functional changes.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Tested-by: Eduard Zingerman <eddyz87@gmail.com>
---
 btf_encoder.c | 51 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index bdda7d0..b37ee7f 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -773,6 +773,7 @@ static int btf__tag_bpf_arena_arg(struct btf *btf, struct btf_encoder_func_state
 	return id;
 }
 
+/* Modifies state->ret_type_id and state->parms[i].type_id for flagged kfuncs */
 static int btf__add_bpf_arena_type_tags(struct btf *btf, struct btf_encoder_func_state *state)
 {
 	uint32_t flags = state->elf->kfunc_flags;
@@ -883,11 +884,6 @@ static int32_t btf_encoder__add_func_proto_for_state(struct btf_encoder *encoder
 	const char *name;
 	bool is_last;
 
-	/* Beware: btf__add_bpf_arena_type_tags may change some members of the state */
-	if (is_kfunc_state(state) && encoder->tag_kfuncs && encoder->encode_attributes)
-		if (btf__add_bpf_arena_type_tags(encoder->btf, state) < 0)
-			return -1;
-
 	type_id = state->ret_type_id;
 	nr_params = state->nr_parms;
 
@@ -1357,14 +1353,13 @@ static int btf__tag_kfunc(struct btf *btf, struct elf_function *kfunc, __u32 btf
 static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
 				     struct btf_encoder_func_state *state)
 {
-	struct elf_function *func = state->elf;
 	int btf_fnproto_id, btf_fn_id, tag_type_id = 0;
+	struct elf_function *func = state->elf;
+	char tmp_value[KSYM_NAME_LEN];
 	int16_t component_idx = -1;
-	const char *name;
 	const char *value;
-	char tmp_value[KSYM_NAME_LEN];
+	const char *name;
 	uint16_t idx;
-	int err;
 
 	btf_fnproto_id = btf_encoder__add_func_proto_for_state(encoder, state);
 	name = func->name;
@@ -1377,15 +1372,6 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
 		return -1;
 	}
 
-	if (func->kfunc && encoder->tag_kfuncs && !encoder->skip_encoding_decl_tag) {
-		err = btf__tag_kfunc(encoder->btf, func, btf_fn_id);
-		if (err < 0)
-			return err;
-	}
-
-	if (state->nr_annots == 0)
-		return 0;
-
 	for (idx = 0; idx < state->nr_annots; idx++) {
 		struct btf_encoder_func_annot *a = &state->annots[idx];
 
@@ -1408,6 +1394,30 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,
 		return -1;
 	}
 
+	return btf_fn_id;
+}
+
+static int btf_encoder__add_bpf_kfunc(struct btf_encoder *encoder,
+				      struct btf_encoder_func_state *state)
+{
+	int btf_fn_id, err;
+
+	if (encoder->tag_kfuncs && encoder->encode_attributes) {
+		err = btf__add_bpf_arena_type_tags(encoder->btf, state);
+		if (err < 0)
+			return err;
+	}
+
+	btf_fn_id = btf_encoder__add_func(encoder, state);
+	if (btf_fn_id < 0)
+		return -1;
+
+	if (encoder->tag_kfuncs && !encoder->skip_encoding_decl_tag) {
+		err = btf__tag_kfunc(encoder->btf, state->elf, btf_fn_id);
+		if (err < 0)
+			return err;
+	}
+
 	return 0;
 }
 
@@ -1507,7 +1517,10 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder, bool skip_e
 					0, 0);
 
 		if (add_to_btf) {
-			err = btf_encoder__add_func(encoder, state);
+			if (is_kfunc_state(state))
+				err = btf_encoder__add_bpf_kfunc(encoder, state);
+			else
+				err = btf_encoder__add_func(encoder, state);
 			if (err < 0)
 				goto out;
 		}
-- 
2.51.1


  parent reply	other threads:[~2025-11-06  1:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06  1:28 [PATCH dwarves v4 0/3] btf_encoder: refactor emission of BTF funcs Ihor Solodrai
2025-11-06  1:28 ` [PATCH dwarves v4 1/3] btf_encoder: Remove encoder pointer from btf_encoder_func_state Ihor Solodrai
2025-11-06  1:28 ` [PATCH dwarves v4 2/3] btf_encoder: Refactor btf_encoder__add_func_proto Ihor Solodrai
2025-11-06  1:28 ` Ihor Solodrai [this message]
2025-11-13 16:36 ` [PATCH dwarves v4 0/3] btf_encoder: refactor emission of BTF funcs Alan Maguire
2025-11-13 17:20   ` Alexei Starovoitov
2025-11-14  2:10     ` Arnaldo Carvalho de Melo
2025-11-14  2:14       ` Alexei Starovoitov
2025-11-14 15:40       ` Alan Maguire
2025-11-14 16:27         ` Arnaldo Melo
2025-11-14 20:52         ` Arnaldo Carvalho de Melo
2025-11-14 20:54           ` Arnaldo Carvalho de Melo

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=20251106012835.260373-4-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 \
    /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.