All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue
@ 2018-11-28 17:38 Yonghong Song
  2018-11-28 23:28 ` Song Liu
  2018-11-29  0:42 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Yonghong Song @ 2018-11-28 17:38 UTC (permalink / raw)
  To: ast, daniel, netdev, kafai; +Cc: kernel-team

Commit b12d6ec09730 ("bpf: btf: add btf print functionality")
added btf pretty print functionality to bpftool.
There is a problem though in printing a bitfield whose type
has modifiers.

For example, for a type like
  typedef int ___int;
  struct tmp_t {
          int a:3;
          ___int b:3;
  };
Suppose we have a map
  struct bpf_map_def SEC("maps") tmpmap = {
          .type = BPF_MAP_TYPE_HASH,
          .key_size = sizeof(__u32),
          .value_size = sizeof(struct tmp_t),
          .max_entries = 1,
  };
and the hash table is populated with one element with
key 0 and value (.a = 1 and .b = 2).

In BTF, the struct member "b" will have a type "typedef" which
points to an int type. The current implementation does not
pass the bit offset during transition from typedef to int type,
hence incorrectly print the value as
  $ bpftool m d id 79
  [{
          "key": 0,
          "value": {
              "a": 0x1,
              "b": 0x1
          }
      }
  ]

This patch fixed the issue by carrying bit_offset along the type
chain during bit_field print. The correct result can be printed as
  $ bpftool m d id 76
  [{
          "key": 0,
          "value": {
              "a": 0x1,
              "b": 0x2
          }
      }
  ]

The kernel pretty print is implemented correctly and does not
have this issue.

Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/bpf/bpftool/btf_dumper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index 55bc512a1831..e4e6e2b3fd84 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -32,7 +32,7 @@ static void btf_dumper_ptr(const void *data, json_writer_t *jw,
 }
 
 static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
-			       const void *data)
+			       __u8 bit_offset, const void *data)
 {
 	int actual_type_id;
 
@@ -40,7 +40,7 @@ static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
 	if (actual_type_id < 0)
 		return actual_type_id;
 
-	return btf_dumper_do_type(d, actual_type_id, 0, data);
+	return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
 }
 
 static void btf_dumper_enum(const void *data, json_writer_t *jw)
@@ -237,7 +237,7 @@ static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_CONST:
 	case BTF_KIND_RESTRICT:
-		return btf_dumper_modifier(d, type_id, data);
+		return btf_dumper_modifier(d, type_id, bit_offset, data);
 	default:
 		jsonw_printf(d->jw, "(unsupported-kind");
 		return -EINVAL;
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue
  2018-11-28 17:38 [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue Yonghong Song
@ 2018-11-28 23:28 ` Song Liu
  2018-11-29  0:42 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Song Liu @ 2018-11-28 23:28 UTC (permalink / raw)
  To: yhs; +Cc: ast, Daniel Borkmann, Networking, Martin KaFai Lau, kernel-team

On Wed, Nov 28, 2018 at 10:09 AM Yonghong Song <yhs@fb.com> wrote:
>
> Commit b12d6ec09730 ("bpf: btf: add btf print functionality")
> added btf pretty print functionality to bpftool.
> There is a problem though in printing a bitfield whose type
> has modifiers.
>
> For example, for a type like
>   typedef int ___int;
>   struct tmp_t {
>           int a:3;
>           ___int b:3;
>   };
> Suppose we have a map
>   struct bpf_map_def SEC("maps") tmpmap = {
>           .type = BPF_MAP_TYPE_HASH,
>           .key_size = sizeof(__u32),
>           .value_size = sizeof(struct tmp_t),
>           .max_entries = 1,
>   };
> and the hash table is populated with one element with
> key 0 and value (.a = 1 and .b = 2).
>
> In BTF, the struct member "b" will have a type "typedef" which
> points to an int type. The current implementation does not
> pass the bit offset during transition from typedef to int type,
> hence incorrectly print the value as
>   $ bpftool m d id 79
>   [{
>           "key": 0,
>           "value": {
>               "a": 0x1,
>               "b": 0x1
>           }
>       }
>   ]
>
> This patch fixed the issue by carrying bit_offset along the type
> chain during bit_field print. The correct result can be printed as
>   $ bpftool m d id 76
>   [{
>           "key": 0,
>           "value": {
>               "a": 0x1,
>               "b": 0x2
>           }
>       }
>   ]
>
> The kernel pretty print is implemented correctly and does not
> have this issue.
>
> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> Signed-off-by: Yonghong Song <yhs@fb.com>

