From: Yonghong Song <yhs@fb.com>
To: Hao Luo <haoluo@google.com>
Cc: "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>, "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>,
"KP Singh" <kpsingh@kernel.org>,
"Benjamin Tissoires" <benjamin.tissoires@redhat.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"Michal Koutný" <mkoutny@suse.com>,
"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 v5 4/8] bpf: Introduce cgroup iter
Date: Thu, 28 Jul 2022 11:08:16 -0700 [thread overview]
Message-ID: <1c4bac1c-ba65-5033-6271-6f79e3ca0cb0@fb.com> (raw)
In-Reply-To: <CA+khW7hvLgCKVA0kiKhREW-PZ4aOYvkGHoEqKAggEdyY9TRp7Q@mail.gmail.com>
On 7/28/22 10:25 AM, Hao Luo wrote:
> On Wed, Jul 27, 2022 at 10:49 PM Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 7/22/22 10:48 AM, Yosry Ahmed wrote:
>>> From: Hao Luo <haoluo@google.com>
>>>
>>> Cgroup_iter is a type of bpf_iter. It walks over cgroups in three modes:
>>>
>>> - walking a cgroup's descendants in pre-order.
>>> - walking a cgroup's descendants in post-order.
>>> - 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.
>>>
>>> Currently only one session is supported, which means, depending on the
>>> volume of data bpf program intends to send to user space, the number
>>> of cgroups that can be walked is limited. For example, given the current
>>> buffer size is 8 * PAGE_SIZE, if the program sends 64B data for each
>>> cgroup, the total number of cgroups that can be walked is 512. This is
>>
>> PAGE_SIZE needs to be 4KB in order to conclude that the total number of
>> walked cgroups is 512.
>>
>
> Sure. Will change that.
>
>>> a limitation of cgroup_iter. If the output data is larger than the
>>> buffer size, the second read() will signal EOPNOTSUPP. In order to work
>>> around, the user may have to update their program to reduce the volume
>>> of data sent to output. For example, skip some uninteresting cgroups.
>>> In future, we may extend bpf_iter flags to allow customizing buffer
>>> size.
>>>
>>> Signed-off-by: Hao Luo <haoluo@google.com>
>>> Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
>>> Acked-by: Yonghong Song <yhs@fb.com>
>>> ---
>>> include/linux/bpf.h | 8 +
>>> include/uapi/linux/bpf.h | 30 +++
>>> kernel/bpf/Makefile | 3 +
>>> kernel/bpf/cgroup_iter.c | 252 ++++++++++++++++++
>>> tools/include/uapi/linux/bpf.h | 30 +++
>>> .../selftests/bpf/prog_tests/btf_dump.c | 4 +-
>>> 6 files changed, 325 insertions(+), 2 deletions(-)
>>> create mode 100644 kernel/bpf/cgroup_iter.c
>>
>> This patch cannot apply to bpf-next cleanly, so please rebase
>> and post again.
>>
>
> Sorry about that. Will do.
>
>>>
>>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
>>> index a97751d845c9..9061618fe929 100644
>>> --- a/include/linux/bpf.h
>>> +++ b/include/linux/bpf.h
>>> @@ -47,6 +47,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;
>>> @@ -1717,7 +1718,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;
>>> };
>>>
>>> typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
>>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
>>> index ffcbf79a556b..fe50c2489350 100644
>>> --- a/include/uapi/linux/bpf.h
>>> +++ b/include/uapi/linux/bpf.h
>>> @@ -87,10 +87,30 @@ struct bpf_cgroup_storage_key {
>>> __u32 attach_type; /* program attach type (enum bpf_attach_type) */
>>> };
>>>
>>> +enum bpf_iter_cgroup_traversal_order {
>>> + BPF_ITER_CGROUP_PRE = 0, /* pre-order traversal */
>>> + BPF_ITER_CGROUP_POST, /* post-order traversal */
>>> + BPF_ITER_CGROUP_PARENT_UP, /* traversal of ancestors up to the root */
>>> +};
>>> +
>>> union bpf_iter_link_info {
>>> struct {
>>> __u32 map_fd;
>>> } map;
>>> +
>>> + /* cgroup_iter walks either the live descendants of a cgroup subtree, or the
>>> + * ancestors of a given cgroup.
>>> + */
>>> + struct {
>>> + /* Cgroup file descriptor. This is root of the subtree if walking
>>> + * descendants; it's the starting cgroup if walking the ancestors.
>>> + * If it is left 0, the traversal starts from the default cgroup v2
>>> + * root. For walking v1 hierarchy, one should always explicitly
>>> + * specify the cgroup_fd.
>>> + */
>>
>> I did see how the above cgroup v1/v2 scenarios are enforced.
>>
>
> Do you mean _not_ see? Yosry and I experimented a bit. We found even
Ya, I mean 'not see'...
> on systems where v2 is not enabled, cgroup v2 root always exists and
> can be attached to, and can be iterated on (only trivially). We didn't
> find a way to tell v1 and v2 apart and deemed a comment to instruct v1
> users is fine?
So, cgroup_fd = 0, start from cgroup v2 root.
cgroup_fd != 0, start from that particular cgroup (cgroup_v1 or v2)
Okay, since cgroup v2 root is always available and can be iterated,
I think comments should be okay.
>
>>> + __u32 cgroup_fd;
>>> + __u32 traversal_order;
>>> + } cgroup;
>>> };
>>>
>>> /* BPF syscall commands, see bpf(2) man-page for more details. */
[...]
next prev parent reply other threads:[~2022-07-28 18:09 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-22 17:48 [PATCH bpf-next v5 0/8] bpf: rstat: cgroup hierarchical stats Yosry Ahmed
2022-07-22 17:48 ` [PATCH bpf-next v5 1/8] btf: Add a new kfunc flag which allows to mark a function to be sleepable Yosry Ahmed
2022-07-22 17:48 ` [PATCH bpf-next v5 2/8] cgroup: enable cgroup_get_from_file() on cgroup1 Yosry Ahmed
2022-07-28 16:51 ` Tejun Heo
2022-07-22 17:48 ` [PATCH bpf-next v5 3/8] bpf, iter: Fix the condition on p when calling stop Yosry Ahmed
2022-07-22 17:48 ` [PATCH bpf-next v5 4/8] bpf: Introduce cgroup iter Yosry Ahmed
2022-07-22 18:35 ` Kumar Kartikeya Dwivedi
2022-07-22 20:33 ` Hao Luo
2022-07-28 6:55 ` Yonghong Song
2022-07-28 17:26 ` Hao Luo
2022-07-28 5:49 ` Yonghong Song
2022-07-28 17:25 ` Hao Luo
2022-07-28 18:08 ` Yonghong Song [this message]
2022-07-28 16:51 ` Tejun Heo
2022-07-28 17:20 ` Hao Luo
2022-07-28 17:35 ` Tejun Heo
2022-08-02 3:42 ` Andrii Nakryiko
2022-08-02 22:27 ` Hao Luo
2022-08-02 22:49 ` Andrii Nakryiko
2022-08-03 20:29 ` Hao Luo
2022-08-03 20:39 ` Andrii Nakryiko
2022-08-04 0:29 ` Hao Luo
2022-07-22 17:48 ` [PATCH bpf-next v5 5/8] selftests/bpf: Test cgroup_iter Yosry Ahmed
2022-07-22 17:48 ` [PATCH bpf-next v5 6/8] cgroup: bpf: enable bpf programs to integrate with rstat Yosry Ahmed
2022-07-28 16:51 ` Tejun Heo
2022-07-22 17:48 ` [PATCH bpf-next v5 7/8] selftests/bpf: extend cgroup helpers Yosry Ahmed
2022-07-22 17:48 ` [PATCH bpf-next v5 8/8] bpf: add a selftest for cgroup hierarchical stats collection Yosry Ahmed
2022-07-28 5:51 ` Yonghong Song
2022-07-28 22:40 ` Andrii Nakryiko
2022-07-29 17:36 ` Hao Luo
2022-07-28 5:53 ` [PATCH bpf-next v5 0/8] bpf: rstat: cgroup hierarchical stats Yonghong Song
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=1c4bac1c-ba65-5033-6271-6f79e3ca0cb0@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=benjamin.tissoires@redhat.com \
--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=mkoutny@suse.com \
--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