Skip to contentSkip to conversation

Add BTF_KIND_FLOAT support

PATCH Superseded Ilya Leoshkevich 2 participants 14 messages Feb 22, 2021 – Feb 23, 2021

Subsystems (6)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add BTF_KIND_FLOAT support

Some BPF programs compiled on s390 fail to load, because s390 arch-specific linux headers contain float and double types.

Introduce support for such types by representing them using the new BTF_KIND_FLOAT. This series deals with libbpf, bpftool, in-kernel BTF parser as well as selftests and documentation.

There are also pahole and LLVM parts:

* https://github.com/iii-i/dwarves/commit/btf-kind-float-v2
* https://reviews.llvm.org/D83289

but they should go in after the libbpf part is integrated.

---

v0: https://lore.kernel.org/bpf/[email protected]/
v0 -> v1: Per Andrii's suggestion, remove the unnecessary trailing u32.

v1: https://lore.kernel.org/bpf/[email protected]/
v1 -> v2: John noticed that sanitization corrupts BTF, because new and
old sizes don't match. Per Yonghong's suggestion, use a modifier type (which has the same size as the float type) as a replacement.
Per Yonghong's suggestion, add size and alignment checks to the kernel BTF parser.

v2: https://lore.kernel.org/bpf/[email protected]/
v2 -> v3: Based on Yonghong's suggestions: Use BTF_KIND_CONST instead of
BTF_KIND_TYPEDEF and make sure that the C code generated from the sanitized BTF is well-formed; fix size calculation in tests and use NAME_TBD everywhere; limit allowed sizes to 2, 4, 8, 12 and 16 (this should also fix m68k and nds32le builds).

