Skip to contentSkip to conversation

locking/mutex: Test for initialized mutex

PATCH In review Sebastian Andrzej Siewior 3 participants 5 messages Jul 3, 2019 – Jul 25, 2019 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/mutex: Test for initialized mutex

An uninitialized/ zeroed mutex will go unnoticed because there is no check for it. There is a magic check in the unlock's slowpath path which might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

---
Nothing screamed during uninitialized usage of init_mm's context->lock
https://git.kernel.org/tip/32232b350d7cd93cdc65fe5a453e6a40b539e9f9

 kernel/locking/mutex.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/locking/mutex.c +10 -1
index 0c601ae072b3f..fb1f6f1e1cc61 100644
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 
 	might_sleep();
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif
+
 	ww = container_of(lock, struct ww_mutex, base);
 	if (use_ww_ctx && ww_ctx) {
 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
@@ -1379,8 +1383,13 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
  */
 int __sched mutex_trylock(struct mutex *lock)
 {
-	bool locked = __mutex_trylock(lock);
+	bool locked;
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif
+
+	locked = __mutex_trylock(lock);
 	if (locked)
 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
 
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 19 lines · Sebastian Andrzej Siewior

An uninitialized/ zeroed mutex will go unnoticed because there is no check for it. There is a magic check in the unlock's slowpath path which might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

---
Nothing screamed during uninitialized usage of init_mm's context->lock
https://git.kernel.org/tip/32232b350d7cd93cdc65fe5a453e6a40b539e9f9

 kernel/locking/mutex.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/locking/mutex.c +0 -0
index 0c601ae072b3f..fb1f6f1e1cc61 100644

kernel/locking/mutex.c __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, · line 913

Quoted 6 lines
kernel/locking/mutex.c +3 -0
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 
 	might_sleep();
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif

Why do we need to check this so early, or could we move it into debug_mutex_lock_common() instead?

Will

#2 lore raw
Quoted 21 lines · Will Deacon
Quoted 20 lines · Sebastian Andrzej Siewior

An uninitialized/ zeroed mutex will go unnoticed because there is no check for it. There is a magic check in the unlock's slowpath path which might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

---
Nothing screamed during uninitialized usage of init_mm's context->lock
https://git.kernel.org/tip/32232b350d7cd93cdc65fe5a453e6a40b539e9f9

 kernel/locking/mutex.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/locking/mutex.c +0 -0
index 0c601ae072b3f..fb1f6f1e1cc61 100644
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 
Quoted 8 lines

kernel/locking/mutex.c __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, · line 913

Quoted 5 lines
kernel/locking/mutex.c +3 -0
 	might_sleep();
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif

Why do we need to check this so early, or could we move it into debug_mutex_lock_common() instead?

debug_mutex_lock_common() is too late. A few lines later, before "preempt_disable()" would be possible. After that, there is __mutex_trylock() which would succeed so you don't catch the uninitialized case. By the time you get to debug_mutex_lock_common() you need contention and then acquire ->wait_lock which should complain about missing magic.

Quoted 1 line

Will

Sebastian

maintainer Acked-by
#3 lore raw
Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 21 lines · Will Deacon
Quoted 20 lines · Sebastian Andrzej Siewior

An uninitialized/ zeroed mutex will go unnoticed because there is no check for it. There is a magic check in the unlock's slowpath path which might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

---
Nothing screamed during uninitialized usage of init_mm's context->lock
https://git.kernel.org/tip/32232b350d7cd93cdc65fe5a453e6a40b539e9f9

 kernel/locking/mutex.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/locking/mutex.c +0 -0
index 0c601ae072b3f..fb1f6f1e1cc61 100644
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 
Quoted 8 lines

kernel/locking/mutex.c __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, · line 913

Quoted 5 lines
kernel/locking/mutex.c +3 -0
 	might_sleep();
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif

Why do we need to check this so early, or could we move it into debug_mutex_lock_common() instead?

Quoted 6 lines

debug_mutex_lock_common() is too late. A few lines later, before "preempt_disable()" would be possible. After that, there is __mutex_trylock() which would succeed so you don't catch the uninitialized case. By the time you get to debug_mutex_lock_common() you need contention and then acquire ->wait_lock which should complain about missing magic.

Right you are; thanks for the explanation. I don't see a better approach than what you've done, so:

Will

#4 lore raw
In reply to Sebastian Andrzej Siewior
locking/mutex: Test for initialized mutex
Commit-ID:  6c11c6e3d5e9e5caf8686cd6a5e4552cfc3ea326
Gitweb:     https://git.kernel.org/tip/6c11c6e3d5e9e5caf8686cd6a5e4552cfc3ea326
Author:     Sebastian Andrzej Siewior <[email protected]>
AuthorDate: Wed, 3 Jul 2019 11:21:26 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 25 Jul 2019 15:39:27 +0200

locking/mutex: Test for initialized mutex

An uninitialized/ zeroed mutex will go unnoticed because there is no check for it. There is a magic check in the unlock's slowpath path which might go unnoticed if the unlock happens in the fastpath.

Add a ->magic check early in the mutex_lock() and mutex_trylock() path.

---
 kernel/locking/mutex.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/locking/mutex.c +10 -1
index edd1c082dbf5..5e069734363c 100644
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 
 	might_sleep();
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif
+
 	ww = container_of(lock, struct ww_mutex, base);
 	if (use_ww_ctx && ww_ctx) {
 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
@@ -1379,8 +1383,13 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
  */
 int __sched mutex_trylock(struct mutex *lock)
 {
-	bool locked = __mutex_trylock(lock);
+	bool locked;
+
+#ifdef CONFIG_DEBUG_MUTEXES
+	DEBUG_LOCKS_WARN_ON(lock->magic != lock);
+#endif
 
+	locked = __mutex_trylock(lock);
 	if (locked)
 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);