Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4478660rdb; Fri, 15 Sep 2023 03:40:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGYCToVQf5Rq2IDeb4tq5zuPSQTpVIYihGL9hkO7kx/Rv22Qqz/wwGu1m7YMyHTjOUi1y32 X-Received: by 2002:a17:903:1c4:b0:1c3:bfb8:8c1 with SMTP id e4-20020a17090301c400b001c3bfb808c1mr1084711plh.65.1694774441688; Fri, 15 Sep 2023 03:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694774441; cv=none; d=google.com; s=arc-20160816; b=y+Q31+G+qFl0IZHR5IEr3DfgtBL9A0BLtbRupFpUtfJhtrRNqZlnqwmN2KvJXLlSgN IAzGDUsBUYrUwU+Psp9rXGqzmRsMMjfqUPnCZrCv2NGUEHSNBAX1KDC3Pc/lOdzYKFRB k72JV3q5/qQ8M+2AOvwPD3x5IlZtPqhOrqv+pjj4VyLu1DxdqmciD6QKO8uu1HxV4i8L 5ltQkzqtgB1TLUIkSofT5f/8kZ3Tsy90H9JbNaWC6SmuuJUa+u9GVFkfO5IPyMDqa9bL gARdmF5Bq5vKFnyT+sMNDPYkxeRyvhY7Ui+aaA6hvBZDfot7KL50cnQVq86ig3LCufpF H1DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=t8S+/VTA8YpPOP0TMPbEv9qezFi+CjZX1hX0AW8uUSc=; fh=luN1T+TZmJdWrv2MwLqOcKf0btUR/zj1LntAQkciNa8=; b=VxLEVUnqz0JMz33vwOVYgv2wV8Hvii9cCeYG7Y1jICOL5PCoJDOIw/6l6iSvoTPBwy j2I9Niyx6zTUtkcKpjxE8pjAYCA8ksBkl6qvWqYUiKTIwILmIazD5lxIOvjBih9DbZCQ r2RD5k/wWnJVj8rV5jkrlGIFezBoLYeMb1d+m3w+xrs9YpcxlXen8HZvDFYXu7AqOWav genVIRfTPi29/20vBM5QW/sq+DlLDCwtI2wyydJE6SPIPfY8ScRL++RYQpAw7YiuHHaY bljSAExawhfKR5bjOJhB3Z9ajISCVDRaBEMK3tfpJMv/Z6fP5bj7G4JAhaBDj0Lj7nCm F38w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=W7dpTm2T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id i4-20020a170902eb4400b001bde5c68aedsi3124484pli.251.2023.09.15.03.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 03:40:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=W7dpTm2T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5C56982BBB6A; Thu, 14 Sep 2023 16:26:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230194AbjINX0o (ORCPT + 99 others); Thu, 14 Sep 2023 19:26:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230086AbjINX0o (ORCPT ); Thu, 14 Sep 2023 19:26:44 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A16DE1FE5; Thu, 14 Sep 2023 16:26:39 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-53087f0e18bso82985a12.3; Thu, 14 Sep 2023 16:26:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694733998; x=1695338798; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=t8S+/VTA8YpPOP0TMPbEv9qezFi+CjZX1hX0AW8uUSc=; b=W7dpTm2TESAci2MHfIlrrBC7W9tuzXYhTppTKfWwCpCh3GtwvE9GKls+Je4i3JevYk VoDde5bWn+A8X9qBnwEDdKFTkKiXrKayWzTvAA5D+VceEJEHpxEabeaq8xlYt4WpoG9Y TPyUnqvWA/pi53yBOhicq5pcKKzDSaOQ39yYq7z1DQVS5lJvHkoyvD8JrpeHA0Olckbf r0XtNq41wfdGtjEcfL7k3saL2dHj8hhGGQKID+6CmIltwmdlKAFEHMtqzsQpsZCxKZZc pe0dtXQgXQJAQaxTTPMklKGDqtCQ9h0bDP79r+tsbs117XFskiuFrAPeArDCKPb8kRix 9WRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694733998; x=1695338798; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t8S+/VTA8YpPOP0TMPbEv9qezFi+CjZX1hX0AW8uUSc=; b=O/0vg2nfx3x+EXQQkZZP33+pbSCSjCABe+8KZEGYlNQ1k2GFLmkK5mQ0jh15S+uE4j az2O8J5G9m9EuEpE+ier7ndLcBes6Y9n4zlrJC8Mw6/42oG3HQOu+1LjXnKG/0dxnNGg WjIDsIff3eIlrpLVf3VBn+O8z9Gya8Flnc2YaTZ+MNqOdBPQh3WH3lLtfpn8wT4YqZ9B sAwfKKDGMGiWha5avtUlb1AlIBYH4D0bS/ftFrMo9qmGJblmWv3dvv/OePho+O842sVg 5VaiBgQbJePrSnRkjPMS1VZ2QCPZKilFtyo7e4xQT1bKdT2XxtXNcv5vZ4oD/C23K8sB iW4Q== X-Gm-Message-State: AOJu0Ywn6TCbIksXLnV0+heblXoUf21HNFDisyrNsObWAF+AMrO1VKEp 7x1XKaj8jRPfEX+knua6S36HE/10Gq2WB/fv0ko= X-Received: by 2002:aa7:d804:0:b0:52f:2521:d234 with SMTP id v4-20020aa7d804000000b0052f2521d234mr4742212edq.42.1694733997729; Thu, 14 Sep 2023 16:26:37 -0700 (PDT) MIME-Version: 1.0 References: <20230912070149.969939-1-zhouchuyi@bytedance.com> <20230912070149.969939-3-zhouchuyi@bytedance.com> In-Reply-To: <20230912070149.969939-3-zhouchuyi@bytedance.com> From: Andrii Nakryiko Date: Thu, 14 Sep 2023 16:26:26 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 2/6] bpf: Introduce css_task open-coded iterator kfuncs To: Chuyi Zhou Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, tj@kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 14 Sep 2023 16:26:51 -0700 (PDT) X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email On Tue, Sep 12, 2023 at 12:02=E2=80=AFAM Chuyi Zhou wrote: > > This patch adds kfuncs bpf_iter_css_task_{new,next,destroy} which allow > creation and manipulation of struct bpf_iter_css_task in open-coded > iterator style. These kfuncs actually wrapps css_task_iter_{start,next, > end}. BPF programs can use these kfuncs through bpf_for_each macro for > iteration of all tasks under a css. > > css_task_iter_*() would try to get the global spin-lock *css_set_lock*, s= o > the bpf side has to be careful in where it allows to use this iter. > Currently we only allow it in bpf_lsm and bpf iter-s. > > Signed-off-by: Chuyi Zhou > --- > include/uapi/linux/bpf.h | 4 +++ > kernel/bpf/helpers.c | 3 +++ > kernel/bpf/task_iter.c | 48 ++++++++++++++++++++++++++++++++++ > kernel/bpf/verifier.c | 23 ++++++++++++++++ > tools/include/uapi/linux/bpf.h | 4 +++ > tools/lib/bpf/bpf_helpers.h | 7 +++++ > 6 files changed, 89 insertions(+) > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index 73b155e52204..de02c0971428 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -7318,4 +7318,8 @@ struct bpf_iter_num { > __u64 __opaque[1]; > } __attribute__((aligned(8))); > > +struct bpf_iter_css_task { > + __u64 __opaque[1]; > +} __attribute__((aligned(8))); > + > #endif /* _UAPI__LINUX_BPF_H__ */ > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index b0a9834f1051..d6a16becfbb9 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -2504,6 +2504,9 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NU= LL) > BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW) > BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL) > BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY) > +BTF_ID_FLAGS(func, bpf_iter_css_task_new, KF_ITER_NEW) > +BTF_ID_FLAGS(func, bpf_iter_css_task_next, KF_ITER_NEXT | KF_RET_NULL) > +BTF_ID_FLAGS(func, bpf_iter_css_task_destroy, KF_ITER_DESTROY) > BTF_ID_FLAGS(func, bpf_dynptr_adjust) > BTF_ID_FLAGS(func, bpf_dynptr_is_null) > BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) > diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c > index 7473068ed313..d8539cc05ffd 100644 > --- a/kernel/bpf/task_iter.c > +++ b/kernel/bpf/task_iter.c > @@ -803,6 +803,54 @@ const struct bpf_func_proto bpf_find_vma_proto =3D { > .arg5_type =3D ARG_ANYTHING, > }; > > +struct bpf_iter_css_task_kern { > + struct css_task_iter *css_it; > +} __attribute__((aligned(8))); > + > +__bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it, > + struct cgroup_subsys_state *css, unsigned int flags) > +{ > + struct bpf_iter_css_task_kern *kit =3D (void *)it; > + > + BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) !=3D sizeof(st= ruct bpf_iter_css_task)); > + BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=3D > + __alignof__(struct bpf_iter_css_t= ask)); > + > + switch (flags) { > + case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED: > + case CSS_TASK_ITER_PROCS: > + case 0: > + break; > + default: > + return -EINVAL; > + } > + > + kit->css_it =3D kzalloc(sizeof(struct css_task_iter), GFP_KERNEL)= ; Dave used bpf_mem_alloc() inside his iterator, any reason to not use it her= e? > + if (!kit->css_it) > + return -ENOMEM; > + css_task_iter_start(css, flags, kit->css_it); > + return 0; > +} > + > +__bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_c= ss_task *it) > +{ > + struct bpf_iter_css_task_kern *kit =3D (void *)it; > + > + if (!kit->css_it) > + return NULL; > + return css_task_iter_next(kit->css_it); > +} > + > +__bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it) > +{ > + struct bpf_iter_css_task_kern *kit =3D (void *)it; > + > + if (!kit->css_it) > + return; > + css_task_iter_end(kit->css_it); > + kfree(kit->css_it); > +} > + > DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work); > > static void do_mmap_read_unlock(struct irq_work *entry) > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index dbba2b806017..2367483bf4c2 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -10332,6 +10332,7 @@ enum special_kfunc_type { > KF_bpf_dynptr_clone, > KF_bpf_percpu_obj_new_impl, > KF_bpf_percpu_obj_drop_impl, > + KF_bpf_iter_css_task_new, > }; > > BTF_SET_START(special_kfunc_set) > @@ -10354,6 +10355,7 @@ BTF_ID(func, bpf_dynptr_slice_rdwr) > BTF_ID(func, bpf_dynptr_clone) > BTF_ID(func, bpf_percpu_obj_new_impl) > BTF_ID(func, bpf_percpu_obj_drop_impl) > +BTF_ID(func, bpf_iter_css_task_new) > BTF_SET_END(special_kfunc_set) > > BTF_ID_LIST(special_kfunc_list) > @@ -10378,6 +10380,7 @@ BTF_ID(func, bpf_dynptr_slice_rdwr) > BTF_ID(func, bpf_dynptr_clone) > BTF_ID(func, bpf_percpu_obj_new_impl) > BTF_ID(func, bpf_percpu_obj_drop_impl) > +BTF_ID(func, bpf_iter_css_task_new) > > static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) > { > @@ -10902,6 +10905,20 @@ static int process_kf_arg_ptr_to_rbtree_node(str= uct bpf_verifier_env *env, > &meta->arg_rbtree_root.= field); > } > > +static bool check_css_task_iter_allowlist(struct bpf_verifier_env *env) > +{ > + enum bpf_prog_type prog_type =3D resolve_prog_type(env->prog); > + > + switch (prog_type) { > + case BPF_PROG_TYPE_LSM: > + return true; > + case BPF_TRACE_ITER: > + return env->prog->aux->sleepable; > + default: > + return false; > + } > +} > + > static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfu= nc_call_arg_meta *meta, > int insn_idx) > { > @@ -11152,6 +11169,12 @@ static int check_kfunc_args(struct bpf_verifier_= env *env, struct bpf_kfunc_call_ > break; > } > case KF_ARG_PTR_TO_ITER: > + if (meta->func_id =3D=3D special_kfunc_list[KF_bp= f_iter_css_task_new]) { > + if (!check_css_task_iter_allowlist(env)) = { > + verbose(env, "css_task_iter is on= ly allowed in bpf_lsm and bpf iter-s\n"); > + return -EINVAL; > + } > + } > ret =3D process_iter_arg(env, regno, insn_idx, me= ta); > if (ret < 0) > return ret; > diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bp= f.h > index 73b155e52204..de02c0971428 100644 > --- a/tools/include/uapi/linux/bpf.h > +++ b/tools/include/uapi/linux/bpf.h > @@ -7318,4 +7318,8 @@ struct bpf_iter_num { > __u64 __opaque[1]; > } __attribute__((aligned(8))); > > +struct bpf_iter_css_task { > + __u64 __opaque[1]; > +} __attribute__((aligned(8))); > + > #endif /* _UAPI__LINUX_BPF_H__ */ > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 77ceea575dc7..f48723c6c593 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -303,6 +303,13 @@ extern int bpf_iter_num_new(struct bpf_iter_num *it,= int start, int end) __weak > extern int *bpf_iter_num_next(struct bpf_iter_num *it) __weak __ksym; > extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __weak __ksym; > > +struct bpf_iter_css_task; > +struct cgroup_subsys_state; > +extern int bpf_iter_css_task_new(struct bpf_iter_css_task *it, > + struct cgroup_subsys_state *css, unsigned int flags) __we= ak __ksym; > +extern struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_ta= sk *it) __weak __ksym; > +extern void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it) __we= ak __ksym; please move this into bpf_experimental.h under selftests, this shouldn't be in libbpf's stable API headers > + > #ifndef bpf_for_each > /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct= for > * using BPF open-coded iterators without having to write mundane explic= it > -- > 2.20.1 >