Skip to contentSkip to conversation

Add resolve_btfids support for __arena kfunc suffix

PATCH Applied Kumar Kartikeya Dwivedi 4 participants 7 messages Aug 12 – Aug 13

Subsystems (4)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v2 cover Add resolve_btfids support for __arena kfunc suffix

Use __arena/__arena__nullable suffixes to emit address_space(1) annotations on kfunc definitions in vmlinux.h. See commits for details.

Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]

* Avoid enumerating all the ways resolve_btfids can emit the "address_space(1)" attribute in its header comment and in kfuncs.rst. (Ihor)
* Drop the kfunc_has_arena_arg() helper: add_arena_tagged_proto() returns the original prototype when nothing needs tagging, so it can be invoked unconditionally for every kfunc. (Ihor)
* Add resolve_btfids selftest cases with mixed tagged and untagged arguments, and a kfunc that combines the KF_ARENA_RET flag with suffixed arena arguments. (Ihor)

Kumar Kartikeya Dwivedi (2):
resolve_btfids: Emit arena attributes from kfunc parameter suffixes selftests/bpf: Test resolve_btfids arena argument suffixes

 Documentation/bpf/kfuncs.rst                  |  4 +-
 tools/bpf/resolve_btfids/main.c               | 72 +++++++++++++------
 .../selftests/bpf/prog_tests/resolve_btfids.c | 41 ++++++-----
 tools/testing/selftests/bpf/progs/btf_data.c  | 20 ++++++
 4 files changed, 99 insertions(+), 38 deletions(-)

base-commit: 3a59f11e0f989bdd637c87151992605a6559a7cb

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/2 resolve_btfids: Emit arena attributes from kfunc parameter suffixes

Kfunc declarations can identify arena arguments through parameter name suffixes without repeating KF_ARENA_ARG flags in their BTF ID sets. resolve_btfids currently misses those arguments when synthesizing the address_space(1) attributes used by generated vmlinux.h files.

Teach the arena prototype rewrite to recognize __arena and __arena__nullable directly on each parameter. Keep KF_ARENA_ARG1 and KF_ARENA_ARG2 handling for explicitly flagged kfuncs, while allowing suffixes on any argument without synthesizing kfunc flags.

---
 Documentation/bpf/kfuncs.rst    |  4 +-
 tools/bpf/resolve_btfids/main.c | 72 ++++++++++++++++++++++++---------
 2 files changed, 54 insertions(+), 22 deletions(-)
Documentation/bpf/kfuncs.rst +2 -2
index 1004eb0bec61..10e725cbe64c 100644
@@ -513,8 +513,8 @@ At kernel build time the ``resolve_btfids`` tool finds all kfuncs declared with
 ``BTF_KFUNCS_START()`` and emits their BTF annotations into the kernel's BTF.
 For each kfunc it emits a ``bpf_kfunc`` BTF decl tag, a ``bpf_fastcall`` decl
 tag when the kfunc is flagged ``KF_FASTCALL``, and the ``address_space(1)`` type
-attribute on the return value and/or arguments flagged ``KF_ARENA_RET``,
-``KF_ARENA_ARG1`` or ``KF_ARENA_ARG2`` (see section 2.8).
+attribute on the return value and/or arguments that use arena pointers (see
+sections 2.3.8 and 2.8).
 
 2.7  Specifying no-cast aliases with ___init
 --------------------------------------------
tools/bpf/resolve_btfids/main.c +52 -20

140 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH bpf-next v2 1/2] resolve_btfids: Emit arena attributes from kfunc parameter suffixes

This looks like a bug fix for missing arena suffix handling. Should this include:
Quoted 7 lines

resolve_btfids: Emit arena attributes from kfunc parameter suffixes

Kfunc declarations can identify arena arguments through parameter name suffixes without repeating KF_ARENA_ARG flags in their BTF ID sets. resolve_btfids currently misses those arguments when synthesizing the address_space(1) attributes used by generated vmlinux.h files.

Quoted 6 lines

Teach the arena prototype rewrite to recognize __arena and __arena__nullable directly on each parameter. Keep KF_ARENA_ARG1 and KF_ARENA_ARG2 handling for explicitly flagged kfuncs, while allowing suffixes on any argument without synthesizing kfunc flags.

This looks like a bug fix for missing arena suffix handling. Should this include:

