MCS Lock: MCS lock code cleanup and optimizations
A message that is not in the archive (sent off-list or not yet received): [email protected]
cover MCS Lock: MCS lock code cleanup and optimizations
In this patch series, we separated out the MCS lock code which was previously embedded in the mutex.c. This allows for easier reuse of MCS lock in other places like rwsem and qrwlock. We also did some micro optimizations and barrier cleanup.
This patches were previously part of the rwsem optimization patch series but now we spearate them out.
Tim Chen
Jason Low (2):
MCS Lock: optimizations and extra comments
MCS Lock: Barrier corrections
Tim Chen (1):
MCS Lock: Restructure the MCS lock defines and locking code into its
own file
Waiman Long (1):
MCS Lock: Make mcs_spinlock.h includable in other files
include/linux/mcs_spinlock.h | 100 ++++++++++++++++++++++++++++++++++++++++++ include/linux/mutex.h | 5 +- kernel/Makefile | 6 +- kernel/mcs_spinlock.c | 37 +++++++++++++++ kernel/mutex.c | 60 +++---------------------- 5 files changed, 150 insertions(+), 58 deletions(-) create mode 100644 include/linux/mcs_spinlock.h create mode 100644 kernel/mcs_spinlock.c
Signature · 8 lines
1.7.4.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/4 MCS Lock: Restructure the MCS lock defines and locking code into its own file
In reply to a message not in the archiveWe will need the MCS lock code for doing optimistic spinning for rwsem. Extracting the MCS code from mutex.c and put into its own file allow us to reuse this code easily for rwsem.
--- include/linux/mcs_spinlock.h | 64 ++++++++++++++++++++++++++++++++++++++++++ include/linux/mutex.h | 5 ++- kernel/mutex.c | 60 ++++---------------------------------- 3 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 include/linux/mcs_spinlock.h
include/linux/mcs_spinlock.h new file +64 -0
@@ -0,0 +1,64 @@ +/* +* MCS lock defines +* +* This file contains the main data structure and API definitions of MCS lock. +* +* The MCS lock (proposed by Mellor-Crummey and Scott) is a simple spin-lock +* with the desirable properties of being fair, and with each cpu trying +* to acquire the lock spinning on a local variable. +* It avoids expensive cache bouncings that common test-and-set spin-lock +* implementations incur. +*/ +#ifndef __LINUX_MCS_SPINLOCK_H +#define __LINUX_MCS_SPINLOCK_H + +struct mcs_spinlock { +struct mcs_spinlock *next; +int locked;/* 1 if lock acquired */ +}; + +/* +* We don't inline mcs_spin_lock() so that perf can correctly account for the +* time spent in this lock function. +*/ +static noinline +void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) +{ +struct mcs_spinlock *prev; + +/* Init node */ + node->locked =0 ; + node->next =NULL ; + + prev =xchg (lock, node); +if (likely (prev ==NULL )) { +/* Lock acquired */ + node->locked =1 ; +return ; + } +ACCESS_ONCE (prev->next) = node; +smp_wmb (); +/* Wait until the lock holder passes the lock down */ +while (!ACCESS_ONCE (node->locked)) +arch_mutex_cpu_relax (); +} + +static void mcs_spin_unlock (struct mcs_spinlock **lock,struct mcs_spinlock *node) +{ +struct mcs_spinlock *next =ACCESS_ONCE (node->next); + +if (likely (!next)) { +/* +* Release the lock by setting it to NULL +*/ +if (cmpxchg (lock, node,NULL ) == node) +return ; +/* Wait until the next pointer is set */ +while (!(next =ACCESS_ONCE (node->next))) +arch_mutex_cpu_relax (); + } +ACCESS_ONCE (next->locked) =1 ; +smp_wmb (); +} + +#endif /* __LINUX_MCS_SPINLOCK_H */
include/linux/mutex.h +3 -2
@@ -46,6 +46,7 @@* - detects multi-task circular deadlocks and prints out all affected * locks and tasks (and only those tasks) */ +struct mcs_spinlock ;struct mutex {/* 1: unlocked, 0: locked, negative: locked, possible waiters */ atomic_t count;
@@ -55,7 +56,7 @@ struct mutex {struct task_struct *owner;#endif #ifdef CONFIG_MUTEX_SPIN_ON_OWNER -void *spin_mlock;/* Spinner MCS lock */ +struct mcs_spinlock *mcs_lock;/* Spinner MCS lock */ #endif #ifdef CONFIG_DEBUG_MUTEXES const char *name;
@@ -179,4 +180,4 @@ extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);#define arch_mutex_cpu_relax () cpu_relax()#endif -#endif +#endif /* __LINUX_MUTEX_H */
kernel/mutex.c +7 -53
102 lines not on this page · raw message
Signature · 9 lines
1.7.4.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/4 MCS Lock: optimizations and extra comments
In reply to a message not in the archiveRemove unnecessary operation and make the cmpxchg(lock, node, NULL) == node check in mcs_spin_unlock() likely() as it is likely that a race did not occur most of the time.
Also add in more comments describing how the local node is used in MCS locks.
--- include/linux/mcs_spinlock.h | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-)
include/linux/mcs_spinlock.h +11 -2
@@ -18,6 +18,12 @@ struct mcs_spinlock { };/* +* In order to acquire the lock, the caller should declare a local node and +* pass a reference of the node to this function in addition to the lock. +* If the lock has already been acquired, then this will proceed to spin +* on this node->locked until the previous lock holder sets the node->locked +* in mcs_spin_unlock(). +* * We don't inline mcs_spin_lock() so that perf can correctly account for the * time spent in this lock function. */
@@ -33,7 +39,6 @@ void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) prev =xchg (lock, node);if (likely (prev ==NULL )) {/* Lock acquired */ - node->locked =1 ;return ; }ACCESS_ONCE (prev->next) = node;
@@ -43,6 +48,10 @@ void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node)arch_mutex_cpu_relax (); } +/* +* Releases the lock. The caller should pass in the corresponding node that +* was used to acquire the lock. +*/ static void mcs_spin_unlock (struct mcs_spinlock **lock,struct mcs_spinlock *node) {struct mcs_spinlock *next =ACCESS_ONCE (node->next);
@@ -51,7 +60,7 @@ static void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *nod/* * Release the lock by setting it to NULL */ -if (cmpxchg (lock, node,NULL ) == node) +if (likely (cmpxchg (lock, node,NULL ) == node))return ;/* Wait until the next pointer is set */ while (!(next =ACCESS_ONCE (node->next)))
Signature · 9 lines
1.7.4.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/4 MCS Lock: Barrier corrections
In reply to a message not in the archiveThis patch corrects the way memory barriers are used in the MCS lock and removes ones that are not needed. Also add comments on all barriers.
--- include/linux/mcs_spinlock.h | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-)
include/linux/mcs_spinlock.h +11 -2
@@ -36,16 +36,19 @@ void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) node->locked =0 ; node->next =NULL ; +/* xchg() provides a memory barrier */ prev =xchg (lock, node);if (likely (prev ==NULL )) {/* Lock acquired */ return ; }ACCESS_ONCE (prev->next) = node; -smp_wmb ();/* Wait until the lock holder passes the lock down */ while (!ACCESS_ONCE (node->locked))arch_mutex_cpu_relax (); + +/* Make sure subsequent operations happen after the lock is acquired */ +smp_rmb (); }/*
@@ -58,6 +61,7 @@ static void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *nodif (likely (!next)) {/* +* cmpxchg() provides a memory barrier. * Release the lock by setting it to NULL */ if (likely (cmpxchg (lock, node,NULL ) == node))
@@ -65,9 +69,14 @@ static void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *nod/* Wait until the next pointer is set */ while (!(next =ACCESS_ONCE (node->next))) arch_mutex_cpu_relax(); + }else { +/* +* Make sure all operations within the critical section +* happen before the lock is released. +*/ +smp_wmb (); }ACCESS_ONCE (next->locked) =1 ; -smp_wmb (); }#endif /* __LINUX_MCS_SPINLOCK_H */
Signature · 9 lines
1.7.4.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/4 MCS Lock: Make mcs_spinlock.h includable in other files
In reply to a message not in the archiveThe following changes are made to enable mcs_spinlock.h file to be widely included in other files without causing problem:
1) Include a number of prerequisite header files and define arch_mutex_cpu_relax(), if not previously defined.
2) Separate out mcs_spin_lock() into a mcs_spinlock.c file.
3) Make mcs_spin_unlock() an inlined function.
--- include/linux/mcs_spinlock.h | 28 +++++++++++++++++++++++----- kernel/Makefile | 6 +++--- kernel/mcs_spinlock.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 kernel/mcs_spinlock.c
include/linux/mcs_spinlock.h +23 -5
@@ -12,11 +12,27 @@#ifndef __LINUX_MCS_SPINLOCK_H#define __LINUX_MCS_SPINLOCK_H +/* +* asm/processor.h may define arch_mutex_cpu_relax(). +* If it is not defined, cpu_relax() will be used. +*/ +#include <asm/barrier.h> +#include <asm/cmpxchg.h> +#include <asm/processor.h> +#include <linux/compiler.h> + +#ifndef arch_mutex_cpu_relax +# define arch_mutex_cpu_relax () cpu_relax() +#endif +struct mcs_spinlock {struct mcs_spinlock *next;int locked;/* 1 if lock acquired */ }; +extern +void mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node); +/* * In order to acquire the lock, the caller should declare a local node and * pass a reference of the node to this function in addition to the lock.
@@ -24,11 +40,11 @@ struct mcs_spinlock {* on this node->locked until the previous lock holder sets the node->locked * in mcs_spin_unlock(). * -* We don't inline mcs_spin_lock() so that perf can correctly account for the -* time spent in this lock function. +* The _raw_mcs_spin_lock() function should not be called directly. Instead, +* users should call mcs_spin_lock(). */ -static noinline -void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) +static inline +void _raw_mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node) {struct mcs_spinlock *prev;
@@ -55,7 +71,9 @@ void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node)* Releases the lock. The caller should pass in the corresponding node that * was used to acquire the lock. */ -static void mcs_spin_unlock (struct mcs_spinlock **lock,struct mcs_spinlock *node) + +static inline +void mcs_spin_unlock (struct mcs_spinlock **lock,struct mcs_spinlock *node) {struct mcs_spinlock *next =ACCESS_ONCE (node->next);
kernel/Makefile +3 -3
@@ -50,9 +50,9 @@ obj-$(CONFIG_SMP) += smp.o ifneq ($(CONFIG_SMP),y) obj-y += up.o endif -obj-$(CONFIG_SMP) += spinlock.o -obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o -obj-$(CONFIG_PROVE_LOCKING) += spinlock.o +obj-$(CONFIG_SMP) += spinlock.o mcs_spinlock.o +obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o mcs_spinlock.o +obj-$(CONFIG_PROVE_LOCKING) += spinlock.o mcs_spinlock.o obj-$(CONFIG_UID16) += uid16.o obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_MODULE_SIG) += module_signing.o modsign_pubkey.o modsign_certificate.o
kernel/mcs_spinlock.c new file +37 -0
@@ -0,0 +1,37 @@ +/* +* MCS lock +* +* The MCS lock (proposed by Mellor-Crummey and Scott) is a simple spin-lock +* with the desirable properties of being fair, and with each cpu trying +* to acquire the lock spinning on a local variable. +* It avoids expensive cache bouncings that common test-and-set spin-lock +* implementations incur. +*/ +#include <linux/mcs_spinlock.h> +#include <linux/export.h> + +/* +* We don't inline mcs_spin_lock() so that perf can correctly account for the +* time spent in this lock function. +*/ +void mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node) +{ +struct mcs_spinlock *prev; + +/* Init node */ + node->locked =0 ; + node->next =NULL ; + + prev =xchg (lock, node); +if (likely (prev ==NULL )) { +/* Lock acquired */ + node->locked =1 ; +return ; + } +ACCESS_ONCE (prev->next) = node; +smp_wmb (); +/* Wait until the lock holder passes the lock down */ +while (!ACCESS_ONCE (node->locked)) +arch_mutex_cpu_relax (); +} +EXPORT_SYMBOL (mcs_spin_lock);
Signature · 8 lines
1.7.4.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 4/4] MCS Lock: Make mcs_spinlock.h includable in other files
Hello,
Quoted 7 lines · Tim Chen
The following changes are made to enable
mcs_spinlock.hfile to be widely included in other files without causing problem:1) Include a number of prerequisite header files and define
arch_mutex_cpu_relax(), if not previously defined.
2) Separate outmcs_spin_lock()into amcs_spinlock.cfile.
3) Makemcs_spin_unlock()an inlined function.
[...]
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+void mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node) +{ +struct mcs_spinlock *prev; + +/* Init node */ + node->locked =0 ; + node->next =NULL ; + + prev =xchg (lock, node); +if (likely (prev ==NULL )) {
kernel/mcs_spinlock.c line 36
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+/* Lock acquired */ + node->locked =1 ; +return ; + } +ACCESS_ONCE (prev->next) = node; +smp_wmb (); +/* Wait until the lock holder passes the lock down */ +while (!ACCESS_ONCE (node->locked)) +arch_mutex_cpu_relax (); +}
You have the barrier in a different place than the version in the header file; is this intentional?
Also, why is an smp_wmb() sufficient (as opposed to a full smp_mb()?). Are there restrictions on the types of access that can occur in the critical section?
Will
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Peter Zijlstra
maintainerRe: [PATCH 4/4] MCS Lock: Make mcs_spinlock.h includable in other files
Quoted 37 lines · Will Deacon
Hello,
Quoted 7 lines · Tim Chen
The following changes are made to enable
mcs_spinlock.hfile to be widely included in other files without causing problem:1) Include a number of prerequisite header files and define
arch_mutex_cpu_relax(), if not previously defined.
2) Separate outmcs_spin_lock()into amcs_spinlock.cfile.
3) Makemcs_spin_unlock()an inlined function.[...]
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+void mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node) +{ +struct mcs_spinlock *prev; + +/* Init node */ + node->locked =0 ; + node->next =NULL ; + + prev =xchg (lock, node); +if (likely (prev ==NULL )) {kernel/mcs_spinlock.c line 36
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+/* Lock acquired */ + node->locked =1 ; +return ; + } +ACCESS_ONCE (prev->next) = node; +smp_wmb (); +/* Wait until the lock holder passes the lock down */ +while (!ACCESS_ONCE (node->locked)) +arch_mutex_cpu_relax (); +}You have the barrier in a different place than the version in the header file; is this intentional?
Quoted 3 lines
Also, why is an
smp_wmb()sufficient (as opposed to a fullsmp_mb()?). Are there restrictions on the types of access that can occur in the critical section?
Oh, good spot. I missed it because it doesn't actually remove the one in the header, why is that?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 4/4] MCS Lock: Make mcs_spinlock.h includable in other files
Quoted 38 lines · Peter Zijlstra
Quoted 37 lines · Will Deacon
Hello,
Quoted 7 lines · Tim Chen
The following changes are made to enable
mcs_spinlock.hfile to be widely included in other files without causing problem:1) Include a number of prerequisite header files and define
arch_mutex_cpu_relax(), if not previously defined.
2) Separate outmcs_spin_lock()into amcs_spinlock.cfile.
3) Makemcs_spin_unlock()an inlined function.[...]
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+void mcs_spin_lock (struct mcs_spinlock **lock,struct mcs_spinlock *node) +{ +struct mcs_spinlock *prev; + +/* Init node */ + node->locked =0 ; + node->next =NULL ; + + prev =xchg (lock, node); +if (likely (prev ==NULL )) {kernel/mcs_spinlock.c line 36
Quoted 10 lines
kernel/mcs_spinlock.c new file +10
-0+/* Lock acquired */ + node->locked =1 ; +return ; + } +ACCESS_ONCE (prev->next) = node; +smp_wmb (); +/* Wait until the lock holder passes the lock down */ +while (!ACCESS_ONCE (node->locked)) +arch_mutex_cpu_relax (); +}You have the barrier in a different place than the version in the header file; is this intentional?
Quoted 7 lines
Quoted 3 lines
Also, why is an
smp_wmb()sufficient (as opposed to a fullsmp_mb()?). Are there restrictions on the types of access that can occur in the critical section?Oh, good spot. I missed it because it doesn't actually remove the one in the header, why is that?
Good catch. I merged an older version of Waiman's patch. I'll correct this and update with the patch series with the newer one that should only call the mcs_spin_lock in the header file.
Tim
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Peter Zijlstra
maintainerRe: [PATCH 4/4] MCS Lock: Make mcs_spinlock.h includable in other files
In reply to Tim ChenQuoted 1 line · Tim Chen
+EXPORT_SYMBOL(mcs_spin_lock);
If that can be a GPL, please make it so.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>