[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250709193910.151497-1-longman@redhat.com>
Date: Wed, 9 Jul 2025 15:39:10 -0400
From: Waiman Long <longman@...hat.com>
To: Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Will Deacon <will@...nel.org>,
Boqun Feng <boqun.feng@...il.com>
Cc: linux-kernel@...r.kernel.org,
Linus Torvalds <torvalds@...ux-foundation.org>,
Jann Horn <jannh@...gle.com>,
Waiman Long <longman@...hat.com>
Subject: [PATCH] locking/mutex: Add debug code to help catching violation of mutex lifetime rule
Callers of mutex_lock/unlock() must ensure the validity of the memory
objects holding mutexes until after the return of all outstanding
and upcoming mutex_unlock() calls. Add a cond_resched() call in
__mutex_unlock_slowpath() to help KASAN catch a violation of this
rule. This will enforce a context switch if another runnable task is
present and the CPU is not in an atomic context.
Suggested-by: Linus Torvalds <torvalds@...ux-foundation.org>
Signed-off-by: Waiman Long <longman@...hat.com>
---
kernel/locking/mutex.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index a39ecccbd106..dd107fb9dad7 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -931,6 +931,17 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne
}
}
+#if defined(CONFIG_KASAN) && defined(CONFIG_DEBUG_MUTEXES)
+ /*
+ * Mutex users must ensure that the memory object holding the mutex
+ * remains valid until after the return of all the outstanding and
+ * upcoming mutex_unlock() calls. Enforce a context switch here if
+ * another runnable task is present and this CPU is not in an atomic
+ * context to increase the chance that KASAN can catch a violation of
+ * this rule.
+ */
+ cond_resched();
+#endif
raw_spin_lock_irqsave(&lock->wait_lock, flags);
debug_mutex_unlock(lock);
if (!list_empty(&lock->wait_list)) {
--
2.50.0
Powered by blists - more mailing lists