selftests/bpf: Add kfunc set test to resolve_btfids

Extend the resolve_btfids selftest to cover kfunc sets defined with
BTF_KFUNCS_START/BTF_KFUNCS_END.

The test verifies that resolve_btfids correctly processes BTF_ID_FLAGS,
resolves function IDs, and checks the kfunc set is sorted.

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20260624005546.1818483-5-ihor.solodrai@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Ihor Solodrai
2026-06-23 17:55:46 -07:00
committed by Alexei Starovoitov
parent 6f10765ecd
commit 7956388294
2 changed files with 82 additions and 7 deletions

View File

@@ -12,6 +12,10 @@
#define BTF_DATA_FILE "resolve_btfids.test.o.BTF"
#ifndef KF_FASTCALL
#define KF_FASTCALL (1 << 12)
#endif
struct symbol {
const char *name;
int type;
@@ -28,6 +32,17 @@ struct symbol test_symbols[] = {
{ "func", BTF_KIND_FUNC, -1 },
};
struct kfunc_symbol {
const char *name;
s32 id;
u32 flags;
};
static struct kfunc_symbol kfunc_symbols[] = {
{ "kfunc_a", -1, 0 },
{ "kfunc_b", -1, KF_FASTCALL },
};
/* Align the .BTF_ids section to 4 bytes */
asm (
".pushsection " BTF_IDS_SECTION " ,\"a\"; \n"
@@ -35,9 +50,9 @@ asm (
".popsection; \n");
/*
* test_list_local and test_set are .local symbols placed in .BTF_ids by
* inline asm, and are read here directly by C name. To the compiler they
* are plain, default-visibility extern objects.
* test_list_local, test_set and test_kfunc_set are .local symbols placed
* in .BTF_ids by inline asm, and are read here directly by C name. To the
* compiler they are plain, default-visibility extern objects.
*
* When test_progs is linked as a position-independent executable (PIE),
* taking the address of such an extern is routed through the GOT. The
@@ -69,6 +84,20 @@ BTF_ID(struct, S)
BTF_ID(union, U)
BTF_ID(func, func)
BTF_SET_END(test_set)
BTF_KFUNCS_START(test_kfunc_set)
BTF_ID_FLAGS(func, kfunc_a)
BTF_ID_FLAGS(func, kfunc_b, KF_FASTCALL)
BTF_KFUNCS_END(test_kfunc_set)
/*
* Same kfuncs in reverse declaration order, so resolve_btfids has to
* actually sort at least one of the two sets.
*/
BTF_KFUNCS_START(test_kfunc_set_rev)
BTF_ID_FLAGS(func, kfunc_b, KF_FASTCALL)
BTF_ID_FLAGS(func, kfunc_a)
BTF_KFUNCS_END(test_kfunc_set_rev)
#pragma GCC visibility pop
extern __u32 test_list_global[];
@@ -92,6 +121,8 @@ __resolve_symbol(struct btf *btf, int type_id)
if (!ASSERT_OK_PTR(type, "btf__type_by_id"))
return -1;
str = btf__name_by_offset(btf, type->name_off);
for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
if (test_symbols[i].id >= 0)
continue;
@@ -99,14 +130,20 @@ __resolve_symbol(struct btf *btf, int type_id)
if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
continue;
str = btf__name_by_offset(btf, type->name_off);
if (!ASSERT_OK_PTR(str, "btf__name_by_offset"))
return -1;
if (!strcmp(str, test_symbols[i].name))
test_symbols[i].id = type_id;
}
if (!btf_is_func(type))
return 0;
for (i = 0; i < ARRAY_SIZE(kfunc_symbols); i++) {
if (kfunc_symbols[i].id >= 0)
continue;
if (!strcmp(str, kfunc_symbols[i].name))
kfunc_symbols[i].id = type_id;
}
return 0;
}
@@ -122,6 +159,31 @@ static int resolve_symbols(struct btf *btf)
return 0;
}
static void check_kfunc_set(struct btf_id_set8 *set)
{
unsigned int i, j;
ASSERT_EQ(set->flags, BTF_SET8_KFUNCS, "kfunc_set_flags");
ASSERT_EQ(set->cnt, ARRAY_SIZE(kfunc_symbols), "kfunc_set_cnt");
for (i = 0; i < set->cnt; i++) {
for (j = 0; j < ARRAY_SIZE(kfunc_symbols); j++) {
if (kfunc_symbols[j].id == (s32)set->pairs[i].id) {
ASSERT_EQ(set->pairs[i].flags,
kfunc_symbols[j].flags, "kfunc_flags_check");
break;
}
}
ASSERT_TRUE(j < ARRAY_SIZE(kfunc_symbols), "kfunc_id_found");
if (i > 0) {
ASSERT_LE(set->pairs[i - 1].id,
set->pairs[i].id, "kfunc_sort_check");
}
}
}
void test_resolve_btfids(void)
{
__u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
@@ -162,6 +224,9 @@ void test_resolve_btfids(void)
ASSERT_LE(test_set.ids[i - 1], test_set.ids[i], "sort_check");
}
check_kfunc_set(&test_kfunc_set);
check_kfunc_set(&test_kfunc_set_rev);
out:
btf__free(btf);
}

View File

@@ -48,3 +48,13 @@ int func(struct root_struct *root)
{
return 0;
}
int kfunc_a(struct root_struct *root)
{
return 0;
}
int kfunc_b(struct root_struct *root)
{
return 0;
}