Skip to content

Commit 0f12e58

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Add BTF_ID_LIST_GLOBAL in btf_ids.h
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
1 parent d8dfe5b commit 0f12e58

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

include/linux/btf_ids.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ asm( \
5757
* .zero 4
5858
*
5959
*/
60-
#define __BTF_ID_LIST(name) \
60+
#define __BTF_ID_LIST(name, scope) \
6161
asm( \
6262
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
63-
".local " #name "; \n" \
63+
"." #scope " " #name "; \n" \
6464
#name ":; \n" \
6565
".popsection; \n"); \
6666

6767
#define BTF_ID_LIST(name) \
68-
__BTF_ID_LIST(name) \
68+
__BTF_ID_LIST(name, local) \
6969
extern u32 name[];
7070

71+
#define BTF_ID_LIST_GLOBAL(name) \
72+
__BTF_ID_LIST(name, globl)
73+
7174
/*
7275
* The BTF_ID_UNUSED macro defines 4 zero bytes.
7376
* It's used when we want to define 'unused' entry
@@ -90,6 +93,7 @@ asm( \
9093
#define BTF_ID_LIST(name) static u32 name[5];
9194
#define BTF_ID(prefix, name)
9295
#define BTF_ID_UNUSED
96+
#define BTF_ID_LIST_GLOBAL(name) u32 name[1];
9397

9498
#endif /* CONFIG_DEBUG_INFO_BTF */
9599

tools/include/linux/btf_ids.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ asm( \
5757
* .zero 4
5858
*
5959
*/
60-
#define __BTF_ID_LIST(name) \
60+
#define __BTF_ID_LIST(name, scope) \
6161
asm( \
6262
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
63-
".local " #name "; \n" \
63+
"." #scope " " #name "; \n" \
6464
#name ":; \n" \
6565
".popsection; \n"); \
6666

6767
#define BTF_ID_LIST(name) \
68-
__BTF_ID_LIST(name) \
68+
__BTF_ID_LIST(name, local) \
6969
extern u32 name[];
7070

71+
#define BTF_ID_LIST_GLOBAL(name) \
72+
__BTF_ID_LIST(name, globl)
73+
7174
/*
7275
* The BTF_ID_UNUSED macro defines 4 zero bytes.
7376
* It's used when we want to define 'unused' entry
@@ -90,6 +93,7 @@ asm( \
9093
#define BTF_ID_LIST(name) static u32 name[5];
9194
#define BTF_ID(prefix, name)
9295
#define BTF_ID_UNUSED
96+
#define BTF_ID_LIST_GLOBAL(name) u32 name[1];
9397

9498
#endif /* CONFIG_DEBUG_INFO_BTF */
9599

tools/testing/selftests/bpf/prog_tests/resolve_btfids.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ struct symbol test_symbols[] = {
2828
{ "func", BTF_KIND_FUNC, -1 },
2929
};
3030

31-
BTF_ID_LIST(test_list)
31+
BTF_ID_LIST(test_list_local)
32+
BTF_ID_UNUSED
33+
BTF_ID(typedef, S)
34+
BTF_ID(typedef, T)
35+
BTF_ID(typedef, U)
36+
BTF_ID(struct, S)
37+
BTF_ID(union, U)
38+
BTF_ID(func, func)
39+
40+
extern __u32 test_list_global[];
41+
BTF_ID_LIST_GLOBAL(test_list_global)
3242
BTF_ID_UNUSED
3343
BTF_ID(typedef, S)
3444
BTF_ID(typedef, T)
@@ -94,18 +104,25 @@ static int resolve_symbols(void)
94104

95105
int test_resolve_btfids(void)
96106
{
97-
unsigned int i;
107+
__u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
108+
unsigned int i, j;
98109
int ret = 0;
99110

100111
if (resolve_symbols())
101112
return -1;
102113

103-
/* Check BTF_ID_LIST(test_list) IDs */
104-
for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
105-
ret = CHECK(test_list[i] != test_symbols[i].id,
106-
"id_check",
107-
"wrong ID for %s (%d != %d)\n", test_symbols[i].name,
108-
test_list[i], test_symbols[i].id);
114+
/* Check BTF_ID_LIST(test_list_local) and
115+
* BTF_ID_LIST_GLOBAL(test_list_global) IDs
116+
*/
117+
for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
118+
test_list = test_lists[j];
119+
for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
120+
ret = CHECK(test_list[i] != test_symbols[i].id,
121+
"id_check",
122+
"wrong ID for %s (%d != %d)\n",
123+
test_symbols[i].name,
124+
test_list[i], test_symbols[i].id);
125+
}
109126
}
110127

111128
return ret;

0 commit comments

Comments
 (0)