All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
@ 2021-11-10  5:19 Yonghong Song
  2021-11-10  5:19 ` [PATCH bpf-next 01/10] bpf: " Yonghong Song
                   ` (10 more replies)
  0 siblings, 11 replies; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

LLVM patches ([1] for clang, [2] and [3] for BPF backend)
added support for btf_type_tag attributes. This patch
added support for the kernel.

The main motivation for btf_type_tag is to bring kernel
annotations __user, __rcu etc. to btf. With such information
available in btf, bpf verifier can detect mis-usages
and reject the program. For example, for __user tagged pointer,
developers can then use proper helper like bpf_probe_read_kernel()
etc. to read the data.

BTF_KIND_TYPE_TAG may also useful for other tracing
facility where instead of to require user to specify
kernel/user address type, the kernel can detect it
by itself with btf.

Patch 1 added support in kernel, Patch 2 for libbpf and Patch 3
for bpftool. Patches 4-9 are for bpf selftests and Patch 10
updated docs/bpf/btf.rst file with new btf kind.

  [1] https://reviews.llvm.org/D111199
  [2] https://reviews.llvm.org/D113222
  [3] https://reviews.llvm.org/D113496

Yonghong Song (10):
  bpf: Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  libbpf: Support BTF_KIND_TYPE_TAG
  bpftool: Support BTF_KIND_TYPE_TAG
  selftests/bpf: Test libbpf API function btf__add_type_tag()
  selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests
  selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication
  selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c
  selftests/bpf: Add a C test for btf_type_tag
  selftests/bpf: Clarify llvm dependency with btf_tag selftest
  docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support

 Documentation/bpf/btf.rst                     | 13 +++-
 include/uapi/linux/btf.h                      |  3 +-
 kernel/bpf/btf.c                              | 14 +++-
 tools/bpf/bpftool/btf.c                       |  2 +
 tools/include/uapi/linux/btf.h                |  3 +-
 tools/lib/bpf/btf.c                           | 23 +++++++
 tools/lib/bpf/btf.h                           |  9 ++-
 tools/lib/bpf/btf_dump.c                      |  9 +++
 tools/lib/bpf/libbpf.c                        | 31 ++++++++-
 tools/lib/bpf/libbpf.map                      |  1 +
 tools/lib/bpf/libbpf_internal.h               |  2 +
 tools/testing/selftests/bpf/README.rst        |  9 +--
 tools/testing/selftests/bpf/btf_helpers.c     |  4 +-
 tools/testing/selftests/bpf/prog_tests/btf.c  | 64 ++++++++++++++++--
 .../selftests/bpf/prog_tests/btf_tag.c        | 44 ++++++++++--
 .../selftests/bpf/prog_tests/btf_write.c      | 67 +++++++++++--------
 .../bpf/progs/{tag.c => btf_decl_tag.c}       |  0
 .../selftests/bpf/progs/btf_type_tag.c        | 29 ++++++++
 tools/testing/selftests/bpf/test_btf.h        |  3 +
 19 files changed, 281 insertions(+), 49 deletions(-)
 rename tools/testing/selftests/bpf/progs/{tag.c => btf_decl_tag.c} (100%)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag.c

-- 
2.30.2


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

* [PATCH bpf-next 01/10] bpf: Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
@ 2021-11-10  5:19 ` Yonghong Song
  2021-11-11 18:27   ` Andrii Nakryiko
  2021-11-10  5:19 ` [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG Yonghong Song
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

LLVM patches ([1] for clang, [2] and [3] for BPF backend)
added support for btf_type_tag attributes. This patch
added support for the kernel.

The main motivation for btf_type_tag is to bring kernel
annotations __user, __rcu etc. to btf. With such information
available in btf, bpf verifier can detect mis-usages
and reject the program. For example, for __user tagged pointer,
developers can then use proper helper like bpf_probe_read_kernel()
etc. to read the data.

BTF_KIND_TYPE_TAG may also useful for other tracing
facility where instead of to require user to specify
kernel/user address type, the kernel can detect it
by itself with btf.

  [1] https://reviews.llvm.org/D111199
  [2] https://reviews.llvm.org/D113222
  [3] https://reviews.llvm.org/D113496

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 include/uapi/linux/btf.h       |  3 ++-
 kernel/bpf/btf.c               | 14 +++++++++++++-
 tools/include/uapi/linux/btf.h |  3 ++-
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h
index deb12f755f0f..b0d8fea1951d 100644
--- a/include/uapi/linux/btf.h
+++ b/include/uapi/linux/btf.h
@@ -43,7 +43,7 @@ struct btf_type {
 	 * "size" tells the size of the type it is describing.
 	 *
 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
-	 * FUNC, FUNC_PROTO, VAR and DECL_TAG.
+	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
 	 * "type" is a type_id referring to another type.
 	 */
 	union {
@@ -75,6 +75,7 @@ enum {
 	BTF_KIND_DATASEC	= 15,	/* Section	*/
 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
+	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
 
 	NR_BTF_KINDS,
 	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index cdb0fba65600..1dd9ba82da1e 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -282,6 +282,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_DATASEC]	= "DATASEC",
 	[BTF_KIND_FLOAT]	= "FLOAT",
 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
+	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
 };
 
 const char *btf_type_str(const struct btf_type *t)
@@ -418,6 +419,7 @@ static bool btf_type_is_modifier(const struct btf_type *t)
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_CONST:
 	case BTF_KIND_RESTRICT:
+	case BTF_KIND_TYPE_TAG:
 		return true;
 	}
 
@@ -1737,6 +1739,7 @@ __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
 		case BTF_KIND_VOLATILE:
 		case BTF_KIND_CONST:
 		case BTF_KIND_RESTRICT:
+		case BTF_KIND_TYPE_TAG:
 			id = type->type;
 			type = btf_type_by_id(btf, type->type);
 			break;
@@ -2345,6 +2348,8 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
 				   const struct btf_type *t,
 				   u32 meta_left)
 {
+	const char *value;
+
 	if (btf_type_vlen(t)) {
 		btf_verifier_log_type(env, t, "vlen != 0");
 		return -EINVAL;
@@ -2360,7 +2365,7 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
 		return -EINVAL;
 	}
 
-	/* typedef type must have a valid name, and other ref types,
+	/* typedef/type_tag type must have a valid name, and other ref types,
 	 * volatile, const, restrict, should have a null name.
 	 */
 	if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
@@ -2369,6 +2374,12 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
 			btf_verifier_log_type(env, t, "Invalid name");
 			return -EINVAL;
 		}
+	} else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) {
+		value = btf_name_by_offset(env->btf, t->name_off);
+		if (!value || !value[0]) {
+			btf_verifier_log_type(env, t, "Invalid name");
+			return -EINVAL;
+		}
 	} else {
 		if (t->name_off) {
 			btf_verifier_log_type(env, t, "Invalid name");
@@ -4059,6 +4070,7 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
 	[BTF_KIND_DATASEC] = &datasec_ops,
 	[BTF_KIND_FLOAT] = &float_ops,
 	[BTF_KIND_DECL_TAG] = &decl_tag_ops,
+	[BTF_KIND_TYPE_TAG] = &modifier_ops,
 };
 
 static s32 btf_check_meta(struct btf_verifier_env *env,
diff --git a/tools/include/uapi/linux/btf.h b/tools/include/uapi/linux/btf.h
index deb12f755f0f..b0d8fea1951d 100644
--- a/tools/include/uapi/linux/btf.h
+++ b/tools/include/uapi/linux/btf.h
@@ -43,7 +43,7 @@ struct btf_type {
 	 * "size" tells the size of the type it is describing.
 	 *
 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
-	 * FUNC, FUNC_PROTO, VAR and DECL_TAG.
+	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
 	 * "type" is a type_id referring to another type.
 	 */
 	union {
@@ -75,6 +75,7 @@ enum {
 	BTF_KIND_DATASEC	= 15,	/* Section	*/
 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/
 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */
+	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */
 
 	NR_BTF_KINDS,
 	BTF_KIND_MAX		= NR_BTF_KINDS - 1,
-- 
2.30.2


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

* [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
  2021-11-10  5:19 ` [PATCH bpf-next 01/10] bpf: " Yonghong Song
