Skip to content

Commit d89c703

Browse files
wildea01ingomolnar
authored andcommitted
locking/core: Remove break_lock field when CONFIG_GENERIC_LOCKBREAK=y
When CONFIG_GENERIC_LOCKBEAK=y, locking structures grow an extra int ->break_lock field which is used to implement raw_spin_is_contended() by setting the field to 1 when waiting on a lock and clearing it to zero when holding a lock. However, there are a few problems with this approach: - There is a write-write race between a CPU successfully taking the lock (and subsequently writing break_lock = 0) and a waiter waiting on the lock (and subsequently writing break_lock = 1). This could result in a contended lock being reported as uncontended and vice-versa. - On machines with store buffers, nothing guarantees that the writes to break_lock are visible to other CPUs at any particular time. - READ_ONCE/WRITE_ONCE are not used, so the field is potentially susceptible to harmful compiler optimisations, Consequently, the usefulness of this field is unclear and we'd be better off removing it and allowing architectures to implement raw_spin_is_contended() by providing a definition of arch_spin_is_contended(), as they can when CONFIG_GENERIC_LOCKBREAK=n. Signed-off-by: Will Deacon <will.deacon@arm.com> Acked-by: Peter Zijlstra <peterz@infradead.org> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Sebastian Ott <sebott@linux.vnet.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1511894539-7988-3-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent f87f3a3 commit d89c703

4 files changed

Lines changed: 1 addition & 19 deletions

File tree

‎include/linux/rwlock_types.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
*/
1111
typedef struct {
1212
arch_rwlock_t raw_lock;
13-
#ifdef CONFIG_GENERIC_LOCKBREAK
14-
unsigned int break_lock;
15-
#endif
1613
#ifdef CONFIG_DEBUG_SPINLOCK
1714
unsigned int magic, owner_cpu;
1815
void *owner;

‎include/linux/spinlock.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,11 @@ do { \
107107

108108
#define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock)
109109

110-
#ifdef CONFIG_GENERIC_LOCKBREAK
111-
#define raw_spin_is_contended(lock) ((lock)->break_lock)
112-
#else
113-
114110
#ifdef arch_spin_is_contended
115111
#define raw_spin_is_contended(lock) arch_spin_is_contended(&(lock)->raw_lock)
116112
#else
117113
#define raw_spin_is_contended(lock) (((void)(lock), 0))
118114
#endif /*arch_spin_is_contended*/
119-
#endif
120115

121116
/*
122117
* This barrier must provide two things:

‎include/linux/spinlock_types.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
typedef struct raw_spinlock {
2121
arch_spinlock_t raw_lock;
22-
#ifdef CONFIG_GENERIC_LOCKBREAK
23-
unsigned int break_lock;
24-
#endif
2522
#ifdef CONFIG_DEBUG_SPINLOCK
2623
unsigned int magic, owner_cpu;
2724
void *owner;

‎kernel/locking/spinlock.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
6666
break; \
6767
preempt_enable(); \
6868
\
69-
if (!(lock)->break_lock) \
70-
(lock)->break_lock = 1; \
71-
\
7269
arch_##op##_relax(&lock->raw_lock); \
7370
} \
74-
(lock)->break_lock = 0; \
7571
} \
7672
\
7773
unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
@@ -86,12 +82,9 @@ unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
8682
local_irq_restore(flags); \
8783
preempt_enable(); \
8884
\
89-
if (!(lock)->break_lock) \
90-
(lock)->break_lock = 1; \
91-
\
9285
arch_##op##_relax(&lock->raw_lock); \
9386
} \
94-
(lock)->break_lock = 0; \
87+
\
9588
return flags; \
9689
} \
9790
\

0 commit comments

Comments
 (0)