v3: https://lore.kernel.org/bpf/[email protected]/
v3 -> v4: More fixes for the Yonghong's findings: fix the outdated
comment in bpf_object__sanitize_btf() and add the error handling there (I've decided to check uint_id and uchar_id too in order to simplify debugging); add bpftool output example; use div64_u64_rem() instead of % in order to fix the linker error.
Also fix the "invalid BTF_INFO" test (new commit, #4).

Based on Alexei's feedback [1] I'm proceeding with the BTF_KIND_FLOAT approach.

[1] https://lore.kernel.org/bpf/CAADnVQKWPODWZ2RSJ5FJhfYpxkuV0cvSAL1O+FSr9oP1ercoBg@mail.gmail.com/

Ilya Leoshkevich (7):
bpf: Add BTF_KIND_FLOAT to uapi
libbpf: Add BTF_KIND_FLOAT support tools/bpftool: Add BTF_KIND_FLOAT support selftests/bpf: Use 25th bit in "invalid BTF_INFO" test
bpf: Add BTF_KIND_FLOAT support
selftest/bpf: Add BTF_KIND_FLOAT tests
bpf: Document BTF_KIND_FLOAT in btf.rst

 Documentation/bpf/btf.rst                    |  17 ++-
 include/uapi/linux/btf.h                     |   5 +-
 kernel/bpf/btf.c                             |  79 ++++++++++-
 tools/bpf/bpftool/btf.c                      |   8 ++
 tools/bpf/bpftool/btf_dumper.c               |   1 +
 tools/include/uapi/linux/btf.h               |   5 +-
 tools/lib/bpf/btf.c                          |  44 +++++++
 tools/lib/bpf/btf.h                          |   8 ++
 tools/lib/bpf/btf_dump.c                     |   4 +
 tools/lib/bpf/libbpf.c                       |  84 +++++++++++-
 tools/lib/bpf/libbpf.map                     |   5 +
 tools/lib/bpf/libbpf_internal.h              |   2 +
 tools/testing/selftests/bpf/btf_helpers.c    |   4 +
 tools/testing/selftests/bpf/prog_tests/btf.c | 131 ++++++++++++++++++-
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 15 files changed, 386 insertions(+), 14 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v4 1/7 bpf: Add BTF_KIND_FLOAT to uapi

Add a new kind value and expand the kind bitfield.

---
 include/uapi/linux/btf.h       | 5 +++--
 tools/include/uapi/linux/btf.h | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
include/uapi/linux/btf.h +3 -2
index 5a667107ad2c..d27b1708efe9 100644
@@ -52,7 +52,7 @@ struct btf_type {
 	};
 };
 
-#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x0f)
+#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
 #define BTF_INFO_VLEN(info)	((info) & 0xffff)
 #define BTF_INFO_KFLAG(info)	((info) >> 31)
 
@@ -72,7 +72,8 @@ struct btf_type {
 #define BTF_KIND_FUNC_PROTO	13	/* Function Proto	*/
 #define BTF_KIND_VAR		14	/* Variable	*/
 #define BTF_KIND_DATASEC	15	/* Section	*/
-#define BTF_KIND_MAX		BTF_KIND_DATASEC
+#define BTF_KIND_FLOAT		16	/* Floating point	*/
+#define BTF_KIND_MAX		BTF_KIND_FLOAT
 #define NR_BTF_KINDS		(BTF_KIND_MAX + 1)
 
 /* For some specific BTF_KIND, "struct btf_type" is immediately
tools/include/uapi/linux/btf.h +3 -2
index 5a667107ad2c..d27b1708efe9 100644
@@ -52,7 +52,7 @@ struct btf_type {
 	};
 };
 
-#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x0f)
+#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
 #define BTF_INFO_VLEN(info)	((info) & 0xffff)
 #define BTF_INFO_KFLAG(info)	((info) >> 31)
 
@@ -72,7 +72,8 @@ struct btf_type {
 #define BTF_KIND_FUNC_PROTO	13	/* Function Proto	*/
 #define BTF_KIND_VAR		14	/* Variable	*/
 #define BTF_KIND_DATASEC	15	/* Section	*/
-#define BTF_KIND_MAX		BTF_KIND_DATASEC
+#define BTF_KIND_FLOAT		16	/* Floating point	*/
+#define BTF_KIND_MAX		BTF_KIND_FLOAT
 #define NR_BTF_KINDS		(BTF_KIND_MAX + 1)
 
 /* For some specific BTF_KIND, "struct btf_type" is immediately
Signature · 1 line
2.29.2
#2 lore raw

PATCH v4 2/7 libbpf: Add BTF_KIND_FLOAT support

In reply to Ilya Leoshkevich

The logic follows that of BTF_KIND_INT most of the time. Sanitization replaces BTF_KIND_FLOATs with BTF_KIND_CONSTs pointing to equally-sized BTF_KIND_ARRAYs on older kernels, for example, the following:

    [4] FLOAT 'float' size=4

becomes the following:

    [4] CONST '(anon)' type_id=10
    ...
    [8] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [9] INT 'unsigned char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
    [10] ARRAY '(anon)' type_id=9 index_type_id=8 nr_elems=4
---
 tools/lib/bpf/btf.c             | 44 +++++++++++++++++
 tools/lib/bpf/btf.h             |  8 ++++
 tools/lib/bpf/btf_dump.c        |  4 ++
 tools/lib/bpf/libbpf.c          | 84 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.map        |  5 ++
 tools/lib/bpf/libbpf_internal.h |  2 +
 6 files changed, 142 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +44 -0
index fdfff544f59a..1ebfcc687dab 100644
@@ -292,6 +292,7 @@ static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_PTR:
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
+	case BTF_KIND_FLOAT:
 		return base_size;
 	case BTF_KIND_INT:
 		return base_size + sizeof(__u32);
@@ -339,6 +340,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
 	case BTF_KIND_PTR:
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
+	case BTF_KIND_FLOAT:
 		return 0;
 	case BTF_KIND_INT:
 		*(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
@@ -579,6 +581,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
 		case BTF_KIND_UNION:
 		case BTF_KIND_ENUM:
 		case BTF_KIND_DATASEC:
+		case BTF_KIND_FLOAT:
 			size = t->size;
 			goto done;
 		case BTF_KIND_PTR:
@@ -622,6 +625,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
 	switch (kind) {
 	case BTF_KIND_INT:
 	case BTF_KIND_ENUM:
+	case BTF_KIND_FLOAT:
 		return min(btf_ptr_sz(btf), (size_t)t->size);
 	case BTF_KIND_PTR:
 		return btf_ptr_sz(btf);
@@ -2400,6 +2404,42 @@ int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
 	return btf_commit_type(btf, sz);
 }
 
+/*
+ * Append new BTF_KIND_FLOAT type with:
+ *   - *name* - non-empty, non-NULL type name;
+ *   - *sz* - size of the type, in bytes;
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.
+ */
+int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
+{
+	struct btf_type *t;
+	int sz, name_off;
+
+	/* non-empty name */
+	if (!name || !name[0])
+		return -EINVAL;
+
+	if (btf_ensure_modifiable(btf))
+		return -ENOMEM;
+
+	sz = sizeof(struct btf_type);
+	t = btf_add_type_mem(btf, sz);
+	if (!t)
+		return -ENOMEM;
+
+	name_off = btf__add_str(btf, name);
+	if (name_off < 0)
+		return name_off;
+
+	t->name_off = name_off;
+	t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
+	t->size = byte_sz;
+
+	return btf_commit_type(btf, sz);
+}
+
 /*
  * Append new data section variable information entry for current DATASEC type:
  *   - *var_type_id* - type ID, describing type of the variable;
@@ -3653,6 +3693,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
 		case BTF_KIND_FWD:
 		case BTF_KIND_TYPEDEF:
 		case BTF_KIND_FUNC:
+		case BTF_KIND_FLOAT:
 			h = btf_hash_common(t);
 			break;
 		case BTF_KIND_INT:
@@ -3749,6 +3790,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
 		break;
 
 	case BTF_KIND_FWD:
+	case BTF_KIND_FLOAT:
 		h = btf_hash_common(t);
 		for_each_dedup_cand(d, hash_entry, h) {
 			cand_id = (__u32)(long)hash_entry->value;
@@ -4010,6 +4052,7 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
 			return btf_compat_enum(cand_type, canon_type);
 
 	case BTF_KIND_FWD:
+	case BTF_KIND_FLOAT:
 		return btf_equal_common(cand_type, canon_type);
 
 	case BTF_KIND_CONST:
@@ -4506,6 +4549,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
 	switch (btf_kind(t)) {
 	case BTF_KIND_INT:
 	case BTF_KIND_ENUM:
+	case BTF_KIND_FLOAT:
 		break;
 
 	case BTF_KIND_FWD:
tools/lib/bpf/btf.h +8 -0
index 1237bcd1dd17..c3b11bcebeda 100644
@@ -132,6 +132,9 @@ LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz
 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
 					 __u32 offset, __u32 byte_sz);
 
+/* float construction APIs */
+LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);
+
 struct btf_dedup_opts {
 	unsigned int dedup_table_size;
 	bool dont_resolve_fwds;
@@ -294,6 +297,11 @@ static inline bool btf_is_datasec(const struct btf_type *t)
 	return btf_kind(t) == BTF_KIND_DATASEC;
 }
 
+static inline bool btf_is_float(const struct btf_type *t)
+{
+	return btf_kind(t) == BTF_KIND_FLOAT;
+}
+
 static inline __u8 btf_int_encoding(const struct btf_type *t)
 {
 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
tools/lib/bpf/btf_dump.c +4 -0
index 2f9d685bd522..5e957fcceee6 100644
@@ -279,6 +279,7 @@ static int btf_dump_mark_referenced(struct btf_dump *d)
 		case BTF_KIND_INT:
 		case BTF_KIND_ENUM:
 		case BTF_KIND_FWD:
+		case BTF_KIND_FLOAT:
 			break;
 
 		case BTF_KIND_VOLATILE:
@@ -453,6 +454,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
 
 	switch (btf_kind(t)) {
 	case BTF_KIND_INT:
+	case BTF_KIND_FLOAT:
 		tstate->order_state = ORDERED;
 		return 0;
 
@@ -1133,6 +1135,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 		case BTF_KIND_STRUCT:
 		case BTF_KIND_UNION:
 		case BTF_KIND_TYPEDEF:
+		case BTF_KIND_FLOAT:
 			goto done;
 		default:
 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
@@ -1247,6 +1250,7 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
 
 		switch (kind) {
 		case BTF_KIND_INT:
+		case BTF_KIND_FLOAT:
 			btf_dump_emit_mods(d, decls);
 			name = btf_name_of(d, t->name_off);
 			btf_dump_printf(d, "%s", name);
tools/lib/bpf/libbpf.c +79 -5

161 lines not on this page · raw message

tools/lib/bpf/libbpf.map +5 -0
index 1c0fd2dd233a..ec898f464ab9 100644
@@ -350,3 +350,8 @@ LIBBPF_0.3.0 {
 		xsk_setup_xdp_prog;
 		xsk_socket__update_xskmap;
 } LIBBPF_0.2.0;
+
+LIBBPF_0.4.0 {
+	global:
+		btf__add_float;
+} LIBBPF_0.3.0;
tools/lib/bpf/libbpf_internal.h +2 -0
index 969d0ac592ba..343f6eb05637 100644
@@ -31,6 +31,8 @@
 #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
 #define BTF_PARAM_ENC(name, type) (name), (type)
 #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
+#define BTF_TYPE_FLOAT_ENC(name, sz) \
+	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
 
 #ifndef likely
 #define likely(x) __builtin_expect(!!(x), 1)
Signature · 1 line
2.29.2
#3 lore raw

Re: [PATCH v4 bpf-next 2/7] libbpf: Add BTF_KIND_FLOAT support

Quoted 8 lines · Ilya Leoshkevich

The logic follows that of BTF_KIND_INT most of the time. Sanitization replaces BTF_KIND_FLOATs with BTF_KIND_CONSTs pointing to equally-sized BTF_KIND_ARRAYs on older kernels, for example, the following:

    [4] FLOAT 'float' size=4
Quoted 8 lines

becomes the following:

    [4] CONST '(anon)' type_id=10
    ...
    [8] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [9] INT 'unsigned char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
    [10] ARRAY '(anon)' type_id=9 index_type_id=8 nr_elems=4

I liked Yonghong's initial suggestion to replace it with PTR to VOID. The only concern was that if this type was used from VAR, then sizeof(void *) != sizeof(float) on 64-bit architectures, which might theoretically mess up DATASEC layout. But is this a real concern? BPF programs don't really support floats, so there is no point in declaring float variables. I'd rather stick to a simple FLOAT -> PTR substitution than extend generated BTF.

Alternatively, was FLOAT -> anonymous empty STRUCT with desired size considered? Any problems with that?

Quoted 39 lines
---
 tools/lib/bpf/btf.c             | 44 +++++++++++++++++
 tools/lib/bpf/btf.h             |  8 ++++
 tools/lib/bpf/btf_dump.c        |  4 ++
 tools/lib/bpf/libbpf.c          | 84 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.map        |  5 ++
 tools/lib/bpf/libbpf_internal.h |  2 +
 6 files changed, 142 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +3 -0
index fdfff544f59a..1ebfcc687dab 100644
@@ -292,6 +292,7 @@ static int btf_type_size(const struct btf_type *t)
        case BTF_KIND_PTR:
        case BTF_KIND_TYPEDEF:
        case BTF_KIND_FUNC:
+       case BTF_KIND_FLOAT:
                return base_size;
        case BTF_KIND_INT:
                return base_size + sizeof(__u32);
@@ -339,6 +340,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
        case BTF_KIND_PTR:
        case BTF_KIND_TYPEDEF:
        case BTF_KIND_FUNC:
+       case BTF_KIND_FLOAT:
                return 0;
        case BTF_KIND_INT:
                *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
@@ -579,6 +581,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
                case BTF_KIND_UNION:
                case BTF_KIND_ENUM:
                case BTF_KIND_DATASEC:
+               case BTF_KIND_FLOAT:
                        size = t->size;
                        goto done;
                case BTF_KIND_PTR:

tools/lib/bpf/btf.c int btf__align_of(const struct btf *btf, __u32 id) · line 629

Quoted 5 lines
tools/lib/bpf/btf.c +1 -0
@@ -622,6 +625,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
        switch (kind) {
        case BTF_KIND_INT:
        case BTF_KIND_ENUM:
+       case BTF_KIND_FLOAT:
                return min(btf_ptr_sz(btf), (size_t)t->size);

well this won't work for 12-byte floats, would it?

Quoted 13 lines
tools/lib/bpf/btf.c +7 -0
        case BTF_KIND_PTR:
                return btf_ptr_sz(btf);
@@ -2400,6 +2404,42 @@ int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
        return btf_commit_type(btf, sz);
 }
 
+/*
+ * Append new BTF_KIND_FLOAT type with:
+ *   - *name* - non-empty, non-NULL type name;
+ *   - *sz* - size of the type, in bytes;
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.

tools/lib/bpf/btf.c int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz) · line 2423

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+ */
+int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
+{
+       struct btf_type *t;
+       int sz, name_off;
+
+       /* non-empty name */
+       if (!name || !name[0])
+               return -EINVAL;
+

check byte_sz here?

Quoted 59 lines
tools/lib/bpf/btf.c +23 -0
+       if (btf_ensure_modifiable(btf))
+               return -ENOMEM;
+
+       sz = sizeof(struct btf_type);
+       t = btf_add_type_mem(btf, sz);
+       if (!t)
+               return -ENOMEM;
+
+       name_off = btf__add_str(btf, name);
+       if (name_off < 0)
+               return name_off;
+
+       t->name_off = name_off;
+       t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
+       t->size = byte_sz;
+
+       return btf_commit_type(btf, sz);
+}
+
 /*
  * Append new data section variable information entry for current DATASEC type:
  *   - *var_type_id* - type ID, describing type of the variable;
@@ -3653,6 +3693,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
                case BTF_KIND_FWD:
                case BTF_KIND_TYPEDEF:
                case BTF_KIND_FUNC:
+               case BTF_KIND_FLOAT:
                        h = btf_hash_common(t);
                        break;
                case BTF_KIND_INT:
@@ -3749,6 +3790,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
                break;
 
        case BTF_KIND_FWD:
+       case BTF_KIND_FLOAT:
                h = btf_hash_common(t);
                for_each_dedup_cand(d, hash_entry, h) {
                        cand_id = (__u32)(long)hash_entry->value;
@@ -4010,6 +4052,7 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
                        return btf_compat_enum(cand_type, canon_type);
 
        case BTF_KIND_FWD:
+       case BTF_KIND_FLOAT:
                return btf_equal_common(cand_type, canon_type);
 
        case BTF_KIND_CONST:
@@ -4506,6 +4549,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
        switch (btf_kind(t)) {
        case BTF_KIND_INT:
        case BTF_KIND_ENUM:
+       case BTF_KIND_FLOAT:
                break;
 
        case BTF_KIND_FWD:
tools/lib/bpf/btf.h +0 -0
index 1237bcd1dd17..c3b11bcebeda 100644

tools/lib/bpf/btf.h LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz · line 136

Quoted 5 lines
tools/lib/bpf/btf.h +2 -0
@@ -132,6 +132,9 @@ LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz
 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
                                         __u32 offset, __u32 byte_sz);
 
+/* float construction APIs */
+LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);

nit: can you please put it right after btf__add_int() and drop the comment?

Quoted 4 lines
tools/lib/bpf/btf.h +1 -0
+
 struct btf_dedup_opts {
        unsigned int dedup_table_size;
        bool dont_resolve_fwds;

[...]

tools/lib/bpf/btf.h static int bpf_core_fields_are_compat(const struct btf *local_btf, · line 5014

Quoted 7 lines

static bool kernel_supports(enum kern_feature_id feat_id)

tools/lib/bpf/btf.h +2 -0
@@ -4940,6 +5010,8 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
                local_id = btf_array(local_type)->type;
                targ_id = btf_array(targ_type)->type;
                goto recur;
+       case BTF_KIND_FLOAT:
+               return local_type->size == targ_type->size;

we don't enforce this for INTs, so maybe let's not do this for FLOATs as well?

But really, FLOAT is not supported by BPF programs, so we should probably just error out on FLOAT relo.

tools/lib/bpf/btf.h static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id · line 5198

Quoted 9 lines
tools/lib/bpf/btf.h +2 -0
        default:
                pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
                        btf_kind(local_type), local_id, targ_id);
@@ -5122,6 +5194,8 @@ static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id
                skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
                goto recur;
        }
+       case BTF_KIND_FLOAT:
+               return local_type->size == targ_type->size;

ditto

Quoted 3 lines
tools/lib/bpf/btf.h +0 -0
        default:
                pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
                        btf_kind_str(local_type), local_id, targ_id);

[...]

#4 lore raw

Re: [PATCH v4 bpf-next 2/7] libbpf: Add BTF_KIND_FLOAT support

Quoted 68 lines · Andrii Nakryiko
Quoted 8 lines · Ilya Leoshkevich

The logic follows that of BTF_KIND_INT most of the time. Sanitization replaces BTF_KIND_FLOATs with BTF_KIND_CONSTs pointing to equally-sized BTF_KIND_ARRAYs on older kernels, for example, the following:

    [4] FLOAT 'float' size=4
Quoted 8 lines

becomes the following:

    [4] CONST '(anon)' type_id=10
    ...
    [8] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [9] INT 'unsigned char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
    [10] ARRAY '(anon)' type_id=9 index_type_id=8 nr_elems=4

I liked Yonghong's initial suggestion to replace it with PTR to VOID. The only concern was that if this type was used from VAR, then sizeof(void *) != sizeof(float) on 64-bit architectures, which might theoretically mess up DATASEC layout. But is this a real concern? BPF programs don't really support floats, so there is no point in declaring float variables. I'd rather stick to a simple FLOAT -> PTR substitution than extend generated BTF.

Alternatively, was FLOAT -> anonymous empty STRUCT with desired size considered? Any problems with that?

Quoted 38 lines
---
 tools/lib/bpf/btf.c             | 44 +++++++++++++++++
 tools/lib/bpf/btf.h             |  8 ++++
 tools/lib/bpf/btf_dump.c        |  4 ++
 tools/lib/bpf/libbpf.c          | 84 +++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.map        |  5 ++
 tools/lib/bpf/libbpf_internal.h |  2 +
 6 files changed, 142 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +3 -0
index fdfff544f59a..1ebfcc687dab 100644
@@ -292,6 +292,7 @@ static int btf_type_size(const struct btf_type *t)
        case BTF_KIND_PTR:
        case BTF_KIND_TYPEDEF:
        case BTF_KIND_FUNC:
+       case BTF_KIND_FLOAT:
                return base_size;
        case BTF_KIND_INT:
                return base_size + sizeof(__u32);
@@ -339,6 +340,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
        case BTF_KIND_PTR:
        case BTF_KIND_TYPEDEF:
        case BTF_KIND_FUNC:
+       case BTF_KIND_FLOAT:
                return 0;
        case BTF_KIND_INT:
                *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
@@ -579,6 +581,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
                case BTF_KIND_UNION:
                case BTF_KIND_ENUM:
                case BTF_KIND_DATASEC:
+               case BTF_KIND_FLOAT:
                        size = t->size;
                        goto done;
                case BTF_KIND_PTR:
Quoted 8 lines

tools/lib/bpf/btf.c int btf__align_of(const struct btf *btf, __u32 id) · line 629

Quoted 6 lines
tools/lib/bpf/btf.c +1 -0
@@ -622,6 +625,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
        switch (kind) {
        case BTF_KIND_INT:
        case BTF_KIND_ENUM:
+       case BTF_KIND_FLOAT:
                return min(btf_ptr_sz(btf), (size_t)t->size);

well this won't work for 12-byte floats, would it?

never mind, btf_ptr_sz() takes care of it

Quoted 133 lines
Quoted 14 lines
tools/lib/bpf/btf.c +7 -0
        case BTF_KIND_PTR:
                return btf_ptr_sz(btf);
@@ -2400,6 +2404,42 @@ int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
        return btf_commit_type(btf, sz);
 }
 
+/*
+ * Append new BTF_KIND_FLOAT type with:
+ *   - *name* - non-empty, non-NULL type name;
+ *   - *sz* - size of the type, in bytes;
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.

tools/lib/bpf/btf.c int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz) · line 2423

Quoted 10 lines
tools/lib/bpf/btf.c +10 -0
+ */
+int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
+{
+       struct btf_type *t;
+       int sz, name_off;
+
+       /* non-empty name */
+       if (!name || !name[0])
+               return -EINVAL;
+

check byte_sz here?

Quoted 59 lines
tools/lib/bpf/btf.c +23 -0
+       if (btf_ensure_modifiable(btf))
+               return -ENOMEM;
+
+       sz = sizeof(struct btf_type);
+       t = btf_add_type_mem(btf, sz);
+       if (!t)
+               return -ENOMEM;
+
+       name_off = btf__add_str(btf, name);
+       if (name_off < 0)
+               return name_off;
+
+       t->name_off = name_off;
+       t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
+       t->size = byte_sz;
+
+       return btf_commit_type(btf, sz);
+}
+
 /*
  * Append new data section variable information entry for current DATASEC type:
  *   - *var_type_id* - type ID, describing type of the variable;
@@ -3653,6 +3693,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
                case BTF_KIND_FWD:
                case BTF_KIND_TYPEDEF:
                case BTF_KIND_FUNC:
+               case BTF_KIND_FLOAT:
                        h = btf_hash_common(t);
                        break;
                case BTF_KIND_INT:
@@ -3749,6 +3790,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
                break;
 
        case BTF_KIND_FWD:
+       case BTF_KIND_FLOAT:
                h = btf_hash_common(t);
                for_each_dedup_cand(d, hash_entry, h) {
                        cand_id = (__u32)(long)hash_entry->value;
@@ -4010,6 +4052,7 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
                        return btf_compat_enum(cand_type, canon_type);
 
        case BTF_KIND_FWD:
+       case BTF_KIND_FLOAT:
                return btf_equal_common(cand_type, canon_type);
 
        case BTF_KIND_CONST:
@@ -4506,6 +4549,7 @@ static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
        switch (btf_kind(t)) {
        case BTF_KIND_INT:
        case BTF_KIND_ENUM:
+       case BTF_KIND_FLOAT:
                break;
 
        case BTF_KIND_FWD:
tools/lib/bpf/btf.h +0 -0
index 1237bcd1dd17..c3b11bcebeda 100644

tools/lib/bpf/btf.h LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz · line 136

Quoted 5 lines
tools/lib/bpf/btf.h +2 -0
@@ -132,6 +132,9 @@ LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz
 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
                                         __u32 offset, __u32 byte_sz);
 
+/* float construction APIs */
+LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);

nit: can you please put it right after btf__add_int() and drop the comment?

Quoted 4 lines
tools/lib/bpf/btf.h +1 -0
+
 struct btf_dedup_opts {
        unsigned int dedup_table_size;
        bool dont_resolve_fwds;

[...]

tools/lib/bpf/btf.h static int bpf_core_fields_are_compat(const struct btf *local_btf, · line 5014

Quoted 7 lines

static bool kernel_supports(enum kern_feature_id feat_id)

tools/lib/bpf/btf.h +2 -0
@@ -4940,6 +5010,8 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
                local_id = btf_array(local_type)->type;
                targ_id = btf_array(targ_type)->type;
                goto recur;
+       case BTF_KIND_FLOAT:
+               return local_type->size == targ_type->size;

we don't enforce this for INTs, so maybe let's not do this for FLOATs as well?

But really, FLOAT is not supported by BPF programs, so we should probably just error out on FLOAT relo.

tools/lib/bpf/btf.h static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id · line 5198

Quoted 9 lines
tools/lib/bpf/btf.h +2 -0
        default:
                pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
                        btf_kind(local_type), local_id, targ_id);
@@ -5122,6 +5194,8 @@ static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id
                skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
                goto recur;
        }
+       case BTF_KIND_FLOAT:
+               return local_type->size == targ_type->size;

ditto

Quoted 3 lines
tools/lib/bpf/btf.h +0 -0
        default:
                pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
                        btf_kind_str(local_type), local_id, targ_id);

[...]

#5 lore raw

Re: [PATCH v4 bpf-next 2/7] libbpf: Add BTF_KIND_FLOAT support

In reply to Andrii Nakryiko
Quoted 29 lines · Andrii Nakryiko
Quoted 10 lines · Ilya Leoshkevich

The logic follows that of BTF_KIND_INT most of the time. Sanitization replaces BTF_KIND_FLOATs with BTF_KIND_CONSTs pointing to equally-sized BTF_KIND_ARRAYs on older kernels, for example, the following:

    [4] FLOAT 'float' size=4

becomes the following:

Quoted 8 lines

    [4] CONST '(anon)' type_id=10
    ...
    [8] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [9] INT 'unsigned char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
    [10] ARRAY '(anon)' type_id=9 index_type_id=8 nr_elems=4

I liked Yonghong's initial suggestion to replace it with PTR to VOID. The only concern was that if this type was used from VAR, then sizeof(void *) != sizeof(float) on 64-bit architectures, which might theoretically mess up DATASEC layout. But is this a real concern? BPF programs don't really support floats, so there is no point in declaring float variables. I'd rather stick to a simple FLOAT -> PTR substitution than extend generated BTF.

Quoted 2 lines

Alternatively, was FLOAT -> anonymous empty STRUCT with desired size considered? Any problems with that?

