Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp724836pxj; Wed, 2 Jun 2021 09:50:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAxea79eqZiZQ6YlCXqW0M/8x39J4GWG5MfuJwcHJaWAuDxQal5+B1CUe18J/FLmyN6Ikm X-Received: by 2002:a17:906:4d56:: with SMTP id b22mr20243005ejv.78.1622652602260; Wed, 02 Jun 2021 09:50:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622652602; cv=none; d=google.com; s=arc-20160816; b=WiaanbmPkAGDw6cQqsNZfgf93CL7mj0SgH/cMOzoqNjMep9WMDq8KxZKuv9Ea4y7k6 qF1fV2Ff/YjVk+2rpG0nVjSA+AtG71ULXvhcGq8VAGZLwFsa2/9bWBjH4TcnNljpXwqQ MHapiZ33dhWXfmGQshYfWmhANTwAQnrUAlq3P9CW6pBrvdjVabbXRfDCWXkCTI9zUtAL dpVCJOW7HJEoxPHFZDQfOnfcV3bJPwxa/qy2SB6ePo2OBezRVTJOE1H0S8Qu55JGw/1P 134LNDHya+f+lb/x9d2CbbgbTm8Wu9GGAdQv35k5JMSsGb2za72BmhUpFuwwxn+bQj4x 9peg== 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=A7WLGvUQyqda/UKb0ieAaYnT3QoWGNcPjbW+WmG1Woo=; b=unlHZZYhs4V46MdUHvgsAv7aux8qRcCBRcbneEk8ASuj1Hj+osHt6QUOoDny9d5M78 fuMCXpTgrMkiDBHRAnrO9Dc6k1+iQWgDdegOsxaJGtmVA6BKTvp3zFY7Kt5JRwDKJ++O ub3rCnLuSM/dWVetQeaTNlHgHUuNqKZDl7+ZXPcs2+wjScWSYHWF90oXBKxzVtzzN9WB 7JgNTQ4opePeutZkgBu4PeKDhqUwkejj+0UOxuIktGDL6xzQ+jz1OVkpbizSvBI6KCu8 eAJrFqojfoWfRewyJ9fn5K5X1dOn7CJtTV8yczoiZiOqXrQaN0N4knEbb+c9ysgPp2Q8 1EoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="dHkzD/8Y"; 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 dk3si308842edb.476.2021.06.02.09.49.39; Wed, 02 Jun 2021 09:50:02 -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="dHkzD/8Y"; 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 S231200AbhFBQtx (ORCPT + 99 others); Wed, 2 Jun 2021 12:49:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:33058 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230129AbhFBQtu (ORCPT ); Wed, 2 Jun 2021 12:49:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4433361C23; Wed, 2 Jun 2021 16:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622652486; bh=AS0Dfvtc9CwjqEfp017w8qkmCTdP83Khr1WVDy6alPY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dHkzD/8YGi5Dp4yVDq8+zNzL+0NxpN8YyW7qzVMxP2p5OQ2rylZiSfzun8aYObiKn /DYYid+IVqYatIHScN4ZB1TO1fg6pDBA6aHVaSQrVxbPfSzK2oOx/d2HW4OYKC7ohB M4/EeQJVV63sxPKDqYK7hT5uNtH9tAzUKjmgurXxum/pyg1aejMUbuoiud9K3LFYeH NZXTZm0tCwj/OwHSg4FBmVmQz7JGVJTOdhv3yCC1QkiC33SKRN3sU83RsciiT2LmS/ WIuW/WFL6Po6ZbIdXrsEgCdTNqc9SYP2E6D+W/aALeBWfVmOGp4WLjD2aHbHYOddtq wON7Wy1ji2uCw== 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 , Johannes Weiner , Ingo Molnar , Juri Lelli , Vincent Guittot , "Rafael J. Wysocki" , Dietmar Eggemann , Daniel Bristot de Oliveira , Valentin Schneider , kernel-team@android.com Subject: [PATCH v8 09/19] sched: Introduce task_struct::user_cpus_ptr to track requested affinity Date: Wed, 2 Jun 2021 17:47:09 +0100 Message-Id: <20210602164719.31777-10-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210602164719.31777-1-will@kernel.org> References: <20210602164719.31777-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 b23c7f0ab31a..206e7bf95c79 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2126,6 +2126,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.32.0.rc0.204.g9fa02ecfa5-goog