From: Waiman Long <llong@redhat.com>
To: buckzhang1212@yeah.net, Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Boqun Feng <boqun.feng@gmail.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex
Date: Sun, 28 Sep 2025 11:55:22 -0400 [thread overview]
Message-ID: <f6b88959-aed3-423f-a12d-46bb679820e1@redhat.com> (raw)
In-Reply-To: <20250927080018.13909-1-buckzhang1212@yeah.net>
On 9/27/25 4:00 AM, buckzhang1212@yeah.net wrote:
> From: "buck.zhang" <buckzhang1212@yeah.net>
>
> Here is a kernel exception about mutex and I can recreate it stably.
> First we define a struct contains a mutex.
> Then allocate this struct by kmalloc without calling mutex_init.
> Finally when multiple tasks call mutex_lock together,kernel will panic.
> This lock is used normally when only one task is using at a time.
> the exception reason is that lock->wait_list is an invalid kernel list.
> I want to add more warnings when enable CONFIG_DEBUG_MUTEXES
> 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 | 2 ++
> kernel/locking/mutex.h | 11 +++++++++++
> 2 files changed, 13 insertions(+)
>
> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
> index de7d6702c..318d98f2f 100644
> --- a/kernel/locking/mutex.c
> +++ b/kernel/locking/mutex.c
> @@ -574,6 +574,8 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
>
> might_sleep();
>
> + /* In case the mutex is uninitiated, add more warning */
> + MUTEX_WARN_ON(mutex_waitlist_invalid(&lock->wait_list));
> MUTEX_WARN_ON(lock->magic != lock);
By enabling CONFIG_DEBUG_MUTEXES, the lock->magic check should have
uncovered the case of mutex_lock() call without mutex initialization.
The extra wait_list check is likely not necessary.
Cheers,
Longman
next prev parent reply other threads:[~2025-09-28 15:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-27 8:00 buckzhang1212
2025-09-28 15:55 ` Waiman Long [this message]
[not found] ` <73ba8425.3229c.19991241c52.Coremail.buckzhang1212@yeah.net>
2025-09-28 16:37 ` Waiman Long
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=f6b88959-aed3-423f-a12d-46bb679820e1@redhat.com \
--to=llong@redhat.com \
--cc=boqun.feng@gmail.com \
--cc=buckzhang1212@yeah.net \
--cc=linux-kernel@vger.kernel.org \
--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®