diff options
| author | Mauro Carvalho Chehab <m.chehab@samsung.com> | 2014-04-14 11:00:36 -0400 |
|---|---|---|
| committer | Mauro Carvalho Chehab <m.chehab@samsung.com> | 2014-04-14 11:00:36 -0400 |
| commit | 277a163c83d7ba93fba1e8980d29a9f8bfcfba6c (patch) | |
| tree | ccfd357d152292958957b6b8a993892e7a8cc95f /kernel/locking/mcs_spinlock.c | |
| parent | a83b93a7480441a47856dc9104bea970e84cda87 (diff) | |
| parent | c9eaa447e77efe77b7fa4c953bd62de8297fd6c5 (diff) | |
Merge tag 'v3.15-rc1' into patchwork
Linux 3.15-rc1
* tag 'v3.15-rc1': (12180 commits)
Linux 3.15-rc1
mm: Initialize error in shmem_file_aio_read()
cifs: Use min_t() when comparing "size_t" and "unsigned long"
sym53c8xx_2: Set DID_REQUEUE return code when aborting squeue
powerpc: Don't try to set LPCR unless we're in hypervisor mode
futex: update documentation for ordering guarantees
ceph: fix pr_fmt() redefinition
vti: don't allow to add the same tunnel twice
gre: don't allow to add the same tunnel twice
drivers: net: xen-netfront: fix array initialization bug
missing bits of "splice: fix racy pipe->buffers uses"
cifs: fix the race in cifs_writev()
ceph_sync_{,direct_}write: fix an oops on ceph_osdc_new_request() failure
pktgen: be friendly to LLTX devices
r8152: check RTL8152_UNPLUG
net: sun4i-emac: add promiscuous support
net/apne: replace IS_ERR and PTR_ERR with PTR_ERR_OR_ZERO
blackfin: cleanup board files
bf609: clock: drop unused clock bit set/clear functions
Blackfin: bf537: rename "CONFIG_ADT75"
...
Diffstat (limited to 'kernel/locking/mcs_spinlock.c')
| -rw-r--r-- | kernel/locking/mcs_spinlock.c | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/kernel/locking/mcs_spinlock.c b/kernel/locking/mcs_spinlock.c new file mode 100644 index 000000000000..838dc9e00669 --- /dev/null +++ b/kernel/locking/mcs_spinlock.c | |||
| @@ -0,0 +1,178 @@ | |||
| 1 | |||
| 2 | #include <linux/percpu.h> | ||
| 3 | #include <linux/mutex.h> | ||
| 4 | #include <linux/sched.h> | ||
| 5 | #include "mcs_spinlock.h" | ||
| 6 | |||
| 7 | #ifdef CONFIG_SMP | ||
| 8 | |||
| 9 | /* | ||
| 10 | * An MCS like lock especially tailored for optimistic spinning for sleeping | ||
| 11 | * lock implementations (mutex, rwsem, etc). | ||
| 12 | * | ||
| 13 | * Using a single mcs node per CPU is safe because sleeping locks should not be | ||
| 14 | * called from interrupt context and we have preemption disabled while | ||
| 15 | * spinning. | ||
| 16 | */ | ||
| 17 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_queue, osq_node); | ||
| 18 | |||
| 19 | /* | ||
| 20 | * Get a stable @node->next pointer, either for unlock() or unqueue() purposes. | ||
| 21 | * Can return NULL in case we were the last queued and we updated @lock instead. | ||
| 22 | */ | ||
| 23 | static inline struct optimistic_spin_queue * | ||
| 24 | osq_wait_next(struct optimistic_spin_queue **lock, | ||
| 25 | struct optimistic_spin_queue *node, | ||
| 26 | struct optimistic_spin_queue *prev) | ||
| 27 | { | ||
| 28 | struct optimistic_spin_queue *next = NULL; | ||
| 29 | |||
| 30 | for (;;) { | ||
| 31 | if (*lock == node && cmpxchg(lock, node, prev) == node) { | ||
| 32 | /* | ||
| 33 | * We were the last queued, we moved @lock back. @prev | ||
| 34 | * will now observe @lock and will complete its | ||
| 35 | * unlock()/unqueue(). | ||
| 36 | */ | ||
| 37 | break; | ||
| 38 | } | ||
| 39 | |||
| 40 | /* | ||
| 41 | * We must xchg() the @node->next value, because if we were to | ||
| 42 | * leave it in, a concurrent unlock()/unqueue() from | ||
| 43 | * @node->next might complete Step-A and think its @prev is | ||
| 44 | * still valid. | ||
| 45 | * | ||
| 46 | * If the concurrent unlock()/unqueue() wins the race, we'll | ||
| 47 | * wait for either @lock to point to us, through its Step-B, or | ||
| 48 | * wait for a new @node->next from its Step-C. | ||
| 49 | */ | ||
| 50 | if (node->next) { | ||
| 51 | next = xchg(&node->next, NULL); | ||
| 52 | if (next) | ||
| 53 | break; | ||
| 54 | } | ||
| 55 | |||
| 56 | arch_mutex_cpu_relax(); | ||
| 57 | } | ||
| 58 | |||
| 59 | return next; | ||
| 60 | } | ||
| 61 | |||
| 62 | bool osq_lock(struct optimistic_spin_queue **lock) | ||
| 63 | { | ||
| 64 | struct optimistic_spin_queue *node = this_cpu_ptr(&osq_node); | ||
| 65 | struct optimistic_spin_queue *prev, *next; | ||
| 66 | |||
| 67 | node->locked = 0; | ||
| 68 | node->next = NULL; | ||
| 69 | |||
| 70 | node->prev = prev = xchg(lock, node); | ||
| 71 | if (likely(prev == NULL)) | ||
| 72 | return true; | ||
| 73 | |||
| 74 | ACCESS_ONCE(prev->next) = node; | ||
| 75 | |||
| 76 | /* | ||
| 77 | * Normally @prev is untouchable after the above store; because at that | ||
| 78 | * moment unlock can proceed and wipe the node element from stack. | ||
| 79 | * | ||
| 80 | * However, since our nodes are static per-cpu storage, we're | ||
| 81 | * guaranteed their existence -- this allows us to apply | ||
| 82 | * cmpxchg in an attempt to undo our queueing. | ||
| 83 | */ | ||
| 84 | |||
| 85 | while (!smp_load_acquire(&node->locked)) { | ||
| 86 | /* | ||
| 87 | * If we need to reschedule bail... so we can block. | ||
| 88 | */ | ||
| 89 | if (need_resched()) | ||
| 90 | goto unqueue; | ||
| 91 | |||
| 92 | arch_mutex_cpu_relax(); | ||
| 93 | } | ||
| 94 | return true; | ||
| 95 | |||
| 96 | unqueue: | ||
| 97 | /* | ||
| 98 | * Step - A -- stabilize @prev | ||
| 99 | * | ||
| 100 | * Undo our @prev->next assignment; this will make @prev's | ||
| 101 | * unlock()/unqueue() wait for a next pointer since @lock points to us | ||
| 102 | * (or later). | ||
| 103 | */ | ||
| 104 | |||
| 105 | for (;;) { | ||
| 106 | if (prev->next == node && | ||
| 107 | cmpxchg(&prev->next, node, NULL) == node) | ||
| 108 | break; | ||
| 109 | |||
| 110 | /* | ||
| 111 | * We can only fail the cmpxchg() racing against an unlock(), | ||
| 112 | * in which case we should observe @node->locked becomming | ||
| 113 | * true. | ||
| 114 | */ | ||
| 115 | if (smp_load_acquire(&node->locked)) | ||
| 116 | return true; | ||
| 117 | |||
| 118 | arch_mutex_cpu_relax(); | ||
| 119 | |||
| 120 | /* | ||
| 121 | * Or we race against a concurrent unqueue()'s step-B, in which | ||
| 122 | * case its step-C will write us a new @node->prev pointer. | ||
| 123 | */ | ||
| 124 | prev = ACCESS_ONCE(node->prev); | ||
| 125 | } | ||
| 126 | |||
| 127 | /* | ||
| 128 | * Step - B -- stabilize @next | ||
| 129 | * | ||
| 130 | * Similar to unlock(), wait for @node->next or move @lock from @node | ||
| 131 | * back to @prev. | ||
| 132 | */ | ||
| 133 | |||
| 134 | next = osq_wait_next(lock, node, prev); | ||
| 135 | if (!next) | ||
| 136 | return false; | ||
| 137 | |||
| 138 | /* | ||
| 139 | * Step - C -- unlink | ||
| 140 | * | ||
| 141 | * @prev is stable because its still waiting for a new @prev->next | ||
| 142 | * pointer, @next is stable because our @node->next pointer is NULL and | ||
| 143 | * it will wait in Step-A. | ||
| 144 | */ | ||
| 145 | |||
| 146 | ACCESS_ONCE(next->prev) = prev; | ||
| 147 | ACCESS_ONCE(prev->next) = next; | ||
| 148 | |||
| 149 | return false; | ||
| 150 | } | ||
| 151 | |||
| 152 | void osq_unlock(struct optimistic_spin_queue **lock) | ||
| 153 | { | ||
| 154 | struct optimistic_spin_queue *node = this_cpu_ptr(&osq_node); | ||
| 155 | struct optimistic_spin_queue *next; | ||
| 156 | |||
| 157 | /* | ||
| 158 | * Fast path for the uncontended case. | ||
| 159 | */ | ||
| 160 | if (likely(cmpxchg(lock, node, NULL) == node)) | ||
| 161 | return; | ||
| 162 | |||
| 163 | /* | ||
| 164 | * Second most likely case. | ||
| 165 | */ | ||
| 166 | next = xchg(&node->next, NULL); | ||
| 167 | if (next) { | ||
| 168 | ACCESS_ONCE(next->locked) = 1; | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | next = osq_wait_next(lock, node, NULL); | ||
| 173 | if (next) | ||
| 174 | ACCESS_ONCE(next->locked) = 1; | ||
| 175 | } | ||
| 176 | |||
| 177 | #endif | ||
| 178 | |||
