diff mbox series

[v3] libbpf: Add some details for BTF parsing failures

Message ID 20240125231840.1647951-1-irogers@google.com (mailing list archive)
State Accepted
Commit f2e4040c82d3fddd11fa7c64e8f810e6f9cb7460
Delegated to: BPF
Headers show
Series [v3] libbpf: Add some details for BTF parsing failures | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-45 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-46 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-43 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-44 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-47 success Logs for x86_64-llvm-18 / veristat
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 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-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-23 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-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-24 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-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-31 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-32 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-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-33 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-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 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-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-41 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-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 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-39 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-37 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-30 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-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-4 pending Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 pending Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-VM_Test-9 pending Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-10 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-VM_Test-11 pending Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-12 pending Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-VM_Test-13 pending Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-14 pending Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization

Commit Message

Ian Rogers Jan. 25, 2024, 11:18 p.m. UTC
As CONFIG_DEBUG_INFO_BTF is default off the existing "failed to find
valid kernel BTF" message makes diagnosing the kernel build issue some
what cryptic. Add a little more detail with the hope of helping users.

Before:
```
libbpf: failed to find valid kernel BTF
libbpf: Error loading vmlinux BTF: -3
```

After not accessible:
```
libbpf: access to canonical vmlinux (/sys/kernel/btf/vmlinux) to load BTF failed: No such file or directory
libbpf: was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find valid kernel BTF
libbpf: Error loading vmlinux BTF: -3
```

After not readable:
```
libbpf: unable to read canonical vmlinux (/sys/kernel/btf/vmlinux): Permission denied
libbpf: failed to find valid kernel BTF
libbpf: Error loading vmlinux BTF: -3
```

Closes: https://lore.kernel.org/bpf/CAP-5=fU+DN_+Y=Y4gtELUsJxKNDDCOvJzPHvjUVaUoeFAzNnig@mail.gmail.com/
Signed-off-by: Ian Rogers <irogers@google.com>

---
v3. Try to address review comments from Andrii Nakryiko.
---
 tools/lib/bpf/btf.c | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

Comments

Andrii Nakryiko Jan. 30, 2024, 12:43 a.m. UTC | #1
On Thu, Jan 25, 2024 at 3:18 PM Ian Rogers <irogers@google.com> wrote:
>
> As CONFIG_DEBUG_INFO_BTF is default off the existing "failed to find
> valid kernel BTF" message makes diagnosing the kernel build issue some
> what cryptic. Add a little more detail with the hope of helping users.
>
> Before:
> ```
> libbpf: failed to find valid kernel BTF
> libbpf: Error loading vmlinux BTF: -3
> ```
>
> After not accessible:
> ```
> libbpf: access to canonical vmlinux (/sys/kernel/btf/vmlinux) to load BTF failed: No such file or directory
> libbpf: was CONFIG_DEBUG_INFO_BTF enabled?
> libbpf: failed to find valid kernel BTF
> libbpf: Error loading vmlinux BTF: -3
> ```
>
> After not readable:
> ```
> libbpf: unable to read canonical vmlinux (/sys/kernel/btf/vmlinux): Permission denied
> libbpf: failed to find valid kernel BTF
> libbpf: Error loading vmlinux BTF: -3
> ```
>
> Closes: https://lore.kernel.org/bpf/CAP-5=fU+DN_+Y=Y4gtELUsJxKNDDCOvJzPHvjUVaUoeFAzNnig@mail.gmail.com/
> Signed-off-by: Ian Rogers <irogers@google.com>
>
> ---
> v3. Try to address review comments from Andrii Nakryiko.

I did some further simplifications and clean ups while applying.

