Skip to contentSkip to conversation

Fix BTF-to-C converter's padding generation

PATCH In review Andrii Nakryiko 4 participants 7 messages Oct 8, 2019 – Oct 9, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover Fix BTF-to-C converter's padding generation

Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move test_btf_dump into a test_progs test to leverage common infrastructure.

Andrii Nakryiko (3):
libbpf: fix struct end padding in btf_dump selftests/bpf: convert test_btf_dump into test_progs test selftests/bpf: fix btf_dump padding test case

 tools/lib/bpf/btf_dump.c                      |  8 +-
 tools/testing/selftests/bpf/Makefile          |  2 +-
 .../btf_dump.c}                               | 88 +++++++------------
 .../bpf/progs/btf_dump_test_case_padding.c    |  5 +-
 4 files changed, 46 insertions(+), 57 deletions(-)
 rename tools/testing/selftests/bpf/{test_btf_dump.c => prog_tests/btf_dump.c} (51%)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 libbpf: fix struct end padding in btf_dump

Fix a case where explicit padding at the end of a struct is necessary due to non-standart alignment requirements of fields (which BTF doesn't capture explicitly).

Fixes 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
---
 tools/lib/bpf/btf_dump.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +7 -1
index ede55fec3618..87f27e2664c5 100644
@@ -876,7 +876,6 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
 	__u16 vlen = btf_vlen(t);
 
 	packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
-	align = packed ? 1 : btf_align_of(d->btf, id);
 
 	btf_dump_printf(d, "%s%s%s {",
 			is_struct ? "struct" : "union",
@@ -906,6 +905,13 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
 		btf_dump_printf(d, ";");
 	}
 
+	/* pad at the end, if necessary */
+	if (is_struct) {
+		align = packed ? 1 : btf_align_of(d->btf, id);
+		btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
+					  lvl + 1);
+	}
+
 	if (vlen)
 		btf_dump_printf(d, "\n");
 	btf_dump_printf(d, "%s}", pfx(lvl));
Signature · 1 line
2.17.1
Tested-by on v1 1/3
#2 lore raw

RE: [PATCH bpf-next 1/3] libbpf: fix struct end padding in btf_dump

Quoted 8 lines · Andrii Nakryiko

Fix a case where explicit padding at the end of a struct is necessary due to non-standart alignment requirements of fields (which BTF doesn't capture explicitly).

Fixes 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")

---

Tested a few different kernels and seems to have resolved the issue I was seeing.

#3 lore raw

PATCH 2/3 selftests/bpf: convert test_btf_dump into test_progs test

In reply to Andrii Nakryiko

Convert test_btf_dump into a part of test_progs, instead of a stand-alone test binary.

---
 tools/testing/selftests/bpf/Makefile          |  2 +-
 .../btf_dump.c}                               | 88 +++++++------------
 2 files changed, 35 insertions(+), 55 deletions(-)
 rename tools/testing/selftests/bpf/{test_btf_dump.c => prog_tests/btf_dump.c} (51%)
tools/testing/selftests/bpf/Makefile +1 -1
index 771a4e82128b..0de5d6eb62de 100644
@@ -29,7 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
 	test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
 	test_cgroup_storage test_select_reuseport test_section_names \
 	test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
-	test_btf_dump test_cgroup_attach xdping
+	test_cgroup_attach xdping
 
 BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
 TEST_GEN_FILES = $(BPF_OBJ_FILES)
tools/testing/selftests/bpf/prog_tests/btf_dump.c renamed from tools/testing/selftests/bpf/test_btf_dump.c +34 -54

158 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

PATCH 3/3 selftests/bpf: fix btf_dump padding test case

In reply to Andrii Nakryiko

Existing padding test case for btf_dump has a good test that was supposed to test padding generation at the end of a struct, but its expected output was specified incorrectly. Fix this.

Fixes 2d2a3ad872f8 ("selftests/bpf: add btf_dump BTF-to-C conversion tests")
---
 .../testing/selftests/bpf/progs/btf_dump_test_case_padding.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/btf_dump_test_case_padding.c +4 -1
index 3a62119c7498..35c512818a56 100644
@@ -62,6 +62,10 @@ struct padded_a_lot {
  *	long: 64;
  *	long: 64;
  *	int b;
+ *	long: 32;
+ *	long: 64;
+ *	long: 64;
+ *	long: 64;
  *};
  *
  */
@@ -95,7 +99,6 @@ struct zone_padding {
 struct zone {
 	int a;
 	short b;
-	short: 16;
 	struct zone_padding __pad__;
 };
 
Signature · 1 line
2.17.1
#5 lore raw
In reply to Andrii Nakryiko
Quoted 4 lines · Andrii Nakryiko

Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move test_btf_dump into a test_progs test to leverage common infrastructure.

Applied.
But I see some build weirdness.
Probably due to some previous patches.
Parallel build in selftests/bpf/ is not always succeeding.
For this particular set. New progs/*.c files failed to build the first time.

#6 lore raw
Quoted 7 lines · Alexei Starovoitov
Quoted 4 lines · Andrii Nakryiko

Fix BTF-to-C logic of handling padding at the end of a struct. Fix existing test that should have captured this. Also move test_btf_dump into a test_progs test to leverage common infrastructure.

Quoted 5 lines

Applied.
But I see some build weirdness.
Probably due to some previous patches.
Parallel build in selftests/bpf/ is not always succeeding.
For this particular set. New progs/*.c files failed to build the first time.

I think it's another problem with test_attach_probe.c - it depends on libbpf's auto-generated bpf_helper_defs.h, which is not enforced to happen before progs/*.c are built. I think it's time to do some selftests/bpf/Makefile overhaul, honestly. But I'll try to come up with a quick fix for this particular issue as well.