History

bpf: Add btf enum64 support

Currently, BTF only supports upto 32bit enum value with BTF_KIND_ENUM. But in kernel, some enum indeed 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), }; In this case, BTF_KIND_ENUM will encode the value of BPF_F_CTXLEN_MASK as 0, which certainly is incorrect. This patch added a new btf kind, BTF_KIND_ENUM64, which permits 64bit value to cover the above use case. The BTF_KIND_ENUM64 has the following three fields followed by the common type: struct bpf_enum64 { __u32 nume_off; __u32 val_lo32; __u32 val_hi32; }; Currently, btf type section has an alignment of 4 as all element types are u32. Representing the value with __u64 will introduce a pad for bpf_enum64 and may also introduce misalignment for the 64bit value. Hence, two members of val_hi32 and val_lo32 are chosen to avoid these issues. The kflag is also introduced for BTF_KIND_ENUM and BTF_KIND_ENUM64 to indicate whether the value is signed or unsigned. The kflag intends to provide consistent output of BTF C fortmat with the original source code. For example, the original BTF_KIND_ENUM bit value is 0xffffffff. The format C has two choices, printing out 0xffffffff or -1 and current libbpf prints out as unsigned value. But if the signedness is preserved in btf, the value can be printed the same as the original source code. The kflag value 0 means unsigned values, which is consistent to the default by libbpf and should also cover most cases as well. The new BTF_KIND_ENUM64 is intended to support the enum value represented as 64bit value. But it can represent all BTF_KIND_ENUM values as well. The compiler ([1]) and pahole will generate BTF_KIND_ENUM64 only if the value has to be represented with 64 bits. In addition, a static inline function btf_kind_core_compat() is introduced which will be used later when libbpf relo_core.c changed. Here the kernel shares the same relo_core.c with libbpf. [1] https://reviews.llvm.org/D124641

Yonghong Song committed 4 years ago

Commit 6089fb31 parent 02f4afe5 files · +185 −21Browse files

Showing the changes to tools/include/uapi/linux (1 of 5 files). Show all

Unified · Split

tools/include/uapi/linux/btf.h+14−3
@@ -36,10 +36,10 @@ struct btf_type { · 35 unmodified lines
36 * bits 24-28: kind (e.g. int, ptr, array...etc)36 * bits 24-28: kind (e.g. int, ptr, array...etc)
37 * bits 29-30: unused37 * bits 29-30: unused
38 * bit 31: kind_flag, currently used by38 * bit 31: kind_flag, currently used by
39 * struct, union and fwd39 * struct, union, enum, fwd and enum64
40 */40 */
41 __u32 info;41 __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.
43 * "size" tells the size of the type it is describing.43 * "size" tells the size of the type it is describing.
44 *44 *
45 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,45 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
@@ -63,7 +63,7 @@ enum { · 17 unmodified lines
63 BTF_KIND_ARRAY = 3, /* Array */63 BTF_KIND_ARRAY = 3, /* Array */
64 BTF_KIND_STRUCT = 4, /* Struct */64 BTF_KIND_STRUCT = 4, /* Struct */
65 BTF_KIND_UNION = 5, /* Union */65 BTF_KIND_UNION = 5, /* Union */
66 BTF_KIND_ENUM = 6, /* Enumeration */66 BTF_KIND_ENUM = 6, /* Enumeration up to 32-bit values */
67 BTF_KIND_FWD = 7, /* Forward */67 BTF_KIND_FWD = 7, /* Forward */
68 BTF_KIND_TYPEDEF = 8, /* Typedef */68 BTF_KIND_TYPEDEF = 8, /* Typedef */
69 BTF_KIND_VOLATILE = 9, /* Volatile */69 BTF_KIND_VOLATILE = 9, /* Volatile */
@@ -76,6 +76,7 @@ enum { · 6 unmodified lines
76 BTF_KIND_FLOAT = 16, /* Floating point */76 BTF_KIND_FLOAT = 16, /* Floating point */
77 BTF_KIND_DECL_TAG = 17, /* Decl Tag */77 BTF_KIND_DECL_TAG = 17, /* Decl Tag */
78 BTF_KIND_TYPE_TAG = 18, /* Type Tag */78 BTF_KIND_TYPE_TAG = 18, /* Type Tag */
79 BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
79 80
80 NR_BTF_KINDS,81 NR_BTF_KINDS,
81 BTF_KIND_MAX = NR_BTF_KINDS - 1,82 BTF_KIND_MAX = NR_BTF_KINDS - 1,
@@ -186,4 +187,14 @@ struct btf_decl_tag { · 104 unmodified lines
186 __s32 component_idx;187 __s32 component_idx;
187};188};
188 189
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 */
194struct btf_enum64 {
195 __u32 name_off;
196 __u32 val_lo32;
197 __u32 val_hi32;
198};
199
189#endif /* _UAPI__LINUX_BTF_H__ */200#endif /* _UAPI__LINUX_BTF_H__ */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t