Skip to contentSkip to conversation

Extend cgroup interface with bpf

PATCH In review Hao Luo 6 participants 54 messages Feb 25, 2022 – Mar 15, 2022

Subsystems (10)
Kselftest BPF Tracing BPF selftests +6 more subsystems
lore
Share & tools
lore

Hao Luo

author
#0 lore raw

cover Extend cgroup interface with bpf

This patchset provides a bpf solution for monitoring cgroup activities and exporting cgroup states in an organized way in bpffs. It introduces the following features:

1. sleepable tracepoints and sleepable tracing programs.
2. a set of bpf helpers for creating and deleting files and directories in bpffs.
3. a new iter prog, parameterizable by cgroup ids, to print cgroup state.

Sleepable tracepoints and tracing progs allow us to run bpf progs when a new cgroup is created or an existing cgroup is removed. The set of filesystem helpers allows sleepable tracing progs to set up directories in bpffs for each cgroup. The progs can also pin and unlink bpf objects from these bpffs directories. The new iter prog can be used to export cgroup states. Using this set of additions, we are creating an extension to the current cgroup interface to export per-cgroup stats.

See the selftest added in patch 09/09, test_cgroup_stats, as a full example on how it can be done. The test develops a custom metric measuring per-cgroup scheduling latencies and exports it via cgroup iters, which are pinned by sleepable tracing progs attaching at cgroup tracepoints.

Not only for per-cgroup stats, the same approach can be used for other states such as task_vma iter and per-bpf-prog state. As an example, we can write sleepable tracing progs to monitor task fork and exit, and let the tracing prog to set up directories, parameterize task_vma iter and pin the iters.

Hao Luo (9):
bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall
bpf: Add BPF_OBJ_PIN and BPF_OBJ_GET in the bpf_sys_bpf helper selftests/bpf: tests mkdir, rmdir, unlink and pin in syscall
bpf: Introduce sleepable tracepoints
cgroup: Sleepable cgroup tracepoints.
libbpf: Add sleepable tp_btf
bpf: Lift permission check in __sys_bpf when called from kernel.
bpf: Introduce cgroup iter
selftests/bpf: Tests using sleepable tracepoints to monitor cgroup
events

 include/linux/bpf.h                           |  16 +-
 include/linux/tracepoint-defs.h               |   1 +
 include/trace/bpf_probe.h                     |  22 +-
 include/trace/events/cgroup.h                 |  45 ++++
 include/uapi/linux/bpf.h                      |  32 +++
 kernel/bpf/Makefile                           |   2 +-
 kernel/bpf/cgroup_iter.c                      | 141 +++++++++++
 kernel/bpf/inode.c                            |  33 ++-
 kernel/bpf/syscall.c                          | 237 ++++++++++++++++--
 kernel/cgroup/cgroup.c                        |   5 +
 kernel/trace/bpf_trace.c                      |   5 +
 tools/include/uapi/linux/bpf.h                |  32 +++
 tools/lib/bpf/libbpf.c                        |   1 +
 .../selftests/bpf/prog_tests/syscall.c        |  67 ++++-
 .../bpf/prog_tests/test_cgroup_stats.c        | 187 ++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_iter.h  |   7 +
 .../selftests/bpf/progs/cgroup_monitor.c      |  78 ++++++
 .../selftests/bpf/progs/cgroup_sched_lat.c    | 232 +++++++++++++++++
 .../testing/selftests/bpf/progs/syscall_fs.c  |  69 +++++
 19 files changed, 1175 insertions(+), 37 deletions(-)
 create mode 100644 kernel/bpf/cgroup_iter.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_cgroup_stats.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_monitor.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_sched_lat.c
 create mode 100644 tools/testing/selftests/bpf/progs/syscall_fs.c
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
author
#1 lore raw

PATCH 1/9 bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
 include/linux/bpf.h            |   1 +
 include/uapi/linux/bpf.h       |  26 +++++
 kernel/bpf/inode.c             |   9 +-
 kernel/bpf/syscall.c           | 177 +++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  26 +++++
 5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
index f19abc59b6cd..fce5e26179f5 100644
@@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link);
 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
