Skip to contentSkip to conversation

btf: Add support for the floating-point types

PATCH Applied Ilya Leoshkevich 3 participants 9 messages Mar 6, 2021 – Mar 8, 2021 1 message not in the archive

Subsystems (2)

v1 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btf: Add support for the floating-point types

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8
---
 btf_loader.c   | 21 +++++++++++++++++++--
 dwarf_loader.c | 11 +++++++++++
 lib/bpf        |  2 +-
 libbtf.c       | 28 +++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 4 deletions(-)
btf_loader.c +19 -2
index ec286f4..7cc39aa 100644
@@ -160,7 +160,7 @@ static struct variable *variable__new(strings_t name, uint32_t linkage)
 	return var;
 }
 
-static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+static int create_new_int_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
 	uint32_t attrs = btf_int_encoding(tp);
 	strings_t name = tp->name_off;
@@ -175,6 +175,20 @@ static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp,
 	return 0;
 }
 
+static int create_new_float_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+{
+	strings_t name = tp->name_off;
+	struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
+
+	if (base == NULL)
+		return -ENOMEM;
+
+	base->tag.tag = DW_TAG_base_type;
+	cu__add_tag_with_id(btfe->priv, &base->tag, id);
+
+	return 0;
+}
+
 static int create_new_array(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
 	struct btf_array *ap = btf_array(tp);
@@ -397,7 +411,7 @@ static int btf_elf__load_types(struct btf_elf *btfe)
 
 		switch (type) {
 		case BTF_KIND_INT:
-			err = create_new_base_type(btfe, type_ptr, type_index);
+			err = create_new_int_type(btfe, type_ptr, type_index);
 			break;
 		case BTF_KIND_ARRAY:
 			err = create_new_array(btfe, type_ptr, type_index);
@@ -442,6 +456,9 @@ static int btf_elf__load_types(struct btf_elf *btfe)
 			// BTF_KIND_FUNC corresponding to a defined subprogram.
 			err = create_new_function(btfe, type_ptr, type_index);
 			break;
+		case BTF_KIND_FLOAT:
+			err = create_new_float_type(btfe, type_ptr, type_index);
+			break;
 		default:
 			fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
 			fflush(stderr);
dwarf_loader.c +11 -0
index b73d786..c5e6681 100644
@@ -461,6 +461,16 @@ static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
 	return ptr;
 }
 
+static uint8_t encoding_to_float_type(uint64_t encoding)
+{
+	switch (encoding) {
+	case DW_ATE_complex_float:	return BT_FP_CMPLX;
+	case DW_ATE_float:		return BT_FP_SINGLE;
+	case DW_ATE_imaginary_float:	return BT_FP_IMGRY;
+	default:			return 0;
+	}
+}
+
 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
 {
 	struct base_type *bt = tag__alloc(cu, sizeof(*bt));
@@ -474,6 +484,7 @@ static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
 		bt->is_signed = encoding == DW_ATE_signed;
 		bt->is_varargs = false;
 		bt->name_has_encoding = true;
+		bt->float_type = encoding_to_float_type(encoding);
 	}
 
 	return bt;
lib/bpf +1 -1
index 5af3d86..986962f 160000
@@ -1 +1 @@
-Subproject commit 5af3d86b5a2c5fecdc3ab83822d083edd32b4396
+Subproject commit 986962fade5dfa89c2890f3854eb040d2a64ab38
libbtf.c +27 -1
index 9f76283..ccd9f90 100644
@@ -227,6 +227,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_elf__printable_name(const struct btf_elf *btfe, uint32_t offset)
@@ -367,6 +368,27 @@ static void btf_log_func_param(const struct btf_elf *btfe,
 	}
 }
 
+static int32_t btf_elf__add_float_type(struct btf_elf *btfe,
+				       const struct base_type *bt,
+				       const char *name)
+{
+	int32_t id;
+
+	id = btf__add_float(btfe->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
+	if (id < 0) {
+		btf_elf__log_err(btfe, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
+	} else {
+		const struct btf_type *t;
+
+		t = btf__type_by_id(btfe->btf, id);
+		btf_elf__log_type(btfe, t, false, true,
+				  "size=%u nr_bits=%u",
+				  t->size, bt->bit_size);
+	}
+
+	return id;
+}
+
 int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
 			       const char *name)
 {
@@ -380,7 +402,11 @@ int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
 	} else if (bt->is_bool) {
 		encoding = BTF_INT_BOOL;
 	} else if (bt->float_type) {
-		fprintf(stderr, "float_type is not supported\n");
+		if (bt->float_type == BT_FP_SINGLE ||
+		    bt->float_type == BT_FP_DOUBLE ||
+		    bt->float_type == BT_FP_LDBL)
+			return btf_elf__add_float_type(btfe, bt, name);
+		fprintf(stderr, "Complex, interval and imaginary float types are not supported\n");
 		return -1;
 	}
 
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Ilya Leoshkevich

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Quoted 6 lines

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8

---

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

Quoted 151 lines
 btf_loader.c   | 21 +++++++++++++++++++--
 dwarf_loader.c | 11 +++++++++++
 lib/bpf        |  2 +-
 libbtf.c       | 28 +++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 4 deletions(-)
btf_loader.c +19 -2
index ec286f4..7cc39aa 100644
@@ -160,7 +160,7 @@ static struct variable *variable__new(strings_t name, uint32_t linkage)
        return var;
 }
 
