From nobody Sat Apr 11 02:17:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A54AAC28B2B for ; Tue, 16 Aug 2022 15:11:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235458AbiHPPLn (ORCPT ); Tue, 16 Aug 2022 11:11:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231383AbiHPPLj (ORCPT ); Tue, 16 Aug 2022 11:11:39 -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 988C61408A for ; Tue, 16 Aug 2022 08:11:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660662697; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PRoaaXzNkD+Bdn3hh0xmbqUKPl2NIrI1DRsHepuWxtY=; b=hEKVy3o1CINivtAIc+HJUIgP21ZitYS6N720owbCycUKjGPmPRf0HQK+zRsBAK2J82l50I bA9fIiXIww2/p5Dd5gCrMxZ3Q+rzBv/D17V5lwl4YeSfVa3NM5O1lqQHJLIehNMIf7DWfi 52Fe8qzWsHLMypD306yqFjn574sN8To= 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-477-QfuFRt9cNcmgfVtoNLlPQQ-1; Tue, 16 Aug 2022 11:11:35 -0400 X-MC-Unique: QfuFRt9cNcmgfVtoNLlPQQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8C2F8180F6E1; Tue, 16 Aug 2022 15:11:33 +0000 (UTC) Received: from llong.com (unknown [10.22.10.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id EBBE8492C3B; Tue, 16 Aug 2022 15:11:32 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Tejun Heo , Zefan Li , Johannes Weiner , Will Deacon Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds , Waiman Long Subject: [PATCH v4 1/3] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity() Date: Tue, 16 Aug 2022 11:11:17 -0400 Message-Id: <20220816151119.29534-2-longman@redhat.com> In-Reply-To: <20220816151119.29534-1-longman@redhat.com> References: <20220816151119.29534-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The user_cpus_ptr field is added by commit b90ca8badbd1 ("sched: Introduce task_struct::user_cpus_ptr to track requested affinity"). It is currently used only by arm64 arch due to possible asymmetric CPU setup. This patch extends its usage to save user provided cpumask when sched_setaffinity() is called for all arches. With this patch applied, user_cpus_ptr, once allocated after a call to sched_setaffinity(), will only be freed when the task exits. Since user_cpus_ptr is supposed to be used for "requested affinity", there is actually no point to save current cpu affinity in restrict_cpus_allowed_ptr() if sched_setaffinity() has never been called. Modify the logic to set user_cpus_ptr only in sched_setaffinity() and use it in restrict_cpus_allowed_ptr() and relax_compatible_cpus_allowed_ptr() if defined but not changing it. This will be some changes in behavior for arm64 systems with asymmetric CPUs in some corner cases. For instance, if sched_setaffinity() has never been called and there is a cpuset change before relax_compatible_cpus_allowed_ptr() is called, its subsequent call will follow what the cpuset allows but not what the previous cpu affinity setting allows. As a call to sched_setaffinity() will no longer clear user_cpus_ptr but set it instead, the SCA_USER flag is no longer necessary and can be removed. Signed-off-by: Waiman Long --- kernel/sched/core.c | 100 ++++++++++++++++++++++--------------------- kernel/sched/sched.h | 1 - 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ee28253c9ac0..9353641efdca 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2848,7 +2848,6 @@ static int __set_cpus_allowed_ptr_locked(struct task_= struct *p, const struct cpumask *cpu_allowed_mask =3D task_cpu_possible_mask(p); const struct cpumask *cpu_valid_mask =3D cpu_active_mask; bool kthread =3D p->flags & PF_KTHREAD; - struct cpumask *user_mask =3D NULL; unsigned int dest_cpu; int ret =3D 0; =20 @@ -2907,14 +2906,7 @@ static int __set_cpus_allowed_ptr_locked(struct task= _struct *p, =20 __do_set_cpus_allowed(p, new_mask, flags); =20 - if (flags & SCA_USER) - user_mask =3D clear_user_cpus_ptr(p); - - ret =3D affine_move_task(rq, p, rf, dest_cpu, flags); - - kfree(user_mask); - - return ret; + return affine_move_task(rq, p, rf, dest_cpu, flags); =20 out: task_rq_unlock(rq, p, rf); @@ -2949,8 +2941,10 @@ EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); =20 /* * Change a given task's CPU affinity to the intersection of its current - * affinity mask and @subset_mask, writing the resulting mask to @new_mask - * and pointing @p->user_cpus_ptr to a copy of the old mask. + * affinity mask and @subset_mask, writing the resulting mask to @new_mask. + * If user_cpus_ptr is defined, use it as the the basis for restricting CPU + * affinity or use cpu_online_mask instead. + * * If the resulting mask is empty, leave the affinity unchanged and return * -EINVAL. */ @@ -2958,16 +2952,10 @@ static int restrict_cpus_allowed_ptr(struct task_st= ruct *p, struct cpumask *new_mask, const struct cpumask *subset_mask) { - struct cpumask *user_mask =3D NULL; struct rq_flags rf; struct rq *rq; int err; - - if (!p->user_cpus_ptr) { - user_mask =3D kmalloc(cpumask_size(), GFP_KERNEL); - if (!user_mask) - return -ENOMEM; - } + bool not_empty; =20 rq =3D task_rq_lock(p, &rf); =20 @@ -2981,25 +2969,21 @@ static int restrict_cpus_allowed_ptr(struct task_st= ruct *p, goto err_unlock; } =20 - if (!cpumask_and(new_mask, &p->cpus_mask, subset_mask)) { + + if (p->user_cpus_ptr) + not_empty =3D cpumask_and(new_mask, p->user_cpus_ptr, subset_mask); + else + not_empty =3D cpumask_and(new_mask, cpu_online_mask, subset_mask); + + if (!not_empty) { err =3D -EINVAL; goto err_unlock; } =20 - /* - * We're about to butcher the task affinity, so keep track of what - * the user asked for in case we're able to restore it later on. - */ - if (user_mask) { - cpumask_copy(user_mask, p->cpus_ptr); - p->user_cpus_ptr =3D user_mask; - } - return __set_cpus_allowed_ptr_locked(p, new_mask, 0, rq, &rf); =20 err_unlock: task_rq_unlock(rq, p, &rf); - kfree(user_mask); return err; } =20 @@ -3049,34 +3033,27 @@ void force_compatible_cpus_allowed_ptr(struct task_= struct *p) } =20 static int -__sched_setaffinity(struct task_struct *p, const struct cpumask *mask); +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask, boo= l save_mask); =20 /* * Restore the affinity of a task @p which was previously restricted by a - * call to force_compatible_cpus_allowed_ptr(). This will clear (and free) - * @p->user_cpus_ptr. + * call to force_compatible_cpus_allowed_ptr(). * * It is the caller's responsibility to serialise this with any calls to * force_compatible_cpus_allowed_ptr(@p). */ void relax_compatible_cpus_allowed_ptr(struct task_struct *p) { - struct cpumask *user_mask =3D p->user_cpus_ptr; - unsigned long flags; + const struct cpumask *user_mask =3D p->user_cpus_ptr; + + if (!user_mask) + user_mask =3D cpu_online_mask; =20 /* - * Try to restore the old affinity mask. If this fails, then - * we free the mask explicitly to avoid it being inherited across - * a subsequent fork(). + * Try to restore the old affinity mask with __sched_setaffinity(). + * Cpuset masking will be done there too. */ - if (!user_mask || !__sched_setaffinity(p, user_mask)) - return; - - raw_spin_lock_irqsave(&p->pi_lock, flags); - user_mask =3D clear_user_cpus_ptr(p); - raw_spin_unlock_irqrestore(&p->pi_lock, flags); - - kfree(user_mask); + __sched_setaffinity(p, user_mask, false); } =20 void set_task_cpu(struct task_struct *p, unsigned int new_cpu) @@ -8079,10 +8056,11 @@ int dl_task_check_affinity(struct task_struct *p, c= onst struct cpumask *mask) #endif =20 static int -__sched_setaffinity(struct task_struct *p, const struct cpumask *mask) +__sched_setaffinity(struct task_struct *p, const struct cpumask *mask, boo= l save_mask) { int retval; cpumask_var_t cpus_allowed, new_mask; + struct cpumask *user_mask =3D NULL; =20 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) return -ENOMEM; @@ -8098,8 +8076,33 @@ __sched_setaffinity(struct task_struct *p, const str= uct cpumask *mask) retval =3D dl_task_check_affinity(p, new_mask); if (retval) goto out_free_new_mask; + + /* + * Save the user requested mask into user_cpus_ptr if save_mask set. + * pi_lock is used for protecting user_cpus_ptr. + */ + if (save_mask && !p->user_cpus_ptr) { + user_mask =3D kmalloc(cpumask_size(), GFP_KERNEL); + + if (!user_mask) { + retval =3D -ENOMEM; + goto out_free_new_mask; + } + } + if (save_mask) { + unsigned long flags; + + raw_spin_lock_irqsave(&p->pi_lock, flags); + if (!p->user_cpus_ptr) { + p->user_cpus_ptr =3D user_mask; + user_mask =3D NULL; + } + + cpumask_copy(p->user_cpus_ptr, mask); + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + } again: - retval =3D __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK | SCA_USER); + retval =3D __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); if (retval) goto out_free_new_mask; =20 @@ -8113,6 +8116,7 @@ __sched_setaffinity(struct task_struct *p, const stru= ct cpumask *mask) goto again; } =20 + kfree(user_mask); out_free_new_mask: free_cpumask_var(new_mask); out_free_cpus_allowed: @@ -8156,7 +8160,7 @@ long sched_setaffinity(pid_t pid, const struct cpumas= k *in_mask) if (retval) goto out_put_task; =20 - retval =3D __sched_setaffinity(p, in_mask); + retval =3D __sched_setaffinity(p, in_mask, true); out_put_task: put_task_struct(p); return retval; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index e26688d387ae..15eefcd65faa 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2283,7 +2283,6 @@ extern struct task_struct *pick_next_task_idle(struct= rq *rq); #define SCA_CHECK 0x01 #define SCA_MIGRATE_DISABLE 0x02 #define SCA_MIGRATE_ENABLE 0x04 -#define SCA_USER 0x08 =20 #ifdef CONFIG_SMP =20 --=20 2.31.1 From nobody Sat Apr 11 02:17:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15784C25B0E for ; Tue, 16 Aug 2022 15:11:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235864AbiHPPLr (ORCPT ); Tue, 16 Aug 2022 11:11:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233076AbiHPPLk (ORCPT ); Tue, 16 Aug 2022 11:11:40 -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 3D2A96FA3F for ; Tue, 16 Aug 2022 08:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660662698; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wtQ3T2SnjxmANB9ZOMVYpxJhFmJErNinRxxSjh7HxJA=; b=aWDUtQWEp/CvjO6j0e0piWFBdhaAWMmGNxvMcUjugMsq2mveXwGU2snrtJWe5/IuHlOFSI O91phw6HX8cez68HSYFxeH3cLx/ilBmaSYoiBLRAxfffnuTcGKeAovSGY+rUVUmMN1somp u7IZnDOvsYlB1NZUfvgA/UAl4zeb4f4= 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-641-mg0bDmKvOx-UZNTFV0jDxg-1; Tue, 16 Aug 2022 11:11:35 -0400 X-MC-Unique: mg0bDmKvOx-UZNTFV0jDxg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3F7AD803301; Tue, 16 Aug 2022 15:11:34 +0000 (UTC) Received: from llong.com (unknown [10.22.10.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99494492C3B; Tue, 16 Aug 2022 15:11:33 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Tejun Heo , Zefan Li , Johannes Weiner , Will Deacon Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds , Waiman Long Subject: [PATCH v4 2/3] sched: Provide copy_user_cpus_mask() to copy out user mask Date: Tue, 16 Aug 2022 11:11:18 -0400 Message-Id: <20220816151119.29534-3-longman@redhat.com> In-Reply-To: <20220816151119.29534-1-longman@redhat.com> References: <20220816151119.29534-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since accessing the content of the user_cpus_ptr requires lock protection to ensure its validity, provide a helper function copy_user_cpus_mask() to facilitate its reading. Suggested-by: Peter Zijlstra Signed-off-by: Waiman Long --- include/linux/sched.h | 1 + kernel/sched/core.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/linux/sched.h b/include/linux/sched.h index e7b2f8a5c711..f2b0340c094e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1830,6 +1830,7 @@ extern int task_can_attach(struct task_struct *p, con= st struct cpumask *cs_effec extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumas= k *new_mask); extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumas= k *new_mask); extern int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *= src, int node); +extern struct cpumask *copy_user_cpus_mask(struct task_struct *p, struct c= pumask *user_mask); extern void release_user_cpus_ptr(struct task_struct *p); extern int dl_task_check_affinity(struct task_struct *p, const struct cpum= ask *mask); extern void force_compatible_cpus_allowed_ptr(struct task_struct *p); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 9353641efdca..ee0fa1635163 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2618,6 +2618,25 @@ void release_user_cpus_ptr(struct task_struct *p) kfree(clear_user_cpus_ptr(p)); } =20 +/* + * Return the copied mask pointer or NULL if user mask not available. + * Acquiring pi_lock for read access protection. + */ +struct cpumask *copy_user_cpus_mask(struct task_struct *p, + struct cpumask *user_mask) +{ + struct cpumask *mask =3D NULL; + unsigned long flags; + + raw_spin_lock_irqsave(&p->pi_lock, flags); + if (p->user_cpus_ptr) { + cpumask_copy(user_mask, p->user_cpus_ptr); + mask =3D user_mask; + } + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + return mask; +} + /* * This function is wildly self concurrent; here be dragons. * --=20 2.31.1 From nobody Sat Apr 11 02:17:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FD21C25B0E for ; Tue, 16 Aug 2022 15:12:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236038AbiHPPL6 (ORCPT ); Tue, 16 Aug 2022 11:11:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235449AbiHPPLn (ORCPT ); Tue, 16 Aug 2022 11:11:43 -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 550BF6FA3F for ; Tue, 16 Aug 2022 08:11:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660662701; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8DDEyD7Oc6PgrEZHkHqv7mnoGrf2YiFnazEzhrXs74I=; b=J+0RNLJPSDaYQF+1k5lgSymS4V9n7f937UmHWfZcpBjso8+U0gjm4Gqh1/764K4oX9LFja LyXEOeThfI/QxSD0joav/kcRlDjw4wyZUgZVI4/IfmcH5cFlVcT4j1shFQ0SaEDdf0raUY Q94z5udV3CLV6HRbydV1HGqsPDygdeo= 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-219-fJIR_h_2O9K3A2toj1k55g-1; Tue, 16 Aug 2022 11:11:36 -0400 X-MC-Unique: fJIR_h_2O9K3A2toj1k55g-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E2E663C11980; Tue, 16 Aug 2022 15:11:34 +0000 (UTC) Received: from llong.com (unknown [10.22.10.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4D7D3492C3B; Tue, 16 Aug 2022 15:11:34 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Tejun Heo , Zefan Li , Johannes Weiner , Will Deacon Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds , Waiman Long Subject: [PATCH v4 3/3] cgroup/cpuset: Keep user set cpus affinity Date: Tue, 16 Aug 2022 11:11:19 -0400 Message-Id: <20220816151119.29534-4-longman@redhat.com> In-Reply-To: <20220816151119.29534-1-longman@redhat.com> References: <20220816151119.29534-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It was found that any change to the current cpuset hierarchy may reset the cpumask of the tasks in the affected cpusets to the default cpuset value even if those tasks have cpus affinity explicitly set by the users before. That is especially easy to trigger under a cgroup v2 environment where writing "+cpuset" to the root cgroup's cgroup.subtree_control file will reset the cpus affinity of all the processes in the system. That is problematic in a nohz_full environment where the tasks running in the nohz_full CPUs usually have their cpus affinity explicitly set and will behave incorrectly if cpus affinity changes. Fix this problem by looking at user_cpus_ptr which will be set if cpus affinity have been explicitly set before and use it to restrcit the given cpumask unless there is no overlap. In that case, it will fallback to the given one. With that change in place, it was verified that tasks that have its cpus affinity explicitly set will not be affected by changes made to the v2 cgroup.subtree_control files. Signed-off-by: Waiman Long --- kernel/cgroup/cpuset.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index 58aadfda9b8b..cabfac540fd8 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -704,6 +704,30 @@ static int validate_change(struct cpuset *cur, struct = cpuset *trial) return ret; } =20 +/* + * Preserve user provided cpumask (if set) as much as possible unless there + * is no overlap with the given mask. + */ +static int cpuset_set_cpus_allowed_ptr(struct task_struct *p, + const struct cpumask *mask) +{ + if (p->user_cpus_ptr) { + cpumask_var_t new_mask; + + if (alloc_cpumask_var(&new_mask, GFP_KERNEL) && + copy_user_cpus_mask(p, new_mask) && + cpumask_and(new_mask, new_mask, mask)) { + int ret =3D set_cpus_allowed_ptr(p, new_mask); + + free_cpumask_var(new_mask); + return ret; + } + free_cpumask_var(new_mask); + } + + return set_cpus_allowed_ptr(p, mask); +} + #ifdef CONFIG_SMP /* * Helper routine for generate_sched_domains(). @@ -1130,7 +1154,7 @@ static void update_tasks_cpumask(struct cpuset *cs) =20 css_task_iter_start(&cs->css, 0, &it); while ((task =3D css_task_iter_next(&it))) - set_cpus_allowed_ptr(task, cs->effective_cpus); + cpuset_set_cpus_allowed_ptr(task, cs->effective_cpus); css_task_iter_end(&it); } =20 @@ -2303,7 +2327,7 @@ static void cpuset_attach(struct cgroup_taskset *tset) * can_attach beforehand should guarantee that this doesn't * fail. TODO: have a better way to handle failure here */ - WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); + WARN_ON_ONCE(cpuset_set_cpus_allowed_ptr(task, cpus_attach)); =20 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); cpuset_update_task_spread_flag(cs, task); --=20 2.31.1