From: buckzhang1212@yeah.net
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Boqun Feng <boqun.feng@gmail.com>,
Waiman Long <longman@redhat.com>,
linux-kernel@vger.kernel.org
Cc: buckzhang1212@yeah.net
Subject: [PATCH] locking/mutex:add MUTEX_CHCEK_INIT to detect uninitialized mutex lock
Date: Tue, 23 Sep 2025 19:25:00 -0700 [thread overview]
Message-ID: <20250924022500.2577-1-buckzhang1212@yeah.net> (raw)
From: "buck.zhang" <buckzhang1212@yeah.net>
Here is a kernel exception about mutex and I can recreate it stably.
First we define a custome struct that includes a mutex lock.
Then allocate this struct by kmalloc without calling mutex_init.
Finally when multiple tasks call mutex_lock together,kernel will panic.
But Kernel is good if only one task call this mutex at the same time.
the exception reason is that lock->wait_list is an invalid kernel list.
kernel crash log:
Unable to handle kernel NULL pointer dereference at virtual address 0000000
pc: __mutex_add_waiter+0x68/0x160
lr: __mutex_add_waiter+0x128/0x160
sp: ffffffc0866f3ac0
x29: ffffffc0866f3ad0 x28: ffffff8095148000 x27: 0000000000000000
x2: ffffffc0866f3b18 x1 : 0000000000000000 x0 : 0000000000000000
Call trace:
__mutex_add_waiter+0x68/0x160
__mutex_lock+0x48c/0x119c
__mutex_lock_slowpath+0x1c/0x2c
mutex_lock+0x48/0x144
Test case:
struct chip_mutex {
struct mutex tmutex;
};
static void work_handler1(struct chip_mutex *cmutex)
{
mutex_lock(&(cmutex->tmutex));
}
static void work_handler2(struct chip_mutex *cmutex)
{
mutex_lock(&(cmutex->tmutex));
}
static void chip_tmutex(void)
{
struct chip_mutex *cmutex;
cmutex = kzalloc(sizeof(struct chip_mutex),GFP_KERNEL);
work_handler1(cmutex);
------
work_handler2(cmutex);
}
Signed-off-by: buck.zhang <buckzhang1212@yeah.net>
---
kernel/locking/mutex.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index de7d6702c..8fbe858c8 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -42,6 +42,16 @@
#else
# define MUTEX_WARN_ON(cond)
#endif
+#define MUTEX_CHCEK_INIT(lock) mutex_check_waitlist(lock)
+static void mutex_check_waitlist(struct mutex *lock)
+{
+ struct list_head *list = &lock->wait_list;
+
+ if ((unsigned long)list->next < PAGE_OFFSET) {
+ pr_err("BUG: mutex lock is uninitialized,wait_list is Error\n");
+ MUTEX_WARN_ON("mutex lock is uninitialized");
+ }
+}
void
__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
@@ -269,6 +279,7 @@ static void __sched __mutex_lock_slowpath(struct mutex *lock);
void __sched mutex_lock(struct mutex *lock)
{
might_sleep();
+ MUTEX_CHCEK_INIT(lock);
if (!__mutex_trylock_fast(lock))
__mutex_lock_slowpath(lock);
@@ -991,6 +1002,7 @@ __mutex_lock_interruptible_slowpath(struct mutex *lock);
int __sched mutex_lock_interruptible(struct mutex *lock)
{
might_sleep();
+ MUTEX_CHCEK_INIT(lock);
if (__mutex_trylock_fast(lock))
return 0;
@@ -1015,6 +1027,7 @@ EXPORT_SYMBOL(mutex_lock_interruptible);
int __sched mutex_lock_killable(struct mutex *lock)
{
might_sleep();
+ MUTEX_CHCEK_INIT(lock);
if (__mutex_trylock_fast(lock))
return 0;
--
2.17.1
next reply other threads:[~2025-09-24 2:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-24 2:25 buckzhang1212 [this message]
2025-09-24 3:10 ` Waiman Long
2025-09-24 6:58 ` Peter Zijlstra
[not found] ` <377cba8b.2bd3a.19979cf5b04.Coremail.buckzhang1212@yeah.net>
2025-09-24 14:22 ` Waiman Long
2025-09-24 16:15 ` kernel test robot
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=20250924022500.2577-1-buckzhang1212@yeah.net \
--to=buckzhang1212@yeah.net \
--cc=boqun.feng@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.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®