+bool bpf_path_is_bpf_dir(const struct path *path);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
include/uapi/linux/bpf.h +26 -0
index afe3d0d7f5f2..a5dbc794403d 100644
@@ -5086,6 +5086,29 @@ union bpf_attr {
  *	Return
  *		0 on success, or a negative error in case of failure. On error
  *		*dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ *	Description
+ *		Attempts to create a directory name *pathname*. The argument
+ *		*pathname_sz* specifies the length of the string *pathname*.
+ *		The argument *mode* specifies the mode for the new directory. It
+ *		is modified by the process's umask. It has the same semantic as
+ *		the syscall mkdir(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a directory, which must be empty.
+ *	Return
+ *		0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a name and possibly the file it refers to. It has the
+ *		same semantic as the syscall unlink(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5280,6 +5303,9 @@ union bpf_attr {
 	FN(xdp_load_bytes),		\
 	FN(xdp_store_bytes),		\
 	FN(copy_from_user_task),	\
+	FN(mkdir),			\
+	FN(rmdir),			\
+	FN(unlink),			\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/inode.c +6 -3
index 4f841e16779e..3aca00e9e950 100644
@@ -414,6 +414,11 @@ static const struct inode_operations bpf_dir_iops = {
 	.unlink		= simple_unlink,
 };
 
+bool bpf_path_is_bpf_dir(const struct path *path)
+{
+	return d_inode(path->dentry)->i_op == &bpf_dir_iops;
+}
+
 /* pin iterator link into bpffs */
 static int bpf_iter_link_pin_kernel(struct dentry *parent,
 				    const char *name, struct bpf_link *link)
@@ -439,7 +444,6 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 			  enum bpf_type type)
 {
 	struct dentry *dentry;
-	struct inode *dir;
 	struct path path;
 	umode_t mode;
 	int ret;
@@ -454,8 +458,7 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	if (ret)
 		goto out;
 
-	dir = d_inode(path.dentry);
-	if (dir->i_op != &bpf_dir_iops) {
+	if (!bpf_path_is_bpf_dir(&path)) {
 		ret = -EPERM;
 		goto out;
 	}
kernel/bpf/syscall.c +177 -0

195 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +26 -0
index afe3d0d7f5f2..a5dbc794403d 100644
@@ -5086,6 +5086,29 @@ union bpf_attr {
  *	Return
  *		0 on success, or a negative error in case of failure. On error
  *		*dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ *	Description
+ *		Attempts to create a directory name *pathname*. The argument
+ *		*pathname_sz* specifies the length of the string *pathname*.
+ *		The argument *mode* specifies the mode for the new directory. It
+ *		is modified by the process's umask. It has the same semantic as
+ *		the syscall mkdir(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a directory, which must be empty.
+ *	Return
+ *		0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a name and possibly the file it refers to. It has the
+ *		same semantic as the syscall unlink(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5280,6 +5303,9 @@ union bpf_attr {
 	FN(xdp_load_bytes),		\
 	FN(xdp_store_bytes),		\
 	FN(copy_from_user_task),	\
+	FN(mkdir),			\
+	FN(rmdir),			\
+	FN(unlink),			\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#2 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 72 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
 include/linux/bpf.h            |   1 +
 include/uapi/linux/bpf.h       |  26 +++++
 kernel/bpf/inode.c             |   9 +-
 kernel/bpf/syscall.c           | 177 +++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  26 +++++
 5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
index f19abc59b6cd..fce5e26179f5 100644
@@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link);
 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
+bool bpf_path_is_bpf_dir(const struct path *path);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
include/uapi/linux/bpf.h +23 -0
index afe3d0d7f5f2..a5dbc794403d 100644
@@ -5086,6 +5086,29 @@ union bpf_attr {
  *	Return
  *		0 on success, or a negative error in case of failure. On error
  *		*dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ *	Description
+ *		Attempts to create a directory name *pathname*. The argument
+ *		*pathname_sz* specifies the length of the string *pathname*.
+ *		The argument *mode* specifies the mode for the new directory. It
+ *		is modified by the process's umask. It has the same semantic as
+ *		the syscall mkdir(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a directory, which must be empty.
+ *	Return
+ *		0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a name and possibly the file it refers to. It has the
+ *		same semantic as the syscall unlink(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\

include/uapi/linux/bpf.h union bpf_attr { · line 5310

Quoted 8 lines
include/uapi/linux/bpf.h +3 -0
@@ -5280,6 +5303,9 @@ union bpf_attr {
 	FN(xdp_load_bytes),		\
 	FN(xdp_store_bytes),		\
 	FN(copy_from_user_task),	\
+	FN(mkdir),			\
+	FN(rmdir),			\
+	FN(unlink),			\
 	/* */
 

How about only introducing bpf_sys_mkdirat and bpf_sys_unlinkat? That would be more useful for other cases in future, and when AT_FDCWD is passed, has the same functionality as these, but when openat/fget is supported, it would work relative to other dirfds as well. It can also allow using dirfd of the process calling read for a iterator (e.g. if it sets the fd number using skel->bss). unlinkat's AT_REMOVEDIR flag also removes the need for a bpf_rmdir.

WDYT?

Quoted 1 line

[...]

--
Kartikeya

author
#3 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Hi Kumar,

Quoted 46 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ * Description

+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
Quoted 17 lines

+ * Return
+ * 0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a directory, which must be empty.
+ * Return
+ * 0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a name and possibly the file it refers to. It has the
+ * same semantic as the syscall unlink(2).
+ * Return
+ * 0 on success, or a negative error in case of failure.
*/

Quoted 9 lines

How about only introducing bpf_sys_mkdirat and bpf_sys_unlinkat? That would be more useful for other cases in future, and when AT_FDCWD is passed, has the same functionality as these, but when openat/fget is supported, it would work relative to other dirfds as well. It can also allow using dirfd of the process calling read for a iterator (e.g. if it sets the fd number using skel->bss). unlinkat's AT_REMOVEDIR flag also removes the need for a bpf_rmdir.

WDYT?

The idea sounds good to me, more flexible. But I don't have a real use case for using the added 'dirfd' at this moment. For all the use cases I can think of, absolute paths will suffice, I think. Unless other reviewers have opposition, I will try switching to mkdirat and unlinkat in v2.

#4 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 57 lines · Hao Luo

Hi Kumar,

Quoted 53 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ * Description

+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
Quoted 17 lines

+ * Return
+ * 0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a directory, which must be empty.
+ * Return
+ * 0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a name and possibly the file it refers to. It has the
+ * same semantic as the syscall unlink(2).
+ * Return
+ * 0 on success, or a negative error in case of failure.
*/

How about only introducing bpf_sys_mkdirat and bpf_sys_unlinkat? That would be more useful for other cases in future, and when AT_FDCWD is passed, has the same functionality as these, but when openat/fget is supported, it would work relative to other dirfds as well. It can also allow using dirfd of the process calling read for a iterator (e.g. if it sets the fd number using skel->bss). unlinkat's AT_REMOVEDIR flag also removes the need for a bpf_rmdir.

Quoted 8 lines
Quoted 2 lines

WDYT?

The idea sounds good to me, more flexible. But I don't have a real use case for using the added 'dirfd' at this moment. For all the use cases I can think of, absolute paths will suffice, I think. Unless other reviewers have opposition, I will try switching to mkdirat and unlinkat in v2.

I'm surprised you don't need "at" variants.
I thought your production setup has a top level cgroup controller and then inner tasks inside containers manage cgroups on their own.
Since containers are involved they likely run inside their own mountns. cgroupfs mount is single. So you probably don't even need to bind mount it inside containers, but bpffs is not a single mount. You need
to bind mount top bpffs inside containers for tasks to access it.
Now for cgroupfs the abs path is not an issue, but for bpffs
the AT_FDCWD becomes a problem. AT_FDCWD is using current mount ns. Inside container that will be different. Unless you bind mount into exact same path the full path has different meanings inside and outside of the container. It seems to me the bpf progs attached to cgroup sleepable events should be using FD of bpffs. Then when these tracepoints are triggered from different containers in different mountns they will get the right dir prefix. What am I missing?

I think non-AT variants are not needed. The prog can always pass AT_FDCWD if it's really the intent, but passing actual FD seems more error-proof.

author
#5 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 68 lines · Alexei Starovoitov
Quoted 57 lines · Hao Luo

Hi Kumar,

Quoted 53 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ * Description

+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
Quoted 17 lines

+ * Return
+ * 0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a directory, which must be empty.
+ * Return
+ * 0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a name and possibly the file it refers to. It has the
+ * same semantic as the syscall unlink(2).
+ * Return
+ * 0 on success, or a negative error in case of failure.
*/

How about only introducing bpf_sys_mkdirat and bpf_sys_unlinkat? That would be more useful for other cases in future, and when AT_FDCWD is passed, has the same functionality as these, but when openat/fget is supported, it would work relative to other dirfds as well. It can also allow using dirfd of the process calling read for a iterator (e.g. if it sets the fd number using skel->bss). unlinkat's AT_REMOVEDIR flag also removes the need for a bpf_rmdir.

Quoted 8 lines
Quoted 2 lines

WDYT?

The idea sounds good to me, more flexible. But I don't have a real use case for using the added 'dirfd' at this moment. For all the use cases I can think of, absolute paths will suffice, I think. Unless other reviewers have opposition, I will try switching to mkdirat and unlinkat in v2.

Quoted 16 lines

I'm surprised you don't need "at" variants.
I thought your production setup has a top level cgroup controller and then inner tasks inside containers manage cgroups on their own.
Since containers are involved they likely run inside their own mountns. cgroupfs mount is single. So you probably don't even need to bind mount it inside containers, but bpffs is not a single mount. You need
to bind mount top bpffs inside containers for tasks to access it.
Now for cgroupfs the abs path is not an issue, but for bpffs
the AT_FDCWD becomes a problem. AT_FDCWD is using current mount ns. Inside container that will be different. Unless you bind mount into exact same path the full path has different meanings inside and outside of the container. It seems to me the bpf progs attached to cgroup sleepable events should be using FD of bpffs. Then when these tracepoints are triggered from different containers in different mountns they will get the right dir prefix. What am I missing?

Alexei, you are perfectly right. To be honest, I failed to see the fact that the sleepable tp prog is in the container's mount ns. I think we can bind mount the top bpffs into exactly the same path inside container, right? But I haven't tested this idea. Passing FDs should be better.

Quoted 2 lines

I think non-AT variants are not needed. The prog can always pass AT_FDCWD if it's really the intent, but passing actual FD seems more error-proof.

Let's have the AT version. Passing FD seems the right approach, when we use it in the context of container.

author
#6 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 88 lines · Hao Luo
Quoted 68 lines · Alexei Starovoitov
Quoted 57 lines · Hao Luo

Hi Kumar,

Quoted 53 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)
+ * Description

+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
Quoted 17 lines

+ * Return
+ * 0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a directory, which must be empty.
+ * Return
+ * 0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ * Description
+ * Deletes a name and possibly the file it refers to. It has the
+ * same semantic as the syscall unlink(2).
+ * Return
+ * 0 on success, or a negative error in case of failure.
*/

How about only introducing bpf_sys_mkdirat and bpf_sys_unlinkat? That would be more useful for other cases in future, and when AT_FDCWD is passed, has the same functionality as these, but when openat/fget is supported, it would work relative to other dirfds as well. It can also allow using dirfd of the process calling read for a iterator (e.g. if it sets the fd number using skel->bss). unlinkat's AT_REMOVEDIR flag also removes the need for a bpf_rmdir.

Quoted 8 lines
Quoted 2 lines

WDYT?

The idea sounds good to me, more flexible. But I don't have a real use case for using the added 'dirfd' at this moment. For all the use cases I can think of, absolute paths will suffice, I think. Unless other reviewers have opposition, I will try switching to mkdirat and unlinkat in v2.

Quoted 16 lines

I'm surprised you don't need "at" variants.
I thought your production setup has a top level cgroup controller and then inner tasks inside containers manage cgroups on their own.
Since containers are involved they likely run inside their own mountns. cgroupfs mount is single. So you probably don't even need to bind mount it inside containers, but bpffs is not a single mount. You need
to bind mount top bpffs inside containers for tasks to access it.
Now for cgroupfs the abs path is not an issue, but for bpffs
the AT_FDCWD becomes a problem. AT_FDCWD is using current mount ns. Inside container that will be different. Unless you bind mount into exact same path the full path has different meanings inside and outside of the container. It seems to me the bpf progs attached to cgroup sleepable events should be using FD of bpffs. Then when these tracepoints are triggered from different containers in different mountns they will get the right dir prefix. What am I missing?

Quoted 6 lines

Alexei, you are perfectly right. To be honest, I failed to see the fact that the sleepable tp prog is in the container's mount ns. I think we can bind mount the top bpffs into exactly the same path inside container, right? But I haven't tested this idea. Passing FDs should be better.

I gave this question more thought. We don't need to bind mount the top bpffs into the container, instead, we may be able to overlay a bpffs directory into the container. Here is the workflow in my mind:

For each job, let's say A, the container runtime can create a directory in bpffs, for example

/sys/fs/bpf/jobs/A

and then create the cgroup for A. The sleepable tracing prog will create the file:

/sys/fs/bpf/jobs/A/100/stats

100 is the created cgroup's id. Then the container runtime overlays the bpffs directory into container A in the same path:

[A's container path]/sys/fs/bpf/jobs/A.

A can see the stats at the path within its mount ns:

/sys/fs/bpf/jobs/A/100/stats

When A creates cgroup, it is able to write to the top layer of the overlayed directory. So it is

/sys/fs/bpf/jobs/A/101/stats

Some of my thoughts:
1. Compared to bind mount top bpffs into container, overlaying a
directory avoids exposing other jobs' stats. This gives better isolation. I already have a patch for supporting laying bpffs over other fs, it's not too hard.
2. Once the container runtime has overlayed directory into the
container, it has no need to create more cgroups for this job. It doesn't need to track the stats of job-created cgroups, which are mainly for inspection by the job itself. Even if it needs to collect the stats from those cgroups, it can read from the path in the container.
3. The overlay path in container doesn't have to be exactly the same
as the path in root mount ns. In the sleepable tracing prog, we may select paths based on current process's ns. If we choose to do this, we can further avoid exposing cgroup id and job name to the container.

Quoted 5 lines
Quoted 2 lines

I think non-AT variants are not needed. The prog can always pass AT_FDCWD if it's really the intent, but passing actual FD seems more error-proof.

Let's have the AT version. Passing FD seems the right approach, when we use it in the context of container.

#7 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 4 lines · Hao Luo

I gave this question more thought. We don't need to bind mount the top bpffs into the container, instead, we may be able to overlay a bpffs directory into the container. Here is the workflow in my mind:

I don't quite follow what you mean by 'overlay' here. Another bpffs mount or future overlayfs that supports bpffs?

Quoted 5 lines

For each job, let's say A, the container runtime can create a directory in bpffs, for example

/sys/fs/bpf/jobs/A

Quoted 7 lines

and then create the cgroup for A. The sleepable tracing prog will create the file:

/sys/fs/bpf/jobs/A/100/stats

100 is the created cgroup's id. Then the container runtime overlays the bpffs directory into container A in the same path:

Why cgroup id ? Wouldn't it be easier to use the same cgroup name as in cgroupfs ?

Quoted 9 lines

[A's container path]/sys/fs/bpf/jobs/A.

A can see the stats at the path within its mount ns:

/sys/fs/bpf/jobs/A/100/stats

When A creates cgroup, it is able to write to the top layer of the overlayed directory. So it is

Quoted 7 lines

/sys/fs/bpf/jobs/A/101/stats

Some of my thoughts:
1. Compared to bind mount top bpffs into container, overlaying a
directory avoids exposing other jobs' stats. This gives better isolation. I already have a patch for supporting laying bpffs over other fs, it's not too hard.

So it's overlayfs combination of bpffs and something like ext4, right? I thought you found out that overlaryfs has to be upper fs and lower fs shouldn't be modified underneath.
So if bpffs is a lower fs the writes into it should go through the upper overlayfs, right?

Quoted 10 lines

2. Once the container runtime has overlayed directory into the
container, it has no need to create more cgroups for this job. It doesn't need to track the stats of job-created cgroups, which are mainly for inspection by the job itself. Even if it needs to collect the stats from those cgroups, it can read from the path in the container.
3. The overlay path in container doesn't have to be exactly the same
as the path in root mount ns. In the sleepable tracing prog, we may select paths based on current process's ns. If we choose to do this, we can further avoid exposing cgroup id and job name to the container.

The benefits make sense.

author
#8 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 18 lines · Alexei Starovoitov
Quoted 4 lines · Hao Luo

I gave this question more thought. We don't need to bind mount the top bpffs into the container, instead, we may be able to overlay a bpffs directory into the container. Here is the workflow in my mind:

I don't quite follow what you mean by 'overlay' here. Another bpffs mount or future overlayfs that supports bpffs?

Quoted 8 lines

For each job, let's say A, the container runtime can create a directory in bpffs, for example

/sys/fs/bpf/jobs/A

and then create the cgroup for A. The sleepable tracing prog will create the file:

Quoted 8 lines
Quoted 4 lines

/sys/fs/bpf/jobs/A/100/stats

100 is the created cgroup's id. Then the container runtime overlays the bpffs directory into container A in the same path:

Why cgroup id ? Wouldn't it be easier to use the same cgroup name as in cgroupfs ?

Cgroup name isn't unique. We don't need the hierarchy information of cgroups. We can use a library function to translate cgroup path to cgroup id. See the get_cgroup_id() in patch 9/9. It works fine in the selftest.

Quoted 17 lines
Quoted 9 lines

[A's container path]/sys/fs/bpf/jobs/A.

A can see the stats at the path within its mount ns:

/sys/fs/bpf/jobs/A/100/stats

When A creates cgroup, it is able to write to the top layer of the overlayed directory. So it is

Quoted 7 lines

/sys/fs/bpf/jobs/A/101/stats

Some of my thoughts:
1. Compared to bind mount top bpffs into container, overlaying a
directory avoids exposing other jobs' stats. This gives better isolation. I already have a patch for supporting laying bpffs over other fs, it's not too hard.

Quoted 6 lines

So it's overlayfs combination of bpffs and something like ext4, right? I thought you found out that overlaryfs has to be upper fs and lower fs shouldn't be modified underneath.
So if bpffs is a lower fs the writes into it should go through the upper overlayfs, right?

It's overlayfs combining bpffs and ext4. Bpffs is the upper layer. The lower layer is an empty ext4 directory. The merged directory is a directory in the container.
The upper layer contains bpf objects that we want to expose to the container, for example, the sleepable tracing progs and the iter link for reading stats. Only the merged directory is visible to the container and all the updates go through the merged directory.

The following is the example of workflow I'm thinking:

Step 1: We first set up directories and bpf objects needed by containers.

[# ~] ls /sys/fs/bpf/container/upper
tracing_prog iter_link
[# ~] ls /sys/fs/bpf/container/work
[# ~] ls /container
root lower
[# ~] ls /container/root
bpf
[# ~] ls /container/root/bpf

Step 2: Use overlayfs to mount a directory from bpffs into the container's home.

[# ~] mkdir /container/lower
[# ~] mkdir /sys/fs/bpf/container/workdir
[# ~] mount -t overlay overlay -o \
lowerdir=/container/lower,\
upperdir=/sys/fs/bpf/container/upper,\
workdir=/sys/fs/bpf/container/work \
/container/root/bpf
[# ~] ls /container/root/bpf
tracing_prog iter_link

Step 3: pivot root for container, we expect to see the bpf objects are mapped into container,

[# ~] chroot /container/root
[# ~] ls /
bpf
[# ~] ls /bpf
tracing_prog iter_link

Note:

- I haven't tested Step 3. But Step 1 and step 2 seem to be working as expected. I am testing the behaviors of the bpf objects, after we enter the container.

- Only a directory in bpffs is mapped into the container, not the top bpffs. The path is uniform in all containers, that is, /bpf. The container should be able to mkdir in /bpf, etc.

Quoted 12 lines
Quoted 10 lines

2. Once the container runtime has overlayed directory into the
container, it has no need to create more cgroups for this job. It doesn't need to track the stats of job-created cgroups, which are mainly for inspection by the job itself. Even if it needs to collect the stats from those cgroups, it can read from the path in the container.
3. The overlay path in container doesn't have to be exactly the same
as the path in root mount ns. In the sleepable tracing prog, we may select paths based on current process's ns. If we choose to do this, we can further avoid exposing cgroup id and job name to the container.

The benefits make sense.

#9 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

In reply to Hao Luo
Quoted 42 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |  26 +++++
  kernel/bpf/inode.c             |   9 +-
  kernel/bpf/syscall.c           | 177 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |  26 +++++
  5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
index f19abc59b6cd..fce5e26179f5 100644
@@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link);
  struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
  struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
+bool bpf_path_is_bpf_dir(const struct path *path);
  int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
  int bpf_obj_get_user(const char __user *pathname, int flags);
 
include/uapi/linux/bpf.h +0 -0
index afe3d0d7f5f2..a5dbc794403d 100644

include/uapi/linux/bpf.h union bpf_attr { · line 5090

Quoted 5 lines
include/uapi/linux/bpf.h +2 -0
@@ -5086,6 +5086,29 @@ union bpf_attr {
   *	Return
   *		0 on success, or a negative error in case of failure. On error
   *		*dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)

Can we make pathname_sz to be u32 instead of int? pathname_sz should never be negative any way.

Also, I think it is a good idea to add 'u64 flags' parameter for all three helpers, so we have room in the future to tune for new use cases.

Quoted 34 lines
include/uapi/linux/bpf.h +24 -0
+ *	Description
+ *		Attempts to create a directory name *pathname*. The argument
+ *		*pathname_sz* specifies the length of the string *pathname*.
+ *		The argument *mode* specifies the mode for the new directory. It
+ *		is modified by the process's umask. It has the same semantic as
+ *		the syscall mkdir(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a directory, which must be empty.
+ *	Return
+ *		0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *	Description
+ *		Deletes a name and possibly the file it refers to. It has the
+ *		same semantic as the syscall unlink(2).
+ *	Return
+ *		0 on success, or a negative error in case of failure.
   */
  #define __BPF_FUNC_MAPPER(FN)		\
  	FN(unspec),			\
@@ -5280,6 +5303,9 @@ union bpf_attr {
  	FN(xdp_load_bytes),		\
  	FN(xdp_store_bytes),		\
  	FN(copy_from_user_task),	\
+	FN(mkdir),			\
+	FN(rmdir),			\
+	FN(unlink),			\
  	/* */
 
  /* integer value in 'imm' field of BPF_CALL instruction selects which helper

[...]

author
#10 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 55 lines · Yonghong Song
Quoted 42 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |  26 +++++
  kernel/bpf/inode.c             |   9 +-
  kernel/bpf/syscall.c           | 177 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |  26 +++++
  5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
index f19abc59b6cd..fce5e26179f5 100644
@@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link);
  struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
  struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
+bool bpf_path_is_bpf_dir(const struct path *path);
  int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
  int bpf_obj_get_user(const char __user *pathname, int flags);
 
include/uapi/linux/bpf.h +0 -0
index afe3d0d7f5f2..a5dbc794403d 100644

include/uapi/linux/bpf.h union bpf_attr { · line 5090

Quoted 5 lines
include/uapi/linux/bpf.h +2 -0
@@ -5086,6 +5086,29 @@ union bpf_attr {
   *  Return
   *          0 on success, or a negative error in case of failure. On error
   *          *dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)

Can we make pathname_sz to be u32 instead of int? pathname_sz should never be negative any way.

Quoted 3 lines

Also, I think it is a good idea to add 'u64 flags' parameter for all three helpers, so we have room in the future to tune for new use cases.

SG. Will make this change.

Actually, I think I need to cap patthname_sz from above, to ensure pathname_sz isn't too big. Is that necessary? I see there are other helpers that don't have this type of check.

Quoted 35 lines
Quoted 34 lines
include/uapi/linux/bpf.h +24 -0
+ *   Description
+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
+ *   Return
+ *           0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *   Description
+ *           Deletes a directory, which must be empty.
+ *   Return
+ *           0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *   Description
+ *           Deletes a name and possibly the file it refers to. It has the
+ *           same semantic as the syscall unlink(2).
+ *   Return
+ *           0 on success, or a negative error in case of failure.
   */
  #define __BPF_FUNC_MAPPER(FN)               \
      FN(unspec),                     \
@@ -5280,6 +5303,9 @@ union bpf_attr {
      FN(xdp_load_bytes),             \
      FN(xdp_store_bytes),            \
      FN(copy_from_user_task),        \
+     FN(mkdir),                      \
+     FN(rmdir),                      \
+     FN(unlink),                     \
      /* */
 
  /* integer value in 'imm' field of BPF_CALL instruction selects which helper

[...]

#11 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 60 lines · Hao Luo
Quoted 55 lines · Yonghong Song
Quoted 42 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

---
   include/linux/bpf.h            |   1 +
   include/uapi/linux/bpf.h       |  26 +++++
   kernel/bpf/inode.c             |   9 +-
   kernel/bpf/syscall.c           | 177 +++++++++++++++++++++++++++++++++
   tools/include/uapi/linux/bpf.h |  26 +++++
   5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
index f19abc59b6cd..fce5e26179f5 100644
@@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link);
   struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
   struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
+bool bpf_path_is_bpf_dir(const struct path *path);
   int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
   int bpf_obj_get_user(const char __user *pathname, int flags);
 
include/uapi/linux/bpf.h +0 -0
index afe3d0d7f5f2..a5dbc794403d 100644

include/uapi/linux/bpf.h union bpf_attr { · line 5090

Quoted 5 lines
include/uapi/linux/bpf.h +2 -0
@@ -5086,6 +5086,29 @@ union bpf_attr {
    *  Return
    *          0 on success, or a negative error in case of failure. On error
    *          *dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)

Can we make pathname_sz to be u32 instead of int? pathname_sz should never be negative any way.

Quoted 3 lines

Also, I think it is a good idea to add 'u64 flags' parameter for all three helpers, so we have room in the future to tune for new use cases.

Quoted 5 lines

SG. Will make this change.

Actually, I think I need to cap patthname_sz from above, to ensure pathname_sz isn't too big. Is that necessary? I see there are other helpers that don't have this type of check.

There is no need. The verifier should ensure the memory held by pathname will have at least size of pathname_sz.

Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
include/uapi/linux/bpf.h +24 -0
+ *   Description
+ *           Attempts to create a directory name *pathname*. The argument
+ *           *pathname_sz* specifies the length of the string *pathname*.
+ *           The argument *mode* specifies the mode for the new directory. It
+ *           is modified by the process's umask. It has the same semantic as
+ *           the syscall mkdir(2).
+ *   Return
+ *           0 on success, or a negative error in case of failure.
+ *
+ * long bpf_rmdir(const char *pathname, int pathname_sz)
+ *   Description
+ *           Deletes a directory, which must be empty.
+ *   Return
+ *           0 on sucess, or a negative error in case of failure.
+ *
+ * long bpf_unlink(const char *pathname, int pathname_sz)
+ *   Description
+ *           Deletes a name and possibly the file it refers to. It has the
+ *           same semantic as the syscall unlink(2).
+ *   Return
+ *           0 on success, or a negative error in case of failure.
    */
   #define __BPF_FUNC_MAPPER(FN)               \
       FN(unspec),                     \
@@ -5280,6 +5303,9 @@ union bpf_attr {
       FN(xdp_load_bytes),             \
       FN(xdp_store_bytes),            \
       FN(copy_from_user_task),        \
+     FN(mkdir),                      \
+     FN(rmdir),                      \
+     FN(unlink),                     \
       /* */
 
   /* integer value in 'imm' field of BPF_CALL instruction selects which helper

[...]

author
#12 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 20 lines · Yonghong Song
Quoted 16 lines · Hao Luo
Quoted 14 lines · Yonghong Song

include/uapi/linux/bpf.h union bpf_attr { · line 5090

Quoted 6 lines · Hao Luo
include/uapi/linux/bpf.h +2 -0
@@ -5086,6 +5086,29 @@ union bpf_attr {
    *  Return
    *          0 on success, or a negative error in case of failure. On error
    *          *dst* buffer is zeroed out.
+ *
+ * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode)

Can we make pathname_sz to be u32 instead of int? pathname_sz should never be negative any way.

Also, I think it is a good idea to add 'u64 flags' parameter for all three helpers, so we have room in the future to tune for new use cases.

Quoted 9 lines
Quoted 5 lines

SG. Will make this change.

Actually, I think I need to cap patthname_sz from above, to ensure pathname_sz isn't too big. Is that necessary? I see there are other helpers that don't have this type of check.

There is no need. The verifier should ensure the memory held by pathname will have at least size of pathname_sz.

SG. Thanks!

#13 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

In reply to Hao Luo
Quoted 7 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

Quoted 8 lines

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

In which contexts can those be called?

Quoted 14 lines
kernel/bpf/syscall.c +14 -0
+BPF_CALL_2(bpf_rmdir, const char *, pathname, int, pathname_sz)
+{
+	struct user_namespace *mnt_userns;
+	struct path parent;
+	struct dentry *dentry;
+	int err;
+
+	if (pathname_sz <= 1 || pathname[pathname_sz - 1])
+		return -EINVAL;
+
+	err = kern_path(pathname, 0, &parent);
+	if (err)
+		return err;
+

kernel/bpf/syscall.c const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { · line 4938

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+	if (!bpf_path_is_bpf_dir(&parent)) {
+		err = -EPERM;
+		goto exit1;
+	}
+
+	err = mnt_want_write(parent.mnt);
+	if (err)
+		goto exit1;
+
+	dentry = kern_path_locked(pathname, &parent);

This can't be right. Ever. There is no promise whatsoever that these two lookups will resolve to the same place.

Quoted 14 lines
kernel/bpf/syscall.c +14 -0
+BPF_CALL_2(bpf_unlink, const char *, pathname, int, pathname_sz)
+{
+	struct user_namespace *mnt_userns;
+	struct path parent;
+	struct dentry *dentry;
+	struct inode *inode = NULL;
+	int err;
+
+	if (pathname_sz <= 1 || pathname[pathname_sz - 1])
+		return -EINVAL;
+
+	err = kern_path(pathname, 0, &parent);
+	if (err)
+		return err;

kernel/bpf/syscall.c const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { · line 4996

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+
+	err = mnt_want_write(parent.mnt);
+	if (err)
+		goto exit1;
+
+	dentry = kern_path_locked(pathname, &parent);
+	if (IS_ERR(dentry)) {
+		err = PTR_ERR(dentry);
+		goto exit2;
+	}

Ditto. NAK; if you want to poke into fs/namei.c guts, do it right. Or at least discuss that on fsdevel. As it is, it's completely broken. It's racy *and* it blatantly leaks both vfsmount and dentry references.

author
#14 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Hello Al,

Quoted 14 lines · Al Viro
Quoted 12 lines · Hao Luo

This patch allows bpf_syscall prog to perform some basic filesystem
operations: create, remove directories and unlink files. Three bpf helpers are added for this purpose. When combined with the following patches that allow pinning and getting bpf objects from bpf prog, this feature can be used to create directory hierarchy in bpffs that help manage bpf objects purely using bpf progs.

The added helpers subject to the same permission checks as their syscall version. For example, one can not write to a read-only file system; The identity of the current process is checked to see whether it has sufficient permission to perform the operations.

Quoted 6 lines
Quoted 3 lines

Only directories and files in bpffs can be created or removed by these helpers. But it won't be too hard to allow these helpers to operate on files in other filesystems, if we want.

In which contexts can those be called?

In a sleepable context. The plan is to introduce a certain tracepoints as sleepable, a program that attaches to sleepable tracepoints is allowed to call these functions. In particular, the first sleepable tracepoint introduced in this patchset is one at the end of cgroup_mkdir(). Do you have any advices?

Quoted 57 lines
Quoted 14 lines
kernel/bpf/syscall.c +14 -0
+BPF_CALL_2(bpf_rmdir, const char *, pathname, int, pathname_sz)
+{
+     struct user_namespace *mnt_userns;
+     struct path parent;
+     struct dentry *dentry;
+     int err;
+
+     if (pathname_sz <= 1 || pathname[pathname_sz - 1])
+             return -EINVAL;
+
+     err = kern_path(pathname, 0, &parent);
+     if (err)
+             return err;
+

kernel/bpf/syscall.c const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { · line 4938

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+     if (!bpf_path_is_bpf_dir(&parent)) {
+             err = -EPERM;
+             goto exit1;
+     }
+
+     err = mnt_want_write(parent.mnt);
+     if (err)
+             goto exit1;
+
+     dentry = kern_path_locked(pathname, &parent);

This can't be right. Ever. There is no promise whatsoever that these two lookups will resolve to the same place.

Quoted 14 lines
kernel/bpf/syscall.c +14 -0
+BPF_CALL_2(bpf_unlink, const char *, pathname, int, pathname_sz)
+{
+     struct user_namespace *mnt_userns;
+     struct path parent;
+     struct dentry *dentry;
+     struct inode *inode = NULL;
+     int err;
+
+     if (pathname_sz <= 1 || pathname[pathname_sz - 1])
+             return -EINVAL;
+
+     err = kern_path(pathname, 0, &parent);
+     if (err)
+             return err;

kernel/bpf/syscall.c const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { · line 4996

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+
+     err = mnt_want_write(parent.mnt);
+     if (err)
+             goto exit1;
+
+     dentry = kern_path_locked(pathname, &parent);
+     if (IS_ERR(dentry)) {
+             err = PTR_ERR(dentry);
+             goto exit2;
+     }

Ditto. NAK; if you want to poke into fs/namei.c guts, do it right. Or at least discuss that on fsdevel. As it is, it's completely broken. It's racy *and* it blatantly leaks both vfsmount and dentry references.

Quoted 1 line

Thanks Al for taking a look. Actually, there is a simpler approach:
can we export two functions in namei.c that wrap call to do_mkdirat and do_unlinkat, but take a kernel string as pathname? Then these two bpf helpers can use them, don't have to be this complicated. Does this sound good to you?

Thanks!

#15 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 10 lines · Hao Luo

Hello Al,

Quoted 2 lines

In which contexts can those be called?

In a sleepable context. The plan is to introduce a certain tracepoints as sleepable, a program that attaches to sleepable tracepoints is allowed to call these functions. In particular, the first sleepable tracepoint introduced in this patchset is one at the end of cgroup_mkdir(). Do you have any advices?

Yes - don't do it, unless you really want a lot of user-triggerable deadlocks.

Pathname resolution is not locking-agnostic. In particular, you can't do it if you are under any ->i_rwsem, whether it's shared or exclusive. That includes cgroup_mkdir() callchains. And if the pathname passed to these functions will have you walk through the parent directory, you would get screwed (e.g. if the next component happens to be inexistent, triggering a lookup, which takes ->i_rwsem shared).

author
#16 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 16 lines · Al Viro
Quoted 10 lines · Hao Luo

Hello Al,

Quoted 2 lines

In which contexts can those be called?

In a sleepable context. The plan is to introduce a certain tracepoints as sleepable, a program that attaches to sleepable tracepoints is allowed to call these functions. In particular, the first sleepable tracepoint introduced in this patchset is one at the end of cgroup_mkdir(). Do you have any advices?

Yes - don't do it, unless you really want a lot of user-triggerable deadlocks.

Quoted 6 lines

Pathname resolution is not locking-agnostic. In particular, you can't do it if you are under any ->i_rwsem, whether it's shared or exclusive. That includes cgroup_mkdir() callchains. And if the pathname passed to these functions will have you walk through the parent directory, you would get screwed (e.g. if the next component happens to be inexistent, triggering a lookup, which takes ->i_rwsem shared).

I'm thinking of two options, let's see if either can work out:

Option 1: We can put restrictions on the pathname passed into this helper. We can explicitly require the parameter dirfd to be in bpffs (we can verify). In addition, we check pathname to be not containing any dot or dotdot, so the resolved path will end up inside bpffs, therefore won't take ->i_rwsem that is in the callchain of cgroup_mkdir().

Option 2: We can avoid pathname resolution entirely. Like above, we can adjust the semantics of this helper to be: making an immediate directory under the dirfd passed in. In particular, like above, we can enforce the dirfd to be in bpffs and pathname to consist of only alphabet and numbers. With these restrictions, we call vfs_mkdir() to create directories.

Being able to mkdir from bpf has useful use cases, let's try to make it happen even with many limitations.

Thanks!

#17 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 41 lines · Hao Luo
Quoted 16 lines · Al Viro
Quoted 10 lines · Hao Luo

Hello Al,

Quoted 2 lines

In which contexts can those be called?

In a sleepable context. The plan is to introduce a certain tracepoints as sleepable, a program that attaches to sleepable tracepoints is allowed to call these functions. In particular, the first sleepable tracepoint introduced in this patchset is one at the end of cgroup_mkdir(). Do you have any advices?

Yes - don't do it, unless you really want a lot of user-triggerable deadlocks.

Quoted 6 lines

Pathname resolution is not locking-agnostic. In particular, you can't do it if you are under any ->i_rwsem, whether it's shared or exclusive. That includes cgroup_mkdir() callchains. And if the pathname passed to these functions will have you walk through the parent directory, you would get screwed (e.g. if the next component happens to be inexistent, triggering a lookup, which takes ->i_rwsem shared).

I'm thinking of two options, let's see if either can work out:

Option 1: We can put restrictions on the pathname passed into this helper. We can explicitly require the parameter dirfd to be in bpffs (we can verify). In addition, we check pathname to be not containing any dot or dotdot, so the resolved path will end up inside bpffs, therefore won't take ->i_rwsem that is in the callchain of cgroup_mkdir().

Option 2: We can avoid pathname resolution entirely. Like above, we can adjust the semantics of this helper to be: making an immediate directory under the dirfd passed in. In particular, like above, we can enforce the dirfd to be in bpffs and pathname to consist of only alphabet and numbers. With these restrictions, we call vfs_mkdir() to create directories.

Quoted 2 lines

Being able to mkdir from bpf has useful use cases, let's try to make it happen even with many limitations.

Option 3. delegate vfs_mkdir to a worker and wait in the helper.

#18 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 43 lines · Alexei Starovoitov
Quoted 41 lines · Hao Luo
Quoted 16 lines · Al Viro
Quoted 10 lines · Hao Luo

Hello Al,

Quoted 2 lines

In which contexts can those be called?

In a sleepable context. The plan is to introduce a certain tracepoints as sleepable, a program that attaches to sleepable tracepoints is allowed to call these functions. In particular, the first sleepable tracepoint introduced in this patchset is one at the end of cgroup_mkdir(). Do you have any advices?

Yes - don't do it, unless you really want a lot of user-triggerable deadlocks.

Quoted 6 lines

Pathname resolution is not locking-agnostic. In particular, you can't do it if you are under any ->i_rwsem, whether it's shared or exclusive. That includes cgroup_mkdir() callchains. And if the pathname passed to these functions will have you walk through the parent directory, you would get screwed (e.g. if the next component happens to be inexistent, triggering a lookup, which takes ->i_rwsem shared).

I'm thinking of two options, let's see if either can work out:

Option 1: We can put restrictions on the pathname passed into this helper. We can explicitly require the parameter dirfd to be in bpffs (we can verify). In addition, we check pathname to be not containing any dot or dotdot, so the resolved path will end up inside bpffs, therefore won't take ->i_rwsem that is in the callchain of cgroup_mkdir().

Option 2: We can avoid pathname resolution entirely. Like above, we can adjust the semantics of this helper to be: making an immediate directory under the dirfd passed in. In particular, like above, we can enforce the dirfd to be in bpffs and pathname to consist of only alphabet and numbers. With these restrictions, we call vfs_mkdir() to create directories.

Quoted 4 lines
Quoted 2 lines

Being able to mkdir from bpf has useful use cases, let's try to make it happen even with many limitations.

Option 3. delegate vfs_mkdir to a worker and wait in the helper.

I meant _dont_ wait, of course.

#19 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

In reply to Hao Luo
Quoted 6 lines · Hao Luo

Option 1: We can put restrictions on the pathname passed into this helper. We can explicitly require the parameter dirfd to be in bpffs (we can verify). In addition, we check pathname to be not containing any dot or dotdot, so the resolved path will end up inside bpffs, therefore won't take ->i_rwsem that is in the callchain of cgroup_mkdir().

Won't be enough - mount --bind the parent under itself and there you go... Sure, you could prohibit mountpoint crossing, etc., but at that point I'd question the usefulness of pathname resolution in the first place.

author
#20 lore raw

Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall

Quoted 10 lines · Al Viro
Quoted 6 lines · Hao Luo

Option 1: We can put restrictions on the pathname passed into this helper. We can explicitly require the parameter dirfd to be in bpffs (we can verify). In addition, we check pathname to be not containing any dot or dotdot, so the resolved path will end up inside bpffs, therefore won't take ->i_rwsem that is in the callchain of cgroup_mkdir().

Quoted 3 lines

Won't be enough - mount --bind the parent under itself and there you go... Sure, you could prohibit mountpoint crossing, etc., but at that point I'd question the usefulness of pathname resolution in the first place.

[Apologies for resend, my response did not get delivered to mail list]

I don't see a use case where we need to bind mount the directories in bpffs, right now. So in option 1, we can also prohibit mountpoint crossing.

Pathname resolution is still useful in this case. Imagine we want to put all the created dirs under a base dir, we can open the base dir and reuse its fd for multiple mkdirs, for example:

Userspace:
fd = openat(..., "/sys/fs/bpf", ...);
pass fd to the bpf prog

bpf prog:
bpf_mkdirat(fd, "common1", ...);
bpf_mkdirat(fd, "common1/a", ...);
bpf_mkdirat(fd, "common1/b", ...);
bpf_mkdirat(fd, "common2", ...);
...

It would be very inconvenient if we can't resolve multi-level paths.

As Alexei said, another option is to delegate syscall to a worker thread. IMHO, we could do that in future if we find there is a need for the full feature of pathname resolution.

Al, does that sound good?

author
#21 lore raw

PATCH 2/9 bpf: Add BPF_OBJ_PIN and BPF_OBJ_GET in the bpf_sys_bpf helper

In reply to Hao Luo

Now bpf syscall prog is able to pin bpf objects into bpffs and get a pinned object from file system. Combining the previous patch that introduced the helpers for creating and deleting directories in bpffs, syscall prog can now persist bpf objects and organize them in a directory hierarchy.

---
 include/linux/bpf.h  |  4 ++--
 kernel/bpf/inode.c   | 24 ++++++++++++++++++------
 kernel/bpf/syscall.c | 21 ++++++++++++++-------
 3 files changed, 34 insertions(+), 15 deletions(-)
include/linux/bpf.h +2 -2
index fce5e26179f5..c36eeced3838 100644
@@ -1585,8 +1585,8 @@ struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 
 bool bpf_path_is_bpf_dir(const struct path *path);
-int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
-int bpf_obj_get_user(const char __user *pathname, int flags);
+int bpf_obj_pin_path(u32 ufd, bpfptr_t pathname);
+int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
kernel/bpf/inode.c +18 -6
index 3aca00e9e950..6c2db54a2ff9 100644
@@ -440,7 +440,7 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent,
 	return ret;
 }
 
-static int bpf_obj_do_pin(const char __user *pathname, void *raw,
+static int bpf_obj_do_pin(bpfptr_t pathname, void *raw,
 			  enum bpf_type type)
 {
 	struct dentry *dentry;
@@ -448,7 +448,13 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	umode_t mode;
 	int ret;
 
-	dentry = user_path_create(AT_FDCWD, pathname, &path, 0);
+	if (bpfptr_is_null(pathname))
+		return -EINVAL;
+
+	if (bpfptr_is_kernel(pathname))
+		dentry = kern_path_create(AT_FDCWD, pathname.kernel, &path, 0);
+	else
+		dentry = user_path_create(AT_FDCWD, pathname.user, &path, 0);
 	if (IS_ERR(dentry))
 		return PTR_ERR(dentry);
 
@@ -481,7 +487,7 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	return ret;
 }
 
-int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
+int bpf_obj_pin_path(u32 ufd, bpfptr_t pathname)
 {
 	enum bpf_type type;
 	void *raw;
@@ -498,7 +504,7 @@ int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
 	return ret;
 }
 
-static void *bpf_obj_do_get(const char __user *pathname,
+static void *bpf_obj_do_get(bpfptr_t pathname,
 			    enum bpf_type *type, int flags)
 {
 	struct inode *inode;
@@ -506,7 +512,13 @@ static void *bpf_obj_do_get(const char __user *pathname,
 	void *raw;
 	int ret;
 
-	ret = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW, &path);
+	if (bpfptr_is_null(pathname))
+		return ERR_PTR(-EINVAL);
+
+	if (bpfptr_is_kernel(pathname))
+		ret = kern_path(pathname.kernel, LOOKUP_FOLLOW, &path);
+	else
+		ret = user_path_at(AT_FDCWD, pathname.user, LOOKUP_FOLLOW, &path);
 	if (ret)
 		return ERR_PTR(ret);
 
@@ -530,7 +542,7 @@ static void *bpf_obj_do_get(const char __user *pathname,
 	return ERR_PTR(ret);
 }
 
-int bpf_obj_get_user(const char __user *pathname, int flags)
+int bpf_obj_get_path(bpfptr_t pathname, int flags)
 {
 	enum bpf_type type = BPF_TYPE_UNSPEC;
 	int f_flags;
kernel/bpf/syscall.c +14 -7
index 07683b791733..9e6d8d0c8af5 100644
@@ -2402,22 +2402,27 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
 
 #define BPF_OBJ_LAST_FIELD file_flags
 
-static int bpf_obj_pin(const union bpf_attr *attr)
+static int bpf_obj_pin(const union bpf_attr *attr, bpfptr_t uattr)
 {
+	bpfptr_t pathname;
+
 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
 		return -EINVAL;
 
-	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
+	pathname = make_bpfptr(attr->pathname, bpfptr_is_kernel(uattr));
+	return bpf_obj_pin_path(attr->bpf_fd, pathname);
 }
 
-static int bpf_obj_get(const union bpf_attr *attr)
+static int bpf_obj_get(const union bpf_attr *attr, bpfptr_t uattr)
 {
+	bpfptr_t pathname;
+
 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
 	    attr->file_flags & ~BPF_OBJ_FLAG_MASK)
 		return -EINVAL;
 
-	return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
-				attr->file_flags);
+	pathname = make_bpfptr(attr->pathname, bpfptr_is_kernel(uattr));
+	return bpf_obj_get_path(pathname, attr->file_flags);
 }
 
 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
@@ -4648,10 +4653,10 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
 		err = bpf_prog_load(&attr, uattr);
 		break;
 	case BPF_OBJ_PIN:
-		err = bpf_obj_pin(&attr);
+		err = bpf_obj_pin(&attr, uattr);
 		break;
 	case BPF_OBJ_GET:
-		err = bpf_obj_get(&attr);
+		err = bpf_obj_get(&attr, uattr);
 		break;
 	case BPF_PROG_ATTACH:
 		err = bpf_prog_attach(&attr);
@@ -4776,6 +4781,8 @@ BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
 	case BPF_BTF_LOAD:
 	case BPF_LINK_CREATE:
 	case BPF_RAW_TRACEPOINT_OPEN:
+	case BPF_OBJ_PIN:
+	case BPF_OBJ_GET:
 		break;
 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
 	case BPF_PROG_TEST_RUN:
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
author
#22 lore raw

PATCH 3/9 selftests/bpf: tests mkdir, rmdir, unlink and pin in syscall

In reply to Hao Luo

Add a subtest in syscall to test bpf_mkdir(), bpf_rmdir(), bpf_unlink() and object pinning in syscall prog.

---
 .../selftests/bpf/prog_tests/syscall.c        | 67 +++++++++++++++++-
 .../testing/selftests/bpf/progs/syscall_fs.c  | 69 +++++++++++++++++++
 2 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/syscall_fs.c
tools/testing/selftests/bpf/prog_tests/syscall.c +66 -1
index f4d40001155a..782b5fe73096 100644
@@ -1,7 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2021 Facebook */
+#include <sys/stat.h>
 #include <test_progs.h>
 #include "syscall.skel.h"
+#include "syscall_fs.skel.h"
 
 struct args {
 	__u64 log_buf;
@@ -12,7 +14,7 @@ struct args {
 	int btf_fd;
 };
 
-void test_syscall(void)
+static void test_syscall_basic(void)
 {
 	static char verifier_log[8192];
 	struct args ctx = {
@@ -53,3 +55,66 @@ void test_syscall(void)
 	if (ctx.btf_fd > 0)
 		close(ctx.btf_fd);
 }
+
+static void test_syscall_fs(void)
+{
+	char tmpl[] = "/sys/fs/bpf/syscall_XXXXXX";
+	struct stat statbuf = {};
+	static char verifier_log[8192];
+	struct args ctx = {
+		.log_buf = (uintptr_t) verifier_log,
+		.log_size = sizeof(verifier_log),
+		.prog_fd = 0,
+	};
+	LIBBPF_OPTS(bpf_test_run_opts, tattr,
+		.ctx_in = &ctx,
+		.ctx_size_in = sizeof(ctx),
+	);
+	struct syscall_fs *skel = NULL;
+	int err, mkdir_fd, rmdir_fd;
+	char *root, *dir, *path;
+
+	/* prepares test directories */
+	system("mount -t bpf bpffs /sys/fs/bpf");
+	root = mkdtemp(tmpl);
+	chmod(root, 0755);
+
+	/* loads prog */
+	skel = syscall_fs__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_load"))
+		goto cleanup;
+
+	dir = skel->bss->dirname;
+	snprintf(dir, sizeof(skel->bss->dirname), "%s/test", root);
+	path = skel->bss->pathname;
+	snprintf(path, sizeof(skel->bss->pathname), "%s/prog", dir);
+
+	/* tests mkdir */
+	mkdir_fd = bpf_program__fd(skel->progs.mkdir_prog);
+	err = bpf_prog_test_run_opts(mkdir_fd, &tattr);
+	ASSERT_EQ(err, 0, "mkdir_err");
+	ASSERT_EQ(tattr.retval, 0, "mkdir_retval");
+	ASSERT_OK(stat(dir, &statbuf), "mkdir_success");
+	ASSERT_OK(stat(path, &statbuf), "pin_success");
+
+	/* tests rmdir */
+	rmdir_fd = bpf_program__fd(skel->progs.rmdir_prog);
+	err = bpf_prog_test_run_opts(rmdir_fd, &tattr);
+	ASSERT_EQ(err, 0, "rmdir_err");
+	ASSERT_EQ(tattr.retval, 0, "rmdir_retval");
+	ASSERT_ERR(stat(path, &statbuf), "unlink_success");
+	ASSERT_ERR(stat(dir, &statbuf), "rmdir_success");
+
+cleanup:
+	syscall_fs__destroy(skel);
+	if (ctx.prog_fd > 0)
+		close(ctx.prog_fd);
+	rmdir(root);
+}
+
+void test_syscall(void) {
+	if (test__start_subtest("basic"))
+		test_syscall_basic();
+	if (test__start_subtest("filesystem"))
+		test_syscall_fs();
+}
tools/testing/selftests/bpf/progs/syscall_fs.c new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
author
#23 lore raw

PATCH 4/9 bpf: Introduce sleepable tracepoints

In reply to Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

In the following patches, we will whitelist some tracepoints to be sleepable.

---
 include/linux/bpf.h             | 10 +++++++-
 include/linux/tracepoint-defs.h |  1 +
 include/trace/bpf_probe.h       | 22 ++++++++++++++----
 kernel/bpf/syscall.c            | 41 +++++++++++++++++++++++----------
 kernel/trace/bpf_trace.c        |  5 ++++
 5 files changed, 61 insertions(+), 18 deletions(-)
include/linux/bpf.h +9 -1
index c36eeced3838..759ade7b24b3 100644
@@ -1810,6 +1810,9 @@ struct bpf_prog *bpf_prog_by_id(u32 id);
 struct bpf_link *bpf_link_by_id(u32 id);
 
 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
+const struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
+
 void bpf_task_storage_free(struct task_struct *task);
 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
 const struct btf_func_model *
@@ -1822,7 +1825,6 @@ struct bpf_core_ctx {
 
 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
 		   int relo_idx, void *insn);
-
 #else /* !CONFIG_BPF_SYSCALL */
 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
 {
@@ -2011,6 +2013,12 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 	return NULL;
 }
 
+static inline struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+	return NULL;
+}
+
 static inline void bpf_task_storage_free(struct task_struct *task)
 {
 }
include/linux/tracepoint-defs.h +1 -0
index e7c2276be33e..c73c7ab3680e 100644
@@ -51,6 +51,7 @@ struct bpf_raw_event_map {
 	void			*bpf_func;
 	u32			num_args;
 	u32			writable_size;
+	u32			sleepable;
 } __aligned(32);
 
 /*
include/trace/bpf_probe.h +17 -5
index 7660a7846586..4edfc6df2f52 100644
@@ -88,7 +88,7 @@ __bpf_trace_##call(void *__data, proto)					\
  * to make sure that if the tracepoint handling changes, the
  * bpf probe will fail to compile unless it too is updated.
  */
-#define __DEFINE_EVENT(template, call, proto, args, size)		\
+#define __DEFINE_EVENT(template, call, proto, args, size, sleep)	\
 static inline void bpf_test_probe_##call(void)				\
 {									\
 	check_trace_callback_type_##call(__bpf_trace_##template);	\
@@ -104,6 +104,7 @@ __section("__bpf_raw_tp_map") = {					\
 		.bpf_func	= __bpf_trace_##template,		\
 		.num_args	= COUNT_ARGS(args),			\
 		.writable_size	= size,					\
+		.sleepable	= sleep,				\
 	},								\
 };
 
@@ -123,11 +124,15 @@ static inline void bpf_test_buffer_##call(void)				\
 #undef DEFINE_EVENT_WRITABLE
 #define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
 	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
-	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
+	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size, 0)
+
+#undef DEFINE_EVENT_SLEEPABLE
+#define DEFINE_EVENT_SLEEPABLE(template, call, proto, args)	\
+	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0, 1)
 
 #undef DEFINE_EVENT
 #define DEFINE_EVENT(template, call, proto, args)			\
-	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0)
+	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0, 0)
 
 #undef DEFINE_EVENT_PRINT
 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
@@ -136,19 +141,26 @@ static inline void bpf_test_buffer_##call(void)				\
 #undef DECLARE_TRACE
 #define DECLARE_TRACE(call, proto, args)				\
 	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))		\
-	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
+	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0, 0)
 
 #undef DECLARE_TRACE_WRITABLE
 #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
 	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
 	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
-	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
+	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size, 0)
+
+#undef DECLARE_TRACE_SLEEPABLE
+#define DECLARE_TRACE_SLEEPABLE(call, proto, args)			\
+	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))		\
+	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0, 1)
 
 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
 
 #undef DECLARE_TRACE_WRITABLE
 #undef DEFINE_EVENT_WRITABLE
 #undef __CHECK_WRITABLE_BUF_SIZE
+#undef DECLARE_TRACE_SLEEPABLE
+#undef DEFINE_EVENT_SLEEPABLE
 #undef __DEFINE_EVENT
 #undef FIRST
 
kernel/bpf/syscall.c +29 -12

65 lines not on this page · raw message

kernel/trace/bpf_trace.c +5 -0
index a2024ba32a20..c816e0e0d4a0 100644
@@ -1691,6 +1691,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		fn = raw_tp_prog_func_proto(func_id, prog);
 		if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
 			fn = bpf_iter_get_func_proto(func_id, prog);
+		if (!fn && prog->aux->sleepable)
+			fn = tracing_prog_syscall_func_proto(func_id, prog);
 		return fn;
 	}
 }
@@ -2053,6 +2055,9 @@ static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *
 	if (prog->aux->max_tp_access > btp->writable_size)
 		return -EINVAL;
 
+	if (prog->aux->sleepable && !btp->sleepable)
+		return -EPERM;
+
 	return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
 						   prog);
 }
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#24 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

include/linux/tracepoint-defs.h struct bpf_raw_event_map { · line 54

Quoted 9 lines · Hao Luo
include/linux/tracepoint-defs.h +1 -0
index e7c2276be33e..c73c7ab3680e 100644
@@ -51,6 +51,7 @@ struct bpf_raw_event_map {
 	void			*bpf_func;
 	u32			num_args;
 	u32			writable_size;
+	u32			sleepable;

It increases the size for all tracepoints. See BPF_RAW_TP in include/asm-generic/vmlinux.lds.h Please switch writeable_size and sleepable to u16.

Quoted 17 lines
include/linux/tracepoint-defs.h +4 -6
-static const struct bpf_func_proto *
-syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+/* Syscall helpers that are also allowed in sleepable tracing prog. */
+const struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id,
+				const struct bpf_prog *prog)
 {
 	switch (func_id) {
 	case BPF_FUNC_sys_bpf:
 		return &bpf_sys_bpf_proto;
-	case BPF_FUNC_btf_find_by_name_kind:
-		return &bpf_btf_find_by_name_kind_proto;
 	case BPF_FUNC_sys_close:
 		return &bpf_sys_close_proto;
-	case BPF_FUNC_kallsyms_lookup_name:
-		return &bpf_kallsyms_lookup_name_proto;

include/linux/tracepoint-defs.h struct bpf_raw_event_map {

Quoted 10 lines
include/linux/tracepoint-defs.h +4 -0
 	case BPF_FUNC_mkdir:
 		return &bpf_mkdir_proto;
 	case BPF_FUNC_rmdir:
 		return &bpf_rmdir_proto;
 	case BPF_FUNC_unlink:
 		return &bpf_unlink_proto;
+	default:
+		return NULL;
+	}
+}

If I read this correctly the goal is to disallow find_by_name_kind and lookup_name from sleepable tps. Why? What's the harm?

author
#25 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 15 lines · Alexei Starovoitov

include/linux/tracepoint-defs.h struct bpf_raw_event_map { · line 54

Quoted 9 lines · Hao Luo
include/linux/tracepoint-defs.h +1 -0
index e7c2276be33e..c73c7ab3680e 100644
@@ -51,6 +51,7 @@ struct bpf_raw_event_map {
      void                    *bpf_func;
      u32                     num_args;
      u32                     writable_size;
+     u32                     sleepable;

It increases the size for all tracepoints. See BPF_RAW_TP in include/asm-generic/vmlinux.lds.h Please switch writeable_size and sleepable to u16.

No problem.

Quoted 22 lines
Quoted 22 lines
include/linux/tracepoint-defs.h +4 -6
-static const struct bpf_func_proto *
-syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+/* Syscall helpers that are also allowed in sleepable tracing prog. */
+const struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id,
+                             const struct bpf_prog *prog)
 {
      switch (func_id) {
      case BPF_FUNC_sys_bpf:
              return &bpf_sys_bpf_proto;
-     case BPF_FUNC_btf_find_by_name_kind:
-             return &bpf_btf_find_by_name_kind_proto;
      case BPF_FUNC_sys_close:
              return &bpf_sys_close_proto;
-     case BPF_FUNC_kallsyms_lookup_name:
-             return &bpf_kallsyms_lookup_name_proto;
      case BPF_FUNC_mkdir:
              return &bpf_mkdir_proto;
      case BPF_FUNC_rmdir:
              return &bpf_rmdir_proto;
      case BPF_FUNC_unlink:
Quoted 8 lines

include/linux/tracepoint-defs.h

Quoted 5 lines
              return &bpf_unlink_proto;
include/linux/tracepoint-defs.h +4 -0
+     default:
+             return NULL;
+     }
+}

If I read this correctly the goal is to disallow find_by_name_kind and lookup_name from sleepable tps. Why? What's the harm?

A couple of thoughts, please correct me if they don't make sense. I may think too much.

1. The very first reason is, I don't know the use case of them in tracing. So I think I can leave them right now and add them later if the maintainers want them.

2. A related question is, do we actually want all syscall helpers to be in sleepable tracing? Some helpers may cause re-entering the tracepoints. For a hypothetical example, if we call mkdir while tracing some tracepoints in vfs_mkdir. Do we have protection for this? Another potential problem is about lookup_name in particular, sleepable_tracing could be triggered by any user, will lookup_name leak kernel addresses to users in some way? The filesystem helpers have some basic perm checks, I would think it's relatively safer.

#26 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 55 lines · Hao Luo
Quoted 15 lines · Alexei Starovoitov

include/linux/tracepoint-defs.h struct bpf_raw_event_map { · line 54

Quoted 9 lines · Hao Luo
include/linux/tracepoint-defs.h +1 -0
index e7c2276be33e..c73c7ab3680e 100644
@@ -51,6 +51,7 @@ struct bpf_raw_event_map {
      void                    *bpf_func;
      u32                     num_args;
      u32                     writable_size;
+     u32                     sleepable;

It increases the size for all tracepoints. See BPF_RAW_TP in include/asm-generic/vmlinux.lds.h Please switch writeable_size and sleepable to u16.

No problem.

Quoted 22 lines
Quoted 22 lines
include/linux/tracepoint-defs.h +4 -6
-static const struct bpf_func_proto *
-syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+/* Syscall helpers that are also allowed in sleepable tracing prog. */
+const struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id,
+                             const struct bpf_prog *prog)
 {
      switch (func_id) {
      case BPF_FUNC_sys_bpf:
              return &bpf_sys_bpf_proto;
-     case BPF_FUNC_btf_find_by_name_kind:
-             return &bpf_btf_find_by_name_kind_proto;
      case BPF_FUNC_sys_close:
              return &bpf_sys_close_proto;
-     case BPF_FUNC_kallsyms_lookup_name:
-             return &bpf_kallsyms_lookup_name_proto;
      case BPF_FUNC_mkdir:
              return &bpf_mkdir_proto;
      case BPF_FUNC_rmdir:
              return &bpf_rmdir_proto;
      case BPF_FUNC_unlink:
Quoted 8 lines

include/linux/tracepoint-defs.h

Quoted 5 lines
              return &bpf_unlink_proto;
include/linux/tracepoint-defs.h +4 -0
+     default:
+             return NULL;
+     }
+}

If I read this correctly the goal is to disallow find_by_name_kind and lookup_name from sleepable tps. Why? What's the harm?

A couple of thoughts, please correct me if they don't make sense. I may think too much.

Quoted 8 lines

1. The very first reason is, I don't know the use case of them in tracing. So I think I can leave them right now and add them later if the maintainers want them.

2. A related question is, do we actually want all syscall helpers to be in sleepable tracing? Some helpers may cause re-entering the tracepoints. For a hypothetical example, if we call mkdir while tracing some tracepoints in vfs_mkdir. Do we have protection for this?

If we go with noinline weak nop function approach then we will get recursion protection for free. All trampoline powered progs have it. Both sleepable and not.

Quoted 4 lines

Another potential problem is about lookup_name in particular, sleepable_tracing could be triggered by any user, will lookup_name leak kernel addresses to users in some way? The filesystem helpers have some basic perm checks, I would think it's relatively safer.

The tracepoint may be triggerable by any user, but the sleepable tp bpf prog will be loaded with cap_perfmon permissions, so it has the rights to read anything.
So I don't see any concerns with enabling lookup_name to both syscall bpf prog and tp progs.

#27 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

In reply to Hao Luo
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 60 lines

In the following patches, we will whitelist some tracepoints to be sleepable.

---
  include/linux/bpf.h             | 10 +++++++-
  include/linux/tracepoint-defs.h |  1 +
  include/trace/bpf_probe.h       | 22 ++++++++++++++----
  kernel/bpf/syscall.c            | 41 +++++++++++++++++++++++----------
  kernel/trace/bpf_trace.c        |  5 ++++
  5 files changed, 61 insertions(+), 18 deletions(-)
include/linux/bpf.h +9 -1
index c36eeced3838..759ade7b24b3 100644
@@ -1810,6 +1810,9 @@ struct bpf_prog *bpf_prog_by_id(u32 id);
  struct bpf_link *bpf_link_by_id(u32 id);
 
  const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
+const struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
+
  void bpf_task_storage_free(struct task_struct *task);
  bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
  const struct btf_func_model *
@@ -1822,7 +1825,6 @@ struct bpf_core_ctx {
 
  int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
  		   int relo_idx, void *insn);
-
  #else /* !CONFIG_BPF_SYSCALL */
  static inline struct bpf_prog *bpf_prog_get(u32 ufd)
  {
@@ -2011,6 +2013,12 @@ bpf_base_func_proto(enum bpf_func_id func_id)
  	return NULL;
  }
 
+static inline struct bpf_func_proto *
+tracing_prog_syscall_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+	return NULL;
+}
+
  static inline void bpf_task_storage_free(struct task_struct *task)
  {
  }
include/linux/tracepoint-defs.h +1 -0
index e7c2276be33e..c73c7ab3680e 100644
@@ -51,6 +51,7 @@ struct bpf_raw_event_map {
  	void			*bpf_func;
  	u32			num_args;
  	u32			writable_size;
+	u32			sleepable;
  } __aligned(32);
 
  /*

[...]

#28 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 9 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

Quoted 4 lines

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

#29 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

Quoted 7 lines
Quoted 1 line

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

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

#30 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 19 lines · Yonghong Song
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 8 lines
Quoted 5 lines

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

Just remembered that we can also do trivial noinline __weak nop function and mark it sleepable on the verifier side. That's what we were planning to do to trace map update/delete ops in Joe Burton's series.
Then we don't need to extend tp infra.
I'm fine whichever way. I see pros and cons in both options.

author
#31 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 30 lines · Alexei Starovoitov
Quoted 19 lines · Yonghong Song
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 8 lines
Quoted 5 lines

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

Quoted 6 lines

Just remembered that we can also do trivial noinline __weak nop function and mark it sleepable on the verifier side. That's what we were planning to do to trace map update/delete ops in Joe Burton's series.
Then we don't need to extend tp infra.
I'm fine whichever way. I see pros and cons in both options.

Joe is also cc'ed in this patchset, I will sync up with him on the status of trace map work.

Alexei, do we have potentially other variants of tp? We can make the current u16 sleepable a flag, so we can reuse this flag later when we have another type of tracepoints.

#32 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 40 lines · Hao Luo
Quoted 30 lines · Alexei Starovoitov
Quoted 19 lines · Yonghong Song
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 8 lines
Quoted 5 lines

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

Quoted 6 lines

Just remembered that we can also do trivial noinline __weak nop function and mark it sleepable on the verifier side. That's what we were planning to do to trace map update/delete ops in Joe Burton's series.
Then we don't need to extend tp infra.
I'm fine whichever way. I see pros and cons in both options.

Quoted 6 lines

Joe is also cc'ed in this patchset, I will sync up with him on the status of trace map work.

Alexei, do we have potentially other variants of tp? We can make the current u16 sleepable a flag, so we can reuse this flag later when we have another type of tracepoints.

When we added the ability to attach to kernel functions and mark them as allow_error_inject the usefulness of tracepoints and even writeable tracepoints was deminissed.
If we do sleepable tracepoint, I suspect, it may be the last extension in that area.
I guess I'm convincing myself that noinline weak nop func is better here. Just like it's better for Joe's map tracing.

#33 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 49 lines · Alexei Starovoitov
Quoted 40 lines · Hao Luo
Quoted 30 lines · Alexei Starovoitov
Quoted 19 lines · Yonghong Song
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 8 lines
Quoted 5 lines

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

Quoted 6 lines

Just remembered that we can also do trivial noinline __weak nop function and mark it sleepable on the verifier side. That's what we were planning to do to trace map update/delete ops in Joe Burton's series.
Then we don't need to extend tp infra.
I'm fine whichever way. I see pros and cons in both options.

Quoted 6 lines

Joe is also cc'ed in this patchset, I will sync up with him on the status of trace map work.

Alexei, do we have potentially other variants of tp? We can make the current u16 sleepable a flag, so we can reuse this flag later when we have another type of tracepoints.

Quoted 7 lines

When we added the ability to attach to kernel functions and mark them as allow_error_inject the usefulness of tracepoints and even writeable tracepoints was deminissed.
If we do sleepable tracepoint, I suspect, it may be the last extension in that area.
I guess I'm convincing myself that noinline weak nop func is better here. Just like it's better for Joe's map tracing.

To add to the above... The only downside of sleepable nop func comparing to tp is the lack of static_branch.
So this nop call will always be there.
For map tracing and for cgroup mkdir/rmdir the few nanosecond overhead of calling an empty function isn't even measurable.

author
#34 lore raw

Re: [PATCH bpf-next v1 4/9] bpf: Introduce sleepable tracepoints

Quoted 60 lines · Alexei Starovoitov
Quoted 49 lines · Alexei Starovoitov
Quoted 40 lines · Hao Luo
Quoted 30 lines · Alexei Starovoitov
Quoted 19 lines · Yonghong Song
Quoted 15 lines · Alexei Starovoitov
Quoted 13 lines · Yonghong Song
Quoted 4 lines · Hao Luo

Add a new type of bpf tracepoints: sleepable tracepoints, which allows the handler to make calls that may sleep. With sleepable tracepoints, a set of syscall helpers (which may sleep) may also be called from sleepable tracepoints.

There are some old discussions on sleepable tracepoints, maybe worthwhile to take a look.

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

Quoted 8 lines
Quoted 5 lines

Right. It's very much related, but obsolete too. We don't need any of that for sleeptable _raw_ tps. I prefer to stay with "sleepable" name as well to match the rest of the bpf sleepable code. In all cases it's faultable.

sounds good to me. Agree that for the bpf user case, Hao's implementation should be enough.

Quoted 6 lines

Just remembered that we can also do trivial noinline __weak nop function and mark it sleepable on the verifier side. That's what we were planning to do to trace map update/delete ops in Joe Burton's series.
Then we don't need to extend tp infra.
I'm fine whichever way. I see pros and cons in both options.

Quoted 6 lines

Joe is also cc'ed in this patchset, I will sync up with him on the status of trace map work.

Alexei, do we have potentially other variants of tp? We can make the current u16 sleepable a flag, so we can reuse this flag later when we have another type of tracepoints.

Quoted 7 lines

When we added the ability to attach to kernel functions and mark them as allow_error_inject the usefulness of tracepoints and even writeable tracepoints was deminissed.
If we do sleepable tracepoint, I suspect, it may be the last extension in that area.
I guess I'm convincing myself that noinline weak nop func is better here. Just like it's better for Joe's map tracing.

Quoted 5 lines

To add to the above... The only downside of sleepable nop func comparing to tp is the lack of static_branch.
So this nop call will always be there.
For map tracing and for cgroup mkdir/rmdir the few nanosecond overhead of calling an empty function isn't even measurable.

The overhead should be fine, I think. mkdir/rmdir won't be frequent operations. Thanks for the explanation. Let me give it a try and report back how it works.

author
#35 lore raw

PATCH 5/9 cgroup: Sleepable cgroup tracepoints.

In reply to Hao Luo

Add two new sleepable tracepoints in cgroup: cgroup_mkdir_s and cgroup_rmdir_s. The suffix _s means they are in a sleepable context. These two tracepoints don't need full cgroup paths, they don't have to live in atomic context. These two tracepoints are also called without holding cgroup_mutex.

They can be used for bpf to monitor cgroup creation and deletion. Bpf sleepable programs can attach to these two tracepoints and create corresponding directories in bpffs. The created directories don't need the cgroup paths, cgroup id is sufficient to identify the cgroup. Once the bpffs directories have been created, the bpf prog can further pin bpf objects inside the directories and allow users to read the pinned objects.

This serves a way to extend the fixed cgroup interface.

---
 include/trace/events/cgroup.h | 45 +++++++++++++++++++++++++++++++++++
 kernel/cgroup/cgroup.c        |  5 ++++
 2 files changed, 50 insertions(+)
include/trace/events/cgroup.h +45 -0
index dd7d7c9efecd..4483a7d6c43a 100644
@@ -204,6 +204,51 @@ DEFINE_EVENT(cgroup_event, cgroup_notify_frozen,
 	TP_ARGS(cgrp, path, val)
 );
 
+/*
+ * The following tracepoints are supposed to be called in a sleepable context.
+ */
+DECLARE_EVENT_CLASS(cgroup_sleepable_tp,
+
+	TP_PROTO(struct cgroup *cgrp),
+
+	TP_ARGS(cgrp),
+
+	TP_STRUCT__entry(
+		__field(	int,		root			)
+		__field(	int,		level			)
+		__field(	u64,		id			)
+	),
+
+	TP_fast_assign(
+		__entry->root = cgrp->root->hierarchy_id;
+		__entry->id = cgroup_id(cgrp);
+		__entry->level = cgrp->level;
+	),
+
+	TP_printk("root=%d id=%llu level=%d",
+		  __entry->root, __entry->id, __entry->level)
+);
+
+#ifdef DEFINE_EVENT_SLEEPABLE
+#undef DEFINE_EVENT
+#define DEFINE_EVENT(template, call, proto, args)		\
+	DEFINE_EVENT_SLEEPABLE(template, call, PARAMS(proto), PARAMS(args))
+#endif
+
+DEFINE_EVENT(cgroup_sleepable_tp, cgroup_mkdir_s,
+
+	TP_PROTO(struct cgroup *cgrp),
+
+	TP_ARGS(cgrp)
+);
+
+DEFINE_EVENT(cgroup_sleepable_tp, cgroup_rmdir_s,
+
+	TP_PROTO(struct cgroup *cgrp),
+
+	TP_ARGS(cgrp)
+);
+
 #endif /* _TRACE_CGROUP_H */
 
 /* This part must be outside protection */
kernel/cgroup/cgroup.c +5 -0
index 9d05c3ca2d5e..f14ab00d9ef5 100644
@@ -5535,6 +5535,8 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
 	cgroup_destroy_locked(cgrp);
 out_unlock:
 	cgroup_kn_unlock(parent_kn);
+	if (!ret)
+		trace_cgroup_mkdir_s(cgrp);
 	return ret;
 }
 
@@ -5725,6 +5727,9 @@ int cgroup_rmdir(struct kernfs_node *kn)
 		TRACE_CGROUP_PATH(rmdir, cgrp);
 
 	cgroup_kn_unlock(kn);
+
+	if (!ret)
+		trace_cgroup_rmdir_s(cgrp);
 	return ret;
 }
 
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
author
#36 lore raw

PATCH 6/9 libbpf: Add sleepable tp_btf

In reply to Hao Luo

In the previous patches, we have introduced sleepable tracepoints in the kernel and listed a couple of cgroup tracepoints as sleepable. This patch introduces a sleepable version of tp_btf. Sleepable tp_btf progs can only attach to sleepable tracepoints.

---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
index 776b8e034d62..910682357390 100644
@@ -8619,6 +8619,7 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("fentry/",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fmod_ret/",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
 	SEC_DEF("fexit/",		TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
+	SEC_DEF("tp_btf.s/",            TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fentry.s/",		TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fmod_ret.s/",		TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
 	SEC_DEF("fexit.s/",		TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
author
#37 lore raw

PATCH 7/9 bpf: Lift permission check in __sys_bpf when called from kernel.

In reply to Hao Luo

After we introduced sleepable tracing programs, we now have an interesting problem. There are now three execution paths that can reach bpf_sys_bpf:

1. called from bpf syscall.
2. called from kernel context (e.g. kernel modules).
3. called from bpf programs.

Ideally, capability check in bpf_sys_bpf is necessary for the first two scenarios. But it may not be necessary for the third case.

The use case of sleepable tracepoints is to allow root user to deploy bpf progs which run when a certain kernel tracepoints are triggered. An example use case is to monitor cgroup creation and perform bpf operations whenever a cgroup is created. These operations include pinning an iter to export the cgroup's state. Using sleepable tracing is preferred because it eliminates the need of a userspace daemon to monitor cgroup changes.

However, in this use case, the current task who triggers the tracepoint may be unprivileged and the permission check in __sys_bpf will thus prevent it from making bpf syscalls. Therefore the tracing progs deployed by root can not be used by non-root users.

A solution to this problem is to lift the permission check if the caller of bpf_sys_bpf comes from either kernel context or bpf programs.

An alternative of lifting this permission check would be introducing an 'unpriv' version of bpf_sys_bpf, which doesn't check the current task's capability. If the owner of the tracing prog wants it to be exclusively used by root users, they can use the 'priv' version of bpf_sys_bpf; if the owner wants it to be usable for non-root users, they can use the 'unpriv' version.

---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
index 0a12f52fe8a9..3bf88002ee56 100644
@@ -4613,7 +4613,7 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
 	union bpf_attr attr;
 	int err;
 
-	if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
+	if (sysctl_unprivileged_bpf_disabled && !bpf_capable() && !uattr.is_kernel)
 		return -EPERM;
 
 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#38 lore raw

Re: [PATCH bpf-next v1 7/9] bpf: Lift permission check in __sys_bpf when called from kernel.

Quoted 10 lines · Hao Luo

After we introduced sleepable tracing programs, we now have an interesting problem. There are now three execution paths that can reach bpf_sys_bpf:

1. called from bpf syscall.
2. called from kernel context (e.g. kernel modules).
3. called from bpf programs.

Ideally, capability check in bpf_sys_bpf is necessary for the first two scenarios. But it may not be necessary for the third case.

Well, it's unnecessary for the first two as well.
When called from the kernel lskel it's a pointless check. The kernel module can do anything regardless.
When called from bpf syscall program it's not quite correct either. When CAP_BPF was introduced we've designed it to enforce permissions at prog load time. The prog_run doesn't check permissions. So syscall progs don't need this secondary permission check. Please add "case BPF_PROG_TYPE_SYSCALL:" to is_perfmon_prog_type() and combine it with this patch.

That would be the best. The alternative below is less appealing.

Quoted 6 lines

An alternative of lifting this permission check would be introducing an 'unpriv' version of bpf_sys_bpf, which doesn't check the current task's capability. If the owner of the tracing prog wants it to be exclusively used by root users, they can use the 'priv' version of bpf_sys_bpf; if the owner wants it to be usable for non-root users, they can use the 'unpriv' version.

...

kernel/bpf/syscall.c static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) · line 4616

Quoted 2 lines
kernel/bpf/syscall.c +1 -1
-	if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
+	if (sysctl_unprivileged_bpf_disabled && !bpf_capable() && !uattr.is_kernel)

This is great idea. If I could think of this I would went with it when prog_syscall was introduced.

author
#39 lore raw

Re: [PATCH bpf-next v1 7/9] bpf: Lift permission check in __sys_bpf when called from kernel.

Quoted 13 lines · Alexei Starovoitov
Quoted 10 lines · Hao Luo

After we introduced sleepable tracing programs, we now have an interesting problem. There are now three execution paths that can reach bpf_sys_bpf:

1. called from bpf syscall.
2. called from kernel context (e.g. kernel modules).
3. called from bpf programs.

Ideally, capability check in bpf_sys_bpf is necessary for the first two scenarios. But it may not be necessary for the third case.

Quoted 10 lines

Well, it's unnecessary for the first two as well.
When called from the kernel lskel it's a pointless check. The kernel module can do anything regardless.
When called from bpf syscall program it's not quite correct either. When CAP_BPF was introduced we've designed it to enforce permissions at prog load time. The prog_run doesn't check permissions. So syscall progs don't need this secondary permission check. Please add "case BPF_PROG_TYPE_SYSCALL:" to is_perfmon_prog_type() and combine it with this patch.

Sure, will do.

Quoted 9 lines

That would be the best. The alternative below is less appealing.

Quoted 6 lines

An alternative of lifting this permission check would be introducing an 'unpriv' version of bpf_sys_bpf, which doesn't check the current task's capability. If the owner of the tracing prog wants it to be exclusively used by root users, they can use the 'priv' version of bpf_sys_bpf; if the owner wants it to be usable for non-root users, they can use the 'unpriv' version.

Quoted 7 lines

...

kernel/bpf/syscall.c static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) · line 4616

Quoted 2 lines
kernel/bpf/syscall.c +1 -1
-     if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
+     if (sysctl_unprivileged_bpf_disabled && !bpf_capable() && !uattr.is_kernel)

This is great idea. If I could think of this I would went with it when prog_syscall was introduced.

Thanks!

author
#40 lore raw

PATCH 8/9 bpf: Introduce cgroup iter

In reply to Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
 include/linux/bpf.h            |   1 +
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/Makefile            |   2 +-
 kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |   6 ++
 5 files changed, 155 insertions(+), 1 deletion(-)
 create mode 100644 kernel/bpf/cgroup_iter.c
include/linux/bpf.h +1 -0
index 759ade7b24b3..3ce9b0b7ed89 100644
@@ -1595,6 +1595,7 @@ int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
 struct bpf_iter_aux_info {
 	struct bpf_map *map;
+	u64 cgroup_id;
 };
 
 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
 				struct {
 					__u32 map_id;
 				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
 			};
 		} iter;
 		struct  {
kernel/bpf/Makefile +1 -1
index c1a9be6a4b9f..52a0e4c6e96e 100644
@@ -8,7 +8,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
 
 obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
 obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
+obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o cgroup_iter.o
 obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
 obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o
kernel/bpf/cgroup_iter.c new file +141 -0

141 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
 				struct {
 					__u32 map_id;
 				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
 			};
 		} iter;
 		struct  {
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#41 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Thank you for the patch! Perhaps something to improve:

Hi Hao,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: hexagon-randconfig-r023-20220226 (https://download.01.org/0day-ci/archive/20220226/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project d271fc04d5b97b12e6b797c6067d3c96a8d7470e)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ee74423719e2efb4efa7a4491920c78b60024ec7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615
git checkout ee74423719e2efb4efa7a4491920c78b60024ec7
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash kernel/bpf/

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

All warnings (new ones prefixed by >>):

>> kernel/bpf/cgroup_iter.c:107:39: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
           seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
                                        ~~~     ^~~~~~~~~~~~~~
                                        %llu
>> kernel/bpf/cgroup_iter.c:101:6: warning: no previous prototype for function 'bpf_iter_cgroup_show_fdinfo' [-Wmissing-prototypes]
   void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
        ^
   kernel/bpf/cgroup_iter.c:101:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
   ^
   static 
>> kernel/bpf/cgroup_iter.c:111:5: warning: no previous prototype for function 'bpf_iter_cgroup_fill_link_info' [-Wmissing-prototypes]
   int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
       ^

kernel/bpf/cgroup_iter.c:111:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
^
static
3 warnings generated.

vim +107 kernel/bpf/cgroup_iter.c

100

 > 101	void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
   102					 struct seq_file *seq)
   103	{
   104		char buf[64] = {0};
   105	
   106		cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));
 > 107		seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
   108		seq_printf(seq, "cgroup_path:\t%s\n", buf);
   109	}
   110	
 > 111	int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,

112 struct bpf_link_info *info)
113 {
114 info->iter.cgroup.cgroup_id = aux->cgroup_id;
115 return 0;
116 }
117

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#42 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Hao Luo
Thank you for the patch! Yet something to improve:

Hi Hao,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: m68k-defconfig (https://download.01.org/0day-ci/archive/20220226/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ee74423719e2efb4efa7a4491920c78b60024ec7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615 git checkout ee74423719e2efb4efa7a4491920c78b60024ec7
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=m68k SHELL=/bin/bash

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

All errors (new ones prefixed by >>):

kernel/bpf/cgroup_iter.c: In function 'cgroup_iter_seq_stop':

>> kernel/bpf/cgroup_iter.c:60:17: error: implicit declaration of function 'cgroup_put'; did you mean 'cgroup_psi'? [-Werror=implicit-function-declaration]
      60 |                 cgroup_put(v);
         |                 ^~~~~~~~~~
         |                 cgroup_psi
   kernel/bpf/cgroup_iter.c: At top level:
   kernel/bpf/cgroup_iter.c:101:6: warning: no previous prototype for 'bpf_iter_cgroup_show_fdinfo' [-Wmissing-prototypes]
     101 | void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/cgroup_iter.c: In function 'bpf_iter_cgroup_show_fdinfo':
   kernel/bpf/cgroup_iter.c:107:40: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'u64' {aka 'long long unsigned int'} [-Wformat=]
     107 |         seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
         |                                      ~~^     ~~~~~~~~~~~~~~
         |                                        |        |
         |                                        |        u64 {aka long long unsigned int}
         |                                        long unsigned int
         |                                      %llu

kernel/bpf/cgroup_iter.c: At top level:
kernel/bpf/cgroup_iter.c:111:5: warning: no previous prototype for 'bpf_iter_cgroup_fill_link_info' [-Wmissing-prototypes]
111 | int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +60 kernel/bpf/cgroup_iter.c

    56	
    57	static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
    58	{
    59		if (v)
  > 60			cgroup_put(v);
    61	}
    62	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#43 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Hao Luo
Thank you for the patch! Perhaps something to improve:

Hi Hao,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: arm-randconfig-c002-20220226 (https://download.01.org/0day-ci/archive/20220226/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ee74423719e2efb4efa7a4491920c78b60024ec7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Extend-cgroup-interface-with-bpf/20220226-074615 git checkout ee74423719e2efb4efa7a4491920c78b60024ec7
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arm SHELL=/bin/bash kernel/bpf/

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

All warnings (new ones prefixed by >>):

>> kernel/bpf/cgroup_iter.c:101:6: warning: no previous prototype for 'bpf_iter_cgroup_show_fdinfo' [-Wmissing-prototypes]
     101 | void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/bpf/cgroup_iter.c: In function 'bpf_iter_cgroup_show_fdinfo':
>> kernel/bpf/cgroup_iter.c:107:40: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'u64' {aka 'long long unsigned int'} [-Wformat=]
     107 |         seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
         |                                      ~~^     ~~~~~~~~~~~~~~
         |                                        |        |
         |                                        |        u64 {aka long long unsigned int}
         |                                        long unsigned int
         |                                      %llu
   kernel/bpf/cgroup_iter.c: At top level:
>> kernel/bpf/cgroup_iter.c:111:5: warning: no previous prototype for 'bpf_iter_cgroup_fill_link_info' [-Wmissing-prototypes]

111 | int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/bpf_iter_cgroup_show_fdinfo +101 kernel/bpf/cgroup_iter.c

100

 > 101	void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
   102					 struct seq_file *seq)
   103	{
   104		char buf[64] = {0};
   105	
   106		cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));
 > 107		seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
   108		seq_printf(seq, "cgroup_path:\t%s\n", buf);
   109	}
   110	
 > 111	int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,

112 struct bpf_link_info *info)
113 {
114 info->iter.cgroup.cgroup_id = aux->cgroup_id;
115 return 0;
116 }
117

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#44 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Hao Luo
Quoted 23 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/Makefile            |   2 +-
  kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |   6 ++
  5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

[...]

Quoted 14 lines
kernel/bpf/cgroup_iter.c new file +14 -0
+
+static const struct seq_operations cgroup_iter_seq_ops = {
+	.start  = cgroup_iter_seq_start,
+	.next   = cgroup_iter_seq_next,
+	.stop   = cgroup_iter_seq_stop,
+	.show   = cgroup_iter_seq_show,
+};
+
+BTF_ID_LIST_SINGLE(bpf_cgroup_btf_id, struct, cgroup)
+
+static int cgroup_iter_seq_init(void *priv_data, struct bpf_iter_aux_info *aux)
+{
+	*(u64 *)priv_data = aux->cgroup_id;
+	return 0;

kernel/bpf/cgroup_iter.c line 85

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+}
+
+static void cgroup_iter_seq_fini(void *priv_data)
+{
+}
+
+static const struct bpf_iter_seq_info cgroup_iter_seq_info = {
+	.seq_ops                = &cgroup_iter_seq_ops,
+	.init_seq_private       = cgroup_iter_seq_init,
+	.fini_seq_private       = cgroup_iter_seq_fini,

Since cgroup_iter_seq_fini() is a nop, you can just have
.fini_seq_private = NULL,

Quoted 9 lines
kernel/bpf/cgroup_iter.c new file +9 -0
+	.seq_priv_size          = sizeof(u64),
+};
+
+static int bpf_iter_attach_cgroup(struct bpf_prog *prog,
+				  union bpf_iter_link_info *linfo,
+				  struct bpf_iter_aux_info *aux)
+{
+	aux->cgroup_id = linfo->cgroup.cgroup_id;
+	return 0;

kernel/bpf/cgroup_iter.c line 104

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+}
+
+static void bpf_iter_detach_cgroup(struct bpf_iter_aux_info *aux)
+{
+}
+
+void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
+				 struct seq_file *seq)
+{
+	char buf[64] = {0};

Is this 64 the maximum possible cgroup path length?
If there is a macro for that, I think it would be good to use it.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));

cgroup_path_from_kernfs_id() might fail in which case, buf will be 0. and cgroup_path will be nothing. I guess this might be the expected result. I might be good to add a comment to clarify in the code.

Quoted 8 lines
kernel/bpf/cgroup_iter.c new file +8 -0
+	seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
+	seq_printf(seq, "cgroup_path:\t%s\n", buf);
+}
+
+int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
+				   struct bpf_link_info *info)
+{
+	info->iter.cgroup.cgroup_id = aux->cgroup_id;

kernel/bpf/cgroup_iter.c line 124

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+	return 0;
+}
+
+DEFINE_BPF_ITER_FUNC(cgroup, struct bpf_iter_meta *meta,
+		     struct cgroup *cgroup)
+
+static struct bpf_iter_reg bpf_cgroup_reg_info = {
+	.target			= "cgroup",
+	.attach_target		= bpf_iter_attach_cgroup,
+	.detach_target		= bpf_iter_detach_cgroup,

The same ehre, since bpf_iter_detach_cgroup() is a nop, you can replace it with NULL in the above.

Quoted 16 lines
kernel/bpf/cgroup_iter.c new file +16 -0
+	.show_fdinfo		= bpf_iter_cgroup_show_fdinfo,
+	.fill_link_info		= bpf_iter_cgroup_fill_link_info,
+	.ctx_arg_info_size	= 1,
+	.ctx_arg_info		= {
+		{ offsetof(struct bpf_iter__cgroup, cgroup),
+		  PTR_TO_BTF_ID },
+	},
+	.seq_info		= &cgroup_iter_seq_info,
+};
+
+static int __init bpf_cgroup_iter_init(void)
+{
+	bpf_cgroup_reg_info.ctx_arg_info[0].btf_id = bpf_cgroup_btf_id[0];
+	return bpf_iter_reg_target(&bpf_cgroup_reg_info);
+}
+

[...]

author
#45 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Thanks Yonghong,

Quoted 27 lines · Yonghong Song
Quoted 23 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/Makefile            |   2 +-
  kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |   6 ++
  5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

[...]

Quoted 8 lines

kernel/bpf/cgroup_iter.c line 85

Quoted 4 lines
kernel/bpf/cgroup_iter.c new file +4 -0
+static const struct bpf_iter_seq_info cgroup_iter_seq_info = {
+     .seq_ops                = &cgroup_iter_seq_ops,
+     .init_seq_private       = cgroup_iter_seq_init,
+     .fini_seq_private       = cgroup_iter_seq_fini,

Since cgroup_iter_seq_fini() is a nop, you can just have
.fini_seq_private = NULL,

Sounds good. It looks weird to have .init without .fini. This may indicate a bug somewhere. .attach and .detach the same. I see that you pointed out a bug in a followed reply and the fix has paired attach and detach. That explains something. :)

Quoted 8 lines

kernel/bpf/cgroup_iter.c line 104

Quoted 4 lines
kernel/bpf/cgroup_iter.c new file +4 -0
+void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
+                              struct seq_file *seq)
+{
+     char buf[64] = {0};

Is this 64 the maximum possible cgroup path length?
If there is a macro for that, I think it would be good to use it.

64 is something I made up. There is a macro for path length. Let me use that in v2.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));

cgroup_path_from_kernfs_id() might fail in which case, buf will be 0. and cgroup_path will be nothing. I guess this might be the expected result. I might be good to add a comment to clarify in the code.

No problem.

Quoted 40 lines
Quoted 9 lines
kernel/bpf/cgroup_iter.c new file +8 -0
+     seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
+     seq_printf(seq, "cgroup_path:\t%s\n", buf);
+}
+
+int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
+                                struct bpf_link_info *info)
+{
+     info->iter.cgroup.cgroup_id = aux->cgroup_id;

kernel/bpf/cgroup_iter.c line 124

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+     return 0;
+}
+
+DEFINE_BPF_ITER_FUNC(cgroup, struct bpf_iter_meta *meta,
+                  struct cgroup *cgroup)
+
+static struct bpf_iter_reg bpf_cgroup_reg_info = {
+     .target                 = "cgroup",
+     .attach_target          = bpf_iter_attach_cgroup,
+     .detach_target          = bpf_iter_detach_cgroup,

The same ehre, since bpf_iter_detach_cgroup() is a nop, you can replace it with NULL in the above.

Quoted 16 lines
kernel/bpf/cgroup_iter.c new file +16 -0
+     .show_fdinfo            = bpf_iter_cgroup_show_fdinfo,
+     .fill_link_info         = bpf_iter_cgroup_fill_link_info,
+     .ctx_arg_info_size      = 1,
+     .ctx_arg_info           = {
+             { offsetof(struct bpf_iter__cgroup, cgroup),
+               PTR_TO_BTF_ID },
+     },
+     .seq_info               = &cgroup_iter_seq_info,
+};
+
+static int __init bpf_cgroup_iter_init(void)
+{
+     bpf_cgroup_reg_info.ctx_arg_info[0].btf_id = bpf_cgroup_btf_id[0];
+     return bpf_iter_reg_target(&bpf_cgroup_reg_info);
+}
+

[...]

#46 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Hao Luo
Quoted 129 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
 include/linux/bpf.h            |   1 +
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/Makefile            |   2 +-
 kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |   6 ++
 5 files changed, 155 insertions(+), 1 deletion(-)
 create mode 100644 kernel/bpf/cgroup_iter.c
include/linux/bpf.h +1 -0
index 759ade7b24b3..3ce9b0b7ed89 100644
@@ -1595,6 +1595,7 @@ int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
 struct bpf_iter_aux_info {
 	struct bpf_map *map;
+	u64 cgroup_id;
 };
 
 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
 				struct {
 					__u32 map_id;
 				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
 			};
 		} iter;
 		struct  {
kernel/bpf/Makefile +1 -1
index c1a9be6a4b9f..52a0e4c6e96e 100644
@@ -8,7 +8,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
 
 obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
 obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
+obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o cgroup_iter.o
 obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
 obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o
kernel/bpf/cgroup_iter.c new file +51 -0
new file mode 100644
index 000000000000..011d9dcd1d51
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2022 Google */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/kernel.h>
+#include <linux/seq_file.h>
+
+struct bpf_iter__cgroup {
+	__bpf_md_ptr(struct bpf_iter_meta *, meta);
+	__bpf_md_ptr(struct cgroup *, cgroup);
+};
+
+static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct cgroup *cgroup;
+	u64 cgroup_id;
+
+	/* Only one session is supported. */
+	if (*pos > 0)
+		return NULL;
+
+	cgroup_id = *(u64 *)seq->private;
+	cgroup = cgroup_get_from_id(cgroup_id);
+	if (!cgroup)
+		return NULL;
+
+	if (*pos == 0)
+		++*pos;
+
+	return cgroup;
+}
+
+static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int cgroup_iter_seq_show(struct seq_file *seq, void *v)
+{
+	struct bpf_iter__cgroup ctx;
+	struct bpf_iter_meta meta;
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.meta = &meta;
+	ctx.cgroup = v;
+	meta.seq = seq;
+	prog = bpf_iter_get_info(&meta, false);
+	if (prog)

kernel/bpf/cgroup_iter.c line 61

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+		ret = bpf_iter_run_prog(prog, &ctx);
+
+	return ret;
+}
+
+static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
+{
+	if (v)
+		cgroup_put(v);
+}

I think in existing iterators, we make a final call to seq_show, with v as NULL, is there a specific reason to do it differently for this? There is logic in bpf_iter.c to trigger ->stop() callback again when ->start() or ->next() returns NULL, to execute BPF program with NULL p, see the comment above stop label.

If you do add the seq_show call with NULL, you'd also need to change the ctx_arg_info PTR_TO_BTF_ID to PTR_TO_BTF_ID_OR_NULL.

Quoted 105 lines
kernel/bpf/cgroup_iter.c new file +80 -0
+
+static const struct seq_operations cgroup_iter_seq_ops = {
+	.start  = cgroup_iter_seq_start,
+	.next   = cgroup_iter_seq_next,
+	.stop   = cgroup_iter_seq_stop,
+	.show   = cgroup_iter_seq_show,
+};
+
+BTF_ID_LIST_SINGLE(bpf_cgroup_btf_id, struct, cgroup)
+
+static int cgroup_iter_seq_init(void *priv_data, struct bpf_iter_aux_info *aux)
+{
+	*(u64 *)priv_data = aux->cgroup_id;
+	return 0;
+}
+
+static void cgroup_iter_seq_fini(void *priv_data)
+{
+}
+
+static const struct bpf_iter_seq_info cgroup_iter_seq_info = {
+	.seq_ops                = &cgroup_iter_seq_ops,
+	.init_seq_private       = cgroup_iter_seq_init,
+	.fini_seq_private       = cgroup_iter_seq_fini,
+	.seq_priv_size          = sizeof(u64),
+};
+
+static int bpf_iter_attach_cgroup(struct bpf_prog *prog,
+				  union bpf_iter_link_info *linfo,
+				  struct bpf_iter_aux_info *aux)
+{
+	aux->cgroup_id = linfo->cgroup.cgroup_id;
+	return 0;
+}
+
+static void bpf_iter_detach_cgroup(struct bpf_iter_aux_info *aux)
+{
+}
+
+void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
+				 struct seq_file *seq)
+{
+	char buf[64] = {0};
+
+	cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));
+	seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
+	seq_printf(seq, "cgroup_path:\t%s\n", buf);
+}
+
+int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
+				   struct bpf_link_info *info)
+{
+	info->iter.cgroup.cgroup_id = aux->cgroup_id;
+	return 0;
+}
+
+DEFINE_BPF_ITER_FUNC(cgroup, struct bpf_iter_meta *meta,
+		     struct cgroup *cgroup)
+
+static struct bpf_iter_reg bpf_cgroup_reg_info = {
+	.target			= "cgroup",
+	.attach_target		= bpf_iter_attach_cgroup,
+	.detach_target		= bpf_iter_detach_cgroup,
+	.show_fdinfo		= bpf_iter_cgroup_show_fdinfo,
+	.fill_link_info		= bpf_iter_cgroup_fill_link_info,
+	.ctx_arg_info_size	= 1,
+	.ctx_arg_info		= {
+		{ offsetof(struct bpf_iter__cgroup, cgroup),
+		  PTR_TO_BTF_ID },
+	},
+	.seq_info		= &cgroup_iter_seq_info,
+};
+
+static int __init bpf_cgroup_iter_init(void)
+{
+	bpf_cgroup_reg_info.ctx_arg_info[0].btf_id = bpf_cgroup_btf_id[0];
+	return bpf_iter_reg_target(&bpf_cgroup_reg_info);
+}
+
+late_initcall(bpf_cgroup_iter_init);
tools/include/uapi/linux/bpf.h +6 -1
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
 				struct {
 					__u32 map_id;
 				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
 			};
 		} iter;
 		struct  {
--
Quoted 2 lines

2.35.1.574.g5d30c73bfb-goog

--
Kartikeya

#47 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 129 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/Makefile            |   2 +-
  kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |   6 ++
  5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

include/linux/bpf.h +1 -0
index 759ade7b24b3..3ce9b0b7ed89 100644
@@ -1595,6 +1595,7 @@ int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
  struct bpf_iter_aux_info {
  	struct bpf_map *map;
+	u64 cgroup_id;
  };
 
  typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
  	struct {
  		__u32	map_fd;
  	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
  };
 
  /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
  				struct {
  					__u32 map_id;
  				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
  			};
  		} iter;
  		struct  {
kernel/bpf/Makefile +1 -1
index c1a9be6a4b9f..52a0e4c6e96e 100644
@@ -8,7 +8,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
 
  obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
  obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
+obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o cgroup_iter.o
  obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
  obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
  obj-$(CONFIG_BPF_SYSCALL) += disasm.o
kernel/bpf/cgroup_iter.c new file +51 -0
new file mode 100644
index 000000000000..011d9dcd1d51
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2022 Google */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/kernel.h>
+#include <linux/seq_file.h>
+
+struct bpf_iter__cgroup {
+	__bpf_md_ptr(struct bpf_iter_meta *, meta);
+	__bpf_md_ptr(struct cgroup *, cgroup);
+};
+
+static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct cgroup *cgroup;
+	u64 cgroup_id;
+
+	/* Only one session is supported. */
+	if (*pos > 0)
+		return NULL;
+
+	cgroup_id = *(u64 *)seq->private;
+	cgroup = cgroup_get_from_id(cgroup_id);
+	if (!cgroup)
+		return NULL;
+
+	if (*pos == 0)
+		++*pos;
+
+	return cgroup;
+}
+
+static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int cgroup_iter_seq_show(struct seq_file *seq, void *v)
+{
+	struct bpf_iter__cgroup ctx;
+	struct bpf_iter_meta meta;
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.meta = &meta;
+	ctx.cgroup = v;
+	meta.seq = seq;
+	prog = bpf_iter_get_info(&meta, false);
+	if (prog)

kernel/bpf/cgroup_iter.c line 61

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+		ret = bpf_iter_run_prog(prog, &ctx);
+
+	return ret;
+}
+
+static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
+{
+	if (v)
+		cgroup_put(v);
+}

I think in existing iterators, we make a final call to seq_show, with v as NULL, is there a specific reason to do it differently for this? There is logic in bpf_iter.c to trigger ->stop() callback again when ->start() or ->next() returns NULL, to execute BPF program with NULL p, see the comment above stop label.

Quoted 2 lines

If you do add the seq_show call with NULL, you'd also need to change the ctx_arg_info PTR_TO_BTF_ID to PTR_TO_BTF_ID_OR_NULL.

Kumar, PTR_TO_BTF_ID should be okay since the show() never takes a non-NULL cgroup. But we do have issues for cgroup_iter_seq_stop() which I missed earlier.

For cgroup_iter, the following is the current workflow:
start -> not NULL -> show -> next -> NULL -> stop
or
start -> NULL -> stop

So for cgroup_iter_seq_stop, the input parameter 'v' will be NULL, so the cgroup_put() is not actually called, i.e., corresponding cgroup is not freed.

There are two ways to fix the issue:
. call cgroup_put() in next() before return NULL. This way,
stop() will be a noop.
. put cgroup_get_from_id() and cgroup_put() in
bpf_iter_attach_cgroup() and bpf_iter_detach_cgroup().

I prefer the second approach as it is cleaner.

Quoted 111 lines
Quoted 106 lines
kernel/bpf/cgroup_iter.c new file +80 -0
+
+static const struct seq_operations cgroup_iter_seq_ops = {
+	.start  = cgroup_iter_seq_start,
+	.next   = cgroup_iter_seq_next,
+	.stop   = cgroup_iter_seq_stop,
+	.show   = cgroup_iter_seq_show,
+};
+
+BTF_ID_LIST_SINGLE(bpf_cgroup_btf_id, struct, cgroup)
+
+static int cgroup_iter_seq_init(void *priv_data, struct bpf_iter_aux_info *aux)
+{
+	*(u64 *)priv_data = aux->cgroup_id;
+	return 0;
+}
+
+static void cgroup_iter_seq_fini(void *priv_data)
+{
+}
+
+static const struct bpf_iter_seq_info cgroup_iter_seq_info = {
+	.seq_ops                = &cgroup_iter_seq_ops,
+	.init_seq_private       = cgroup_iter_seq_init,
+	.fini_seq_private       = cgroup_iter_seq_fini,
+	.seq_priv_size          = sizeof(u64),
+};
+
+static int bpf_iter_attach_cgroup(struct bpf_prog *prog,
+				  union bpf_iter_link_info *linfo,
+				  struct bpf_iter_aux_info *aux)
+{
+	aux->cgroup_id = linfo->cgroup.cgroup_id;
+	return 0;
+}
+
+static void bpf_iter_detach_cgroup(struct bpf_iter_aux_info *aux)
+{
+}
+
+void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
+				 struct seq_file *seq)
+{
+	char buf[64] = {0};
+
+	cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));
+	seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
+	seq_printf(seq, "cgroup_path:\t%s\n", buf);
+}
+
+int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
+				   struct bpf_link_info *info)
+{
+	info->iter.cgroup.cgroup_id = aux->cgroup_id;
+	return 0;
+}
+
+DEFINE_BPF_ITER_FUNC(cgroup, struct bpf_iter_meta *meta,
+		     struct cgroup *cgroup)
+
+static struct bpf_iter_reg bpf_cgroup_reg_info = {
+	.target			= "cgroup",
+	.attach_target		= bpf_iter_attach_cgroup,
+	.detach_target		= bpf_iter_detach_cgroup,
+	.show_fdinfo		= bpf_iter_cgroup_show_fdinfo,
+	.fill_link_info		= bpf_iter_cgroup_fill_link_info,
+	.ctx_arg_info_size	= 1,
+	.ctx_arg_info		= {
+		{ offsetof(struct bpf_iter__cgroup, cgroup),
+		  PTR_TO_BTF_ID },
+	},
+	.seq_info		= &cgroup_iter_seq_info,
+};
+
+static int __init bpf_cgroup_iter_init(void)
+{
+	bpf_cgroup_reg_info.ctx_arg_info[0].btf_id = bpf_cgroup_btf_id[0];
+	return bpf_iter_reg_target(&bpf_cgroup_reg_info);
+}
+
+late_initcall(bpf_cgroup_iter_init);
tools/include/uapi/linux/bpf.h +6 -1
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
  	struct {
  		__u32	map_fd;
  	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
  };
 
  /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
  				struct {
  					__u32 map_id;
  				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
  			};
  		} iter;
  		struct  {
--
Quoted 2 lines

2.35.1.574.g5d30c73bfb-goog

--
Kartikeya

#48 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Quoted 149 lines · Yonghong Song
Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 129 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
  include/linux/bpf.h            |   1 +
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/Makefile            |   2 +-
  kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
  tools/include/uapi/linux/bpf.h |   6 ++
  5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

include/linux/bpf.h +1 -0
index 759ade7b24b3..3ce9b0b7ed89 100644
@@ -1595,6 +1595,7 @@ int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
  struct bpf_iter_aux_info {
  	struct bpf_map *map;
+	u64 cgroup_id;
  };
 
  typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
  	struct {
  		__u32	map_fd;
  	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
  };
 
  /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
  				struct {
  					__u32 map_id;
  				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
  			};
  		} iter;
  		struct  {
kernel/bpf/Makefile +1 -1
index c1a9be6a4b9f..52a0e4c6e96e 100644
@@ -8,7 +8,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
 
  obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
  obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
+obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o cgroup_iter.o
  obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
  obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
  obj-$(CONFIG_BPF_SYSCALL) += disasm.o
kernel/bpf/cgroup_iter.c new file +51 -0
new file mode 100644
index 000000000000..011d9dcd1d51
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2022 Google */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/kernel.h>
+#include <linux/seq_file.h>
+
+struct bpf_iter__cgroup {
+	__bpf_md_ptr(struct bpf_iter_meta *, meta);
+	__bpf_md_ptr(struct cgroup *, cgroup);
+};
+
+static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct cgroup *cgroup;
+	u64 cgroup_id;
+
+	/* Only one session is supported. */
+	if (*pos > 0)
+		return NULL;
+
+	cgroup_id = *(u64 *)seq->private;
+	cgroup = cgroup_get_from_id(cgroup_id);
+	if (!cgroup)
+		return NULL;
+
+	if (*pos == 0)
+		++*pos;
+
+	return cgroup;
+}
+
+static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int cgroup_iter_seq_show(struct seq_file *seq, void *v)
+{
+	struct bpf_iter__cgroup ctx;
+	struct bpf_iter_meta meta;
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.meta = &meta;
+	ctx.cgroup = v;
+	meta.seq = seq;
+	prog = bpf_iter_get_info(&meta, false);
+	if (prog)

kernel/bpf/cgroup_iter.c line 61

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+		ret = bpf_iter_run_prog(prog, &ctx);
+
+	return ret;
+}
+
+static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
+{
+	if (v)
+		cgroup_put(v);
+}

