diff mbox series

[v3,bpf-next,1/3] libbpf: Fix event name too long error

Message ID 20250414093402.384872-2-yangfeng59949@163.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series libbpf: Fix event name too long error and add tests | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
netdev/series_format warning Series does not have a cover letter
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success Errors and warnings before: 26 (+2) this patch: 26 (+2)
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch fail ERROR: do not initialise statics to 0
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-12 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-20 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-21 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-43 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-44 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-49 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-50 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-51 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-18 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-46 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-47 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-48 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Feng Yang April 14, 2025, 9:34 a.m. UTC
From: Feng Yang <yangfeng@kylinos.cn>

When the binary path is excessively long, the generated probe_name in libbpf
exceeds the kernel's MAX_EVENT_NAME_LEN limit (64 bytes).
This causes legacy uprobe event attachment to fail with error code -22.

Before Fix:
	./test_progs -t attach_probe/kprobe-long_name
	......
	libbpf: failed to add legacy kprobe event for 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0': -EINVAL
	libbpf: prog 'handle_kprobe': failed to create kprobe 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0' perf event: -EINVAL
	test_attach_kprobe_long_event_name:FAIL:attach_kprobe_long_event_name unexpected error: -22
	test_attach_probe:PASS:uprobe_ref_ctr_cleanup 0 nsec
	#13/11   attach_probe/kprobe-long_name:FAIL
	#13      attach_probe:FAIL

	./test_progs -t attach_probe/uprobe-long_name
	......
	libbpf: failed to add legacy uprobe event for /root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9: -EINVAL
	libbpf: prog 'handle_uprobe': failed to create uprobe '/root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9' perf event: -EINVAL
	test_attach_uprobe_long_event_name:FAIL:attach_uprobe_long_event_name unexpected error: -22
	#13/10   attach_probe/uprobe-long_name:FAIL
	#13      attach_probe:FAIL
After Fix:
	./test_progs -t attach_probe/uprobe-long_name
	#13/10   attach_probe/uprobe-long_name:OK
	#13      attach_probe:OK
	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

	./test_progs -t attach_probe/kprobe-long_name
	#13/11   attach_probe/kprobe-long_name:OK
	#13      attach_probe:OK
	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

Fixes: 46ed5fc33db9 ("libbpf: Refactor and simplify legacy kprobe code")
Fixes: cc10623c6810 ("libbpf: Add legacy uprobe attaching support")
Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
 tools/lib/bpf/libbpf.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

Comments

Jiri Olsa April 14, 2025, 11:43 a.m. UTC | #1
On Mon, Apr 14, 2025 at 05:34:00PM +0800, Feng Yang wrote:
> From: Feng Yang <yangfeng@kylinos.cn>
> 
> When the binary path is excessively long, the generated probe_name in libbpf
> exceeds the kernel's MAX_EVENT_NAME_LEN limit (64 bytes).
> This causes legacy uprobe event attachment to fail with error code -22.
> 
> Before Fix:
> 	./test_progs -t attach_probe/kprobe-long_name
> 	......
> 	libbpf: failed to add legacy kprobe event for 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0': -EINVAL
> 	libbpf: prog 'handle_kprobe': failed to create kprobe 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0' perf event: -EINVAL
> 	test_attach_kprobe_long_event_name:FAIL:attach_kprobe_long_event_name unexpected error: -22
> 	test_attach_probe:PASS:uprobe_ref_ctr_cleanup 0 nsec
> 	#13/11   attach_probe/kprobe-long_name:FAIL
> 	#13      attach_probe:FAIL
> 
> 	./test_progs -t attach_probe/uprobe-long_name
> 	......
> 	libbpf: failed to add legacy uprobe event for /root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9: -EINVAL
> 	libbpf: prog 'handle_uprobe': failed to create uprobe '/root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9' perf event: -EINVAL
> 	test_attach_uprobe_long_event_name:FAIL:attach_uprobe_long_event_name unexpected error: -22
> 	#13/10   attach_probe/uprobe-long_name:FAIL
> 	#13      attach_probe:FAIL
> After Fix:
> 	./test_progs -t attach_probe/uprobe-long_name
> 	#13/10   attach_probe/uprobe-long_name:OK
> 	#13      attach_probe:OK
> 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> 
> 	./test_progs -t attach_probe/kprobe-long_name
> 	#13/11   attach_probe/kprobe-long_name:OK
> 	#13      attach_probe:OK
> 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> 
> Fixes: 46ed5fc33db9 ("libbpf: Refactor and simplify legacy kprobe code")
> Fixes: cc10623c6810 ("libbpf: Add legacy uprobe attaching support")
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
> ---
>  tools/lib/bpf/libbpf.c | 19 ++++++++++++-------
>  1 file changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index b2591f5cab65..9e047641e001 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -60,6 +60,8 @@
>  #define BPF_FS_MAGIC		0xcafe4a11
>  #endif
>  
> +#define MAX_EVENT_NAME_LEN	64
> +
>  #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
>  
>  #define BPF_INSN_SZ (sizeof(struct bpf_insn))
> @@ -11142,10 +11144,10 @@ static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
>  	static int index = 0;
>  	int i;
>  
> -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
> -		 __sync_fetch_and_add(&index, 1));
> +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> +		 __sync_fetch_and_add(&index, 1), kfunc_name, offset);

so the fix is to move unique id before kfunc_name to make sure it gets
to the event name right? would be great to have it in changelog

>  
> -	/* sanitize binary_path in the probe name */
> +	/* sanitize kfunc_name in the probe name */
>  	for (i = 0; buf[i]; i++) {
>  		if (!isalnum(buf[i]))
>  			buf[i] = '_';
> @@ -11270,7 +11272,7 @@ int probe_kern_syscall_wrapper(int token_fd)
>  
>  		return pfd >= 0 ? 1 : 0;
>  	} else { /* legacy mode */
> -		char probe_name[128];
> +		char probe_name[MAX_EVENT_NAME_LEN];
>  
>  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
>  		if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
> @@ -11328,7 +11330,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
>  					    func_name, offset,
>  					    -1 /* pid */, 0 /* ref_ctr_off */);
>  	} else {
> -		char probe_name[256];
> +		char probe_name[MAX_EVENT_NAME_LEN];
>  
>  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
>  					     func_name, offset);
> @@ -11878,9 +11880,12 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
>  static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
>  					 const char *binary_path, uint64_t offset)
>  {
> +	static int index = 0;
>  	int i;
>  
> -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
> +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> +		 __sync_fetch_and_add(&index, 1),
> +		 basename((void *)binary_path), (size_t)offset);

gen_kprobe_legacy_event_name and gen_uprobe_legacy_event_name seem to
be identical now, maybe we can have just one ?

thanks,
jirka

