Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755323AbcIAPGu (ORCPT ); Thu, 1 Sep 2016 11:06:50 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33224 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933105AbcIAPF3 (ORCPT ); Thu, 1 Sep 2016 11:05:29 -0400 From: Manfred Spraul To: benh@kernel.crashing.org, paulmck@linux.vnet.ibm.com, Ingo Molnar , Boqun Feng , Peter Zijlstra , Andrew Morton Cc: LKML , will.deacon@arm.com, 1vier1@web.de, Davidlohr Bueso , Manfred Spraul Subject: [PATCH 2/7] spinlock: Document memory barrier rules for spin_lock and spin_unlock(). Date: Thu, 1 Sep 2016 17:04:12 +0200 Message-Id: <1472742257-10515-3-git-send-email-manfred@colorfullife.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1472742257-10515-1-git-send-email-manfred@colorfullife.com> References: <1472742257-10515-1-git-send-email-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In theory, the memory ordering rules for spinlock() and spin_unlock() are simple: spin_lock is ACQUIRE, spin_unlock is RELEASE. What is missing is a clear documentation for the corner cases: - What is covered by the ACQUIRE during spin_lock: only the lock load or also the lock store? - spin_unlock_wait() is spin_lock()+spin_unlock(). - No memory ordering is enforced by spin_is_locked(). The patch adds this into Documentation/locking/spinlock.txt. Signed-off-by: Manfred Spraul Cc: Will Deacon --- Documentation/locking/spinlocks.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Documentation/locking/spinlocks.txt b/Documentation/locking/spinlocks.txt index ff35e40..653b2fa 100644 --- a/Documentation/locking/spinlocks.txt +++ b/Documentation/locking/spinlocks.txt @@ -40,6 +40,15 @@ example, internal driver data structures that nobody else ever touches). touches a shared variable has to agree about the spinlock they want to use. + NOTE! Code that needs stricter memory barriers than ACQUIRE during + LOCK and RELEASE during UNLOCK must use appropriate memory barriers + such as smp_mb__before_spinlock(). Especially, the ACQUIRE during + LOCK applies to reading the lock state. Operations within the + guarded area can happen before the lock store. + spin_unlock_wait() behaves like spin_lock();spin_unlock(). + spin_is_locked() should not be used to build synchronization + primitives, no memory ordering is guaranteed. + ---- Lesson 2: reader-writer spinlocks. -- 2.7.4