Message ID | 20231011120857.251943-4-zhouchuyi@bytedance.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp491152vqb; Wed, 11 Oct 2023 05:11:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEerA8Bl+wxLPBDRBhkP4u/RBvtMyYfVa6mWW4Lqnm6zMEK+7/sKcAletwH8IpfWwugyvdP X-Received: by 2002:a05:6a21:a587:b0:163:c167:964a with SMTP id gd7-20020a056a21a58700b00163c167964amr26315876pzc.1.1697026296488; Wed, 11 Oct 2023 05:11:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697026296; cv=none; d=google.com; s=arc-20160816; b=NIGkGgigNGzg6Db2DeGJrpRaAVYLGPN85VwkUa9EeClvY7zURe6B/S3//ofBVYl1zy eOZ/Pz2lSQ1S81kdkBFq1OlIDJNmsEOdhzH6Z0zcqXzMAEd5cBT7YSAMrC1N1+2IevQX DzF3TC2/uIOpUHVt5AZPaLTXkVnXRho9bebfDd9WLTNvI38mqzC5Wyea/8AM8q5cIRqy jySAC2SxlZNdJiZmROeOeqGe/oWUIGtWwALirVPRudyAlssmM+K97DxVp84801lbuDLU zZtjjWDAZYwzvtH4RC3oGoRVTBqmJiwi/gXsw5bbM5ea9GDXy0uJgKt3xdF9vnlz0cKb 99Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+eGErJyAmlj72NVapjxnwiwBdTl0+C6Vctyi1OYX3+k=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=FF7L0tmr82fNqHJfRhkhPGx0R6GVt8cpXNrS1XXbqBKuo5RqBMQNhMSHzhKSLUPwE7 PkLrUMYyvatV7+EwDekrt9cD1uCNUaLkanovJMBZNKxcuGdmV/siH3fgDXV3fZzvCbYd KKBULneASYtKUogQ/kGf9xiUzw6tpfIs/whEFMpHCTis3Wxnms6MTrWCLkYryn6zYyme XQAZPgpUlIhVA7CFa5qgFabWnpWq78ByQPz4W0yUi8xqo1xhEohggN3GkSyb638Gmthf n1vTFSxGORC7GxyH9mcpSUDIuCbZZ2W2DA86gTM3FGBMhckzyTZyBdLr6FtjK9/UX4YI HJkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Q75PKDfv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id h14-20020a170902680e00b001c9c10f0d44si2610421plk.8.2023.10.11.05.11.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 05:11:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Q75PKDfv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 63091824C4C1; Wed, 11 Oct 2023 05:11:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346860AbjJKMKq (ORCPT <rfc822;kartikey406@gmail.com> + 18 others); Wed, 11 Oct 2023 08:10:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346822AbjJKMJv (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 11 Oct 2023 08:09:51 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB3CCB7 for <linux-kernel@vger.kernel.org>; Wed, 11 Oct 2023 05:09:31 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-690ba63891dso5161284b3a.2 for <linux-kernel@vger.kernel.org>; Wed, 11 Oct 2023 05:09:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1697026165; x=1697630965; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+eGErJyAmlj72NVapjxnwiwBdTl0+C6Vctyi1OYX3+k=; b=Q75PKDfvOvXM4GmpyNYcTLyL865BRJGnxPG/0nEQPKGAln2BUNx0CLGfcuIrYrHh1x /24IrWqAKKxI4OYreihRAq0SU+j9ZBpzKrNVgfTAMViUn3tsjtwhPWpsC6MbADbWF40Z ic/qoMn+lHgV4KTE8YZOjO19uXeZllxslPZ07EemKRyV4UETu4/NdWr0Tg7y8v62Svml JCpUZSgOZwVzh+4NGFrcl3JW5ShACsUZt0d3tS95d3a97i6T3fYHkfoRrXHtcbqfC6lY DvipNl+CEAnea/UEviD1pbqXDBcIiFKSUSv5BD+pkiQ/8LuqMo9SSQVTRy1s3pqMICM8 QkHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697026165; x=1697630965; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+eGErJyAmlj72NVapjxnwiwBdTl0+C6Vctyi1OYX3+k=; b=VpiTzavW/CszEMQ8E4J5E9gNgwz6IH/8jhI+woCUQ44pE4M+Sz0m5NakLd7DVBCSQv YbzcPHiwuOCrwpryAPMatd+C6h0yansdrEwwbJABl840Tj9T0bicR7PWEm6GKGd1kNYA rTljDszMG31yeFD46cUfZivJEjFocJ5poHJ9UzAYpcgoSRnX478VWqzkW2XFeeY1TZ+y sVnMmHFbiJZ4V52RtmP7hJtJXnu4iEOuD6S629osDD1ql59aptkgtbN6Ox1NLhZEWB7u mCyiKlDWZHngb40rrJFxCsinOvLoTqhZlfTU3czZ7OrBRrEfLSvH2DnD9Smv7Y7UYkBR cFsw== X-Gm-Message-State: AOJu0YyvqZf/1YQZmO1/F2GxLINO41IugYoXWogIkdagLxUjLuDRBPPP SarF4qP8I3AZJhtvaIdgVB94Zw== X-Received: by 2002:a05:6a00:1790:b0:68f:c057:b567 with SMTP id s16-20020a056a00179000b0068fc057b567mr21438572pfg.26.1697026164800; Wed, 11 Oct 2023 05:09:24 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id u13-20020a62ed0d000000b006930db1e6cfsm9962769pfh.62.2023.10.11.05.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 05:09:24 -0700 (PDT) From: Chuyi Zhou <zhouchuyi@bytedance.com> To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, tj@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou <zhouchuyi@bytedance.com> Subject: [PATCH bpf-next v5 3/8] bpf: Introduce task open coded iterator kfuncs Date: Wed, 11 Oct 2023 20:08:52 +0800 Message-Id: <20231011120857.251943-4-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231011120857.251943-1-zhouchuyi@bytedance.com> References: <20231011120857.251943-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 11 Oct 2023 05:11:28 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779461046210792922 X-GMAIL-MSGID: 1779461046210792922 |
Series |
Add Open-coded task, css_task and css iters
|
|
Commit Message
Chuyi Zhou
Oct. 11, 2023, 12:08 p.m. UTC
This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow
creation and manipulation of struct bpf_iter_task in open-coded iterator
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_new()
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 <zhouchuyi@bytedance.com>
---
kernel/bpf/helpers.c | 3 +
kernel/bpf/task_iter.c | 82 +++++++++++++++++++
.../testing/selftests/bpf/bpf_experimental.h | 5 ++
3 files changed, 90 insertions(+)
Comments
On Wed, Oct 11, 2023 at 5:09 AM Chuyi Zhou <zhouchuyi@bytedance.com> wrote: > > This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow > creation and manipulation of struct bpf_iter_task in open-coded iterator > 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_new() > 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 <zhouchuyi@bytedance.com> > --- > 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_ARGS) > 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_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 = (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) != > + __alignof__(struct bpf_iter_task)); > + > + kit->task = kit->pos = 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 == BPF_TASK_ITER_PROC_THREADS) > + kit->task = task; > + else > + kit->task = &init_task; > + kit->pos = kit->task; > + kit->flags = flags; > + return 0; > +} > + > +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) > +{ > + struct bpf_iter_task_kern *kit = (void *)it; > + struct task_struct *pos; > + unsigned int flags; > + > + flags = kit->flags; > + pos = kit->pos; > + > + if (!pos) > + goto out; > + > + if (flags == BPF_TASK_ITER_ALL_PROCS) > + goto get_next_task; > + > + kit->pos = next_thread(kit->pos); > + if (kit->pos == kit->task) { > + if (flags == BPF_TASK_ITER_PROC_THREADS) { > + kit->pos = 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? > + > +get_next_task: > + kit->pos = next_task(kit->pos); > + kit->task = kit->pos; > + if (kit->pos == &init_task) > + kit->pos = 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/testing/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_css_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 *it) __weak __ksym; > +extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __ksym; > > #endif > -- > 2.20.1 >
Hello, 在 2023/10/14 05:27, Andrii Nakryiko 写道: > On Wed, Oct 11, 2023 at 5:09 AM Chuyi Zhou <zhouchuyi@bytedance.com> wrote: >> >> This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow >> creation and manipulation of struct bpf_iter_task in open-coded iterator >> 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_new() >> 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 <zhouchuyi@bytedance.com> >> --- >> 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_ARGS) >> 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_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 = (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) != >> + __alignof__(struct bpf_iter_task)); >> + >> + kit->task = kit->pos = 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 == BPF_TASK_ITER_PROC_THREADS) >> + kit->task = task; >> + else >> + kit->task = &init_task; >> + kit->pos = kit->task; >> + kit->flags = flags; >> + return 0; >> +} >> + >> +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) >> +{ >> + struct bpf_iter_task_kern *kit = (void *)it; >> + struct task_struct *pos; >> + unsigned int flags; >> + >> + flags = kit->flags; >> + pos = kit->pos; >> + >> + if (!pos) >> + goto out; >> + >> + if (flags == BPF_TASK_ITER_ALL_PROCS) >> + goto get_next_task; >> + >> + kit->pos = next_thread(kit->pos); >> + if (kit->pos == kit->task) { >> + if (flags == BPF_TASK_ITER_PROC_THREADS) { >> + kit->pos = 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: 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 = kit->pos; if (!pos) - goto out; + return pos; if (flags == 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 == kit->task) { if (flags == BPF_TASK_ITER_PROC_THREADS) { kit->pos = NULL; - goto out; + return pos; } } else - goto out; + return pos; + /* + * goto get_next_task means: + * case 1: flags == BPF_TASK_ITER_ALL_PROCS + * case 2: kit->pos == kit->task && flags == BPF_TASK_ITER_ALL_THREADS + */ get_next_task: kit->pos = next_task(kit->pos); kit->task = kit->pos; if (kit->pos == &init_task) kit->pos = 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 ? Thanks. > >> + >> +get_next_task: >> + kit->pos = next_task(kit->pos); >> + kit->task = kit->pos; >> + if (kit->pos == &init_task) >> + kit->pos = 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/testing/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_css_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 *it) __weak __ksym; >> +extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __ksym; >> >> #endif >> -- >> 2.20.1 >>
在 2023/10/11 20:08, Chuyi Zhou 写道: > This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow > creation and manipulation of struct bpf_iter_task in open-coded iterator > 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_new() > 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 <zhouchuyi@bytedance.com> > --- > 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_ARGS) > 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_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 > +}; > + In next version, I would add the missing __diag_ignore_all for -Wmissing-prototypes in Patch2 ~ Patch4 to avoid kernel build warning. Thanks. > +__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 = (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) != > + __alignof__(struct bpf_iter_task)); > + > + kit->task = kit->pos = 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 == BPF_TASK_ITER_PROC_THREADS) > + kit->task = task; > + else > + kit->task = &init_task; > + kit->pos = kit->task; > + kit->flags = flags; > + return 0; > +} > + > +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) > +{ > + struct bpf_iter_task_kern *kit = (void *)it; > + struct task_struct *pos; > + unsigned int flags; > + > + flags = kit->flags; > + pos = kit->pos; > + > + if (!pos) > + goto out; > + > + if (flags == BPF_TASK_ITER_ALL_PROCS) > + goto get_next_task; > + > + kit->pos = next_thread(kit->pos); > + if (kit->pos == kit->task) { > + if (flags == BPF_TASK_ITER_PROC_THREADS) { > + kit->pos = NULL; > + goto out; > + } > + } else > + goto out; > + > +get_next_task: > + kit->pos = next_task(kit->pos); > + kit->task = kit->pos; > + if (kit->pos == &init_task) > + kit->pos = 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); >
On Fri, Oct 13, 2023 at 7:02 PM Chuyi Zhou <zhouchuyi@bytedance.com> wrote: > > Hello, > > 在 2023/10/14 05:27, Andrii Nakryiko 写道: > > On Wed, Oct 11, 2023 at 5:09 AM Chuyi Zhou <zhouchuyi@bytedance.com> wrote: > >> > >> This patch adds kfuncs bpf_iter_task_{new,next,destroy} which allow > >> creation and manipulation of struct bpf_iter_task in open-coded iterator > >> 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_new() > >> 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 <zhouchuyi@bytedance.com> > >> --- > >> 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_ARGS) > >> 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_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 = (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) != > >> + __alignof__(struct bpf_iter_task)); > >> + > >> + kit->task = kit->pos = 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 == BPF_TASK_ITER_PROC_THREADS) > >> + kit->task = task; > >> + else > >> + kit->task = &init_task; > >> + kit->pos = kit->task; > >> + kit->flags = flags; > >> + return 0; > >> +} > >> + > >> +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) > >> +{ > >> + struct bpf_iter_task_kern *kit = (void *)it; > >> + struct task_struct *pos; > >> + unsigned int flags; > >> + > >> + flags = kit->flags; > >> + pos = kit->pos; > >> + > >> + if (!pos) > >> + goto out; > >> + > >> + if (flags == BPF_TASK_ITER_ALL_PROCS) > >> + goto get_next_task; > >> + > >> + kit->pos = next_thread(kit->pos); > >> + if (kit->pos == kit->task) { > >> + if (flags == BPF_TASK_ITER_PROC_THREADS) { > >> + kit->pos = 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 = kit->pos; > > if (!pos) > - goto out; > + return pos; > > if (flags == 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 == kit->task) { > if (flags == BPF_TASK_ITER_PROC_THREADS) { > kit->pos = NULL; > - goto out; > + return pos; > } > } else > - goto out; > + return pos; > > + /* > + * goto get_next_task means: > + * case 1: flags == BPF_TASK_ITER_ALL_PROCS > + * case 2: kit->pos == kit->task && flags == > BPF_TASK_ITER_ALL_THREADS > + */ > get_next_task: > kit->pos = next_task(kit->pos); > kit->task = kit->pos; > if (kit->pos == &init_task) > kit->pos = 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 = next_task(kit->pos); > >> + kit->task = kit->pos; > >> + if (kit->pos == &init_task) > >> + kit->pos = 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/testing/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_css_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 *it) __weak __ksym; > >> +extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __ksym; > >> > >> #endif > >> -- > >> 2.20.1 > >>
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_ARGS) 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_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 = (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) != + __alignof__(struct bpf_iter_task)); + + kit->task = kit->pos = 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 == BPF_TASK_ITER_PROC_THREADS) + kit->task = task; + else + kit->task = &init_task; + kit->pos = kit->task; + kit->flags = flags; + return 0; +} + +__bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) +{ + struct bpf_iter_task_kern *kit = (void *)it; + struct task_struct *pos; + unsigned int flags; + + flags = kit->flags; + pos = kit->pos; + + if (!pos) + goto out; + + if (flags == BPF_TASK_ITER_ALL_PROCS) + goto get_next_task; + + kit->pos = next_thread(kit->pos); + if (kit->pos == kit->task) { + if (flags == BPF_TASK_ITER_PROC_THREADS) { + kit->pos = NULL; + goto out; + } + } else + goto out; + +get_next_task: + kit->pos = next_task(kit->pos); + kit->task = kit->pos; + if (kit->pos == &init_task) + kit->pos = 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/testing/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_css_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 *it) __weak __ksym; +extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __ksym; #endif