Re: [tip: locking/urgent] locking/ww_mutex: Treat ww_mutex_lock() like a trylock

From: Waiman Long
Date: Wed Mar 17 2021 - 14:33:23 EST


On 3/17/21 1:45 PM, Peter Zijlstra wrote:
On Wed, Mar 17, 2021 at 06:40:27PM +0100, Peter Zijlstra wrote:
On Wed, Mar 17, 2021 at 05:48:48PM +0100, Peter Zijlstra wrote:

I think you'll find that if you use ww_mutex_init() it'll all work. Let
me go and zap this patch, and then I'll try and figure out why
DEFINE_WW_MUTEX() is buggered.
Moo, I can't get the compiler to do as I want :/

The below is so close but doesn't actually compile.. Maybe we should
just give up on DEFINE_WW_MUTEX and simply remove it.

---
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 0cd631a19727..85f50538f26a 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -129,12 +129,15 @@ do { \
# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
#endif
-#define __MUTEX_INITIALIZER(lockname) \
+#define ___MUTEX_INITIALIZER(lockname, depmap) \
{ .owner = ATOMIC_LONG_INIT(0) \
, .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
__DEBUG_MUTEX_INITIALIZER(lockname) \
- __DEP_MAP_MUTEX_INITIALIZER(lockname) }
+ depmap }
+
+#define __MUTEX_INITIALIZER(lockname) \
+ ___MUTEX_INITIALIZER(lockname, __DEP_MAP_MUTEX_INITIALIZER(lockname))
#define DEFINE_MUTEX(mutexname) \
struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h
index 6ecf2a0220db..c62a030652b4 100644
--- a/include/linux/ww_mutex.h
+++ b/include/linux/ww_mutex.h
@@ -50,9 +50,17 @@ struct ww_acquire_ctx {
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
- , .ww_class = class
+ , .ww_class = &(class)
+
+# define __DEP_MAP_WW_MUTEX_INITIALIZER(lockname, class) \
+ , .dep_map = { \
+ .key = &(class).mutex_key, \
+ .name = (class).mutex_name, \
,name = #class "_mutex", \

and it 'works', but shees!

The name string itself may be duplicated for multiple instances of DEFINE_WW_MUTEX(). Do you want to keep DEFINE_WW_MUTEX() or just use ww_mutex_init() for all?

I notice that the problem with DEFINE_WW_MUTEX is that the ww_mutex themselves has null key instead of the same key from the ww_class as with ww_mutex_init().

Cheers,
Longman