locking/mutex: Move mutex flag macros to linux/mutex.h
PATCH 1/2 locking/mutex: Move mutex flag macros to linux/mutex.h
There exist a place where we use hard code value for mutex flag(e.g in mutex.h __mutex_owner()). Let's move the mutex flag macros to header linux/mutex.h, so that it could be reused at other places as well.
--- include/linux/mutex.h | 15 +++++++++++++++ kernel/locking/mutex.c | 15 --------------- 2 files changed, 15 insertions(+), 15 deletions(-)
include/linux/mutex.h +15 -0
@@ -20,6 +20,21 @@#include <linux/osq_lock.h> #include <linux/debug_locks.h> +/* +* @owner: contains: 'struct task_struct *' to the current lock owner, +* NULL means not owned. Since task_struct pointers are aligned at +* at least L1_CACHE_BYTES, we have low bits to store extra state. +* +* Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. +* Bit1 indicates unlock needs to hand the lock to the top-waiter +* Bit2 indicates handoff has been done and we're waiting for pickup. +*/ +#define MUTEX_FLAG_WAITERS 0x01 +#define MUTEX_FLAG_HANDOFF 0x02 +#define MUTEX_FLAG_PICKUP 0x04 + +#define MUTEX_FLAGS 0x07 +struct ww_acquire_ctx ;/*
kernel/locking/mutex.c +0 -15
@@ -50,21 +50,6 @@ }EXPORT_SYMBOL (__mutex_init); -/* -* @owner: contains: 'struct task_struct *' to the current lock owner, -* NULL means not owned. Since task_struct pointers are aligned at -* at least L1_CACHE_BYTES, we have low bits to store extra state. -* -* Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. -* Bit1 indicates unlock needs to hand the lock to the top-waiter -* Bit2 indicates handoff has been done and we're waiting for pickup. -*/ -#define MUTEX_FLAG_WAITERS 0x01 -#define MUTEX_FLAG_HANDOFF 0x02 -#define MUTEX_FLAG_PICKUP 0x04 - -#define MUTEX_FLAGS 0x07 -static inline struct task_struct *__owner_task (unsigned long owner) {return (struct task_struct *)(owner & ~MUTEX_FLAGS );
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 2/2 locking/mutex: Use mutex flags macro instead of hard code value
Let's use the mutex flag macro(which got moved from mutex.c to linux/mutex.h in the last patch) instead of hard code value which was used in __mutex_owner().
--- include/linux/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mutex.h +1 -1
@@ -87,7 +87,7 @@ struct mutex {*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }/*
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 14 lines · Mukesh Ojha
Let's use the mutex flag macro(which got moved from
mutex.ctolinux/mutex.hin the last patch) instead of hard code value which was used in__mutex_owner().Signed-off-by Mukesh Ojha <[email protected]>--- include/linux/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/mutex.h +0
-0
include/linux/mutex.h struct mutex { · line 91
Quoted 6 lines
include/linux/mutex.h +1
-1@@ -87,7 +87,7 @@ struct mutex {*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }
I would _much_ rather move __mutex_owner() out of line, you're exposing far too much stuff.
Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Mukesh Ojha
Let's use the mutex flag macro(which got moved from
mutex.ctolinux/mutex.hin the last patch) instead of hard code value which was used in__mutex_owner().Signed-off-by Mukesh Ojha <[email protected]>--- include/linux/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
include/linux/mutex.h struct mutex { · line 91
Quoted 11 lines
include/linux/mutex.h +1
-1@@ -87,7 +87,7 @@ struct mutex {*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }I would _much_ rather move
__mutex_owner()out of line, you're exposing far too much stuff.
if i understand you correctly, you want me to move __mutex_owner() to mutex.c __mutex_owner() is used in mutex_is_locked() and mutex_trylock_recursive inside linux/mutex.h.
Shall i move them as well ?
Thanks,
Mukesh
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 26 lines · Mukesh Ojha
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Mukesh Ojha
Let's use the mutex flag macro(which got moved from
mutex.ctolinux/mutex.hin the last patch) instead of hard code value which was used in__mutex_owner().Signed-off-by Mukesh Ojha <[email protected]>--- include/linux/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 13 lines
include/linux/mutex.h struct mutex { · line 91
Quoted 11 lines
include/linux/mutex.h +1
-1@@ -87,7 +87,7 @@ struct mutex {*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }I would _much_ rather move
__mutex_owner()out of line, you're exposing far too much stuff.
Quoted 6 lines
if i understand you correctly, you want me to move
__mutex_owner()tomutex.c__mutex_owner()is used inmutex_is_locked()andmutex_trylock_recursiveinsidelinux/mutex.h.Shall i move them as well ?
Yes, then you can make __mutex_owner() static.
Thanks!
Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Mukesh Ojha
Quoted 23 lines · Peter Zijlstra
Quoted 14 lines · Mukesh Ojha
Let's use the mutex flag macro(which got moved from
mutex.ctolinux/mutex.hin the last patch) instead of hard code value which was used in__mutex_owner().Signed-off-by Mukesh Ojha <[email protected]>--- include/linux/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/mutex.h +0
-0include/linux/mutex.h struct mutex { · line 91
Quoted 6 lines
include/linux/mutex.h +1
-1@@ -87,7 +87,7 @@ struct mutex {*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }I would _much_ rather move
__mutex_owner()out of line, you're exposing far too much stuff.
Quoted 7 lines
Quoted 6 lines
if i understand you correctly, you want me to move
__mutex_owner()tomutex.c__mutex_owner()is used inmutex_is_locked()andmutex_trylock_recursiveinsidelinux/mutex.h.Shall i move them as well ?
Yes, then you can make
__mutex_owner()static.
To make it static , i have to export mutex_is_locked() after moving it inside mutex.c, so that other module can use it.
Also are we thinking of removing
static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum mutex_trylock_recursive(struct mutex *lock)
inside linux/mutex.h in future ?
As i see it is used at one or two places and there is a check inside checkpatch guarding its further use .
Thanks,
Mukesh
Quoted 2 lines
Thanks!
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 2 lines · Mukesh Ojha
To make it static , i have to export
mutex_is_locked()after moving it insidemutex.c, so that other module can use it.
Yep, see below -- completely untested.
Quoted 9 lines
Also are we thinking of removing
static inline /*__deprecated*/__must_checkenummutex_trylock_recursive_enummutex_trylock_recursive(struct mutex *lock)inside
linux/mutex.hin future ?As i see it is used at one or two places and there is a check inside checkpatch guarding its further use .
That was the idea; recursive locking is evil, but we have these two legacy sites.
---
include/linux/mutex.h +3 -35
@@ -65,29 +65,6 @@ struct mutex {#endif }; -/* -* Internal helper function; C doesn't allow us to hide it :/ -* -* DO NOT USE (outside of mutex code). -*/ -static inline struct task_struct *__mutex_owner (struct mutex *lock) -{ -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); -} - -/* -* This is the control structure for tasks blocked on mutex, -* which resides on the blocked task's kernel stack: -*/ -struct mutex_waiter { -struct list_head list; -struct task_struct *task; -struct ww_acquire_ctx *ww_ctx; -#ifdef CONFIG_DEBUG_MUTEXES -void *magic; -#endif -}; -#ifdef CONFIG_DEBUG_MUTEXES #define __DEBUG_MUTEX_INITIALIZER (lockname) \
@@ -144,10 +121,7 @@ extern void __mutex_init(struct mutex *lock, const char *name,* * Returns true if the mutex is locked, false if unlocked. */ -static inline bool mutex_is_locked (struct mutex *lock) -{ -return __mutex_owner (lock) !=NULL ; -} +extern bool mutex_is_locked (struct mutex *lock);/* * See kernel/locking/mutex.c for detailed documentation of these APIs.
@@ -220,13 +194,7 @@ enum mutex_trylock_recursive_enum {* - MUTEX_TRYLOCK_SUCCESS - lock acquired, * - MUTEX_TRYLOCK_RECURSIVE - we already owned the lock. */ -static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum -mutex_trylock_recursive (struct mutex *lock) -{ -if (unlikely (__mutex_owner (lock) == current)) -return MUTEX_TRYLOCK_RECURSIVE ; - -return mutex_trylock (lock); -} +extern /* __deprecated */ __must_check enum mutex_trylock_recursive_enum +mutex_trylock_recursive (struct mutex *lock);#endif /* __LINUX_MUTEX_H */
kernel/locking/mutex.c +39 -0
@@ -36,6 +36,19 @@# include "mutex.h" #endif +/* +* This is the control structure for tasks blocked on mutex, +* which resides on the blocked task's kernel stack: +*/ +struct mutex_waiter { +struct list_head list; +struct task_struct *task; +struct ww_acquire_ctx *ww_ctx; +#ifdef CONFIG_DEBUG_MUTEXES +void *magic; +#endif +}; +void __mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key) {
@@ -65,6 +78,16 @@ EXPORT_SYMBOL(__mutex_init);#define MUTEX_FLAGS 0x07 +/* +* Internal helper function; C doesn't allow us to hide it :/ +* +* DO NOT USE (outside of mutex code). +*/ +static inline struct task_struct *__mutex_owner (struct mutex *lock) +{ +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); +} +static inline struct task_struct *__owner_task (unsigned long owner) {return (struct task_struct *)(owner & ~MUTEX_FLAGS );
@@ -75,6 +98,22 @@ static inline unsigned long __owner_flags(unsigned long owner)return owner &MUTEX_FLAGS ; } +bool mutex_is_locked (struct mutex *lock) +{ +return __mutex_owner (lock) !=NULL ; +} +EXPORT_SYMBOL (mutex_is_locked); + +__must_check enum mutex_trylock_recursive_enum +mutex_trylock_recursive (struct mutex *lock) +{ +if (unlikely (__mutex_owner (lock) == current)) +return MUTEX_TRYLOCK_RECURSIVE ; + +return mutex_trylock (lock); +} +EXPORT_SYMBOL (mutex_trylock_recursive); +/* * Trylock variant that retuns the owning task on failure. */
Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value
Quoted 92 lines · Peter Zijlstra
Quoted 2 lines · Mukesh Ojha
To make it static , i have to export
mutex_is_locked()after moving it insidemutex.c, so that other module can use it.Yep, see below -- completely untested.
Quoted 9 lines
Also are we thinking of removing
static inline /*__deprecated*/__must_checkenummutex_trylock_recursive_enummutex_trylock_recursive(struct mutex *lock)inside
linux/mutex.hin future ?As i see it is used at one or two places and there is a check inside checkpatch guarding its further use .
That was the idea; recursive locking is evil, but we have these two legacy sites.
---
include/linux/mutex.h +3
-35@@ -65,29 +65,6 @@ struct mutex {#endif }; -/* -* Internal helper function; C doesn't allow us to hide it :/ -* -* DO NOT USE (outside of mutex code). -*/ -static inline struct task_struct *__mutex_owner (struct mutex *lock) -{ -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); -} - -/* -* This is the control structure for tasks blocked on mutex, -* which resides on the blocked task's kernel stack: -*/ -struct mutex_waiter { -struct list_head list; -struct task_struct *task; -struct ww_acquire_ctx *ww_ctx; -#ifdef CONFIG_DEBUG_MUTEXES -void *magic; -#endif -}; -#ifdef CONFIG_DEBUG_MUTEXES #define __DEBUG_MUTEX_INITIALIZER (lockname) \@@ -144,10 +121,7 @@ extern void __mutex_init(struct mutex *lock, const char *name,* * Returns true if the mutex is locked, false if unlocked. */ -static inline bool mutex_is_locked (struct mutex *lock) -{ -return __mutex_owner (lock) !=NULL ; -} +extern bool mutex_is_locked (struct mutex *lock);/* * See kernel/locking/mutex.c for detailed documentation of these APIs. @@ -220,13 +194,7 @@ enum mutex_trylock_recursive_enum {* - MUTEX_TRYLOCK_SUCCESS - lock acquired, * - MUTEX_TRYLOCK_RECURSIVE - we already owned the lock. */ -static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum -mutex_trylock_recursive (struct mutex *lock) -{ -if (unlikely (__mutex_owner (lock) == current)) -return MUTEX_TRYLOCK_RECURSIVE ; - -return mutex_trylock (lock); -} +extern /* __deprecated */ __must_check enum mutex_trylock_recursive_enum +mutex_trylock_recursive (struct mutex *lock);#endif /* __LINUX_MUTEX_H */ kernel/locking/mutex.c +2
-0@@ -36,6 +36,19 @@# include "mutex.h" #endif +/* +* This is the control structure for tasks blocked on mutex,
kernel/locking/mutex.c line 50
Quoted 10 lines
kernel/locking/mutex.c +10
-0+* which resides on the blocked task's kernel stack: +*/ +struct mutex_waiter { +struct list_head list; +struct task_struct *task; +struct ww_acquire_ctx *ww_ctx; +#ifdef CONFIG_DEBUG_MUTEXES +void *magic; +#endif +};
i already did in v2 except this above waiter struct, will do it in v3.
Cheers,
Mukesh
Quoted 44 lines
kernel/locking/mutex.c +27
-0+void __mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key) {@@ -65,6 +78,16 @@ EXPORT_SYMBOL(__mutex_init);#define MUTEX_FLAGS 0x07 +/* +* Internal helper function; C doesn't allow us to hide it :/ +* +* DO NOT USE (outside of mutex code). +*/ +static inline struct task_struct *__mutex_owner (struct mutex *lock) +{ +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); +} +static inline struct task_struct *__owner_task (unsigned long owner) {return (struct task_struct *)(owner & ~MUTEX_FLAGS );@@ -75,6 +98,22 @@ static inline unsigned long __owner_flags(unsigned long owner)return owner &MUTEX_FLAGS ; } +bool mutex_is_locked (struct mutex *lock) +{ +return __mutex_owner (lock) !=NULL ; +} +EXPORT_SYMBOL (mutex_is_locked); + +__must_check enum mutex_trylock_recursive_enum +mutex_trylock_recursive (struct mutex *lock) +{ +if (unlikely (__mutex_owner (lock) == current)) +return MUTEX_TRYLOCK_RECURSIVE ; + +return mutex_trylock (lock); +} +EXPORT_SYMBOL (mutex_trylock_recursive); +/* * Trylock variant that retuns the owning task on failure. */
PATCH v2 1/2 locking/mutex: Make __mutex_owner static to mutex.c
In reply to Peter Zijlstra__mutex_owner() should only be used by the mutex api's. So, put this restiction let's move the __mutex_owner() function definition from linux/mutex.h to mutex.c file.
There exist functions that uses __mutex_owner() like mutex_is_locked() and mutex_trylock_recursive(), So to keep the thing intact move them as well and export them.
---
Changes in v2:
- On Peterz suggestion, moved __mutex_owner() to mutex.c to make it static to mutex.c.
- Exported mutex_is_owner() and mutex_trylock_recursive() to keep the existing thing intact as there are loadable modules which uses these functions.
include/linux/mutex.h | 44 +++----------------------------------------- kernel/locking/mutex.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 41 deletions(-)
include/linux/mutex.h +3 -41
@@ -66,16 +66,6 @@ struct mutex { };/* -* Internal helper function; C doesn't allow us to hide it :/ -* -* DO NOT USE (outside of mutex code). -*/ -static inline struct task_struct *__mutex_owner (struct mutex *lock) -{ -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); -} - -/* * This is the control structure for tasks blocked on mutex, * which resides on the blocked task's kernel stack: */
@@ -138,17 +128,10 @@ static inline void mutex_destroy(struct mutex *lock) {}extern void __mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key); -/** -* mutex_is_locked - is the mutex locked -* @lock: the mutex to be queried -* -* Returns true if the mutex is locked, false if unlocked. -*/ -static inline bool mutex_is_locked (struct mutex *lock) -{ -return __mutex_owner (lock) !=NULL ; -} +extern inline bool mutex_is_locked (struct mutex *lock); +extern inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum +mutex_trylock_recursive (struct mutex *lock);/* * See kernel/locking/mutex.c for detailed documentation of these APIs. * Also see Documentation/locking/mutex-design.rst.
@@ -208,25 +191,4 @@ enum mutex_trylock_recursive_enum { MUTEX_TRYLOCK_RECURSIVE, }; -/** -* mutex_trylock_recursive - trylock variant that allows recursive locking -* @lock: mutex to be locked -* -* This function should not be used, _ever_. It is purely for hysterical GEM -* raisins, and once those are gone this will be removed. -* -* Returns: -* - MUTEX_TRYLOCK_FAILED - trylock failed, -* - MUTEX_TRYLOCK_SUCCESS - lock acquired, -* - MUTEX_TRYLOCK_RECURSIVE - we already owned the lock. -*/ -static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum -mutex_trylock_recursive (struct mutex *lock) -{ -if (unlikely (__mutex_owner (lock) == current)) -return MUTEX_TRYLOCK_RECURSIVE ; - -return mutex_trylock (lock); -} -#endif /* __LINUX_MUTEX_H */
kernel/locking/mutex.c +44 -0
50 lines not on this page · raw message
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH v2 2/2 locking/mutex: Use mutex flags macro instead of hard code value
Use the mutex flag macro instead of hard code value inside __mutex_owner().
---
Changes in v2:
- Framed the commit according the changes done in 1/2 of the patchset.
kernel/locking/mutex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/mutex.c +1 -1
@@ -72,7 +72,7 @@*/ static inline struct task_struct *__mutex_owner (struct mutex *lock) { -return (struct task_struct *)(atomic_long_read (&lock->owner) & ~0x07 ); +return (struct task_struct *)(atomic_long_read (&lock->owner) & ~MUTEX_FLAGS ); }/**
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project