[tip:core/urgent] mutex: Fix optimistic spinning vs. BKL

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: tip-bot for Tony Breeds
Date: Wednesday, May 19, 2010 - 12:56 am

Commit-ID:  fd6be105b883244127a734ac9f14ae94a022dcc0
Gitweb:     http://git.kernel.org/tip/fd6be105b883244127a734ac9f14ae94a022dcc0
Author:     Tony Breeds <tony@bakeyournoodle.com>
AuthorDate: Wed, 19 May 2010 15:46:36 +1000
Committer:  Ingo Molnar <mingo@elte.hu>
CommitDate: Wed, 19 May 2010 08:18:44 +0200

mutex: Fix optimistic spinning vs. BKL

Currently, we can hit a nasty case with optimistic
spinning on mutexes:

    CPU A tries to take a mutex, while holding the BKL

    CPU B tried to take the BLK while holding the mutex

This looks like a AB-BA scenario but in practice, is
allowed and happens due to the auto-release on
schedule() nature of the BKL.

In that case, the optimistic spinning code can get us
into a situation where instead of going to sleep, A
will spin waiting for B who is spinning waiting for
A, and the only way out of that loop is the
need_resched() test in mutex_spin_on_owner().

This patch fixes it by completely disabling spinning
if we own the BKL. This adds one more detail to the
extensive list of reasons why it's a bad idea for
kernel code to be holding the BKL.

Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: <stable@kernel.org>
LKML-Reference: <20100519054636.GC12389@ozlabs.org>
[ added an unlikely() attribute to the branch ]
Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
 kernel/mutex.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/kernel/mutex.c b/kernel/mutex.c
index 632f04c..4c0b7b3 100644
--- a/kernel/mutex.c
+++ b/kernel/mutex.c
@@ -172,6 +172,13 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 		struct thread_info *owner;
 
 		/*
+		 * If we own the BKL, then don't spin. The owner of
+		 * the mutex might be waiting on us to release the BKL.
+		 */
+		if (unlikely(current->lock_depth >= 0))
+			break;
+
+		/*
 		 * If there's an owner, wait for it to either
 		 * release the lock or go to sleep.
 		 */
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Tue Apr 27, 9:38 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Tue Apr 27, 9:39 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Arnd Bergmann, (Wed Apr 28, 5:06 am)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Wed Apr 28, 3:35 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Frederic Weisbecker, (Thu May 6, 10:30 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Thu May 6, 11:01 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Mike Galbraith, (Thu May 6, 11:16 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Frederic Weisbecker, (Fri May 7, 2:29 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Fri May 7, 3:27 pm)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Peter Zijlstra, (Mon May 10, 12:55 am)
[tip:core/locking] mutex: Fix optimistic spinning vs. BKL, tip-bot for Tony Breeds, (Tue May 11, 8:43 am)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Linus Torvalds, (Tue May 11, 11:06 am)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Peter Zijlstra, (Tue May 11, 11:19 am)
Re: [PATCH/RFC] mutex: Fix optimistic spinning vs. BKL, Benjamin Herrenschmidt, (Tue May 11, 2:13 pm)
[tip:core/urgent] mutex: Fix optimistic spinning vs. BKL, tip-bot for Tony Breeds, (Wed May 19, 12:56 am)