All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Peter Zijlstra <peterz@infradead.org>,
	"Paul E . McKenney" <paulmck@linux.vnet.ibm.com>,
	Boqun Feng <boqun.feng@gmail.com>,
	Andy Lutomirski <luto@amacapital.net>,
	Dave Watson <davejwatson@fb.com>
Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
	Paul Turner <pjt@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Russell King <linux@arm.linux.org.uk>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H . Peter Anvin" <hpa@zytor.com>,
	Andrew Hunter <ahh@google.com>, Andi Kleen <andi@firstfloor.org>,
	Chris Lameter <cl@linux.com>, Ben Maurer <bmaurer@fb.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Josh Triplett <josh@joshtriplett.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Michael Kerrisk <mtk.manpages@gmail.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Subject: [RFC PATCH for 4.17 09/21] sched: Implement push_task_to_cpu (v2)
Date: Tue, 27 Mar 2018 12:05:30 -0400	[thread overview]
Message-ID: <20180327160542.28457-10-mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <20180327160542.28457-1-mathieu.desnoyers@efficios.com>

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 <mathieu.desnoyers@efficios.com>
CC: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
CC: Peter Zijlstra <peterz@infradead.org>
CC: Paul Turner <pjt@google.com>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Andrew Hunter <ahh@google.com>
CC: Andy Lutomirski <luto@amacapital.net>
CC: Andi Kleen <andi@firstfloor.org>
CC: Dave Watson <davejwatson@fb.com>
CC: Chris Lameter <cl@linux.com>
CC: Ingo Molnar <mingo@redhat.com>
CC: "H. Peter Anvin" <hpa@zytor.com>
CC: Ben Maurer <bmaurer@fb.com>
CC: Steven Rostedt <rostedt@goodmis.org>
CC: Josh Triplett <josh@joshtriplett.org>
CC: Linus Torvalds <torvalds@linux-foundation.org>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: Russell King <linux@arm.linux.org.uk>
CC: Catalin Marinas <catalin.marinas@arm.com>
CC: Will Deacon <will.deacon@arm.com>
CC: Michael Kerrisk <mtk.manpages@gmail.com>
CC: Boqun Feng <boqun.feng@gmail.com>
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 771caa7e95c6..ef7f5eb5d56e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1062,6 +1062,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 66b070444a7e..4aaf70d54afc 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1252,6 +1252,15 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
 	rseq_migrate(p);
 }
 
+#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


  parent reply	other threads:[~2018-03-27 16:21 UTC|newest]

