* [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex
@ 2025-09-27 8:00 buckzhang1212
2025-09-28 15:55 ` Waiman Long
0 siblings, 1 reply; 3+ messages in thread
From: buckzhang1212 @ 2025-09-27 8:00 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng,
Waiman Long, linux-kernel
Cc: buckzhang1212
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);
ww = container_of(lock, struct ww_mutex, base);
diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
index 2e8080a9b..4aaaad10d 100644
--- a/kernel/locking/mutex.h
+++ b/kernel/locking/mutex.h
@@ -48,6 +48,7 @@ static inline struct task_struct *__mutex_owner(struct mutex *lock)
}
#ifdef CONFIG_DEBUG_MUTEXES
+
extern void debug_mutex_lock_common(struct mutex *lock,
struct mutex_waiter *waiter);
extern void debug_mutex_wake_waiter(struct mutex *lock,
@@ -61,6 +62,15 @@ extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *w
extern void debug_mutex_unlock(struct mutex *lock);
extern void debug_mutex_init(struct mutex *lock, const char *name,
struct lock_class_key *key);
+
+/*
+ * list_invalid - tests whether mutex-> waitlist is invalid
+ * @head: the list to test.
+ */
+static inline int mutex_waitlist_invalid(const struct list_head *head)
+{
+ return (unsigned long) READ_ONCE(head->next) < PAGE_OFFSET;
+}
#else /* CONFIG_DEBUG_MUTEXES */
# define debug_mutex_lock_common(lock, waiter) do { } while (0)
# define debug_mutex_wake_waiter(lock, waiter) do { } while (0)
@@ -69,5 +79,6 @@ extern void debug_mutex_init(struct mutex *lock, const char *name,
# define debug_mutex_remove_waiter(lock, waiter, ti) do { } while (0)
# define debug_mutex_unlock(lock) do { } while (0)
# define debug_mutex_init(lock, name, key) do { } while (0)
+# define mutex_waitlist_invalid() do { } while (0)
#endif /* !CONFIG_DEBUG_MUTEXES */
#endif /* CONFIG_PREEMPT_RT */
--
2.17.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex
2025-09-27 8:00 [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex buckzhang1212
@ 2025-09-28 15:55 ` Waiman Long
[not found] ` <73ba8425.3229c.19991241c52.Coremail.buckzhang1212@yeah.net>
0 siblings, 1 reply; 3+ messages in thread
From: Waiman Long @ 2025-09-28 15:55 UTC (permalink / raw)
To: buckzhang1212, Peter Zijlstra, Ingo Molnar, Will Deacon,
Boqun Feng, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex
[not found] ` <73ba8425.3229c.19991241c52.Coremail.buckzhang1212@yeah.net>
@ 2025-09-28 16:37 ` Waiman Long
0 siblings, 0 replies; 3+ messages in thread
From: Waiman Long @ 2025-09-28 16:37 UTC (permalink / raw)
To: buckzhang1212, llong
Cc: peterz, mingo, will, boqun.feng, linux-kernel@vger.kernel…
On 9/28/25 12:24 PM, buckzhang1212 wrote:
> Hey Long
> you are right,lock->magic check can help me to get what I want .
> It's a bit late for me, and I want to get the warning when I first
> use the invalid mutex.
> Can I move the lock->magic check to the mutex_lock?
All the mutex_lock*() functions will eventually call
__mutex_lock_common() or __mutex_trylock_fast() which also has the check
. There is no need to duplicate the check in each of them.
Cheers,
Longman
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-09-28 16:37 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-27 8:00 [PATCH] locking/mutex: add MUTEX_WARN_ON to warn invalid mutex buckzhang1212
2025-09-28 15:55 ` Waiman Long
[not found] ` <73ba8425.3229c.19991241c52.Coremail.buckzhang1212@yeah.net>
2025-09-28 16:37 ` Waiman Long
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®