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

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

  reply	other threads:[~2025-07-11 22:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-09 19:39 Waiman Long
2025-07-11 22:20 ` Boqun Feng [this message]
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=aHGONjuRiA3KfH1q@tardis-2.local \
    --to=boqun.feng@gmail.com \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --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®