Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3340161rwb; Tue, 16 Aug 2022 00:41:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR6k+T8j2jIfmuGAykHVX4j/jJSEqmBNsM+q0TArE/wdH+Tt2Rj4ZGcja32PbqPS0DUaIW3Z X-Received: by 2002:a05:6402:13c1:b0:43b:e330:9bbf with SMTP id a1-20020a05640213c100b0043be3309bbfmr17661202edx.417.1660635670643; Tue, 16 Aug 2022 00:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660635670; cv=none; d=google.com; s=arc-20160816; b=PHQaGKCYKfRzSWSx7hZTcoMBEeC4Z2py6clfsiNc7jobbslGTKtMkQhp9rmwW8E7Y3 4c32TCaMcj+wGVGGxA/sOebxNRRus4H371riHiH5XJkYzWl3dRSrQVWhhdIXcKbkwbuW Y2JcFT9H8g1HxFKYwCaNjlMvogGFb9RjhppojwVBej5mV0e4cY7V3iigGODokCAcSXUQ ENgsXfKho0JLqEvw/dVJQFu2PAaUDy+kTm1AnRk3giVF1hFl+eClD0tvPBpnzitRQSwi 09EWycRXWcuVcfnarKaNjrBTVVWbYGn4DyayMPp5mY1To3d51e9ArNOBu5aURbScsIEP StRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=j/gGIYHVvGXD1yDEp46mElKYaYYPTYLmXjRKXScf7hY=; b=zY5WjwRxgnOW1Ac7SWj+b+gMRKtHDqqiIC9MBSh3V0XLwt0LCqKqagWCFYYl2Hudcj Xxc1hI+f22DR8GIXKpaMQyxmTAWrg74TlTUy0x7krvy34UmFUB2wCci2Mj2nhxEFV7n1 cAzve0Mf28IeXM4xHYiCJ0qauKqPliKypgG2if0bG5e2aLAvmMXyHODk1DIFkp9iv5mL +ulapfOhczdlwt5qFa2uZ1hGMeY4kzXLN83JEPWZwtxPI2BBIKZw0LV/wVgX1gZAjOFW i0xpFjk7ZqXzAevmHBKVK+cuRxrXyBXBYeC9kJi7s+tEFyJNfm6+2T8L8xuIfnzkGXAS iCdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=VNUa8f0P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw7-20020a170906c78700b00730abba3122si7847380ejb.467.2022.08.16.00.40.44; Tue, 16 Aug 2022 00:41:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=VNUa8f0P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231774AbiHPHfx (ORCPT + 99 others); Tue, 16 Aug 2022 03:35:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbiHPHey (ORCPT ); Tue, 16 Aug 2022 03:34:54 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12F5D13E980; Mon, 15 Aug 2022 21:17:22 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id fy5so16778431ejc.3; Mon, 15 Aug 2022 21:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=j/gGIYHVvGXD1yDEp46mElKYaYYPTYLmXjRKXScf7hY=; b=VNUa8f0PEtm55xS3sXA1K8l77VMtcbnvYrI2gLVUGAt4pR2iZK0FqWrxO6A/c7VLog mPNy+vUcwyoF/j7hNHJ9J9dyAsVHZMf1qa+C3oXPOuR3Yv8QEK2pYWex27VgW3f326n8 52LdK4pl/jK14F/2eJo/SjdWz1uhtNFgJtmm/wD3YTHUcI+nJmAZ4jLw4rsfAcMrcYry L1DZ40JETewL88nD23C6xxL2BaXI5MMBf5bzOGLi9tSxfEKSU/yB2ZTdZ0RR7RdUTqYQ I5kiXHvQXwmqzdq5ol9LFZnqxaIHntzerAsgcSoLDk3/ikf96lr5DrHI1DV2RTAUUNVA tWyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=j/gGIYHVvGXD1yDEp46mElKYaYYPTYLmXjRKXScf7hY=; b=bTHj0RvbHp2dryS1QnVMy2cE6SOg7mNpbnwbBp8m6YZvVHk9tC7gXaJq7DvVcYVPn5 P+jNkM5Cbe/+1nv2e3FQY+3nuoRI+QfsgeRkEK0KwFUXRcX2vfki1UVmfvkZ900oI9a/ qME+a3nlguyMGS/TCyl6G8+emnOx3VW/0QUXlMhFMK5mwfwQGqGp5jzJhUFhORTPDVyA furycWjG0ZHOh5SyuVASxOUj8UxNTHouupSButI5SDrWreovBy1GXMSPMBW6D1/xQZ/X FC7XNtqFFVyzUvKbpCVxiKJy+BmTBtjOiJEggxNaanumVQCoKcHt2slXacMyPT2l36gR 71DA== X-Gm-Message-State: ACgBeo3tLStL/i0iK3CJtOItqKEFf9aYPaCMSgmfa21pB9hyVkEdGUsN 0OfXjex0OgkXPefx/b1KJTlRA49/3SG1MWfUbjE= X-Received: by 2002:a17:907:6e22:b0:731:152:2504 with SMTP id sd34-20020a1709076e2200b0073101522504mr12582106ejc.545.1660623440628; Mon, 15 Aug 2022 21:17:20 -0700 (PDT) MIME-Version: 1.0 References: <20220812202802.3774257-1-haoluo@google.com> <20220812202802.3774257-2-haoluo@google.com> In-Reply-To: <20220812202802.3774257-2-haoluo@google.com> From: Andrii Nakryiko Date: Mon, 15 Aug 2022 21:17:09 -0700 Message-ID: Subject: Re: [PATCH bpf-next v8 1/5] bpf: Introduce cgroup iter To: Hao Luo Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, cgroups@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Tejun Heo , Zefan Li , KP Singh , Johannes Weiner , Michal Hocko , John Fastabend , Michal Koutny , Roman Gushchin , David Rientjes , Stanislav Fomichev , Shakeel Butt , Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 12, 2022 at 1:28 PM Hao Luo wrote: > > Cgroup_iter is a type of bpf_iter. It walks over cgroups in four modes: > > - walking a cgroup's descendants in pre-order. > - walking a cgroup's descendants in post-order. > - walking a cgroup's ancestors. > - process only the given cgroup. > > When attaching cgroup_iter, one can set a cgroup to the iter_link > created from attaching. This cgroup is passed as a file descriptor > or cgroup id and serves as the starting point of the walk. If no > cgroup is specified, the starting point will be the root cgroup v2. > > 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, assuming PAGE_SIZE is 4kb, the total number of cgroups that can > be walked is 512. This is a limitation of cgroup_iter. If the output > data is larger than the kernel buffer size, after all data in the > kernel buffer is consumed by user space, the subsequent read() syscall > 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. > > Acked-by: Yonghong Song > Acked-by: Tejun Heo > Signed-off-by: Hao Luo > --- > include/linux/bpf.h | 8 + > include/uapi/linux/bpf.h | 35 +++ > kernel/bpf/Makefile | 3 + > kernel/bpf/cgroup_iter.c | 283 ++++++++++++++++++ > tools/include/uapi/linux/bpf.h | 35 +++ > .../selftests/bpf/prog_tests/btf_dump.c | 4 +- > 6 files changed, 366 insertions(+), 2 deletions(-) > create mode 100644 kernel/bpf/cgroup_iter.c > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index a627a02cf8ab..ecb8c61178a1 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -48,6 +48,7 @@ struct mem_cgroup; > struct module; > struct bpf_func_state; > struct ftrace_ops; > +struct cgroup; > > extern struct idr btf_idr; > extern spinlock_t btf_idr_lock; > @@ -1730,7 +1731,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; why not using enum as a type here? > + } 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 7d1e2794d83e..bc3c901b9f70 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -87,10 +87,34 @@ struct bpf_cgroup_storage_key { > __u32 attach_type; /* program attach type (enum bpf_attach_type) */ > }; > > +enum bpf_iter_order { > + BPF_ITER_DESCENDANTS_PRE = 0, /* walk descendants in pre-order. */ > + BPF_ITER_DESCENDANTS_POST, /* walk descendants in post-order. */ > + BPF_ITER_ANCESTORS_UP, /* walk ancestors upward. */ > + BPF_ITER_SELF_ONLY, /* process only a single object. */ > +}; > + > union bpf_iter_link_info { > struct { > __u32 map_fd; > } map; > + struct { > + /* Users must specify order using one of the following values: > + * - BPF_ITER_DESCENDANTS_PRE > + * - BPF_ITER_DESCENDANTS_POST > + * - BPF_ITER_ANCESTORS_UP > + * - BPF_ITER_SELF_ONLY > + */ > + __u32 order; same, we just declared the UAPI enum above, why not specify that this is that enum here? > + > + /* At most one of cgroup_fd and cgroup_id can be non-zero. If > + * both are zero, the walk starts from the default cgroup v2 > + * root. For walking v1 hierarchy, one should always explicitly > + * specify cgroup_fd. > + */ > + __u32 cgroup_fd; > + __u64 cgroup_id; for my own education, does root cgroup has cgroup_id == 0? > + } cgroup; > }; > [...]