[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEf4BzaPKc_ocE+XTEw6bU0GQ1KOVp+UosZkVKZcqWcJed2ebg@mail.gmail.com>
Date: Mon, 12 May 2025 15:33:58 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Lorenz Bauer <lmb@...valent.com>
Cc: Arnd Bergmann <arnd@...db.de>, Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>, Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>,
linux-arch@...r.kernel.org, linux-kernel@...r.kernel.org, bpf@...r.kernel.org,
linux-kselftest@...r.kernel.org
Subject: Re: [PATCH bpf-next v4 3/3] libbpf: Use mmap to parse vmlinux BTF
from sysfs
On Sat, May 10, 2025 at 5:35 AM Lorenz Bauer <lmb@...valent.com> wrote:
>
> Teach libbpf to use mmap when parsing vmlinux BTF from /sys. We don't
> apply this to fall-back paths on the regular file system because there
> is no way to ensure that modifications underlying the MAP_PRIVATE
> mapping are not visible to the process.
>
> Signed-off-by: Lorenz Bauer <lmb@...valent.com>
> ---
> tools/lib/bpf/btf.c | 85 +++++++++++++++++++++++++++++++++++++++++++----------
> 1 file changed, 69 insertions(+), 16 deletions(-)
>
Almost there, see below. With those changes feel free to add my ack
Acked-by: Andrii Nakryiko <andrii@...nel.org>
> +static struct btf *btf_parse_raw_mmap(const char *path, struct btf *base_btf)
> +{
> + struct stat st;
> + void *data;
> + struct btf *btf;
> + int fd, err;
> +
> + fd = open(path, O_RDONLY);
> + if (fd < 0)
> + return libbpf_err_ptr(-errno);
> +
> + if (fstat(fd, &st) < 0) {
> + err = -errno;
> + close(fd);
> + return libbpf_err_ptr(err);
> + }
> +
> + data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
err = -errno;
> + close(fd);
> +
> + if (data == MAP_FAILED)
> + return NULL;
s/return NULL;/libbpf_err_ptr(err);/
> +
> + btf = btf_new(data, st.st_size, base_btf, true);
> + if (IS_ERR(btf))
> + munmap(data, st.st_size);
> +
> + return btf;
> +}
> +
> static struct btf *btf_parse(const char *path, struct btf *base_btf, struct btf_ext **btf_ext)
> {
> struct btf *btf;
> @@ -1618,7 +1669,7 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
> goto exit_free;
> }
>
> - btf = btf_new(ptr, btf_info.btf_size, base_btf);
> + btf = btf_new(ptr, btf_info.btf_size, base_btf, false);
>
> exit_free:
> free(ptr);
> @@ -1659,8 +1710,7 @@ struct btf *btf__load_from_kernel_by_id(__u32 id)
> static void btf_invalidate_raw_data(struct btf *btf)
> {
> if (btf->raw_data) {
> - free(btf->raw_data);
> - btf->raw_data = NULL;
> + btf_free_raw_data(btf);
> }
drop now unnecessary {} ?
> if (btf->raw_data_swapped) {
> free(btf->raw_data_swapped);
> @@ -5331,7 +5381,10 @@ struct btf *btf__load_vmlinux_btf(void)
> pr_warn("kernel BTF is missing at '%s', was CONFIG_DEBUG_INFO_BTF enabled?\n",
> sysfs_btf_path);
> } else {
> - btf = btf__parse(sysfs_btf_path, NULL);
> + btf = btf_parse_raw_mmap(sysfs_btf_path, NULL);
> + if (IS_ERR_OR_NULL(btf))
just IS_ERR() with the fixes I pointed out above
> + btf = btf__parse(sysfs_btf_path, NULL);
> +
> if (!btf) {
> err = -errno;
> pr_warn("failed to read kernel BTF from '%s': %s\n",
>
> --
> 2.49.0
>
Powered by blists - more mailing lists