public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE()
@ 2016-05-19 22:23 Jason Low
  2016-05-20  1:19 ` Davidlohr Bueso
  2016-05-20 20:27 ` Waiman Long
  0 siblings, 2 replies; 9+ messages in thread
From: Jason Low @ 2016-05-19 22:23 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 without the wait_lock.
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>
---
 kernel/locking/mutex.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
index 5cda397..469b61e 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] 9+ messages in thread

end of thread, other threads:[~2016-05-23 21:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-19 22:23 [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low
2016-05-20  1:19 ` Davidlohr Bueso
2016-05-20 20:27 ` Waiman Long
2016-05-20 22:09   ` Jason Low
2016-05-21  1:00   ` Davidlohr Bueso
2016-05-21  4:09     ` Waiman Long
2016-05-23 20:40     ` Jason Low
2016-05-23 21:31       ` Davidlohr Bueso
2016-05-23 21:49         ` Jason Low

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox