diff mbox series

[bpf-next,1/2] bpftool: fix newline for struct with padding only fields

Message ID 20220930164918.342310-2-eddyz87@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpftool: fix newline for struct with padding only fields | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: sdf@google.com john.fastabend@gmail.com yhs@fb.com haoluo@google.com jolsa@kernel.org kpsingh@kernel.org song@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 88 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16

Commit Message

Eduard Zingerman Sept. 30, 2022, 4:49 p.m. UTC
An update for `bpftool btf dump file ... format c`.
Add a missing newline print for structures that consist of
anonymous-only padding fields. 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;
 };

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 tools/lib/bpf/btf_dump.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

Comments

Andrii Nakryiko Sept. 30, 2022, 11:01 p.m. UTC | #1
On Fri, Sep 30, 2022 at 9:50 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> An update for `bpftool btf dump file ... format c`.
> Add a missing newline print for structures that consist of
> anonymous-only padding fields. 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;
>  };

Without looking at source code it wasn't clear what the original issue
was. It would be good to explain that libbpf's btf_dumper attempts to
emit empty structs with {} on the same line. But this breaks for
non-zero-sized structs due to padding.

>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---
>  tools/lib/bpf/btf_dump.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index 4221f73a74d0..ebbba19ac122 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -852,7 +852,7 @@ static int chip_away_bits(int total, int at_most)
>         return total % at_most ? : at_most;
>  }
>
> -static void btf_dump_emit_bit_padding(const struct btf_dump *d,
> +static bool btf_dump_emit_bit_padding(const struct btf_dump *d,
>                                       int cur_off, int m_off, int m_bit_sz,
>                                       int align, int lvl)
>  {
> @@ -861,10 +861,10 @@ static void btf_dump_emit_bit_padding(const struct btf_dump *d,
>
>         if (off_diff <= 0)
>                 /* no gap */
> -               return;
> +               return false;
>         if (m_bit_sz == 0 && off_diff < align * 8)
>                 /* natural padding will take care of a gap */
> -               return;
> +               return false;
>
>         while (off_diff > 0) {
>                 const char *pad_type;
> @@ -886,6 +886,8 @@ static void btf_dump_emit_bit_padding(const struct btf_dump *d,
>                 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
>                 off_diff -= pad_bits;
>         }
> +
> +       return true;
>  }
>
>  static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
> @@ -906,6 +908,7 @@ static void btf_dump_emit_struct_def(struct btf_dump *d,
>         bool is_struct = btf_is_struct(t);
>         int align, i, packed, off = 0;
>         __u16 vlen = btf_vlen(t);
> +       bool padding_added = false;
>
>         packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
>
> @@ -940,11 +943,11 @@ static void btf_dump_emit_struct_def(struct btf_dump *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);
> +               padding_added = btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
> +                                                         lvl + 1);
>         }
>
> -       if (vlen)
> +       if (vlen || padding_added)

What if instead of returning the padding_added flag we just check that
struct is non-zero-sized? Clearly vlen isn't an appropriate check
here.

>                 btf_dump_printf(d, "\n");
>         btf_dump_printf(d, "%s}", pfx(lvl));
>         if (packed)
> --
> 2.37.3
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 4221f73a74d0..ebbba19ac122 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -852,7 +852,7 @@  static int chip_away_bits(int total, int at_most)
 	return total % at_most ? : at_most;
 }
 
-static void btf_dump_emit_bit_padding(const struct btf_dump *d,
+static bool btf_dump_emit_bit_padding(const struct btf_dump *d,
 				      int cur_off, int m_off, int m_bit_sz,
 				      int align, int lvl)
 {
@@ -861,10 +861,10 @@  static void btf_dump_emit_bit_padding(const struct btf_dump *d,
 
 	if (off_diff <= 0)
 		/* no gap */
-		return;
+		return false;
 	if (m_bit_sz == 0 && off_diff < align * 8)
 		/* natural padding will take care of a gap */
-		return;
+		return false;
 
 	while (off_diff > 0) {
 		const char *pad_type;
@@ -886,6 +886,8 @@  static void btf_dump_emit_bit_padding(const struct btf_dump *d,
 		btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
 		off_diff -= pad_bits;
 	}
+
+	return true;
 }
 
 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
@@ -906,6 +908,7 @@  static void btf_dump_emit_struct_def(struct btf_dump *d,
 	bool is_struct = btf_is_struct(t);
 	int align, i, packed, off = 0;
 	__u16 vlen = btf_vlen(t);
+	bool padding_added = false;
 
 	packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
 
@@ -940,11 +943,11 @@  static void btf_dump_emit_struct_def(struct btf_dump *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);
+		padding_added = btf_dump_emit_bit_padding(d, off, t->size * 8, 0, align,
+							  lvl + 1);
 	}
 
-	if (vlen)
+	if (vlen || padding_added)
 		btf_dump_printf(d, "\n");
 	btf_dump_printf(d, "%s}", pfx(lvl));
 	if (packed)