From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932806AbcFCK6I (ORCPT ); Fri, 3 Jun 2016 06:58:08 -0400 Received: from terminus.zytor.com ([198.137.202.10]:56698 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932170AbcFCK6E (ORCPT ); Fri, 3 Jun 2016 06:58:04 -0400 Date: Fri, 3 Jun 2016 03:57:18 -0700 From: tip-bot for Jason Low Message-ID: Cc: paulmck@linux.vnet.ibm.com, torvalds@linux-foundation.org, Waiman.Long@hpe.com, hpa@zytor.com, linux-kernel@vger.kernel.org, terry.rudd@hpe.com, tglx@linutronix.de, akpm@linux-foundation.org, scott.norton@hpe.com, dave@stgolabs.net, peterz@infradead.org, mingo@kernel.org, jason.low2@hpe.com Reply-To: hpa@zytor.com, linux-kernel@vger.kernel.org, paulmck@linux.vnet.ibm.com, torvalds@linux-foundation.org, Waiman.Long@hpe.com, peterz@infradead.org, mingo@kernel.org, jason.low2@hpe.com, terry.rudd@hpe.com, tglx@linutronix.de, akpm@linux-foundation.org, scott.norton@hpe.com, dave@stgolabs.net In-Reply-To: <1463782776.2479.9.camel@j-VirtualBox> References: <1463782776.2479.9.camel@j-VirtualBox> To: linux-tip-commits@vger.kernel.org Subject: [tip:locking/core] locking/mutex: Set and clear owner using WRITE_ONCE() Git-Commit-ID: 6e2814745c67ab422b86262b05e6f23a56f28aa3 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 6e2814745c67ab422b86262b05e6f23a56f28aa3 Gitweb: http://git.kernel.org/tip/6e2814745c67ab422b86262b05e6f23a56f28aa3 Author: Jason Low AuthorDate: Fri, 20 May 2016 15:19:36 -0700 Committer: Ingo Molnar 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 Signed-off-by: Peter Zijlstra (Intel) Acked-by: Davidlohr Bueso Acked-by: Waiman Long Cc: Andrew Morton Cc: Linus Torvalds Cc: Paul E. McKenney Cc: Peter Zijlstra Cc: Scott J Norton Cc: Terry Rudd Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/1463782776.2479.9.camel@j-VirtualBox Signed-off-by: Ingo Molnar --- 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)