From: Boqun Feng <boqun.feng@gmail.com>
To: Waiman Long <llong@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
linux-kernel@vger.kernel.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 19:24:36 -0700 [thread overview]
Message-ID: <aHHHZJ5sKGscTCqo@tardis.local> (raw)
In-Reply-To: <00fdac3e-092d-46f0-bbe7-6067c0f22eeb@redhat.com>
On Fri, Jul 11, 2025 at 09:48:13PM -0400, Waiman Long wrote:
> On 7/11/25 8:42 PM, Waiman Long wrote:
> >
> > On 7/11/25 7:28 PM, Boqun Feng wrote:
> > > On Fri, Jul 11, 2025 at 03:30:05PM -0700, Linus Torvalds wrote:
> > > > On Fri, 11 Jul 2025 at 15:20, Boqun Feng <boqun.feng@gmail.com> wrote:
> > > > > Meta question: are we able to construct a case that shows
> > > > > this can help
> > > > > detect the issue?
> > > > Well, the thing that triggered this was hopefully fixed by
> > > > 8c2e52ebbe88 ("eventpoll: don't decrement ep refcount while still
> > > > holding the ep mutex"), but I think Jann figured that one out by code
> > > > inspection.
> > > >
> > > > I doubt it can be triggered in real life without something like
> > > > Waiman's patch, but *with* Waiman's patch, and commit 8c2e52ebbe88
> > > > reverted (and obviously with CONFIG_KASAN and CONFIG_DEBUG_MUTEXES
> > > > enabled), doing lots of concurrent epoll closes would hopefully then
> > > > trigger the warning.
> > > >
> > > > Of course, to then find *other* potential bugs would be the whole
> > > > point, and some of these kinds of bugs are definitely of the kind
> > > > where the race condition doesn't actually trigger in any real load,
> > > > because it's unlikely that real loads end up doing that kind of
> > > > "release all these objects concurrently".
> > > >
> > > > But it might be interesting to try that "can you even recreate the bug
> > > > fixed by 8c2e52ebbe88" with this. Because if that one *known* bug
> > > > can't be found by this, then it's obviously unlikely to help find
> > > > others.
> > > >
> > > Yeah, I guess I asked the question because there is no clear link from
> > > the bug scenario to an extra context switch, that is, even if the
> > > context switch didn't happen, the bug would trigger if
> > > __mutex_unlock_slowpath() took too long after giving the ownership to
> > > someone else. So my instinct was: would cond_resched() be slow enough
> > > ;-)
> > >
> > > But I agree it's a trivel thing to do, and I think another thing we can
> > > do is adding a kasan_check_byte(lock) at the end of
> > > __mutex_unlock_slowpath(), because conceptually the mutex should be
> > > valid throughout the whole __mutex_unlock_slowpath() function, i.e.
> > >
> > > void __mutex_unlock_slowpath(...)
> > > {
> > > ...
> > > raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags,
> > > &wake_q);
> > > // <- conceptually "lock" should still be valid here.
> > > // so if anyone free the memory of the mutex, it's going
> > > // to be a problem.
> > > kasan_check_byte(lock);
> > > }
> > >
> > > I think this may also give us a good chance of finding more bugs, one of
> > > the reasons is that raw_spin_unlock_irqrestore_wake() has a
> > > preempt_enable() at last, which may trigger a context switch.
> > >
> > > Regards,
> > > Boqun
> >
> > I think this is a good idea. We should extend that to add the check in
> > rwsem as well. Will a post a patch to do that.
>
> Digging into it some more, I think adding kasan_check_byte() may not be
> necessary. If KASAN is enabled, it will instrument the locking code
> including __mutex_unlock_slowpath(). I checked the generated assembly code,
> it has 2 __kasan_check_read() and 4 __kasan_check_write() calls. Adding an
The point is we want to check the memory at the end of
__mutex_unlock_slowpath(), so it's an extra checking.
Also since kasan will instrument all memory accesses, what you saw may
not be the instrument on "lock" but something else, for example,
wake_q_init() in raw_spin_unlock_irqrestore_wake().
Actually, I have 3 extension to the idea:
First it occurs to me that we could just put the kasan_check_byte() at
the outermost thing, for example, mutex_unlock().
Second I wonder whether kasan has a way to tag a pointer parameter of a
function, for example for mutex_unlock():
void mutex_unlock(struct mutex * __ref lock)
{
...
}
a kasan_check_byte(lock) will auto generate whenever the function
returns.
I actually tried to use __cleanup to implement __ref, like
#define __ref __cleanup(kasan_check_byte)
but seems the "cleanup" attritube doesn't work on function parameters ;(
Third, I went to implement a always_alive():
#define always_alive(ptr) \
typeof(ptr) __UNIQUE_ID(always_alive_guard) __cleanup(kasan_check_byte) = ptr;
and you can use in mutex_unlock():
void mutex_unlock(struct mutex *lock)
{
always_alive(lock);
...
}
This also guarantee we emit a kasan_check_byte() at the very end.
Regards,
Boqun
> extra kasan_check_byte() can be redundant.
>
> Cheers,
> Longman
>
next prev parent reply other threads:[~2025-07-12 2:24 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-09 19:39 [PATCH] locking/mutex: Add debug code to help catching violation of mutex lifetime rule Waiman Long
2025-07-11 22:20 ` Boqun Feng
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 [this message]
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=aHHHZJ5sKGscTCqo@tardis.local \
--to=boqun.feng@gmail.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=llong@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.