Skip to contentSkip to conversation

bpf: implement bpf based dumping of kernel data structures

RFC PATCH In review Yonghong Song 4 participants 26 messages Apr 15, 2020 – Apr 19, 2020

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

v2 cover bpf: implement bpf based dumping of kernel data structures

The v1 version is here:
https://lore.kernel.org/bpf/[email protected]/T/#m058a6817dc3ded9d2db0192ca08486b4a3f4daf0
Compared to v1, I have made changes:
. use BPF_RAW_TRACEPOINT_OPEN to create an anonymous dumper . use BPF_OBJ_PIN with pathname instead dumper name to
create a file dumper
. support PTR_TO_BTF_ID_OR_NULL so bpf program will be called when
the dumping session ends. This gives bpf program an opportunity to print footer or accumulate and send summaries for anonymous dumper.
. use BPF_OBJ_GET_INFO_BY_FD to get bpfdump target/dumper info.
Still missing:
. bpf_seq_printf()/bpf_seq_write() related changes
. double check seq_ops implementation for bpf_map/task/task_file. . libbpf/bpftool implementation
. tests for new features
. ...

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you need change the kernel. For example, Martin wants to dump socket local storage with "ss". Kernel change is needed for it to work ([1]). This is also the direct motivation for this work.

drgn ([2]) solves this proble nicely and no kernel change is not needed. But since drgn is not able to verify the validity of a particular pointer value, it might present the wrong results in rare cases.

In this patch set, we introduce bpf based dumping. Initial kernel changes are still needed, but a data structure change will not require kernel changes any more. bpf program itself is used to adapt to new data structure changes. This will give certain flexibility with guaranteed correctness.

Here, kernel seq_ops is used to facilitate dumping, similar to current /proc and many other lossless kernel dumping facilities.

User Interfaces:
1. A new mount file system, bpfdump at /sys/kernel/bpfdump is introduced. Different from /sys/fs/bpf, this is a single user mount. Mount command can be:
mount -t bpfdump bpfdump /sys/kernel/bpfdump
2. Kernel bpf dumpable data structures are represented as directories under /sys/kernel/bpfdump, e.g., /sys/kernel/bpfdump/ipv6_route/ /sys/kernel/bpfdump/netlink/ /sys/kernel/bpfdump/bpf_map/
/sys/kernel/bpfdump/task/
/sys/kernel/bpfdump/task/file/
In this patch set, we use "target" to represent a particular bpf supported data structure, for example, targets "ipv6_route", "netlink", "bpf_map", "task", "task/file", which are actual directory hierarchy relative to /sys/kernel/bpfdump/.

Note that nested structures are supported for sub fields in a major data structure. For example, target "task/file" is to examine all open files for all tasks (task_struct->files) as reference count and locks are needed to access task_struct->files safely.
3. The bpftool command can be used to create a dumper:
bpftool dumper pin <bpf_prog.o> <dumper_name>
where the bpf_prog.o encodes the target information. For example, the following dumpers can be created:
/sys/kernel/bpfdump/ipv6_route/{my1, my2} /sys/kernel/bpfdump/task/file/{f1, f2}
4. Use "cat <dumper>" to dump the contents.
Use "rm -f <dumper>" to delete the dumper.
5. An anonymous dumper can be created without pinning to a physical file. The fd will return to the application and the application can then "read" the contents.

Please see patch #15 and #16 for bpf programs and
bpf dumper output examples.

Two new helpers bpf_seq_printf() and bpf_seq_write() are introduced. bpf_seq_printf() mostly for file based dumpers and bpf_seq_write() mostly for anonymous dumpers.

Note that certain dumpers are namespace aware. For example, task and task/... targets only iterate through current pid namespace. ipv6_route and netlink will iterate through current net namespace.

For introspection, see patch #14,
bpftool dumper show {target|dumper}
can show all targets and their context structure type name (for writing bpf programs), or all dumpers with their associated bpf prog_id. For any open file descriptors (anonymous or from dumper file),
cat /proc/<pid>/fdinfo/<fd>
will show target and its associated prog_id as well.

Although the initial motivation is from Martin's sk_local_storage, this patch didn't implement tcp6 sockets and sk_local_storage. The /proc/net/tcp6 involves three types of sockets, timewait, request and tcp6 sockets. Some kind of type casting is needed to convert socket_common to these three types of sockets based on socket state. This will be addressed in future work.

References:
[1]: https://lore.kernel.org/bpf/[email protected]
[2]: https://github.com/osandov/drgn

Yonghong Song (17):
net: refactor net assignment for seq_net_private structure
bpf: create /sys/kernel/bpfdump mount file system
bpf: provide a way for targets to register themselves
bpf: allow loading of a dumper program
bpf: create file or anonymous dumpers
bpf: add PTR_TO_BTF_ID_OR_NULL support
bpf: add netlink and ipv6_route targets
bpf: add bpf_map target
bpf: add task and task/file targets
bpf: add bpf_seq_printf and bpf_seq_write helpers
bpf: support variable length array in tracing programs
bpf: implement query for target_proto and file dumper prog_id tools/libbpf: libbpf support for bpfdump
tools/bpftool: add bpf dumper support
tools/bpf: selftests: add dumper programs for ipv6_route and netlink tools/bpf: selftests: add dumper progs for bpf_map/task/task_file tools/bpf: selftests: add a selftest for anonymous dumper

 fs/proc/proc_net.c                            |   5 +-
 include/linux/bpf.h                           |  31 +
 include/linux/seq_file_net.h                  |   8 +
 include/uapi/linux/bpf.h                      |  35 +-
 include/uapi/linux/magic.h                    |   1 +
 kernel/bpf/Makefile                           |   1 +
 kernel/bpf/btf.c                              |  30 +-
 kernel/bpf/dump.c                             | 806 ++++++++++++++++++
 kernel/bpf/dump_task.c                        | 320 +++++++
 kernel/bpf/syscall.c                          | 146 +++-
 kernel/bpf/verifier.c                         |  33 +-
 kernel/trace/bpf_trace.c                      | 172 ++++
 net/ipv6/ip6_fib.c                            |  71 +-
 net/ipv6/route.c                              |  29 +
 net/netlink/af_netlink.c                      |  94 +-
 scripts/bpf_helpers_doc.py                    |   2 +
 tools/bpf/bpftool/dumper.c                    | 135 +++
 tools/bpf/bpftool/main.c                      |   3 +-
 tools/bpf/bpftool/main.h                      |   1 +
 tools/include/uapi/linux/bpf.h                |  35 +-
 tools/lib/bpf/bpf.c                           |   9 +-
 tools/lib/bpf/bpf.h                           |   1 +
 tools/lib/bpf/libbpf.c                        |  88 +-
 tools/lib/bpf/libbpf.h                        |   3 +
 tools/lib/bpf/libbpf.map                      |   2 +
 .../selftests/bpf/prog_tests/bpfdump_test.c   |  42 +
 .../selftests/bpf/progs/bpfdump_bpf_map.c     |  33 +
 .../selftests/bpf/progs/bpfdump_ipv6_route.c  |  71 ++
 .../selftests/bpf/progs/bpfdump_netlink.c     |  80 ++
 .../selftests/bpf/progs/bpfdump_task.c        |  29 +
 .../selftests/bpf/progs/bpfdump_task_file.c   |  30 +
 .../selftests/bpf/progs/bpfdump_test_kern.c   |  31 +
 32 files changed, 2343 insertions(+), 34 deletions(-)
 create mode 100644 kernel/bpf/dump.c
 create mode 100644 kernel/bpf/dump_task.c
 create mode 100644 tools/bpf/bpftool/dumper.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_bpf_map.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_ipv6_route.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_netlink.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_task.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_task_file.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
Signature · 1 line
2.24.1
#1 lore raw

RFC PATCH v2 1/17 net: refactor net assignment for seq_net_private structure

Refactor assignment of "net" in seq_net_private structure in proc_net.c to a helper function. The helper later will be used by bpfdump.

---
 fs/proc/proc_net.c           | 5 ++---
 include/linux/seq_file_net.h | 8 ++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
fs/proc/proc_net.c +2 -3
index 4888c5224442..aee07c19cf8b 100644
@@ -75,9 +75,8 @@ static int seq_open_net(struct inode *inode, struct file *file)
 		put_net(net);
 		return -ENOMEM;
 	}
-#ifdef CONFIG_NET_NS
-	p->net = net;
-#endif
+
+	set_seq_net_private(p, net);
 	return 0;
 }
 
include/linux/seq_file_net.h +8 -0
index 0fdbe1ddd8d1..0ec4a18b9aca 100644
@@ -35,4 +35,12 @@ static inline struct net *seq_file_single_net(struct seq_file *seq)
 #endif
 }
 
+static inline void set_seq_net_private(struct seq_net_private *p,
+				       struct net *net)
+{
+#ifdef CONFIG_NET_NS
+	p->net = net;
+#endif
+}
+
 #endif
Signature · 1 line
2.24.1
#2 lore raw

RFC PATCH v2 2/17 bpf: create /sys/kernel/bpfdump mount file system

In reply to Yonghong Song

This patch creates a mount point "bpfdump" under
/sys/kernel. The file system has a single user
mode, i.e., all mount points will be identical.

The magic number I picked for the new file system
is "dump".

---
 include/uapi/linux/magic.h |  1 +
 kernel/bpf/Makefile        |  1 +
 kernel/bpf/dump.c          | 79 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 kernel/bpf/dump.c
include/uapi/linux/magic.h +1 -0
index d78064007b17..4ce3d8882315 100644
@@ -88,6 +88,7 @@
 #define BPF_FS_MAGIC		0xcafe4a11
 #define AAFS_MAGIC		0x5a3c69f0
 #define ZONEFS_MAGIC		0x5a4f4653
+#define DUMPFS_MAGIC		0x64756d70
 
 /* Since UDF 2.01 is ISO 13346 based... */
 #define UDF_SUPER_MAGIC		0x15013346
kernel/bpf/Makefile +1 -0
index f2d7be596966..4a1376ab2bea 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o
 endif
 ifeq ($(CONFIG_SYSFS),y)
 obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o
+obj-$(CONFIG_BPF_SYSCALL) += dump.o
 endif
 ifeq ($(CONFIG_BPF_JIT),y)
 obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
kernel/bpf/dump.c new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.24.1
#3 lore raw

RFC PATCH v2 3/17 bpf: provide a way for targets to register themselves

In reply to Yonghong Song

Here, the target refers to a particular data structure inside the kernel we want to dump. For example, it can be all task_structs in the current pid namespace, or it could be all open files for all task_structs in the current pid namespace.

Each target is identified with the following information:
target_rel_path <=== relative path to /sys/kernel/bpfdump
target_proto <=== kernel func proto used by kernel verifier
prog_ctx_type_name <=== prog ctx type used by bpf programs

   seq_ops            <=== seq_ops for seq_file operations
   seq_priv_size      <=== seq_file private data size
   target_feature     <=== target specific feature which needs

handling outside seq_ops.

The target relative path is a relative directory to /sys/kernel/bpfdump/. For example, it could be:

   task                  <=== all tasks
   task/file             <=== all open files under all tasks
   ipv6_route            <=== all ipv6_routes

tcp6/sk_local_storage <=== all tcp6 socket local storages
foo/bar/tar <=== all tar's in bar in foo

The "target_feature" is mostly used for reusing existing seq_ops. For example, for /proc/net/<> stats, the "net" namespace is often stored in file private data. The target_feature enables bpf based dumper to set "net" properly for itself before calling shared seq_ops.

bpf_dump_reg_target() is implemented so targets can register themselves. Currently, module is not supported, so there is no bpf_dump_unreg_target(). The main reason is that BTF is not available for modules yet.

Since target might call bpf_dump_reg_target() before bpfdump mount point is created, __bpfdump_init() may be called in bpf_dump_reg_target() as well.

The file-based dumpers will be regular files under
the specific target directory. For example,
task/my1 <=== dumper "my1" iterates through all tasks
task/file/my2 <=== dumper "my2" iterates through all open files
under all tasks

---
 include/linux/bpf.h |  12 +++
 kernel/bpf/dump.c   | 198 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 208 insertions(+), 2 deletions(-)
include/linux/bpf.h +12 -0
index fd2b2322412d..84c7eb40d7bc 100644
@@ -31,6 +31,7 @@ struct seq_file;
 struct btf;
 struct btf_type;
 struct exception_table_entry;
+struct seq_operations;
 
 extern struct idr btf_idr;
 extern spinlock_t btf_idr_lock;
@@ -1109,6 +1110,17 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
+struct bpf_dump_reg {
+	const char *target;
+	const char *target_proto;
+	const char *prog_ctx_type_name;
+	const struct seq_operations *seq_ops;
+	u32 seq_priv_size;
+	u32 target_feature;
+};
+
+int bpf_dump_reg_target(struct bpf_dump_reg *reg_info);
+
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
kernel/bpf/dump.c +196 -2

224 lines not on this page · raw message

Signature · 1 line
2.24.1
#4 lore raw

RFC PATCH v2 4/17 bpf: allow loading of a dumper program

In reply to Yonghong Song

A dumper bpf program is a tracing program with attach type BPF_TRACE_DUMP. During bpf program load, the load attribute
attach_prog_fd
carries the target directory fd. The program will be verified against btf_id of the target_proto.

If the program is loaded successfully, the dump target, as represented as a relative path to /sys/kernel/bpfdump, will be remembered in prog->aux->dump_target, which will be used later to create dumpers.

---
 include/linux/bpf.h            |  2 ++
 include/uapi/linux/bpf.h       |  6 ++++-
 kernel/bpf/dump.c              | 42 ++++++++++++++++++++++++++++++++++
 kernel/bpf/syscall.c           |  8 ++++++-
 kernel/bpf/verifier.c          | 15 ++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++-
 6 files changed, 76 insertions(+), 3 deletions(-)
