Skip to content

Commit d352bd8

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf: Add 64bit enum value support'
Yonghong Song says: ==================== Currently, btf only supports upto 32bit enum value with BTF_KIND_ENUM. But in kernel, some enum has 64bit values, e.g., in uapi bpf.h, we have enum { BPF_F_INDEX_MASK = 0xffffffffULL, BPF_F_CURRENT_CPU = BPF_F_INDEX_MASK, BPF_F_CTXLEN_MASK = (0xfffffULL << 32), }; With BTF_KIND_ENUM, the value for BPF_F_CTXLEN_MASK will be encoded as 0 which is incorrect. To solve this problem, BTF_KIND_ENUM64 is proposed in this patch set to support enum 64bit values. Also, since sometimes there is a need to generate C code from btf, e.g., vmlinux.h, btf kflag support is also added for BTF_KIND_ENUM and BTF_KIND_ENUM64 to indicate signedness, helping proper value printout. Changelog: v4 -> v5: - skip newly-added enum64 C test if clang version <= 14. v3 -> v4: - rename btf_type_is_any_enum() to btf_is_any_enum() to favor consistency in libbpf. - fix sign extension issue in btf_dump_get_enum_value(). - fix BPF_CORE_FIELD_SIGNED signedness issue in bpf_core_calc_field_relo(). v2 -> v3: - Implement separate btf_equal_enum()/btf_equal_enum64() and btf_compat_enum()/btf_compat_enum64(). - Add a new enum64 placeholder type dynamicly for enum64 sanitization. - For bpftool output and unit selftest, printed out signed/unsigned encoding as well. - fix some issues with BTF_KIND_ENUM is doc and clarified sign extension rules for enum values. v1 -> v2: - Changed kflag default from signed to unsigned - Fixed sanitization issue - Broke down libbpf related patches for easier review - Added more tests - More code refactorization - Corresponding llvm patch (to support enum64) is also updated ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 02f4afe + 61dbd59 commit d352bd8

29 files changed

+1217
-190
lines changed

Documentation/bpf/btf.rst

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ sequentially and type id is assigned to each recognized type starting from id
7474
#define BTF_KIND_ARRAY 3 /* Array */
7575
#define BTF_KIND_STRUCT 4 /* Struct */
7676
#define BTF_KIND_UNION 5 /* Union */
77-
#define BTF_KIND_ENUM 6 /* Enumeration */
77+
#define BTF_KIND_ENUM 6 /* Enumeration up to 32-bit values */
7878
#define BTF_KIND_FWD 7 /* Forward */
7979
#define BTF_KIND_TYPEDEF 8 /* Typedef */
8080
#define BTF_KIND_VOLATILE 9 /* Volatile */
@@ -87,6 +87,7 @@ sequentially and type id is assigned to each recognized type starting from id
8787
#define BTF_KIND_FLOAT 16 /* Floating point */
8888
#define BTF_KIND_DECL_TAG 17 /* Decl Tag */
8989
#define BTF_KIND_TYPE_TAG 18 /* Type Tag */
90+
#define BTF_KIND_ENUM64 19 /* Enumeration up to 64-bit values */
9091

