From: Peter Zijlstra <peterz@infradead.org>
To: mingo@kernel.org, oleg@redhat.com
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
paulmck@linux.vnet.ibm.com, boqun.feng@gmail.com, corbet@lwn.net,
mhocko@kernel.org, dhowells@redhat.com,
torvalds@linux-foundation.org, will.deacon@arm.com,
waiman.long@hpe.com, pjt@google.com
Subject: [PATCH 2/4] sched: Fix a race in try_to_wake_up() vs schedule()
Date: Thu, 03 Dec 2015 13:40:12 +0100 [thread overview]
Message-ID: <20151203124339.433602788@infradead.org> (raw)
In-Reply-To: 20151203124010.627312076@infradead.org
[-- Attachment #1: peterz-sched-fix-ttwu-race.patch --]
[-- Type: text/plain, Size: 2503 bytes --]
Oleg noticed that its possible to falsely observe p->on_cpu == 0 such
that we'll prematurely continue with the wakeup and effectively run p on
two CPUs at the same time.
Even though the overlap is very limited; the task is in the middle of
being scheduled out; it could still result in corruption of the
scheduler data structures.
CPU0 CPU1
set_current_state(...)
<preempt_schedule>
context_switch(X, Y)
prepare_lock_switch(Y)
Y->on_cpu = 1;
finish_lock_switch(X)
store_release(X->on_cpu, 0);
try_to_wake_up(X)
LOCK(p->pi_lock);
t = X->on_cpu; // 0
context_switch(Y, X)
prepare_lock_switch(X)
X->on_cpu = 1;
finish_lock_switch(Y)
store_release(Y->on_cpu, 0);
</preempt_schedule>
schedule();
deactivate_task(X);
X->on_rq = 0;
if (X->on_rq) // false
if (t) while (X->on_cpu)
cpu_relax();
context_switch(X, ..)
finish_lock_switch(X)
store_release(X->on_cpu, 0);
Avoid the load of X->on_cpu being hoisted over the X->on_rq load.
Reported-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/sched/core.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2084,6 +2084,25 @@ try_to_wake_up(struct task_struct *p, un
#ifdef CONFIG_SMP
/*
+ * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
+ * possible to, falsely, observe p->on_cpu == 0.
+ *
+ * One must be running (->on_cpu == 1) in order to remove oneself
+ * from the runqueue.
+ *
+ * [S] ->on_cpu = 1; [L] ->on_rq
+ * UNLOCK rq->lock
+ * RMB
+ * LOCK rq->lock
+ * [S] ->on_rq = 0; [L] ->on_cpu
+ *
+ * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
+ * from the consecutive calls to schedule(); the first switching to our
+ * task, the second putting it to sleep.
+ */
+ smp_rmb();
+
+ /*
* If the owning (remote) cpu is still in the middle of schedule() with
* this task as prev, wait until its done referencing the task.
*/
next prev parent reply other threads:[~2015-12-03 12:46 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-03 12:40 [PATCH 0/4] scheduler ordering bits -v2 Peter Zijlstra
2015-12-03 12:40 ` [PATCH 1/4] sched: Better document the try_to_wake_up() barriers Peter Zijlstra
2015-12-03 12:40 ` Peter Zijlstra [this message]
2015-12-03 12:40 ` [PATCH 3/4] locking: Introduce smp_cond_acquire() Peter Zijlstra
2015-12-03 16:37 ` Will Deacon
2015-12-03 20:26 ` Peter Zijlstra
2015-12-03 21:16 ` Peter Zijlstra
2015-12-04 14:57 ` Will Deacon
2015-12-04 20:51 ` Waiman Long
2015-12-04 22:05 ` Linus Torvalds
2015-12-04 22:48 ` Waiman Long
2015-12-04 23:43 ` Peter Zijlstra
2015-12-07 15:18 ` Will Deacon
2015-12-03 19:41 ` Davidlohr Bueso
2015-12-03 20:31 ` Peter Zijlstra
2015-12-03 12:40 ` [PATCH 4/4] sched: Document Program-Order guarantees Peter Zijlstra
2015-12-03 13:16 ` Boqun Feng
2015-12-03 13:29 ` 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=20151203124339.433602788@infradead.org \
--to=peterz@infradead.org \
--cc=boqun.feng@gmail.com \
--cc=corbet@lwn.net \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhocko@kernel.org \
--cc=mingo@kernel.org \
--cc=oleg@redhat.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=pjt@google.com \
--cc=torvalds@linux-foundation.org \
--cc=waiman.long@hpe.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox