Skip to content

Commit

Permalink
locking/mutex: Complain upon mutex API misuse in IRQ contexts
Browse files Browse the repository at this point in the history
Add warning checks if mutex_trylock() or mutex_unlock() are used in
IRQ contexts, under CONFIG_DEBUG_MUTEXES=y.

While the mutex rules and semantics are explicitly documented, this allows
to expose any abusers and robustifies the whole thing.

While trylock and unlock are non-blocking, calling from IRQ context
is still forbidden (lock must be within the same context as unlock).

Signed-off-by: Davidlohr Bueso <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Davidlohr Bueso authored and Ingo Molnar committed Oct 29, 2019
1 parent 7514590 commit a0855d2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kernel/locking/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne
*/
void __sched mutex_unlock(struct mutex *lock)
{
#ifdef CONFIG_DEBUG_MUTEXES
WARN_ON(in_interrupt());
#endif
#ifndef CONFIG_DEBUG_LOCK_ALLOC
if (__mutex_unlock_fast(lock))
return;
Expand Down Expand Up @@ -1413,6 +1416,7 @@ int __sched mutex_trylock(struct mutex *lock)

#ifdef CONFIG_DEBUG_MUTEXES
DEBUG_LOCKS_WARN_ON(lock->magic != lock);
WARN_ON(in_interrupt());
#endif

locked = __mutex_trylock(lock);
Expand Down

0 comments on commit a0855d2

Please sign in to comment.