Skip to content

Commit d84b672

Browse files
Davidlohr Buesoingomolnar
authored andcommitted
locking/mcs: Better differentiate between MCS variants
We have two flavors of the MCS spinlock: standard and cancelable (OSQ). While each one is independent of the other, we currently mix and match them. This patch: - Moves the OSQ code out of mcs_spinlock.h (which only deals with the traditional version) into include/linux/osq_lock.h. No unnecessary code is added to the more global header file, anything locks that make use of OSQ must include it anyway. - Renames mcs_spinlock.c to osq_lock.c. This file only contains osq code. - Introduces a CONFIG_LOCK_SPIN_ON_OWNER in order to only build osq_lock if there is support for it. Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Jason Low <jason.low2@hp.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mikulas Patocka <mpatocka@redhat.com> Cc: Waiman Long <Waiman.Long@hp.com> Link: http://lkml.kernel.org/r/1420573509-24774-5-git-send-email-dave@stgolabs.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4bd1908 commit d84b672

5 files changed

Lines changed: 17 additions & 25 deletions

File tree

‎include/linux/osq_lock.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* An MCS like lock especially tailored for optimistic spinning for sleeping
66
* lock implementations (mutex, rwsem, etc).
77
*/
8-
9-
#define OSQ_UNLOCKED_VAL (0)
8+
struct optimistic_spin_node {
9+
struct optimistic_spin_node *next, *prev;
10+
int locked; /* 1 if lock acquired */
11+
int cpu; /* encoded CPU # + 1 value */
12+
};
1013

1114
struct optimistic_spin_queue {
1215
/*
@@ -16,6 +19,8 @@ struct optimistic_spin_queue {
1619
atomic_t tail;
1720
};
1821

22+
#define OSQ_UNLOCKED_VAL (0)
23+
1924
/* Init macro and function. */
2025
#define OSQ_LOCK_UNLOCKED { ATOMIC_INIT(OSQ_UNLOCKED_VAL) }
2126

@@ -24,4 +29,7 @@ static inline void osq_lock_init(struct optimistic_spin_queue *lock)
2429
atomic_set(&lock->tail, OSQ_UNLOCKED_VAL);
2530
}
2631

32+
extern bool osq_lock(struct optimistic_spin_queue *lock);
33+
extern void osq_unlock(struct optimistic_spin_queue *lock);
34+
2735
#endif

‎kernel/Kconfig.locks‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ config RWSEM_SPIN_ON_OWNER
231231
def_bool y
232232
depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
233233

234+
config LOCK_SPIN_ON_OWNER
235+
def_bool y
236+
depends on MUTEX_SPIN_ON_OWNER || RWSEM_SPIN_ON_OWNER
237+
234238
config ARCH_USE_QUEUE_RWLOCK
235239
bool
236240

‎kernel/locking/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
obj-y += mutex.o semaphore.o rwsem.o mcs_spinlock.o
2+
obj-y += mutex.o semaphore.o rwsem.o
33

44
ifdef CONFIG_FUNCTION_TRACER
55
CFLAGS_REMOVE_lockdep.o = -pg
@@ -14,6 +14,7 @@ ifeq ($(CONFIG_PROC_FS),y)
1414
obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
1515
endif
1616
obj-$(CONFIG_SMP) += spinlock.o
17+
obj-$(CONFIG_LOCK_SPIN_ON_OWNER) += osq_lock.o
1718
obj-$(CONFIG_SMP) += lglock.o
1819
obj-$(CONFIG_PROVE_LOCKING) += spinlock.o
1920
obj-$(CONFIG_RT_MUTEXES) += rtmutex.o

‎kernel/locking/mcs_spinlock.h‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,4 @@ void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *node)
108108
arch_mcs_spin_unlock_contended(&next->locked);
109109
}
110110

111-
/*
112-
* Cancellable version of the MCS lock above.
113-
*
114-
* Intended for adaptive spinning of sleeping locks:
115-
* mutex_lock()/rwsem_down_{read,write}() etc.
116-
*/
117-
118-
struct optimistic_spin_node {
119-
struct optimistic_spin_node *next, *prev;
120-
int locked; /* 1 if lock acquired */
121-
int cpu; /* encoded CPU # value */
122-
};
123-
124-
extern bool osq_lock(struct optimistic_spin_queue *lock);
125-
extern void osq_unlock(struct optimistic_spin_queue *lock);
126-
127111
#endif /* __LINUX_MCS_SPINLOCK_H */
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <linux/percpu.h>
22
#include <linux/sched.h>
3-
#include "mcs_spinlock.h"
4-
5-
#ifdef CONFIG_SMP
3+
#include <linux/osq_lock.h>
64

75
/*
86
* An MCS like lock especially tailored for optimistic spinning for sleeping
@@ -203,6 +201,3 @@ void osq_unlock(struct optimistic_spin_queue *lock)
203201
if (next)
204202
ACCESS_ONCE(next->locked) = 1;
205203
}
206-
207-
#endif
208-

0 commit comments

Comments
 (0)