Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp2714071rdb; Tue, 12 Sep 2023 09:51:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKyFqrgbiEfpPHVi7Kpx/UalpLSHjocS9a3+tuVZezwws7+uZNrIGUBcqPucoTqyIQ8qve X-Received: by 2002:a17:903:234f:b0:1c3:aee0:7d27 with SMTP id c15-20020a170903234f00b001c3aee07d27mr315697plh.24.1694537471591; Tue, 12 Sep 2023 09:51:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694537471; cv=none; d=google.com; s=arc-20160816; b=DiWIo5jJ1HQm9uRfcG+9jSYFLpXLkU94Otd2y7gniC4UkvKhvQ6OM6T9aqTfcxyRGg EvKhTS3GVkpO759HHq8c8CFI/PH+dS2O2zydxmC8ZIHt1iIOAqACxGlC2MV3zsxjM22Q 85ZmR4QWWfe8rUfvulawcKCIX4CLQW0czRmaxcuPK787KrSuGxRk0G+vPDP2dzS3GG9B yVLD0w9Esn/skfcTGld2T1ueox28SOSpd2B2MJNJSEqqIU+Z18jWN6BNXMo44N9YZm+1 RCs5FztN64CEaii4sWCAzWYKdyLknkkrTh/MLEGOvu6+v/Hc54DKgrFDATpb6LLY4tlo Wp3Q== 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=zbzquHAnwok7U8VH5ASiuRWtN2QtgmEjZHX4A6sEdQ0=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=Aimknu35kC+YEXoLu5S+mj89ucaNye3/pce0m6v39RD5kdrLoEPDhFf6bM9dNRDuGg 4mgawfoi/MQzDcgmXN+vA1yakIiGTVJvAXmWwjEY01d0ZQFZmsPZZ8F424OWGZpF7ocl mn6VDynrqMLj9CkgZXSJmJBIV1C3yyWek6v7DXm2ftTEKm1CPmluquBgn3l9TUd7OMcy 4VHaBSdFgNl4aFJTN8iLclNds8tTNc+eAHiTDw6Uvg7wdALDkfYgMXWtXzRziFo5M6LU jklucPSqbZ68AtcWiRGaxuPXZgIs+hDVKn0ANDFXvFDIWBFx/b7W8/jCvLMdYhXvYEAw 4XXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b="EgE/SEvh"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id q13-20020a170902a3cd00b001bbf293f45esi8078703plb.625.2023.09.12.09.51.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 09:51:11 -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=@bytedance.com header.s=google header.b="EgE/SEvh"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id E6AA481F356A; Tue, 12 Sep 2023 00:02:51 -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 S231324AbjILHCq (ORCPT + 99 others); Tue, 12 Sep 2023 03:02:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231253AbjILHCj (ORCPT ); Tue, 12 Sep 2023 03:02:39 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6625EE75 for ; Tue, 12 Sep 2023 00:02:13 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1c336f3f449so45836235ad.3 for ; Tue, 12 Sep 2023 00:02:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1694502133; x=1695106933; 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=zbzquHAnwok7U8VH5ASiuRWtN2QtgmEjZHX4A6sEdQ0=; b=EgE/SEvhM6y7H0wOdoJWkDzV29CKcPkS3iWL8GssBkcc0O9ymUk5IhMnFKhvYVz7M0 OhAQpdsl5k1naQB1oRrSa/OXFxDDPGjdfgZG/ECrAQa2+x71LlfB7pXbFUe4Ox/t9F77 cLIcHTy2RaBomVkoR/Kljy5ollN4wNcDVT6/jAE5Xi/aZtEhd7wzp8WKSN2XV4qi4hSs 7+fs9Ifk/Us6R8GOVpjdC57+grmarGBjpPdJIKcUIgqzTPIyM5tMTYV4ZqmplsSyMVsH bQC6P6th9E06JuHAEDCV018vo8zzU5d4N6htKV8QC2a9LDXHUL8vF3u7yKRamWZKRcZT S5GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694502133; x=1695106933; 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=zbzquHAnwok7U8VH5ASiuRWtN2QtgmEjZHX4A6sEdQ0=; b=PU+0fSV6O57JJVoAyImX/GKH/zcZpQn3CUjyOIeKr9w47axpbg4mm623g5DG2M5ItI d7DHvgVB6y6iixhkwQ6WI+Wnc8vLGly8poOjqetvq2/zdIEajtQoHLDJZ2wAc7K4+ME3 goN8WT8Cz0rVc79Xo/KjEWrFCaSW0ckm3LBYue2R4LPC1GqTLS3iOPA55dQnycmWjUjP 882tbZK1VxwCmfJbIyQB3QzooElJL2sRQGVjO4/2p8uxwduEy8MKsxekMz5ash1tQEBq V9JySNdx/oxhdpFBdc+Ip5E3zX2l7yFeCPL3U+xImsqUphmRL4Q6MfdnucoQUnN1P0DJ Dz7A== X-Gm-Message-State: AOJu0YwxNJ5bO26ZeQQtvqVIUJVD6gWyDnYOqWNaWiT6VfjIhSm8Eanu 5rMDsTzdyHkSIldgOfn8oK/YdD+JrzOAqfmacv8OBg== X-Received: by 2002:a17:902:8693:b0:1b8:66f6:87a3 with SMTP id g19-20020a170902869300b001b866f687a3mr10957357plo.52.1694502132925; Tue, 12 Sep 2023 00:02:12 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.84.173]) by smtp.gmail.com with ESMTPSA id b8-20020a170902d50800b001b8953365aesm7635401plg.22.2023.09.12.00.02.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 00:02:12 -0700 (PDT) From: Chuyi Zhou 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 Subject: [PATCH bpf-next v2 4/6] bpf: Introduce css_descendant open-coded iterator kfuncs Date: Tue, 12 Sep 2023 15:01:47 +0800 Message-Id: <20230912070149.969939-5-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230912070149.969939-1-zhouchuyi@bytedance.com> References: <20230912070149.969939-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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]); Tue, 12 Sep 2023 00:02:52 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 This Patch adds kfuncs bpf_iter_css_{pre,post}_{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. Signed-off-by: Chuyi Zhou --- include/uapi/linux/bpf.h | 8 +++++ kernel/bpf/helpers.c | 6 ++++ kernel/bpf/task_iter.c | 53 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 8 +++++ tools/lib/bpf/bpf_helpers.h | 12 ++++++++ 5 files changed, 87 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index befa55b52e29..57760afc13d0 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7326,4 +7326,12 @@ struct bpf_iter_process { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_css_pre { + __u64 __opaque[2]; +} __attribute__((aligned(8))); + +struct bpf_iter_css_post { + __u64 __opaque[2]; +} __attribute__((aligned(8))); + #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 9b7d2c6f99d1..ca1f6404af9e 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2510,6 +2510,12 @@ 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_pre_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_css_pre_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_css_pre_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_css_post_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_css_post_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_css_post_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 9d1927dc3a06..8963fc779b87 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -880,6 +880,59 @@ __bpf_kfunc void bpf_iter_process_destroy(struct bpf_iter_process *it) { } +struct bpf_iter_css_kern { + struct cgroup_subsys_state *root; + struct cgroup_subsys_state *pos; +} __attribute__((aligned(8))); + +__bpf_kfunc int bpf_iter_css_pre_new(struct bpf_iter_css_pre *it, + struct cgroup_subsys_state *root) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css_pre)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_css_kern) != __alignof__(struct bpf_iter_css_pre)); + kit->root = root; + kit->pos = NULL; + return 0; +} + +__bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_pre_next(struct bpf_iter_css_pre *it) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + kit->pos = css_next_descendant_pre(kit->pos, kit->root); + return kit->pos; +} + +__bpf_kfunc void bpf_iter_css_pre_destroy(struct bpf_iter_css_pre *it) +{ +} + +__bpf_kfunc int bpf_iter_css_post_new(struct bpf_iter_css_post *it, + struct cgroup_subsys_state *root) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css_post)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_css_kern) != __alignof__(struct bpf_iter_css_post)); + kit->root = root; + kit->pos = NULL; + return 0; +} + +__bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_post_next(struct bpf_iter_css_post *it) +{ + struct bpf_iter_css_kern *kit = (void *)it; + + kit->pos = css_next_descendant_post(kit->pos, kit->root); + return kit->pos; +} + +__bpf_kfunc void bpf_iter_css_post_destroy(struct bpf_iter_css_post *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 befa55b52e29..57760afc13d0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7326,4 +7326,12 @@ struct bpf_iter_process { __u64 __opaque[1]; } __attribute__((aligned(8))); +struct bpf_iter_css_pre { + __u64 __opaque[2]; +} __attribute__((aligned(8))); + +struct bpf_iter_css_post { + __u64 __opaque[2]; +} __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 858252c2641c..6e5bd9ef14d6 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -315,6 +315,18 @@ 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_pre; +extern int bpf_iter_css_pre_new(struct bpf_iter_css_pre *it, + struct cgroup_subsys_state *root) __weak __ksym; +extern struct cgroup_subsys_state *bpf_iter_css_pre_next(struct bpf_iter_css_pre *it) __weak __ksym; +extern void bpf_iter_css_pre_destroy(struct bpf_iter_css_pre *it) __weak __ksym; + +struct bpf_iter_css_post; +extern int bpf_iter_css_post_new(struct bpf_iter_css_post *it, + struct cgroup_subsys_state *root) __weak __ksym; +extern struct cgroup_subsys_state *bpf_iter_css_post_next(struct bpf_iter_css_post *it) __weak __ksym; +extern void bpf_iter_css_post_destroy(struct bpf_iter_css_post *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 -- 2.20.1