From patchwork Wed Oct 18 06:17:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 154700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4595043vqb; Tue, 17 Oct 2023 23:20:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH6EFtdxkbfwSbSLv+7/WyeotTfiX2jNbnEdenhR3z/V6i2lc9XAwJ93KdZ0XcLVaoAjXlF X-Received: by 2002:a05:6358:72a6:b0:166:d9c9:dbe with SMTP id w38-20020a05635872a600b00166d9c90dbemr4845971rwf.3.1697610032911; Tue, 17 Oct 2023 23:20:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697610032; cv=none; d=google.com; s=arc-20160816; b=AOTBbODJ/Ybn+ANrNkTrXsCt4d35cQWLf2ebsDAG8+RMVy9Q50+eUt3VIS6mN6uaFJ /aHMUZFR/g+eEWJ78obaNI/laM80aXGKUNkRbWEpezuxTBFq46BXq5kFXEPoWaTr/iwF 7Z/gUw6L2S5scZuBNn5P8P4v803ykR5PWdZnPwitpFSaasE+uVX0kpWkJrty3ImKIje0 Jv+C3mMWYfwACo3091XHY3/LHY8QgchRXkCa2EblhwdyjCqsQ/CxwZ1JAZDzS+HvYdIZ FRtNQDr69KzDC+Ya9o6jWlBsXxg2k9riPzUBDCuKtcmA56TkFsgOoyZbRUQxGk3XjBds AMUw== 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=AL8epgVpk2Z5NetWJuhkyFSgrHs5uVsKQ57V0KAcMww=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=bc3vdnm/iBVpLjI7OeiWNOwngg8ojKBLxKUnakc4Uv9hIJosWHH3T2gkM4S0tFbZlF h9W3bM55s4vKXPLv//9xTcraCRTcIWOEX5rg+Hk6qUyJ05Da593aY7jJJui0kzxIlbEu DnSerYItD64UA+FgESZ9UTUL74V9qa0BJ4eGecOqPBQKvWpcTdpzFwdC37FzJp8p71P4 T6WQa0nGBYJqyS2WFnwGdWIPjuX4YqM2cU9hJtSTCrpHBfpCqOyzRAiZwy2rFhuuRpyq M9tNHHVkUDShKP82/p0PEXMn6vK28UQKIT9w2Y6azUbtgAKEFhasa1e/c3ArgVUPlbkW 68pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Yv2ZdPMd; 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 fc14-20020a056a002e0e00b006bec15892c9si377930pfb.141.2023.10.17.23.20.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 23:20:32 -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=Yv2ZdPMd; 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 93E098149739; Tue, 17 Oct 2023 23:20:30 -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 S1344621AbjJRGUR (ORCPT + 23 others); Wed, 18 Oct 2023 02:20:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344515AbjJRGTS (ORCPT ); Wed, 18 Oct 2023 02:19:18 -0400 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 16A5210E3 for ; Tue, 17 Oct 2023 23:18:07 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1c0ecb9a075so44247805ad.2 for ; Tue, 17 Oct 2023 23:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1697609887; x=1698214687; 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=AL8epgVpk2Z5NetWJuhkyFSgrHs5uVsKQ57V0KAcMww=; b=Yv2ZdPMdTZqcA++WVr52++fTcyXkS+b38DTPX4BzDYC42khJIP7Jy+/onhnjxsPH7K o2tBwTDJEQPE9RXZaoQI/KEyGnN45sY13GDrNIT3rdAmMDuV9yvWv6dawMm6BMGuu2Q0 ZWb2RKk+DbRjr10UCtZyAHAY4ch+V6JNfyZeH9eFKPZyRi0ln+XnImQJH1MYp5+SFIg0 6E8C7xremEzEZirBGQoh1J9lZX28MjpevIW3jnCLGe9gNwdqjndtfPtY3QZNveWn1jaP 3iFwdvT9xAdioQVo0u7/X3AW0MVj3ubPW3cRaXENJajE5HIugR1AyJgy4pO5wnk0Nr/F Xzew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697609887; x=1698214687; 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=AL8epgVpk2Z5NetWJuhkyFSgrHs5uVsKQ57V0KAcMww=; b=IYHNuubJwcXXIkV342bOTa/sOQmifbCXMywLamdhEvKwCTURX0kLdxer4WCHJtGsZ2 T6z5ivCMW+jSdEDCCXvT4iEddvjJO7K4OM8KxEy+5vDImqsvNIIsUW3rUG5dKW0Ijkgv RxGoPFOpeEXC8hkMziQYbLS2as8TNv5SnAlNkqZGzcc9pJa4ZW4zfXQlNMYAnAD0rUbl stw7qF8a/p10hnT0ZGclx1gNNKVWQgDkJzXTkRmp0/HBgACmWwgff3BxA9qgCxm99C9e HtZMRGP2ZZhVYCDSrzCtbM2oVVDYYoBMrYsiQIjutC7oMIZ2cfJ8dhn188A+AsZI0Jh5 989g== X-Gm-Message-State: AOJu0Yya+lDtTXzTMSJ3i+t84tFXa2UyFFgZAhz7ZK/fmWqD0UGgLOFF 7KfCvnyuLyZDAKeAMbXr4gdCOQ== X-Received: by 2002:a17:902:fa8e:b0:1c9:d90b:c3e4 with SMTP id lc14-20020a170902fa8e00b001c9d90bc3e4mr4246104plb.10.1697609886810; Tue, 17 Oct 2023 23:18:06 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.103.200]) by smtp.gmail.com with ESMTPSA id ix13-20020a170902f80d00b001c61acd5bd2sm2659116plb.112.2023.10.17.23.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 23:18: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, tj@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [RESEND PATCH bpf-next v6 4/8] bpf: Introduce css open-coded iterator kfuncs Date: Wed, 18 Oct 2023 14:17:42 +0800 Message-Id: <20231018061746.111364-5-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231018061746.111364-1-zhouchuyi@bytedance.com> References: <20231018061746.111364-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 17 Oct 2023 23:20:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780073137848829999 X-GMAIL-MSGID: 1780073137848829999 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}. css_iter can be used to: 1) iterating a sepcific cgroup tree with pre/post/up order 2) iterating cgroup_subsystem in BPF Prog, like for_each_mem_cgroup_tree/cpuset_for_each_descendant_pre in kernel. The API design is consistent with cgroup_iter. bpf_iter_css_new accepts parameters defining iteration order and starting css. Here we also reuse BPF_CGROUP_ITER_DESCENDANTS_PRE, BPF_CGROUP_ITER_DESCENDANTS_POST, BPF_CGROUP_ITER_ANCESTORS_UP enums. Signed-off-by: Chuyi Zhou Acked-by: Tejun Heo --- kernel/bpf/cgroup_iter.c | 65 +++++++++++++++++++ kernel/bpf/helpers.c | 3 + .../testing/selftests/bpf/bpf_experimental.h | 6 ++ 3 files changed, 74 insertions(+) diff --git a/kernel/bpf/cgroup_iter.c b/kernel/bpf/cgroup_iter.c index 810378f04fbc..209e5135f9fb 100644 --- a/kernel/bpf/cgroup_iter.c +++ b/kernel/bpf/cgroup_iter.c @@ -294,3 +294,68 @@ static int __init bpf_cgroup_iter_init(void) } late_initcall(bpf_cgroup_iter_init); + +struct bpf_iter_css { + __u64 __opaque[3]; +} __attribute__((aligned(8))); + +struct bpf_iter_css_kern { + struct cgroup_subsys_state *start; + struct cgroup_subsys_state *pos; + unsigned int flags; +} __attribute__((aligned(8))); + +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global functions as their definitions will be in vmlinux BTF"); + +__bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, + struct cgroup_subsys_state *start, unsigned int flags) +{ + 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->start = NULL; + switch (flags) { + case BPF_CGROUP_ITER_DESCENDANTS_PRE: + case BPF_CGROUP_ITER_DESCENDANTS_POST: + case BPF_CGROUP_ITER_ANCESTORS_UP: + break; + default: + return -EINVAL; + } + + kit->start = start; + kit->pos = NULL; + kit->flags = flags; + 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->start) + return NULL; + + switch (kit->flags) { + case BPF_CGROUP_ITER_DESCENDANTS_PRE: + kit->pos = css_next_descendant_pre(kit->pos, kit->start); + break; + case BPF_CGROUP_ITER_DESCENDANTS_POST: + kit->pos = css_next_descendant_post(kit->pos, kit->start); + break; + case BPF_CGROUP_ITER_ANCESTORS_UP: + kit->pos = kit->pos ? kit->pos->parent : kit->start; + } + + return kit->pos; +} + +__bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) +{ +} + +__diag_pop(); \ No newline at end of file diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index c25941531265..b1d285ed4796 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2566,6 +2566,9 @@ 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_iter_css_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +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/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index 2f6c747aa874..1386baf9ae4a 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -471,4 +471,10 @@ extern int bpf_iter_task_new(struct bpf_iter_task *it, 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; +struct bpf_iter_css; +extern int bpf_iter_css_new(struct bpf_iter_css *it, + struct cgroup_subsys_state *start, unsigned int flags) __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; + #endif