Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1653911pxj; Wed, 19 May 2021 10:42:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwc3lQMH8m0Rnmf8k8hOfhMfbBaxognJSPFMiZmnhudnOElhqiu1hW91FsSYDCDE5DgrPHq X-Received: by 2002:a5d:8b85:: with SMTP id p5mr618168iol.43.1621446157189; Wed, 19 May 2021 10:42:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621446157; cv=none; d=google.com; s=arc-20160816; b=w00HrIVuP0qGOXwFDakdHCF0QIheSF88lJWZCgAHIoa459QYzuKqhg6XW9mx65h4xU Rbu3pTlAyYbP5fcjXNK8LT9m666JEclYm0AsvihTy6ulA959xP3TTQiykvIUT57sVWSm AuvxQ83PDwefjLJQsPd8LXXFVtc+K+55pKcUr0UCXCA/YszEOFn89J8N9wSnHgu4eacf cCasOQTmLybaqhW4ckVfmaqXsIu/tvfcBAX971bB+x1mXjE0jm+fULbzE2VSqAubXf+C NAPVJFkFEbxveqHLm2WK2JyKiRW6Ok+mi9DRuz/MgaTeEjLVHtdlSLBqAKOYDvuNCvjv UDLA== 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=PErYAO4RwcsAe3B1Llbjj15mMeoNwmDiNctF5O/2p1I=; b=Dp3HfFk4XOzziWflbAfFmUWwsH2z27lLYDNeP/+lUqIiFs0coFzO7zftKoLLU+YXPA 1bJp4NzGvVw07PaBYqg+uiCNZe3xiJzLl6SUjebMoAQ7U0Ia6kY+PVaG05MC1eYZ+Sh1 Xn2hmtbsmZJ7D20ZJKeaJQ5wzM1rKQ2jAmlx0pYwaVedeGBI8OSPr/t4Bak4Hns9WXP3 StGXAS2p6x7XXKjy8H+uL3MR4uGJdYqnq4KXvrHFVOMHd0bhWWLED/pAOIeCS/Lexfe3 RfAqrNwPM2GxHC/c0GX5iugc2JxF65JbOc8N9f53BGWl//XEyU3luoRGy7TZ+WeP9Tsx xb4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jhIWlDKt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x17si18801164jan.4.2021.05.19.10.42.25; Wed, 19 May 2021 10:42:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jhIWlDKt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243084AbhERJuV (ORCPT + 99 others); Tue, 18 May 2021 05:50:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:50190 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348139AbhERJtb (ORCPT ); Tue, 18 May 2021 05:49:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 57CF1613E6; Tue, 18 May 2021 09:48:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1621331293; bh=3IIlWuzmNSSaLHDQQBcc0aUminZ817yeuijTww75wDE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jhIWlDKt5jYI09Emq1vje2nk2fkytA9qWdsOJveSi29sEf7xh5MH/O61vAAaZgLLY xmGF2hJM9JuETDkUpDVM48Wr6uyNi9rn3KkRge+l+ujaYNLqFgmf5v/kdeOqsTWOSG Udz/fowf5cFwGnCz5UctZQlz6M7BP9HQXjtE0cYhHBlwod8x4N0FKdcOpoqyIQuV1n tEaum75VtMAiznWVNebBUwCpQ2L+XyHbaDI3PtoXPdvb6KXQqgR12EToCZPUfP0sBp oluPnLaOrjqgKubHYAZaHuSXgvB39WbZ5xmGOzQDhEcahqfibLF2K8yfppeAuFev3T VsYJN5MqEu/Yw== From: Will Deacon To: linux-arm-kernel@lists.infradead.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas , Marc Zyngier , Greg Kroah-Hartman , Peter Zijlstra , Morten Rasmussen , Qais Yousef , Suren Baghdasaryan , Quentin Perret , Tejun Heo , Li Zefan , Johannes Weiner , Ingo Molnar , Juri Lelli , Vincent Guittot , "Rafael J. Wysocki" , kernel-team@android.com Subject: [PATCH v6 10/21] sched: Introduce task_struct::user_cpus_ptr to track requested affinity Date: Tue, 18 May 2021 10:47:14 +0100 Message-Id: <20210518094725.7701-11-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210518094725.7701-1-will@kernel.org> References: <20210518094725.7701-1-will@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation for saving and restoring the user-requested CPU affinity mask of a task, add a new cpumask_t pointer to 'struct task_struct'. If the pointer is non-NULL, then the mask is copied across fork() and freed on task exit. Signed-off-by: Will Deacon --- include/linux/sched.h | 13 +++++++++++++ init/init_task.c | 1 + kernel/fork.c | 2 ++ kernel/sched/core.c | 20 ++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/linux/sched.h b/include/linux/sched.h index d2c881384517..db32d4f7e5b3 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -730,6 +730,7 @@ struct task_struct { unsigned int policy; int nr_cpus_allowed; const cpumask_t *cpus_ptr; + cpumask_t *user_cpus_ptr; cpumask_t cpus_mask; void *migration_pending; #ifdef CONFIG_SMP @@ -1688,6 +1689,8 @@ extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_ #ifdef CONFIG_SMP extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask); extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask); +extern int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, int node); +extern void release_user_cpus_ptr(struct task_struct *p); #else static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) { @@ -1698,6 +1701,16 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpuma return -EINVAL; return 0; } +static inline int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, int node) +{ + if (src->user_cpus_ptr) + return -EINVAL; + return 0; +} +static inline void release_user_cpus_ptr(struct task_struct *p) +{ + WARN_ON(p->user_cpus_ptr); +} #endif extern int yield_to(struct task_struct *p, bool preempt); diff --git a/init/init_task.c b/init/init_task.c index 8b08c2e19cbb..158c2b1689e1 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -80,6 +80,7 @@ struct task_struct init_task .normal_prio = MAX_PRIO - 20, .policy = SCHED_NORMAL, .cpus_ptr = &init_task.cpus_mask, + .user_cpus_ptr = NULL, .cpus_mask = CPU_MASK_ALL, .nr_cpus_allowed= NR_CPUS, .mm = NULL, diff --git a/kernel/fork.c b/kernel/fork.c index dc06afd725cb..d3710e7f1686 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -446,6 +446,7 @@ void put_task_stack(struct task_struct *tsk) void free_task(struct task_struct *tsk) { + release_user_cpus_ptr(tsk); scs_release(tsk); #ifndef CONFIG_THREAD_INFO_IN_TASK @@ -918,6 +919,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) #endif if (orig->cpus_ptr == &orig->cpus_mask) tsk->cpus_ptr = &tsk->cpus_mask; + dup_user_cpus_ptr(tsk, orig, node); /* * One for the user space visible state that goes away when reaped. diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 21da3d7f9e47..9512623d5a60 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2129,6 +2129,26 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) __do_set_cpus_allowed(p, new_mask, 0); } +int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, + int node) +{ + if (!src->user_cpus_ptr) + return 0; + + dst->user_cpus_ptr = kmalloc_node(cpumask_size(), GFP_KERNEL, node); + if (!dst->user_cpus_ptr) + return -ENOMEM; + + cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr); + return 0; +} + +void release_user_cpus_ptr(struct task_struct *p) +{ + kfree(p->user_cpus_ptr); + p->user_cpus_ptr = NULL; +} + /* * This function is wildly self concurrent; here be dragons. * -- 2.31.1.751.gd2f1c929bd-goog