An empty STRUCT is a really nice idea - it's future-proof and has much smaller implementation than char[]. I'll go for it.

[...]

#6 lore raw

PATCH v4 3/7 tools/bpftool: Add BTF_KIND_FLOAT support

In reply to Ilya Leoshkevich

Only dumping support needs to be adjusted, the code structure follows that of BTF_KIND_INT. Example plain and JSON outputs:

    [4] FLOAT 'float' size=4
    {"id":4,"kind":"FLOAT","name":"float","size":4}
---
 tools/bpf/bpftool/btf.c        | 8 ++++++++
 tools/bpf/bpftool/btf_dumper.c | 1 +
 2 files changed, 9 insertions(+)
tools/bpf/bpftool/btf.c +8 -0
index fe9e7b3a4b50..985610c3f193 100644
@@ -36,6 +36,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
 	[BTF_KIND_VAR]		= "VAR",
 	[BTF_KIND_DATASEC]	= "DATASEC",
+	[BTF_KIND_FLOAT]	= "FLOAT",
 };
 
 struct btf_attach_table {
@@ -327,6 +328,13 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
 			jsonw_end_array(w);
 		break;
 	}
+	case BTF_KIND_FLOAT: {
+		if (json_output)
+			jsonw_uint_field(w, "size", t->size);
+		else
+			printf(" size=%u", t->size);
+		break;
+	}
 	default:
 		break;
 	}
