mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
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
This commit is contained in:
parent
e5659e4e19
commit
c302378bc1
27 changed files with 410 additions and 340 deletions
|
@ -5601,21 +5601,16 @@ int bpf_core_types_match(const struct btf *local_btf, __u32 local_id,
|
|||
return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32);
|
||||
}
|
||||
|
||||
static size_t bpf_core_hash_fn(const void *key, void *ctx)
|
||||
static size_t bpf_core_hash_fn(const long key, void *ctx)
|
||||
{
|
||||
return (size_t)key;
|
||||
return key;
|
||||
}
|
||||
|
||||
static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
|
||||
static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx)
|
||||
{
|
||||
return k1 == k2;
|
||||
}
|
||||
|
||||
static void *u32_as_hash_key(__u32 x)
|
||||
{
|
||||
return (void *)(uintptr_t)x;
|
||||
}
|
||||
|
||||
static int record_relo_core(struct bpf_program *prog,
|
||||
const struct bpf_core_relo *core_relo, int insn_idx)
|
||||
{
|
||||
|
@ -5658,7 +5653,6 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
|
|||
struct bpf_core_relo_res *targ_res)
|
||||
{
|
||||
struct bpf_core_spec specs_scratch[3] = {};
|
||||
const void *type_key = u32_as_hash_key(relo->type_id);
|
||||
struct bpf_core_cand_list *cands = NULL;
|
||||
const char *prog_name = prog->name;
|
||||
const struct btf_type *local_type;
|
||||
|
@ -5675,7 +5669,7 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
|
|||
return -EINVAL;
|
||||
|
||||
if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
|
||||
!hashmap__find(cand_cache, type_key, (void **)&cands)) {
|
||||
!hashmap__find(cand_cache, local_id, &cands)) {
|
||||
cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
|
||||
if (IS_ERR(cands)) {
|
||||
pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
|
||||
|
@ -5683,7 +5677,7 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
|
|||
local_name, PTR_ERR(cands));
|
||||
return PTR_ERR(cands);
|
||||
}
|
||||
err = hashmap__set(cand_cache, type_key, cands, NULL, NULL);
|
||||
err = hashmap__set(cand_cache, local_id, cands, NULL, NULL);
|
||||
if (err) {
|
||||
bpf_core_free_cands(cands);
|
||||
return err;
|
||||
|
@ -5806,7 +5800,7 @@ out:
|
|||
|
||||
if (!IS_ERR_OR_NULL(cand_cache)) {
|
||||
hashmap__for_each_entry(cand_cache, entry, i) {
|
||||
bpf_core_free_cands(entry->value);
|
||||
bpf_core_free_cands(entry->pvalue);
|
||||
}
|
||||
hashmap__free(cand_cache);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue