Skip to content

Commit 6c11c6e

Browse files
Sebastian Andrzej SiewiorIngo Molnar
authored andcommitted
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. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Will Deacon <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 30a35f7 commit 6c11c6e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

kernel/locking/mutex.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
908908

909909
might_sleep();
910910

911+
#ifdef CONFIG_DEBUG_MUTEXES
912+
DEBUG_LOCKS_WARN_ON(lock->magic != lock);
913+
#endif
914+
911915
ww = container_of(lock, struct ww_mutex, base);
912916
if (use_ww_ctx && ww_ctx) {
913917
if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
@@ -1379,8 +1383,13 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
13791383
*/
13801384
int __sched mutex_trylock(struct mutex *lock)
13811385
{
1382-
bool locked = __mutex_trylock(lock);
1386+
bool locked;
1387+
1388+
#ifdef CONFIG_DEBUG_MUTEXES
1389+
DEBUG_LOCKS_WARN_ON(lock->magic != lock);
1390+
#endif
13831391

1392+
locked = __mutex_trylock(lock);
13841393
if (locked)
13851394
mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
13861395

0 commit comments

Comments
 (0)