libbpf: handle unsupported mmap-based /sys/kernel/btf/vmlinux correctly
Andrii Nakryiko
maintainerPATCH 1/1 libbpf: handle unsupported mmap-based /sys/kernel/btf/vmlinux correctly
libbpf_err_ptr() helpers are meant to return NULL and set errno, if there is an error. But btf_parse_raw_mmap() is meant to be used internally and is expected to return ERR_PTR() values. Because of this mismatch, when libbpf tries to mmap /sys/kernel/btf/vmlinux, we don't detect the error correctly with IS_ERR() check, and never fallback to old non-mmap-based way of loading vmlinux BTF.
Fix this by using proper ERR_PTR() returns internally.
--- tools/lib/bpf/btf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +3 -3
@@ -1384,12 +1384,12 @@ static struct btf *btf_parse_raw_mmap(const char *path, struct btf *base_btf) fd =open (path,O_RDONLY );if (fd <0 ) -return libbpf_err_ptr (-errno); +return ERR_PTR (-errno);if (fstat (fd, &st) <0 ) { err = -errno;close (fd); -return libbpf_err_ptr (err); +return ERR_PTR (err); } data =mmap (NULL , st.st_size,PROT_READ ,MAP_PRIVATE , fd,0 );
@@ -1397,7 +1397,7 @@ static struct btf *btf_parse_raw_mmap(const char *path, struct btf *base_btf)close (fd);if (data ==MAP_FAILED ) -return libbpf_err_ptr (err); +return ERR_PTR (err); btf =btf_new (data, st.st_size, base_btf, true);if (IS_ERR (btf))
Signature · 1 line
2.47.1
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Fri, 6 Jun 2025 13:21:34 -0700 you wrote:
libbpf_err_ptr()helpers are meant to return NULL and set errno, if there is an error. Butbtf_parse_raw_mmap()is meant to be used internally and is expected to returnERR_PTR()values. Because of this mismatch, when libbpf tries to mmap/sys/kernel/btf/vmlinux, we don't detect the error correctly withIS_ERR()check, and never fallback to old non-mmap-based way of loading vmlinux BTF.[...]
Here is the summary with links:
- [bpf] libbpf: handle unsupported mmap-based /sys/kernel/btf/vmlinux correctly
https://git.kernel.org/bpf/bpf/c/02670deede22
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html