Skip to content

Commit

Permalink
libbpf: Add btf__set_fd() for more control over loaded BTF FD
Browse files Browse the repository at this point in the history
Add setter for BTF FD to allow application more fine-grained control in more
advanced scenarios. Storing BTF FD inside `struct btf` provides little benefit
and probably would be better done differently (e.g., btf__load() could just
return FD on success), but we are stuck with this due to backwards
compatibility. The main problem is that it's impossible to load BTF and than
free user-space memory, but keep FD intact, because `struct btf` assumes
ownership of that FD upon successful load and will attempt to close it during
btf__free(). To allow callers (e.g., libbpf itself for BTF sanitization) to
have more control over this, add btf__set_fd() to allow to reset FD
arbitrarily, if necessary.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200708015318.3827358-3-andriin@fb.com
  • Loading branch information
Andrii Nakryiko authored and Daniel Borkmann committed Jul 8, 2020
1 parent bfc9665 commit 81372e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tools/lib/bpf/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void btf__free(struct btf *btf)
if (!btf)
return;

if (btf->fd != -1)
if (btf->fd >= 0)
close(btf->fd);

free(btf->data);
Expand Down Expand Up @@ -700,6 +700,11 @@ int btf__fd(const struct btf *btf)
return btf->fd;
}

void btf__set_fd(struct btf *btf, int fd)
{
btf->fd = fd;
}

const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
{
*size = btf->data_size;
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/btf.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
LIBBPF_API int btf__fd(const struct btf *btf);
LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,5 @@ LIBBPF_0.1.0 {
bpf_map__value_size;
bpf_program__autoload;
bpf_program__set_autoload;
btf__set_fd;
} LIBBPF_0.0.9;

0 comments on commit 81372e1

Please sign in to comment.