Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2202341rwl; Thu, 6 Apr 2023 07:11:49 -0700 (PDT) X-Google-Smtp-Source: AKy350Yu6+FriOUmxvOdoLe5izpQWD7B9Jj/fOCFX0nwHNEKGSGQMP8uBBnW2HYQYmU2r+cxRT5g X-Received: by 2002:a17:907:c017:b0:947:ad38:6184 with SMTP id ss23-20020a170907c01700b00947ad386184mr6006135ejc.31.1680790309011; Thu, 06 Apr 2023 07:11:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680790308; cv=none; d=google.com; s=arc-20160816; b=sxRWYovVUE39bK30Q/jcxN3YHU9H3pXOTvHOMLc2OKWC3OX9fjMkz0jKiB3SGDRIdu upCj4wHw6MkuUOi9StNq5ew4Dstaiq5KDDKSZ3aqOVEuwk4y2IfGo5YJqtwkBildcUHF wWgoFK3MxRR1rI2VTnuZVZgdCyKHiiMcKXuxd6UqJB39MWuQV1VTBTBtbRwOb5CUxk0l RqlBzpHxCp/o0+uPLtSFlUE/ulAV492mpOKrRZXCHhCtTeW+7exbG2IDl51FN24x04U+ Rxs+ynDjkIK3WDyWY/i+JmuFKcpgcu+e7pDYEL2gf2uuWdMxO+BK7HA0hQHJbAoiwduh T14g== 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=8dMo/PkUQpVPvNP5N/JYP8JDoaJqZJ0p/4b3QAb5N/o=; b=ia5M7tl/RmtVXslY1mCH3bPyz1SnDiN3MjvIqepzh7UhrSC5xrJmY2g3mxQia9LbeG JGivBL9VZTHaGjPJtCMd2KRakxIRU0xLKyHnu3ARpPwfqs32ZcNH3tY5fNrfrpITjNbu TCrGYSyB2gDJ/HQ3FTtXqIuogTp9lVDmtQMAm+NjYVyt6FkL8Hmh5PyZy/tE7RuVLc0B IKRdDk0JYZXyTx9mqX7BUVs4hPLFdFtz7zZ16mJr2F31Mkt3s0hngdgNlK21IhFe58AZ pJtBG1EIXNtq5/CFDkAkA+71EJbbnJ4E+b5ZTUBWep5unu9NJyHW0R+eHmA5SQN7jVnh uUAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="b6g/Jqcq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fx2-20020a170906b74200b0092fe3fdc1fdsi1139357ejb.605.2023.04.06.07.11.20; Thu, 06 Apr 2023 07:11:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="b6g/Jqcq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238942AbjDFOFI (ORCPT + 99 others); Thu, 6 Apr 2023 10:05:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230029AbjDFOFE (ORCPT ); Thu, 6 Apr 2023 10:05:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 659935B92 for ; Thu, 6 Apr 2023 07:04:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680789862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8dMo/PkUQpVPvNP5N/JYP8JDoaJqZJ0p/4b3QAb5N/o=; b=b6g/JqcqGa8o8vb/qS0CtKgMMA2EIamS4n9fIyve1al/1aaUyo50ehK0LGj7xSrpY24cUY PQojA/Qu+51/RFwcAWNaAxCh8rMxszTrLDnWpeKh+KQaBycpUO7j9kNYsssch3oHgkk0Jn WhhEbpQ9zLaqIPtJT8UcgS3Z0cCSIHA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-343-_LH91P3NNAapgP0IAYnkcQ-1; Thu, 06 Apr 2023 10:04:20 -0400 X-MC-Unique: _LH91P3NNAapgP0IAYnkcQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B108E29AA2CE; Thu, 6 Apr 2023 14:04:19 +0000 (UTC) Received: from llong.com (unknown [10.22.9.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44CB040C6EC4; Thu, 6 Apr 2023 14:04:19 +0000 (UTC) From: Waiman Long To: Tejun Heo , Zefan Li , Johannes Weiner , Christian Brauner Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Juri Lelli , Dietmar Eggemann , =?UTF-8?q?Michal=20Koutn=C3=BD?= , gscrivan@redhat.com, Waiman Long Subject: [PATCH v3 3/4] cgroup/cpuset: Add cpuset_can_fork() and cpuset_cancel_fork() methods Date: Thu, 6 Apr 2023 10:04:03 -0400 Message-Id: <20230406140404.2718574-4-longman@redhat.com> In-Reply-To: <20230406140404.2718574-1-longman@redhat.com> References: <20230406140404.2718574-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the case of CLONE_INTO_CGROUP, not all cpusets are ready to accept new tasks. It is too late to check that in cpuset_fork(). So we need to add the cpuset_can_fork() and cpuset_cancel_fork() methods to pre-check it before we can allow attachment to a different cpuset. We also need to set the attach_in_progress flag to alert other code that a new task is going to be added to the cpuset. Fixes: ef2c41cf38a7 ("clone3: allow spawning processes into cgroups") Signed-off-by: Waiman Long --- kernel/cgroup/cpuset.c | 92 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index e954d5abb784..a331e11e3d20 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -2458,6 +2458,20 @@ static int fmeter_getrate(struct fmeter *fmp) static struct cpuset *cpuset_attach_old_cs; +/* + * Check to see if a cpuset can accept a new task + * For v1, cpus_allowed and mems_allowed can't be empty. + * For v2, effective_cpus can't be empty. + * Note that in v1, effective_cpus = cpus_allowed. + */ +static int cpuset_can_attach_check(struct cpuset *cs) +{ + if (cpumask_empty(cs->effective_cpus) || + (!is_in_v2_mode() && nodes_empty(cs->mems_allowed))) + return -ENOSPC; + return 0; +} + /* Called by cgroups to determine if a cpuset is usable; cpuset_rwsem held */ static int cpuset_can_attach(struct cgroup_taskset *tset) { @@ -2472,16 +2486,9 @@ static int cpuset_can_attach(struct cgroup_taskset *tset) percpu_down_write(&cpuset_rwsem); - /* allow moving tasks into an empty cpuset if on default hierarchy */ - ret = -ENOSPC; - if (!is_in_v2_mode() && - (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))) - goto out_unlock; - - /* - * Task cannot be moved to a cpuset with empty effective cpus. - */ - if (cpumask_empty(cs->effective_cpus)) + /* Check to see if task is allowed in the cpuset */ + ret = cpuset_can_attach_check(cs); + if (ret) goto out_unlock; cgroup_taskset_for_each(task, css, tset) { @@ -2498,7 +2505,6 @@ static int cpuset_can_attach(struct cgroup_taskset *tset) * changes which zero cpus/mems_allowed. */ cs->attach_in_progress++; - ret = 0; out_unlock: percpu_up_write(&cpuset_rwsem); return ret; @@ -3269,6 +3275,63 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css) percpu_up_write(&cpuset_rwsem); } +/* + * In case the child is cloned into a cpuset different from its parent, + * additional checks are done to see if the move is allowed. + */ +static int cpuset_can_fork(struct task_struct *task, struct css_set *cset) +{ + struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); + bool same_cs; + int ret; + + rcu_read_lock(); + same_cs = (cs == task_cs(current)); + rcu_read_unlock(); + + if (same_cs) + return 0; + + lockdep_assert_held(&cgroup_mutex); + percpu_down_write(&cpuset_rwsem); + + /* Check to see if task is allowed in the cpuset */ + ret = cpuset_can_attach_check(cs); + if (ret) + goto out_unlock; + + ret = task_can_attach(task, cs->effective_cpus); + if (ret) + goto out_unlock; + + ret = security_task_setscheduler(task); + if (ret) + goto out_unlock; + + /* + * Mark attach is in progress. This makes validate_change() fail + * changes which zero cpus/mems_allowed. + */ + cs->attach_in_progress++; +out_unlock: + percpu_up_write(&cpuset_rwsem); + return ret; +} + +static void cpuset_cancel_fork(struct task_struct *task, struct css_set *cset) +{ + struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); + + if (cs == task_cs(current)) + return; + + percpu_down_write(&cpuset_rwsem); + cs->attach_in_progress--; + if (!cs->attach_in_progress) + wake_up(&cpuset_attach_wq); + percpu_up_write(&cpuset_rwsem); +} + /* * Make sure the new task conform to the current state of its parent, * which could have been changed by cpuset just after it inherits the @@ -3297,6 +3360,11 @@ static void cpuset_fork(struct task_struct *task) percpu_down_write(&cpuset_rwsem); guarantee_online_mems(cs, &cpuset_attach_nodemask_to); cpuset_attach_task(cs, task); + + cs->attach_in_progress--; + if (!cs->attach_in_progress) + wake_up(&cpuset_attach_wq); + percpu_up_write(&cpuset_rwsem); } @@ -3310,6 +3378,8 @@ struct cgroup_subsys cpuset_cgrp_subsys = { .attach = cpuset_attach, .post_attach = cpuset_post_attach, .bind = cpuset_bind, + .can_fork = cpuset_can_fork, + .cancel_fork = cpuset_cancel_fork, .fork = cpuset_fork, .legacy_cftypes = legacy_files, .dfl_cftypes = dfl_files, -- 2.31.1