>  
>  	/* sanitize binary_path in the probe name */
>  	for (i = 0; buf[i]; i++) {
> @@ -12312,7 +12317,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
>  		pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
>  					    func_offset, pid, ref_ctr_off);
>  	} else {
> -		char probe_name[PATH_MAX + 64];
> +		char probe_name[MAX_EVENT_NAME_LEN];
>  
>  		if (ref_ctr_off)
>  			return libbpf_err_ptr(-EINVAL);
> -- 
> 2.43.0
>
Feng Yang April 15, 2025, 2:01 a.m. UTC | #2
On Mon, 14 Apr 2025 13:43:38 +0200 Jiri Olsa <olsajiri@gmail.com> wrote:
> On Mon, Apr 14, 2025 at 05:34:00PM +0800, Feng Yang wrote:
> > From: Feng Yang <yangfeng@kylinos.cn>
> > 
> > When the binary path is excessively long, the generated probe_name in libbpf
> > exceeds the kernel's MAX_EVENT_NAME_LEN limit (64 bytes).
> > This causes legacy uprobe event attachment to fail with error code -22.
> > 
> > Before Fix:
> > 	./test_progs -t attach_probe/kprobe-long_name
> > 	......
> > 	libbpf: failed to add legacy kprobe event for 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0': -EINVAL
> > 	libbpf: prog 'handle_kprobe': failed to create kprobe 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0' perf event: -EINVAL
> > 	test_attach_kprobe_long_event_name:FAIL:attach_kprobe_long_event_name unexpected error: -22
> > 	test_attach_probe:PASS:uprobe_ref_ctr_cleanup 0 nsec
> > 	#13/11   attach_probe/kprobe-long_name:FAIL
> > 	#13      attach_probe:FAIL
> > 
> > 	./test_progs -t attach_probe/uprobe-long_name
> > 	......
> > 	libbpf: failed to add legacy uprobe event for /root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9: -EINVAL
> > 	libbpf: prog 'handle_uprobe': failed to create uprobe '/root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9' perf event: -EINVAL
> > 	test_attach_uprobe_long_event_name:FAIL:attach_uprobe_long_event_name unexpected error: -22
> > 	#13/10   attach_probe/uprobe-long_name:FAIL
> > 	#13      attach_probe:FAIL
> > After Fix:
> > 	./test_progs -t attach_probe/uprobe-long_name
> > 	#13/10   attach_probe/uprobe-long_name:OK
> > 	#13      attach_probe:OK
> > 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> > 
> > 	./test_progs -t attach_probe/kprobe-long_name
> > 	#13/11   attach_probe/kprobe-long_name:OK
> > 	#13      attach_probe:OK
> > 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> > 
> > Fixes: 46ed5fc33db9 ("libbpf: Refactor and simplify legacy kprobe code")
> > Fixes: cc10623c6810 ("libbpf: Add legacy uprobe attaching support")
> > Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> > Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
> > ---
> >  tools/lib/bpf/libbpf.c | 19 ++++++++++++-------
> >  1 file changed, 12 insertions(+), 7 deletions(-)
> > 
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index b2591f5cab65..9e047641e001 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -60,6 +60,8 @@
> >  #define BPF_FS_MAGIC		0xcafe4a11
> >  #endif
> >  
> > +#define MAX_EVENT_NAME_LEN	64
> > +
> >  #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
> >  
> >  #define BPF_INSN_SZ (sizeof(struct bpf_insn))
> > @@ -11142,10 +11144,10 @@ static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
> >  	static int index = 0;
> >  	int i;
> >  
> > -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
> > -		 __sync_fetch_and_add(&index, 1));
> > +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> > +		 __sync_fetch_and_add(&index, 1), kfunc_name, offset);
> 
> so the fix is to move unique id before kfunc_name to make sure it gets
> to the event name right? would be great to have it in changelog
> 

Yes, defining MAX_EVENT_NAME_LEN ensures event names are truncated via snprintf
to prevent exceeding the maximum length limit.
Moving the unique id before kfunc_name avoids truncating the id.
Regarding the changelog: Should this information go into the commit message of the patch, or somewhere else?

> 
> >  
> > -	/* sanitize binary_path in the probe name */
> > +	/* sanitize kfunc_name in the probe name */
> >  	for (i = 0; buf[i]; i++) {
> >  		if (!isalnum(buf[i]))
> >  			buf[i] = '_';
> > @@ -11270,7 +11272,7 @@ int probe_kern_syscall_wrapper(int token_fd)
> >  
> >  		return pfd >= 0 ? 1 : 0;
> >  	} else { /* legacy mode */
> > -		char probe_name[128];
> > +		char probe_name[MAX_EVENT_NAME_LEN];
> >  
> >  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
> >  		if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
> > @@ -11328,7 +11330,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
> >  					    func_name, offset,
> >  					    -1 /* pid */, 0 /* ref_ctr_off */);
> >  	} else {
> > -		char probe_name[256];
> > +		char probe_name[MAX_EVENT_NAME_LEN];
> >  
> >  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
> >  					     func_name, offset);
> > @@ -11878,9 +11880,12 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
> >  static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
> >  					 const char *binary_path, uint64_t offset)
> >  {
> > +	static int index = 0;
> >  	int i;
> >  
> > -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
> > +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> > +		 __sync_fetch_and_add(&index, 1),
> > +		 basename((void *)binary_path), (size_t)offset);
> 
> gen_kprobe_legacy_event_name and gen_uprobe_legacy_event_name seem to
> be identical now, maybe we can have just one ?
> 
> thanks,
> jirka
> 

