Support associating BPF programs with struct_ops
Subsystems (8)
cover Support associating BPF programs with struct_ops
This patchset adds a new BPF command BPF_STRUCT_OPS_ASSOCIATE_PROG to the bpf() syscall to allow associating a BPF program with a struct_ops. The command is introduced to address a emerging need from struct_ops users. As the number of subsystems adopting struct_ops grows, more users are building their struct_ops-based solution with some help from other BPF programs. For exmample, scx_layer uses a syscall program as a user space trigger to refresh layers [0]. It also uses tracing program to infer whether a task is using GPU and needs to be prioritized [1]. In these use cases, when there are multiple struct_ops instances, the struct_ops kfuncs called from different BPF programs, whether struct_ops or not needs to be able to refer to a specific one, which currently is not possible.
The new BPF command will allow users to explicitly associate a BPF program with a struct_ops map. The libbpf wrapper can be called after loading programs and before attaching programs and struct_ops.
Internally, it will set prog->aux->st_ops_assoc to the struct_ops struct (i.e., kdata). struct_ops kfuncs can then get the associated struct_ops by adding a "__prog" argument. The value of the speical argument will be fixed up by the verifier during verification.
The command conceptually associates the implementation of BPF programs with struct_ops map, not the attachment. A program associated with the map will take a refcount of it so that st_ops_assoc always points to a valid struct_ops struct. However, the struct_ops can be in an uninitialized or unattached state. The struct_ops implementer will be responsible to maintain and check the state of the associated struct_ops before accessing it.
We can also consider support associating struct_ops link with BPF programs, which on one hand make struct_ops implementer's job easier, but might complicate libbpf workflow and does not apply to legacy struct_ops attachment.
[0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L557
[1] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L754
Amery Hung (4):
bpf: Allow verifier to fixup kernel module kfuncs
bpf: Support associating BPF program with struct_ops
libbpf: Add bpf_struct_ops_associate_prog() API selftests/bpf: Test BPF_STRUCT_OPS_ASSOCIATE_PROG command
include/linux/bpf.h | 11 ++ include/uapi/linux/bpf.h | 16 +++ kernel/bpf/bpf_struct_ops.c | 32 ++++++ kernel/bpf/core.c | 6 + kernel/bpf/syscall.c | 38 +++++++ kernel/bpf/verifier.c | 3 +- tools/include/uapi/linux/bpf.h | 16 +++ tools/lib/bpf/bpf.c | 18 +++ tools/lib/bpf/bpf.h | 19 ++++ tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 14 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c
Signature · 1 line
2.47.3
RFC PATCH 1/4 bpf: Allow verifier to fixup kernel module kfuncs
Allow verifier to fixup kfuncs in kernel module to support kfuncs with __prog arguments. Currently, special kfuncs and kfuncs with __prog arguments are kernel kfuncs. As there is no safety reason that prevents a kernel module kfunc from accessing prog->aux, allow it by removing the kernel BTF check.
--- kernel/bpf/verifier.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/bpf/verifier.c +1 -2
@@ -21889,8 +21889,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (!bpf_jit_supports_far_kfunc_call ()) insn->imm =BPF_CALL_IMM (desc->addr); -if (insn->off) -return 0 ; +if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl] || desc->func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl]) {struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta;
Signature · 1 line
2.47.3
Re: [RFC PATCH v1 bpf-next 1/4] bpf: Allow verifier to fixup kernel module kfuncs
Quoted 6 lines · Amery Hung
Allow verifier to fixup kfuncs in kernel module to support kfuncs with
__progarguments. Currently, special kfuncs and kfuncs with__progarguments are kernel kfuncs. As there is no safety reason that prevents a kernel module kfunc from accessingprog->aux, allow it by removing the kernel BTF check.
I'd just clarify that this should be fine and shouldn't confuse all those desc->func_id comparisons because BTF IDs in module BTF are always greater than any of vmlinux BTF ID due to split BTF setup.
Quoted 23 lines
Signed-off-by Amery Hung <[email protected]>--- kernel/bpf/verifier.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)kernel/bpf/verifier.c +1
-3@@ -21889,8 +21889,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (!bpf_jit_supports_far_kfunc_call ()) insn->imm =BPF_CALL_IMM (desc->addr); -if (insn->off) -return 0 ; +if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl] || desc->func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl]) {struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; --2.47.3
Re: [RFC PATCH v1 bpf-next 1/4] bpf: Allow verifier to fixup kernel module kfuncs
Quoted 9 lines · Andrii Nakryiko
Quoted 6 lines · Amery Hung
Allow verifier to fixup kfuncs in kernel module to support kfuncs with
__progarguments. Currently, special kfuncs and kfuncs with__progarguments are kernel kfuncs. As there is no safety reason that prevents a kernel module kfunc from accessingprog->aux, allow it by removing the kernel BTF check.
Quoted 3 lines
I'd just clarify that this should be fine and shouldn't confuse all those
desc->func_idcomparisons because BTF IDs in module BTF are always greater than any of vmlinux BTF ID due to split BTF setup.
Got it. I will make the commit message less confusing.
Quoted 25 lines
Quoted 25 lines
Signed-off-by Amery Hung <[email protected]>--- kernel/bpf/verifier.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)kernel/bpf/verifier.c +1
-3@@ -21889,8 +21889,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,if (!bpf_jit_supports_far_kfunc_call ()) insn->imm =BPF_CALL_IMM (desc->addr); -if (insn->off) -return 0 ; +if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl] || desc->func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl]) {struct btf_struct_meta *kptr_struct_meta = env->insn_aux_data[insn_idx].kptr_struct_meta; --2.47.3
RFC PATCH 2/4 bpf: Support associating BPF program with struct_ops
In reply to Amery HungAdd a new BPF command BPF_STRUCT_OPS_ASSOCIATE_PROG to allow associating a BPF program with a struct_ops. This command takes a file descriptor of a struct_ops map and a BPF program and set prog->aux->st_ops_assoc to the kdata of the struct_ops map.
The command does not accept a struct_ops program or a non-struct_ops map. Programs of a struct_ops map is automatically associated with the map during map update. If a program is shared between two struct_ops maps, the first one will be the map associated with the program. The associated struct_ops map, once set cannot be changed later. This restriction may be lifted in the future if there is a use case.
Each associated programs except struct_ops programs of the map will take a refcount on the map to pin it so that prog->aux->st_ops_assoc, if set, is always valid. However, it is not guaranteed whether the map members are fully updated nor is it attached or not. For example, a BPF program can be associated with a struct_ops map before map_update. The struct_ops implementer will be responsible for maintaining and checking the state of the associated struct_ops map before accessing it.
--- include/linux/bpf.h | 11 ++++++++++ include/uapi/linux/bpf.h | 16 ++++++++++++++ kernel/bpf/bpf_struct_ops.c | 32 ++++++++++++++++++++++++++++ kernel/bpf/core.c | 6 ++++++ kernel/bpf/syscall.c | 38 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 16 ++++++++++++++ 6 files changed, 119 insertions(+)
include/linux/bpf.h +11 -0
@@ -1710,6 +1710,8 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +struct mutex st_ops_assoc_mutex; +void *st_ops_assoc; };struct bpf_prog {
@@ -2010,6 +2012,8 @@ 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); +int bpf_struct_ops_assoc_prog (struct bpf_map *map,struct bpf_prog *prog); +void bpf_struct_ops_disassoc_prog (struct bpf_prog *prog);u32 bpf_struct_ops_id (const void *kdata);#ifdef CONFIG_NET
@@ -2057,6 +2061,13 @@ static inline int bpf_struct_ops_link_create(union bpf_attr *attr) {return -EOPNOTSUPP ; } +static inline void bpf_struct_ops_assoc_prog (struct bpf_map *map,struct bpf_prog *prog) +{ +return -EOPNOTSUPP ; +} +static inline void bpf_struct_ops_disassoc_prog (struct bpf_prog *prog) +{ +}static inline void bpf_map_struct_ops_info_fill (struct bpf_map_info *info,struct bpf_map *map) { }
include/uapi/linux/bpf.h +16 -0
@@ -918,6 +918,16 @@ union bpf_iter_link_info {* Number of bytes read from the stream on success, or -1 if an * error occurred (in which case, *errno* is set appropriately). * +* BPF_STRUCT_OPS_ASSOCIATE_PROG +* Description +* Associate a BPF program with a struct_ops map. The struct_ops +* map is identified by *map_fd* and the BPF program is +* identified by *prog_fd*. +* +* Return +* 0 on success or -1 if an error occurred (in which case, +* *errno* is set appropriately). +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -974,6 +984,7 @@ enum bpf_cmd {BPF_PROG_BIND_MAP ,BPF_TOKEN_CREATE ,BPF_PROG_STREAM_READ_BY_FD , +BPF_STRUCT_OPS_ASSOCIATE_PROG , __MAX_BPF_CMD, };
@@ -1890,6 +1901,11 @@ union bpf_attr {__u32 prog_fd; } prog_stream_read; +struct { +__u32 map_fd; +__u32 prog_fd; + } struct_ops_assoc_prog; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
kernel/bpf/bpf_struct_ops.c +32 -0
@@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)for (i =0 ; i < st_map->funcs_cnt; i++) {if (!st_map->links[i])break ; +bpf_struct_ops_disassoc_prog (st_map->links[i]->prog);bpf_link_put (st_map->links[i]); st_map->links[i] =NULL ; }
@@ -801,6 +802,11 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto reset_unlock; } +/* Don't stop a program from being reused. prog->aux->st_ops_assoc +* will point to the first struct_ops kdata. +*/ +bpf_struct_ops_assoc_prog (&st_map->map, prog); + link =kzalloc (sizeof (*link),GFP_USER );if (!link) {bpf_prog_put (prog);
@@ -1394,6 +1400,32 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)return err; } +int bpf_struct_ops_assoc_prog (struct bpf_map *map,struct bpf_prog *prog) +{ +struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; +void *kdata = &st_map->kvalue.data; +int ret =0 ; + +mutex_lock (&prog->aux->st_ops_assoc_mutex); + +if (prog->aux->st_ops_assoc && prog->aux->st_ops_assoc != kdata) { + ret = -EBUSY ; + goto out; + } + + prog->aux->st_ops_assoc = kdata; +out: +mutex_unlock (&prog->aux->st_ops_assoc_mutex); +return ret; +} + +void bpf_struct_ops_disassoc_prog (struct bpf_prog *prog) +{ +mutex_lock (&prog->aux->st_ops_assoc_mutex); + prog->aux->st_ops_assoc =NULL ; +mutex_unlock (&prog->aux->st_ops_assoc_mutex); +} +void bpf_map_struct_ops_info_fill (struct bpf_map_info *info,struct bpf_map *map) {struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
kernel/bpf/core.c +6 -0
@@ -136,6 +136,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flagmutex_init (&fp->aux->used_maps_mutex);mutex_init (&fp->aux->ext_mutex);mutex_init (&fp->aux->dst_mutex); +mutex_init (&fp->aux->st_ops_assoc_mutex);#ifdef CONFIG_BPF_SYSCALL bpf_prog_stream_init (fp);
@@ -286,6 +287,7 @@ void __bpf_prog_free(struct bpf_prog *fp)if (fp->aux) {mutex_destroy (&fp->aux->used_maps_mutex);mutex_destroy (&fp->aux->dst_mutex); +mutex_destroy (&fp->aux->st_ops_assoc_mutex);kfree (fp->aux->poke_tab);kfree (fp->aux); }
@@ -2875,6 +2877,10 @@ static void bpf_prog_free_deferred(struct work_struct *work)#endif bpf_free_used_maps (aux);bpf_free_used_btfs (aux); +if (aux->st_ops_assoc) { +bpf_struct_ops_put (aux->st_ops_assoc); +bpf_struct_ops_disassoc_prog (aux->prog); + }if (bpf_prog_is_dev_bound (aux))bpf_prog_dev_bound_destroy (aux->prog);#ifdef CONFIG_PERF_EVENTS
kernel/bpf/syscall.c +38 -0
@@ -6092,6 +6092,41 @@ static int prog_stream_read(union bpf_attr *attr)return ret; } +#define BPF_STRUCT_OPS_ASSOCIATE_PROG_LAST_FIELD struct_ops_assoc_prog.prog_fd + +static int struct_ops_assoc_prog (union bpf_attr *attr) +{ +struct bpf_prog *prog; +struct bpf_map *map; +int ret; + +if (CHECK_ATTR (BPF_STRUCT_OPS_ASSOCIATE_PROG )) +return -EINVAL ; + + prog =bpf_prog_get (attr->struct_ops_assoc_prog.prog_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + map =bpf_map_get (attr->struct_ops_assoc_prog.map_fd); +if (IS_ERR (map)) { + ret =PTR_ERR (map); + goto out; + } + +if (map->map_type !=BPF_MAP_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) { + ret = -EINVAL ; + goto out; + } + + ret =bpf_struct_ops_assoc_prog (map, prog); +out: +if (ret && !IS_ERR (map)) +bpf_map_put (map); +bpf_prog_put (prog); +return ret; +} +static int __sys_bpf (enum bpf_cmd cmd,bpfptr_t uattr,unsigned int size) {union bpf_attr attr;
@@ -6231,6 +6266,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)case BPF_PROG_STREAM_READ_BY_FD : err =prog_stream_read (&attr);break ; +case BPF_STRUCT_OPS_ASSOCIATE_PROG : + err =struct_ops_assoc_prog (&attr); +break ;default : err = -EINVAL ;break ;
tools/include/uapi/linux/bpf.h +16 -0
@@ -918,6 +918,16 @@ union bpf_iter_link_info {* Number of bytes read from the stream on success, or -1 if an * error occurred (in which case, *errno* is set appropriately). * +* BPF_STRUCT_OPS_ASSOCIATE_PROG +* Description +* Associate a BPF program with a struct_ops map. The struct_ops +* map is identified by *map_fd* and the BPF program is +* identified by *prog_fd*. +* +* Return +* 0 on success or -1 if an error occurred (in which case, +* *errno* is set appropriately). +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -974,6 +984,7 @@ enum bpf_cmd {BPF_PROG_BIND_MAP ,BPF_TOKEN_CREATE ,BPF_PROG_STREAM_READ_BY_FD , +BPF_STRUCT_OPS_ASSOCIATE_PROG , __MAX_BPF_CMD, };
@@ -1890,6 +1901,11 @@ union bpf_attr {__u32 prog_fd; } prog_stream_read; +struct { +__u32 map_fd; +__u32 prog_fd; + } struct_ops_assoc_prog; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
Signature · 1 line
2.47.3
kernel test robot
build botRe: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
kernel test robot noticed the following build errors:
Hi Amery,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Amery-Hung/bpf-Allow-verifier-to-fixup-kernel-module-kfuncs/20251011-015144 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20251010174953.2884682-3-ameryhung%40gmail.com patch subject: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops config: i386-buildonly-randconfig-004-20251011 (https://download.01.org/0day-ci/archive/20251011/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251011/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/security.h:35,
from include/linux/perf_event.h:53, from include/linux/trace_events.h:10, from include/trace/syscall.h:7, from include/linux/syscalls.h:95, from include/linux/syscalls_api.h:1, from kernel/sched/sched.h:61,
from kernel/sched/rq-offsets.c:5:
include/linux/bpf.h: In function 'bpf_struct_ops_assoc_prog':
>> include/linux/bpf.h:2066:16: error: 'return' with a value, in function returning void [-Wreturn-mismatch]
2066 | return -EOPNOTSUPP;
| ^ include/linux/bpf.h:2064:20: note: declared here
2064 | static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
make[3]: *** [scripts/Makefile.build:182: kernel/sched/rq-offsets.s] Error 1 shuffle=2700263361 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1280: prepare0] Error 2 shuffle=2700263361
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=2700263361
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=2700263361
make: Target 'prepare' not remade because of errors.
vim +/return +2066 include/linux/bpf.h
2031
2032 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
2033 union bpf_attr __user *uattr);
2034 #endif
2035 int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
2036 struct btf *btf,
2037 struct bpf_verifier_log *log);
2038 void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2039 void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);
2040 #else
2041 #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })
2042 static inline bool bpf_try_module_get(const void *data, struct module *owner)
2043 {
2044 return try_module_get(owner);
2045 }
2046 static inline void bpf_module_put(const void *data, struct module *owner)
2047 {
2048 module_put(owner);
2049 }
2050 static inline int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff)
2051 {
2052 return -ENOTSUPP;
2053 }
2054 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
2055 void *key,
2056 void *value)
2057 {
2058 return -EINVAL;
2059 }
2060 static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
2061 {
2062 return -EOPNOTSUPP;
2063 }
2064 static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
2065 {
> 2066 return -EOPNOTSUPP;
2067 }
2068 static inline void bpf_struct_ops_disassoc_prog(struct bpf_prog *prog)
2069 {
2070 }
2071 static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
2072 {
2073 }
2074
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
In reply to Amery Hungkernel test robot noticed the following build warnings:
Hi Amery,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Amery-Hung/bpf-Allow-verifier-to-fixup-kernel-module-kfuncs/20251011-015144 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20251010174953.2884682-3-ameryhung%40gmail.com patch subject: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops config: i386-buildonly-randconfig-001-20251011 (https://download.01.org/0day-ci/archive/20251011/[email protected]/config) compiler: gcc-13 (Debian 13.3.0-16) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251011/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from include/linux/security.h:35,
from include/linux/perf_event.h:53, from include/linux/trace_events.h:10, from include/trace/syscall.h:7, from include/linux/syscalls.h:95, from include/linux/syscalls_api.h:1, from kernel/sched/sched.h:61,
from kernel/sched/rq-offsets.c:5:
include/linux/bpf.h: In function 'bpf_struct_ops_assoc_prog':
>> include/linux/bpf.h:2066:16: warning: 'return' with a value, in function returning void [-Wreturn-type]
2066 | return -EOPNOTSUPP;
| ^ include/linux/bpf.h:2064:20: note: declared here
2064 | static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
--
In file included from include/linux/security.h:35,
from include/linux/perf_event.h:53,
from include/linux/trace_events.h:10,
from include/trace/syscall.h:7,
from include/linux/syscalls.h:95,
from include/linux/syscalls_api.h:1,
from kernel/sched/sched.h:61,
from kernel/sched/build_policy.c:41:
include/linux/bpf.h: In function 'bpf_struct_ops_assoc_prog':
>> include/linux/bpf.h:2066:16: warning: 'return' with a value, in function returning void [-Wreturn-type]
2066 | return -EOPNOTSUPP;
| ^
include/linux/bpf.h:2064:20: note: declared here
2064 | static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from kernel/sched/build_policy.c:52:
kernel/sched/rt.c: At top level:
kernel/sched/rt.c:12:18: warning: 'max_rt_runtime' defined but not used [-Wunused-const-variable=]
12 | static const u64 max_rt_runtime = MAX_BW;
| ^~~~~~~~~~~~~~
--
In file included from include/linux/filter.h:9,
from kernel/bpf/core.c:22:
include/linux/bpf.h: In function 'bpf_struct_ops_assoc_prog':
>> include/linux/bpf.h:2066:16: warning: 'return' with a value, in function returning void [-Wreturn-type]
2066 | return -EOPNOTSUPP;
| ^
include/linux/bpf.h:2064:20: note: declared here
2064 | static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/core.c: In function 'bpf_prog_free_deferred':
kernel/bpf/core.c:2881:17: error: implicit declaration of function 'bpf_struct_ops_put'; did you mean 'bpf_struct_ops_find'? [-Werror=implicit-function-declaration]
2881 | bpf_struct_ops_put(aux->st_ops_assoc);
| ^~~~~~~~~~~~~~~~~~
| bpf_struct_ops_find cc1: some warnings being treated as errors
--
In file included from include/linux/security.h:35,
from include/linux/perf_event.h:53, from include/linux/trace_events.h:10, from include/trace/syscall.h:7, from include/linux/syscalls.h:95, from include/linux/syscalls_api.h:1, from kernel/sched/sched.h:61,
from kernel/sched/rq-offsets.c:5:
include/linux/bpf.h: In function 'bpf_struct_ops_assoc_prog':
>> include/linux/bpf.h:2066:16: warning: 'return' with a value, in function returning void [-Wreturn-type]
2066 | return -EOPNOTSUPP;
| ^ include/linux/bpf.h:2064:20: note: declared here
2064 | static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
vim +/return +2066 include/linux/bpf.h
2031
2032 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
2033 union bpf_attr __user *uattr);
2034 #endif
2035 int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
2036 struct btf *btf,
2037 struct bpf_verifier_log *log);
2038 void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2039 void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);
2040 #else
2041 #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })
2042 static inline bool bpf_try_module_get(const void *data, struct module *owner)
2043 {
2044 return try_module_get(owner);
2045 }
2046 static inline void bpf_module_put(const void *data, struct module *owner)
2047 {
2048 module_put(owner);
2049 }
2050 static inline int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff)
2051 {
2052 return -ENOTSUPP;
2053 }
2054 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
2055 void *key,
2056 void *value)
2057 {
2058 return -EINVAL;
2059 }
2060 static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
2061 {
2062 return -EOPNOTSUPP;
2063 }
2064 static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
2065 {
> 2066 return -EOPNOTSUPP;
2067 }
2068 static inline void bpf_struct_ops_disassoc_prog(struct bpf_prog *prog)
2069 {
2070 }
2071 static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
2072 {
2073 }
2074
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
In reply to Amery HungQuoted 36 lines · Amery Hung
Add a new BPF command
BPF_STRUCT_OPS_ASSOCIATE_PROGto allow associating a BPF program with astruct_ops. This command takes a file descriptor of astruct_opsmap and a BPF program and setprog->aux->st_ops_assocto the kdata of thestruct_opsmap.The command does not accept a
struct_opsprogram or a non-struct_ops map. Programs of astruct_opsmap is automatically associated with the map during map update. If a program is shared between twostruct_opsmaps, the first one will be the map associated with the program. The associatedstruct_opsmap, once set cannot be changed later. This restriction may be lifted in the future if there is a use case.Each associated programs except
struct_opsprograms of the map will take a refcount on the map to pin it so thatprog->aux->st_ops_assoc, if set, is always valid. However, it is not guaranteed whether the map members are fully updated nor is it attached or not. For example, a BPF program can be associated with astruct_opsmap beforemap_update. Thestruct_opsimplementer will be responsible for maintaining and checking the state of the associatedstruct_opsmap before accessing it.Signed-off-by Amery Hung <[email protected]>--- include/linux/bpf.h | 11 ++++++++++ include/uapi/linux/bpf.h | 16 ++++++++++++++ kernel/bpf/bpf_struct_ops.c | 32 ++++++++++++++++++++++++++++ kernel/bpf/core.c | 6 ++++++ kernel/bpf/syscall.c | 38 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 16 ++++++++++++++ 6 files changed, 119 insertions(+)include/linux/bpf.h +0
-0
include/linux/bpf.h struct bpf_prog_aux { · line 1713
Quoted 4 lines
include/linux/bpf.h +1
-0@@ -1710,6 +1710,8 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +struct mutex st_ops_assoc_mutex;
do we need a mutex at all? cmpxchg() should work just fine. We'll also potentially need to access st_ops_assoc from kprobes/fentry anyways, and we can't just take mutex there
Quoted 4 lines
include/linux/bpf.h +1
-0+void *st_ops_assoc; };struct bpf_prog {
[...]
include/linux/bpf.h union bpf_attr { · line 1906
Quoted 8 lines
include/linux/bpf.h +3
-0@@ -1890,6 +1901,11 @@ union bpf_attr {__u32 prog_fd; } prog_stream_read; +struct { +__u32 map_fd; +__u32 prog_fd;
let's add flags, we normally have some sort of flags for commands for extensibility
Quoted 18 lines
include/linux/bpf.h +2
-0+ } struct_ops_assoc_prog; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF kernel/bpf/bpf_struct_ops.c +1
-0@@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)for (i =0 ; i < st_map->funcs_cnt; i++) {if (!st_map->links[i])break ; +bpf_struct_ops_disassoc_prog (st_map->links[i]->prog);bpf_link_put (st_map->links[i]); st_map->links[i] =NULL ; }
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 805
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +1
-0@@ -801,6 +802,11 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto reset_unlock; } +/* Don't stop a program from being reused. prog->aux->st_ops_assoc
nit: comment style, we are converging onto /* on separate line
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 808
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +3
-0+* will point to the first struct_ops kdata. +*/ +bpf_struct_ops_assoc_prog (&st_map->map, prog);
ignoring error? we should do something better here... poisoning this association altogether if program is used in multiple struct_ops seems like the only thing we can reasonable do, no?
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +1
-0+ link =kzalloc (sizeof (*link),GFP_USER );if (!link) {bpf_prog_put (prog);
[...]
kernel/bpf/bpf_struct_ops.c
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +2
-0+#define BPF_STRUCT_OPS_ASSOCIATE_PROG_LAST_FIELD struct_ops_assoc_prog.prog_fd +
looking at libbpf side, it's quite a mouthful to write out bpf_struct_ops_associate_prog()... maybe let's shorten this to BPF_STRUCT_OPS_ASSOC or BPF_ASSOC_STRUCT_OPS (with the idea that we associate struct_ops with a program). The latter is actually a bit more preferable, because then we can have a meaningful high-level bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map), where map has to be struct_ops. Having bpf_map__assoc_prog() is a bit too generic, as this works only for struct_ops maps.
It's all not major, but I think that lends for a bit better naming and more logical usage throughout.
Quoted 11 lines
kernel/bpf/bpf_struct_ops.c +11
-0+static int struct_ops_assoc_prog (union bpf_attr *attr) +{ +struct bpf_prog *prog; +struct bpf_map *map; +int ret; + +if (CHECK_ATTR (BPF_STRUCT_OPS_ASSOCIATE_PROG )) +return -EINVAL ; + + prog =bpf_prog_get (attr->struct_ops_assoc_prog.prog_fd); +if (IS_ERR (prog))
kernel/bpf/bpf_struct_ops.c
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +10
-0+return PTR_ERR (prog); + + map =bpf_map_get (attr->struct_ops_assoc_prog.map_fd); +if (IS_ERR (map)) { + ret =PTR_ERR (map); + goto out; + } + +if (map->map_type !=BPF_MAP_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) {
you can check prog->type earlier, before getting map itself
kernel/bpf/bpf_struct_ops.c
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ ret = -EINVAL ; + goto out; + } + + ret =bpf_struct_ops_assoc_prog (map, prog); +out: +if (ret && !IS_ERR (map))
nit: purely stylistic preference, but I'd rather have a clear error-only clean up path, and success with explicit return 0, instead of checking ret or IS_ERR(map)
...
/* goto to put_{map,prog}, depending on how far we've got */
err = bpf_struct_ops_assoc_prog(map, prog);
if (err)
goto put_map;
return 0;put_map:
bpf_map_put(map);
put_prog:
bpf_prog_put(prog);
return err;Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +5
-0+bpf_map_put (map); +bpf_prog_put (prog); +return ret; +} +
[...]
Re: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
Quoted 59 lines · Andrii Nakryiko
Quoted 36 lines · Amery Hung
Add a new BPF command
BPF_STRUCT_OPS_ASSOCIATE_PROGto allow associating a BPF program with astruct_ops. This command takes a file descriptor of astruct_opsmap and a BPF program and setprog->aux->st_ops_assocto the kdata of thestruct_opsmap.The command does not accept a
struct_opsprogram or a non-struct_ops map. Programs of astruct_opsmap is automatically associated with the map during map update. If a program is shared between twostruct_opsmaps, the first one will be the map associated with the program. The associatedstruct_opsmap, once set cannot be changed later. This restriction may be lifted in the future if there is a use case.Each associated programs except
struct_opsprograms of the map will take a refcount on the map to pin it so thatprog->aux->st_ops_assoc, if set, is always valid. However, it is not guaranteed whether the map members are fully updated nor is it attached or not. For example, a BPF program can be associated with astruct_opsmap beforemap_update. Thestruct_opsimplementer will be responsible for maintaining and checking the state of the associatedstruct_opsmap before accessing it.Signed-off-by Amery Hung <[email protected]>--- include/linux/bpf.h | 11 ++++++++++ include/uapi/linux/bpf.h | 16 ++++++++++++++ kernel/bpf/bpf_struct_ops.c | 32 ++++++++++++++++++++++++++++ kernel/bpf/core.c | 6 ++++++ kernel/bpf/syscall.c | 38 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 16 ++++++++++++++ 6 files changed, 119 insertions(+)include/linux/bpf.h +0
-0include/linux/bpf.h struct bpf_prog_aux { · line 1713
Quoted 4 lines
include/linux/bpf.h +1
-0@@ -1710,6 +1710,8 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +struct mutex st_ops_assoc_mutex;do we need a mutex at all?
cmpxchg()should work just fine. We'll also potentially need to accessst_ops_assocfrom kprobes/fentry anyways, and we can't just take mutex thereQuoted 4 lines
include/linux/bpf.h +1
-0+void *st_ops_assoc; };struct bpf_prog {[...]
Quoted 5 lines
include/linux/bpf.h +0
-0@@ -1890,6 +1901,11 @@ union bpf_attr {__u32 prog_fd; } prog_stream_read;
Quoted 6 lines
include/linux/bpf.h union bpf_attr { · line 1906
Quoted 3 lines
include/linux/bpf.h +3
-0+struct { +__u32 map_fd; +__u32 prog_fd;let's add flags, we normally have some sort of flags for commands for extensibility
I will add a flag
Quoted 22 lines
Quoted 22 lines
include/linux/bpf.h +2
-0+ } struct_ops_assoc_prog; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF kernel/bpf/bpf_struct_ops.c +1
-0@@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)for (i =0 ; i < st_map->funcs_cnt; i++) {if (!st_map->links[i])break ; +bpf_struct_ops_disassoc_prog (st_map->links[i]->prog);bpf_link_put (st_map->links[i]); st_map->links[i] =NULL ; }@@ -801,6 +802,11 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto reset_unlock; }
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 805
Quoted 1 line
kernel/bpf/bpf_struct_ops.c +1
-0+/* Don't stop a program from being reused. prog->aux->st_ops_assoc nit: comment style, we are converging onto /* on separate line
Got it, so I assume it applies to kerne/bpf/* even existing comments in the file are netdev style. Is it also the case for net/core/filter.c?
Quoted 29 lines
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 808
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +3
-0+* will point to the first struct_ops kdata. +*/ +bpf_struct_ops_assoc_prog (&st_map->map, prog);ignoring error? we should do something better here... poisoning this association altogether if program is used in multiple
struct_opsseems like the only thing we can reasonable do, no?Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +1
-0+ link =kzalloc (sizeof (*link),GFP_USER );if (!link) {bpf_prog_put (prog);[...]
kernel/bpf/bpf_struct_ops.c
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +2
-0+#define BPF_STRUCT_OPS_ASSOCIATE_PROG_LAST_FIELD struct_ops_assoc_prog.prog_fd +looking at libbpf side, it's quite a mouthful to write out
bpf_struct_ops_associate_prog()... maybe let's shorten this toBPF_STRUCT_OPS_ASSOCorBPF_ASSOC_STRUCT_OPS(with the idea that we associatestruct_opswith a program). The latter is actually a bit more preferable, because then we can have a meaningful high-levelbpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map), where map has to bestruct_ops. Havingbpf_map__assoc_prog()is a bit too generic, as this works only forstruct_opsmaps.
Quoted 2 lines
It's all not major, but I think that lends for a bit better naming and more logical usage throughout.
Will change the naming.
Quoted 16 lines
Quoted 16 lines
kernel/bpf/bpf_struct_ops.c +15
-0+static int struct_ops_assoc_prog (union bpf_attr *attr) +{ +struct bpf_prog *prog; +struct bpf_map *map; +int ret; + +if (CHECK_ATTR (BPF_STRUCT_OPS_ASSOCIATE_PROG )) +return -EINVAL ; + + prog =bpf_prog_get (attr->struct_ops_assoc_prog.prog_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + map =bpf_map_get (attr->struct_ops_assoc_prog.map_fd); +if (IS_ERR (map)) {
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +6
-0+ ret =PTR_ERR (map); + goto out; + } + +if (map->map_type !=BPF_MAP_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) {you can check
prog->typeearlier, before getting map itself
Got it. I will make it a separate check right after getting prog.
Quoted 23 lines
kernel/bpf/bpf_struct_ops.c
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ ret = -EINVAL ; + goto out; + } + + ret =bpf_struct_ops_assoc_prog (map, prog); +out: +if (ret && !IS_ERR (map))nit: purely stylistic preference, but I'd rather have a clear error-only clean up path, and success with explicit return 0, instead of checking ret or
IS_ERR(map)... /* goto to put_{map,prog}, depending on how far we've got */ err = bpf_struct_ops_assoc_prog(map, prog); if (err) goto put_map; return 0;
Quoted 5 lines
put_map:bpf_map_put(map); put_prog: bpf_prog_put(prog); return err;
I will separate error path out.
Quoted 9 lines
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +5
-0+bpf_map_put (map); +bpf_prog_put (prog); +return ret; +} +[...]
Re: [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops
Quoted 36 lines · Amery Hung
Quoted 33 lines · Andrii Nakryiko
Quoted 31 lines · Amery Hung
Add a new BPF command
BPF_STRUCT_OPS_ASSOCIATE_PROGto allow associating a BPF program with astruct_ops. This command takes a file descriptor of astruct_opsmap and a BPF program and setprog->aux->st_ops_assocto the kdata of thestruct_opsmap.The command does not accept a
struct_opsprogram or a non-struct_ops map. Programs of astruct_opsmap is automatically associated with the map during map update. If a program is shared between twostruct_opsmaps, the first one will be the map associated with the program. The associatedstruct_opsmap, once set cannot be changed later. This restriction may be lifted in the future if there is a use case.Each associated programs except
struct_opsprograms of the map will take a refcount on the map to pin it so thatprog->aux->st_ops_assoc, if set, is always valid. However, it is not guaranteed whether the map members are fully updated nor is it attached or not. For example, a BPF program can be associated with astruct_opsmap beforemap_update. Thestruct_opsimplementer will be responsible for maintaining and checking the state of the associatedstruct_opsmap before accessing it.Signed-off-by Amery Hung <[email protected]>--- include/linux/bpf.h | 11 ++++++++++ include/uapi/linux/bpf.h | 16 ++++++++++++++ kernel/bpf/bpf_struct_ops.c | 32 ++++++++++++++++++++++++++++ kernel/bpf/core.c | 6 ++++++ kernel/bpf/syscall.c | 38 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 16 ++++++++++++++ 6 files changed, 119 insertions(+)
[...]
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
include/uapi/linux/bpf.h +2
-0+ } struct_ops_assoc_prog; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF kernel/bpf/bpf_struct_ops.c +1
-0@@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)for (i =0 ; i < st_map->funcs_cnt; i++) {if (!st_map->links[i])break ; +bpf_struct_ops_disassoc_prog (st_map->links[i]->prog);bpf_link_put (st_map->links[i]); st_map->links[i] =NULL ; }@@ -801,6 +802,11 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto reset_unlock; }
Quoted 7 lines
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 805
Quoted 1 line
kernel/bpf/bpf_struct_ops.c +1
-0+/* Don't stop a program from being reused. prog->aux->st_ops_assoc nit: comment style, we are converging onto /* on separate line
Got it, so I assume it applies to kerne/bpf/* even existing comments in the file are netdev style. Is it also the case for
net/core/filter.c?
yeah
Quoted 31 lines
Quoted 31 lines
kernel/bpf/bpf_struct_ops.c static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · line 808
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +3
-0+* will point to the first struct_ops kdata. +*/ +bpf_struct_ops_assoc_prog (&st_map->map, prog);ignoring error? we should do something better here... poisoning this association altogether if program is used in multiple
struct_opsseems like the only thing we can reasonable do, no?Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +1
-0+ link =kzalloc (sizeof (*link),GFP_USER );if (!link) {bpf_prog_put (prog);[...]
kernel/bpf/bpf_struct_ops.c
Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +2
-0+#define BPF_STRUCT_OPS_ASSOCIATE_PROG_LAST_FIELD struct_ops_assoc_prog.prog_fd +looking at libbpf side, it's quite a mouthful to write out
bpf_struct_ops_associate_prog()... maybe let's shorten this toBPF_STRUCT_OPS_ASSOCorBPF_ASSOC_STRUCT_OPS(with the idea that we associatestruct_opswith a program). The latter is actually a bit more preferable, because then we can have a meaningful high-levelbpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map), where map has to bestruct_ops. Havingbpf_map__assoc_prog()is a bit too generic, as this works only forstruct_opsmaps.
Quoted 4 lines
Quoted 2 lines
It's all not major, but I think that lends for a bit better naming and more logical usage throughout.
Will change the naming.
thanks
Quoted 51 lines
Quoted 17 lines
Quoted 17 lines
kernel/bpf/bpf_struct_ops.c +15
-0+static int struct_ops_assoc_prog (union bpf_attr *attr) +{ +struct bpf_prog *prog; +struct bpf_map *map; +int ret; + +if (CHECK_ATTR (BPF_STRUCT_OPS_ASSOCIATE_PROG )) +return -EINVAL ; + + prog =bpf_prog_get (attr->struct_ops_assoc_prog.prog_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + map =bpf_map_get (attr->struct_ops_assoc_prog.map_fd); +if (IS_ERR (map)) {Quoted 8 lines
kernel/bpf/bpf_struct_ops.c
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +6
-0+ ret =PTR_ERR (map); + goto out; + } + +if (map->map_type !=BPF_MAP_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) {you can check
prog->typeearlier, before getting map itselfGot it. I will make it a separate check right after getting prog.
Quoted 23 lines
kernel/bpf/bpf_struct_ops.c
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +7
-0+ ret = -EINVAL ; + goto out; + } + + ret =bpf_struct_ops_assoc_prog (map, prog); +out: +if (ret && !IS_ERR (map))nit: purely stylistic preference, but I'd rather have a clear error-only clean up path, and success with explicit return 0, instead of checking ret or
IS_ERR(map)... /* goto to put_{map,prog}, depending on how far we've got */ err = bpf_struct_ops_assoc_prog(map, prog); if (err) goto put_map; return 0;
Quoted 7 lines
Quoted 5 lines
put_map:bpf_map_put(map); put_prog: bpf_prog_put(prog); return err;I will separate error path out.
great, thanks
Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +5
-0+bpf_map_put (map); +bpf_prog_put (prog); +return ret; +} +[...]
RFC PATCH 3/4 libbpf: Add bpf_struct_ops_associate_prog() API
In reply to Amery HungAdd low-level wrapper API for BPF_STRUCT_OPS_ASSOCIATE_PROG command in bpf() syscall.
--- tools/lib/bpf/bpf.c | 18 ++++++++++++++++++ tools/lib/bpf/bpf.h | 19 +++++++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 38 insertions(+)
tools/lib/bpf/bpf.c +18 -0
@@ -1397,3 +1397,21 @@ int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, err =sys_bpf (BPF_PROG_STREAM_READ_BY_FD , &attr, attr_sz);return libbpf_err_errno (err); } + +int bpf_struct_ops_associate_prog (int map_fd,int prog_fd, +struct bpf_struct_ops_associate_prog_opts *opts) +{ + const size_t attr_sz = offsetofend(union bpf_attr, struct_ops_assoc_prog); +union bpf_attr attr; +int err; + +if (!OPTS_VALID (opts, bpf_struct_ops_associate_prog_opts)) +return libbpf_err (-EINVAL ); + +memset (&attr,0 , attr_sz); + attr.struct_ops_assoc_prog.map_fd = map_fd; + attr.struct_ops_assoc_prog.prog_fd = prog_fd; + + err =sys_bpf (BPF_STRUCT_OPS_ASSOCIATE_PROG , &attr, attr_sz); +return libbpf_err_errno (err); +}
tools/lib/bpf/bpf.h +19 -0
@@ -733,6 +733,25 @@ struct bpf_prog_stream_read_opts { LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,struct bpf_prog_stream_read_opts *opts); +struct bpf_struct_ops_associate_prog_opts { +size_t sz; + size_t :0; +}; +#define bpf_struct_ops_associate_prog_opts__last_field sz +/** +* @brief **bpf_struct_ops_associate_prog** associate a BPF program with a +* struct_ops map. +* +* @param map_fd FD for the struct_ops map to be associated with a BPF progam +* @param prog_fd FD for the BPF program +* @param opts optional options, can be NULL +* +* @return 0 on success; negative error code, otherwise (errno is also set to +* the error code) +*/ +LIBBPF_API int bpf_struct_ops_associate_prog(int map_fd, int prog_fd, +struct bpf_struct_ops_associate_prog_opts *opts); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +1 -0
@@ -451,4 +451,5 @@ LIBBPF_1.7.0 { global: bpf_map__set_exclusive_program; bpf_map__exclusive_program; + bpf_struct_ops_associate_prog; } LIBBPF_1.6.0;
Signature · 1 line
2.47.3
Re: [RFC PATCH v1 bpf-next 3/4] libbpf: Add bpf_struct_ops_associate_prog() API
Quoted 42 lines · Amery Hung
Add low-level wrapper API for
BPF_STRUCT_OPS_ASSOCIATE_PROGcommand inbpf()syscall.Signed-off-by Amery Hung <[email protected]>--- tools/lib/bpf/bpf.c | 18 ++++++++++++++++++ tools/lib/bpf/bpf.h | 19 +++++++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 38 insertions(+)tools/lib/bpf/bpf.c +18
-0@@ -1397,3 +1397,21 @@ int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len, err =sys_bpf (BPF_PROG_STREAM_READ_BY_FD , &attr, attr_sz);return libbpf_err_errno (err); } + +int bpf_struct_ops_associate_prog (int map_fd,int prog_fd, +struct bpf_struct_ops_associate_prog_opts *opts) +{ + const size_t attr_sz = offsetofend(union bpf_attr, struct_ops_assoc_prog); +union bpf_attr attr; +int err; + +if (!OPTS_VALID (opts, bpf_struct_ops_associate_prog_opts)) +return libbpf_err (-EINVAL ); + +memset (&attr,0 , attr_sz); + attr.struct_ops_assoc_prog.map_fd = map_fd; + attr.struct_ops_assoc_prog.prog_fd = prog_fd; + + err =sys_bpf (BPF_STRUCT_OPS_ASSOCIATE_PROG , &attr, attr_sz); +return libbpf_err_errno (err); +}tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h struct bpf_prog_stream_read_opts { · line 737
Quoted 5 lines
tools/lib/bpf/bpf.h +2
-0@@ -733,6 +733,25 @@ struct bpf_prog_stream_read_opts { LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,struct bpf_prog_stream_read_opts *opts); +struct bpf_struct_ops_associate_prog_opts { +size_t sz;
there will be flags, justifying this struct :)
Quoted 33 lines
tools/lib/bpf/bpf.h +17
-0+ size_t :0; +}; +#define bpf_struct_ops_associate_prog_opts__last_field sz +/** +* @brief **bpf_struct_ops_associate_prog** associate a BPF program with a +* struct_ops map. +* +* @param map_fd FD for the struct_ops map to be associated with a BPF progam +* @param prog_fd FD for the BPF program +* @param opts optional options, can be NULL +* +* @return 0 on success; negative error code, otherwise (errno is also set to +* the error code) +*/ +LIBBPF_API int bpf_struct_ops_associate_prog(int map_fd, int prog_fd, +struct bpf_struct_ops_associate_prog_opts *opts); + #ifdef __cplusplus }/* extern "C" */ #endif tools/lib/bpf/libbpf.map +1
-1@@ -451,4 +451,5 @@ LIBBPF_1.7.0 { global: bpf_map__set_exclusive_program; bpf_map__exclusive_program; + bpf_struct_ops_associate_prog; } LIBBPF_1.6.0; --2.47.3
RFC PATCH 4/4 selftests/bpf: Test BPF_STRUCT_OPS_ASSOCIATE_PROG command
In reply to Amery HungTest BPF_STRUCT_OPS_ASSOCIATE_PROG command that associates a BPF program with a struct_ops. The test follows the same logic in commit ba7000f1c360 ("selftests/bpf: Test multi_st_ops and calling kfuncs from different programs"), but instead of using map id to identify a specific struct_ops this test uses the new BPF command to associate a struct_ops with a program.
The test consists of two set of almost identical struct_ops maps and BPF programs associated with the map. Their only difference is a unique value returned by bpf_testmod_multi_st_ops::test_1().
The test first loads the programs and associates them with struct_ops maps. Then, the test exercises the BPF programs. They will in turn call kfunc bpf_kfunc_multi_st_ops_test_1_prog_arg() to trigger test_1() of the associated struct_ops map, and then check if the right unique value is returned.
--- .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 199 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c
tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c new file +76 -0
@@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "struct_ops_assoc.skel.h" + +static void test_st_ops_assoc (void ) +{ +int sys_enter_prog_a_fd, sys_enter_prog_b_fd; +int syscall_prog_a_fd, syscall_prog_b_fd; +struct struct_ops_assoc *skel =NULL ; +int err, pid, map_a_fd, map_b_fd; + + skel =struct_ops_assoc__open_and_load (); +if (!ASSERT_OK_PTR (skel,"struct_ops_assoc__open" )) + goto out; + + sys_enter_prog_a_fd =bpf_program__fd (skel->progs.sys_enter_prog_a); + sys_enter_prog_b_fd =bpf_program__fd (skel->progs.sys_enter_prog_b); + syscall_prog_a_fd =bpf_program__fd (skel->progs.syscall_prog_a); + syscall_prog_b_fd =bpf_program__fd (skel->progs.syscall_prog_b); + map_a_fd =bpf_map__fd (skel->maps.st_ops_map_a); + map_b_fd =bpf_map__fd (skel->maps.st_ops_map_b); + + err =bpf_struct_ops_associate_prog (map_a_fd, syscall_prog_a_fd,NULL ); +if (!ASSERT_OK (err,"bpf_struct_ops_associate_prog" )) + goto out; + + err =bpf_struct_ops_associate_prog (map_a_fd, sys_enter_prog_a_fd,NULL ); +if (!ASSERT_OK (err,"bpf_struct_ops_associate_prog" )) + goto out; + + err =bpf_struct_ops_associate_prog (map_b_fd, syscall_prog_b_fd,NULL ); +if (!ASSERT_OK (err,"bpf_struct_ops_associate_prog" )) + goto out; + + err =bpf_struct_ops_associate_prog (map_b_fd, sys_enter_prog_b_fd,NULL ); +if (!ASSERT_OK (err,"bpf_struct_ops_associate_prog" )) + goto out; + +/* sys_enter_prog_a already associated with map_a */ + err =bpf_struct_ops_associate_prog (map_b_fd, sys_enter_prog_a_fd,NULL ); +if (!ASSERT_ERR (err,"bpf_struct_ops_associate_prog" )) + goto out; + + err =struct_ops_assoc__attach (skel); +if (!ASSERT_OK (err,"struct_ops_assoc__attach" )) + goto out; + +/* run tracing prog that calls .test_1 and checks return */ + pid =getpid (); + skel->bss->test_pid = pid; +sys_gettid (); + skel->bss->test_pid =0 ; + +ASSERT_EQ (skel->bss->test_err_a,0 ,"skel->bss->test_err_a" ); +ASSERT_EQ (skel->bss->test_err_b,0 ,"skel->bss->test_err_b" ); + +/* run syscall_prog that calls .test_1 and checks return */ + err =bpf_prog_test_run_opts (syscall_prog_a_fd,NULL ); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + + err =bpf_prog_test_run_opts (syscall_prog_b_fd,NULL ); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +ASSERT_EQ (skel->bss->test_err_a,0 ,"skel->bss->test_err" ); +ASSERT_EQ (skel->bss->test_err_b,0 ,"skel->bss->test_err" ); + +out: +struct_ops_assoc__destroy (skel); +} + +void test_struct_ops_assoc (void ) +{ +if (test__start_subtest ("st_ops_assoc" )) +test_st_ops_assoc (); +}
tools/testing/selftests/bpf/progs/struct_ops_assoc.c new file +105 -0
105 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +17 -0
@@ -1101,6 +1101,7 @@ __bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args) } __bpf_kfunc int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id); +__bpf_kfunc int bpf_kfunc_multi_st_ops_test_1_prog_arg(struct st_ops_args *args, void *aux_prog);BTF_KFUNCS_START (bpf_testmod_check_kfunc_ids)BTF_ID_FLAGS (func, bpf_testmod_test_mod_kfunc)
@@ -1143,6 +1144,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABL 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_ID_FLAGS(func, bpf_kfunc_multi_st_ops_test_1_prog_arg, KF_TRUSTED_ARGS)BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)
@@ -1604,6 +1606,7 @@ static struct bpf_testmod_multi_st_ops *multi_st_ops_find_nolock(u32 id)return NULL ; } +/* Call test_1() of the struct_ops map identified by the id */ int bpf_kfunc_multi_st_ops_test_1 (struct st_ops_args *args,u32 id) {struct bpf_testmod_multi_st_ops *st_ops;
@@ -1619,6 +1622,20 @@ int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id)return ret; } +/* Call test_1() of the associated struct_ops map */ +int bpf_kfunc_multi_st_ops_test_1_prog_arg (struct st_ops_args *args,void *aux__prog) +{ +struct bpf_prog_aux *prog_aux = (struct bpf_prog_aux *)aux__prog; +struct bpf_testmod_multi_st_ops *st_ops; +int ret =-1 ; + + st_ops = (struct bpf_testmod_multi_st_ops *)prog_aux->st_ops_assoc; +if (st_ops) + ret = st_ops->test_1 (args); + +return ret; +} +static int multi_st_ops_reg (void *kdata,struct bpf_link *link) { struct bpf_testmod_multi_st_ops *st_ops =
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1 -0
@@ -162,5 +162,6 @@ struct task_struct *bpf_kfunc_ret_rcu_test(void) __ksym;int *bpf_kfunc_ret_rcu_test_nostruct (int rdonly_buf_size) __ksym; int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym; +int bpf_kfunc_multi_st_ops_test_1_prog_arg(struct st_ops_args *args, void *aux__prog) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */
Signature · 1 line
2.47.3
Re: [RFC PATCH v1 bpf-next 4/4] selftests/bpf: Test BPF_STRUCT_OPS_ASSOCIATE_PROG command
Quoted 28 lines · Amery Hung
Test
BPF_STRUCT_OPS_ASSOCIATE_PROGcommand that associates a BPF program with astruct_ops. The test follows the same logic in commitba7000f1c360("selftests/bpf: Testmulti_st_opsand calling kfuncs from different programs"), but instead of using map id to identify a specificstruct_opsthis test uses the new BPF command to associate astruct_opswith a program.The test consists of two set of almost identical
struct_opsmaps and BPF programs associated with the map. Their only difference is a unique value returned bybpf_testmod_multi_st_ops::test_1().The test first loads the programs and associates them with
struct_opsmaps. Then, the test exercises the BPF programs. They will in turn call kfuncbpf_kfunc_multi_st_ops_test_1_prog_arg()to triggertest_1()of the associatedstruct_opsmap, and then check if the right unique value is returned.Signed-off-by Amery Hung <[email protected]>--- .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 199 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c
[...]
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) · line 1632
Quoted 8 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +8
-0+/* Call test_1() of the associated struct_ops map */ +int bpf_kfunc_multi_st_ops_test_1_prog_arg (struct st_ops_args *args,void *aux__prog) +{ +struct bpf_prog_aux *prog_aux = (struct bpf_prog_aux *)aux__prog; +struct bpf_testmod_multi_st_ops *st_ops; +int ret =-1 ; + + st_ops = (struct bpf_testmod_multi_st_ops *)prog_aux->st_ops_assoc;
let's have internal helper API to fetch struct_ops association, this will give us a bit more freedom in handling various edge cases (like the poisoning I mentioned)
Quoted 23 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +6
-0+if (st_ops) + ret = st_ops->test_1 (args); + +return ret; +} +static int multi_st_ops_reg (void *kdata,struct bpf_link *link) { struct bpf_testmod_multi_st_ops *st_ops =tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1
-1@@ -162,5 +162,6 @@ struct task_struct *bpf_kfunc_ret_rcu_test(void) __ksym;int *bpf_kfunc_ret_rcu_test_nostruct (int rdonly_buf_size) __ksym; int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym; +int bpf_kfunc_multi_st_ops_test_1_prog_arg(struct st_ops_args *args, void *aux__prog) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */ --2.47.3
Re: [RFC PATCH v1 bpf-next 4/4] selftests/bpf: Test BPF_STRUCT_OPS_ASSOCIATE_PROG command
Quoted 33 lines · Andrii Nakryiko
Quoted 28 lines · Amery Hung
Test
BPF_STRUCT_OPS_ASSOCIATE_PROGcommand that associates a BPF program with astruct_ops. The test follows the same logic in commitba7000f1c360("selftests/bpf: Testmulti_st_opsand calling kfuncs from different programs"), but instead of using map id to identify a specificstruct_opsthis test uses the new BPF command to associate astruct_opswith a program.The test consists of two set of almost identical
struct_opsmaps and BPF programs associated with the map. Their only difference is a unique value returned bybpf_testmod_multi_st_ops::test_1().The test first loads the programs and associates them with
struct_opsmaps. Then, the test exercises the BPF programs. They will in turn call kfuncbpf_kfunc_multi_st_ops_test_1_prog_arg()to triggertest_1()of the associatedstruct_opsmap, and then check if the right unique value is returned.Signed-off-by Amery Hung <[email protected]>--- .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 4 files changed, 199 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c[...]
Quoted 12 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) · line 1632
Quoted 8 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +8
-0+/* Call test_1() of the associated struct_ops map */ +int bpf_kfunc_multi_st_ops_test_1_prog_arg (struct st_ops_args *args,void *aux__prog) +{ +struct bpf_prog_aux *prog_aux = (struct bpf_prog_aux *)aux__prog; +struct bpf_testmod_multi_st_ops *st_ops; +int ret =-1 ; + + st_ops = (struct bpf_testmod_multi_st_ops *)prog_aux->st_ops_assoc;let's have internal helper API to fetch
struct_opsassociation, this will give us a bit more freedom in handling various edge cases (like the poisoning I mentioned)
I will poison st_ops_assoc when struct_ops programs get reused and add an API to access this field.
Quoted 25 lines
Quoted 25 lines
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +6
-0+if (st_ops) + ret = st_ops->test_1 (args); + +return ret; +} +static int multi_st_ops_reg (void *kdata,struct bpf_link *link) { struct bpf_testmod_multi_st_ops *st_ops =tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +1
-1@@ -162,5 +162,6 @@ struct task_struct *bpf_kfunc_ret_rcu_test(void) __ksym;int *bpf_kfunc_ret_rcu_test_nostruct (int rdonly_buf_size) __ksym; int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym; +int bpf_kfunc_multi_st_ops_test_1_prog_arg(struct st_ops_args *args, void *aux__prog) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */ --2.47.3
Quoted 36 lines · Amery Hung
This patchset adds a new BPF command
BPF_STRUCT_OPS_ASSOCIATE_PROGto thebpf()syscall to allow associating a BPF program with astruct_ops. The command is introduced to address a emerging need fromstruct_opsusers. As the number of subsystems adoptingstruct_opsgrows, more users are building their struct_ops-based solution with some help from other BPF programs. For exmample,scx_layeruses a syscall program as a user space trigger to refresh layers [0]. It also uses tracing program to infer whether a task is using GPU and needs to be prioritized [1]. In these use cases, when there are multiplestruct_opsinstances, thestruct_opskfuncs called from different BPF programs, whetherstruct_opsor not needs to be able to refer to a specific one, which currently is not possible.The new BPF command will allow users to explicitly associate a BPF program with a
struct_opsmap. The libbpf wrapper can be called after loading programs and before attaching programs andstruct_ops.Internally, it will set
prog->aux->st_ops_assocto thestruct_opsstruct (i.e., kdata).struct_opskfuncs can then get the associatedstruct_opsby adding a "__prog" argument. The value of the speical argument will be fixed up by the verifier during verification.The command conceptually associates the implementation of BPF programs with
struct_opsmap, not the attachment. A program associated with the map will take a refcount of it so thatst_ops_assocalways points to a validstruct_opsstruct. However, thestruct_opscan be in an uninitialized or unattached state. Thestruct_opsimplementer will be responsible to maintain and check the state of the associatedstruct_opsbefore accessing it.We can also consider support associating
struct_opslink with BPF programs, which on one hand makestruct_opsimplementer's job easier, but might complicate libbpf workflow and does not apply to legacystruct_opsattachment.
Quoted 9 lines
[0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L557
[1] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L754Amery Hung (4):
bpf: Allow verifier to fixup kernel module kfuncs
bpf: Support associating BPF program withstruct_ops
libbpf: Addbpf_struct_ops_associate_prog()API selftests/bpf: TestBPF_STRUCT_OPS_ASSOCIATE_PROGcommand
please also drop RFC from the next revision
Quoted 21 lines
include/linux/bpf.h | 11 ++ include/uapi/linux/bpf.h | 16 +++ kernel/bpf/bpf_struct_ops.c | 32 ++++++ kernel/bpf/core.c | 6 + kernel/bpf/syscall.c | 38 +++++++ kernel/bpf/verifier.c | 3 +- tools/include/uapi/linux/bpf.h | 16 +++ tools/lib/bpf/bpf.c | 18 +++ tools/lib/bpf/bpf.h | 19 ++++ tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 14 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c--
2.47.3
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Amery Hung
This patchset adds a new BPF command
BPF_STRUCT_OPS_ASSOCIATE_PROGto thebpf()syscall to allow associating a BPF program with astruct_ops. The command is introduced to address a emerging need fromstruct_opsusers. As the number of subsystems adoptingstruct_opsgrows, more users are building their struct_ops-based solution with some help from other BPF programs. For exmample,scx_layeruses a syscall program as a user space trigger to refresh layers [0]. It also uses tracing program to infer whether a task is using GPU and needs to be prioritized [1]. In these use cases, when there are multiplestruct_opsinstances, thestruct_opskfuncs called from different BPF programs, whetherstruct_opsor not needs to be able to refer to a specific one, which currently is not possible.The new BPF command will allow users to explicitly associate a BPF program with a
struct_opsmap. The libbpf wrapper can be called after loading programs and before attaching programs andstruct_ops.Internally, it will set
prog->aux->st_ops_assocto thestruct_opsstruct (i.e., kdata).struct_opskfuncs can then get the associatedstruct_opsby adding a "__prog" argument. The value of the speical argument will be fixed up by the verifier during verification.The command conceptually associates the implementation of BPF programs with
struct_opsmap, not the attachment. A program associated with the map will take a refcount of it so thatst_ops_assocalways points to a validstruct_opsstruct. However, thestruct_opscan be in an uninitialized or unattached state. Thestruct_opsimplementer will be responsible to maintain and check the state of the associatedstruct_opsbefore accessing it.We can also consider support associating
struct_opslink with BPF programs, which on one hand makestruct_opsimplementer's job easier, but might complicate libbpf workflow and does not apply to legacystruct_opsattachment.[0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L557
[1] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c#L754
Quoted 8 lines
Quoted 6 lines
Amery Hung (4):
bpf: Allow verifier to fixup kernel module kfuncs
bpf: Support associating BPF program withstruct_ops
libbpf: Addbpf_struct_ops_associate_prog()API selftests/bpf: TestBPF_STRUCT_OPS_ASSOCIATE_PROGcommandplease also drop RFC from the next revision
Thanks for the review. I will drop the RFC tag in the next respin.
Quoted 22 lines
Quoted 22 lines
include/linux/bpf.h | 11 ++ include/uapi/linux/bpf.h | 16 +++ kernel/bpf/bpf_struct_ops.c | 32 ++++++ kernel/bpf/core.c | 6 + kernel/bpf/syscall.c | 38 +++++++ kernel/bpf/verifier.c | 3 +- tools/include/uapi/linux/bpf.h | 16 +++ tools/lib/bpf/bpf.c | 18 +++ tools/lib/bpf/bpf.h | 19 ++++ tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/test_struct_ops_assoc.c | 76 +++++++++++++ .../selftests/bpf/progs/struct_ops_assoc.c | 105 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 17 +++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 1 + 14 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_assoc.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_assoc.c--
2.47.3