I think in existing iterators, we make a final call to seq_show, with v as NULL, is there a specific reason to do it differently for this? There is logic in bpf_iter.c to trigger ->stop() callback again when ->start() or ->next() returns NULL, to execute BPF program with NULL p, see the comment above stop label.

Quoted 7 lines
Quoted 2 lines

If you do add the seq_show call with NULL, you'd also need to change the ctx_arg_info PTR_TO_BTF_ID to PTR_TO_BTF_ID_OR_NULL.

Kumar, PTR_TO_BTF_ID should be okay since the show() never takes a non-NULL cgroup. But we do have issues for cgroup_iter_seq_stop() which I missed earlier.

Right, I was thinking whether it should call seq_show for v == NULL case. All other iterators seem to do so, it's a bit different here since it is only iterating over a single cgroup, I guess, but it would be nice to have some consistency.

Quoted 9 lines

For cgroup_iter, the following is the current workflow:

   start -> not NULL -> show -> next -> NULL -> stop

or
start -> NULL -> stop

So for cgroup_iter_seq_stop, the input parameter 'v' will be NULL, so the cgroup_put() is not actually called, i.e., corresponding cgroup is not freed.

Quoted 8 lines

There are two ways to fix the issue:
. call cgroup_put() in next() before return NULL. This way,
stop() will be a noop.
. put cgroup_get_from_id() and cgroup_put() in
bpf_iter_attach_cgroup() and bpf_iter_detach_cgroup().

