All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef
@ 2021-10-21 19:56 Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 1/5] bpf: add BTF_KIND_DECL_TAG typedef support Yonghong Song
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Latest upstream llvm-project added support for btf_decl_tag attributes
for typedef declarations ([1], [2]). Similar to other btf_decl_tag cases,
func/func_param/global_var/struct/union/field, btf_decl_tag with typedef
declaration can carry information from kernel source to clang compiler
and then to dwarf/BTF, for bpf verification or other use cases.

This patch set added kernel support for BTF_KIND_DECL_TAG to typedef
declaration (Patch 1). Additional selftests are added to cover
unit testing, dedup, or bpf program usage of btf_decl_tag with typedef.
(Patches 2, 3 and 4). The btf documentation is updated to include
BTF_KIND_DECL_TAG typedef (Patch 5).

  [1] https://reviews.llvm.org/D110127
  [2] https://reviews.llvm.org/D112259

Yonghong Song (5):
  bpf: add BTF_KIND_DECL_TAG typedef support
  selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests
  selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef
  selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c
  docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support

 Documentation/bpf/btf.rst                    |  6 +-
 kernel/bpf/btf.c                             |  4 +-
 tools/testing/selftests/bpf/prog_tests/btf.c | 83 ++++++++++++++++++--
 tools/testing/selftests/bpf/progs/tag.c      |  9 ++-
 4 files changed, 89 insertions(+), 13 deletions(-)

-- 
2.30.2


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

* [PATCH bpf-next 1/5] bpf: add BTF_KIND_DECL_TAG typedef support
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
@ 2021-10-21 19:56 ` Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 2/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests Yonghong Song
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

The llvm patches ([1], [2]) added support to attach btf_decl_tag
attributes to typedef declarations. This patch added
support in kernel.

  [1] https://reviews.llvm.org/D110127
  [2] https://reviews.llvm.org/D112259

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 kernel/bpf/btf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 9059053088b9..dbc3ad07e21b 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -468,7 +468,7 @@ static bool btf_type_is_decl_tag(const struct btf_type *t)
 static bool btf_type_is_decl_tag_target(const struct btf_type *t)
 {
 	return btf_type_is_func(t) || btf_type_is_struct(t) ||
-	       btf_type_is_var(t);
+	       btf_type_is_var(t) || btf_type_is_typedef(t);
 }
 
 u32 btf_nr_types(const struct btf *btf)
@@ -3885,7 +3885,7 @@ static int btf_decl_tag_resolve(struct btf_verifier_env *env,
 
 	component_idx = btf_type_decl_tag(t)->component_idx;
 	if (component_idx != -1) {
-		if (btf_type_is_var(next_type)) {
+		if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) {
 			btf_verifier_log_type(env, v->t, "Invalid component_idx");
 			return -EINVAL;
 		}
-- 
2.30.2


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

* [PATCH bpf-next 2/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 1/5] bpf: add BTF_KIND_DECL_TAG typedef support Yonghong Song
@ 2021-10-21 19:56 ` Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 3/5] selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef Yonghong Song
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Test good and bad variants of typedef BTF_KIND_DECL_TAG encoding.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 36 ++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index fa67f25bbef5..a00418b8b252 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -3903,6 +3903,42 @@ static struct btf_raw_test raw_tests[] = {
 	.btf_load_err = true,
 	.err_str = "Invalid component_idx",
 },
+{
+	.descr = "decl_tag test #13, typedef, well-formed",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
+		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0t\0tag"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "tag_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 1,
+	.max_entries = 1,
+},
+{
+	.descr = "decl_tag test #14, typedef, invalid component_idx",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
+		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0local\0tag"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "tag_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 1,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "Invalid component_idx",
+},
 
 }; /* struct btf_raw_test raw_tests[] */
 
-- 
2.30.2


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