9192
Note that the type section encodes debug info, not just pure types.
9293
``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -101,10 +102,10 @@ Each type contains the following common data::
101102
* bits 24-28: kind (e.g. int, ptr, array...etc)
102103
* bits 29-30: unused
103104
* bit 31: kind_flag, currently used by
104-
* struct, union and fwd
105+
* struct, union, fwd, enum and enum64.
105106
*/
106107
__u32 info;
107-
/* "size" is used by INT, ENUM, STRUCT and UNION.
108+
/* "size" is used by INT, ENUM, STRUCT, UNION and ENUM64.
108109
* "size" tells the size of the type it is describing.
109110
*
110111
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
@@ -281,10 +282,10 @@ modes exist:
281282

282283
``struct btf_type`` encoding requirement:
283284
* ``name_off``: 0 or offset to a valid C identifier
284-
* ``info.kind_flag``: 0
285+
* ``info.kind_flag``: 0 for unsigned, 1 for signed
285286
* ``info.kind``: BTF_KIND_ENUM
286287
* ``info.vlen``: number of enum values
287-
* ``size``: 4
288+
* ``size``: 1/2/4/8
288289

289290
``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum``.::
290291

@@ -297,6 +298,10 @@ The ``btf_enum`` encoding:
297298
* ``name_off``: offset to a valid C identifier
298299
* ``val``: any value
299300

301+
If the original enum value is signed and the size is less than 4,
302+
that value will be sign extended into 4 bytes. If the size is 8,
303+
the value will be truncated into 4 bytes.
304+
300305
2.2.7 BTF_KIND_FWD
301306
~~~~~~~~~~~~~~~~~~
302307

@@ -493,7 +498,7 @@ the attribute is applied to a ``struct``/``union`` member or
493498
a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
494499
valid index (starting from 0) pointing to a member or an argument.
495500

496-
2.2.17 BTF_KIND_TYPE_TAG
501+
2.2.18 BTF_KIND_TYPE_TAG
497502
~~~~~~~~~~~~~~~~~~~~~~~~
498503

499504
``struct btf_type`` encoding requirement:
@@ -516,6 +521,32 @@ type_tag, then zero or more const/volatile/restrict/typedef
516521
and finally the base type. The base type is one of
517522
int, ptr, array, struct, union, enum, func_proto and float types.
518523

524+
2.2.19 BTF_KIND_ENUM64
525+
~~~~~~~~~~~~~~~~~~~~~~
526+
527+
``struct btf_type`` encoding requirement:
528+
* ``name_off``: 0 or offset to a valid C identifier
529+
* ``info.kind_flag``: 0 for unsigned, 1 for signed
530+
* ``info.kind``: BTF_KIND_ENUM64
531+
* ``info.vlen``: number of enum values
532+
* ``size``: 1/2/4/8
533+
534+
``btf_type`` is followed by ``info.vlen`` number of ``struct btf_enum64``.::
535+
536+
struct btf_enum64 {
537+
__u32 name_off;
538+
__u32 val_lo32;
539+
__u32 val_hi32;
540+
};
541+
542+
The ``btf_enum64`` encoding:
543+
* ``name_off``: offset to a valid C identifier
544+
* ``val_lo32``: lower 32-bit value for a 64-bit value
545+
* ``val_hi32``: high 32-bit value for a 64-bit value
546+
547+
If the original enum value is signed and the size is less than 8,
548+
that value will be sign extended into 8 bytes.
549+
519550
3. BTF Kernel API
520551
=================
521552

include/linux/btf.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ static inline bool btf_type_is_enum(const struct btf_type *t)
177177
return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
178178
}
179179

180+
static inline bool btf_is_any_enum(const struct btf_type *t)
181+
{
182+
return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
183+
BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
184+
}
185+
186+
static inline bool btf_kind_core_compat(const struct btf_type *t1,
187+
const struct btf_type *t2)
188+
{
189+
return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
190+
(btf_is_any_enum(t1) && btf_is_any_enum(t2));
191+
}
192+
180193
static inline bool str_is_empty(const char *s)
181194
{
182195
return !s || !s[0];
@@ -192,6 +205,16 @@ static inline bool btf_is_enum(const struct btf_type *t)
192205
return btf_kind(t) == BTF_KIND_ENUM;
193206
}
194207

208+
static inline bool btf_is_enum64(const struct btf_type *t)
209+
{
210+
return btf_kind(t) == BTF_KIND_ENUM64;
211+
}
212+
213+
static inline u64 btf_enum64_value(const struct btf_enum64 *e)
214+
{
215+
return ((u64)e->val_hi32 << 32) | e->val_lo32;
216+
}
217+
195218
static inline bool btf_is_composite(const struct btf_type *t)
196219
{
197220
u16 kind = btf_kind(t);
@@ -332,6 +355,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t)
332355
return (struct btf_enum *)(t + 1);
333356
}
334357

358+
static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
359+
{
360+
return (struct btf_enum64 *)(t + 1);
361+
}
362+
335363
static inline const struct btf_var_secinfo *btf_type_var_secinfo(
336364
const struct btf_type *t)
337365
{

include/uapi/linux/btf.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ struct btf_type {
3636
* bits 24-28: kind (e.g. int, ptr, array...etc)
3737
* bits 29-30: unused
3838
* bit 31: kind_flag, currently used by
39-
* struct, union and fwd
39+
* struct, union, enum, fwd and enum64
4040
*/
4141
__u32 info;
42-
/* "size" is used by INT, ENUM, STRUCT, UNION and DATASEC.
42+
/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
4343
* "size" tells the size of the type it is describing.
4444
*
4545
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
@@ -63,7 +63,7 @@ enum {
6363
BTF_KIND_ARRAY = 3, /* Array */
6464
BTF_KIND_STRUCT = 4, /* Struct */
6565
BTF_KIND_UNION = 5, /* Union */
66-
BTF_KIND_ENUM = 6, /* Enumeration */
66+
BTF_KIND_ENUM = 6, /* Enumeration up to 32-bit values */
6767
BTF_KIND_FWD = 7, /* Forward */
6868
BTF_KIND_TYPEDEF = 8, /* Typedef */
6969
BTF_KIND_VOLATILE = 9, /* Volatile */
@@ -76,6 +76,7 @@ enum {
7676
BTF_KIND_FLOAT = 16, /* Floating point */
7777
BTF_KIND_DECL_TAG = 17, /* Decl Tag */
7878
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
79+
BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
7980

8081
NR_BTF_KINDS,
8182
BTF_KIND_MAX = NR_BTF_KINDS - 1,
@@ -186,4 +187,14 @@ struct btf_decl_tag {
186187
__s32 component_idx;
187188
};
188189

190+
/* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
191+
* The exact number of btf_enum64 is stored in the vlen (of the
192+
* info in "struct btf_type").
193+
*/
194+
struct btf_enum64 {
195+
__u32 name_off;
196+
__u32 val_lo32;
197+
__u32 val_hi32;
198+
};
199+
189200
#endif /* _UAPI__LINUX_BTF_H__ */

0 commit comments

Comments
 (0)