I prefer the second approach as it is cleaner.

I think current approach is also not safe if cgroup_id gets reused, right? I.e. it only does cgroup_get_from_id in seq_start, not at attach time, so it may not be the same cgroup when calling read(2). kernfs is using idr_alloc_cyclic, so it is less likely to occur, but since it wraps around to find a free ID it might not be theoretical.

Quoted 103 lines
Quoted 103 lines
Quoted 103 lines
kernel/bpf/cgroup_iter.c new file +80 -0
+
+static const struct seq_operations cgroup_iter_seq_ops = {
+	.start  = cgroup_iter_seq_start,
+	.next   = cgroup_iter_seq_next,
+	.stop   = cgroup_iter_seq_stop,
+	.show   = cgroup_iter_seq_show,
+};
+
+BTF_ID_LIST_SINGLE(bpf_cgroup_btf_id, struct, cgroup)
+
+static int cgroup_iter_seq_init(void *priv_data, struct bpf_iter_aux_info *aux)
+{
+	*(u64 *)priv_data = aux->cgroup_id;
+	return 0;
+}
+
+static void cgroup_iter_seq_fini(void *priv_data)
+{
+}
+
+static const struct bpf_iter_seq_info cgroup_iter_seq_info = {
+	.seq_ops                = &cgroup_iter_seq_ops,
+	.init_seq_private       = cgroup_iter_seq_init,
+	.fini_seq_private       = cgroup_iter_seq_fini,
+	.seq_priv_size          = sizeof(u64),
+};
+
+static int bpf_iter_attach_cgroup(struct bpf_prog *prog,
+				  union bpf_iter_link_info *linfo,
+				  struct bpf_iter_aux_info *aux)
+{
+	aux->cgroup_id = linfo->cgroup.cgroup_id;
+	return 0;
+}
+
+static void bpf_iter_detach_cgroup(struct bpf_iter_aux_info *aux)
+{
+}
+
+void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,
+				 struct seq_file *seq)
+{
+	char buf[64] = {0};
+
+	cgroup_path_from_kernfs_id(aux->cgroup_id, buf, sizeof(buf));
+	seq_printf(seq, "cgroup_id:\t%lu\n", aux->cgroup_id);
+	seq_printf(seq, "cgroup_path:\t%s\n", buf);
+}
+
+int bpf_iter_cgroup_fill_link_info(const struct bpf_iter_aux_info *aux,
+				   struct bpf_link_info *info)
+{
+	info->iter.cgroup.cgroup_id = aux->cgroup_id;
+	return 0;
+}
+
+DEFINE_BPF_ITER_FUNC(cgroup, struct bpf_iter_meta *meta,
+		     struct cgroup *cgroup)
+
+static struct bpf_iter_reg bpf_cgroup_reg_info = {
+	.target			= "cgroup",
+	.attach_target		= bpf_iter_attach_cgroup,
+	.detach_target		= bpf_iter_detach_cgroup,
+	.show_fdinfo		= bpf_iter_cgroup_show_fdinfo,
+	.fill_link_info		= bpf_iter_cgroup_fill_link_info,
+	.ctx_arg_info_size	= 1,
+	.ctx_arg_info		= {
+		{ offsetof(struct bpf_iter__cgroup, cgroup),
+		  PTR_TO_BTF_ID },
+	},
+	.seq_info		= &cgroup_iter_seq_info,
+};
+
+static int __init bpf_cgroup_iter_init(void)
+{
+	bpf_cgroup_reg_info.ctx_arg_info[0].btf_id = bpf_cgroup_btf_id[0];
+	return bpf_iter_reg_target(&bpf_cgroup_reg_info);
+}
+
+late_initcall(bpf_cgroup_iter_init);
tools/include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
  	struct {
  		__u32	map_fd;
  	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
  };
 
  /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
  				struct {
  					__u32 map_id;
  				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
  			};
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
tools/include/uapi/linux/bpf.h +0 -1
  		} iter;
  		struct  {
--

2.35.1.574.g5d30c73bfb-goog

--
Kartikeya

--
Kartikeya

#49 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Quoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 1 line

I think current approach is also not safe if cgroup_id gets reused, right? I.e. it only does cgroup_get_from_id in seq_start, not at attach time, so it may not be the same cgroup when calling read(2). kernfs is using idr_alloc_cyclic, so it is less likely to occur, but since it wraps around to find a free ID it might not be theoretical.

cgroupid is 64-bit.

#50 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Kumar Kartikeya Dwivedi
Quoted 158 lines · Kumar Kartikeya Dwivedi
Quoted 149 lines · Yonghong Song
Quoted 146 lines · Kumar Kartikeya Dwivedi
Quoted 129 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
   include/linux/bpf.h            |   1 +
   include/uapi/linux/bpf.h       |   6 ++
   kernel/bpf/Makefile            |   2 +-
   kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
   tools/include/uapi/linux/bpf.h |   6 ++
   5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

include/linux/bpf.h +1 -0
index 759ade7b24b3..3ce9b0b7ed89 100644
@@ -1595,6 +1595,7 @@ int bpf_obj_get_path(bpfptr_t pathname, int flags);
 
   struct bpf_iter_aux_info {
   	struct bpf_map *map;
+	u64 cgroup_id;
   };
 
   typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index a5dbc794403d..855ad80d9983 100644
@@ -91,6 +91,9 @@ union bpf_iter_link_info {
   	struct {
   		__u32	map_fd;
   	} map;
+	struct {
+		__u64	cgroup_id;
+	} cgroup;
   };
 
   /* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -5887,6 +5890,9 @@ struct bpf_link_info {
   				struct {
   					__u32 map_id;
   				} map;
+				struct {
+					__u64 cgroup_id;
+				} cgroup;
   			};
   		} iter;
   		struct  {
kernel/bpf/Makefile +1 -1
index c1a9be6a4b9f..52a0e4c6e96e 100644
@@ -8,7 +8,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
 
   obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
   obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
-obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
+obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o cgroup_iter.o
   obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
   obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
   obj-$(CONFIG_BPF_SYSCALL) += disasm.o
kernel/bpf/cgroup_iter.c new file +51 -0
new file mode 100644
index 000000000000..011d9dcd1d51
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2022 Google */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/kernel.h>
+#include <linux/seq_file.h>
+
+struct bpf_iter__cgroup {
+	__bpf_md_ptr(struct bpf_iter_meta *, meta);
+	__bpf_md_ptr(struct cgroup *, cgroup);
+};
+
+static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	struct cgroup *cgroup;
+	u64 cgroup_id;
+
+	/* Only one session is supported. */
+	if (*pos > 0)
+		return NULL;
+
+	cgroup_id = *(u64 *)seq->private;
+	cgroup = cgroup_get_from_id(cgroup_id);
+	if (!cgroup)
+		return NULL;
+
+	if (*pos == 0)
+		++*pos;
+
+	return cgroup;
+}
+
+static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int cgroup_iter_seq_show(struct seq_file *seq, void *v)
+{
+	struct bpf_iter__cgroup ctx;
+	struct bpf_iter_meta meta;
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.meta = &meta;
+	ctx.cgroup = v;
+	meta.seq = seq;
+	prog = bpf_iter_get_info(&meta, false);
+	if (prog)

kernel/bpf/cgroup_iter.c line 61

Quoted 10 lines
kernel/bpf/cgroup_iter.c new file +10 -0
+		ret = bpf_iter_run_prog(prog, &ctx);
+
+	return ret;
+}
+
+static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
+{
+	if (v)
+		cgroup_put(v);
+}

