[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aHGONjuRiA3KfH1q@tardis-2.local>
Date: Fri, 11 Jul 2025 15:20:38 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Waiman Long <longman@...hat.com>
Cc: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
Will Deacon <will@...nel.org>, linux-kernel@...r.kernel.org,
Linus Torvalds <torvalds@...ux-foundation.org>,
Jann Horn <jannh@...gle.com>
Subject: Re: [PATCH] locking/mutex: Add debug code to help catching violation
of mutex lifetime rule
On Wed, Jul 09, 2025 at 03:39:10PM -0400, Waiman Long wrote:
> 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>
Meta question: are we able to construct a case that shows this can help
detect the issue?
Regards,
Boqun
> ---
> 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