From: Hou Tao <houtao1@xxxxxxxxxx> Add helper btf_find_dynptr() to check whether or not the passed btf type is bpf_dynptr. It can be extend to find an embedded dynptr in passed btf type if needed later. It will be used by bpf map (e.g. qp-trie) with bpf_dynptr key. Signed-off-by: Hou Tao <houtao1@xxxxxxxxxx> --- include/linux/btf.h | 1 + kernel/bpf/btf.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/linux/btf.h b/include/linux/btf.h index f9aababc5d78..5bf508c2bad2 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -165,6 +165,7 @@ int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); int btf_find_timer(const struct btf *btf, const struct btf_type *t); struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, const struct btf_type *t); +int btf_find_dynptr(const struct btf *btf, const struct btf_type *t); bool btf_type_is_void(const struct btf_type *t); s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index a44ad4b347ff..fefbe84c6998 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3522,6 +3522,19 @@ struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, return ERR_PTR(ret); } +/* Now only allow to use 'struct bpf_dynptr' as map key. + * Map key with embedded bpf_dynptr is not allowed. + */ +int btf_find_dynptr(const struct btf *btf, const struct btf_type *t) +{ + /* Only allow struct type */ + if (__btf_type_is_struct(t) && t->size == sizeof(struct bpf_dynptr) && + !strcmp("bpf_dynptr", __btf_name_by_offset(btf, t->name_off))) + return 0; + + return -EINVAL; +} + static void __btf_struct_show(const struct btf *btf, const struct btf_type *t, u32 type_id, void *data, u8 bits_offset, struct btf_show *show) -- 2.29.2