mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Jann Horn <jannh@google.com>, Waiman Long <longman@redhat.com>
Subject: [PATCH] locking/mutex: Add debug code to help catching violation of mutex lifetime rule
Date: Wed,  9 Jul 2025 15:39:10 -0400	[thread overview]
Message-ID: <20250709193910.151497-1-longman@redhat.com> (raw)

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@linux-foundation.org>
Signed-off-by: Waiman Long <longman@redhat.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


             reply	other threads:[~2025-07-09 19:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-09 19:39 Waiman Long [this message]
2025-07-11 22:20 ` Boqun Feng
2025-07-11 22:30   ` Linus Torvalds
2025-07-11 23:28     ` Boqun Feng
2025-07-12  0:14       ` Linus Torvalds
2025-07-12  0:42       ` Waiman Long
2025-07-12  1:48         ` Waiman Long
2025-07-12  2:24           ` Boqun Feng
2025-07-12  3:16             ` Waiman Long
2025-07-12  4:20               ` Boqun Feng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250709193910.151497-1-longman@redhat.com \
    --to=longman@redhat.com \
    --cc=boqun.feng@gmail.com \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®