* [PATCH v2] locking/mutex: Set and clear owner using WRITE_ONCE()
@ 2016-05-20 22:19 Jason Low
2016-05-20 23:01 ` Waiman Long
2016-06-03 10:57 ` [tip:locking/core] " tip-bot for Jason Low
0 siblings, 2 replies; 3+ messages in thread
From: Jason Low @ 2016-05-20 22:19 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar
Cc: linux-kernel, Waiman Long, Davidlohr Bueso, Paul E. McKenney,
Terry Rudd, Scott J Norton, Jason Low
The mutex owner can get read and written to locklessly.
Use WRITE_ONCE when setting and clearing the owner field
in order to avoid optimizations such as store tearing. This
avoids situations where the owner field gets written to with
multiple stores and another thread could concurrently read
and use a partially written owner value.
Signed-off-by: Jason Low <jason.low2@hpe.com>
Acked-by: Davidlohr Bueso <dave@stgolabs.net>
---
kernel/locking/mutex-debug.h | 4 ++--
kernel/locking/mutex.h | 10 ++++++++--
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/kernel/locking/mutex-debug.h b/kernel/locking/mutex-debug.h
index 0799fd3..372e653 100644
--- a/kernel/locking/mutex-debug.h
+++ b/kernel/locking/mutex-debug.h
@@ -29,12 +29,12 @@ extern void debug_mutex_init(struct mutex *lock, const char *name,
static inline void mutex_set_owner(struct mutex *lock)
{
- lock->owner = current;
+ WRITE_ONCE(lock->owner, current);
}
static inline void mutex_clear_owner(struct mutex *lock)
{
- lock->owner = NULL;
+ WRITE_ONCE(lock->owner, NULL);
}
#define spin_lock_mutex(lock, flags) \
diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
index 5cda397..12f9619 100644
--- a/kernel/locking/mutex.h
+++ b/kernel/locking/mutex.h
@@ -17,14 +17,20 @@
__list_del((waiter)->list.prev, (waiter)->list.next)
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
+/*
+ * The mutex owner can get read and written to locklessly.
+ * We should use WRITE_ONCE when writing the owner value to
+ * avoid store tearing, otherwise, a thread could potentially
+ * read a partially written and incomplete owner value.
+ */
static inline void mutex_set_owner(struct mutex *lock)
{
- lock->owner = current;
+ WRITE_ONCE(lock->owner, current);
}
static inline void mutex_clear_owner(struct mutex *lock)
{
- lock->owner = NULL;
+ WRITE_ONCE(lock->owner, NULL);
}
#else
static inline void mutex_set_owner(struct mutex *lock)
--
2.1.4
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] locking/mutex: Set and clear owner using WRITE_ONCE()
2016-05-20 22:19 [PATCH v2] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low
@ 2016-05-20 23:01 ` Waiman Long
2016-06-03 10:57 ` [tip:locking/core] " tip-bot for Jason Low
1 sibling, 0 replies; 3+ messages in thread
From: Waiman Long @ 2016-05-20 23:01 UTC (permalink / raw)
To: Jason Low
Cc: Peter Zijlstra, Ingo Molnar, linux-kernel, Davidlohr Bueso,
Paul E. McKenney, Terry Rudd, Scott J Norton, Jason Low
On 05/20/2016 06:19 PM, Jason Low wrote:
> The mutex owner can get read and written to locklessly.
> Use WRITE_ONCE when setting and clearing the owner field
> in order to avoid optimizations such as store tearing. This
> avoids situations where the owner field gets written to with
> multiple stores and another thread could concurrently read
> and use a partially written owner value.
>
> Signed-off-by: Jason Low<jason.low2@hpe.com>
> Acked-by: Davidlohr Bueso<dave@stgolabs.net>
> ---
> kernel/locking/mutex-debug.h | 4 ++--
> kernel/locking/mutex.h | 10 ++++++++--
> 2 files changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/locking/mutex-debug.h b/kernel/locking/mutex-debug.h
> index 0799fd3..372e653 100644
> --- a/kernel/locking/mutex-debug.h
> +++ b/kernel/locking/mutex-debug.h
> @@ -29,12 +29,12 @@ extern void debug_mutex_init(struct mutex *lock, const char *name,
>
> static inline void mutex_set_owner(struct mutex *lock)
> {
> - lock->owner = current;
> + WRITE_ONCE(lock->owner, current);
> }
>
> static inline void mutex_clear_owner(struct mutex *lock)
> {
> - lock->owner = NULL;
> + WRITE_ONCE(lock->owner, NULL);
> }
>
> #define spin_lock_mutex(lock, flags) \
> diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
> index 5cda397..12f9619 100644
> --- a/kernel/locking/mutex.h
> +++ b/kernel/locking/mutex.h
> @@ -17,14 +17,20 @@
> __list_del((waiter)->list.prev, (waiter)->list.next)
>
> #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
> +/*
> + * The mutex owner can get read and written to locklessly.
> + * We should use WRITE_ONCE when writing the owner value to
> + * avoid store tearing, otherwise, a thread could potentially
> + * read a partially written and incomplete owner value.
> + */
> static inline void mutex_set_owner(struct mutex *lock)
> {
> - lock->owner = current;
> + WRITE_ONCE(lock->owner, current);
> }
>
> static inline void mutex_clear_owner(struct mutex *lock)
> {
> - lock->owner = NULL;
> + WRITE_ONCE(lock->owner, NULL);
> }
> #else
> static inline void mutex_set_owner(struct mutex *lock)
Acked-by: Waiman Long <Waiman.Long@hpe.com>
^ permalink raw reply [flat|nested] 3+ messages in thread* [tip:locking/core] locking/mutex: Set and clear owner using WRITE_ONCE()
2016-05-20 22:19 [PATCH v2] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low
2016-05-20 23:01 ` Waiman Long
@ 2016-06-03 10:57 ` tip-bot for Jason Low
1 sibling, 0 replies; 3+ messages in thread
From: tip-bot for Jason Low @ 2016-06-03 10:57 UTC (permalink / raw)
To: linux-tip-commits
Cc: paulmck, torvalds, Waiman.Long, hpa, linux-kernel, terry.rudd,
tglx, akpm, scott.norton, dave, peterz, mingo, jason.low2
Commit-ID: 6e2814745c67ab422b86262b05e6f23a56f28aa3
Gitweb: http://git.kernel.org/tip/6e2814745c67ab422b86262b05e6f23a56f28aa3
Author: Jason Low <jason.low2@hpe.com>
AuthorDate: Fri, 20 May 2016 15:19:36 -0700
Committer: Ingo Molnar <mingo@kernel.org>
CommitDate: Fri, 3 Jun 2016 12:06:10 +0200
locking/mutex: Set and clear owner using WRITE_ONCE()
The mutex owner can get read and written to locklessly.
Use WRITE_ONCE when setting and clearing the owner field
in order to avoid optimizations such as store tearing. This
avoids situations where the owner field gets written to with
multiple stores and another thread could concurrently read
and use a partially written owner value.
Signed-off-by: Jason Low <jason.low2@hpe.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Davidlohr Bueso <dave@stgolabs.net>
Acked-by: Waiman Long <Waiman.Long@hpe.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Scott J Norton <scott.norton@hpe.com>
Cc: Terry Rudd <terry.rudd@hpe.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1463782776.2479.9.camel@j-VirtualBox
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
kernel/locking/mutex-debug.h | 4 ++--
kernel/locking/mutex.h | 10 ++++++++--
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/kernel/locking/mutex-debug.h b/kernel/locking/mutex-debug.h
index 0799fd3..372e653 100644
--- a/kernel/locking/mutex-debug.h
+++ b/kernel/locking/mutex-debug.h
@@ -29,12 +29,12 @@ extern void debug_mutex_init(struct mutex *lock, const char *name,
static inline void mutex_set_owner(struct mutex *lock)
{
- lock->owner = current;
+ WRITE_ONCE(lock->owner, current);
}
static inline void mutex_clear_owner(struct mutex *lock)
{
- lock->owner = NULL;
+ WRITE_ONCE(lock->owner, NULL);
}
#define spin_lock_mutex(lock, flags) \
diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
index 5cda397..12f9619 100644
--- a/kernel/locking/mutex.h
+++ b/kernel/locking/mutex.h
@@ -17,14 +17,20 @@
__list_del((waiter)->list.prev, (waiter)->list.next)
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
+/*
+ * The mutex owner can get read and written to locklessly.
+ * We should use WRITE_ONCE when writing the owner value to
+ * avoid store tearing, otherwise, a thread could potentially
+ * read a partially written and incomplete owner value.
+ */
static inline void mutex_set_owner(struct mutex *lock)
{
- lock->owner = current;
+ WRITE_ONCE(lock->owner, current);
}
static inline void mutex_clear_owner(struct mutex *lock)
{
- lock->owner = NULL;
+ WRITE_ONCE(lock->owner, NULL);
}
#else
static inline void mutex_set_owner(struct mutex *lock)
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-06-03 10:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-20 22:19 [PATCH v2] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low
2016-05-20 23:01 ` Waiman Long
2016-06-03 10:57 ` [tip:locking/core] " tip-bot for Jason Low
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox