Skip to contentSkip to conversation

Clarify/standardize memory barriers for lock/unlock

PATCH New Manfred Spraul 1 participant 8 messages Sep 1, 2016

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v6 cover Clarify/standardize memory barriers for lock/unlock

Hi,

Based on the new consensus:
- spin_unlock_wait() is spin_lock();spin_unlock();
- no guarantees are provided by spin_is_locked().
- the acquire during spin_lock() is for the load, not for the store.

Summary:
If a high-scalability locking scheme is built with multiple spinlocks, then often additional memory barriers are required.

The documentation was not as clear as possible, and memory barriers were missing / superfluous in the implementation.

Patch 1: sem.c: Remove the smp_rmb() after spin_unlock_wait(). Patch 2: Documentation
Patch 3: Update ipc/sem.c based on rules above
Patch 4: Move smp_mb__after_unlock_lock to <linux/spinlock.h> Patch 5: Fix memory ordering for nf_conntrack
Patch 6: nf_conntrack: Remove smp_rmb() after spin_unlock_wait() Patch 7: nf_conntrack: Remove smp_mb() after spin_lock().

Patch 5 is larger than required, it rewrites the conntrack logic with the code from ipc/sem.c. I think the new code is simpler and more realtime-friendly.

@netfilter team: Over which tree should the patch be sent? Usually, I ask Andrew to merge my patches (as there is no maintainer tree for ipc).

@Andrew: The patches are relative to mmots.
Could you include them in your tree, with the target of including in linux-next?

--
Manfred

#1 lore raw

PATCH v6 1/7 ipc/sem.c: Remove smp_rmb() from complexmode_enter()

complexmode_enter() contains an smp_rmb() after spin_unlock_wait(). This was done to allow safe backporting.

Commit 2c6100227116 ("locking/qspinlock: Fix spin_unlock_wait() some more") (and the commits for the other archs) ensure that spin_unlock_wait() is an ACQUIRE.
Therefore the smp_rmb() after spin_unlock_wait() can be removed.

Not for stable!

---
 ipc/sem.c | 8 --------
 1 file changed, 8 deletions(-)
ipc/sem.c +0 -8
index 5e318c5..6586e0a 100644
@@ -290,14 +290,6 @@ static void complexmode_enter(struct sem_array *sma)
 		sem = sma->sem_base + i;
 		spin_unlock_wait(&sem->lock);
 	}
-	/*
-	 * spin_unlock_wait() is not a memory barriers, it is only a
-	 * control barrier. The code must pair with spin_unlock(&sem->lock),
-	 * thus just the control barrier is insufficient.
-	 *
-	 * smp_rmb() is sufficient, as writes cannot pass the control barrier.
-	 */
-	smp_rmb();
 }
 
 /*
Signature · 1 line
2.7.4
#2 lore raw

PATCH v6 2/7 spinlock: Document memory barrier rules for spin_lock and spin_unlock().

In reply to Manfred Spraul

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.

---
 Documentation/locking/spinlocks.txt | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/locking/spinlocks.txt +9 -0
index ff35e40..653b2fa 100644
@@ -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.
Signature · 1 line
2.7.4
#3 lore raw

PATCH v6 3/7 ipc/sem.c: Rely on spin_unlock_wait() = spin_lock();spin_unlock().

In reply to Manfred Spraul
Quoted 1 line

From memory ordering point of view, spin_unlock_wait() provides

the same guarantees as spin_lock(); spin_unlock().

Therefore the smp_mb() after spin_lock() is not necessary, spin_unlock_wait() must provide the memory ordering.

---
 ipc/sem.c | 8 --------
 1 file changed, 8 deletions(-)
ipc/sem.c +0 -8
index 6586e0a..a5da82c 100644
@@ -355,14 +355,6 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
 		 */
 		spin_lock(&sem->lock);
 
