bpftool: Do not expose and init hash maps for pinned path in main.c
authorQuentin Monnet <quentin@isovalent.com>
Sat, 23 Oct 2021 20:51:51 +0000 (21:51 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 26 Oct 2021 00:31:38 +0000 (17:31 -0700)
BPF programs, maps, and links, can all be listed with their pinned paths
by bpftool, when the "-f" option is provided. To do so, bpftool builds
hash maps containing all pinned paths for each kind of objects.

These three hash maps are always initialised in main.c, and exposed
through main.h. There appear to be no particular reason to do so: we can
just as well make them static to the files that need them (prog.c,
map.c, and link.c respectively), and initialise them only when we want
to show objects and the "-f" switch is provided.

This may prevent unnecessary memory allocations if the implementation of
the hash maps was to change in the future.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211023205154.6710-3-quentin@isovalent.com
tools/bpf/bpftool/link.c
tools/bpf/bpftool/main.c
tools/bpf/bpftool/main.h
tools/bpf/bpftool/map.c
tools/bpf/bpftool/prog.c

index 8cc3e36f8cc6959d203ec9780313d0be0d053b06..a5effb1816b78bbe942865e634aaf1dc92438f67 100644 (file)
@@ -20,6 +20,8 @@ static const char * const link_type_name[] = {
        [BPF_LINK_TYPE_NETNS]                   = "netns",
 };
 
+static struct pinned_obj_table link_table;
+
 static int link_parse_fd(int *argc, char ***argv)
 {
        int fd;
@@ -302,8 +304,10 @@ static int do_show(int argc, char **argv)
        __u32 id = 0;
        int err, fd;
 
-       if (show_pinned)
+       if (show_pinned) {
+               hash_init(link_table.table);
                build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
+       }
        build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
 
        if (argc == 2) {
@@ -346,6 +350,9 @@ static int do_show(int argc, char **argv)
 
        delete_obj_refs_table(&refs_table);
 
+       if (show_pinned)
+               delete_pinned_obj_table(&link_table);
+
        return errno == ENOENT ? 0 : -1;
 }
 
index 02eaaf065f65153f920168530a334aee2245c7a6..7a33f0e6da281148266aa47b2600025035fba19c 100644 (file)
@@ -31,9 +31,6 @@ bool verifier_logs;
 bool relaxed_maps;
 bool use_loader;
 struct btf *base_btf;
-struct pinned_obj_table prog_table;
-struct pinned_obj_table map_table;
-struct pinned_obj_table link_table;
 struct obj_refs_table refs_table;
 
 static void __noreturn clean_and_exit(int i)
@@ -409,10 +406,6 @@ int main(int argc, char **argv)
        block_mount = false;
        bin_name = argv[0];
 
-       hash_init(prog_table.table);
-       hash_init(map_table.table);
-       hash_init(link_table.table);
-
        opterr = 0;
        while ((opt = getopt_long(argc, argv, "VhpjfLmndB:",
                                  options, NULL)) >= 0) {
@@ -479,11 +472,6 @@ int main(int argc, char **argv)
        if (json_output)
                jsonw_destroy(&json_wtr);
 
-       if (show_pinned) {
-               delete_pinned_obj_table(&prog_table);
-               delete_pinned_obj_table(&map_table);
-               delete_pinned_obj_table(&link_table);
-       }
        btf__free(base_btf);
 
        return ret;
index 90caa42aac4cf3f593eda32d61e7f61a4fdc2574..baf607cd59241b460789d557fb59996f79c95f56 100644 (file)
@@ -91,9 +91,6 @@ extern bool verifier_logs;
 extern bool relaxed_maps;
 extern bool use_loader;
 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;
 extern struct obj_refs_table refs_table;
 
 void __printf(1, 2) p_err(const char *fmt, ...);
index 407071d54ab1ce6ba549480c2049e87b307e4e8c..0085039d961038b98f4c06a8f514729198536e23 100644 (file)
@@ -56,6 +56,8 @@ const char * const map_type_name[] = {
 
 const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
 
+static struct pinned_obj_table map_table;
+
 static bool map_is_per_cpu(__u32 type)
 {
        return type == BPF_MAP_TYPE_PERCPU_HASH ||
@@ -694,8 +696,10 @@ static int do_show(int argc, char **argv)
        int err;
        int fd;
 
-       if (show_pinned)
+       if (show_pinned) {
+               hash_init(map_table.table);
                build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
+       }
        build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
 
        if (argc == 2)
@@ -742,6 +746,9 @@ static int do_show(int argc, char **argv)
 
        delete_obj_refs_table(&refs_table);
 
+       if (show_pinned)
+               delete_pinned_obj_table(&map_table);
+
        return errno == ENOENT ? 0 : -1;
 }
 
index f633299b1261803a48cb448c84c1db832d26b044..48c2fa4d068e5e132e5c4c475eaf68482ff44d86 100644 (file)
@@ -84,6 +84,8 @@ static const char * const attach_type_strings[] = {
        [__MAX_BPF_ATTACH_TYPE] = NULL,
 };
 
+static struct pinned_obj_table prog_table;
+
 static enum bpf_attach_type parse_attach_type(const char *str)
 {
        enum bpf_attach_type type;
@@ -567,8 +569,10 @@ static int do_show(int argc, char **argv)
        int err;
        int fd;
 
-       if (show_pinned)
+       if (show_pinned) {
+               hash_init(prog_table.table);
                build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
+       }
        build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
 
        if (argc == 2)
@@ -613,6 +617,9 @@ static int do_show(int argc, char **argv)
 
        delete_obj_refs_table(&refs_table);
 
+       if (show_pinned)
+               delete_pinned_obj_table(&prog_table);
+
        return err;
 }