tools/bpf/bpftool/btf_dumper.c +1 -0
index 0e9310727281..7ca54d046362 100644
@@ -596,6 +596,7 @@ static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
 	switch (BTF_INFO_KIND(t->info)) {
 	case BTF_KIND_INT:
 	case BTF_KIND_TYPEDEF:
+	case BTF_KIND_FLOAT:
 		BTF_PRINT_ARG("%s ", btf__name_by_offset(btf, t->name_off));
 		break;
 	case BTF_KIND_STRUCT:
Signature · 1 line
2.29.2
#7 lore raw

PATCH v4 4/7 selftests/bpf: Use 25th bit in "invalid BTF_INFO" test

In reply to Ilya Leoshkevich

The bit being checked by this test is no longer reserved after introducing BTF_KIND_FLOAT, use the next one instead.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/btf.c +1 -1
index 6a7ee7420701..c29406736138 100644
@@ -1903,7 +1903,7 @@ static struct btf_raw_test raw_tests[] = {
 	.raw_types = {
 		/* int */				/* [1] */
 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
-		BTF_TYPE_ENC(0, 0x10000000, 4),
+		BTF_TYPE_ENC(0, 0x20000000, 4),
 		BTF_END_RAW,
 	},
 	.str_sec = "",
Signature · 1 line
2.29.2
#8 lore raw

PATCH v4 5/7 bpf: Add BTF_KIND_FLOAT support

In reply to Ilya Leoshkevich

On the kernel side, introduce a new btf_kind_operations. It is similar to that of BTF_KIND_INT, however, it does not need to handle encodings and bit offsets. Do not implement printing, since the kernel does not know how to format floating-point values.

---
 kernel/bpf/btf.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 2 deletions(-)
kernel/bpf/btf.c +77 -2
index 2efeb5f4b343..c405edc8e615 100644
@@ -173,7 +173,7 @@
 #define BITS_ROUNDUP_BYTES(bits) \
 	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
 
-#define BTF_INFO_MASK 0x8f00ffff
+#define BTF_INFO_MASK 0x9f00ffff
 #define BTF_INT_MASK 0x0fffffff
 #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
 #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
@@ -280,6 +280,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
 	[BTF_KIND_VAR]		= "VAR",
 	[BTF_KIND_DATASEC]	= "DATASEC",
+	[BTF_KIND_FLOAT]	= "FLOAT",
 };
 
 static const char *btf_type_str(const struct btf_type *t)
@@ -574,6 +575,7 @@ static bool btf_type_has_size(const struct btf_type *t)
 	case BTF_KIND_UNION:
 	case BTF_KIND_ENUM:
 	case BTF_KIND_DATASEC:
+	case BTF_KIND_FLOAT:
 		return true;
 	}
 