I think in existing iterators, we make a final call to seq_show, with v as NULL, is there a specific reason to do it differently for this? There is logic in bpf_iter.c to trigger ->stop() callback again when ->start() or ->next() returns NULL, to execute BPF program with NULL p, see the comment above stop label.

Quoted 7 lines
Quoted 2 lines

If you do add the seq_show call with NULL, you'd also need to change the ctx_arg_info PTR_TO_BTF_ID to PTR_TO_BTF_ID_OR_NULL.

Kumar, PTR_TO_BTF_ID should be okay since the show() never takes a non-NULL cgroup. But we do have issues for cgroup_iter_seq_stop() which I missed earlier.

Quoted 4 lines

Right, I was thinking whether it should call seq_show for v == NULL case. All other iterators seem to do so, it's a bit different here since it is only iterating over a single cgroup, I guess, but it would be nice to have some consistency.

You are correct that I think it is okay since it only iterates with one
cgroup. This is different from other cases so far where more than one objects may be traversed. We may have future other use cases, e.g., one task. I think we can abstract out start()/next()/stop() callbacks
for such use cases. So it is okay it is different from other existing
iterators since they are indeed different.

Quoted 16 lines
Quoted 16 lines

For cgroup_iter, the following is the current workflow:

    start -> not NULL -> show -> next -> NULL -> stop
