From patchwork Mon Sep 25 10:55:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 144418 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp1192976vqu; Mon, 25 Sep 2023 06:02:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGxJ0g+UlZqlYS2ju9XNTYQqYjzlzZHxIFyJ0HSAIlboNVj419VqloTx5/Ht45IBwgw8ky1 X-Received: by 2002:a17:90a:e14b:b0:268:7ec:51ae with SMTP id ez11-20020a17090ae14b00b0026807ec51aemr3986023pjb.41.1695646970525; Mon, 25 Sep 2023 06:02:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695646970; cv=none; d=google.com; s=arc-20160816; b=aohaD0V44Kn4lmWaMeV7m7bLryO5aHjYxl9dhVH6siCCeTUpj4i2GEGTBtzqhRVsiM dPcRJ+/LjIKgTyASaSYRbr9IIBnA4FtQboEpOlvnM/bq0Sn8hpinsCzBsknTM3KAV7/N 7qx8T6vPETEdozkoQEWErSK1VlkyVDWPboDiqjvRNfMyqXmGMc3oy1CI2BaVOfx6hzcE o8cpPExR1LUz5AGBeCHj2Lb32jrwVhLtgt4L7KbSPxDTDJLJ5Wl4UZaeqMklu4kX0Lnu kdagu1GxLFAGQY9BiHSEHC/hlu3uZLJRss2BquMi/xIcGGy1TrHS4hzWlHQbd9+NPavB Fgew== 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=FNW2oYGtelVvgczyk2A13Ij/ocoUNYFTCyV6b+P6gh8=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=sEqKjogVE8UZzy+mO2Ja+xxdH1CSASoFrm2q/+MU34ZHF0WW1azYGHClE3p8sOXzYR QtbSlhxl/eVzLabvBBt04HYF1SCdWFaxQbXeTrPZTk3nmbkMkJC2wZH0FsU/aV7c3y3o kPWjVvOfZRXkvQ+UewUf6ajPbmxkovKGTW/KgBCiq6IfjEDXNi3xeOjB32giKStdC1Hv 0Qfq13BygHvMd7Tnfk8cQIaViqYwowWT5Y+bdxaS5biC9yhkjEi+djSHQ+W5CbF6KxE/ zTkS3ABL+h/KcZNpWhQF/G0gC92Au/J2IVfqChsSRK4+ibJEicJY7OuYQwY9oBWYRkF2 Q9bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=OEwcpaKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id l15-20020a65680f000000b0055ffed90cc9si9751912pgt.609.2023.09.25.06.02.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 06:02:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=OEwcpaKF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 BF5A48068203; Mon, 25 Sep 2023 03:56:21 -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 S229997AbjIYK4K (ORCPT + 29 others); Mon, 25 Sep 2023 06:56:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjIYK4J (ORCPT ); Mon, 25 Sep 2023 06:56:09 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D07BFC6 for ; Mon, 25 Sep 2023 03:56:02 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-577fb90bb76so2999881a12.2 for ; Mon, 25 Sep 2023 03:56:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1695639362; x=1696244162; 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=FNW2oYGtelVvgczyk2A13Ij/ocoUNYFTCyV6b+P6gh8=; b=OEwcpaKFyUBrhux6ODETbu0bBt7ufnZGf78YErg8LsIrzPqTYE+VUom7slonPVHwW1 97S6H+00UmkrTq0AFH35AvdQFWouBCpgNBGC6c+EPGvdRab0FJKUn48mpoBv07/wXZBv L90JM6rlJoky0sicp8VCZn9GSXf8GC/XF03ib67JI3+Emn4pFnflXpGEa7wFctDuSdpU BcBCmoLAlG4MPVRDrFAqHLNL3kxLL6B9k432WmiXU0nQ41IUQaX1TUf1Do/dV4JZRylR 0yGtKz9rE3cLW4+qWF436aqOx1UCcpeNUfjmHDhZM0UTT+36DzCKN9yKNxuR12pYto96 VaaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695639362; x=1696244162; 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=FNW2oYGtelVvgczyk2A13Ij/ocoUNYFTCyV6b+P6gh8=; b=vX96LHgDjBgI1ZrdGTo+nKKbL8LBgtPCp+gJGSFTvnRb9YpD8+rdTYd25G24MNgczf wUbNY8ZOIR5Ub9jaVxMJCRinZpS6M6oR/RiIGERUBWFR7QFhhmdNmFlKvF0noYck6oJ3 +7zhgP7n8pEx1h/QF7h60foS9siy/2rcxW2tuRhLi4AyJ49AzZHvKcSQXf1rxPGgoCVo ctd/Wf54ZDu+GEVHON+xbuaUdFwA70p6ldDjTQ4rGVVriLq/SbWkTR0+GvlaS69LBuAr T8wk59iLnS99QVikKtnCggB0MPVqMfbLJaUXn08v1fKq2XMiTNsrETemfYo2mxWyoplq Ll5w== X-Gm-Message-State: AOJu0Yzvbh62HSo+mW55mUqlFIPaGatVn6OwSgXA32TUHhbHGZWHg3uZ U5zeVen9IQR+x2Yx46RUG2ncGA== X-Received: by 2002:a17:90b:4f8d:b0:268:c5c7:f7f1 with SMTP id qe13-20020a17090b4f8d00b00268c5c7f7f1mr3640155pjb.29.1695639362307; Mon, 25 Sep 2023 03:56:02 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id y9-20020a17090a16c900b002772faee740sm2297842pje.5.2023.09.25.03.56.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 03:56:02 -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 v3 1/7] cgroup: Prepare for using css_task_iter_*() in BPF Date: Mon, 25 Sep 2023 18:55:46 +0800 Message-Id: <20230925105552.817513-2-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230925105552.817513-1-zhouchuyi@bytedance.com> References: <20230925105552.817513-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 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 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]); Mon, 25 Sep 2023 03:56:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778014717608640129 X-GMAIL-MSGID: 1778014717608640129 This patch makes some preparations for using css_task_iter_*() in BPF Program. 1. Flags CSS_TASK_ITER_* are #define-s and it's not easy for bpf prog to use them. Convert them to enum so bpf prog can take them from vmlinux.h. 2. In the next patch we will add css_task_iter_*() in common kfuncs which is not safe. Since css_task_iter_*() does spin_unlock_irq() which might screw up irq flags depending on the context where bpf prog is running. So we should use irqsave/irqrestore here and the switching is harmless. Suggested-by: Alexei Starovoitov Signed-off-by: Chuyi Zhou Acked-by: Tejun Heo --- include/linux/cgroup.h | 12 +++++------- kernel/cgroup/cgroup.c | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index b307013b9c6c..0ef0af66080e 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -40,13 +40,11 @@ struct kernel_clone_args; #define CGROUP_WEIGHT_DFL 100 #define CGROUP_WEIGHT_MAX 10000 -/* walk only threadgroup leaders */ -#define CSS_TASK_ITER_PROCS (1U << 0) -/* walk all threaded css_sets in the domain */ -#define CSS_TASK_ITER_THREADED (1U << 1) - -/* internal flags */ -#define CSS_TASK_ITER_SKIPPED (1U << 16) +enum { + CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */ + CSS_TASK_ITER_THREADED = (1U << 1), /* walk all threaded css_sets in the domain */ + CSS_TASK_ITER_SKIPPED = (1U << 16), /* internal flags */ +}; /* a css_task_iter should be treated as an opaque object */ struct css_task_iter { diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 1fb7f562289d..b6d64f3b8888 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4917,9 +4917,11 @@ static void css_task_iter_advance(struct css_task_iter *it) void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, struct css_task_iter *it) { + unsigned long irqflags; + memset(it, 0, sizeof(*it)); - spin_lock_irq(&css_set_lock); + spin_lock_irqsave(&css_set_lock, irqflags); it->ss = css->ss; it->flags = flags; @@ -4933,7 +4935,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, css_task_iter_advance(it); - spin_unlock_irq(&css_set_lock); + spin_unlock_irqrestore(&css_set_lock, irqflags); } /** @@ -4946,12 +4948,14 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, */ struct task_struct *css_task_iter_next(struct css_task_iter *it) { + unsigned long irqflags; + if (it->cur_task) { put_task_struct(it->cur_task); it->cur_task = NULL; } - spin_lock_irq(&css_set_lock); + spin_lock_irqsave(&css_set_lock, irqflags); /* @it may be half-advanced by skips, finish advancing */ if (it->flags & CSS_TASK_ITER_SKIPPED) @@ -4964,7 +4968,7 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it) css_task_iter_advance(it); } - spin_unlock_irq(&css_set_lock); + spin_unlock_irqrestore(&css_set_lock, irqflags); return it->cur_task; } @@ -4977,11 +4981,13 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it) */ void css_task_iter_end(struct css_task_iter *it) { + unsigned long irqflags; + if (it->cur_cset) { - spin_lock_irq(&css_set_lock); + spin_lock_irqsave(&css_set_lock, irqflags); list_del(&it->iters_node); put_css_set_locked(it->cur_cset); - spin_unlock_irq(&css_set_lock); + spin_unlock_irqrestore(&css_set_lock, irqflags); } if (it->cur_dcset)