bpf: Prepare btf_ctx_access for non raw_tp use case.
Subsystems (7)
PATCH 1/1 bpf: Prepare btf_ctx_access for non raw_tp use case.
This patch makes a few changes to btf_ctx_access() to prepare it for non raw_tp use case.
btf_ctx_access() only needs the attach_btf_id from prog. Hence, this patch only passes the attach_btf_id instead of passing prog. It allows other use cases when the prog->aux->attach_btf_id may not be a typedef. For example, in the future, a bpf_prog can attach to
"struct tcp_congestion_ops" and its attach_btf_id is
pointing to the btf_id of "struct tcp_congestion_ops".
While at it, allow btf_ctx_access to directly take a BTF_KIND_FUNC_PROTO btf_id. It is to prepare for a later patch that does not need a "typedef" to figure out the func_proto. For example, when attaching a bpf_prog to an ops in "struct tcp_congestion_ops", the func_proto can be found directly by following the members of "struct tcp_congestion_ops".
For the no typedef use case, there is no extra first arg. Hence, this patch only limits the skip arg logic to raw_tp only.
Since a BTF_KIND_FUNC_PROTO type does not have a name (i.e. "(anon)"), an optional name arg is added also. If specified, this name will be used in the bpf_log() message instead of the type's name obtained from btf_id. For example, the function pointer member name of
"struct tcp_congestion_ops" can be used.
--- include/linux/bpf.h | 2 +- kernel/bpf/btf.c | 53 ++++++++++++++++++++++++++-------------- kernel/trace/bpf_trace.c | 3 ++- 3 files changed, 38 insertions(+), 20 deletions(-)
include/linux/bpf.h +1 -1
@@ -766,7 +766,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,const union bpf_attr *kattr, union bpf_attr __user *uattr);bool btf_ctx_access (int off,int size,enum bpf_access_type type, -const struct bpf_prog *prog, +u32 btf_id,const char *name,struct bpf_insn_access_aux *info); int btf_struct_access(struct bpf_verifier_log *log, const struct btf_type *t, int off, int size,
kernel/bpf/btf.c +35 -18
112 lines not on this page · raw message
kernel/trace/bpf_trace.c +2 -1
@@ -1080,7 +1080,8 @@ static bool raw_tp_prog_is_valid_access(int off, int size,return false;if (off % size !=0 )return false; -return btf_ctx_access (off, size, type, prog, info); +return btf_ctx_access (off, size, type, prog->aux->attach_btf_id,NULL , + info); }const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
Signature · 1 line
2.17.1
Quoted 26 lines · Martin KaFai Lau
This patch makes a few changes to
btf_ctx_access()to prepare it for nonraw_tpuse case.
btf_ctx_access()only needs theattach_btf_idfrom prog. Hence, this patch only passes theattach_btf_idinstead of passing prog. It allows other use cases when theprog->aux->attach_btf_idmay not be a typedef. For example, in the future, abpf_progcan attach to
"structtcp_congestion_ops" and itsattach_btf_idis
pointing to thebtf_idof "structtcp_congestion_ops".While at it, allow
btf_ctx_accessto directly take aBTF_KIND_FUNC_PROTObtf_id. It is to prepare for a later patch that does not need a "typedef" to figure out thefunc_proto. For example, when attaching abpf_progto an ops in "structtcp_congestion_ops", thefunc_protocan be found directly by following the members of "structtcp_congestion_ops".For the no typedef use case, there is no extra first arg. Hence, this patch only limits the skip arg logic to
raw_tponly.Since a
BTF_KIND_FUNC_PROTOtype does not have a name (i.e. "(anon)"), an optional name arg is added also. If specified, this name will be used in thebpf_log()message instead of the type's name obtained frombtf_id. For example, the function pointer member name of
"structtcp_congestion_ops" can be used.Signed-off-by Martin KaFai Lau <[email protected]>
...
kernel/bpf/btf.c static bool btf_type_is_func_proto(const struct btf_type *t) · line 352
Quoted 4 lines
kernel/bpf/btf.c +4
-0+static bool btf_type_is_typedef (const struct btf_type *t) +{ +return BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ; +}
yeah. I was to lazy to add it. Thanks for doing this cleanup.
Quoted 5 lines
kernel/bpf/btf.c +0
-1@@ -3459,37 +3464,49 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, } t =btf_type_by_id (btf_vmlinux, btf_id); -if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_TYPEDEF ) {
kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 3473
Quoted 10 lines
kernel/bpf/btf.c +2
-3+if (!t || (!btf_type_is_typedef (t) && !btf_type_is_func_proto (t))) {bpf_log (log,"btf_id is invalid\n" );return false; } tname =__btf_name_by_offset (btf_vmlinux, t->name_off); -if (strncmp (prefix, tname,sizeof (prefix) -1 )) { -bpf_log (log,"btf_id points to wrong type name %s\n" , tname); -return false; +if (btf_type_is_typedef (t)) {
This check cannot be done conditionally.
Otherwise raw_tp bpf prog with partially "invalid" attach_btf_id will successfully load, but will fail to attach to raw_tp in raw_tp_open command.
I think better way is to move this check early into the verifier. That will speed up this function as well that is called multiple times for ever ctx access in the prog.
Thanks!