Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* rwsem.c: R/W semaphores: contention handling functions |
2 | * | |
3 | * Written by David Howells (dhowells@redhat.com). | |
4 | * Derived from arch/i386/kernel/semaphore.c | |
ce6711f3 AS |
5 | * |
6 | * Writer lock-stealing by Alex Shi <alex.shi@intel.com> | |
1da177e4 LT |
7 | */ |
8 | #include <linux/rwsem.h> | |
9 | #include <linux/sched.h> | |
10 | #include <linux/init.h> | |
8bc3bcc9 | 11 | #include <linux/export.h> |
1da177e4 | 12 | |
4ea2176d IM |
13 | /* |
14 | * Initialize an rwsem: | |
15 | */ | |
16 | void __init_rwsem(struct rw_semaphore *sem, const char *name, | |
17 | struct lock_class_key *key) | |
18 | { | |
19 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
20 | /* | |
21 | * Make sure we are not reinitializing a held semaphore: | |
22 | */ | |
23 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); | |
4dfbb9d8 | 24 | lockdep_init_map(&sem->dep_map, name, key, 0); |
4ea2176d IM |
25 | #endif |
26 | sem->count = RWSEM_UNLOCKED_VALUE; | |
ddb6c9b5 | 27 | raw_spin_lock_init(&sem->wait_lock); |
4ea2176d IM |
28 | INIT_LIST_HEAD(&sem->wait_list); |
29 | } | |
30 | ||
31 | EXPORT_SYMBOL(__init_rwsem); | |
32 | ||
e2d57f78 ML |
33 | enum rwsem_waiter_type { |
34 | RWSEM_WAITING_FOR_WRITE, | |
35 | RWSEM_WAITING_FOR_READ | |
36 | }; | |
37 | ||
1da177e4 LT |
38 | struct rwsem_waiter { |
39 | struct list_head list; | |
40 | struct task_struct *task; | |
e2d57f78 | 41 | enum rwsem_waiter_type type; |
1da177e4 LT |
42 | }; |
43 | ||
70bdc6e0 ML |
44 | /* Wake types for __rwsem_do_wake(). Note that RWSEM_WAKE_NO_ACTIVE and |
45 | * RWSEM_WAKE_READ_OWNED imply that the spinlock must have been kept held | |
46 | * since the rwsem value was observed. | |
47 | */ | |
48 | #define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */ | |
49 | #define RWSEM_WAKE_NO_ACTIVE 1 /* rwsem was observed with no active thread */ | |
50 | #define RWSEM_WAKE_READ_OWNED 2 /* rwsem was observed to be read owned */ | |
51 | ||
1da177e4 LT |
52 | /* |
53 | * handle the lock release when processes blocked on it that can now run | |
54 | * - if we come here from up_xxxx(), then: | |
55 | * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed) | |
56 | * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so) | |
345af7bf | 57 | * - there must be someone on the queue |
1da177e4 LT |
58 | * - the spinlock must be held by the caller |
59 | * - woken process blocks are discarded from the list after having task zeroed | |
60 | * - writers are only woken if downgrading is false | |
61 | */ | |
70bdc6e0 ML |
62 | static struct rw_semaphore * |
63 | __rwsem_do_wake(struct rw_semaphore *sem, int wake_type) | |
1da177e4 LT |
64 | { |
65 | struct rwsem_waiter *waiter; | |
66 | struct task_struct *tsk; | |
67 | struct list_head *next; | |
ce6711f3 | 68 | signed long woken, loop, adjustment; |
1da177e4 | 69 | |
345af7bf | 70 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
e2d57f78 | 71 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
345af7bf ML |
72 | goto readers_only; |
73 | ||
70bdc6e0 | 74 | if (wake_type == RWSEM_WAKE_READ_OWNED) |
424acaae ML |
75 | /* Another active reader was observed, so wakeup is not |
76 | * likely to succeed. Save the atomic op. | |
77 | */ | |
345af7bf | 78 | goto out; |
1da177e4 | 79 | |
ce6711f3 AS |
80 | /* Wake up the writing waiter and let the task grab the sem: */ |
81 | wake_up_process(waiter->task); | |
1da177e4 LT |
82 | goto out; |
83 | ||
345af7bf | 84 | readers_only: |
70bdc6e0 ML |
85 | /* If we come here from up_xxxx(), another thread might have reached |
86 | * rwsem_down_failed_common() before we acquired the spinlock and | |
87 | * woken up a waiter, making it now active. We prefer to check for | |
88 | * this first in order to not spend too much time with the spinlock | |
89 | * held if we're not going to be able to wake up readers in the end. | |
90 | * | |
91 | * Note that we do not need to update the rwsem count: any writer | |
92 | * trying to acquire rwsem will run rwsem_down_write_failed() due | |
93 | * to the waiting threads and block trying to acquire the spinlock. | |
94 | * | |
95 | * We use a dummy atomic update in order to acquire the cache line | |
96 | * exclusively since we expect to succeed and run the final rwsem | |
97 | * count adjustment pretty soon. | |
98 | */ | |
99 | if (wake_type == RWSEM_WAKE_ANY && | |
424acaae ML |
100 | rwsem_atomic_update(0, sem) < RWSEM_WAITING_BIAS) |
101 | /* Someone grabbed the sem for write already */ | |
70bdc6e0 | 102 | goto out; |
1da177e4 | 103 | |
345af7bf ML |
104 | /* Grant an infinite number of read locks to the readers at the front |
105 | * of the queue. Note we increment the 'active part' of the count by | |
106 | * the number of readers before waking any processes up. | |
1da177e4 | 107 | */ |
1da177e4 LT |
108 | woken = 0; |
109 | do { | |
110 | woken++; | |
111 | ||
112 | if (waiter->list.next == &sem->wait_list) | |
113 | break; | |
114 | ||
115 | waiter = list_entry(waiter->list.next, | |
116 | struct rwsem_waiter, list); | |
117 | ||
e2d57f78 | 118 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
1da177e4 | 119 | |
fd41b334 | 120 | adjustment = woken * RWSEM_ACTIVE_READ_BIAS; |
e2d57f78 | 121 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
fd41b334 ML |
122 | /* hit end of list above */ |
123 | adjustment -= RWSEM_WAITING_BIAS; | |
1da177e4 | 124 | |
fd41b334 | 125 | rwsem_atomic_add(adjustment, sem); |
1da177e4 LT |
126 | |
127 | next = sem->wait_list.next; | |
fd41b334 | 128 | for (loop = woken; loop > 0; loop--) { |
1da177e4 LT |
129 | waiter = list_entry(next, struct rwsem_waiter, list); |
130 | next = waiter->list.next; | |
131 | tsk = waiter->task; | |
d59dd462 | 132 | smp_mb(); |
1da177e4 LT |
133 | waiter->task = NULL; |
134 | wake_up_process(tsk); | |
135 | put_task_struct(tsk); | |
136 | } | |
137 | ||
138 | sem->wait_list.next = next; | |
139 | next->prev = &sem->wait_list; | |
140 | ||
141 | out: | |
1da177e4 | 142 | return sem; |
ce6711f3 AS |
143 | } |
144 | ||
145 | /* Try to get write sem, caller holds sem->wait_lock: */ | |
146 | static int try_get_writer_sem(struct rw_semaphore *sem, | |
147 | struct rwsem_waiter *waiter) | |
148 | { | |
149 | struct rwsem_waiter *fwaiter; | |
150 | long oldcount, adjustment; | |
1da177e4 | 151 | |
ce6711f3 AS |
152 | /* only steal when first waiter is writing */ |
153 | fwaiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); | |
e2d57f78 | 154 | if (fwaiter->type != RWSEM_WAITING_FOR_WRITE) |
ce6711f3 AS |
155 | return 0; |
156 | ||
157 | adjustment = RWSEM_ACTIVE_WRITE_BIAS; | |
158 | /* Only one waiter in the queue: */ | |
159 | if (fwaiter == waiter && waiter->list.next == &sem->wait_list) | |
160 | adjustment -= RWSEM_WAITING_BIAS; | |
161 | ||
162 | try_again_write: | |
163 | oldcount = rwsem_atomic_update(adjustment, sem) - adjustment; | |
023fe4f7 | 164 | if (!(oldcount & RWSEM_ACTIVE_MASK)) |
ce6711f3 | 165 | return 1; |
ce6711f3 | 166 | /* some one grabbed the sem already */ |
fd41b334 | 167 | if (rwsem_atomic_update(-adjustment, sem) & RWSEM_ACTIVE_MASK) |
ce6711f3 | 168 | return 0; |
345af7bf | 169 | goto try_again_write; |
1da177e4 LT |
170 | } |
171 | ||
172 | /* | |
1e78277c | 173 | * wait for the read lock to be granted |
1da177e4 | 174 | */ |
1e78277c | 175 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) |
1da177e4 | 176 | { |
1e78277c | 177 | signed long adjustment = -RWSEM_ACTIVE_READ_BIAS; |
a8618a0e | 178 | struct rwsem_waiter waiter; |
1da177e4 LT |
179 | struct task_struct *tsk = current; |
180 | signed long count; | |
181 | ||
1da177e4 | 182 | /* set up my own style of waitqueue */ |
a8618a0e | 183 | waiter.task = tsk; |
da16922c | 184 | waiter.type = RWSEM_WAITING_FOR_READ; |
1da177e4 LT |
185 | get_task_struct(tsk); |
186 | ||
f7dd1cee | 187 | raw_spin_lock_irq(&sem->wait_lock); |
fd41b334 ML |
188 | if (list_empty(&sem->wait_list)) |
189 | adjustment += RWSEM_WAITING_BIAS; | |
a8618a0e | 190 | list_add_tail(&waiter.list, &sem->wait_list); |
1da177e4 | 191 | |
70bdc6e0 | 192 | /* we're now waiting on the lock, but no longer actively locking */ |
1da177e4 LT |
193 | count = rwsem_atomic_update(adjustment, sem); |
194 | ||
da16922c | 195 | /* If there are no active locks, wake the front queued process(es). */ |
424acaae | 196 | if (count == RWSEM_WAITING_BIAS) |
70bdc6e0 | 197 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE); |
1da177e4 | 198 | |
ddb6c9b5 | 199 | raw_spin_unlock_irq(&sem->wait_lock); |
1da177e4 LT |
200 | |
201 | /* wait to be given the lock */ | |
f7dd1cee ML |
202 | while (true) { |
203 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); | |
a8618a0e | 204 | if (!waiter.task) |
1da177e4 LT |
205 | break; |
206 | schedule(); | |
1da177e4 LT |
207 | } |
208 | ||
209 | tsk->state = TASK_RUNNING; | |
210 | ||
211 | return sem; | |
212 | } | |
213 | ||
1da177e4 | 214 | /* |
023fe4f7 | 215 | * wait until we successfully acquire the write lock |
1da177e4 | 216 | */ |
d1233754 | 217 | struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem) |
1da177e4 | 218 | { |
1e78277c ML |
219 | signed long adjustment = -RWSEM_ACTIVE_WRITE_BIAS; |
220 | struct rwsem_waiter waiter; | |
221 | struct task_struct *tsk = current; | |
222 | signed long count; | |
223 | ||
224 | /* set up my own style of waitqueue */ | |
225 | waiter.task = tsk; | |
023fe4f7 | 226 | waiter.type = RWSEM_WAITING_FOR_WRITE; |
1e78277c ML |
227 | |
228 | raw_spin_lock_irq(&sem->wait_lock); | |
229 | if (list_empty(&sem->wait_list)) | |
230 | adjustment += RWSEM_WAITING_BIAS; | |
231 | list_add_tail(&waiter.list, &sem->wait_list); | |
232 | ||
233 | /* we're now waiting on the lock, but no longer actively locking */ | |
234 | count = rwsem_atomic_update(adjustment, sem); | |
235 | ||
236 | /* If there are no active locks, wake the front queued process(es) up. | |
237 | * | |
238 | * Alternatively, if we're called from a failed down_write(), there | |
239 | * were already threads queued before us and there are no active | |
240 | * writers, the lock must be read owned; so we try to wake any read | |
241 | * locks that were queued ahead of us. */ | |
242 | if (count == RWSEM_WAITING_BIAS) | |
243 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE); | |
244 | else if (count > RWSEM_WAITING_BIAS && | |
245 | adjustment == -RWSEM_ACTIVE_WRITE_BIAS) | |
246 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); | |
247 | ||
023fe4f7 | 248 | /* wait until we successfully acquire the lock */ |
1e78277c ML |
249 | while (true) { |
250 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); | |
023fe4f7 ML |
251 | |
252 | if (try_get_writer_sem(sem, &waiter)) | |
1e78277c ML |
253 | break; |
254 | ||
1e78277c ML |
255 | raw_spin_unlock_irq(&sem->wait_lock); |
256 | schedule(); | |
023fe4f7 | 257 | raw_spin_lock_irq(&sem->wait_lock); |
1e78277c ML |
258 | } |
259 | ||
023fe4f7 ML |
260 | list_del(&waiter.list); |
261 | raw_spin_unlock_irq(&sem->wait_lock); | |
1e78277c ML |
262 | tsk->state = TASK_RUNNING; |
263 | ||
264 | return sem; | |
1da177e4 LT |
265 | } |
266 | ||
267 | /* | |
268 | * handle waking up a waiter on the semaphore | |
269 | * - up_read/up_write has decremented the active part of count if we come here | |
270 | */ | |
d1233754 | 271 | struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) |
1da177e4 LT |
272 | { |
273 | unsigned long flags; | |
274 | ||
ddb6c9b5 | 275 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 LT |
276 | |
277 | /* do nothing if list empty */ | |
278 | if (!list_empty(&sem->wait_list)) | |
70bdc6e0 | 279 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
1da177e4 | 280 | |
ddb6c9b5 | 281 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 282 | |
1da177e4 LT |
283 | return sem; |
284 | } | |
285 | ||
286 | /* | |
287 | * downgrade a write lock into a read lock | |
288 | * - caller incremented waiting part of count and discovered it still negative | |
289 | * - just wake up any readers at the front of the queue | |
290 | */ | |
d1233754 | 291 | struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
1da177e4 LT |
292 | { |
293 | unsigned long flags; | |
294 | ||
ddb6c9b5 | 295 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 LT |
296 | |
297 | /* do nothing if list empty */ | |
298 | if (!list_empty(&sem->wait_list)) | |
70bdc6e0 | 299 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
1da177e4 | 300 | |
ddb6c9b5 | 301 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 302 | |
1da177e4 LT |
303 | return sem; |
304 | } | |
305 | ||
306 | EXPORT_SYMBOL(rwsem_down_read_failed); | |
307 | EXPORT_SYMBOL(rwsem_down_write_failed); | |
308 | EXPORT_SYMBOL(rwsem_wake); | |
309 | EXPORT_SYMBOL(rwsem_downgrade_wake); |