Skip to contentSkip to conversation

BTF: BPF Type Format

PATCH In review Martin KaFai Lau 7 participants 38 messages Apr 18, 2018 – Jun 15, 2018

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v5 cover BTF: BPF Type Format

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Martin KaFai Lau (10):
bpf: btf: Introduce BPF Type Format (BTF)
bpf: btf: Validate type reference
bpf: btf: Check members of struct/union
bpf: btf: Add pretty print capability for data with BTF type info
bpf: btf: Add BPF_BTF_LOAD command
bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd
bpf: btf: Add pretty print support to the basic arraymap
bpf: btf: Sync bpf.h and btf.h to tools/
bpf: btf: Add BTF support to libbpf
bpf: btf: Add BTF tests

 include/linux/bpf.h                          |   20 +-
 include/linux/btf.h                          |   48 +
 include/uapi/linux/bpf.h                     |   12 +
 include/uapi/linux/btf.h                     |  130 ++
 kernel/bpf/Makefile                          |    1 +
 kernel/bpf/arraymap.c                        |   50 +
 kernel/bpf/btf.c                             | 2064 ++++++++++++++++++++++++++
 kernel/bpf/inode.c                           |  156 +-
 kernel/bpf/syscall.c                         |   51 +-
 tools/include/uapi/linux/bpf.h               |   12 +
 tools/include/uapi/linux/btf.h               |  130 ++
 tools/lib/bpf/Build                          |    2 +-
 tools/lib/bpf/bpf.c                          |   92 +-
 tools/lib/bpf/bpf.h                          |   16 +
 tools/lib/bpf/btf.c                          |  374 +++++
 tools/lib/bpf/btf.h                          |   22 +
 tools/lib/bpf/libbpf.c                       |  148 +-
 tools/lib/bpf/libbpf.h                       |    3 +
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 22 files changed, 5076 insertions(+), 41 deletions(-)
 create mode 100644 include/linux/btf.h
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
 create mode 100644 tools/include/uapi/linux/btf.h
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
Signature · 1 line
2.9.5
#2 lore raw

PATCH v5 2/10 bpf: btf: Validate type reference

After collecting all btf_type in the first pass in an earlier patch, the second pass (in this patch) can validate the reference types (e.g. the referring type does exist and it does not refer to itself).

While checking the reference type, it also gathers other information (e.g. the size of an array). This info will be useful in checking the struct's members in a later patch. They will also be useful in doing pretty print later.

---
 include/linux/btf.h |  37 +++
 kernel/bpf/btf.c    | 666 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 702 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/btf.h
include/linux/btf.h new file +37 -0
new file mode 100644
index 000000000000..f14b60368753
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2018 Facebook */
+
+#ifndef _LINUX_BTF_H
+#define _LINUX_BTF_H 1
+
+#include <linux/types.h>
+
+struct btf;
+struct btf_type;
+
+/* Figure out the size of a type_id.  If type_id is a modifier
+ * (e.g. const), it will be resolved to find out the type with size.
+ *
+ * For example:
+ * In describing "const void *",  type_id is "const" and "const"
+ * refers to "void *".  The return type will be "void *".
+ *
+ * If type_id is a simple "int", then return type will be "int".
+ *
+ * @btf: struct btf object
+ * @type_id: Find out the size of type_id. The type_id of the return
+ *           type is set to *type_id.
+ * @ret_size: It can be NULL.  If not NULL, the size of the return
+ *            type is set to *ret_size.
+ * Return: The btf_type (resolved to another type with size info if needed).
+ *         NULL is returned if type_id itself does not have size info
+ *         (e.g. void) or it cannot be resolved to another type that
+ *         has size info.
+ *         *type_id and *ret_size will not be changed in the
+ *         NULL return case.
+ */
+const struct btf_type *btf_type_id_size(const struct btf *btf,
+					u32 *type_id,
+					u32 *ret_size);
+
+#endif
kernel/bpf/btf.c +665 -1

799 lines not on this page · raw message

Signature · 1 line
2.9.5
#3 lore raw

PATCH v5 3/10 bpf: btf: Check members of struct/union

In reply to Martin KaFai Lau

This patch checks a few things of struct's members:

1) It has a valid size (e.g. a "const void" is invalid)
2) A member's size (+ its member's offset) does not exceed the containing struct's size.
3) The member's offset satisfies the alignment requirement

The above can only be done after the needs_resolve member's type is resolved. Hence, the above is done together in btf_struct_resolve().

Each possible member's type (e.g. int, enum, modifier...) implements the check_member() ops which will be called from btf_struct_resolve().

---
 kernel/bpf/btf.c | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 205 insertions(+)
kernel/bpf/btf.c +205 -0
index 18bf266ceeda..4e31249f6c61 100644
@@ -250,6 +250,10 @@ struct btf_kind_operations {
 			  u32 meta_left);
 	int (*resolve)(struct btf_verifier_env *env,
 		       const struct resolve_vertex *v);
+	int (*check_member)(struct btf_verifier_env *env,
+			    const struct btf_type *struct_type,
+			    const struct btf_member *member,
+			    const struct btf_type *member_type);
 	void (*log_details)(struct btf_verifier_env *env,
 			    const struct btf_type *t);
 };
@@ -760,6 +764,16 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,
 	return size_type;
 }
 
