Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Yonghong Song and committed by
Alexei Starovoitov
cfe14564 e181d3f1

+19 -1
+8
kernel/bpf/btf.c
··· 199 199 DEFINE_SPINLOCK(btf_idr_lock); 200 200 201 201 enum btf_kfunc_hook { 202 + BTF_KFUNC_HOOK_COMMON, 202 203 BTF_KFUNC_HOOK_XDP, 203 204 BTF_KFUNC_HOOK_TC, 204 205 BTF_KFUNC_HOOK_STRUCT_OPS, ··· 7532 7531 static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) 7533 7532 { 7534 7533 switch (prog_type) { 7534 + case BPF_PROG_TYPE_UNSPEC: 7535 + return BTF_KFUNC_HOOK_COMMON; 7535 7536 case BPF_PROG_TYPE_XDP: 7536 7537 return BTF_KFUNC_HOOK_XDP; 7537 7538 case BPF_PROG_TYPE_SCHED_CLS: ··· 7562 7559 u32 kfunc_btf_id) 7563 7560 { 7564 7561 enum btf_kfunc_hook hook; 7562 + u32 *kfunc_flags; 7563 + 7564 + kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id); 7565 + if (kfunc_flags) 7566 + return kfunc_flags; 7565 7567 7566 7568 hook = bpf_prog_type_to_kfunc_hook(prog_type); 7567 7569 return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);
+11 -1
kernel/bpf/helpers.c
··· 1901 1901 .set = &generic_btf_ids, 1902 1902 }; 1903 1903 1904 + 1904 1905 BTF_ID_LIST(generic_dtor_ids) 1905 1906 BTF_ID(struct, task_struct) 1906 1907 BTF_ID(func, bpf_task_release) 1908 + 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 + }; 1907 1916 1908 1917 static int __init kfunc_init(void) 1909 1918 { ··· 1927 1918 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set); 1928 1919 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set); 1929 1920 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); 1930 - return ret ?: register_btf_id_dtor_kfuncs(generic_dtors, 1921 + ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, 1931 1922 ARRAY_SIZE(generic_dtors), 1932 1923 THIS_MODULE); 1924 + return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set); 1933 1925 } 1934 1926 1935 1927 late_initcall(kfunc_init);