]> www.infradead.org Git - users/dwmw2/linux.git/commit
locking/ww_mutex: Adjust to lockdep nest_lock requirements
authorThomas Hellström <thomas.hellstrom@linux.intel.com>
Wed, 9 Oct 2024 09:20:31 +0000 (11:20 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Wed, 9 Oct 2024 13:08:25 +0000 (15:08 +0200)
commit823a566221a5639f6c69424897218e5d6431a970
treed8f7ccf97629a6693dc89e4374f5b63448d83626
parentafc256e131bb0e1ecb5e2b1df310b20fa7bd714d
locking/ww_mutex: Adjust to lockdep nest_lock requirements

When using mutex_acquire_nest() with a nest_lock, lockdep refcounts the
number of acquired lockdep_maps of mutexes of the same class, and also
keeps a pointer to the first acquired lockdep_map of a class. That pointer
is then used for various comparison-, printing- and checking purposes,
but there is no mechanism to actively ensure that lockdep_map stays in
memory. Instead, a warning is printed if the lockdep_map is freed and
there are still held locks of the same lock class, even if the lockdep_map
itself has been released.

In the context of WW/WD transactions that means that if a user unlocks
and frees a ww_mutex from within an ongoing ww transaction, and that
mutex happens to be the first ww_mutex grabbed in the transaction,
such a warning is printed and there might be a risk of a UAF.

Note that this is only problem when lockdep is enabled and affects only
dereferences of struct lockdep_map.

Adjust to this by adding a fake lockdep_map to the acquired context and
make sure it is the first acquired lockdep map of the associated
ww_mutex class. Then hold it for the duration of the WW/WD transaction.

This has the side effect that trying to lock a ww mutex *without* a
ww_acquire_context but where a such context has been acquire, we'd see
a lockdep splat. The test-ww_mutex.c selftest attempts to do that, so
modify that particular test to not acquire a ww_acquire_context if it
is not going to be used.

Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20241009092031.6356-1-thomas.hellstrom@linux.intel.com
include/linux/ww_mutex.h
kernel/locking/test-ww_mutex.c