+static int btf_df_check_member(struct btf_verifier_env *env,
+			       const struct btf_type *struct_type,
+			       const struct btf_member *member,
+			       const struct btf_type *member_type)
+{
+	btf_verifier_log_basic(env, struct_type,
+			       "Unsupported check_member");
+	return -EINVAL;
+}
+
 static int btf_df_resolve(struct btf_verifier_env *env,
 			  const struct resolve_vertex *v)
 {
@@ -767,6 +781,44 @@ static int btf_df_resolve(struct btf_verifier_env *env,
 	return -EINVAL;
 }
 
+static int btf_int_check_member(struct btf_verifier_env *env,
+				const struct btf_type *struct_type,
+				const struct btf_member *member,
+				const struct btf_type *member_type)
+{
+	u32 int_data = btf_type_int(member_type);
+	u32 struct_bits_off = member->offset;
+	u32 struct_size = struct_type->size;
+	u32 nr_copy_bits;
+	u32 bytes_offset;
+
+	if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"bits_offset exceeds U32_MAX");
+		return -EINVAL;
+	}
+
+	struct_bits_off += BTF_INT_OFFSET(int_data);
+	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
+	nr_copy_bits = BTF_INT_BITS(int_data) +
+		BITS_PER_BYTE_MASKED(struct_bits_off);
+
+	if (nr_copy_bits > BITS_PER_U64) {
+		btf_verifier_log_member(env, struct_type, member,
+					"nr_copy_bits exceeds 64");
+		return -EINVAL;
+	}
+
+	if (struct_size < bytes_offset ||
+	    struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static s32 btf_int_check_meta(struct btf_verifier_env *env,
 			      const struct btf_type *t,
 			      u32 meta_left)
@@ -830,9 +882,61 @@ static void btf_int_log(struct btf_verifier_env *env,
 static const struct btf_kind_operations int_ops = {
 	.check_meta = btf_int_check_meta,
 	.resolve = btf_df_resolve,
+	.check_member = btf_int_check_member,
 	.log_details = btf_int_log,
 };
 
+static int btf_modifier_check_member(struct btf_verifier_env *env,
+				     const struct btf_type *struct_type,
+				     const struct btf_member *member,
+				     const struct btf_type *member_type)
+{
+	const struct btf_type *resolved_type;
+	u32 resolved_type_id = member->type;
+	struct btf_member resolved_member;
+	struct btf *btf = env->btf;
+
+	resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
+	if (!resolved_type) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Invalid member");
+		return -EINVAL;
+	}
+
+	resolved_member = *member;
+	resolved_member.type = resolved_type_id;
+
+	return btf_type_ops(resolved_type)->check_member(env, struct_type,
+							 &resolved_member,
+							 resolved_type);
+}
+
+static int btf_ptr_check_member(struct btf_verifier_env *env,
+				const struct btf_type *struct_type,
+				const struct btf_member *member,
+				const struct btf_type *member_type)
+{
+	u32 struct_size, struct_bits_off, bytes_offset;
+
+	struct_size = struct_type->size;
+	struct_bits_off = member->offset;
+	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
+
+	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member is not byte aligned");
+		return -EINVAL;
+	}
+
+	if (struct_size - bytes_offset < sizeof(void *)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int btf_ref_type_check_meta(struct btf_verifier_env *env,
 				   const struct btf_type *t,
 				   u32 meta_left)
@@ -959,21 +1063,53 @@ static void btf_ref_type_log(struct btf_verifier_env *env,
 static struct btf_kind_operations modifier_ops = {
 	.check_meta = btf_ref_type_check_meta,
 	.resolve = btf_modifier_resolve,
+	.check_member = btf_modifier_check_member,
 	.log_details = btf_ref_type_log,
 };
 
 static struct btf_kind_operations ptr_ops = {
 	.check_meta = btf_ref_type_check_meta,
 	.resolve = btf_ptr_resolve,
+	.check_member = btf_ptr_check_member,
 	.log_details = btf_ref_type_log,
 };
 
 static struct btf_kind_operations fwd_ops = {
 	.check_meta = btf_ref_type_check_meta,
 	.resolve = btf_df_resolve,
+	.check_member = btf_df_check_member,
 	.log_details = btf_ref_type_log,
 };
 
+static int btf_array_check_member(struct btf_verifier_env *env,
+				  const struct btf_type *struct_type,
+				  const struct btf_member *member,
+				  const struct btf_type *member_type)
+{
+	u32 struct_bits_off = member->offset;
+	u32 struct_size, bytes_offset;
+	u32 array_type_id, array_size;
+	struct btf *btf = env->btf;
+
+	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member is not byte aligned");
+		return -EINVAL;
+	}
+
+	array_type_id = member->type;
+	btf_type_id_size(btf, &array_type_id, &array_size);
+	struct_size = struct_type->size;
+	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
+	if (struct_size - bytes_offset < array_size) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static s32 btf_array_check_meta(struct btf_verifier_env *env,
 				const struct btf_type *t,
 				u32 meta_left)
@@ -1076,9 +1212,35 @@ static void btf_array_log(struct btf_verifier_env *env,
 static struct btf_kind_operations array_ops = {
 	.check_meta = btf_array_check_meta,
 	.resolve = btf_array_resolve,
+	.check_member = btf_array_check_member,
 	.log_details = btf_array_log,
 };
 
+static int btf_struct_check_member(struct btf_verifier_env *env,
+				   const struct btf_type *struct_type,
+				   const struct btf_member *member,
+				   const struct btf_type *member_type)
+{
+	u32 struct_bits_off = member->offset;
+	u32 struct_size, bytes_offset;
+
+	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member is not byte aligned");
+		return -EINVAL;
+	}
+
+	struct_size = struct_type->size;
+	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
+	if (struct_size - bytes_offset < member_type->size) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static s32 btf_struct_check_meta(struct btf_verifier_env *env,
 				 const struct btf_type *t,
 				 u32 meta_left)
@@ -1137,6 +1299,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env,
 			      const struct resolve_vertex *v)
 {
 	const struct btf_member *member;
+	int err;
 	u16 i;
 
 	/* Before continue resolving the next_member,
@@ -1144,6 +1307,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env,
 	 * type with size info.
 	 */
 	if (v->next_member) {
+		const struct btf_type *last_member_type;
 		const struct btf_member *last_member;
 		u16 last_member_type_id;
 
@@ -1152,6 +1316,14 @@ static int btf_struct_resolve(struct btf_verifier_env *env,
 		if (WARN_ON_ONCE(!env_type_is_resolved(env,
 						       last_member_type_id)))
 			return -EINVAL;
+
+		last_member_type = btf_type_by_id(env->btf,
+						  last_member_type_id);
+		err = btf_type_ops(last_member_type)->check_member(env, v->t,
+							last_member,
+							last_member_type);
+		if (err)
+			return err;
 	}
 
 	for_each_member_from(i, v->next_member, v->t, member) {
@@ -1170,6 +1342,12 @@ static int btf_struct_resolve(struct btf_verifier_env *env,
 			env_stack_set_next_member(env, i + 1);
 			return env_stack_push(env, member_type, member_type_id);
 		}
+
+		err = btf_type_ops(member_type)->check_member(env, v->t,
+							      member,
+							      member_type);
+		if (err)
+			return err;
 	}
 
 	env_stack_pop_resolved(env, 0, 0);
@@ -1186,9 +1364,35 @@ static void btf_struct_log(struct btf_verifier_env *env,
 static struct btf_kind_operations struct_ops = {
 	.check_meta = btf_struct_check_meta,
 	.resolve = btf_struct_resolve,
+	.check_member = btf_struct_check_member,
 	.log_details = btf_struct_log,
 };
 
+static int btf_enum_check_member(struct btf_verifier_env *env,
+				 const struct btf_type *struct_type,
+				 const struct btf_member *member,
+				 const struct btf_type *member_type)
+{
+	u32 struct_bits_off = member->offset;
+	u32 struct_size, bytes_offset;
+
+	if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member is not byte aligned");
+		return -EINVAL;
+	}
+
+	struct_size = struct_type->size;
+	bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
+	if (struct_size - bytes_offset < sizeof(int)) {
+		btf_verifier_log_member(env, struct_type, member,
+					"Member exceeds struct_size");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static s32 btf_enum_check_meta(struct btf_verifier_env *env,
 			       const struct btf_type *t,
 			       u32 meta_left)
@@ -1240,6 +1444,7 @@ static void btf_enum_log(struct btf_verifier_env *env,
 static struct btf_kind_operations enum_ops = {
 	.check_meta = btf_enum_check_meta,
 	.resolve = btf_df_resolve,
+	.check_member = btf_enum_check_member,
 	.log_details = btf_enum_log,
 };
 
Signature · 1 line
2.9.5
#8 lore raw

PATCH v5 8/10 bpf: btf: Sync bpf.h and btf.h to tools/

In reply to Martin KaFai Lau

This patch sync up the bpf.h and btf.h to tools/

---
 tools/include/uapi/linux/bpf.h |  12 ++++
 tools/include/uapi/linux/btf.h | 130 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)
 create mode 100644 tools/include/uapi/linux/btf.h
tools/include/uapi/linux/bpf.h +12 -0
index 9d07465023a2..33107232e1f0 100644
@@ -95,6 +95,7 @@ enum bpf_cmd {
 	BPF_OBJ_GET_INFO_BY_FD,
 	BPF_PROG_QUERY,
 	BPF_RAW_TRACEPOINT_OPEN,
+	BPF_BTF_LOAD,
 };
 
 enum bpf_map_type {
@@ -279,6 +280,9 @@ union bpf_attr {
 					 */
 		char	map_name[BPF_OBJ_NAME_LEN];
 		__u32	map_ifindex;	/* ifindex of netdev to create on */
+		__u32	btf_fd;		/* fd pointing to a BTF type data */
+		__u32	btf_key_id;	/* BTF type_id of the key */
+		__u32	btf_value_id;	/* BTF type_id of the value */
 	};
 
 	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -363,6 +367,14 @@ union bpf_attr {
 		__u64 name;
 		__u32 prog_fd;
 	} raw_tracepoint;
+
+	struct { /* anonymous struct for BPF_BTF_LOAD */
+		__aligned_u64	btf;
+		__aligned_u64	btf_log_buf;
+		__u32		btf_size;
+		__u32		btf_log_size;
+		__u32		btf_log_level;
+	};
 } __attribute__((aligned(8)));
 
 /* BPF helper function descriptions:
tools/include/uapi/linux/btf.h new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.9.5
#1 lore raw

PATCH v5 1/10 bpf: btf: Introduce BPF Type Format (BTF)

In reply to Martin KaFai Lau

This patch introduces BPF type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

BTF has its root from CTF (Compact C-Type format). To simplify the handling of BTF data, BTF removes the differences between small and big type/struct-member. Hence, BTF consistently uses u32 instead of supporting both "one u16" and "two u32 (+padding)" in describing type and struct-member.

It also raises the number of types (and functions) limit from 0x7fff to 0x7fffffff.

Due to the above changes, the format is not compatible to CTF. Hence, BTF starts with a new BTF_MAGIC and version number.

This patch does the first verification pass to the BTF. The first pass checks:
1. meta-data size (e.g. It does not go beyond the total btf's size)
2. name_offset is valid
3. Each BTF_KIND (e.g. int, enum, struct....) does its own check of its meta-data.

Some other checks, like checking a struct's member is referring to a valid type, can only be done in the second pass. The second verification pass will be implemented in the next patch.

---
 include/uapi/linux/btf.h | 130 +++++++
 kernel/bpf/Makefile      |   1 +
 kernel/bpf/btf.c         | 915 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1046 insertions(+)
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
include/uapi/linux/btf.h new file +130 -0
new file mode 100644
index 000000000000..74a30b1090df
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/* Copyright (c) 2018 Facebook */
+#ifndef _UAPI__LINUX_BTF_H__
+#define _UAPI__LINUX_BTF_H__
+
+#include <linux/types.h>
+
+#define BTF_MAGIC	0xeB9F
+#define BTF_MAGIC_SWAP	0x9FeB
+#define BTF_VERSION	1
+#define BTF_FLAGS_COMPR	0x01
+
+struct btf_header {
+	__u16	magic;
+	__u8	version;
+	__u8	flags;
+
+	__u32	parent_label;
+	__u32	parent_name;
+
+	/* All offsets are in bytes relative to the end of this header */
+	__u32	label_off;	/* offset of label section	*/
+	__u32	object_off;	/* offset of data object section*/
+	__u32	func_off;	/* offset of function section	*/
+	__u32	type_off;	/* offset of type section	*/
+	__u32	str_off;	/* offset of string section	*/
+	__u32	str_len;	/* length of string section	*/
+};
+
+/* Max # of type identifier */
+#define BTF_MAX_TYPE	0x7fffffff
+/* Max offset into the string section */
+#define BTF_MAX_NAME_OFFSET	0x7fffffff
+/* Max # of struct/union/enum members or func args */
+#define BTF_MAX_VLEN	0xffff
+
+/* The type id is referring to a parent BTF */
+#define BTF_TYPE_PARENT(id)	(((id) >> 31) & 0x1)
+#define BTF_TYPE_ID(id)		((id) & BTF_MAX_TYPE)
+
+/* String is in the ELF string section */
+#define BTF_STR_TBL_ELF_ID(ref)	(((ref) >> 31) & 0x1)
+#define BTF_STR_OFFSET(ref)	((ref) & BTF_MAX_NAME_OFFSET)
+
+struct btf_type {
+	__u32 name;
+	/* "info" bits arrangement
+	 * bits  0-15: vlen (e.g. # of struct's members)
+	 * bits 16-23: unused
+	 * bits 24-28: kind (e.g. int, ptr, array...etc)
+	 * bits 29-30: unused
+	 * bits    31: root
+	 */
+	__u32 info;
+	/* "size" is used by INT, ENUM, STRUCT and UNION.
+	 * "size" tells the size of the type it is describing.
+	 *
+	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT.
+	 * "type" is a type_id referring to another type.
+	 */
+	union {
+		__u32 size;
+		__u32 type;
+	};
+};
+
+#define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
+#define BTF_INFO_ISROOT(info)	(!!(((info) >> 24) & 0x80))
+#define BTF_INFO_VLEN(info)	((info) & 0xffff)
+
+#define BTF_KIND_UNKN		0	/* Unknown	*/
+#define BTF_KIND_INT		1	/* Integer	*/
+#define BTF_KIND_PTR		2	/* Pointer	*/
+#define BTF_KIND_ARRAY		3	/* Array	*/
+#define BTF_KIND_STRUCT		4	/* Struct	*/
+#define BTF_KIND_UNION		5	/* Union	*/
+#define BTF_KIND_ENUM		6	/* Enumeration	*/
+#define BTF_KIND_FWD		7	/* Forward	*/
+#define BTF_KIND_TYPEDEF	8	/* Typedef	*/
+#define BTF_KIND_VOLATILE	9	/* Volatile	*/
+#define BTF_KIND_CONST		10	/* Const	*/
+#define BTF_KIND_RESTRICT	11	/* Restrict	*/
+#define BTF_KIND_MAX		11
+#define NR_BTF_KINDS		12
+
+/* For some specific BTF_KIND, "struct btf_type" is immediately
+ * followed by extra data.
+ */
+
+/* BTF_KIND_INT is followed by a u32 and the following
+ * is the 32 bits arrangement:
+ */
+#define BTF_INT_ENCODING(VAL)	(((VAL) & 0xff000000) >> 24)
+#define BTF_INT_OFFSET(VAL)	(((VAL  & 0x00ff0000)) >> 16)
+#define BTF_INT_BITS(VAL)	((VAL)  & 0x0000ffff)
+
+/* Attributes stored in the BTF_INT_ENCODING */
+#define BTF_INT_SIGNED	0x1
+#define BTF_INT_CHAR	0x2
+#define BTF_INT_BOOL	0x4
+#define BTF_INT_VARARGS	0x8
+
+/* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
+ * The exact number of btf_enum is stored in the vlen (of the
+ * info in "struct btf_type").
+ */
+struct btf_enum {
+	__u32	name;
+	__s32	val;
+};
+
+/* BTF_KIND_ARRAY is followed by one "struct btf_array" */
+struct btf_array {
+	__u32	type;
+	__u32	index_type;
+	__u32	nelems;
+};
+
+/* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
+ * by multiple "struct btf_member".  The exact number
+ * of btf_member is stored in the vlen (of the info in
+ * "struct btf_type").
+ */
+struct btf_member {
+	__u32	name;
+	__u32	type;
+	__u32	offset;	/* offset in bits */
+};
+
+#endif /* _UAPI__LINUX_BTF_H__ */
kernel/bpf/Makefile +1 -0
index a713fd23ec88..35c485fa9ea3 100644
@@ -4,6 +4,7 @@ obj-y := core.o
 obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o
 obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o
+obj-$(CONFIG_BPF_SYSCALL) += btf.o
 ifeq ($(CONFIG_NET),y)
 obj-$(CONFIG_BPF_SYSCALL) += devmap.o
 obj-$(CONFIG_BPF_SYSCALL) += cpumap.o
kernel/bpf/btf.c new file +915 -0

915 lines not on this page · raw message

Signature · 1 line
2.9.5
#5 lore raw

PATCH v5 5/10 bpf: btf: Add BPF_BTF_LOAD command

In reply to Martin KaFai Lau

This patch adds a BPF_BTF_LOAD command which
1) loads and verifies the BTF (implemented in earlier patches)
2) returns a BTF fd to userspace. In the next patch, the BTF fd can be specified during BPF_MAP_CREATE.

It currently limits to CAP_SYS_ADMIN.

---
 include/linux/btf.h      |  4 +++
 include/uapi/linux/bpf.h |  9 +++++++
 kernel/bpf/btf.c         | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/syscall.c     | 17 ++++++++++++
 4 files changed, 97 insertions(+)
include/linux/btf.h +4 -0
index d8bdab0280ba..a7c7072535ea 100644
@@ -8,7 +8,11 @@
 
 struct btf;
 struct btf_type;
+union bpf_attr;
 
+void btf_put(struct btf *btf);
+int btf_new_fd(const union bpf_attr *attr);
+struct btf *btf_get_by_fd(int fd);
 /* Figure out the size of a type_id.  If type_id is a modifier
  * (e.g. const), it will be resolved to find out the type with size.
  *
include/uapi/linux/bpf.h +9 -0
index c5ec89732a8d..c7d75f18521b 100644
@@ -95,6 +95,7 @@ enum bpf_cmd {
 	BPF_OBJ_GET_INFO_BY_FD,
 	BPF_PROG_QUERY,
 	BPF_RAW_TRACEPOINT_OPEN,
+	BPF_BTF_LOAD,
 };
 
 enum bpf_map_type {
@@ -363,6 +364,14 @@ union bpf_attr {
 		__u64 name;
 		__u32 prog_fd;
 	} raw_tracepoint;
+
+	struct { /* anonymous struct for BPF_BTF_LOAD */
+		__aligned_u64	btf;
+		__aligned_u64	btf_log_buf;
+		__u32		btf_size;
+		__u32		btf_log_size;
+		__u32		btf_log_level;
+	};
 } __attribute__((aligned(8)));
 
 /* BPF helper function descriptions:
kernel/bpf/btf.c +67 -0

94 lines not on this page · raw message

kernel/bpf/syscall.c +17 -0
index 4ca46df19c9a..cd8ebadc66eb 100644
@@ -11,6 +11,7 @@
  */
 #include <linux/bpf.h>
 #include <linux/bpf_trace.h>
+#include <linux/btf.h>
 #include <linux/syscalls.h>
 #include <linux/slab.h>
 #include <linux/sched/signal.h>
@@ -2023,6 +2024,19 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
 	return err;
 }
 
+#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
+
+static int bpf_btf_load(const union bpf_attr *attr)
+{
+	if (CHECK_ATTR(BPF_BTF_LOAD))
+		return -EINVAL;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	return btf_new_fd(attr);
+}
+
 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
 {
 	union bpf_attr attr = {};
@@ -2103,6 +2117,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	case BPF_RAW_TRACEPOINT_OPEN:
 		err = bpf_raw_tracepoint_open(&attr);
 		break;
+	case BPF_BTF_LOAD:
+		err = bpf_btf_load(&attr);
+		break;
 	default:
 		err = -EINVAL;
 		break;
Signature · 1 line
2.9.5
#6 lore raw

PATCH v5 6/10 bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd

In reply to Martin KaFai Lau

This patch adds BPF_OBJ_GET_INFO_BY_FD support to BTF fd. The original BTF data, which was used to create the BTF fd during the earlier BPF_BTF_LOAD call, will be returned.

The userspace is expected to allocate buffer
to info.info and the buffer size is set to info.info_len before calling BPF_OBJ_GET_INFO_BY_FD.

The original BTF data is copied to the userspace buffer (info.info). Only upto the user's specified info.info_len will be copied.

The original BTF data size is set to info.info_len. The userspace needs to check if it is bigger than its allocated buffer size. If it is, the userspace should realloc with the kernel-returned info.info_len and call the BPF_OBJ_GET_INFO_BY_FD again.

---
 include/linux/btf.h  |  5 +++++
 kernel/bpf/btf.c     | 17 ++++++++++++++++-
 kernel/bpf/syscall.c |  2 ++
 3 files changed, 23 insertions(+), 1 deletion(-)
include/linux/btf.h +5 -0
index a7c7072535ea..a966dc6d61ee 100644
@@ -10,9 +10,14 @@ struct btf;
 struct btf_type;
 union bpf_attr;
 
+extern const struct file_operations btf_fops;
+
 void btf_put(struct btf *btf);
 int btf_new_fd(const union bpf_attr *attr);
 struct btf *btf_get_by_fd(int fd);
+int btf_get_info_by_fd(const struct btf *btf,
+		       const union bpf_attr *attr,
+		       union bpf_attr __user *uattr);
 /* Figure out the size of a type_id.  If type_id is a modifier
  * (e.g. const), it will be resolved to find out the type with size.
  *
kernel/bpf/btf.c +16 -1
index 2322340694cf..eb56ac760547 100644
@@ -2002,7 +2002,7 @@ static int btf_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
-static const struct file_operations btf_fops = {
+const struct file_operations btf_fops = {
 	.release	= btf_release,
 };
 
@@ -2047,3 +2047,18 @@ struct btf *btf_get_by_fd(int fd)
 
 	return btf;
 }
+
+int btf_get_info_by_fd(const struct btf *btf,
+		       const union bpf_attr *attr,
+		       union bpf_attr __user *uattr)
+{
+	void __user *udata = u64_to_user_ptr(attr->info.info);
+	u32 copy_len = min_t(u32, btf->data_size,
+			     attr->info.info_len);
+
+	if (copy_to_user(udata, btf->data, copy_len) ||
+	    put_user(btf->data_size, &uattr->info.info_len))
+		return -EFAULT;
+
+	return 0;
+}
kernel/bpf/syscall.c +2 -0
index cd8ebadc66eb..0a4924a0a8da 100644
@@ -2017,6 +2017,8 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
 	else if (f.file->f_op == &bpf_map_fops)
 		err = bpf_map_get_info_by_fd(f.file->private_data, attr,
 					     uattr);
+	else if (f.file->f_op == &btf_fops)
+		err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
 	else
 		err = -EINVAL;
 
Signature · 1 line
2.9.5
#9 lore raw

PATCH v5 9/10 bpf: btf: Add BTF support to libbpf

In reply to Martin KaFai Lau

If the ".BTF" elf section exists, libbpf will try to create a btf_fd (through BPF_BTF_LOAD). If that fails, it will still continue loading the bpf prog/map without the BTF.

If the bpf_object has a BTF loaded, it will create a map with the btf_fd. libbpf will try to figure out the btf_key_id and btf_value_id of a map by finding the BTF type with name "<map_name>_key" and "<map_name>_value". If they cannot be found, it will continue without using the BTF.

---
 tools/lib/bpf/Build    |   2 +-
 tools/lib/bpf/bpf.c    |  92 +++++++++---
 tools/lib/bpf/bpf.h    |  16 +++
 tools/lib/bpf/btf.c    | 374 +++++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/btf.h    |  22 +++
 tools/lib/bpf/libbpf.c | 148 +++++++++++++++++--
 tools/lib/bpf/libbpf.h |   3 +
 7 files changed, 626 insertions(+), 31 deletions(-)
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
tools/lib/bpf/Build +1 -1
index 64c679d67109..6070e655042d 100644
@@ -1 +1 @@
-libbpf-y := libbpf.o bpf.o nlattr.o
+libbpf-y := libbpf.o bpf.o nlattr.o btf.o
tools/lib/bpf/bpf.c +75 -17

121 lines not on this page · raw message

tools/lib/bpf/bpf.h +16 -0
index 39f6a0d64a3b..01bda076310f 100644
@@ -26,6 +26,20 @@
 #include <linux/bpf.h>
 #include <stddef.h>
 
+struct bpf_create_map_attr {
+	const char *name;
+	enum bpf_map_type map_type;
+	__u32 map_flags;
+	__u32 key_size;
+	__u32 value_size;
+	__u32 max_entries;
+	__u32 numa_node;
+	__u32 btf_fd;
+	__u32 btf_key_id;
+	__u32 btf_value_id;
+};
+
+int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
 			int key_size, int value_size, int max_entries,
 			__u32 map_flags, int node);
@@ -87,4 +101,6 @@ int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt);
 int bpf_raw_tracepoint_open(const char *name, int prog_fd);
+int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
+		 bool do_log);
 #endif
tools/lib/bpf/btf.c new file +374 -0

374 lines not on this page · raw message

tools/lib/bpf/btf.h new file +22 -0
new file mode 100644
index 000000000000..74bb344035bb
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2018 Facebook */
+
+#ifndef __BPF_BTF_H
+#define __BPF_BTF_H
+
+#include <stdint.h>
+
+#define BTF_ELF_SEC ".BTF"
+
+struct btf;
+
+typedef int (*btf_print_fn_t)(const char *, ...)
+	__attribute__((format(printf, 1, 2)));
+
+void btf__free(struct btf *btf);
+struct btf *btf__new(uint8_t *data, uint32_t size, btf_print_fn_t err_log);
+int32_t btf__find_by_name(const struct btf *btf, const char *type_name);
+int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id);
+int btf__fd(const struct btf *btf);
+
+#endif
tools/lib/bpf/libbpf.c +135 -13

211 lines not on this page · raw message

tools/lib/bpf/libbpf.h +3 -0
index 8b242486b464..d6ac4fa6f472 100644
@@ -78,6 +78,7 @@ int bpf_object__load(struct bpf_object *obj);
 int bpf_object__unload(struct bpf_object *obj);
 const char *bpf_object__name(struct bpf_object *obj);
 unsigned int bpf_object__kversion(struct bpf_object *obj);
+int bpf_object__btf_fd(const struct bpf_object *obj);
 
 struct bpf_object *bpf_object__next(struct bpf_object *prev);
 #define bpf_object__for_each_safe(pos, tmp)			\
@@ -241,6 +242,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
 int bpf_map__fd(struct bpf_map *map);
 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
 const char *bpf_map__name(struct bpf_map *map);
+uint32_t bpf_map__btf_key_id(const struct bpf_map *map);
+uint32_t bpf_map__btf_value_id(const struct bpf_map *map);
 
 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
 int bpf_map__set_priv(struct bpf_map *map, void *priv,
Signature · 1 line
2.9.5
#12 lore raw

PATCH v5 10/10 bpf: btf: Add BTF tests

In reply to Martin KaFai Lau

This patch tests the BTF loading, map_create with BTF and the changes in libbpf.

-r: Raw tests that test raw crafted BTF data
-f: Test LLVM compiled bpf prog with BTF data
-g: Test BPF_OBJ_GET_INFO_BY_FD for btf_fd
-p: Test pretty print

The tools/testing/selftests/bpf/Makefile will probe for BTF support in llc and pahole before generating debug info (-g) and convert them to BTF. You can supply the BTF supported binary through the following make variables:
LLC, BTF_PAHOLE and LLVM_OBJCOPY.

LLC: The lastest llc with -mattr=dwarfris support for the bpf target.
It is only in the master of the llvm repo for now.
BTF_PAHOLE: The modified pahole with BTF support:
https://github.com/iamkafai/pahole/tree/btf
To add a BTF section: "pahole -J bpf_prog.o"
LLVM_OBJCOPY: Any llvm-objcopy should do

---
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 ++++++++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 4 files changed, 1783 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
tools/testing/selftests/bpf/Makefile +23 -3
index 0a315ddabbf4..8d954926de91 100644
@@ -24,14 +24,15 @@ urandom_read: urandom_read.c
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
 	test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
-	test_sock test_sock_addr
+	test_sock test_sock_addr test_btf
 
 TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
 	test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o     \
 	sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
 	test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
 	sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
-	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o
+	sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o \
+	test_btf_haskv.o test_btf_nokv.o
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
@@ -66,6 +67,8 @@ $(BPFOBJ): force
 
 CLANG ?= clang
 LLC   ?= llc
+LLVM_OBJCOPY ?= llvm-objcopy
+BTF_PAHOLE ?= pahole
 
 PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1)
 
@@ -83,9 +86,26 @@ CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
 $(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
 $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
 
+BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help |& grep dwarfris)
+BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help |& grep BTF)
+BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --version |& grep LLVM)
+
+ifneq ($(BTF_LLC_PROBE),)
+ifneq ($(BTF_PAHOLE_PROBE),)
+ifneq ($(BTF_OBJCOPY_PROBE),)
+	CLANG_FLAGS += -g
+	LLC_FLAGS += -mattr=dwarfris
+	DWARF2BTF = y
+endif
+endif
+endif
+
 $(OUTPUT)/%.o: %.c
 	$(CLANG) $(CLANG_FLAGS) \
 		 -O2 -target bpf -emit-llvm -c $< -o - |      \
