rwsem.c 9.8 KB

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