2019-10-25 19:21:44

by Davidlohr Bueso

[permalink] [raw]
Subject: [PATCH -tip] locking/mutex: Complain upon api misuse wrt interrupt context

Sprinkle warning checks, under CONFIG_DEBUG_MUTEXES. 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]>
---
kernel/locking/mutex.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 468a9b8422e3..7e9c316c646c 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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;
@@ -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);
--
2.16.4


2019-10-25 20:47:40

by Davidlohr Bueso

[permalink] [raw]
Subject: Re: [PATCH -tip] locking/mutex: Complain upon api misuse wrt interrupt context

On Fri, 25 Oct 2019, Peter Zijlstra wrote:
>No real objection, but should not lockdep already complain about this?
>__mutex_unlock_slowpath() takes ->wait_lock irq-unsafe, so then using it
>from an IRQ should generate an insta IRQ inversion report.

But we still have the unlock fastpath and the trylock scenarios which
don't take locks.

Thanks,
davidlohr

2019-10-25 22:15:59

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH -tip] locking/mutex: Complain upon api misuse wrt interrupt context

On Thu, Oct 24, 2019 at 08:36:34PM -0700, Davidlohr Bueso wrote:
> Sprinkle warning checks, under CONFIG_DEBUG_MUTEXES. 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]>
> ---
> kernel/locking/mutex.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
> index 468a9b8422e3..7e9c316c646c 100644
> --- a/kernel/locking/mutex.c
> +++ b/kernel/locking/mutex.c
> @@ -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;
> @@ -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);


No real objection, but should not lockdep already complain about this?
__mutex_unlock_slowpath() takes ->wait_lock irq-unsafe, so then using it
from an IRQ should generate an insta IRQ inversion report.

Subject: [tip: locking/core] locking/mutex: Complain upon mutex API misuse in IRQ contexts

The following commit has been merged into the locking/core branch of tip:

Commit-ID: a0855d24fc22d49cdc25664fb224caee16998683
Gitweb: https://git.kernel.org/tip/a0855d24fc22d49cdc25664fb224caee16998683
Author: Davidlohr Bueso <[email protected]>
AuthorDate: Thu, 24 Oct 2019 20:36:34 -07:00
Committer: Ingo Molnar <[email protected]>
CommitterDate: Tue, 29 Oct 2019 12:22:52 +01:00

locking/mutex: Complain upon mutex API misuse in IRQ contexts

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]>
---
kernel/locking/mutex.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 5352ce5..54cc5f9 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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;
@@ -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);