On Fri, Jan 28, 2022 at 2:33 PM Mauricio Vásquez <mauricio@xxxxxxxxxx> wrote: > > This commit extends libbpf with the features that are needed to > implement BTFGen: > > - Implement bpf_core_create_cand_cache() and bpf_core_free_cand_cache() > to handle candidates cache. > - Expose bpf_core_add_cands() and bpf_core_free_cands to handle > candidates list. > - Expose bpf_core_calc_relo_insn() to bpftool. > > Signed-off-by: Mauricio Vásquez <mauricio@xxxxxxxxxx> > Signed-off-by: Rafael David Tinoco <rafael.tinoco@xxxxxxxxxxx> > Signed-off-by: Lorenzo Fontana <lorenzo.fontana@xxxxxxxxxx> > Signed-off-by: Leonardo Di Donato <leonardo.didonato@xxxxxxxxxx> > --- > tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++----------- > tools/lib/bpf/libbpf_internal.h | 12 +++++++++ > 2 files changed, 41 insertions(+), 15 deletions(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index 12771f71a6e7..61384d219e28 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -5195,18 +5195,18 @@ size_t bpf_core_essential_name_len(const char *name) > return n; > } > > -static void bpf_core_free_cands(struct bpf_core_cand_list *cands) > +void bpf_core_free_cands(struct bpf_core_cand_list *cands) > { > free(cands->cands); > free(cands); > } > > -static int bpf_core_add_cands(struct bpf_core_cand *local_cand, > - size_t local_essent_len, > - const struct btf *targ_btf, > - const char *targ_btf_name, > - int targ_start_id, > - struct bpf_core_cand_list *cands) > +int bpf_core_add_cands(struct bpf_core_cand *local_cand, > + size_t local_essent_len, > + const struct btf *targ_btf, > + const char *targ_btf_name, > + int targ_start_id, > + struct bpf_core_cand_list *cands) > { > struct bpf_core_cand *new_cands, *cand; > const struct btf_type *t, *local_t; > @@ -5577,6 +5577,25 @@ static int bpf_core_resolve_relo(struct bpf_program *prog, > targ_res); > } > > +struct hashmap *bpf_core_create_cand_cache(void) > +{ > + return hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); > +} > + > +void bpf_core_free_cand_cache(struct hashmap *cand_cache) > +{ > + struct hashmap_entry *entry; > + int i; > + > + if (IS_ERR_OR_NULL(cand_cache)) > + return; > + > + hashmap__for_each_entry(cand_cache, entry, i) { > + bpf_core_free_cands(entry->value); > + } > + hashmap__free(cand_cache); > +} > + > static int > bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) > { > @@ -5584,7 +5603,6 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) > struct bpf_core_relo_res targ_res; > const struct bpf_core_relo *rec; > const struct btf_ext_info *seg; > - struct hashmap_entry *entry; > struct hashmap *cand_cache = NULL; > struct bpf_program *prog; > struct bpf_insn *insn; > @@ -5603,7 +5621,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) > } > } > > - cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); > + cand_cache = bpf_core_create_cand_cache(); > if (IS_ERR(cand_cache)) { > err = PTR_ERR(cand_cache); > goto out; > @@ -5694,12 +5712,8 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) > btf__free(obj->btf_vmlinux_override); > obj->btf_vmlinux_override = NULL; > > - if (!IS_ERR_OR_NULL(cand_cache)) { > - hashmap__for_each_entry(cand_cache, entry, i) { > - bpf_core_free_cands(entry->value); > - } > - hashmap__free(cand_cache); > - } > + bpf_core_free_cand_cache(cand_cache); > + > return err; > } > > diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h > index bc86b82e90d1..686a5654262b 100644 > --- a/tools/lib/bpf/libbpf_internal.h > +++ b/tools/lib/bpf/libbpf_internal.h > @@ -529,4 +529,16 @@ static inline int ensure_good_fd(int fd) > return fd; > } > > +struct hashmap; > + > +struct hashmap *bpf_core_create_cand_cache(void); > +void bpf_core_free_cand_cache(struct hashmap *cand_cache); looking at patch #5, there is nothing special about this cand_cache, it's just a hashmap from u32 to some pointer. There is no need for libbpf to expose it to bpftool, you already have hashmap itself and also btfgen_hash_fn and equality callback, just do the same thing as you do with btfgen_info->types hashmap. > +int bpf_core_add_cands(struct bpf_core_cand *local_cand, > + size_t local_essent_len, > + const struct btf *targ_btf, > + const char *targ_btf_name, > + int targ_start_id, > + struct bpf_core_cand_list *cands); > +void bpf_core_free_cands(struct bpf_core_cand_list *cands); > + > #endif /* __LIBBPF_LIBBPF_INTERNAL_H */ > -- > 2.25.1 >