Skip to contentSkip to conversation

bpftool: Print newline before '}' for struct with padding only fields

PATCH Applied Eduard Zingerman 2 participants 3 messages Oct 1, 2022 – Oct 5, 2022

Subsystems (4)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH v2 1/2 bpftool: Print newline before '}' for struct with padding only fields

btf_dump_emit_struct_def attempts to print empty structures at a single line, e.g. struct empty {}. However, it has to account for a case when there are no regular but some padding fields in the struct. In such case vlen would be zero, but size would be non-zero.

E.g. here is struct bpf_timer from vmlinux.h before this patch:

struct bpf_timer {

 	long: 64;
	long: 64;};

And after this patch:

struct bpf_dynptr {

 	long: 64;
	long: 64;

};

---
 tools/lib/bpf/btf_dump.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +5 -1
index 4221f73a74d0..e4da6de68d8f 100644
@@ -944,7 +944,11 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
 					  lvl + 1);
 	}
 
-	if (vlen)
+	/*
+	 * Keep `struct empty {}` on a single line,
+	 * only print newline when there are regular or padding fields.
+	 */
+	if (vlen || t->size)
 		btf_dump_printf(d, "\n");
 	btf_dump_printf(d, "%s}", pfx(lvl));
 	if (packed)
Signature · 1 line
2.37.3
author maintainer
#1 lore raw

PATCH v2 2/2 selftests/bpf: Test btf dump for struct with padding only fields

Structures with zero regular fields but some padding constitute a special case in btf_dump.c:btf_dump_emit_struct_def with regards to newline before closing '}'.

---
 .../selftests/bpf/progs/btf_dump_test_case_padding.c     | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/testing/selftests/bpf/progs/btf_dump_test_case_padding.c +9 -0
index f2661c8d2d90..7cb522d22a66 100644
@@ -102,12 +102,21 @@ struct zone {
 	struct zone_padding __pad__;
 };
 
+/* ----- START-EXPECTED-OUTPUT ----- */
+struct padding_wo_named_members {
+	long: 64;
+	long: 64;
+};
+
+/* ------ END-EXPECTED-OUTPUT ------ */
+
 int f(struct {
 	struct padded_implicitly _1;
 	struct padded_explicitly _2;
 	struct padded_a_lot _3;
 	struct padded_cache_line _4;
 	struct zone _5;
+	struct padding_wo_named_members _6;
 } *_)
 {
 	return 0;
Signature · 1 line
2.37.3
#2 lore raw
In reply to Eduard Zingerman
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Sat, 1 Oct 2022 13:44:24 +0300 you wrote:

btf_dump_emit_struct_def attempts to print empty structures at a single line, e.g. struct empty {}. However, it has to account for a case when there are no regular but some padding fields in the struct. In such case vlen would be zero, but size would be non-zero.

E.g. here is struct bpf_timer from vmlinux.h before this patch:

[...]

Here is the summary with links:
- [bpf-next,v2,1/2] bpftool: Print newline before '}' for struct with padding only fields

    https://git.kernel.org/bpf/bpf-next/c/44a726c3f23c
  - [bpf-next,v2,2/2] selftests/bpf: Test btf dump for struct with padding only fields
    https://git.kernel.org/bpf/bpf-next/c/d503f1176b14

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html