Fixes 27a78c2e7eee ("resolve_btfids: Process KF_ARENA_* flags in resolve_btfids")
Quoted 4 lines
Documentation/bpf/kfuncs.rst +0 -0
index 1004eb0bec617..10e725cbe64cd 100644

[ ... ]

Quoted 4 lines
tools/bpf/resolve_btfids/main.c +0 -0
index d2e4176339da6..37d7e7224207d 100644

[ ... ]

tools/bpf/resolve_btfids/main.c static int collect_decl_tags(struct btf2btf_context *ctx) · line 1078

Quoted 11 lines
tools/bpf/resolve_btfids/main.c +7 -0
@@ -1067,6 +1069,22 @@ static int collect_decl_tags(struct btf2btf_context *ctx)
 	return 0;
 }
 
+static bool param_name_has_suffix(const char *name, const char *suffix)
+{
+	size_t name_len = strlen(name);
+	size_t suffix_len = strlen(suffix);
+
+	return name_len >= suffix_len && !strcmp(name + name_len - suffix_len, suffix);
+}

Does this accept parameter names that are exactly "__arena" or "__arena__nullable"? The kernel's btf_param_match_suffix() in kernel/bpf/btf.c deliberately rejects that case:

    len = strlen(param_name);
    if (len <= suffix_len)
            return false;

Both verifier and JIT go through that helper. If resolve_btfids uses >= but the kernel uses >, a parameter whose whole name is the suffix would get address_space(1) in the generated vmlinux.h but would be treated as an ordinary pointer by the verifier and JIT. The header and kernel would then disagree about the calling convention.

Should the comparison here be name_len > suffix_len to match the kernel?

Quoted 8 lines
tools/bpf/resolve_btfids/main.c +8 -0
+
+static bool is_arena_param(const struct btf *btf, const struct btf_param *param)
+{
+	const char *name = btf__name_by_offset(btf, param->name_off);
+
+	return param_name_has_suffix(name, PARAM_SUFFIX_ARENA) ||
+	       param_name_has_suffix(name, PARAM_SUFFIX_ARENA_NULLABLE);
+}

[ ... ]

Quoted 4 lines
tools/bpf/resolve_btfids/main.c +0 -0
@@ -1299,8 +1317,12 @@ static int process_kfunc_with_implicit_args(struct btf2btf_context *ctx, struct
 	return 0;
 }
 

