Skip to contentSkip to conversation

tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs

PATCH In review Song Chen 5 participants 12 messages May 10 – May 17

Subsystems (2)
lore
Share & tools
lore

Song Chen

author
#0 lore raw

PATCH v2 1/2 tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching for kfuncs.

Previously, vmlinux kfuncs were always preferred, making it impossible for modules to provide enhanced or fixed versions of existing kfuncs. With this change, modules can now override kernel kfuncs, while programs that don't use module BTFs remain unaffected.

---
changelog:
v1 --> v2:
1, introduce namespace to specify which module the kfunc belongs to, like:
modulea__foo
moduleb__foo
foo
As a result, kfunc foo can co-exist in modulea, moduleb and vmlinux, ebpf code owner can specify which one he wants to call.

---
 tools/lib/bpf/libbpf.c | 60 +++++++++++++++++++++++++++++++-----------
 1 file changed, 45 insertions(+), 15 deletions(-)
tools/lib/bpf/libbpf.c +45 -15
index 0be7017800fe..9c9e5ff4d754 100644
@@ -8532,35 +8532,65 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
 	return libbpf_kallsyms_parse(kallsyms_cb, obj);
 }
 
+static void split_module_from_ksym(const char *ksym_name,
+			const char **module_name,
+			size_t *module_name_len,
+			const char **kfunc_name)
+{
+	const char *sep = strstr(ksym_name, "__");
+
+	if (!sep) {
+		*module_name = NULL;
+		*module_name_len = 0;
+		*kfunc_name = ksym_name;
+	} else {
+		*module_name = ksym_name;
+		*module_name_len = sep - ksym_name;
+		*kfunc_name = sep + strlen("__");
+	}
+}
+
 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
