diff mbox series

[bpf-next,3/3] libbpf: Fix dumping __int128

Message ID 20211012023218.399568-4-iii@linux.ibm.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series btf_dump fixes for s390 | expand

Checks

Context Check Description
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary
netdev/cover_letter success Series has a cover letter
netdev/fixes_present success Fixes tag not required for -next series
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 7 maintainers not CCed: andrii@kernel.org songliubraving@fb.com yhs@fb.com john.fastabend@gmail.com kafai@fb.com netdev@vger.kernel.org kpsingh@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success No Fixes tag
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 23 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success No static functions without inline keyword in header files

Commit Message

Ilya Leoshkevich Oct. 12, 2021, 2:32 a.m. UTC
On s390 __int128 can be 8-byte aligned, therefore in libbpf will
occasionally consider variables of this type non-aligned and try to
dump them as a bitfield, which is supported for at most 64-bit
integers.

Fix by using the same trick as btf_dump_float_data(): copy non-aligned
values to the local buffer.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 tools/lib/bpf/btf_dump.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Andrii Nakryiko Oct. 12, 2021, 3:52 a.m. UTC | #1
On Tue, Oct 12, 2021 at 4:32 AM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> On s390 __int128 can be 8-byte aligned, therefore in libbpf will
> occasionally consider variables of this type non-aligned and try to
> dump them as a bitfield, which is supported for at most 64-bit
> integers.
>
> Fix by using the same trick as btf_dump_float_data(): copy non-aligned
> values to the local buffer.
>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>  tools/lib/bpf/btf_dump.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index ab45771d0cb4..d8264c1762e8 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -1672,9 +1672,10 @@ static int btf_dump_int_data(struct btf_dump *d,
>  {
>         __u8 encoding = btf_int_encoding(t);
>         bool sign = encoding & BTF_INT_SIGNED;
> +       char buf[16] __aligned(16);
>         int sz = t->size;
>
> -       if (sz == 0) {
> +       if (sz == 0 || sz > sizeof(buf)) {
>                 pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
>                 return -EINVAL;
>         }
> @@ -1682,8 +1683,10 @@ static int btf_dump_int_data(struct btf_dump *d,
>         /* handle packed int data - accesses of integers not aligned on
>          * int boundaries can cause problems on some platforms.
>          */
> -       if (!ptr_is_aligned(data, sz))
> -               return btf_dump_bitfield_data(d, t, data, 0, 0);
> +       if (!ptr_is_aligned(data, sz)) {

I think ptr_is_aligned() logic is wrong. We should probably not assume
that __int128 has 16-byte alignment. Can you try fixing it by using
btf__align_of() API to get the natural alignment?

> +               memcpy(buf, data, sz);
> +               data = buf;
> +       }
>
>         switch (sz) {
>         case 16: {
> --
> 2.31.1
>
Ilya Leoshkevich Oct. 12, 2021, 11:44 a.m. UTC | #2
On Tue, 2021-10-12 at 05:52 +0200, Andrii Nakryiko wrote:
> On Tue, Oct 12, 2021 at 4:32 AM Ilya Leoshkevich <iii@linux.ibm.com>
> wrote:
> > 
> > On s390 __int128 can be 8-byte aligned, therefore in libbpf will
> > occasionally consider variables of this type non-aligned and try to
> > dump them as a bitfield, which is supported for at most 64-bit
> > integers.
> > 
> > Fix by using the same trick as btf_dump_float_data(): copy non-
> > aligned
> > values to the local buffer.
> > 
> > Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > ---
> >  tools/lib/bpf/btf_dump.c | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> > 
> > diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> > index ab45771d0cb4..d8264c1762e8 100644
> > --- a/tools/lib/bpf/btf_dump.c
> > +++ b/tools/lib/bpf/btf_dump.c
> > @@ -1672,9 +1672,10 @@ static int btf_dump_int_data(struct btf_dump
> > *d,
> >  {
> >         __u8 encoding = btf_int_encoding(t);
> >         bool sign = encoding & BTF_INT_SIGNED;
> > +       char buf[16] __aligned(16);
> >         int sz = t->size;
> > 
> > -       if (sz == 0) {
> > +       if (sz == 0 || sz > sizeof(buf)) {
> >                 pr_warn("unexpected size %d for id [%u]\n", sz,
> > type_id);
> >                 return -EINVAL;
> >         }
> > @@ -1682,8 +1683,10 @@ static int btf_dump_int_data(struct btf_dump
> > *d,
> >         /* handle packed int data - accesses of integers not
> > aligned on
> >          * int boundaries can cause problems on some platforms.
> >          */
> > -       if (!ptr_is_aligned(data, sz))
> > -               return btf_dump_bitfield_data(d, t, data, 0, 0);
> > +       if (!ptr_is_aligned(data, sz)) {
> 
> I think ptr_is_aligned() logic is wrong. We should probably not
> assume
> that __int128 has 16-byte alignment. Can you try fixing it by using
> btf__align_of() API to get the natural alignment?

Ok, but this patch makes sense anyway, doesn't it? I can fix
ptr_is_aligned() separately.

> 
> > +               memcpy(buf, data, sz);
> > +               data = buf;
> > +       }
> > 
> >         switch (sz) {
> >         case 16: {
> > --
> > 2.31.1
> >
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index ab45771d0cb4..d8264c1762e8 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -1672,9 +1672,10 @@  static int btf_dump_int_data(struct btf_dump *d,
 {
 	__u8 encoding = btf_int_encoding(t);
 	bool sign = encoding & BTF_INT_SIGNED;
+	char buf[16] __aligned(16);
 	int sz = t->size;
 
-	if (sz == 0) {
+	if (sz == 0 || sz > sizeof(buf)) {
 		pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
 		return -EINVAL;
 	}
@@ -1682,8 +1683,10 @@  static int btf_dump_int_data(struct btf_dump *d,
 	/* handle packed int data - accesses of integers not aligned on
 	 * int boundaries can cause problems on some platforms.
 	 */
-	if (!ptr_is_aligned(data, sz))
-		return btf_dump_bitfield_data(d, t, data, 0, 0);
+	if (!ptr_is_aligned(data, sz)) {
+		memcpy(buf, data, sz);
+		data = buf;
+	}
 
 	switch (sz) {
 	case 16: {