Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 1 | #include <linux/percpu.h> |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 2 | #include <linux/sched.h> |
Davidlohr Bueso | d84b672 | 2015-01-06 11:45:07 -0800 | [diff] [blame] | 3 | #include <linux/osq_lock.h> |
Prateek Sood | 1d0f440 | 2017-06-16 10:32:47 +0530 | [diff] [blame] | 4 | #include <linux/sched/rt.h> |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 5 | |
| 6 | /* |
| 7 | * An MCS like lock especially tailored for optimistic spinning for sleeping |
| 8 | * lock implementations (mutex, rwsem, etc). |
| 9 | * |
| 10 | * Using a single mcs node per CPU is safe because sleeping locks should not be |
| 11 | * called from interrupt context and we have preemption disabled while |
| 12 | * spinning. |
| 13 | */ |
Jason Low | 046a619 | 2014-07-14 10:27:48 -0700 | [diff] [blame] | 14 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 15 | |
| 16 | /* |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 17 | * We use the value 0 to represent "no CPU", thus the encoded value |
| 18 | * will be the CPU number incremented by 1. |
| 19 | */ |
| 20 | static inline int encode_cpu(int cpu_nr) |
| 21 | { |
| 22 | return cpu_nr + 1; |
| 23 | } |
| 24 | |
| 25 | static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) |
| 26 | { |
| 27 | int cpu_nr = encoded_cpu_val - 1; |
| 28 | |
| 29 | return per_cpu_ptr(&osq_node, cpu_nr); |
| 30 | } |
| 31 | |
| 32 | /* |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 33 | * Get a stable @node->next pointer, either for unlock() or unqueue() purposes. |
| 34 | * Can return NULL in case we were the last queued and we updated @lock instead. |
| 35 | */ |
Jason Low | 046a619 | 2014-07-14 10:27:48 -0700 | [diff] [blame] | 36 | static inline struct optimistic_spin_node * |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 37 | osq_wait_next(struct optimistic_spin_queue *lock, |
Jason Low | 046a619 | 2014-07-14 10:27:48 -0700 | [diff] [blame] | 38 | struct optimistic_spin_node *node, |
| 39 | struct optimistic_spin_node *prev) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 40 | { |
Jason Low | 046a619 | 2014-07-14 10:27:48 -0700 | [diff] [blame] | 41 | struct optimistic_spin_node *next = NULL; |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 42 | int curr = encode_cpu(smp_processor_id()); |
| 43 | int old; |
| 44 | |
| 45 | /* |
| 46 | * If there is a prev node in queue, then the 'old' value will be |
| 47 | * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if |
| 48 | * we're currently last in queue, then the queue will then become empty. |
| 49 | */ |
| 50 | old = prev ? prev->cpu : OSQ_UNLOCKED_VAL; |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 51 | |
| 52 | for (;;) { |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 53 | if (atomic_read(&lock->tail) == curr && |
Davidlohr Bueso | c55a6ff | 2015-09-14 00:37:24 -0700 | [diff] [blame] | 54 | atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) { |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 55 | /* |
| 56 | * We were the last queued, we moved @lock back. @prev |
| 57 | * will now observe @lock and will complete its |
| 58 | * unlock()/unqueue(). |
| 59 | */ |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * We must xchg() the @node->next value, because if we were to |
| 65 | * leave it in, a concurrent unlock()/unqueue() from |
| 66 | * @node->next might complete Step-A and think its @prev is |
| 67 | * still valid. |
| 68 | * |
| 69 | * If the concurrent unlock()/unqueue() wins the race, we'll |
| 70 | * wait for either @lock to point to us, through its Step-B, or |
| 71 | * wait for a new @node->next from its Step-C. |
| 72 | */ |
| 73 | if (node->next) { |
| 74 | next = xchg(&node->next, NULL); |
| 75 | if (next) |
| 76 | break; |
| 77 | } |
| 78 | |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 79 | cpu_relax_lowlatency(); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return next; |
| 83 | } |
| 84 | |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 85 | bool osq_lock(struct optimistic_spin_queue *lock) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 86 | { |
Jason Low | 046a619 | 2014-07-14 10:27:48 -0700 | [diff] [blame] | 87 | struct optimistic_spin_node *node = this_cpu_ptr(&osq_node); |
| 88 | struct optimistic_spin_node *prev, *next; |
Prateek Sood | 1d0f440 | 2017-06-16 10:32:47 +0530 | [diff] [blame] | 89 | struct task_struct *task = current; |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 90 | int curr = encode_cpu(smp_processor_id()); |
| 91 | int old; |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 92 | |
| 93 | node->locked = 0; |
| 94 | node->next = NULL; |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 95 | node->cpu = curr; |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 96 | |
Davidlohr Bueso | c55a6ff | 2015-09-14 00:37:24 -0700 | [diff] [blame] | 97 | /* |
Will Deacon | b4b29f9 | 2015-12-11 17:46:41 +0000 | [diff] [blame] | 98 | * We need both ACQUIRE (pairs with corresponding RELEASE in |
| 99 | * unlock() uncontended, or fastpath) and RELEASE (to publish |
| 100 | * the node fields we just initialised) semantics when updating |
| 101 | * the lock tail. |
Davidlohr Bueso | c55a6ff | 2015-09-14 00:37:24 -0700 | [diff] [blame] | 102 | */ |
Will Deacon | b4b29f9 | 2015-12-11 17:46:41 +0000 | [diff] [blame] | 103 | old = atomic_xchg(&lock->tail, curr); |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 104 | if (old == OSQ_UNLOCKED_VAL) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 105 | return true; |
| 106 | |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 107 | prev = decode_cpu(old); |
| 108 | node->prev = prev; |
Prateek Sood | 3c25a9d | 2017-07-14 19:17:56 +0530 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * osq_lock() unqueue |
| 112 | * |
| 113 | * node->prev = prev osq_wait_next() |
| 114 | * WMB MB |
| 115 | * prev->next = node next->prev = prev // unqueue-C |
| 116 | * |
| 117 | * Here 'node->prev' and 'next->prev' are the same variable and we need |
| 118 | * to ensure these stores happen in-order to avoid corrupting the list. |
| 119 | */ |
| 120 | smp_wmb(); |
| 121 | |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 122 | WRITE_ONCE(prev->next, node); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * Normally @prev is untouchable after the above store; because at that |
| 126 | * moment unlock can proceed and wipe the node element from stack. |
| 127 | * |
| 128 | * However, since our nodes are static per-cpu storage, we're |
| 129 | * guaranteed their existence -- this allows us to apply |
| 130 | * cmpxchg in an attempt to undo our queueing. |
| 131 | */ |
| 132 | |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 133 | while (!READ_ONCE(node->locked)) { |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 134 | /* |
| 135 | * If we need to reschedule bail... so we can block. |
Prateek Sood | 1d0f440 | 2017-06-16 10:32:47 +0530 | [diff] [blame] | 136 | * If a task spins on owner on a CPU after acquiring |
| 137 | * osq_lock while a RT task spins on another CPU to |
| 138 | * acquire osq_lock, it will starve the owner from |
| 139 | * completing if owner is to be scheduled on the same CPU. |
| 140 | * It will be a live lock. |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 141 | */ |
Prateek Sood | 1d0f440 | 2017-06-16 10:32:47 +0530 | [diff] [blame] | 142 | if (need_resched() || rt_task(task)) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 143 | goto unqueue; |
| 144 | |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 145 | cpu_relax_lowlatency(); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 146 | } |
| 147 | return true; |
| 148 | |
| 149 | unqueue: |
| 150 | /* |
| 151 | * Step - A -- stabilize @prev |
| 152 | * |
| 153 | * Undo our @prev->next assignment; this will make @prev's |
| 154 | * unlock()/unqueue() wait for a next pointer since @lock points to us |
| 155 | * (or later). |
| 156 | */ |
| 157 | |
| 158 | for (;;) { |
| 159 | if (prev->next == node && |
| 160 | cmpxchg(&prev->next, node, NULL) == node) |
| 161 | break; |
| 162 | |
| 163 | /* |
| 164 | * We can only fail the cmpxchg() racing against an unlock(), |
| 165 | * in which case we should observe @node->locked becomming |
| 166 | * true. |
| 167 | */ |
| 168 | if (smp_load_acquire(&node->locked)) |
| 169 | return true; |
| 170 | |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 171 | cpu_relax_lowlatency(); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Or we race against a concurrent unqueue()'s step-B, in which |
| 175 | * case its step-C will write us a new @node->prev pointer. |
| 176 | */ |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 177 | prev = READ_ONCE(node->prev); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Step - B -- stabilize @next |
| 182 | * |
| 183 | * Similar to unlock(), wait for @node->next or move @lock from @node |
| 184 | * back to @prev. |
| 185 | */ |
| 186 | |
| 187 | next = osq_wait_next(lock, node, prev); |
| 188 | if (!next) |
| 189 | return false; |
| 190 | |
| 191 | /* |
| 192 | * Step - C -- unlink |
| 193 | * |
| 194 | * @prev is stable because its still waiting for a new @prev->next |
| 195 | * pointer, @next is stable because our @node->next pointer is NULL and |
| 196 | * it will wait in Step-A. |
| 197 | */ |
| 198 | |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 199 | WRITE_ONCE(next->prev, prev); |
| 200 | WRITE_ONCE(prev->next, next); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 205 | void osq_unlock(struct optimistic_spin_queue *lock) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 206 | { |
Jason Low | 33ecd20 | 2014-07-14 10:27:51 -0700 | [diff] [blame] | 207 | struct optimistic_spin_node *node, *next; |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 208 | int curr = encode_cpu(smp_processor_id()); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Fast path for the uncontended case. |
| 212 | */ |
Davidlohr Bueso | c55a6ff | 2015-09-14 00:37:24 -0700 | [diff] [blame] | 213 | if (likely(atomic_cmpxchg_release(&lock->tail, curr, |
| 214 | OSQ_UNLOCKED_VAL) == curr)) |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 215 | return; |
| 216 | |
| 217 | /* |
| 218 | * Second most likely case. |
| 219 | */ |
Jason Low | 33ecd20 | 2014-07-14 10:27:51 -0700 | [diff] [blame] | 220 | node = this_cpu_ptr(&osq_node); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 221 | next = xchg(&node->next, NULL); |
| 222 | if (next) { |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 223 | WRITE_ONCE(next->locked, 1); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
| 227 | next = osq_wait_next(lock, node, NULL); |
| 228 | if (next) |
Davidlohr Bueso | 4d3199e | 2015-02-22 19:31:41 -0800 | [diff] [blame] | 229 | WRITE_ONCE(next->locked, 1); |
Peter Zijlstra | fb0527b | 2014-01-29 12:51:42 +0100 | [diff] [blame] | 230 | } |