Acked-by: Song Liu <songliubraving@fb.com>

> ---
>  tools/bpf/bpftool/btf_dumper.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> index 55bc512a1831..e4e6e2b3fd84 100644
> --- a/tools/bpf/bpftool/btf_dumper.c
> +++ b/tools/bpf/bpftool/btf_dumper.c
> @@ -32,7 +32,7 @@ static void btf_dumper_ptr(const void *data, json_writer_t *jw,
>  }
>
>  static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
> -                              const void *data)
> +                              __u8 bit_offset, const void *data)
>  {
>         int actual_type_id;
>
> @@ -40,7 +40,7 @@ static int btf_dumper_modifier(const struct btf_dumper *d, __u32 type_id,
>         if (actual_type_id < 0)
>                 return actual_type_id;
>
> -       return btf_dumper_do_type(d, actual_type_id, 0, data);
> +       return btf_dumper_do_type(d, actual_type_id, bit_offset, data);
>  }
>
>  static void btf_dumper_enum(const void *data, json_writer_t *jw)
> @@ -237,7 +237,7 @@ static int btf_dumper_do_type(const struct btf_dumper *d, __u32 type_id,
>         case BTF_KIND_VOLATILE:
>         case BTF_KIND_CONST:
>         case BTF_KIND_RESTRICT:
> -               return btf_dumper_modifier(d, type_id, data);
> +               return btf_dumper_modifier(d, type_id, bit_offset, data);
>         default:
>                 jsonw_printf(d->jw, "(unsupported-kind");
>                 return -EINVAL;
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue
  2018-11-28 17:38 [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue Yonghong Song
  2018-11-28 23:28 ` Song Liu
@ 2018-11-29  0:42 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2018-11-29  0:42 UTC (permalink / raw)
  To: Yonghong Song; +Cc: ast, daniel, netdev, kafai, kernel-team

On Wed, Nov 28, 2018 at 09:38:23AM -0800, Yonghong Song wrote:
> Commit b12d6ec09730 ("bpf: btf: add btf print functionality")
> added btf pretty print functionality to bpftool.
> There is a problem though in printing a bitfield whose type
> has modifiers.
> 
> For example, for a type like
>   typedef int ___int;
>   struct tmp_t {
>           int a:3;
>           ___int b:3;
>   };
> Suppose we have a map
>   struct bpf_map_def SEC("maps") tmpmap = {
>           .type = BPF_MAP_TYPE_HASH,
>           .key_size = sizeof(__u32),
>           .value_size = sizeof(struct tmp_t),
>           .max_entries = 1,
>   };
> and the hash table is populated with one element with
> key 0 and value (.a = 1 and .b = 2).
> 
> In BTF, the struct member "b" will have a type "typedef" which
> points to an int type. The current implementation does not
> pass the bit offset during transition from typedef to int type,
> hence incorrectly print the value as
>   $ bpftool m d id 79
>   [{
>           "key": 0,
>           "value": {
>               "a": 0x1,
>               "b": 0x1
>           }
>       }
>   ]
> 
> This patch fixed the issue by carrying bit_offset along the type
> chain during bit_field print. The correct result can be printed as
>   $ bpftool m d id 76
>   [{
>           "key": 0,
>           "value": {
>               "a": 0x1,
>               "b": 0x2
>           }
>       }
>   ]
> 
> The kernel pretty print is implemented correctly and does not
> have this issue.
> 
> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> Signed-off-by: Yonghong Song <yhs@fb.com>

Applied to bpf tree. Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-11-29 11:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-28 17:38 [PATCH bpf] tools: bpftool: fix a bitfield pretty print issue Yonghong Song
2018-11-28 23:28 ` Song Liu
2018-11-29  0:42 ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.