2019-07-29 10:56:10

by Mukesh Ojha

[permalink] [raw]
Subject: [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().

Signed-off-by: Mukesh Ojha <[email protected]>
---
include/linux/mutex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 79b28be..c3833ba 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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);
}

/*
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project


2019-07-29 13:24:20

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value

On Mon, Jul 29, 2019 at 04:22:58PM +0530, Mukesh Ojha wrote:
> 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().
>
> Signed-off-by: Mukesh Ojha <[email protected]>
> ---
> include/linux/mutex.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> index 79b28be..c3833ba 100644
> --- a/include/linux/mutex.h
> +++ b/include/linux/mutex.h
> @@ -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.

2019-07-30 11:03:13

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value


On 7/29/2019 4:37 PM, Peter Zijlstra wrote:
> On Mon, Jul 29, 2019 at 04:22:58PM +0530, Mukesh Ojha wrote:
>> 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().
>>
>> Signed-off-by: Mukesh Ojha <[email protected]>
>> ---
>> include/linux/mutex.h | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
>> index 79b28be..c3833ba 100644
>> --- a/include/linux/mutex.h
>> +++ b/include/linux/mutex.h
>> @@ -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


2019-07-30 11:04:41

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value

On Tue, Jul 30, 2019 at 01:23:13PM +0530, Mukesh Ojha wrote:
>
> On 7/29/2019 4:37 PM, Peter Zijlstra wrote:
> > On Mon, Jul 29, 2019 at 04:22:58PM +0530, Mukesh Ojha wrote:
> > > 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().
> > >
> > > Signed-off-by: Mukesh Ojha <[email protected]>
> > > ---
> > > include/linux/mutex.h | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> > > index 79b28be..c3833ba 100644
> > > --- a/include/linux/mutex.h
> > > +++ b/include/linux/mutex.h
> > > @@ -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 ?

Yes, then you can make __mutex_owner() static.

Thanks!

2019-07-30 16:43:38

by Mukesh Ojha

[permalink] [raw]
Subject: [PATCH 1/2 v2] locking/mutex: Make __mutex_owner static to mutex.c

__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.

Signed-off-by: Mukesh Ojha <[email protected]>
---
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(-)

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index dcd03fe..841b47d 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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 */
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 5e06973..f73250a 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -65,6 +65,50 @@

#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) & ~0x07);
+}
+
+/**
+ * mutex_is_locked - is the mutex locked
+ * @lock: the mutex to be queried
+ *
+ * Returns true if the mutex is locked, false if unlocked.
+ */
+inline bool mutex_is_locked(struct mutex *lock)
+{
+ return __mutex_owner(lock) != NULL;
+}
+EXPORT_SYMBOL(mutex_is_locked);
+
+/**
+ * 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.
+ */
+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);
+}
+EXPORT_SYMBOL(mutex_trylock_recursive);
+
static inline struct task_struct *__owner_task(unsigned long owner)
{
return (struct task_struct *)(owner & ~MUTEX_FLAGS);
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

2019-07-30 16:43:42

by Mukesh Ojha

[permalink] [raw]
Subject: [PATCH 2/2 v2] locking/mutex: Use mutex flags macro instead of hard code value

Use the mutex flag macro instead of hard code value inside
__mutex_owner().

Signed-off-by: Mukesh Ojha <[email protected]>
---
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(-)

diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index f73250a..1c8f86a 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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);
}

/**
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

2019-07-30 16:47:46

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value


On 7/30/2019 9:32 PM, Peter Zijlstra wrote:
> On Tue, Jul 30, 2019 at 06:10:49PM +0530, Mukesh Ojha wrote:
>
>> To make it static , i have to export mutex_is_locked() after moving it
>> inside mutex.c, so that other module can use it.
> Yep, see below -- completely untested.
>
>> 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 .
> That was the idea; recursive locking is evil, but we have these two
> legacy sites.
>
> ---
>
> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> index dcd03fee6e01..eb8c62aba263 100644
> --- a/include/linux/mutex.h
> +++ b/include/linux/mutex.h
> @@ -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 */
> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
> index 5e069734363c..2f73935a6053 100644
> --- a/kernel/locking/mutex.c
> +++ b/kernel/locking/mutex.c
> @@ -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
> +};

i already did in v2  except this above waiter struct, will do it in v3.

Cheers,

Mukesh

> +
> 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.
> */

2019-07-30 17:12:09

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value


On 7/30/2019 1:33 PM, Peter Zijlstra wrote:
> On Tue, Jul 30, 2019 at 01:23:13PM +0530, Mukesh Ojha wrote:
>> On 7/29/2019 4:37 PM, Peter Zijlstra wrote:
>>> On Mon, Jul 29, 2019 at 04:22:58PM +0530, Mukesh Ojha wrote:
>>>> 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().
>>>>
>>>> Signed-off-by: Mukesh Ojha <[email protected]>
>>>> ---
>>>> include/linux/mutex.h | 2 +-
>>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
>>>> index 79b28be..c3833ba 100644
>>>> --- a/include/linux/mutex.h
>>>> +++ b/include/linux/mutex.h
>>>> @@ -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 ?
> 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


>
> Thanks!

2019-07-30 18:05:17

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/2] locking/mutex: Use mutex flags macro instead of hard code value

On Tue, Jul 30, 2019 at 06:10:49PM +0530, Mukesh Ojha wrote:

> To make it static , i have to export mutex_is_locked() after moving it
> inside mutex.c, so that other module can use it.

Yep, see below -- completely untested.

> 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 .

That was the idea; recursive locking is evil, but we have these two
legacy sites.

---

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index dcd03fee6e01..eb8c62aba263 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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 */
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 5e069734363c..2f73935a6053 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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.
*/