Re: [PATCH 01/17] locking/mutex: assert static storage for DEFINE_MUTEX()

From: Peter Zijlstra

Date: Tue Sep 15 2026 - 04:52:14 EST


On Mon, Sep 14, 2026 at 11:03:19PM -0400, Yury Norov wrote:
> DEFINE_MUTEX() uses a static lock initializer whose lockdep class key
> requires a persistent object address. Reject automatic local declarations
> with ASSERT_STATIC_STORAGE(), in both the regular and PREEMPT_RT variants.
> Align the VMA and memblock tools stubs with the kernel declaration.
>
> The two automatic mutexes in the WireGuard allowedips selftests already
> call mutex_init(). Replace their redundant static initializers with plain
> struct mutex declarations.
>
> Assisted-by: OpenAI Codex
> Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
> ---
> drivers/net/wireguard/selftest/allowedips.c | 4 ++--
> include/linux/mutex.h | 7 +++++--
> tools/testing/memblock/linux/mutex.h | 8 ++++++--
> tools/testing/vma/include/dup.h | 5 ++++-
> 4 files changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/wireguard/selftest/allowedips.c b/drivers/net/wireguard/selftest/allowedips.c
> index 3e857e6fb627..ecb2908228e7 100644
> --- a/drivers/net/wireguard/selftest/allowedips.c
> +++ b/drivers/net/wireguard/selftest/allowedips.c
> @@ -257,7 +257,7 @@ static __init bool randomized_test(void)
> u8 ip[16], mutate_mask[16], mutated[16];
> struct wg_peer **peers, *peer;
> struct horrible_allowedips h;
> - DEFINE_MUTEX(mutex);
> + struct mutex mutex;
> struct allowedips t;
> bool ret = false;
>
> @@ -499,7 +499,7 @@ bool __init wg_allowedips_selftest(void)
> struct allowedips_node *iter_node;
> bool success = false;
> struct allowedips t;
> - DEFINE_MUTEX(mutex);
> + struct mutex mutex;
> struct in6_addr ip;
> size_t i = 0, count = 0;
> __be64 part;

This should probably be a separate patch.

> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> index 734048c02f4f..2f2d95f3097c 100644
> --- a/include/linux/mutex.h
> +++ b/include/linux/mutex.h
> @@ -11,6 +11,7 @@
> #ifndef __LINUX_MUTEX_H
> #define __LINUX_MUTEX_H
>
> +#include <linux/compiler.h>
> #include <asm/current.h>
> #include <linux/list.h>
> #include <linux/spinlock_types.h>
> @@ -84,7 +85,8 @@ do { \
> __DEP_MAP_MUTEX_INITIALIZER(lockname) }
>
> #define DEFINE_MUTEX(mutexname) \
> - struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
> + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname); \
> + ASSERT_STATIC_STORAGE(mutexname)

My tree no has ASSERT_STATIC_STORAGE. Please repost when that lands?