On 12/6/24 8:24 AM, Maciej Fijalkowski wrote:
I think we can remove the projection_of call from the
bpf_is_prog_ctx_type() such that it honors the exact argument
type written in the kernel source code. Add this particular projection_of
check (renamed to bpf_is_kern_ctx in the diff) to the other callers for
backward compat such that the caller can selectively translate
the argument of a subprog to the corresponding prog ctx type.
Lightly tested only:
I tried the kernel diff on my side and it addressed my needs. Will you
send a patch?
There is no real kfunc taking the "struct sk_buff *" now. It is better to make
this change together with your skb acquire/release kfunc introduction. You can
include this patch in your future set.
diff --git i/kernel/bpf/btf.c w/kernel/bpf/btf.c
index e7a59e6462a9..2d39f91617fb 100644
--- i/kernel/bpf/btf.c
+++ w/kernel/bpf/btf.c
@@ -5914,6 +5914,26 @@ bool btf_is_projection_of(const char *pname, const char *tname)
return false;
}
+static bool btf_is_kern_ctx(const struct btf *btf,
+ const struct btf_type *t,
+ enum bpf_prog_type prog_type)
+{
+ const struct btf_type *ctx_type;
+ const char *tname, *ctx_tname;
+
+ t = btf_type_skip_modifiers(btf, t->type, NULL);
+ if (!btf_type_is_struct(t))
+ return false;
+
+ tname = btf_name_by_offset(btf, t->name_off);
+ if (!tname)
+ return false;
+
+ ctx_type = find_canonical_prog_ctx_type(prog_type);
+ ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off);
+ return btf_is_projection_of(ctx_tname, tname);
We're sort of doubling the work that btf_is_prog_ctx_type() is doing also,
maybe add a flag to btf_is_prog_ctx_type() that will allow us to skip
btf_is_projection_of() call when needed? e.g. in get_kfunc_ptr_arg_type().
It is pretty cheap to do the btf_is_kern_ctx().
I don't have a strong opinion on either way. may be a "bool check_kern_ctx".