Commit b6b466a8 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'libbpf: split BTF support'



Andrii Nakryiko says:

====================
This patch set adds support for generating and deduplicating split BTF. This
is an enhancement to the BTF, which allows to designate one BTF as the "base
BTF" (e.g., vmlinux BTF), and one or more other BTFs as "split BTF" (e.g.,
kernel module BTF), which are building upon and extending base BTF with extra
types and strings.

Once loaded, split BTF appears as a single unified BTF superset of base BTF,
with continuous and transparent numbering scheme. This allows all the existing
users of BTF to work correctly and stay agnostic to the base/split BTFs
composition.  The only difference is in how to instantiate split BTF: it
requires base BTF to be alread instantiated and passed to btf__new_xxx_split()
or btf__parse_xxx_split() "constructors" explicitly.

This split approach is necessary if we are to have a reasonably-sized kernel
module BTFs. By deduping each kernel module's BTF individually, resulting
module BTFs contain copies of a lot of kernel types that are already present
in vmlinux BTF. Even those single copies result in a big BTF size bloat. On my
kernel configuration with 700 modules built, non-split BTF approach results in
115MBs of BTFs across all modules. With split BTF deduplication approach,
total size is down to 5.2MBs total, which is on part with vmlinux BTF (at
around 4MBs). This seems reasonable and practical. As to why we'd need kernel
module BTFs, that should be pretty obvious to anyone using BPF at this point,
as it allows all the BTF-powered features to be used with kernel modules:
tp_btf, fentry/fexit/fmod_ret, lsm, bpf_iter, etc.

This patch set is a pre-requisite to adding split BTF support to pahole, which
is a prerequisite to integrating split BTF into the Linux kernel build setup
to generate BTF for kernel modules. The latter will come as a follow-up patch
series once this series makes it to the libbpf and pahole makes use of it.

Patch #4 introduces necessary basic support for split BTF into libbpf APIs.
Patch #8 implements minimal changes to BTF dedup algorithm to allow
deduplicating split BTFs. Patch #11 adds extra -B flag to bpftool to allow to
specify the path to base BTF for cases when one wants to dump or inspect split
BTF. All the rest are refactorings, clean ups, bug fixes and selftests.

v1->v2:
  - addressed Song's feedback.
====================

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents d0b3d2d7 75fa1777
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -358,8 +358,12 @@ static int dump_btf_raw(const struct btf *btf,
		}
	} else {
		int cnt = btf__get_nr_types(btf);
		int start_id = 1;

		for (i = 1; i <= cnt; i++) {
		if (base_btf)
			start_id = btf__get_nr_types(base_btf) + 1;

		for (i = start_id; i <= cnt; i++) {
			t = btf__type_by_id(btf, i);
			dump_btf_type(btf, i, t);
		}
@@ -438,7 +442,6 @@ static int do_dump(int argc, char **argv)
		return -1;
	}
	src = GET_ARG();

	if (is_prefix(src, "map")) {
		struct bpf_map_info info = {};
		__u32 len = sizeof(info);
@@ -499,7 +502,7 @@ static int do_dump(int argc, char **argv)
		}
		NEXT_ARG();
	} else if (is_prefix(src, "file")) {
		btf = btf__parse(*argv, NULL);
		btf = btf__parse_split(*argv, base_btf);
		if (IS_ERR(btf)) {
			err = -PTR_ERR(btf);
			btf = NULL;
+14 −1
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@

#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>

#include "main.h"

@@ -28,6 +29,7 @@ bool show_pinned;
bool block_mount;
bool verifier_logs;
bool relaxed_maps;
struct btf *base_btf;
struct pinned_obj_table prog_table;
struct pinned_obj_table map_table;
struct pinned_obj_table link_table;
@@ -391,6 +393,7 @@ int main(int argc, char **argv)
		{ "mapcompat",	no_argument,	NULL,	'm' },
		{ "nomount",	no_argument,	NULL,	'n' },
		{ "debug",	no_argument,	NULL,	'd' },
		{ "base-btf",	required_argument, NULL, 'B' },
		{ 0 }
	};
	int opt, ret;
@@ -407,7 +410,7 @@ int main(int argc, char **argv)
	hash_init(link_table.table);

	opterr = 0;
	while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
	while ((opt = getopt_long(argc, argv, "VhpjfmndB:",
				  options, NULL)) >= 0) {
		switch (opt) {
		case 'V':
@@ -441,6 +444,15 @@ int main(int argc, char **argv)
			libbpf_set_print(print_all_levels);
			verifier_logs = true;
			break;
		case 'B':
			base_btf = btf__parse(optarg, NULL);
			if (libbpf_get_error(base_btf)) {
				p_err("failed to parse base BTF at '%s': %ld\n",
				      optarg, libbpf_get_error(base_btf));
				base_btf = NULL;
				return -1;
			}
			break;
		default:
			p_err("unrecognized option '%s'", argv[optind - 1]);
			if (json_output)
@@ -465,6 +477,7 @@ int main(int argc, char **argv)
		delete_pinned_obj_table(&map_table);
		delete_pinned_obj_table(&link_table);
	}
	btf__free(base_btf);

	return ret;
}
+1 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ extern bool show_pids;
extern bool block_mount;
extern bool verifier_logs;
extern bool relaxed_maps;
extern struct btf *base_btf;
extern struct pinned_obj_table prog_table;
extern struct pinned_obj_table map_table;
extern struct pinned_obj_table link_table;
+472 −335

File changed.

Preview size limit exceeded, changes collapsed.

+8 −0
Original line number Diff line number Diff line
@@ -31,11 +31,19 @@ enum btf_endianness {
};

LIBBPF_API void btf__free(struct btf *btf);

LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
LIBBPF_API struct btf *btf__new_empty(void);
LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);

LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
LIBBPF_API struct btf *btf__parse_raw(const char *path);
LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);

LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
LIBBPF_API int btf__load(struct btf *btf);
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
Loading