Skip to contentSkip to conversation

bpf: Refactor how the verifier matches kfunc checks

PATCH In review Chengkaitao 1 participant 5 messages Apr 3 – Apr 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover bpf: Refactor how the verifier matches kfunc checks

From: Kaitao Cheng <[email protected]>

The verifier contains a lot of logic like the following, scattered everywhere, which makes the code harder and harder to maintain:

static bool is_bpf_rbtree_add_kfunc(u32 func_id)

{
	return func_id == special_kfunc_list[KF_bpf_rbtree_add] ||
	       func_id == special_kfunc_list[KF_bpf_rbtree_add_impl];
}

This series introduces a new approach: each is_bpf_*-style set can place its entries in its own section (.BTF_ids.##sfx), and the linker script merges those per-set sections into the main .BTF_ids section.

When merging subsections, the linker script inserts an end symbol for each one, so we can derive how many entries each subsection holds. The kernel can then walk the entries in a subsection and get the same effect as calling an is_bpf_* helper.

With this we can drop the BTF_SET_END macro; BTF_ID-style macros can live anywhere in a C file and are no longer tied to a strict BTF_SET_START … BTF_ID … BTF_SET_END ordering. On top of that, all BTF_ID-style macros can eventually be consolidated in a uniform way.

The current patch only partially migrates the rbtree kfuncs. If this direction looks reasonable, I can continue with further migrations.

The end goal is something like module_init(): adding a new module (kfunc) would only need a single line at the end—like module_init (BPF_VERIF_KFUNC_DEF) to perform all the upfront wiring. Another benefit of BPF_VERIF_KFUNC_DEF is that it pushes us to untangle messy verifier safety cases and make them modular, so they can be expressed as parameters to BPF_VERIF_KFUNC_DEF.

Reaching that goal may still need:

1. Further migration of other kfuncs. One issue worth stating up front:
there will be many .BTF_ids.##sfx subsections, and a large fraction may contain only a single entry. I think that is acceptable; avoiding it would mean keeping some logic on the old special_kfunc_list path for those cases.

2. Trying to unify other macro families (BTF_ID_*, BTF_KFUNCS_*, …); the exact design still needs more thought.

Kaitao Cheng (3):
bpf: Teach resolve_btfids about the setsc type
bpf: Introduce BTF_SET/ID_SUB and BPF_VERIF_KFUNC_DEF
bpf: classify rbtree kfuncs with BPF_VERIF_KFUNC_DEF sets

 include/asm-generic/btf_ids.lds.h | 26 +++++++++
 include/asm-generic/vmlinux.lds.h |  2 +
 include/linux/btf_ids.h           | 92 +++++++++++++++++++++++++++----
 kernel/bpf/helpers.c              |  7 +++
 kernel/bpf/verifier.c             | 44 +++++----------
 tools/bpf/resolve_btfids/main.c   | 89 +++++++++++++++++++++++++++++-
 tools/include/linux/btf_ids.h     | 83 +++++++++++++++++++++++++---
 7 files changed, 292 insertions(+), 51 deletions(-)
 create mode 100644 include/asm-generic/btf_ids.lds.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 bpf: Teach resolve_btfids about the setsc type

From: Kaitao Cheng <[email protected]>

Add .BTF_ids.##sfx subsections under the .BTF_ids output section. The number of entries in each .BTF_ids.##sfx subsection is derived from (__BTF_ids_seg_end_##sfx - sym.st_value). With this, resolve_btfids no longer relies on BTF_SET_END(). That allows kernel code to avoid forcing symbols with similar properties to sit strictly between BTF_SET_START and BTF_SET_END, and it sets the stage for refactoring the BTF_SET* and BTF_ID* macros later.

---
 include/asm-generic/btf_ids.lds.h | 23 ++++++++
 include/asm-generic/vmlinux.lds.h |  2 +
 tools/bpf/resolve_btfids/main.c   | 89 +++++++++++++++++++++++++++++--
 3 files changed, 111 insertions(+), 3 deletions(-)
 create mode 100644 include/asm-generic/btf_ids.lds.h
include/asm-generic/btf_ids.lds.h new file +23 -0
new file mode 100644
index 000000000000..7579ba58f5ff
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2026 KylinSoft Corporation.
+ * Copyright (c) 2026 Kaitao Cheng <[email protected]>
+ */
+#ifndef __ASM_GENERIC_BTF_IDS_LDS_H
+#define __ASM_GENERIC_BTF_IDS_LDS_H
+
+/*
+ * Linker script helpers for CONFIG_DEBUG_INFO_BTF .BTF_ids subsections.
+ * Input section .BTF_ids.##sfx must match __BTF_IDS_SUBSEC(sfx) in btf_ids.h.
+ */
+#ifdef CONFIG_DEBUG_INFO_BTF
+
+#define BTF_IDS_SUBSEG(sfx)							\
+		KEEP(*(.BTF_ids.##sfx))						\
+		__BTF_ids_seg_end_##sfx = .;
+
+#define BTF_IDS_VERIFIER_SUBSEGS
+
+#endif /* CONFIG_DEBUG_INFO_BTF */
+
+#endif /* __ASM_GENERIC_BTF_IDS_LDS_H */
include/asm-generic/vmlinux.lds.h +2 -0
index 1e1580febe4b..96407498629e 100644
@@ -51,6 +51,7 @@
  */
 
 #include <asm-generic/codetag.lds.h>
+#include <asm-generic/btf_ids.lds.h>
 
 #ifndef LOAD_OFFSET
 #define LOAD_OFFSET 0
@@ -691,6 +692,7 @@
 	. = ALIGN(PAGE_SIZE);						\
 	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
 		*(.BTF_ids)						\
+		BTF_IDS_VERIFIER_SUBSEGS				\
 	}
 #else
 #define BTF
tools/bpf/resolve_btfids/main.c +86 -3

131 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 bpf: Introduce BTF_SET/ID_SUB and BPF_VERIF_KFUNC_DEF

In reply to Chengkaitao

From: Kaitao Cheng <[email protected]>

Use BTF_SET_SUB to provide the same kind of grouping as BTF_SET_START/END, so BTF_ID_SUB no longer has to follow the strict sequence (BTF_SET_START, BTF_ID, BTF_SET_END). That lets us scatter BTF_ID_SUB definitions across the file and collect entries with similar properties together, which reduces maintenance overhead.

---
 include/linux/btf_ids.h       | 92 +++++++++++++++++++++++++++++++----
 tools/include/linux/btf_ids.h | 83 +++++++++++++++++++++++++++----
 2 files changed, 156 insertions(+), 19 deletions(-)
include/linux/btf_ids.h +82 -10
index af011db39ab3..c45275c88649 100644
@@ -40,9 +40,9 @@ struct btf_id_set8 {
 
 #define BTF_IDS_SECTION ".BTF_ids"
 
-#define ____BTF_ID(symbol, word)			\
+#define ____BTF_ID(symbol, word, sec)			\
 asm(							\
-".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
+".pushsection " sec ",\"a\";                   \n"	\
 ".local " #symbol " ;                          \n"	\
 ".type  " #symbol ", STT_OBJECT;               \n"	\
 ".size  " #symbol ", 4;                        \n"	\
@@ -52,7 +52,7 @@ word							\
 ".popsection;                                  \n");
 
 #define __BTF_ID(symbol, word) \
-	____BTF_ID(symbol, word)
+	____BTF_ID(symbol, word, BTF_IDS_SECTION)
 
 #define __ID(prefix) \
 	__PASTE(__PASTE(prefix, __COUNTER__), __LINE__)
@@ -86,19 +86,19 @@ word							\
  * .zero 4
  *
  */
-#define __BTF_ID_LIST(name, scope)			\
+#define __BTF_ID_LIST(name, scope, sec)			\
 asm(							\
-".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
+".pushsection " sec ",\"a\";                   \n"	\
 "." #scope " " #name ";                        \n"	\
 #name ":;                                      \n"	\
 ".popsection;                                  \n");
 
 #define BTF_ID_LIST(name)				\
-__BTF_ID_LIST(name, local)				\
+__BTF_ID_LIST(name, local, BTF_IDS_SECTION)		\
 extern u32 name[];
 
 #define BTF_ID_LIST_GLOBAL(name, n)			\
-__BTF_ID_LIST(name, globl)
+__BTF_ID_LIST(name, globl, BTF_IDS_SECTION)
 
 /* The BTF_ID_LIST_SINGLE macro defines a BTF_ID_LIST with
  * a single entry.
@@ -154,11 +154,11 @@ asm(							\
 ".popsection;                                  \n");
 
 #define BTF_SET_START(name)				\
-__BTF_ID_LIST(name, local)				\
+__BTF_ID_LIST(name, local, BTF_IDS_SECTION)		\
 __BTF_SET_START(name, local)
 
 #define BTF_SET_START_GLOBAL(name)			\
-__BTF_ID_LIST(name, globl)				\
+__BTF_ID_LIST(name, globl, BTF_IDS_SECTION)		\
 __BTF_SET_START(name, globl)
 
 #define BTF_SET_END(name)				\
@@ -168,6 +168,75 @@ asm(							\
 ".popsection;                                 \n");	\
 extern struct btf_id_set name;
 
+/*
+ * BTF_SET_SUB — place a set in .BTF_ids.<name> so vmlinux.lds.h can merge
+ * multiple input sections into one output .BTF_ids in a fixed order.
+ * <name> must be a single preprocessor token (e.g. bpf_verif_kfunc_arena).
+ *
+ * Member count: begin is __BTF_ID__setsc__<name> (first in .BTF_ids.<name>);
+ * end is linker symbol __BTF_ids_seg_end_<name> (see btf_ids.lds.h). resolve_btfids
+ * uses cnt = (end - begin) / 4 - 1.  <name> must not contain "__seg__".
+ *
+ * extern struct btf_id_set name is emitted by BTF_SET_SUB.  BTF_ID_SUB(name, ...)
+ * must use the same <name> as the subsection token.
+ */
+#define __BTF_IDS_SUBSEC(sub) ".BTF_ids." #sub
+
+/* Indirection so __ID() expands before ____BTF_ID() stringifies its symbol arg. */
+#define __BTF_ID_SUB(sym, sec)	____BTF_ID(sym, "", sec)
+
+#define BTF_ID_SUB(sub, prefix, name)				\
+	__BTF_ID_SUB(__ID(__BTF_ID__##prefix##__##name##__), __BTF_IDS_SUBSEC(sub))
+
+#define __BTF_ID_LIST_SUB(name, scope)				\
+	__BTF_ID_LIST(name, scope, __BTF_IDS_SUBSEC(name))
+
+#define __BTF_SET_SUB(name, scope)				\
+asm(								\
+".pushsection " __BTF_IDS_SUBSEC(name) ",\"a\";       \n"	\
+"." #scope " __BTF_ID__setsc__" #name ";              \n"	\
+"__BTF_ID__setsc__" #name ":;                         \n"	\
+".zero 4                                              \n"	\
+".popsection;                                         \n");
+
+#define BTF_SET_SUB(name)					\
+extern struct btf_id_set name;					\
+__BTF_ID_LIST_SUB(name, local)					\
+__BTF_SET_SUB(name, local)
+
+#include <linux/args.h> /* CONCATENATE, COUNT_ARGS */
+
+/* bpf_verif_kfunc_<sub> (e.g. rbtree_add → bpf_verif_kfunc_rbtree_add) */
+#define __BPF_VERIF_KFUNC_SUB(sub) bpf_verif_kfunc_##sub
+
+/* Cascade: emit first subsection, recurse on the rest (same kfunc @name). Up to 6 subs. */
+#define __BPF_VERIF_KFUNC_DEF_1(name, s1)				\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)
+
+#define __BPF_VERIF_KFUNC_DEF_2(name, s1, s2)				\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)		\
+	__BPF_VERIF_KFUNC_DEF_1(name, s2)
+
+#define __BPF_VERIF_KFUNC_DEF_3(name, s1, s2, s3)			\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)		\
+	__BPF_VERIF_KFUNC_DEF_2(name, s2, s3)
+
+#define __BPF_VERIF_KFUNC_DEF_4(name, s1, s2, s3, s4)			\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)		\
+	__BPF_VERIF_KFUNC_DEF_3(name, s2, s3, s4)
+
+#define __BPF_VERIF_KFUNC_DEF_5(name, s1, s2, s3, s4, s5)		\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)		\
+	__BPF_VERIF_KFUNC_DEF_4(name, s2, s3, s4, s5)
+
+#define __BPF_VERIF_KFUNC_DEF_6(name, s1, s2, s3, s4, s5, s6)		\
+	BTF_ID_SUB(__BPF_VERIF_KFUNC_SUB(s1), func, name)		\
+	__BPF_VERIF_KFUNC_DEF_5(name, s2, s3, s4, s5, s6)
+
+/* First arg: kfunc symbol; rest: subsection suffix tokens matching bpf_verif_kfunc_<s>. */
+#define BPF_VERIF_KFUNC_DEF(name, ...)					\
+	CONCATENATE(__BPF_VERIF_KFUNC_DEF_, COUNT_ARGS(__VA_ARGS__))(name, __VA_ARGS__)
+
 /*
  * The BTF_SET8_START/END macros pair defines sorted list of
  * BTF IDs and their flags plus its members count, with the
@@ -190,7 +259,7 @@ extern struct btf_id_set name;
  *
  */
 #define __BTF_SET8_START(name, scope, flags)		\
-__BTF_ID_LIST(name, local)				\
+__BTF_ID_LIST(name, local, BTF_IDS_SECTION)		\
 asm(							\
 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
 "." #scope " __BTF_ID__set8__" #name ";        \n"	\
@@ -227,6 +296,9 @@ BTF_SET8_END(name)
 #define BTF_SET_START(name) static struct btf_id_set __maybe_unused name = { 0 };
 #define BTF_SET_START_GLOBAL(name) static struct btf_id_set __maybe_unused name = { 0 };
 #define BTF_SET_END(name)
+#define BTF_SET_SUB(name) static struct btf_id_set __maybe_unused name = { 0 };
+#define BTF_ID_SUB(sub, prefix, name)
+#define BPF_VERIF_KFUNC_DEF(name, ...)
 #define BTF_SET8_START(name) static struct btf_id_set8 __maybe_unused name = { 0 };
 #define BTF_SET8_END(name)
 #define BTF_KFUNCS_START(name) static struct btf_id_set8 __maybe_unused name = { .flags = BTF_SET8_KFUNCS };
tools/include/linux/btf_ids.h +74 -9

132 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 bpf: classify rbtree kfuncs with BPF_VERIF_KFUNC_DEF sets

In reply to Chengkaitao

From: Kaitao Cheng <[email protected]>

Remove verifier logic that enumerated special_kfunc_list[KF_bpf_rbtree_*] and compared against it. Use BPF_VERIF_KFUNC_DEF so each kfunc's verifier-facing safety classification is declared in one place next to the implementation.

When adding new kfuncs later, you no longer need to thread ad-hoc allowlists through scattered verifier branches—tagging the kfunc once with BPF_VERIF_KFUNC_DEF is enough.

This patch only migrates the rbtree kfuncs; other kfunc families can be converted the same way in follow-up work.

---
 include/asm-generic/btf_ids.lds.h |  5 +++-
 kernel/bpf/helpers.c              |  7 +++++
 kernel/bpf/verifier.c             | 44 +++++++++++--------------------
 3 files changed, 26 insertions(+), 30 deletions(-)
include/asm-generic/btf_ids.lds.h +4 -1
index 7579ba58f5ff..879391725d97 100644
@@ -16,7 +16,10 @@
 		KEEP(*(.BTF_ids.##sfx))						\
 		__BTF_ids_seg_end_##sfx = .;
 
-#define BTF_IDS_VERIFIER_SUBSEGS
+#define BTF_IDS_VERIFIER_SUBSEGS						\
+	BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_add)				\
+	BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_graph_node)			\
+	BTF_IDS_SUBSEG(bpf_verif_kfunc_rbtree_api)
 
 #endif /* CONFIG_DEBUG_INFO_BTF */
 
kernel/bpf/helpers.c +7 -0
index 2d8538bf4cfa..d4a8b0b98210 100644
@@ -2606,6 +2606,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,
 	WRITE_ONCE(node_internal->owner, NULL);
 	return (struct bpf_rb_node *)n;
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_remove, rbtree_api, rbtree_graph_node)
 
 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF
  * program
@@ -2667,6 +2668,7 @@ __bpf_kfunc int bpf_rbtree_add(struct bpf_rb_root *root,
 
 	return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off);
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_add, rbtree_api, rbtree_add, rbtree_graph_node)
 
 __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,
 				    bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b),
@@ -2674,6 +2676,7 @@ __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node
 {
 	return bpf_rbtree_add(root, node, less, meta__ign, off);
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_add_impl, rbtree_api, rbtree_add, rbtree_graph_node)
 
 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)
 {
@@ -2681,6 +2684,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)
 
 	return (struct bpf_rb_node *)rb_first_cached(r);
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_first, rbtree_api)
 
 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root)
 {
@@ -2688,6 +2692,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_root(struct bpf_rb_root *root)
 
 	return (struct bpf_rb_node *)r->rb_root.rb_node;
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_root, rbtree_api)
 
 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, struct bpf_rb_node *node)
 {
@@ -2698,6 +2703,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_left(struct bpf_rb_root *root, struct
 
 	return (struct bpf_rb_node *)node_internal->rb_node.rb_left;
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_left, rbtree_api, rbtree_graph_node)
 
 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struct bpf_rb_node *node)
 {
@@ -2708,6 +2714,7 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_right(struct bpf_rb_root *root, struc
 
 	return (struct bpf_rb_node *)node_internal->rb_node.rb_right;
 }
+BPF_VERIF_KFUNC_DEF(bpf_rbtree_right, rbtree_api, rbtree_graph_node)
 
 /**
  * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
kernel/bpf/verifier.c +15 -29

121 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw
In reply to Chengkaitao
Quoted 73 lines · Ihor Solodrai
Quoted 62 lines · Chengkaitao
Quoted 50 lines · Ihor Solodrai
Quoted 20 lines · Chengkaitao
Quoted 5 lines · Alexei Starovoitov
Quoted 3 lines · Chengkaitao

From: Kaitao Cheng <[email protected]>

Quoted 12 lines

Review

Quoted 9 lines

[...]

(quoted diff) +8 -0
+
+/* Kfunc family related to spin_lock. */
+static const enum special_kfunc_type bpf_res_spin_lock_api_kfuncs[] = {
+       KF_bpf_res_spin_lock,
+       KF_bpf_res_spin_unlock,
+       KF_bpf_res_spin_lock_irqsave,
+       KF_bpf_res_spin_unlock_irqrestore,
+};

I think it's a step in the wrong direction.
I'd wait for Ihor's BTF_ID_NAMED cleanup.

Quoted 6 lines

After reading Ihor's messages on the list, if I understand correctly, our two approaches seem to target different problems. What Ihor's work appears to achieve is the ability to remove the entire enum special_kfunc_type. My goal, on the other hand, is to replace many scattered func_id == special_kfunc_list[...] comparisons with a table-driven approach.

Hi Kaitao,

I appreciate your efforts, however after a quick pass over the changes you propose (both here and in the new series) with respect to BTF_ID macros and special_kfuncs_list, I don't understand what problem you're trying to solve.

The inherent complexity is in the fact that the verifier must know when a particular BTF id identifies a specific kfunc, or whether it belongs to some pre-defined set of ids. This is why special_kfuncs_list and other BTF_ID_SET/LIST-s exist.

And so there is no way around defining those ids and sets *somewhere*, and so far BTF_ID_* macros did a fine job of that, all things considered.

AFAICT your changes simply move around the same definitions from functions with if statements to constant arrays with a runtime search on them (which is slower by the way). What is the benefit of that vs the current implementation? We still have to maintain those arrays in the same way we have to maintain the is_foo_kfunc helpers.

Quoted 7 lines

Your newer proposal [1] takes the same idea to the next level, by introducing an entire new BTF kind, new ELF sections and a bunch of macros that are no less complicated than existing. And all of that just moves the same arrays "upstream" to the .BTF_ids section. Again, I fail to see any benefits to that complexity. Having differentiation between LIST and SET, and having to mark START and END is not a problem that needs solving IMO.

Your analysis of the code implementation for the new proposal is correct. Let me elaborate on the purpose behind my approach.

Quoted 8 lines

****** Purpose 1 ******

As described in this patch:
https://lore.kernel.org/bpf/[email protected]/

If we want to add a new kfunc, bpf_list_add_impl, we would today have to add "btf_id == special_kfunc_list[KF_bpf_list_back]" (or similar) five times in verifier.c. ...

Quoted 6 lines

Kaitao,

I think the maintainability pain in the current verifier-side kfunc handling is clear, I don't think anyone would argue that it's not a problem. Your bpf_list_* work is a good demonstration of that. I agree it can be improved.

Hi Ihor,

There is already discussion of this patch series at the link below. Please keep follow-up replies on this thread so the conversation stays easy to track.
https://lore.kernel.org/all/[email protected]/

Adding more logic in verifier.c is not necessarily a good thing—it is already over 26000 lines. The .BTF_ids approach does not reduce the verifier's logical complexity, but it can cut down redundant verifier code and hide the intricate logic from kfunc developers.

To address what you raised (for example, lowering verifier complexity and making the verifier more modular), I need to think about whether there is a better approach that balances these goals.

Quoted 109 lines

That said, I don't think extending .BTF_ids and/or BTF is the right way to solve it.

Quoted 14 lines

... Under the newer proposal, that is no longer
necessary: defining the kfunc and its verifier metadata in one place is enough, for example:

__bpf_kfunc int bpf_list_add_impl(struct bpf_list_head *head,
struct bpf_list_node *new,
struct bpf_list_node *prev,
void *meta__ign, u64 off)
{
/* kfunc implementation */
.......
}
BPF_VERIF_KFUNC_DEF(bpf_list_add_impl, list_api, graph_node_api, ... )

Quoted 4 lines

If BPF_VERIF_KFUNC_DEF is extended further, BTF_ID(func, bpf_list_add_impl) and BTF_ID_FLAGS(func, bpf_list_add_impl) might also become unnecessary, so the snippet above could eventually be close to all the code required to add a new kfunc.

Defining a kfunc metadata in one place is a reasonable direction in principle, I agree.

Where I disagree is the mechanism. The complexity does not disappear, it just moves into .BTF_ids, new macros and resolve_btfids. The primary consumer of this metadata is the verifier, and I think it is better to keep that metadata in verifier-local C definitions instead of encoding it into .BTF_ids. Adding more indirection to this will only hurt maintainability IMO.

Quoted 6 lines

****** Purpose 2 ******

The kernel no longer needs enum special_kfunc_type to list every KF_bpf_* entry. That information is folded into the .BTF_ids.##sfx section instead, so kfunc authors do not have to touch or think about special_kfunc_type.

This is not an argument for the .BTF_ids extension. Removing special_kfunc_type mirroring is useful, but that will be addressed directly by the targeted cleanup already under discussion.

Quoted 11 lines

****** Purpose 3 ******

As described in this patch:
https://lore.kernel.org/bpf/[email protected]/

In is_bpf_list_api_kfunc(u32 btf_id) there are on the order of eleven "btf_id == special_kfunc_list[*]" comparisons. As more kfuncs are added, every is_bpf_* helper will only grow longer and the verifier will get more repetitive. With the new design, those is_bpf_* helpers can be removed entirely, including the awkward scattered "btf_id == *" checks.

The repetition is real. But removing is_bpf_*() helpers is not automatically a simplification if the same information is now encoded indirectly through section layout or local const arrays.

The same metadata is being maintained somewhere else, with more indirection than before. The complexity just moved around, and was not reduced.

The scattered "btf_id == *" will remain as long as it is necessary for the verifier to do certain checks or other work depending on the kfunc. Whether it's an array lookup, helper call or anything else in the condition, the condition must be there. Refactoring can't reduce this complexity, without changes in the verifier control flow.

Quoted 5 lines

****** Purpose 4 ******

It pushes us to untangle messy verifier safety cases and make them modular, so they can be expressed as parameters to BPF_VERIF_KFUNC_DEF

Again, I agree with the premise that verifier safety checks could become more modular where possible. But I think we should first separate two questions:

1. What kfunc properties should be declared centrally?
2. Where that declaration should live?

While I'd like to answer (1) with "all of them", I am not convinced the answer to (2) is .BTF_ids or BTF. A better C side declarative representation would give us most of the benefit here without making the BTF tooling more complex.

Here is how I think we should move forward:

1. Your bpf_list_* work is orthogonal to BTF_ID refactoring, so it's reasonable to first focus on landing it without changes to generic kfunc handling.

2. I plan to send patches (soon) for resolve_btfids, and then for BTF_ID macrology to eliminate the enum + array pattern. You are welcome to join the discussion and review / test the patches.

3. After all of the above lands, we can come back to the general BTF_ID / kfunc handling discussion. If you are interested in developing this further, I suggest to re-think the approach and come up with a "single kfunc metadata definition" that doesn't require significant changes in .BTF_ids section layout.

A slightly off-topic comment: the usage of _impl pattern for kfuncs should be considered deprecated. Any new kfuncs that work with verifier-supplied arguments should use KF_IMPLICIT_ARGS mechanism. So the bpf_list_add_impl in your seires should only have one version:
bpf_list_add marked with KF_IMPLICIT_ARGS flag.

Signature · 2 lines
Yours,
Chengkaitao