Subject: [PATCH 4/4] locking/rtmutex: Add a lockdep assert to catch potential nested blocking
From: Thomas Gleixner <[email-masked]>
Date: Thu, 27 Apr 2023 13:19:37 +0200

There used to be a BUG_ON(current->pi_blocked_on) in the lock acquisition
functions, but that vanished in one of the rtmutex overhauls.

Bring it back in form of a lockdep assert to catch code paths which take
rtmutex based locks with current::pi_blocked_on != NULL.

Reported-by: Crystal Wood <[email-masked]>
Signed-off-by: Thomas Gleixner <[email-masked]>
Signed-off-by: Sebastian Andrzej Siewior <[email-masked]>
Link: https://lore.kernel.org/r/[email-masked]
---
 kernel/locking/rtmutex.c     |    2 ++
 kernel/locking/rwbase_rt.c   |    2 ++
 kernel/locking/spinlock_rt.c |    2 ++
 3 files changed, 6 insertions(+)

Index: linux-6.3.0-rt11/kernel/locking/rtmutex.c
===================================================================
--- linux-6.3.0-rt11.orig/kernel/locking/rtmutex.c
+++ linux-6.3.0-rt11/kernel/locking/rtmutex.c
@@ -1727,6 +1727,8 @@ static int __sched rt_mutex_slowlock(str
 static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
 					   unsigned int state)
 {
+	lockdep_assert(!current->pi_blocked_on);
+
 	if (likely(rt_mutex_try_acquire(lock)))
 		return 0;
 
Index: linux-6.3.0-rt11/kernel/locking/rwbase_rt.c
===================================================================
--- linux-6.3.0-rt11.orig/kernel/locking/rwbase_rt.c
+++ linux-6.3.0-rt11/kernel/locking/rwbase_rt.c
@@ -133,6 +133,8 @@ static __always_inline int rwbase_read_l
 {
 	int ret;
 
+	lockdep_assert(!current->pi_blocked_on);
+
 	if (rwbase_read_trylock(rwb))
 		return 0;
 
Index: linux-6.3.0-rt11/kernel/locking/spinlock_rt.c
===================================================================
--- linux-6.3.0-rt11.orig/kernel/locking/spinlock_rt.c
+++ linux-6.3.0-rt11/kernel/locking/spinlock_rt.c
@@ -37,6 +37,8 @@
 
 static __always_inline void rtlock_lock(struct rt_mutex_base *rtm)
 {
+	lockdep_assert(!current->pi_blocked_on);
+
 	if (unlikely(!rt_mutex_cmpxchg_acquire(rtm, NULL, current)))
 		rtlock_slowlock(rtm);
 }