Skip to content

Commit cfe1456

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Add support for kfunc set with common btf_ids
Later on, we will introduce kfuncs bpf_cast_to_kern_ctx() and bpf_rdonly_cast() which apply to all program types. Currently kfunc set only supports individual prog types. This patch added support for kfunc applying to all program types. Signed-off-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/r/20221120195426.3113828-1-yhs@fb.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e181d3f commit cfe1456

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

kernel/bpf/btf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ DEFINE_IDR(btf_idr);
199199
DEFINE_SPINLOCK(btf_idr_lock);
200200

201201
enum btf_kfunc_hook {
202+
BTF_KFUNC_HOOK_COMMON,
202203
BTF_KFUNC_HOOK_XDP,
203204
BTF_KFUNC_HOOK_TC,
204205
BTF_KFUNC_HOOK_STRUCT_OPS,
@@ -7531,6 +7532,8 @@ static u32 *__btf_kfunc_id_set_contains(const struct btf *btf,
75317532
static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
75327533
{
75337534
switch (prog_type) {
7535+
case BPF_PROG_TYPE_UNSPEC:
7536+
return BTF_KFUNC_HOOK_COMMON;
75347537
case BPF_PROG_TYPE_XDP:
75357538
return BTF_KFUNC_HOOK_XDP;
75367539
case BPF_PROG_TYPE_SCHED_CLS:
@@ -7559,6 +7562,11 @@ u32 *btf_kfunc_id_set_contains(const struct btf *btf,
75597562
u32 kfunc_btf_id)
75607563
{
75617564
enum btf_kfunc_hook hook;
7565+
u32 *kfunc_flags;
7566+
7567+
kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id);
7568+
if (kfunc_flags)
7569+
return kfunc_flags;
75627570

75637571
hook = bpf_prog_type_to_kfunc_hook(prog_type);
75647572
return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);

kernel/bpf/helpers.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1901,10 +1901,19 @@ static const struct btf_kfunc_id_set generic_kfunc_set = {
19011901
.set = &generic_btf_ids,
19021902
};
19031903

1904+
19041905
BTF_ID_LIST(generic_dtor_ids)
19051906
BTF_ID(struct, task_struct)
19061907
BTF_ID(func, bpf_task_release)
19071908

1909+
BTF_SET8_START(common_btf_ids)
1910+
BTF_SET8_END(common_btf_ids)
1911+
1912+
static const struct btf_kfunc_id_set common_kfunc_set = {
1913+
.owner = THIS_MODULE,
1914+
.set = &common_btf_ids,
1915+
};
1916+
19081917
static int __init kfunc_init(void)
19091918
{
19101919
int ret;
@@ -1918,9 +1927,10 @@ static int __init kfunc_init(void)
19181927
ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set);
19191928
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set);
19201929
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
1921-
return ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
1930+
ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
19221931
ARRAY_SIZE(generic_dtors),
19231932
THIS_MODULE);
1933+
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set);
19241934
}
19251935

19261936
late_initcall(kfunc_init);

0 commit comments

Comments
 (0)