BPF List
 help / color / mirror / Atom feed
* [PATCH 1/2] tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs
@ 2026-04-30  5:47 Song Chen
  2026-05-07 16:14 ` Mykyta Yatsenko
  0 siblings, 1 reply; 2+ messages in thread
From: Song Chen @ 2026-04-30  5:47 UTC (permalink / raw)
  To: andrii, eddyz87, ast, daniel, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, alexei.starovoitov
  Cc: bpf, linux-kernel, 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.

Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Song Chen <chensong_2000@126.com>
---
 tools/lib/bpf/libbpf.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0be7017800fe..9c308930bddb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8538,29 +8538,31 @@ static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
 {
 	struct module_btf *mod_btf;
 	struct btf *btf;
-	int i, id, err;
+	int i, id = 0, err;
 
-	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];
-			btf = mod_btf->btf;
-			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
-			if (id != -ENOENT)
-				break;
-		}
+	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];
+		btf = mod_btf->btf;
+		id = btf__find_by_name_kind_own(btf, ksym_name, kind);
+		if (id != -ENOENT)
+			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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/2] tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs
  2026-04-30  5:47 [PATCH 1/2] tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs Song Chen
@ 2026-05-07 16:14 ` Mykyta Yatsenko
  0 siblings, 0 replies; 2+ messages in thread
From: Mykyta Yatsenko @ 2026-05-07 16:14 UTC (permalink / raw)
  To: Song Chen, andrii, eddyz87, ast, daniel, martin.lau, song,
	yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa,
	alexei.starovoitov
  Cc: bpf, linux-kernel

On 4/30/26 6:47 AM, Song Chen wrote:
> 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.
> 
> Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
> Signed-off-by: Song Chen <chensong_2000@126.com>
> ---
>  tools/lib/bpf/libbpf.c | 34 ++++++++++++++++++----------------
>  1 file changed, 18 insertions(+), 16 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 0be7017800fe..9c308930bddb 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8538,29 +8538,31 @@ static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
>  {
>  	struct module_btf *mod_btf;
>  	struct btf *btf;
> -	int i, id, err;
> +	int i, id = 0, err;

nit: I think you can keep id uninitialized, it's set below 
unconditionally.

>  
> -	btf = obj->btf_vmlinux;
>  	mod_btf = NULL;

nit: It looks like you don't need setting mod_btf to NULL here,
 both code paths set it anyway.

> -	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];
> -			btf = mod_btf->btf;
> -			id = btf__find_by_name_kind_own(btf, ksym_name, kind);
> -			if (id != -ENOENT)
> -				break;
> -		}
> +	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];
> +		btf = mod_btf->btf;
> +		id = btf__find_by_name_kind_own(btf, ksym_name, kind);
> +		if (id != -ENOENT)
> +			goto found;

very nit: Perhaps setting *res_btf and *res_mod_btf here inline
and return will make code a bit easier to follow and allow
removing found label.

Overall it looks like the refactoring looks correct, module
btf is processed before the vmlinux btf.

Acked-by: Mykyta Yatsenko <yatsenko@meta.com>

>  	}
> -	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;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-07 16:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-30  5:47 [PATCH 1/2] tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs Song Chen
2026-05-07 16:14 ` Mykyta Yatsenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox