bpf: Introduce cgroup_task iter
Subsystems (5)
cover bpf: Introduce cgroup_task iter
This patch introduces cgroup_task iter, which allows for efficient iteration of tasks within a specific cgroup. For example, we can effiently get the nr_{running,blocked} of a container with this new feature.
The cgroup_task iteration serves as an alternative to task_iter in container environments due to certain limitations associated with task_iter.
- Firstly, task_iter only supports the 'current' pidns. However, since our data collector operates on the host, we may need to collect information from multiple containers simultaneously. Using task_iter would require us to fork the collector for each container, which is not ideal.
- Additionally, task_iter is unable to collect task information from containers running in the host pidns.
In our container environment, we have containers running in the host pidns, and we would like to collect task information from them as well.
- Lastly, task_iter does not support multiple-container pods. In a Kubernetes environment, a single pod may contain multiple containers, all sharing the same pidns. However, we are only interested in iterating tasks within the main container, which is not possible with task_iter.
To address the first issue, we could potentially extend task_iter to support specifying a pidns other than the current one. However, for the other two issues, extending task_iter would not provide a solution. Therefore, we believe it is preferable to introduce the cgroup_task iter to handle these scenarios effectively.
Patch #1: Preparation
Patch #2: Add cgroup_task iter
Patch #3: Add support for cgroup_task iter in bpftool Patch #4: Selftests for cgroup_task iter
Yafang Shao (4):
bpf: Add __bpf_iter_attach_cgroup()
bpf: Add cgroup_task iter
bpftool: Add support for cgroup_task selftests/bpf: Add selftest for cgroup_task iter
include/linux/btf_ids.h | 14 ++ kernel/bpf/cgroup_iter.c | 181 ++++++++++++++-- tools/bpf/bpftool/link.c | 3 +- .../bpf/prog_tests/cgroup_task_iter.c | 197 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_task_iter.c | 39 ++++ 5 files changed, 419 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_task_iter.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_task_iter.c
Signature · 1 line
2.39.3
RFC PATCH 1/4 bpf: Add __bpf_iter_attach_cgroup()
This is a preparation for the followup patch. No functional change.
--- kernel/bpf/cgroup_iter.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
kernel/bpf/cgroup_iter.c +19 -11
@@ -191,21 +191,14 @@ static const struct bpf_iter_seq_info cgroup_iter_seq_info = { .seq_priv_size = sizeof(struct cgroup_iter_priv), }; -static int bpf_iter_attach_cgroup (struct bpf_prog *prog, -union bpf_iter_link_info *linfo, -struct bpf_iter_aux_info *aux) +static int __bpf_iter_attach_cgroup (struct bpf_prog *prog, +union bpf_iter_link_info *linfo, +struct bpf_iter_aux_info *aux) {int fd = linfo->cgroup.cgroup_fd;u64 id = linfo->cgroup.cgroup_id; -int order = linfo->cgroup.order;struct cgroup *cgrp; -if (order !=BPF_CGROUP_ITER_DESCENDANTS_PRE && - order !=BPF_CGROUP_ITER_DESCENDANTS_POST && - order !=BPF_CGROUP_ITER_ANCESTORS_UP && - order !=BPF_CGROUP_ITER_SELF_ONLY ) -return -EINVAL ; -if (fd && id)return -EINVAL ;
@@ -220,10 +213,25 @@ static int bpf_iter_attach_cgroup(struct bpf_prog *prog,return PTR_ERR (cgrp); aux->cgroup.start = cgrp; - aux->cgroup.order = order;return 0 ; } +static int bpf_iter_attach_cgroup (struct bpf_prog *prog, +union bpf_iter_link_info *linfo, +struct bpf_iter_aux_info *aux) +{ +int order = linfo->cgroup.order; + +if (order !=BPF_CGROUP_ITER_DESCENDANTS_PRE && + order !=BPF_CGROUP_ITER_DESCENDANTS_POST && + order !=BPF_CGROUP_ITER_ANCESTORS_UP && + order !=BPF_CGROUP_ITER_SELF_ONLY ) +return -EINVAL ; + + aux->cgroup.order = order; +return __bpf_iter_attach_cgroup (prog, linfo, aux); +} +static void bpf_iter_detach_cgroup (struct bpf_iter_aux_info *aux) {cgroup_put (aux->cgroup.start);
Signature · 1 line
2.39.3
RFC PATCH 2/4 bpf: Add cgroup_task iter
In reply to Yafang ShaoThis patch introduces cgroup_task iter, which allows for efficient iteration of tasks within a specific cgroup. For example, we can effiently get the nr_{running,blocked} of a container with this new feature.
The cgroup_task iteration serves as an alternative to task_iter in container environments due to certain limitations associated with task_iter.
- Firstly, task_iter only supports the 'current' pidns. However, since our data collector operates on the host, we may need to collect information from multiple containers simultaneously. Using task_iter would require us to fork the collector for each container, which is not ideal.
- Additionally, task_iter is unable to collect task information from containers running in the host pidns.
In our container environment, we have containers running in the host pidns, and we would like to collect task information from them as well.
- Lastly, task_iter does not support multiple-container pods. In a Kubernetes environment, a single pod may contain multiple containers, all sharing the same pidns. However, we are only interested in iterating tasks within the main container, which is not possible with task_iter.
To address the first issue, we could potentially extend task_iter to support specifying a pidns other than the current one. However, for the other two issues, extending task_iter would not provide a solution. Therefore, we believe it is preferable to introduce the cgroup_task iter to handle these scenarios effectively.
--- include/linux/btf_ids.h | 14 ++++ kernel/bpf/cgroup_iter.c | 151 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 162 insertions(+), 3 deletions(-)
include/linux/btf_ids.h +14 -0
@@ -265,6 +265,20 @@ MAX_BTF_TRACING_TYPE, };extern u32 btf_tracing_ids[]; + +#ifdef CONFIG_CGROUPS +#define BTF_CGROUP_TYPE_xxx \ + BTF_CGROUP_TYPE(BTF_CGROUP_TYPE_CGROUP, cgroup) \ + BTF_CGROUP_TYPE(BTF_CGROUP_TYPE_TASK, task_struct) + +enum { +#define BTF_CGROUP_TYPE(name, type) name, +BTF_CGROUP_TYPE_xxx +#undef BTF_CGROUP_TYPE +MAX_BTF_CGROUP_TYPE , +}; +#endif +extern u32 bpf_cgroup_btf_id[];extern u32 bpf_local_storage_map_btf_id[];
kernel/bpf/cgroup_iter.c +148 -3
165 lines not on this page · raw message
Signature · 1 line
2.39.3
RFC PATCH 3/4 bpftool: Add support for cgroup_task
In reply to Yafang Shaowe need to make corresponding changes to the bpftool to support cgroup_task iter.
The result:
$ bpftool link show 3: iter prog 15 target_name cgroup_task cgroup_id 7427 order self_only
--- tools/bpf/bpftool/link.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/bpf/bpftool/link.c +2 -1
@@ -158,7 +158,8 @@ static bool is_iter_map_target(const char *target_name)static bool is_iter_cgroup_target (const char *target_name) { -return strcmp (target_name,"cgroup" ) ==0 ; +return strcmp (target_name,"cgroup" ) ==0 || +strcmp (target_name,"cgroup_task" ) ==0 ; }static const char *cgroup_order_string (__u32 order)
Signature · 1 line
2.39.3
RFC PATCH 4/4 selftests/bpf: Add selftest for cgroup_task iter
In reply to Yafang ShaoAdd selftests for the newly introduced cgroup_task iter.
The result:
#42/1 cgroup_task_iter/cgroup_task_iter__invalid_order:OK #42/2 cgroup_task_iter/cgroup_task_iter__no_task:OK #42/3 cgroup_task_iter/cgroup_task_iter__task_pid:OK #42/4 cgroup_task_iter/cgroup_task_iter__task_cnt:OK #42 cgroup_task_iter:OK
Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/prog_tests/cgroup_task_iter.c | 197 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_task_iter.c | 39 ++++ 2 files changed, 236 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_task_iter.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_task_iter.c
tools/testing/selftests/bpf/prog_tests/cgroup_task_iter.c new file +197 -0
@@ -0,0 +1,197 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Yafang Shao <[email protected]> */ + +#include <signal.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <test_progs.h> +#include <bpf/libbpf.h> +#include <bpf/btf.h> +#include "cgroup_helpers.h" +#include "cgroup_task_iter.skel.h" + +#define PID_CNT (2) +static char expected_output[128 ]; + +static void read_from_cgroup_iter (struct bpf_program *prog,int cgroup_fd, +int order,const char *testname) +{ +DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts, opts); +union bpf_iter_link_info linfo; +struct bpf_link *link; +int len, iter_fd; +static char buf[128 ]; +size_t left; +char *p; + +memset (&linfo,0 ,sizeof (linfo)); + linfo.cgroup.cgroup_fd = cgroup_fd; + linfo.cgroup.order = order; + opts.link_info = &linfo; + opts.link_info_len =sizeof (linfo); + + link =bpf_program__attach_iter (prog, &opts); +if (!ASSERT_OK_PTR (link,"attach_iter" )) +return ; + + iter_fd =bpf_iter_create (bpf_link__fd (link)); +if (iter_fd <0 ) + goto free_link; + +memset (buf,0 ,sizeof (buf)); + left =ARRAY_SIZE (buf); + p = buf; +while ((len =read (iter_fd, p, left)) >0 ) { + p += len; + left -= len; + } + +ASSERT_STREQ (buf, expected_output, testname); + +/* read() after iter finishes should be ok. */ +if (len ==0 ) +ASSERT_OK (read (iter_fd, buf,sizeof (buf)),"second_read" ); + +close (iter_fd); +free_link: +bpf_link__destroy (link); +} + +/* Invalid walk order */ +static void test_invalid_order (struct cgroup_task_iter *skel,int fd) +{ +DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts, opts); +enum bpf_cgroup_iter_order order; +union bpf_iter_link_info linfo; +struct bpf_link *link; + +memset (&linfo,0 ,sizeof (linfo)); + linfo.cgroup.cgroup_fd = fd; + opts.link_info = &linfo; + opts.link_info_len =sizeof (linfo); + +/* Only BPF_CGROUP_ITER_SELF_ONLY is supported */ +for (order =0 ; order <=BPF_CGROUP_ITER_ANCESTORS_UP ; order++) { +if (order ==BPF_CGROUP_ITER_SELF_ONLY ) +continue ; + linfo.cgroup.order = order; + link =bpf_program__attach_iter (skel->progs.cgroup_task_cnt, &opts); +ASSERT_ERR_PTR (link,"attach_task_iter" ); +ASSERT_EQ (errno,EINVAL ,"error code on invalid walk order" ); + } +} + +/* Iterate a cgroup withouth any task */ +static void test_walk_no_task (struct cgroup_task_iter *skel,int fd) +{ +snprintf (expected_output,sizeof (expected_output),"nr_total 0\n" ); + +read_from_cgroup_iter (skel->progs.cgroup_task_cnt, fd, +BPF_CGROUP_ITER_SELF_ONLY ,"self_only" ); +} + +/* The forked child process do nothing. */ +static void child_sleep (void ) +{ +while (1 ) +sleep (1 ); +} + +/* Get task pid under a cgroup */ +static void test_walk_task_pid (struct cgroup_task_iter *skel,int fd) +{ +int pid, status, err; +char pid_str[16 ]; + + pid =fork (); +if (!ASSERT_GE (pid,0 ,"fork_task" )) +return ; +if (pid) { +snprintf (pid_str,sizeof (pid_str),"%u" , pid); + err =write_cgroup_file ("cgroup_task_iter" ,"cgroup.procs" , pid_str); +if (!ASSERT_EQ (err,0 ,"write cgrp file" )) + goto out; +snprintf (expected_output,sizeof (expected_output),"pid %u\n" , pid); +read_from_cgroup_iter (skel->progs.cgroup_task_pid, fd, +BPF_CGROUP_ITER_SELF_ONLY ,"self_only" ); +out: +kill (pid,SIGKILL ); +waitpid (pid, &status,0 ); + }else { +child_sleep (); + } +} + +/* Get task count under a cgroup */ +static void test_walk_task_cnt (struct cgroup_task_iter *skel,int fd) +{ +int pids[PID_CNT ], pid, status, err, i; +char pid_str[16 ]; + +for (i =0 ; i <PID_CNT ; i++) + pids[i] =0 ; + +for (i =0 ; i <PID_CNT ; i++) { + pid =fork (); +if (!ASSERT_GE (pid,0 ,"fork_task" )) + goto out; +if (pid) { + pids[i] = pid; +snprintf (pid_str,sizeof (pid_str),"%u" , pid); + err =write_cgroup_file ("cgroup_task_iter" ,"cgroup.procs" , pid_str); +if (!ASSERT_EQ (err,0 ,"write cgrp file" )) + goto out; + }else { +child_sleep (); + } + } + +snprintf (expected_output,sizeof (expected_output),"nr_total %u\n" ,PID_CNT ); +read_from_cgroup_iter (skel->progs.cgroup_task_cnt, fd, +BPF_CGROUP_ITER_SELF_ONLY ,"self_only" ); + +out: +for (i =0 ; i <PID_CNT ; i++) { +if (!pids[i]) +continue ; +kill (pids[i],SIGKILL ); +waitpid (pids[i], &status,0 ); + } +} + +void test_cgroup_task_iter (void ) +{ +struct cgroup_task_iter *skel =NULL ; +int cgrp_fd; + +if (setup_cgroup_environment ()) +return ; + + cgrp_fd =create_and_get_cgroup ("cgroup_task_iter" ); +if (!ASSERT_GE (cgrp_fd,0 ,"create cgrp" )) + goto cleanup_cgrp_env; + + skel =cgroup_task_iter__open_and_load (); +if (!ASSERT_OK_PTR (skel,"cgroup_task_iter__open_and_load" )) + goto out; + +if (test__start_subtest ("cgroup_task_iter__invalid_order" )) +test_invalid_order (skel, cgrp_fd); +if (test__start_subtest ("cgroup_task_iter__no_task" )) +test_walk_no_task (skel, cgrp_fd); +if (test__start_subtest ("cgroup_task_iter__task_pid" )) +test_walk_task_pid (skel, cgrp_fd); +if (test__start_subtest ("cgroup_task_iter__task_cnt" )) +test_walk_task_cnt (skel, cgrp_fd); + +out: +cgroup_task_iter__destroy (skel); +close (cgrp_fd); +remove_cgroup ("cgroup_task_iter" ); +cleanup_cgrp_env: +cleanup_cgroup_environment (); +}
tools/testing/selftests/bpf/progs/cgroup_task_iter.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Yafang Shao <[email protected]> */ + +#include "bpf_iter.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +SEC("iter/cgroup_task") +int cgroup_task_cnt (struct bpf_iter__cgroup_task *ctx) +{ +struct seq_file *seq = ctx->meta->seq; +struct task_struct *task = ctx->task; +static __u32 nr_total; + +if (!task) { +BPF_SEQ_PRINTF (seq,"nr_total %u\n" , nr_total); +return 0 ; + } + +if (ctx->meta->seq_num ==0 ) + nr_total =0 ; + nr_total++; +return 0 ; +} + +SEC("iter/cgroup_task") +int cgroup_task_pid (struct bpf_iter__cgroup_task *ctx) +{ +struct seq_file *seq = ctx->meta->seq; +struct task_struct *task = ctx->task; + +if (!task) +return 0 ; + +BPF_SEQ_PRINTF (seq,"pid %u\n" , task->pid); +return 0 ; +}
Signature · 1 line
2.39.3
Quoted 43 lines · Yafang Shao
This patch introduces
cgroup_taskiter, which allows for efficient iteration of tasks within a specific cgroup. For example, we can effiently get the nr_{running,blocked} of a container with this new feature.The
cgroup_taskiteration serves as an alternative totask_iterin container environments due to certain limitations associated withtask_iter.- Firstly,
task_iteronly supports the 'current' pidns. However, since our data collector operates on the host, we may need to collect information from multiple containers simultaneously. Usingtask_iterwould require us to fork the collector for each container, which is not ideal.- Additionally,
task_iteris unable to collect task information from containers running in the host pidns.
In our container environment, we have containers running in the host pidns, and we would like to collect task information from them as well.- Lastly,
task_iterdoes not support multiple-container pods. In a Kubernetes environment, a single pod may contain multiple containers, all sharing the same pidns. However, we are only interested in iterating tasks within the main container, which is not possible withtask_iter.To address the first issue, we could potentially extend
task_iterto support specifying a pidns other than the current one. However, for the other two issues, extendingtask_iterwould not provide a solution. Therefore, we believe it is preferable to introduce thecgroup_taskiter to handle these scenarios effectively.Patch #1: Preparation
Patch #2: Addcgroup_taskiter
Patch #3: Add support forcgroup_taskiter in bpftool Patch #4: Selftests forcgroup_taskiterYafang Shao (4):
bpf: Add__bpf_iter_attach_cgroup()
bpf: Addcgroup_taskiter
bpftool: Add support forcgroup_taskselftests/bpf: Add selftest forcgroup_taskiter
Quoted 9 lines
include/linux/btf_ids.h | 14 ++ kernel/bpf/cgroup_iter.c | 181 ++++++++++++++-- tools/bpf/bpftool/link.c | 3 +- .../bpf/prog_tests/cgroup_task_iter.c | 197 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_task_iter.c | 39 ++++ 5 files changed, 419 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_task_iter.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_task_iter.c
Just a kind reminder.
Anyone is interested in this idea ?
Signature · 2 lines
Regards Yafang