-		/*
-		 * See 51d7d5205d33
-		 * ("powerpc: Add smp_mb() to arch_spin_is_locked()"):
-		 * A full barrier is required: the write of sem->lock
-		 * must be visible before the read is executed
-		 */
-		smp_mb();
-
 		if (!smp_load_acquire(&sma->complex_mode)) {
 			/* fast path successful! */
 			return sops->sem_num;
Signature · 1 line
2.7.4
#4 lore raw

PATCH v6 4/7 spinlock.h: Move smp_mb__after_unlock_lock to spinlock.h

In reply to Manfred Spraul

Right now, there are two places for special memory barriers around lock/unlock:
- <include/linux/spinlock.h> defines smp_mb__before_spinlock()
- <kernel/rcu/tree.h> defines smb_mb_after_unlock_lock(), with hardcoded CONFIG_PPC as arch override.

To improve transparency, move the define from kernel/rcu/tree.h to <include/linux>, and add the standard approach for overrides.

v3: If smp_mb__after_unlock_lock() is in barrier.h, then for arm64, kernel/rcu/tree.c doesn't compile because barrier.h is not included in kernel/rcu/tree.c

(v2 was: add example from Paul, something that can happen on real HW)

spin_unlock() + spin_lock() together do not form a full memory barrier:
(everything initialized to 0)

CPU1:
a=1;
spin_unlock(&b);
spin_lock(&c);
+ smp_mb__after_unlock_lock();
r1=d;

CPU2:
d=1;
smp_mb();
r2=a;

Without the smp_mb__after_unlock_lock(), r1==0 && r2==0 would be possible.

--

---
 include/linux/spinlock.h | 16 ++++++++++++++++
 kernel/rcu/tree.h        | 12 ------------
 2 files changed, 16 insertions(+), 12 deletions(-)
include/linux/spinlock.h +16 -0
index 47dd0ce..6436a66 100644
@@ -130,6 +130,22 @@ do {								\
 #define smp_mb__before_spinlock()	smp_wmb()
 #endif
 
+#ifndef smp_mb__after_unlock_lock
+/**
+ * smp_mb__after_unlock_lock() - Provide smp_mb() after unlock+lock
+ *
+ * Place this after a lock-acquisition primitive to guarantee that
+ * an UNLOCK+LOCK pair act as a full barrier.  This guarantee applies
+ * if the UNLOCK and LOCK are executed by the same CPU or if the
+ * UNLOCK and LOCK operate on the same lock variable.
+ */
+#ifdef CONFIG_PPC
+#define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
+#else /* #ifdef CONFIG_PPC */
+#define smp_mb__after_unlock_lock()	do { } while (0)
+#endif /* #else #ifdef CONFIG_PPC */
+#endif
+
 /**
  * raw_spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
kernel/rcu/tree.h +0 -12
index e99a523..a0cd9ab 100644
@@ -687,18 +687,6 @@ static inline void rcu_nocb_q_lengths(struct rcu_data *rdp, long *ql, long *qll)
 #endif /* #ifdef CONFIG_RCU_TRACE */
 
 /*
- * Place this after a lock-acquisition primitive to guarantee that
- * an UNLOCK+LOCK pair act as a full barrier.  This guarantee applies
- * if the UNLOCK and LOCK are executed by the same CPU or if the
- * UNLOCK and LOCK operate on the same lock variable.
- */
-#ifdef CONFIG_PPC
-#define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
-#else /* #ifdef CONFIG_PPC */
-#define smp_mb__after_unlock_lock()	do { } while (0)
-#endif /* #else #ifdef CONFIG_PPC */
-
-/*
  * Wrappers for the rcu_node::lock acquire and release.
  *
  * Because the rcu_nodes form a tree, the tree traversal locking will observe
Signature · 1 line
2.7.4
#5 lore raw

PATCH v6 5/7 net/netfilter/nf_conntrack_core: Fix memory barriers.

In reply to Manfred Spraul

1) reading nf_conntrack_locks_all needs ACQUIRE memory ordering.

In addition, to simplify backporting, the patch also adds:

2) For some architectures the ACQUIRE during spin_lock only applies to loading the lock, not to storing the lock state. E.g. see commit 51d7d5205d33 ("powerpc: Add smp_mb() to arch_spin_is_locked()")

nf_conntrack_lock() does not handle this correctly:

    /* 1) Acquire the lock */
    spin_lock(lock);
    while (unlikely(nf_conntrack_locks_all)) {
        spin_unlock(lock);

Thus a memory barrier might be missing between spin_lock and reading nf_conntrack_locks_all.

3) Between the write of nf_conntrack_locks_all and spin_unlock_wait(), a memory barrier might be required.

As improvement:

4) Minor issue: If there would be many nf_conntrack_all_lock() callers, then nf_conntrack_lock() would loop forever.

Therefore: Change nf_conntrack_lock and nf_conntract_lock_all() to the approach used by ipc/sem.c:

1) add smp_load_acquire()

2) add smb_mb() after spin_lock()
Note: redundant if spin_unlock_wait() is implemented as spin_lock(); spin_unlock().
3) add smb_rmb() after spin_unlock_wait()
Note: redundant after
commit 2c6100227116 ("locking/qspinlock: Fix spin_unlock_wait() some more")

4) for nf_conntrack_lock, use spin_lock(&global_lock) instead of spin_unlock_wait(&global_lock) and loop backward.
5) use smp_store_mb() instead of a raw smp_mb()

As the minimal bugfix, it might be sufficient just to add the smp_load_acquire(), but then it must be checked first if all updates to qspinlock were backported.

Fixes b16c29191dc8
Cc Sasha Levin <[email protected]>
Cc Pablo Neira Ayuso <[email protected]>
---
 net/netfilter/nf_conntrack_core.c | 41 ++++++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 14 deletions(-)