-	$(LLC) -march=bpf -mcpu=$(CPU) -filetype=obj -o $@
+	$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
+ifeq ($(DWARF2BTF),y)
+	$(BTF_PAHOLE) -J $@
+endif
 
 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS)
tools/testing/selftests/bpf/test_btf.c new file +1669 -0

1,669 lines not on this page · raw message

tools/testing/selftests/bpf/test_btf_haskv.c new file +48 -0

48 lines not on this page · raw message

tools/testing/selftests/bpf/test_btf_nokv.c new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.9.5
#4 lore raw

PATCH v5 4/10 bpf: btf: Add pretty print capability for data with BTF type info

In reply to Martin KaFai Lau

This patch adds pretty print capability for data with BTF type info. The current usage is to allow pretty print for a BPF map.

The next few patches will allow a read() on a pinned map with BTF type info for its key and value.

This patch uses the seq_printf() infra.

---
 include/linux/btf.h |   2 +
 kernel/bpf/btf.c    | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)
include/linux/btf.h +2 -0
index f14b60368753..d8bdab0280ba 100644
@@ -33,5 +33,7 @@ struct btf_type;
 const struct btf_type *btf_type_id_size(const struct btf *btf,
 					u32 *type_id,
 					u32 *ret_size);
+void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
+		       struct seq_file *m);
 
 #endif
kernel/bpf/btf.c +198 -0

287 lines not on this page · raw message

Signature · 1 line
2.9.5
#7 lore raw

PATCH v5 7/10 bpf: btf: Add pretty print support to the basic arraymap

In reply to Martin KaFai Lau

This patch adds pretty print support to the basic arraymap. Support for other bpf maps can be added later.

This patch adds new attrs to the BPF_MAP_CREATE command to allow specifying the btf_fd, btf_key_id and btf_value_id. The BPF_MAP_CREATE can then associate the btf to the map if the creating map supports BTF.

A BTF supported map needs to implement two new map ops, map_seq_show_elem() and map_check_btf(). This patch has implemented these new map ops for the basic arraymap.

It also adds file_operations, bpffs_map_fops, to the pinned map such that the pinned map can be opened and read. After that, the user has an intuitive way to do "cat bpffs/pathto/a-pinned-map" instead of getting an error.

bpffs_map_fops should not be extended further to support other operations. Other operations (e.g. write/key-lookup...) should be realized by the userspace tools (e.g. bpftool) through the BPF_OBJ_GET_INFO_BY_FD, map's lookup/update interface...etc. Follow up patches will allow the userspace to obtain the BTF from a map-fd.

Here is a sample output when reading a pinned arraymap with the following map's value:

struct map_value {
	int count_a;
	int count_b;
};

cat /sys/fs/bpf/pinned_array_map:

0: {1,2}
1: {3,4}
2: {5,6}
...

---
 include/linux/bpf.h      |  20 +++++-
 include/uapi/linux/bpf.h |   3 +
 kernel/bpf/arraymap.c    |  50 +++++++++++++++
 kernel/bpf/inode.c       | 156 ++++++++++++++++++++++++++++++++++++++++++++++-
 kernel/bpf/syscall.c     |  32 +++++++++-
 5 files changed, 254 insertions(+), 7 deletions(-)
include/linux/bpf.h +17 -3
index 95a7abd0ee92..ee5275e7d4df 100644
@@ -22,6 +22,8 @@ struct perf_event;
 struct bpf_prog;
 struct bpf_map;
 struct sock;
+struct seq_file;
+struct btf;
 
 /* map is generic key/value storage optionally accesible by eBPF programs */
 struct bpf_map_ops {
@@ -43,10 +45,14 @@ struct bpf_map_ops {
 	void (*map_fd_put_ptr)(void *ptr);
 	u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
+	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
+				  struct seq_file *m);
+	int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
+			     u32 key_type_id, u32 value_type_id);
 };
 
 struct bpf_map {
-	/* 1st cacheline with read-mostly members of which some
+	/* The first two cachelines with read-mostly members of which some
 	 * are also accessed in fast-path (e.g. ops, max_entries).
 	 */
 	const struct bpf_map_ops *ops ____cacheline_aligned;
@@ -62,10 +68,13 @@ struct bpf_map {
 	u32 pages;
 	u32 id;
 	int numa_node;
+	u32 btf_key_id;
+	u32 btf_value_id;
+	struct btf *btf;
 	bool unpriv_array;
-	/* 7 bytes hole */
+	/* 55 bytes hole */
 
-	/* 2nd cacheline with misc members to avoid false sharing
+	/* The 3rd and 4th cacheline with misc members to avoid false sharing
 	 * particularly with refcounting.
 	 */
 	struct user_struct *user ____cacheline_aligned;
@@ -100,6 +109,11 @@ static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
 	return container_of(map, struct bpf_offloaded_map, map);
 }
 
+static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
+{
+	return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+}
+
 extern const struct bpf_map_ops bpf_map_offload_ops;
 
 /* function argument constraints */
include/uapi/linux/bpf.h +3 -0
index c7d75f18521b..2c010a6b25e7 100644
@@ -280,6 +280,9 @@ union bpf_attr {
 					 */
 		char	map_name[BPF_OBJ_NAME_LEN];
 		__u32	map_ifindex;	/* ifindex of netdev to create on */
+		__u32	btf_fd;		/* fd pointing to a BTF type data */
+		__u32	btf_key_id;	/* BTF type_id of the key */
+		__u32	btf_value_id;	/* BTF type_id of the value */
 	};
 
 	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
kernel/bpf/arraymap.c +50 -0

73 lines not on this page · raw message

kernel/bpf/inode.c +153 -3

179 lines not on this page · raw message

kernel/bpf/syscall.c +31 -1
index 0a4924a0a8da..fe23dc5a3ec4 100644
@@ -27,6 +27,7 @@
 #include <linux/cred.h>
 #include <linux/timekeeping.h>
 #include <linux/ctype.h>
+#include <linux/btf.h>
 
 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
 			   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
@@ -251,6 +252,7 @@ static void bpf_map_free_deferred(struct work_struct *work)
 
 	bpf_map_uncharge_memlock(map);
 	security_bpf_map_free(map);
+	btf_put(map->btf);
 	/* implementation dependent freeing */
 	map->ops->map_free(map);
 }
@@ -416,7 +418,7 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
 	return 0;
 }
 
-#define BPF_MAP_CREATE_LAST_FIELD map_ifindex
+#define BPF_MAP_CREATE_LAST_FIELD btf_value_id
 /* called via syscall */
 static int map_create(union bpf_attr *attr)
 {
@@ -450,6 +452,33 @@ static int map_create(union bpf_attr *attr)
 	atomic_set(&map->refcnt, 1);
 	atomic_set(&map->usercnt, 1);
 
+	if (bpf_map_support_seq_show(map) &&
+	    (attr->btf_key_id || attr->btf_value_id)) {
+		struct btf *btf;
+
+		if (!attr->btf_key_id || !attr->btf_value_id) {
+			err = -EINVAL;
+			goto free_map_nouncharge;
+		}
+
+		btf = btf_get_by_fd(attr->btf_fd);
+		if (IS_ERR(btf)) {
+			err = PTR_ERR(btf);
+			goto free_map_nouncharge;
+		}
+
+		err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
+					      attr->btf_value_id);
+		if (err) {
+			btf_put(btf);
+			goto free_map_nouncharge;
+		}
+
+		map->btf = btf;
+		map->btf_key_id = attr->btf_key_id;
+		map->btf_value_id = attr->btf_value_id;
+	}
+
 	err = security_bpf_map_alloc(map);
 	if (err)
 		goto free_map_nouncharge;
@@ -482,6 +511,7 @@ static int map_create(union bpf_attr *attr)
 free_map_sec:
 	security_bpf_map_free(map);
 free_map_nouncharge:
+	btf_put(map->btf);
 	map->ops->map_free(map);
 	return err;
 }
Signature · 1 line
2.9.5
#13 lore raw
In reply to Martin KaFai Lau

Em Wed, Apr 18, 2018 at 03:55:56PM -0700, Martin KaFai Lau escreveu:

Quoted 8 lines

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

Quoted 4 lines

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Thanks for letting me know, I'm starting to look at this,

- Arnaldo

Quoted 79 lines

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Martin KaFai Lau (10):
bpf: btf: Introduce BPF Type Format (BTF)
bpf: btf: Validate type reference
bpf: btf: Check members of struct/union
bpf: btf: Add pretty print capability for data with BTF type info
bpf: btf: Add BPF_BTF_LOAD command
bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd
bpf: btf: Add pretty print support to the basic arraymap
bpf: btf: Sync bpf.h and btf.h to tools/
bpf: btf: Add BTF support to libbpf
bpf: btf: Add BTF tests

 include/linux/bpf.h                          |   20 +-
 include/linux/btf.h                          |   48 +
 include/uapi/linux/bpf.h                     |   12 +
 include/uapi/linux/btf.h                     |  130 ++
 kernel/bpf/Makefile                          |    1 +
 kernel/bpf/arraymap.c                        |   50 +
 kernel/bpf/btf.c                             | 2064 ++++++++++++++++++++++++++
 kernel/bpf/inode.c                           |  156 +-
 kernel/bpf/syscall.c                         |   51 +-
 tools/include/uapi/linux/bpf.h               |   12 +
 tools/include/uapi/linux/btf.h               |  130 ++
 tools/lib/bpf/Build                          |    2 +-
 tools/lib/bpf/bpf.c                          |   92 +-
 tools/lib/bpf/bpf.h                          |   16 +
 tools/lib/bpf/btf.c                          |  374 +++++
 tools/lib/bpf/btf.h                          |   22 +
 tools/lib/bpf/libbpf.c                       |  148 +-
 tools/lib/bpf/libbpf.h                       |    3 +
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 22 files changed, 5076 insertions(+), 41 deletions(-)
 create mode 100644 include/linux/btf.h
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
 create mode 100644 tools/include/uapi/linux/btf.h
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
Signature · 1 line
2.9.5
#14 lore raw
Quoted 9 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 18, 2018 at 03:55:56PM -0700, Martin KaFai Lau escreveu:

Quoted 8 lines

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

Quoted 6 lines
Quoted 4 lines

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Thanks for letting me know, I'm starting to look at this,

Thanks for reviewing. Feel free to comment directly on the github diff. Also, I think it may make sense to wait for the kernel pieces to land first.

Quoted 82 lines

- Arnaldo

