From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A068C4707A for ; Fri, 21 May 2021 16:41:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D7688613EC for ; Fri, 21 May 2021 16:41:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236563AbhEUQmc (ORCPT ); Fri, 21 May 2021 12:42:32 -0400 Received: from foss.arm.com ([217.140.110.172]:51282 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbhEUQma (ORCPT ); Fri, 21 May 2021 12:42:30 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 869721424; Fri, 21 May 2021 09:41:06 -0700 (PDT) Received: from e107158-lin.cambridge.arm.com (e107158-lin.cambridge.arm.com [10.1.195.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 357A53F73B; Fri, 21 May 2021 09:41:04 -0700 (PDT) Date: Fri, 21 May 2021 17:41:01 +0100 From: Qais Yousef To: Will Deacon Cc: linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Catalin Marinas , Marc Zyngier , Greg Kroah-Hartman , Peter Zijlstra , Morten Rasmussen , Suren Baghdasaryan , Quentin Perret , Tejun Heo , Li Zefan , Johannes Weiner , Ingo Molnar , Juri Lelli , Vincent Guittot , "Rafael J. Wysocki" , kernel-team@android.com Subject: Re: [PATCH v6 11/21] sched: Split the guts of sched_setaffinity() into a helper function Message-ID: <20210521164101.lwq5wr4mbb32co6l@e107158-lin.cambridge.arm.com> References: <20210518094725.7701-1-will@kernel.org> <20210518094725.7701-12-will@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210518094725.7701-12-will@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/18/21 10:47, Will Deacon wrote: > In preparation for replaying user affinity requests using a saved mask, > split sched_setaffinity() up so that the initial task lookup and > security checks are only performed when the request is coming directly > from userspace. > > Signed-off-by: Will Deacon > --- > kernel/sched/core.c | 110 +++++++++++++++++++++++--------------------- > 1 file changed, 58 insertions(+), 52 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 9512623d5a60..808bbe669a6d 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -6788,9 +6788,61 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, > return retval; > } > > -long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > +static int > +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask) > { > + int retval; > cpumask_var_t cpus_allowed, new_mask; > + > + if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) > + return -ENOMEM; > + > + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) > + return -ENOMEM; Shouldn't we free cpus_allowed first? Cheers -- Qais Yousef > + > + cpuset_cpus_allowed(p, cpus_allowed); > + cpumask_and(new_mask, mask, cpus_allowed); > + > + /* > + * Since bandwidth control happens on root_domain basis, > + * if admission test is enabled, we only admit -deadline > + * tasks allowed to run on all the CPUs in the task's > + * root_domain. > + */ > +#ifdef CONFIG_SMP > + if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { > + rcu_read_lock(); > + if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { > + retval = -EBUSY; > + rcu_read_unlock(); > + goto out_free_masks; > + } > + rcu_read_unlock(); > + } > +#endif > +again: > + retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); > + if (retval) > + goto out_free_masks; > + > + cpuset_cpus_allowed(p, cpus_allowed); > + if (!cpumask_subset(new_mask, cpus_allowed)) { > + /* > + * We must have raced with a concurrent cpuset update. > + * Just reset the cpumask to the cpuset's cpus_allowed. > + */ > + cpumask_copy(new_mask, cpus_allowed); > + goto again; > + } > + > +out_free_masks: > + free_cpumask_var(new_mask); > + free_cpumask_var(cpus_allowed); > + return retval; > +} > + > +long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > +{ > struct task_struct *p; > int retval; > > @@ -6810,68 +6862,22 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > retval = -EINVAL; > goto out_put_task; > } > - if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { > - retval = -ENOMEM; > - goto out_put_task; > - } > - if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { > - retval = -ENOMEM; > - goto out_free_cpus_allowed; > - } > - retval = -EPERM; > + > if (!check_same_owner(p)) { > rcu_read_lock(); > if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { > rcu_read_unlock(); > - goto out_free_new_mask; > + retval = -EPERM; > + goto out_put_task; > } > rcu_read_unlock(); > } > > retval = security_task_setscheduler(p); > if (retval) > - goto out_free_new_mask; > - > - > - cpuset_cpus_allowed(p, cpus_allowed); > - cpumask_and(new_mask, in_mask, cpus_allowed); > - > - /* > - * Since bandwidth control happens on root_domain basis, > - * if admission test is enabled, we only admit -deadline > - * tasks allowed to run on all the CPUs in the task's > - * root_domain. > - */ > -#ifdef CONFIG_SMP > - if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { > - rcu_read_lock(); > - if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { > - retval = -EBUSY; > - rcu_read_unlock(); > - goto out_free_new_mask; > - } > - rcu_read_unlock(); > - } > -#endif > -again: > - retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); > + goto out_put_task; > > - if (!retval) { > - cpuset_cpus_allowed(p, cpus_allowed); > - if (!cpumask_subset(new_mask, cpus_allowed)) { > - /* > - * We must have raced with a concurrent cpuset > - * update. Just reset the cpus_allowed to the > - * cpuset's cpus_allowed > - */ > - cpumask_copy(new_mask, cpus_allowed); > - goto again; > - } > - } > -out_free_new_mask: > - free_cpumask_var(new_mask); > -out_free_cpus_allowed: > - free_cpumask_var(cpus_allowed); > + retval = __sched_setaffinity(p, in_mask); > out_put_task: > put_task_struct(p); > return retval; > -- > 2.31.1.751.gd2f1c929bd-goog > From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26E6AC47076 for ; Fri, 21 May 2021 16:42:48 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 999AD613D8 for ; Fri, 21 May 2021 16:42:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 999AD613D8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=yME05uw+usyLdGiN30APlxWNLnASGIs7mWXjDJZnoF8=; b=M3mv3xj/Nvl4Om/AjO9q+cFMU4 aVc6ggxVFcsu7bqvnLHsR9JdY7yB+SQAFqaCacCV5opwL2HnEafnzfVcKIAW2aNmG5HE4vgbEDADJ cvIy2ioXI56NbjnidAoCZYuV8NPXhZ9rLCqXCX/TRxCmvAgNaenSfNG9JPrCKVEoHEn8RMu4MjgiH fNIZh4m1FbXcRykYj3wHCfw7A64gK4sA/3rb71C3woydwdmjfWh/6UeOm+KtCOQaka4XwzAp3i3C9 sxV/wT3Ez72hMjHDwi6x/A2CX/4AMasfSTx5IxugCqYR5wW0CmUscqYSfnKOy3f+rcj/dkHsz0k2K ktTML13g==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lk8D5-000In5-9n; Fri, 21 May 2021 16:41:15 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lk8D0-000ImM-SA for linux-arm-kernel@desiato.infradead.org; Fri, 21 May 2021 16:41:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=lT1qQTlgI0mgKLNqzFoY2ExD4zRsp4TImS+SlmxdJnI=; b=ISRYTFdxzDU+SP6a/ZZYi3oVg4 DiFUZondU5JK1T5l7endE3NNQYdV9VjAl7JBFJqxF3KpPENP1I7y0PBcSNf3DsXtgY7OfIJ6r3/dx ROCVt/4k5ls9xwVoX8o0adwpZ8Vls9sWUq7c3mNAae9l5mij1bwCmCz4QLX+glp+311HYDJzDhGkE lRRfhlql5zJeoR3U3gtZpThiRhoSyutvlmOXdKuLU3025HVS2wyPflylvOCof+M225FbP7SFGwhMC Mdv3O+eivGbv4LVB4G1W5GYQwYtXjnw0xhqClKbjnR8PKEeNu7q0Pel3W3ep49TYXukx06yqAJM9+ nxrCaWkQ==; Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lk8Cx-00HHh9-RE for linux-arm-kernel@lists.infradead.org; Fri, 21 May 2021 16:41:09 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 869721424; Fri, 21 May 2021 09:41:06 -0700 (PDT) Received: from e107158-lin.cambridge.arm.com (e107158-lin.cambridge.arm.com [10.1.195.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 357A53F73B; Fri, 21 May 2021 09:41:04 -0700 (PDT) Date: Fri, 21 May 2021 17:41:01 +0100 From: Qais Yousef To: Will Deacon Cc: linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Catalin Marinas , Marc Zyngier , Greg Kroah-Hartman , Peter Zijlstra , Morten Rasmussen , Suren Baghdasaryan , Quentin Perret , Tejun Heo , Li Zefan , Johannes Weiner , Ingo Molnar , Juri Lelli , Vincent Guittot , "Rafael J. Wysocki" , kernel-team@android.com Subject: Re: [PATCH v6 11/21] sched: Split the guts of sched_setaffinity() into a helper function Message-ID: <20210521164101.lwq5wr4mbb32co6l@e107158-lin.cambridge.arm.com> References: <20210518094725.7701-1-will@kernel.org> <20210518094725.7701-12-will@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210518094725.7701-12-will@kernel.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210521_094108_005124_F16BAE98 X-CRM114-Status: GOOD ( 23.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 05/18/21 10:47, Will Deacon wrote: > In preparation for replaying user affinity requests using a saved mask, > split sched_setaffinity() up so that the initial task lookup and > security checks are only performed when the request is coming directly > from userspace. > > Signed-off-by: Will Deacon > --- > kernel/sched/core.c | 110 +++++++++++++++++++++++--------------------- > 1 file changed, 58 insertions(+), 52 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 9512623d5a60..808bbe669a6d 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -6788,9 +6788,61 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, > return retval; > } > > -long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > +static int > +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask) > { > + int retval; > cpumask_var_t cpus_allowed, new_mask; > + > + if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) > + return -ENOMEM; > + > + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) > + return -ENOMEM; Shouldn't we free cpus_allowed first? Cheers -- Qais Yousef > + > + cpuset_cpus_allowed(p, cpus_allowed); > + cpumask_and(new_mask, mask, cpus_allowed); > + > + /* > + * Since bandwidth control happens on root_domain basis, > + * if admission test is enabled, we only admit -deadline > + * tasks allowed to run on all the CPUs in the task's > + * root_domain. > + */ > +#ifdef CONFIG_SMP > + if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { > + rcu_read_lock(); > + if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { > + retval = -EBUSY; > + rcu_read_unlock(); > + goto out_free_masks; > + } > + rcu_read_unlock(); > + } > +#endif > +again: > + retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); > + if (retval) > + goto out_free_masks; > + > + cpuset_cpus_allowed(p, cpus_allowed); > + if (!cpumask_subset(new_mask, cpus_allowed)) { > + /* > + * We must have raced with a concurrent cpuset update. > + * Just reset the cpumask to the cpuset's cpus_allowed. > + */ > + cpumask_copy(new_mask, cpus_allowed); > + goto again; > + } > + > +out_free_masks: > + free_cpumask_var(new_mask); > + free_cpumask_var(cpus_allowed); > + return retval; > +} > + > +long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > +{ > struct task_struct *p; > int retval; > > @@ -6810,68 +6862,22 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > retval = -EINVAL; > goto out_put_task; > } > - if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { > - retval = -ENOMEM; > - goto out_put_task; > - } > - if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { > - retval = -ENOMEM; > - goto out_free_cpus_allowed; > - } > - retval = -EPERM; > + > if (!check_same_owner(p)) { > rcu_read_lock(); > if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { > rcu_read_unlock(); > - goto out_free_new_mask; > + retval = -EPERM; > + goto out_put_task; > } > rcu_read_unlock(); > } > > retval = security_task_setscheduler(p); > if (retval) > - goto out_free_new_mask; > - > - > - cpuset_cpus_allowed(p, cpus_allowed); > - cpumask_and(new_mask, in_mask, cpus_allowed); > - > - /* > - * Since bandwidth control happens on root_domain basis, > - * if admission test is enabled, we only admit -deadline > - * tasks allowed to run on all the CPUs in the task's > - * root_domain. > - */ > -#ifdef CONFIG_SMP > - if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { > - rcu_read_lock(); > - if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { > - retval = -EBUSY; > - rcu_read_unlock(); > - goto out_free_new_mask; > - } > - rcu_read_unlock(); > - } > -#endif > -again: > - retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); > + goto out_put_task; > > - if (!retval) { > - cpuset_cpus_allowed(p, cpus_allowed); > - if (!cpumask_subset(new_mask, cpus_allowed)) { > - /* > - * We must have raced with a concurrent cpuset > - * update. Just reset the cpus_allowed to the > - * cpuset's cpus_allowed > - */ > - cpumask_copy(new_mask, cpus_allowed); > - goto again; > - } > - } > -out_free_new_mask: > - free_cpumask_var(new_mask); > -out_free_cpus_allowed: > - free_cpumask_var(cpus_allowed); > + retval = __sched_setaffinity(p, in_mask); > out_put_task: > put_task_struct(p); > return retval; > -- > 2.31.1.751.gd2f1c929bd-goog > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel