Skip to content

Commit

Permalink
libbpf: bpf__find_by_name[_kind] should use btf__get_nr_types()
Browse files Browse the repository at this point in the history
When operating on split BTF, btf__find_by_name[_kind] will not
iterate over all types since they use btf->nr_types to show
the number of types to iterate over. For split BTF this is
the number of types _on top of base BTF_, so it will
underestimate the number of types to iterate over, especially
for vmlinux + module BTF, where the latter is much smaller.

Use btf__get_nr_types() instead.

Fixes: ba45136 ("libbpf: Implement basic split BTF support")
Signed-off-by: Alan Maguire <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
alan-maguire authored and anakryiko committed Nov 17, 2020
1 parent b93ef08 commit de91e63
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tools/lib/bpf/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,12 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id)

__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
{
__u32 i;
__u32 i, nr_types = btf__get_nr_types(btf);

if (!strcmp(type_name, "void"))
return 0;

for (i = 1; i <= btf->nr_types; i++) {
for (i = 1; i <= nr_types; i++) {
const struct btf_type *t = btf__type_by_id(btf, i);
const char *name = btf__name_by_offset(btf, t->name_off);

Expand All @@ -693,12 +693,12 @@ __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
__s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
__u32 kind)
{
__u32 i;
__u32 i, nr_types = btf__get_nr_types(btf);

if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
return 0;

for (i = 1; i <= btf->nr_types; i++) {
for (i = 1; i <= nr_types; i++) {
const struct btf_type *t = btf__type_by_id(btf, i);
const char *name;

Expand Down

0 comments on commit de91e63

Please sign in to comment.