The gen_uprobe_legacy_event_name function includes an extra basename compared to gen_kprobe_legacy_event_name,
as the prefixes of binary_path are often too similar to distinguish easily.
When merging these two into a single function, is it acceptable to pass basename((void *)binary_path)
directly during the uprobe invocation, or should we remove the addition of basename? Thank you!

> >  
> >  	/* sanitize binary_path in the probe name */
> >  	for (i = 0; buf[i]; i++) {
> > @@ -12312,7 +12317,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
> >  		pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
> >  					    func_offset, pid, ref_ctr_off);
> >  	} else {
> > -		char probe_name[PATH_MAX + 64];
> > +		char probe_name[MAX_EVENT_NAME_LEN];
> >  
> >  		if (ref_ctr_off)
> >  			return libbpf_err_ptr(-EINVAL);
> > -- 
> > 2.43.0
> >
Jiri Olsa April 15, 2025, 7:20 a.m. UTC | #3
On Tue, Apr 15, 2025 at 10:01:15AM +0800, Feng Yang wrote:
> On Mon, 14 Apr 2025 13:43:38 +0200 Jiri Olsa <olsajiri@gmail.com> wrote:
> > On Mon, Apr 14, 2025 at 05:34:00PM +0800, Feng Yang wrote:
> > > From: Feng Yang <yangfeng@kylinos.cn>
> > > 
> > > When the binary path is excessively long, the generated probe_name in libbpf
> > > exceeds the kernel's MAX_EVENT_NAME_LEN limit (64 bytes).
> > > This causes legacy uprobe event attachment to fail with error code -22.
> > > 
> > > Before Fix:
> > > 	./test_progs -t attach_probe/kprobe-long_name
> > > 	......
> > > 	libbpf: failed to add legacy kprobe event for 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0': -EINVAL
> > > 	libbpf: prog 'handle_kprobe': failed to create kprobe 'bpf_kfunc_looooooooooooooooooooooooooooooong_name+0x0' perf event: -EINVAL
> > > 	test_attach_kprobe_long_event_name:FAIL:attach_kprobe_long_event_name unexpected error: -22
> > > 	test_attach_probe:PASS:uprobe_ref_ctr_cleanup 0 nsec
> > > 	#13/11   attach_probe/kprobe-long_name:FAIL
> > > 	#13      attach_probe:FAIL
> > > 
> > > 	./test_progs -t attach_probe/uprobe-long_name
> > > 	......
> > > 	libbpf: failed to add legacy uprobe event for /root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9: -EINVAL
> > > 	libbpf: prog 'handle_uprobe': failed to create uprobe '/root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9' perf event: -EINVAL
> > > 	test_attach_uprobe_long_event_name:FAIL:attach_uprobe_long_event_name unexpected error: -22
> > > 	#13/10   attach_probe/uprobe-long_name:FAIL
> > > 	#13      attach_probe:FAIL
> > > After Fix:
> > > 	./test_progs -t attach_probe/uprobe-long_name
> > > 	#13/10   attach_probe/uprobe-long_name:OK
> > > 	#13      attach_probe:OK
> > > 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> > > 
> > > 	./test_progs -t attach_probe/kprobe-long_name
> > > 	#13/11   attach_probe/kprobe-long_name:OK
> > > 	#13      attach_probe:OK
> > > 	Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
> > > 
> > > Fixes: 46ed5fc33db9 ("libbpf: Refactor and simplify legacy kprobe code")
> > > Fixes: cc10623c6810 ("libbpf: Add legacy uprobe attaching support")
> > > Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> > > Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
> > > ---
> > >  tools/lib/bpf/libbpf.c | 19 ++++++++++++-------
> > >  1 file changed, 12 insertions(+), 7 deletions(-)
> > > 
> > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > > index b2591f5cab65..9e047641e001 100644
> > > --- a/tools/lib/bpf/libbpf.c
> > > +++ b/tools/lib/bpf/libbpf.c
> > > @@ -60,6 +60,8 @@
> > >  #define BPF_FS_MAGIC		0xcafe4a11
> > >  #endif
> > >  
> > > +#define MAX_EVENT_NAME_LEN	64
> > > +
> > >  #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
> > >  
> > >  #define BPF_INSN_SZ (sizeof(struct bpf_insn))
> > > @@ -11142,10 +11144,10 @@ static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
> > >  	static int index = 0;
> > >  	int i;
> > >  
> > > -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
> > > -		 __sync_fetch_and_add(&index, 1));
> > > +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> > > +		 __sync_fetch_and_add(&index, 1), kfunc_name, offset);
> > 
> > so the fix is to move unique id before kfunc_name to make sure it gets
> > to the event name right? would be great to have it in changelog
> > 
> 
> Yes, defining MAX_EVENT_NAME_LEN ensures event names are truncated via snprintf
> to prevent exceeding the maximum length limit.
> Moving the unique id before kfunc_name avoids truncating the id.
> Regarding the changelog: Should this information go into the commit message of the patch, or somewhere else?

