Allow struct_ops to create map id to
Subsystems (7)
v2 cover Allow struct_ops to create map id to
v1 -> v2
Add bpf_struct_ops_id() instead of using bpf_struct_ops_get()
Hi,
This patchset allows struct_ops implementors to get map id from kdata in reg(), unreg() and update() so that they create an id to struct_ops instance mapping. This in turn allows struct_ops kfuncs to refer to the calling instance without passing a pointer to the struct_ops. The selftest provides an end-to-end example.
Some struct_ops users extend themselves with other bpf programs, which also need to call struct_ops kfuncs. For example, scx_layered uses syscall bpf programs as a scx_layered specific control plane and uses tracing programs to get additional information for scheduling [0]. The kfuncs may need to refer to the struct_ops instance and perform jobs accordingly. To allow calling struct_ops kfuncs referring to specific instances from different program types and context (e.g., struct_ops, tracing, async callbacks), the traditional way is to pass the struct_ops pointer to kfuncs.
This patchset provides an alternative way, through a combination of bpf map id and global variable. First, a struct_ops implementor will use the map id of the struct_ops map as the id of an instance. Then, it needs to maintain an id to instance mapping: inserting a new mapping during reg() and removing it during unreg(). The map id can be acquired by calling bpf_struct_ops_id().
[0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c
Amery Hung (3):
bpf: Allow struct_ops to get map id by kdata selftests/bpf: Add multi_st_ops that supports multiple instances selftests/bpf: Test multi_st_ops and calling kfuncs from different
programs
include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 12 ++ .../test_struct_ops_id_ops_mapping.c | 77 +++++++++++++ .../bpf/progs/struct_ops_id_ops_mapping1.c | 59 ++++++++++ .../bpf/progs/struct_ops_id_ops_mapping2.c | 59 ++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 109 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.h | 8 ++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 2 + 8 files changed, 327 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c
Signature · 1 line
2.47.3
PATCH v2 1/3 bpf: Allow struct_ops to get map id by kdata
Add bpf_struct_ops_id() to enable struct_ops implementors to use struct_ops map id as the unique id of a struct_ops in their subsystem. A subsystem that wishes to create a mapping between id and struct_ops instance pointer can update the mapping accordingly during bpf_struct_ops::reg(), unreg(), and update().
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 12 ++++++++++++ 2 files changed, 13 insertions(+)
include/linux/bpf.h +1 -0
@@ -1975,6 +1975,7 @@ static inline void bpf_module_put(const void *data, struct module *owner)module_put (owner); }int bpf_struct_ops_link_create (union bpf_attr *attr); +u32 bpf_struct_ops_id (const void *kdata);#ifdef CONFIG_NET /* Define it here to avoid the use of forward declaration */
kernel/bpf/bpf_struct_ops.c +12 -0
@@ -1174,6 +1174,18 @@ void bpf_struct_ops_put(const void *kdata)bpf_map_put (&st_map->map); } +u32 bpf_struct_ops_id (const void *kdata) +{ +struct bpf_struct_ops_value *kvalue; +struct bpf_struct_ops_map *st_map; + + kvalue = container_of(kdata, struct bpf_struct_ops_value, data); + st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue); + +return st_map->map.id; +} +EXPORT_SYMBOL_GPL (bpf_struct_ops_id); +static bool bpf_struct_ops_valid_to_reg (struct bpf_map *map) {struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
Signature · 1 line
2.47.3
PATCH v2 2/3 selftests/bpf: Add multi_st_ops that supports multiple instances
In reply to Amery HungCurrent struct_ops in bpf_testmod only support attaching single instance. Add multi_st_ops that supports multiple instances. The struct_ops uses map id as the struct_ops id and will reject attachment with an existing id.
--- .../selftests/bpf/test_kmods/bpf_testmod.c | 109 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.h | 8 ++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 2 + 3 files changed, 119 insertions(+)
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +109 -0
@@ -1057,6 +1057,8 @@ __bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args)return args->a; } +__bpf_kfunc int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id); + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -1097,6 +1099,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_prologue, KF_TRUSTED_ARGS | KF_SLEEPABL BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_pro_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kfunc_st_ops_inc10, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_kfunc_multi_st_ops_test_1, KF_TRUSTED_ARGS)BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)
@@ -1528,6 +1531,111 @@ static struct bpf_struct_ops testmod_st_ops = { .owner = THIS_MODULE, }; +struct hlist_head multi_st_ops_list; +static DEFINE_SPINLOCK (multi_st_ops_lock); + +static int multi_st_ops_init (struct btf *btf) +{ +spin_lock_init (&multi_st_ops_lock); +INIT_HLIST_HEAD (&multi_st_ops_list); + +return 0 ; +} + +static int multi_st_ops_init_member (const struct btf_type *t, +const struct btf_member *member, +void *kdata,const void *udata) +{ +return 0 ; +} + +static struct bpf_testmod_multi_st_ops *multi_st_ops_find_nolock (u32 id) +{ +struct bpf_testmod_multi_st_ops *st_ops; + +hlist_for_each_entry (st_ops, &multi_st_ops_list, node) { +if (st_ops->id == id) +return st_ops; + } + +return NULL ; +} + +int bpf_kfunc_multi_st_ops_test_1 (struct st_ops_args *args,u32 id) +{ +struct bpf_testmod_multi_st_ops *st_ops; +unsigned long flags; +int ret =-1 ; + +spin_lock_irqsave (&multi_st_ops_lock, flags); + st_ops =multi_st_ops_find_nolock (id); +if (st_ops) + ret = st_ops->test_1 (args); +spin_unlock_irqrestore (&multi_st_ops_lock, flags); + +return ret; +} + +static int multi_st_ops_reg (void *kdata,struct bpf_link *link) +{ +struct bpf_testmod_multi_st_ops *st_ops = + (struct bpf_testmod_multi_st_ops *)kdata; +unsigned long flags; +int err =0 ; +u32 id; + + id =bpf_struct_ops_id (kdata); + +spin_lock_irqsave (&multi_st_ops_lock, flags); +if (multi_st_ops_find_nolock (id)) { +pr_err ("multi_st_ops(id:%d) has already been registered\n" , id); + err = -EEXIST ; + goto unlock; + } + + st_ops->id = id; +hlist_add_head (&st_ops->node, &multi_st_ops_list); +unlock: +spin_unlock_irqrestore (&multi_st_ops_lock, flags); + +return err; +} + +static void multi_st_ops_unreg (void *kdata,struct bpf_link *link) +{ +struct bpf_testmod_multi_st_ops *st_ops; +unsigned long flags; +u32 id; + + id =bpf_struct_ops_id (kdata); + +spin_lock_irqsave (&multi_st_ops_lock, flags); + st_ops =multi_st_ops_find_nolock (id); +if (st_ops) +hlist_del (&st_ops->node); +spin_unlock_irqrestore (&multi_st_ops_lock, flags); +} + +static int bpf_testmod_multi_st_ops__test_1 (struct st_ops_args *args) +{ +return 0 ; +} + +static struct bpf_testmod_multi_st_ops multi_st_ops_cfi_stubs = { + .test_1 = bpf_testmod_multi_st_ops__test_1, +}; + +struct bpf_struct_ops testmod_multi_st_ops = { + .verifier_ops = &bpf_testmod_verifier_ops, + .init = multi_st_ops_init, + .init_member = multi_st_ops_init_member, + .reg = multi_st_ops_reg, + .unreg = multi_st_ops_unreg, + .cfi_stubs = &multi_st_ops_cfi_stubs, + .name ="bpf_testmod_multi_st_ops" , + .owner =THIS_MODULE , +}; +extern int bpf_fentry_test1 (int a);static int bpf_testmod_init (void )
@@ -1550,6 +1658,7 @@ static int bpf_testmod_init(void) ret = ret ?:register_bpf_struct_ops (&bpf_testmod_ops2, bpf_testmod_ops2); ret = ret ?:register_bpf_struct_ops (&bpf_testmod_ops3, bpf_testmod_ops3); ret = ret ?:register_bpf_struct_ops (&testmod_st_ops, bpf_testmod_st_ops); + ret = ret ?:register_bpf_struct_ops (&testmod_multi_st_ops, bpf_testmod_multi_st_ops); ret = ret ?:register_btf_id_dtor_kfuncs (bpf_testmod_dtors,ARRAY_SIZE (bpf_testmod_dtors),THIS_MODULE );
tools/testing/selftests/bpf/test_kmods/bpf_testmod.h +8 -0
@@ -116,4 +116,12 @@ struct bpf_testmod_st_ops { struct module *owner; }; +#define BPF_TESTMOD_NAME_SZ 16 + +struct bpf_testmod_multi_st_ops { +int (*test_1)(struct st_ops_args *args); +struct hlist_node node; +int id; +}; +#endif /* _BPF_TESTMOD_H */
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +2 -0
@@ -159,4 +159,6 @@ void bpf_kfunc_trusted_task_test(struct task_struct *ptr) __ksym; void bpf_kfunc_trusted_num_test(int *ptr) __ksym; void bpf_kfunc_rcu_task_test(struct task_struct *ptr) __ksym; +int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym; +#endif /* _BPF_TESTMOD_KFUNC_H */
Signature · 1 line
2.47.3
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 2/3] selftests/bpf: Add multi_st_ops that supports multiple instances
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c static struct bpf_struct_ops testmod_st_ops = { · line 1573
Quoted 10 lines · Amery Hung
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +10
-0+int bpf_kfunc_multi_st_ops_test_1 (struct st_ops_args *args,u32 id) +{ +struct bpf_testmod_multi_st_ops *st_ops; +unsigned long flags; +int ret =-1 ; + +spin_lock_irqsave (&multi_st_ops_lock, flags); + st_ops =multi_st_ops_find_nolock (id); +if (st_ops) + ret = st_ops->test_1 (args);
test_1 cannot be NULL,
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c static struct bpf_struct_ops testmod_st_ops = { · line 1586
Quoted 13 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +13
-0+spin_unlock_irqrestore (&multi_st_ops_lock, flags); + +return ret; +} + +static int multi_st_ops_reg (void *kdata,struct bpf_link *link) +{ +struct bpf_testmod_multi_st_ops *st_ops = + (struct bpf_testmod_multi_st_ops *)kdata; +unsigned long flags; +int err =0 ; +u32 id; +
so I added a "if (!st_ops->test_1)" test.
Quoted 16 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +16
-0+ id =bpf_struct_ops_id (kdata); + +spin_lock_irqsave (&multi_st_ops_lock, flags); +if (multi_st_ops_find_nolock (id)) { +pr_err ("multi_st_ops(id:%d) has already been registered\n" , id); + err = -EEXIST ; + goto unlock; + } + + st_ops->id = id; +hlist_add_head (&st_ops->node, &multi_st_ops_list); +unlock: +spin_unlock_irqrestore (&multi_st_ops_lock, flags); + +return err; +}
[ ... ]
tools/testing/selftests/bpf/test_kmods/bpf_testmod.h struct bpf_testmod_st_ops { · line 119
Quoted 9 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.h +1
-0@@ -116,4 +116,12 @@ struct bpf_testmod_st_ops {struct module *owner; }; +#define BPF_TESTMOD_NAME_SZ 16
Not sure why it is here. I don't see it is used, so removed.
PATCH v2 3/3 selftests/bpf: Test multi_st_ops and calling kfuncs from different programs
In reply to Amery HungTest multi_st_ops and demonstrate how different bpf programs can call a kfuncs that refers to the struct_ops instance in the same source file by id. The id is defined as a global vairable and initialized before attaching the skeleton. Kfuncs that take the id can hide the argument with a macro to make it almost transparent to bpf program developers.
The test involves two struct_ops returning different values from .test_1. In syscall and tracing programs, check if the correct value is returned by a kfunc that calls .test_1.
--- .../test_struct_ops_id_ops_mapping.c | 77 +++++++++++++++++++ .../bpf/progs/struct_ops_id_ops_mapping1.c | 59 ++++++++++++++ .../bpf/progs/struct_ops_id_ops_mapping2.c | 59 ++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c new file +77 -0
@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "struct_ops_id_ops_mapping1.skel.h" +#include "struct_ops_id_ops_mapping2.skel.h" + +static void test_st_ops_id_ops_mapping (void ) +{ +struct struct_ops_id_ops_mapping1 *skel1 =NULL ; +struct struct_ops_id_ops_mapping2 *skel2 =NULL ; +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct bpf_map_info info = {}; +__u32 len =sizeof (info); +int err, pid, prog1_fd, prog2_fd; + + skel1 =struct_ops_id_ops_mapping1__open_and_load (); +if (!ASSERT_OK_PTR (skel1,"struct_ops_id_ops_mapping1__open" )) + goto out; + + skel2 =struct_ops_id_ops_mapping2__open_and_load (); +if (!ASSERT_OK_PTR (skel2,"struct_ops_id_ops_mapping2__open" )) + goto out; + + err =bpf_map_get_info_by_fd (bpf_map__fd (skel1->maps.st_ops_map), + &info, &len); +if (!ASSERT_OK (err,"bpf_map_get_info_by_fd" )) + goto out; + + skel1->bss->st_ops_id = info.id; + + err =bpf_map_get_info_by_fd (bpf_map__fd (skel2->maps.st_ops_map), + &info, &len); +if (!ASSERT_OK (err,"bpf_map_get_info_by_fd" )) + goto out; + + skel2->bss->st_ops_id = info.id; + + err =struct_ops_id_ops_mapping1__attach (skel1); +if (!ASSERT_OK (err,"struct_ops_id_ops_mapping1__attach" )) + goto out; + + err =struct_ops_id_ops_mapping2__attach (skel2); +if (!ASSERT_OK (err,"struct_ops_id_ops_mapping2__attach" )) + goto out; + +/* run tracing prog that calls .test_1 and checks return */ + pid =getpid (); + skel1->bss->test_pid = pid; + skel2->bss->test_pid = pid; +sys_gettid (); + skel1->bss->test_pid =0 ; + skel2->bss->test_pid =0 ; + +/* run syscall_prog that calls .test_1 and checks return */ + prog1_fd =bpf_program__fd (skel1->progs.syscall_prog); + err =bpf_prog_test_run_opts (prog1_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + + prog2_fd =bpf_program__fd (skel2->progs.syscall_prog); + err =bpf_prog_test_run_opts (prog2_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +ASSERT_EQ (skel1->bss->test_err,0 ,"skel1->bss->test_err" ); +ASSERT_EQ (skel2->bss->test_err,0 ,"skel2->bss->test_err" ); + +out: +if (skel1) +struct_ops_id_ops_mapping1__destroy (skel1); +if (skel2) +struct_ops_id_ops_mapping2__destroy (skel2); +} + +void test_struct_ops_id_ops_mapping (void ) +{ +if (test__start_subtest ("st_ops_id_ops_mapping" )) +test_st_ops_id_ops_mapping (); +}
tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c new file +59 -0
59 lines not on this page · raw message
tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v2 3/3] selftests/bpf: Test multi_st_ops and calling kfuncs from different programs
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c line 11
Quoted 5 lines · Amery Hung
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c new file +5
-0+static void test_st_ops_id_ops_mapping (void ) +{ +struct struct_ops_id_ops_mapping1 *skel1 =NULL ; +struct struct_ops_id_ops_mapping2 *skel2 =NULL ; +LIBBPF_OPTS (bpf_test_run_opts, topts);
A default topts is not needed. Passing NULL is as good, so removed.
Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c new file +48
-0+struct bpf_map_info info = {}; +__u32 len =sizeof (info); +int err, pid, prog1_fd, prog2_fd; + + skel1 =struct_ops_id_ops_mapping1__open_and_load (); +if (!ASSERT_OK_PTR (skel1,"struct_ops_id_ops_mapping1__open" )) + goto out; + + skel2 =struct_ops_id_ops_mapping2__open_and_load (); +if (!ASSERT_OK_PTR (skel2,"struct_ops_id_ops_mapping2__open" )) + goto out; + + err =bpf_map_get_info_by_fd (bpf_map__fd (skel1->maps.st_ops_map), + &info, &len); +if (!ASSERT_OK (err,"bpf_map_get_info_by_fd" )) + goto out; + + skel1->bss->st_ops_id = info.id; + + err =bpf_map_get_info_by_fd (bpf_map__fd (skel2->maps.st_ops_map), + &info, &len); +if (!ASSERT_OK (err,"bpf_map_get_info_by_fd" )) + goto out; + + skel2->bss->st_ops_id = info.id; + + err =struct_ops_id_ops_mapping1__attach (skel1); +if (!ASSERT_OK (err,"struct_ops_id_ops_mapping1__attach" )) + goto out; + + err =struct_ops_id_ops_mapping2__attach (skel2); +if (!ASSERT_OK (err,"struct_ops_id_ops_mapping2__attach" )) + goto out; + +/* run tracing prog that calls .test_1 and checks return */ + pid =getpid (); + skel1->bss->test_pid = pid; + skel2->bss->test_pid = pid; +sys_gettid (); + skel1->bss->test_pid =0 ; + skel2->bss->test_pid =0 ; + +/* run syscall_prog that calls .test_1 and checks return */ + prog1_fd =bpf_program__fd (skel1->progs.syscall_prog); + err =bpf_prog_test_run_opts (prog1_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + + prog2_fd =bpf_program__fd (skel2->progs.syscall_prog);
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c line 69
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c new file +10
-0+ err =bpf_prog_test_run_opts (prog2_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +ASSERT_EQ (skel1->bss->test_err,0 ,"skel1->bss->test_err" ); +ASSERT_EQ (skel2->bss->test_err,0 ,"skel2->bss->test_err" ); + +out: +if (skel1) +struct_ops_id_ops_mapping1__destroy (skel1); +if (skel2)
NULL check on skel[12] is not needed, so removed.
Applied. Thanks.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c new file +3
-0+struct_ops_id_ops_mapping2__destroy (skel2); +} +
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 5 lines · On Wed, 6 Aug 2025 09:25:37 -0700 you wrote:
v1 -> v2
Addbpf_struct_ops_id()instead of usingbpf_struct_ops_get()Hi,
Quoted 7 lines
This patchset allows
struct_opsimplementors to get map id from kdata inreg(),unreg()andupdate()so that they create an id tostruct_opsinstance mapping. This in turn allowsstruct_opskfuncs to refer to the calling instance without passing a pointer to thestruct_ops. The selftest provides an end-to-end example.[...]
Here is the summary with links:
- [bpf-next,v2,1/3] bpf: Allow struct_ops to get map id by kdata
https://git.kernel.org/bpf/bpf-next/c/d87a513d0937 - [bpf-next,v2,2/3] selftests/bpf: Add multi_st_ops that supports multiple instances https://git.kernel.org/bpf/bpf-next/c/eeb52b6279cf - [bpf-next,v2,3/3] selftests/bpf: Test multi_st_ops and calling kfuncs from different programs https://git.kernel.org/bpf/bpf-next/c/ba7000f1c360
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html