Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6287627rwl; Tue, 4 Apr 2023 10:14:32 -0700 (PDT) X-Google-Smtp-Source: AKy350aPf0Ts1+qvYOzmMyQarBMrxIwsCtyPqtm+aFv7nAJ882T/2rZcjuS5uWRFqyaRTTI7a6WU X-Received: by 2002:a17:906:ae85:b0:908:7fed:f0f with SMTP id md5-20020a170906ae8500b009087fed0f0fmr253234ejb.42.1680628471807; Tue, 04 Apr 2023 10:14:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680628471; cv=none; d=google.com; s=arc-20160816; b=asDXItsZAxDd5QvcVMNb+EMfV+OKln97CQ+c/NbfVemkhPUmL9fBcHEgiy+PVeT+hv hwqM80jP4rNBDShg0p4Dc7cCIsaArt/urb6gf/OGmSWYoUsIpTSZ4hvpsvSCj04tvHwj CLyQmQWm2AGJDqn0ic7Oyy6+YfEp5pGXBTSvwiJ1m17+kCSLWTr/jDa1f8i0BsCiuOXy dpXLwKArhCnl7ecfGl6EC+C0ZKXEtbRbSRz48UfR8PpLCUWUJMalOUEHQJUpURpFj6U5 eaDjhQpx5clo5Hq8Xm4R+YZ91Yty1uiz1MKZU8n/LYbbR6jMw6hznq8B8bGZuM0AgscN s8vA== 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=+e6sqRHNS3qAQLVSnMsuMIX+XPuRaTp7/RZUmzbwZM0=; b=walRYL7Mw6L+4Oi3oosPOlW4AWZHuyvcJSPN7fqcS0EhaloMZyz+r1sRYnnuKYFzlC mPH17Lhx6hep+CIomVRTpF1X1V9WKbk0263pThiTT0WPE05TUlM2/ebsF0ivFA/10AEy Jyn8+QP4P4lQ4smsDdNlY5Etgu36AIJw2BGKYUgdBnYB4ZpZQpIl2LY9l+VMTXN5RlXG qj6f5IimjhDxVHhlRnsRhr2kHjn/dOjo1tNS53LqYLUIZf59G/5zJsMQQTUkg5C55KEn ODONDFJa0lB2pFOFhLbiHZoYkwrbvIkzgIIjhZ8jHHzxnmx7vsLZhZPa5hJh0rrtYb9P 3r8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GL6uhdaR; 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 hr28-20020a1709073f9c00b00933b7f95468si372078ejc.277.2023.04.04.10.14.07; Tue, 04 Apr 2023 10:14:31 -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=GL6uhdaR; 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 S235627AbjDDRHL (ORCPT + 99 others); Tue, 4 Apr 2023 13:07:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235456AbjDDRHI (ORCPT ); Tue, 4 Apr 2023 13:07:08 -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 BC8F81BC9 for ; Tue, 4 Apr 2023 10:06:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680627984; 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=+e6sqRHNS3qAQLVSnMsuMIX+XPuRaTp7/RZUmzbwZM0=; b=GL6uhdaR08hVo4JMChmDa5kQviRlhl711rGtgvKsD554M8yOm6lvM4pZSPfYQivhTz2PxI vTceDANCY95fsSVbMs0EPV7vVWBRYyuRHw8FQI14MDk5wyhTqLHAdkJgD23lLMorEXp2+8 feQSeYb5Q/xDUdE1lw77BdAZ6ZnAlko= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-313-wffusJxgNTO3I_GzG5OjSg-1; Tue, 04 Apr 2023 13:06:18 -0400 X-MC-Unique: wffusJxgNTO3I_GzG5OjSg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 16AFF85A5B1; Tue, 4 Apr 2023 17:06:18 +0000 (UTC) Received: from llong.com (unknown [10.22.32.153]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BC93FD6E; Tue, 4 Apr 2023 17:06:17 +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 v2 3/4] cgroup/cpuset: Add cpuset_can_fork() and cpuset_cancel_fork() methods Date: Tue, 4 Apr 2023 13:05:45 -0400 Message-Id: <20230404170546.2585050-4-longman@redhat.com> In-Reply-To: <20230404170546.2585050-1-longman@redhat.com> References: <20230404170546.2585050-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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 | 87 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index dc82f753373e..db1fca5cba06 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,58 @@ 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]); + int ret; + + if (cs == task_cs(current)) + 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 @@ -3291,6 +3349,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); } @@ -3304,6 +3367,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