Messages in this thread Patch in this message | | | From | Manfred Spraul <> | Subject | [PATCH 3/5] spinlock: define spinlock_store_acquire | Date | Wed, 31 Aug 2016 15:42:28 +0200 |
| |
A spinlock is an ACQUIRE regarding reading the lock state. The store of the lock may be postponed past the first operations within the protected area, e.g. see commit 51d7d5205d33 ("powerpc: Add smp_mb() to arch_spin_is_locked()".
The patch defines a new spinlock_store_acquire primitive: It guarantees that the store is ordered relative to the following load/store operations. Adding the barrier into spin_is_locked() does help as not everyone uses spin_is_locked() or spin_unlock_wait().
The patch: - adds a definition into <linux/spinlock.h> (as smp_mb(), which is safe for all architectures) - converts ipc/sem.c to the new define.
For overriding, the same approach as for smp_mb__before_spin_lock() is used: If smp_mb__after_spin_lock is already defined, then it is not changed.
The default is smp_mb(), to ensure that no architecture gets broken.
Signed-off-by: Manfred Spraul <manfred@colorfullife.com> --- include/linux/spinlock.h | 12 ++++++++++++ ipc/sem.c | 8 +------- 2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h index 47dd0ce..496f288 100644 --- a/include/linux/spinlock.h +++ b/include/linux/spinlock.h @@ -130,6 +130,18 @@ do { \ #define smp_mb__before_spinlock() smp_wmb() #endif +#ifndef spinlock_store_acquire +/** + * spinlock_store_acuqire() - Provide acquire() after store part + * + * spin_lock() provides ACQUIRE semantics regarding reading the lock. + * There are no guarantees that the lock write is visible before any read + * read or write operation within the protected area is performed. + * If the lock write must happen first, this function is required. + */ +#define spinlock_store_acquire() smp_mb() +#endif + /** * raw_spin_unlock_wait - wait until the spinlock gets unlocked * @lock: the spinlock in question. diff --git a/ipc/sem.c b/ipc/sem.c index 6586e0a..49d0ae3 100644 --- a/ipc/sem.c +++ b/ipc/sem.c @@ -355,13 +355,7 @@ 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(); + spinlock_store_acquire(); if (!smp_load_acquire(&sma->complex_mode)) { /* fast path successful! */ -- 2.7.4
| |