or
    start -> NULL -> stop

So for cgroup_iter_seq_stop, the input parameter 'v' will be NULL, so the cgroup_put() is not actually called, i.e., corresponding cgroup is not freed.

There are two ways to fix the issue:
. call cgroup_put() in next() before return NULL. This way,
stop() will be a noop.
. put cgroup_get_from_id() and cgroup_put() in
bpf_iter_attach_cgroup() and bpf_iter_detach_cgroup().

Quoted 8 lines
Quoted 2 lines

I prefer the second approach as it is cleaner.

I think current approach is also not safe if cgroup_id gets reused, right? I.e. it only does cgroup_get_from_id in seq_start, not at attach time, so it may not be the same cgroup when calling read(2). kernfs is using idr_alloc_cyclic, so it is less likely to occur, but since it wraps around to find a free ID it might not be theoretical.

As Alexei mentioned, cgroup id is 64-bit, the collision should be nearly impossible. Another option is to get a fd from the cgroup path, and send the fd to the kernel. This probably works.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
kernel/bpf/cgroup_iter.c new file +16 -0
+
+static const struct seq_operations cgroup_iter_seq_ops = {
+	.start  = cgroup_iter_seq_start,
+	.next   = cgroup_iter_seq_next,
+	.stop   = cgroup_iter_seq_stop,
+	.show   = cgroup_iter_seq_show,
+};
+
+BTF_ID_LIST_SINGLE(bpf_cgroup_btf_id, struct, cgroup)
+
+static int cgroup_iter_seq_init(void *priv_data, struct bpf_iter_aux_info *aux)
+{
+	*(u64 *)priv_data = aux->cgroup_id;
+	return 0;
+}
+