@@ -1704,6 +1706,7 @@ __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
 		case BTF_KIND_STRUCT:
 		case BTF_KIND_UNION:
 		case BTF_KIND_ENUM:
+		case BTF_KIND_FLOAT:
 			size = type->size;
 			goto resolved;
 
@@ -1849,7 +1852,7 @@ static int btf_df_check_kflag_member(struct btf_verifier_env *env,
 	return -EINVAL;
 }
 
-/* Used for ptr, array and struct/union type members.
+/* Used for ptr, array struct/union and float type members.
  * int, enum and modifier types have their specific callback functions.
  */
 static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
@@ -3675,6 +3678,77 @@ static const struct btf_kind_operations datasec_ops = {
 	.show			= btf_datasec_show,
 };
 
+static s32 btf_float_check_meta(struct btf_verifier_env *env,
+				const struct btf_type *t,
+				u32 meta_left)
+{
+	if (btf_type_vlen(t)) {
+		btf_verifier_log_type(env, t, "vlen != 0");
+		return -EINVAL;
+	}
+
+	if (btf_type_kflag(t)) {
+		btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
+		return -EINVAL;
+	}
+
+	if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 &&
+	    t->size != 16) {
+		btf_verifier_log_type(env, t, "Invalid type_size");
+		return -EINVAL;
+	}
+
+	btf_verifier_log_type(env, t, NULL);
+
+	return 0;
+}
+
+static int btf_float_check_member(struct btf_verifier_env *env,
+				  const struct btf_type *struct_type,
+				  const struct btf_member *member,
+				  const struct btf_type *member_type)
+{
+	u64 start_offset_bytes;
+	u64 end_offset_bytes;
+	u64 misalign_bits;
+	u64 align_bytes;
+	u64 align_bits;
+
+	align_bytes = min_t(u64, sizeof(void *), member_type->size);
+	align_bits = align_bytes * BITS_PER_BYTE;
+	div64_u64_rem(member->offset, align_bits, &misalign_bits);
+	if (misalign_bits) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member is not properly aligned");
+		return -EINVAL;
+	}
+
+	start_offset_bytes = member->offset / BITS_PER_BYTE;
+	end_offset_bytes = start_offset_bytes + member_type->size;
+	if (end_offset_bytes > struct_type->size) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void btf_float_log(struct btf_verifier_env *env,
+			  const struct btf_type *t)
+{
+	btf_verifier_log(env, "size=%u", t->size);
+}
+
+static const struct btf_kind_operations float_ops = {
+	.check_meta = btf_float_check_meta,
+	.resolve = btf_df_resolve,
+	.check_member = btf_float_check_member,
+	.check_kflag_member = btf_generic_check_kflag_member,
+	.log_details = btf_float_log,
+	.show = btf_df_show,
+};
+
 static int btf_func_proto_check(struct btf_verifier_env *env,
 				const struct btf_type *t)
 {
@@ -3808,6 +3882,7 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
 	[BTF_KIND_FUNC_PROTO] = &func_proto_ops,
 	[BTF_KIND_VAR] = &var_ops,
 	[BTF_KIND_DATASEC] = &datasec_ops,
+	[BTF_KIND_FLOAT] = &float_ops,
 };
 
 static s32 btf_check_meta(struct btf_verifier_env *env,
Signature · 1 line
2.29.2
#9 lore raw

PATCH v4 6/7 selftest/bpf: Add BTF_KIND_FLOAT tests

In reply to Ilya Leoshkevich

Test the good variants as well as the potential malformed ones.

---
 tools/testing/selftests/bpf/btf_helpers.c    |   4 +
 tools/testing/selftests/bpf/prog_tests/btf.c | 129 +++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 3 files changed, 136 insertions(+)
tools/testing/selftests/bpf/btf_helpers.c +4 -0
index 48f90490f922..b692e6ead9b5 100644
@@ -23,6 +23,7 @@ static const char * const btf_kind_str_mapping[] = {
 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
 	[BTF_KIND_VAR]		= "VAR",
 	[BTF_KIND_DATASEC]	= "DATASEC",
+	[BTF_KIND_FLOAT]	= "FLOAT",
 };
 
 static const char *btf_kind_str(__u16 kind)
@@ -173,6 +174,9 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
 		}
 		break;
 	}
