Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1404000

[PATCH] locking/mutex: Set and clear owner using WRITE_ONCE()

From Jason Low <jason.low2@hpe.com>
Newsgroups linux.kernel
Subject [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE()
Date 2016-05-20 00:40 +0200
Message-ID <rAELL-5Nf-7@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low <jason.low2@hpe.com> - 2016-05-20 00:40 +0200
  Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Davidlohr Bueso <dave@stgolabs.net> - 2016-05-20 03:20 +0200
  Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low <jason.low2@hpe.com> - 2016-05-21 00:20 +0200
  Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Davidlohr Bueso <dave@stgolabs.net> - 2016-05-21 03:10 +0200
    Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Waiman Long <waiman.long@hpe.com> - 2016-05-21 06:10 +0200
    Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low <jason.low2@hpe.com> - 2016-05-23 22:50 +0200
      Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Davidlohr Bueso <dave@stgolabs.net> - 2016-05-23 23:40 +0200
        Re: [PATCH] locking/mutex: Set and clear owner using WRITE_ONCE() Jason Low <jason.low2@hpe.com> - 2016-05-24 00:00 +0200

csiph-web