rwsem.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. {
  128. long oldcount, adjustment;
  129. adjustment = RWSEM_ACTIVE_WRITE_BIAS;
  130. if (list_is_singular(&sem->wait_list))
  131. adjustment -= RWSEM_WAITING_BIAS;
  132. try_again_write:
  133. oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
  134. if (!(oldcount & RWSEM_ACTIVE_MASK))
  135. return 1;
  136. /* some one grabbed the sem already */
  137. if (rwsem_atomic_update(-adjustment, sem) & RWSEM_ACTIVE_MASK)
  138. return 0;
  139. goto try_again_write;
  140. }
  141. /*
  142. * wait for the read lock to be granted
  143. */
  144. struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
  145. {
  146. signed long adjustment = -RWSEM_ACTIVE_READ_BIAS;
  147. struct rwsem_waiter waiter;
  148. struct task_struct *tsk = current;
  149. signed long count;
  150. /* set up my own style of waitqueue */
  151. waiter.task = tsk;
  152. waiter.type = RWSEM_WAITING_FOR_READ;
  153. get_task_struct(tsk);
  154. raw_spin_lock_irq(&sem->wait_lock);
  155. if (list_empty(&sem->wait_list))
  156. adjustment += RWSEM_WAITING_BIAS;
  157. list_add_tail(&waiter.list, &sem->wait_list);
  158. /* we're now waiting on the lock, but no longer actively locking */
  159. count = rwsem_atomic_update(adjustment, sem);
  160. /* If there are no active locks, wake the front queued process(es). */
  161. if (count == RWSEM_WAITING_BIAS)
  162. sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
  163. raw_spin_unlock_irq(&sem->wait_lock);
  164. /* wait to be given the lock */
  165. while (true) {
  166. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  167. if (!waiter.task)
  168. break;
  169. schedule();
  170. }
  171. tsk->state = TASK_RUNNING;
  172. return sem;
  173. }
  174. /*
  175. * wait until we successfully acquire the write lock
  176. */
  177. struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
  178. {
  179. signed long adjustment = -RWSEM_ACTIVE_WRITE_BIAS;
  180. struct rwsem_waiter waiter;
  181. struct task_struct *tsk = current;
  182. signed long count;
  183. /* set up my own style of waitqueue */
  184. waiter.task = tsk;
  185. waiter.type = RWSEM_WAITING_FOR_WRITE;
  186. raw_spin_lock_irq(&sem->wait_lock);
  187. if (list_empty(&sem->wait_list))
  188. adjustment += RWSEM_WAITING_BIAS;
  189. list_add_tail(&waiter.list, &sem->wait_list);
  190. /* we're now waiting on the lock, but no longer actively locking */
  191. count = rwsem_atomic_update(adjustment, sem);
  192. /* If there were already threads queued before us and there are no
  193. * active writers, the lock must be read owned; so we try to wake
  194. * any read locks that were queued ahead of us. */
  195. if (count > RWSEM_WAITING_BIAS &&
  196. adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
  197. sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
  198. /* wait until we successfully acquire the lock */
  199. while (true) {
  200. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  201. if (try_get_writer_sem(sem))
  202. break;
  203. raw_spin_unlock_irq(&sem->wait_lock);
  204. schedule();
  205. raw_spin_lock_irq(&sem->wait_lock);
  206. }
  207. list_del(&waiter.list);
  208. raw_spin_unlock_irq(&sem->wait_lock);
  209. tsk->state = TASK_RUNNING;
  210. return sem;
  211. }
  212. /*
  213. * handle waking up a waiter on the semaphore
  214. * - up_read/up_write has decremented the active part of count if we come here
  215. */
  216. struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
  217. {
  218. unsigned long flags;
  219. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  220. /* do nothing if list empty */
  221. if (!list_empty(&sem->wait_list))
  222. sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
  223. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  224. return sem;
  225. }
  226. /*
  227. * downgrade a write lock into a read lock
  228. * - caller incremented waiting part of count and discovered it still negative
  229. * - just wake up any readers at the front of the queue
  230. */
  231. struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
  232. {
  233. unsigned long flags;
  234. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  235. /* do nothing if list empty */
  236. if (!list_empty(&sem->wait_list))
  237. sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
  238. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  239. return sem;
  240. }
  241. EXPORT_SYMBOL(rwsem_down_read_failed);
  242. EXPORT_SYMBOL(rwsem_down_write_failed);
  243. EXPORT_SYMBOL(rwsem_wake);
  244. EXPORT_SYMBOL(rwsem_downgrade_wake);