Quoted 79 lines

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Martin KaFai Lau (10):
bpf: btf: Introduce BPF Type Format (BTF)
bpf: btf: Validate type reference
bpf: btf: Check members of struct/union
bpf: btf: Add pretty print capability for data with BTF type info
bpf: btf: Add BPF_BTF_LOAD command
bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd
bpf: btf: Add pretty print support to the basic arraymap
bpf: btf: Sync bpf.h and btf.h to tools/
bpf: btf: Add BTF support to libbpf
bpf: btf: Add BTF tests

 include/linux/bpf.h                          |   20 +-
 include/linux/btf.h                          |   48 +
 include/uapi/linux/bpf.h                     |   12 +
 include/uapi/linux/btf.h                     |  130 ++
 kernel/bpf/Makefile                          |    1 +
 kernel/bpf/arraymap.c                        |   50 +
 kernel/bpf/btf.c                             | 2064 ++++++++++++++++++++++++++
 kernel/bpf/inode.c                           |  156 +-
 kernel/bpf/syscall.c                         |   51 +-
 tools/include/uapi/linux/bpf.h               |   12 +
 tools/include/uapi/linux/btf.h               |  130 ++
 tools/lib/bpf/Build                          |    2 +-
 tools/lib/bpf/bpf.c                          |   92 +-
 tools/lib/bpf/bpf.h                          |   16 +
 tools/lib/bpf/btf.c                          |  374 +++++
 tools/lib/bpf/btf.h                          |   22 +
 tools/lib/bpf/libbpf.c                       |  148 +-
 tools/lib/bpf/libbpf.h                       |    3 +
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 22 files changed, 5076 insertions(+), 41 deletions(-)
 create mode 100644 include/linux/btf.h
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
 create mode 100644 tools/include/uapi/linux/btf.h
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
Signature · 1 line
2.9.5
maintainer
#37 lore raw
In reply to Martin KaFai Lau
Quoted 37 lines · Martin KaFai Lau

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

Quoted 7 lines

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Series applied to bpf-next, thanks Martin. As discussed please follow up with the bpftool patches.

Thanks,
Daniel

#10 lore raw

libbpf backward compatibility (was: [PATCH bpf-next v5 09/10] bpf: btf: Add BTF support to libbpf)

In reply to Martin KaFai Lau
Quoted 26 lines · Martin KaFai Lau
tools/lib/bpf/bpf.h +14 -0
index 39f6a0d64a3b..01bda076310f 100644
@@ -26,6 +26,20 @@
 #include <linux/bpf.h>
 #include <stddef.h>
 
+struct bpf_create_map_attr {
+	const char *name;
+	enum bpf_map_type map_type;
+	__u32 map_flags;
+	__u32 key_size;
+	__u32 value_size;
+	__u32 max_entries;
+	__u32 numa_node;
+	__u32 btf_fd;
+	__u32 btf_key_id;
+	__u32 btf_value_id;
+};
+
+int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
 			int key_size, int value_size, int max_entries,
 			__u32 map_flags, int node);

tools/lib/bpf/bpf.h int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); · line 106

Quoted 6 lines
tools/lib/bpf/bpf.h +2 -0
@@ -87,4 +101,6 @@ int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt);
 int bpf_raw_tracepoint_open(const char *name, int prog_fd);
+int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
+		 bool do_log);
 #endif

Does libbpf have to provide backward compatibility? Are the function prototypes not supposed to change?

Recently a number of *_xattr functions were added, these are nice for limiting the number of parameters passed to functions, but they don't buy us anything in terms of extensibility unless we can grow the structures. Should structure size be passed in as one of parameters?

#11 lore raw

Re: libbpf backward compatibility (was: [PATCH bpf-next v5 09/10] bpf: btf: Add BTF support to libbpf)

Quoted 37 lines · Jakub Kicinski
Quoted 26 lines · Martin KaFai Lau
tools/lib/bpf/bpf.h +14 -0
index 39f6a0d64a3b..01bda076310f 100644
@@ -26,6 +26,20 @@
 #include <linux/bpf.h>
 #include <stddef.h>
 
+struct bpf_create_map_attr {
+	const char *name;
+	enum bpf_map_type map_type;
+	__u32 map_flags;
+	__u32 key_size;
+	__u32 value_size;
+	__u32 max_entries;
+	__u32 numa_node;
+	__u32 btf_fd;
+	__u32 btf_key_id;
+	__u32 btf_value_id;
+};
+
+int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
 			int key_size, int value_size, int max_entries,
 			__u32 map_flags, int node);

tools/lib/bpf/bpf.h int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); · line 106

Quoted 6 lines
tools/lib/bpf/bpf.h +2 -0
@@ -87,4 +101,6 @@ int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt);
 int bpf_raw_tracepoint_open(const char *name, int prog_fd);
+int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
+		 bool do_log);
 #endif  

Does libbpf have to provide backward compatibility? Are the function prototypes not supposed to change?

Quoted 4 lines

Recently a number of *_xattr functions were added, these are nice for limiting the number of parameters passed to functions, but they don't buy us anything in terms of extensibility unless we can grow the structures. Should structure size be passed in as one of parameters?

Or is the backward compatibility not at the binary level so we can grow the structures at will?

#15 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 9 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 18, 2018 at 03:55:56PM -0700, Martin KaFai Lau escreveu:

Quoted 8 lines

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

Quoted 6 lines
Quoted 4 lines

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Thanks for letting me know, I'm starting to look at this,

Hi Arnaldo,

Do you have a chance to take a look and pull it? The kernel changes will be in 4.18, so it will be handy if it is available in the pahole repository.

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

Thanks,
Martin

Quoted 82 lines

- Arnaldo

Quoted 79 lines

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Martin KaFai Lau (10):
bpf: btf: Introduce BPF Type Format (BTF)
bpf: btf: Validate type reference
bpf: btf: Check members of struct/union
bpf: btf: Add pretty print capability for data with BTF type info
bpf: btf: Add BPF_BTF_LOAD command
bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd
bpf: btf: Add pretty print support to the basic arraymap
bpf: btf: Sync bpf.h and btf.h to tools/
bpf: btf: Add BTF support to libbpf
bpf: btf: Add BTF tests

 include/linux/bpf.h                          |   20 +-
 include/linux/btf.h                          |   48 +
 include/uapi/linux/bpf.h                     |   12 +
 include/uapi/linux/btf.h                     |  130 ++
 kernel/bpf/Makefile                          |    1 +
 kernel/bpf/arraymap.c                        |   50 +
 kernel/bpf/btf.c                             | 2064 ++++++++++++++++++++++++++
 kernel/bpf/inode.c                           |  156 +-
 kernel/bpf/syscall.c                         |   51 +-
 tools/include/uapi/linux/bpf.h               |   12 +
 tools/include/uapi/linux/btf.h               |  130 ++
 tools/lib/bpf/Build                          |    2 +-
 tools/lib/bpf/bpf.c                          |   92 +-
 tools/lib/bpf/bpf.h                          |   16 +
 tools/lib/bpf/btf.c                          |  374 +++++
 tools/lib/bpf/btf.h                          |   22 +
 tools/lib/bpf/libbpf.c                       |  148 +-
 tools/lib/bpf/libbpf.h                       |    3 +
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 22 files changed, 5076 insertions(+), 41 deletions(-)
 create mode 100644 include/linux/btf.h
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
 create mode 100644 tools/include/uapi/linux/btf.h
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
Signature · 1 line
2.9.5
#16 lore raw

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 15 lines · Arnaldo Carvalho de Melo
Quoted 14 lines

Em Wed, Apr 18, 2018 at 03:55:56PM -0700, Martin KaFai Lau escreveu:

Quoted 12 lines

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Quoted 8 lines
Quoted 1 line

Thanks for letting me know, I'm starting to look at this,

Hi Arnaldo,

Do you have a chance to take a look and pull it? The kernel changes will be in 4.18, so it will be handy if it is available in the pahole repository.

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

Got sidetracked, will get back to it later today.

- Arnaldo

#17 lore raw
In reply to Martin KaFai Lau

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 15 lines · Arnaldo Carvalho de Melo
Quoted 14 lines

Em Wed, Apr 18, 2018 at 03:55:56PM -0700, Martin KaFai Lau escreveu:

Quoted 12 lines

This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

A modified pahole that can convert dwarf to BTF is here:
https://github.com/iamkafai/pahole/tree/btf
(Arnaldo, there is some BTF_KIND numbering changes on
Apr 18th, d61426c1571)

Quoted 8 lines
Quoted 1 line

Thanks for letting me know, I'm starting to look at this,

Hi Arnaldo,

Do you have a chance to take a look and pull it? The kernel changes will be in 4.18, so it will be handy if it is available in the pahole repository.

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

Yeah, the one I had before had:

    It also raises the number of types (and functions) limit from 0x7fff to
    0x7fffffff.

----

And on this last one I see that:

/* Max # of type identifier */
-#define BTF_MAX_TYPE 0x7fffffff
+#define BTF_MAX_TYPE 0x0000ffff
/* Max offset into the string section */
-#define BTF_MAX_NAME_OFFSET 0x7fffffff
+#define BTF_MAX_NAME_OFFSET 0x0000ffff

So somehow (still reading) you'll be able to get more space, if we find necessary, to have more types and names, ok.

Continuing...

- Arnaldo

Quoted 82 lines
Quoted 82 lines

- Arnaldo

Quoted 79 lines

Please see individual patch for details.

v5:
- Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together.
- Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further.

v4:
- Fix warning (remove unneeded semicolon)
- Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1.

v3:
- Rebase to bpf-next
- Fix sparse warning (by adding static)
- Add BTF header logging: btf_verifier_log_hdr()
- Fix the alignment test on btf->type_off
- Add tests for the BTF header
- Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed.

v2:
- Use kvfree where needed in patch 1 and 2
- Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1
- Fix an incorrect goto target in map_create() during the btf-error-path in patch 7
- re-org some local vars to keep the rev xmas tree in btf.c

Martin KaFai Lau (10):
bpf: btf: Introduce BPF Type Format (BTF)
bpf: btf: Validate type reference
bpf: btf: Check members of struct/union
bpf: btf: Add pretty print capability for data with BTF type info
bpf: btf: Add BPF_BTF_LOAD command
bpf: btf: Add BPF_OBJ_GET_INFO_BY_FD support to BTF fd
bpf: btf: Add pretty print support to the basic arraymap
bpf: btf: Sync bpf.h and btf.h to tools/
bpf: btf: Add BTF support to libbpf
bpf: btf: Add BTF tests

 include/linux/bpf.h                          |   20 +-
 include/linux/btf.h                          |   48 +
 include/uapi/linux/bpf.h                     |   12 +
 include/uapi/linux/btf.h                     |  130 ++
 kernel/bpf/Makefile                          |    1 +
 kernel/bpf/arraymap.c                        |   50 +
 kernel/bpf/btf.c                             | 2064 ++++++++++++++++++++++++++
 kernel/bpf/inode.c                           |  156 +-
 kernel/bpf/syscall.c                         |   51 +-
 tools/include/uapi/linux/bpf.h               |   12 +
 tools/include/uapi/linux/btf.h               |  130 ++
 tools/lib/bpf/Build                          |    2 +-
 tools/lib/bpf/bpf.c                          |   92 +-
 tools/lib/bpf/bpf.h                          |   16 +
 tools/lib/bpf/btf.c                          |  374 +++++
 tools/lib/bpf/btf.h                          |   22 +
 tools/lib/bpf/libbpf.c                       |  148 +-
 tools/lib/bpf/libbpf.h                       |    3 +
 tools/testing/selftests/bpf/Makefile         |   26 +-
 tools/testing/selftests/bpf/test_btf.c       | 1669 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_btf_haskv.c |   48 +
 tools/testing/selftests/bpf/test_btf_nokv.c  |   43 +
 22 files changed, 5076 insertions(+), 41 deletions(-)
 create mode 100644 include/linux/btf.h
 create mode 100644 include/uapi/linux/btf.h
 create mode 100644 kernel/bpf/btf.c
 create mode 100644 tools/include/uapi/linux/btf.h
 create mode 100644 tools/lib/bpf/btf.c
 create mode 100644 tools/lib/bpf/btf.h
 create mode 100644 tools/testing/selftests/bpf/test_btf.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_haskv.c
 create mode 100644 tools/testing/selftests/bpf/test_btf_nokv.c
Signature · 1 line
2.9.5
#18 lore raw

Em Thu, Jun 07, 2018 at 10:54:01AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 2 lines

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 1 line

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

So, the commit log message for the pahole patch is non-existent:

https://github.com/iamkafai/pahole/commit/94a11b59e5920908085bfc8d24c92f95c8ffceaf

we should do better in describing what is done and how, I'm staring with a message you sent to the kernel part:

--
This patch introduces BPF Type Format (BTF).

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

#19 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 07, 2018 at 10:54:01AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 2 lines

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 1 line

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

So, the commit log message for the pahole patch is non-existent:

https://github.com/iamkafai/pahole/commit/94a11b59e5920908085bfc8d24c92f95c8ffceaf

we should do better in describing what is done and how, I'm staring with a message you sent to the kernel part:

--
This patch introduces BPF Type Format (BTF).

Quoted 6 lines

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.
--

I will add details in the next github respin/push.

Quoted 6 lines

Now I'm going to do the step-by-step guide on testing the feature just introduced, and will try to convert from dwarf to BTF and back, compare the pahole output for types encoded in DWARF and BTF, etc.

If you have something ressembling this already, please share.

The pahole only has the encoder part. I tested with the verbose output from the "pahole -V -J". Loading the btf to the kernel is also tested.

#20 lore raw

Em Thu, Jun 07, 2018 at 12:05:10PM -0700, Martin KaFai Lau escreveu:

Quoted 14 lines · Arnaldo Carvalho de Melo
Quoted 12 lines

Em Thu, Jun 07, 2018 at 10:54:01AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 2 lines

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 1 line

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

So, the commit log message for the pahole patch is non-existent:

https://github.com/iamkafai/pahole/commit/94a11b59e5920908085bfc8d24c92f95c8ffceaf

we should do better in describing what is done and how, I'm staring with a message you sent to the kernel part:
--
This patch introduces BPF Type Format (BTF).

Quoted 7 lines
Quoted 5 lines

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

I will add details in the next github respin/push.

Ok, but I can do that if there is nothing else to do in the code at this stage :-)

Quoted 8 lines
Quoted 5 lines

Now I'm going to do the step-by-step guide on testing the feature just introduced, and will try to convert from dwarf to BTF and back, compare the pahole output for types encoded in DWARF and BTF, etc.

If you have something ressembling this already, please share.

The pahole only has the encoder part. I tested with the verbose output from the "pahole -V -J". Loading the btf to the kernel is also tested.

Ok, so here it goes my first stab at testing, using perf's BPF integration:

[root@jouet bpf]# cat hello.c
#include "stdio.h"

int syscall_enter(openat)(void *ctx)

{
	puts("Hello, world\n");
	return 0;
}

[root@jouet bpf]# cat ~/.perfconfig
[llvm]
dump-obj = true
[root@jouet bpf]# perf trace -e open*,hello.c touch /tmp/hello.BTF
LLVM: dumping hello.o

     0.017 (         ): __bpf_stdout__:Hello, world
     0.019 ( 0.011 ms): touch/28147 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
     0.053 (         ): __bpf_stdout__:Hello, world
     0.055 ( 0.011 ms): touch/28147 openat(dfd: CWD, filename: /lib64/libc.so.6, flags: CLOEXEC           ) = 3
     0.354 ( 0.012 ms): touch/28147 open(filename: /usr/lib/locale/locale-archive, flags: CLOEXEC         ) = 3
     0.411 (         ): __bpf_stdout__:Hello, world
     0.412 ( 0.198 ms): touch/28147 openat(dfd: CWD, filename: /tmp/hello.BTF, flags: CREAT|NOCTTY|NONBLOCK|WRONLY, mode: IRUGO|IWUGO) = 3

