Message ID | 20231007124522.34834-5-zhouchuyi@bytedance.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp906708vqo; Sat, 7 Oct 2023 05:47:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpPuDngPhLetNZBE88Z4v6GpbF6ADzV8JvqHzjNs3ij/fT7sAZolWtNsIgwL0w9E33jkAv X-Received: by 2002:a05:6a20:3c9e:b0:13e:90aa:8c8b with SMTP id b30-20020a056a203c9e00b0013e90aa8c8bmr12254531pzj.4.1696682820542; Sat, 07 Oct 2023 05:47:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696682820; cv=none; d=google.com; s=arc-20160816; b=vNH+zTFnI6mKIjIawHEUDLlkEePLVhzEq+IXycC3YGcKAN6LcgOWIpzN38KDWZgwTS hlO4QgUMg+10oo08xWfJ3m5kplFW33GM2m2LtLAQww3DoC4UPbQC1K1aKq+0fStnD/EM O+HVzwKZJ3fBc9gOY3tmU3E/mjM3XbHVuCzE8QkldcNWfzEfARONyyu6/7pYwqOlwiTP xYilCXVy+T/FZosxxw4ZFWKaSE1XFGpg4iNax7uaT+1TnW44Td75j6xEImOTzIIE3ePe 0ga+J0aQOTKE7MznH45q/Tq5WQIdOoHEu0vD2sCgsq+3NCaghd5+IZGJRCE4R8CyFYIF 8WMA== 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=xGhoDnxquywg7pvND9cuNzWhEguzChXTrMgGU/wV1/E=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=MVtG5T+dEKqXAYhIYsp1riXlmtKa+WjMPiL5On8XiR6+oxn31dAQUWudx4zMZlxGNW eDgvVr93qi+twyAgh5N/zMzMJLMDZG3k/ot4DcDLuGkbs2xu+NvtFRzh2xU8zFQdrUB/ m5UMtf/Rs/kqpFzesoiOIXSGYpwv3pKI8Ly2gEKscx9mxu5IZy6in+GiV8OrpI+i05Yj QcLXT29YCImU1ZS3D78x+ey4aefeA8526npWWedl0jFrKOdlR3ENClxiA0racfHPLjUe r66awuIod9QJISpC2zFh87royQanTKcxt6CLy3UrSRK2y8VD1V8gJEU1GL8UP58qs0U/ /d/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=jjzo0vFP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id c129-20020a633587000000b005775161c510si852724pga.504.2023.10.07.05.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 05:47:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=jjzo0vFP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 B9E778047445; Sat, 7 Oct 2023 05:46:49 -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 S1343939AbjJGMqS (ORCPT <rfc822;ezelljr.billy@gmail.com> + 18 others); Sat, 7 Oct 2023 08:46:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343948AbjJGMqK (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 7 Oct 2023 08:46:10 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA3F1CF for <linux-kernel@vger.kernel.org>; Sat, 7 Oct 2023 05:45:45 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-578d791dd91so2309393a12.0 for <linux-kernel@vger.kernel.org>; Sat, 07 Oct 2023 05:45:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1696682745; x=1697287545; 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=xGhoDnxquywg7pvND9cuNzWhEguzChXTrMgGU/wV1/E=; b=jjzo0vFPXsS+HiyQm3pbvwfo6h1VeiXDIaWVl08R5FkOsuKuJbLT+z+vauoRd+S4y7 ATXS8x2KEPXvQh3t7knR64tG75CW0Mg92J+o6ovpuJouWXFUsKS5wUq10O5EHrmBRnw0 5hUoO64fz9gb43P+sGcBtDkNy7JKJmXvd6L5PIgQIGVMCE1wDm0lCWXRcwzBXKpatL6O 3ooZ+vgIS+cNcWJalvaHWyweQnw1ZQsH1YWvx0t/288zCjUkxAphClhh+35Q2FcTq+YL B5eO1HmT1e/E+hCAvj80PiXZ7h97lr62BBcJc+SfMiRGAeKBIfYxk2FS0qFZgpWbYwHv 52JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696682745; x=1697287545; 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=xGhoDnxquywg7pvND9cuNzWhEguzChXTrMgGU/wV1/E=; b=q3k7G9Lv5c2gNOQhwtD7IbkDVcXgCYcwJiSYTYmA8xgGnhIDvYE2taMtXgX+Nh7146 uN9i5UJ+b11O0oOVI9lSey0raM14m+uk2hilGOKfQeCJ8Gwb4CPX3u9FgwFUqePG0lm9 hSX7/oA84UvuDrv6IxlrONp7oZR6AZeeRgReINz6efJcMTZxAVmE6WSEqwph+MLQjAdM IahC0HJsGlOKFAvCe+50jPOdyQKk7HdquStUcTiQnP9gTP044LO7+C5kRcL+hhQSLGZj MSCcXCMaj6S/sKAck41pN3nnC+jdaGq008ALgFpCph/nAS/uoOwZz2jDZdqwlI9PDgcv SEqw== X-Gm-Message-State: AOJu0YyDCzssYF0aRpGv6OSkiYggwTC2636lzerwMqajOCZs19tQ+W7N eKBUgaJKYrXoSjqzX8we2ya0yw== X-Received: by 2002:a17:90a:d796:b0:279:e3a:1bd3 with SMTP id z22-20020a17090ad79600b002790e3a1bd3mr10279926pju.21.1696682745288; Sat, 07 Oct 2023 05:45:45 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id d6-20020a17090ad3c600b00256799877ffsm5095388pjw.47.2023.10.07.05.45.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 05:45:45 -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 v4 4/8] bpf: Introduce css open-coded iterator kfuncs Date: Sat, 7 Oct 2023 20:45:18 +0800 Message-Id: <20231007124522.34834-5-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231007124522.34834-1-zhouchuyi@bytedance.com> References: <20231007124522.34834-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 pete.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 (pete.vger.email [0.0.0.0]); Sat, 07 Oct 2023 05:46:49 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779100884876932930 X-GMAIL-MSGID: 1779100884876932930 |
Series |
Add Open-coded task, css_task and css iters
|
|
Commit Message
Chuyi Zhou
Oct. 7, 2023, 12:45 p.m. UTC
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 <zhouchuyi@bytedance.com> Acked-by: Tejun Heo <tj@kernel.org> --- kernel/bpf/cgroup_iter.c | 59 +++++++++++++++++++ kernel/bpf/helpers.c | 3 + .../testing/selftests/bpf/bpf_experimental.h | 6 ++ 3 files changed, 68 insertions(+)
Comments
Hi Chuyi, kernel test robot noticed the following build warnings: [auto build test WARNING on bpf-next/master] url: https://github.com/intel-lab-lkp/linux/commits/Chuyi-Zhou/cgroup-Prepare-for-using-css_task_iter_-in-BPF/20231007-204750 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231007124522.34834-5-zhouchuyi%40bytedance.com patch subject: [PATCH bpf-next v4 4/8] bpf: Introduce css open-coded iterator kfuncs config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231007/202310072337.CzRlbffm-lkp@intel.com/config) compiler: m68k-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231007/202310072337.CzRlbffm-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202310072337.CzRlbffm-lkp@intel.com/ All warnings (new ones prefixed by >>): >> kernel/bpf/cgroup_iter.c:308:17: warning: no previous prototype for 'bpf_iter_css_new' [-Wmissing-prototypes] 308 | __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, | ^~~~~~~~~~~~~~~~ >> kernel/bpf/cgroup_iter.c:332:41: warning: no previous prototype for 'bpf_iter_css_next' [-Wmissing-prototypes] 332 | __bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) | ^~~~~~~~~~~~~~~~~ >> kernel/bpf/cgroup_iter.c:353:18: warning: no previous prototype for 'bpf_iter_css_destroy' [-Wmissing-prototypes] 353 | __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) | ^~~~~~~~~~~~~~~~~~~~ In file included from <command-line>: kernel/bpf/cgroup_iter.c: In function 'bpf_iter_css_new': include/linux/compiler_types.h:425:45: error: call to '__compiletime_assert_320' declared with attribute error: BUILD_BUG_ON failed: sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css) 425 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^ include/linux/compiler_types.h:406:25: note: in definition of macro '__compiletime_assert' 406 | prefix ## suffix(); \ | ^~~~~~ include/linux/compiler_types.h:425:9: note: in expansion of macro '_compiletime_assert' 425 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^~~~~~~~~~~~~~~~~~~ include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert' 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ^~~~~~~~~~~~~~~~~~ include/linux/build_bug.h:50:9: note: in expansion of macro 'BUILD_BUG_ON_MSG' 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) | ^~~~~~~~~~~~~~~~ kernel/bpf/cgroup_iter.c:313:9: note: in expansion of macro 'BUILD_BUG_ON' 313 | BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css)); | ^~~~~~~~~~~~ vim +/bpf_iter_css_new +308 kernel/bpf/cgroup_iter.c 307 > 308 __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, 309 struct cgroup_subsys_state *start, unsigned int flags) 310 { 311 struct bpf_iter_css_kern *kit = (void *)it; 312 313 BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css)); 314 BUILD_BUG_ON(__alignof__(struct bpf_iter_css_kern) != __alignof__(struct bpf_iter_css)); 315 316 kit->start = NULL; 317 switch (flags) { 318 case BPF_CGROUP_ITER_DESCENDANTS_PRE: 319 case BPF_CGROUP_ITER_DESCENDANTS_POST: 320 case BPF_CGROUP_ITER_ANCESTORS_UP: 321 break; 322 default: 323 return -EINVAL; 324 } 325 326 kit->start = start; 327 kit->pos = NULL; 328 kit->flags = flags; 329 return 0; 330 } 331 > 332 __bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) 333 { 334 struct bpf_iter_css_kern *kit = (void *)it; 335 336 if (!kit->start) 337 return NULL; 338 339 switch (kit->flags) { 340 case BPF_CGROUP_ITER_DESCENDANTS_PRE: 341 kit->pos = css_next_descendant_pre(kit->pos, kit->start); 342 break; 343 case BPF_CGROUP_ITER_DESCENDANTS_POST: 344 kit->pos = css_next_descendant_post(kit->pos, kit->start); 345 break; 346 case BPF_CGROUP_ITER_ANCESTORS_UP: 347 kit->pos = kit->pos ? kit->pos->parent : kit->start; 348 } 349 350 return kit->pos; 351 } 352 > 353 __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it)
在 2023/10/7 20:45, Chuyi Zhou 写道: > 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 <zhouchuyi@bytedance.com> > Acked-by: Tejun Heo <tj@kernel.org> > --- > kernel/bpf/cgroup_iter.c | 59 +++++++++++++++++++ > kernel/bpf/helpers.c | 3 + > .../testing/selftests/bpf/bpf_experimental.h | 6 ++ > 3 files changed, 68 insertions(+) > > diff --git a/kernel/bpf/cgroup_iter.c b/kernel/bpf/cgroup_iter.c > index 810378f04fbc..9c6ad892ae82 100644 > --- a/kernel/bpf/cgroup_iter.c > +++ b/kernel/bpf/cgroup_iter.c > @@ -294,3 +294,62 @@ 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))); > + > +__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)); > + This would cause the fail of netdev/build_32bit CI (https://netdev.bots.linux.dev/static/nipa/790929/13412333/build_32bit/stderr): tools/testing/selftests/kvm/settings: warning: ignored by one of the .gitignore files ../kernel/bpf/cgroup_iter.c:308:17: warning: no previous prototype for ‘bpf_iter_css_new’ [-Wmissing-prototypes] 308 | __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, | ^~~~~~~~~~~~~~~~ ../kernel/bpf/cgroup_iter.c:332:41: warning: no previous prototype for ‘bpf_iter_css_next’ [-Wmissing-prototypes] 332 | __bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) | ^~~~~~~~~~~~~~~~~ ../kernel/bpf/cgroup_iter.c:353:18: warning: no previous prototype for ‘bpf_iter_css_destroy’ [-Wmissing-prototypes] 353 | __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) | ^~~~~~~~~~~~~~~~~~~~ In file included from <command-line>: ../kernel/bpf/cgroup_iter.c: In function ‘bpf_iter_css_new’: ./../include/linux/compiler_types.h:425:45: error: call to ‘__compiletime_assert_322’ declared with attribute error: BUILD_BUG_ON failed: sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css) 425 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^ ./../include/linux/compiler_types.h:406:25: note: in definition of macro ‘__compiletime_assert’ 406 | prefix ## suffix(); \ | ^~~~~~ ./../include/linux/compiler_types.h:425:9: note: in expansion of macro ‘_compiletime_assert’ 425 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) | ^~~~~~~~~~~~~~~~~~~ ../include/linux/build_bug.h:39:37: note: in expansion of macro ‘compiletime_assert’ 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ^~~~~~~~~~~~~~~~~~ ../include/linux/build_bug.h:50:9: note: in expansion of macro ‘BUILD_BUG_ON_MSG’ 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) | ^~~~~~~~~~~~~~~~ ../kernel/bpf/cgroup_iter.c:313:9: note: in expansion of macro ‘BUILD_BUG_ON’ 313 | BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css)); The reason seems on 32-bit machine, sizeof(struct bpf_iter_css) is 24 and sizeof(struct bpf_iter_css_kern) is 16. I was wondering whether the BUILD_BUG_ON check is necessary. Looking at the struct bpf_list_node and struct bpf_list_node_kern wich are very similay to bpf_iter_css, I didn't see the BUILD_BUG_ON check when convert from (struct bpf_list_node *) to (struct bpf_list_node_kern *) /* Non-opaque version of bpf_list_node in uapi/linux/bpf.h */ struct bpf_list_node_kern { struct list_head list_head; void *owner; } __attribute__((aligned(8))); struct bpf_list_node { __u64 :64; __u64 :64; __u64 :64; } __attribute__((aligned(8))); __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head, struct bpf_list_node *node, void *meta__ign, u64 off) { struct bpf_list_node_kern *n = (void *)node; }
在 2023/10/11 12:44, Chuyi Zhou 写道: > > > 在 2023/10/7 20:45, Chuyi Zhou 写道: >> 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 <zhouchuyi@bytedance.com> >> Acked-by: Tejun Heo <tj@kernel.org> >> --- >> kernel/bpf/cgroup_iter.c | 59 +++++++++++++++++++ >> kernel/bpf/helpers.c | 3 + >> .../testing/selftests/bpf/bpf_experimental.h | 6 ++ >> 3 files changed, 68 insertions(+) >> >> diff --git a/kernel/bpf/cgroup_iter.c b/kernel/bpf/cgroup_iter.c >> index 810378f04fbc..9c6ad892ae82 100644 >> --- a/kernel/bpf/cgroup_iter.c >> +++ b/kernel/bpf/cgroup_iter.c >> @@ -294,3 +294,62 @@ 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))); >> + >> +__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)); >> + > > This would cause the fail of netdev/build_32bit CI > (https://netdev.bots.linux.dev/static/nipa/790929/13412333/build_32bit/stderr): > > tools/testing/selftests/kvm/settings: warning: ignored by one of the > .gitignore files > ../kernel/bpf/cgroup_iter.c:308:17: warning: no previous prototype for > ‘bpf_iter_css_new’ [-Wmissing-prototypes] > 308 | __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, > | ^~~~~~~~~~~~~~~~ > ../kernel/bpf/cgroup_iter.c:332:41: warning: no previous prototype for > ‘bpf_iter_css_next’ [-Wmissing-prototypes] > 332 | __bpf_kfunc struct cgroup_subsys_state > *bpf_iter_css_next(struct bpf_iter_css *it) > | ^~~~~~~~~~~~~~~~~ > ../kernel/bpf/cgroup_iter.c:353:18: warning: no previous prototype for > ‘bpf_iter_css_destroy’ [-Wmissing-prototypes] > 353 | __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it) > | ^~~~~~~~~~~~~~~~~~~~ > In file included from <command-line>: > ../kernel/bpf/cgroup_iter.c: In function ‘bpf_iter_css_new’: > ./../include/linux/compiler_types.h:425:45: error: call to > ‘__compiletime_assert_322’ declared with attribute error: BUILD_BUG_ON > failed: sizeof(struct bpf_iter_css_kern) != sizeof(struct bpf_iter_css) > 425 | _compiletime_assert(condition, msg, > __compiletime_assert_, __COUNTER__) > | ^ > ./../include/linux/compiler_types.h:406:25: note: in definition of macro > ‘__compiletime_assert’ > 406 | prefix ## suffix(); \ > | ^~~~~~ > ./../include/linux/compiler_types.h:425:9: note: in expansion of macro > ‘_compiletime_assert’ > 425 | _compiletime_assert(condition, msg, > __compiletime_assert_, __COUNTER__) > | ^~~~~~~~~~~~~~~~~~~ > ../include/linux/build_bug.h:39:37: note: in expansion of macro > ‘compiletime_assert’ > 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), > msg) > | ^~~~~~~~~~~~~~~~~~ > ../include/linux/build_bug.h:50:9: note: in expansion of macro > ‘BUILD_BUG_ON_MSG’ > 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " > #condition) > | ^~~~~~~~~~~~~~~~ > ../kernel/bpf/cgroup_iter.c:313:9: note: in expansion of macro > ‘BUILD_BUG_ON’ > 313 | BUILD_BUG_ON(sizeof(struct bpf_iter_css_kern) != > sizeof(struct bpf_iter_css)); > > > The reason seems on 32-bit machine, sizeof(struct bpf_iter_css) is 24 > and sizeof(struct bpf_iter_css_kern) is 16. > > I was wondering whether the BUILD_BUG_ON check is necessary. Looking at > the struct bpf_list_node and struct bpf_list_node_kern wich are very > similay to bpf_iter_css, I didn't see the BUILD_BUG_ON check when > convert from (struct bpf_list_node *) to (struct bpf_list_node_kern *) > > /* Non-opaque version of bpf_list_node in uapi/linux/bpf.h */ > struct bpf_list_node_kern { > struct list_head list_head; > void *owner; > } __attribute__((aligned(8))); > > struct bpf_list_node { > __u64 :64; > __u64 :64; > __u64 :64; > } __attribute__((aligned(8))); > > __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head, > struct bpf_list_node *node, > void *meta__ign, u64 off) > { > struct bpf_list_node_kern *n = (void *)node; > > } > or we can change the BUILD_BUG_ON check, like bpf_timer_kern in bpf_timer_init: --- a/kernel/bpf/cgroup_iter.c +++ b/kernel/bpf/cgroup_iter.c @@ -310,7 +310,7 @@ __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it, { 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(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; diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 773be9a221f5..0772545568f1 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -877,7 +877,7 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it, { 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(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));
diff --git a/kernel/bpf/cgroup_iter.c b/kernel/bpf/cgroup_iter.c index 810378f04fbc..9c6ad892ae82 100644 --- a/kernel/bpf/cgroup_iter.c +++ b/kernel/bpf/cgroup_iter.c @@ -294,3 +294,62 @@ 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))); + +__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) +{ +} diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 8111437a999e..7596e83d6715 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2555,6 +2555,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 1ec82997cce7..9aab609f6edd 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -463,4 +463,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