rtmutex: Code clarification and optimization
Subsystems (5)
v2 cover rtmutex: Code clarification and optimization
Changes vs. v1 [1]:
Dropped 1/N as it's queued already.
Removed the config symbols as pointed out by Steven.
Addressed the review feedback from Steven and Jason.
Thanks,
tglx
---
[1] http://lkml.kernel.org/r/[email protected]
PATCH v2 1/5 rtmutex: Remove builtin tester
The tester has been broken for quite some time. It's possible to fix it, but the main reason for having it in the kernel was the lock steal mechanism in the rtmutex code. That's gone, so we can implement a stateful correctness tester just via the futex syscall.
--- arch/parisc/configs/c8000_defconfig | 1 arch/parisc/configs/generic-32bit_defconfig | 1 arch/s390/configs/default_defconfig | 1 arch/xtensa/configs/iss_defconfig | 1 arch/xtensa/configs/s6105_defconfig | 1 kernel/locking/Makefile | 1 kernel/locking/rtmutex-tester.c | 420 ------------------ kernel/locking/rtmutex.c | 2 kernel/locking/rtmutex_common.h | 22 lib/Kconfig.debug | 6 scripts/rt-tester/check-all.sh | 22 scripts/rt-tester/rt-tester.py | 220 --------- scripts/rt-tester/t2-l1-2rt-sameprio.tst | 94 ---- scripts/rt-tester/t2-l1-pi.tst | 77 --- scripts/rt-tester/t2-l1-signal.tst | 72 --- scripts/rt-tester/t2-l2-2rt-deadlock.tst | 84 --- scripts/rt-tester/t3-l1-pi-1rt.tst | 87 --- scripts/rt-tester/t3-l1-pi-2rt.tst | 88 --- scripts/rt-tester/t3-l1-pi-3rt.tst | 87 --- scripts/rt-tester/t3-l1-pi-signal.tst | 93 --- scripts/rt-tester/t3-l1-pi-steal.tst | 91 --- scripts/rt-tester/t3-l2-pi.tst | 87 --- scripts/rt-tester/t4-l2-pi-deboost.tst | 118 ----- scripts/rt-tester/t5-l4-pi-boost-deboost-setsched.tst | 178 ------- scripts/rt-tester/t5-l4-pi-boost-deboost.tst | 138 ----- 25 files changed, 1 insertion(+), 1991 deletions(-)
arch/parisc/configs/c8000_defconfig +0 -1
@@ -241,7 +241,6 @@ CONFIG_LOCKUP_DETECTOR=y CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC=y CONFIG_PANIC_ON_OOPS=y CONFIG_DEBUG_RT_MUTEXES=y -CONFIG_RT_MUTEX_TESTER=y CONFIG_PROVE_RCU_DELAY=y CONFIG_DEBUG_BLOCK_EXT_DEVT=y CONFIG_LATENCYTOP=y
arch/parisc/configs/generic-32bit_defconfig +0 -1
@@ -295,7 +295,6 @@ CONFIG_DEBUG_SHIRQ=y CONFIG_DETECT_HUNG_TASK=y CONFIG_TIMER_STATS=y CONFIG_DEBUG_RT_MUTEXES=y -CONFIG_RT_MUTEX_TESTER=y CONFIG_DEBUG_SPINLOCK=y CONFIG_DEBUG_MUTEXES=y CONFIG_RCU_CPU_STALL_INFO=y
arch/s390/configs/default_defconfig +0 -1
@@ -574,7 +574,6 @@ CONFIG_DEBUG_SHIRQ=y CONFIG_DETECT_HUNG_TASK=y CONFIG_TIMER_STATS=y CONFIG_DEBUG_RT_MUTEXES=y -CONFIG_RT_MUTEX_TESTER=y CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y CONFIG_PROVE_LOCKING=y CONFIG_LOCK_STAT=y
arch/xtensa/configs/iss_defconfig +0 -1
@@ -619,7 +619,6 @@ CONFIG_SCHED_DEBUG=y # CONFIG_SLUB_DEBUG_ON is not set # CONFIG_SLUB_STATS is not set # CONFIG_DEBUG_RT_MUTEXES is not set -# CONFIG_RT_MUTEX_TESTER is not set # CONFIG_DEBUG_SPINLOCK is not set # CONFIG_DEBUG_MUTEXES is not set # CONFIG_DEBUG_SPINLOCK_SLEEP is not set
arch/xtensa/configs/s6105_defconfig +0 -1
@@ -560,7 +560,6 @@ CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE= # CONFIG_DEBUG_OBJECTS is not set # CONFIG_DEBUG_SLAB is not set # CONFIG_DEBUG_RT_MUTEXES is not set -# CONFIG_RT_MUTEX_TESTER is not set CONFIG_DEBUG_SPINLOCK=y CONFIG_DEBUG_MUTEXES=y CONFIG_DEBUG_SPINLOCK_SLEEP=y
kernel/locking/Makefile +0 -1
@@ -18,7 +18,6 @@ obj-$(CONFIG_SMP) += lglock.o obj-$(CONFIG_PROVE_LOCKING) += spinlock.o obj-$(CONFIG_RT_MUTEXES) += rtmutex.o obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o -obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
kernel/locking/rtmutex-tester.c deleted +0 -420
420 lines not on this page · raw message
kernel/locking/rtmutex.c +1 -1
@@ -754,7 +754,7 @@ __rt_mutex_slowlock(struct rt_mutex *locdebug_rt_mutex_print_deadlock (waiter); -schedule_rt_mutex (lock); +schedule ();raw_spin_lock (&lock->wait_lock);set_current_state (state);
kernel/locking/rtmutex_common.h +0 -22
@@ -15,28 +15,6 @@#include <linux/rtmutex.h> /* -* The rtmutex in kernel tester is independent of rtmutex debugging. We -* call schedule_rt_mutex_test() instead of schedule() for the tasks which -* belong to the tester. That way we can delay the wakeup path of those -* threads to provoke lock stealing and testing of complex boosting scenarios. -*/ -#ifdef CONFIG_RT_MUTEX_TESTER - -extern void schedule_rt_mutex_test (struct rt_mutex *lock); - -#define schedule_rt_mutex (_lock) \ - do { \ - if (!(current->flags & PF_MUTEX_TESTER)) \ - schedule(); \ - else \ - schedule_rt_mutex_test(_lock); \ - } while (0) - -#else -# define schedule_rt_mutex (_lock) schedule() -#endif - -/* * This is the control structure for tasks blocked on a rt_mutex, * which is allocated on the kernel stack on of the blocked task. *
lib/Kconfig.debug +0 -6
@@ -828,12 +828,6 @@ config DEBUG_PI_LIST default y depends on DEBUG_RT_MUTEXES -config RT_MUTEX_TESTER - bool "Built-in scriptable tester for rt-mutexes" - depends on DEBUG_KERNEL && RT_MUTEXES - help - This option enables a rt-mutex tester. - config DEBUG_SPINLOCK bool "Spinlock and rw-lock debugging: basic checks" depends on DEBUG_KERNEL
scripts/rt-tester/check-all.sh deleted +0 -22
@@ -1,22 +0,0 @@ - - -function testit () -{ - printf "%-30s: " $1 - ./rt-tester.py $1 | grep Pass -} - -testit t2-l1-2rt-sameprio.tst -testit t2-l1-pi.tst -testit t2-l1-signal.tst -#testit t2-l2-2rt-deadlock.tst -testit t3-l1-pi-1rt.tst -testit t3-l1-pi-2rt.tst -testit t3-l1-pi-3rt.tst -testit t3-l1-pi-signal.tst -testit t3-l1-pi-steal.tst -testit t3-l2-pi.tst -testit t4-l2-pi-deboost.tst -testit t5-l4-pi-boost-deboost.tst -testit t5-l4-pi-boost-deboost-setsched.tst -
scripts/rt-tester/rt-tester.py deleted +0 -220
220 lines not on this page · raw message
scripts/rt-tester/t2-l1-2rt-sameprio.tst deleted +0 -94
94 lines not on this page · raw message
scripts/rt-tester/t2-l1-pi.tst deleted +0 -77
77 lines not on this page · raw message
scripts/rt-tester/t2-l1-signal.tst deleted +0 -72
72 lines not on this page · raw message
scripts/rt-tester/t2-l2-2rt-deadlock.tst deleted +0 -84
84 lines not on this page · raw message
scripts/rt-tester/t3-l1-pi-1rt.tst deleted +0 -87
87 lines not on this page · raw message
scripts/rt-tester/t3-l1-pi-2rt.tst deleted +0 -88
88 lines not on this page · raw message
scripts/rt-tester/t3-l1-pi-3rt.tst deleted +0 -87
87 lines not on this page · raw message
scripts/rt-tester/t3-l1-pi-signal.tst deleted +0 -93
93 lines not on this page · raw message
scripts/rt-tester/t3-l1-pi-steal.tst deleted +0 -91
91 lines not on this page · raw message
scripts/rt-tester/t3-l2-pi.tst deleted +0 -87
87 lines not on this page · raw message
scripts/rt-tester/t4-l2-pi-deboost.tst deleted +0 -118
118 lines not on this page · raw message
scripts/rt-tester/t5-l4-pi-boost-deboost-setsched.tst deleted +0 -178
178 lines not on this page · raw message
scripts/rt-tester/t5-l4-pi-boost-deboost.tst deleted +0 -138
138 lines not on this page · raw message
Re: [patch v2 1/5] rtmutex: Remove builtin tester
Quoted 8 lines · On Sat, 31 May 2014 15:57:48 -0000 Thomas Gleixner <[email protected]> wrote:
The tester has been broken for quite some time. It's possible to fix it, but the main reason for having it in the kernel was the lock steal mechanism in the rtmutex code. That's gone, so we can implement a stateful correctness tester just via the futex syscall.
Signed-off-by Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>
I'm assuming that you only added what I said was missing. I didn't re-review the rest of the patch.
-- Steve
Quoted 3 lines
Cc Lai Jiangshan <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>
PATCH v2 2/5 rtmutex: Cleanup deadlock detector debug logic
In reply to Thomas GleixnerThe conditions under which deadlock detection is conducted are unclear and undocumented.
Add constants instead of using 0/1 and provide a selection function which hides the additional debug dependency from the calling code.
Add comments where needed.
--- kernel/futex.c | 13 +++++--- kernel/locking/rtmutex.c | 59 ++++++++++++++++++++++++++++------------ kernel/locking/rtmutex.h | 7 ++++ kernel/locking/rtmutex_common.h | 15 ++++++++++ 4 files changed, 71 insertions(+), 23 deletions(-)
kernel/futex.c +8 -5
@@ -1619,7 +1619,8 @@ retry_private: this->pi_state = pi_state; ret =rt_mutex_start_proxy_lock (&pi_state->pi_mutex, this->rt_waiter, - this->task,1 ); + this->task, +RT_MUTEX_FULL_CHAINWALK );if (ret ==1 ) {/* We got the lock. */ requeue_pi_wake_futex (this, &key2, hb2);
@@ -2238,9 +2239,10 @@ retry_private:/* * Block on the PI mutex: */ -if (!trylock) - ret =rt_mutex_timed_lock (&q.pi_state->pi_mutex, to,1 ); -else { +if (!trylock) { + ret =rt_mutex_timed_lock (&q.pi_state->pi_mutex, to, +RT_MUTEX_FULL_CHAINWALK ); + }else { ret =rt_mutex_trylock (&q.pi_state->pi_mutex);/* Fixup the trylock return value: */ ret = ret ?0 : -EWOULDBLOCK ;
@@ -2562,7 +2564,8 @@ static int futex_wait_requeue_pi(u32 __u*/ WARN_ON (!q.pi_state); pi_mutex = &q.pi_state->pi_mutex; - ret =rt_mutex_finish_proxy_lock (pi_mutex, to, &rt_waiter,1 ); + ret =rt_mutex_finish_proxy_lock (pi_mutex, to, &rt_waiter, +RT_MUTEX_FULL_CHAINWALK );debug_rt_mutex_free_waiter (&rt_waiter);spin_lock (q.lock_ptr);
kernel/locking/rtmutex.c +42 -17
144 lines not on this page · raw message
kernel/locking/rtmutex.h +6 -1
@@ -22,5 +22,10 @@#define debug_rt_mutex_init (m, n) do { } while (0)#define debug_rt_mutex_deadlock (d, a ,l) do { } while (0)#define debug_rt_mutex_print_deadlock (w) do { } while (0) -#define debug_rt_mutex_detect_deadlock (w,d) (d)#define debug_rt_mutex_reset_waiter (w) do { } while (0) + +static inline int debug_rt_mutex_detect_deadlock (struct rt_mutex_waiter *waiter, +enum rtmutex_chainwalk detect) +{ +return detect ==RT_MUTEX_FULL_CHAINWALK ; +}
kernel/locking/rtmutex_common.h +15 -0
@@ -80,6 +80,21 @@ static inline struct task_struct *rt_mut }/* +* Constants for rt mutex functions which have a selectable deadlock +* detection. +* +* RT_MUTEX_MIN_CHAINWALK: Stops the lock chain walk when there are +* no further PI adjustments to be made. +* +* RT_MUTEX_FULL_CHAINWALK: Invoke deadlock detection with a full +* walk of the lock chain. +*/ +enum rtmutex_chainwalk { +RT_MUTEX_MIN_CHAINWALK , +RT_MUTEX_FULL_CHAINWALK , +}; + +/* * PI-futex support (proxy locking functions, etc.): */ extern struct task_struct *rt_mutex_next_owner (struct rt_mutex *lock);
Re: [patch v2 2/5] rtmutex: Cleanup deadlock detector debug logic
Quoted 17 lines · On Sat, 31 May 2014 15:57:49 -0000 Thomas Gleixner <[email protected]> wrote:
Index:
tip/kernel/locking/rtmutex.c
===================================================================kernel/locking/rtmutex.c +9
-0@@ -256,6 +256,25 @@ static void rt_mutex_adjust_prio(struct }/* +* Deadlock detection is conditional: +* +* If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted +* if the detect argument is == RT_MUTEX_FULL_CHAINWALK. +* +* If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always +* conducted independent of the detect argument. +* +* If the waiter argument is NULL this indicates the deboost path and
kernel/locking/rtmutex.c static void rt_mutex_adjust_prio(struct · line 277
Quoted 10 lines
kernel/locking/rtmutex.c +10
-0+* deadlock detection is disabled independent of the detect argument +* and the config settings. +*/ +static int rt_mutex_cond_detect_deadlock (struct rt_mutex_waiter *waiter, +enum rtmutex_chainwalk detect) +{ +return debug_rt_mutex_detect_deadlock (waiter, detect); +} + +/*
I'm curious to why you created this wrapper function that adds no value? Why not call debug_rt_mutex_detect_deadlock() directly?
Anyway...
Again, I didn't really check this version of the patch except to see the additions of what I mentioned before. I did thoroughly review the first version.
-- Steve
Re: [patch v2 2/5] rtmutex: Cleanup deadlock detector debug logic
Quoted 26 lines · Steven Rostedt
Quoted 22 lines · On Sat, 31 May 2014 15:57:49 -0000 Thomas Gleixner <[email protected]> wrote:
Index:
tip/kernel/locking/rtmutex.c
===================================================================kernel/locking/rtmutex.c +14
-0@@ -256,6 +256,25 @@ static void rt_mutex_adjust_prio(struct }/* +* Deadlock detection is conditional: +* +* If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted +* if the detect argument is == RT_MUTEX_FULL_CHAINWALK. +* +* If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always +* conducted independent of the detect argument. +* +* If the waiter argument is NULL this indicates the deboost path and +* deadlock detection is disabled independent of the detect argument +* and the config settings. +*/ +static int rt_mutex_cond_detect_deadlock (struct rt_mutex_waiter *waiter, +enum rtmutex_chainwalk detect)
Quoted 8 lines
kernel/locking/rtmutex.c static void rt_mutex_adjust_prio(struct · line 277
Quoted 5 lines
kernel/locking/rtmutex.c +5
-0+{ +return debug_rt_mutex_detect_deadlock (waiter, detect); +} + +/* I'm curious to why you created this wrapper function that adds no value? Why not call
debug_rt_mutex_detect_deadlock()directly?
debug_rt_mutex_detect_deadlock() smells like a magic debug feature, while rt_mutex_cond_detect_deadlock() clearly says; It's conditional.
Thanks,
tglx
Re: [patch v2 2/5] rtmutex: Cleanup deadlock detector debug logic
Quoted 11 lines · On Wed, 4 Jun 2014 15:17:35 +0200 (CEST) Thomas Gleixner <[email protected]>…
Quoted 10 lines
kernel/locking/rtmutex.c static void rt_mutex_adjust_prio(struct · line 277
Quoted 7 lines
kernel/locking/rtmutex.c +7
-0+static int rt_mutex_cond_detect_deadlock (struct rt_mutex_waiter *waiter, +enum rtmutex_chainwalk detect) +{ +return debug_rt_mutex_detect_deadlock (waiter, detect); +} + +/* I'm curious to why you created this wrapper function that adds no value? Why not call
debug_rt_mutex_detect_deadlock()directly?
Quoted 3 lines
debug_rt_mutex_detect_deadlock()smells like a magic debug feature, whilert_mutex_cond_detect_deadlock()clearly says; It's conditional.
Which brings up the next obvious question. Um, why not just rename debug_rt_mutex_detect_deadlock() to rt_mutex_cond_detect_deadlock()?
-- Steve
Re: [patch v2 2/5] rtmutex: Cleanup deadlock detector debug logic
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Wed, 4 Jun 2014 15:17:35 +0200 (CEST) Thomas Gleixner <[email protected]>…
Quoted 10 lines
kernel/locking/rtmutex.c static void rt_mutex_adjust_prio(struct · line 277
Quoted 7 lines
kernel/locking/rtmutex.c +7
-0+static int rt_mutex_cond_detect_deadlock (struct rt_mutex_waiter *waiter, +enum rtmutex_chainwalk detect) +{ +return debug_rt_mutex_detect_deadlock (waiter, detect); +} + +/* I'm curious to why you created this wrapper function that adds no value? Why not call
debug_rt_mutex_detect_deadlock()directly?
Quoted 6 lines
Quoted 3 lines
debug_rt_mutex_detect_deadlock()smells like a magic debug feature, whilert_mutex_cond_detect_deadlock()clearly says; It's conditional.Which brings up the next obvious question. Um, why not just rename
debug_rt_mutex_detect_deadlock()tort_mutex_cond_detect_deadlock()?
Because I wanted to keep the cond function in the main source file along with the comments instead of having two of the same in the headers.
Thanks,
tglx
PATCH v2 3/5 rtmutex: Confine deadlock logic to futex
In reply to Thomas GleixnerThe builtin tester is gone,, so the deadlock logic is now only required for futexes.
Remove the extra arguments for the public functions and also for the futex specific ones which get always called with deadlock detection enabled.
--- include/linux/rtmutex.h | 6 +-- kernel/futex.c | 9 +---- kernel/locking/rtmutex.c | 65 ++++++++++++++++++++-------------------- kernel/locking/rtmutex_common.h | 7 +--- 4 files changed, 41 insertions(+), 46 deletions(-)
include/linux/rtmutex.h +2 -4
@@ -90,11 +90,9 @@ extern void __rt_mutex_init(struct rt_muextern void rt_mutex_destroy (struct rt_mutex *lock);extern void rt_mutex_lock (struct rt_mutex *lock); -extern int rt_mutex_lock_interruptible (struct rt_mutex *lock, -int detect_deadlock); +extern int rt_mutex_lock_interruptible (struct rt_mutex *lock);extern int rt_mutex_timed_lock (struct rt_mutex *lock, -struct hrtimer_sleeper *timeout, -int detect_deadlock); +struct hrtimer_sleeper *timeout);extern int rt_mutex_trylock (struct rt_mutex *lock);
kernel/futex.c +3 -6
@@ -1619,8 +1619,7 @@ retry_private: this->pi_state = pi_state; ret =rt_mutex_start_proxy_lock (&pi_state->pi_mutex, this->rt_waiter, - this->task, -RT_MUTEX_FULL_CHAINWALK ); + this->task);if (ret ==1 ) {/* We got the lock. */ requeue_pi_wake_futex (this, &key2, hb2);
@@ -2240,8 +2239,7 @@ retry_private:* Block on the PI mutex: */ if (!trylock) { - ret =rt_mutex_timed_lock (&q.pi_state->pi_mutex, to, -RT_MUTEX_FULL_CHAINWALK ); + ret =__rt_mutex_timed_lock (&q.pi_state->pi_mutex, to); }else { ret =rt_mutex_trylock (&q.pi_state->pi_mutex);/* Fixup the trylock return value: */
@@ -2564,8 +2562,7 @@ static int futex_wait_requeue_pi(u32 __u*/ WARN_ON (!q.pi_state); pi_mutex = &q.pi_state->pi_mutex; - ret =rt_mutex_finish_proxy_lock (pi_mutex, to, &rt_waiter, -RT_MUTEX_FULL_CHAINWALK ); + ret =rt_mutex_finish_proxy_lock (pi_mutex, to, &rt_waiter);debug_rt_mutex_free_waiter (&rt_waiter);spin_lock (q.lock_ptr);
kernel/locking/rtmutex.c +33 -32
149 lines not on this page · raw message
kernel/locking/rtmutex_common.h +3 -4
@@ -104,12 +104,11 @@ extern void rt_mutex_proxy_unlock(struct struct task_struct *proxy_owner);extern int rt_mutex_start_proxy_lock (struct rt_mutex *lock,struct rt_mutex_waiter *waiter, -struct task_struct *task, -int detect_deadlock); +struct task_struct *task);extern int rt_mutex_finish_proxy_lock (struct rt_mutex *lock,struct hrtimer_sleeper *to, -struct rt_mutex_waiter *waiter, -int detect_deadlock); +struct rt_mutex_waiter *waiter); +extern int __rt_mutex_timed_lock (struct rt_mutex *l,struct hrtimer_sleeper *to);#ifdef CONFIG_DEBUG_RT_MUTEXES # include "rtmutex-debug.h"
Re: [patch v2 3/5] rtmutex: Confine deadlock logic to futex
Quoted 23 lines · On Sat, 31 May 2014 15:57:50 -0000 Thomas Gleixner <[email protected]> wrote:
===================================================================
kernel/locking/rtmutex.c +2
-3@@ -904,16 +904,15 @@ rt_mutex_slowunlock(struct rt_mutex *loc*/ static inline int rt_mutex_fastlock (struct rt_mutex *lock,int state, -enum rtmutex_chainwalk detect_deadlock,int (*slowfn)(struct rt_mutex *lock,int state,struct hrtimer_sleeper *timeout,enum rtmutex_chainwalk detect_deadlock)) { -if (!detect_deadlock &&likely (rt_mutex_cmpxchg (lock,NULL , current))) { +if (likely (rt_mutex_cmpxchg (lock,NULL , current))) {rt_mutex_deadlock_account_lock (lock, current);return 0 ; }else -return slowfn (lock, state,NULL , detect_deadlock); +return slowfn (lock, state,NULL ,RT_MUTEX_MIN_CHAINWALK ); } static inline int
kernel/locking/rtmutex.c rt_mutex_timed_fastlock(struct rt_mutex · line 926
Quoted 5 lines
kernel/locking/rtmutex.c +1
-1@@ -924,7 +923,7 @@ rt_mutex_timed_fastlock(struct rt_mutex struct hrtimer_sleeper *timeout, enum rtmutex_chainwalk detect_deadlock)) { -if (!detect_deadlock &&likely (rt_mutex_cmpxchg (lock,NULL , current))) { + if (detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
Did you mean to remove that "!"? Don't we want to call the slowfn all the time if detect_deadlock is FULL?
-- Steve
Quoted 12 lines
kernel/locking/rtmutex.c +1
-2rt_mutex_deadlock_account_lock (lock, current);return 0 ; }else @@ -961,55 +960,59 @@ void __sched rt_mutex_lock(struct rt_mut {might_sleep (); -rt_mutex_fastlock (lock,TASK_UNINTERRUPTIBLE ,RT_MUTEX_MIN_CHAINWALK , - rt_mutex_slowlock); +rt_mutex_fastlock (lock,TASK_UNINTERRUPTIBLE , rt_mutex_slowlock); }EXPORT_SYMBOL_GPL (rt_mutex_lock);
Re: [patch v2 3/5] rtmutex: Confine deadlock logic to futex
Quoted 24 lines · Steven Rostedt
Quoted 21 lines · On Sat, 31 May 2014 15:57:50 -0000 Thomas Gleixner <[email protected]> wrote:
===================================================================
kernel/locking/rtmutex.c +2
-3@@ -904,16 +904,15 @@ rt_mutex_slowunlock(struct rt_mutex *loc*/ static inline int rt_mutex_fastlock (struct rt_mutex *lock,int state, -enum rtmutex_chainwalk detect_deadlock,int (*slowfn)(struct rt_mutex *lock,int state,struct hrtimer_sleeper *timeout,enum rtmutex_chainwalk detect_deadlock)) { -if (!detect_deadlock &&likely (rt_mutex_cmpxchg (lock,NULL , current))) { +if (likely (rt_mutex_cmpxchg (lock,NULL , current))) {rt_mutex_deadlock_account_lock (lock, current);return 0 ; }else -return slowfn (lock, state,NULL , detect_deadlock); +return slowfn (lock, state,NULL ,RT_MUTEX_MIN_CHAINWALK ); }
Quoted 10 lines
kernel/locking/rtmutex.c rt_mutex_timed_fastlock(struct rt_mutex · line 926
Quoted 7 lines
kernel/locking/rtmutex.c +1
-1static inline int@@ -924,7 +923,7 @@ rt_mutex_timed_fastlock(struct rt_mutex struct hrtimer_sleeper *timeout, enum rtmutex_chainwalk detect_deadlock)) { -if (!detect_deadlock &&likely (rt_mutex_cmpxchg (lock,NULL , current))) { + if (detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {Did you mean to remove that "!"? Don't we want to call the slowfn all the time if
detect_deadlockis FULL?
No idea how I fat fingered this one ....
PATCH v2 4/5 rtmutex: Clarify the lock chain walk
In reply to Thomas GleixnerAdd a separate local variable for the boost/deboost logic to make the code more readable. Add comments where appropriate.
--- kernel/locking/rtmutex.c | 51 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-)
kernel/locking/rtmutex.c +43 -8
@@ -303,10 +303,11 @@ static int rt_mutex_adjust_prio_chain(ststruct rt_mutex_waiter *orig_waiter, struct task_struct *top_task) { - struct rt_mutex *lock; struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter; +struct rt_mutex_waiter *prerequeue_top_waiter;enum rtmutex_chainwalk detect_deadlock;int ret =0 , depth =0 ; +struct rt_mutex *lock;unsigned long flags; detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter,
@@ -381,6 +382,11 @@ static int rt_mutex_adjust_prio_chain(stif (!detect_deadlock && waiter->prio == task->prio) goto out_unlock_pi; +/* +* We need to trylock here as we are holding task->pi_lock, +* which is the reverse lock order versus the other rtmutex +* operations. +*/ lock = waiter->lock;if (!raw_spin_trylock (&lock->wait_lock)) {raw_spin_unlock_irqrestore (&task->pi_lock, flags);
@@ -401,7 +407,11 @@ static int rt_mutex_adjust_prio_chain(st goto out_unlock_pi; } - top_waiter =rt_mutex_top_waiter (lock); +/* +* Store the top waiter before doing any requeue operation. We +* need it for the boost/deboost decision below. +*/ + prerequeue_top_waiter =rt_mutex_top_waiter (lock);/* Requeue the waiter */ rt_mutex_dequeue (lock, waiter);
@@ -410,13 +420,18 @@ static int rt_mutex_adjust_prio_chain(st/* Release the task */ raw_spin_unlock_irqrestore (&task->pi_lock, flags); + +/* +* We must abort the chain walk if there is no lock owner even +* in the dead lock detection case, as we have nothing to +* follow here. This is the end of the chain we are walking. +*/ if (!rt_mutex_owner (lock)) {/* * If the requeue above changed the top waiter, then we need * to wake the new top waiter up to try to get the lock. */ - -if (top_waiter !=rt_mutex_top_waiter (lock)) +if (prerequeue_top_waiter !=rt_mutex_top_waiter (lock))wake_up_process (rt_mutex_top_waiter (lock)->task);raw_spin_unlock (&lock->wait_lock); goto out_put_task;
@@ -429,17 +444,32 @@ static int rt_mutex_adjust_prio_chain(straw_spin_lock_irqsave (&task->pi_lock, flags);if (waiter ==rt_mutex_top_waiter (lock)) { -/* Boost the owner */ -rt_mutex_dequeue_pi (task, top_waiter); +/* +* The waiter became the new top (highest priority) +* waiter on the lock. Replace the previous top waiter +* in the owner tasks pi waiters list with this waiter. +*/ +rt_mutex_dequeue_pi (task, prerequeue_top_waiter);rt_mutex_enqueue_pi (task, waiter);__rt_mutex_adjust_prio (task); - }else if (top_waiter == waiter) { -/* Deboost the owner */ + }else if (prerequeue_top_waiter == waiter) { +/* +* The waiter was the top waiter on the lock, but is +* no longer the top prority waiter. Replace waiter in +* the owner tasks pi waiters list with the new top +* (highest priority) waiter. +*/ rt_mutex_dequeue_pi (task, waiter); waiter =rt_mutex_top_waiter (lock);rt_mutex_enqueue_pi (task, waiter);__rt_mutex_adjust_prio (task); + + }else { +/* +* Nothing changed. No need to do any priority +* adjustment. +*/ }raw_spin_unlock_irqrestore (&task->pi_lock, flags);
@@ -447,6 +477,11 @@ static int rt_mutex_adjust_prio_chain(st top_waiter =rt_mutex_top_waiter (lock);raw_spin_unlock (&lock->wait_lock); +/* +* If the current waiter is not the top waiter on the lock, +* then we can stop the chain walk here if we are not in full +* deadlock detection mode. +*/ if (!detect_deadlock && waiter != top_waiter) goto out_put_task;
Re: [patch v2 4/5] rtmutex: Clarify the lock chain walk
Quoted 6 lines · On Sat, 31 May 2014 15:57:51 -0000 Thomas Gleixner <[email protected]> wrote:
Add a separate local variable for the boost/deboost logic to make the code more readable. Add comments where appropriate.
Signed-off-by Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>
-- Steve
Quoted 4 lines
Cc Lai Jiangshan <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>---
PATCH v2 5/5 rtmutex: Avoid pointless requeueing in the deadlock detection chain walk
In reply to Thomas GleixnerIn case the dead lock detector is enabled we follow the lock chain to the end in rt_mutex_adjust_prio_chain, even if we could stop earlier due to the priority/waiter constellation.
But once we are not longer the top priority waiter in a certain step or the task holding the lock has already the same priority then there is no point in dequeing and enqueing along the lock chain as there is no change at all.
So stop the queueing at this point.
--- kernel/locking/rtmutex.c | 107 ++++++++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 41 deletions(-)
kernel/locking/rtmutex.c +66 -41
@@ -309,6 +309,7 @@ static int rt_mutex_adjust_prio_chain(stint ret =0 , depth =0 ;struct rt_mutex *lock;unsigned long flags; +bool requeue = true; detect_deadlock =rt_mutex_cond_detect_deadlock (orig_waiter, deadlock_detect);
@@ -369,18 +370,29 @@ static int rt_mutex_adjust_prio_chain(st goto out_unlock_pi;/* * If deadlock detection is off, we stop here if we -* are not the top pi waiter of the task. -*/ -if (!detect_deadlock && top_waiter !=task_top_pi_waiter (task)) - goto out_unlock_pi; +* are not the top pi waiter of the task. If deadlock +* detection is enabled we continue, but stop the +* requeueing in the chain walk. +*/ +if (top_waiter !=task_top_pi_waiter (task)) { +if (!detect_deadlock) + goto out_unlock_pi; + requeue = false; + } }/* -* When deadlock detection is off then we check, if further -* priority adjustment is necessary. +* If the waiter priority is the same as the task priority +* then there is no further priority adjustment necessary. If +* deadlock detection is off, we stop the chain walk. If its +* enable we continue, but stop the requeueing in the chain +* walk. */ -if (!detect_deadlock && waiter->prio == task->prio) - goto out_unlock_pi; +if (waiter->prio == task->prio) { +if (!detect_deadlock) + goto out_unlock_pi; + requeue = false; + }/* * We need to trylock here as we are holding task->pi_lock,
@@ -413,10 +425,16 @@ static int rt_mutex_adjust_prio_chain(st*/ prerequeue_top_waiter =rt_mutex_top_waiter (lock); -/* Requeue the waiter */ -rt_mutex_dequeue (lock, waiter); - waiter->prio = task->prio; -rt_mutex_enqueue (lock, waiter); +/* +* Requeue the waiter, if we are in the boost/deboost +* operation and not just following the lock chain for +* deadlock detection. +*/ +if (requeue) { +rt_mutex_dequeue (lock, waiter); + waiter->prio = task->prio; +rt_mutex_enqueue (lock, waiter); + }/* Release the task */ raw_spin_unlock_irqrestore (&task->pi_lock, flags);
@@ -431,7 +449,8 @@ static int rt_mutex_adjust_prio_chain(st* If the requeue above changed the top waiter, then we need * to wake the new top waiter up to try to get the lock. */ -if (prerequeue_top_waiter !=rt_mutex_top_waiter (lock)) +if (requeue && + prerequeue_top_waiter !=rt_mutex_top_waiter (lock))wake_up_process (rt_mutex_top_waiter (lock)->task);raw_spin_unlock (&lock->wait_lock); goto out_put_task;
@@ -441,38 +460,44 @@ static int rt_mutex_adjust_prio_chain(st/* Grab the next task */ task =rt_mutex_owner (lock);get_task_struct (task); -raw_spin_lock_irqsave (&task->pi_lock, flags); -if (waiter ==rt_mutex_top_waiter (lock)) { -/* -* The waiter became the new top (highest priority) -* waiter on the lock. Replace the previous top waiter -* in the owner tasks pi waiters list with this waiter. -*/ -rt_mutex_dequeue_pi (task, prerequeue_top_waiter); -rt_mutex_enqueue_pi (task, waiter); -__rt_mutex_adjust_prio (task); +if (requeue) { +raw_spin_lock_irqsave (&task->pi_lock, flags); - }else if (prerequeue_top_waiter == waiter) { -/* -* The waiter was the top waiter on the lock, but is -* no longer the top prority waiter. Replace waiter in -* the owner tasks pi waiters list with the new top -* (highest priority) waiter. -*/ -rt_mutex_dequeue_pi (task, waiter); - waiter =rt_mutex_top_waiter (lock); -rt_mutex_enqueue_pi (task, waiter); -__rt_mutex_adjust_prio (task); +if (waiter ==rt_mutex_top_waiter (lock)) { +/* +* The waiter became the new top (highest +* priority) waiter on the lock. Replace the +* previous top waiter in the owner tasks pi +* waiters list with this waiter. +*/ +rt_mutex_dequeue_pi (task, prerequeue_top_waiter); +rt_mutex_enqueue_pi (task, waiter); +__rt_mutex_adjust_prio (task); + + }else if (prerequeue_top_waiter == waiter) { +/* +* The waiter was the top waiter on the lock, +* but is no longer the top prority +* waiter. Replace waiter in the owner tasks +* pi waiters list with the new top (highest +* priority) waiter. +*/ +rt_mutex_dequeue_pi (task, waiter); + waiter =rt_mutex_top_waiter (lock); +rt_mutex_enqueue_pi (task, waiter); +__rt_mutex_adjust_prio (task); + + }else { +/* +* Nothing changed. No need to do any priority +* adjustment. +*/ - }else { -/* -* Nothing changed. No need to do any priority -* adjustment. -*/ - } + } -raw_spin_unlock_irqrestore (&task->pi_lock, flags); +raw_spin_unlock_irqrestore (&task->pi_lock, flags); + } top_waiter =rt_mutex_top_waiter (lock);raw_spin_unlock (&lock->wait_lock);
Re: [patch v2 5/5] rtmutex: Avoid pointless requeueing in the deadlock detection chain walk
On Sat, 31 May 2014 15:57:51 -0000
Thomas Gleixner <[email protected]> wrote:
/*
Quoted 5 lines
kernel/locking/rtmutex.c +3
-2-* When deadlock detection is off then we check, if further -* priority adjustment is necessary. +* If the waiter priority is the same as the task priority +* then there is no further priority adjustment necessary. If +* deadlock detection is off, we stop the chain walk. If its
kernel/locking/rtmutex.c static int rt_mutex_adjust_prio_chain(st · line 395
Quoted 10 lines
kernel/locking/rtmutex.c +7
-2+* enable we continue, but stop the requeueing in the chain +* walk. */ -if (!detect_deadlock && waiter->prio == task->prio) - goto out_unlock_pi; +if (waiter->prio == task->prio) { +if (!detect_deadlock) + goto out_unlock_pi; + requeue = false; + }
I've been thinking about this some more. Not just from an optimization point of view, but also code clarity point of view. For some reason, every time I look at this if block, I need to formulate how and why this works. I'm wondering, for readability, if we should add an else?
if (waiter->prio == task->prio) {
if (detect_deadlock)
requeue = false;
else
goto out_unlock_pi;
}Does that read better? Functionality wise it's the same, so this would only be to help understand the code better.
kernel/locking/rtmutex.c static int rt_mutex_adjust_prio_chain(st · line 426
Quoted 6 lines
kernel/locking/rtmutex.c +0
-0/* * We need to trylock here as we are holding task->pi_lock, @@ -413,10 +425,16 @@ static int rt_mutex_adjust_prio_chain(st*/ prerequeue_top_waiter =rt_mutex_top_waiter (lock);
We could move the setting of prerequeue_top_waiter into the if (requeue) block too.
Quoted 53 lines
kernel/locking/rtmutex.c +14
-22-/* Requeue the waiter */ -rt_mutex_dequeue (lock, waiter); - waiter->prio = task->prio; -rt_mutex_enqueue (lock, waiter); +/* +* Requeue the waiter, if we are in the boost/deboost +* operation and not just following the lock chain for +* deadlock detection. +*/ +if (requeue) { +rt_mutex_dequeue (lock, waiter); + waiter->prio = task->prio; +rt_mutex_enqueue (lock, waiter); + }/* Release the task */ raw_spin_unlock_irqrestore (&task->pi_lock, flags);@@ -431,7 +449,8 @@ static int rt_mutex_adjust_prio_chain(st* If the requeue above changed the top waiter, then we need * to wake the new top waiter up to try to get the lock. */ -if (prerequeue_top_waiter !=rt_mutex_top_waiter (lock)) +if (requeue && + prerequeue_top_waiter !=rt_mutex_top_waiter (lock))wake_up_process (rt_mutex_top_waiter (lock)->task);raw_spin_unlock (&lock->wait_lock); goto out_put_task;@@ -441,38 +460,44 @@ static int rt_mutex_adjust_prio_chain(st/* Grab the next task */ task =rt_mutex_owner (lock);get_task_struct (task); -raw_spin_lock_irqsave (&task->pi_lock, flags); -if (waiter ==rt_mutex_top_waiter (lock)) { -/* -* The waiter became the new top (highest priority) -* waiter on the lock. Replace the previous top waiter -* in the owner tasks pi waiters list with this waiter. -*/ -rt_mutex_dequeue_pi (task, prerequeue_top_waiter); -rt_mutex_enqueue_pi (task, waiter); -__rt_mutex_adjust_prio (task); +if (requeue) { +raw_spin_lock_irqsave (&task->pi_lock, flags); - }else if (prerequeue_top_waiter == waiter) { -/* -* The waiter was the top waiter on the lock, but is -* no longer the top prority waiter. Replace waiter in -* the owner tasks pi waiters list with the new top -* (highest priority) waiter. -*/
kernel/locking/rtmutex.c static int rt_mutex_adjust_prio_chain(st · line 472
Quoted 10 lines
kernel/locking/rtmutex.c +6
-4-rt_mutex_dequeue_pi (task, waiter); - waiter =rt_mutex_top_waiter (lock); -rt_mutex_enqueue_pi (task, waiter); -__rt_mutex_adjust_prio (task); +if (waiter ==rt_mutex_top_waiter (lock)) { +/* +* The waiter became the new top (highest +* priority) waiter on the lock. Replace the +* previous top waiter in the owner tasks pi +* waiters list with this waiter.
FYI,
* The waiter became the new top (highest priority)
* waiter on the lock. Replace the previous top waiter
* in the owner tasks pi waiters list with this waiter.
Is still under the 80 char limit.
kernel/locking/rtmutex.c static int rt_mutex_adjust_prio_chain(st · line 484
Quoted 12 lines
kernel/locking/rtmutex.c +12
-0+*/ +rt_mutex_dequeue_pi (task, prerequeue_top_waiter); +rt_mutex_enqueue_pi (task, waiter); +__rt_mutex_adjust_prio (task); + + }else if (prerequeue_top_waiter == waiter) { +/* +* The waiter was the top waiter on the lock, +* but is no longer the top prority +* waiter. Replace waiter in the owner tasks +* pi waiters list with the new top (highest +* priority) waiter.
FYI,
* The waiter was the top waiter on the lock, but is no
* longer the top priority waiter. Replace waiter in the
* owner tasks pi waiters list with the new top (hightest
* priority) waiter.
Is also under the 80 char limit.
Quoted 17 lines
kernel/locking/rtmutex.c +11
-4+*/ +rt_mutex_dequeue_pi (task, waiter); + waiter =rt_mutex_top_waiter (lock); +rt_mutex_enqueue_pi (task, waiter); +__rt_mutex_adjust_prio (task); + + }else { +/* +* Nothing changed. No need to do any priority +* adjustment. +*/ - }else { -/* -* Nothing changed. No need to do any priority -* adjustment.
kernel/locking/rtmutex.c static int rt_mutex_adjust_prio_chain(st · line 503
Quoted 10 lines
kernel/locking/rtmutex.c +3
-3-*/ - } + } -raw_spin_unlock_irqrestore (&task->pi_lock, flags); +raw_spin_unlock_irqrestore (&task->pi_lock, flags); + } top_waiter =rt_mutex_top_waiter (lock);raw_spin_unlock (&lock->wait_lock);
As I only had small nits to comment about, the rest looks good...
-- Steve