From: Ian Rogers <irogers@google.com>
To: Alan Maguire <alan.maguire@oracle.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Ian Rogers <irogers@google.com>
Subject: [PATCH v3] libbpf: Add some details for BTF parsing failures
Date: Thu, 25 Jan 2024 15:18:40 -0800 [thread overview]
Message-ID: <20240125231840.1647951-1-irogers@google.com> (raw)
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(-)
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
next reply other threads:[~2024-01-25 23:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-25 23:18 Ian Rogers [this message]
2024-01-30 0:43 ` [PATCH v3] libbpf: Add some details for BTF parsing failures Andrii Nakryiko
2024-01-30 13:41 ` Ian Rogers
2024-01-30 0:50 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240125231840.1647951-1-irogers@google.com \
--to=irogers@google.com \
--cc=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox