Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp755601rdb; Sun, 1 Oct 2023 04:25:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEimf3xClSCzpAcsCTEzR/ep/7A5bJHgq0SJSVAe17db3Hd4OGqHpV7drqHNnjdtBTS7JsT X-Received: by 2002:a05:6a00:cc3:b0:68b:e710:ee9c with SMTP id b3-20020a056a000cc300b0068be710ee9cmr8271963pfv.19.1696159542259; Sun, 01 Oct 2023 04:25:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696159542; cv=none; d=google.com; s=arc-20160816; b=0r11bk55Hp+3ZjAmb2AcFfdrHouFImkUfaPAUGShVugC93K68vZfoK3Otu3B0gPmzR mmE218kAgktKQuGJMuPFpfWcOMASLYRZE3mFcDDWLWkEZvdK9NZhhH/ESLi2uaCux6Nx QxmBwnmNm8a675VojIDNOgyKyyRbkY/YiqOsuQ84TQbsSAFTeBs9nETPGqebzuZhrcuq czX9Auni6pXatt0vKYvV2veh0dR0eomALS79Ez3is2hlFj6H43nlAYl4eiy56P7PYqPC uik6Ky4zBFbioQP7bdHOuLXRIHz3ub0Uhwsowjhe2m90cq0bzKavp2khHsC2vw2ZIZaG bITQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=nb2D3LuQXiqDzGOTpeZzuHnR6ZfiMCGFmdYta0VCauI=; fh=v95LKH/FC/ox3RbaAOhxEcPZKseyeUOjmVin6/WheA4=; b=TawxWO1vI7D8enVftNx5revCuvGLiIriM+Qxt9AR00eESI5gbDdhBy5dfzugrx7g7V LWQkzgGfsyYMY2E2rXsHhn7BPlxwmsdWH+jbt9U3G6OfEMjES0o/lpajSGCRPXZx57Kf 66qElXAkgiI9aDQN/VPtX7eKuzqdUuxDAEE6JJW8yrme6Ylk8s1FN5h7vPtYF+NyjVcS vLbJFOUIAc4pT3F4qM6fSDn0YWzQq7Mm3MEq0DJHIX/ok1vHWfpgZnBHRZwnpTw3eN1p HkFOucKb69bjhL//tWc/7xKBHiEwwMk7abbPJX4wJCoDOTFB6JuB5hTtyZqwB5c0hvEZ yp3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=BNdqHacU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id fa25-20020a056a002d1900b00690ce34a02bsi27170606pfb.224.2023.10.01.04.25.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 04:25:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=BNdqHacU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 7838680C8DEA; Sun, 1 Oct 2023 01:21:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234505AbjJAIVn (ORCPT + 99 others); Sun, 1 Oct 2023 04:21:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234402AbjJAIVl (ORCPT ); Sun, 1 Oct 2023 04:21:41 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA453C2 for ; Sun, 1 Oct 2023 01:21:16 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-690bd59322dso12223941b3a.3 for ; Sun, 01 Oct 2023 01:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1696148476; x=1696753276; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=nb2D3LuQXiqDzGOTpeZzuHnR6ZfiMCGFmdYta0VCauI=; b=BNdqHacUuGF9KIhNxHqfX/PSVxUjgTKkJY5g845ujOsvAcxhe2NvYVP1vpWoNUEu0Z ZQLjHeLuMxArel/PNxQgFcz7ZTj6Sa6MkPZGtUjX/azGxaKy7o420hc6OFu/7MtWr5kN MOkNtE7yCDHQaiUZBxyTAYYbhQmaVXXaYt3Iyl/aZSlWomrsHFHbgsVlSAZk0AUk7n6u uYhLEqJoNkCINyyrpY4DiDcUHXH6AjhBPxh2FN6bIOulG4HW2SStoogQc5dLYejh7gSb Cj1Fwe0GVPudTNNFxy1lcQrfOXRfeetlNOegSnxaASUJREoA/U3+DS9KbEVNgD2M04cP CvSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696148476; x=1696753276; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=nb2D3LuQXiqDzGOTpeZzuHnR6ZfiMCGFmdYta0VCauI=; b=gooIyEKdZBNg+1Gq/acWJQST06SNxCgCLWWyJVOLVyER5/1UruyQyC3Oqwgj1rBgJW XJAuXTxVGKa4JldLio2lBJKriF91dAQ0glj13SIuACP6DRXrRZir7vl712fu0KNKuLaz TO0Xs/+5E35f8DJwVZXqUo+kfG/Kgc3ctAgI7Co4ljv4zR8+Y+0+PyX/OA7ZmCrHd/pH su/3J7bVrLf3TEPtUIQZWP4Od5xGRd0/5EzAV5y3ZMuzn4fmVQrwJPuPqoqdAYO6ivTS 6dyoyfmdx1gumclEkPDtqs6rcRkNqPAJyOspc2KPZDRe3nrg+x8e2yR7c6cEznomA7Q9 N7gQ== X-Gm-Message-State: AOJu0Yxs3M8XPJP1Yma6ElqscW87p9lMm+50sDDOrZb54M+n39JY5NhI ohny+HGeSkM+ZQ3GgKGY2/m28A== X-Received: by 2002:aa7:88c3:0:b0:68a:4261:ab7f with SMTP id k3-20020aa788c3000000b0068a4261ab7fmr8452493pff.31.1696148476108; Sun, 01 Oct 2023 01:21:16 -0700 (PDT) Received: from ?IPV6:2409:8a28:5060:6c21:2872:efd0:e8fb:f8d8? ([2409:8a28:5060:6c21:2872:efd0:e8fb:f8d8]) by smtp.gmail.com with ESMTPSA id g23-20020aa78197000000b00684ca1b45b9sm17972119pfi.149.2023.10.01.01.21.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 01 Oct 2023 01:21:15 -0700 (PDT) Message-ID: <425309da-ec03-df8b-3565-d226dd1a1715@bytedance.com> Date: Sun, 1 Oct 2023 16:21:08 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH bpf-next v3 3/7] bpf: Introduce task open coded iterator kfuncs To: Andrii Nakryiko 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 References: <20230925105552.817513-1-zhouchuyi@bytedance.com> <20230925105552.817513-4-zhouchuyi@bytedance.com> <716adfa5-bd5d-3fe2-108c-ff24b2e81420@bytedance.com> From: Chuyi Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,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 groat.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 (groat.vger.email [0.0.0.0]); Sun, 01 Oct 2023 01:21:53 -0700 (PDT) Hello, Andrii 在 2023/9/30 05:27, Andrii Nakryiko 写道: > On Wed, Sep 27, 2023 at 8:29 PM Chuyi Zhou wrote: >> >> Hello, >> >> 在 2023/9/28 07:20, Andrii Nakryiko 写道: >>> On Mon, Sep 25, 2023 at 3:56 AM 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 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 >>>> >>>> 2. iterating all threads in the system >>>> >>>> 3. iterating all threads of a specific task >>>> Here we also resuse enum bpf_iter_task_type and rename BPF_TASK_ITER_TID >>>> to BPF_TASK_ITER_THREAD, rename BPF_TASK_ITER_TGID to BPF_TASK_ITER_PROC. >>>> >>>> The newly-added struct bpf_iter_task has a name collision with a selftest >>>> for the seq_file task iter's bpf skel, so the selftests/bpf/progs file is >>>> renamed in order to avoid the collision. >>>> >>>> Signed-off-by: Chuyi Zhou >>>> --- >>>> include/linux/bpf.h | 8 +- >>>> kernel/bpf/helpers.c | 3 + >>>> kernel/bpf/task_iter.c | 96 ++++++++++++++++--- >>>> .../testing/selftests/bpf/bpf_experimental.h | 5 + >>>> .../selftests/bpf/prog_tests/bpf_iter.c | 18 ++-- >>>> .../{bpf_iter_task.c => bpf_iter_tasks.c} | 0 >>>> 6 files changed, 106 insertions(+), 24 deletions(-) >>>> rename tools/testing/selftests/bpf/progs/{bpf_iter_task.c => bpf_iter_tasks.c} (100%) >>>> >>> [...] >>>> +get_next_task: >>>> + kit->pos = next_task(kit->pos); >>>> + kit->task = kit->pos; >>>> + if (kit->pos == &init_task) >>>> + kit->pos = NULL; >>> >>> I can't say I completely follow the logic (e.g., for >>> BPF_TASK_ITER_PROC, why do we do next_task() on first next() call)? >>> Can you elabore the expected behavior for various combinations of >>> types and starting task argument? >>> >> >> Thanks for the review. >> >> The expected behavior of current implementation is: >> >> BPF_TASK_ITER_PROC: >> >> init_task->first_process->second_process->...->last_process->init_task >> >> We would exit before visiting init_task again. > > ah, ok, so in this case it's more like BPF_TASK_ITER_ALL_PROCS, i.e., > we iterate all processes in the system. Input `task` that we provide > is ignored/meaningless, right? Maybe we should express it as > ALL_PROCS? > >> >> BPF_TASK_ITER_THREAD: >> >> group_task->first_thread->second_thread->...->last_thread->group_task >> >> We would exit before visiting group_task again. >> > > And this one is iterating threads of a process specified by given > `task`, right? This is where my confusion comes from. ITER_PROC and > ITER_THREAD, by their name, seems to be very similar, but in reality > ITER_PROC is more like ITER_ALL (except process vs thread iteration), > while ITER_THREAD is parameterized by input `task`. > > I'm not sure what's the least confusing way to name and organize > everything, but I think it's quite confusing right now, unfortunately. > I wonder if you or someone else have a better suggestion on making > this more straightforward? > Maybe here we can introduce new enums and not reuse or rename BPF_TASK_ITER_TID/BPF_TASK_ITER_TGID? { BPF_TASK_ITER_ALL_PROC, BPF_TASK_ITER_ALL_THREAD, BPF_TASK_ITER_THREAD } BPF_TASK_ITER_TID/BPF_TASK_ITER_TGID are inner flags. Looking at the example usage of SEC("iter/task"), unlike BPF_CGROUP_ITER_DESCENDANTS_PRE/BPF_CGROUP_ITER_DESCENDANTS_POST, we actually don't use BPF_TASK_ITER_TID/BPF_TASK_ITER_TGID directly. When using SEC("iter/task"), we just set pid/tid for struct bpf_iter_link_info. Exposing new enums to users for open coded task_iters will not confuse users. Thanks.