libbpf: Hashmap interface update to allow both long and void* keys/values
authorEduard Zingerman <eddyz87@gmail.com>
Wed, 9 Nov 2022 14:26:09 +0000 (16:26 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 10 Nov 2022 04:45:14 +0000 (20:45 -0800)
commitc302378bc157f6a73b6cae4ca67f5f6aa931dcec
tree9fbf9394bc776c8bb7da06be70ed83a5836ac60d
parente5659e4e19e49f1eac58bb07ce8bc2d78a89fe65
libbpf: Hashmap interface update to allow both long and void* keys/values

An update for libbpf's hashmap interface from void* -> void* to a
polymorphic one, allowing both long and void* keys and values.

This simplifies many use cases in libbpf as hashmaps there are mostly
integer to integer.

Perf copies hashmap implementation from libbpf and has to be
updated as well.

Changes to libbpf, selftests/bpf and perf are packed as a single
commit to avoid compilation issues with any future bisect.

Polymorphic interface is acheived by hiding hashmap interface
functions behind auxiliary macros that take care of necessary
type casts, for example:

    #define hashmap_cast_ptr(p) \
({ \
_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),\
       #p " pointee should be a long-sized integer or a pointer"); \
(long *)(p); \
})

    bool hashmap_find(const struct hashmap *map, long key, long *value);

    #define hashmap__find(map, key, value) \
hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

- hashmap__find macro casts key and value parameters to long
  and long* respectively
- hashmap_cast_ptr ensures that value pointer points to a memory
  of appropriate size.

This hack was suggested by Andrii Nakryiko in [1].
This is a follow up for [2].

[1] https://lore.kernel.org/bpf/CAEf4BzZ8KFneEJxFAaNCCFPGqp20hSpS2aCj76uRk3-qZUH5xg@mail.gmail.com/
[2] https://lore.kernel.org/bpf/af1facf9-7bc8-8a3d-0db4-7b3f333589a2@meta.com/T/#m65b28f1d6d969fcd318b556db6a3ad499a42607d

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20221109142611.879983-2-eddyz87@gmail.com
27 files changed:
tools/bpf/bpftool/btf.c
tools/bpf/bpftool/common.c
tools/bpf/bpftool/gen.c
tools/bpf/bpftool/link.c
tools/bpf/bpftool/main.h
tools/bpf/bpftool/map.c
tools/bpf/bpftool/pids.c
tools/bpf/bpftool/prog.c
tools/lib/bpf/btf.c
tools/lib/bpf/btf_dump.c
tools/lib/bpf/hashmap.c
tools/lib/bpf/hashmap.h
tools/lib/bpf/libbpf.c
tools/lib/bpf/strset.c
tools/lib/bpf/usdt.c
tools/perf/tests/expr.c
tools/perf/tests/pmu-events.c
tools/perf/util/bpf-loader.c
tools/perf/util/evsel.c
tools/perf/util/expr.c
tools/perf/util/hashmap.c
tools/perf/util/hashmap.h
tools/perf/util/metricgroup.c
tools/perf/util/stat-shadow.c
tools/perf/util/stat.c
tools/testing/selftests/bpf/prog_tests/hashmap.c
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c