BTF: arbitrary __attribute__ encoding
Subsystems (5)
Ihor Solodrai
reviewerv2 cover BTF: arbitrary __attribute__ encoding
This patch series extends BPF Type Format (BTF) to support arbitrary __attribute__ encoding.
Setting the kind_flag to 1 in BTF type tags and decl tags now changes the meaning for the encoded tag, in particular with respect to btf_dump in libbpf.
If the kflag is set, then the string encoded by the tag represents the full attribute-list of an attribute specifier [1].
This feature will allow extending tools such as pahole and bpftool to capture and use more granular type information, and make it easier to manage compatibility between clang and gcc BPF compilers.
[1] https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Attribute-Syntax.html
v1->v2:
- When checking for specific BTF tags in the verifier, make sure the tag's kflag is 0
- Split docs and libbpf changes into separate patches
- Various renames, as suggested by Andrii and Eduard
v1: https://lore.kernel.org/bpf/[email protected]
Ihor Solodrai (6):
libbpf: introduce kflag for type_tags and decl_tags in BTF docs/bpf: document the semantics of BTF tags with kind_flag
libbpf: check the kflag of type tags in btf_dump selftests/bpf: add a btf_dump test for type_tags
bpf: allow kind_flag for BTF type and decl tags selftests/bpf: add a BTF verification test for kflagged type_tag
Documentation/bpf/btf.rst | 25 ++- include/uapi/linux/btf.h | 3 +- kernel/bpf/btf.c | 26 ++-- tools/include/uapi/linux/btf.h | 3 +- tools/lib/bpf/btf.c | 86 +++++++--- tools/lib/bpf/btf.h | 3 + tools/lib/bpf/btf_dump.c | 5 +- tools/lib/bpf/libbpf.map | 2 + tools/testing/selftests/bpf/prog_tests/btf.c | 23 ++- .../selftests/bpf/prog_tests/btf_dump.c | 147 +++++++++++++----- tools/testing/selftests/bpf/test_btf.h | 6 + 11 files changed, 244 insertions(+), 85 deletions(-)
Signature · 1 line
2.48.1
Ihor Solodrai
reviewerPATCH v2 1/6 libbpf: introduce kflag for type_tags and decl_tags in BTF
Add the following functions to libbpf API:
* btf__add_type_attr()
* btf__add_decl_attr()
These functions allow to add to BTF the type tags and decl tags with info->kflag set to 1. The kflag indicates that the tag directly encodes an __attribute__ and not a normal tag.
See Documentation/bpf/btf.rst changes in the subsequent patch for details on the semantics.
--- tools/lib/bpf/btf.c | 86 +++++++++++++++++++++++++++++----------- tools/lib/bpf/btf.h | 3 ++ tools/lib/bpf/libbpf.map | 2 + 3 files changed, 68 insertions(+), 23 deletions(-)
tools/lib/bpf/btf.c +63 -23
@@ -2090,7 +2090,7 @@ static int validate_type_id(int id) }/* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */ -static int btf_add_ref_kind (struct btf *btf,int kind,const char *name,int ref_type_id) +static int btf_add_ref_kind (struct btf *btf,int kind,const char *name,int ref_type_id,int kflag) {struct btf_type *t;int sz, name_off =0 ;
@@ -2113,7 +2113,7 @@ static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref } t->name_off = name_off; - t->info =btf_type_info (kind,0 ,0 ); + t->info =btf_type_info (kind,0 , kflag); t->type = ref_type_id;return btf_commit_type (btf, sz);
@@ -2128,7 +2128,7 @@ static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref*/ int btf__add_ptr (struct btf *btf,int ref_type_id) { -return btf_add_ref_kind (btf,BTF_KIND_PTR ,NULL , ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_PTR ,NULL , ref_type_id,0 ); }/*
@@ -2506,7 +2506,7 @@ int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)struct btf_type *t;int id; - id =btf_add_ref_kind (btf,BTF_KIND_FWD , name,0 ); + id =btf_add_ref_kind (btf,BTF_KIND_FWD , name,0 ,0 );if (id <=0 )return id; t =btf_type_by_id (btf, id);
@@ -2536,7 +2536,7 @@ int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)if (!name || !name[0 ])return libbpf_err (-EINVAL ); -return btf_add_ref_kind (btf,BTF_KIND_TYPEDEF , name, ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_TYPEDEF , name, ref_type_id,0 ); }/*
@@ -2548,7 +2548,7 @@ int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)*/ int btf__add_volatile (struct btf *btf,int ref_type_id) { -return btf_add_ref_kind (btf,BTF_KIND_VOLATILE ,NULL , ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_VOLATILE ,NULL , ref_type_id,0 ); }/*
@@ -2560,7 +2560,7 @@ int btf__add_volatile(struct btf *btf, int ref_type_id)*/ int btf__add_const (struct btf *btf,int ref_type_id) { -return btf_add_ref_kind (btf,BTF_KIND_CONST ,NULL , ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_CONST ,NULL , ref_type_id,0 ); }/*
@@ -2572,7 +2572,7 @@ int btf__add_const(struct btf *btf, int ref_type_id)*/ int btf__add_restrict (struct btf *btf,int ref_type_id) { -return btf_add_ref_kind (btf,BTF_KIND_RESTRICT ,NULL , ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_RESTRICT ,NULL , ref_type_id,0 ); }/*
@@ -2588,7 +2588,24 @@ int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id)if (!value || !value[0 ])return libbpf_err (-EINVAL ); -return btf_add_ref_kind (btf,BTF_KIND_TYPE_TAG , value, ref_type_id); +return btf_add_ref_kind (btf,BTF_KIND_TYPE_TAG , value, ref_type_id,0 ); +} + +/* +* Append new BTF_KIND_TYPE_TAG type with: +* - *value*, non-empty/non-NULL tag value; +* - *ref_type_id* - referenced type ID, it might not exist yet; +* Set info->kflag to 1, indicating this tag is an __attribute__ +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_type_attr (struct btf *btf,const char *value,int ref_type_id) +{ +if (!value || !value[0 ]) +return libbpf_err (-EINVAL ); + +return btf_add_ref_kind (btf,BTF_KIND_TYPE_TAG , value, ref_type_id,1 ); }/*
@@ -2610,7 +2627,7 @@ int btf__add_func(struct btf *btf, const char *name, linkage != BTF_FUNC_EXTERN)return libbpf_err (-EINVAL ); - id =btf_add_ref_kind (btf,BTF_KIND_FUNC , name, proto_type_id); + id =btf_add_ref_kind (btf,BTF_KIND_FUNC , name, proto_type_id,0 );if (id >0 ) {struct btf_type *t =btf_type_by_id (btf, id);
@@ -2845,18 +2862,8 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __return 0 ; } -/* -* Append new BTF_KIND_DECL_TAG type with: -* - *value* - non-empty/non-NULL string; -* - *ref_type_id* - referenced type ID, it might not exist yet; -* - *component_idx* - -1 for tagging reference type, otherwise struct/union -* member or function argument index; -* Returns: -* - >0, type ID of newly added BTF type; -* - <0, on error. -*/ -int btf__add_decl_tag (struct btf *btf,const char *value,int ref_type_id, -int component_idx) +static int btf_add_decl_tag (struct btf *btf,const char *value,int ref_type_id, +int component_idx,int kflag) {struct btf_type *t;int sz, value_off;
@@ -2880,13 +2887,46 @@ int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,return value_off; t->name_off = value_off; - t->info =btf_type_info (BTF_KIND_DECL_TAG ,0 , false); + t->info =btf_type_info (BTF_KIND_DECL_TAG ,0 , kflag); t->type = ref_type_id;btf_decl_tag (t)->component_idx = component_idx;return btf_commit_type (btf, sz); } +/* +* Append new BTF_KIND_DECL_TAG type with: +* - *value* - non-empty/non-NULL string; +* - *ref_type_id* - referenced type ID, it might not exist yet; +* - *component_idx* - -1 for tagging reference type, otherwise struct/union +* member or function argument index; +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_decl_tag (struct btf *btf,const char *value,int ref_type_id, +int component_idx) +{ +return btf_add_decl_tag (btf, value, ref_type_id, component_idx,0 ); +} + +/* +* Append new BTF_KIND_DECL_TAG type with: +* - *value* - non-empty/non-NULL string; +* - *ref_type_id* - referenced type ID, it might not exist yet; +* - *component_idx* - -1 for tagging reference type, otherwise struct/union +* member or function argument index; +* Set info->kflag to 1, indicating this tag is an __attribute__ +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_decl_attr (struct btf *btf,const char *value,int ref_type_id, +int component_idx) +{ +return btf_add_decl_tag (btf, value, ref_type_id, component_idx,1 ); +} +struct btf_ext_sec_info_param {__u32 off;__u32 len;
tools/lib/bpf/btf.h +3 -0
@@ -227,6 +227,7 @@ LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id); +LIBBPF_API int btf__add_type_attr(struct btf *btf, const char *value, int ref_type_id);/* func and func_proto construction APIs */ LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
@@ -243,6 +244,8 @@ LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,/* tag construction API */ LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,int component_idx); +LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id, +int component_idx);struct btf_dedup_opts {size_t sz;
tools/lib/bpf/libbpf.map +2 -0
@@ -436,4 +436,6 @@ LIBBPF_1.6.0 { bpf_linker__add_buf; bpf_linker__add_fd; bpf_linker__new_fd; + btf__add_decl_attr; + btf__add_type_attr; } LIBBPF_1.5.0;
Signature · 1 line
2.48.1
Re: [PATCH bpf-next v2 1/6] libbpf: introduce kflag for type_tags and decl_tags in BTF
Quoted 12 lines · Ihor Solodrai
Add the following functions to libbpf API:
*btf__add_type_attr()
*btf__add_decl_attr()These functions allow to add to BTF the type tags and decl tags with
info->kflagset to 1. The kflag indicates that the tag directly encodes an__attribute__and not a normal tag.See
Documentation/bpf/btf.rstchanges in the subsequent patch for details on the semantics.
Quoted 8 lines
Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Ihor Solodrai <[email protected]>Reviewed-by Alan Maguire <[email protected]>--- tools/lib/bpf/btf.c | 86 +++++++++++++++++++++++++++++----------- tools/lib/bpf/btf.h | 3 ++ tools/lib/bpf/libbpf.map | 2 + 3 files changed, 68 insertions(+), 23 deletions(-)
[...]
LGTM, but still messed up whitespaces, see below.
pw-bot: cr
Quoted 26 lines
tools/lib/bpf/btf.h +3
-0@@ -227,6 +227,7 @@ LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id); LIBBPF_API int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id); +LIBBPF_API int btf__add_type_attr(struct btf *btf, const char *value, int ref_type_id);/* func and func_proto construction APIs */ LIBBPF_API int btf__add_func(struct btf *btf, const char *name,@@ -243,6 +244,8 @@ LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,/* tag construction API */ LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,int component_idx); +LIBBPF_API int btf__add_decl_attr(struct btf *btf, const char *value, int ref_type_id, +int component_idx);struct btf_dedup_opts {size_t sz;tools/lib/bpf/libbpf.map +0
-0
tools/lib/bpf/libbpf.map LIBBPF_1.6.0 { · line 440
Quoted 5 lines
tools/lib/bpf/libbpf.map +2
-0@@ -436,4 +436,6 @@ LIBBPF_1.6.0 { bpf_linker__add_buf; bpf_linker__add_fd; bpf_linker__new_fd; + btf__add_decl_attr; + btf__add_type_attr;
spaces here
Quoted 6 lines
tools/lib/bpf/libbpf.map +0
-1} LIBBPF_1.5.0; --2.48.1
Ihor Solodrai
reviewerPATCH v2 2/6 docs/bpf: document the semantics of BTF tags with kind_flag
In reply to Ihor SolodraiExplain the meaning of kind_flag in BTF type_tags and decl_tags. Update uapi btf.h kind_flag comment to reflect the changes.
--- Documentation/bpf/btf.rst | 25 +++++++++++++++++++++---- include/uapi/linux/btf.h | 3 ++- tools/include/uapi/linux/btf.h | 3 ++- 3 files changed, 25 insertions(+), 6 deletions(-)
Documentation/bpf/btf.rst +21 -4
@@ -102,7 +102,8 @@ Each type contains the following common data:: * bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by - * struct, union, fwd, enum and enum64. + * struct, union, enum, fwd, enum64, + * decl_tag and type_tag */ __u32 info; /* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
@@ -478,7 +479,7 @@ No additional type data follow ``btf_type``. ``struct btf_type`` encoding requirement: * ``name_off``: offset to a non-empty string - * ``info.kind_flag``: 0 + * ``info.kind_flag``: 0 or 1 * ``info.kind``: BTF_KIND_DECL_TAG * ``info.vlen``: 0 * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
@@ -489,7 +490,6 @@ No additional type data follow ``btf_type``. __u32 component_idx; }; -The ``name_off`` encodes btf_decl_tag attribute string. The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``. For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``. For the other three types, if the btf_decl_tag attribute is
@@ -499,12 +499,21 @@ the attribute is applied to a ``struct``/``union`` member or a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a valid index (starting from 0) pointing to a member or an argument. +If ``info.kind_flag`` is 0, then this is a normal decl tag, and the +``name_off`` encodes btf_decl_tag attribute string. + +If ``info.kind_flag`` is 1, then the decl tag represents an arbitrary +__attribute__. In this case, ``name_off`` encodes a string +representing the attribute-list of the attribute specifier. For +example, for an ``__attribute__((aligned(4)))`` the string's contents +is ``aligned(4)``. + 2.2.18 BTF_KIND_TYPE_TAG ~~~~~~~~~~~~~~~~~~~~~~~~ ``struct btf_type`` encoding requirement: * ``name_off``: offset to a non-empty string - * ``info.kind_flag``: 0 + * ``info.kind_flag``: 0 or 1 * ``info.kind``: BTF_KIND_TYPE_TAG * ``info.vlen``: 0 * ``type``: the type with ``btf_type_tag`` attribute
@@ -522,6 +531,14 @@ type_tag, then zero or more const/volatile/restrict/typedef and finally the base type. The base type is one of int, ptr, array, struct, union, enum, func_proto and float types. +Similarly to decl tags, if the ``info.kind_flag`` is 0, then this is a +normal type tag, and the ``name_off`` encodes btf_type_tag attribute +string. + +If ``info.kind_flag`` is 1, then the type tag represents an arbitrary +__attribute__, and the ``name_off`` encodes a string representing the +attribute-list of the attribute specifier. + 2.2.19 BTF_KIND_ENUM64 ~~~~~~~~~~~~~~~~~~~~~~
include/uapi/linux/btf.h +2 -1
@@ -36,7 +36,8 @@ struct btf_type {* bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by -* struct, union, enum, fwd and enum64 +* struct, union, enum, fwd, enum64, +* decl_tag and type_tag */ __u32 info;/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
tools/include/uapi/linux/btf.h +2 -1
@@ -36,7 +36,8 @@ struct btf_type {* bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by -* struct, union, enum, fwd and enum64 +* struct, union, enum, fwd, enum64, +* decl_tag and type_tag */ __u32 info;/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
Signature · 1 line
2.48.1
Re: [PATCH bpf-next v2 2/6] docs/bpf: document the semantics of BTF tags with kind_flag
Quoted 16 lines · Ihor Solodrai
Explain the meaning of
kind_flagin BTFtype_tagsanddecl_tags. Update uapibtf.hkind_flagcomment to reflect the changes.Signed-off-by Ihor Solodrai <[email protected]>--- Documentation/bpf/btf.rst | 25 +++++++++++++++++++++---- include/uapi/linux/btf.h | 3 ++- tools/include/uapi/linux/btf.h | 3 ++- 3 files changed, 25 insertions(+), 6 deletions(-)Documentation/bpf/btf.rst +0
-0
Documentation/bpf/btf.rst Each type contains the following common data:: · line 106
Quoted 6 lines
Documentation/bpf/btf.rst +2
-1@@ -102,7 +102,8 @@ Each type contains the following common data:: * bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by - * struct, union, fwd, enum and enum64. + * struct, union, enum, fwd, enum64, + * decl_tag and type_tag
tabs instead of spaces (docs seem to be using spaces)
Quoted 89 lines
Documentation/bpf/btf.rst +19
-3*/ __u32 info; /* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.@@ -478,7 +479,7 @@ No additional type data follow ``btf_type``. ``struct btf_type`` encoding requirement: * ``name_off``: offset to a non-empty string - * ``info.kind_flag``: 0 + * ``info.kind_flag``: 0 or 1 * ``info.kind``: BTF_KIND_DECL_TAG * ``info.vlen``: 0 * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``@@ -489,7 +490,6 @@ No additional type data follow ``btf_type``. __u32 component_idx; }; -The ``name_off`` encodes btf_decl_tag attribute string. The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``. For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``. For the other three types, if the btf_decl_tag attribute is@@ -499,12 +499,21 @@ the attribute is applied to a ``struct``/``union`` member or a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a valid index (starting from 0) pointing to a member or an argument. +If ``info.kind_flag`` is 0, then this is a normal decl tag, and the +``name_off`` encodes btf_decl_tag attribute string. + +If ``info.kind_flag`` is 1, then the decl tag represents an arbitrary +__attribute__. In this case, ``name_off`` encodes a string +representing the attribute-list of the attribute specifier. For +example, for an ``__attribute__((aligned(4)))`` the string's contents +is ``aligned(4)``. + 2.2.18 BTF_KIND_TYPE_TAG ~~~~~~~~~~~~~~~~~~~~~~~~ ``struct btf_type`` encoding requirement: * ``name_off``: offset to a non-empty string - * ``info.kind_flag``: 0 + * ``info.kind_flag``: 0 or 1 * ``info.kind``: BTF_KIND_TYPE_TAG * ``info.vlen``: 0 * ``type``: the type with ``btf_type_tag`` attribute@@ -522,6 +531,14 @@ type_tag, then zero or more const/volatile/restrict/typedef and finally the base type. The base type is one of int, ptr, array, struct, union, enum, func_proto and float types. +Similarly to decl tags, if the ``info.kind_flag`` is 0, then this is a +normal type tag, and the ``name_off`` encodes btf_type_tag attribute +string. + +If ``info.kind_flag`` is 1, then the type tag represents an arbitrary +__attribute__, and the ``name_off`` encodes a string representing the +attribute-list of the attribute specifier. + 2.2.19 BTF_KIND_ENUM64 ~~~~~~~~~~~~~~~~~~~~~~include/uapi/linux/btf.h +2
-1@@ -36,7 +36,8 @@ struct btf_type {* bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by -* struct, union, enum, fwd and enum64 +* struct, union, enum, fwd, enum64, +* decl_tag and type_tag */ __u32 info;/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. tools/include/uapi/linux/btf.h +2
-2@@ -36,7 +36,8 @@ struct btf_type {* bits 24-28: kind (e.g. int, ptr, array...etc) * bits 29-30: unused * bit 31: kind_flag, currently used by -* struct, union, enum, fwd and enum64 +* struct, union, enum, fwd, enum64, +* decl_tag and type_tag */ __u32 info;/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. -- 2.48.1
Ihor Solodrai
reviewerPATCH v2 3/6 libbpf: check the kflag of type tags in btf_dump
In reply to Ihor SolodraiIf the kflag is set for a BTF type tag, then the tag represents an arbitrary __attribute__. Change btf_dump accordingly.
--- tools/lib/bpf/btf_dump.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +4 -1
@@ -1494,7 +1494,10 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,case BTF_KIND_TYPE_TAG :btf_dump_emit_mods (d, decls); name =btf_name_of (d, t->name_off); -btf_dump_printf (d," __attribute__((btf_type_tag(\"%s\")))" , name); +if (btf_kflag (t)) +btf_dump_printf (d," __attribute__((%s))" , name); +else +btf_dump_printf (d," __attribute__((btf_type_tag(\"%s\")))" , name);break ;case BTF_KIND_ARRAY : {const struct btf_array *a =btf_array (t);
Signature · 1 line
2.48.1
Re: [PATCH bpf-next v2 3/6] libbpf: check the kflag of type tags in btf_dump
Quoted 10 lines · Ihor Solodrai
If the kflag is set for a BTF type tag, then the tag represents an arbitrary
__attribute__. Changebtf_dumpaccordingly.Signed-off-by Ihor Solodrai <[email protected]>Reviewed-by Alan Maguire <[email protected]>--- tools/lib/bpf/btf_dump.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
LGTM
Quoted 19 lines
tools/lib/bpf/btf_dump.c +4
-2@@ -1494,7 +1494,10 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,case BTF_KIND_TYPE_TAG :btf_dump_emit_mods (d, decls); name =btf_name_of (d, t->name_off); -btf_dump_printf (d," __attribute__((btf_type_tag(\"%s\")))" , name); +if (btf_kflag (t)) +btf_dump_printf (d," __attribute__((%s))" , name); +else +btf_dump_printf (d," __attribute__((btf_type_tag(\"%s\")))" , name);break ;case BTF_KIND_ARRAY : {const struct btf_array *a =btf_array (t); --2.48.1
Ihor Solodrai
reviewerPATCH v2 4/6 selftests/bpf: add a btf_dump test for type_tags
In reply to Ihor SolodraiFactor out common routines handling custom BTF from test_btf_dump_incremental. Then use them in the test_btf_dump_type_tags.
test_btf_dump_type_tags verifies that a type tag is dumped correctly with respect to its kflag.
--- .../selftests/bpf/prog_tests/btf_dump.c | 147 +++++++++++++----- 1 file changed, 110 insertions(+), 37 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf_dump.c +110 -37
@@ -126,26 +126,69 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t)return err; } -static char *dump_buf; -static size_t dump_buf_sz; -static FILE *dump_buf_file; +struct test_ctx { +struct btf *btf; +struct btf_dump *d; +char *dump_buf; +size_t dump_buf_sz; +FILE *dump_buf_file; +}; -static void test_btf_dump_incremental (void ) +static void test_ctx__free (struct test_ctx *t) { -struct btf *btf =NULL ; -struct btf_dump *d =NULL ; -int id, err, i; +fclose (t->dump_buf_file); +free (t->dump_buf); +btf_dump__free (t->d); +btf__free (t->btf); +} - dump_buf_file =open_memstream (&dump_buf, &dump_buf_sz); -if (!ASSERT_OK_PTR (dump_buf_file,"dump_memstream" )) -return ; - btf =btf__new_empty (); -if (!ASSERT_OK_PTR (btf,"new_empty" )) +static int test_ctx__init (struct test_ctx *t) +{ + t->dump_buf_file =open_memstream (&t->dump_buf, &t->dump_buf_sz); +if (!ASSERT_OK_PTR (t->dump_buf_file,"dump_memstream" )) +return -1 ; + t->btf =btf__new_empty (); +if (!ASSERT_OK_PTR (t->btf,"new_empty" )) goto err_out; - d =btf_dump__new (btf, btf_dump_printf, dump_buf_file,NULL ); -if (!ASSERT_OK (libbpf_get_error (d),"btf_dump__new" )) + t->d =btf_dump__new (t->btf, btf_dump_printf, t->dump_buf_file,NULL ); +if (!ASSERT_OK (libbpf_get_error (t->d),"btf_dump__new" )) goto err_out; +return 0 ; + +err_out: +test_ctx__free (t); +return -1 ; +} + +static void test_ctx__dump_and_compare (struct test_ctx *t, +const char *expected_output, +const char *message) +{ +int i, err; + +for (i =1 ; i <btf__type_cnt (t->btf); i++) { + err =btf_dump__dump_type (t->d, i); +ASSERT_OK (err,"dump_type_ok" ); + } + +fflush (t->dump_buf_file); + t->dump_buf[t->dump_buf_sz] =0 ;/* some libc implementations don't do this */ + +ASSERT_STREQ (t->dump_buf, expected_output, message); +} + +static void test_btf_dump_incremental (void ) +{ +struct test_ctx t = {}; +struct btf *btf; +int id, err; + +if (test_ctx__init (&t)) +return ; + + btf = t.btf; +/* First, generate BTF corresponding to the following C code: * * enum x;
@@ -182,15 +225,7 @@ static void test_btf_dump_incremental(void) err =btf__add_field (btf,"x" ,4 ,0 ,0 );ASSERT_OK (err,"field_ok" ); -for (i =1 ; i <btf__type_cnt (btf); i++) { - err =btf_dump__dump_type (d, i); -ASSERT_OK (err,"dump_type_ok" ); - } - -fflush (dump_buf_file); - dump_buf[dump_buf_sz] =0 ;/* some libc implementations don't do this */ - -ASSERT_STREQ (dump_buf, +test_ctx__dump_and_compare (&t,"enum x;\n" "\n" "enum x {\n"
@@ -221,7 +256,7 @@ static void test_btf_dump_incremental(void)* enum values don't conflict; * */ -fseek (dump_buf_file,0 ,SEEK_SET ); +fseek (t.dump_buf_file,0 ,SEEK_SET ); id =btf__add_struct (btf,"s" ,4 );ASSERT_EQ (id,7 ,"struct_id" );
@@ -232,14 +267,7 @@ static void test_btf_dump_incremental(void) err =btf__add_field (btf,"s" ,6 ,64 ,0 );ASSERT_OK (err,"field_ok" ); -for (i =1 ; i <btf__type_cnt (btf); i++) { - err =btf_dump__dump_type (d, i); -ASSERT_OK (err,"dump_type_ok" ); - } - -fflush (dump_buf_file); - dump_buf[dump_buf_sz] =0 ;/* some libc implementations don't do this */ -ASSERT_STREQ (dump_buf, +test_ctx__dump_and_compare (&t,"struct s___2 {\n" " enum x x;\n" " enum {\n"
@@ -248,11 +276,53 @@ static void test_btf_dump_incremental(void)" struct s s;\n" "};\n\n" , "c_dump1"); -err_out: -fclose (dump_buf_file); -free (dump_buf); -btf_dump__free (d); -btf__free (btf); +test_ctx__free (&t); +} + +static void test_btf_dump_type_tags (void ) +{ +struct test_ctx t = {}; +struct btf *btf; +int id, err; + +if (test_ctx__init (&t)) +return ; + + btf = t.btf; + +/* Generate BTF corresponding to the following C code: +* +* struct s { +* void __attribute__((btf_type_tag(\"void_tag\"))) *p1; +* void __attribute__((void_attr)) *p2; +* }; +* +*/ + + id =btf__add_type_tag (btf,"void_tag" ,0 ); +ASSERT_EQ (id,1 ,"type_tag_id" ); + id =btf__add_ptr (btf, id); +ASSERT_EQ (id,2 ,"void_ptr_id1" ); + + id =btf__add_type_attr (btf,"void_attr" ,0 ); +ASSERT_EQ (id,3 ,"type_attr_id" ); + id =btf__add_ptr (btf, id); +ASSERT_EQ (id,4 ,"void_ptr_id2" ); + + id =btf__add_struct (btf,"s" ,8 ); +ASSERT_EQ (id,5 ,"struct_id" ); + err =btf__add_field (btf,"p1" ,2 ,0 ,0 ); +ASSERT_OK (err,"field_ok1" ); + err =btf__add_field (btf,"p2" ,4 ,0 ,0 ); +ASSERT_OK (err,"field_ok2" ); + +test_ctx__dump_and_compare (&t, +"struct s {\n" +" void __attribute__((btf_type_tag(\"void_tag\"))) *p1;\n" +" void __attribute__((void_attr)) *p2;\n" +"};\n\n" ,"dump_and_compare" ); + +test_ctx__free (&t); }#define STRSIZE 4096
@@ -874,6 +944,9 @@ void test_btf_dump() {if (test__start_subtest ("btf_dump: incremental" ))test_btf_dump_incremental (); +if (test__start_subtest ("btf_dump: type_tags" )) +test_btf_dump_type_tags (); + btf =libbpf_find_kernel_btf ();if (!ASSERT_OK_PTR (btf,"no kernel BTF found" ))return ;
Signature · 1 line
2.48.1
Ihor Solodrai
reviewerPATCH v2 5/6 bpf: allow kind_flag for BTF type and decl tags
In reply to Ihor SolodraiBTF type tags and decl tags now may have info->kflag set to 1, changing the semantics of the tag.
Change BTF verification to permit BTF that makes use of this feature:
* remove kflag check in btf_decl_tag_check_meta(), as both values are valid
* allow kflag to be set for BTF_KIND_TYPE_TAG type in btf_ref_type_check_meta()
Make sure kind_flag is NOT set when checking for specific BTF tags, such as "kptr", "user" etc.
Modify a selftest checking for kflag in decl_tag accordingly.
--- kernel/bpf/btf.c | 26 +++++++++----------- tools/testing/selftests/bpf/prog_tests/btf.c | 4 +-- 2 files changed, 13 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +12 -14
@@ -2575,7 +2575,7 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,return -EINVAL ; } -if (btf_type_kflag (t)) { +if (btf_type_kflag (t) &&BTF_INFO_KIND (t->info) !=BTF_KIND_TYPE_TAG ) {btf_verifier_log_type (env, t,"Invalid btf_info kind_flag" );return -EINVAL ; }
@@ -3332,6 +3332,8 @@ static int btf_find_kptr(const struct btf *btf, const struct btf_type *t, u32 off, int sz, struct btf_field_info *info, u32 field_mask) { enum btf_field_type type; +const char *tag_value; +bool is_type_tag;u32 res_id;/* Permit modifiers on the pointer itself */
@@ -3341,19 +3343,20 @@ static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,if (!btf_type_is_ptr (t))return BTF_FIELD_IGNORE ; t =btf_type_by_id (btf, t->type); - -if (!btf_type_is_type_tag (t)) + is_type_tag =btf_type_is_type_tag (t) && !btf_type_kflag (t); +if (!is_type_tag)return BTF_FIELD_IGNORE ;/* Reject extra tags */ if (btf_type_is_type_tag (btf_type_by_id (btf, t->type)))return -EINVAL ; -if (!strcmp ("kptr_untrusted" ,__btf_name_by_offset (btf, t->name_off))) + tag_value =__btf_name_by_offset (btf, t->name_off); +if (!strcmp ("kptr_untrusted" , tag_value)) type =BPF_KPTR_UNREF ; -else if (!strcmp ("kptr" ,__btf_name_by_offset (btf, t->name_off))) +else if (!strcmp ("kptr" , tag_value)) type =BPF_KPTR_REF ; -else if (!strcmp ("percpu_kptr" ,__btf_name_by_offset (btf, t->name_off))) +else if (!strcmp ("percpu_kptr" , tag_value)) type =BPF_KPTR_PERCPU ; -else if (!strcmp ("uptr" ,__btf_name_by_offset (btf, t->name_off))) +else if (!strcmp ("uptr" , tag_value)) type =BPF_UPTR ;else return -EINVAL ;
@@ -4944,11 +4947,6 @@ static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env,return -EINVAL ; } -if (btf_type_kflag (t)) { -btf_verifier_log_type (env, t,"Invalid btf_info kind_flag" ); -return -EINVAL ; - } - component_idx =btf_type_decl_tag (t)->component_idx;if (component_idx <-1 ) {btf_verifier_log_type (env, t,"Invalid component_idx" );
@@ -6743,7 +6741,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, info->btf_id = t->type; t =btf_type_by_id (btf, t->type); -if (btf_type_is_type_tag (t)) { +if (btf_type_is_type_tag (t) && !btf_type_kflag (t)) { tag_value =__btf_name_by_offset (btf, t->name_off);if (strcmp (tag_value,"user" ) ==0 ) info->reg_type |=MEM_USER ;
@@ -7002,7 +7000,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,/* check type tag */ t =btf_type_by_id (btf, mtype->type); -if (btf_type_is_type_tag (t)) { +if (btf_type_is_type_tag (t) && !btf_type_kflag (t)) { tag_value =__btf_name_by_offset (btf, t->name_off);/* check __user tag */ if (strcmp (tag_value,"user" ) ==0 )
tools/testing/selftests/bpf/prog_tests/btf.c +1 -3
@@ -3866,7 +3866,7 @@ static struct btf_raw_test raw_tests[] = { .err_str = "vlen != 0", }, { - .descr = "decl_tag test #8, invalid kflag", + .descr = "decl_tag test #8, tag with kflag", .raw_types = {BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
@@ -3881,8 +3881,6 @@ static struct btf_raw_test raw_tests[] = { .key_type_id = 1, .value_type_id = 1, .max_entries = 1, - .btf_load_err = true, - .err_str = "Invalid btf_info kind_flag", }, { .descr = "decl_tag test #9, var, invalid component_idx",
Signature · 1 line
2.48.1
Re: [PATCH bpf-next v2 5/6] bpf: allow kind_flag for BTF type and decl tags
Quoted 26 lines · Ihor Solodrai
BTF type tags and decl tags now may have
info->kflagset to 1, changing the semantics of the tag.Change BTF verification to permit BTF that makes use of this feature:
* remove kflag check inbtf_decl_tag_check_meta(), as both values are valid
* allow kflag to be set forBTF_KIND_TYPE_TAGtype inbtf_ref_type_check_meta()Make sure
kind_flagis NOT set when checking for specific BTF tags, such as "kptr", "user" etc.Modify a selftest checking for kflag in
decl_tagaccordingly.Signed-off-by Ihor Solodrai <[email protected]>--- kernel/bpf/btf.c | 26 +++++++++----------- tools/testing/selftests/bpf/prog_tests/btf.c | 4 +-- 2 files changed, 13 insertions(+), 17 deletions(-)kernel/bpf/btf.c +0
-0
kernel/bpf/btf.c static int btf_ref_type_check_meta(struct btf_verifier_env *env, · line 2578
Quoted 5 lines
kernel/bpf/btf.c +1
-1@@ -2575,7 +2575,7 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,return -EINVAL ; } -if (btf_type_kflag (t)) { +if (btf_type_kflag (t) &&BTF_INFO_KIND (t->info) !=BTF_KIND_TYPE_TAG ) {
nit: use btf_type_is_type_tag(t) ?
[...]
Ihor Solodrai
reviewerPATCH v2 6/6 selftests/bpf: add a BTF verification test for kflagged type_tag
In reply to Ihor SolodraiAdd a BTF verification test case for a type_tag with a kflag set. Type tags with a kflag are now valid.
Add BTF_DECL_ATTR_ENC and BTF_TYPE_ATTR_ENC test helper macros, corresponding to *_TAG_ENC.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 19 ++++++++++++++++++- tools/testing/selftests/bpf/test_btf.h | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/btf.c +18 -1
@@ -3870,7 +3870,7 @@ static struct btf_raw_test raw_tests[] = { .raw_types = {BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ -BTF_TYPE_ENC (NAME_TBD ,BTF_INFO_ENC (BTF_KIND_DECL_TAG ,1 ,0 ),2 ), (-1 ), +BTF_DECL_ATTR_ENC (NAME_TBD ,2 ,-1 ),BTF_END_RAW , }, BTF_STR_SEC("\0local\0tag1"),
@@ -4204,6 +4204,23 @@ static struct btf_raw_test raw_tests[] = { .btf_load_err = true, .err_str = "Type tags don't precede modifiers", }, +{ + .descr = "type_tag test #7, tag with kflag", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_TYPE_ATTR_ENC (NAME_TBD ,1 ),/* [2] */ +BTF_PTR_ENC (2 ),/* [3] */ +BTF_END_RAW , + }, +BTF_STR_SEC ("\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, +}, { .descr ="enum64 test #1, unsigned, size 8" , .raw_types = {
tools/testing/selftests/bpf/test_btf.h +6 -0
@@ -72,9 +72,15 @@#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_DECL_ATTR_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 1, 0), type), (component_idx) +#define BTF_DECL_TAG_ENC (value, type, component_idx) \ BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx) +#define BTF_TYPE_ATTR_ENC (value, type) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 1, 0), type) +#define BTF_TYPE_TAG_ENC (value, type) \ BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
Signature · 1 line
2.48.1