* [PATCH bpf-next 3/5] selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 1/5] bpf: add BTF_KIND_DECL_TAG typedef support Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 2/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests Yonghong Song
@ 2021-10-21 19:56 ` Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 4/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c Yonghong Song
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Add unit tests for deduplication of BTF_KIND_DECL_TAG to typedef types.
Also changed a few comments from "tag" to "decl_tag" to match
BTF_KIND_DECL_TAG enum value name.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 47 +++++++++++++++++---
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index a00418b8b252..3477f272560f 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -6877,11 +6877,12 @@ const struct btf_dedup_test dedup_tests[] = {
 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
 			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
 			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
-			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] tag */
-			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
 	},
 	.expect = {
 		.raw_types = {
@@ -6905,11 +6906,12 @@ const struct btf_dedup_test dedup_tests[] = {
 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
 			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
 			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
-			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] tag */
-			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
+			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
 	},
 	.opts = {
 		.dont_resolve_fwds = false,
@@ -7204,6 +7206,39 @@ const struct btf_dedup_test dedup_tests[] = {
 		.dont_resolve_fwds = false,
 	},
 },
+{
+	.descr = "dedup: typedef tags",
+	.input = {
+		.raw_types = {
+			/* int */
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [3] */
+			/* tag -> t: tag1, tag2 */
+			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [4] */
+			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [5] */
+			/* tag -> t: tag1, tag3 */
+			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),		/* [6] */
+			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [7] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
+			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [3] */
+			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [4] */
+			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [5] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
+	},
+	.opts = {
+		.dont_resolve_fwds = false,
+	},
+},
 
 };
 
-- 
2.30.2


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

* [PATCH bpf-next 4/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
                   ` (2 preceding siblings ...)
  2021-10-21 19:56 ` [PATCH bpf-next 3/5] selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef Yonghong Song
@ 2021-10-21 19:56 ` Yonghong Song
  2021-10-21 19:56 ` [PATCH bpf-next 5/5] docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support Yonghong Song
  2021-10-23  0:05 ` [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Alexei Starovoitov
  5 siblings, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Change value type in progs/tag.c to a typedef with a btf_decl_tag.
With `bpftool btf dump file tag.o`, we have
  ...
  [14] TYPEDEF 'value_t' type_id=17
  [15] DECL_TAG 'tag1' type_id=14 component_idx=-1
  [16] DECL_TAG 'tag2' type_id=14 component_idx=-1
  [17] STRUCT '(anon)' size=8 vlen=2
        'a' type_id=2 bits_offset=0
        'b' type_id=2 bits_offset=32
  ...

The btf_tag selftest also succeeded:
  $ ./test_progs -t tag
    #21 btf_tag:OK
    Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/progs/tag.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/tag.c b/tools/testing/selftests/bpf/progs/tag.c
index 672d19e7b120..1792f4eda095 100644
--- a/tools/testing/selftests/bpf/progs/tag.c
+++ b/tools/testing/selftests/bpf/progs/tag.c
@@ -24,18 +24,23 @@ struct key_t {
 	int c;
 } __tag1 __tag2;
 
+typedef struct {
+	int a;
+	int b;
+} value_t __tag1 __tag2;
+
 struct {
 	__uint(type, BPF_MAP_TYPE_HASH);
 	__uint(max_entries, 3);
 	__type(key, struct key_t);
-	__type(value, __u64);
+	__type(value, value_t);
 } hashmap1 SEC(".maps");
 
 
 static __noinline int foo(int x __tag1 __tag2) __tag1 __tag2
 {
 	struct key_t key;
-	__u64 val = 1;
+	value_t val = {};
 
 	key.a = key.b = key.c = x;
 	bpf_map_update_elem(&hashmap1, &key, &val, 0);
-- 
2.30.2


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

* [PATCH bpf-next 5/5] docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
                   ` (3 preceding siblings ...)
  2021-10-21 19:56 ` [PATCH bpf-next 4/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c Yonghong Song
@ 2021-10-21 19:56 ` Yonghong Song
  2021-10-23  0:05 ` [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Alexei Starovoitov
  5 siblings, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2021-10-21 19:56 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Add BTF_KIND_DECL_TAG typedef support in btf.rst.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 Documentation/bpf/btf.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Documentation/bpf/btf.rst b/Documentation/bpf/btf.rst
index 9e5b4a98af76..9ad4218a751f 100644
--- a/Documentation/bpf/btf.rst
+++ b/Documentation/bpf/btf.rst
@@ -474,7 +474,7 @@ No additional type data follow ``btf_type``.
  * ``info.kind_flag``: 0
  * ``info.kind``: BTF_KIND_DECL_TAG
  * ``info.vlen``: 0
- * ``type``: ``struct``, ``union``, ``func`` or ``var``
+ * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef``
 
 ``btf_type`` is followed by ``struct btf_decl_tag``.::
 
@@ -483,8 +483,8 @@ No additional type data follow ``btf_type``.
     };
 
 The ``name_off`` encodes btf_decl_tag attribute string.
-The ``type`` should be ``struct``, ``union``, ``func`` or ``var``.
-For ``var`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
+The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``.
+For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``.
 For the other three types, if the btf_decl_tag attribute is
 applied to the ``struct``, ``union`` or ``func`` itself,
 ``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
-- 
2.30.2


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

* Re: [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef
  2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
                   ` (4 preceding siblings ...)
  2021-10-21 19:56 ` [PATCH bpf-next 5/5] docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support Yonghong Song
@ 2021-10-23  0:05 ` Alexei Starovoitov
  5 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2021-10-23  0:05 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Kernel Team

On Thu, Oct 21, 2021 at 12:56 PM Yonghong Song <yhs@fb.com> wrote:
>
> Latest upstream llvm-project added support for btf_decl_tag attributes
> for typedef declarations ([1], [2]). Similar to other btf_decl_tag cases,
> func/func_param/global_var/struct/union/field, btf_decl_tag with typedef
> declaration can carry information from kernel source to clang compiler
> and then to dwarf/BTF, for bpf verification or other use cases.
>
> This patch set added kernel support for BTF_KIND_DECL_TAG to typedef
> declaration (Patch 1). Additional selftests are added to cover
> unit testing, dedup, or bpf program usage of btf_decl_tag with typedef.
> (Patches 2, 3 and 4). The btf documentation is updated to include
> BTF_KIND_DECL_TAG typedef (Patch 5).
>
>   [1] https://reviews.llvm.org/D110127
>   [2] https://reviews.llvm.org/D112259

Applied. Thanks

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

end of thread, other threads:[~2021-10-23  0:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-21 19:56 [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef Yonghong Song
2021-10-21 19:56 ` [PATCH bpf-next 1/5] bpf: add BTF_KIND_DECL_TAG typedef support Yonghong Song
2021-10-21 19:56 ` [PATCH bpf-next 2/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests Yonghong Song
2021-10-21 19:56 ` [PATCH bpf-next 3/5] selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef Yonghong Song
2021-10-21 19:56 ` [PATCH bpf-next 4/5] selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c Yonghong Song
2021-10-21 19:56 ` [PATCH bpf-next 5/5] docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support Yonghong Song
2021-10-23  0:05 ` [PATCH bpf-next 0/5] bpf: add support for BTF_KIND_DECL_TAG typedef 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.