Thread overview: 123+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-27 16:05 [RFC PATCH for 4.17 00/21] Restartable sequences and CPU op vector Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 01/21] uapi headers: Provide types_32_64.h Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 02/21] rseq: Introduce restartable sequences system call (v12) Mathieu Desnoyers
2018-03-28  6:47   ` Boqun Feng
2018-03-28  6:47     ` Boqun Feng
2018-03-28 14:06     ` Mathieu Desnoyers
2018-03-28 14:06       ` Mathieu Desnoyers
2018-03-28 14:31       ` Mathieu Desnoyers
2018-03-28 14:31         ` Mathieu Desnoyers
2018-03-28 11:19   ` Peter Zijlstra
2018-03-28 11:19     ` Peter Zijlstra
2018-03-28 14:19     ` Mathieu Desnoyers
2018-03-28 14:19       ` Mathieu Desnoyers
2018-03-28 11:22   ` Peter Zijlstra
2018-03-28 11:22     ` Peter Zijlstra
2018-03-28 14:26     ` Mathieu Desnoyers
2018-03-28 14:26       ` Mathieu Desnoyers
2018-03-28 12:29   ` Peter Zijlstra
2018-03-28 12:29     ` Peter Zijlstra
2018-03-28 12:52     ` Peter Zijlstra
2018-03-28 12:52       ` Peter Zijlstra
2018-03-28 15:03       ` Mathieu Desnoyers
2018-03-28 15:03         ` Mathieu Desnoyers
2018-03-28 16:19     ` Mathieu Desnoyers
2018-03-28 16:19       ` Mathieu Desnoyers
2018-03-28 12:50   ` Peter Zijlstra
2018-03-28 12:50     ` Peter Zijlstra
2018-03-28 14:47     ` Mathieu Desnoyers
2018-03-28 14:47       ` Mathieu Desnoyers
2018-03-28 14:59       ` Peter Zijlstra
2018-03-28 14:59         ` Peter Zijlstra
2018-03-28 15:14         ` Mathieu Desnoyers
2018-03-28 15:14           ` Mathieu Desnoyers
2018-03-28 15:28           ` Peter Zijlstra
2018-03-28 15:28             ` Peter Zijlstra
2018-03-28 15:37             ` Mathieu Desnoyers
2018-03-28 15:37               ` Mathieu Desnoyers
2018-03-28 17:49               ` Peter Zijlstra
2018-03-28 17:49                 ` Peter Zijlstra
2018-03-28 20:19                 ` Mathieu Desnoyers
2018-03-28 20:19                   ` Mathieu Desnoyers
2018-03-28 21:25                   ` Thomas Gleixner
2018-03-28 21:25                     ` Thomas Gleixner
2018-03-29 13:54                     ` Mathieu Desnoyers
2018-03-29 13:54                       ` Mathieu Desnoyers
2018-03-29 14:23                       ` Peter Zijlstra
2018-03-29 14:23                         ` Peter Zijlstra
2018-03-29 15:39                         ` Mathieu Desnoyers
2018-03-29 15:39                           ` Mathieu Desnoyers
2018-03-29 16:24                           ` Steven Rostedt
2018-03-29 16:24                             ` Steven Rostedt
2018-03-29 18:02                             ` Mathieu Desnoyers
2018-03-29 18:02                               ` Mathieu Desnoyers
2018-03-29 18:07                               ` Steven Rostedt
2018-03-29 18:07                                 ` Steven Rostedt
2018-03-29 18:35                                 ` Mathieu Desnoyers
2018-03-29 18:35                                   ` Mathieu Desnoyers
2018-03-29 18:46                                   ` Steven Rostedt
2018-03-29 18:46                                     ` Steven Rostedt
2018-03-29 18:47                                     ` Steven Rostedt
2018-03-29 18:47                                       ` Steven Rostedt
2018-04-01 16:13   ` Alan Cox
2018-04-01 16:13     ` Alan Cox
2018-04-02 15:03     ` Christopher Lameter
2018-04-02 15:03       ` Christopher Lameter
2018-04-02 15:27       ` Paul E. McKenney
2018-04-02 15:27         ` Paul E. McKenney
2018-04-02 15:33     ` Mathieu Desnoyers
2018-04-02 15:33       ` Mathieu Desnoyers
2018-04-03 16:36       ` Mathieu Desnoyers
2018-04-03 16:36         ` Mathieu Desnoyers
2018-04-03 20:32         ` Mathieu Desnoyers
2018-04-03 20:32           ` Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 03/21] arm: Add restartable sequences support Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 04/21] arm: Wire up restartable sequences system call Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 05/21] x86: Add support for restartable sequences Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 06/21] x86: Wire up restartable sequence system call Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 07/21] powerpc: Add support for restartable sequences Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 08/21] powerpc: Wire up restartable sequences system call Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05 ` Mathieu Desnoyers [this message]
2018-03-27 16:05 ` [RFC PATCH for 4.17 10/21] cpu_opv: Provide cpu_opv system call (v6) Mathieu Desnoyers
2018-03-28 15:22   ` Peter Zijlstra
2018-03-28 15:22     ` Peter Zijlstra
2018-03-28 17:54     ` Mathieu Desnoyers
2018-03-28 17:54       ` Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 11/21] x86: Wire up cpu_opv system call Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 12/21] powerpc: " Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 13/21] arm: " Mathieu Desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 14/21] selftests: lib.mk: Introduce OVERRIDE_TARGETS Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 15/21] cpu_opv: selftests: Implement selftests (v7) Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 16/21] rseq: selftests: Provide rseq library (v5) Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 17/21] rseq: selftests: Provide percpu_op API Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 18/21] rseq: selftests: Provide basic test Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 19/21] rseq: selftests: Provide basic percpu ops test Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 20/21] rseq: selftests: Provide parametrized tests Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 16:05 ` [RFC PATCH for 4.17 21/21] rseq: selftests: Provide Makefile, scripts, gitignore Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` Mathieu Desnoyers
2018-03-27 16:05   ` mathieu.desnoyers
2018-03-27 19:09 ` [RFC PATCH for 4.17 00/21] Restartable sequences and CPU op vector Peter Zijlstra
2018-03-27 19:09   ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180327160542.28457-10-mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=ahh@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=bmaurer@fb.com \
    --cc=boqun.feng@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=cl@linux.com \
    --cc=davejwatson@fb.com \
    --cc=hpa@zytor.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=luto@amacapital.net \
    --cc=mingo@redhat.com \
    --cc=mtk.manpages@gmail.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=pjt@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.