diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-09-02 15:12:15 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-09-02 15:12:15 -0400 |
commit | bc08b449ee14ace4d869adaa1bb35a44ce68d775 (patch) | |
tree | 133319a81192fad41aa999eac135e87905ab5bab | |
parent | 2f4f12e571c4e2f50f3818a3c2544929145f75dd (diff) |
lockref: implement lockless reference count updates using cmpxchg()
Instead of taking the spinlock, the lockless versions atomically check
that the lock is not taken, and do the reference count update using a
cmpxchg() loop. This is semantically identical to doing the reference
count update protected by the lock, but avoids the "wait for lock"
contention that you get when accesses to the reference count are
contended.
Note that a "lockref" is absolutely _not_ equivalent to an atomic_t.
Even when the lockref reference counts are updated atomically with
cmpxchg, the fact that they also verify the state of the spinlock means
that the lockless updates can never happen while somebody else holds the
spinlock.
So while "lockref_put_or_lock()" looks a lot like just another name for
"atomic_dec_and_lock()", and both optimize to lockless updates, they are
fundamentally different: the decrement done by atomic_dec_and_lock() is
truly independent of any lock (as long as it doesn't decrement to zero),
so a locked region can still see the count change.
The lockref structure, in contrast, really is a *locked* reference
count. If you hold the spinlock, the reference count will be stable and
you can modify the reference count without using atomics, because even
the lockless updates will see and respect the state of the lock.
In order to enable the cmpxchg lockless code, the architecture needs to
do three things:
(1) Make sure that the "arch_spinlock_t" and an "unsigned int" can fit
in an aligned u64, and have a "cmpxchg()" implementation that works
on such a u64 data type.
(2) define a helper function to test for a spinlock being unlocked
("arch_spin_value_unlocked()")
(3) select the "ARCH_USE_CMPXCHG_LOCKREF" config variable in its
Kconfig file.
This enables it for x86-64 (but not 32-bit, we'd need to make sure
cmpxchg() turns into the proper cmpxchg8b in order to enable it for
32-bit mode).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | arch/x86/Kconfig | 1 | ||||
-rw-r--r-- | arch/x86/include/asm/spinlock.h | 5 | ||||
-rw-r--r-- | include/linux/lockref.h | 11 | ||||
-rw-r--r-- | lib/Kconfig | 10 | ||||
-rw-r--r-- | lib/lockref.c | 60 |
5 files changed, 84 insertions, 3 deletions
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index b32ebf92b0ce..67e00740531c 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig | |||
@@ -16,6 +16,7 @@ config X86_64 | |||
16 | def_bool y | 16 | def_bool y |
17 | depends on 64BIT | 17 | depends on 64BIT |
18 | select X86_DEV_DMA_OPS | 18 | select X86_DEV_DMA_OPS |
19 | select ARCH_USE_CMPXCHG_LOCKREF | ||
19 | 20 | ||
20 | ### Arch settings | 21 | ### Arch settings |
21 | config X86 | 22 | config X86 |
diff --git a/arch/x86/include/asm/spinlock.h b/arch/x86/include/asm/spinlock.h index e3ddd7db723f..e0e668422c75 100644 --- a/arch/x86/include/asm/spinlock.h +++ b/arch/x86/include/asm/spinlock.h | |||
@@ -34,6 +34,11 @@ | |||
34 | # define UNLOCK_LOCK_PREFIX | 34 | # define UNLOCK_LOCK_PREFIX |
35 | #endif | 35 | #endif |
36 | 36 | ||
37 | static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) | ||
38 | { | ||
39 | return lock.tickets.head == lock.tickets.tail; | ||
40 | } | ||
41 | |||
37 | /* | 42 | /* |
38 | * Ticket locks are conceptually two parts, one indicating the current head of | 43 | * Ticket locks are conceptually two parts, one indicating the current head of |
39 | * the queue, and the other indicating the current tail. The lock is acquired | 44 | * the queue, and the other indicating the current tail. The lock is acquired |
diff --git a/include/linux/lockref.h b/include/linux/lockref.h index 4c0af31c8d47..ca07b5028b01 100644 --- a/include/linux/lockref.h +++ b/include/linux/lockref.h | |||
@@ -17,8 +17,15 @@ | |||
17 | #include <linux/spinlock.h> | 17 | #include <linux/spinlock.h> |
18 | 18 | ||
19 | struct lockref { | 19 | struct lockref { |
20 | spinlock_t lock; | 20 | union { |
21 | unsigned int count; | 21 | #ifdef CONFIG_CMPXCHG_LOCKREF |
22 | aligned_u64 lock_count; | ||
23 | #endif | ||
24 | struct { | ||
25 | spinlock_t lock; | ||
26 | unsigned int count; | ||
27 | }; | ||
28 | }; | ||
22 | }; | 29 | }; |
23 | 30 | ||
24 | extern void lockref_get(struct lockref *); | 31 | extern void lockref_get(struct lockref *); |
diff --git a/lib/Kconfig b/lib/Kconfig index 71d9f81f6eed..65561716c16c 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
@@ -48,6 +48,16 @@ config STMP_DEVICE | |||
48 | config PERCPU_RWSEM | 48 | config PERCPU_RWSEM |
49 | boolean | 49 | boolean |
50 | 50 | ||
51 | config ARCH_USE_CMPXCHG_LOCKREF | ||
52 | bool | ||
53 | |||
54 | config CMPXCHG_LOCKREF | ||
55 | def_bool y if ARCH_USE_CMPXCHG_LOCKREF | ||
56 | depends on SMP | ||
57 | depends on !GENERIC_LOCKBREAK | ||
58 | depends on !DEBUG_SPINLOCK | ||
59 | depends on !DEBUG_LOCK_ALLOC | ||
60 | |||
51 | config CRC_CCITT | 61 | config CRC_CCITT |
52 | tristate "CRC-CCITT functions" | 62 | tristate "CRC-CCITT functions" |
53 | help | 63 | help |
diff --git a/lib/lockref.c b/lib/lockref.c index a9a4f4e1eff5..7819c2d1d315 100644 --- a/lib/lockref.c +++ b/lib/lockref.c | |||
@@ -1,6 +1,33 @@ | |||
1 | #include <linux/export.h> | 1 | #include <linux/export.h> |
2 | #include <linux/lockref.h> | 2 | #include <linux/lockref.h> |
3 | 3 | ||
4 | #ifdef CONFIG_CMPXCHG_LOCKREF | ||
5 | |||
6 | /* | ||
7 | * Note that the "cmpxchg()" reloads the "old" value for the | ||
8 | * failure case. | ||
9 | */ | ||
10 | #define CMPXCHG_LOOP(CODE, SUCCESS) do { \ | ||
11 | struct lockref old; \ | ||
12 | BUILD_BUG_ON(sizeof(old) != 8); \ | ||
13 | old.lock_count = ACCESS_ONCE(lockref->lock_count); \ | ||
14 | while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \ | ||
15 | struct lockref new = old, prev = old; \ | ||
16 | CODE \ | ||
17 | old.lock_count = cmpxchg(&lockref->lock_count, \ | ||
18 | old.lock_count, new.lock_count); \ | ||
19 | if (likely(old.lock_count == prev.lock_count)) { \ | ||
20 | SUCCESS; \ | ||
21 | } \ | ||
22 | } \ | ||
23 | } while (0) | ||
24 | |||
25 | #else | ||
26 | |||
27 | #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0) | ||
28 | |||
29 | #endif | ||
30 | |||
4 | /** | 31 | /** |
5 | * lockref_get - Increments reference count unconditionally | 32 | * lockref_get - Increments reference count unconditionally |
6 | * @lockcnt: pointer to lockref structure | 33 | * @lockcnt: pointer to lockref structure |
@@ -10,6 +37,12 @@ | |||
10 | */ | 37 | */ |
11 | void lockref_get(struct lockref *lockref) | 38 | void lockref_get(struct lockref *lockref) |
12 | { | 39 | { |
40 | CMPXCHG_LOOP( | ||
41 | new.count++; | ||
42 | , | ||
43 | return; | ||
44 | ); | ||
45 | |||
13 | spin_lock(&lockref->lock); | 46 | spin_lock(&lockref->lock); |
14 | lockref->count++; | 47 | lockref->count++; |
15 | spin_unlock(&lockref->lock); | 48 | spin_unlock(&lockref->lock); |
@@ -23,9 +56,18 @@ EXPORT_SYMBOL(lockref_get); | |||
23 | */ | 56 | */ |
24 | int lockref_get_not_zero(struct lockref *lockref) | 57 | int lockref_get_not_zero(struct lockref *lockref) |
25 | { | 58 | { |
26 | int retval = 0; | 59 | int retval; |
60 | |||
61 | CMPXCHG_LOOP( | ||
62 | new.count++; | ||
63 | if (!old.count) | ||
64 | return 0; | ||
65 | , | ||
66 | return 1; | ||
67 | ); | ||
27 | 68 | ||
28 | spin_lock(&lockref->lock); | 69 | spin_lock(&lockref->lock); |
70 | retval = 0; | ||
29 | if (lockref->count) { | 71 | if (lockref->count) { |
30 | lockref->count++; | 72 | lockref->count++; |
31 | retval = 1; | 73 | retval = 1; |
@@ -43,6 +85,14 @@ EXPORT_SYMBOL(lockref_get_not_zero); | |||
43 | */ | 85 | */ |
44 | int lockref_get_or_lock(struct lockref *lockref) | 86 | int lockref_get_or_lock(struct lockref *lockref) |
45 | { | 87 | { |
88 | CMPXCHG_LOOP( | ||
89 | new.count++; | ||
90 | if (!old.count) | ||
91 | break; | ||
92 | , | ||
93 | return 1; | ||
94 | ); | ||
95 | |||
46 | spin_lock(&lockref->lock); | 96 | spin_lock(&lockref->lock); |
47 | if (!lockref->count) | 97 | if (!lockref->count) |
48 | return 0; | 98 | return 0; |
@@ -59,6 +109,14 @@ EXPORT_SYMBOL(lockref_get_or_lock); | |||
59 | */ | 109 | */ |
60 | int lockref_put_or_lock(struct lockref *lockref) | 110 | int lockref_put_or_lock(struct lockref *lockref) |
61 | { | 111 | { |
112 | CMPXCHG_LOOP( | ||
113 | new.count--; | ||
114 | if (old.count <= 1) | ||
115 | break; | ||
116 | , | ||
117 | return 1; | ||
118 | ); | ||
119 | |||
62 | spin_lock(&lockref->lock); | 120 | spin_lock(&lockref->lock); |
63 | if (lockref->count <= 1) | 121 | if (lockref->count <= 1) |
64 | return 0; | 122 | return 0; |