include/linux/bpf.h +2 -0
index 84c7eb40d7bc..068552c2d2cf 100644
@@ -674,6 +674,7 @@ struct bpf_prog_aux {
 	struct bpf_map **used_maps;
 	struct bpf_prog *prog;
 	struct user_struct *user;
+	const char *dump_target;
 	u64 load_time; /* ns since boottime */
 	struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
 	char name[BPF_OBJ_NAME_LEN];
@@ -1120,6 +1121,7 @@ struct bpf_dump_reg {
 };
 
 int bpf_dump_reg_target(struct bpf_dump_reg *reg_info);
+int bpf_dump_set_target_info(u32 target_fd, struct bpf_prog *prog);
 
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
include/uapi/linux/bpf.h +5 -1
index 2e29a671d67e..f92b919c723e 100644
@@ -215,6 +215,7 @@ enum bpf_attach_type {
 	BPF_TRACE_FEXIT,
 	BPF_MODIFY_RETURN,
 	BPF_LSM_MAC,
+	BPF_TRACE_DUMP,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -476,7 +477,10 @@ union bpf_attr {
 		__aligned_u64	line_info;	/* line info */
 		__u32		line_info_cnt;	/* number of bpf_line_info records */
 		__u32		attach_btf_id;	/* in-kernel BTF type id to attach to */
-		__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+		union {
+			__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+			__u32		attach_target_fd;
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/dump.c +42 -0

54 lines not on this page · raw message

kernel/bpf/syscall.c +7 -1
index 64783da34202..1ce2f74f8efc 100644
@@ -2060,7 +2060,12 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 
 	prog->expected_attach_type = attr->expected_attach_type;
 	prog->aux->attach_btf_id = attr->attach_btf_id;
-	if (attr->attach_prog_fd) {
+	if (type == BPF_PROG_TYPE_TRACING &&
+	    attr->expected_attach_type == BPF_TRACE_DUMP) {
+		err = bpf_dump_set_target_info(attr->attach_target_fd, prog);
+		if (err)
+			goto free_prog_nouncharge;
+	} else if (attr->attach_prog_fd) {
 		struct bpf_prog *tgt_prog;
 
 		tgt_prog = bpf_prog_get(attr->attach_prog_fd);
@@ -2145,6 +2150,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 	err = bpf_prog_new_fd(prog);
 	if (err < 0)
 		bpf_prog_put(prog);
+
 	return err;
 
 free_used_maps:
kernel/bpf/verifier.c +15 -0
index 04c6630cc18f..f531cee24fc5 100644
@@ -10426,6 +10426,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	struct bpf_prog *tgt_prog = prog->aux->linked_prog;
 	u32 btf_id = prog->aux->attach_btf_id;
 	const char prefix[] = "btf_trace_";
+	struct btf_func_model fmodel;
 	int ret = 0, subprog = -1, i;
 	struct bpf_trampoline *tr;
 	const struct btf_type *t;
@@ -10566,6 +10567,20 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 		prog->aux->attach_func_proto = t;
 		prog->aux->attach_btf_trace = true;
 		return 0;
+	case BPF_TRACE_DUMP:
+		if (!btf_type_is_func(t)) {
+			verbose(env, "attach_btf_id %u is not a function\n",
+				btf_id);
+			return -EINVAL;
+		}
+		t = btf_type_by_id(btf, t->type);
+		if (!btf_type_is_func_proto(t))
+			return -EINVAL;
+		prog->aux->attach_func_name = tname;
+		prog->aux->attach_func_proto = t;
+		ret = btf_distill_func_proto(&env->log, btf, t,
+					     tname, &fmodel);
+		return ret;
 	default:
 		if (!prog_extension)
 			return -EINVAL;
tools/include/uapi/linux/bpf.h +5 -1
index 2e29a671d67e..f92b919c723e 100644
@@ -215,6 +215,7 @@ enum bpf_attach_type {
 	BPF_TRACE_FEXIT,
 	BPF_MODIFY_RETURN,
 	BPF_LSM_MAC,
+	BPF_TRACE_DUMP,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -476,7 +477,10 @@ union bpf_attr {
 		__aligned_u64	line_info;	/* line info */
 		__u32		line_info_cnt;	/* number of bpf_line_info records */
 		__u32		attach_btf_id;	/* in-kernel BTF type id to attach to */
-		__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+		union {
+			__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+			__u32		attach_target_fd;
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
Signature · 1 line
2.24.1
#5 lore raw

RFC PATCH v2 5/17 bpf: create file or anonymous dumpers

In reply to Yonghong Song

Given a loaded dumper bpf program, which already knows which target it should bind to, there two ways to create a dumper:
- a file based dumper under hierarchy of /sys/kernel/bpfdump/ which uses can "cat" to print out the output.
- an anonymous dumper which user application can "read" the dumping output.

For file based dumper, BPF_OBJ_PIN syscall interface is used. For anonymous dumper, BPF_RAW_TRACEPOINT_OPEN syscall interface is used.

To facilitate target seq_ops->show() to get the bpf program easily, dumper creation increased the target-provided seq_file private data size so bpf program pointer is also stored in seq_file private data.

A session_id, which is unique for each bpfdump open file, is available to the
bpf program. This can differentiate different sessions if the same program is used
by multiple open files.

A seq_num, which represents how many bpf_dump_get_prog() has been called, is also available to the bpf program.
Such information can be used to e.g., print banner before printing out actual data.

Note the seq_num does not represent the num of unique kernel objects the bpf program has seen. But it should be a good approximate.

A target feature BPF_DUMP_SEQ_NET_PRIVATE is implemented specifically useful for
net based dumpers. It sets net namespace
as the current process net namespace.
This avoids changing existing net seq_ops in order to retrieve net namespace from
the seq_file pointer.

For open dumper files, anonymous or not, the fdinfo will show the target and prog_id associated with that file descriptor. For dumper file itself, a kernel interface will be provided to retrieve the prog_ctx_type and prog_id in one of the later patches.

---
 include/linux/bpf.h  |   7 +
 kernel/bpf/dump.c    | 407 ++++++++++++++++++++++++++++++++++++++++++-
 kernel/bpf/syscall.c |  20 ++-
 3 files changed, 429 insertions(+), 5 deletions(-)
include/linux/bpf.h +7 -0
index 068552c2d2cf..3cc16991c287 100644
@@ -1111,6 +1111,8 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
+#define BPF_DUMP_SEQ_NET_PRIVATE	BIT(0)
+
 struct bpf_dump_reg {
 	const char *target;
 	const char *target_proto;
@@ -1122,6 +1124,11 @@ struct bpf_dump_reg {
 
 int bpf_dump_reg_target(struct bpf_dump_reg *reg_info);
 int bpf_dump_set_target_info(u32 target_fd, struct bpf_prog *prog);
+int bpf_fd_dump_create(u32 prog_fd, const char __user *dumper_name,
+		       bool *is_dump_prog);
+int bpf_prog_dump_create(struct bpf_prog *prog);
+struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size,
+				   u64 *session_id, u64 *seq_num, bool is_last);
 
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
kernel/bpf/dump.c +406 -1

447 lines not on this page · raw message

kernel/bpf/syscall.c +16 -4
index 1ce2f74f8efc..4a3c9fceebb8 100644
@@ -2173,9 +2173,18 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 
 static int bpf_obj_pin(const union bpf_attr *attr)
 {
+	bool is_dump_prog = false;
+	int err;
+
 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
 		return -EINVAL;
 
+	err = bpf_fd_dump_create(attr->bpf_fd,
+				 u64_to_user_ptr(attr->pathname),
+				 &is_dump_prog);
+	if (!err || is_dump_prog)
+		return err;
+
 	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
 }
 
@@ -2490,10 +2499,13 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
 			err = -EINVAL;
 			goto out_put_prog;
 		}
-		if (prog->type == BPF_PROG_TYPE_TRACING &&
-		    prog->expected_attach_type == BPF_TRACE_RAW_TP) {
-			tp_name = prog->aux->attach_func_name;
-			break;
+		if (prog->type == BPF_PROG_TYPE_TRACING) {
+			if (prog->expected_attach_type == BPF_TRACE_RAW_TP) {
+				tp_name = prog->aux->attach_func_name;
+				break;
+			} else if (prog->expected_attach_type == BPF_TRACE_DUMP) {
+				return bpf_prog_dump_create(prog);
+			}
 		}
 		return bpf_tracing_prog_attach(prog);
 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
Signature · 1 line
2.24.1
#6 lore raw

RFC PATCH v2 6/17 bpf: add PTR_TO_BTF_ID_OR_NULL support

In reply to Yonghong Song

Add bpf_reg_type PTR_TO_BTF_ID_OR_NULL support. For tracing/dump program, the bpf program context definition, e.g., for ipv6_route target, looks like

   struct bpfdump__ipv6_route {
     struct bpf_dump_meta *meta;
     struct fib6_info *rt;

};

The kernel guarantees that meta is not NULL, but rt maybe NULL. The NULL rt indicates the data structure traversal has done. So bpf program can take proper action.

Add btf_id_or_null_non0_off to prog->aux structure, to indicate that for tracing programs, if the context access offset is not 0, set to PTR_TO_BTF_ID_OR_NULL instead of PTR_TO_BTF_ID. This bit is set for tracing/dump program.

---
 include/linux/bpf.h   |  2 ++
 kernel/bpf/btf.c      |  5 ++++-
 kernel/bpf/dump.c     |  1 +
 kernel/bpf/verifier.c | 18 +++++++++++++-----
 4 files changed, 20 insertions(+), 6 deletions(-)
include/linux/bpf.h +2 -0
index 3cc16991c287..1179ca3d0230 100644
@@ -320,6 +320,7 @@ enum bpf_reg_type {
 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
 	PTR_TO_BTF_ID,		 /* reg points to kernel struct */
+	PTR_TO_BTF_ID_OR_NULL,	 /* reg points to kernel struct or NULL */
 };
 
 /* The information passed from prog-specific *_is_valid_access
@@ -658,6 +659,7 @@ struct bpf_prog_aux {
 	bool offload_requested;
 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
 	bool func_proto_unreliable;
+	bool btf_id_or_null_non0_off;
 	enum bpf_tramp_prog_type trampoline_prog_type;
 	struct bpf_trampoline *trampoline;
 	struct hlist_node tramp_hlist;
kernel/bpf/btf.c +4 -1
index d65c6912bdaf..2c098e6b1acc 100644
@@ -3788,7 +3788,10 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 		return true;
 
 	/* this is a pointer to another type */
-	info->reg_type = PTR_TO_BTF_ID;
+	if (off != 0 && prog->aux->btf_id_or_null_non0_off)
+		info->reg_type = PTR_TO_BTF_ID_OR_NULL;
+	else
+		info->reg_type = PTR_TO_BTF_ID;
 
 	if (tgt_prog) {
 		ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg);
kernel/bpf/dump.c +1 -0
index f39b82430977..c6d4d64aaa8e 100644
@@ -244,6 +244,7 @@ int bpf_dump_set_target_info(u32 target_fd, struct bpf_prog *prog)
 
 	prog->aux->dump_target = tinfo->target;
 	prog->aux->attach_btf_id = btf_id;
+	prog->aux->btf_id_or_null_non0_off = true;
 
 done:
 	fdput(tfd);
kernel/bpf/verifier.c +13 -5
index f531cee24fc5..af711dd15e08 100644
@@ -382,7 +382,8 @@ static bool reg_type_may_be_null(enum bpf_reg_type type)
 	return type == PTR_TO_MAP_VALUE_OR_NULL ||
 	       type == PTR_TO_SOCKET_OR_NULL ||
 	       type == PTR_TO_SOCK_COMMON_OR_NULL ||
-	       type == PTR_TO_TCP_SOCK_OR_NULL;
+	       type == PTR_TO_TCP_SOCK_OR_NULL ||
+	       type == PTR_TO_BTF_ID_OR_NULL;
 }
 
 static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
@@ -396,7 +397,8 @@ static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
 	return type == PTR_TO_SOCKET ||
 		type == PTR_TO_SOCKET_OR_NULL ||
 		type == PTR_TO_TCP_SOCK ||
-		type == PTR_TO_TCP_SOCK_OR_NULL;
+		type == PTR_TO_TCP_SOCK_OR_NULL ||
+		type == PTR_TO_BTF_ID_OR_NULL;
 }
 
 static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
@@ -448,6 +450,7 @@ static const char * const reg_type_str[] = {
 	[PTR_TO_TP_BUFFER]	= "tp_buffer",
 	[PTR_TO_XDP_SOCK]	= "xdp_sock",
 	[PTR_TO_BTF_ID]		= "ptr_",
+	[PTR_TO_BTF_ID_OR_NULL]	= "null_or_ptr_",
 };
 
 static char slot_type_char[] = {
@@ -508,7 +511,7 @@ static void print_verifier_state(struct bpf_verifier_env *env,
 			/* reg->off should be 0 for SCALAR_VALUE */
 			verbose(env, "%lld", reg->var_off.value + reg->off);
 		} else {
-			if (t == PTR_TO_BTF_ID)
+			if (t == PTR_TO_BTF_ID || t == PTR_TO_BTF_ID_OR_NULL)
 				verbose(env, "%s", kernel_type_name(reg->btf_id));
 			verbose(env, "(id=%d", reg->id);
 			if (reg_type_may_be_refcounted_or_null(t))
@@ -2102,6 +2105,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
 	case PTR_TO_TCP_SOCK_OR_NULL:
 	case PTR_TO_XDP_SOCK:
 	case PTR_TO_BTF_ID:
+	case PTR_TO_BTF_ID_OR_NULL:
 		return true;
 	default:
 		return false;
@@ -2603,7 +2607,7 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
 		 */
 		*reg_type = info.reg_type;
 
-		if (*reg_type == PTR_TO_BTF_ID)
+		if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL)
 			*btf_id = info.btf_id;
 		else
 			env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
@@ -3196,7 +3200,8 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 				 * a sub-register.
 				 */
 				regs[value_regno].subreg_def = DEF_NOT_SUBREG;
-				if (reg_type == PTR_TO_BTF_ID)
+				if (reg_type == PTR_TO_BTF_ID ||
+				    reg_type == PTR_TO_BTF_ID_OR_NULL)
 					regs[value_regno].btf_id = btf_id;
 			}
 			regs[value_regno].type = reg_type;
@@ -6521,6 +6526,8 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
 			reg->type = PTR_TO_SOCK_COMMON;
 		} else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
 			reg->type = PTR_TO_TCP_SOCK;
+		} else if (reg->type == PTR_TO_BTF_ID_OR_NULL) {
+			reg->type = PTR_TO_BTF_ID;
 		}
 		if (is_null) {
 			/* We don't need id and ref_obj_id from this point
@@ -8374,6 +8381,7 @@ static bool reg_type_mismatch_ok(enum bpf_reg_type type)
 	case PTR_TO_TCP_SOCK_OR_NULL:
 	case PTR_TO_XDP_SOCK:
 	case PTR_TO_BTF_ID:
+	case PTR_TO_BTF_ID_OR_NULL:
 		return false;
 	default:
 		return true;
Signature · 1 line
2.24.1
#7 lore raw

RFC PATCH v2 7/17 bpf: add netlink and ipv6_route targets

In reply to Yonghong Song

This patch added netlink and ipv6_route targets, using the same seq_ops (except show()) for /proc/net/{netlink,ipv6_route}.

Since module is not supported for now, ipv6_route is supported only if the IPV6 is built-in, i.e., not compiled as a module. The restriction can be lifted once module is properly supported for bpfdump.

---
 include/linux/bpf.h      |  8 +++-
 kernel/bpf/dump.c        | 13 ++++++
 net/ipv6/ip6_fib.c       | 71 +++++++++++++++++++++++++++++-
 net/ipv6/route.c         | 29 +++++++++++++
 net/netlink/af_netlink.c | 94 +++++++++++++++++++++++++++++++++++++++-
 5 files changed, 210 insertions(+), 5 deletions(-)
include/linux/bpf.h +7 -1
index 1179ca3d0230..401e5bf921a2 100644
@@ -1124,6 +1124,12 @@ struct bpf_dump_reg {
 	u32 target_feature;
 };
 
+struct bpf_dump_meta {
+	struct seq_file *seq;
+	u64 session_id;
+	u64 seq_num;
+};
+
 int bpf_dump_reg_target(struct bpf_dump_reg *reg_info);
 int bpf_dump_set_target_info(u32 target_fd, struct bpf_prog *prog);
 int bpf_fd_dump_create(u32 prog_fd, const char __user *dumper_name,
@@ -1131,7 +1137,7 @@ int bpf_fd_dump_create(u32 prog_fd, const char __user *dumper_name,
 int bpf_prog_dump_create(struct bpf_prog *prog);
 struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size,
 				   u64 *session_id, u64 *seq_num, bool is_last);
-
+int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx);
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
kernel/bpf/dump.c +13 -0
index c6d4d64aaa8e..789b35772a81 100644
@@ -487,6 +487,19 @@ struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size,
 	return extra_data->prog;
 }
 
+int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx)
+{
+	int ret;
+
+	migrate_disable();
+	rcu_read_lock();
+	ret = BPF_PROG_RUN(prog, ctx);
+	rcu_read_unlock();
+	migrate_enable();
+
+	return ret;
+}
+
 int bpf_dump_reg_target(struct bpf_dump_reg *reg_info)
 {
 	struct bpfdump_target_info *tinfo, *ptinfo;
net/ipv6/ip6_fib.c +69 -2

89 lines not on this page · raw message

net/ipv6/route.c +29 -0
index 310cbddaa533..ea87d3f2c363 100644
@@ -6390,10 +6390,31 @@ void __init ip6_route_init_special_entries(void)
   #endif
 }
 
+#if IS_BUILTIN(CONFIG_IPV6)
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
+int __init __bpfdump__ipv6_route(struct bpf_dump_meta *meta, struct fib6_info *rt)
+{
+	return 0;
+}
+#endif
+#endif
+
 int __init ip6_route_init(void)
 {
 	int ret;
 	int cpu;
+#if IS_BUILTIN(CONFIG_IPV6)
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
+	struct bpf_dump_reg reg_info = {
+		.target			= "ipv6_route",
+		.target_proto		= "__bpfdump__ipv6_route",
+		.prog_ctx_type_name	= "bpfdump__ipv6_route",
+		.seq_ops		= &ipv6_route_seq_ops,
+		.seq_priv_size		= sizeof(struct ipv6_route_iter),
+		.target_feature		= BPF_DUMP_SEQ_NET_PRIVATE,
+	};
+#endif
+#endif
 
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
@@ -6452,6 +6473,14 @@ int __init ip6_route_init(void)
 	if (ret)
 		goto out_register_late_subsys;
 
+#if IS_BUILTIN(CONFIG_IPV6)
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
+	ret = bpf_dump_reg_target(&reg_info);
+	if (ret)
+		goto out_register_late_subsys;
+#endif
+#endif
+
 	for_each_possible_cpu(cpu) {
 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
 
net/netlink/af_netlink.c +92 -2

124 lines not on this page · raw message

Signature · 1 line
2.24.1
#8 lore raw

RFC PATCH v2 8/17 bpf: add bpf_map target

In reply to Yonghong Song

This patch added bpf_map target. Traversing all bpf_maps through map_idr. A reference is held for the map during the show() to ensure safety and correctness for field accesses.

---
 kernel/bpf/syscall.c | 116 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
kernel/bpf/syscall.c +116 -0
index 4a3c9fceebb8..e6a4514435c4 100644
@@ -3800,3 +3800,119 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 
 	return err;
 }
+
+struct bpfdump_seq_map_info {
+	struct bpf_map *map;
+	u32 id;
+};
+
+static struct bpf_map *bpf_map_seq_get_next(u32 *id)
+{
+	struct bpf_map *map;
+
+	spin_lock_bh(&map_idr_lock);
+	map = idr_get_next(&map_idr, id);
+	if (map)
+		map = __bpf_map_inc_not_zero(map, false);
+	spin_unlock_bh(&map_idr_lock);
+
+	return map;
+}
+
+static void *bpf_map_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct bpfdump_seq_map_info *info = seq->private;
+	struct bpf_map *map;
+	u32 id = info->id + 1;
+
+	map = bpf_map_seq_get_next(&id);
+	if (!map)
+		return NULL;
+
+	++*pos;
+	info->map = map;
+	info->id = id;
+	return map;
+}
+
+static void *bpf_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	struct bpfdump_seq_map_info *info = seq->private;
+	struct bpf_map *map;
+	u32 id = info->id + 1;
+
+	++*pos;
+	map = bpf_map_seq_get_next(&id);
+	if (!map)
+		return NULL;
+
+	__bpf_map_put(info->map, true);
+	info->map = map;
+	info->id = id;
+	return map;
+}
+
+struct bpfdump__bpf_map {
+	struct bpf_dump_meta *meta;
+	struct bpf_map *map;
+};
+
+int __init __bpfdump__bpf_map(struct bpf_dump_meta *meta, struct bpf_map *map)
+{
+	return 0;
+}
+
+static int bpf_map_seq_show(struct seq_file *seq, void *v)
+{
+	struct bpf_dump_meta meta;
+	struct bpfdump__bpf_map ctx;
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.meta = &meta;
+	ctx.map = v;
+	meta.seq = seq;
+	prog = bpf_dump_get_prog(seq, sizeof(struct bpfdump_seq_map_info),
+				 &meta.session_id, &meta.seq_num,
+				 v == (void *)0);
+	if (prog)
+		ret = bpf_dump_run_prog(prog, &ctx);
+
+	return ret == 0 ? 0 : -EINVAL;
+}
+
+static void bpf_map_seq_stop(struct seq_file *seq, void *v)
+{
+	struct bpfdump_seq_map_info *info = seq->private;
+
+	if (!v)
+		bpf_map_seq_show(seq, v);
+
+	if (info->map) {
+		__bpf_map_put(info->map, true);
+		info->map = NULL;
+	}
+}
+
+static const struct seq_operations bpf_map_seq_ops = {
+	.start	= bpf_map_seq_start,
+	.next	= bpf_map_seq_next,
+	.stop	= bpf_map_seq_stop,
+	.show	= bpf_map_seq_show,
+};
+
+static int __init bpf_map_dump_init(void)
+{
+	struct bpf_dump_reg reg_info = {
+		.target			= "bpf_map",
+		.target_proto 		= "__bpfdump__bpf_map",
+		.prog_ctx_type_name	= "bpfdump__bpf_map",
+		.seq_ops		= &bpf_map_seq_ops,
+		.seq_priv_size		= sizeof(struct bpfdump_seq_map_info),
+		.target_feature		= 0,
+	};
+
+	return bpf_dump_reg_target(&reg_info);
+}
+
+late_initcall(bpf_map_dump_init);
Signature · 1 line
2.24.1
#9 lore raw

RFC PATCH v2 9/17 bpf: add task and task/file targets

In reply to Yonghong Song

Only the tasks belonging to "current" pid namespace are enumerated.

For task/file target, the bpf program will have access to
struct task_struct *task
u32 fd
struct file *file
where fd/file is an open file for the task.

---
 kernel/bpf/Makefile    |   2 +-
 kernel/bpf/dump_task.c | 320 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 321 insertions(+), 1 deletion(-)
 create mode 100644 kernel/bpf/dump_task.c
kernel/bpf/Makefile +1 -1
index 4a1376ab2bea..7e2c73deabab 100644
@@ -26,7 +26,7 @@ obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o
 endif
 ifeq ($(CONFIG_SYSFS),y)
 obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o
-obj-$(CONFIG_BPF_SYSCALL) += dump.o
+obj-$(CONFIG_BPF_SYSCALL) += dump.o dump_task.o
 endif
 ifeq ($(CONFIG_BPF_JIT),y)
 obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
kernel/bpf/dump_task.c new file +320 -0

320 lines not on this page · raw message

Signature · 1 line
2.24.1
#10 lore raw

RFC PATCH v2 10/17 bpf: add bpf_seq_printf and bpf_seq_write helpers

In reply to Yonghong Song

Two helpers bpf_seq_printf and bpf_seq_write, are added for writing data to the seq_file buffer.

bpf_seq_printf supports common format string flag/width/type fields so at least I can get identical results for netlink and ipv6_route targets.

For bpf_seq_printf, return value 1 specifically indicates a write failure due to overflow in order to differentiate the failure from format strings.

For seq_file show, since the same object may be called twice, some bpf_prog might be sensitive to this. With return value indicating overflow happens the bpf program can react differently.

---
 include/uapi/linux/bpf.h       |  18 +++-
 kernel/trace/bpf_trace.c       | 172 +++++++++++++++++++++++++++++++++
 scripts/bpf_helpers_doc.py     |   2 +
 tools/include/uapi/linux/bpf.h |  18 +++-
 4 files changed, 208 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +17 -1
index f92b919c723e..75f3657d526c 100644
@@ -3029,6 +3029,20 @@ union bpf_attr {
  *		* **-EOPNOTSUPP**	Unsupported operation, for example a
  *					call from outside of TC ingress.
  *		* **-ESOCKTNOSUPPORT**	Socket type not supported (reuseport).
+ *
+ * int bpf_seq_printf(struct seq_file *m, const char *fmt, u32 fmt_size, ...)
+ * 	Description
+ * 		seq_printf
+ * 	Return
+ * 		0 if successful, or
+ * 		1 if failure due to buffer overflow, or
+ * 		a negative value for format string related failures.
+ *
+ * int bpf_seq_write(struct seq_file *m, const void *data, u32 len)
+ * 	Description
+ * 		seq_write
+ * 	Return
+ * 		0 if successful, non-zero otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3155,7 +3169,9 @@ union bpf_attr {
 	FN(xdp_output),			\
 	FN(get_netns_cookie),		\
 	FN(get_current_ancestor_cgroup_id),	\
-	FN(sk_assign),
+	FN(sk_assign),			\
+	FN(seq_printf),			\
+	FN(seq_write),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
kernel/trace/bpf_trace.c +172 -0

184 lines not on this page · raw message

scripts/bpf_helpers_doc.py +2 -0
index f43d193aff3a..ded304c96a05 100755
@@ -414,6 +414,7 @@ class PrinterHelpers(Printer):
             'struct sk_reuseport_md',
             'struct sockaddr',
             'struct tcphdr',
+            'struct seq_file',
 
             'struct __sk_buff',
             'struct sk_msg_md',
@@ -450,6 +451,7 @@ class PrinterHelpers(Printer):
             'struct sk_reuseport_md',
             'struct sockaddr',
             'struct tcphdr',
+            'struct seq_file',
     }
     mapped_types = {
             'u8': '__u8',
tools/include/uapi/linux/bpf.h +17 -1
index f92b919c723e..75f3657d526c 100644
@@ -3029,6 +3029,20 @@ union bpf_attr {
  *		* **-EOPNOTSUPP**	Unsupported operation, for example a
  *					call from outside of TC ingress.
  *		* **-ESOCKTNOSUPPORT**	Socket type not supported (reuseport).
+ *
+ * int bpf_seq_printf(struct seq_file *m, const char *fmt, u32 fmt_size, ...)
+ * 	Description
+ * 		seq_printf
+ * 	Return
+ * 		0 if successful, or
+ * 		1 if failure due to buffer overflow, or
+ * 		a negative value for format string related failures.
+ *
+ * int bpf_seq_write(struct seq_file *m, const void *data, u32 len)
+ * 	Description
+ * 		seq_write
+ * 	Return
+ * 		0 if successful, non-zero otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3155,7 +3169,9 @@ union bpf_attr {
 	FN(xdp_output),			\
 	FN(get_netns_cookie),		\
 	FN(get_current_ancestor_cgroup_id),	\
-	FN(sk_assign),
+	FN(sk_assign),			\
+	FN(seq_printf),			\
+	FN(seq_write),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
Signature · 1 line
2.24.1
#11 lore raw

RFC PATCH v2 11/17 bpf: support variable length array in tracing programs

In reply to Yonghong Song

In /proc/net/ipv6_route, we have

  struct fib6_info {
    struct fib6_table *fib6_table;
    ...
    struct fib6_nh fib6_nh[0];
  }
  struct fib6_nh {
    struct fib_nh_common nh_common;
    struct rt6_info **rt6i_pcpu;
    struct rt6_exception_bucket *rt6i_exception_bucket;
  };
  struct fib_nh_common {
    ...
    u8 nhc_gw_family;
    ...

}

The access:
struct fib6_nh *fib6_nh = &rt->fib6_nh;
... fib6_nh->nh_common.nhc_gw_family ...

This patch ensures such an access is handled properly.

---
 kernel/bpf/btf.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
kernel/bpf/btf.c +25 -0
index 2c098e6b1acc..dcee5ca0d501 100644
@@ -3840,6 +3840,31 @@ int btf_struct_access(struct bpf_verifier_log *log,
 	}
 
 	if (off + size > t->size) {
+		/* If the last element is a variable size array, we may
+		 * need to relax the rule.
+		 */
+		struct btf_array *array_elem;
+		u32 vlen = btf_type_vlen(t);
+		u32 last_member_type;
+
+		member = btf_type_member(t);
+		last_member_type = member[vlen - 1].type;
+		mtype = btf_type_by_id(btf_vmlinux, last_member_type);
+		if (!btf_type_is_array(mtype))
+			goto error;
+
+		array_elem = (struct btf_array *)(mtype + 1);
+		if (array_elem->nelems != 0)
+			goto error;
+
+		elem_type = btf_type_by_id(btf_vmlinux, array_elem->type);
+		if (!btf_type_is_struct(elem_type))
+			goto error;
+
+		off = (off - t->size) % elem_type->size;
+		return btf_struct_access(log, elem_type, off, size, atype, next_btf_id);
+
+error:
 		bpf_log(log, "access beyond struct %s at off %u size %u\n",
 			tname, off, size);
 		return -EACCES;
Signature · 1 line
2.24.1
#12 lore raw

RFC PATCH v2 12/17 bpf: implement query for target_proto and file dumper prog_id

In reply to Yonghong Song

Given a fd representing a bpfdump target, user can retrieve the target_proto name which represents the bpf program prototype.

Given a fd representing a file dumper, user can retrieve the bpf_prog id associated with that dumper.

---
 include/linux/bpf.h            |  2 +
 include/uapi/linux/bpf.h       | 11 +++++-
 kernel/bpf/dump.c              | 72 ++++++++++++++++++++++++++++++++++
 kernel/bpf/syscall.c           |  2 +-
 tools/include/uapi/linux/bpf.h | 11 +++++-
 5 files changed, 95 insertions(+), 3 deletions(-)
include/linux/bpf.h +2 -0
index 401e5bf921a2..a1ae8509e735 100644
@@ -1138,6 +1138,8 @@ int bpf_prog_dump_create(struct bpf_prog *prog);
 struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size,
 				   u64 *session_id, u64 *seq_num, bool is_last);
 int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx);
+int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
+
 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
include/uapi/linux/bpf.h +10 -1
index 75f3657d526c..856e3f8a63b8 100644
@@ -533,7 +533,10 @@ union bpf_attr {
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
-		__u32		bpf_fd;
+		union {
+			__u32		bpf_fd;
+			__u32		dump_query_fd;
+		};
 		__u32		info_len;
 		__aligned_u64	info;
 	} info;
@@ -3618,6 +3621,12 @@ struct bpf_btf_info {
 	__u32 id;
 } __attribute__((aligned(8)));
 
+struct bpf_dump_info {
+	__aligned_u64 prog_ctx_type_name;
+	__u32 type_name_buf_len;
+	__u32 prog_id;
+} __attribute__((aligned(8)));
+
 /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
  * by user and intended to be used by socket (e.g. to bind to, depends on
  * attach attach type).
kernel/bpf/dump.c +72 -0

78 lines not on this page · raw message

kernel/bpf/syscall.c +1 -1
index e6a4514435c4..1cde78e53a17 100644
@@ -3358,7 +3358,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
 	else if (f.file->f_op == &btf_fops)
 		err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
 	else
-		err = -EINVAL;
+		err = bpf_dump_query(attr, uattr);
 
 	fdput(f);
 	return err;
tools/include/uapi/linux/bpf.h +10 -1
index 75f3657d526c..856e3f8a63b8 100644
@@ -533,7 +533,10 @@ union bpf_attr {
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
-		__u32		bpf_fd;
+		union {
+			__u32		bpf_fd;
+			__u32		dump_query_fd;
+		};
 		__u32		info_len;
 		__aligned_u64	info;
 	} info;
@@ -3618,6 +3621,12 @@ struct bpf_btf_info {
 	__u32 id;
 } __attribute__((aligned(8)));
 
+struct bpf_dump_info {
+	__aligned_u64 prog_ctx_type_name;
+	__u32 type_name_buf_len;
+	__u32 prog_id;
+} __attribute__((aligned(8)));
+
 /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
  * by user and intended to be used by socket (e.g. to bind to, depends on
  * attach attach type).
Signature · 1 line
2.24.1
#13 lore raw

Re: [RFC PATCH bpf-next v2 12/17] bpf: implement query for target_proto and file dumper prog_id

[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Yonghong,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master net/master net-next/master linus/master v5.7-rc1 next-20200416] [cannot apply to vhost/linux-next ipvs/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Yonghong-Song/bpf-implement-bpf-based-dumping-of-kernel-data-structures/20200416-064938
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: i386-debian-10.3 (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

kernel//bpf/dump.c: In function 'bpf_dump_query':

>> kernel//bpf/dump.c:143:28: error: 'struct bpfdump_target_info' has no member named 'prog_ctx_type_name'
     prog_ctx_type_name = tinfo->prog_ctx_type_name;
                               ^~

vim +143 kernel//bpf/dump.c

    95	
    96	int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
    97	{
    98		struct bpf_dump_info __user *ubpf_dinfo;
    99		struct bpfdump_target_info *tinfo;
   100		struct dumper_inode_info *i_info;
   101		struct bpf_dump_info bpf_dinfo;
   102		const char *prog_ctx_type_name;
   103		void * __user tname_buf;
   104		u32 tname_len, info_len;
   105		struct file *filp;
   106		struct fd qfd;
   107		int err = 0;
   108	
   109		qfd = fdget(attr->info.dump_query_fd);
   110		filp = qfd.file;
   111		if (!filp)
   112			return -EBADF;
   113	
   114		if (filp->f_op != &bpf_dumper_ops &&
   115		    filp->f_inode->i_op != &bpfdump_dir_iops) {
   116			err = -EINVAL;
   117			goto done;
   118		}
   119	
   120		info_len = attr->info.info_len;
   121		ubpf_dinfo = u64_to_user_ptr(attr->info.info);
   122		err = bpf_check_uarg_tail_zero(ubpf_dinfo, sizeof(bpf_dinfo),
   123					       info_len);
   124		if (err)
   125			goto done;
   126		info_len = min_t(u32, sizeof(bpf_dinfo), info_len);
   127	
   128		memset(&bpf_dinfo, 0, sizeof(bpf_dinfo));
   129		if (copy_from_user(&bpf_dinfo, ubpf_dinfo, info_len)) {
   130			err = -EFAULT;
   131			goto done;
   132		}
   133	
   134		/* copy prog_id for dumpers */
   135		if (filp->f_op == &bpf_dumper_ops) {
   136			i_info = filp->f_inode->i_private;
   137			bpf_dinfo.prog_id = i_info->prog->aux->id;
   138			tinfo = i_info->tinfo;
   139		} else {
   140			tinfo = filp->f_inode->i_private;
   141		}
   142	
 > 143		prog_ctx_type_name = tinfo->prog_ctx_type_name;

144
145 tname_len = strlen(prog_ctx_type_name) + 1;
146 if (bpf_dinfo.type_name_buf_len < tname_len) {
147 err = -ENOSPC;
148 goto done;
149 }
150
151 /* copy prog_ctx_type_name */
152 tname_buf = u64_to_user_ptr(bpf_dinfo.prog_ctx_type_name);
153 if (copy_to_user(tname_buf, prog_ctx_type_name, tname_len)) {
154 err = -EFAULT;
155 goto done;
156 }
157
158 /* copy potentially updated bpf_dinfo and info_len */
159 if (copy_to_user(ubpf_dinfo, &bpf_dinfo, info_len) ||
160 put_user(info_len, &uattr->info.info_len))
161 return -EFAULT;
162
163 done:
164 fdput(qfd);
165 return err;
166 }
167

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#14 lore raw

Re: [RFC PATCH bpf-next v2 12/17] bpf: implement query for target_proto and file dumper prog_id

Quoted 18 lines · kbuild test robot

Hi Yonghong,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master net/master net-next/master linus/master v5.7-rc1 next-20200416]
[cannot apply to vhost/linux-next ipvs/master]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://urldefense.proofpoint.com/v2/url?u=https-3A__stackoverflow.com_a_37406982&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=DA8e1B5r073vIqRrFz7MRA&m=LHMcEj8K8aldylxdqEXw1QKNZxwB9ohVMd4MJyHpQ_8&s=K9CBIzalCNAABYVV6OfY4D-6Fl9Gm27j0QVLr78cnvo&e= ]

url:    https://github.com/0day-ci/linux/commits/Yonghong-Song/bpf-implement-bpf-based-dumping-of-kernel-data-structures/20200416-064938
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: i386-debian-10.3 (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
         # save the attached .config to linux build tree
         make ARCH=i386
Quoted 8 lines

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

kernel//bpf/dump.c: In function 'bpf_dump_query':

Quoted 1 line
Quoted 1 line

kernel//bpf/dump.c:143:28: error: 'struct bpfdump_target_info' has no member named 'prog_ctx_type_name'

      prog_ctx_type_name = tinfo->prog_ctx_type_name;

Thanks for the report. Somehow RFC patch missed this so the whole series cannot compile due to this. FYI, will fix the issue and send out another RFC patch soon.

Quoted 82 lines
                                ^~

vim +143 kernel//bpf/dump.c

     95	
     96	int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
     97	{
     98		struct bpf_dump_info __user *ubpf_dinfo;
     99		struct bpfdump_target_info *tinfo;
    100		struct dumper_inode_info *i_info;
    101		struct bpf_dump_info bpf_dinfo;
    102		const char *prog_ctx_type_name;
    103		void * __user tname_buf;
    104		u32 tname_len, info_len;
    105		struct file *filp;
    106		struct fd qfd;
    107		int err = 0;
    108	
    109		qfd = fdget(attr->info.dump_query_fd);
    110		filp = qfd.file;
    111		if (!filp)
    112			return -EBADF;
    113	
    114		if (filp->f_op != &bpf_dumper_ops &&
    115		    filp->f_inode->i_op != &bpfdump_dir_iops) {
    116			err = -EINVAL;
    117			goto done;
    118		}
    119	
    120		info_len = attr->info.info_len;
    121		ubpf_dinfo = u64_to_user_ptr(attr->info.info);
    122		err = bpf_check_uarg_tail_zero(ubpf_dinfo, sizeof(bpf_dinfo),
    123					       info_len);
    124		if (err)
    125			goto done;
    126		info_len = min_t(u32, sizeof(bpf_dinfo), info_len);
    127	
    128		memset(&bpf_dinfo, 0, sizeof(bpf_dinfo));
    129		if (copy_from_user(&bpf_dinfo, ubpf_dinfo, info_len)) {
    130			err = -EFAULT;
    131			goto done;
    132		}
    133	
    134		/* copy prog_id for dumpers */
    135		if (filp->f_op == &bpf_dumper_ops) {
    136			i_info = filp->f_inode->i_private;
    137			bpf_dinfo.prog_id = i_info->prog->aux->id;
    138			tinfo = i_info->tinfo;
    139		} else {
    140			tinfo = filp->f_inode->i_private;
    141		}
    142	
Quoted 1 line

143 prog_ctx_type_name = tinfo->prog_ctx_type_name;

    144	
    145		tname_len = strlen(prog_ctx_type_name) + 1;
    146		if (bpf_dinfo.type_name_buf_len < tname_len) {
    147			err = -ENOSPC;
    148			goto done;
    149		}
    150	
    151		/* copy prog_ctx_type_name */
    152		tname_buf = u64_to_user_ptr(bpf_dinfo.prog_ctx_type_name);
    153		if (copy_to_user(tname_buf, prog_ctx_type_name, tname_len)) {
    154			err = -EFAULT;
    155			goto done;
    156		}
    157	
    158		/* copy potentially updated bpf_dinfo and info_len */
    159		if (copy_to_user(ubpf_dinfo, &bpf_dinfo, info_len) ||
    160		    put_user(info_len, &uattr->info.info_len))
    161			return -EFAULT;
    162	
    163	done:
    164		fdput(qfd);
    165		return err;
    166	}
    167	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.01.org_hyperkitty_list_kbuild-2Dall-40lists.01.org&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=DA8e1B5r073vIqRrFz7MRA&m=LHMcEj8K8aldylxdqEXw1QKNZxwB9ohVMd4MJyHpQ_8&s=fAl6kDNiR5pwjMzanMk8h2IJTWcp-pG4N_1ES9a6-0I&e=

#15 lore raw

RFC PATCH v2 13/17 tools/libbpf: libbpf support for bpfdump

In reply to Yonghong Song

Add a few libbpf APIs for bpfdump pin.

Also, parse the dump program section name, retrieve the dump target path and open the path to get a fd and assignment to prog->attach_target_fd. The implementation is absolutely minimum and hacky now.

---
 tools/lib/bpf/bpf.c      |  9 +++-
 tools/lib/bpf/bpf.h      |  1 +
 tools/lib/bpf/libbpf.c   | 88 +++++++++++++++++++++++++++++++++++++---
 tools/lib/bpf/libbpf.h   |  3 ++
 tools/lib/bpf/libbpf.map |  2 +
 5 files changed, 95 insertions(+), 8 deletions(-)
tools/lib/bpf/bpf.c +7 -2
index 5cc1b0785d18..b23f11c53109 100644
@@ -238,10 +238,15 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS ||
 	    attr.prog_type == BPF_PROG_TYPE_LSM) {
 		attr.attach_btf_id = load_attr->attach_btf_id;
-	} else if (attr.prog_type == BPF_PROG_TYPE_TRACING ||
-		   attr.prog_type == BPF_PROG_TYPE_EXT) {
+	} else if (attr.prog_type == BPF_PROG_TYPE_EXT) {
 		attr.attach_btf_id = load_attr->attach_btf_id;
 		attr.attach_prog_fd = load_attr->attach_prog_fd;
+	} else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
+		attr.attach_btf_id = load_attr->attach_btf_id;
+		if (attr.expected_attach_type == BPF_TRACE_DUMP)
+			attr.attach_target_fd = load_attr->attach_target_fd;
+		else
+			attr.attach_prog_fd = load_attr->attach_prog_fd;
 	} else {
 		attr.prog_ifindex = load_attr->prog_ifindex;
 		attr.kern_version = load_attr->kern_version;
tools/lib/bpf/bpf.h +1 -0
index 46d47afdd887..7f8d740afde9 100644
@@ -81,6 +81,7 @@ struct bpf_load_program_attr {
 	union {
 		__u32 kern_version;
 		__u32 attach_prog_fd;
+		__u32 attach_target_fd;
 	};
 	union {
 		__u32 prog_ifindex;
tools/lib/bpf/libbpf.c +82 -6

144 lines not on this page · raw message

tools/lib/bpf/libbpf.h +3 -0
index 44df1d3e7287..e0d31e93d21c 100644
@@ -217,6 +217,9 @@ LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
 LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path);
 LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
 
+LIBBPF_API int bpf_dump__pin(struct bpf_program *prog, const char *dname);
+LIBBPF_API int bpf_dump__unpin(struct bpf_program *prog, const char *dname);
+
 struct bpf_link;
 
 LIBBPF_API struct bpf_link *bpf_link__open(const char *path);
tools/lib/bpf/libbpf.map +2 -0
index bb8831605b25..0beb70bfe65a 100644
@@ -238,6 +238,8 @@ LIBBPF_0.0.7 {
 
 LIBBPF_0.0.8 {
 	global:
+		bpf_dump__pin;
+		bpf_dump__unpin;
 		bpf_link__fd;
 		bpf_link__open;
 		bpf_link__pin;
Signature · 1 line
2.24.1
#16 lore raw

RFC PATCH v2 14/17 tools/bpftool: add bpf dumper support

In reply to Yonghong Song

Implemented "bpftool dumper" command. Two subcommands now:
bpftool dumper pin <bpf_prog.o> <dumper_name> bpftool dumper show {target|dumper}

The "pin" subcommand will create a dumper with <dumper_name> under the dump target (specified in <bpf_prog>.o).
The "show" subcommand will show bpf prog ctx type name , which will be useful on how to write the bpf program, and
the "dumper" subcommand will further show the corresponding prog_id for each dumper.

For example, with some of later selftest dumpers are pinned in the kernel, we can do inspection like below:

  $ bpftool dumper show target
  target                  prog_ctx_type
  task                    bpfdump__task
  task/file               bpfdump__task_file
  bpf_map                 bpfdump__bpf_map
  ipv6_route              bpfdump__ipv6_route
  netlink                 bpfdump__netlink
  $ bpftool dumper show dumper
  dumper                  prog_id   prog_ctx_type
  task/my1                8         bpfdump__task
  task/file/my1           12        bpfdump__task_file
  bpf_map/my1             4         bpfdump__bpf_map
  ipv6_route/my2          16        bpfdump__ipv6_route
  netlink/my2             24        bpfdump__ipv6_route
  netlink/my3             20        bpfdump__netlink
---
 tools/bpf/bpftool/dumper.c | 135 +++++++++++++++++++++++++++++++++++++
 tools/bpf/bpftool/main.c   |   3 +-
 tools/bpf/bpftool/main.h   |   1 +
 3 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 tools/bpf/bpftool/dumper.c
tools/bpf/bpftool/dumper.c new file +135 -0
new file mode 100644
index 000000000000..46ca9d1d9a67
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (C) 2020 Facebook
+// Author: Yonghong Song <[email protected]>
+
+#define _GNU_SOURCE
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <ftw.h>
+
+#include <linux/err.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+
+#include "main.h"
+
+static int do_pin(int argc, char **argv)
+{
+	struct bpf_program *prog;
+	struct bpf_object *obj;
+	const char *objfile, *dname;
+	int err;
+
+	if (!REQ_ARGS(2)) {
+		usage();
+		return -1;
+	}
+
+	objfile = GET_ARG();
+	dname = GET_ARG();
+
+	obj = bpf_object__open(objfile);
+	if (IS_ERR_OR_NULL(obj))
+		return -1;
+
+	err = bpf_object__load(obj);
+	if (err < 0)
+		return -1;
+
+	prog = bpf_program__next(NULL, obj);
+	return bpf_dump__pin(prog, dname);
+}
+
+static bool for_targets;
+static const char *bpfdump_root = "/sys/kernel/bpfdump";
+
+static int check_file(const char *fpath, const struct stat *sb,
+		      int typeflag, struct FTW *ftwbuf)
+{
+	char prog_ctx_cname_buf[64];
+	struct bpf_dump_info info;
+	unsigned info_len;
+	const char *name;
+	int ret, fd;
+
+	if ((for_targets && typeflag == FTW_F) ||
+	    (!for_targets && typeflag == FTW_D))
+		return 0;
+
+	if (for_targets && strcmp(fpath, bpfdump_root) == 0)
+		return 0;
+
+	fd = open(fpath, O_RDONLY);
+	if (fd < 0)
+		return fd;
+
+	info_len = sizeof(info);
+	memset(&info, 0, info_len);
+	info.prog_ctx_type_name  = ptr_to_u64(prog_ctx_cname_buf);
+	info.type_name_buf_len = sizeof(prog_ctx_cname_buf);
+	ret = bpf_obj_get_info_by_fd(fd, &info, &info_len);
+	if (ret < 0)
+		goto done;
+
+	name = fpath + strlen(bpfdump_root) + 1;
+	if (for_targets)
+		fprintf(stdout, "%-24s%-24s\n", name, prog_ctx_cname_buf);
+	else
+		fprintf(stdout, "%-24s%-10d%-24s\n", name, info.prog_id,
+			prog_ctx_cname_buf);
+
+done:
+	close(fd);
+	return ret;
+}
+
+static int do_show(int argc, char **argv)
+{
+	int flags = FTW_PHYS;
+	int nopenfd = 16;
+	const char *spec;
+
+	if (!REQ_ARGS(1)) {
+		usage();
+		return -1;
+	}
+
+	spec = GET_ARG();
+	if (strcmp(spec, "target") == 0) {
+		for_targets = true;
+		fprintf(stdout, "target                  prog_ctx_type\n");
+	} else if (strcmp(spec, "dumper") == 0) {
+		fprintf(stdout, "dumper                  prog_id   prog_ctx_type\n");
+		for_targets = false;
+	} else {
+		return -1;
+	}
+
+	if (nftw(bpfdump_root, check_file, nopenfd, flags) == -1)
+		return -1;
+
+	return 0;
+}
+
+static int do_help(int argc, char **argv)
+{
+	return 0;
+}
+
+static const struct cmd cmds[] = {
+	{ "help",	do_help },
+	{ "show",	do_show },
+	{ "pin",	do_pin },
+	{ 0 }
+};
+
+int do_dumper(int argc, char **argv)
+{
+	return cmd_select(cmds, argc, argv, do_help);
+}
tools/bpf/bpftool/main.c +2 -1
index 466c269eabdd..8489aba6543d 100644
@@ -58,7 +58,7 @@ static int do_help(int argc, char **argv)
 		"       %s batch file FILE\n"
 		"       %s version\n"
 		"\n"
-		"       OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
+		"       OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops | dumper}\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
 		bin_name, bin_name, bin_name);
@@ -222,6 +222,7 @@ static const struct cmd cmds[] = {
 	{ "btf",	do_btf },
 	{ "gen",	do_gen },
 	{ "struct_ops",	do_struct_ops },
+	{ "dumper",	do_dumper },
 	{ "version",	do_version },
 	{ 0 }
 };
tools/bpf/bpftool/main.h +1 -0
index 86f14ce26fd7..2c59f319bbe9 100644
@@ -162,6 +162,7 @@ int do_feature(int argc, char **argv);
 int do_btf(int argc, char **argv);
 int do_gen(int argc, char **argv);
 int do_struct_ops(int argc, char **argv);
+int do_dumper(int argc, char **arg);
 
 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
 int prog_parse_fd(int *argc, char ***argv);
Signature · 1 line
2.24.1
#17 lore raw

RFC PATCH v2 15/17 tools/bpf: selftests: add dumper programs for ipv6_route and netlink

In reply to Yonghong Song

Two bpf programs are added in this patch for netlink and ipv6_route target. On my VM, I am able to achieve identical
results compared to /proc/net/netlink and /proc/net/ipv6_route.

  $ cat /proc/net/netlink
  sk               Eth Pid        Groups   Rmem     Wmem     Dump  Locks    Drops    Inode
  000000002c42d58b 0   0          00000000 0        0        0     2        0        7
  00000000a4e8b5e1 0   1          00000551 0        0        0     2        0        18719
  00000000e1b1c195 4   0          00000000 0        0        0     2        0        16422
  000000007e6b29f9 6   0          00000000 0        0        0     2        0        16424
  ....
  00000000159a170d 15  1862       00000002 0        0        0     2        0        1886
  000000009aca4bc9 15  3918224839 00000002 0        0        0     2        0        19076
  00000000d0ab31d2 15  1          00000002 0        0        0     2        0        18683
  000000008398fb08 16  0          00000000 0        0        0     2        0        27
  $ cat /sys/kernel/bpfdump/netlink/my1
  sk               Eth Pid        Groups   Rmem     Wmem     Dump  Locks    Drops    Inode
  000000002c42d58b 0   0          00000000 0        0        0     2        0        7
  00000000a4e8b5e1 0   1          00000551 0        0        0     2        0        18719
  00000000e1b1c195 4   0          00000000 0        0        0     2        0        16422
  000000007e6b29f9 6   0          00000000 0        0        0     2        0        16424
  ....
  00000000159a170d 15  1862       00000002 0        0        0     2        0        1886
  000000009aca4bc9 15  3918224839 00000002 0        0        0     2        0        19076
  00000000d0ab31d2 15  1          00000002 0        0        0     2        0        18683
  000000008398fb08 16  0          00000000 0        0        0     2        0        27
  $ cat /proc/net/ipv6_route
  fe800000000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000001 00000000 00000001     eth0
  00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 00000000 00200200       lo
  00000000000000000000000000000001 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000003 00000000 80200001       lo
  fe80000000000000c04b03fffe7827ce 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000002 00000000 80200001     eth0
  ff000000000000000000000000000000 08 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000003 00000000 00000001     eth0
  00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 00000000 00200200       lo
  $ cat /sys/kernel/bpfdump/ipv6_route/my1
  fe800000000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000001 00000000 00000001     eth0
  00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 00000000 00200200       lo
  00000000000000000000000000000001 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000003 00000000 80200001       lo
  fe80000000000000c04b03fffe7827ce 80 00000000000000000000000000000000 00 00000000000000000000000000000000 00000000 00000002 00000000 80200001     eth0
  ff000000000000000000000000000000 08 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000003 00000000 00000001     eth0
  00000000000000000000000000000000 00 00000000000000000000000000000000 00 00000000000000000000000000000000 ffffffff 00000001 00000000 00200200       lo
---
 .../selftests/bpf/progs/bpfdump_ipv6_route.c  | 71 ++++++++++++++++
 .../selftests/bpf/progs/bpfdump_netlink.c     | 80 +++++++++++++++++++
 2 files changed, 151 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_ipv6_route.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_netlink.c
tools/testing/selftests/bpf/progs/bpfdump_ipv6_route.c new file +71 -0
new file mode 100644
index 000000000000..ff187577e2b5
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+extern bool CONFIG_IPV6_SUBTREES __kconfig __weak;
+
+#define	RTF_GATEWAY		0x0002
+#define IFNAMSIZ		16
+#define fib_nh_gw_family        nh_common.nhc_gw_family
+#define fib_nh_gw6              nh_common.nhc_gw.ipv6
+#define fib_nh_dev              nh_common.nhc_dev
+
+SEC("dump//sys/kernel/bpfdump/ipv6_route")
+int dump_ipv6_route(struct bpfdump__ipv6_route *ctx)
+{
+	static const char fmt1[] = "%pi6 %02x ";
+	static const char fmt2[] = "%pi6 ";
+	static const char fmt3[] = "00000000000000000000000000000000 ";
+	static const char fmt4[] = "%08x %08x ";
+	static const char fmt5[] = "%8s\n";
+	static const char fmt6[] = "\n";
+	static const char fmt7[] = "00000000000000000000000000000000 00 ";
+	struct seq_file *seq = ctx->meta->seq;
+	struct fib6_info *rt = ctx->rt;
+	const struct net_device *dev;
+	struct fib6_nh *fib6_nh;
+	unsigned int flags;
+	struct nexthop *nh;
+
+	if (rt == (void *)0)
+		return 0;
+
+	fib6_nh = &rt->fib6_nh[0];
+	flags = rt->fib6_flags;
+
+	/* FIXME: nexthop_is_multipath is not handled here. */
+	nh = rt->nh;
+	if (rt->nh)
+		fib6_nh = &nh->nh_info->fib6_nh;
+
+	bpf_seq_printf(seq, fmt1, sizeof(fmt1), &rt->fib6_dst.addr,
+		       rt->fib6_dst.plen);
+
+	if (CONFIG_IPV6_SUBTREES)
+		bpf_seq_printf(seq, fmt1, sizeof(fmt1), &rt->fib6_src.addr,
+			       rt->fib6_src.plen);
+	else
+		bpf_seq_printf(seq, fmt7, sizeof(fmt7));
+
+	if (fib6_nh->fib_nh_gw_family) {
+		flags |= RTF_GATEWAY;
+		bpf_seq_printf(seq, fmt2, sizeof(fmt2), &fib6_nh->fib_nh_gw6);
+	} else {
+		bpf_seq_printf(seq, fmt3, sizeof(fmt3));
+	}
+
+	dev = fib6_nh->fib_nh_dev;
+	bpf_seq_printf(seq, fmt4, sizeof(fmt4), rt->fib6_metric, rt->fib6_ref.refs.counter);
+	bpf_seq_printf(seq, fmt4, sizeof(fmt4), 0, flags);
+	if (dev)
+		bpf_seq_printf(seq, fmt5, sizeof(fmt5), dev->name);
+	else
+		bpf_seq_printf(seq, fmt6, sizeof(fmt6));
+
+	return 0;
+}
tools/testing/selftests/bpf/progs/bpfdump_netlink.c new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.24.1
#18 lore raw

RFC PATCH v2 16/17 tools/bpf: selftests: add dumper progs for bpf_map/task/task_file

In reply to Yonghong Song

The implementation is arbitrary, just to show how the bpf programs can be written for bpf_map/task/task_file. They can be costomized for specific needs.

For example, for bpf_map, the dumper prints out:

  $ cat /sys/kernel/bpfdump/bpf_map/my1
      id   refcnt  usercnt  locked_vm
       3        2        0         20
       6        2        0         20
       9        2        0         20
      12        2        0         20
      13        2        0         20
      16        2        0         20
      19        2        0         20
      === END ===

For task, the dumper prints out:

  $ cat /sys/kernel/bpfdump/task/my1
    tgid      gid
       1        1
       2        2
    ....
    1944     1944
    1948     1948
    1949     1949
    1953     1953
    === END ===

For task/file, the dumper prints out:

  $ cat /sys/kernel/bpfdump/task/file/my1
    tgid      gid       fd      file
       1        1        0 ffffffff95c97600
       1        1        1 ffffffff95c97600
       1        1        2 ffffffff95c97600
    ....
    1895     1895      255 ffffffff95c8fe00
    1932     1932        0 ffffffff95c8fe00
    1932     1932        1 ffffffff95c8fe00
    1932     1932        2 ffffffff95c8fe00
    1932     1932        3 ffffffff95c185c0

This is able to print out all open files (fd and file->f_op), so user can compare f_op against a particular kernel file operations to find what it is. For example, from /proc/kallsyms, we can find
ffffffff95c185c0 r eventfd_fops
so we will know tgid 1932 fd 3 is an eventfd file descriptor.

---
 .../selftests/bpf/progs/bpfdump_bpf_map.c     | 33 +++++++++++++++++++
 .../selftests/bpf/progs/bpfdump_task.c        | 29 ++++++++++++++++
 .../selftests/bpf/progs/bpfdump_task_file.c   | 30 +++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_bpf_map.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_task.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_task_file.c
tools/testing/selftests/bpf/progs/bpfdump_bpf_map.c new file +33 -0
new file mode 100644
index 000000000000..94e97a5358e9
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("dump//sys/kernel/bpfdump/bpf_map")
+int dump_bpf_map(struct bpfdump__bpf_map *ctx)
+{
+	static const char banner[] = "      id   refcnt  usercnt  locked_vm\n";
+	static const char footer[] = "      === END ===\n";
+	static const char fmt1[] = "%8u %8ld ";
+	static const char fmt2[] = "%8ld %10lu\n";
+	struct seq_file *seq = ctx->meta->seq;
+	__u64 seq_num = ctx->meta->seq_num;
+	struct bpf_map *map = ctx->map;
+
+	if (map == (void *)0) {
+		bpf_seq_printf(seq, footer, sizeof(footer));
+		return 0;
+	}
+
+	if (seq_num == 0)
+		bpf_seq_printf(seq, banner, sizeof(banner));
+
+	bpf_seq_printf(seq, fmt1, sizeof(fmt1), map->id, map->refcnt.counter);
+	bpf_seq_printf(seq, fmt2, sizeof(fmt2), map->usercnt.counter,
+		       map->memory.user->locked_vm.counter);
+	return 0;
+}
tools/testing/selftests/bpf/progs/bpfdump_task.c new file +29 -0
new file mode 100644
index 000000000000..70c5b14934a2
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("dump//sys/kernel/bpfdump/task")
+int dump_tasks(struct bpfdump__task *ctx)
+{
+	static char const banner[] = "    tgid      gid\n";
+	static char const footer[] = "=== END ===\n";
+	static char const fmt[] = "%8d %8d\n";
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+
+	if (task == (void *)0) {
+		bpf_seq_printf(seq, footer, sizeof(footer));
+		return 0;
+	}
+
+	if (ctx->meta->seq_num == 0)
+		bpf_seq_printf(seq, banner, sizeof(banner));
+
+	bpf_seq_printf(seq, fmt, sizeof(fmt), task->tgid, task->pid);
+	return 0;
+}
tools/testing/selftests/bpf/progs/bpfdump_task_file.c new file +30 -0
new file mode 100644
index 000000000000..95e3ff6f8a06
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("dump//sys/kernel/bpfdump/task/file")
+int dump_tasks(struct bpfdump__task_file *ctx)
+{
+	static char const banner[] = "    tgid      gid       fd      file\n";
+	static char const fmt1[] = "%8d %8d";
+	static char const fmt2[] = " %8d %lx\n";
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+	__u32 fd = ctx->fd;
+	struct file *file = ctx->file;
+
+	if (task == (void *)0 || file == (void *)0)
+		return 0;
+
+	if (ctx->meta->seq_num == 0)
+		bpf_seq_printf(seq, banner, sizeof(banner));
+
+	bpf_seq_printf(seq, fmt1, sizeof(fmt1), task->tgid, task->pid);
+	bpf_seq_printf(seq, fmt2, sizeof(fmt2), fd, (long)file->f_op);
+	return 0;
+}
Signature · 1 line
2.24.1
#19 lore raw

RFC PATCH v2 17/17 tools/bpf: selftests: add a selftest for anonymous dumper

In reply to Yonghong Song

The selftest creates a anonymous dumper for the
/sys/kernel/bpfdump/task/ target and ensure the
user space got the expected contents. Both
bpf_seq_printf() and bpf_seq_write() helpers
are tested in this selftest.

  $ test_progs -n 2
  #2 bpfdump_test:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/bpfdump_test.c   | 42 +++++++++++++++++++
 .../selftests/bpf/progs/bpfdump_test_kern.c   | 31 ++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpfdump_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpfdump_test_kern.c
tools/testing/selftests/bpf/prog_tests/bpfdump_test.c new file +42 -0
new file mode 100644
index 000000000000..8978e04c3ca9
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "bpfdump_test_kern.skel.h"
+
+void test_bpfdump_test(void)
+{
+	int err, prog_fd, dumper_fd, duration = 0;
+	struct bpfdump_test_kern *skel;
+	char buf[16] = {};
+	const char *expected = "0A1B2C3D";
+
+	skel = bpfdump_test_kern__open_and_load();
+	if (CHECK(!skel, "skel_open_and_load",
+		  "skeleton open_and_load failed\n"))
+		return;
+
+	prog_fd = bpf_program__fd(skel->progs.dump_tasks);
+	dumper_fd = bpf_raw_tracepoint_open(NULL, prog_fd);
+	if (CHECK(dumper_fd < 0, "bpf_raw_tracepoint_open",
+		  "anonymous dumper creation failed\n"))
+		goto destroy_skel;
+
+	err = -EINVAL;
+	while (read(dumper_fd, buf, sizeof(buf)) > 0) {
+		if (CHECK(!err, "read", "unexpected extra read\n"))
+			goto close_fd;
+
+		err = strcmp(buf, expected) != 0;
+		if (CHECK(err, "read",
+			  "read failed: buf %s, expected %s\n", buf,
+			  expected))
+			goto close_fd;
+	}
+
+	CHECK(err, "read", "real failed: no read, expected %s\n",
+	      expected);
+
+close_fd:
+	close(dumper_fd);
+destroy_skel:
+	bpfdump_test_kern__destroy(skel);
+}
tools/testing/selftests/bpf/progs/bpfdump_test_kern.c new file +31 -0
new file mode 100644
index 000000000000..f6bd61a75a22
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+int count = 0;
+
+SEC("dump//sys/kernel/bpfdump/task")
+int dump_tasks(struct bpfdump__task *ctx)
+{
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+	static char fmt[] = "%d";
+	char c;
+
+	if (task == (void *)0)
+		return 0;
+
+	if (count < 4) {
+		bpf_seq_printf(seq, fmt, sizeof(fmt), count);
+		c = 'A' + count;
+		bpf_seq_write(seq, &c, sizeof(c));
+		count++;
+	}
+
+	return 0;
+}
Signature · 1 line
2.24.1
#20 lore raw
In reply to Yonghong Song
Quoted 5 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Quoted 7 lines

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you need change the kernel. For example, Martin wants to dump socket local

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

Quoted 15 lines

storage with "ss". Kernel change is needed for it to work ([1]). This is also the direct motivation for this work.

drgn ([2]) solves this proble nicely and no kernel change is not needed. But since drgn is not able to verify the validity of a particular pointer value, it might present the wrong results in rare cases.

In this patch set, we introduce bpf based dumping. Initial kernel changes are still needed, but a data structure change will not require kernel changes any more. bpf program itself is used to adapt to new data structure changes. This will give certain flexibility with guaranteed correctness.

Here, kernel seq_ops is used to facilitate dumping, similar to current /proc and many other lossless kernel dumping facilities.

Quoted 9 lines

User Interfaces:
1. A new mount file system, bpfdump at /sys/kernel/bpfdump is introduced. Different from /sys/fs/bpf, this is a single user mount. Mount command can be:
mount -t bpfdump bpfdump /sys/kernel/bpfdump
2. Kernel bpf dumpable data structures are represented as directories under /sys/kernel/bpfdump, e.g., /sys/kernel/bpfdump/ipv6_route/ /sys/kernel/bpfdump/netlink/

The names of bpfdump fs entries do not match actual data structure names
- e.g., there is no ipv6_route struct. On the one hand that is a good thing since structure names can change, but that also means a mapping is needed between the dumper filesystem entries and what you get for context.

Further, what is the expectation in terms of stable API for these fs entries? Entries in the context can change. Data structure names can change. Entries in the structs can change. All of that breaks the idea of stable programs that are compiled once and run for all future releases. When structs change, those programs will break - and structures will change.

What does bpfdumper provide that you can not do with a tracepoint on a relevant function and then putting a program on the tracepoint? ie., why not just put a tracepoint in the relevant dump functions.

#21 lore raw
Quoted 14 lines · David Ahern
Quoted 5 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Quoted 7 lines

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you need change the kernel. For example, Martin wants to dump socket local

Quoted 3 lines

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

Yes, as mentioned two paragraphs below. kernel change is required. The tradeoff is that this is a one-time investment. Once kernel change is in place, printing new fields (in most cases except new fields which need additional locks etc.) no need for kernel change any more.

Quoted 27 lines
Quoted 17 lines

storage with "ss". Kernel change is needed for it to work ([1]). This is also the direct motivation for this work.

drgn ([2]) solves this proble nicely and no kernel change is not needed. But since drgn is not able to verify the validity of a particular pointer value, it might present the wrong results in rare cases.

In this patch set, we introduce bpf based dumping. Initial kernel changes are still needed, but a data structure change will not require kernel changes any more. bpf program itself is used to adapt to new data structure changes. This will give certain flexibility with guaranteed correctness.

Here, kernel seq_ops is used to facilitate dumping, similar to current /proc and many other lossless kernel dumping facilities.

Quoted 9 lines

User Interfaces:
1. A new mount file system, bpfdump at /sys/kernel/bpfdump is introduced. Different from /sys/fs/bpf, this is a single user mount. Mount command can be:
mount -t bpfdump bpfdump /sys/kernel/bpfdump
2. Kernel bpf dumpable data structures are represented as directories under /sys/kernel/bpfdump, e.g., /sys/kernel/bpfdump/ipv6_route/ /sys/kernel/bpfdump/netlink/

Quoted 4 lines

The names of bpfdump fs entries do not match actual data structure names
- e.g., there is no ipv6_route struct. On the one hand that is a good thing since structure names can change, but that also means a mapping is needed between the dumper filesystem entries and what you get for context.

Yes, the later bpftool patch implements a new command to dump such information.

  $ bpftool dumper show target
  target                  prog_ctx_type
  task                    bpfdump__task
  task/file               bpfdump__task_file
  bpf_map                 bpfdump__bpf_map
  ipv6_route              bpfdump__ipv6_route
  netlink                 bpfdump__netlink

in vmlinux.h generated by vmlinux BTF, we have

struct bpf_dump_meta {
        struct seq_file *seq;
        u64 session_id;
        u64 seq_num;
};
struct bpfdump__ipv6_route {
        struct bpf_dump_meta *meta;
        struct fib6_info *rt;
};

Here, bpfdump__ipv6_route is the bpf program context type. User can based on this to write the bpf program.

Quoted 7 lines

Further, what is the expectation in terms of stable API for these fs entries? Entries in the context can change. Data structure names can change. Entries in the structs can change. All of that breaks the idea of stable programs that are compiled once and run for all future releases. When structs change, those programs will break - and structures will change.

Yes, the API (ctx) we presented to bpf program is indeed unstable.
CO-RE should help to certain extend but if some fields are gone, e.g.,
bpf program will need to be rewritten for that particular kernel version, or kernel bpfdump infrastructure can be enhanced to change its ctx structure to have more information to the program
for that kernel version. In summary, I agree with you that this is
an unstable API similar to other tracing program
since it accesses kernel internal data structures.

Quoted 4 lines

What does bpfdumper provide that you can not do with a tracepoint on a relevant function and then putting a program on the tracepoint? ie., why not just put a tracepoint in the relevant dump functions.

In my very beginning to explore bpfdump, kprobe to "show" function is one of options. But quickly we realized that we actually do not want to just piggyback on "show" function, but want to replace it with bpf. This will be useful in following different use cases:
1. first catable dumper file, similar to /proc/net/ipv6_route, we want /sys/kernel/bpfdump/ipv6_route/my_dumper and you can cat to get it.

Using kprobe when you are doing cat /proc/net/ipv6_route is complicated. You probably need an application which runs through cat /proc/net/ipv6_route and discard its output, and at the same time gets the result from bpf program (filtered by pid since somebody may run
cat /proc/net/ipv6_route at the same time. You may use perf ring_buffer to send the result back to the application.

note that perf ring buffer may lose records for whatever reason and seq_ops are implemented not to lose records by built-in retries.

Using kprobe approach above is complicated and for each dumper you need an application. We would like it to be just catable with minimum user overhead to create such a dumper.

2. second, anonymous dumper, kprobe/tracepoint will incur original overhead of seq_printf per object. but user may be only interested in a very small portion of information. In such cases, bpf program directly doing filtering in the kernel can potentially speed up a lot if there are a lot of records to traverse.

3. for data structures which do not have catable dumpers for example task, hopefully, as demonstrated in this patch set, kernel implementation and writing a bpf program are not too hard. This especially enables people to do in-kernel filtering which is the strength of the bpf.

#22 lore raw
Quoted 18 lines · Yonghong Song
Quoted 15 lines · David Ahern
Quoted 13 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you
need change the kernel. For example, Martin wants to dump socket local

Quoted 9 lines
Quoted 3 lines

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

Yes, as mentioned two paragraphs below. kernel change is required. The tradeoff is that this is a one-time investment. Once kernel change is in place, printing new fields (in most cases except new fields which need additional locks etc.) no need for kernel change any more.

One thing I struggled with initially when reading the cover letter was understanding how BPF dumper programs get run. Patch 7 deals with that I think and the answer seems to be to create additional seq file infrastructure to the exisiting one which executes the BPF dumper programs where appropriate. Have I got this right? I guess more lightweight methods such as instrumenting functions associated with an existing /proc dumper are a bit too messy?

Thanks!

Alan

Quoted 125 lines
Quoted 32 lines
Quoted 20 lines

storage with "ss". Kernel change is needed for it to work ([1]). This is also the direct motivation for this work.

drgn ([2]) solves this proble nicely and no kernel change is not needed. But since drgn is not able to verify the validity of a particular pointer value,
it might present the wrong results in rare cases.

In this patch set, we introduce bpf based dumping. Initial kernel changes are
still needed, but a data structure change will not require kernel changes
any more. bpf program itself is used to adapt to new data structure changes. This will give certain flexibility with guaranteed correctness.

Here, kernel seq_ops is used to facilitate dumping, similar to current /proc and many other lossless kernel dumping facilities.

Quoted 11 lines

User Interfaces:
1. A new mount file system, bpfdump at /sys/kernel/bpfdump is introduced.
Different from /sys/fs/bpf, this is a single user mount. Mount command
can be:
mount -t bpfdump bpfdump /sys/kernel/bpfdump
2. Kernel bpf dumpable data structures are represented as directories under /sys/kernel/bpfdump, e.g., /sys/kernel/bpfdump/ipv6_route/ /sys/kernel/bpfdump/netlink/

Quoted 4 lines

The names of bpfdump fs entries do not match actual data structure names
- e.g., there is no ipv6_route struct. On the one hand that is a good thing since structure names can change, but that also means a mapping is needed between the dumper filesystem entries and what you get for context.

Yes, the later bpftool patch implements a new command to dump such information.

  $ bpftool dumper show target
  target                  prog_ctx_type
  task                    bpfdump__task
  task/file               bpfdump__task_file
  bpf_map                 bpfdump__bpf_map
  ipv6_route              bpfdump__ipv6_route
  netlink                 bpfdump__netlink

in vmlinux.h generated by vmlinux BTF, we have

struct bpf_dump_meta {
        struct seq_file *seq;
        u64 session_id;
        u64 seq_num;
};
struct bpfdump__ipv6_route {
        struct bpf_dump_meta *meta;
        struct fib6_info *rt;
};

Here, bpfdump__ipv6_route is the bpf program context type. User can based on this to write the bpf program.

Quoted 7 lines

Further, what is the expectation in terms of stable API for these fs entries? Entries in the context can change. Data structure names can change. Entries in the structs can change. All of that breaks the idea of stable programs that are compiled once and run for all future releases. When structs change, those programs will break - and structures will change.

Yes, the API (ctx) we presented to bpf program is indeed unstable. CO-RE should help to certain extend but if some fields are gone, e.g., bpf program will need to be rewritten for that particular kernel version, or kernel bpfdump infrastructure can be enhanced to
change its ctx structure to have more information to the program for that kernel version. In summary, I agree with you that this is an unstable API similar to other tracing program
since it accesses kernel internal data structures.

Quoted 4 lines

What does bpfdumper provide that you can not do with a tracepoint on a relevant function and then putting a program on the tracepoint? ie., why not just put a tracepoint in the relevant dump functions.

In my very beginning to explore bpfdump, kprobe to "show" function is one of options. But quickly we realized that we actually do not want to just piggyback on "show" function, but want to replace it with bpf. This will be useful in following different use cases:
1. first catable dumper file, similar to /proc/net/ipv6_route, we want /sys/kernel/bpfdump/ipv6_route/my_dumper and you can cat to get it.

Using kprobe when you are doing cat /proc/net/ipv6_route is complicated. You probably need an application which runs through cat /proc/net/ipv6_route and discard its output, and at the same time gets the result from bpf program (filtered by pid since somebody may run
cat /proc/net/ipv6_route at the same time. You may use perf ring_buffer to send the result back to the application.

note that perf ring buffer may lose records for whatever reason and seq_ops are implemented not to lose records by built-in retries.

Using kprobe approach above is complicated and for each dumper you need an application. We would like it to be just catable with minimum user overhead to create such a dumper.

2. second, anonymous dumper, kprobe/tracepoint will incur original overhead of seq_printf per object. but user may be only interested in a very small portion of information. In such cases, bpf program directly doing filtering in the kernel can potentially speed up a lot if there are a lot of records to traverse.

3. for data structures which do not have catable dumpers for example task, hopefully, as demonstrated in this patch set, kernel implementation and writing a bpf program are not too hard. This especially enables people to do in-kernel filtering which is the strength of the bpf.

#23 lore raw
Quoted 30 lines · Alan Maguire
Quoted 18 lines · Yonghong Song
Quoted 15 lines · David Ahern
Quoted 13 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you
need change the kernel. For example, Martin wants to dump socket local

Quoted 9 lines
Quoted 3 lines

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

Yes, as mentioned two paragraphs below. kernel change is required. The tradeoff is that this is a one-time investment. Once kernel change is in place, printing new fields (in most cases except new fields which need additional locks etc.) no need for kernel change any more.

Quoted 6 lines

One thing I struggled with initially when reading the cover letter was understanding how BPF dumper programs get run. Patch 7 deals with that I think and the answer seems to be to create additional seq file infrastructure to the exisiting one which executes the BPF dumper programs where appropriate. Have I got this right? I guess more lightweight methods

Yes. The reason is that some data structures like bpf_map, task, or task/file do not have existing seq_ops infrastructure so I created new ones to iterate them.

Quoted 2 lines

such as instrumenting functions associated with an existing /proc dumper are a bit too messy?

We did use existing seq_ops from /proc/net/ipv6_route and /proc/net/netlink as an example. In the future, we will do /proc/net/tcp[6] and /proc/net/udp[6] which will reuse existing seq_ops with slight modifications.

#24 lore raw
In reply to David Ahern
Quoted 14 lines · David Ahern
Quoted 5 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Quoted 7 lines

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you need change the kernel. For example, Martin wants to dump socket local

Quoted 4 lines

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

I need to dig into this patchset a bit more, but if there is a need for in-kernel BTF-based structure dumping I've got a work-in-progress patchset that does this by generalizing the code that deals with seq output in the verifier. I've posted it as an RFC in case it has anything useful to offer here:

https://lore.kernel.org/bpf/[email protected]/T/#t

The idea is that by using different callback function we can achieve seq, snprintf or other output in-kernel using the kernel BTF data. I created one consumer as a proof-of-concept; it's a printk pointer format specifier. Since the dump format is determined in kernel it's a bit constrained format-wise, but may be good enough for some cases.

To give a flavour for what the printed-out data looks like, here we use pr_info() to display a struct sk_buff *. Note we specify the 'N' modifier to show type field names:

  struct sk_buff *skb = alloc_skb(64, GFP_KERNEL);

  pr_info("%pTN<struct sk_buff>", skb);

...gives us:

{{{.next=00000000c7916e9c,.prev=00000000c7916e9c,{.dev=00000000c7916e9c|.dev_scratch=0}}|.rbnode={.__rb_parent_color=0,.rb_right=00000000c7916e9c,.rb_left=00000000c7916e9c}|.list={.next=00000000c7916e9c,.prev=00000000c7916e9c}},{.sk=00000000c7916e9c|.ip_defrag_offset=0},{.tstamp=0|.skb_mstamp_ns=0},.cb=['\0'],{{._skb_refdst=0,.destructor=00000000c7916e9c}|.tcp_tsorted_anchor={.next=00000000c7916e9c,.prev=00000000c7916e9c}},._nfct=0,.len=0,.data_len=0,.mac_len=0,.hdr_len=0,.queue_mapping=0,.__cloned_offset=[],.cloned=0x0,.nohdr=0x0,.fclone=0x0,.peeked=0x0,.head_frag=0x0,.pfmemalloc=0x0,.active_extensions=0,.headers_start=[],.__pkt_type_offset=[],.pkt_type=0x0,.ignore_df=0x0,.nf_trace=0x0,.ip_summed=0x0,.ooo_okay=0x0,.l4_hash=0x0,.sw_hash=0x0,.wifi_acked_valid=0x0,.wifi_acked=0x0,.no_fcs=0x0,.encapsulation=0x0,.encap_hdr_csum=0x0,.csum_valid=0x0,.__pkt_vlan_present_offset=[],.vlan_present=0x0,.csum_complete_sw=0x0,.csum_level=0x0,.csum_not_inet=0x0,.dst_pending_co

[printk output is truncated at 1024 bytes, but more compact output can be achieved by not specifying 'N' for type names. I may need to add a specifier to avoid pointer obfuscation]

With a printk format specifier, trace_printk() in BPF then inherits this dumping behaviour for free, but I think it would also be possible to add a helper so that the type name didn't have to be specified. The verifier could insert BTF ids and type data could be dumped for tracing arguments via a flavour of bpf_perf_event_output() helper or similar. To be clear I haven't done any of that yet in the RFC patchset, but it seems feasible at least.

Anyway perhaps there's something useful in it which can help towards the goal of easier dumping of data structures.

I'll spend some time over the weekend looking at the BTF dumper patchset; apologies I haven't got very far with it yet.

Thanks!

Alan

Quoted 42 lines
Quoted 16 lines

storage with "ss". Kernel change is needed for it to work ([1]). This is also the direct motivation for this work.

drgn ([2]) solves this proble nicely and no kernel change is not needed. But since drgn is not able to verify the validity of a particular pointer value, it might present the wrong results in rare cases.

In this patch set, we introduce bpf based dumping. Initial kernel changes are still needed, but a data structure change will not require kernel changes any more. bpf program itself is used to adapt to new data structure changes. This will give certain flexibility with guaranteed correctness.

Here, kernel seq_ops is used to facilitate dumping, similar to current /proc and many other lossless kernel dumping facilities.

Quoted 9 lines

User Interfaces:
1. A new mount file system, bpfdump at /sys/kernel/bpfdump is introduced. Different from /sys/fs/bpf, this is a single user mount. Mount command can be:
mount -t bpfdump bpfdump /sys/kernel/bpfdump
2. Kernel bpf dumpable data structures are represented as directories under /sys/kernel/bpfdump, e.g., /sys/kernel/bpfdump/ipv6_route/ /sys/kernel/bpfdump/netlink/

The names of bpfdump fs entries do not match actual data structure names
- e.g., there is no ipv6_route struct. On the one hand that is a good thing since structure names can change, but that also means a mapping is needed between the dumper filesystem entries and what you get for context.

Further, what is the expectation in terms of stable API for these fs entries? Entries in the context can change. Data structure names can change. Entries in the structs can change. All of that breaks the idea of stable programs that are compiled once and run for all future releases. When structs change, those programs will break - and structures will change.

What does bpfdumper provide that you can not do with a tracepoint on a relevant function and then putting a program on the tracepoint? ie., why not just put a tracepoint in the relevant dump functions.

#25 lore raw
Quoted 27 lines · Alan Maguire
Quoted 14 lines · David Ahern
Quoted 5 lines · Yonghong Song

As there are some discussions regarding to the kernel interface/steps to create file/anonymous dumpers, I think it will be beneficial for discussion with this work in progress.

Quoted 7 lines

Motivation:
The current way to dump kernel data structures mostly:
1. /proc system
2. various specific tools like "ss" which requires kernel support.
3. drgn
The dropback for the first two is that whenever you want to dump more, you need change the kernel. For example, Martin wants to dump socket local

Quoted 4 lines

If kernel support is needed for bpfdump of kernel data structures, you are not really solving the kernel support problem. i.e., to dump ipv4_route's you need to modify the relevant proc show function.

I need to dig into this patchset a bit more, but if there is a need for in-kernel BTF-based structure dumping I've got a work-in-progress patchset that does this by generalizing the code that deals with seq output in the verifier. I've posted it as an RFC in case it has anything useful to offer here:

Quoted 8 lines

https://lore.kernel.org/bpf/[email protected]/T/#t

The idea is that by using different callback function we can achieve seq, snprintf or other output in-kernel using the kernel BTF data. I created one consumer as a proof-of-concept; it's a printk pointer format specifier. Since the dump format is determined in kernel it's a bit constrained format-wise, but may be good enough for some cases.

The bpfdump work and here in-kernel btf dumping are different. The bpfdump BPF programs are triggered with a user syscall, e.g.,
cat /sys/kernel/bpfdump/task/my_dumper (also calling open())
or when user open() an anonymous dumper.

The BPF program can "print" some data through bpf_seq_printf() helper. These printed data will be received by user space through read() syscall.

Your work is greater as it makes kernel print more readable. There is no overlap between your work and bpfdump.