I dropped an extra faccessat(R_OK) check for /sys/kernel/btf/vmlinux
and instead if F_OK passes, just go ahead and try to parse
/sys/kernel/btf/vmlinux. If we have no access, we should get -EPERM or
-EACCESS (I didn't check which), otherwise we'll either parse or won't
find any BTF, both are errors. If /sys/kernel/btf/vmlinux exists,
there seems to be little point nowadays to try fallback locations,
kernel clearly is modern enough to generate /sys/kernel/btf/vmlinux,
so we just bail out with error.

Please check the landed commit in bpf-next and let me know if it
doesn't cover your use case properly.

> ---
>  tools/lib/bpf/btf.c | 35 +++++++++++++++++++++++++++--------
>  1 file changed, 27 insertions(+), 8 deletions(-)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index ec92b87cae01..45983f42aba9 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -4932,10 +4932,9 @@ static int btf_dedup_remap_types(struct btf_dedup *d)
>   */
>  struct btf *btf__load_vmlinux_btf(void)
>  {
> +       const char *canonical_vmlinux = "/sys/kernel/btf/vmlinux";
> +       /* fall back locations, trying to find vmlinux on disk */
>         const char *locations[] = {
> -               /* try canonical vmlinux BTF through sysfs first */
> -               "/sys/kernel/btf/vmlinux",
> -               /* fall back to trying to find vmlinux on disk otherwise */
>                 "/boot/vmlinux-%1$s",
>                 "/lib/modules/%1$s/vmlinux-%1$s",
>                 "/lib/modules/%1$s/build/vmlinux",
> @@ -4946,14 +4945,34 @@ struct btf *btf__load_vmlinux_btf(void)
>         };
>         char path[PATH_MAX + 1];
>         struct utsname buf;
> -       struct btf *btf;
> +       struct btf *btf = NULL;
>         int i, err;
>
> -       uname(&buf);
> +       /* is canonical sysfs location accessible? */
> +       err = faccessat(AT_FDCWD, canonical_vmlinux, F_OK, AT_EACCESS);
> +       if (err) {
> +               pr_warn("access to canonical vmlinux (%s) to load BTF failed: %s\n",
> +                       canonical_vmlinux, strerror(errno));
> +               pr_warn("was CONFIG_DEBUG_INFO_BTF enabled?\n");
> +       } else {
> +               err = faccessat(AT_FDCWD, canonical_vmlinux, R_OK, AT_EACCESS);
> +               if (err) {
> +                       pr_warn("unable to read canonical vmlinux (%s): %s\n",
> +                               canonical_vmlinux, strerror(errno));
> +               }
> +       }
> +       if (!err) {
> +               /* load canonical and return any parsing failures */
> +               btf = btf__parse(canonical_vmlinux, NULL);
> +               err = libbpf_get_error(btf);
> +               pr_debug("loading kernel BTF '%s': %d\n", canonical_vmlinux, err);
> +               return btf;
> +       }
>
> +       /* try fallback locations */
> +       uname(&buf);
>         for (i = 0; i < ARRAY_SIZE(locations); i++) {
>                 snprintf(path, PATH_MAX, locations[i], buf.release);
> -
>                 if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS))
>                         continue;
>
> @@ -4965,9 +4984,9 @@ struct btf *btf__load_vmlinux_btf(void)
>
>                 return btf;
>         }
> -
>         pr_warn("failed to find valid kernel BTF\n");
> -       return libbpf_err_ptr(-ESRCH);
> +       /* return the last error or ESRCH if no fallback locations were found */
> +       return btf ?: libbpf_err_ptr(-ESRCH);
>  }
>
>  struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf")));
> --
> 2.43.0.429.g432eaa2c6b-goog
>
patchwork-bot+netdevbpf@kernel.org Jan. 30, 2024, 12:50 a.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 25 Jan 2024 15:18:40 -0800 you wrote:
> As CONFIG_DEBUG_INFO_BTF is default off the existing "failed to find
> valid kernel BTF" message makes diagnosing the kernel build issue some
> what cryptic. Add a little more detail with the hope of helping users.
> 
> Before:
> ```
> libbpf: failed to find valid kernel BTF
> libbpf: Error loading vmlinux BTF: -3
> ```
> 
> [...]

Here is the summary with links:
  - [v3] libbpf: Add some details for BTF parsing failures
    https://git.kernel.org/bpf/bpf-next/c/f2e4040c82d3

You are awesome, thank you!
Ian Rogers Jan. 30, 2024, 1:41 p.m. UTC | #3
On Mon, Jan 29, 2024 at 4:43 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Jan 25, 2024 at 3:18 PM Ian Rogers <irogers@google.com> wrote:
> >
> > As CONFIG_DEBUG_INFO_BTF is default off the existing "failed to find
> > valid kernel BTF" message makes diagnosing the kernel build issue some
> > what cryptic. Add a little more detail with the hope of helping users.
> >
> > Before:
> > ```
> > libbpf: failed to find valid kernel BTF
> > libbpf: Error loading vmlinux BTF: -3
> > ```
> >
> > After not accessible:
> > ```
> > libbpf: access to canonical vmlinux (/sys/kernel/btf/vmlinux) to load BTF failed: No such file or directory
> > libbpf: was CONFIG_DEBUG_INFO_BTF enabled?
> > libbpf: failed to find valid kernel BTF
> > libbpf: Error loading vmlinux BTF: -3
> > ```
> >
> > After not readable:
> > ```
> > libbpf: unable to read canonical vmlinux (/sys/kernel/btf/vmlinux): Permission denied
> > libbpf: failed to find valid kernel BTF
> > libbpf: Error loading vmlinux BTF: -3
> > ```
> >
> > Closes: https://lore.kernel.org/bpf/CAP-5=fU+DN_+Y=Y4gtELUsJxKNDDCOvJzPHvjUVaUoeFAzNnig@mail.gmail.com/
> > Signed-off-by: Ian Rogers <irogers@google.com>
> >
> > ---
> > v3. Try to address review comments from Andrii Nakryiko.
>
> I did some further simplifications and clean ups while applying.
>
> I dropped an extra faccessat(R_OK) check for /sys/kernel/btf/vmlinux
> and instead if F_OK passes, just go ahead and try to parse
> /sys/kernel/btf/vmlinux. If we have no access, we should get -EPERM or
> -EACCESS (I didn't check which), otherwise we'll either parse or won't
> find any BTF, both are errors. If /sys/kernel/btf/vmlinux exists,
> there seems to be little point nowadays to try fallback locations,
> kernel clearly is modern enough to generate /sys/kernel/btf/vmlinux,
> so we just bail out with error.
>
> Please check the landed commit in bpf-next and let me know if it
> doesn't cover your use case properly.

It does, thanks Andrii!

Ian

> > ---
> >  tools/lib/bpf/btf.c | 35 +++++++++++++++++++++++++++--------
> >  1 file changed, 27 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > index ec92b87cae01..45983f42aba9 100644
> > --- a/tools/lib/bpf/btf.c
> > +++ b/tools/lib/bpf/btf.c
> > @@ -4932,10 +4932,9 @@ static int btf_dedup_remap_types(struct btf_dedup *d)
> >   */
> >  struct btf *btf__load_vmlinux_btf(void)
> >  {
> > +       const char *canonical_vmlinux = "/sys/kernel/btf/vmlinux";
> > +       /* fall back locations, trying to find vmlinux on disk */
> >         const char *locations[] = {
> > -               /* try canonical vmlinux BTF through sysfs first */
> > -               "/sys/kernel/btf/vmlinux",
> > -               /* fall back to trying to find vmlinux on disk otherwise */
> >                 "/boot/vmlinux-%1$s",
> >                 "/lib/modules/%1$s/vmlinux-%1$s",
> >                 "/lib/modules/%1$s/build/vmlinux",
> > @@ -4946,14 +4945,34 @@ struct btf *btf__load_vmlinux_btf(void)
> >         };
> >         char path[PATH_MAX + 1];
> >         struct utsname buf;
> > -       struct btf *btf;
> > +       struct btf *btf = NULL;
> >         int i, err;
> >
> > -       uname(&buf);
> > +       /* is canonical sysfs location accessible? */
> > +       err = faccessat(AT_FDCWD, canonical_vmlinux, F_OK, AT_EACCESS);
> > +       if (err) {
> > +               pr_warn("access to canonical vmlinux (%s) to load BTF failed: %s\n",
> > +                       canonical_vmlinux, strerror(errno));
> > +               pr_warn("was CONFIG_DEBUG_INFO_BTF enabled?\n");
> > +       } else {
> > +               err = faccessat(AT_FDCWD, canonical_vmlinux, R_OK, AT_EACCESS);
> > +               if (err) {
> > +                       pr_warn("unable to read canonical vmlinux (%s): %s\n",
> > +                               canonical_vmlinux, strerror(errno));
> > +               }
> > +       }
> > +       if (!err) {
> > +               /* load canonical and return any parsing failures */
> > +               btf = btf__parse(canonical_vmlinux, NULL);
> > +               err = libbpf_get_error(btf);
> > +               pr_debug("loading kernel BTF '%s': %d\n", canonical_vmlinux, err);
> > +               return btf;
> > +       }
> >
> > +       /* try fallback locations */
> > +       uname(&buf);
> >         for (i = 0; i < ARRAY_SIZE(locations); i++) {
> >                 snprintf(path, PATH_MAX, locations[i], buf.release);
> > -
> >                 if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS))
> >                         continue;
> >
> > @@ -4965,9 +4984,9 @@ struct btf *btf__load_vmlinux_btf(void)
> >
> >                 return btf;
> >         }
> > -
> >         pr_warn("failed to find valid kernel BTF\n");
> > -       return libbpf_err_ptr(-ESRCH);
> > +       /* return the last error or ESRCH if no fallback locations were found */
> > +       return btf ?: libbpf_err_ptr(-ESRCH);
> >  }
> >
> >  struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf")));
> > --
> > 2.43.0.429.g432eaa2c6b-goog
> >
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index ec92b87cae01..45983f42aba9 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -4932,10 +4932,9 @@  static int btf_dedup_remap_types(struct btf_dedup *d)
  */
 struct btf *btf__load_vmlinux_btf(void)
 {
+	const char *canonical_vmlinux = "/sys/kernel/btf/vmlinux";
+	/* fall back locations, trying to find vmlinux on disk */
 	const char *locations[] = {
-		/* try canonical vmlinux BTF through sysfs first */
-		"/sys/kernel/btf/vmlinux",
-		/* fall back to trying to find vmlinux on disk otherwise */
 		"/boot/vmlinux-%1$s",
 		"/lib/modules/%1$s/vmlinux-%1$s",
 		"/lib/modules/%1$s/build/vmlinux",
@@ -4946,14 +4945,34 @@  struct btf *btf__load_vmlinux_btf(void)
 	};
 	char path[PATH_MAX + 1];
 	struct utsname buf;
-	struct btf *btf;
+	struct btf *btf = NULL;
 	int i, err;
 
-	uname(&buf);
+	/* is canonical sysfs location accessible? */
+	err = faccessat(AT_FDCWD, canonical_vmlinux, F_OK, AT_EACCESS);
+	if (err) {
+		pr_warn("access to canonical vmlinux (%s) to load BTF failed: %s\n",
+			canonical_vmlinux, strerror(errno));
+		pr_warn("was CONFIG_DEBUG_INFO_BTF enabled?\n");
+	} else {
+		err = faccessat(AT_FDCWD, canonical_vmlinux, R_OK, AT_EACCESS);
+		if (err) {
+			pr_warn("unable to read canonical vmlinux (%s): %s\n",
+				canonical_vmlinux, strerror(errno));
+		}
+	}
+	if (!err) {
+		/* load canonical and return any parsing failures */
+		btf = btf__parse(canonical_vmlinux, NULL);
+		err = libbpf_get_error(btf);
+		pr_debug("loading kernel BTF '%s': %d\n", canonical_vmlinux, err);
+		return btf;
+	}
 
+	/* try fallback locations */
+	uname(&buf);
 	for (i = 0; i < ARRAY_SIZE(locations); i++) {
 		snprintf(path, PATH_MAX, locations[i], buf.release);
-
 		if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS))
 			continue;
 
@@ -4965,9 +4984,9 @@  struct btf *btf__load_vmlinux_btf(void)
 
 		return btf;
 	}
-
 	pr_warn("failed to find valid kernel BTF\n");
-	return libbpf_err_ptr(-ESRCH);
+	/* return the last error or ESRCH if no fallback locations were found */
+	return btf ?: libbpf_err_ptr(-ESRCH);
 }
 
 struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf")));