Introduce eBPF support for HID devices (new attempt)
Subsystems (7)
v4 cover Introduce eBPF support for HID devices (new attempt)
Hi,
so after the reviews from v3, and some discussion with Alexei, I am back with a new version of HID-BPF.
This version is not complete (thus the RFC), but I'd like to share it now to get initial feedback, in case I am too far from the actual goal.
FTR, the goal is to provide some changes in the core verifier/btf so that we can plug in HID-BPF independently from BPF core. This way we can extend it without having to care about bpf-next.
The things I am not entirely sure are:
- do we need only fentry/fexit/fmod_ret BPF program types or should programs that modify the data stream use a different kind?
- patch 3/7 is probably not the correct approach (see comments in the patch itself)
We are missing quite a few bits here:
- selftests for patches 1 to 4
- add the ability to attach a program to a struct device, and run that program only for that struct device
- when running through bpf_prog_test_run_opts, how can we ensure we are talking to the correct device? (I have a feeling this is linked to the previous point)
- how can we reconnect the device when a report descriptor fixup BPF program is loaded (would it make sense to allow some notifications on when a BPF program is attached/detached to a device, and which function have been traced?)
Cheers,
Benjamin
Benjamin Tissoires (7):
bpf/btf: also allow kfunc in tracing programs bpf/verifier: allow kfunc to return an allocated mem
error-inject: add new type that carries if the function is non
sleepable
btf: Add a new kfunc set which allows to mark a function to be
sleepable
HID: initial BPF new way implementation
samples/bpf: add new hid_mouse example
selftests/bpf: add tests for the HID-bpf initial implementation
drivers/hid/hid-core.c | 115 +++++ include/asm-generic/error-injection.h | 1 + include/linux/btf.h | 8 + include/linux/hid_bpf.h | 29 ++ kernel/bpf/btf.c | 50 +- kernel/bpf/verifier.c | 76 ++- lib/error-inject.c | 2 + samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 23 + samples/bpf/hid_mouse.bpf.c | 59 +++ samples/bpf/hid_mouse.c | 131 +++++ tools/testing/selftests/bpf/config | 3 + tools/testing/selftests/bpf/prog_tests/hid.c | 482 +++++++++++++++++++ tools/testing/selftests/bpf/progs/hid.c | 32 ++ 14 files changed, 987 insertions(+), 25 deletions(-) create mode 100644 include/linux/hid_bpf.h create mode 100644 samples/bpf/hid_mouse.bpf.c create mode 100644 samples/bpf/hid_mouse.c create mode 100644 tools/testing/selftests/bpf/prog_tests/hid.c create mode 100644 tools/testing/selftests/bpf/progs/hid.c
Signature · 1 line
2.35.1
RFC v4 1/7 bpf/btf: also allow kfunc in tracing programs
using fentry/fexit and fmod_ret is very convenient to access module BPF capabilities, so be able to define kfuncs there is a must.
---
new in v4:
- I think this is where I need to add my new kfuncs, though in the end I need to be able to change the incoming data, so maybe only fmod_ret is the one we need to be able to be RW.
--- kernel/bpf/btf.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/bpf/btf.c +3 -0
@@ -202,6 +202,7 @@ enum btf_kfunc_hook {BTF_KFUNC_HOOK_XDP ,BTF_KFUNC_HOOK_TC ,BTF_KFUNC_HOOK_STRUCT_OPS , +BTF_KFUNC_HOOK_TRACING , BTF_KFUNC_HOOK_MAX, };
@@ -6892,6 +6893,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_TC ;case BPF_PROG_TYPE_STRUCT_OPS :return BTF_KFUNC_HOOK_STRUCT_OPS ; +case BPF_PROG_TYPE_TRACING : +return BTF_KFUNC_HOOK_TRACING ;default :return BTF_KFUNC_HOOK_MAX ; }
Signature · 1 line
2.35.1
RFC v4 2/7 bpf/verifier: allow kfunc to return an allocated mem
In reply to Benjamin TissoiresWhen a kfunc is not returning a pointer to a struct but to a plain type, check if one of the arguments is called __sz and is a const from the caller, and use this as the size of the allocated memory.
For tracing programs, we consider the provided memory to be read only unless the program is BPF_MODIFY_RETURN.
---
new in v4
--- include/linux/btf.h | 6 ++++ kernel/bpf/btf.c | 31 ++++++++++++++++---- kernel/bpf/verifier.c | 66 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 20 deletions(-)
include/linux/btf.h +6 -0
@@ -332,6 +332,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)return (struct btf_param *)(t +1 ); } +struct bpf_reg_state ; + +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg); +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;
kernel/bpf/btf.c +26 -5
@@ -5851,9 +5851,9 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,return true; } -static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) {int len, sfx_len =sizeof ("__sz" ) -1 ;const struct btf_type *t;
@@ -5976,7 +5976,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_btf = reg->btf; reg_ref_id = reg->btf_id;/* Ensure only one argument is referenced -* PTR_TO_BTF_ID, check_func_arg_reg_off relies +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off relies * on only one referenced register being allowed * for kfuncs. */
@@ -6012,7 +6012,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,u32 type_size;if (is_kfunc) { -bool arg_mem_size = i +1 < nargs &&is_kfunc_arg_mem_size (btf, &args[i +1 ], ®s[regno +1 ]); +bool arg_mem_size = i +1 < nargs && +btf_is_kfunc_arg_mem_size (btf, + &args[i +1 ], + ®s[regno +1 ]);/* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed
@@ -6039,6 +6042,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, i++;continue ; } + +if (rel && reg->ref_obj_id) { +/* Ensure only one argument is referenced +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off +* relies on only one referenced register being allowed +* for kfuncs. +*/ +if (ref_obj_id) { +bpf_log (log, +"verifier internal error: more than one arg with ref_obj_id R%d %u %u\n" , + regno, + reg->ref_obj_id, + ref_obj_id); +return -EFAULT ; + } + ref_regno = regno; + ref_obj_id = reg->ref_obj_id; + } } resolve_ret =btf_resolve_size (btf, ref_t, &type_size);
kernel/bpf/verifier.c +51 -15
104 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [RFC bpf-next v4 2/7] bpf/verifier: allow kfunc to return an allocated mem
Quoted 62 lines · Benjamin Tissoires
When a kfunc is not returning a pointer to a struct but to a plain type, check if one of the arguments is called
__szand is a const from the caller, and use this as the size of the allocated memory.For tracing programs, we consider the provided memory to be read only unless the program is
BPF_MODIFY_RETURN.Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4
--- include/linux/btf.h | 6 ++++ kernel/bpf/btf.c | 31 ++++++++++++++++---- kernel/bpf/verifier.c | 66 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 20 deletions(-)include/linux/btf.h +6
-0@@ -332,6 +332,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)return (struct btf_param *)(t +1 ); } +struct bpf_reg_state ; + +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg); +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;kernel/bpf/btf.c +4
-4@@ -5851,9 +5851,9 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,return true; } -static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) {int len, sfx_len =sizeof ("__sz" ) -1 ;const struct btf_type *t;@@ -5976,7 +5976,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_btf = reg->btf; reg_ref_id = reg->btf_id;/* Ensure only one argument is referenced -* PTR_TO_BTF_ID, check_func_arg_reg_off relies +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off relies * on only one referenced register being allowed * for kfuncs. */
kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 6018
Quoted 8 lines
kernel/bpf/btf.c +4
-1@@ -6012,7 +6012,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,u32 type_size;if (is_kfunc) { -bool arg_mem_size = i +1 < nargs &&is_kfunc_arg_mem_size (btf, &args[i +1 ], ®s[regno +1 ]); +bool arg_mem_size = i +1 < nargs && +btf_is_kfunc_arg_mem_size (btf, + &args[i +1 ], + ®s[regno +1 ]);
bpf allows ~100 chars. No need to break the line so much.
Quoted 96 lines
kernel/bpf/btf.c +18
-0/* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed @@ -6039,6 +6042,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, i++;continue ; } + +if (rel && reg->ref_obj_id) { +/* Ensure only one argument is referenced +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off +* relies on only one referenced register being allowed +* for kfuncs. +*/ +if (ref_obj_id) { +bpf_log (log, +"verifier internal error: more than one arg with ref_obj_id R%d %u %u\n" , + regno, + reg->ref_obj_id, + ref_obj_id); +return -EFAULT ; + } + ref_regno = regno; + ref_obj_id = reg->ref_obj_id; + } } resolve_ret =btf_resolve_size (btf, ref_t, &type_size);kernel/bpf/verifier.c +30
-8@@ -6974,7 +6974,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,int err, insn_idx = *insn_idx_p;const struct btf_param *args;struct btf *desc_btf; +enum bpf_prog_type prog_type =resolve_prog_type (env->prog);bool acq; +size_t reg_size =0 ;/* skip for now, but return error when we find this in fixup_kfunc_call */ if (!insn->imm)@@ -7015,8 +7017,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } -for (i =0 ; i <CALLER_SAVED_REGS ; i++) -mark_reg_not_init (env, regs, caller_saved[i]); +/* reset REG_0 */ +mark_reg_not_init (env, regs,BPF_REG_0 );/* Check return type */ t =btf_type_skip_modifiers (desc_btf, func_proto->type,NULL );@@ -7026,6 +7028,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return -EINVAL ; } + nargs =btf_type_vlen (func_proto); + args =btf_params (func_proto); +if (btf_type_is_scalar (t)) {mark_reg_unknown (env, regs,BPF_REG_0 );mark_btf_func_reg_size (env,BPF_REG_0 , t->size);@@ -7033,24 +7038,54 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ptr_type =btf_type_skip_modifiers (desc_btf, t->type, &ptr_type_id);if (!btf_type_is_struct (ptr_type)) { - ptr_type_name =btf_name_by_offset (desc_btf, - ptr_type->name_off); -verbose (env,"kernel function %s returns pointer type %s %s is not supported\n" , - func_name,btf_type_str (ptr_type), - ptr_type_name); -return -EINVAL ; +/* if we have an array, we must have a const argument named "__sz" */ +for (i =0 ; i < nargs; i++) { +u32 regno = i +BPF_REG_1 ; +struct bpf_reg_state *reg = ®s[regno]; + +/* look for any const scalar parameter of name "__sz" */ +if (!check_reg_arg (env, regno,SRC_OP ) && +tnum_is_const (regs[regno].var_off) && +btf_is_kfunc_arg_mem_size (desc_btf, &args[i], reg)) + reg_size = regs[regno].var_off.value; + } + +if (!reg_size) { + ptr_type_name =btf_name_by_offset (desc_btf, + ptr_type->name_off); +verbose (env, +"kernel function %s returns pointer type %s %s is not supported\n" , + func_name, +btf_type_str (ptr_type), + ptr_type_name); +return -EINVAL ; + } +
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 7073
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_MEM ; + regs[BPF_REG_0 ].mem_size = reg_size; + +/* in case of tracing, only allow write access to +* BPF_MODIFY_RETURN programs +*/ +if (prog_type ==BPF_PROG_TYPE_TRACING && + env->prog->expected_attach_type !=BPF_MODIFY_RETURN ) + regs[BPF_REG_0 ].type |=MEM_RDONLY ;
MOD_RET restriction looks artificial.
We can distinguish readonly vs writeable PTR_TO_MEM based on another naming convention.
Currently arg_name__sz applies to the previous argument. Matching suffix made sense there.
Reusing the same suffix matching for a different purpose could be confusing. For this use case we may reserve a full argument name.
Like "rdonly_buf_size" and "rdwr_buf_size" ?
Re: [RFC bpf-next v4 2/7] bpf/verifier: allow kfunc to return an allocated mem
Quoted 179 lines · Alexei Starovoitov
Quoted 62 lines · Benjamin Tissoires
When a kfunc is not returning a pointer to a struct but to a plain type, check if one of the arguments is called
__szand is a const from the caller, and use this as the size of the allocated memory.For tracing programs, we consider the provided memory to be read only unless the program is
BPF_MODIFY_RETURN.Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4
--- include/linux/btf.h | 6 ++++ kernel/bpf/btf.c | 31 ++++++++++++++++---- kernel/bpf/verifier.c | 66 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 20 deletions(-)include/linux/btf.h +6
-0@@ -332,6 +332,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)return (struct btf_param *)(t +1 ); } +struct bpf_reg_state ; + +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg); +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;kernel/bpf/btf.c +4
-4@@ -5851,9 +5851,9 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,return true; } -static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) {int len, sfx_len =sizeof ("__sz" ) -1 ;const struct btf_type *t;@@ -5976,7 +5976,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_btf = reg->btf; reg_ref_id = reg->btf_id;/* Ensure only one argument is referenced -* PTR_TO_BTF_ID, check_func_arg_reg_off relies +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off relies * on only one referenced register being allowed * for kfuncs. */ kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 6018
Quoted 8 lines
kernel/bpf/btf.c +4
-1@@ -6012,7 +6012,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,u32 type_size;if (is_kfunc) { -bool arg_mem_size = i +1 < nargs &&is_kfunc_arg_mem_size (btf, &args[i +1 ], ®s[regno +1 ]); +bool arg_mem_size = i +1 < nargs && +btf_is_kfunc_arg_mem_size (btf, + &args[i +1 ], + ®s[regno +1 ]);bpf allows ~100 chars. No need to break the line so much.
Quoted 104 lines
kernel/bpf/btf.c +18
-0/* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed @@ -6039,6 +6042,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, i++;continue ; } + +if (rel && reg->ref_obj_id) { +/* Ensure only one argument is referenced +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off +* relies on only one referenced register being allowed +* for kfuncs. +*/ +if (ref_obj_id) { +bpf_log (log, +"verifier internal error: more than one arg with ref_obj_id R%d %u %u\n" , + regno, + reg->ref_obj_id, + ref_obj_id); +return -EFAULT ; + } + ref_regno = regno; + ref_obj_id = reg->ref_obj_id; + } } resolve_ret =btf_resolve_size (btf, ref_t, &type_size);kernel/bpf/verifier.c +38
-8@@ -6974,7 +6974,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,int err, insn_idx = *insn_idx_p;const struct btf_param *args;struct btf *desc_btf; +enum bpf_prog_type prog_type =resolve_prog_type (env->prog);bool acq; +size_t reg_size =0 ;/* skip for now, but return error when we find this in fixup_kfunc_call */ if (!insn->imm)@@ -7015,8 +7017,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } -for (i =0 ; i <CALLER_SAVED_REGS ; i++) -mark_reg_not_init (env, regs, caller_saved[i]); +/* reset REG_0 */ +mark_reg_not_init (env, regs,BPF_REG_0 );/* Check return type */ t =btf_type_skip_modifiers (desc_btf, func_proto->type,NULL );@@ -7026,6 +7028,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return -EINVAL ; } + nargs =btf_type_vlen (func_proto); + args =btf_params (func_proto); +if (btf_type_is_scalar (t)) {mark_reg_unknown (env, regs,BPF_REG_0 );mark_btf_func_reg_size (env,BPF_REG_0 , t->size);@@ -7033,24 +7038,54 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ptr_type =btf_type_skip_modifiers (desc_btf, t->type, &ptr_type_id);if (!btf_type_is_struct (ptr_type)) { - ptr_type_name =btf_name_by_offset (desc_btf, - ptr_type->name_off); -verbose (env,"kernel function %s returns pointer type %s %s is not supported\n" , - func_name,btf_type_str (ptr_type), - ptr_type_name); -return -EINVAL ; +/* if we have an array, we must have a const argument named "__sz" */ +for (i =0 ; i < nargs; i++) { +u32 regno = i +BPF_REG_1 ; +struct bpf_reg_state *reg = ®s[regno]; + +/* look for any const scalar parameter of name "__sz" */ +if (!check_reg_arg (env, regno,SRC_OP ) && +tnum_is_const (regs[regno].var_off) && +btf_is_kfunc_arg_mem_size (desc_btf, &args[i], reg)) + reg_size = regs[regno].var_off.value; + } + +if (!reg_size) { + ptr_type_name =btf_name_by_offset (desc_btf, + ptr_type->name_off); +verbose (env, +"kernel function %s returns pointer type %s %s is not supported\n" , + func_name, +btf_type_str (ptr_type), + ptr_type_name); +return -EINVAL ; + } + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_MEM ; + regs[BPF_REG_0 ].mem_size = reg_size; + +/* in case of tracing, only allow write access to +* BPF_MODIFY_RETURN programs +*/ + if (prog_type == BPF_PROG_TYPE_TRACING &&
Quoted 8 lines
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 7073
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ env->prog->expected_attach_type != BPF_MODIFY_RETURN) + regs[BPF_REG_0 ].type |=MEM_RDONLY ;
MOD_RETrestriction looks artificial.
We can distinguish readonly vs writeablePTR_TO_MEMbased on another naming convention.
Currentlyarg_name__szapplies to the previous argument. Matching suffix made sense there.
Oh, I missed the point of the "__sz". I did not realize it was supposed to be a suffix.
Quoted 4 lines
Reusing the same suffix matching for a different purpose could be confusing. For this use case we may reserve a full argument name.
Like "rdonly_buf_size" and "rdwr_buf_size" ?
I like the idea but I have 2 problems here:
1. I do not really want to have 2 separate kfuncs for read only and write operations
2. How can I restrict the write operation to fmod_ret?
For 1, my guess is that the read-only operation will not be used unless we solve 2.
For 2, the rationale is that I think tracing functions are not supposed to change the behavior. This was said on the thread about priorities for BPF programs. And it somehow makes sense that fentry should be used for tracing only. OTOH, fmod_ret is clearly affecting the behavior of the program, so I see it more "natural" that it can change the context too.
Cheers,
Benjamin
Re: [RFC bpf-next v4 2/7] bpf/verifier: allow kfunc to return an allocated mem
Quoted 204 lines · Benjamin Tissoires
Quoted 179 lines · Alexei Starovoitov
Quoted 62 lines · Benjamin Tissoires
When a kfunc is not returning a pointer to a struct but to a plain type, check if one of the arguments is called
__szand is a const from the caller, and use this as the size of the allocated memory.For tracing programs, we consider the provided memory to be read only unless the program is
BPF_MODIFY_RETURN.Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4
--- include/linux/btf.h | 6 ++++ kernel/bpf/btf.c | 31 ++++++++++++++++---- kernel/bpf/verifier.c | 66 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 20 deletions(-)include/linux/btf.h +6
-0@@ -332,6 +332,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)return (struct btf_param *)(t +1 ); } +struct bpf_reg_state ; + +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg); +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;kernel/bpf/btf.c +4
-4@@ -5851,9 +5851,9 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,return true; } -static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) {int len, sfx_len =sizeof ("__sz" ) -1 ;const struct btf_type *t;@@ -5976,7 +5976,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_btf = reg->btf; reg_ref_id = reg->btf_id;/* Ensure only one argument is referenced -* PTR_TO_BTF_ID, check_func_arg_reg_off relies +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off relies * on only one referenced register being allowed * for kfuncs. */ kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 6018
Quoted 8 lines
kernel/bpf/btf.c +4
-1@@ -6012,7 +6012,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,u32 type_size;if (is_kfunc) { -bool arg_mem_size = i +1 < nargs &&is_kfunc_arg_mem_size (btf, &args[i +1 ], ®s[regno +1 ]); +bool arg_mem_size = i +1 < nargs && +btf_is_kfunc_arg_mem_size (btf, + &args[i +1 ], + ®s[regno +1 ]);bpf allows ~100 chars. No need to break the line so much.
Quoted 104 lines
kernel/bpf/btf.c +18
-0/* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed @@ -6039,6 +6042,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, i++;continue ; } + +if (rel && reg->ref_obj_id) { +/* Ensure only one argument is referenced +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off +* relies on only one referenced register being allowed +* for kfuncs. +*/ +if (ref_obj_id) { +bpf_log (log, +"verifier internal error: more than one arg with ref_obj_id R%d %u %u\n" , + regno, + reg->ref_obj_id, + ref_obj_id); +return -EFAULT ; + } + ref_regno = regno; + ref_obj_id = reg->ref_obj_id; + } } resolve_ret =btf_resolve_size (btf, ref_t, &type_size);kernel/bpf/verifier.c +38
-8@@ -6974,7 +6974,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,int err, insn_idx = *insn_idx_p;const struct btf_param *args;struct btf *desc_btf; +enum bpf_prog_type prog_type =resolve_prog_type (env->prog);bool acq; +size_t reg_size =0 ;/* skip for now, but return error when we find this in fixup_kfunc_call */ if (!insn->imm)@@ -7015,8 +7017,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } -for (i =0 ; i <CALLER_SAVED_REGS ; i++) -mark_reg_not_init (env, regs, caller_saved[i]); +/* reset REG_0 */ +mark_reg_not_init (env, regs,BPF_REG_0 );/* Check return type */ t =btf_type_skip_modifiers (desc_btf, func_proto->type,NULL );@@ -7026,6 +7028,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return -EINVAL ; } + nargs =btf_type_vlen (func_proto); + args =btf_params (func_proto); +if (btf_type_is_scalar (t)) {mark_reg_unknown (env, regs,BPF_REG_0 );mark_btf_func_reg_size (env,BPF_REG_0 , t->size);@@ -7033,24 +7038,54 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ptr_type =btf_type_skip_modifiers (desc_btf, t->type, &ptr_type_id);if (!btf_type_is_struct (ptr_type)) { - ptr_type_name =btf_name_by_offset (desc_btf, - ptr_type->name_off); -verbose (env,"kernel function %s returns pointer type %s %s is not supported\n" , - func_name,btf_type_str (ptr_type), - ptr_type_name); -return -EINVAL ; +/* if we have an array, we must have a const argument named "__sz" */ +for (i =0 ; i < nargs; i++) { +u32 regno = i +BPF_REG_1 ; +struct bpf_reg_state *reg = ®s[regno]; + +/* look for any const scalar parameter of name "__sz" */ +if (!check_reg_arg (env, regno,SRC_OP ) && +tnum_is_const (regs[regno].var_off) && +btf_is_kfunc_arg_mem_size (desc_btf, &args[i], reg)) + reg_size = regs[regno].var_off.value; + } + +if (!reg_size) { + ptr_type_name =btf_name_by_offset (desc_btf, + ptr_type->name_off); +verbose (env, +"kernel function %s returns pointer type %s %s is not supported\n" , + func_name, +btf_type_str (ptr_type), + ptr_type_name); +return -EINVAL ; + } + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_MEM ; + regs[BPF_REG_0 ].mem_size = reg_size; + +/* in case of tracing, only allow write access to +* BPF_MODIFY_RETURN programs +*/ + if (prog_type == BPF_PROG_TYPE_TRACING &&Quoted 8 lines
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 7073
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ env->prog->expected_attach_type != BPF_MODIFY_RETURN) + regs[BPF_REG_0 ].type |=MEM_RDONLY ;
MOD_RETrestriction looks artificial.
We can distinguish readonly vs writeablePTR_TO_MEMbased on another naming convention.
Currentlyarg_name__szapplies to the previous argument. Matching suffix made sense there.Oh, I missed the point of the "
__sz". I did not realize it was supposed to be a suffix.Quoted 4 lines
Reusing the same suffix matching for a different purpose could be confusing. For this use case we may reserve a full argument name.
Like "rdonly_buf_size" and "rdwr_buf_size" ?I like the idea but I have 2 problems here:
1. I do not really want to have 2 separate kfuncs for read only and write operations
2. How can I restrict the write operation tofmod_ret?
Quoted 8 lines
For 1, my guess is that the read-only operation will not be used unless we solve 2.
For 2, the rationale is that I think tracing functions are not supposed to change the behavior. This was said on the thread about priorities for BPF programs. And it somehow makes sense that fentry should be used for tracing only. OTOH,fmod_retis clearly affecting the behavior of the program, so I see it more "natural" that it can change the context too.
Well, if we say that fentry is rdonly and fmod_ret is rdwr then we probably shouldn't stop at return value.
If bpf prog can access the argument and this argument is an array it should be writable.
We can allow different kfuncs for fentry and fmod_ret too. They can be two tiny wrappers with different arg names (to distinguish rdonly vs rdwr) on top of the single always_inline function that returns a buffer.
Re: [RFC bpf-next v4 2/7] bpf/verifier: allow kfunc to return an allocated mem
Quoted 216 lines · Alexei Starovoitov
Quoted 204 lines · Benjamin Tissoires
Quoted 179 lines · Alexei Starovoitov
Quoted 62 lines · Benjamin Tissoires
When a kfunc is not returning a pointer to a struct but to a plain type, check if one of the arguments is called
__szand is a const from the caller, and use this as the size of the allocated memory.For tracing programs, we consider the provided memory to be read only unless the program is
BPF_MODIFY_RETURN.Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4
--- include/linux/btf.h | 6 ++++ kernel/bpf/btf.c | 31 ++++++++++++++++---- kernel/bpf/verifier.c | 66 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 20 deletions(-)include/linux/btf.h +6
-0@@ -332,6 +332,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)return (struct btf_param *)(t +1 ); } +struct bpf_reg_state ; + +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg); +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;kernel/bpf/btf.c +4
-4@@ -5851,9 +5851,9 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,return true; } -static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +bool btf_is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) {int len, sfx_len =sizeof ("__sz" ) -1 ;const struct btf_type *t;@@ -5976,7 +5976,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_btf = reg->btf; reg_ref_id = reg->btf_id;/* Ensure only one argument is referenced -* PTR_TO_BTF_ID, check_func_arg_reg_off relies +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off relies * on only one referenced register being allowed * for kfuncs. */ kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 6018
Quoted 8 lines
kernel/bpf/btf.c +4
-1@@ -6012,7 +6012,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,u32 type_size;if (is_kfunc) { -bool arg_mem_size = i +1 < nargs &&is_kfunc_arg_mem_size (btf, &args[i +1 ], ®s[regno +1 ]); +bool arg_mem_size = i +1 < nargs && +btf_is_kfunc_arg_mem_size (btf, + &args[i +1 ], + ®s[regno +1 ]);bpf allows ~100 chars. No need to break the line so much.
Quoted 104 lines
kernel/bpf/btf.c +18
-0/* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed @@ -6039,6 +6042,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, i++;continue ; } + +if (rel && reg->ref_obj_id) { +/* Ensure only one argument is referenced +* PTR_TO_BTF_ID or PTR_TO_MEM, check_func_arg_reg_off +* relies on only one referenced register being allowed +* for kfuncs. +*/ +if (ref_obj_id) { +bpf_log (log, +"verifier internal error: more than one arg with ref_obj_id R%d %u %u\n" , + regno, + reg->ref_obj_id, + ref_obj_id); +return -EFAULT ; + } + ref_regno = regno; + ref_obj_id = reg->ref_obj_id; + } } resolve_ret =btf_resolve_size (btf, ref_t, &type_size);kernel/bpf/verifier.c +38
-8@@ -6974,7 +6974,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,int err, insn_idx = *insn_idx_p;const struct btf_param *args;struct btf *desc_btf; +enum bpf_prog_type prog_type =resolve_prog_type (env->prog);bool acq; +size_t reg_size =0 ;/* skip for now, but return error when we find this in fixup_kfunc_call */ if (!insn->imm)@@ -7015,8 +7017,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } -for (i =0 ; i <CALLER_SAVED_REGS ; i++) -mark_reg_not_init (env, regs, caller_saved[i]); +/* reset REG_0 */ +mark_reg_not_init (env, regs,BPF_REG_0 );/* Check return type */ t =btf_type_skip_modifiers (desc_btf, func_proto->type,NULL );@@ -7026,6 +7028,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return -EINVAL ; } + nargs =btf_type_vlen (func_proto); + args =btf_params (func_proto); +if (btf_type_is_scalar (t)) {mark_reg_unknown (env, regs,BPF_REG_0 );mark_btf_func_reg_size (env,BPF_REG_0 , t->size);@@ -7033,24 +7038,54 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ptr_type =btf_type_skip_modifiers (desc_btf, t->type, &ptr_type_id);if (!btf_type_is_struct (ptr_type)) { - ptr_type_name =btf_name_by_offset (desc_btf, - ptr_type->name_off); -verbose (env,"kernel function %s returns pointer type %s %s is not supported\n" , - func_name,btf_type_str (ptr_type), - ptr_type_name); -return -EINVAL ; +/* if we have an array, we must have a const argument named "__sz" */ +for (i =0 ; i < nargs; i++) { +u32 regno = i +BPF_REG_1 ; +struct bpf_reg_state *reg = ®s[regno]; + +/* look for any const scalar parameter of name "__sz" */ +if (!check_reg_arg (env, regno,SRC_OP ) && +tnum_is_const (regs[regno].var_off) && +btf_is_kfunc_arg_mem_size (desc_btf, &args[i], reg)) + reg_size = regs[regno].var_off.value; + } + +if (!reg_size) { + ptr_type_name =btf_name_by_offset (desc_btf, + ptr_type->name_off); +verbose (env, +"kernel function %s returns pointer type %s %s is not supported\n" , + func_name, +btf_type_str (ptr_type), + ptr_type_name); +return -EINVAL ; + } + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_MEM ; + regs[BPF_REG_0 ].mem_size = reg_size; + +/* in case of tracing, only allow write access to +* BPF_MODIFY_RETURN programs +*/ + if (prog_type == BPF_PROG_TYPE_TRACING &&Quoted 8 lines
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 7073
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ env->prog->expected_attach_type != BPF_MODIFY_RETURN) + regs[BPF_REG_0 ].type |=MEM_RDONLY ;
MOD_RETrestriction looks artificial.
We can distinguish readonly vs writeablePTR_TO_MEMbased on another naming convention.
Currentlyarg_name__szapplies to the previous argument. Matching suffix made sense there.Oh, I missed the point of the "
__sz". I did not realize it was supposed to be a suffix.Quoted 4 lines
Reusing the same suffix matching for a different purpose could be confusing. For this use case we may reserve a full argument name.
Like "rdonly_buf_size" and "rdwr_buf_size" ?I like the idea but I have 2 problems here:
1. I do not really want to have 2 separate kfuncs for read only and write operations
2. How can I restrict the write operation tofmod_ret?Quoted 8 lines
For 1, my guess is that the read-only operation will not be used unless we solve 2.
For 2, the rationale is that I think tracing functions are not supposed to change the behavior. This was said on the thread about priorities for BPF programs. And it somehow makes sense that fentry should be used for tracing only. OTOH,fmod_retis clearly affecting the behavior of the program, so I see it more "natural" that it can change the context too.
Quoted 2 lines
Well, if we say that fentry is rdonly and
fmod_retis rdwr then we probably shouldn't stop at return value.
Yeah, it makes sense, but it will be a slightly bigger effort.
Quoted 3 lines
If bpf prog can access the argument and this argument is an array it should be writable.
We can allow different kfuncs for fentry andfmod_rettoo.
Good idea. No idea if this will be easy to implement though :)
But after the prototype I describe in 0/7 I don't really need to enforce if read or write is based on fentry/fmod_ret.
So I think let's not over engineer this, and stick to your initial suggestion of the naming system which will be largely enough for my use case.
Quoted 4 lines
They can be two tiny wrappers with different arg names (to distinguish rdonly vs rdwr) on top of the single
always_inlinefunction that returns a buffer.
Cheers,
Benjamin
RFC v4 3/7 error-inject: add new type that carries if the function is non sleepable
In reply to Benjamin TissoiresWhen using error-injection function through bpf to change the return code, we need to know if the function is sleepable or not.
Currently the code assumes that all error-inject functions are sleepable, except for a few selected of them, hardcoded in kernel/bpf/verifier.c
Add a new flag to error-inject so we can code that information where the function is declared.
---
new in v4:
- another approach would be to define a new kfunc_set, and register it with btf. But in that case, what program type would we use? BPF_PROG_TYPE_UNSPEC?
- also note that maybe we should consider all of the functions non-sleepable and only mark some as sleepable. IMO it makes more sense to be more restrictive by default.
--- include/asm-generic/error-injection.h | 1 + kernel/bpf/verifier.c | 10 ++++++++-- lib/error-inject.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-)
include/asm-generic/error-injection.h +1 -0
@@ -9,6 +9,7 @@ enum {EI_ETYPE_ERRNO ,/* Return -ERRNO if failure */ EI_ETYPE_ERRNO_NULL ,/* Return -ERRNO or NULL if failure */ EI_ETYPE_TRUE ,/* Return true if failure */ + EI_ETYPE_NS_ERRNO, /* Return -ERRNO if failure and tag the function as non-sleepable */ }; struct error_injection_entry {
kernel/bpf/verifier.c +8 -2
@@ -14085,6 +14085,11 @@ static int check_non_sleepable_error_inject(u32 btf_id)return btf_id_set_contains (&btf_non_sleepable_error_inject, btf_id); } +static int is_non_sleepable_error_inject (unsigned long addr) +{ +return get_injectable_error_type (addr) ==EI_ETYPE_NS_ERRNO ; +} + int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog,
@@ -14281,8 +14286,9 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,/* fentry/fexit/fmod_ret progs can be sleepable only if they are * attached to ALLOW_ERROR_INJECTION and are not in denylist. */ -if (!check_non_sleepable_error_inject (btf_id) && -within_error_injection_list (addr)) +if (within_error_injection_list (addr) && + !check_non_sleepable_error_inject (btf_id) && + !is_non_sleepable_error_inject (addr)) ret =0 ;break ;case BPF_PROG_TYPE_LSM :
lib/error-inject.c +2 -0
@@ -183,6 +183,8 @@ static const char *error_type_string(int etype)return "ERRNO_NULL" ;case EI_ETYPE_TRUE :return "TRUE" ; +case EI_ETYPE_NS_ERRNO : +return "NS_ERRNO" ;default :return "(unknown)" ; }
Signature · 1 line
2.35.1
Re: [RFC bpf-next v4 3/7] error-inject: add new type that carries if the function is non sleepable
Quoted 13 lines · Benjamin Tissoires
When using error-injection function through bpf to change the return code, we need to know if the function is sleepable or not.
Currently the code assumes that all error-inject functions are sleepable, except for a few selected of them, hardcoded in
kernel/bpf/verifier.cAdd a new flag to error-inject so we can code that information where the function is declared.
Signed-off-by Benjamin Tissoires <[email protected]>---
Quoted 7 lines
new in v4:
- another approach would be to define a newkfunc_set, and register it with btf. But in that case, what program type would we use?BPF_PROG_TYPE_UNSPEC?
- also note that maybe we should consider all of the functions non-sleepable and only mark some as sleepable. IMO it makes more sense to be more restrictive by default.
I think the approach in this patch is fine.
We didn't have issues with check_non_sleepable_error_inject() so far, so I wouldn't start refactoring it.
Quoted 24 lines
--- include/asm-generic/error-injection.h | 1 + kernel/bpf/verifier.c | 10 ++++++++-- lib/error-inject.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-)include/asm-generic/error-injection.h +1
-0@@ -9,6 +9,7 @@ enum {EI_ETYPE_ERRNO ,/* Return -ERRNO if failure */ EI_ETYPE_ERRNO_NULL ,/* Return -ERRNO or NULL if failure */ EI_ETYPE_TRUE ,/* Return true if failure */ + EI_ETYPE_NS_ERRNO, /* Return -ERRNO if failure and tag the function as non-sleepable */ };struct
error_injection_entry{kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_non_sleepable_error_inject(u32 btf_id) · line 14090
Quoted 6 lines
kernel/bpf/verifier.c +3
-0@@ -14085,6 +14085,11 @@ static int check_non_sleepable_error_inject(u32 btf_id)return btf_id_set_contains (&btf_non_sleepable_error_inject, btf_id); } +static int is_non_sleepable_error_inject (unsigned long addr) +{ +return get_injectable_error_type (addr) ==EI_ETYPE_NS_ERRNO ;
It's a linear search. Probably ok. But would be good to double check that we're not calling it a lot.
Quoted 33 lines
kernel/bpf/verifier.c +5
-2+} + int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog,@@ -14281,8 +14286,9 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,/* fentry/fexit/fmod_ret progs can be sleepable only if they are * attached to ALLOW_ERROR_INJECTION and are not in denylist. */ -if (!check_non_sleepable_error_inject (btf_id) && -within_error_injection_list (addr)) +if (within_error_injection_list (addr) && + !check_non_sleepable_error_inject (btf_id) && + !is_non_sleepable_error_inject (addr)) ret =0 ;break ;case BPF_PROG_TYPE_LSM :lib/error-inject.c +2
-0@@ -183,6 +183,8 @@ static const char *error_type_string(int etype)return "ERRNO_NULL" ;case EI_ETYPE_TRUE :return "TRUE" ; +case EI_ETYPE_NS_ERRNO : +return "NS_ERRNO" ;default :return "(unknown)" ; }Signature · 1 line
2.35.1
Re: [RFC bpf-next v4 3/7] error-inject: add new type that carries if the function is non sleepable
Quoted 23 lines · Alexei Starovoitov
Quoted 13 lines · Benjamin Tissoires
When using error-injection function through bpf to change the return code, we need to know if the function is sleepable or not.
Currently the code assumes that all error-inject functions are sleepable, except for a few selected of them, hardcoded in
kernel/bpf/verifier.cAdd a new flag to error-inject so we can code that information where the function is declared.
Signed-off-by Benjamin Tissoires <[email protected]>---
Quoted 7 lines
new in v4:
- another approach would be to define a newkfunc_set, and register it with btf. But in that case, what program type would we use?BPF_PROG_TYPE_UNSPEC?
- also note that maybe we should consider all of the functions non-sleepable and only mark some as sleepable. IMO it makes more sense to be more restrictive by default.
Quoted 3 lines
I think the approach in this patch is fine.
We didn't have issues withcheck_non_sleepable_error_inject()so far, so I wouldn't start refactoring it.
OK... though I can't help but thinking that adding a new error-inject.h enum value is going to be bad, because it's an API change, and users might not expect NS_ERRNO.
OTOH, if we had a new kfunc_set, we keep the existing error-inject API in place with all the variants and we just teach the verifier that the function is non sleepable.
Quoted 28 lines
Quoted 28 lines
--- include/asm-generic/error-injection.h | 1 + kernel/bpf/verifier.c | 10 ++++++++-- lib/error-inject.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-)include/asm-generic/error-injection.h +1
-0@@ -9,6 +9,7 @@ enum {EI_ETYPE_ERRNO ,/* Return -ERRNO if failure */ EI_ETYPE_ERRNO_NULL ,/* Return -ERRNO or NULL if failure */ EI_ETYPE_TRUE ,/* Return true if failure */ + EI_ETYPE_NS_ERRNO, /* Return -ERRNO if failure and tag the function as non-sleepable */ };struct
error_injection_entry{kernel/bpf/verifier.c +0
-0@@ -14085,6 +14085,11 @@ static int check_non_sleepable_error_inject(u32 btf_id)return btf_id_set_contains (&btf_non_sleepable_error_inject, btf_id); }
Quoted 6 lines
kernel/bpf/verifier.c static int check_non_sleepable_error_inject(u32 btf_id) · line 14090
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+static int is_non_sleepable_error_inject (unsigned long addr) +{ +return get_injectable_error_type (addr) ==EI_ETYPE_NS_ERRNO ;It's a linear search. Probably ok. But would be good to double check that we're not calling it a lot.
IIUC, the kfunc_set approach would solve that, no?
Cheers,
Benjamin
Quoted 35 lines
Quoted 34 lines
kernel/bpf/verifier.c +5
-2+} + int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog,@@ -14281,8 +14286,9 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,/* fentry/fexit/fmod_ret progs can be sleepable only if they are * attached to ALLOW_ERROR_INJECTION and are not in denylist. */ -if (!check_non_sleepable_error_inject (btf_id) && -within_error_injection_list (addr)) +if (within_error_injection_list (addr) && + !check_non_sleepable_error_inject (btf_id) && + !is_non_sleepable_error_inject (addr)) ret =0 ;break ;case BPF_PROG_TYPE_LSM :lib/error-inject.c +2
-1@@ -183,6 +183,8 @@ static const char *error_type_string(int etype)return "ERRNO_NULL" ;case EI_ETYPE_TRUE :return "TRUE" ; +case EI_ETYPE_NS_ERRNO : +return "NS_ERRNO" ;default :return "(unknown)" ; } --2.35.1
Re: [RFC bpf-next v4 3/7] error-inject: add new type that carries if the function is non sleepable
Quoted 26 lines · Benjamin Tissoires
Quoted 23 lines · Alexei Starovoitov
Quoted 20 lines · Benjamin Tissoires
When using error-injection function through bpf to change the return code, we need to know if the function is sleepable or not.
Currently the code assumes that all error-inject functions are sleepable, except for a few selected of them, hardcoded in
kernel/bpf/verifier.cAdd a new flag to error-inject so we can code that information where the function is declared.
Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4:
- another approach would be to define a newkfunc_set, and register it with btf. But in that case, what program type would we use?BPF_PROG_TYPE_UNSPEC?
- also note that maybe we should consider all of the functions non-sleepable and only mark some as sleepable. IMO it makes more sense to be more restrictive by default.
Quoted 7 lines
Quoted 3 lines
I think the approach in this patch is fine.
We didn't have issues withcheck_non_sleepable_error_inject()so far, so I wouldn't start refactoring it.OK... though I can't help but thinking that adding a new
error-inject.henum value is going to be bad, because it's an API change, and users might not expectNS_ERRNO.
Not sure about api concern. This is the kernel internal tag. bpf progs are not aware of them. The functions can change from sleepable to non-sleepable too. allow_error_inject can be removed. And so on.
Quoted 3 lines
OTOH, if we had a new
kfunc_set, we keep the existing error-inject API in place with all the variants and we just teach the verifier that the function is non sleepable.
...
Quoted 1 line
IIUC, the
kfunc_setapproach would solve that, no?
Makes sense. Let's figure out an extensible kfunc_set approach that is not centralized in verifier.c
Re: [RFC bpf-next v4 3/7] error-inject: add new type that carries if the function is non sleepable
Quoted 42 lines · Alexei Starovoitov
Quoted 26 lines · Benjamin Tissoires
Quoted 23 lines · Alexei Starovoitov
Quoted 20 lines · Benjamin Tissoires
When using error-injection function through bpf to change the return code, we need to know if the function is sleepable or not.
Currently the code assumes that all error-inject functions are sleepable, except for a few selected of them, hardcoded in
kernel/bpf/verifier.cAdd a new flag to error-inject so we can code that information where the function is declared.
Signed-off-by Benjamin Tissoires <[email protected]>---
new in v4:
- another approach would be to define a newkfunc_set, and register it with btf. But in that case, what program type would we use?BPF_PROG_TYPE_UNSPEC?
- also note that maybe we should consider all of the functions non-sleepable and only mark some as sleepable. IMO it makes more sense to be more restrictive by default.Quoted 7 lines
Quoted 3 lines
I think the approach in this patch is fine.
We didn't have issues withcheck_non_sleepable_error_inject()so far, so I wouldn't start refactoring it.OK... though I can't help but thinking that adding a new
error-inject.henum value is going to be bad, because it's an API change, and users might not expectNS_ERRNO.Not sure about api concern. This is the kernel internal tag. bpf progs are not aware of them. The functions can change from sleepable to non-sleepable too.
allow_error_injectcan be removed. And so on.
Quoted 9 lines
Quoted 3 lines
OTOH, if we had a new
kfunc_set, we keep the existing error-inject API in place with all the variants and we just teach the verifier that the function is non sleepable....
Quoted 1 line
IIUC, the
kfunc_setapproach would solve that, no?Makes sense. Let's figure out an extensible
kfunc_setapproach that is not centralized inverifier.c
OK, I'll work on this in v5.
But I need to rethink the whole sleepable/non-sleepable definitions for my use case, because I have now a clear separation between not sleepable context (in fentry/fexit/fmod_ret) and sleepable context (in SEC("syscall")), so maybe the whole thing is not really required.
Cheers,
Benjamin
RFC v4 4/7 btf: Add a new kfunc set which allows to mark a function to be sleepable
In reply to Benjamin TissoiresThis allows to declare a kfunc as sleepable and prevents its use in a non sleepable program.
---
new in v4
--- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
include/linux/btf.h +2 -0
@@ -17,6 +17,7 @@ enum btf_kfunc_type {BTF_KFUNC_TYPE_ACQUIRE ,BTF_KFUNC_TYPE_RELEASE ,BTF_KFUNC_TYPE_RET_NULL , +BTF_KFUNC_TYPE_SLEEPABLE , BTF_KFUNC_TYPE_MAX, };
@@ -35,6 +36,7 @@ struct btf_kfunc_id_set {struct btf_id_set *acquire_set;struct btf_id_set *release_set;struct btf_id_set *ret_null_set; +struct btf_id_set *sleepable_set; };struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX ]; };
kernel/bpf/btf.c +13 -3
@@ -5889,7 +5889,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,const struct btf_type *t, *ref_t;const struct btf_param *args;int ref_regno =0 , ret; -bool rel = false; +bool rel = false, sleepable = false; t =btf_type_by_id (btf, func_id);if (!t || !btf_type_is_func (t)) {
@@ -5915,10 +5915,13 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,return -EINVAL ; } -/* Only kfunc can be release func */ -if (is_kfunc) +/* Only kfunc can be release or sleepable func */ +if (is_kfunc) { rel =btf_kfunc_id_set_contains (btf,resolve_prog_type (env->prog),BTF_KFUNC_TYPE_RELEASE , func_id); + sleepable =btf_kfunc_id_set_contains (btf,resolve_prog_type (env->prog), +BTF_KFUNC_TYPE_SLEEPABLE , func_id); + }/* check that BTF function arguments match actual types that the * verifier sees. */
@@ -6092,6 +6095,13 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, func_name);return -EINVAL ; } + +if (sleepable && !env->prog->aux->sleepable) { +bpf_log (log,"kernel function %s is sleepable but the program is not\n" , + func_name); +return -EINVAL ; + } +/* returns argument register number > 0 in case of reference release kfunc */ return rel ? ref_regno :0 ; }
Signature · 1 line
2.35.1
Re: [RFC bpf-next v4 4/7] btf: Add a new kfunc set which allows to mark a function to be sleepable
Quoted 4 lines · Benjamin Tissoires
This allows to declare a kfunc as sleepable and prevents its use in a non sleepable program.
Signed-off-by Benjamin Tissoires <[email protected]>
makes sense.
Quoted 13 lines
include/linux/btf.h +2
-0@@ -17,6 +17,7 @@ enum btf_kfunc_type {BTF_KFUNC_TYPE_ACQUIRE ,BTF_KFUNC_TYPE_RELEASE ,BTF_KFUNC_TYPE_RET_NULL , +BTF_KFUNC_TYPE_SLEEPABLE , BTF_KFUNC_TYPE_MAX, };@@ -35,6 +36,7 @@ struct btf_kfunc_id_set {struct btf_id_set *acquire_set;struct btf_id_set *release_set;struct btf_id_set *ret_null_set; +struct btf_id_set *sleepable_set;
RFC v4 5/7 HID: initial BPF new way implementation
In reply to Benjamin TissoiresDeclare an entry point that can use fmod_ret BPF programs, and also an API to access and change the incoming data.
---
new-ish in v4:
- far from complete, but gives an overview of what we can do now.
--- drivers/hid/hid-core.c | 115 ++++++++++++++++++++++++++++++++++++++++ include/linux/hid_bpf.h | 29 ++++++++++ 2 files changed, 144 insertions(+) create mode 100644 include/linux/hid_bpf.h
drivers/hid/hid-core.c +115 -0
@@ -32,6 +32,9 @@#include <linux/hiddev.h> #include <linux/hid-debug.h> #include <linux/hidraw.h> +#include <linux/btf.h> +#include <linux/btf_ids.h> +#include <linux/hid_bpf.h> #include "hid-ids.h"
@@ -2008,6 +2011,99 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, }EXPORT_SYMBOL_GPL (hid_report_raw_event); +struct hid_bpf_ctx_kern { +struct hid_device *hdev; +struct hid_bpf_ctx ctx; +u8 *data; +size_t size; +}; + +__weak int hid_bpf_device_event (struct hid_bpf_ctx *ctx,s64 type) +{ +return 0 ; +} +ALLOW_ERROR_INJECTION (hid_bpf_device_event,NS_ERRNO ); + +noinline __u8 * +hid_bpf_kfunc_get_data (struct hid_bpf_ctx *ctx,unsigned int offset,const size_t __sz) +{ +struct hid_bpf_ctx_kern *ctx_kern; + +if (!ctx) +return NULL ; + + ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); + +return ctx_kern->data; +} + +noinline void +hid_bpf_kfunc_data_release (__u8 *data) +{ +} + +noinline int +hid_bpf_kfunc_hw_request (struct hid_bpf_ctx *ctx) +{ +if (!ctx) +return -EINVAL ; + +pr_err ("%s test ctx->bus: %04x %s:%d" , __func__, ctx->bus, __FILE__, __LINE__); + +return 0 ; +} + +/* +* The following set contains all functions we agree BPF programs +* can use. +*/ +BTF_SET_START (hid_bpf_kfunc_ids) +BTF_ID (func ,hid_bpf_kfunc_get_data ) +BTF_ID (func, hid_bpf_kfunc_data_release) +BTF_ID (func, hid_bpf_kfunc_hw_request) +BTF_SET_END (hid_bpf_kfunc_ids) + +/* +* The following set contains all functions to provide a kernel +* resource to the BPF program. +* We need to add a counterpart release function. +*/ +BTF_SET_START (hid_bpf_kfunc_acquire_ids) +BTF_ID (func, hid_bpf_kfunc_get_data) +BTF_SET_END (hid_bpf_kfunc_acquire_ids) + +/* +* The following set is the release counterpart of the previous +* function set. +*/ +BTF_SET_START (hid_bpf_kfunc_release_ids) +BTF_ID (func, hid_bpf_kfunc_data_release) +BTF_SET_END (hid_bpf_kfunc_release_ids) + +/* +* The following set tells which functions are sleepable. +*/ +BTF_SET_START (hid_bpf_kfunc_sleepable_ids) +BTF_ID (func, hid_bpf_kfunc_hw_request) +BTF_SET_END (hid_bpf_kfunc_sleepable_ids) + +static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { + .owner =THIS_MODULE , + .check_set = &hid_bpf_kfunc_ids, + .acquire_set = &hid_bpf_kfunc_acquire_ids, + .release_set = &hid_bpf_kfunc_release_ids, + .ret_null_set = &hid_bpf_kfunc_acquire_ids,/* duplicated to force BPF programs to +* check the validity of the returned pointer +* in acquire function +*/ + .sleepable_set = &hid_bpf_kfunc_sleepable_ids, +}; + +static int __init hid_bpf_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &hid_bpf_kfunc_set); +} +/** * hid_input_report - report data from lower layer (usb, bt...) *
@@ -2025,6 +2121,17 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int istruct hid_driver *hdrv;struct hid_report *report;int ret =0 ; +struct hid_bpf_ctx_kern ctx_kern = { + .hdev = hid, + .ctx = { + .bus = hid->bus, + .group = hid->group, + .vendor = hid->vendor, + .product = hid->product, + }, + .data = data, + .size = size, + };if (!hid)return -ENODEV ;
@@ -2039,6 +2146,10 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i report_enum = hid->report_enum + type; hdrv = hid->driver; + ret =hid_bpf_device_event (&ctx_kern.ctx, type); +if (ret) + goto unlock; +if (!size) {dbg_hid ("empty report\n" ); ret =-1 ;
@@ -2914,6 +3025,10 @@ static int __init hid_init(void)hid_debug_init (); + ret =hid_bpf_init (); +if (ret) +pr_err ("%s error in bpf_init: %d %s:%d" , __func__, ret, __FILE__, __LINE__); +return 0 ; err_bus:bus_unregister (&hid_bus_type);
include/linux/hid_bpf.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */ + +#ifndef __HID_BPF_H +#define __HID_BPF_H + +/* +* The following is the HID BPF API. +* +* It should be treated as UAPI, so extra care is required +* when making change to this file. +*/ + +struct hid_bpf_ctx { +__u16 bus;/* BUS ID */ +__u16 group;/* Report group */ +__u32 vendor;/* Vendor ID */ +__u32 product;/* Product ID */ +__u32 version;/* HID version */ +}; + +/* Following functions are tracepoints that BPF programs can attach to */ +int hid_bpf_device_event (struct hid_bpf_ctx *ctx,__s64 type); + +/* Following functions are kfunc that we export to BPF programs */ +__u8 *hid_bpf_kfunc_get_data (struct hid_bpf_ctx *ctx,unsigned int offset,const size_t __sz); +void hid_bpf_kfunc_data_release (__u8 *data); +int hid_bpf_kfunc_hw_request (struct hid_bpf_ctx *ctx); + +#endif /* __HID_BPF_H */
Signature · 1 line
2.35.1
Re: [RFC bpf-next v4 5/7] HID: initial BPF new way implementation
Quoted 48 lines · Benjamin Tissoires
Declare an entry point that can use
fmod_retBPF programs, and also an API to access and change the incoming data.Signed-off-by Benjamin Tissoires <[email protected]>---
new-ish in v4:
- far from complete, but gives an overview of what we can do now.--- drivers/hid/hid-core.c | 115 ++++++++++++++++++++++++++++++++++++++++ include/linux/hid_bpf.h | 29 ++++++++++ 2 files changed, 144 insertions(+) create mode 100644 include/linux/hid_bpf.hdrivers/hid/hid-core.c +18
-0@@ -32,6 +32,9 @@#include <linux/hiddev.h> #include <linux/hid-debug.h> #include <linux/hidraw.h> +#include <linux/btf.h> +#include <linux/btf_ids.h> +#include <linux/hid_bpf.h> #include "hid-ids.h" @@ -2008,6 +2011,99 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, }EXPORT_SYMBOL_GPL (hid_report_raw_event); +struct hid_bpf_ctx_kern { +struct hid_device *hdev; +struct hid_bpf_ctx ctx; +u8 *data; +size_t size; +}; + +__weak int hid_bpf_device_event (struct hid_bpf_ctx *ctx,s64 type) +{ +return 0 ; +} +ALLOW_ERROR_INJECTION (hid_bpf_device_event,NS_ERRNO ); + +noinline __u8 * +hid_bpf_kfunc_get_data (struct hid_bpf_ctx *ctx,unsigned int offset,const size_t __sz)
drivers/hid/hid-core.c int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, · line 2038
Quoted 10 lines
drivers/hid/hid-core.c +10
-0+{ +struct hid_bpf_ctx_kern *ctx_kern; + +if (!ctx) +return NULL ; + + ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); + +return ctx_kern->data; +}
It probably needs to check that "rdonly_buf_sz" or "__sz" constant passed by the program is less than what was allocated in hid_bpf_ctx_kern->data. Right?
drivers/hid/hid-core.c int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, · line 2043
Quoted 5 lines
drivers/hid/hid-core.c +5
-0+ +noinline void +hid_bpf_kfunc_data_release (__u8 *data) +{ +}
Not clear what release() is for.hid_bpf_kfunc_get_data() will return PTR_TO_MEM.
There is no need to release it.
Quoted 71 lines
drivers/hid/hid-core.c +64
-0+ +noinline int +hid_bpf_kfunc_hw_request (struct hid_bpf_ctx *ctx) +{ +if (!ctx) +return -EINVAL ; + +pr_err ("%s test ctx->bus: %04x %s:%d" , __func__, ctx->bus, __FILE__, __LINE__); + +return 0 ; +} + +/* +* The following set contains all functions we agree BPF programs +* can use. +*/ +BTF_SET_START (hid_bpf_kfunc_ids) +BTF_ID (func ,hid_bpf_kfunc_get_data ) +BTF_ID (func, hid_bpf_kfunc_data_release) +BTF_ID (func, hid_bpf_kfunc_hw_request) +BTF_SET_END (hid_bpf_kfunc_ids) + +/* +* The following set contains all functions to provide a kernel +* resource to the BPF program. +* We need to add a counterpart release function. +*/ +BTF_SET_START (hid_bpf_kfunc_acquire_ids) +BTF_ID (func, hid_bpf_kfunc_get_data) +BTF_SET_END (hid_bpf_kfunc_acquire_ids) + +/* +* The following set is the release counterpart of the previous +* function set. +*/ +BTF_SET_START (hid_bpf_kfunc_release_ids) +BTF_ID (func, hid_bpf_kfunc_data_release) +BTF_SET_END (hid_bpf_kfunc_release_ids) + +/* +* The following set tells which functions are sleepable. +*/ +BTF_SET_START (hid_bpf_kfunc_sleepable_ids) +BTF_ID (func, hid_bpf_kfunc_hw_request) +BTF_SET_END (hid_bpf_kfunc_sleepable_ids) + +static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { + .owner =THIS_MODULE , + .check_set = &hid_bpf_kfunc_ids, + .acquire_set = &hid_bpf_kfunc_acquire_ids, + .release_set = &hid_bpf_kfunc_release_ids, + .ret_null_set = &hid_bpf_kfunc_acquire_ids,/* duplicated to force BPF programs to +* check the validity of the returned pointer +* in acquire function +*/ + .sleepable_set = &hid_bpf_kfunc_sleepable_ids, +}; + +static int __init hid_bpf_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &hid_bpf_kfunc_set); +} +/** * hid_input_report - report data from lower layer (usb, bt...) * @@ -2025,6 +2121,17 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int istruct hid_driver *hdrv;struct hid_report *report;int ret =0 ; +struct hid_bpf_ctx_kern ctx_kern = {
drivers/hid/hid-core.c int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i · line 2134
Quoted 10 lines
drivers/hid/hid-core.c +10
-0+ .hdev = hid, + .ctx = { + .bus = hid->bus, + .group = hid->group, + .vendor = hid->vendor, + .product = hid->product, + }, + .data = data, + .size = size, + };
I'm not sure why hid_bpf_ctx_kern is needed.
Just to scope all args into one struct?
Quoted 4 lines
drivers/hid/hid-core.c +4
-0+/* +* The following is the HID BPF API. +* +* It should be treated as UAPI, so extra care is required
drivers/hid/hid-core.c int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i
Quoted 10 lines
drivers/hid/hid-core.c +10
-0+* when making change to this file. +*/ + +struct hid_bpf_ctx { +__u16 bus;/* BUS ID */ +__u16 group;/* Report group */ +__u32 vendor;/* Vendor ID */ +__u32 product;/* Product ID */ +__u32 version;/* HID version */ +};
Your choice of course,
but not clear why kernel has to populate it with explicit:
+ .bus = hid->bus,
+ .group = hid->group,
+ .vendor = hid->vendor,
+ .product = hid->product,
and waste cpu cycles on that while bpf program can read all these fields on demand when necessary.
RFC v4 6/7 samples/bpf: add new hid_mouse example
In reply to Benjamin TissoiresEverything should be available in the selftest part of the tree, but providing an example without uhid and hidraw will be more easy to follow for users.
---
changes in v4:
- dropped the not-yet-implemented rdesc_fixup
- use the new API
changes in v3:
- use the new hid_get_data API
- add a comment for the report descriptor fixup to explain what is done
changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
--- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 23 +++++++ samples/bpf/hid_mouse.bpf.c | 59 ++++++++++++++++ samples/bpf/hid_mouse.c | 131 ++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 samples/bpf/hid_mouse.bpf.c create mode 100644 samples/bpf/hid_mouse.c
samples/bpf/.gitignore +1 -0
@@ -2,6 +2,7 @@ cpustat fds_example hbm +hid_mouse ibumad lathist lwt_len_hist
samples/bpf/Makefile +23 -0
@@ -60,6 +60,8 @@ tprogs-y += xdp_redirect_map tprogs-y += xdp_redirect tprogs-y += xdp_monitor +tprogs-y += hid_mouse + # Libbpf dependencies LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
@@ -125,6 +127,8 @@ xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE) xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE) xdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE) +hid_mouse-objs := hid_mouse.o + # Tell kbuild to always build the programs always-y := $(tprogs-y) always-y += sockex1_kern.o
@@ -349,6 +353,8 @@ $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h $(obj)/hbm.o: $(src)/hbm.h $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h +$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h + # Override includes for xdp_sample_user.o because $(srctree)/usr/include in # TPROGS_CFLAGS causes conflicts XDP_SAMPLE_CFLAGS += -Wall -O2 \
@@ -434,6 +440,23 @@ $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL) @echo " BPF GEN-SKEL" $(@:.skel.h=) $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@ +# Generate BPF skeletons for non XDP progs +OTHER_BPF_SKELS := hid_mouse.skel.h + +hid_mouse.skel.h-deps := hid_mouse.bpf.o + +OTHER_BPF_SRCS_LINKED := $(patsubst %.skel.h,%.bpf.c, $(OTHER_BPF_SKELS)) +OTHER_BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(OTHER_BPF_SRCS_LINKED)) +OTHER_BPF_SKELS_LINKED := $(addprefix $(obj)/,$(OTHER_BPF_SKELS)) + +$(OTHER_BPF_SKELS_LINKED): $(OTHER_BPF_OBJS_LINKED) $(BPFTOOL) + @echo " BPF GEN-OBJ " $(@:.skel.h=) + $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) + @echo " BPF GEN-SKEL" $(@:.skel.h=) + $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=_lskel)) > $@ +# $(call msg,GEN-SKEL,$@) +# $(Q)$(BPFTOOL) gen skeleton $< > $@ + # asm/sysreg.h - inline assembly used by it is incompatible with llvm. # But, there is no easy way to fix it, so just exclude it since it is # useless for BPF samples.
samples/bpf/hid_mouse.bpf.c new file +59 -0
59 lines not on this page · raw message
samples/bpf/hid_mouse.c new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.35.1
RFC v4 7/7 selftests/bpf: add tests for the HID-bpf initial implementation
In reply to Benjamin TissoiresThe test is pretty basic:
- create a virtual uhid device that no userspace will like (to not mess up the running system)
- attach a BPF prog to it
- open the matching hidraw node
- inject one event and check:
* that the BPF program can do something on the event stream
* can modify the event stream
---
changes in v4:
- manually retrieve the hidraw node from the sysfs (we can't get it for free from BPF)
- use the new API
changes in v3:
- squashed "hid: rely on uhid event to know if a test device is ready" into this one
- add selftests bpf VM config changes
- s/hidraw_ino/hidraw_number/
changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
--- tools/testing/selftests/bpf/config | 3 + tools/testing/selftests/bpf/prog_tests/hid.c | 482 +++++++++++++++++++ tools/testing/selftests/bpf/progs/hid.c | 32 ++ 3 files changed, 517 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/hid.c create mode 100644 tools/testing/selftests/bpf/progs/hid.c
tools/testing/selftests/bpf/config +3 -0
@@ -53,3 +53,6 @@ CONFIG_NF_DEFRAG_IPV4=y CONFIG_NF_DEFRAG_IPV6=y CONFIG_NF_CONNTRACK=y CONFIG_USERFAULTFD=y +CONFIG_HID=y +CONFIG_HIDRAW=y +CONFIG_UHID=y
tools/testing/selftests/bpf/prog_tests/hid.c new file +482 -0
482 lines not on this page · raw message
tools/testing/selftests/bpf/progs/hid.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Red hat */ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +extern __u8 *hid_bpf_kfunc_get_data (struct hid_bpf_ctx *ctx, +unsigned int offset, +const size_t __sz) __ksym; +extern void hid_bpf_kfunc_data_release(__u8 *data) __ksym; + +__u64 callback_check =52 ; +__u64 callback2_check =52 ; + +SEC("fmod_ret/hid_bpf_device_event") +int BPF_PROG (hid_first_event ,struct hid_bpf_ctx *hid_ctx,__s64 type) +{ +__u8 *rw_data =hid_bpf_kfunc_get_data (hid_ctx,0 /* offset */ ,3 /* size */ ); + +if (!rw_data) +return 0 ;/* EPERM check */ + + callback_check = rw_data[1 ]; + + rw_data[2 ] = rw_data[1 ] +5 ; + +hid_bpf_kfunc_data_release (rw_data); + +return 0 ; +}
Signature · 1 line
2.35.1
Quoted 5 lines · Benjamin Tissoires
Hi,
so after the reviews from v3, and some discussion with Alexei, I am back with a new version of HID-BPF.
Quoted 7 lines
This version is not complete (thus the RFC), but I'd like to share it now to get initial feedback, in case I am too far from the actual goal.
FTR, the goal is to provide some changes in the core verifier/btf so that we can plug in HID-BPF independently from BPF core. This way we can extend it without having to care about bpf-next.
Overall looks great. imo much cleaner, simpler and more extensible than the earlier versions.
The bpf core extensions are nicely contained and HID side can be worked on in parallel.
Quoted 3 lines
The things I am not entirely sure are:
- do we need only fentry/fexit/fmod_ret BPF program types or should programs that modify the data stream use a different kind?
Probably not. I'll reply in patch 2.
Quoted 7 lines
- patch 3/7 is probably not the correct approach (see comments in the patch itself)
We are missing quite a few bits here:
- selftests for patches 1 to 4
- add the ability to attach a program to a struct device, and run that program only for that struct device
yes. That is still to be figured out.
Quoted 7 lines
- when running through
bpf_prog_test_run_opts, how can we ensure we are talking to the correct device? (I have a feeling this is linked to the previous point)
- how can we reconnect the device when a report descriptor fixup BPF program is loaded (would it make sense to allow some notifications on when a BPF program is attached/detached to a device, and which function have been traced?)
Not sure I follow. What kind of notification do you have in mind? To user space?
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Benjamin Tissoires
Hi,
so after the reviews from v3, and some discussion with Alexei, I am back with a new version of HID-BPF.
This version is not complete (thus the RFC), but I'd like to share it now to get initial feedback, in case I am too far from the actual goal.
Quoted 8 lines
Quoted 3 lines
FTR, the goal is to provide some changes in the core verifier/btf so that we can plug in HID-BPF independently from BPF core. This way we can extend it without having to care about bpf-next.
Overall looks great. imo much cleaner, simpler and more extensible than the earlier versions.
The bpf core extensions are nicely contained and HID side can be worked on in parallel.
\o/
Quoted 10 lines
Quoted 4 lines
The things I am not entirely sure are:
- do we need only fentry/fexit/fmod_ret BPF program types or should programs that modify the data stream use a different kind?Probably not. I'll reply in patch 2.
Quoted 3 lines
- patch 3/7 is probably not the correct approach (see comments in the patch itself)
Quoted 6 lines
Quoted 4 lines
We are missing quite a few bits here:
- selftests for patches 1 to 4
- add the ability to attach a program to a struct device, and run that program only for that struct deviceyes. That is still to be figured out.
I spent some time on that, and I don't think it makes a lot of sense to use the current trampoline approach if we want to keep on using fentry/fexit...
- the trampoline is pretty nice, but it adds instructions before calling the actual function, meaning that adding a check on struct device will be quite hard to do ()we have no idea where the struct device is in the arguments) and will take more space on the trampoline itself
- there is a limit on how many functions can be attached to a trampoline (38 IIRC), and we probably will explode that number quickly enough when we get more BPF programs to support HID devices.
So my chain of thoughts from yesterday was the following (completely untested of course):
- instead of writing a new BPF API that might move in the future while things are settling, I can actually simply load a tracer BPF program from HID that monitors the BPF programs that are attached to a given function
- I can also add a new API (a kfunc likely) that "registers" a given BPF program (through its fd) to a given HID device
- when a device sends data, it hits hid_bpf_device_event() which will have a default BPF program (loaded by the kernel) that dispatches the registered BPF programs based on the HID device.
This would solve the 2 issues above IMO, except that the kfunc to register a HID BPF program will suddenly be not standard.
Quoted 9 lines
Quoted 8 lines
- when running through
bpf_prog_test_run_opts, how can we ensure we are talking to the correct device? (I have a feeling this is linked to the previous point)
- how can we reconnect the device when a report descriptor fixup BPF program is loaded (would it make sense to allow some notifications on when a BPF program is attached/detached to a device, and which function have been traced?)
Quoted 3 lines
Not sure I follow. What kind of notification do you have in mind? To user space?
No, this is in-kernel notifications.
What I want to do, is when I load a BPF program that changes the HID report descriptor, hid-core detects that and reconnects the attached device.
But after a couple of days of thinking, and with the above approach where HID would preload a BPF program, I should be able to achieve that with the "register BPF through a HID kfunc call". When I see that we are attaching a HID report descriptor fixup to a given HID device, I can then reconnect the matching device.
It would certainly be cleaner to have a general "notify me when a tracer is attached to this particular function", but we can hide that right now with a preloaded BPF program :)
Cheers,
Benjamin
Quoted 42 lines · Benjamin Tissoires
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Benjamin Tissoires
Hi,
so after the reviews from v3, and some discussion with Alexei, I am back with a new version of HID-BPF.
This version is not complete (thus the RFC), but I'd like to share it now to get initial feedback, in case I am too far from the actual goal.
Quoted 8 lines
Quoted 3 lines
FTR, the goal is to provide some changes in the core verifier/btf so that we can plug in HID-BPF independently from BPF core. This way we can extend it without having to care about bpf-next.
Overall looks great. imo much cleaner, simpler and more extensible than the earlier versions.
The bpf core extensions are nicely contained and HID side can be worked on in parallel.\o/
Quoted 10 lines
Quoted 4 lines
The things I am not entirely sure are:
- do we need only fentry/fexit/fmod_ret BPF program types or should programs that modify the data stream use a different kind?Probably not. I'll reply in patch 2.
Quoted 3 lines
- patch 3/7 is probably not the correct approach (see comments in the patch itself)
Quoted 6 lines
Quoted 4 lines
We are missing quite a few bits here:
- selftests for patches 1 to 4
- add the ability to attach a program to a struct device, and run that program only for that struct deviceyes. That is still to be figured out.
Quoted 11 lines
I spent some time on that, and I don't think it makes a lot of sense to use the current trampoline approach if we want to keep on using fentry/fexit...
- the trampoline is pretty nice, but it adds instructions before calling the actual function, meaning that adding a check on struct device will be quite hard to do ()we have no idea where the struct device is in the arguments) and will take more space on the trampoline itself
- there is a limit on how many functions can be attached to a trampoline (38 IIRC), and we probably will explode that number quickly enough when we get more BPF programs to support HID devices.
Ohh. This is an obsolete limitation.
38 was the number since we used half page optimization for bpf trampoline.
It's gone now. We can easily lift this max.
Quoted 12 lines
So my chain of thoughts from yesterday was the following (completely untested of course):
- instead of writing a new BPF API that might move in the future while things are settling, I can actually simply load a tracer BPF program from HID that monitors the BPF programs that are attached to a given function
- I can also add a new API (a kfunc likely) that "registers" a given BPF program (through its fd) to a given HID device
- when a device sends data, it hitshid_bpf_device_event()which will have a default BPF program (loaded by the kernel) that dispatches the registered BPF programs based on the HID device.
Quoted 2 lines
This would solve the 2 issues above IMO, except that the kfunc to register a HID BPF program will suddenly be not standard.
Could you add more details to these ideas?
I thought you wanted bpf prog writers to be independent of each other. They would tell some framework HID device id/pcie id that they need and the rest would be automatic.
Maybe we can achieve that by adding another layer before libbpf that would accept (bpf_prog, hid_id) tuple and insert
if (hid->id != hid_id) return -E..;
as the first insn into bpf_prog before loading into the kernel. All such progs will be kfunc-s attached to the same hook. The kernel will execute them sequentially.
The framework will provide demux by auto-inserting this 'if'. This 'if (hid)' could be a part of sample code too.
We can simply ask prog writers to follow this style.
Another idea would be to do something like libxdp. Attach a "dispatcher" bpf prog to the kfunc hook and use some library to attach hid-specific progs as "freplace" kind of programs. It's a more involved solution.
Another option is to use tail_calls.
If hid_id is a relatively small number. The "dispatcher" bpf prog can do bpf_tail_call(prog_array, hid_id)
while hid specific progs insert itself into prog_array instead of attaching to kfunc.
Quoted 24 lines
Quoted 9 lines
Quoted 8 lines
- when running through
bpf_prog_test_run_opts, how can we ensure we are talking to the correct device? (I have a feeling this is linked to the previous point)
- how can we reconnect the device when a report descriptor fixup BPF program is loaded (would it make sense to allow some notifications on when a BPF program is attached/detached to a device, and which function have been traced?)Quoted 3 lines
Not sure I follow. What kind of notification do you have in mind? To user space?
No, this is in-kernel notifications.
What I want to do, is when I load a BPF program that changes the HID report descriptor, hid-core detects that and reconnects the attached device.But after a couple of days of thinking, and with the above approach where HID would preload a BPF program, I should be able to achieve that with the "register BPF through a HID kfunc call". When I see that we are attaching a HID report descriptor fixup to a given HID device, I can then reconnect the matching device.
Quoted 3 lines
It would certainly be cleaner to have a general "notify me when a tracer is attached to this particular function", but we can hide that right now with a preloaded BPF program :)
There are few lsm hooks in bpf core. It probably wwill be eird for hid core to hook into them. We can add a few tracepoints at attach functions if that helps.
Quoted 57 lines · Alexei Starovoitov
Quoted 42 lines · Benjamin Tissoires
Quoted 11 lines · Alexei Starovoitov
Quoted 9 lines · Benjamin Tissoires
Hi,
so after the reviews from v3, and some discussion with Alexei, I am back with a new version of HID-BPF.
This version is not complete (thus the RFC), but I'd like to share it now to get initial feedback, in case I am too far from the actual goal.
Quoted 8 lines
Quoted 3 lines
FTR, the goal is to provide some changes in the core verifier/btf so that we can plug in HID-BPF independently from BPF core. This way we can extend it without having to care about bpf-next.
Overall looks great. imo much cleaner, simpler and more extensible than the earlier versions.
The bpf core extensions are nicely contained and HID side can be worked on in parallel.\o/
Quoted 10 lines
Quoted 4 lines
The things I am not entirely sure are:
- do we need only fentry/fexit/fmod_ret BPF program types or should programs that modify the data stream use a different kind?Probably not. I'll reply in patch 2.
Quoted 3 lines
- patch 3/7 is probably not the correct approach (see comments in the patch itself)
Quoted 6 lines
Quoted 4 lines
We are missing quite a few bits here:
- selftests for patches 1 to 4
- add the ability to attach a program to a struct device, and run that program only for that struct deviceyes. That is still to be figured out.
Quoted 11 lines
I spent some time on that, and I don't think it makes a lot of sense to use the current trampoline approach if we want to keep on using fentry/fexit...
- the trampoline is pretty nice, but it adds instructions before calling the actual function, meaning that adding a check on struct device will be quite hard to do ()we have no idea where the struct device is in the arguments) and will take more space on the trampoline itself
- there is a limit on how many functions can be attached to a trampoline (38 IIRC), and we probably will explode that number quickly enough when we get more BPF programs to support HID devices.
Quoted 4 lines
Ohh. This is an obsolete limitation.
38 was the number since we used half page optimization for bpf trampoline.
It's gone now. We can easily lift this max.
Oh, interesting.
I have been working this week with that limitation in place, and I just managed to get something working yesterday night. See below.
Quoted 13 lines
Quoted 13 lines
So my chain of thoughts from yesterday was the following (completely untested of course):
- instead of writing a new BPF API that might move in the future while things are settling, I can actually simply load a tracer BPF program from HID that monitors the BPF programs that are attached to a given function
- I can also add a new API (a kfunc likely) that "registers" a given BPF program (through its fd) to a given HID device
- when a device sends data, it hitshid_bpf_device_event()which will have a default BPF program (loaded by the kernel) that dispatches the registered BPF programs based on the HID device.
Quoted 4 lines
Quoted 2 lines
This would solve the 2 issues above IMO, except that the kfunc to register a HID BPF program will suddenly be not standard.
Could you add more details to these ideas?
That's basically your last suggestion below.
Quoted 10 lines
I thought you wanted bpf prog writers to be independent of each other. They would tell some framework HID device id/pcie id that they need and the rest would be automatic.
Maybe we can achieve that by adding another layer before libbpf that would accept (bpf_prog,hid_id) tuple and insert
if (hid->id!=hid_id) return -E..;
as the first insn intobpf_progbefore loading into the kernel. All such progs will be kfunc-s attached to the same hook. The kernel will execute them sequentially.
The framework will provide demux by auto-inserting this 'if'.
That's an interesting approach. Much less convoluted than the one I have right now. The only downside I see is that every program will be called for every event, which might not be ideal (though the event stream from HID is way smaller than network).
Quoted 2 lines
This 'if (hid)' could be a part of sample code too. We can simply ask prog writers to follow this style.
Not a big fan of that...
Quoted 5 lines
Another idea would be to do something like libxdp. Attach a "dispatcher" bpf prog to the kfunc hook and use some library to attach hid-specific progs as "freplace" kind of programs. It's a more involved solution.
freplace might not work for me because I want to attach more than one program to a given HID device. Not so sure BPF_PROG_TYPE_REPLACE will do something enough there.
Quoted 6 lines
Another option is to use
tail_calls.
Ifhid_idis a relatively small number. The "dispatcher" bpf prog can dobpf_tail_call(prog_array, hid_id)
while hid specific progs insert itself intoprog_arrayinstead of attaching to kfunc.
This is roughly what I have now:
- hid-core is not aware of BPF except for a few __weak ALLOW_ERROR_INJECTION hooks (dispatch_hid_bpf_device_event for example)
- I have a separate hid-bpf module that attaches BPF traces to these hooks and calls a "dispatch" kfunc within hid-bpf
- the dispatch function then do a succession of BPF calls to programs attached to it by using bpf_tail_call(prog_array, hid_id)
- for the clients, they define one or more SEC("fmod_ret/hid_bpf_device_event"). That __weak hook is declared in the kernel by hid-bpf but is never called, it's just an API declaration
- then clients call in a SEC("syscall") hid_bpf_attach_prog(ctx->prog_fd, ctx->hid_id, ctx->flags);
- hid_bpf_attach_prog is a kfunc that takes a ref on the struct bpf_prog*, and stores that program in the correct struct bpf_map *for the given attached_btf_id (hid_bpf_device_event in our case)
And that's about it.
I still need to handle automatic release of the bpf prog when there is no userspace open fd on it unless it's pinned but I think this should be working fine.
I also probably need to pin some SEC("syscall") (hid_bpf_attach_prog and hid_bpf_dettach_prog) so users don't have to write them down and can just use the ones provided by the kernel.
The nice thing is that I can define my own API for the attach call without dealing with bpf core. I can thus add a priority flag that is relevant here because the data coming through the bpf program can be modified.
The other thing is that now, I don't care which function we are in to decide if a RET_PTR_MEM is read only or not. I can deal with that by either playing with the flags or even replacing entirely the dispatch trace prog from userspace if I want to access the raw events.
However, the downsides are:
- I need to also define kfuncs for BPF_PROG_TYPE_SYSCALL (I don't think It'll be a big issue)
- The only way I could store the bpf_prog into the map was to hack around the map ops, because the fd of the map in the skel is not available while doing a SEC("syscall") from a different process.
Also, I wonder if we should not have some way to namespace kfuncs. Ideally, I would like to prevent the usage of those kfuncs outside of some helpers that I define in HID so I don't have to worry too much about other trace programs fuzzing and segfaulting the kernel.
Quoted 25 lines
Quoted 25 lines
Quoted 10 lines
Quoted 9 lines
- when running through
bpf_prog_test_run_opts, how can we ensure we are talking to the correct device? (I have a feeling this is linked to the previous point)
- how can we reconnect the device when a report descriptor fixup BPF program is loaded (would it make sense to allow some notifications on when a BPF program is attached/detached to a device, and which function have been traced?)Quoted 3 lines
Not sure I follow. What kind of notification do you have in mind? To user space?
No, this is in-kernel notifications.
What I want to do, is when I load a BPF program that changes the HID report descriptor, hid-core detects that and reconnects the attached device.But after a couple of days of thinking, and with the above approach where HID would preload a BPF program, I should be able to achieve that with the "register BPF through a HID kfunc call". When I see that we are attaching a HID report descriptor fixup to a given HID device, I can then reconnect the matching device.
Quoted 8 lines
Quoted 3 lines
It would certainly be cleaner to have a general "notify me when a tracer is attached to this particular function", but we can hide that right now with a preloaded BPF program :)
There are few lsm hooks in bpf core. It probably wwill be eird for hid core to hook into them. We can add a few tracepoints at attach functions if that helps.
With the above, I actually don't need those notifications. Users are calling my own "syscall" when attaching their BPF prog, and they are also not required to attach the program to the actual BPF trace (through BPF_PROG_ATTACH), so this request is now gone :)
Cheers,
Benjamin
Quoted 1 line · Benjamin Tissoires
[...]
Quoted 39 lines
This is roughly what I have now:
- hid-core is not aware of BPF except for a few
__weakALLOW_ERROR_INJECTIONhooks (dispatch_hid_bpf_device_eventfor example)
- I have a separate hid-bpf module that attaches BPF traces to these hooks and calls a "dispatch" kfunc within hid-bpf
- the dispatch function then do a succession of BPF calls to programs attached to it by usingbpf_tail_call(prog_array, hid_id)- for the clients, they define one or more
SEC("fmod_ret/hid_bpf_device_event"). That__weakhook is declared in the kernel by hid-bpf but is never called, it's just an API declaration
- then clients call in aSEC("syscall")hid_bpf_attach_prog(ctx->prog_fd, ctx->hid_id, ctx->flags);
-hid_bpf_attach_progis a kfunc that takes a ref on the struct bpf_prog*, and stores that program in the correct structbpf_map*for the givenattached_btf_id(hid_bpf_device_eventin our case)And that's about it.
I still need to handle automatic release of the bpf prog when there is no userspace open fd on it unless it's pinned but I think this should be working fine.I also probably need to pin some
SEC("syscall")(hid_bpf_attach_progandhid_bpf_dettach_prog) so users don't have to write them down and can just use the ones provided by the kernel.The nice thing is that I can define my own API for the attach call without dealing with bpf core. I can thus add a priority flag that is relevant here because the data coming through the bpf program can be modified.
The other thing is that now, I don't care which function we are in to decide if a
RET_PTR_MEMis read only or not. I can deal with that by either playing with the flags or even replacing entirely the dispatch trace prog from userspace if I want to access the raw events.
Quoted 6 lines
However, the downsides are:
- I need to also define kfuncs forBPF_PROG_TYPE_SYSCALL(I don't think It'll be a big issue)
- The only way I could store thebpf_proginto the map was to hack around the map ops, because the fd of the map in the skel is not available while doing aSEC("syscall")from a different process.
Update on this side: I realized that I could use the syscall BPF_MAP_GET_FD_BY_ID instead to get an fd for the current task. However, I've been bitten quite hard today because I was using bpf_map_get() instead of bpf_map_get_with_uref(), and so every time I closed the fd in the syscall the map was cleared...
But now I would like to have more than one program of a type per hid device, meaning that I can not have only one bpf_map of type BPF_MAP_TYPE_PROG_ARRAY.
I have explored BPF_MAP_TYPE_HASH_OF_MAPS, but we can not have BPF_MAP_TYPE_PROG_ARRAY as inner maps with the current code. And I'd need 2 levels of nesting (which is not authorized today):
- hid_jmp_table (key: HID id)
- array of different program type per HID device (key: HID_BPF_PROG_TYPE)
- BPF_MAP_TYPE_PROG_ARRAY with the actual progs (key: int)
The other solution would be to be able to create a map when needed, store it in struct hid_device, and then call bpf_tail_call on this map. The problem is that I need a way to teach the verifier that the struct bpf_map pointer I have in the context is a true bpf_map...
Any input would be appreciated :)
Cheers,
Benjamin
Quoted 6 lines
Also, I wonder if we should not have some way to namespace kfuncs. Ideally, I would like to prevent the usage of those kfuncs outside of some helpers that I define in HID so I don't have to worry too much about other trace programs fuzzing and segfaulting the kernel.
[...]
Quoted 66 lines · Benjamin Tissoires
Quoted 1 line · Benjamin Tissoires
[...]
Quoted 39 lines
This is roughly what I have now:
- hid-core is not aware of BPF except for a few
__weakALLOW_ERROR_INJECTIONhooks (dispatch_hid_bpf_device_eventfor example)
- I have a separate hid-bpf module that attaches BPF traces to these hooks and calls a "dispatch" kfunc within hid-bpf
- the dispatch function then do a succession of BPF calls to programs attached to it by usingbpf_tail_call(prog_array, hid_id)- for the clients, they define one or more
SEC("fmod_ret/hid_bpf_device_event"). That__weakhook is declared in the kernel by hid-bpf but is never called, it's just an API declaration
- then clients call in aSEC("syscall")hid_bpf_attach_prog(ctx->prog_fd, ctx->hid_id, ctx->flags);
-hid_bpf_attach_progis a kfunc that takes a ref on the struct bpf_prog*, and stores that program in the correct structbpf_map*for the givenattached_btf_id(hid_bpf_device_eventin our case)And that's about it.
I still need to handle automatic release of the bpf prog when there is no userspace open fd on it unless it's pinned but I think this should be working fine.I also probably need to pin some
SEC("syscall")(hid_bpf_attach_progandhid_bpf_dettach_prog) so users don't have to write them down and can just use the ones provided by the kernel.The nice thing is that I can define my own API for the attach call without dealing with bpf core. I can thus add a priority flag that is relevant here because the data coming through the bpf program can be modified.
The other thing is that now, I don't care which function we are in to decide if a
RET_PTR_MEMis read only or not. I can deal with that by either playing with the flags or even replacing entirely the dispatch trace prog from userspace if I want to access the raw events.Quoted 6 lines
However, the downsides are:
- I need to also define kfuncs forBPF_PROG_TYPE_SYSCALL(I don't think It'll be a big issue)
- The only way I could store thebpf_proginto the map was to hack around the map ops, because the fd of the map in the skel is not available while doing aSEC("syscall")from a different process.Update on this side: I realized that I could use the syscall
BPF_MAP_GET_FD_BY_IDinstead to get an fd for the current task. However, I've been bitten quite hard today because I was usingbpf_map_get()instead ofbpf_map_get_with_uref(), and so every time I closed the fd in the syscall the map was cleared...But now I would like to have more than one program of a type per hid device, meaning that I can not have only one
bpf_mapof typeBPF_MAP_TYPE_PROG_ARRAY.
I have exploredBPF_MAP_TYPE_HASH_OF_MAPS, but we can not haveBPF_MAP_TYPE_PROG_ARRAYas inner maps with the current code. And I'd need 2 levels of nesting (which is not authorized today):
-hid_jmp_table(key: HID id)
- array of different program type per HID device (key:HID_BPF_PROG_TYPE)
-BPF_MAP_TYPE_PROG_ARRAYwith the actual progs (key: int)
Quoted 4 lines
The other solution would be to be able to create a map when needed, store it in struct
hid_device, and then callbpf_tail_callon this map. The problem is that I need a way to teach the verifier that the structbpf_mappointer I have in the context is a truebpf_map...
We have kptr feature now.
So bpf progs can store pointers to specific kernel data structures inside map values.
Storing 'struct bpf_map *' in a map value would be something :) Circular dependency issues to address. Maybe it's doable.
Would hash based prog_array work ?
Then the key can be an arbitrary combination.
There is fd_htab logic. It's used for map-in-map.
We can tweak it to store progs in a hash map.
Quoted 79 lines · Alexei Starovoitov
Quoted 66 lines · Benjamin Tissoires
Quoted 1 line · Benjamin Tissoires
[...]
Quoted 39 lines
This is roughly what I have now:
- hid-core is not aware of BPF except for a few
__weakALLOW_ERROR_INJECTIONhooks (dispatch_hid_bpf_device_eventfor example)
- I have a separate hid-bpf module that attaches BPF traces to these hooks and calls a "dispatch" kfunc within hid-bpf
- the dispatch function then do a succession of BPF calls to programs attached to it by usingbpf_tail_call(prog_array, hid_id)- for the clients, they define one or more
SEC("fmod_ret/hid_bpf_device_event"). That__weakhook is declared in the kernel by hid-bpf but is never called, it's just an API declaration
- then clients call in aSEC("syscall")hid_bpf_attach_prog(ctx->prog_fd, ctx->hid_id, ctx->flags);
-hid_bpf_attach_progis a kfunc that takes a ref on the struct bpf_prog*, and stores that program in the correct structbpf_map*for the givenattached_btf_id(hid_bpf_device_eventin our case)And that's about it.
I still need to handle automatic release of the bpf prog when there is no userspace open fd on it unless it's pinned but I think this should be working fine.I also probably need to pin some
SEC("syscall")(hid_bpf_attach_progandhid_bpf_dettach_prog) so users don't have to write them down and can just use the ones provided by the kernel.The nice thing is that I can define my own API for the attach call without dealing with bpf core. I can thus add a priority flag that is relevant here because the data coming through the bpf program can be modified.
The other thing is that now, I don't care which function we are in to decide if a
RET_PTR_MEMis read only or not. I can deal with that by either playing with the flags or even replacing entirely the dispatch trace prog from userspace if I want to access the raw events.Quoted 6 lines
However, the downsides are:
- I need to also define kfuncs forBPF_PROG_TYPE_SYSCALL(I don't think It'll be a big issue)
- The only way I could store thebpf_proginto the map was to hack around the map ops, because the fd of the map in the skel is not available while doing aSEC("syscall")from a different process.Update on this side: I realized that I could use the syscall
BPF_MAP_GET_FD_BY_IDinstead to get an fd for the current task. However, I've been bitten quite hard today because I was usingbpf_map_get()instead ofbpf_map_get_with_uref(), and so every time I closed the fd in the syscall the map was cleared...But now I would like to have more than one program of a type per hid device, meaning that I can not have only one
bpf_mapof typeBPF_MAP_TYPE_PROG_ARRAY.
I have exploredBPF_MAP_TYPE_HASH_OF_MAPS, but we can not haveBPF_MAP_TYPE_PROG_ARRAYas inner maps with the current code. And I'd need 2 levels of nesting (which is not authorized today):
-hid_jmp_table(key: HID id)
- array of different program type per HID device (key:HID_BPF_PROG_TYPE)
-BPF_MAP_TYPE_PROG_ARRAYwith the actual progs (key: int)Quoted 4 lines
The other solution would be to be able to create a map when needed, store it in struct
hid_device, and then callbpf_tail_callon this map. The problem is that I need a way to teach the verifier that the structbpf_mappointer I have in the context is a truebpf_map...We have kptr feature now.
So bpf progs can store pointers to specific kernel data structures inside map values.
Storing 'structbpf_map*' in a map value would be something :) Circular dependency issues to address. Maybe it's doable.
Quoted 5 lines
Would hash based
prog_arraywork ?
Then the key can be an arbitrary combination.
There isfd_htablogic. It's used for map-in-map.
We can tweak it to store progs in a hash map.
In the end, I am just using a global prog_array map, and handling the indexes myself. It is probably not the cleaner and the most reusable, but it allows me at least to move forward.
FWIW, I should be able to send v5 next week. I am almost done reimplementing everything I had in v3, and I am now fighting with hid.ko as a module (should be solved soon enough).
Cheers,
Benjamin
Quoted 5 lines · Benjamin Tissoires
Also, I wonder if we should not have some way to namespace kfuncs. Ideally, I would like to prevent the usage of those kfuncs outside of some helpers that I define in HID so I don't have to worry too much about other trace programs fuzzing and segfaulting the kernel.
That would be a great feature to have. Other folks expressed the same interest. Just grouping them by prog type is not flexible enough.
It feels kfuncs could be scoped by (prog_type, attach_btf_id or attach_hook) pair. What are your thoughts?
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · Benjamin Tissoires
Also, I wonder if we should not have some way to namespace kfuncs. Ideally, I would like to prevent the usage of those kfuncs outside of some helpers that I define in HID so I don't have to worry too much about other trace programs fuzzing and segfaulting the kernel.
Quoted 5 lines
That would be a great feature to have. Other folks expressed the same interest. Just grouping them by prog type is not flexible enough.
It feels kfuncs could be scoped by (prog_type,attach_btf_idorattach_hook) pair. What are your thoughts?
Scoping by attach_btf_id is very appealing to me (attach_hook less TBH):
I have internal functions I do not want normal users to use, and also it would also restrict who can call what in the more general case.
However, I don't think I'll put that effort in v5. It is a nice to have feature IMO, but not really required ATM.
Cheers,
Benjamin
Quoted 17 lines · Benjamin Tissoires
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · Benjamin Tissoires
Also, I wonder if we should not have some way to namespace kfuncs. Ideally, I would like to prevent the usage of those kfuncs outside of some helpers that I define in HID so I don't have to worry too much about other trace programs fuzzing and segfaulting the kernel.
Quoted 5 lines
That would be a great feature to have. Other folks expressed the same interest. Just grouping them by prog type is not flexible enough.
It feels kfuncs could be scoped by (prog_type,attach_btf_idorattach_hook) pair. What are your thoughts?
Quoted 6 lines
Scoping by
attach_btf_idis very appealing to me (attach_hookless TBH):
I have internal functions I do not want normal users to use, and also it would also restrict who can call what in the more general case.However, I don't think I'll put that effort in v5. It is a nice to have feature IMO, but not really required ATM.
Great. Looking forward to it.