Skip to content

Commit

Permalink
bpf: Add BTF_ID_LIST_GLOBAL in btf_ids.h
Browse files Browse the repository at this point in the history
Existing BTF_ID_LIST used a local static variable
to store btf_ids. This patch provided a new macro
BTF_ID_LIST_GLOBAL to store btf_ids in a global
variable which can be shared among multiple files.

The existing BTF_ID_LIST is still retained.
Two reasons. First, BTF_ID_LIST is also used to build
btf_ids for helper arguments which typically
is an array of 5. Since typically different
helpers have different signature, it makes
little sense to share them. Second, some
current computed btf_ids are indeed local.
If later those btf_ids are shared between
different files, they can use BTF_ID_LIST_GLOBAL then.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Link: https://lore.kernel.org/bpf/20200720163401.1393159-1-yhs@fb.com
  • Loading branch information
Yonghong Song authored and Alexei Starovoitov committed Jul 21, 2020
1 parent d8dfe5b commit 0f12e58
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
10 changes: 7 additions & 3 deletions include/linux/btf_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ asm( \
* .zero 4
*
*/
#define __BTF_ID_LIST(name) \
#define __BTF_ID_LIST(name, scope) \
asm( \
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
".local " #name "; \n" \
"." #scope " " #name "; \n" \
#name ":; \n" \
".popsection; \n"); \

#define BTF_ID_LIST(name) \
__BTF_ID_LIST(name) \
__BTF_ID_LIST(name, local) \
extern u32 name[];

#define BTF_ID_LIST_GLOBAL(name) \
__BTF_ID_LIST(name, globl)

/*
* The BTF_ID_UNUSED macro defines 4 zero bytes.
* It's used when we want to define 'unused' entry
Expand All @@ -90,6 +93,7 @@ asm( \
#define BTF_ID_LIST(name) static u32 name[5];
#define BTF_ID(prefix, name)
#define BTF_ID_UNUSED
#define BTF_ID_LIST_GLOBAL(name) u32 name[1];

#endif /* CONFIG_DEBUG_INFO_BTF */

Expand Down
10 changes: 7 additions & 3 deletions tools/include/linux/btf_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ asm( \
* .zero 4
*
*/
#define __BTF_ID_LIST(name) \
#define __BTF_ID_LIST(name, scope) \
asm( \
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
".local " #name "; \n" \
"." #scope " " #name "; \n" \
#name ":; \n" \
".popsection; \n"); \

#define BTF_ID_LIST(name) \
__BTF_ID_LIST(name) \
__BTF_ID_LIST(name, local) \
extern u32 name[];

#define BTF_ID_LIST_GLOBAL(name) \
__BTF_ID_LIST(name, globl)

/*
* The BTF_ID_UNUSED macro defines 4 zero bytes.
* It's used when we want to define 'unused' entry
Expand All @@ -90,6 +93,7 @@ asm( \
#define BTF_ID_LIST(name) static u32 name[5];
#define BTF_ID(prefix, name)
#define BTF_ID_UNUSED
#define BTF_ID_LIST_GLOBAL(name) u32 name[1];

#endif /* CONFIG_DEBUG_INFO_BTF */

Expand Down
33 changes: 25 additions & 8 deletions tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ struct symbol test_symbols[] = {
{ "func", BTF_KIND_FUNC, -1 },
};

BTF_ID_LIST(test_list)
BTF_ID_LIST(test_list_local)
BTF_ID_UNUSED
BTF_ID(typedef, S)
BTF_ID(typedef, T)
BTF_ID(typedef, U)
BTF_ID(struct, S)
BTF_ID(union, U)
BTF_ID(func, func)

extern __u32 test_list_global[];
BTF_ID_LIST_GLOBAL(test_list_global)
BTF_ID_UNUSED
BTF_ID(typedef, S)
BTF_ID(typedef, T)
Expand Down Expand Up @@ -94,18 +104,25 @@ static int resolve_symbols(void)

int test_resolve_btfids(void)
{
unsigned int i;
__u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
unsigned int i, j;
int ret = 0;

if (resolve_symbols())
return -1;

/* Check BTF_ID_LIST(test_list) IDs */
for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
ret = CHECK(test_list[i] != test_symbols[i].id,
"id_check",
"wrong ID for %s (%d != %d)\n", test_symbols[i].name,
test_list[i], test_symbols[i].id);
/* Check BTF_ID_LIST(test_list_local) and
* BTF_ID_LIST_GLOBAL(test_list_global) IDs
*/
for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
test_list = test_lists[j];
for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
ret = CHECK(test_list[i] != test_symbols[i].id,
"id_check",
"wrong ID for %s (%d != %d)\n",
test_symbols[i].name,
test_list[i], test_symbols[i].id);
}
}

return ret;
Expand Down

0 comments on commit 0f12e58

Please sign in to comment.