On Sat, May 10, 2025 at 5:35 AM Lorenz Bauer <l...@isovalent.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 <l...@isovalent.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 <and...@kernel.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
>

Reply via email to