-			    __u16 kind, struct btf **res_btf,
-			    struct module_btf **res_mod_btf)
+			__u16 kind, struct btf **res_btf,
+			struct module_btf **res_mod_btf)
 {
 	struct module_btf *mod_btf;
 	struct btf *btf;
-	int i, id, err;
+	int i, id = 0, err;
+	const char *module_name;
+	const char *kfunc_name;
+	size_t module_name_len;
+
+	split_module_from_ksym(ksym_name, &module_name, &module_name_len, &kfunc_name);
+	if (module_name_len == 0)
+		goto search_vmlinux;
 
-	btf = obj->btf_vmlinux;
 	mod_btf = NULL;
-	id = btf__find_by_name_kind(btf, ksym_name, kind);
 
-	if (id == -ENOENT) {
-		err = load_module_btfs(obj);
-		if (err)
-			return err;
+	err = load_module_btfs(obj);
+	if (err)
+		goto search_vmlinux;
 
-		for (i = 0; i < obj->btf_module_cnt; i++) {
-			/* we assume module_btf's BTF FD is always >0 */
-			mod_btf = &obj->btf_modules[i];
+	for (i = 0; i < obj->btf_module_cnt; i++) {
+		/* we assume module_btf's BTF FD is always >0 */
+		mod_btf = &obj->btf_modules[i];
+		if (strlen(mod_btf->name) == module_name_len &&
+			!strncmp(mod_btf->name, module_name, module_name_len)) {
 			btf = mod_btf->btf;
-			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
+			id = btf__find_by_name_kind_own(btf, kfunc_name, kind);
 			if (id != -ENOENT)
-				break;
+				goto found;
 		}
 	}
-	if (id <= 0)
+
+search_vmlinux:
+	btf = obj->btf_vmlinux;
+	mod_btf = NULL;
+	id = btf__find_by_name_kind(btf, ksym_name, kind);
+	if (id == -ENOENT)
 		return -ESRCH;
 
+found:
 	*res_btf = btf;
 	*res_mod_btf = mod_btf;
 	return id;
Signature · 1 line
2.43.0
#1 lore raw
Does this break resolution of existing kfuncs and ksyms that live in kernel modules?
Quoted 33 lines
tools/lib/bpf/libbpf.c +20 -2
index ab2071fdd3e8..b62ec39685e9 100644
@@ -8587,35 +8587,65 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
 	return libbpf_kallsyms_parse(kallsyms_cb, obj);
 }
 
+static void split_module_from_ksym(const char *ksym_name,
+			const char **module_name,
+			size_t *module_name_len,
+			const char **kfunc_name)
+{
+	const char *sep = strstr(ksym_name, "__");
+
+	if (!sep) {
+		*module_name = NULL;
+		*module_name_len = 0;
+		*kfunc_name = ksym_name;
+	} else {
+		*module_name = ksym_name;
+		*module_name_len = sep - ksym_name;
+		*kfunc_name = sep + strlen("__");
+	}
+}
+
 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
-			    __u16 kind, struct btf **res_btf,
-			    struct module_btf **res_mod_btf)
+			__u16 kind, struct btf **res_btf,
+			struct module_btf **res_mod_btf)
 {
 	struct module_btf *mod_btf;

tools/lib/bpf/libbpf.c static int bpf_object__read_kallsyms_file(struct bpf_object *obj) · line 8621

Quoted 10 lines
tools/lib/bpf/libbpf.c +8 -1
 	struct btf *btf;
-	int i, id, err;
+	int i, id = 0, err;
+	const char *module_name;
+	const char *kfunc_name;
+	size_t module_name_len;
+
+	split_module_from_ksym(ksym_name, &module_name, &module_name_len, &kfunc_name);
+	if (module_name_len == 0)
+		goto search_vmlinux;

Does this break resolution of existing kfuncs and ksyms that live in kernel modules?

For any ksym name that does NOT contain '__' (the default for essentially all existing BPF programs and in-tree selftests), split_module_from_ksym sets module_name_len = 0 and the code takes 'goto search_vmlinux' and never iterates obj->btf_modules.

In the old code, when a ksym was not found in vmlinux BTF (id == -ENOENT), load_module_btfs() was called and every loaded module BTF was searched with btf__find_by_name_kind_own(btf, ksym_name, kind). That fallback is now gone for names without '__'.

Concrete breaking example: tools/testing/selftests/bpf/progs/kfunc_call_test.c uses kfuncs registered in bpf_testmod.ko such as bpf_kfunc_call_test1, bpf_kfunc_call_test5, bpf_kfunc_call_test_acquire, bpf_kfunc_call_test_pass_ctx, bpf_kfunc_call_test_release. None contain '__', so split_module_from_ksym sets module_name_len = 0 and find_ksym_btf_id jumps straight to search_vmlinux.

Since these kfuncs live in bpf_testmod.ko's BTF (not vmlinux), btf__find_by_name_kind() returns -ENOENT and find_ksym_btf_id returns -ESRCH. The BPF program load then fails at bpf_object__resolve_ksym_func_btf_id with 'extern (func ksym) ...: not found in kernel or module BTFs'.

Call chain demonstrating the breakage:

bpf_object__load (libbpf.c)
-> bpf_object__resolve_externs

    -> bpf_object__resolve_ksyms_btf_id
      -> bpf_object__resolve_ksym_func_btf_id
        -> find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, ...)
          -> split_module_from_ksym
            -> module_name_len == 0
              -> goto search_vmlinux
                -> returns -ESRCH

Same issue applies to struct_ops registered in modules. For example, test_kmods/bpf_testmod.c calls register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops). bpf_map__init_kern_struct_ops -> find_struct_ops_kern_types calls find_ksym_btf_id with stname = "bpf_struct_ops_bpf_testmod_ops" (no '__'), so the module BTF search is skipped and the struct_ops map can no longer be initialized.

Same issue applies to bpf_object__resolve_ksym_var_btf_id for any variable-kind ksym that lives in a module.

Quoted 32 lines
tools/lib/bpf/libbpf.c +12 -12
-	btf = obj->btf_vmlinux;
 	mod_btf = NULL;
-	id = btf__find_by_name_kind(btf, ksym_name, kind);
 