@ 2021-11-10  5:19 ` Yonghong Song
  2021-11-11 18:41   ` Andrii Nakryiko
  2021-11-10  5:19 ` [PATCH bpf-next 03/10] bpftool: " Yonghong Song
                   ` (8 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Add libbpf support for BTF_KIND_TYPE_TAG.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/lib/bpf/btf.c             | 23 +++++++++++++++++++++++
 tools/lib/bpf/btf.h             |  9 ++++++++-
 tools/lib/bpf/btf_dump.c        |  9 +++++++++
 tools/lib/bpf/libbpf.c          | 31 ++++++++++++++++++++++++++++++-
 tools/lib/bpf/libbpf.map        |  1 +
 tools/lib/bpf/libbpf_internal.h |  2 ++
 6 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 7e4c5586bd87..4d9883bef330 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -299,6 +299,7 @@ static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
 	case BTF_KIND_FLOAT:
+	case BTF_KIND_TYPE_TAG:
 		return base_size;
 	case BTF_KIND_INT:
 		return base_size + sizeof(__u32);
@@ -349,6 +350,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
 	case BTF_KIND_FLOAT:
+	case BTF_KIND_TYPE_TAG:
 		return 0;
 	case BTF_KIND_INT:
 		*(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
@@ -649,6 +651,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_CONST:
 	case BTF_KIND_RESTRICT:
+	case BTF_KIND_TYPE_TAG:
 		return btf__align_of(btf, t->type);
 	case BTF_KIND_ARRAY:
 		return btf__align_of(btf, btf_array(t)->type);
@@ -2235,6 +2238,22 @@ int btf__add_restrict(struct btf *btf, int ref_type_id)
 	return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
 }
 
+/*
+ * Append new BTF_KIND_TYPE_TAGtype with:
+ *   - *value*, non-empty/non-NULL name;
+ *   - *ref_type_id* - referenced type ID, it might not exist yet;
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.
+ */
+int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id)
+{
+	if (!value|| !value[0])
+		return libbpf_err(-EINVAL);
+
+	return btf_add_ref_kind(btf, BTF_KIND_TYPE_TAG, value, ref_type_id);
+}
+
 /*
  * Append new BTF_KIND_FUNC type with:
  *   - *name*, non-empty/non-NULL name;
@@ -3625,6 +3644,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
 		case BTF_KIND_TYPEDEF:
 		case BTF_KIND_FUNC:
 		case BTF_KIND_FLOAT:
+		case BTF_KIND_TYPE_TAG:
 			h = btf_hash_common(t);
 			break;
 		case BTF_KIND_INT:
@@ -3685,6 +3705,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
 	case BTF_KIND_VAR:
 	case BTF_KIND_DATASEC:
 	case BTF_KIND_DECL_TAG:
+	case BTF_KIND_TYPE_TAG:
 		return 0;
 
 	case BTF_KIND_INT:
@@ -4289,6 +4310,7 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
 	case BTF_KIND_PTR:
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
+	case BTF_KIND_TYPE_TAG:
 		ref_type_id = btf_dedup_ref_type(d, t->type);
 		if (ref_type_id < 0)
 			return ref_type_id;
@@ -4595,6 +4617,7 @@ int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ct
 	case BTF_KIND_FUNC:
 	case BTF_KIND_VAR:
 	case BTF_KIND_DECL_TAG:
+	case BTF_KIND_TYPE_TAG:
 		return visit(&t->type, ctx);
 
 	case BTF_KIND_ARRAY: {
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index bc005ba3ceec..129c2acc8493 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -227,6 +227,7 @@ LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_
 LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
 LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
 LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
+LIBBPF_API int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id);
 
 /* func and func_proto construction APIs */
 LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
@@ -403,7 +404,8 @@ static inline bool btf_is_mod(const struct btf_type *t)
 
 	return kind == BTF_KIND_VOLATILE ||
 	       kind == BTF_KIND_CONST ||
-	       kind == BTF_KIND_RESTRICT;
+	       kind == BTF_KIND_RESTRICT ||
+	       kind == BTF_KIND_TYPE_TAG;
 }
 
 static inline bool btf_is_func(const struct btf_type *t)
@@ -436,6 +438,11 @@ static inline bool btf_is_decl_tag(const struct btf_type *t)
 	return btf_kind(t) == BTF_KIND_DECL_TAG;
 }
 
+static inline bool btf_is_type_tag(const struct btf_type *t)
+{
+	return btf_kind(t) == BTF_KIND_TYPE_TAG;
+}
+
 static inline __u8 btf_int_encoding(const struct btf_type *t)
 {
 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 17db62b5002e..380250a5bfcd 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -317,6 +317,7 @@ static int btf_dump_mark_referenced(struct btf_dump *d)
 		case BTF_KIND_FUNC:
 		case BTF_KIND_VAR:
 		case BTF_KIND_DECL_TAG:
+		case BTF_KIND_TYPE_TAG:
 			d->type_states[t->type].referenced = 1;
 			break;
 
@@ -560,6 +561,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_CONST:
 	case BTF_KIND_RESTRICT:
+	case BTF_KIND_TYPE_TAG:
 		return btf_dump_order_type(d, t->type, through_ptr);
 
 	case BTF_KIND_FUNC_PROTO: {
@@ -734,6 +736,7 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_CONST:
 	case BTF_KIND_RESTRICT:
+	case BTF_KIND_TYPE_TAG:
 		btf_dump_emit_type(d, t->type, cont_id);
 		break;
 	case BTF_KIND_ARRAY:
@@ -1154,6 +1157,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 		case BTF_KIND_CONST:
 		case BTF_KIND_RESTRICT:
 		case BTF_KIND_FUNC_PROTO:
+		case BTF_KIND_TYPE_TAG:
 			id = t->type;
 			break;
 		case BTF_KIND_ARRAY:
@@ -1322,6 +1326,11 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
 		case BTF_KIND_RESTRICT:
 			btf_dump_printf(d, " restrict");
 			break;
+		case BTF_KIND_TYPE_TAG:
+			btf_dump_emit_mods(d, decls);
+			name = btf_name_of(d, t->name_off);
+			btf_dump_printf(d, " __attribute__((btf_type_tag(\"%s\")))", name);
+			break;
 		case BTF_KIND_ARRAY: {
 			const struct btf_array *a = btf_array(t);
 			const struct btf_type *next_t;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d869ebee1e27..f6bf8ce24bbd 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -197,6 +197,8 @@ enum kern_feature_id {
 	FEAT_PERF_LINK,
 	/* BTF_KIND_DECL_TAG support */
 	FEAT_BTF_DECL_TAG,
+	/* BTF_KIND_TYPE_TAG support */
+	FEAT_BTF_TYPE_TAG,
 	__FEAT_CNT,
 };
 
@@ -2076,6 +2078,7 @@ static const char *__btf_kind_str(__u16 kind)
 	case BTF_KIND_DATASEC: return "datasec";
 	case BTF_KIND_FLOAT: return "float";
 	case BTF_KIND_DECL_TAG: return "decl_tag";
+	case BTF_KIND_TYPE_TAG: return "type_tag";
 	default: return "unknown";
 	}
 }
@@ -2588,8 +2591,10 @@ static bool btf_needs_sanitization(struct bpf_object *obj)
 	bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
 	bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
 	bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
+	bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
 
-	return !has_func || !has_datasec || !has_func_global || !has_float || !has_decl_tag;
+	return !has_func || !has_datasec || !has_func_global || !has_float ||
+	       !has_decl_tag || !has_type_tag;
 }
 
 static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
@@ -2599,6 +2604,7 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
 	bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
 	bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
 	bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
+	bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
 	struct btf_type *t;
 	int i, j, vlen;
 
@@ -2657,6 +2663,10 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
 			 */
 			t->name_off = 0;
 			t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
+		} else if (!has_type_tag && btf_is_type_tag(t)) {
+			/* replace TYPE_TAG with a CONST */
+			t->name_off = 0;
+			t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
 		}
 	}
 }
@@ -4460,6 +4470,22 @@ static int probe_kern_btf_decl_tag(void)
 					     strs, sizeof(strs)));
 }
 
