Thread (12 messages) 12 messages, 2 authors, 2021-10-23

Re: [PATCH bpf-next 5/5] bpftool: Switch to libbpf's hashmap for PIDs/names references

From: Andrii Nakryiko <hidden>
Date: 2021-10-23 01:50:31
Also in: netdev

On Fri, Oct 22, 2021 at 10:16 AM Quentin Monnet [off-list ref] wrote:
In order to show PIDs and names for processes holding references to BPF
programs, maps, links, or BTF objects, bpftool creates hash maps to
store all relevant information. This commit is part of a set that
transitions from the kernel's hash map implementation to the one coming
with libbpf.

The motivation is to make bpftool less dependent of kernel headers, to
ease the path to a potential out-of-tree mirror, like libbpf has.

This is the third and final step of the transition, in which we convert
the hash maps used for storing the information about the processes
holding references to BPF objects (programs, maps, links, BTF), and at
last we drop the inclusion of tools/include/linux/hashtable.h.

Note: Checkpatch complains about the use of __weak declarations, and the
missing empty lines after the bunch of empty function declarations when
compiling without the BPF skeletons (none of these were introduced in
this patch). We want to keep things as they are, and the reports should
be safe to ignore.

Signed-off-by: Quentin Monnet <redacted>
---
 tools/bpf/bpftool/btf.c  |  7 ++--
 tools/bpf/bpftool/link.c |  6 +--
 tools/bpf/bpftool/main.c |  5 ++-
 tools/bpf/bpftool/main.h | 17 +++-----
 tools/bpf/bpftool/map.c  |  6 +--
 tools/bpf/bpftool/pids.c | 84 ++++++++++++++++++++++------------------
 tools/bpf/bpftool/prog.c |  6 +--
 7 files changed, 67 insertions(+), 64 deletions(-)
[...]
quoted hunk ↗ jump to hunk
 #include "pid_iter.skel.h"

-static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e)
+static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
 {
+       struct hashmap_entry *entry;
        struct obj_refs *refs;
        struct obj_ref *ref;
        void *tmp;
        int i;

-       hash_for_each_possible(table->table, refs, node, e->id) {
-               if (refs->id != e->id)
-                       continue;
+       hashmap__for_each_key_entry(map, entry, u32_as_hash_field(e->id)) {
+               refs = entry->value;

                for (i = 0; i < refs->ref_cnt; i++) {
                        if (refs->refs[i].pid == e->pid)
@@ -64,7 +66,6 @@ static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e)
                return;
        }

-       refs->id = e->id;
        refs->refs = malloc(sizeof(*refs->refs));
        if (!refs->refs) {
                free(refs);
@@ -76,7 +77,7 @@ static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e)
        ref->pid = e->pid;
        memcpy(ref->comm, e->comm, sizeof(ref->comm));
        refs->ref_cnt = 1;
-       hash_add(table->table, &refs->node, e->id);
+       hashmap__append(map, u32_as_hash_field(e->id), refs);
here as well, can fail
quoted hunk ↗ jump to hunk
 }

 static int __printf(2, 0)
@@ -87,7 +88,7 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
        return 0;
 }
[...]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help