From: Willy Tarreau <w@1wt.eu>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org, linux@roeck-us.net
Cc: Balbir Singh <bsingharora@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Alexey Kardashevskiy <aik@ozlabs.ru>,
Linus Torvalds <torvalds@linux-foundation.org>,
Nicholas Piggin <nicholas.piggin@gmail.com>,
Nicholas Piggin <npiggin@gmail.com>,
Oleg Nesterov <oleg@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Willy Tarreau <w@1wt.eu>
Subject: [PATCH 3.10 001/319] sched/core: Fix a race between try_to_wake_up() and a woken up task
Date: Sun, 5 Feb 2017 20:09:05 +0100 [thread overview]
Message-ID: <1486322063-7558-2-git-send-email-w@1wt.eu> (raw)
In-Reply-To: <1486322063-7558-1-git-send-email-w@1wt.eu>
From: Balbir Singh <bsingharora@gmail.com>
commit 135e8c9250dd5c8c9aae5984fde6f230d0cbfeaf upstream.
The origin of the issue I've seen is related to
a missing memory barrier between check for task->state and
the check for task->on_rq.
The task being woken up is already awake from a schedule()
and is doing the following:
do {
schedule()
set_current_state(TASK_(UN)INTERRUPTIBLE);
} while (!cond);
The waker, actually gets stuck doing the following in
try_to_wake_up():
while (p->on_cpu)
cpu_relax();
Analysis:
The instance I've seen involves the following race:
CPU1 CPU2
while () {
if (cond)
break;
do {
schedule();
set_current_state(TASK_UN..)
} while (!cond);
wakeup_routine()
spin_lock_irqsave(wait_lock)
raw_spin_lock_irqsave(wait_lock) wake_up_process()
} try_to_wake_up()
set_current_state(TASK_RUNNING); ..
list_del(&waiter.list);
CPU2 wakes up CPU1, but before it can get the wait_lock and set
current state to TASK_RUNNING the following occurs:
CPU3
wakeup_routine()
raw_spin_lock_irqsave(wait_lock)
if (!list_empty)
wake_up_process()
try_to_wake_up()
raw_spin_lock_irqsave(p->pi_lock)
..
if (p->on_rq && ttwu_wakeup())
..
while (p->on_cpu)
cpu_relax()
..
CPU3 tries to wake up the task on CPU1 again since it finds
it on the wait_queue, CPU1 is spinning on wait_lock, but immediately
after CPU2, CPU3 got it.
CPU3 checks the state of p on CPU1, it is TASK_UNINTERRUPTIBLE and
the task is spinning on the wait_lock. Interestingly since p->on_rq
is checked under pi_lock, I've noticed that try_to_wake_up() finds
p->on_rq to be 0. This was the most confusing bit of the analysis,
but p->on_rq is changed under runqueue lock, rq_lock, the p->on_rq
check is not reliable without this fix IMHO. The race is visible
(based on the analysis) only when ttwu_queue() does a remote wakeup
via ttwu_queue_remote. In which case the p->on_rq change is not
done uder the pi_lock.
The result is that after a while the entire system locks up on
the raw_spin_irqlock_save(wait_lock) and the holder spins infintely
Reproduction of the issue:
The issue can be reproduced after a long run on my system with 80
threads and having to tweak available memory to very low and running
memory stress-ng mmapfork test. It usually takes a long time to
reproduce. I am trying to work on a test case that can reproduce
the issue faster, but thats work in progress. I am still testing the
changes on my still in a loop and the tests seem OK thus far.
Big thanks to Benjamin and Nick for helping debug this as well.
Ben helped catch the missing barrier, Nick caught every missing
bit in my theory.
Signed-off-by: Balbir Singh <bsingharora@gmail.com>
[ Updated comment to clarify matching barriers. Many
architectures do not have a full barrier in switch_to()
so that cannot be relied upon. ]
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Nicholas Piggin <nicholas.piggin@gmail.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/e02cce7b-d9ca-1ad0-7a61-ea97c7582b37@gmail.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
---
kernel/sched/core.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 655d611..c8afc2c 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1501,6 +1501,28 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
success = 1; /* we're going to change ->state */
cpu = task_cpu(p);
+ /*
+ * Ensure we load p->on_rq _after_ p->state, otherwise it would
+ * be possible to, falsely, observe p->on_rq == 0 and get stuck
+ * in smp_cond_load_acquire() below.
+ *
+ * sched_ttwu_pending() try_to_wake_up()
+ * [S] p->on_rq = 1; [L] P->state
+ * UNLOCK rq->lock -----.
+ * \
+ * +--- RMB
+ * schedule() /
+ * LOCK rq->lock -----'
+ * UNLOCK rq->lock
+ *
+ * [task p]
+ * [S] p->state = UNINTERRUPTIBLE [L] p->on_rq
+ *
+ * Pairs with the UNLOCK+LOCK on rq->lock from the
+ * last wakeup of our task and the schedule that got our task
+ * current.
+ */
+ smp_rmb();
if (p->on_rq && ttwu_remote(p, wake_flags))
goto stat;
--
2.8.0.rc2.1.gbe9624a
next prev parent reply other threads:[~2017-02-05 19:16 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-05 19:09 [PATCH 3.10 000/319] 3.10.105-stable review Willy Tarreau
2017-02-05 19:09 ` Willy Tarreau [this message]
2017-02-05 19:09 ` [PATCH 3.10 002/319] sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule() Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 003/319] crypto: algif_skcipher - Require setkey before accept(2) Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 004/319] crypto: af_alg - Disallow bind/setkey/... after accept(2) Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 005/319] crypto: af_alg - Add nokey compatibility path Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 006/319] crypto: algif_skcipher " Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 007/319] crypto: hash - Add crypto_ahash_has_setkey Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 008/319] crypto: shash - Fix has_key setting Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 009/319] crypto: algif_hash - Require setkey before accept(2) Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 010/319] crypto: skcipher - Add crypto_skcipher_has_setkey Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 011/319] crypto: algif_skcipher - Add key check exception for cipher_null Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 012/319] crypto: af_alg - Allow af_af_alg_release_parent to be called on nokey path Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 013/319] crypto: algif_hash - Remove custom release parent function Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 014/319] crypto: algif_skcipher " Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 015/319] crypto: af_alg - Forbid bind(2) when nokey child sockets are present Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 016/319] crypto: algif_hash - Fix race condition in hash_check_key Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 017/319] crypto: algif_skcipher - Fix race condition in skcipher_check_key Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 018/319] crypto: algif_skcipher - Load TX SG list after waiting Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 019/319] crypto: cryptd - initialize child shash_desc on import Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 020/319] crypto: skcipher - Fix blkcipher walk OOM crash Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 021/319] crypto: gcm - Fix IV buffer size in crypto_gcm_setkey Willy Tarreau
2017-02-05 19:09 ` [PATCH 3.10 022/319] MIPS: KVM: Fix unused variable build warning Willy Tarreau
2017-02-06 3:12 ` [PATCH 3.10 000/319] 3.10.105-stable review Guenter Roeck
2017-02-06 6:30 ` Willy Tarreau
2017-02-06 8:13 ` Willy Tarreau
2017-02-06 14:46 ` Guenter Roeck
2017-02-06 15:10 ` Willy Tarreau
2017-02-06 22:48 ` Willy Tarreau
2017-02-06 23:48 ` Guenter Roeck
2017-02-07 4:56 ` Guenter Roeck
2017-02-07 6:39 ` Willy Tarreau
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=1486322063-7558-2-git-send-email-w@1wt.eu \
--to=w@1wt.eu \
--cc=aik@ozlabs.ru \
--cc=bsingharora@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=mingo@kernel.org \
--cc=nicholas.piggin@gmail.com \
--cc=npiggin@gmail.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
/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
all inboxes | Powered by JetHome®