+	case BTF_KIND_FLOAT:
+		fprintf(out, " size=%u", t->size);
+		break;
 	default:
 		break;
 	}
tools/testing/selftests/bpf/prog_tests/btf.c +129 -0

141 lines not on this page · raw message

tools/testing/selftests/bpf/test_btf.h +3 -0
index 2023725f1962..e2394eea4b7f 100644
@@ -66,4 +66,7 @@
 #define BTF_FUNC_ENC(name, func_proto) \
 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
 
+#define BTF_TYPE_FLOAT_ENC(name, sz) \
+	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
+
 #endif /* _TEST_BTF_H */
Signature · 1 line
2.29.2
#10 lore raw

Re: [PATCH v4 bpf-next 6/7] selftest/bpf: Add BTF_KIND_FLOAT tests

Quoted 6 lines · Ilya Leoshkevich

Test the good variants as well as the potential malformed ones.

---

Would be nice to have BTF dumping and dedup tests added/adjusted as well.

Quoted 5 lines
 tools/testing/selftests/bpf/btf_helpers.c    |   4 +
 tools/testing/selftests/bpf/prog_tests/btf.c | 129 +++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 3 files changed, 136 insertions(+)

[...]

#11 lore raw

Re: [PATCH v4 bpf-next 6/7] selftest/bpf: Add BTF_KIND_FLOAT tests