tools/bpf/resolve_btfids/main.c static int process_kfunc_with_implicit_args(struct btf2btf_context *ctx, struct · line 1328

Quoted 10 lines
tools/bpf/resolve_btfids/main.c +5 -1
-static bool is_arena_arg(struct kfunc *kfunc, u32 idx)
+static bool is_arena_arg(const struct btf *btf, const struct kfunc *kfunc,
+			 const struct btf_param *param, u32 idx)
 {
+	if (is_arena_param(btf, param))
+		return true;
+
 	switch (idx) {
 	case 0:
 		return kfunc->flags & KF_ARENA_ARG1;

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31635245405

author maintainer
#3 lore raw

PATCH v2 2/2 selftests/bpf: Test resolve_btfids arena argument suffixes

In reply to Kumar Kartikeya Dwivedi

Add a suffix-only kfunc declaration with arena annotations on all five arguments. Verify that resolve_btfids emits address_space(1) type tags for every position without KF_ARENA_ARG flags in the BTF ID sets.

Represent expected arena arguments as a per-parameter bitmap so the test covers suffixes beyond the two positions expressible by flags.

---
 .../selftests/bpf/prog_tests/resolve_btfids.c | 41 +++++++++++--------
 tools/testing/selftests/bpf/progs/btf_data.c  | 20 +++++++++
 2 files changed, 45 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/prog_tests/resolve_btfids.c +25 -16
index 732cfed35e1c..3f9949e8227d 100644
@@ -15,6 +15,7 @@
 #define DECL_TAG_FASTCALL "bpf_fastcall"
 #define DECL_TAG_KFUNC "bpf_kfunc"
 #define TYPE_ATTR_ARENA "address_space(1)"
+#define ARENA_ARG(n) (1U << (n))
 
 #ifndef KF_FASTCALL
 #define KF_FASTCALL (1 << 12)
@@ -49,13 +50,20 @@ struct kfunc_symbol {
 	const char	*name;
 	s32		 id;
 	u32		 flags;
+	u32		 arena_args;
+	bool		 arena_ret;
 };
 
 static struct kfunc_symbol kfunc_symbols[] = {
-	{ "kfunc_a", -1, 0 },
-	{ "kfunc_b", -1, KF_FASTCALL },
-	{ "kfunc_c", -1, KF_ARENA_RET | KF_ARENA_ARG1 | KF_ARENA_ARG2 },
-	{ "kfunc_d", -1, KF_ARENA_ARG2 },
+	{ "kfunc_a", -1, 0, 0, false },
+	{ "kfunc_b", -1, KF_FASTCALL, 0, false },
+	{ "kfunc_c", -1, KF_ARENA_RET | KF_ARENA_ARG1 | KF_ARENA_ARG2,
+	  ARENA_ARG(0) | ARENA_ARG(1), true },
+	{ "kfunc_d", -1, KF_ARENA_ARG2, ARENA_ARG(1), false },
+	{ "kfunc_e", -1, 0, ARENA_ARG(0) | ARENA_ARG(1) | ARENA_ARG(2) |
+	  ARENA_ARG(3) | ARENA_ARG(4), false },
+	{ "kfunc_f", -1, 0, ARENA_ARG(1), false },
+	{ "kfunc_g", -1, KF_ARENA_RET, ARENA_ARG(0) | ARENA_ARG(1), true },
 };
 
 /* Align the .BTF_ids section to 4 bytes */
@@ -105,6 +113,9 @@ BTF_ID_FLAGS(func, kfunc_a)
 BTF_ID_FLAGS(func, kfunc_b, KF_FASTCALL)
 BTF_ID_FLAGS(func, kfunc_c, KF_ARENA_RET | KF_ARENA_ARG1 | KF_ARENA_ARG2)
 BTF_ID_FLAGS(func, kfunc_d, KF_ARENA_ARG2)
+BTF_ID_FLAGS(func, kfunc_e)
+BTF_ID_FLAGS(func, kfunc_f)
+BTF_ID_FLAGS(func, kfunc_g, KF_ARENA_RET)
 BTF_KFUNCS_END(test_kfunc_set)
 
 /*
@@ -112,6 +123,9 @@ BTF_KFUNCS_END(test_kfunc_set)
  * actually sort at least one of the two sets.
  */
 BTF_KFUNCS_START(test_kfunc_set_rev)
+BTF_ID_FLAGS(func, kfunc_g, KF_ARENA_RET)
+BTF_ID_FLAGS(func, kfunc_f)
+BTF_ID_FLAGS(func, kfunc_e)
 BTF_ID_FLAGS(func, kfunc_d, KF_ARENA_ARG2)
 BTF_ID_FLAGS(func, kfunc_c, KF_ARENA_RET | KF_ARENA_ARG1 | KF_ARENA_ARG2)
 BTF_ID_FLAGS(func, kfunc_b, KF_FASTCALL)
@@ -301,15 +315,15 @@ void test_resolve_btfids(void)
 	}
 
 	/*
-	 * Check resolve_btfids wrapped exactly the arena-flagged return/args
-	 * with the address_space(1) type attribute, and left other
+	 * Check resolve_btfids wrapped exactly the arena-flagged or suffixed
+	 * return/args with the address_space(1) type attribute, and left other
 	 * pointers/returns untouched.
 	 */
 	for (i = 0; i < ARRAY_SIZE(kfunc_symbols); i++) {
 		const struct btf_type *fn, *proto;
 		const struct btf_param *params;
 		const char *name = kfunc_symbols[i].name;
-		u32 fl = kfunc_symbols[i].flags;
+		u32 arena_args = kfunc_symbols[i].arena_args;
 		__u32 nr;
 
 		fn = btf__type_by_id(btf, kfunc_symbols[i].id);
@@ -322,15 +336,10 @@ void test_resolve_btfids(void)
 		nr = btf_vlen(proto);
 
 		ASSERT_EQ(is_arena_tagged_ptr(btf, proto->type),
-			  !!(fl & KF_ARENA_RET), name);
-		if (nr > 0) {
-			ASSERT_EQ(is_arena_tagged_ptr(btf, params[0].type),
-				  !!(fl & KF_ARENA_ARG1), name);
-		}
-		if (nr > 1) {
-			ASSERT_EQ(is_arena_tagged_ptr(btf, params[1].type),
-				  !!(fl & KF_ARENA_ARG2), name);
-		}
+			  kfunc_symbols[i].arena_ret, name);
+		for (j = 0; j < nr; j++)
+			ASSERT_EQ(is_arena_tagged_ptr(btf, params[j].type),
+				  !!(arena_args & ARENA_ARG(j)), name);
 	}
 
 out:
tools/testing/selftests/bpf/progs/btf_data.c +20 -0
index ec34f7a6e038..8082c13490ab 100644
@@ -68,3 +68,23 @@ int kfunc_d(struct root_struct *a, struct root_struct *b)
 {
 	return 0;
 }
+
+int kfunc_e(struct root_struct *a__arena,
+	    struct root_struct *b__arena__nullable,
+	    struct root_struct *c__arena,
+	    struct root_struct *d__arena__nullable,
+	    struct root_struct *e__arena)
+{
+	return 0;
+}
+
+int kfunc_f(struct root_struct *a, struct root_struct *b__arena, int flags)
+{
+	return 0;
+}
+
+struct root_struct *kfunc_g(struct root_struct *a__arena,
+			    struct root_struct *b__arena__nullable)
+{
+	return a__arena;
+}
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Test resolve_btfids arena argument suffixes

This isn't a bug, but would it be worth mentioning kfunc_f and kfunc_g in the changelog?
Quoted 10 lines

selftests/bpf: Test resolve_btfids arena argument suffixes

Add a suffix-only kfunc declaration with arena annotations on all five arguments. Verify that resolve_btfids emits address_space(1) type tags for every position without KF_ARENA_ARG flags in the BTF ID sets.

Represent expected arena arguments as a per-parameter bitmap so the test covers suffixes beyond the two positions expressible by flags.

This isn't a bug, but would it be worth mentioning kfunc_f and kfunc_g in the changelog?

The description talks about "a suffix-only kfunc declaration with arena annotations on all five arguments" (which is kfunc_e), but the patch also adds kfunc_f (a mixed prototype where only the second of three parameters is suffixed and the third is a non-pointer) and kfunc_g (suffixes combined with KF_ARENA_RET).

kfunc_f and kfunc_g arguably cover more interesting cases since they pin down that unsuffixed and non-pointer parameters stay untagged and that suffixes and flags compose.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31635245405

reviewer Acked-by on the v2 series
#5 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 8 lines · Kumar Kartikeya Dwivedi

Use __arena/__arena__nullable suffixes to emit address_space(1) annotations on kfunc definitions in vmlinux.h. See commits for details.

Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]