[...]

#51 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

Quoted 39 lines · Yonghong Song

[...]

Quoted 5 lines

Right, I was thinking whether it should call seq_show for v == NULL case. All other iterators seem to do so, it's a bit different here since it is only iterating over a single cgroup, I guess, but it would be nice to have some consistency.

You are correct that I think it is okay since it only iterates with one cgroup. This is different from other cases so far where more than one objects may be traversed. We may have future other use cases, e.g., one task. I think we can abstract out start()/next()/stop() callbacks for such use cases. So it is okay it is different from other existing iterators since they are indeed different.

Quoted 16 lines
Quoted 16 lines

For cgroup_iter, the following is the current workflow:

    start -> not NULL -> show -> next -> NULL -> stop
or
    start -> NULL -> stop

So for cgroup_iter_seq_stop, the input parameter 'v' will be NULL, so the cgroup_put() is not actually called, i.e., corresponding cgroup is not freed.

There are two ways to fix the issue:
. call cgroup_put() in next() before return NULL. This way,
stop() will be a noop.
. put cgroup_get_from_id() and cgroup_put() in
bpf_iter_attach_cgroup() and bpf_iter_detach_cgroup().

Quoted 8 lines
Quoted 2 lines

I prefer the second approach as it is cleaner.

I think current approach is also not safe if cgroup_id gets reused, right? I.e. it only does cgroup_get_from_id in seq_start, not at attach time, so it may not be the same cgroup when calling read(2). kernfs is using idr_alloc_cyclic, so it is less likely to occur, but since it wraps around to find a free ID it might not be theoretical.

Quoted 5 lines

As Alexei mentioned, cgroup id is 64-bit, the collision should be nearly impossible. Another option is to get a fd from the cgroup path, and send the fd to the kernel. This probably works.

I see, even on 32-bit systems the actual id is 64-bit.
As for cgroup fd vs id, existing cgroup BPF programs seem to take fd, map iter also takes map fd, so it might make sense to use cgroup fd here as well.

Quoted 1 line

[...]

--
Kartikeya

author
#52 lore raw

Re: [PATCH bpf-next v1 8/9] bpf: Introduce cgroup iter

In reply to Yonghong Song
Quoted 31 lines · Yonghong Song
Quoted 27 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Yonghong Song
Quoted 23 lines · Kumar Kartikeya Dwivedi
Quoted 22 lines · Hao Luo

Introduce a new type of iter prog: cgroup. Unlike other bpf_iter, this iter doesn't iterate a set of kernel objects. Instead, it is supposed to be parameterized by a cgroup id and prints only that cgroup. So one needs to specify a target cgroup id when attaching this iter.

The target cgroup's state can be read out via a link of this iter. Typically, we can monitor cgroup creation and deletion using sleepable tracing and use it to create corresponding directories in bpffs and pin a cgroup id parameterized link in the directory. Then we can read the auto-pinned iter link to get cgroup's state. The output of the iter link is determined by the program. See the selftest test_cgroup_stats.c for an example.

---
   include/linux/bpf.h            |   1 +
   include/uapi/linux/bpf.h       |   6 ++
   kernel/bpf/Makefile            |   2 +-
   kernel/bpf/cgroup_iter.c       | 141 +++++++++++++++++++++++++++++++++
   tools/include/uapi/linux/bpf.h |   6 ++
   5 files changed, 155 insertions(+), 1 deletion(-)

create mode 100644 kernel/bpf/cgroup_iter.c

[...]

Quoted 19 lines
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines

I think in existing iterators, we make a final call to seq_show, with v as NULL, is there a specific reason to do it differently for this? There is logic in bpf_iter.c to trigger ->stop() callback again when ->start() or ->next() returns NULL, to execute BPF program with NULL p, see the comment above stop label.

Quoted 7 lines
Quoted 2 lines

If you do add the seq_show call with NULL, you'd also need to change the ctx_arg_info PTR_TO_BTF_ID to PTR_TO_BTF_ID_OR_NULL.

Kumar, PTR_TO_BTF_ID should be okay since the show() never takes a non-NULL cgroup. But we do have issues for cgroup_iter_seq_stop() which I missed earlier.

Quoted 4 lines

Right, I was thinking whether it should call seq_show for v == NULL case. All other iterators seem to do so, it's a bit different here since it is only iterating over a single cgroup, I guess, but it would be nice to have some consistency.

Quoted 7 lines

You are correct that I think it is okay since it only iterates with one cgroup. This is different from other cases so far where more than one objects may be traversed. We may have future other use cases, e.g., one task. I think we can abstract out start()/next()/stop() callbacks for such use cases. So it is okay it is different from other existing iterators since they are indeed different.

Right. This iter is special. It has a single element. So we don't really need preamble and epilogue, which can directly be coded up in the iter program. And we can also guarantee the cgroup passed is always valid, otherwise we wouldn't invoke show(). So passing PTR_TO_BTF_ID is fine. I did so mainly in order to save a null check inside the prog.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

For cgroup_iter, the following is the current workflow:

    start -> not NULL -> show -> next -> NULL -> stop
or
    start -> NULL -> stop

So for cgroup_iter_seq_stop, the input parameter 'v' will be NULL, so the cgroup_put() is not actually called, i.e., corresponding cgroup is not freed.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

There are two ways to fix the issue:
. call cgroup_put() in next() before return NULL. This way,
stop() will be a noop.
. put cgroup_get_from_id() and cgroup_put() in
bpf_iter_attach_cgroup() and bpf_iter_detach_cgroup().

I prefer the second approach as it is cleaner.

Yeah, the second approach should be fine. I was thinking of holding the cgroup's reference only when we actually start reading, so that a cgroup can go at any time and this iter gets a reference only in best effort. Now a reference is held from attach to detach, but I think it should be fine. Let me test.

Quoted 7 lines
Quoted 6 lines

I think current approach is also not safe if cgroup_id gets reused, right? I.e. it only does cgroup_get_from_id in seq_start, not at attach time, so it may not be the same cgroup when calling read(2). kernfs is using idr_alloc_cyclic, so it is less likely to occur, but since it wraps around to find a free ID it might not be theoretical.

Quoted 5 lines

As Alexei mentioned, cgroup id is 64-bit, the collision should be nearly impossible. Another option is to get a fd from the cgroup path, and send the fd to the kernel. This probably works.

64bit cgroup id should be fine. Using cgroup path and fd is more complicated, unnecessarily IMHO.

Quoted 1 line

[...]

author
#53 lore raw

PATCH 9/9 selftests/bpf: Tests using sleepable tracepoints to monitor cgroup events

In reply to Hao Luo

Tests the functionalities of sleepable tracing prog, sleepable tracepoints (i.e. cgroup_mkdir_s and cgroup_rmdir_s) and cgroup iter prog all together.

The added selftest resembles a real-world application, where bpf is used to export cgroup-level performance stats. There are two sets of progs in the test: cgroup_monitor and cgroup_sched_lat

- Cgroup_monitor monitors cgroup creation and deletion using sleepable tracing; for each cgroup created, creates a directory in bpffs; creates a cgroup iter link and pins it in that directory.

- Cgroup_sched_lat is the program that collects cgroup's scheduling latencies and store them in hash map. Cgroup_sched_lat implements a cgroup iter prog, which reads the stats from the map and seq_prints them. This cgroup iter prog is the prog pinned by cgroup_monitor in each bpffs directory.

The cgroup_sched_lat in this test can be adapted for exporting similar cgroup-level performance stats.

---
 .../bpf/prog_tests/test_cgroup_stats.c        | 187 ++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_iter.h  |   7 +
 .../selftests/bpf/progs/cgroup_monitor.c      |  78 ++++++
 .../selftests/bpf/progs/cgroup_sched_lat.c    | 232 ++++++++++++++++++
 4 files changed, 504 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_cgroup_stats.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_monitor.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_sched_lat.c
tools/testing/selftests/bpf/prog_tests/test_cgroup_stats.c new file +187 -0
new file mode 100644
index 000000000000..b6607ac074bc
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Google */
+#define _GNU_SOURCE
+#include <sys/stat.h>	/* mkdir */
+#include <fcntl.h>	/* name_to_handle_at */
+#include <stdlib.h>
+#include <test_progs.h>
+#include "cgroup_monitor.skel.h"
+#include "cgroup_sched_lat.skel.h"
+
+static char mkdir_prog_path[64];
+static char rmdir_prog_path[64];
+static char dump_prog_path[64];
+
+/* Get cgroup id from a full path to cgroup */
+static int get_cgroup_id(const char *cgroup)
+{
+	int mount_id = 0;
+	struct {
+		struct file_handle fh;
+		__u64 cgid;
+	} fh = {};
+
+	fh.fh.handle_bytes = sizeof(fh.cgid);
+	if (name_to_handle_at(AT_FDCWD, cgroup, &fh.fh, &mount_id, 0))
+		return -1;
+
+	return fh.cgid;
+}
+
+static void spin_on_cpu(int seconds)
+{
+	time_t start, now;
+
+	start = time(NULL);
+	do {
+		now = time(NULL);
+	} while (now - start < seconds);
+}
+
+static void do_work(const char *cgroup)
+{
+	int i, cpu = 0, pid;
+	char cmd[128];
+
+	/* make cgroup threaded */
+	snprintf(cmd, 128, "echo threaded > %s/cgroup.type", cgroup);
+	system(cmd);
+
+	/* try to enable cpu controller. this may fail if cpu controller is not
+	 * available in cgroup.controllers or there is a cgroup v1 already
+	 * mounted in the system.
+	 */
+	snprintf(cmd, 128, "echo \"+cpu\" > %s/cgroup.subtree_control", cgroup);
+	system(cmd);
+
+	/* launch two children, both running in child cgroup */
+	for (i = 0; i < 2; ++i) {
+		pid = fork();
+		if (pid == 0) {
+			/* attach to cgroup */
+			snprintf(cmd, 128, "echo %d > %s/cgroup.procs", getpid(), cgroup);
+			system(cmd);
+
+			/* pin process to target cpu */
+			snprintf(cmd, 128, "taskset -pc %d %d", cpu, getpid());
+			system(cmd);
+
+			spin_on_cpu(3); /* spin on cpu for 3 seconds */
+			exit(0);
+		}
+	}
+
+	/* pin parent process to target cpu */
+	snprintf(cmd, 128, "taskset -pc %d %d", cpu, getpid());
+	system(cmd);
+
+	spin_on_cpu(3); /* spin on cpu for 3 seconds */
+	wait(NULL);
+}
+
+/* Check reading cgroup stats from auto pinned objects
+ * @root: root directory in bpffs set up for this test
+ * @cgroup: cgroup path
+ */
+static void check_cgroup_stats(const char *root, const char *cgroup)
+{
+	unsigned long queue_self, queue_other;
+	char buf[64], path[64];
+	int id, cgroup_id;
+	FILE *file;
+
+	id = get_cgroup_id(cgroup);
+	if (!ASSERT_GE(id, 0, "get_cgroup_id"))
+		return;
+
+	snprintf(path, sizeof(path), "%s/%d/stats", root, id);
+	file = fopen(path, "r");
+	if (!ASSERT_OK_PTR(file, "open"))
+		return;
+
+	ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat");
+	ASSERT_EQ(sscanf(buf, "cgroup_id: %8d", &cgroup_id), 1, "output");
+	ASSERT_EQ(id, cgroup_id, "cgroup_id");
+
+	ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat");
+	ASSERT_EQ(sscanf(buf, "queue_self: %8lu", &queue_self), 1, "output");
+
+	ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat");
+	ASSERT_EQ(sscanf(buf, "queue_other: %8lu", &queue_other), 1, "output");
+	fclose(file);
+}
+
+/* Set up bpf progs for monitoring cgroup activities. */
+static void setup_cgroup_monitor(const char *root)
+{
+	struct cgroup_monitor *skel = NULL;
+
+	skel = cgroup_monitor__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "cgroup_monitor_skel_load"))
+		return;
+
+	cgroup_monitor__attach(skel);
+
+	snprintf(skel->bss->root, sizeof(skel->bss->root), "%s", root);
+
+	snprintf(mkdir_prog_path, 64, "%s/mkdir_prog", root);
+	bpf_obj_pin(bpf_link__fd(skel->links.mkdir_prog), mkdir_prog_path);
+
+	snprintf(rmdir_prog_path, 64, "%s/rmdir_prog", root);
+	bpf_obj_pin(bpf_link__fd(skel->links.rmdir_prog), rmdir_prog_path);
+
+	cgroup_monitor__destroy(skel);
+}
+
+void test_cgroup_stats(void)
+{
+	char bpf_tmpl[] = "/sys/fs/bpf/XXXXXX";
+	char cgrp_tmpl[] = "/sys/fs/cgroup/XXXXXX";
+	struct cgroup_sched_lat *skel = NULL;
+	char *root, *cgroup;
+
+	/* prepare test directories */
+	system("mount -t cgroup2 none /sys/fs/cgroup");
+	system("mount -t bpf bpffs /sys/fs/bpf");
+	root = mkdtemp(bpf_tmpl);
+	chmod(root, 0777);
+
+	/* set up progs for monitoring cgroup events */
+	setup_cgroup_monitor(root);
+
+	/* set up progs for profiling cgroup stats */
+	skel = cgroup_sched_lat__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "cgroup_sched_lat_skel_load"))
+		goto cleanup_root;
+
+	snprintf(dump_prog_path, 64, "%s/prog", root);
+	bpf_obj_pin(bpf_program__fd(skel->progs.dump_cgroup), dump_prog_path);
+	chmod(dump_prog_path, 0644);
+
+	cgroup_sched_lat__attach(skel);
+
+	/* thanks to cgroup monitoring progs, a directory corresponding to the
+	 * cgroup is created in bpffs.
+	 */
+	cgroup = mkdtemp(cgrp_tmpl);
+
+	/* collect some cgroup-level stats and check reading them from bpffs */
+	do_work(cgroup);
+	check_cgroup_stats(root, cgroup);
+
+	/* thanks to cgroup monitoring progs, removing cgroups also removes
+	 * the created directory in bpffs.
+	 */
+	rmdir(cgroup);
+
+	/* clean up cgroup monitoring progs */
+	cgroup_sched_lat__detach(skel);
+	cgroup_sched_lat__destroy(skel);
+	unlink(dump_prog_path);
+cleanup_root:
+	/* remove test directories in bpffs */
+	unlink(mkdir_prog_path);
+	unlink(rmdir_prog_path);
+	rmdir(root);
+	return;
+}
tools/testing/selftests/bpf/progs/bpf_iter.h +7 -0
index 8cfaeba1ddbf..0d1bf954e831 100644
@@ -16,6 +16,7 @@
 #define bpf_iter__bpf_map_elem bpf_iter__bpf_map_elem___not_used
 #define bpf_iter__bpf_sk_storage_map bpf_iter__bpf_sk_storage_map___not_used
 #define bpf_iter__sockmap bpf_iter__sockmap___not_used
+#define bpf_iter__cgroup bpf_iter__cgroup__not_used
 #define btf_ptr btf_ptr___not_used
 #define BTF_F_COMPACT BTF_F_COMPACT___not_used
 #define BTF_F_NONAME BTF_F_NONAME___not_used
@@ -37,6 +38,7 @@
 #undef bpf_iter__bpf_map_elem
 #undef bpf_iter__bpf_sk_storage_map
 #undef bpf_iter__sockmap
+#undef bpf_iter__cgroup
 #undef btf_ptr
 #undef BTF_F_COMPACT
 #undef BTF_F_NONAME
@@ -132,6 +134,11 @@ struct bpf_iter__sockmap {
 	struct sock *sk;
 };
 
+struct bpf_iter__cgroup {
+	struct bpf_iter_meta *meta;
+	struct cgroup *cgroup;
+} __attribute__((preserve_access_index));
+
 struct btf_ptr {
 	void *ptr;
 	__u32 type_id;
tools/testing/selftests/bpf/progs/cgroup_monitor.c new file +78 -0

78 lines not on this page · raw message

tools/testing/selftests/bpf/progs/cgroup_sched_lat.c new file +232 -0

232 lines not on this page · raw message

Signature · 1 line
2.35.1.574.g5d30c73bfb-goog