-	if (id == -ENOENT) {
-		err = load_module_btfs(obj);
-		if (err)
-			return err;
+	err = load_module_btfs(obj);
+	if (err)
+		goto search_vmlinux;
 
-		for (i = 0; i < obj->btf_module_cnt; i++) {
-			/* we assume module_btf's BTF FD is always >0 */
-			mod_btf = &obj->btf_modules[i];
+	for (i = 0; i < obj->btf_module_cnt; i++) {
+		/* we assume module_btf's BTF FD is always >0 */
+		mod_btf = &obj->btf_modules[i];
+		if (strlen(mod_btf->name) == module_name_len &&
+			!strncmp(mod_btf->name, module_name, module_name_len)) {
 			btf = mod_btf->btf;
-			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
+			id = btf__find_by_name_kind_own(btf, kfunc_name, kind);
 			if (id != -ENOENT)
-				break;
+				goto found;
 		}
 	}
-	if (id <= 0)
+
+search_vmlinux:

tools/lib/bpf/libbpf.c static int bpf_object__read_kallsyms_file(struct bpf_object *obj) · line 8596

Quoted 10 lines
tools/lib/bpf/libbpf.c +5 -0
+	btf = obj->btf_vmlinux;
+	mod_btf = NULL;
+	id = btf__find_by_name_kind(btf, ksym_name, kind);
+	if (id == -ENOENT)
 		return -ESRCH;
 
+found:
 	*res_btf = btf;
 	*res_mod_btf = mod_btf;
 	return id;

The commit message states:

Quoted 3 lines

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching

Does the implementation match this description?

For any ksym_name without '__', split_module_from_ksym sets module_name_len = 0 and find_ksym_btf_id jumps directly to the search_vmlinux label, never touching obj->btf_modules. So there is no module-first ordering for ordinary kfunc names.

For ksym_names that contain '__', the code treats the substring before '__' as a module name and only searches the single module whose mod_btf->name matches that prefix (strlen(mod_btf->name) == module_name_len && !strncmp(...)). It does not iterate all modules.

So a kernel module cannot 'override vmlinux kfuncs with the same name' unless the BPF program author explicitly spells the kfunc as '<module-name>__<kfunc-name>'. That is a new opt-in naming convention, not a transparent override, and it is not documented anywhere in the patch.

Either the implementation should actually search modules first for all names (and reviewers should then decide if that is acceptable), or the commit message should be rewritten to describe the real semantics: 'introduce a module__kfunc naming convention so BPF programs can explicitly target a specific module's kfunc' and document the new convention and its impact on existing callers of find_ksym_btf_id.

---
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/25618517155

#2 lore raw
In reply to Song Chen
Quoted 23 lines · Song Chen

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching for kfuncs.

Previously, vmlinux kfuncs were always preferred, making it impossible for modules to provide enhanced or fixed versions of existing kfuncs. With this change, modules can now override kernel kfuncs, while programs that don't use module BTFs remain unaffected.

---
changelog:
v1 --> v2:
1, introduce namespace to specify which module the kfunc belongs to, like:
modulea__foo
moduleb__foo
foo
As a result, kfunc foo can co-exist in modulea, moduleb and vmlinux, ebpf code owner can specify which one he wants to call.

Quoted 4 lines
---
 tools/lib/bpf/libbpf.c | 60 +++++++++++++++++++++++++++++++-----------
 1 file changed, 45 insertions(+), 15 deletions(-)

Instead of modulea___ prefix, how about using declaration tags instead? We can use namespace:vmlinux and namespace:module to choose precedence for module or vmlinux.
Then further scope using namespace:moduleA or namespace:moduleB.

By default, I think preferring vmlinux symbols over modules makes sense. If one wants to prefer an overload with the same name coming from a module, they can place the right tag. But we need more consensus from other folks before you respin again. If we didn't have a way of indicating the preference, I would agree picking the one in module would allow more interesting cases by default, but since we do have a mechanism, it makes sense to pick a more sensible conservative default, in my opinion.

Anyway, let's see what others might say.

pw-bot: cr

Quoted 1 line

[...]

author
#3 lore raw
Quoted 29 lines · Kumar Kartikeya Dwivedi
Quoted 23 lines · Song Chen

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching for kfuncs.

Previously, vmlinux kfuncs were always preferred, making it impossible for modules to provide enhanced or fixed versions of existing kfuncs. With this change, modules can now override kernel kfuncs, while programs that don't use module BTFs remain unaffected.

---
changelog:
v1 --> v2:
1, introduce namespace to specify which module the kfunc belongs to, like:
modulea__foo
moduleb__foo
foo
As a result, kfunc foo can co-exist in modulea, moduleb and vmlinux, ebpf code owner can specify which one he wants to call.

Quoted 4 lines
---
  tools/lib/bpf/libbpf.c | 60 +++++++++++++++++++++++++++++++-----------
  1 file changed, 45 insertions(+), 15 deletions(-)
Quoted 5 lines

Instead of modulea___ prefix, how about using declaration tags instead? We can use namespace:vmlinux and namespace:module to choose precedence for module or vmlinux.
Then further scope using namespace:moduleA or namespace:moduleB.

Thanks for the suggestion. Just to clarify — are you proposing this as a BTF/string-level naming convention, or as something that would appear in actual C identifiers? Since colons aren't valid in C identifiers, I want to make sure I understand the intended mechanism.

Quoted 11 lines

By default, I think preferring vmlinux symbols over modules makes sense. If one wants to prefer an overload with the same name coming from a module, they can place the right tag. But we need more consensus from other folks before you respin again. If we didn't have a way of indicating the preference, I would agree picking the one in module would allow more interesting cases by default, but since we do have a mechanism, it makes sense to pick a more sensible conservative default, in my opinion.

Anyway, let's see what others might say.

yes, many thanks.

Best regards,

Song

Quoted 3 lines

pw-bot: cr

Quoted 1 line

[...]

#4 lore raw
In reply to Song Chen
Quoted 41 lines · Song Chen

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching for kfuncs.

Previously, vmlinux kfuncs were always preferred, making it impossible for modules to provide enhanced or fixed versions of existing kfuncs. With this change, modules can now override kernel kfuncs, while programs that don't use module BTFs remain unaffected.

---
changelog:
v1 --> v2:
1, introduce namespace to specify which module the kfunc belongs to, like:
modulea__foo
moduleb__foo
foo
As a result, kfunc foo can co-exist in modulea, moduleb and vmlinux, ebpf code owner can specify which one he wants to call.

---
 tools/lib/bpf/libbpf.c | 60 +++++++++++++++++++++++++++++++-----------
 1 file changed, 45 insertions(+), 15 deletions(-)
tools/lib/bpf/libbpf.c +7 -0
index 0be7017800fe..9c9e5ff4d754 100644
@@ -8532,35 +8532,65 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
 	return libbpf_kallsyms_parse(kallsyms_cb, obj);
 }
 
+static void split_module_from_ksym(const char *ksym_name,
+			const char **module_name,
+			size_t *module_name_len,
+			const char **kfunc_name)
+{
+	const char *sep = strstr(ksym_name, "__");
+

tools/lib/bpf/libbpf.c static int bpf_object__read_kallsyms_file(struct bpf_object *obj) · line 8551

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+	if (!sep) {
+		*module_name = NULL;
+		*module_name_len = 0;
+		*kfunc_name = ksym_name;
+	} else {
+		*module_name = ksym_name;
+		*module_name_len = sep - ksym_name;
+		*kfunc_name = sep + strlen("__");
+	}
+}

I don't think overloading "__" in ksym_name is safe.

1. "__" itself is very common in kernel symbol names. Forcibly treating the first "__" as the separator between the module name and the kfunc name is seriously ambiguous. We cannot rule out existing kfuncs, either defined by a module or by the core kernel, whose names already contain "__".

2. ksym_name is not used only locally inside find_ksym_btf_id(). If ext->name / ext->essent_name becomes inconsistent with the actual kfunc name, it may affect other code paths as well.

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Could we use a new libbpf API to establish the mapping manually? for example:

       bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:

       extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

Note: Adding the module name as a prefix to the kfunc name is technically feasible, but it has significant historical compatibility issues, which are hard to solve.

Quoted 59 lines
tools/lib/bpf/libbpf.c +27 -15
 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
-			    __u16 kind, struct btf **res_btf,
-			    struct module_btf **res_mod_btf)
+			__u16 kind, struct btf **res_btf,
+			struct module_btf **res_mod_btf)
 {
 	struct module_btf *mod_btf;
 	struct btf *btf;
-	int i, id, err;
+	int i, id = 0, err;
+	const char *module_name;
+	const char *kfunc_name;
+	size_t module_name_len;
+
+	split_module_from_ksym(ksym_name, &module_name, &module_name_len, &kfunc_name);
+	if (module_name_len == 0)
+		goto search_vmlinux;
 
-	btf = obj->btf_vmlinux;
 	mod_btf = NULL;
-	id = btf__find_by_name_kind(btf, ksym_name, kind);
 
-	if (id == -ENOENT) {
-		err = load_module_btfs(obj);
-		if (err)
-			return err;
+	err = load_module_btfs(obj);
+	if (err)
+		goto search_vmlinux;
 
-		for (i = 0; i < obj->btf_module_cnt; i++) {
-			/* we assume module_btf's BTF FD is always >0 */
-			mod_btf = &obj->btf_modules[i];
+	for (i = 0; i < obj->btf_module_cnt; i++) {
+		/* we assume module_btf's BTF FD is always >0 */
+		mod_btf = &obj->btf_modules[i];
+		if (strlen(mod_btf->name) == module_name_len &&
+			!strncmp(mod_btf->name, module_name, module_name_len)) {
 			btf = mod_btf->btf;
-			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
+			id = btf__find_by_name_kind_own(btf, kfunc_name, kind);
 			if (id != -ENOENT)
-				break;
+				goto found;
 		}
 	}
-	if (id <= 0)
+
+search_vmlinux:
+	btf = obj->btf_vmlinux;
+	mod_btf = NULL;
+	id = btf__find_by_name_kind(btf, ksym_name, kind);
+	if (id == -ENOENT)
 		return -ESRCH;
 
+found:
 	*res_btf = btf;
 	*res_mod_btf = mod_btf;
 	return id;
Signature · 2 lines
Thanks
Kaitao Cheng
#5 lore raw
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:

       bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:

       extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.
libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.
All that only for bpf progs that are loaded after override. Already loaded progs don't need to be live patched.
Thoughts?

author
#6 lore raw

Hi;

Quoted 41 lines · Alexei Starovoitov
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:
bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:
extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.

Quoted 8 lines

libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.
All that only for bpf progs that are loaded after override. Already loaded progs don't need to be live patched. Thoughts?

If i understand you correctly, you suggest instead of introduce namespace in libbpf, we should replace kfunc in vmlinux(btf->types[type_id]) with kfunc in modules by introducing override_btf_kfunc_set.

In [1], I simply returned an error, your suggestion is a little further than that, correct me if i'm wrong.

[1]:https://lore.kernel.org/all/[email protected]/

best regards,

Song

#7 lore raw
Quoted 59 lines · Song Chen

Hi;

Quoted 41 lines · Alexei Starovoitov
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:
bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:
extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.

Quoted 8 lines

libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.
All that only for bpf progs that are loaded after override. Already loaded progs don't need to be live patched. Thoughts?

If i understand you correctly, you suggest instead of introduce namespace in libbpf, we should replace kfunc in vmlinux(btf->types[type_id]) with kfunc in modules by introducing override_btf_kfunc_set.

Quoted 4 lines

In [1], I simply returned an error, your suggestion is a little further than that, correct me if i'm wrong.

[1]:https://lore.kernel.org/all/[email protected]/

Right. The error on name conflict and new api to override if there is a full match.

#8 lore raw
In reply to Alexei Starovoitov
Quoted 32 lines · Alexei Starovoitov
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:

       bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:

       extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

Quoted 14 lines

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.
libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.

If multiple modules are allowed to call override_btf_kfunc_id_set() for the same vmlinux kfunc, the effective implementation becomes dependent on module load order.

For example, module A overrides a vmlinux kfunc and an A BPF program is written with the expectation that this kfunc resolves to module A's implementation. Later, developer B loads module B, which overrides the same kfunc so that B's BPF program can work. If developer A then reloads or reruns the A BPF program without knowing about module B's override, the same kfunc may now resolve to module B's implementation. This can make the behavior hard to debug and increase the maintenance cost of the system.

If this is the intended semantics, I think override_btf_kfunc_id_set() should have clear comments documenting that later overrides replace earlier ones for future BPF program loads. It may also be useful to expose some debugging visibility, e.g. through verifier logs, debugfs, or bpftool, so users can tell whether a given kfunc currently resolves to vmlinux or to a specific module.

If this is not intended, then we may need a mechanism to reject overriding an already-overridden kfunc unless the caller explicitly requests a forced replacement.

Quoted 3 lines

All that only for bpf progs that are loaded after override. Already loaded progs don't need to be live patched. Thoughts?

Signature · 2 lines
Thanks
Kaitao Cheng
author
#9 lore raw

Hi,

Quoted 63 lines · Kaitao Cheng
Quoted 32 lines · Alexei Starovoitov
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:
bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:
extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

Quoted 14 lines

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.
libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.

If multiple modules are allowed to call override_btf_kfunc_id_set() for the same vmlinux kfunc, the effective implementation becomes dependent on module load order.

For example, module A overrides a vmlinux kfunc and an A BPF program is written with the expectation that this kfunc resolves to module A's implementation. Later, developer B loads module B, which overrides the same kfunc so that B's BPF program can work. If developer A then reloads or reruns the A BPF program without knowing about module B's override, the same kfunc may now resolve to module B's implementation. This can make the behavior hard to debug and increase the maintenance cost of the system.

Quoted 6 lines

If this is the intended semantics, I think override_btf_kfunc_id_set() should have clear comments documenting that later overrides replace earlier ones for future BPF program loads. It may also be useful to expose some debugging visibility, e.g. through verifier logs, debugfs, or bpftool, so users can tell whether a given kfunc currently resolves to vmlinux or to a specific module.

I agree with you. I have already had a solution for this proposal:
define a new attribute, for example:

       extern void kfunc_name(void) __ksym __module("module_name");

1) introduce a tag in bpf_helpers.h
#define __kmod(name) __attribute__((btf_decl_tag("kmod:" name)))

2) add module_name in struct extern_desc

struct extern_desc {
    enum extern_type type;
....
                const char *module_name;

} ksym;
3) read tag in bpf_object__collect_externs:
tag = btf__name_by_offset(btf, t->name_off);
ext->ksym.module_name = tag + strlen("kmod:");
4) search kfunc in modules in find_ksym_btf_id if ext->ksym.module_name is valid, otherwise, search it in vmlinux.

This proposal solves the kfunc priority gracefully, if ebpf program declares the kfunc module explicitly like this:

extern int foo(const char *str, const char *substr) __ksym __kmod("hello");

then use foo in module hello, if no __kmod after declaration, ext->ksym.module_name is NULL, the behavior of find_ksym_btf_id remains same, no impact to its original logic.

/Song

Quoted 9 lines

If this is not intended, then we may need a mechanism to reject overriding an already-overridden kfunc unless the caller explicitly requests a forced replacement.

Quoted 3 lines

All that only for bpf progs that are loaded after override. Already loaded progs don't need to be live patched. Thoughts?

#10 lore raw
Quoted 93 lines · Song Chen

Hi,

Quoted 63 lines · Kaitao Cheng
Quoted 32 lines · Alexei Starovoitov
Quoted 12 lines · Kaitao Cheng

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Quoted 8 lines

Could we use a new libbpf API to establish the mapping manually? for example:
bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:
extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

I'm not excited about 2.
Two different modules are not allowed to have two different functions with the same name.
The kernel has only one global namespace.
Hence I don't think we should be introducing such namespacing concept into kfuncs.

But 1, I feel, is useful for experiments and mitigations, but it probably should be done transparently to libbpf.

Quoted 14 lines

register_btf_kfunc_id_set() should be changed to return an error when a module attempts to register a kfunc with a name that already exists in the kernel.
We also introduce override_btf_kfunc_set() that would register
a replacement kfunc.
override_btf_kfunc_set() would check that all overriding kfuncs have the exact same kfunc already in vmlinux BTF: proto and name should match. Then the kernel will just remember that the address of that kfunc is in the module.
libbpf doesn't need to be aware of this substitution.
It will find kfunc in the vmlinux BTF, but the verifier will supply implementation from the module to bpf prog.
2nd module should be able to override_btf_kfunc_set() from vmlinux and from 1st module that replaced some kfuncs.

If multiple modules are allowed to call override_btf_kfunc_id_set() for the same vmlinux kfunc, the effective implementation becomes dependent on module load order.

For example, module A overrides a vmlinux kfunc and an A BPF program is written with the expectation that this kfunc resolves to module A's implementation. Later, developer B loads module B, which overrides the same kfunc so that B's BPF program can work. If developer A then reloads or reruns the A BPF program without knowing about module B's override, the same kfunc may now resolve to module B's implementation. This can make the behavior hard to debug and increase the maintenance cost of the system.

Quoted 6 lines

If this is the intended semantics, I think override_btf_kfunc_id_set() should have clear comments documenting that later overrides replace earlier ones for future BPF program loads. It may also be useful to expose some debugging visibility, e.g. through verifier logs, debugfs, or bpftool, so users can tell whether a given kfunc currently resolves to vmlinux or to a specific module.

I agree with you. I have already had a solution for this proposal:
define a new attribute, for example:

        extern void kfunc_name(void) __ksym __module("module_name");

1) introduce a tag in bpf_helpers.h
#define __kmod(name) __attribute__((btf_decl_tag("kmod:" name)))

2) add module_name in struct extern_desc

struct extern_desc {
     enum extern_type type;
....
                 const char *module_name;
    } ksym;
3) read tag in bpf_object__collect_externs:
   tag = btf__name_by_offset(btf, t->name_off);
   ext->ksym.module_name = tag + strlen("kmod:");

4) search kfunc in modules in find_ksym_btf_id if ext->ksym.module_name is valid, otherwise, search it in vmlinux.

Quoted 8 lines

This proposal solves the kfunc priority gracefully, if ebpf program declares the kfunc module explicitly like this:

extern int foo(const char *str, const char *substr) __ksym __kmod("hello");

then use foo in module hello, if no __kmod after declaration, ext->ksym.module_name is NULL, the behavior of find_ksym_btf_id remains same, no impact to its original logic.

I don't like it for the same reasons as before. This is a half measure of namespaces.
If override is not clear win, let's do error during kmod load on conflicting kfunc name for now.

author
#11 lore raw
In reply to Kaitao Cheng

Hi,

Quoted 57 lines · Kaitao Cheng
Quoted 41 lines · Song Chen

Change the kfunc resolution order in find_ksym_btf_id() to search module BTFs before vmlinux BTF. This allows kernel modules to override vmlinux kfuncs with the same name, enabling a form of live-patching for kfuncs.

Previously, vmlinux kfuncs were always preferred, making it impossible for modules to provide enhanced or fixed versions of existing kfuncs. With this change, modules can now override kernel kfuncs, while programs that don't use module BTFs remain unaffected.

---
changelog:
v1 --> v2:
1, introduce namespace to specify which module the kfunc belongs to, like:
modulea__foo
moduleb__foo
foo
As a result, kfunc foo can co-exist in modulea, moduleb and vmlinux, ebpf code owner can specify which one he wants to call.

---
  tools/lib/bpf/libbpf.c | 60 +++++++++++++++++++++++++++++++-----------
  1 file changed, 45 insertions(+), 15 deletions(-)
tools/lib/bpf/libbpf.c +7 -0
index 0be7017800fe..9c9e5ff4d754 100644
@@ -8532,35 +8532,65 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
  	return libbpf_kallsyms_parse(kallsyms_cb, obj);
  }
 
+static void split_module_from_ksym(const char *ksym_name,
+			const char **module_name,
+			size_t *module_name_len,
+			const char **kfunc_name)
+{
+	const char *sep = strstr(ksym_name, "__");
+

tools/lib/bpf/libbpf.c static int bpf_object__read_kallsyms_file(struct bpf_object *obj) · line 8551

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+	if (!sep) {
+		*module_name = NULL;
+		*module_name_len = 0;
+		*kfunc_name = ksym_name;
+	} else {
+		*module_name = ksym_name;
+		*module_name_len = sep - ksym_name;
+		*kfunc_name = sep + strlen("__");
+	}
+}

I don't think overloading "__" in ksym_name is safe.

Quoted 5 lines

1. "__" itself is very common in kernel symbol names. Forcibly treating the first "__" as the separator between the module name and the kfunc name is seriously ambiguous. We cannot rule out existing kfuncs, either defined by a module or by the core kernel, whose names already contain "__".

It will fallback event kfunc has an ordinary name with "__".

Quoted 4 lines

2. ksym_name is not used only locally inside find_ksym_btf_id(). If ext->name / ext->essent_name becomes inconsistent with the actual kfunc name, it may affect other code paths as well

ksym_name remains same, no impact to other part.

Quoted 80 lines

It seems that there are two problems to solve here:

1. Allowing kernel modules to override vmlinux kfuncs with the same name.

This should be relatively straightforward to implement by matching names in kernel modules first, and then falling back to vmlinux.

2. Handling the case where different kernel modules have kfuncs with the same name.

Could we use a new libbpf API to establish the mapping manually? for example:
bpf_object__set_ksym_btf(obj, "kfunc_name", "module_name")

Or define a new attribute, for example:
extern void kfunc_name(void) __ksym __module("module_name");

We probably need to hear suggestions from other developers on this.

Note: Adding the module name as a prefix to the kfunc name is technically feasible, but it has significant historical compatibility issues, which are hard to solve.

Quoted 55 lines
tools/lib/bpf/libbpf.c +26 -15
  static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
-			    __u16 kind, struct btf **res_btf,
-			    struct module_btf **res_mod_btf)
+			__u16 kind, struct btf **res_btf,
+			struct module_btf **res_mod_btf)
  {
  	struct module_btf *mod_btf;
  	struct btf *btf;
-	int i, id, err;
+	int i, id = 0, err;
+	const char *module_name;
+	const char *kfunc_name;
+	size_t module_name_len;
+
+	split_module_from_ksym(ksym_name, &module_name, &module_name_len, &kfunc_name);
+	if (module_name_len == 0)
+		goto search_vmlinux;
 
-	btf = obj->btf_vmlinux;
  	mod_btf = NULL;
-	id = btf__find_by_name_kind(btf, ksym_name, kind);
 
-	if (id == -ENOENT) {
-		err = load_module_btfs(obj);
-		if (err)
-			return err;
+	err = load_module_btfs(obj);
+	if (err)
+		goto search_vmlinux;
 
-		for (i = 0; i < obj->btf_module_cnt; i++) {
-			/* we assume module_btf's BTF FD is always >0 */
-			mod_btf = &obj->btf_modules[i];
+	for (i = 0; i < obj->btf_module_cnt; i++) {
+		/* we assume module_btf's BTF FD is always >0 */
+		mod_btf = &obj->btf_modules[i];
+		if (strlen(mod_btf->name) == module_name_len &&
+			!strncmp(mod_btf->name, module_name, module_name_len)) {
  			btf = mod_btf->btf;
-			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
+			id = btf__find_by_name_kind_own(btf, kfunc_name, kind);
  			if (id != -ENOENT)
-				break;
+				goto found;
  		}
  	}
-	if (id <= 0)
+
+search_vmlinux:
+	btf = obj->btf_vmlinux;
+	mod_btf = NULL;
+	id = btf__find_by_name_kind(btf, ksym_name, kind);
+	if (id == -ENOENT)
  		return -ESRCH;
 
Quoted 5 lines

tools/lib/bpf/libbpf.c static int bpf_object__read_kallsyms_file(struct bpf_object *obj) · line 8596

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
+found:
  	*res_btf = btf;
  	*res_mod_btf = mod_btf;
  	return id;

best regards,

Song