Quoted 9 lines

* Avoid enumerating all the ways resolve_btfids can emit the "address_space(1)" attribute in its header comment and in kfuncs.rst. (Ihor)
* Drop the kfunc_has_arena_arg() helper: add_arena_tagged_proto() returns the original prototype when nothing needs tagging, so it can be invoked unconditionally for every kfunc. (Ihor)
* Add resolve_btfids selftest cases with mixed tagged and untagged arguments, and a kfunc that combines the KF_ARENA_RET flag with suffixed arena arguments. (Ihor)

For the series:

Thanks!

Quoted 13 lines

Kumar Kartikeya Dwivedi (2):
resolve_btfids: Emit arena attributes from kfunc parameter suffixes selftests/bpf: Test resolve_btfids arena argument suffixes

 Documentation/bpf/kfuncs.rst                  |  4 +-
 tools/bpf/resolve_btfids/main.c               | 72 +++++++++++++------
 .../selftests/bpf/prog_tests/resolve_btfids.c | 41 ++++++-----
 tools/testing/selftests/bpf/progs/btf_data.c  | 20 ++++++
 4 files changed, 99 insertions(+), 38 deletions(-)

base-commit: 3a59f11e0f989bdd637c87151992605a6559a7cb

#6 lore raw
In reply to Kumar Kartikeya Dwivedi
This series was applied to bpf/bpf-next.git (master) by Eduard Zingerman <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Eduard Zingerman <[email protected]>:

Quoted 9 lines · On Wed, 12 Aug 2026 21:38:39 +0200 you wrote:

Use __arena/__arena__nullable suffixes to emit address_space(1) annotations on kfunc definitions in vmlinux.h. See commits for details.

Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]

[...]

Here is the summary with links:
- [bpf-next,v2,1/2] resolve_btfids: Emit arena attributes from kfunc parameter suffixes

    https://git.kernel.org/bpf/bpf-next/c/5e31d32843d3
  - [bpf-next,v2,2/2] selftests/bpf: Test resolve_btfids arena argument suffixes
    https://git.kernel.org/bpf/bpf-next/c/d7842e98ca17

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html