[root@jouet bpf]#
[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), not stripped [root@jouet bpf]# pahole --btf_encode hello.o
pahole: hello.o: No debugging information found
[root@jouet bpf]#

[root@jouet bpf]# readelf -s hello.o

Symbol table '.symtab' contains 5 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    7 __bpf_stdout__
     2: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    5 _license
     3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    6 _version
     4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 syscall_enter_openat

[root@jouet bpf]#
[root@jouet bpf]# readelf -SW hello.o
There are 10 section headers, starting at offset 0x1f8:

Section Headers:

  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 000178 00007f 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] syscalls:sys_enter_openat PROGBITS        0000000000000000 000040 000088 00  AX  0   0  8
  [ 4] .relsyscalls:sys_enter_openat REL             0000000000000000 000168 000010 10      9   3  8
  [ 5] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
  [ 6] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
  [ 7] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
  [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
  [ 9] .symtab           SYMTAB          0000000000000000 0000f0 000078 18      1   1  8

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), p (processor specific)
[root@jouet bpf]#

Humm, lemme try something, add -g to clang-opt:

[root@jouet bpf]# cat ~/.perfconfig
[llvm]
dump-obj = true
clang-opt = -g
[root@jouet bpf]# perf trace -e open*,hello.c touch /tmp/hello.BTF
LLVM: dumping hello.o

     0.185 (         ): __bpf_stdout__:Hello, world
     0.188 ( 0.009 ms): touch/19670 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
     0.219 (         ): __bpf_stdout__:Hello, world
     0.220 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /lib64/libc.so.6, flags: CLOEXEC           ) = 3
     0.480 ( 0.095 ms): touch/19670 open(filename: /usr/lib/locale/locale-archive, flags: CLOEXEC         ) = 3
     0.624 (         ): __bpf_stdout__:Hello, world
     0.626 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /tmp/hello.BTF, flags: CREAT|NOCTTY|NONBLOCK|WRONLY, mode: IRUGO|IWUGO) = 3

[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped [root@jouet bpf]#

Much better:

[root@jouet bpf]# readelf -SW hello.o
There are 25 section headers, starting at offset 0xc70:

Section Headers:

  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 000b50 000119 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] syscalls:sys_enter_openat PROGBITS        0000000000000000 000040 000088 00  AX  0   0  8
  [ 4] .relsyscalls:sys_enter_openat REL             0000000000000000 000910 000010 10     24   3  8
  [ 5] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
  [ 6] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
  [ 7] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
  [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
  [ 9] .debug_str        PROGBITS        0000000000000000 0000ee 00010e 01  MS  0   0  1
  [10] .debug_loc        PROGBITS        0000000000000000 0001fc 000023 00      0   0  1
  [11] .debug_abbrev     PROGBITS        0000000000000000 00021f 0000e3 00      0   0  1
  [12] .debug_info       PROGBITS        0000000000000000 000302 00015e 00      0   0  1
  [13] .rel.debug_info   REL             0000000000000000 000920 0001e0 10     24  12  8
  [14] .debug_ranges     PROGBITS        0000000000000000 000460 000030 00      0   0  1
  [15] .debug_macinfo    PROGBITS        0000000000000000 000490 000001 00      0   0  1
  [16] .debug_pubnames   PROGBITS        0000000000000000 000491 00006e 00      0   0  1
  [17] .rel.debug_pubnames REL             0000000000000000 000b00 000010 10     24  16  8
  [18] .debug_pubtypes   PROGBITS        0000000000000000 0004ff 00005a 00      0   0  1
  [19] .rel.debug_pubtypes REL             0000000000000000 000b10 000010 10     24  18  8
  [20] .debug_frame      PROGBITS        0000000000000000 000560 000028 00      0   0  8
  [21] .rel.debug_frame  REL             0000000000000000 000b20 000020 10     24  20  8
  [22] .debug_line       PROGBITS        0000000000000000 000588 00006e 00      0   0  1
  [23] .rel.debug_line   REL             0000000000000000 000b40 000010 10     24  22  8
  [24] .symtab           SYMTAB          0000000000000000 0005f8 000318 18      1  29  8

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), p (processor specific)
[root@jouet bpf]#

[root@jouet bpf]# readelf -s hello.o

Symbol table '.symtab' contains 33 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    9 
     2: 000000000000002d     0 NOTYPE  LOCAL  DEFAULT    9 
     3: 0000000000000044     0 NOTYPE  LOCAL  DEFAULT    9 
     4: 0000000000000053     0 NOTYPE  LOCAL  DEFAULT    9 
     5: 000000000000005c     0 NOTYPE  LOCAL  DEFAULT    9 
     6: 0000000000000061     0 NOTYPE  LOCAL  DEFAULT    9 
     7: 000000000000006a     0 NOTYPE  LOCAL  DEFAULT    9 
     8: 0000000000000073     0 NOTYPE  LOCAL  DEFAULT    9 
     9: 0000000000000077     0 NOTYPE  LOCAL  DEFAULT    9 
    10: 0000000000000086     0 NOTYPE  LOCAL  DEFAULT    9 
    11: 000000000000008b     0 NOTYPE  LOCAL  DEFAULT    9 
    12: 0000000000000098     0 NOTYPE  LOCAL  DEFAULT    9 
    13: 00000000000000a1     0 NOTYPE  LOCAL  DEFAULT    9 
    14: 00000000000000ac     0 NOTYPE  LOCAL  DEFAULT    9 
    15: 00000000000000b8     0 NOTYPE  LOCAL  DEFAULT    9 
    16: 00000000000000c4     0 NOTYPE  LOCAL  DEFAULT    9 
    17: 00000000000000d6     0 NOTYPE  LOCAL  DEFAULT    9 
    18: 00000000000000e8     0 NOTYPE  LOCAL  DEFAULT    9 
    19: 00000000000000fd     0 NOTYPE  LOCAL  DEFAULT    9 
    20: 0000000000000101     0 NOTYPE  LOCAL  DEFAULT    9 
    21: 0000000000000107     0 NOTYPE  LOCAL  DEFAULT    9 
    22: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
    23: 0000000000000000     0 SECTION LOCAL  DEFAULT   10 
    24: 0000000000000000     0 SECTION LOCAL  DEFAULT   11 
    25: 0000000000000000     0 SECTION LOCAL  DEFAULT   12 
    26: 0000000000000000     0 SECTION LOCAL  DEFAULT   14 
    27: 0000000000000000     0 SECTION LOCAL  DEFAULT   20 
    28: 0000000000000000     0 SECTION LOCAL  DEFAULT   22 
    29: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    7 __bpf_stdout__
    30: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    5 _license
    31: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    6 _version
    32: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 syscall_enter_openat

[root@jouet bpf]#

Progress:

[root@jouet bpf]# pahole --btf_encode hello.o
sh: llvm-objcopy: command not found
Failed to encode BTF
[root@jouet bpf]#

[acme@jouet perf]$ llvm-

llvm-ar          llvm-cov         llvm-dis         llvm-lib         llvm-modextract  llvm-profdata    llvm-split
llvm-as          llvm-c-test      llvm-dlltool     llvm-link        llvm-mt          llvm-ranlib      llvm-stress
llvm-bcanalyzer  llvm-cvtres      llvm-dsymutil    llvm-lto         llvm-nm          llvm-readelf     llvm-strings
llvm-cat         llvm-cxxdump     llvm-dwarfdump   llvm-lto2        llvm-objdump     llvm-readobj     llvm-symbolizer
llvm-config      llvm-cxxfilt     llvm-dwp         llvm-mc          llvm-opt-report  llvm-rtdyld      llvm-tblgen
llvm-config-64   llvm-diff        llvm-extract     llvm-mcmarkup    llvm-pdbutil     llvm-size        llvm-xray

[acme@jouet perf]$ llvm-

So this must be available in a newer llvm version? Which one?

[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$
[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$

- Arnaldo

#21 lore raw
Quoted 208 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 07, 2018 at 12:05:10PM -0700, Martin KaFai Lau escreveu:

Quoted 14 lines · Arnaldo Carvalho de Melo
Quoted 12 lines

Em Thu, Jun 07, 2018 at 10:54:01AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 2 lines

Em Tue, Jun 05, 2018 at 02:25:48PM -0700, Martin KaFai Lau escreveu:

Quoted 1 line

[ btw, the latest commit (1 commit) should be 94a11b59e592 ].

So, the commit log message for the pahole patch is non-existent:

https://github.com/iamkafai/pahole/commit/94a11b59e5920908085bfc8d24c92f95c8ffceaf

we should do better in describing what is done and how, I'm staring with a message you sent to the kernel part:
--
This patch introduces BPF Type Format (BTF).

Quoted 7 lines
Quoted 5 lines

BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map.

I will add details in the next github respin/push.

Ok, but I can do that if there is nothing else to do in the code at this stage :-)

Quoted 8 lines
Quoted 5 lines

Now I'm going to do the step-by-step guide on testing the feature just introduced, and will try to convert from dwarf to BTF and back, compare the pahole output for types encoded in DWARF and BTF, etc.

If you have something ressembling this already, please share.

The pahole only has the encoder part. I tested with the verbose output from the "pahole -V -J". Loading the btf to the kernel is also tested.

Ok, so here it goes my first stab at testing, using perf's BPF integration:

[root@jouet bpf]# cat hello.c
#include "stdio.h"

int syscall_enter(openat)(void *ctx)

{
	puts("Hello, world\n");
	return 0;
}

[root@jouet bpf]# cat ~/.perfconfig
[llvm]
dump-obj = true
[root@jouet bpf]# perf trace -e open*,hello.c touch /tmp/hello.BTF
LLVM: dumping hello.o

     0.017 (         ): __bpf_stdout__:Hello, world
     0.019 ( 0.011 ms): touch/28147 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
     0.053 (         ): __bpf_stdout__:Hello, world
     0.055 ( 0.011 ms): touch/28147 openat(dfd: CWD, filename: /lib64/libc.so.6, flags: CLOEXEC           ) = 3
     0.354 ( 0.012 ms): touch/28147 open(filename: /usr/lib/locale/locale-archive, flags: CLOEXEC         ) = 3
     0.411 (         ): __bpf_stdout__:Hello, world
     0.412 ( 0.198 ms): touch/28147 openat(dfd: CWD, filename: /tmp/hello.BTF, flags: CREAT|NOCTTY|NONBLOCK|WRONLY, mode: IRUGO|IWUGO) = 3

[root@jouet bpf]#
[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), not stripped [root@jouet bpf]# pahole --btf_encode hello.o
pahole: hello.o: No debugging information found
[root@jouet bpf]#

[root@jouet bpf]# readelf -s hello.o

Symbol table '.symtab' contains 5 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    7 __bpf_stdout__
     2: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    5 _license
     3: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    6 _version
     4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 syscall_enter_openat

[root@jouet bpf]#
[root@jouet bpf]# readelf -SW hello.o
There are 10 section headers, starting at offset 0x1f8:

Section Headers:

  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 000178 00007f 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] syscalls:sys_enter_openat PROGBITS        0000000000000000 000040 000088 00  AX  0   0  8
  [ 4] .relsyscalls:sys_enter_openat REL             0000000000000000 000168 000010 10      9   3  8
  [ 5] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
  [ 6] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
  [ 7] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
  [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
  [ 9] .symtab           SYMTAB          0000000000000000 0000f0 000078 18      1   1  8

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), p (processor specific)
[root@jouet bpf]#

Humm, lemme try something, add -g to clang-opt:

[root@jouet bpf]# cat ~/.perfconfig
[llvm]
dump-obj = true
clang-opt = -g
[root@jouet bpf]# perf trace -e open*,hello.c touch /tmp/hello.BTF
LLVM: dumping hello.o

     0.185 (         ): __bpf_stdout__:Hello, world
     0.188 ( 0.009 ms): touch/19670 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
     0.219 (         ): __bpf_stdout__:Hello, world
     0.220 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /lib64/libc.so.6, flags: CLOEXEC           ) = 3
     0.480 ( 0.095 ms): touch/19670 open(filename: /usr/lib/locale/locale-archive, flags: CLOEXEC         ) = 3
     0.624 (         ): __bpf_stdout__:Hello, world
     0.626 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /tmp/hello.BTF, flags: CREAT|NOCTTY|NONBLOCK|WRONLY, mode: IRUGO|IWUGO) = 3

[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped [root@jouet bpf]#

Much better:

[root@jouet bpf]# readelf -SW hello.o
There are 25 section headers, starting at offset 0xc70:

Section Headers:

  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 000b50 000119 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] syscalls:sys_enter_openat PROGBITS        0000000000000000 000040 000088 00  AX  0   0  8
  [ 4] .relsyscalls:sys_enter_openat REL             0000000000000000 000910 000010 10     24   3  8
  [ 5] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
  [ 6] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
  [ 7] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
  [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
  [ 9] .debug_str        PROGBITS        0000000000000000 0000ee 00010e 01  MS  0   0  1
  [10] .debug_loc        PROGBITS        0000000000000000 0001fc 000023 00      0   0  1
  [11] .debug_abbrev     PROGBITS        0000000000000000 00021f 0000e3 00      0   0  1
  [12] .debug_info       PROGBITS        0000000000000000 000302 00015e 00      0   0  1
  [13] .rel.debug_info   REL             0000000000000000 000920 0001e0 10     24  12  8
  [14] .debug_ranges     PROGBITS        0000000000000000 000460 000030 00      0   0  1
  [15] .debug_macinfo    PROGBITS        0000000000000000 000490 000001 00      0   0  1
  [16] .debug_pubnames   PROGBITS        0000000000000000 000491 00006e 00      0   0  1
  [17] .rel.debug_pubnames REL             0000000000000000 000b00 000010 10     24  16  8
  [18] .debug_pubtypes   PROGBITS        0000000000000000 0004ff 00005a 00      0   0  1
  [19] .rel.debug_pubtypes REL             0000000000000000 000b10 000010 10     24  18  8
  [20] .debug_frame      PROGBITS        0000000000000000 000560 000028 00      0   0  8
  [21] .rel.debug_frame  REL             0000000000000000 000b20 000020 10     24  20  8
  [22] .debug_line       PROGBITS        0000000000000000 000588 00006e 00      0   0  1
  [23] .rel.debug_line   REL             0000000000000000 000b40 000010 10     24  22  8
  [24] .symtab           SYMTAB          0000000000000000 0005f8 000318 18      1  29  8

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), p (processor specific)
[root@jouet bpf]#

[root@jouet bpf]# readelf -s hello.o

Symbol table '.symtab' contains 33 entries:

   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    9 
     2: 000000000000002d     0 NOTYPE  LOCAL  DEFAULT    9 
     3: 0000000000000044     0 NOTYPE  LOCAL  DEFAULT    9 
     4: 0000000000000053     0 NOTYPE  LOCAL  DEFAULT    9 
     5: 000000000000005c     0 NOTYPE  LOCAL  DEFAULT    9 
     6: 0000000000000061     0 NOTYPE  LOCAL  DEFAULT    9 
     7: 000000000000006a     0 NOTYPE  LOCAL  DEFAULT    9 
     8: 0000000000000073     0 NOTYPE  LOCAL  DEFAULT    9 
     9: 0000000000000077     0 NOTYPE  LOCAL  DEFAULT    9 
    10: 0000000000000086     0 NOTYPE  LOCAL  DEFAULT    9 
    11: 000000000000008b     0 NOTYPE  LOCAL  DEFAULT    9 
    12: 0000000000000098     0 NOTYPE  LOCAL  DEFAULT    9 
    13: 00000000000000a1     0 NOTYPE  LOCAL  DEFAULT    9 
    14: 00000000000000ac     0 NOTYPE  LOCAL  DEFAULT    9 
    15: 00000000000000b8     0 NOTYPE  LOCAL  DEFAULT    9 
    16: 00000000000000c4     0 NOTYPE  LOCAL  DEFAULT    9 
    17: 00000000000000d6     0 NOTYPE  LOCAL  DEFAULT    9 
    18: 00000000000000e8     0 NOTYPE  LOCAL  DEFAULT    9 
    19: 00000000000000fd     0 NOTYPE  LOCAL  DEFAULT    9 
    20: 0000000000000101     0 NOTYPE  LOCAL  DEFAULT    9 
    21: 0000000000000107     0 NOTYPE  LOCAL  DEFAULT    9 
    22: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
    23: 0000000000000000     0 SECTION LOCAL  DEFAULT   10 
    24: 0000000000000000     0 SECTION LOCAL  DEFAULT   11 
    25: 0000000000000000     0 SECTION LOCAL  DEFAULT   12 
    26: 0000000000000000     0 SECTION LOCAL  DEFAULT   14 
    27: 0000000000000000     0 SECTION LOCAL  DEFAULT   20 
    28: 0000000000000000     0 SECTION LOCAL  DEFAULT   22 
    29: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    7 __bpf_stdout__
    30: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    5 _license
    31: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    6 _version
    32: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 syscall_enter_openat

[root@jouet bpf]#

Progress:

[root@jouet bpf]# pahole --btf_encode hello.o
sh: llvm-objcopy: command not found
Failed to encode BTF
[root@jouet bpf]#

[acme@jouet perf]$ llvm-

llvm-ar          llvm-cov         llvm-dis         llvm-lib         llvm-modextract  llvm-profdata    llvm-split
llvm-as          llvm-c-test      llvm-dlltool     llvm-link        llvm-mt          llvm-ranlib      llvm-stress
llvm-bcanalyzer  llvm-cvtres      llvm-dsymutil    llvm-lto         llvm-nm          llvm-readelf     llvm-strings
llvm-cat         llvm-cxxdump     llvm-dwarfdump   llvm-lto2        llvm-objdump     llvm-readobj     llvm-symbolizer
llvm-config      llvm-cxxfilt     llvm-dwp         llvm-mc          llvm-opt-report  llvm-rtdyld      llvm-tblgen
llvm-config-64   llvm-diff        llvm-extract     llvm-mcmarkup    llvm-pdbutil     llvm-size        llvm-xray
Quoted 3 lines

[acme@jouet perf]$ llvm-

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

Hence, I have been using the llvm's master in github which also has the llvm-objcopy.

2. The kernel's btf part only focus on the BPF map.
Hence, the testing bpf program should have the map's key and map's value. e.g. tools/testing/selftests/bpf/test_btf_haskv.c

Quoted 37 lines

[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$
[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$

- Arnaldo

#22 lore raw

Em Thu, Jun 07, 2018 at 01:07:01PM -0700, Martin KaFai Lau escreveu:

Quoted 11 lines · Arnaldo Carvalho de Melo
Quoted 1 line

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

Quoted 6 lines

Hence, I have been using the llvm's master in github which also has the llvm-objcopy.

2. The kernel's btf part only focus on the BPF map.
Hence, the testing bpf program should have the map's key and map's value. e.g. tools/testing/selftests/bpf/test_btf_haskv.c

Thanks for the version required to test this, but where is this test_btf_haskv.c file? Which tree? net-next?

Ok, just pulled torvalds/master and there it is. Gotcha.

struct bpf_map_def SEC("maps") __bpf_stdout__ = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};

This map is in the above hello.c example, but I guess its way too simple :-)

Ok, I'll test this at home in another machine where I have the llvm's git repo.

- Arnaldo

#23 lore raw
In reply to Martin KaFai Lau

Em Thu, Jun 07, 2018 at 01:07:01PM -0700, Martin KaFai Lau escreveu:

Quoted 11 lines · Arnaldo Carvalho de Melo
Quoted 1 line

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

Quoted 6 lines

Hence, I have been using the llvm's master in github which also has the llvm-objcopy.

2. The kernel's btf part only focus on the BPF map.
Hence, the testing bpf program should have the map's key and map's value. e.g. tools/testing/selftests/bpf/test_btf_haskv.c

So, with llvm and clang HEAD I get:

[root@jouet bpf]# pahole -J hello.o
[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped [root@jouet bpf]# llvm-readelf -s hello.o
There are 26 section headers, starting at offset 0xe30:

Section Headers:

  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 2] syscalls:sys_enter_openat PROGBITS 0000000000000000 000040 000088 00  AX  0   0  8
  [ 3] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
  [ 4] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
  [ 5] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
  [ 6] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
  [ 7] .debug_str        PROGBITS        0000000000000000 0000ee 00010e 01  MS  0   0  1
  [ 8] .debug_loc        PROGBITS        0000000000000000 0001fc 000023 00      0   0  1
  [ 9] .debug_abbrev     PROGBITS        0000000000000000 00021f 0000e3 00      0   0  1
  [10] .debug_info       PROGBITS        0000000000000000 000302 00015e 00      0   0  1
  [11] .debug_ranges     PROGBITS        0000000000000000 000460 000030 00      0   0  1
  [12] .debug_macinfo    PROGBITS        0000000000000000 000490 000001 00      0   0  1
  [13] .debug_pubnames   PROGBITS        0000000000000000 000491 00006e 00      0   0  1
  [14] .debug_pubtypes   PROGBITS        0000000000000000 0004ff 00005a 00      0   0  1
  [15] .debug_frame      PROGBITS        0000000000000000 000560 000028 00      0   0  8
  [16] .debug_line       PROGBITS        0000000000000000 000588 00006e 00      0   0  1
  [17] .symtab           SYMTAB          0000000000000000 0005f8 000318 18     24  29  8
  [18] .relsyscalls:sys_enter_openat REL 0000000000000000 000910 000010 10     17   2  8
  [19] .rel.debug_info   REL             0000000000000000 000920 0001e0 10     17  10  8
  [20] .rel.debug_pubnames REL           0000000000000000 000b00 000010 10     17  13  8
  [21] .rel.debug_pubtypes REL           0000000000000000 000b10 000010 10     17  14  8
  [22] .rel.debug_frame  REL             0000000000000000 000b20 000020 10     17  15  8
  [23] .rel.debug_line   REL             0000000000000000 000b40 000010 10     17  16  8
  [24] .strtab           STRTAB          0000000000000000 000b50 00018e 00      0   0  1
  [25] .BTF              PROGBITS        0000000000000000 000cde 00014e 00      0   0  1

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), l (large) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)
[root@jouet bpf]#
[root@jouet bpf]# pahole hello.o

struct clang version 5.0.1 (tags/RELEASE_501/final) {
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     0     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     4     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     8     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};

[root@jouet bpf]#

Ok, I guess I saw this case in the llvm/clang git logs, so this one was generated with the older clang, will regenerate and add that "-mattr=dwarf" part.

- Arnaldo

#24 lore raw

Em Tue, Jun 12, 2018 at 05:31:24PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 22 lines

Em Thu, Jun 07, 2018 at 01:07:01PM -0700, Martin KaFai Lau escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 1 line

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

[root@jouet bpf]# pahole hello.o

struct clang version 5.0.1 (tags/RELEASE_501/final) {
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     0     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     4     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     8     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};
Quoted 5 lines

[root@jouet bpf]#

Ok, I guess I saw this case in the llvm/clang git logs, so this one was generated with the older clang, will regenerate and add that "-mattr=dwarf" part.

[root@jouet bpf]# pahole hello.o

struct clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34) {
	clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78 clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     0     4 */
	clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78 clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     4     4 */
	clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78 clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     8     4 */
	clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78 clang version 7.0.0 (http://llvm.org/git/clang.git 8c873daccce7ee5339b9fd82c81fe02b73543b65) (http://llvm.org/git/llvm.git 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};

[root@jouet bpf]#

Ideas?

[root@jouet bpf]# trace -e open*,hello.c
clang-6.0: error: unknown argument: '-mattr=dwarf'
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@jouet bpf]#

[root@jouet bpf]# trace -v -e open*,hello.c
bpf: builtin compilation failed: -95, try external compiler
Kernel build dir is set to /lib/modules/4.17.0-rc5/build
set env: KBUILD_DIR=/lib/modules/4.17.0-rc5/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h
set env: NR_CPUS=4
set env: LINUX_VERSION_CODE=0x41100
set env: CLANG_EXEC=/usr/local/bin/clang
set env: CLANG_OPTIONS=-g -mattr=dwarf
set env: KERNEL_INC_OPTIONS= -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/include/perf/bpf
set env: WORKING_DIR=/lib/modules/4.17.0-rc5/build
set env: CLANG_SOURCE=/home/acme/bpf/hello.c
llvm compiling command template: $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o -
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mattr=dwarf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang-6.0: error: unknown argument: '-mattr=dwarf'
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

#25 lore raw
Quoted 30 lines · Arnaldo Carvalho de Melo

Em Tue, Jun 12, 2018 at 05:31:24PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 22 lines

Em Thu, Jun 07, 2018 at 01:07:01PM -0700, Martin KaFai Lau escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 1 line

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

[root@jouet bpf]# pahole hello.o

struct clang version 5.0.1 (tags/RELEASE_501/final) {
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     0     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     4     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     8     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};
Quoted 5 lines

[root@jouet bpf]#

Ok, I guess I saw this case in the llvm/clang git logs, so this one was generated with the older clang, will regenerate and add that "-mattr=dwarf" part.

[root@jouet bpf]# pahole hello.o

Quoted 9 lines
struct clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34) {
	clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78 clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     0     4 */
	clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78 clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     4     4 */
	clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78 clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*     8     4 */
	clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78 clang version 7.0.0 (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_clang.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=_Qzsu689xEjjl9JvYCvJsIZLZZKDLB6rM-Uc0gqQvyg&e= 8c873daccce7ee5339b9fd82c81fe02b73543b65) (https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_git_llvm.git&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=i6WobKxbeG3slzHSIOxTVtYIJw7qjCE6S0spDTKL-J4&m=4d495SlcvobgBOFahId75gM-V2su4Qq2wiLOGkU-adI&s=cFz6VP_YIYy_hubsx05WDqpTDyXl0Wnx_RAmAl1dbpg&e= 98c78e82f54be8fb0bb5f02e3ca674fbde10ef34); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};

That means the "-mattr=dwarf" is not effective. Can you share your clang and llc command to create hello.o?

Quoted 50 lines

[root@jouet bpf]#

Ideas?

[root@jouet bpf]# trace -e open*,hello.c
clang-6.0: error: unknown argument: '-mattr=dwarf'
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@jouet bpf]#

[root@jouet bpf]# trace -v -e open*,hello.c
bpf: builtin compilation failed: -95, try external compiler
Kernel build dir is set to /lib/modules/4.17.0-rc5/build
set env: KBUILD_DIR=/lib/modules/4.17.0-rc5/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h
set env: NR_CPUS=4
set env: LINUX_VERSION_CODE=0x41100
set env: CLANG_EXEC=/usr/local/bin/clang
set env: CLANG_OPTIONS=-g -mattr=dwarf
set env: KERNEL_INC_OPTIONS= -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/include/perf/bpf
set env: WORKING_DIR=/lib/modules/4.17.0-rc5/build
set env: CLANG_SOURCE=/home/acme/bpf/hello.c
llvm compiling command template: $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o -
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mattr=dwarf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang-6.0: error: unknown argument: '-mattr=dwarf'
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

#26 lore raw

Em Wed, Jun 13, 2018 at 04:26:38PM -0700, Martin KaFai Lau escreveu:

Quoted 32 lines · Arnaldo Carvalho de Melo
Quoted 31 lines

Em Tue, Jun 12, 2018 at 05:31:24PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 22 lines

Em Thu, Jun 07, 2018 at 01:07:01PM -0700, Martin KaFai Lau escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 1 line

So this must be available in a newer llvm version? Which one?

I should have put in the details in my last email or in the commit message, my bad.

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

[root@jouet bpf]# pahole hello.o

struct clang version 5.0.1 (tags/RELEASE_501/final) {
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     0     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     4     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*     8     4 */
	clang version 5.0.1 (tags/RELEASE_501/final) clang version 5.0.1 (tags/RELEASE_501/final); /*    12     4 */

	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};
Quoted 5 lines

[root@jouet bpf]#

Ok, I guess I saw this case in the llvm/clang git logs, so this one was generated with the older clang, will regenerate and add that "-mattr=dwarf" part.

[root@jouet bpf]# pahole hello.o
struct clang version 7.0.0 <SNIP>

<SNIP>

Quoted 5 lines
Quoted 3 lines
	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};

That means the "-mattr=dwarf" is not effective. Can you share your clang and llc command to create hello.o?

I tried it, but it didn't work, see:

[root@jouet bpf]# cat hello.c
#include "stdio.h"

int syscall_enter(openat)(void *ctx)

{
	puts("Hello, world\n");
	return 0;
}

[root@jouet bpf]# trace -e openat,hello.c touch /tmp/kafai
clang-6.0: error: unknown argument: '-mattr=dwarf'
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@jouet bpf]#

The full command line with that is:

[root@jouet bpf]# trace -v -e openat,hello.c touch /tmp/kafai |& grep mattr
set env: CLANG_OPTIONS=-g -mattr=dwarf
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mattr=dwarf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang-6.0: error: unknown argument: '-mattr=dwarf'
[root@jouet bpf]#

This is with these llvm and clang trees:

[root@jouet llvm]# git log --oneline -5
98c78e82f54 (HEAD -> master, origin/master, origin/HEAD) [asan] Instrument comdat globals on COFF targets 6ad988b5998 [DAGCombiner] clean up comments; NFC
a735ba5b795 [X86][SSE] Support v8i16/v16i16 rotations
1503b9f6fe8 [x86] add tests for node-level FMF; NFC
4a49826736f [x86] regenerate test checks; NFC
[root@jouet llvm]#

[root@jouet llvm]# cd tools/clang/
[root@jouet clang]# git log --oneline -5
8c873daccc (HEAD -> master, origin/master, origin/HEAD) [X86] Add builtins for vpermq/vpermpd instructions to enable target feature checking. a344be6ba4 [X86] Change immediate type for some builtins from char to int.
dcdd53793e [CUDA] Fix emission of constant strings in sections
a90c85acaf [X86] Add builtins for shufps and shufpd to enable target feature and immediate range checking.
ff71c0eccc [X86] Add builtins for pshufd, pshuflw, and pshufhw to enable target feature and immediate range checking. [root@jouet clang]#