net/netfilter/nf_conntrack_core.c +27 -14
index 7d90a5d..3847f09 100644
@@ -79,20 +79,29 @@ static __read_mostly bool nf_conntrack_locks_all;
 
 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
 {
+	/* 1) Acquire the lock */
 	spin_lock(lock);
-	while (unlikely(nf_conntrack_locks_all)) {
-		spin_unlock(lock);
 
-		/*
-		 * Order the 'nf_conntrack_locks_all' load vs. the
-		 * spin_unlock_wait() loads below, to ensure
-		 * that 'nf_conntrack_locks_all_lock' is indeed held:
-		 */
-		smp_rmb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
-		spin_unlock_wait(&nf_conntrack_locks_all_lock);
-		spin_lock(lock);
-	}
+	/* 2) Order storing the lock and reading nf_conntrack_locks_all */
+	smp_mb();
+
+	/* 3) read nf_conntrack_locks_all, with ACQUIRE semantics */
+	if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
+		return;
+
+	/* fast path failed, unlock */
+	spin_unlock(lock);
+
+	/* Slow path 1) get global lock */
+	spin_lock(&nf_conntrack_locks_all_lock);
+
+	/* Slow path 2) get the lock we want */
+	spin_lock(lock);
+
+	/* Slow path 3) release the global lock */
+	spin_unlock(&nf_conntrack_locks_all_lock);
 }
+
 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
 
 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
@@ -132,19 +141,23 @@ static void nf_conntrack_all_lock(void)
 	int i;
 
 	spin_lock(&nf_conntrack_locks_all_lock);
-	nf_conntrack_locks_all = true;
 
 	/*
-	 * Order the above store of 'nf_conntrack_locks_all' against
+	 * Order the store of 'nf_conntrack_locks_all' against
 	 * the spin_unlock_wait() loads below, such that if
 	 * nf_conntrack_lock() observes 'nf_conntrack_locks_all'
 	 * we must observe nf_conntrack_locks[] held:
 	 */
-	smp_mb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
+	smp_store_mb(nf_conntrack_locks_all, true);
 
 	for (i = 0; i < CONNTRACK_LOCKS; i++) {
 		spin_unlock_wait(&nf_conntrack_locks[i]);
 	}
+	/* spin_unlock_wait() is at least a control barrier.
+	 * Add smp_rmb() to upgrade it to an ACQUIRE, it must
+	 * pair with the spin_unlock(&nf_conntrack_locks[])
+	 */
+	smp_rmb();
 }
 
 static void nf_conntrack_all_unlock(void)
Signature · 1 line
2.7.4
#6 lore raw

PATCH v6 6/7 net/netfilter/nf_conntrack_core: Remove barriers after spin_unlock_wait

In reply to Manfred Spraul

nf_conntrack_all() contains an smp_rmb() after spin_unlock_wait(). This was done to allow safe backporting.

Commit 2c6100227116 ("locking/qspinlock: Fix spin_unlock_wait() some more") (and the commits for the other archs) ensure that spin_unlock_wait() is an ACQUIRE.
Therefore the smp_rmb() after spin_unlock_wait() can be removed.

Not for stable!

---
 net/netfilter/nf_conntrack_core.c | 5 -----
 1 file changed, 5 deletions(-)
net/netfilter/nf_conntrack_core.c +0 -5
index 3847f09..c8a2d26 100644
@@ -153,11 +153,6 @@ static void nf_conntrack_all_lock(void)
 	for (i = 0; i < CONNTRACK_LOCKS; i++) {
 		spin_unlock_wait(&nf_conntrack_locks[i]);
 	}
-	/* spin_unlock_wait() is at least a control barrier.
-	 * Add smp_rmb() to upgrade it to an ACQUIRE, it must
-	 * pair with the spin_unlock(&nf_conntrack_locks[])
-	 */
-	smp_rmb();
 }
 
 static void nf_conntrack_all_unlock(void)
Signature · 1 line
2.7.4
#7 lore raw

PATCH v6 7/7 net/netfilter/nf_conntrack_core: Remove smp_mb() after spin_lock().

In reply to Manfred Spraul

As spin_unlock_wait() is defined as equivalent to spin_lock(); spin_unlock(), the smp_mb() after spin_lock() is not required.

Remove it.

---
 net/netfilter/nf_conntrack_core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
net/netfilter/nf_conntrack_core.c +1 -4
index c8a2d26..7a3b5e6 100644
@@ -82,10 +82,7 @@ void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
 	/* 1) Acquire the lock */
 	spin_lock(lock);
 
-	/* 2) Order storing the lock and reading nf_conntrack_locks_all */
-	smp_mb();
-
-	/* 3) read nf_conntrack_locks_all, with ACQUIRE semantics */
+	/* 2) read nf_conntrack_locks_all, with ACQUIRE semantics */
 	if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
 		return;
 
Signature · 1 line
2.7.4