Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1307899imm; Wed, 10 Oct 2018 12:22:53 -0700 (PDT) X-Google-Smtp-Source: ACcGV62aTdObKxl37MmVAzJlcDLz8+WEjGPZLkcMuCiVtvdemU4/nh4zZNrEwnaUJo5yISeihokX X-Received: by 2002:a63:db55:: with SMTP id x21-v6mr2083152pgi.365.1539199373421; Wed, 10 Oct 2018 12:22:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539199373; cv=none; d=google.com; s=arc-20160816; b=ZRqSNmZ7DTb/mZbHBT6C/JygDTwDuEypZcJtVA1qk6yVf0STWa9DWfpVdrNgmoF8su 2md4EJujNZJn/jXCBvkzhPPDQPXfFIi1eDB7pKgDQeLo6Ck1IXLfS6eAcAAul7rS/yS2 EUUq9H8zvxrnZ+7oyNVaLl+0wki9hmmPDei9DZ5cQZyY7IY9So3NYR4+4siqJsws4wy8 P25EudJd1Y/AwNw4ruKtxilrC0KUlqpnKLjTx9HFiQIE2+iGALaDTAgIj4HZwpofzqK7 CK86kLMBBhgoHuskGPQBRTH/96EEmfvzRESWwm9Qag9KdILUsax6cRSxc1rTpGwYvaGQ SO9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=AM6ze3X0kmmhVIgh8pJVYDD0j6At2CyA49LBJH3hcV8=; b=uWK7w7nfZrfL+/Lqov014uWvQx2GGIayEHNjh44CkS8E9+TAQ9FtbuRQJGeKs4Lv+9 2Nb8FLGYKmFT+YYaXnFXg8150tQAPjSrPu8Lsx2UAG69Bj9dQTHgj7XWNpUusfil8Vpp AJ8c2y31XEwxIl1iC/j6BCnfKglx7R9qwq0P48kGqYeVK+6UIFzwQ34JWicnlCQK/izZ Tl+Ze3Znrzjeu/25YdulUfn+Ij4lEN486mbtHBVMrUD+cLxuOzA3K2GqXMzjZp2HhbrJ 6GDAKyNxYlH+zLBbr0tW/YIaFmE9VDXVy33CaOM+7zWiS1NZBThlS01P7JIBP8mHFf4y enJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=aQA3aFW8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z68-v6si16839694pfb.280.2018.10.10.12.22.38; Wed, 10 Oct 2018 12:22:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=aQA3aFW8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727627AbeJKCnk (ORCPT + 99 others); Wed, 10 Oct 2018 22:43:40 -0400 Received: from mail.efficios.com ([167.114.142.138]:32840 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727583AbeJKCnj (ORCPT ); Wed, 10 Oct 2018 22:43:39 -0400 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id A37A1183B6E; Wed, 10 Oct 2018 15:20:06 -0400 (EDT) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id TSTY1x-0fJit; Wed, 10 Oct 2018 15:20:06 -0400 (EDT) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 25D16183B5E; Wed, 10 Oct 2018 15:20:06 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 25D16183B5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1539199206; bh=AM6ze3X0kmmhVIgh8pJVYDD0j6At2CyA49LBJH3hcV8=; h=From:To:Date:Message-Id; b=aQA3aFW8AzCPh6wHxSQf3H7Vw9GUOW5efxWoRBe7Z1w3ZqU/96dFb+eNeuzi1FuOC yZwkShJjAqYPKnnxY0O24NbmFbvB0Wf2+6CHkVWIq+zPYf4zPPHuV+2qF4KOMJ3umF X7L/9XMAtveBpb3/QSSwCR2Asfdid189qWTXPclDAKi2eoQhWB2OIZqjPLI5Sfr/mv dGu++MaABHUt0k+EypIU8okS0fMNUDpIjIOt6rgCBOOXRMrGYb48EOw5FejeqP6pqB AeNxXVIh4aLTpiwCqgSAfguvhS46D19NrOjhj4xLgxj290NCO9ulG/4Zw4Tww1Pf3G JxDaxbW73hs9g== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id 3Z3X1cTx8lUD; Wed, 10 Oct 2018 15:20:06 -0400 (EDT) Received: from thinkos.internal.efficios.com (192-222-157-41.qc.cable.ebox.net [192.222.157.41]) by mail.efficios.com (Postfix) with ESMTPSA id 1429A183B4D; Wed, 10 Oct 2018 15:20:05 -0400 (EDT) From: Mathieu Desnoyers To: Peter Zijlstra , "Paul E . McKenney" , Boqun Feng Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Thomas Gleixner , Andy Lutomirski , Dave Watson , Paul Turner , Andrew Morton , Russell King , Ingo Molnar , "H . Peter Anvin" , Andi Kleen , Chris Lameter , Ben Maurer , Steven Rostedt , Josh Triplett , Linus Torvalds , Catalin Marinas , Will Deacon , Michael Kerrisk , Joel Fernandes , Mathieu Desnoyers Subject: [RFC PATCH for 4.21 03/16] sched: Implement push_task_to_cpu (v2) Date: Wed, 10 Oct 2018 15:19:23 -0400 Message-Id: <20181010191936.7495-4-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181010191936.7495-1-mathieu.desnoyers@efficios.com> References: <20181010191936.7495-1-mathieu.desnoyers@efficios.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement push_task_to_cpu(), which moves the task received as argument to the destination cpu's runqueue. It only does so if the CPU is within the CPU allowed mask of the task and if the CPU is active. If the CPU is not part of the allowed mask, -EINVAL is returned. If the CPU is not active, -EBUSY is returned. It does not change the CPU allowed mask, and can therefore be used within applications which rely on owning the sched_setaffinity() state. It does not pin the task to the destination CPU, which means that the scheduler may choose to move the task away from that CPU before the task executes. Code invoking push_task_to_cpu() must be prepared to retry in that case. Signed-off-by: Mathieu Desnoyers CC: "Paul E. McKenney" CC: Peter Zijlstra CC: Paul Turner CC: Thomas Gleixner CC: Andy Lutomirski CC: Andi Kleen CC: Dave Watson CC: Chris Lameter CC: Ingo Molnar CC: "H. Peter Anvin" CC: Ben Maurer CC: Steven Rostedt CC: Josh Triplett CC: Linus Torvalds CC: Andrew Morton CC: Russell King CC: Catalin Marinas CC: Will Deacon CC: Michael Kerrisk CC: Boqun Feng CC: linux-api@vger.kernel.org --- Change since v1: - Return -EBUSY if CPU is not active. --- kernel/sched/core.c | 42 ++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 9 +++++++++ 2 files changed, 51 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ad97f3ba5ec5..ee302988b342 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1036,6 +1036,48 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) set_curr_task(rq, p); } +int push_task_to_cpu(struct task_struct *p, unsigned int dest_cpu) +{ + struct rq_flags rf; + struct rq *rq; + int ret = 0; + + rq = task_rq_lock(p, &rf); + update_rq_clock(rq); + + if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) { + ret = -EINVAL; + goto out; + } + + if (!cpumask_test_cpu(dest_cpu, cpu_active_mask)) { + ret = -EBUSY; + goto out; + } + + if (task_cpu(p) == dest_cpu) + goto out; + + if (task_running(rq, p) || p->state == TASK_WAKING) { + struct migration_arg arg = { p, dest_cpu }; + /* Need help from migration thread: drop lock and wait. */ + task_rq_unlock(rq, p, &rf); + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); + tlb_migrate_finish(p->mm); + return 0; + } else if (task_on_rq_queued(p)) { + /* + * OK, since we're going to drop the lock immediately + * afterwards anyway. + */ + rq = move_queued_task(rq, &rf, p, dest_cpu); + } +out: + task_rq_unlock(rq, p, &rf); + + return ret; +} + /* * Change a given task's CPU affinity. Migrate the thread to a * proper CPU and schedule it away if the CPU it's executing on diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 455fa330de04..27ad25780204 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1340,6 +1340,15 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) #endif } +#ifdef CONFIG_SMP +int push_task_to_cpu(struct task_struct *p, unsigned int dest_cpu); +#else +static inline int push_task_to_cpu(struct task_struct *p, unsigned int dest_cpu) +{ + return 0; +} +#endif + /* * Tunables that become constants when CONFIG_SCHED_DEBUG is off: */ -- 2.11.0