[root@jouet clang]# git log | grep mattr=dwarf
[root@jouet clang]# cd -
/home/acme/git.tmp/git/llvm
[root@jouet llvm]# git log | grep mattr=dwarf
bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections This patch introduces a new flag -mattr=dwarfris
[root@jouet llvm]#

Humm, so its -mattr=dwarfris and not -attr=dwarf?

Didn't help :-\

commit 0e0047f8c9ada2f0fe0c5f01579a80e2455b8df5

Author: Yonghong Song <[email protected]>
Date:   Thu Mar 1 23:04:59 2018 +0000

bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections

Commit e4507fb8c94b ("bpf: disable DwarfUsesRelocationsAcrossSections") disables MCAsmInfo DwarfUsesRelocationsAcrossSections unconditionally so that dwarf will not use cross section (between dwarf and symbol table) relocations. This new debug format enables pahole to dump structures correctly as libdwarves.so does not have BPF backend support yet.

This new debug format, however, breaks bcc (https://github.com/iovisor/bcc)
source debug output as llvm in-memory Dwarf support has some issues to handle it. More specifically, with DwarfUsesRelocationsAcrossSections disabled, JIT compiler does not generate .debug_abbrev and Dwarf DIE (debug info entry) processing is not happy about this.

This patch introduces a new flag -mattr=dwarfris
(dwarf relocation in section) to disable DwarfUsesRelocationsAcrossSections. DwarfUsesRelocationsAcrossSections is true by default.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326505 91177308-0d34-0410-b5e6-96231b3b80d8

#27 lore raw
Quoted 4 lines · Arnaldo Carvalho de Melo
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

[ ... ]

Quoted 5 lines

I tried it, but it didn't work, see:

[root@jouet bpf]# cat hello.c
#include "stdio.h"

Quoted 7 lines

int syscall_enter(openat)(void *ctx)

{
	puts("Hello, world\n");
	return 0;
}

[root@jouet bpf]# trace -e openat,hello.c touch /tmp/kafai clang-6.0: error: unknown argument: '-mattr=dwarf'

"-mattr=dwarf" is currently a llc only option.

tools/testing/selftests/bpf/Makefile has example on how to pipe clang to llc.

e.g.:
clang -g -O2 -target bpf -emit-llvm -c hello.c -o - | llc -march=bpf -mcpu=generic -mattr=dwarfris -filetype=obj -o hello.o

Quoted 85 lines

ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@jouet bpf]#

The full command line with that is:

[root@jouet bpf]# trace -v -e openat,hello.c touch /tmp/kafai |& grep mattr
set env: CLANG_OPTIONS=-g -mattr=dwarf
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mattr=dwarf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang-6.0: error: unknown argument: '-mattr=dwarf'
[root@jouet bpf]#

This is with these llvm and clang trees:

[root@jouet llvm]# git log --oneline -5
98c78e82f54 (HEAD -> master, origin/master, origin/HEAD) [asan] Instrument comdat globals on COFF targets 6ad988b5998 [DAGCombiner] clean up comments; NFC
a735ba5b795 [X86][SSE] Support v8i16/v16i16 rotations
1503b9f6fe8 [x86] add tests for node-level FMF; NFC
4a49826736f [x86] regenerate test checks; NFC
[root@jouet llvm]#

[root@jouet llvm]# cd tools/clang/
[root@jouet clang]# git log --oneline -5
8c873daccc (HEAD -> master, origin/master, origin/HEAD) [X86] Add builtins for vpermq/vpermpd instructions to enable target feature checking. a344be6ba4 [X86] Change immediate type for some builtins from char to int.
dcdd53793e [CUDA] Fix emission of constant strings in sections
a90c85acaf [X86] Add builtins for shufps and shufpd to enable target feature and immediate range checking.
ff71c0eccc [X86] Add builtins for pshufd, pshuflw, and pshufhw to enable target feature and immediate range checking. [root@jouet clang]#

[root@jouet clang]# git log | grep mattr=dwarf
[root@jouet clang]# cd -
/home/acme/git.tmp/git/llvm
[root@jouet llvm]# git log | grep mattr=dwarf
bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections This patch introduces a new flag -mattr=dwarfris
[root@jouet llvm]#

Humm, so its -mattr=dwarfris and not -attr=dwarf?

Didn't help :-\

commit 0e0047f8c9ada2f0fe0c5f01579a80e2455b8df5

Author: Yonghong Song <[email protected]>
Date:   Thu Mar 1 23:04:59 2018 +0000

bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections

Commit e4507fb8c94b ("bpf: disable DwarfUsesRelocationsAcrossSections") disables MCAsmInfo DwarfUsesRelocationsAcrossSections unconditionally so that dwarf will not use cross section (between dwarf and symbol table) relocations. This new debug format enables pahole to dump structures correctly as libdwarves.so does not have BPF backend support yet.

This new debug format, however, breaks bcc (https://github.com/iovisor/bcc)
source debug output as llvm in-memory Dwarf support has some issues to handle it. More specifically, with DwarfUsesRelocationsAcrossSections disabled, JIT compiler does not generate .debug_abbrev and Dwarf DIE (debug info entry) processing is not happy about this.

This patch introduces a new flag -mattr=dwarfris
(dwarf relocation in section) to disable DwarfUsesRelocationsAcrossSections. DwarfUsesRelocationsAcrossSections is true by default.

git-svn-id: https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_svn_llvm-2Dproject_llvm_trunk-40326505&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=VQnoQ7LvghIj0gVEaiQSUw&m=LO28-RE-2ZJTXto_gff4BgnxXkbUq8d2CEz1jD_wDl4&s=VCR3pGVfY54-OsZ3BqRsOr3FF5JVyltwbnbzu30_4EY&e= 91177308-0d34-0410-b5e6-96231b3b80d8

#28 lore raw

Em Thu, Jun 14, 2018 at 09:22:27AM -0700, Martin KaFai Lau escreveu:

Quoted 21 lines · Arnaldo Carvalho de Melo
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

1. The tools/testing/selftests/bpf/Makefile has the CLANG_FLAGS and LLC_FLAGS needed to compile the bpf prog. It requires a new "-mattr=dwarf" llc option which was added to the future llvm 7.0.

[ ... ]

Quoted 5 lines

I tried it, but it didn't work, see:

[root@jouet bpf]# cat hello.c
#include "stdio.h"

Quoted 7 lines

int syscall_enter(openat)(void *ctx)

{
	puts("Hello, world\n");
	return 0;
}

[root@jouet bpf]# trace -e openat,hello.c touch /tmp/kafai clang-6.0: error: unknown argument: '-mattr=dwarf'

Quoted 6 lines

"-mattr=dwarf" is currently a llc only option.

tools/testing/selftests/bpf/Makefile has example on how to pipe clang to llc.

e.g.:
clang -g -O2 -target bpf -emit-llvm -c hello.c -o - | llc -march=bpf -mcpu=generic -mattr=dwarfris -filetype=obj -o hello.o

Ok, so I'll probably add a llvm.opts .perfconfig entry that, if present will tell tools/perf/util/llvm-utils.c that piping the output of clang to llvm, so that we can use llvm specific options, needs to be done.

Probably, for the time being I'll check for -g in llvm.clang-opt and if it is there, set up the piping...

Just out of curiosity, is there any plan to have this as a clang option?

Just to finish this thing here, lemme try a slightly modified version of your command line:

[root@jouet bpf]# clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -emit-llvm -O2 -o - | llc -march=bpf -mcpu=generic -mattr=dwarfris -filetype=obj -o hello2.o [root@jouet bpf]#

[root@jouet bpf]# file hello2.o
hello2.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped [root@jouet bpf]# pahole hello2.o
struct bpf_map_def {

	unsigned int               type;                 /*     0     4 */
	unsigned int               key_size;             /*     4     4 */
	unsigned int               value_size;           /*     8     4 */
	unsigned int               max_entries;          /*    12     4 */
	/* size: 16, cachelines: 1, members: 4 */
	/* last cacheline: 16 bytes */
};

[root@jouet bpf]#

Finally works, thanks.

Thanks,

- Arnaldo

Quoted 85 lines
Quoted 85 lines

ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@jouet bpf]#

The full command line with that is:

[root@jouet bpf]# trace -v -e openat,hello.c touch /tmp/kafai |& grep mattr
set env: CLANG_OPTIONS=-g -mattr=dwarf
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mattr=dwarf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang-6.0: error: unknown argument: '-mattr=dwarf'
[root@jouet bpf]#

This is with these llvm and clang trees:

[root@jouet llvm]# git log --oneline -5
98c78e82f54 (HEAD -> master, origin/master, origin/HEAD) [asan] Instrument comdat globals on COFF targets 6ad988b5998 [DAGCombiner] clean up comments; NFC
a735ba5b795 [X86][SSE] Support v8i16/v16i16 rotations
1503b9f6fe8 [x86] add tests for node-level FMF; NFC
4a49826736f [x86] regenerate test checks; NFC
[root@jouet llvm]#

[root@jouet llvm]# cd tools/clang/
[root@jouet clang]# git log --oneline -5
8c873daccc (HEAD -> master, origin/master, origin/HEAD) [X86] Add builtins for vpermq/vpermpd instructions to enable target feature checking. a344be6ba4 [X86] Change immediate type for some builtins from char to int.
dcdd53793e [CUDA] Fix emission of constant strings in sections
a90c85acaf [X86] Add builtins for shufps and shufpd to enable target feature and immediate range checking.
ff71c0eccc [X86] Add builtins for pshufd, pshuflw, and pshufhw to enable target feature and immediate range checking. [root@jouet clang]#

[root@jouet clang]# git log | grep mattr=dwarf
[root@jouet clang]# cd -
/home/acme/git.tmp/git/llvm
[root@jouet llvm]# git log | grep mattr=dwarf
bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections This patch introduces a new flag -mattr=dwarfris
[root@jouet llvm]#

Humm, so its -mattr=dwarfris and not -attr=dwarf?

Didn't help :-\

commit 0e0047f8c9ada2f0fe0c5f01579a80e2455b8df5

Author: Yonghong Song <[email protected]>
Date:   Thu Mar 1 23:04:59 2018 +0000

bpf: introduce -mattr=dwarfris to disable DwarfUsesRelocationsAcrossSections

Commit e4507fb8c94b ("bpf: disable DwarfUsesRelocationsAcrossSections") disables MCAsmInfo DwarfUsesRelocationsAcrossSections unconditionally so that dwarf will not use cross section (between dwarf and symbol table) relocations. This new debug format enables pahole to dump structures correctly as libdwarves.so does not have BPF backend support yet.

This new debug format, however, breaks bcc (https://github.com/iovisor/bcc)
source debug output as llvm in-memory Dwarf support has some issues to handle it. More specifically, with DwarfUsesRelocationsAcrossSections disabled, JIT compiler does not generate .debug_abbrev and Dwarf DIE (debug info entry) processing is not happy about this.

This patch introduces a new flag -mattr=dwarfris
(dwarf relocation in section) to disable DwarfUsesRelocationsAcrossSections. DwarfUsesRelocationsAcrossSections is true by default.

git-svn-id: https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_svn_llvm-2Dproject_llvm_trunk-40326505&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=VQnoQ7LvghIj0gVEaiQSUw&m=LO28-RE-2ZJTXto_gff4BgnxXkbUq8d2CEz1jD_wDl4&s=VCR3pGVfY54-OsZ3BqRsOr3FF5JVyltwbnbzu30_4EY&e= 91177308-0d34-0410-b5e6-96231b3b80d8

#29 lore raw
Quoted 2 lines · Arnaldo Carvalho de Melo

Just out of curiosity, is there any plan to have this as a clang option?

I think
clang ... -mllvm -mattr=dwarfris
should work.

#30 lore raw

Em Thu, Jun 14, 2018 at 10:21:30AM -0700, Alexei Starovoitov escreveu:

Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 2 lines

Just out of curiosity, is there any plan to have this as a clang option?

I think
clang ... -mllvm -mattr=dwarfris

thanks, trying...

- Arnaldo

#31 lore raw
In reply to Alexei Starovoitov

Em Thu, Jun 14, 2018 at 10:21:30AM -0700, Alexei Starovoitov escreveu:

Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 1 line

Just out of curiosity, is there any plan to have this as a clang option?

I think
clang ... -mllvm -mattr=dwarfris
should work.

[root@jouet bpf]# cat ~/.perfconfig
[llvm]
dump-obj = true
clang-opt = -g -mllvm -mattr=dwarfris
[root@jouet bpf]# trace -e openat,hello.c touch /tmp/kafai
clang (LLVM option parsing): Unknown command line argument '-mattr=dwarfris'. Try: 'clang (LLVM option parsing) -help' clang (LLVM option parsing): Did you mean '-mxgot=dwarfris'?
ERROR: unable to compile hello.c
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c hello.c
with proper -I and -D options.
event syntax error: 'hello.c'
\___ Failed to load hello.c from source: Error when compiling BPF scriptlet

(add -v to see detail)
<SNIP>
[root@jouet bpf]#

[root@jouet bpf]# trace -e openat,hello.c touch /tmp/kafai |& grep clang
clang (LLVM option parsing): Unknown command line argument '-mattr=dwarfris'. Try: 'clang (LLVM option parsing) -help'
clang (LLVM option parsing): Did you mean '-mxgot=dwarfris'?
clang -target bpf -O2 -c hello.c
[root@jouet bpf]# trace -v -e openat,hello.c touch /tmp/kafai |& grep clang
set env: CLANG_EXEC=/usr/local/bin/clang
llvm compiling command : /usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -mllvm -mattr=dwarfris -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -O2 -o - clang (LLVM option parsing): Unknown command line argument '-mattr=dwarfris'. Try: 'clang (LLVM option parsing) -help'
clang (LLVM option parsing): Did you mean '-mxgot=dwarfris'?
clang -target bpf -O2 -c hello.c
[root@jouet bpf]#

The message "(LLVM option parsing)" implies what you suggest, but didn't worked :-\

-mllvm <value> Additional arguments to forward to LLVM's option processing

Almost there tho :-\

- Arnaldo

#32 lore raw

Em Thu, Jun 14, 2018 at 02:47:59PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Thu, Jun 14, 2018 at 10:21:30AM -0700, Alexei Starovoitov escreveu:

Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 1 line

Just out of curiosity, is there any plan to have this as a clang option?

I think
clang ... -mllvm -mattr=dwarfris
should work.

Quoted 6 lines

The message "(LLVM option parsing)" implies what you suggest, but didn't worked :-\

-mllvm <value> Additional arguments to forward to LLVM's option processing

Almost there tho :-\

So I thought that this -mattr=dwarfris would be available only after I set the target, because I tried 'llc -mattr=help' and dwarfris wasn't there:

[acme@jouet perf]$ llc -mattr=help |& grep dwarf
[acme@jouet perf]$

Only after I set the arch it appears:

[acme@jouet perf]$ llc -march=bpf -mattr=help |& grep dwarf
dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections. dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections. dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections.
[acme@jouet perf]$

But even after moving the '-mllvm -mattr=dwarfris' to after '-target bpf' it still can't grok it :-\

/usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -mllvm -mattr=dwarfris -O2 -o hello.o

So onlye with 'clang ... -target bpf -emit-llvm -O2 -o - | llc -march=bpf -mattr=dwarfris ...' things work as we expect.

- Arnaldo

#33 lore raw
Quoted 31 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 14, 2018 at 02:47:59PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Thu, Jun 14, 2018 at 10:21:30AM -0700, Alexei Starovoitov escreveu:

Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 1 line

Just out of curiosity, is there any plan to have this as a clang option?

I think
clang ... -mllvm -mattr=dwarfris
should work.

Quoted 6 lines

The message "(LLVM option parsing)" implies what you suggest, but didn't worked :-\

-mllvm <value> Additional arguments to forward to LLVM's option processing

Almost there tho :-\

So I thought that this -mattr=dwarfris would be available only after I set the target, because I tried 'llc -mattr=help' and dwarfris wasn't there:

[acme@jouet perf]$ llc -mattr=help |& grep dwarf
[acme@jouet perf]$

Only after I set the arch it appears:

[acme@jouet perf]$ llc -march=bpf -mattr=help |& grep dwarf
dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections. dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections. dwarfris - Disable MCAsmInfo DwarfUsesRelocationsAcrossSections.
[acme@jouet perf]$

Quoted 7 lines

But even after moving the '-mllvm -mattr=dwarfris' to after '-target bpf' it still can't grok it :-\

/usr/local/bin/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -g -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc5/build -c /home/acme/bpf/hello.c -target bpf -mllvm -mattr=dwarfris -O2 -o hello.o

So onlye with 'clang ... -target bpf -emit-llvm -O2 -o - | llc -march=bpf -mattr=dwarfris ...' things work as we expect.

Right. Currently, the only way to use option -mattr=dwarfris is through llc. The "clang -mllvm -mattr=dwarfris" won't work since
-mllvm <value> Additional arguments to forward to LLVM's option
processing
and -mattr=dwarfris is not in LLVM auto option processing system.
Those options, in llvm source code, typically have a pattern like below:
===
static cl::opt<unsigned> MemCmpEqZeroNumLoadsPerBlock(

    "memcmp-num-loads-per-block", cl::Hidden, cl::init(1),
    cl::desc("The number of loads per basic block for inline expansion of "
             "memcmp that is only being compared against zero."));

===

I really want to get rid of this option as well. To make pahole work with the default default format, I need to add bpf support to libdwfl in elfutils repo. I will work on that.

Quoted 2 lines

- Arnaldo

#36 lore raw
In reply to Arnaldo Carvalho de Melo

Hi,

Quoted 101 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 07, 2018 at 12:05:10PM -0700, Martin KaFai Lau escreveu:
dump-obj = true
clang-opt = -g
[root@jouet bpf]# perf trace -e open*,hello.c touch /tmp/hello.BTF
LLVM: dumping hello.o

    0.185 (         ): __bpf_stdout__:Hello, world
    0.188 ( 0.009 ms): touch/19670 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
    0.219 (         ): __bpf_stdout__:Hello, world
    0.220 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /lib64/libc.so.6, flags: CLOEXEC           ) = 3
    0.480 ( 0.095 ms): touch/19670 open(filename: /usr/lib/locale/locale-archive, flags: CLOEXEC         ) = 3
    0.624 (         ): __bpf_stdout__:Hello, world
    0.626 ( 0.011 ms): touch/19670 openat(dfd: CWD, filename: /tmp/hello.BTF, flags: CREAT|NOCTTY|NONBLOCK|WRONLY, mode: IRUGO|IWUGO) = 3

[root@jouet bpf]# file hello.o
hello.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped [root@jouet bpf]#

Much better:

[root@jouet bpf]# readelf -SW hello.o
There are 25 section headers, starting at offset 0xc70:

Section Headers:

 [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
 [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
 [ 1] .strtab           STRTAB          0000000000000000 000b50 000119 00      0   0  1
 [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
 [ 3] syscalls:sys_enter_openat PROGBITS        0000000000000000 000040 000088 00  AX  0   0  8
 [ 4] .relsyscalls:sys_enter_openat REL             0000000000000000 000910 000010 10     24   3  8
 [ 5] license           PROGBITS        0000000000000000 0000c8 000004 00  WA  0   0  1
 [ 6] version           PROGBITS        0000000000000000 0000cc 000004 00  WA  0   0  4
 [ 7] maps              PROGBITS        0000000000000000 0000d0 000010 00  WA  0   0  4
 [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0000e0 00000e 01 AMS  0   0  1
 [ 9] .debug_str        PROGBITS        0000000000000000 0000ee 00010e 01  MS  0   0  1
 [10] .debug_loc        PROGBITS        0000000000000000 0001fc 000023 00      0   0  1
 [11] .debug_abbrev     PROGBITS        0000000000000000 00021f 0000e3 00      0   0  1
 [12] .debug_info       PROGBITS        0000000000000000 000302 00015e 00      0   0  1
 [13] .rel.debug_info   REL             0000000000000000 000920 0001e0 10     24  12  8
 [14] .debug_ranges     PROGBITS        0000000000000000 000460 000030 00      0   0  1
 [15] .debug_macinfo    PROGBITS        0000000000000000 000490 000001 00      0   0  1
 [16] .debug_pubnames   PROGBITS        0000000000000000 000491 00006e 00      0   0  1
 [17] .rel.debug_pubnames REL             0000000000000000 000b00 000010 10     24  16  8
 [18] .debug_pubtypes   PROGBITS        0000000000000000 0004ff 00005a 00      0   0  1
 [19] .rel.debug_pubtypes REL             0000000000000000 000b10 000010 10     24  18  8
 [20] .debug_frame      PROGBITS        0000000000000000 000560 000028 00      0   0  8
 [21] .rel.debug_frame  REL             0000000000000000 000b20 000020 10     24  20  8
 [22] .debug_line       PROGBITS        0000000000000000 000588 00006e 00      0   0  1
 [23] .rel.debug_line   REL             0000000000000000 000b40 000010 10     24  22  8
 [24] .symtab           SYMTAB          0000000000000000 0005f8 000318 18      1  29  8

Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info), L (link order), O (extra OS processing required), G (group), T (TLS), C (compressed), x (unknown), o (OS specific), E (exclude), p (processor specific)
[root@jouet bpf]#

[root@jouet bpf]# readelf -s hello.o

Symbol table '.symtab' contains 33 entries:

  Num:    Value          Size Type    Bind   Vis      Ndx Name
    0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
    1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    9
    2: 000000000000002d     0 NOTYPE  LOCAL  DEFAULT    9
    3: 0000000000000044     0 NOTYPE  LOCAL  DEFAULT    9
    4: 0000000000000053     0 NOTYPE  LOCAL  DEFAULT    9
    5: 000000000000005c     0 NOTYPE  LOCAL  DEFAULT    9
    6: 0000000000000061     0 NOTYPE  LOCAL  DEFAULT    9
    7: 000000000000006a     0 NOTYPE  LOCAL  DEFAULT    9
    8: 0000000000000073     0 NOTYPE  LOCAL  DEFAULT    9
    9: 0000000000000077     0 NOTYPE  LOCAL  DEFAULT    9
   10: 0000000000000086     0 NOTYPE  LOCAL  DEFAULT    9
   11: 000000000000008b     0 NOTYPE  LOCAL  DEFAULT    9
   12: 0000000000000098     0 NOTYPE  LOCAL  DEFAULT    9
   13: 00000000000000a1     0 NOTYPE  LOCAL  DEFAULT    9
   14: 00000000000000ac     0 NOTYPE  LOCAL  DEFAULT    9
   15: 00000000000000b8     0 NOTYPE  LOCAL  DEFAULT    9
   16: 00000000000000c4     0 NOTYPE  LOCAL  DEFAULT    9
   17: 00000000000000d6     0 NOTYPE  LOCAL  DEFAULT    9
   18: 00000000000000e8     0 NOTYPE  LOCAL  DEFAULT    9
   19: 00000000000000fd     0 NOTYPE  LOCAL  DEFAULT    9
   20: 0000000000000101     0 NOTYPE  LOCAL  DEFAULT    9
   21: 0000000000000107     0 NOTYPE  LOCAL  DEFAULT    9
   22: 0000000000000000     0 SECTION LOCAL  DEFAULT    3
   23: 0000000000000000     0 SECTION LOCAL  DEFAULT   10
   24: 0000000000000000     0 SECTION LOCAL  DEFAULT   11
   25: 0000000000000000     0 SECTION LOCAL  DEFAULT   12
   26: 0000000000000000     0 SECTION LOCAL  DEFAULT   14
   27: 0000000000000000     0 SECTION LOCAL  DEFAULT   20
   28: 0000000000000000     0 SECTION LOCAL  DEFAULT   22
   29: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    7 __bpf_stdout__
   30: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    5 _license
   31: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    6 _version
   32: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 syscall_enter_openat

[root@jouet bpf]#

Progress:

[root@jouet bpf]# pahole --btf_encode hello.o
sh: llvm-objcopy: command not found
Failed to encode BTF
[root@jouet bpf]#

Quoted 8 lines

[acme@jouet perf]$ llvm-

llvm-ar          llvm-cov         llvm-dis         llvm-lib         llvm-modextract  llvm-profdata    llvm-split
llvm-as          llvm-c-test      llvm-dlltool     llvm-link        llvm-mt          llvm-ranlib      llvm-stress
llvm-bcanalyzer  llvm-cvtres      llvm-dsymutil    llvm-lto         llvm-nm          llvm-readelf     llvm-strings
llvm-cat         llvm-cxxdump     llvm-dwarfdump   llvm-lto2        llvm-objdump     llvm-readobj     llvm-symbolizer
llvm-config      llvm-cxxfilt     llvm-dwp         llvm-mc          llvm-opt-report  llvm-rtdyld      llvm-tblgen
llvm-config-64   llvm-diff        llvm-extract     llvm-mcmarkup    llvm-pdbutil     llvm-size        llvm-xray

[acme@jouet perf]$ llvm-

Sorry, maybe i make a noise for the thread.
Could you share how you put the output above in mutt's reply mode? Thank you in advance!

Quoted 39 lines

So this must be available in a newer llvm version? Which one?

[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$
[root@jouet bpf]# clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/7 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/7 Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[root@jouet bpf]#

[acme@jouet perf]$ rpm -q clang llvm
clang-5.0.1-5.fc27.x86_64
llvm-5.0.1-6.fc27.x86_64
[acme@jouet perf]$

- Arnaldo

#34 lore raw
In reply to Yonghong Song

Em Thu, Jun 14, 2018 at 09:56:43PM -0700, Yonghong Song escreveu:

Quoted 3 lines

I really want to get rid of this option as well. To make pahole work with the default default format, I need to add bpf support to libdwfl in elfutils repo. I will work on that.

Right, I haven't looked into detail, but perhaps we can do like we do in tools/perf/ where we add a feature test to check if some function is present in a library (elfutils even) and if so, use it, otherwise, use a copy that we carry in pahole.git.

For instance:

tools/perf/util/symbol-elf.c

#ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
static int elf_getphdrnum(Elf *elf, size_t *dst)

{
        GElf_Ehdr gehdr;
        GElf_Ehdr *ehdr;

        ehdr = gelf_getehdr(elf, &gehdr);
        if (!ehdr)
                return -1;

        *dst = ehdr->e_phnum;

        return 0;
}

#endif

And we have a feature test to check if that is present, simple one, if that builds and links, we have it, then the tools build Makefile magic will end up defining HAVE_ELF_GETPHDRNUM_SUPPORT and our copy doesn't get included, using what is in elfutils:

[acme@jouet perf]$ cat tools/build/feature/test-libelf-getphdrnum.c // SPDX-License-Identifier: GPL-2.0
#include <libelf.h>

int main(void)

{
	size_t dst;

	return elf_getphdrnum(0, &dst);
}

[acme@jouet perf]$

[acme@jouet perf]$ grep elf /tmp/build/perf/FEATURE-DUMP feature-libelf=1
feature-libelf-getphdrnum=1 feature-libelf-gelf_getnote=1 feature-libelf-getshdrstrndx=1 feature-libelf-mmap=1
[acme@jouet perf]$

This way a new pahole version won't get to wait till places where it gets built have these new functions and we stop using it as soon as the library get it.

- Arnaldo

#35 lore raw
Quoted 55 lines · Arnaldo Carvalho de Melo

Em Thu, Jun 14, 2018 at 09:56:43PM -0700, Yonghong Song escreveu:

Quoted 3 lines

I really want to get rid of this option as well. To make pahole work with the default default format, I need to add bpf support to libdwfl in elfutils repo. I will work on that.

Right, I haven't looked into detail, but perhaps we can do like we do in tools/perf/ where we add a feature test to check if some function is present in a library (elfutils even) and if so, use it, otherwise, use a copy that we carry in pahole.git.

For instance:

tools/perf/util/symbol-elf.c

#ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
static int elf_getphdrnum(Elf *elf, size_t *dst)

{
         GElf_Ehdr gehdr;
         GElf_Ehdr *ehdr;

         ehdr = gelf_getehdr(elf, &gehdr);
         if (!ehdr)
                 return -1;

         *dst = ehdr->e_phnum;

         return 0;
}

#endif

And we have a feature test to check if that is present, simple one, if that builds and links, we have it, then the tools build Makefile magic will end up defining HAVE_ELF_GETPHDRNUM_SUPPORT and our copy doesn't get included, using what is in elfutils:

[acme@jouet perf]$ cat tools/build/feature/test-libelf-getphdrnum.c // SPDX-License-Identifier: GPL-2.0
#include <libelf.h>

int main(void)

{
	size_t dst;

	return elf_getphdrnum(0, &dst);
}

[acme@jouet perf]$

[acme@jouet perf]$ grep elf /tmp/build/perf/FEATURE-DUMP feature-libelf=1
feature-libelf-getphdrnum=1 feature-libelf-gelf_getnote=1 feature-libelf-getshdrstrndx=1 feature-libelf-mmap=1
[acme@jouet perf]$

Quoted 3 lines

This way a new pahole version won't get to wait till places where it gets built have these new functions and we stop using it as soon as the library get it.

Agreed that later on we can use feature testing to check pahole output is good or not without dwarfris option. If it is good, compilation does not need this option and we could deprecate dwarfris option and eventually remove it once BPF support is in all major elf libraries.

BTW, I have pushed the following commit
https://reviews.llvm.org/rL334839
to clang so now you can compile with attribute dwarfris directly with clang -tartget bpf.

-bash-4.2$ cat t.c
struct tt {
int a;
char b;
int c;
};

int test(struct tt *a) {
return a->a;
}
-bash-4.2$ clang -target bpf -O2 -g -c -Xclang -target-feature -Xclang +dwarfris t.c -bash-4.2$ llvm-objdump -S -d t.o

t.o: file format ELF64-BPF

Disassembly of section .text:
test:

; int test(struct tt *a) {
       0:       61 10 00 00 00 00 00 00         r0 = *(u32 *)(r1 + 0)
; return a->a;
       1:       95 00 00 00 00 00 00 00         exit
-bash-4.2$ pahole t.o
struct tt {
        int                        a;                    /*     0     4 */
        char                       b;                    /*     4     1 */

        /* XXX 3 bytes hole, try to pack */

        int                        c;                    /*     8     4 */

        /* size: 12, cachelines: 1, members: 3 */
        /* sum members: 9, holes: 1, sum holes: 3 */
        /* last cacheline: 12 bytes */
};

-bash-4.2$