-static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+static int create_new_int_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        uint32_t attrs = btf_int_encoding(tp);
        strings_t name = tp->name_off;
@@ -175,6 +175,20 @@ static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp,
        return 0;
 }
 
+static int create_new_float_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+{
+       strings_t name = tp->name_off;
+       struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
+
+       if (base == NULL)
+               return -ENOMEM;
+
+       base->tag.tag = DW_TAG_base_type;
+       cu__add_tag_with_id(btfe->priv, &base->tag, id);
+
+       return 0;
+}
+
 static int create_new_array(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        struct btf_array *ap = btf_array(tp);
@@ -397,7 +411,7 @@ static int btf_elf__load_types(struct btf_elf *btfe)
 
                switch (type) {
                case BTF_KIND_INT:
-                       err = create_new_base_type(btfe, type_ptr, type_index);
+                       err = create_new_int_type(btfe, type_ptr, type_index);
                        break;
                case BTF_KIND_ARRAY:
                        err = create_new_array(btfe, type_ptr, type_index);
@@ -442,6 +456,9 @@ static int btf_elf__load_types(struct btf_elf *btfe)
                        // BTF_KIND_FUNC corresponding to a defined subprogram.
                        err = create_new_function(btfe, type_ptr, type_index);
                        break;
+               case BTF_KIND_FLOAT:
+                       err = create_new_float_type(btfe, type_ptr, type_index);
+                       break;
                default:
                        fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
                        fflush(stderr);
dwarf_loader.c +11 -0
index b73d786..c5e6681 100644
@@ -461,6 +461,16 @@ static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
        return ptr;
 }
 
+static uint8_t encoding_to_float_type(uint64_t encoding)
+{
+       switch (encoding) {
+       case DW_ATE_complex_float:      return BT_FP_CMPLX;
+       case DW_ATE_float:              return BT_FP_SINGLE;
+       case DW_ATE_imaginary_float:    return BT_FP_IMGRY;
+       default:                        return 0;
+       }
+}
+
 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
 {
        struct base_type *bt = tag__alloc(cu, sizeof(*bt));
@@ -474,6 +484,7 @@ static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
                bt->is_signed = encoding == DW_ATE_signed;
                bt->is_varargs = false;
                bt->name_has_encoding = true;
+               bt->float_type = encoding_to_float_type(encoding);
        }
 
        return bt;
lib/bpf +1 -1
index 5af3d86..986962f 160000
@@ -1 +1 @@
-Subproject commit 5af3d86b5a2c5fecdc3ab83822d083edd32b4396
+Subproject commit 986962fade5dfa89c2890f3854eb040d2a64ab38
libbtf.c +27 -2
index 9f76283..ccd9f90 100644
@@ -227,6 +227,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_elf__printable_name(const struct btf_elf *btfe, uint32_t offset)
@@ -367,6 +368,27 @@ static void btf_log_func_param(const struct btf_elf *btfe,
        }
 }
 
+static int32_t btf_elf__add_float_type(struct btf_elf *btfe,
+                                      const struct base_type *bt,
+                                      const char *name)
+{
+       int32_t id;
+
+       id = btf__add_float(btfe->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
+       if (id < 0) {
+               btf_elf__log_err(btfe, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
+       } else {
+               const struct btf_type *t;
+
+               t = btf__type_by_id(btfe->btf, id);
+               btf_elf__log_type(btfe, t, false, true,
+                                 "size=%u nr_bits=%u",
+                                 t->size, bt->bit_size);
+       }
+
+       return id;
+}
+
 int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
                               const char *name)
 {
@@ -380,7 +402,11 @@ int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
        } else if (bt->is_bool) {
                encoding = BTF_INT_BOOL;
        } else if (bt->float_type) {
-               fprintf(stderr, "float_type is not supported\n");
+               if (bt->float_type == BT_FP_SINGLE ||
+                   bt->float_type == BT_FP_DOUBLE ||
+                   bt->float_type == BT_FP_LDBL)
+                       return btf_elf__add_float_type(btfe, bt, name);
+               fprintf(stderr, "Complex, interval and imaginary float types are not supported\n");
                return -1;
        }
 
--

2.29.2

#2 lore raw

Em Sat, Mar 06, 2021 at 07:16:08PM -0800, Andrii Nakryiko escreveu:

Quoted 16 lines · Ilya Leoshkevich
Quoted 15 lines

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Example of the resulting entry in the vmlinux BTF:

Quoted 8 lines
Quoted 4 lines
    [7164] FLOAT 'double' size=8

---

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

So older versions of bpftool will fail with a .BTF section having this new float? I thought it would just skip it emitting a warning? Probably not possible as we don't have the record size encoded in a header, right?

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT
[acme@five pahole]$ type pahole
pahole is /home/acme/bin/pahole
[acme@five pahole]$ ls -la ~/bin/pahole
lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole [acme@five pahole]$ pahole -J vmlinux
[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
Error: failed to load BTF from vmlinux: Invalid argument
[acme@five pahole]$

Perhaps the warning emitted by bpftool should suggest updating the tool as it found a record type it doesn't know about?

/me goes to update bpftool...

- Arnaldo

Quoted 153 lines
Quoted 151 lines
 btf_loader.c   | 21 +++++++++++++++++++--
 dwarf_loader.c | 11 +++++++++++
 lib/bpf        |  2 +-
 libbtf.c       | 28 +++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 4 deletions(-)
btf_loader.c +19 -2
index ec286f4..7cc39aa 100644
@@ -160,7 +160,7 @@ static struct variable *variable__new(strings_t name, uint32_t linkage)
        return var;
 }
 
-static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+static int create_new_int_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        uint32_t attrs = btf_int_encoding(tp);
        strings_t name = tp->name_off;
@@ -175,6 +175,20 @@ static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp,
        return 0;
 }
 
+static int create_new_float_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+{
+       strings_t name = tp->name_off;
+       struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
+
+       if (base == NULL)
+               return -ENOMEM;
+
+       base->tag.tag = DW_TAG_base_type;
+       cu__add_tag_with_id(btfe->priv, &base->tag, id);
+
+       return 0;
+}
+
 static int create_new_array(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        struct btf_array *ap = btf_array(tp);
@@ -397,7 +411,7 @@ static int btf_elf__load_types(struct btf_elf *btfe)
 
                switch (type) {
                case BTF_KIND_INT:
-                       err = create_new_base_type(btfe, type_ptr, type_index);
+                       err = create_new_int_type(btfe, type_ptr, type_index);
                        break;
                case BTF_KIND_ARRAY:
                        err = create_new_array(btfe, type_ptr, type_index);
@@ -442,6 +456,9 @@ static int btf_elf__load_types(struct btf_elf *btfe)
                        // BTF_KIND_FUNC corresponding to a defined subprogram.
                        err = create_new_function(btfe, type_ptr, type_index);
                        break;
+               case BTF_KIND_FLOAT:
+                       err = create_new_float_type(btfe, type_ptr, type_index);
+                       break;
                default:
                        fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
                        fflush(stderr);
dwarf_loader.c +11 -0
index b73d786..c5e6681 100644
@@ -461,6 +461,16 @@ static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
        return ptr;
 }
 
+static uint8_t encoding_to_float_type(uint64_t encoding)
+{
+       switch (encoding) {
+       case DW_ATE_complex_float:      return BT_FP_CMPLX;
+       case DW_ATE_float:              return BT_FP_SINGLE;
+       case DW_ATE_imaginary_float:    return BT_FP_IMGRY;
+       default:                        return 0;
+       }
+}
+
 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
 {
        struct base_type *bt = tag__alloc(cu, sizeof(*bt));
@@ -474,6 +484,7 @@ static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
                bt->is_signed = encoding == DW_ATE_signed;
                bt->is_varargs = false;
                bt->name_has_encoding = true;
+               bt->float_type = encoding_to_float_type(encoding);
        }
 
        return bt;
lib/bpf +1 -1
index 5af3d86..986962f 160000
@@ -1 +1 @@
-Subproject commit 5af3d86b5a2c5fecdc3ab83822d083edd32b4396
+Subproject commit 986962fade5dfa89c2890f3854eb040d2a64ab38
libbtf.c +27 -2
index 9f76283..ccd9f90 100644
@@ -227,6 +227,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_elf__printable_name(const struct btf_elf *btfe, uint32_t offset)
@@ -367,6 +368,27 @@ static void btf_log_func_param(const struct btf_elf *btfe,
        }
 }
 