+static int probe_kern_btf_type_tag(void)
+{
+	static const char strs[] = "\0tag";
+	__u32 types[] = {
+		/* int */
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
+		/* attr */
+		BTF_TYPE_TYPE_TAG_ENC(1, 1),				/* [2] */
+		/* ptr */
+		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),	/* [3] */
+	};
+
+	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
+					     strs, sizeof(strs)));
+}
+
 static int probe_kern_array_mmap(void)
 {
 	struct bpf_create_map_attr attr = {
@@ -4657,6 +4683,9 @@ static struct kern_feature_desc {
 	[FEAT_BTF_DECL_TAG] = {
 		"BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag,
 	},
+	[FEAT_BTF_TYPE_TAG] = {
+		"BTF_KIND_TYPE_TAG support", probe_kern_btf_type_tag,
+	},
 };
 
 static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index b895861a13c0..0126d924674f 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -401,6 +401,7 @@ LIBBPF_0.6.0 {
 		bpf_program__insns;
 		btf__add_btf;
 		btf__add_decl_tag;
+		btf__add_type_tag;
 		btf__raw_data;
 		btf__type_cnt;
 } LIBBPF_0.5.0;
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 2d873c962f99..5490fa98c0a6 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -73,6 +73,8 @@
 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
 #define BTF_TYPE_DECL_TAG_ENC(value, type, component_idx) \
 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
+#define BTF_TYPE_TYPE_TAG_ENC(value, type) \
+	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
 
 #ifndef likely
 #define likely(x) __builtin_expect(!!(x), 1)
-- 
2.30.2


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

* [PATCH bpf-next 03/10] bpftool: Support BTF_KIND_TYPE_TAG
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
  2021-11-10  5:19 ` [PATCH bpf-next 01/10] bpf: " Yonghong Song
  2021-11-10  5:19 ` [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG Yonghong Song
@ 2021-11-10  5:19 ` Yonghong Song
  2021-11-11 18:43   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag() Yonghong Song
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Add bpftool support for BTF_KIND_TYPE_TAG.

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

diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index 015d2758f826..e77dda35d1de 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -39,6 +39,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
 	[BTF_KIND_DATASEC]	= "DATASEC",
 	[BTF_KIND_FLOAT]	= "FLOAT",
 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
+	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
 };
 
 struct btf_attach_point {
@@ -142,6 +143,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_RESTRICT:
 	case BTF_KIND_TYPEDEF:
+	case BTF_KIND_TYPE_TAG:
 		if (json_output)
 			jsonw_uint_field(w, "type_id", t->type);
 		else
-- 
2.30.2


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

* [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag()
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (2 preceding siblings ...)
  2021-11-10  5:19 ` [PATCH bpf-next 03/10] bpftool: " Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:44   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests Yonghong Song
                   ` (6 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Add unit tests for btf__add_type_tag().

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/btf_helpers.c     |  4 +-
 .../selftests/bpf/prog_tests/btf_write.c      | 67 +++++++++++--------
 2 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/tools/testing/selftests/bpf/btf_helpers.c b/tools/testing/selftests/bpf/btf_helpers.c
index 3d1a748d09d8..89e717961c43 100644
--- a/tools/testing/selftests/bpf/btf_helpers.c
+++ b/tools/testing/selftests/bpf/btf_helpers.c
@@ -25,11 +25,12 @@ static const char * const btf_kind_str_mapping[] = {
 	[BTF_KIND_DATASEC]	= "DATASEC",
 	[BTF_KIND_FLOAT]	= "FLOAT",
 	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
+	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
 };
 
 static const char *btf_kind_str(__u16 kind)
 {
-	if (kind > BTF_KIND_DECL_TAG)
+	if (kind > BTF_KIND_TYPE_TAG)
 		return "UNKNOWN";
 	return btf_kind_str_mapping[kind];
 }
@@ -109,6 +110,7 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
 	case BTF_KIND_VOLATILE:
 	case BTF_KIND_RESTRICT:
 	case BTF_KIND_TYPEDEF:
+	case BTF_KIND_TYPE_TAG:
 		fprintf(out, " type_id=%u", t->type);
 		break;
 	case BTF_KIND_ARRAY: {
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_write.c b/tools/testing/selftests/bpf/prog_tests/btf_write.c
index b912eeb0b6b4..addf99c05896 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_write.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_write.c
@@ -297,6 +297,16 @@ static void gen_btf(struct btf *btf)
 	ASSERT_EQ(btf_decl_tag(t)->component_idx, 1, "tag_component_idx");
 	ASSERT_STREQ(btf_type_raw_dump(btf, 19),
 		     "[19] DECL_TAG 'tag2' type_id=14 component_idx=1", "raw_dump");
+
+	/* TYPE_TAG */
+	id = btf__add_type_tag(btf, "tag1", 1);
+	ASSERT_EQ(id, 20, "tag_id");
+	t = btf__type_by_id(btf, 20);
+	ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag1", "tag_value");
+	ASSERT_EQ(btf_kind(t), BTF_KIND_TYPE_TAG, "tag_kind");
+	ASSERT_EQ(t->type, 1, "tag_type");
+	ASSERT_STREQ(btf_type_raw_dump(btf, 20),
+		     "[20] TYPE_TAG 'tag1' type_id=1", "raw_dump");
 }
 
 static void test_btf_add()
@@ -337,7 +347,8 @@ static void test_btf_add()
 		"[17] DATASEC 'datasec1' size=12 vlen=1\n"
 		"\ttype_id=1 offset=4 size=8",
 		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
-		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1");
+		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1",
+		"[20] TYPE_TAG 'tag1' type_id=1");
 
 	btf__free(btf);
 }
@@ -359,7 +370,7 @@ static void test_btf_add_btf()
 	gen_btf(btf2);
 
 	id = btf__add_btf(btf1, btf2);
-	if (!ASSERT_EQ(id, 20, "id"))
+	if (!ASSERT_EQ(id, 21, "id"))
 		goto cleanup;
 
 	VALIDATE_RAW_BTF(
@@ -391,35 +402,37 @@ static void test_btf_add_btf()
 		"\ttype_id=1 offset=4 size=8",
 		"[18] DECL_TAG 'tag1' type_id=16 component_idx=-1",
 		"[19] DECL_TAG 'tag2' type_id=14 component_idx=1",
+		"[20] TYPE_TAG 'tag1' type_id=1",
 
 		/* types appended from the second BTF */
-		"[20] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
-		"[21] PTR '(anon)' type_id=20",
-		"[22] CONST '(anon)' type_id=24",
-		"[23] VOLATILE '(anon)' type_id=22",
-		"[24] RESTRICT '(anon)' type_id=23",
-		"[25] ARRAY '(anon)' type_id=21 index_type_id=20 nr_elems=10",
-		"[26] STRUCT 's1' size=8 vlen=2\n"
-		"\t'f1' type_id=20 bits_offset=0\n"
-		"\t'f2' type_id=20 bits_offset=32 bitfield_size=16",
-		"[27] UNION 'u1' size=8 vlen=1\n"
-		"\t'f1' type_id=20 bits_offset=0 bitfield_size=16",
-		"[28] ENUM 'e1' size=4 vlen=2\n"
+		"[21] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[22] PTR '(anon)' type_id=21",
+		"[23] CONST '(anon)' type_id=25",
+		"[24] VOLATILE '(anon)' type_id=23",
+		"[25] RESTRICT '(anon)' type_id=24",
+		"[26] ARRAY '(anon)' type_id=22 index_type_id=21 nr_elems=10",
+		"[27] STRUCT 's1' size=8 vlen=2\n"
+		"\t'f1' type_id=21 bits_offset=0\n"
+		"\t'f2' type_id=21 bits_offset=32 bitfield_size=16",
+		"[28] UNION 'u1' size=8 vlen=1\n"
+		"\t'f1' type_id=21 bits_offset=0 bitfield_size=16",
+		"[29] ENUM 'e1' size=4 vlen=2\n"
 		"\t'v1' val=1\n"
 		"\t'v2' val=2",
-		"[29] FWD 'struct_fwd' fwd_kind=struct",
-		"[30] FWD 'union_fwd' fwd_kind=union",
-		"[31] ENUM 'enum_fwd' size=4 vlen=0",
-		"[32] TYPEDEF 'typedef1' type_id=20",
-		"[33] FUNC 'func1' type_id=34 linkage=global",
-		"[34] FUNC_PROTO '(anon)' ret_type_id=20 vlen=2\n"
-		"\t'p1' type_id=20\n"
-		"\t'p2' type_id=21",
-		"[35] VAR 'var1' type_id=20, linkage=global-alloc",
-		"[36] DATASEC 'datasec1' size=12 vlen=1\n"
-		"\ttype_id=20 offset=4 size=8",
-		"[37] DECL_TAG 'tag1' type_id=35 component_idx=-1",
-		"[38] DECL_TAG 'tag2' type_id=33 component_idx=1");
+		"[30] FWD 'struct_fwd' fwd_kind=struct",
+		"[31] FWD 'union_fwd' fwd_kind=union",
+		"[32] ENUM 'enum_fwd' size=4 vlen=0",
+		"[33] TYPEDEF 'typedef1' type_id=21",
+		"[34] FUNC 'func1' type_id=35 linkage=global",
+		"[35] FUNC_PROTO '(anon)' ret_type_id=21 vlen=2\n"
+		"\t'p1' type_id=21\n"
+		"\t'p2' type_id=22",
+		"[36] VAR 'var1' type_id=21, linkage=global-alloc",
+		"[37] DATASEC 'datasec1' size=12 vlen=1\n"
+		"\ttype_id=21 offset=4 size=8",
+		"[38] DECL_TAG 'tag1' type_id=36 component_idx=-1",
+		"[39] DECL_TAG 'tag2' type_id=34 component_idx=1",
+		"[40] TYPE_TAG 'tag1' type_id=21");
 
 cleanup:
 	btf__free(btf1);
-- 
2.30.2


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

* [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (3 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag() Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:45   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication Yonghong Song
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Add BTF_KIND_TYPE_TAG unit tests.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 18 ++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |  3 +++
 2 files changed, 21 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index ebd1aa4d09d6..ebd0ead5f4bc 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -3939,6 +3939,23 @@ static struct btf_raw_test raw_tests[] = {
 	.btf_load_err = true,
 	.err_str = "Invalid component_idx",
 },
+{
+	.descr = "type_tag test #1",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [2] */
+		BTF_PTR_ENC(2),					/* [3] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\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,
+},
 
 }; /* struct btf_raw_test raw_tests[] */
 
@@ -7255,6 +7272,7 @@ static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
 	case BTF_KIND_FLOAT:
+	case BTF_KIND_TYPE_TAG:
 		return base_size;
 	case BTF_KIND_INT:
 		return base_size + sizeof(__u32);
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index 32c7a57867da..128989bed8b7 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -72,4 +72,7 @@
 #define BTF_DECL_TAG_ENC(value, type, component_idx)	\
 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
 
+#define BTF_TYPE_TAG_ENC(value, type)	\
+	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
+
 #endif /* _TEST_BTF_H */
-- 
2.30.2


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

* [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (4 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:49   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c Yonghong Song
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

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

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index ebd0ead5f4bc..91b19c41729f 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -6889,15 +6889,16 @@ const struct btf_dedup_test dedup_tests[] = {
 			BTF_RESTRICT_ENC(8),						/* [11] restrict */
 			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
-				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
+				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
 			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] 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_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
 	},
 	.expect = {
 		.raw_types = {
@@ -6918,15 +6919,16 @@ const struct btf_dedup_test dedup_tests[] = {
 			BTF_RESTRICT_ENC(8),						/* [11] restrict */
 			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
-				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
+				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
 			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] 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_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
 			BTF_END_RAW,
 		},
-		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
+		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
 	},
 	.opts = {
 		.dont_resolve_fwds = false,
@@ -7254,6 +7256,42 @@ const struct btf_dedup_test dedup_tests[] = {
 		.dont_resolve_fwds = false,
 	},
 },
+{
+	.descr = "dedup: btf_tag_type",
+	.input = {
+		.raw_types = {
+			/* int */
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			/* tag: tag1, tag2 */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
+			BTF_PTR_ENC(3),					/* [4] */
+			/* tag: tag1, tag2 */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [5] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),		/* [6] */
+			BTF_PTR_ENC(6),					/* [7] */
+			/* tag: tag1 */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [8] */
+			BTF_PTR_ENC(8),					/* [9] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0tag1\0tag2"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
+			BTF_PTR_ENC(3),					/* [4] */
+			BTF_PTR_ENC(2),					/* [5] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0tag1\0tag2"),
+	},
+	.opts = {
+		.dont_resolve_fwds = false,
+	},
+},
 
 };
 
-- 
2.30.2


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

* [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (5 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:51   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag Yonghong Song
                   ` (3 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Rename progs/tag.c to progs/btf_decl_tag.c so we can introduce
progs/btf_type_tag.c in the next patch.

Also create a subtest for btf_decl_tag in prog_tests/btf_tag.c
so we can introduce btf_type_tag subtest in the next patch.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 .../selftests/bpf/prog_tests/btf_tag.c        | 20 ++++++++++++-------
 .../bpf/progs/{tag.c => btf_decl_tag.c}       |  0
 2 files changed, 13 insertions(+), 7 deletions(-)
 rename tools/testing/selftests/bpf/progs/{tag.c => btf_decl_tag.c} (100%)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
index 91821f42714d..d15cc7a88182 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
@@ -1,20 +1,26 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2021 Facebook */
 #include <test_progs.h>
-#include "tag.skel.h"
+#include "btf_decl_tag.skel.h"
 
-void test_btf_tag(void)
+static void test_btf_decl_tag(void)
 {
-	struct tag *skel;
+	struct btf_decl_tag *skel;
 
-	skel = tag__open_and_load();
-	if (!ASSERT_OK_PTR(skel, "btf_tag"))
+	skel = btf_decl_tag__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "btf_decl_tag"))
 		return;
 
 	if (skel->rodata->skip_tests) {
-		printf("%s:SKIP: btf_tag attribute not supported", __func__);
+		printf("%s:SKIP: btf_decl_tag attribute not supported", __func__);
 		test__skip();
 	}
 
-	tag__destroy(skel);
+	btf_decl_tag__destroy(skel);
+}
+
+void test_btf_tag(void)
+{
+	if (test__start_subtest("btf_decl_tag"))
+		test_btf_decl_tag();
 }
diff --git a/tools/testing/selftests/bpf/progs/tag.c b/tools/testing/selftests/bpf/progs/btf_decl_tag.c
similarity index 100%
rename from tools/testing/selftests/bpf/progs/tag.c
rename to tools/testing/selftests/bpf/progs/btf_decl_tag.c
-- 
2.30.2


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

* [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (6 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:55   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest Yonghong Song
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

For the C test, compiler the kernel and selftest with clang compiler
by adding LLVM=1 to the make command line since btf_type_tag is
only supported by clang compiler now.

The following is the key btf_type_tag usage:
  #define __tag1 __attribute__((btf_type_tag("tag1")))
  #define __tag2 __attribute__((btf_type_tag("tag2")))
  struct btf_type_tag_test {
       int __tag1 * __tag1 __tag2 *p;
  } g;

The bpftool raw dump with related types:
  [4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
  [11] STRUCT 'btf_type_tag_test' size=8 vlen=1
          'p' type_id=14 bits_offset=0
  [12] TYPE_TAG 'tag1' type_id=16
  [13] TYPE_TAG 'tag2' type_id=12
  [14] PTR '(anon)' type_id=13
  [15] TYPE_TAG 'tag1' type_id=4
  [16] PTR '(anon)' type_id=15
  [17] VAR 'g' type_id=11, linkage=global

With format C dump, we have
  struct btf_type_tag_test {
        int __attribute__((btf_type_tag("tag1"))) * __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
  };
The result C code is identical to the original definition except macro's are gone.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 .../selftests/bpf/prog_tests/btf_tag.c        | 24 +++++++++++++++
 .../selftests/bpf/progs/btf_type_tag.c        | 29 +++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
index d15cc7a88182..88d63e23e35f 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
@@ -3,6 +3,12 @@
 #include <test_progs.h>
 #include "btf_decl_tag.skel.h"
 
+/* struct btf_type_tag_test is referenced in btf_type_tag.skel.h */
+struct btf_type_tag_test {
+        int **p;
+};
+#include "btf_type_tag.skel.h"
+
 static void test_btf_decl_tag(void)
 {
 	struct btf_decl_tag *skel;
@@ -19,8 +25,26 @@ static void test_btf_decl_tag(void)
 	btf_decl_tag__destroy(skel);
 }
 
+static void test_btf_type_tag(void)
+{
+	struct btf_type_tag *skel;
+
+	skel = btf_type_tag__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "btf_type_tag"))
+		return;
+
+	if (skel->rodata->skip_tests) {
+		printf("%s:SKIP: btf_type_tag attribute not supported", __func__);
+		test__skip();
+	}
+
+	btf_type_tag__destroy(skel);
+}
+
 void test_btf_tag(void)
 {
 	if (test__start_subtest("btf_decl_tag"))
 		test_btf_decl_tag();
+	if (test__start_subtest("btf_type_tag"))
+		test_btf_type_tag();
 }
diff --git a/tools/testing/selftests/bpf/progs/btf_type_tag.c b/tools/testing/selftests/bpf/progs/btf_type_tag.c
new file mode 100644
index 000000000000..0e18c777862c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_type_tag.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#ifndef __has_attribute
+#define __has_attribute(x) 0
+#endif
+
+#if __has_attribute(btf_type_tag)
+#define __tag1 __attribute__((btf_type_tag("tag1")))
+#define __tag2 __attribute__((btf_type_tag("tag2")))
+volatile const bool skip_tests = false;
+#else
+#define __tag1
+#define __tag2
+volatile const bool skip_tests = true;
+#endif
+
+struct btf_type_tag_test {
+	int __tag1 * __tag1 __tag2 *p;
+} g;
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(sub, int x)
+{
+  return 0;
+}
-- 
2.30.2


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

* [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (7 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:56   ` Andrii Nakryiko
  2021-11-10  5:20 ` [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support Yonghong Song
  2021-11-10  5:28 ` [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Alexei Starovoitov
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

btf_tag selftest needs certain llvm versions (>= llvm14).
Make it clear in the selftests README.rst file.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/README.rst | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/README.rst b/tools/testing/selftests/bpf/README.rst
index 5e287e445f75..42ef250c7acc 100644
--- a/tools/testing/selftests/bpf/README.rst
+++ b/tools/testing/selftests/bpf/README.rst
@@ -204,16 +204,17 @@ __ https://reviews.llvm.org/D93563
 btf_tag test and Clang version
 ==============================
 
-The btf_tag selftest require LLVM support to recognize the btf_decl_tag attribute.
-It was introduced in `Clang 14`__.
+The btf_tag selftest requires LLVM support to recognize the btf_decl_tag and
+btf_type_tag attributes. They are introduced in `Clang 14` [0_, 1_].
 
-Without it, the btf_tag selftest will be skipped and you will observe:
+Without them, the btf_tag selftest will be skipped and you will observe:
 
 .. code-block:: console
 
   #<test_num> btf_tag:SKIP
 
-__ https://reviews.llvm.org/D111588
+.. _0: https://reviews.llvm.org/D111588
+.. _1: https://reviews.llvm.org/D111199
 
 Clang dependencies for static linking tests
 ===========================================
-- 
2.30.2


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

* [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (8 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest Yonghong Song
@ 2021-11-10  5:20 ` Yonghong Song
  2021-11-11 18:56   ` Andrii Nakryiko
  2021-11-10  5:28 ` [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Alexei Starovoitov
  10 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  5:20 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

Add BTF_KIND_TYPE_TAG documentation in btf.rst.

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

diff --git a/Documentation/bpf/btf.rst b/Documentation/bpf/btf.rst
index 9ad4218a751f..d0ec40d00c28 100644
--- a/Documentation/bpf/btf.rst
+++ b/Documentation/bpf/btf.rst
@@ -86,6 +86,7 @@ sequentially and type id is assigned to each recognized type starting from id
     #define BTF_KIND_DATASEC        15      /* Section      */
     #define BTF_KIND_FLOAT          16      /* Floating point       */
     #define BTF_KIND_DECL_TAG       17      /* Decl Tag     */
+    #define BTF_KIND_TYPE_TAG       18      /* Type Tag     */
 
 Note that the type section encodes debug info, not just pure types.
 ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -107,7 +108,7 @@ Each type contains the following common data::
          * "size" tells the size of the type it is describing.
          *
          * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
-         * FUNC, FUNC_PROTO and DECL_TAG.
+         * FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
          * "type" is a type_id referring to another type.
          */
         union {
@@ -492,6 +493,16 @@ the attribute is applied to a ``struct``/``union`` member or
 a ``func`` argument, and ``btf_decl_tag.component_idx`` should be a
 valid index (starting from 0) pointing to a member or an argument.
 
+2.2.17 BTF_KIND_TYPE_TAG
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+``struct btf_type`` encoding requirement:
+ * ``name_off``: offset to a non-empty string
+ * ``info.kind_flag``: 0
+ * ``info.kind``: BTF_KIND_TYPE_TAG
+ * ``info.vlen``: 0
+ * ``type``: the type with ``btf_type_tag`` attribute
+
 3. BTF Kernel API
 *****************
 
-- 
2.30.2


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

* Re: [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
                   ` (9 preceding siblings ...)
  2021-11-10  5:20 ` [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support Yonghong Song
@ 2021-11-10  5:28 ` Alexei Starovoitov
  2021-11-10  6:26   ` Yonghong Song
  10 siblings, 1 reply; 30+ messages in thread
From: Alexei Starovoitov @ 2021-11-10  5:28 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team

On Tue, Nov 09, 2021 at 09:19:40PM -0800, Yonghong Song wrote:
> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
> added support for btf_type_tag attributes. This patch
> added support for the kernel.
> 
> The main motivation for btf_type_tag is to bring kernel
> annotations __user, __rcu etc. to btf. With such information
> available in btf, bpf verifier can detect mis-usages
> and reject the program. For example, for __user tagged pointer,
> developers can then use proper helper like bpf_probe_read_kernel()
> etc. to read the data.

+#define __tag1 __attribute__((btf_type_tag("tag1")))
+#define __tag2 __attribute__((btf_type_tag("tag2")))
+
+struct btf_type_tag_test {
+       int __tag1 * __tag1 __tag2 *p;
+} g;

Can we build the kernel with the latest clang and get __user in BTF ?

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

* Re: [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10  5:28 ` [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Alexei Starovoitov
@ 2021-11-10  6:26   ` Yonghong Song
  2021-11-10 16:40     ` Alexei Starovoitov
  0 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10  6:26 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, kernel-team



On 11/9/21 9:28 PM, Alexei Starovoitov wrote:
> On Tue, Nov 09, 2021 at 09:19:40PM -0800, Yonghong Song wrote:
>> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
>> added support for btf_type_tag attributes. This patch
>> added support for the kernel.
>>
>> The main motivation for btf_type_tag is to bring kernel
>> annotations __user, __rcu etc. to btf. With such information
>> available in btf, bpf verifier can detect mis-usages
>> and reject the program. For example, for __user tagged pointer,
>> developers can then use proper helper like bpf_probe_read_kernel()
>> etc. to read the data.
> 
> +#define __tag1 __attribute__((btf_type_tag("tag1")))
> +#define __tag2 __attribute__((btf_type_tag("tag2")))
> +
> +struct btf_type_tag_test {
> +       int __tag1 * __tag1 __tag2 *p;
> +} g;
> 
> Can we build the kernel with the latest clang and get __user in BTF ?

Not yet. The following are the steps:
   1. land this patch set in the kernel
   2. sync to libbpf repo.
   3. pahole sync with libbpf repo, and pahole convert btf_type_tag
      in llvm to BTF
   4. another kernel patch to define __user as
      __attribute__((btf_type_tag("user")))
and then we will get __user in vmlinux BTF.

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

* Re: [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10  6:26   ` Yonghong Song
@ 2021-11-10 16:40     ` Alexei Starovoitov
  2021-11-10 17:04       ` Yonghong Song
  0 siblings, 1 reply; 30+ messages in thread
From: Alexei Starovoitov @ 2021-11-10 16:40 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 10:26 PM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 11/9/21 9:28 PM, Alexei Starovoitov wrote:
> > On Tue, Nov 09, 2021 at 09:19:40PM -0800, Yonghong Song wrote:
> >> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
> >> added support for btf_type_tag attributes. This patch
> >> added support for the kernel.
> >>
> >> The main motivation for btf_type_tag is to bring kernel
> >> annotations __user, __rcu etc. to btf. With such information
> >> available in btf, bpf verifier can detect mis-usages
> >> and reject the program. For example, for __user tagged pointer,
> >> developers can then use proper helper like bpf_probe_read_kernel()
> >> etc. to read the data.
> >
> > +#define __tag1 __attribute__((btf_type_tag("tag1")))
> > +#define __tag2 __attribute__((btf_type_tag("tag2")))
> > +
> > +struct btf_type_tag_test {
> > +       int __tag1 * __tag1 __tag2 *p;
> > +} g;
> >
> > Can we build the kernel with the latest clang and get __user in BTF ?
>
> Not yet. The following are the steps:
>    1. land this patch set in the kernel
>    2. sync to libbpf repo.
>    3. pahole sync with libbpf repo, and pahole convert btf_type_tag
>       in llvm to BTF
>    4. another kernel patch to define __user as
>       __attribute__((btf_type_tag("user")))
> and then we will get __user in vmlinux BTF.

Makes sense. I was wondering whether clang can handle
the whole kernel source code with
#define __user __attribute__((btf_type_tag("user")))
Steps 1,2,3 are necessary to make use of it,
but step 4 can be tried out already?

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

* Re: [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10 16:40     ` Alexei Starovoitov
@ 2021-11-10 17:04       ` Yonghong Song
  2021-11-11  1:47         ` Alexei Starovoitov
  0 siblings, 1 reply; 30+ messages in thread
From: Yonghong Song @ 2021-11-10 17:04 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team



On 11/10/21 8:40 AM, Alexei Starovoitov wrote:
> On Tue, Nov 9, 2021 at 10:26 PM Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 11/9/21 9:28 PM, Alexei Starovoitov wrote:
>>> On Tue, Nov 09, 2021 at 09:19:40PM -0800, Yonghong Song wrote:
>>>> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
>>>> added support for btf_type_tag attributes. This patch
>>>> added support for the kernel.
>>>>
>>>> The main motivation for btf_type_tag is to bring kernel
>>>> annotations __user, __rcu etc. to btf. With such information
>>>> available in btf, bpf verifier can detect mis-usages
>>>> and reject the program. For example, for __user tagged pointer,
>>>> developers can then use proper helper like bpf_probe_read_kernel()
>>>> etc. to read the data.
>>>
>>> +#define __tag1 __attribute__((btf_type_tag("tag1")))
>>> +#define __tag2 __attribute__((btf_type_tag("tag2")))
>>> +
>>> +struct btf_type_tag_test {
>>> +       int __tag1 * __tag1 __tag2 *p;
>>> +} g;
>>>
>>> Can we build the kernel with the latest clang and get __user in BTF ?
>>
>> Not yet. The following are the steps:
>>     1. land this patch set in the kernel
>>     2. sync to libbpf repo.
>>     3. pahole sync with libbpf repo, and pahole convert btf_type_tag
>>        in llvm to BTF
>>     4. another kernel patch to define __user as
>>        __attribute__((btf_type_tag("user")))
>> and then we will get __user in vmlinux BTF.
> 
> Makes sense. I was wondering whether clang can handle
> the whole kernel source code with
> #define __user __attribute__((btf_type_tag("user")))
> Steps 1,2,3 are necessary to make use of it,
> but step 4 can be tried out already?

Yes, you try clang -> vmlinux dwarf part of step 4 with
the following kernel hack:

diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 05ceb2e92b0e..30e199c30a53 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -32,7 +32,7 @@ static inline void __chk_io_ptr(const volatile void 
__iomem *ptr) { }
  # ifdef STRUCTLEAK_PLUGIN
  #  define __user       __attribute__((user))
  # else
-#  define __user
+#  define __user       __attribute__((btf_type_tag("user")))
  # endif
  # define __iomem
  # define __percpu
[yhs@devbig309.ftw3 ~/work/bpf-next]


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

* Re: [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10 17:04       ` Yonghong Song
@ 2021-11-11  1:47         ` Alexei Starovoitov
  0 siblings, 0 replies; 30+ messages in thread
From: Alexei Starovoitov @ 2021-11-11  1:47 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Wed, Nov 10, 2021 at 9:04 AM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 11/10/21 8:40 AM, Alexei Starovoitov wrote:
> > On Tue, Nov 9, 2021 at 10:26 PM Yonghong Song <yhs@fb.com> wrote:
> >>
> >>
> >>
> >> On 11/9/21 9:28 PM, Alexei Starovoitov wrote:
> >>> On Tue, Nov 09, 2021 at 09:19:40PM -0800, Yonghong Song wrote:
> >>>> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
> >>>> added support for btf_type_tag attributes. This patch
> >>>> added support for the kernel.
> >>>>
> >>>> The main motivation for btf_type_tag is to bring kernel
> >>>> annotations __user, __rcu etc. to btf. With such information
> >>>> available in btf, bpf verifier can detect mis-usages
> >>>> and reject the program. For example, for __user tagged pointer,
> >>>> developers can then use proper helper like bpf_probe_read_kernel()
> >>>> etc. to read the data.
> >>>
> >>> +#define __tag1 __attribute__((btf_type_tag("tag1")))
> >>> +#define __tag2 __attribute__((btf_type_tag("tag2")))
> >>> +
> >>> +struct btf_type_tag_test {
> >>> +       int __tag1 * __tag1 __tag2 *p;
> >>> +} g;
> >>>
> >>> Can we build the kernel with the latest clang and get __user in BTF ?
> >>
> >> Not yet. The following are the steps:
> >>     1. land this patch set in the kernel
> >>     2. sync to libbpf repo.
> >>     3. pahole sync with libbpf repo, and pahole convert btf_type_tag
> >>        in llvm to BTF
> >>     4. another kernel patch to define __user as
> >>        __attribute__((btf_type_tag("user")))
> >> and then we will get __user in vmlinux BTF.
> >
> > Makes sense. I was wondering whether clang can handle
> > the whole kernel source code with
> > #define __user __attribute__((btf_type_tag("user")))
> > Steps 1,2,3 are necessary to make use of it,
> > but step 4 can be tried out already?
>
> Yes, you try clang -> vmlinux dwarf part of step 4 with
> the following kernel hack:
>
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index 05ceb2e92b0e..30e199c30a53 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -32,7 +32,7 @@ static inline void __chk_io_ptr(const volatile void
> __iomem *ptr) { }
>   # ifdef STRUCTLEAK_PLUGIN
>   #  define __user       __attribute__((user))
>   # else
> -#  define __user
> +#  define __user       __attribute__((btf_type_tag("user")))
>   # endif
>   # define __iomem
>   # define __percpu

I've tried the latest LLVM with the above diff and it seems to work!

$ llvm-dwarfdump kernel/bpf/built-in.a |grep -3 btf_type_tag|head
0x00003ace:   DW_TAG_pointer_type

0x00003acf:     DW_TAG_LLVM_annotation
                  DW_AT_name    ("btf_type_tag")
                  DW_AT_const_value    ("user")


Nice!
Didn't notice any warnings. Great.

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

* Re: [PATCH bpf-next 01/10] bpf: Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-10  5:19 ` [PATCH bpf-next 01/10] bpf: " Yonghong Song
@ 2021-11-11 18:27   ` Andrii Nakryiko
  2021-11-11 19:11     ` Yonghong Song
  0 siblings, 1 reply; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:27 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:19 PM Yonghong Song <yhs@fb.com> wrote:
>
> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
> added support for btf_type_tag attributes. This patch
> added support for the kernel.
>
> The main motivation for btf_type_tag is to bring kernel
> annotations __user, __rcu etc. to btf. With such information
> available in btf, bpf verifier can detect mis-usages
> and reject the program. For example, for __user tagged pointer,
> developers can then use proper helper like bpf_probe_read_kernel()

probably meant to write bpf_probe_read_user()?

LGTM, otherwise.

Acked-by: Andrii Nakryiko <andrii@kernel.org>

> etc. to read the data.
>
> BTF_KIND_TYPE_TAG may also useful for other tracing
> facility where instead of to require user to specify
> kernel/user address type, the kernel can detect it
> by itself with btf.
>
>   [1] https://reviews.llvm.org/D111199
>   [2] https://reviews.llvm.org/D113222
>   [3] https://reviews.llvm.org/D113496
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  include/uapi/linux/btf.h       |  3 ++-
>  kernel/bpf/btf.c               | 14 +++++++++++++-
>  tools/include/uapi/linux/btf.h |  3 ++-
>  3 files changed, 17 insertions(+), 3 deletions(-)
>

[...]

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

* Re: [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG
  2021-11-10  5:19 ` [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG Yonghong Song
@ 2021-11-11 18:41   ` Andrii Nakryiko
  2021-11-11 19:13     ` Yonghong Song
  0 siblings, 1 reply; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:41 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add libbpf support for BTF_KIND_TYPE_TAG.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Few nits below.

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/lib/bpf/btf.c             | 23 +++++++++++++++++++++++
>  tools/lib/bpf/btf.h             |  9 ++++++++-
>  tools/lib/bpf/btf_dump.c        |  9 +++++++++
>  tools/lib/bpf/libbpf.c          | 31 ++++++++++++++++++++++++++++++-
>  tools/lib/bpf/libbpf.map        |  1 +
>  tools/lib/bpf/libbpf_internal.h |  2 ++
>  6 files changed, 73 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 7e4c5586bd87..4d9883bef330 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -299,6 +299,7 @@ static int btf_type_size(const struct btf_type *t)
>         case BTF_KIND_TYPEDEF:
>         case BTF_KIND_FUNC:
>         case BTF_KIND_FLOAT:
> +       case BTF_KIND_TYPE_TAG:
>                 return base_size;
>         case BTF_KIND_INT:
>                 return base_size + sizeof(__u32);
> @@ -349,6 +350,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
>         case BTF_KIND_TYPEDEF:
>         case BTF_KIND_FUNC:
>         case BTF_KIND_FLOAT:
> +       case BTF_KIND_TYPE_TAG:
>                 return 0;
>         case BTF_KIND_INT:
>                 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
> @@ -649,6 +651,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
>         case BTF_KIND_VOLATILE:
>         case BTF_KIND_CONST:
>         case BTF_KIND_RESTRICT:
> +       case BTF_KIND_TYPE_TAG:
>                 return btf__align_of(btf, t->type);
>         case BTF_KIND_ARRAY:
>                 return btf__align_of(btf, btf_array(t)->type);
> @@ -2235,6 +2238,22 @@ int btf__add_restrict(struct btf *btf, int ref_type_id)
>         return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
>  }
>
> +/*
> + * Append new BTF_KIND_TYPE_TAGtype with:

missing space

> + *   - *value*, non-empty/non-NULL name;

s/name/tag value/ ? It's not just a name, some tags can have
"parameters", right?

> + *   - *ref_type_id* - referenced type ID, it might not exist yet;
> + * Returns:
> + *   - >0, type ID of newly added BTF type;
> + *   - <0, on error.
> + */
> +int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id)
> +{
> +       if (!value|| !value[0])
> +               return libbpf_err(-EINVAL);
> +
> +       return btf_add_ref_kind(btf, BTF_KIND_TYPE_TAG, value, ref_type_id);
> +}
> +

[...]

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

* Re: [PATCH bpf-next 03/10] bpftool: Support BTF_KIND_TYPE_TAG
  2021-11-10  5:19 ` [PATCH bpf-next 03/10] bpftool: " Yonghong Song
@ 2021-11-11 18:43   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:43 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add bpftool support for BTF_KIND_TYPE_TAG.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/bpf/bpftool/btf.c | 2 ++
>  1 file changed, 2 insertions(+)
>

[...]

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

* Re: [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag()
  2021-11-10  5:20 ` [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag() Yonghong Song
@ 2021-11-11 18:44   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:44 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add unit tests for btf__add_type_tag().
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/testing/selftests/bpf/btf_helpers.c     |  4 +-
>  .../selftests/bpf/prog_tests/btf_write.c      | 67 +++++++++++--------
>  2 files changed, 43 insertions(+), 28 deletions(-)
>

[...]

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

* Re: [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests
  2021-11-10  5:20 ` [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests Yonghong Song
@ 2021-11-11 18:45   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:45 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add BTF_KIND_TYPE_TAG unit tests.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

LGTM.

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/testing/selftests/bpf/prog_tests/btf.c | 18 ++++++++++++++++++
>  tools/testing/selftests/bpf/test_btf.h       |  3 +++
>  2 files changed, 21 insertions(+)
>

[...]

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

* Re: [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication
  2021-11-10  5:20 ` [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication Yonghong Song
@ 2021-11-11 18:49   ` Andrii Nakryiko
  2021-11-11 19:13     ` Yonghong Song
  0 siblings, 1 reply; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:49 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:21 PM Yonghong Song <yhs@fb.com> wrote:
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/btf.c | 46 ++++++++++++++++++--
>  1 file changed, 42 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
> index ebd0ead5f4bc..91b19c41729f 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
> @@ -6889,15 +6889,16 @@ const struct btf_dedup_test dedup_tests[] = {
>                         BTF_RESTRICT_ENC(8),                                            /* [11] restrict */
>                         BTF_FUNC_PROTO_ENC(1, 2),                                       /* [12] func_proto */
>                                 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
> -                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
> +                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
>                         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] 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_TYPE_TAG_ENC(NAME_TBD, 8),                                  /* [18] type_tag */
>                         BTF_END_RAW,
>                 },
> -               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
> +               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
>         },
>         .expect = {
>                 .raw_types = {
> @@ -6918,15 +6919,16 @@ const struct btf_dedup_test dedup_tests[] = {
>                         BTF_RESTRICT_ENC(8),                                            /* [11] restrict */
>                         BTF_FUNC_PROTO_ENC(1, 2),                                       /* [12] func_proto */
>                                 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
> -                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
> +                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
>                         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] 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_TYPE_TAG_ENC(NAME_TBD, 8),                                  /* [18] type_tag */
>                         BTF_END_RAW,
>                 },
> -               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
> +               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
>         },
>         .opts = {
>                 .dont_resolve_fwds = false,
> @@ -7254,6 +7256,42 @@ const struct btf_dedup_test dedup_tests[] = {
>                 .dont_resolve_fwds = false,
>         },
>  },
> +{
> +       .descr = "dedup: btf_tag_type",
> +       .input = {
> +               .raw_types = {
> +                       /* int */
> +                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
> +                       /* tag: tag1, tag2 */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [2] */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),               /* [3] */
> +                       BTF_PTR_ENC(3),                                 /* [4] */
> +                       /* tag: tag1, tag2 */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [5] */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),               /* [6] */
> +                       BTF_PTR_ENC(6),                                 /* [7] */
> +                       /* tag: tag1 */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [8] */
> +                       BTF_PTR_ENC(8),                                 /* [9] */
> +                       BTF_END_RAW,
> +               },
> +               BTF_STR_SEC("\0tag1\0tag2"),
> +       },

Can you please add a test for two more situations:

First, like this:

tag1 -> tag2 -> int
tag1 -> int

tag1's shouldn't be deduped

Second, like this

tag1 -> tag2 -> int
tag2 -> tag1 -> int

Nothing gets deduped.

Actually, also third situation:

tag1 -> int
tag1 -> long

Nothing gets deduped.

That will document expected behavior.

Thanks.

> +       .expect = {
> +               .raw_types = {
> +                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [2] */
> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),               /* [3] */
> +                       BTF_PTR_ENC(3),                                 /* [4] */
> +                       BTF_PTR_ENC(2),                                 /* [5] */
> +                       BTF_END_RAW,
> +               },
> +               BTF_STR_SEC("\0tag1\0tag2"),
> +       },
> +       .opts = {
> +               .dont_resolve_fwds = false,
> +       },
> +},
>
>  };
>
> --
> 2.30.2
>

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

* Re: [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c
  2021-11-10  5:20 ` [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c Yonghong Song
@ 2021-11-11 18:51   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:51 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Rename progs/tag.c to progs/btf_decl_tag.c so we can introduce
> progs/btf_type_tag.c in the next patch.
>
> Also create a subtest for btf_decl_tag in prog_tests/btf_tag.c
> so we can introduce btf_type_tag subtest in the next patch.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  .../selftests/bpf/prog_tests/btf_tag.c        | 20 ++++++++++++-------
>  .../bpf/progs/{tag.c => btf_decl_tag.c}       |  0
>  2 files changed, 13 insertions(+), 7 deletions(-)
>  rename tools/testing/selftests/bpf/progs/{tag.c => btf_decl_tag.c} (100%)
>

[...]

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

* Re: [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag
  2021-11-10  5:20 ` [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag Yonghong Song
@ 2021-11-11 18:55   ` Andrii Nakryiko
  2021-11-11 19:19     ` Yonghong Song
  0 siblings, 1 reply; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:55 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:21 PM Yonghong Song <yhs@fb.com> wrote:
>
> For the C test, compiler the kernel and selftest with clang compiler
> by adding LLVM=1 to the make command line since btf_type_tag is
> only supported by clang compiler now.

I'm confused. Why does kernel compilation matter at all? And then for
progs/*.c we always compile with Clang anyway (except for unused
gcc_bpf flavor, but that's separate). So what am I missing?

>
> The following is the key btf_type_tag usage:
>   #define __tag1 __attribute__((btf_type_tag("tag1")))
>   #define __tag2 __attribute__((btf_type_tag("tag2")))
>   struct btf_type_tag_test {
>        int __tag1 * __tag1 __tag2 *p;
>   } g;
>
> The bpftool raw dump with related types:
>   [4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
>   [11] STRUCT 'btf_type_tag_test' size=8 vlen=1
>           'p' type_id=14 bits_offset=0
>   [12] TYPE_TAG 'tag1' type_id=16
>   [13] TYPE_TAG 'tag2' type_id=12
>   [14] PTR '(anon)' type_id=13
>   [15] TYPE_TAG 'tag1' type_id=4
>   [16] PTR '(anon)' type_id=15
>   [17] VAR 'g' type_id=11, linkage=global
>
> With format C dump, we have
>   struct btf_type_tag_test {
>         int __attribute__((btf_type_tag("tag1"))) * __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
>   };
> The result C code is identical to the original definition except macro's are gone.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  .../selftests/bpf/prog_tests/btf_tag.c        | 24 +++++++++++++++
>  .../selftests/bpf/progs/btf_type_tag.c        | 29 +++++++++++++++++++
>  2 files changed, 53 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
> index d15cc7a88182..88d63e23e35f 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
> @@ -3,6 +3,12 @@
>  #include <test_progs.h>
>  #include "btf_decl_tag.skel.h"
>
> +/* struct btf_type_tag_test is referenced in btf_type_tag.skel.h */
> +struct btf_type_tag_test {
> +        int **p;
> +};
> +#include "btf_type_tag.skel.h"
> +
>  static void test_btf_decl_tag(void)
>  {
>         struct btf_decl_tag *skel;
> @@ -19,8 +25,26 @@ static void test_btf_decl_tag(void)
>         btf_decl_tag__destroy(skel);
>  }
>
> +static void test_btf_type_tag(void)
> +{
> +       struct btf_type_tag *skel;
> +
> +       skel = btf_type_tag__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "btf_type_tag"))
> +               return;
> +
> +       if (skel->rodata->skip_tests) {
> +               printf("%s:SKIP: btf_type_tag attribute not supported", __func__);
> +               test__skip();
> +       }
> +
> +       btf_type_tag__destroy(skel);
> +}
> +
>  void test_btf_tag(void)
>  {
>         if (test__start_subtest("btf_decl_tag"))
>                 test_btf_decl_tag();
> +       if (test__start_subtest("btf_type_tag"))
> +               test_btf_type_tag();
>  }
> diff --git a/tools/testing/selftests/bpf/progs/btf_type_tag.c b/tools/testing/selftests/bpf/progs/btf_type_tag.c
> new file mode 100644
> index 000000000000..0e18c777862c
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/btf_type_tag.c
> @@ -0,0 +1,29 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Facebook */
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +#ifndef __has_attribute
> +#define __has_attribute(x) 0
> +#endif

is this necessary, doesn't the minimum Clang/GCC version that we
support have __has_attribute already?

> +
> +#if __has_attribute(btf_type_tag)
> +#define __tag1 __attribute__((btf_type_tag("tag1")))
> +#define __tag2 __attribute__((btf_type_tag("tag2")))
> +volatile const bool skip_tests = false;
> +#else
> +#define __tag1
> +#define __tag2
> +volatile const bool skip_tests = true;
> +#endif
> +
> +struct btf_type_tag_test {
> +       int __tag1 * __tag1 __tag2 *p;
> +} g;
> +
> +SEC("fentry/bpf_fentry_test1")
> +int BPF_PROG(sub, int x)
> +{
> +  return 0;
> +}
> --
> 2.30.2
>

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

* Re: [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest
  2021-11-10  5:20 ` [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest Yonghong Song
@ 2021-11-11 18:56   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:56 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> btf_tag selftest needs certain llvm versions (>= llvm14).
> Make it clear in the selftests README.rst file.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/testing/selftests/bpf/README.rst | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>

[...]

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

* Re: [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support
  2021-11-10  5:20 ` [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support Yonghong Song
@ 2021-11-11 18:56   ` Andrii Nakryiko
  0 siblings, 0 replies; 30+ messages in thread
From: Andrii Nakryiko @ 2021-11-11 18:56 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team

On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add BTF_KIND_TYPE_TAG documentation in btf.rst.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  Documentation/bpf/btf.rst | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
>

[...]

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

* Re: [PATCH bpf-next 01/10] bpf: Support BTF_KIND_TYPE_TAG for btf_type_tag attributes
  2021-11-11 18:27   ` Andrii Nakryiko
@ 2021-11-11 19:11     ` Yonghong Song
  0 siblings, 0 replies; 30+ messages in thread
From: Yonghong Song @ 2021-11-11 19:11 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team



On 11/11/21 10:27 AM, Andrii Nakryiko wrote:
> On Tue, Nov 9, 2021 at 9:19 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> LLVM patches ([1] for clang, [2] and [3] for BPF backend)
>> added support for btf_type_tag attributes. This patch
>> added support for the kernel.
>>
>> The main motivation for btf_type_tag is to bring kernel
>> annotations __user, __rcu etc. to btf. With such information
>> available in btf, bpf verifier can detect mis-usages
>> and reject the program. For example, for __user tagged pointer,
>> developers can then use proper helper like bpf_probe_read_kernel()
> 
> probably meant to write bpf_probe_read_user()?

Oh, yes, a typo. should be bpf_probe_read_user().

> 
> LGTM, otherwise.
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> 
>> etc. to read the data.
>>
>> BTF_KIND_TYPE_TAG may also useful for other tracing
>> facility where instead of to require user to specify
>> kernel/user address type, the kernel can detect it
>> by itself with btf.
>>
>>    [1] https://reviews.llvm.org/D111199
>>    [2] https://reviews.llvm.org/D113222
>>    [3] https://reviews.llvm.org/D113496
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   include/uapi/linux/btf.h       |  3 ++-
>>   kernel/bpf/btf.c               | 14 +++++++++++++-
>>   tools/include/uapi/linux/btf.h |  3 ++-
>>   3 files changed, 17 insertions(+), 3 deletions(-)
>>
> 
> [...]
> 

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

* Re: [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG
  2021-11-11 18:41   ` Andrii Nakryiko
@ 2021-11-11 19:13     ` Yonghong Song
  0 siblings, 0 replies; 30+ messages in thread
From: Yonghong Song @ 2021-11-11 19:13 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team



On 11/11/21 10:41 AM, Andrii Nakryiko wrote:
> On Tue, Nov 9, 2021 at 9:20 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> Add libbpf support for BTF_KIND_TYPE_TAG.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
> 
> Few nits below.
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> 
>>   tools/lib/bpf/btf.c             | 23 +++++++++++++++++++++++
>>   tools/lib/bpf/btf.h             |  9 ++++++++-
>>   tools/lib/bpf/btf_dump.c        |  9 +++++++++
>>   tools/lib/bpf/libbpf.c          | 31 ++++++++++++++++++++++++++++++-
>>   tools/lib/bpf/libbpf.map        |  1 +
>>   tools/lib/bpf/libbpf_internal.h |  2 ++
>>   6 files changed, 73 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
>> index 7e4c5586bd87..4d9883bef330 100644
>> --- a/tools/lib/bpf/btf.c
>> +++ b/tools/lib/bpf/btf.c
>> @@ -299,6 +299,7 @@ static int btf_type_size(const struct btf_type *t)
>>          case BTF_KIND_TYPEDEF:
>>          case BTF_KIND_FUNC:
>>          case BTF_KIND_FLOAT:
>> +       case BTF_KIND_TYPE_TAG:
>>                  return base_size;
>>          case BTF_KIND_INT:
>>                  return base_size + sizeof(__u32);
>> @@ -349,6 +350,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
>>          case BTF_KIND_TYPEDEF:
>>          case BTF_KIND_FUNC:
>>          case BTF_KIND_FLOAT:
>> +       case BTF_KIND_TYPE_TAG:
>>                  return 0;
>>          case BTF_KIND_INT:
>>                  *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
>> @@ -649,6 +651,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
>>          case BTF_KIND_VOLATILE:
>>          case BTF_KIND_CONST:
>>          case BTF_KIND_RESTRICT:
>> +       case BTF_KIND_TYPE_TAG:
>>                  return btf__align_of(btf, t->type);
>>          case BTF_KIND_ARRAY:
>>                  return btf__align_of(btf, btf_array(t)->type);
>> @@ -2235,6 +2238,22 @@ int btf__add_restrict(struct btf *btf, int ref_type_id)
>>          return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
>>   }
>>
>> +/*
>> + * Append new BTF_KIND_TYPE_TAGtype with:
> 
> missing space

Ack.

> 
>> + *   - *value*, non-empty/non-NULL name;
> 
> s/name/tag value/ ? It's not just a name, some tags can have
> "parameters", right?

copy-paste issue (I copied from btf__add_typedef).
Yes, tag value is the correct terminology.

> 
>> + *   - *ref_type_id* - referenced type ID, it might not exist yet;
>> + * Returns:
>> + *   - >0, type ID of newly added BTF type;
>> + *   - <0, on error.
>> + */
>> +int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id)
>> +{
>> +       if (!value|| !value[0])
>> +               return libbpf_err(-EINVAL);
>> +
>> +       return btf_add_ref_kind(btf, BTF_KIND_TYPE_TAG, value, ref_type_id);
>> +}
>> +
> 
> [...]
> 

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

* Re: [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication
  2021-11-11 18:49   ` Andrii Nakryiko
@ 2021-11-11 19:13     ` Yonghong Song
  0 siblings, 0 replies; 30+ messages in thread
From: Yonghong Song @ 2021-11-11 19:13 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team



On 11/11/21 10:49 AM, Andrii Nakryiko wrote:
> On Tue, Nov 9, 2021 at 9:21 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   tools/testing/selftests/bpf/prog_tests/btf.c | 46 ++++++++++++++++++--
>>   1 file changed, 42 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
>> index ebd0ead5f4bc..91b19c41729f 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
>> @@ -6889,15 +6889,16 @@ const struct btf_dedup_test dedup_tests[] = {
>>                          BTF_RESTRICT_ENC(8),                                            /* [11] restrict */
>>                          BTF_FUNC_PROTO_ENC(1, 2),                                       /* [12] func_proto */
>>                                  BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
>> -                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
>> +                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
>>                          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] 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_TYPE_TAG_ENC(NAME_TBD, 8),                                  /* [18] type_tag */
>>                          BTF_END_RAW,
>>                  },
>> -               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
>> +               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
>>          },
>>          .expect = {
>>                  .raw_types = {
>> @@ -6918,15 +6919,16 @@ const struct btf_dedup_test dedup_tests[] = {
>>                          BTF_RESTRICT_ENC(8),                                            /* [11] restrict */
>>                          BTF_FUNC_PROTO_ENC(1, 2),                                       /* [12] func_proto */
>>                                  BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
>> -                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
>> +                               BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
>>                          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] 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_TYPE_TAG_ENC(NAME_TBD, 8),                                  /* [18] type_tag */
>>                          BTF_END_RAW,
>>                  },
>> -               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"),
>> +               BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
>>          },
>>          .opts = {
>>                  .dont_resolve_fwds = false,
>> @@ -7254,6 +7256,42 @@ const struct btf_dedup_test dedup_tests[] = {
>>                  .dont_resolve_fwds = false,
>>          },
>>   },
>> +{
>> +       .descr = "dedup: btf_tag_type",
>> +       .input = {
>> +               .raw_types = {
>> +                       /* int */
>> +                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
>> +                       /* tag: tag1, tag2 */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [2] */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),               /* [3] */
>> +                       BTF_PTR_ENC(3),                                 /* [4] */
>> +                       /* tag: tag1, tag2 */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [5] */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),               /* [6] */
>> +                       BTF_PTR_ENC(6),                                 /* [7] */
>> +                       /* tag: tag1 */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [8] */
>> +                       BTF_PTR_ENC(8),                                 /* [9] */
>> +                       BTF_END_RAW,
>> +               },
>> +               BTF_STR_SEC("\0tag1\0tag2"),
>> +       },
> 
> Can you please add a test for two more situations:
> 
> First, like this:
> 
> tag1 -> tag2 -> int
> tag1 -> int
> 
> tag1's shouldn't be deduped
> 
> Second, like this
> 
> tag1 -> tag2 -> int
> tag2 -> tag1 -> int
> 
> Nothing gets deduped.
> 
> Actually, also third situation:
> 
> tag1 -> int
> tag1 -> long
> 
> Nothing gets deduped.
> 
> That will document expected behavior.

Will do.

> 
> Thanks.
> 
>> +       .expect = {
>> +               .raw_types = {
>> +                       BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),               /* [2] */
>> +                       BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),               /* [3] */
>> +                       BTF_PTR_ENC(3),                                 /* [4] */
>> +                       BTF_PTR_ENC(2),                                 /* [5] */
>> +                       BTF_END_RAW,
>> +               },
>> +               BTF_STR_SEC("\0tag1\0tag2"),
>> +       },
>> +       .opts = {
>> +               .dont_resolve_fwds = false,
>> +       },
>> +},
>>
>>   };
>>
>> --
>> 2.30.2
>>

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

* Re: [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag
  2021-11-11 18:55   ` Andrii Nakryiko
@ 2021-11-11 19:19     ` Yonghong Song
  0 siblings, 0 replies; 30+ messages in thread
From: Yonghong Song @ 2021-11-11 19:19 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jose E . Marchesi, Kernel Team



On 11/11/21 10:55 AM, Andrii Nakryiko wrote:
> On Tue, Nov 9, 2021 at 9:21 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> For the C test, compiler the kernel and selftest with clang compiler
>> by adding LLVM=1 to the make command line since btf_type_tag is
>> only supported by clang compiler now.
> 
> I'm confused. Why does kernel compilation matter at all? And then for
> progs/*.c we always compile with Clang anyway (except for unused
> gcc_bpf flavor, but that's separate). So what am I missing?

This patch set is tested with additional change with
   #define __user __attribute__((btf_type_tag("user")))
plus pahole hack so I can ensure kernel implementation is
okay with vmlinux + btf_type_tag.
LLVM=1 is needed to test this.

But just for this patch set, you are right, LLVM=1 is not needed.
Will remove it.

> 
>>
>> The following is the key btf_type_tag usage:
>>    #define __tag1 __attribute__((btf_type_tag("tag1")))
>>    #define __tag2 __attribute__((btf_type_tag("tag2")))
>>    struct btf_type_tag_test {
>>         int __tag1 * __tag1 __tag2 *p;
>>    } g;
>>
>> The bpftool raw dump with related types:
>>    [4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
>>    [11] STRUCT 'btf_type_tag_test' size=8 vlen=1
>>            'p' type_id=14 bits_offset=0
>>    [12] TYPE_TAG 'tag1' type_id=16
>>    [13] TYPE_TAG 'tag2' type_id=12
>>    [14] PTR '(anon)' type_id=13
>>    [15] TYPE_TAG 'tag1' type_id=4
>>    [16] PTR '(anon)' type_id=15
>>    [17] VAR 'g' type_id=11, linkage=global
>>
>> With format C dump, we have
>>    struct btf_type_tag_test {
>>          int __attribute__((btf_type_tag("tag1"))) * __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag2"))) *p;
>>    };
>> The result C code is identical to the original definition except macro's are gone.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   .../selftests/bpf/prog_tests/btf_tag.c        | 24 +++++++++++++++
>>   .../selftests/bpf/progs/btf_type_tag.c        | 29 +++++++++++++++++++
>>   2 files changed, 53 insertions(+)
>>   create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag.c
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
>> index d15cc7a88182..88d63e23e35f 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
>> @@ -3,6 +3,12 @@
>>   #include <test_progs.h>
>>   #include "btf_decl_tag.skel.h"
>>
>> +/* struct btf_type_tag_test is referenced in btf_type_tag.skel.h */
>> +struct btf_type_tag_test {
>> +        int **p;
>> +};
>> +#include "btf_type_tag.skel.h"
>> +
>>   static void test_btf_decl_tag(void)
>>   {
>>          struct btf_decl_tag *skel;
>> @@ -19,8 +25,26 @@ static void test_btf_decl_tag(void)
>>          btf_decl_tag__destroy(skel);
>>   }
>>
>> +static void test_btf_type_tag(void)
>> +{
>> +       struct btf_type_tag *skel;
>> +
>> +       skel = btf_type_tag__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "btf_type_tag"))
>> +               return;
>> +
>> +       if (skel->rodata->skip_tests) {
>> +               printf("%s:SKIP: btf_type_tag attribute not supported", __func__);
>> +               test__skip();
>> +       }
>> +
>> +       btf_type_tag__destroy(skel);
>> +}
>> +
>>   void test_btf_tag(void)
>>   {
>>          if (test__start_subtest("btf_decl_tag"))
>>                  test_btf_decl_tag();
>> +       if (test__start_subtest("btf_type_tag"))
>> +               test_btf_type_tag();
>>   }
>> diff --git a/tools/testing/selftests/bpf/progs/btf_type_tag.c b/tools/testing/selftests/bpf/progs/btf_type_tag.c
>> new file mode 100644
>> index 000000000000..0e18c777862c
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/btf_type_tag.c
>> @@ -0,0 +1,29 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2021 Facebook */
>> +#include "vmlinux.h"
>> +#include <bpf/bpf_helpers.h>
>> +#include <bpf/bpf_tracing.h>
>> +
>> +#ifndef __has_attribute
>> +#define __has_attribute(x) 0
>> +#endif
> 
> is this necessary, doesn't the minimum Clang/GCC version that we
> support have __has_attribute already?

No. It is not necessary. I just copy-pasted code from
https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute

We recommend clang >= 11 for kernel. So above is indeed not
needed.

> 
>> +
>> +#if __has_attribute(btf_type_tag)
>> +#define __tag1 __attribute__((btf_type_tag("tag1")))
>> +#define __tag2 __attribute__((btf_type_tag("tag2")))
>> +volatile const bool skip_tests = false;
>> +#else
>> +#define __tag1
>> +#define __tag2
>> +volatile const bool skip_tests = true;
>> +#endif
>> +
>> +struct btf_type_tag_test {
>> +       int __tag1 * __tag1 __tag2 *p;
>> +} g;
>> +
>> +SEC("fentry/bpf_fentry_test1")
>> +int BPF_PROG(sub, int x)
>> +{
>> +  return 0;
>> +}
>> --
>> 2.30.2
>>

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

end of thread, other threads:[~2021-11-11 19:20 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-10  5:19 [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Yonghong Song
2021-11-10  5:19 ` [PATCH bpf-next 01/10] bpf: " Yonghong Song
2021-11-11 18:27   ` Andrii Nakryiko
2021-11-11 19:11     ` Yonghong Song
2021-11-10  5:19 ` [PATCH bpf-next 02/10] libbpf: Support BTF_KIND_TYPE_TAG Yonghong Song
2021-11-11 18:41   ` Andrii Nakryiko
2021-11-11 19:13     ` Yonghong Song
2021-11-10  5:19 ` [PATCH bpf-next 03/10] bpftool: " Yonghong Song
2021-11-11 18:43   ` Andrii Nakryiko
2021-11-10  5:20 ` [PATCH bpf-next 04/10] selftests/bpf: Test libbpf API function btf__add_type_tag() Yonghong Song
2021-11-11 18:44   ` Andrii Nakryiko
2021-11-10  5:20 ` [PATCH bpf-next 05/10] selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests Yonghong Song
2021-11-11 18:45   ` Andrii Nakryiko
2021-11-10  5:20 ` [PATCH bpf-next 06/10] selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication Yonghong Song
2021-11-11 18:49   ` Andrii Nakryiko
2021-11-11 19:13     ` Yonghong Song
2021-11-10  5:20 ` [PATCH bpf-next 07/10] selftests/bpf: Rename progs/tag.c to progs/btf_decl_tag.c Yonghong Song
2021-11-11 18:51   ` Andrii Nakryiko
2021-11-10  5:20 ` [PATCH bpf-next 08/10] selftests/bpf: Add a C test for btf_type_tag Yonghong Song
2021-11-11 18:55   ` Andrii Nakryiko
2021-11-11 19:19     ` Yonghong Song
2021-11-10  5:20 ` [PATCH bpf-next 09/10] selftests/bpf: Clarify llvm dependency with btf_tag selftest Yonghong Song
2021-11-11 18:56   ` Andrii Nakryiko
2021-11-10  5:20 ` [PATCH bpf-next 10/10] docs/bpf: Update documentation for BTF_KIND_TYPE_TAG support Yonghong Song
2021-11-11 18:56   ` Andrii Nakryiko
2021-11-10  5:28 ` [PATCH bpf-next 00/10] Support BTF_KIND_TYPE_TAG for btf_type_tag attributes Alexei Starovoitov
2021-11-10  6:26   ` Yonghong Song
2021-11-10 16:40     ` Alexei Starovoitov
2021-11-10 17:04       ` Yonghong Song
2021-11-11  1:47         ` 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.