Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2737994rdg; Mon, 16 Oct 2023 13:18:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJ9Q4xLQ5kUmx9ZR9YYJa+HN6nPT8n7U4bxZ7K99H56Ph8IZjQIueDBsgCzYcDfZIxt/jO X-Received: by 2002:a17:903:245:b0:1c7:2740:cfb3 with SMTP id j5-20020a170903024500b001c72740cfb3mr423675plh.35.1697487512651; Mon, 16 Oct 2023 13:18:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697487512; cv=none; d=google.com; s=arc-20160816; b=L5CGhfYArPViYC3wx3jwA+CMdKiyTpU4V/i8TMvw5QdN7vD1+lmUVUFkVUGw9ZfnE+ ijPHUTbpUTDQ1+l8N1XIm60NTvkeGgnekjuEebC9KJW4/6j5zaB6wsr5JCcYj+6X1I77 85ytSp6fc+yuDEljP3H45/yDXkcTQxY++6nSAWrXMtC4jkAB+K3qUKda6Pi7+UUPDVO2 3IrJxSbzBkkiB56uiBfHIPARLzqIWcbQJjDZWOYdGK7jmVPiaRqfxfQ/G3T1v4txXzrf WC8hYX5g2D4Pgpr48JeiV6CV/TjQ2+5icA62nysaRgqhd77478oaJrZ6c/SS3Isro+lv 1EWA== 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=1txcMyKL02YN3XqfczPDpjjenESIRV6GuXIGUGvRcWE=; fh=luN1T+TZmJdWrv2MwLqOcKf0btUR/zj1LntAQkciNa8=; b=knziCf3BfUQRUaOGRIriA2ZaMDLHKsY6vCjkKfWk9wCu8kJ/OQ1oCOcZ5nHEZSkPwE dxAmW1nahWzUKSBTFDAsmlzZvIxpcUgar+xKXAXshmTRKP7c9NXhCe8Iha5rnk+kNpPf YdLisSI19t9jd8QXoNcltc0rLE4Nvgn7ZT1VDTMte6xXwKpJsbHFDdE81iYn9WZ27gQQ iikLe+H3L3SX4Dzw6r9bbxFsfzcHE2W6hCVbzXVRa1H8fydWBaRNC4mhv3fCLf8JPVa+ 0EweECe605DxwFDtUd415icFOzKYXo7Z09tJcHMkjBPkP0WymClzILGk4Y4mxEwGGQ1Q we8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ceXwOir3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id jn7-20020a170903050700b001c5f0fd51bfsi68750plb.100.2023.10.16.13.18.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 13:18:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ceXwOir3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id B5F0280BC3E0; Mon, 16 Oct 2023 13:18:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233528AbjJPUSW (ORCPT + 99 others); Mon, 16 Oct 2023 16:18:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231221AbjJPUSV (ORCPT ); Mon, 16 Oct 2023 16:18:21 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76E7783; Mon, 16 Oct 2023 13:18:19 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-53da72739c3so8304941a12.3; Mon, 16 Oct 2023 13:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697487498; x=1698092298; 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=1txcMyKL02YN3XqfczPDpjjenESIRV6GuXIGUGvRcWE=; b=ceXwOir3f//tCJpb1EV44TC41oVA1eV/oUMO6TpM6tHeLXypmN/scdxmnI8PAQsNeN V9HYNCjXOI0Btp34MmzEGQaCamQwjNY/hYneIF1LlKrrMASAsLiTsijfNVNrY0zp/c7v FXUGcCk0cYxi6pUMPNU+Vv0fIaCnpLKTdtVstCZD1rL78HaTXthY614D7fi9kGMHo9U6 N/nUKa2HnxxYXHND5lnd/Wqsbj1xCPPzca2a3zm4EJbb/nSZ1inRRrbkufCRMj3XYddo a+NTsxe2cTGBjVDDGIu0NbUJwtnRbUgzSAMi4S4um9OppNCc6KnHR+HIQPzwc+PEJn4u OHgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697487498; x=1698092298; 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=1txcMyKL02YN3XqfczPDpjjenESIRV6GuXIGUGvRcWE=; b=jSIzSFIw7cGJM2njocQttfcv9uva6pfokeLikq62AauoVdDCDRbdJ1lIaFBdWsNAuH F4txofvJ2wqOH5mPdosiTRunMuDnnLpriPAZmUxQDUeEmxBR00qL6JA2m+t9prD/6We2 ZrFz+WMUN1mWxhmbAwK8/RfCrvVO/olxlc+5lXHC6kcrv1gNHUUK3Z/Xb535ha9KJRU5 nJ/cm0JmKnMsnMRPg4osR3Kafa03IuGs1nBAsiPsoH+3VyC0/3HK/ZHSSODGilYZTyo+ sVrZp+SdBj8DWzQUM/iCqt7pek3PFzbcLxQPksv7I8Nwn1qL7+80uDtshUMOE590w+Z7 BtDw== X-Gm-Message-State: AOJu0Yx/3z1PIzy9AymwtiVuvfo8iQIiqIekrK+UVm91oGzp1IBZAvdp T/kH4uHL/38kkCIQ4RAG23x/a0IFPj7YPO+EUcs= X-Received: by 2002:a50:a6d8:0:b0:53e:37b0:232e with SMTP id f24-20020a50a6d8000000b0053e37b0232emr295780edc.13.1697487497748; Mon, 16 Oct 2023 13:18:17 -0700 (PDT) MIME-Version: 1.0 References: <20231011120857.251943-1-zhouchuyi@bytedance.com> <20231011120857.251943-4-zhouchuyi@bytedance.com> <0dc492a8-7fc8-4cb4-a770-95906b1f311f@bytedance.com> In-Reply-To: <0dc492a8-7fc8-4cb4-a770-95906b1f311f@bytedance.com> From: Andrii Nakryiko Date: Mon, 16 Oct 2023 13:18:04 -0700 Message-ID: Subject: Re: [PATCH bpf-next v5 3/8] bpf: Introduce 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 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 pete.vger.email 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 (pete.vger.email [0.0.0.0]); Mon, 16 Oct 2023 13:18:29 -0700 (PDT) On Fri, Oct 13, 2023 at 7:02=E2=80=AFPM Chuyi Zhou wrote: > > Hello, > > =E5=9C=A8 2023/10/14 05:27, Andrii Nakryiko =E5=86=99=E9=81=93: > > On Wed, Oct 11, 2023 at 5:09=E2=80=AFAM Chuyi Zhou wrote: > >> > >> This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow > >> creation and manipulation of struct bpf_iter_task in open-coded iterat= or > >> style. BPF programs can use these kfuncs or through bpf_for_each macro= to > >> iterate all processes in the system. > >> > >> The API design keep consistent with SEC("iter/task"). bpf_iter_task_ne= w() > >> accepts a specific task and iterating type which allows: > >> > >> 1. iterating all process in the system(BPF_TASK_ITER_ALL_PROCS) > >> > >> 2. iterating all threads in the system(BPF_TASK_ITER_ALL_THREADS) > >> > >> 3. iterating all threads of a specific task(BPF_TASK_ITER_PROC_THREADS= ) > >> > >> Signed-off-by: Chuyi Zhou > >> --- > >> kernel/bpf/helpers.c | 3 + > >> kernel/bpf/task_iter.c | 82 +++++++++++++++++= ++ > >> .../testing/selftests/bpf/bpf_experimental.h | 5 ++ > >> 3 files changed, 90 insertions(+) > >> > >> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > >> index cb24c4a916df..690763751f6e 100644 > >> --- a/kernel/bpf/helpers.c > >> +++ b/kernel/bpf/helpers.c > >> @@ -2555,6 +2555,9 @@ BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER= _DESTROY) > >> BTF_ID_FLAGS(func, bpf_iter_css_task_new, KF_ITER_NEW | KF_TRUSTED_A= RGS) > >> BTF_ID_FLAGS(func, bpf_iter_css_task_next, KF_ITER_NEXT | KF_RET_NUL= L) > >> BTF_ID_FLAGS(func, bpf_iter_css_task_destroy, KF_ITER_DESTROY) > >> +BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS) > >> +BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) > >> +BTF_ID_FLAGS(func, bpf_iter_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 2cfcb4dd8a37..caeddad3d2f1 100644 > >> --- a/kernel/bpf/task_iter.c > >> +++ b/kernel/bpf/task_iter.c > >> @@ -856,6 +856,88 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct= bpf_iter_css_task *it) > >> bpf_mem_free(&bpf_global_ma, kit->css_it); > >> } > >> > >> +struct bpf_iter_task { > >> + __u64 __opaque[3]; > >> +} __attribute__((aligned(8))); > >> + > >> +struct bpf_iter_task_kern { > >> + struct task_struct *task; > >> + struct task_struct *pos; > >> + unsigned int flags; > >> +} __attribute__((aligned(8))); > >> + > >> +enum { > >> + BPF_TASK_ITER_ALL_PROCS, > >> + BPF_TASK_ITER_ALL_THREADS, > >> + BPF_TASK_ITER_PROC_THREADS > >> +}; > >> + > >> +__bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it, > >> + struct task_struct *task, unsigned int flags) > >> +{ > >> + struct bpf_iter_task_kern *kit =3D (void *)it; > >> + > >> + BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct= bpf_iter_task)); > >> + BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=3D > >> + __alignof__(struct bpf_iter_ta= sk)); > >> + > >> + kit->task =3D kit->pos =3D NULL; > >> + switch (flags) { > >> + case BPF_TASK_ITER_ALL_THREADS: > >> + case BPF_TASK_ITER_ALL_PROCS: > >> + case BPF_TASK_ITER_PROC_THREADS: > >> + break; > >> + default: > >> + return -EINVAL; > >> + } > >> + > >> + if (flags =3D=3D BPF_TASK_ITER_PROC_THREADS) > >> + kit->task =3D task; > >> + else > >> + kit->task =3D &init_task; > >> + kit->pos =3D kit->task; > >> + kit->flags =3D flags; > >> + return 0; > >> +} > >> + > >> +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_ta= sk *it) > >> +{ > >> + struct bpf_iter_task_kern *kit =3D (void *)it; > >> + struct task_struct *pos; > >> + unsigned int flags; > >> + > >> + flags =3D kit->flags; > >> + pos =3D kit->pos; > >> + > >> + if (!pos) > >> + goto out; > >> + > >> + if (flags =3D=3D BPF_TASK_ITER_ALL_PROCS) > >> + goto get_next_task; > >> + > >> + kit->pos =3D next_thread(kit->pos); > >> + if (kit->pos =3D=3D kit->task) { > >> + if (flags =3D=3D BPF_TASK_ITER_PROC_THREADS) { > >> + kit->pos =3D NULL; > >> + goto out; > >> + } > >> + } else > >> + goto out; > > > > nit: this should have {} around it to match the other if branch > > > > but actually, why goto out instead of return pos? same above, return > > pos instead of goto out? > > > > Thanks for the review. > > > IIUC, do you mean: > yes, goto only makes sense when there is some common clean up or error handling logic, in this case it's a plain return result, so no point. > diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c > index 0772545568f1..b35debf19edb 100644 > --- a/kernel/bpf/task_iter.c > +++ b/kernel/bpf/task_iter.c > @@ -913,7 +913,7 @@ __bpf_kfunc struct task_struct > *bpf_iter_task_next(struct bpf_iter_task *it) > pos =3D kit->pos; > > if (!pos) > - goto out; > + return pos; > > if (flags =3D=3D BPF_TASK_ITER_ALL_PROCS) > goto get_next_task; > @@ -922,18 +922,22 @@ __bpf_kfunc struct task_struct > *bpf_iter_task_next(struct bpf_iter_task *it) > if (kit->pos =3D=3D kit->task) { > if (flags =3D=3D BPF_TASK_ITER_PROC_THREADS) { > kit->pos =3D NULL; > - goto out; > + return pos; > } > } else > - goto out; > + return pos; > > + /* > + * goto get_next_task means: > + * case 1: flags =3D=3D BPF_TASK_ITER_ALL_PROCS > + * case 2: kit->pos =3D=3D kit->task && flags =3D=3D > BPF_TASK_ITER_ALL_THREADS > + */ > get_next_task: > kit->pos =3D next_task(kit->pos); > kit->task =3D kit->pos; > if (kit->pos =3D=3D &init_task) > kit->pos =3D NULL; > > -out: > return pos; > > > > BTW, do you have some comments on patch-8 ? or I should send next > version and pass all the CI first ? > I didn't think too hard about changes you are proposing, but yes, CI should be green on submission, of course > Thanks. > > > > >> + > >> +get_next_task: > >> + kit->pos =3D next_task(kit->pos); > >> + kit->task =3D kit->pos; > >> + if (kit->pos =3D=3D &init_task) > >> + kit->pos =3D NULL; > >> + > >> +out: > >> + return pos; > >> +} > >> + > >> +__bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *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/tools/testing/selftests/bpf/bpf_experimental.h b/tools/te= sting/selftests/bpf/bpf_experimental.h > >> index 8b53537e0f27..1ec82997cce7 100644 > >> --- a/tools/testing/selftests/bpf/bpf_experimental.h > >> +++ b/tools/testing/selftests/bpf/bpf_experimental.h > >> @@ -457,5 +457,10 @@ extern int bpf_iter_css_task_new(struct bpf_iter_= css_task *it, > >> extern struct task_struct *bpf_iter_css_task_next(struct bpf_iter_cs= s_task *it) __weak __ksym; > >> extern void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it) = __weak __ksym; > >> > >> +struct bpf_iter_task; > >> +extern int bpf_iter_task_new(struct bpf_iter_task *it, > >> + struct task_struct *task, unsigned int flags) __weak _= _ksym; > >> +extern struct task_struct *bpf_iter_task_next(struct bpf_iter_task *i= t) __weak __ksym; > >> +extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __= ksym; > >> > >> #endif > >> -- > >> 2.20.1 > >>