+static int32_t btf_elf__add_float_type(struct btf_elf *btfe,
+                                      const struct base_type *bt,
+                                      const char *name)
+{
+       int32_t id;
+
+       id = btf__add_float(btfe->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
+       if (id < 0) {
+               btf_elf__log_err(btfe, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
+       } else {
+               const struct btf_type *t;
+
+               t = btf__type_by_id(btfe->btf, id);
+               btf_elf__log_type(btfe, t, false, true,
+                                 "size=%u nr_bits=%u",
+                                 t->size, bt->bit_size);
+       }
+
+       return id;
+}
+
 int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
                               const char *name)
 {
@@ -380,7 +402,11 @@ int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
        } else if (bt->is_bool) {
                encoding = BTF_INT_BOOL;
        } else if (bt->float_type) {
-               fprintf(stderr, "float_type is not supported\n");
+               if (bt->float_type == BT_FP_SINGLE ||
+                   bt->float_type == BT_FP_DOUBLE ||
+                   bt->float_type == BT_FP_LDBL)
+                       return btf_elf__add_float_type(btfe, bt, name);
+               fprintf(stderr, "Complex, interval and imaginary float types are not supported\n");
                return -1;
        }
 
--

2.29.2

Signature · 1 line
- Arnaldo
#3 lore raw

Em Sun, Mar 07, 2021 at 10:16:31AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 38 lines

Em Sat, Mar 06, 2021 at 07:16:08PM -0800, Andrii Nakryiko escreveu:

Quoted 14 lines · Ilya Leoshkevich
Quoted 13 lines

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Quoted 7 lines
Quoted 3 lines

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

So older versions of bpftool will fail with a .BTF section having this new float? I thought it would just skip it emitting a warning? Probably not possible as we don't have the record size encoded in a header, right?

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT
[acme@five pahole]$ type pahole
pahole is /home/acme/bin/pahole
[acme@five pahole]$ ls -la ~/bin/pahole
lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole [acme@five pahole]$ pahole -J vmlinux
[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
Error: failed to load BTF from vmlinux: Invalid argument
[acme@five pahole]$

Quoted 4 lines

Perhaps the warning emitted by bpftool should suggest updating the tool as it found a record type it doesn't know about?

/me goes to update bpftool...

Works with the bpftool in bpf-next:

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head [8006] FLOAT 'double' size=8
[acme@five pahole]$

- Arnaldo

Quoted 159 lines

- Arnaldo

Quoted 153 lines
Quoted 151 lines
 btf_loader.c   | 21 +++++++++++++++++++--
 dwarf_loader.c | 11 +++++++++++
 lib/bpf        |  2 +-
 libbtf.c       | 28 +++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 4 deletions(-)
btf_loader.c +19 -2
index ec286f4..7cc39aa 100644
@@ -160,7 +160,7 @@ static struct variable *variable__new(strings_t name, uint32_t linkage)
        return var;
 }
 
-static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+static int create_new_int_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        uint32_t attrs = btf_int_encoding(tp);
        strings_t name = tp->name_off;
@@ -175,6 +175,20 @@ static int create_new_base_type(struct btf_elf *btfe, const struct btf_type *tp,
        return 0;
 }
 
+static int create_new_float_type(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
+{
+       strings_t name = tp->name_off;
+       struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
+
+       if (base == NULL)
+               return -ENOMEM;
+
+       base->tag.tag = DW_TAG_base_type;
+       cu__add_tag_with_id(btfe->priv, &base->tag, id);
+
+       return 0;
+}
+
 static int create_new_array(struct btf_elf *btfe, const struct btf_type *tp, uint32_t id)
 {
        struct btf_array *ap = btf_array(tp);
@@ -397,7 +411,7 @@ static int btf_elf__load_types(struct btf_elf *btfe)
 
                switch (type) {
                case BTF_KIND_INT:
-                       err = create_new_base_type(btfe, type_ptr, type_index);
+                       err = create_new_int_type(btfe, type_ptr, type_index);
                        break;
                case BTF_KIND_ARRAY:
                        err = create_new_array(btfe, type_ptr, type_index);
@@ -442,6 +456,9 @@ static int btf_elf__load_types(struct btf_elf *btfe)
                        // BTF_KIND_FUNC corresponding to a defined subprogram.
                        err = create_new_function(btfe, type_ptr, type_index);
                        break;
+               case BTF_KIND_FLOAT:
+                       err = create_new_float_type(btfe, type_ptr, type_index);
+                       break;
                default:
                        fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
                        fflush(stderr);
dwarf_loader.c +11 -0
index b73d786..c5e6681 100644
@@ -461,6 +461,16 @@ static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
        return ptr;
 }
 
+static uint8_t encoding_to_float_type(uint64_t encoding)
+{
+       switch (encoding) {
+       case DW_ATE_complex_float:      return BT_FP_CMPLX;
+       case DW_ATE_float:              return BT_FP_SINGLE;
+       case DW_ATE_imaginary_float:    return BT_FP_IMGRY;
+       default:                        return 0;
+       }
+}
+
 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
 {
        struct base_type *bt = tag__alloc(cu, sizeof(*bt));
@@ -474,6 +484,7 @@ static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
                bt->is_signed = encoding == DW_ATE_signed;
                bt->is_varargs = false;
                bt->name_has_encoding = true;
+               bt->float_type = encoding_to_float_type(encoding);
        }
 
        return bt;
lib/bpf +1 -1
index 5af3d86..986962f 160000
@@ -1 +1 @@
-Subproject commit 5af3d86b5a2c5fecdc3ab83822d083edd32b4396
+Subproject commit 986962fade5dfa89c2890f3854eb040d2a64ab38
libbtf.c +27 -2
index 9f76283..ccd9f90 100644
@@ -227,6 +227,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_elf__printable_name(const struct btf_elf *btfe, uint32_t offset)
@@ -367,6 +368,27 @@ static void btf_log_func_param(const struct btf_elf *btfe,
        }
 }
 
+static int32_t btf_elf__add_float_type(struct btf_elf *btfe,
+                                      const struct base_type *bt,
+                                      const char *name)
+{
+       int32_t id;
+
+       id = btf__add_float(btfe->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
+       if (id < 0) {
+               btf_elf__log_err(btfe, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
+       } else {
+               const struct btf_type *t;
+
+               t = btf__type_by_id(btfe->btf, id);
+               btf_elf__log_type(btfe, t, false, true,
+                                 "size=%u nr_bits=%u",
+                                 t->size, bt->bit_size);
+       }
+
+       return id;
+}
+
 int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
                               const char *name)
 {
@@ -380,7 +402,11 @@ int32_t btf_elf__add_base_type(struct btf_elf *btfe, const struct base_type *bt,
        } else if (bt->is_bool) {
                encoding = BTF_INT_BOOL;
        } else if (bt->float_type) {
-               fprintf(stderr, "float_type is not supported\n");
+               if (bt->float_type == BT_FP_SINGLE ||
+                   bt->float_type == BT_FP_DOUBLE ||
+                   bt->float_type == BT_FP_LDBL)
+                       return btf_elf__add_float_type(btfe, bt, name);
+               fprintf(stderr, "Complex, interval and imaginary float types are not supported\n");
                return -1;
        }
 
--

2.29.2

Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
#4 lore raw

Em Sun, Mar 07, 2021 at 10:44:21AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 42 lines

Em Sun, Mar 07, 2021 at 10:16:31AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 41 lines

Em Sat, Mar 06, 2021 at 07:16:08PM -0800, Andrii Nakryiko escreveu:

Quoted 14 lines · Ilya Leoshkevich
Quoted 13 lines

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Quoted 7 lines
Quoted 3 lines

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

So older versions of bpftool will fail with a .BTF section having this new float? I thought it would just skip it emitting a warning? Probably not possible as we don't have the record size encoded in a header, right?

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT
[acme@five pahole]$ type pahole
pahole is /home/acme/bin/pahole
[acme@five pahole]$ ls -la ~/bin/pahole
lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole [acme@five pahole]$ pahole -J vmlinux
[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
Error: failed to load BTF from vmlinux: Invalid argument
[acme@five pahole]$

Perhaps the warning emitted by bpftool should suggest updating the tool as it found a record type it doesn't know about?

Quoted 7 lines
Quoted 1 line

/me goes to update bpftool...

Works with the bpftool in bpf-next:

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head [8006] FLOAT 'double' size=8
[acme@five pahole]$

Applied, with this committer notes:

Committer testing:

  $ rm -rf build  # To update the libbpf git submodule
  $ mkdir build
  $ cd build
  $ cmake ..
  $ cd ..
  $ make -C build
  # No BTF_KIND_FLOAT before:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  $ type pahole
  pahole is /home/acme/bin/pahole
  $ ls -la ~/bin/pahole
  lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole
  # Encode BTF:
  $ pahole -J vmlinux
  $ bpftool btf dump file vmlinux | grep -w FLOAT | head
  Error: failed to load BTF from vmlinux: Invalid argument
  $
  # Update bpftool to what is in bpf-next, then try again:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  [8006] FLOAT 'double' size=8
  $
  # Now check that pahole works well, i.e. that the BTF loader works
  $ pahole -F btf vmlinux -C sk_buff_head
  struct sk_buff_head {
        struct sk_buff *           next;                 /*     0     8 */
        struct sk_buff *           prev;                 /*     8     8 */
        __u32                      qlen;                 /*    16     4 */
        spinlock_t                 lock;                 /*    20     4 */
        /* size: 24, cachelines: 1, members: 4 */
        /* last cacheline: 24 bytes */
  };
  $
  $ pahole -F btf vmlinux  | wc -l
  122676
  $

Now will build a kernel with this new version, reboot, then push publicly.

- Arnaldo

#5 lore raw

Adding Jiri to the CC list.

Em Sun, Mar 07, 2021 at 10:50:19AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 92 lines

Em Sun, Mar 07, 2021 at 10:44:21AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 42 lines

Em Sun, Mar 07, 2021 at 10:16:31AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 41 lines

Em Sat, Mar 06, 2021 at 07:16:08PM -0800, Andrii Nakryiko escreveu:

Quoted 14 lines · Ilya Leoshkevich
Quoted 13 lines

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit 986962fade5, so update the libbpf submodule to that version and use the new btf__add_float() function in order to emit the floating-point types when base_type.float_type is set.

Quoted 7 lines
Quoted 3 lines

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

So older versions of bpftool will fail with a .BTF section having this new float? I thought it would just skip it emitting a warning? Probably not possible as we don't have the record size encoded in a header, right?

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT
[acme@five pahole]$ type pahole
pahole is /home/acme/bin/pahole
[acme@five pahole]$ ls -la ~/bin/pahole
lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole [acme@five pahole]$ pahole -J vmlinux
[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
Error: failed to load BTF from vmlinux: Invalid argument
[acme@five pahole]$

Perhaps the warning emitted by bpftool should suggest updating the tool as it found a record type it doesn't know about?

Quoted 7 lines
Quoted 1 line

/me goes to update bpftool...

Works with the bpftool in bpf-next:

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head [8006] FLOAT 'double' size=8
[acme@five pahole]$

Applied, with this committer notes:

Committer testing:

  $ rm -rf build  # To update the libbpf git submodule
  $ mkdir build
  $ cd build
  $ cmake ..
  $ cd ..
  $ make -C build
  # No BTF_KIND_FLOAT before:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  $ type pahole
  pahole is /home/acme/bin/pahole
  $ ls -la ~/bin/pahole
  lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole
  # Encode BTF:
  $ pahole -J vmlinux
  $ bpftool btf dump file vmlinux | grep -w FLOAT | head
  Error: failed to load BTF from vmlinux: Invalid argument
  $
  # Update bpftool to what is in bpf-next, then try again:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  [8006] FLOAT 'double' size=8
  $
  # Now check that pahole works well, i.e. that the BTF loader works
  $ pahole -F btf vmlinux -C sk_buff_head
  struct sk_buff_head {
        struct sk_buff *           next;                 /*     0     8 */
        struct sk_buff *           prev;                 /*     8     8 */
        __u32                      qlen;                 /*    16     4 */
        spinlock_t                 lock;                 /*    20     4 */
        /* size: 24, cachelines: 1, members: 4 */
        /* last cacheline: 24 bytes */
  };
  $
  $ pahole -F btf vmlinux  | wc -l
  122676
  $
Quoted 2 lines

Now will build a kernel with this new version, reboot, then push publicly.

So now trying to build v5.12-rc2 with pahole supporting BTF_KIND_FLOAT:

  AS      .tmp_vmlinux.kallsyms2.S
  LD      vmlinux
  BTFIDS  vmlinux

FAILED: load BTF from vmlinux: Invalid argument
make[1]: *** [/home/acme/git/linux/Makefile:1197: vmlinux] Error 255 make[1]: Leaving directory '/home/acme/git/build/v5.12.0-rc2'
make: *** [Makefile:215: __sub-make] Error 2
[acme@five linux]$

[acme@five linux]$ egrep BTF\|DWARF ../build/v5.12.0-rc2/.config CONFIG_VIDEO_SONY_BTF_MPX=m CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
# CONFIG_DEBUG_INFO_DWARF4 is not set CONFIG_DEBUG_INFO_BTF=y
CONFIG_PAHOLE_HAS_SPLIT_BTF=y CONFIG_DEBUG_INFO_BTF_MODULES=y
[acme@five linux]$

Ideas?

- Arnaldo

#6 lore raw
Quoted 139 lines · Arnaldo Carvalho de Melo

Adding Jiri to the CC list.

Em Sun, Mar 07, 2021 at 10:50:19AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 108 lines

Em Sun, Mar 07, 2021 at 10:44:21AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 55 lines

Em Sun, Mar 07, 2021 at 10:16:31AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 28 lines

Em Sat, Mar 06, 2021 at 07:16:08PM -0800, Andrii Nakryiko escreveu:

Quoted 26 lines
Quoted 16 lines · Ilya Leoshkevich

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

Fix as follows:

- Make DWARF loader fill base_type.float_type.

- libbpf introduced support for the floating-point types in commit
  986962fade5, so update the libbpf submodule to that version and use
  the new btf__add_float() function in order to emit the floating-point
  types when base_type.float_type is set.

Quoted 3 lines

Example of the resulting entry in the vmlinux BTF:

    [7164] FLOAT 'double' size=8

[PATCH dwarves] would make it a bit clearer that this is pahole patch.

But LGTM.

Quoted 7 lines

So older versions of bpftool will fail with a .BTF section having this
new float? I thought it would just skip it emitting a warning? Probably
not possible as we don't have the record size encoded in a header,
right?

Quoted 16 lines

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT [acme@five pahole]$ type pahole
pahole is /home/acme/bin/pahole
[acme@five pahole]$ ls -la ~/bin/pahole
lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole -> /home/acme/git/pahole/build/pahole
[acme@five pahole]$ pahole -J vmlinux
[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
Error: failed to load BTF from vmlinux: Invalid argument [acme@five pahole]$

Perhaps the warning emitted by bpftool should suggest updating the tool
as it found a record type it doesn't know about?

Quoted 8 lines
Quoted 1 line

/me goes to update bpftool...

Works with the bpftool in bpf-next:

[acme@five pahole]$ bpftool btf dump file vmlinux | grep -w FLOAT | head
[8006] FLOAT 'double' size=8
[acme@five pahole]$

Applied, with this committer notes:

Committer testing:

  $ rm -rf build  # To update the libbpf git submodule
  $ mkdir build
  $ cd build
  $ cmake ..
  $ cd ..
  $ make -C build
  # No BTF_KIND_FLOAT before:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  $ type pahole
  pahole is /home/acme/bin/pahole
  $ ls -la ~/bin/pahole
  lrwxrwxrwx. 1 acme acme 34 Jan 29 11:00 /home/acme/bin/pahole ->
/home/acme/git/pahole/build/pahole
  # Encode BTF:
  $ pahole -J vmlinux
  $ bpftool btf dump file vmlinux | grep -w FLOAT | head
  Error: failed to load BTF from vmlinux: Invalid argument
  $
  # Update bpftool to what is in bpf-next, then try again:
  $ bpftool btf dump file vmlinux | grep -w FLOAT
  [8006] FLOAT 'double' size=8
  $
  # Now check that pahole works well, i.e. that the BTF loader works
  $ pahole -F btf vmlinux -C sk_buff_head
  struct sk_buff_head {
        struct sk_buff *           next;                 /*     0    
8 */
        struct sk_buff *           prev;                 /*     8    
8 */
        __u32                      qlen;                 /*    16    
4 */
        spinlock_t                 lock;                 /*    20    
4 */

        /* size: 24, cachelines: 1, members: 4 */
        /* last cacheline: 24 bytes */
  };
  $

Quoted 6 lines
  $ pahole -F btf vmlinux  | wc -l
  122676
  $

Now will build a kernel with this new version, reboot, then push publicly.

So now trying to build v5.12-rc2 with pahole supporting BTF_KIND_FLOAT:

  AS      .tmp_vmlinux.kallsyms2.S
  LD      vmlinux
  BTFIDS  vmlinux
FAILED: load BTF from vmlinux: Invalid argument
make[1]: *** [/home/acme/git/linux/Makefile:1197: vmlinux] Error 255 make[1]: Leaving directory '/home/acme/git/build/v5.12.0-rc2'
make: *** [Makefile:215: __sub-make] Error 2
[acme@five linux]$

[acme@five linux]$ egrep BTF\|DWARF  ../build/v5.12.0-rc2/.config CONFIG_VIDEO_SONY_BTF_MPX=m CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
# CONFIG_DEBUG_INFO_DWARF4 is not set CONFIG_DEBUG_INFO_BTF=y
CONFIG_PAHOLE_HAS_SPLIT_BTF=y CONFIG_DEBUG_INFO_BTF_MODULES=y
[acme@five linux]$

Quoted 3 lines

Ideas?

- Arnaldo

So v5.12-rc2 does not have this series yet:

https://lore.kernel.org/bpf/[email protected]/

pahole generates a BTF_KIND_FLOAT, but libbpf from v5.12-rc2 doesn't know how to handle it and resolve_btfids fails.

I guess this is the first time a new BTF kind is added? I checked the history, and kernel v5.2, which introduced DEBUG_INFO_BTF, already had BTF_KIND_DATASEC.

So should I add a command-line option to pahole, which would tell it the desired libbpf compatibility level?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 41 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 08, 2021 at 04:02:58AM +0100, Ilya Leoshkevich escreveu:

Quoted 38 lines · Arnaldo Carvalho de Melo
Quoted 25 lines

Adding Jiri to the CC list.
Em Sun, Mar 07, 2021 at 10:50:19AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 3 lines

Em Sun, Mar 07, 2021 at 10:44:21AM -0300, Arnaldo Carvalho de Melo escreveu:
Now will build a kernel with this new version, reboot, then push publicly.

So now trying to build v5.12-rc2 with pahole supporting BTF_KIND_FLOAT:

  AS      .tmp_vmlinux.kallsyms2.S
  LD      vmlinux
  BTFIDS  vmlinux

FAILED: load BTF from vmlinux: Invalid argument
make[1]: *** [/home/acme/git/linux/Makefile:1197: vmlinux] Error 255 make[1]: Leaving directory '/home/acme/git/build/v5.12.0-rc2'
make: *** [Makefile:215: __sub-make] Error 2
[acme@five linux]$

[acme@five linux]$ egrep BTF\|DWARF ../build/v5.12.0-rc2/.config CONFIG_VIDEO_SONY_BTF_MPX=m CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
# CONFIG_DEBUG_INFO_DWARF4 is not set CONFIG_DEBUG_INFO_BTF=y
CONFIG_PAHOLE_HAS_SPLIT_BTF=y CONFIG_DEBUG_INFO_BTF_MODULES=y

Quoted 1 line

Ideas?

So v5.12-rc2 does not have this series yet:

https://lore.kernel.org/bpf/[email protected]/

pahole generates a BTF_KIND_FLOAT, but libbpf from v5.12-rc2 doesn't know how to handle it and resolve_btfids fails.

I guess this is the first time a new BTF kind is added? I checked the history, and kernel v5.2, which introduced DEBUG_INFO_BTF, already had BTF_KIND_DATASEC.

Quoted 5 lines
Quoted 2 lines

So should I add a command-line option to pahole, which would tell it the desired libbpf compatibility level?

Yes, that would be best, some sort of capability querying and then a decision about using the new feature.

pahole could be used to add .BTF post-factum to vmlinux image of a very old kernel, even the one that doesn't support BTF at all. So whatever detection system is going to be added, we should make it easy to turn it off.

Quoted 2 lines

- Arnaldo

#9 lore raw
Quoted 62 lines · Andrii Nakryiko
Quoted 52 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 08, 2021 at 04:02:58AM +0100, Ilya Leoshkevich escreveu:

Quoted 48 lines
Quoted 31 lines · Arnaldo Carvalho de Melo

Adding Jiri to the CC list.
Em Sun, Mar 07, 2021 at 10:50:19AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 5 lines

Em Sun, Mar 07, 2021 at 10:44:21AM -0300, Arnaldo Carvalho de Melo escreveu:
Now will build a kernel with this new version, reboot, then push
publicly.

So now trying to build v5.12-rc2 with pahole supporting BTF_KIND_FLOAT:

  AS      .tmp_vmlinux.kallsyms2.S
  LD      vmlinux
  BTFIDS  vmlinux
FAILED: load BTF from vmlinux: Invalid argument make[1]: *** [/home/acme/git/linux/Makefile:1197: vmlinux] Error 255
make[1]: Leaving directory '/home/acme/git/build/v5.12.0-rc2'
make: *** [Makefile:215: __sub-make] Error 2 [acme@five linux]$

[acme@five linux]$ egrep BTF\|DWARF  ../build/v5.12.0- rc2/.config
CONFIG_VIDEO_SONY_BTF_MPX=m CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
# CONFIG_DEBUG_INFO_DWARF4 is not set CONFIG_DEBUG_INFO_BTF=y CONFIG_PAHOLE_HAS_SPLIT_BTF=y CONFIG_DEBUG_INFO_BTF_MODULES=y

Quoted 1 line

Ideas?

So v5.12-rc2 does not have this series yet:

https://lore.kernel.org/bpf/[email protected]/

pahole generates a BTF_KIND_FLOAT, but libbpf from v5.12-rc2 doesn't
know how to handle it and resolve_btfids fails.

I guess this is the first time a new BTF kind is added? I checked the
history, and kernel v5.2, which introduced DEBUG_INFO_BTF, already had
BTF_KIND_DATASEC.

Quoted 7 lines
Quoted 3 lines

So should I add a command-line option to pahole, which would tell it
the desired libbpf compatibility level?

Yes, that would be best, some sort of capability querying and then a
decision about using the new feature.

Quoted 5 lines

pahole could be used to add .BTF post-factum to vmlinux image of a very old kernel, even the one that doesn't support BTF at all. So whatever detection system is going to be added, we should make it easy
to turn it off.

I'd rather not have detection at all. Instead, pahole should encode floats as ints by default (that's not fully correct, but that's the way it is today). Then we can pass --libbpf-compat=0.4.0 in link-vmlinux.sh in the newer kernels, which would turn on the new feature.