having this in changelog would help

> 
> > 
> > >  
> > > -	/* sanitize binary_path in the probe name */
> > > +	/* sanitize kfunc_name in the probe name */
> > >  	for (i = 0; buf[i]; i++) {
> > >  		if (!isalnum(buf[i]))
> > >  			buf[i] = '_';
> > > @@ -11270,7 +11272,7 @@ int probe_kern_syscall_wrapper(int token_fd)
> > >  
> > >  		return pfd >= 0 ? 1 : 0;
> > >  	} else { /* legacy mode */
> > > -		char probe_name[128];
> > > +		char probe_name[MAX_EVENT_NAME_LEN];
> > >  
> > >  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
> > >  		if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
> > > @@ -11328,7 +11330,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
> > >  					    func_name, offset,
> > >  					    -1 /* pid */, 0 /* ref_ctr_off */);
> > >  	} else {
> > > -		char probe_name[256];
> > > +		char probe_name[MAX_EVENT_NAME_LEN];
> > >  
> > >  		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
> > >  					     func_name, offset);
> > > @@ -11878,9 +11880,12 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
> > >  static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
> > >  					 const char *binary_path, uint64_t offset)
> > >  {
> > > +	static int index = 0;
> > >  	int i;
> > >  
> > > -	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
> > > +	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
> > > +		 __sync_fetch_and_add(&index, 1),
> > > +		 basename((void *)binary_path), (size_t)offset);
> > 
> > gen_kprobe_legacy_event_name and gen_uprobe_legacy_event_name seem to
> > be identical now, maybe we can have just one ?
> > 
> > thanks,
> > jirka
> > 
> 
> The gen_uprobe_legacy_event_name function includes an extra basename compared to gen_kprobe_legacy_event_name,
> as the prefixes of binary_path are often too similar to distinguish easily.
> When merging these two into a single function, is it acceptable to pass basename((void *)binary_path)
> directly during the uprobe invocation, or should we remove the addition of basename? Thank you!

I think basename is fine, perhaps just pass it as argument
like below (on top of your change, untested)

jirka


---
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 9e047641e001..93e804b25da1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -11138,14 +11138,13 @@ static const char *tracefs_available_filter_functions_addrs(void)
 			     : TRACEFS"/available_filter_functions_addrs";
 }
 
-static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
-					 const char *kfunc_name, size_t offset)
+static void gen_legacy_event_name(char *buf, size_t buf_sz, const char *name, size_t offset)
 {
 	static int index = 0;
 	int i;
 
 	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
-		 __sync_fetch_and_add(&index, 1), kfunc_name, offset);
+		 __sync_fetch_and_add(&index, 1), name, offset);
 
 	/* sanitize kfunc_name in the probe name */
 	for (i = 0; buf[i]; i++) {
@@ -11274,7 +11273,7 @@ int probe_kern_syscall_wrapper(int token_fd)
 	} else { /* legacy mode */
 		char probe_name[MAX_EVENT_NAME_LEN];
 
-		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
+		gen_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
 		if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
 			return 0;
 
@@ -11332,8 +11331,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 	} else {
 		char probe_name[MAX_EVENT_NAME_LEN];
 
-		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
-					     func_name, offset);
+		gen_legacy_event_name(probe_name, sizeof(probe_name), func_name, offset);
 
 		legacy_probe = strdup(probe_name);
 		if (!legacy_probe)
@@ -11877,23 +11875,6 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
 	return ret;
 }
 
-static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
-					 const char *binary_path, uint64_t offset)
-{
-	static int index = 0;
-	int i;
-
-	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
-		 __sync_fetch_and_add(&index, 1),
-		 basename((void *)binary_path), (size_t)offset);
-
-	/* sanitize binary_path in the probe name */
-	for (i = 0; buf[i]; i++) {
-		if (!isalnum(buf[i]))
-			buf[i] = '_';
-	}
-}
-
 static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
 					  const char *binary_path, size_t offset)
 {
@@ -12322,8 +12303,8 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 		if (ref_ctr_off)
 			return libbpf_err_ptr(-EINVAL);
 
-		gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
-					     binary_path, func_offset);
+		gen_legacy_event_name(probe_name, sizeof(probe_name),
+				      basename((char *) binary_path), func_offset);
 
 		legacy_probe = strdup(probe_name);
 		if (!legacy_probe)
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b2591f5cab65..9e047641e001 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -60,6 +60,8 @@ 
 #define BPF_FS_MAGIC		0xcafe4a11
 #endif
 
+#define MAX_EVENT_NAME_LEN	64
+
 #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
 
 #define BPF_INSN_SZ (sizeof(struct bpf_insn))
@@ -11142,10 +11144,10 @@  static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
 	static int index = 0;
 	int i;
 
-	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
-		 __sync_fetch_and_add(&index, 1));
+	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
+		 __sync_fetch_and_add(&index, 1), kfunc_name, offset);
 
-	/* sanitize binary_path in the probe name */
+	/* sanitize kfunc_name in the probe name */
 	for (i = 0; buf[i]; i++) {
 		if (!isalnum(buf[i]))
 			buf[i] = '_';
@@ -11270,7 +11272,7 @@  int probe_kern_syscall_wrapper(int token_fd)
 
 		return pfd >= 0 ? 1 : 0;
 	} else { /* legacy mode */
-		char probe_name[128];
+		char probe_name[MAX_EVENT_NAME_LEN];
 
 		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
 		if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
@@ -11328,7 +11330,7 @@  bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 					    func_name, offset,
 					    -1 /* pid */, 0 /* ref_ctr_off */);
 	} else {
-		char probe_name[256];
+		char probe_name[MAX_EVENT_NAME_LEN];
 
 		gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
 					     func_name, offset);
@@ -11878,9 +11880,12 @@  static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
 static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
 					 const char *binary_path, uint64_t offset)
 {
+	static int index = 0;
 	int i;
 
-	snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
+	snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
+		 __sync_fetch_and_add(&index, 1),
+		 basename((void *)binary_path), (size_t)offset);
 
 	/* sanitize binary_path in the probe name */
 	for (i = 0; buf[i]; i++) {
@@ -12312,7 +12317,7 @@  bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 		pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
 					    func_offset, pid, ref_ctr_off);
 	} else {
-		char probe_name[PATH_MAX + 64];
+		char probe_name[MAX_EVENT_NAME_LEN];
 
 		if (ref_ctr_off)
 			return libbpf_err_ptr(-EINVAL);