From patchwork Sun Aug 27 07:20:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13366851 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBDABED4 for ; Sun, 27 Aug 2023 07:21:09 +0000 (UTC) Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBDEA12F for ; Sun, 27 Aug 2023 00:21:07 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1bf1935f6c2so14826625ad.1 for ; Sun, 27 Aug 2023 00:21:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1693120867; x=1693725667; 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=PzhP0acP8pgCXiXgIT5pVDiQStW31xdVy7iDR4WyW0I=; b=M9uriV0oDBou+phHxwXSY1DpUBh3pU1M/amkzlMTqxk9KoPkdJKhVfH3Rn1tQehoiQ bS8Y6ukSay/A9Ig3ySf4VRCNbrDtYgH+rcSaMsijUIWxUDsoC2JYZlU31EOz1IQIs3jo gJUTIO3q9S036zcZO7czp/RhlSHXeT7gL03yOZhrgQ3DU/kso1Ho5Caujr+JI7tFLHYB vF/JHYfX84frp/fDHZMmJUUdlO3FLYP9w0hATmfbDw+KH78Mu6QwlprSaZh91UQ124Xp 1gCtbNxv1x+wCrd4y8ZHWHDZqwlp26qNCJgtylgkP/7wTwQZwUqX9HiGjjeAeOhZLIGe fWcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693120867; x=1693725667; 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=PzhP0acP8pgCXiXgIT5pVDiQStW31xdVy7iDR4WyW0I=; b=EJitfh2Nzz94h8MxNX4vhL7emotMHzSEXlJFccrxgdeEJQt7DB+or/0rHWAN4yrlBy PzomGKUDci60G3+9o8q2kSKm9KQVTIlzn/51Z9PS8iimZBWe96jqhxne6J4cs+gfOm/G Z0j4ZchxVK6lL7mCSzDkX3x892RrGX2MIGWBpP/ugZ/9m7NWhooD6ykFik9jWxpbxrWZ 2X3YQq1RK8K5N4O033cqxcykCttDF0lD4N8xbY7RTSEaKv4xALL89I8V65bJgVKV+9bw aBV1BzRlhjTMeO0E0xhhzwCMpJAt/Ni/5QunY9wKTnn9cah8RLOZ+o+TgcVT7qNn/BTH oWow== X-Gm-Message-State: AOJu0YzaJOqofRi0u/9/B875BEu89PujinsvxaKd8JONx9/6xIFaxKYT uKLDh+R/+Db66Jb/z1AYioEu/bf1+Zkc/MdnQ0s= X-Google-Smtp-Source: AGHT+IGabF3YBXqt6mcOkwn094X/QhnZ4R6HHN1HhEJhKSefItAGmzR4qpXEmdGvbs71ifeR+ZMagw== X-Received: by 2002:a17:902:ecc4:b0:1c0:cbaf:6954 with SMTP id a4-20020a170902ecc400b001c0cbaf6954mr13191830plh.25.1693120867032; Sun, 27 Aug 2023 00:21:07 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id m3-20020a1709026bc300b001befac3b3cbsm4769723plt.290.2023.08.27.00.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 00:21:06 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [RFC PATCH bpf-next 1/4] bpf: Introduce css_task open-coded iterator kfuncs Date: Sun, 27 Aug 2023 15:20:54 +0800 Message-Id: <20230827072057.1591929-2-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230827072057.1591929-1-zhouchuyi@bytedance.com> References: <20230827072057.1591929-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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. Signed-off-by: Chuyi Zhou --- include/uapi/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 3 +++ kernel/bpf/task_iter.c | 39 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 4 ++++ tools/lib/bpf/bpf_helpers.h | 7 ++++++ 5 files changed, 57 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 60a9d59beeab..2a6e9b99564b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7195,4 +7195,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 9e80efa59a5d..cf113ad24837 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2455,6 +2455,9 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) 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 c4ab9d6cdbe9..b1bdba40b684 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -823,6 +823,45 @@ const struct bpf_func_proto bpf_find_vma_proto = { .arg5_type = 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 = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) != + __alignof__(struct bpf_iter_css_task)); + + kit->css_it = kzalloc(sizeof(struct css_task_iter), GFP_KERNEL); + 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_css_task *it) +{ + struct bpf_iter_css_task_kern *kit = (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 = (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/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 60a9d59beeab..2a6e9b99564b 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7195,4 +7195,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 bbab9ad9dc5a..f4d74b2aaddd 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -302,6 +302,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) __weak __ksym; +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; + #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 explicit From patchwork Sun Aug 27 07:20:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13366852 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 404341361 for ; Sun, 27 Aug 2023 07:21:12 +0000 (UTC) Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CDC7123 for ; Sun, 27 Aug 2023 00:21:11 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1bf3a2f44ffso17768615ad.1 for ; Sun, 27 Aug 2023 00:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1693120870; x=1693725670; 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=FBwoJcGBnb1veA8KnUD3qT8I054w63hMR1j00YfM/x8=; b=BeWq9KckrnODaNNHCG1z2qbWpWBcfLgX3L/Qu1m7FTHWPA+7aJObjrZT73pVfo2VQ8 PDvOBcCdDGGMGy/4d6NJxGWnu+YBTX1E0z1sl3g55fY0UN+PKURSmFn5mqWUkhsNojOk akXAIq2Jlhx+Isuab+SvAQsA1VU3vnDp6a6fdTdrlEXiec8bKgUAcJc7Kwr/EJujGRIe XUz/5cuve3mM8dEsFn9UvBIGOScMhyDqYU+uANsNnx0oZ7jp04gs/ycrTUW0btrbmCdD zKNLa7SWbPL7dinpaXVNCYfvltjTs/MngWa0W3rrVLzD61mObJp+9goc+D2VT2Y+mGeO 9S1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693120870; x=1693725670; 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=FBwoJcGBnb1veA8KnUD3qT8I054w63hMR1j00YfM/x8=; b=g2bEvrg5pPGbNBXzLByXIIbD0NOPOsOGDuR8hL6PMw+4hVQzqieLUAq6DVfeO9LWB9 6BhZ7Nqg1cu10Nc1RFCtRFMRJnIYeGE2GmTS1fKIZGyh6SuZaKPmYYSpP5yDj/ZojatM haQp2g+OwI6fK2QtMcIc65o1PncZVHdx9Clknsd+edy69fRervM6g6LTnVvKmQ3Nn8ZN 1jdxK0aE1TYnKwzHVj8mUwHZTiM0CimZC2FF+yJn0XMwh+RVv+Foa3ugK1ImK/xamwMI sdTWnrkipZ0pF7FpA0nI8UAWJI0XTCJSIPfzFIsaoPyszbAn5TPwfZyyZv3a4frjeOMv k2ig== X-Gm-Message-State: AOJu0Ywe8LVduLWV5K2iqd0xD7CdmPbZ5JdK1mPFfwda+51lVRzqz5wq xf2HauOOxjisn+g1P9hiL8z0MhfeNq93LHUJChg= X-Google-Smtp-Source: AGHT+IHiEPvFBXe3lELVZXbNjUVfpsIF2DxyqTE1VuJNCcAsiC8D9F7OTFyPQRkKAGq/03B8V+Sj7w== X-Received: by 2002:a17:902:ced0:b0:1bb:97d0:c628 with SMTP id d16-20020a170902ced000b001bb97d0c628mr27739664plg.31.1693120870474; Sun, 27 Aug 2023 00:21:10 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id m3-20020a1709026bc300b001befac3b3cbsm4769723plt.290.2023.08.27.00.21.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 00:21:10 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [RFC PATCH bpf-next 2/4] bpf: Introduce process open coded iterator kfuncs Date: Sun, 27 Aug 2023 15:20:55 +0800 Message-Id: <20230827072057.1591929-3-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230827072057.1591929-1-zhouchuyi@bytedance.com> References: <20230827072057.1591929-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds kfuncs bpf_iter_process_{new,next,destroy} which allow creation and manipulation of struct bpf_iter_process in open-coded iterator style. BPF programs can use these kfuncs or through bpf_for_each macro to iterate all processes in the system. Signed-off-by: Chuyi Zhou --- include/uapi/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 3 +++ kernel/bpf/task_iter.c | 31 +++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 4 ++++ tools/lib/bpf/bpf_helpers.h | 5 +++++ 5 files changed, 47 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 2a6e9b99564b..cfbd527e3733 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7199,4 +7199,8 @@ struct bpf_iter_css_task { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_process { + __u64 __opaque[1]; +} __attribute__((aligned(8))); + #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cf113ad24837..81a2005edc26 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2458,6 +2458,9 @@ 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_iter_process_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_process_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_process_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 b1bdba40b684..a6717a76c1e0 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -862,6 +862,37 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it) kfree(kit->css_it); } +struct bpf_iter_process_kern { + struct task_struct *tsk; +} __attribute__((aligned(8))); + +__bpf_kfunc int bpf_iter_process_new(struct bpf_iter_process *it) +{ + struct bpf_iter_process_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_process_kern) != sizeof(struct bpf_iter_process)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_process_kern) != + __alignof__(struct bpf_iter_process)); + + rcu_read_lock(); + kit->tsk = &init_task; + return 0; +} + +__bpf_kfunc struct task_struct *bpf_iter_process_next(struct bpf_iter_process *it) +{ + struct bpf_iter_process_kern *kit = (void *)it; + + kit->tsk = next_task(kit->tsk); + + return kit->tsk == &init_task ? NULL : kit->tsk; +} + +__bpf_kfunc void bpf_iter_process_destroy(struct bpf_iter_process *it) +{ + rcu_read_unlock(); +} + 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/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 2a6e9b99564b..cfbd527e3733 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7199,4 +7199,8 @@ struct bpf_iter_css_task { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_process { + __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 f4d74b2aaddd..7d6a828d98b5 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -309,6 +309,11 @@ 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_process; +extern int bpf_iter_process_new(struct bpf_iter_process *it) __weak __ksym; +extern struct task_struct *bpf_iter_process_next(struct bpf_iter_process *it) __weak __ksym; +extern void bpf_iter_process_destroy(struct bpf_iter_process *it) __weak __ksym; + #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 explicit From patchwork Sun Aug 27 07:20:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13366853 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0550217E4 for ; Sun, 27 Aug 2023 07:21:16 +0000 (UTC) Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC29712A for ; Sun, 27 Aug 2023 00:21:14 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1bdbbede5d4so17959085ad.2 for ; Sun, 27 Aug 2023 00:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1693120874; x=1693725674; 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=Aaba14g0pYwSo+C1GRPKvs5a7tFEGv787StoXtTJ820=; b=JwVbln6N5wR5/SkAR4HytFsak9tIqVeQnyIIlX5+rht8o2CZ9ArLClTIB8PiC9IsFc McY+zXbts1fQ4YlKjmgkVwMznw+jaHyLo1tPnqd0gqNqtca+NN5Rxu7vbMsGZhv7RLU8 kfQ70FIBr0eeu0n3G2iYVdMxdedqx/DGMHzkZQHuTOj4vPipKdA4dbjCfFQl3DbJc9pU emxbxeVWpLLA04yIW6BD8boa1+H/ZnFTTm814e2gc5R0CovgFNJ8UpxJvPKf78AKgKM5 XQ9hlAXT37NRxQPHe2TPRG6SXmskGf9LbkYfJDcc4ldEWnm64hrmF0XS32qoNtHbvKgC EQpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693120874; x=1693725674; 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=Aaba14g0pYwSo+C1GRPKvs5a7tFEGv787StoXtTJ820=; b=QazCTPELybEX7hVBooamYmkMqMDkTj+hXIWVRwmZlZdzz/p1LYGXCXRWExWunkbgeo RiNiYSTr5uptMGshePg2eKWFVLVk7BjQ2aH+nJmbkhB47YdHnoLU3hz0O0dLEQiXi4AY Cwx2rGK411AcU00sT0rZsMSxiyKy/JzKJRCJKE5sSHhGHRghWdPg6AcxyyhEhPEhnZeL U/G7ggcxf7EDjK5WkkuBq0RZmFYWLYX9c88jrYwwnDvXSjjqfty5FNqjJ/jrpHOjrecX UjnB25bZMus4zIm57+c9uqpjDOmIrM2pR34rHS0PXEA6bf9uM5nclBf733d6s8yaZe3H uUUg== X-Gm-Message-State: AOJu0YxTK+MS/n1p+HWlq4S0ZB04Jvd2EYpHHTS29sMiviBszJiHHGYK YJRHkSxHDDFy/hm3JEYSdgtRvqibLnN0qk9qvvA= X-Google-Smtp-Source: AGHT+IES0aZIVeC31/U5wz+SDgtdO6clWH2gS+rxwgpQr0NFr3Q721xrDf7OcxpufCUAkp3hGCZ4Og== X-Received: by 2002:a17:902:f54b:b0:1b8:92fc:7429 with SMTP id h11-20020a170902f54b00b001b892fc7429mr27897781plf.53.1693120873965; Sun, 27 Aug 2023 00:21:13 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id m3-20020a1709026bc300b001befac3b3cbsm4769723plt.290.2023.08.27.00.21.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 00:21:13 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [RFC PATCH bpf-next 3/4] bpf: Introduce css_descendant open-coded iterator kfuncs Date: Sun, 27 Aug 2023 15:20:56 +0800 Message-Id: <20230827072057.1591929-4-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230827072057.1591929-1-zhouchuyi@bytedance.com> References: <20230827072057.1591929-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This Patch adds kfuncs bpf_iter_css_{new,next,destroy} which allow creation and manipulation of struct bpf_iter_css in open-coded iterator style. These kfuncs actually wrapps css_next_descendant_{pre, post}. BPF programs can use these kfuncs through bpf_for_each macro for iteration of all descendant css under a root css. Normally, css_next_descendant_{pre, post} should be called with rcu locking. Although we have bpf_rcu_read_lock(), here we still calls rcu_read_lock in bpf_iter_css_new and unlock in bpf_iter_css_destroy for convenience use. Signed-off-by: Chuyi Zhou --- include/uapi/linux/bpf.h | 5 +++++ kernel/bpf/helpers.c | 3 +++ kernel/bpf/task_iter.c | 39 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ tools/lib/bpf/bpf_helpers.h | 6 ++++++ 5 files changed, 58 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index cfbd527e3733..19f1f1bf9301 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7203,4 +7203,9 @@ struct bpf_iter_process { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_css { + __u64 __opaque[2]; + char __opaque_c[1]; +} __attribute__((aligned(8))); + #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 81a2005edc26..47d46a51855f 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2461,6 +2461,9 @@ BTF_ID_FLAGS(func, bpf_iter_css_task_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_process_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_process_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_process_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_css_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_css_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_css_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 a6717a76c1e0..ef9aef62f1ac 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -893,6 +893,45 @@ __bpf_kfunc void bpf_iter_process_destroy(struct bpf_iter_process *it) rcu_read_unlock(); } +struct bpf_iter_css_kern { + struct cgroup_subsys_state *root; + struct cgroup_subsys_state *pos; + char flag; +} __attribute__((aligned(8))); + +__bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, + struct cgroup_subsys_state *root, char flag) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_css_kern) != __alignof__(struct bpf_iter_css)); + kit->root = root; + kit->pos = NULL; + kit->flag = flag; + rcu_read_lock(); + return 0; +} + +__bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + if (!kit->pos) + kit->pos = kit->flag ? css_next_descendant_post(NULL, kit->root) + : css_next_descendant_pre(NULL, kit->root); + else + kit->pos = kit->flag ? css_next_descendant_post(kit->pos, kit->root) + : css_next_descendant_pre(kit->pos, kit->root); + + return kit->pos; +} + +__bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) +{ + rcu_read_unlock(); +} + 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/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index cfbd527e3733..19f1f1bf9301 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7203,4 +7203,9 @@ struct bpf_iter_process { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_css { + __u64 __opaque[2]; + char __opaque_c[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 7d6a828d98b5..bb56295b1442 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -314,6 +314,12 @@ extern int bpf_iter_process_new(struct bpf_iter_process *it) __weak __ksym; extern struct task_struct *bpf_iter_process_next(struct bpf_iter_process *it) __weak __ksym; extern void bpf_iter_process_destroy(struct bpf_iter_process *it) __weak __ksym; +struct bpf_iter_css; +extern int bpf_iter_css_new(struct bpf_iter_css *it, + struct cgroup_subsys_state *root, char flag) __weak __ksym; +extern struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) __weak __ksym; +extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym; + #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 explicit From patchwork Sun Aug 27 07:20:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13366854 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63BAE20EC for ; Sun, 27 Aug 2023 07:21:19 +0000 (UTC) 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 EA0B212A for ; Sun, 27 Aug 2023 00:21:17 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-68c0d262933so1108534b3a.0 for ; Sun, 27 Aug 2023 00:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1693120877; x=1693725677; 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=VuhzP1mvDliga8ZbFq7tNuDQGJoX13wAFAeRiusEr7I=; b=SOl9rzX3hguTSxfuEkxfuXzVtNZ5qfD4ojQw7O7tIc1ndHCwJzXzN1T2XQ+ytML/0b MEZIW+DY7E8Y7hyI21eH9SNzkdIssGL6M12YDMq2Sh38jPO5YBh40rU9DzZbH0ZxJeEm zPJAbJ7xblpXB3fgSUZ61l49B6+dr43mnNzZRa0C+YaCTYzEr11HnaS8qitqPWj/rRgX WqE2+CB/EbpZyZl9A/DIIpd7WkclPFyYjRWuTTSWXX+YEmSsiHT2P1vmpXj0PmgYqtbJ dxkIELZunwXiauQyfEuraD/tE5y3JExEZ556CWusZ+bKpVsOppUSI55WEN7mJLkyE9yf 2kaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693120877; x=1693725677; 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=VuhzP1mvDliga8ZbFq7tNuDQGJoX13wAFAeRiusEr7I=; b=LZ1A97apyivveU+TV4k+jWgPP109Ko9KXpv61IR0isVJibyf96SdKVYnDD0udFEi9X v7b8D6d8ZYPAsehelAlhZQgoDAje3FPMo0wMOSVQUk+gECRP6oZyIGVJ0ZUq9bFZdx+6 v9+AdPwYEiG6Z3i+uuuQzVTGEDggFPt18K/U/j2LXPCU3IqrpgKXNHlHQOZTUx9xyGEA aQXgKvWhFsR/0B4aACi2BvYbmFQUJuvXbYIfzPWljROA7TcRX2zE4SPP8l/QwJi5WnKd /pM2bJUhcZJc3Srl5U9e6OdlpIBpAigA/IUYCYpPmhtwt0reUA0xCvOCnuvppn4TL19c icow== X-Gm-Message-State: AOJu0YwJrS1rZ6YHlG/nyCKfbDbhfEYKaB7xrLzhG9X/GO8y5F4Rql/n BRvaBr0yUej/Rg70x2HI6CgX2PHQBXcF2weSq4E= X-Google-Smtp-Source: AGHT+IGWV2gD2RUmv9++GcZpjEOn0s1qVF1eTnrQZ8zvHW1Ywv6QKs+mggUI3tHsWbQu7TX5S5PCDg== X-Received: by 2002:a17:903:1212:b0:1b0:3df7:5992 with SMTP id l18-20020a170903121200b001b03df75992mr21746419plh.32.1693120877251; Sun, 27 Aug 2023 00:21:17 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id m3-20020a1709026bc300b001befac3b3cbsm4769723plt.290.2023.08.27.00.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 00:21:17 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [RFC PATCH bpf-next 4/4] selftests/bpf: Add tests for open-coded task and css iter Date: Sun, 27 Aug 2023 15:20:57 +0800 Message-Id: <20230827072057.1591929-5-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230827072057.1591929-1-zhouchuyi@bytedance.com> References: <20230827072057.1591929-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds three tests to demonstrate these patterns and validating correctness. test1: We use bpf_for_each(process, task) to iterate all processed in the system and search for the current process with a given pid. test2: We create a cgroup and add the current process to the cgroup. In the BPF program, we would use bpf_for_each(css_task, task, css) to iterate all tasks under the cgroup. As expected, we would find the current process. test3: We create a cgroup tree. In the BPF program, we use bpf_for_each(css, pos, root) to iterate all descendant under the root with post order. As expected, we would find all descendant and the last iterating cgroup is root cgroup. Signed-off-by: Chuyi Zhou --- .../testing/selftests/bpf/prog_tests/iters.c | 123 ++++++++++++++++++ .../testing/selftests/bpf/progs/iters_task.c | 83 ++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/iters_task.c diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index 10804ae5ae97..88fb565a3e97 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -2,12 +2,14 @@ /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ #include +#include "cgroup_helpers.h" #include "iters.skel.h" #include "iters_state_safety.skel.h" #include "iters_looping.skel.h" #include "iters_num.skel.h" #include "iters_testmod_seq.skel.h" +#include "iters_task.skel.h" static void subtest_num_iters(void) { @@ -90,6 +92,121 @@ static void subtest_testmod_seq_iters(void) iters_testmod_seq__destroy(skel); } +static void subtest_process_iters(void) +{ + struct iters_task *skel; + int err; + + skel = iters_task__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + goto cleanup; + bpf_program__set_autoload(skel->progs.iter_task_for_each, true); + err = iters_task__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + skel->bss->target_pid = getpid(); + err = iters_task__attach(skel); + if (!ASSERT_OK(err, "iters_task__attach")) + goto cleanup; + + iters_task__detach(skel); + ASSERT_EQ(skel->bss->process_cnt, 1, "process_cnt"); + +cleanup: + iters_task__destroy(skel); +} + +static void subtest_css_task_iters(void) +{ + struct iters_task *skel; + int err, cg_fd, cg_id; + const char *cgrp_path = "/cg1"; + + err = setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + goto cleanup; + cg_fd = create_and_get_cgroup(cgrp_path); + if (!ASSERT_GE(cg_fd, 0, "cg_create")) + goto cleanup; + cg_id = get_cgroup_id(cgrp_path); + err = join_cgroup(cgrp_path); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + goto cleanup; + + skel = iters_task__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + goto cleanup; + + bpf_program__set_autoload(skel->progs.iter_css_task_for_each, true); + err = iters_task__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + skel->bss->target_pid = getpid(); + skel->bss->cg_id = cg_id; + err = iters_task__attach(skel); + + if (!ASSERT_OK(err, "iters_task__attach")) + goto cleanup; + + iters_task__detach(skel); + ASSERT_EQ(skel->bss->css_task_cnt, 1, "css_task_cnt"); + +cleanup: + cleanup_cgroup_environment(); + iters_task__destroy(skel); +} + +static void subtest_css_dec_iters(void) +{ + struct iters_task *skel; + struct { + const char *path; + int fd; + } cgs[] = { + { "/cg1" }, + { "/cg1/cg2" }, + { "/cg1/cg2/cg3" }, + { "/cg1/cg2/cg3/cg4" }, + }; + int err, cg_nr = ARRAY_SIZE(cgs); + int i; + + err = setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + goto cleanup; + for (i = 0; i < cg_nr; i++) { + cgs[i].fd = create_and_get_cgroup(cgs[i].path); + if (!ASSERT_GE(cgs[i].fd, 0, "cg_create")) + goto cleanup; + } + + skel = iters_task__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + goto cleanup; + bpf_program__set_autoload(skel->progs.iter_css_dec_for_each, true); + err = iters_task__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + skel->bss->target_pid = getpid(); + skel->bss->cg_id = get_cgroup_id(cgs[0].path); + + err = iters_task__attach(skel); + + if (!ASSERT_OK(err, "iters_task__attach")) + goto cleanup; + + iters_task__detach(skel); + ASSERT_EQ(skel->bss->css_dec_cnt, cg_nr, "post order search dec count"); + ASSERT_EQ(skel->bss->last_cg_id, get_cgroup_id(cgs[0].path), + "post order search last cgroup id"); + +cleanup: + cleanup_cgroup_environment(); + iters_task__destroy(skel); +} + void test_iters(void) { RUN_TESTS(iters_state_safety); @@ -103,4 +220,10 @@ void test_iters(void) subtest_num_iters(); if (test__start_subtest("testmod_seq")) subtest_testmod_seq_iters(); + if (test__start_subtest("process")) + subtest_process_iters(); + if (test__start_subtest("css_task")) + subtest_css_task_iters(); + if (test__start_subtest("css_dec")) + subtest_css_dec_iters(); } diff --git a/tools/testing/selftests/bpf/progs/iters_task.c b/tools/testing/selftests/bpf/progs/iters_task.c new file mode 100644 index 000000000000..524926b505b0 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task.c @@ -0,0 +1,83 @@ +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +pid_t target_pid = 0; +int process_cnt = 0; +int css_task_cnt = 0; +int css_dec_cnt = 0; + +u64 cg_id; +u64 last_cg_id; + +struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym; +struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp) __ksym; +void bpf_cgroup_release(struct cgroup *p) __ksym; + +SEC("?tp_btf/sys_enter") +int BPF_PROG(iter_task_for_each) +{ + struct task_struct *task; + struct task_struct *cur_task = bpf_get_current_task_btf(); + + if (cur_task->pid != target_pid) + return 0; + + bpf_for_each(process, task) + if (task->pid == target_pid) + process_cnt += 1; + + return 0; +} + +SEC("?tp_btf/sys_enter") +int iter_css_task_for_each(const void *ctx) +{ + struct task_struct *task; + struct task_struct *cur_task = bpf_get_current_task_btf(); + + if (cur_task->pid != target_pid) + return 0; + + struct cgroup *cgrp = bpf_cgroup_from_id(cg_id); + + if (cgrp == NULL) + return 0; + struct cgroup_subsys_state *css = &cgrp->self; + + bpf_for_each(css_task, task, css, 0) + if (task->pid == target_pid) + css_task_cnt += 1; + + bpf_cgroup_release(cgrp); + return 0; +} + +SEC("?tp_btf/sys_enter") +int iter_css_dec_for_each(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + bool is_post_order = true; + + if (cur_task->pid != target_pid) + return 0; + + struct cgroup *cgrp = bpf_cgroup_from_id(cg_id); + + if (cgrp == NULL) + return 0; + struct cgroup_subsys_state *root = &cgrp->self; + struct cgroup_subsys_state *pos = NULL; + + bpf_for_each(css, pos, root, is_post_order) { + struct cgroup *cur_cgrp = pos->cgroup; + + css_dec_cnt += 1; + if (cur_cgrp) + last_cg_id = cur_cgrp->kn->id; + } + bpf_cgroup_release(cgrp); + return 0; +}