Quoted 12 lines · Andrii Nakryiko
Quoted 6 lines · Ilya Leoshkevich

Test the good variants as well as the potential malformed ones.

---

Would be nice to have BTF dumping and dedup tests added/adjusted as well.

Quoted 8 lines
Quoted 6 lines

 tools/testing/selftests/bpf/btf_helpers.c    |   4 + tools/testing/selftests/bpf/prog_tests/btf.c | 129
+++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +

 3 files changed, 136 insertions(+)

[...]

I will add a dedup in the next series, but dumping test requires LLVM support, so it will have to come later separately. Still, in my local setup the following works:

tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c +7 -0
@@ -205,6 +205,12 @@ struct struct_with_embedded_stuff {
        int t[11];
 };
 
+struct float_struct {
+       float *f;
+       const double *d;
+       volatile long double *ld;
+};
+
 struct root_struct {
        enum e1 _1;
        enum e2 _2;
@@ -219,6 +225,7 @@ struct root_struct {
        union_fwd_t *_12;
        union_fwd_ptr_t _13;
        struct struct_with_embedded_stuff _14;
+       struct float_struct _15;
 };
 
 /* ------ END-EXPECTED-OUTPUT ------ */
#12 lore raw

Re: [PATCH v4 bpf-next 6/7] selftest/bpf: Add BTF_KIND_FLOAT tests

Quoted 21 lines · Ilya Leoshkevich
Quoted 19 lines · Andrii Nakryiko
Quoted 6 lines · Ilya Leoshkevich

Test the good variants as well as the potential malformed ones.

---

Would be nice to have BTF dumping and dedup tests added/adjusted as well.

Quoted 6 lines
 tools/testing/selftests/bpf/btf_helpers.c    |   4 +
 tools/testing/selftests/bpf/prog_tests/btf.c | 129
+++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 3 files changed, 136 insertions(+)
Quoted 5 lines
Quoted 1 line

[...]

I will add a dedup in the next series, but dumping test requires LLVM support, so it will have to come later separately. Still, in my local setup the following works:

Awesome, thanks for checking!

Quoted 26 lines
tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c +7 -0
@@ -205,6 +205,12 @@ struct struct_with_embedded_stuff {
        int t[11];
 };
 
+struct float_struct {
+       float *f;
+       const double *d;
+       volatile long double *ld;
+};
+
 struct root_struct {
        enum e1 _1;
        enum e2 _2;
@@ -219,6 +225,7 @@ struct root_struct {
        union_fwd_t *_12;
        union_fwd_ptr_t _13;
        struct struct_with_embedded_stuff _14;
+       struct float_struct _15;
 };
 
 /* ------ END-EXPECTED-OUTPUT ------ */
#13 lore raw

PATCH v4 7/7 bpf: Document BTF_KIND_FLOAT in btf.rst

In reply to Ilya Leoshkevich

Also document the expansion of the kind bitfield.

---
 Documentation/bpf/btf.rst | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
Documentation/bpf/btf.rst +15 -2
index 44dc789de2b4..846354cd2d69 100644
@@ -84,6 +84,7 @@ sequentially and type id is assigned to each recognized type starting from id
     #define BTF_KIND_FUNC_PROTO     13      /* Function Proto       */
     #define BTF_KIND_VAR            14      /* Variable     */
     #define BTF_KIND_DATASEC        15      /* Section      */
+    #define BTF_KIND_FLOAT          16      /* Floating point       */
 
 Note that the type section encodes debug info, not just pure types.
 ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -95,8 +96,8 @@ Each type contains the following common data::
         /* "info" bits arrangement
          * bits  0-15: vlen (e.g. # of struct's members)
          * bits 16-23: unused
-         * bits 24-27: kind (e.g. int, ptr, array...etc)
-         * bits 28-30: unused
+         * bits 24-28: kind (e.g. int, ptr, array...etc)
+         * bits 29-30: unused
          * bit     31: kind_flag, currently used by
          *             struct, union and fwd
          */
@@ -452,6 +453,18 @@ map definition.
   * ``offset``: the in-section offset of the variable
   * ``size``: the size of the variable in bytes
 
+2.2.16 BTF_KIND_FLOAT
+~~~~~~~~~~~~~~~~~~~~~
+
+``struct btf_type`` encoding requirement:
+ * ``name_off``: any valid offset
+ * ``info.kind_flag``: 0
+ * ``info.kind``: BTF_KIND_FLOAT
+ * ``info.vlen``: 0
+ * ``size``: the size of the float type in bytes: 2, 4, 8, 12 or 16.
+
+No additional type data follow ``btf_type``.
+
 3. BTF Kernel API
 *****************
 
Signature · 1 line
2.29.2