On Sun, Nov 20, 2022 at 10:37:02AM -0800, Yonghong Song wrote: > Implement bpf_cast_to_kern_ctx() kfunc which does a type cast > of a uapi ctx object to the corresponding kernel ctx. Previously > if users want to access some data available in kctx but not > in uapi ctx, bpf_probe_read_kernel() helper is needed. > The introduction of bpf_cast_to_kern_ctx() allows direct > memory access which makes code simpler and easier to understand. > > Signed-off-by: Yonghong Song <yhs@xxxxxx> > --- > include/linux/btf.h | 5 +++++ > kernel/bpf/btf.c | 25 +++++++++++++++++++++++++ > kernel/bpf/helpers.c | 6 ++++++ > kernel/bpf/verifier.c | 21 +++++++++++++++++++++ > 4 files changed, 57 insertions(+) > > diff --git a/include/linux/btf.h b/include/linux/btf.h > index d38aa4251c28..9ed00077db6e 100644 > --- a/include/linux/btf.h > +++ b/include/linux/btf.h > @@ -487,6 +487,7 @@ const struct btf_member * > btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, > const struct btf_type *t, enum bpf_prog_type prog_type, > int arg); > +int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type); > bool btf_types_are_same(const struct btf *btf1, u32 id1, > const struct btf *btf2, u32 id2); > #else > @@ -531,6 +532,10 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, > { > return NULL; > } > +static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log, > + enum bpf_prog_type prog_type) { > + return -EINVAL; > +} > static inline bool btf_types_are_same(const struct btf *btf1, u32 id1, > const struct btf *btf2, u32 id2) > { > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > index 1c78d4df9e18..3c662b00d54a 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -5603,6 +5603,31 @@ static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, > return kern_ctx_type->type; > } > > +int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type) > +{ > + const struct btf_member *kctx_member; > + const struct btf_type *conv_struct; > + const struct btf_type *kctx_type; > + u32 kctx_type_id; > + > + conv_struct = bpf_ctx_convert.t; > + if (!conv_struct) { > + bpf_log(log, "btf_vmlinux is malformed\n"); > + return -EINVAL; > + } same comments as in v2.