From: Yonghong Song <yhs@fb.com>
To: Yosry Ahmed <yosryahmed@google.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>, Hao Luo <haoluo@google.com>,
Tejun Heo <tj@kernel.org>, Zefan Li <lizefan.x@bytedance.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Shuah Khan <shuah@kernel.org>, Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>,
David Rientjes <rientjes@google.com>,
Stanislav Fomichev <sdf@google.com>,
Greg Thelen <gthelen@google.com>,
Shakeel Butt <shakeelb@google.com>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
bpf@vger.kernel.org, cgroups@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 4/8] bpf: Introduce cgroup iter
Date: Mon, 27 Jun 2022 21:14:18 -0700 [thread overview]
Message-ID: <40114462-d5e2-ab07-7af9-5e60180027f9@fb.com> (raw)
In-Reply-To: <20220610194435.2268290-5-yosryahmed@google.com>
On 6/10/22 12:44 PM, Yosry Ahmed wrote:
> From: Hao Luo <haoluo@google.com>
>
> Cgroup_iter is a type of bpf_iter. It walks over cgroups in two modes:
>
> - walking a cgroup's descendants.
> - walking a cgroup's ancestors.
>
> When attaching cgroup_iter, one can set a cgroup to the iter_link
> created from attaching. This cgroup is passed as a file descriptor and
> serves as the starting point of the walk. If no cgroup is specified,
> the starting point will be the root cgroup.
>
> For walking descendants, one can specify the order: either pre-order or
> post-order. For walking ancestors, the walk starts at the specified
> cgroup and ends at the root.
>
> One can also terminate the walk early by returning 1 from the iter
> program.
>
> Note that because walking cgroup hierarchy holds cgroup_mutex, the iter
> program is called with cgroup_mutex held.
>
> Signed-off-by: Hao Luo <haoluo@google.com>
> Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
> ---
> include/linux/bpf.h | 8 ++
> include/uapi/linux/bpf.h | 21 +++
> kernel/bpf/Makefile | 2 +-
> kernel/bpf/cgroup_iter.c | 235 +++++++++++++++++++++++++++++++++
> tools/include/uapi/linux/bpf.h | 21 +++
> 5 files changed, 286 insertions(+), 1 deletion(-)
> create mode 100644 kernel/bpf/cgroup_iter.c
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 8e6092d0ea956..48d8e836b9748 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -44,6 +44,7 @@ struct kobject;
> struct mem_cgroup;
> struct module;
> struct bpf_func_state;
> +struct cgroup;
>
> extern struct idr btf_idr;
> extern spinlock_t btf_idr_lock;
> @@ -1590,7 +1591,14 @@ int bpf_obj_get_user(const char __user *pathname, int flags);
> int __init bpf_iter_ ## target(args) { return 0; }
>
> struct bpf_iter_aux_info {
> + /* for map_elem iter */
> struct bpf_map *map;
> +
> + /* for cgroup iter */
> + struct {
> + struct cgroup *start; /* starting cgroup */
> + int order;
> + } cgroup;
> };
>
[...]
> +
> +static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)
> +{
> + struct cgroup_iter_priv *p = seq->private;
> +
> + mutex_lock(&cgroup_mutex);
> +
> + /* support only one session */
> + if (*pos > 0)
> + return NULL;
> +
> + ++*pos;
> + p->terminate = false;
> + if (p->order == BPF_ITER_CGROUP_PRE)
> + return css_next_descendant_pre(NULL, p->start_css);
> + else if (p->order == BPF_ITER_CGROUP_POST)
> + return css_next_descendant_post(NULL, p->start_css);
> + else /* BPF_ITER_CGROUP_PARENT_UP */
> + return p->start_css;
> +}
> +
> +static int __cgroup_iter_seq_show(struct seq_file *seq,
> + struct cgroup_subsys_state *css, int in_stop);
> +
> +static void cgroup_iter_seq_stop(struct seq_file *seq, void *v)
> +{
> + /* pass NULL to the prog for post-processing */
> + if (!v)
> + __cgroup_iter_seq_show(seq, NULL, true);
> + mutex_unlock(&cgroup_mutex);
> +}
> +
> +static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
> +{
> + struct cgroup_subsys_state *curr = (struct cgroup_subsys_state *)v;
> + struct cgroup_iter_priv *p = seq->private;
> +
> + ++*pos;
> + if (p->terminate)
> + return NULL;
> +
> + if (p->order == BPF_ITER_CGROUP_PRE)
> + return css_next_descendant_pre(curr, p->start_css);
> + else if (p->order == BPF_ITER_CGROUP_POST)
> + return css_next_descendant_post(curr, p->start_css);
> + else
> + return curr->parent;
> +}
> +
> +static int __cgroup_iter_seq_show(struct seq_file *seq,
> + struct cgroup_subsys_state *css, int in_stop)
> +{
> + struct cgroup_iter_priv *p = seq->private;
> + struct bpf_iter__cgroup ctx;
> + struct bpf_iter_meta meta;
> + struct bpf_prog *prog;
> + int ret = 0;
> +
> + /* cgroup is dead, skip this element */
> + if (css && cgroup_is_dead(css->cgroup))
> + return 0;
> +
> + ctx.meta = &meta;
> + ctx.cgroup = css ? css->cgroup : NULL;
> + meta.seq = seq;
> + prog = bpf_iter_get_info(&meta, in_stop);
> + if (prog)
> + ret = bpf_iter_run_prog(prog, &ctx);
Do we need to do anything special to ensure bpf program gets
up-to-date stat from ctx.cgroup?
> +
> + /* if prog returns > 0, terminate after this element. */
> + if (ret != 0)
> + p->terminate = true;
> +
> + return 0;
> +}
> +
[...]
next prev parent reply other threads:[~2022-06-28 4:14 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-10 19:44 [PATCH bpf-next v2 0/8] bpf: rstat: cgroup hierarchical stats Yosry Ahmed
2022-06-10 19:44 ` [PATCH bpf-next v2 1/8] cgroup: enable cgroup_get_from_file() on cgroup1 Yosry Ahmed
2022-06-10 19:44 ` [PATCH bpf-next v2 2/8] cgroup: Add cgroup_put() in !CONFIG_CGROUPS case Yosry Ahmed
2022-06-10 19:44 ` [PATCH bpf-next v2 3/8] bpf, iter: Fix the condition on p when calling stop Yosry Ahmed
2022-06-20 18:48 ` Yonghong Song
2022-06-21 7:25 ` Hao Luo
2022-06-24 17:46 ` Yonghong Song
2022-06-24 18:23 ` Yosry Ahmed
2022-06-10 19:44 ` [PATCH bpf-next v2 4/8] bpf: Introduce cgroup iter Yosry Ahmed
2022-06-11 6:23 ` kernel test robot
2022-06-11 7:34 ` kernel test robot
2022-06-11 12:44 ` kernel test robot
2022-06-11 12:55 ` kernel test robot
2022-06-28 4:09 ` Yonghong Song
2022-06-28 6:06 ` Yosry Ahmed
2022-07-07 23:33 ` Hao Luo
2022-06-28 4:14 ` Yonghong Song [this message]
2022-06-28 6:03 ` Yosry Ahmed
2022-06-28 17:03 ` Yonghong Song
2022-07-07 23:36 ` Hao Luo
2022-06-10 19:44 ` [PATCH bpf-next v2 5/8] selftests/bpf: Test cgroup_iter Yosry Ahmed
2022-06-28 6:11 ` Yonghong Song
2022-06-10 19:44 ` [PATCH bpf-next v2 6/8] cgroup: bpf: enable bpf programs to integrate with rstat Yosry Ahmed
2022-06-10 20:52 ` kernel test robot
2022-06-10 21:22 ` kernel test robot
2022-06-10 21:30 ` Yosry Ahmed
2022-06-11 19:57 ` Alexei Starovoitov
2022-06-13 17:05 ` Yosry Ahmed
2022-06-11 10:22 ` kernel test robot
2022-06-28 6:12 ` Yonghong Song
2022-06-10 19:44 ` [PATCH bpf-next v2 7/8] selftests/bpf: extend cgroup helpers Yosry Ahmed
2022-06-10 19:44 ` [PATCH bpf-next v2 8/8] bpf: add a selftest for cgroup hierarchical stats collection Yosry Ahmed
2022-06-28 6:14 ` Yonghong Song
2022-06-28 6:47 ` Yosry Ahmed
2022-06-28 7:14 ` Yosry Ahmed
2022-06-29 0:09 ` Yosry Ahmed
2022-06-29 6:48 ` Yonghong Song
2022-06-29 8:04 ` Yosry Ahmed
2022-07-02 0:55 ` Yonghong Song
2022-07-06 21:29 ` Yosry Ahmed
2022-06-29 6:17 ` Yonghong Song
2022-06-28 7:43 ` Yosry Ahmed
2022-06-29 6:26 ` Yonghong Song
2022-06-29 8:03 ` Yosry Ahmed
2022-07-01 23:28 ` Hao Luo
2022-06-10 19:48 ` [PATCH bpf-next v2 0/8] bpf: rstat: cgroup hierarchical stats Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=40114462-d5e2-ab07-7af9-5e60180027f9@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=gthelen@google.com \
--cc=hannes@cmpxchg.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=mhocko@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=sdf@google.com \
--cc=shakeelb@google.com \
--cc=shuah@kernel.org \
--cc=songliubraving@fb.com \
--cc=tj@kernel.org \
--cc=yosryahmed@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).