]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
btf: separate btf creation and loading
authorAndrii Nakryiko <andriin@fb.com>
Fri, 8 Feb 2019 19:19:36 +0000 (11:19 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 8 Feb 2019 20:04:13 +0000 (12:04 -0800)
This change splits out previous btf__new functionality of constructing
struct btf and loading it into kernel into two:
- btf__new() just creates and initializes struct btf
- btf__load() attempts to load existing struct btf into kernel

btf__free will still close BTF fd, if it was ever loaded successfully
into kernel.

This change allows users of libbpf to manipulate BTF using its API,
without the need to unnecessarily load it into kernel.

One of the intended use cases is pahole, which will do DWARF to BTF
conversion and then use libbpf to do type deduplication, while then
handling ELF sections overwriting and other concerns on its own.

Fixes: 2d3feca8c44f ("bpf: btf: print map dump and lookup with btf info")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/btf.c
tools/lib/bpf/btf.h
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.map

index 4324eb47d214bcd43c10cebc08f3010bb65ad6d3..46db0a3b5cb788070573f4e81031278697550806 100644 (file)
@@ -367,8 +367,6 @@ void btf__free(struct btf *btf)
 
 struct btf *btf__new(__u8 *data, __u32 size)
 {
-       __u32 log_buf_size = 0;
-       char *log_buf = NULL;
        struct btf *btf;
        int err;
 
@@ -378,15 +376,6 @@ struct btf *btf__new(__u8 *data, __u32 size)
 
        btf->fd = -1;
 
-       log_buf = malloc(BPF_LOG_BUF_SIZE);
-       if (!log_buf) {
-               err = -ENOMEM;
-               goto done;
-       }
-
-       *log_buf = 0;
-       log_buf_size = BPF_LOG_BUF_SIZE;
-
        btf->data = malloc(size);
        if (!btf->data) {
                err = -ENOMEM;
@@ -396,17 +385,6 @@ struct btf *btf__new(__u8 *data, __u32 size)
        memcpy(btf->data, data, size);
        btf->data_size = size;
 
-       btf->fd = bpf_load_btf(btf->data, btf->data_size,
-                              log_buf, log_buf_size, false);
-
-       if (btf->fd == -1) {
-               err = -errno;
-               pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
-               if (log_buf && *log_buf)
-                       pr_warning("%s\n", log_buf);
-               goto done;
-       }
-
        err = btf_parse_hdr(btf);
        if (err)
                goto done;
@@ -418,8 +396,6 @@ struct btf *btf__new(__u8 *data, __u32 size)
        err = btf_parse_type_sec(btf);
 
 done:
-       free(log_buf);
-
        if (err) {
                btf__free(btf);
                return ERR_PTR(err);
@@ -428,6 +404,36 @@ struct btf *btf__new(__u8 *data, __u32 size)
        return btf;
 }
 
+int btf__load(struct btf *btf)
+{
+       __u32 log_buf_size = BPF_LOG_BUF_SIZE;
+       char *log_buf = NULL;
+       int err = 0;
+
+       if (btf->fd >= 0)
+               return -EEXIST;
+
+       log_buf = malloc(log_buf_size);
+       if (!log_buf)
+               return -ENOMEM;
+
+       *log_buf = 0;
+
+       btf->fd = bpf_load_btf(btf->data, btf->data_size,
+                              log_buf, log_buf_size, false);
+       if (btf->fd < 0) {
+               err = -errno;
+               pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
+               if (*log_buf)
+                       pr_warning("%s\n", log_buf);
+               goto done;
+       }
+
+done:
+       free(log_buf);
+       return err;
+}
+
 int btf__fd(const struct btf *btf)
 {
        return btf->fd;
index b393da90cc8511a1cf24cb3954b6b23d9f5d8d91..f55b7bc98d9e50342e882c996541b58495c335b5 100644 (file)
@@ -57,6 +57,7 @@ struct btf_ext_header {
 
 LIBBPF_API void btf__free(struct btf *btf);
 LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size);
+LIBBPF_API int btf__load(struct btf *btf);
 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
                                   const char *type_name);
 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
index 8d64ada5f7284b3bcc929c2b2066631c85f39da5..e3c39edfb9d3c701e61cf1dc29b82127dcc6a9ca 100644 (file)
@@ -836,7 +836,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
                        obj->efile.maps_shndx = idx;
                else if (strcmp(name, BTF_ELF_SEC) == 0) {
                        obj->btf = btf__new(data->d_buf, data->d_size);
-                       if (IS_ERR(obj->btf)) {
+                       if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
                                pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
                                           BTF_ELF_SEC, PTR_ERR(obj->btf));
                                obj->btf = NULL;
index 89c1149e32eece93378bbcdc4be8c807f8f26807..f5372df143f48996964f8c1045548a8b09078076 100644 (file)
@@ -137,6 +137,7 @@ LIBBPF_0.0.2 {
                btf__get_map_kv_tids;
                btf__get_nr_types;
                btf__get_strings;
+               btf__load;
                btf_ext__free;
                btf_ext__func_info_rec_size;
                btf_ext__line_info_rec_size;