libbpf: Prefer mounted tracefs over debugfs
PATCH 1/1 libbpf: Prefer mounted tracefs over debugfs
The tracefs path helpers look up /sys/kernel/debug/tracing first, even when tracefs is already mounted at /sys/kernel/tracing. This can trigger an unnecessary legacy tracefs mount and selects a path beneath debugfs, which is commonly restricted.
Prefer the direct tracefs mount when statfs() reports TRACEFS_MAGIC. Checking the filesystem type avoids selecting an existing but unmounted sysfs mountpoint. Preserve the existing debugfs fallback and the default tracefs path when neither location is available.
---
Validation on Fedora 44 (aarch64):
- Built static and shared libbpf with the default warning settings.
- Exercised all five path helpers using wrapped syscalls for both mounts, tracefs only, debugfs only, an unmounted tracefs directory, and neither path available. The original code fails the both-mounts scenario.
- Confirmed the direct tracefs paths on the host's mounted tracefs. Privileged BPF attach/mount tests were not run.
tools/lib/bpf/libbpf.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +13 -4
@@ -59,6 +59,10 @@#define BPF_FS_MAGIC 0xcafe4a11#endif +#ifndef TRACEFS_MAGIC +#define TRACEFS_MAGIC 0x74726163 +#endif +#define MAX_EVENT_NAME_LEN 64#define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
@@ -12713,12 +12717,17 @@ static int append_to_file(const char *file, const char *fmt, ...)static bool use_debugfs (void ) { -static int has_debugfs =-1 ; +static int needs_debugfs =-1 ; +struct statfs st_fs; -if (has_debugfs <0 ) - has_debugfs =faccessat (AT_FDCWD ,DEBUGFS ,F_OK ,AT_EACCESS ) ==0 ; +if (needs_debugfs <0 ) { +if (statfs (TRACEFS , &st_fs) ==0 && st_fs.f_type ==TRACEFS_MAGIC ) + needs_debugfs =0 ; +else + needs_debugfs =faccessat (AT_FDCWD ,DEBUGFS ,F_OK ,AT_EACCESS ) ==0 ; + } -return has_debugfs ==1 ; +return needs_debugfs ==1 ; }static const char *tracefs_path (void )
base-commit: a6303535c2ad9d49598d52556183911f02287791
Signature · 1 line
2.55.0
Ihor Solodrai
reviewerQuoted 4 lines · Eduard Zateev
The tracefs path helpers look up
/sys/kernel/debug/tracingfirst, even when tracefs is already mounted at/sys/kernel/tracing. This can trigger an unnecessary legacy tracefs mount and selects a path beneath debugfs, which is commonly restricted.
This doesn't describe the actual problem. If debugfs is restricted or not mounted, the existing faccessat() check already fails and libbpf falls back to tracefs.
It looks like the actual trivial improvement is in avoiding the debugfs/tracing automount causing:
NOTICE: Automounting of tracing to debugfs is deprecated and will be removed in 2030
in the kernel logs whenever debugfs/tracing gets automounted. libbpf triggers that on legacy kprobe/uprobe/tracepoint attach when debugfs is mounted.
See commit 9ba817fb7c6a ("tracing: Deprecate auto-mounting tracefs in debugfs") and CONFIG_TRACEFS_AUTOMOUNT_DEPRECATED.
Quoted 10 lines
Prefer the direct tracefs mount when
statfs()reportsTRACEFS_MAGIC. Checking the filesystem type avoids selecting an existing but unmounted sysfs mountpoint. Preserve the existing debugfs fallback and the default tracefs path when neither location is available.Fixes a1ac9fd6c650 ("libbpf: fallback to tracefs mount point if debugfs is not mounted")Assisted-by LLMSigned-off-by Eduard Zateev <[email protected]>
Please update the commit message to show the actual consequence of the change. And drop both fixes and assisted-by tags. This is not a bug, and assited-by has been recently deprecated.
The diff looks ok to me, but tbh I don't see much value in it. Any particular reason you decided to fix this?
pw-bot: cr
Quoted 52 lines
---
Validation on Fedora 44 (aarch64):
- Built static and shared libbpf with the default warning settings.
- Exercised all five path helpers using wrapped syscalls for both mounts, tracefs only, debugfs only, an unmounted tracefs directory, and neither path available. The original code fails the both-mounts scenario.
- Confirmed the direct tracefs paths on the host's mounted tracefs. Privileged BPF attach/mount tests were not run.tools/lib/bpf/libbpf.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +13
-4@@ -59,6 +59,10 @@#define BPF_FS_MAGIC 0xcafe4a11#endif +#ifndef TRACEFS_MAGIC +#define TRACEFS_MAGIC 0x74726163 +#endif +#define MAX_EVENT_NAME_LEN 64#define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"@@ -12713,12 +12717,17 @@ static int append_to_file(const char *file, const char *fmt, ...)static bool use_debugfs (void ) { -static int has_debugfs =-1 ; +static int needs_debugfs =-1 ; +struct statfs st_fs; -if (has_debugfs <0 ) - has_debugfs =faccessat (AT_FDCWD ,DEBUGFS ,F_OK ,AT_EACCESS ) ==0 ; +if (needs_debugfs <0 ) { +if (statfs (TRACEFS , &st_fs) ==0 && st_fs.f_type ==TRACEFS_MAGIC ) + needs_debugfs =0 ; +else + needs_debugfs =faccessat (AT_FDCWD ,DEBUGFS ,F_OK ,AT_EACCESS ) ==0 ; + } -return has_debugfs ==1 ; +return needs_debugfs ==1 ; }static const char *tracefs_path (void )base-commit:
a6303535c2ad9d49598d52556183911f02287791