rwsem.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <linux/rwsem.h>
  7. #include <linux/sched.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. /*
  11. * Initialize an rwsem:
  12. */
  13. void __init_rwsem(struct rw_semaphore *sem, const char *name,
  14. struct lock_class_key *key)
  15. {
  16. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  17. /*
  18. * Make sure we are not reinitializing a held semaphore:
  19. */
  20. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  21. lockdep_init_map(&sem->dep_map, name, key, 0);
  22. #endif
  23. sem->count = RWSEM_UNLOCKED_VALUE;
  24. spin_lock_init(&sem->wait_lock);
  25. INIT_LIST_HEAD(&sem->wait_list);
  26. }
  27. EXPORT_SYMBOL(__init_rwsem);
  28. struct rwsem_waiter {
  29. struct list_head list;
  30. struct task_struct *task;
  31. unsigned int flags;
  32. #define RWSEM_WAITING_FOR_READ 0x00000001
  33. #define RWSEM_WAITING_FOR_WRITE 0x00000002
  34. };
  35. /* Wake types for __rwsem_do_wake(). Note that RWSEM_WAKE_NO_ACTIVE and
  36. * RWSEM_WAKE_READ_OWNED imply that the spinlock must have been kept held
  37. * since the rwsem value was observed.
  38. */
  39. #define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */
  40. #define RWSEM_WAKE_NO_ACTIVE 1 /* rwsem was observed with no active thread */
  41. #define RWSEM_WAKE_READ_OWNED 2 /* rwsem was observed to be read owned */
  42. /*
  43. * handle the lock release when processes blocked on it that can now run
  44. * - if we come here from up_xxxx(), then:
  45. * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
  46. * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
  47. * - there must be someone on the queue
  48. * - the spinlock must be held by the caller
  49. * - woken process blocks are discarded from the list after having task zeroed
  50. * - writers are only woken if downgrading is false
  51. */
  52. static struct rw_semaphore *
  53. __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
  54. {
  55. struct rwsem_waiter *waiter;
  56. struct task_struct *tsk;
  57. struct list_head *next;
  58. signed long oldcount, woken, loop;
  59. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  60. if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE))
  61. goto readers_only;
  62. if (wake_type == RWSEM_WAKE_READ_OWNED)
  63. goto out;
  64. /* There's a writer at the front of the queue - try to grant it the
  65. * write lock. However, we only wake this writer if we can transition
  66. * the active part of the count from 0 -> 1
  67. */
  68. try_again_write:
  69. oldcount = rwsem_atomic_update(RWSEM_ACTIVE_BIAS, sem)
  70. - RWSEM_ACTIVE_BIAS;
  71. if (oldcount & RWSEM_ACTIVE_MASK)
  72. /* Someone grabbed the sem already */
  73. goto undo_write;
  74. /* We must be careful not to touch 'waiter' after we set ->task = NULL.
  75. * It is an allocated on the waiter's stack and may become invalid at
  76. * any time after that point (due to a wakeup from another source).
  77. */
  78. list_del(&waiter->list);
  79. tsk = waiter->task;
  80. smp_mb();
  81. waiter->task = NULL;
  82. wake_up_process(tsk);
  83. put_task_struct(tsk);
  84. goto out;
  85. readers_only:
  86. /* If we come here from up_xxxx(), another thread might have reached
  87. * rwsem_down_failed_common() before we acquired the spinlock and
  88. * woken up a waiter, making it now active. We prefer to check for
  89. * this first in order to not spend too much time with the spinlock
  90. * held if we're not going to be able to wake up readers in the end.
  91. *
  92. * Note that we do not need to update the rwsem count: any writer
  93. * trying to acquire rwsem will run rwsem_down_write_failed() due
  94. * to the waiting threads and block trying to acquire the spinlock.
  95. *
  96. * We use a dummy atomic update in order to acquire the cache line
  97. * exclusively since we expect to succeed and run the final rwsem
  98. * count adjustment pretty soon.
  99. */
  100. if (wake_type == RWSEM_WAKE_ANY &&
  101. (rwsem_atomic_update(0, sem) & RWSEM_ACTIVE_MASK))
  102. /* Someone grabbed the sem already */
  103. goto out;
  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.
  107. */
  108. woken = 0;
  109. do {
  110. woken++;
  111. if (waiter->list.next == &sem->wait_list)
  112. break;
  113. waiter = list_entry(waiter->list.next,
  114. struct rwsem_waiter, list);
  115. } while (waiter->flags & RWSEM_WAITING_FOR_READ);
  116. loop = woken;
  117. woken *= RWSEM_ACTIVE_BIAS - RWSEM_WAITING_BIAS;
  118. rwsem_atomic_add(woken, sem);
  119. next = sem->wait_list.next;
  120. for (; loop > 0; loop--) {
  121. waiter = list_entry(next, struct rwsem_waiter, list);
  122. next = waiter->list.next;
  123. tsk = waiter->task;
  124. smp_mb();
  125. waiter->task = NULL;
  126. wake_up_process(tsk);
  127. put_task_struct(tsk);
  128. }
  129. sem->wait_list.next = next;
  130. next->prev = &sem->wait_list;
  131. out:
  132. return sem;
  133. /* undo the change to the active count, but check for a transition
  134. * 1->0 */
  135. undo_write:
  136. if (rwsem_atomic_update(-RWSEM_ACTIVE_BIAS, sem) & RWSEM_ACTIVE_MASK)
  137. goto out;
  138. goto try_again_write;
  139. }
  140. /*
  141. * wait for a lock to be granted
  142. */
  143. static struct rw_semaphore __sched *
  144. rwsem_down_failed_common(struct rw_semaphore *sem,
  145. struct rwsem_waiter *waiter, signed long adjustment)
  146. {
  147. struct task_struct *tsk = current;
  148. signed long count;
  149. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  150. /* set up my own style of waitqueue */
  151. spin_lock_irq(&sem->wait_lock);
  152. waiter->task = tsk;
  153. get_task_struct(tsk);
  154. list_add_tail(&waiter->list, &sem->wait_list);
  155. /* we're now waiting on the lock, but no longer actively locking */
  156. count = rwsem_atomic_update(adjustment, sem);
  157. /* if there are no active locks, wake the front queued process(es) up */
  158. if (!(count & RWSEM_ACTIVE_MASK))
  159. sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
  160. spin_unlock_irq(&sem->wait_lock);
  161. /* wait to be given the lock */
  162. for (;;) {
  163. if (!waiter->task)
  164. break;
  165. schedule();
  166. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  167. }
  168. tsk->state = TASK_RUNNING;
  169. return sem;
  170. }
  171. /*
  172. * wait for the read lock to be granted
  173. */
  174. asmregparm struct rw_semaphore __sched *
  175. rwsem_down_read_failed(struct rw_semaphore *sem)
  176. {
  177. struct rwsem_waiter waiter;
  178. waiter.flags = RWSEM_WAITING_FOR_READ;
  179. rwsem_down_failed_common(sem, &waiter,
  180. RWSEM_WAITING_BIAS - RWSEM_ACTIVE_BIAS);
  181. return sem;
  182. }
  183. /*
  184. * wait for the write lock to be granted
  185. */
  186. asmregparm struct rw_semaphore __sched *
  187. rwsem_down_write_failed(struct rw_semaphore *sem)
  188. {
  189. struct rwsem_waiter waiter;
  190. waiter.flags = RWSEM_WAITING_FOR_WRITE;
  191. rwsem_down_failed_common(sem, &waiter, -RWSEM_ACTIVE_BIAS);
  192. return sem;
  193. }
  194. /*
  195. * handle waking up a waiter on the semaphore
  196. * - up_read/up_write has decremented the active part of count if we come here
  197. */
  198. asmregparm struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
  199. {
  200. unsigned long flags;
  201. spin_lock_irqsave(&sem->wait_lock, flags);
  202. /* do nothing if list empty */
  203. if (!list_empty(&sem->wait_list))
  204. sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
  205. spin_unlock_irqrestore(&sem->wait_lock, flags);
  206. return sem;
  207. }
  208. /*
  209. * downgrade a write lock into a read lock
  210. * - caller incremented waiting part of count and discovered it still negative
  211. * - just wake up any readers at the front of the queue
  212. */
  213. asmregparm struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&sem->wait_lock, flags);
  217. /* do nothing if list empty */
  218. if (!list_empty(&sem->wait_list))
  219. sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
  220. spin_unlock_irqrestore(&sem->wait_lock, flags);
  221. return sem;
  222. }
  223. EXPORT_SYMBOL(rwsem_down_read_failed);
  224. EXPORT_SYMBOL(rwsem_down_write_failed);
  225. EXPORT_SYMBOL(rwsem_wake);
  226. EXPORT_SYMBOL(rwsem_downgrade_wake);