rwsem.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. struct rwsem_waiter {
  11. struct list_head list;
  12. struct task_struct *task;
  13. unsigned int flags;
  14. #define RWSEM_WAITING_FOR_READ 0x00000001
  15. #define RWSEM_WAITING_FOR_WRITE 0x00000002
  16. };
  17. #if RWSEM_DEBUG
  18. #undef rwsemtrace
  19. void rwsemtrace(struct rw_semaphore *sem, const char *str)
  20. {
  21. printk("sem=%p\n", sem);
  22. printk("(sem)=%08lx\n", sem->count);
  23. if (sem->debug)
  24. printk("[%d] %s({%08lx})\n", current->pid, str, sem->count);
  25. }
  26. #endif
  27. /*
  28. * handle the lock release when processes blocked on it that can now run
  29. * - if we come here from up_xxxx(), then:
  30. * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
  31. * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
  32. * - there must be someone on the queue
  33. * - the spinlock must be held by the caller
  34. * - woken process blocks are discarded from the list after having task zeroed
  35. * - writers are only woken if downgrading is false
  36. */
  37. static inline struct rw_semaphore *
  38. __rwsem_do_wake(struct rw_semaphore *sem, int downgrading)
  39. {
  40. struct rwsem_waiter *waiter;
  41. struct task_struct *tsk;
  42. struct list_head *next;
  43. signed long oldcount, woken, loop;
  44. rwsemtrace(sem, "Entering __rwsem_do_wake");
  45. if (downgrading)
  46. goto dont_wake_writers;
  47. /* if we came through an up_xxxx() call, we only only wake someone up
  48. * if we can transition the active part of the count from 0 -> 1
  49. */
  50. try_again:
  51. oldcount = rwsem_atomic_update(RWSEM_ACTIVE_BIAS, sem)
  52. - RWSEM_ACTIVE_BIAS;
  53. if (oldcount & RWSEM_ACTIVE_MASK)
  54. goto undo;
  55. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  56. /* try to grant a single write lock if there's a writer at the front
  57. * of the queue - note we leave the 'active part' of the count
  58. * incremented by 1 and the waiting part incremented by 0x00010000
  59. */
  60. if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE))
  61. goto readers_only;
  62. /* We must be careful not to touch 'waiter' after we set ->task = NULL.
  63. * It is an allocated on the waiter's stack and may become invalid at
  64. * any time after that point (due to a wakeup from another source).
  65. */
  66. list_del(&waiter->list);
  67. tsk = waiter->task;
  68. smp_mb();
  69. waiter->task = NULL;
  70. wake_up_process(tsk);
  71. put_task_struct(tsk);
  72. goto out;
  73. /* don't want to wake any writers */
  74. dont_wake_writers:
  75. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  76. if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
  77. goto out;
  78. /* grant an infinite number of read locks to the readers at the front
  79. * of the queue
  80. * - note we increment the 'active part' of the count by the number of
  81. * readers before waking any processes up
  82. */
  83. readers_only:
  84. woken = 0;
  85. do {
  86. woken++;
  87. if (waiter->list.next == &sem->wait_list)
  88. break;
  89. waiter = list_entry(waiter->list.next,
  90. struct rwsem_waiter, list);
  91. } while (waiter->flags & RWSEM_WAITING_FOR_READ);
  92. loop = woken;
  93. woken *= RWSEM_ACTIVE_BIAS - RWSEM_WAITING_BIAS;
  94. if (!downgrading)
  95. /* we'd already done one increment earlier */
  96. woken -= RWSEM_ACTIVE_BIAS;
  97. rwsem_atomic_add(woken, sem);
  98. next = sem->wait_list.next;
  99. for (; loop > 0; loop--) {
  100. waiter = list_entry(next, struct rwsem_waiter, list);
  101. next = waiter->list.next;
  102. tsk = waiter->task;
  103. smp_mb();
  104. waiter->task = NULL;
  105. wake_up_process(tsk);
  106. put_task_struct(tsk);
  107. }
  108. sem->wait_list.next = next;
  109. next->prev = &sem->wait_list;
  110. out:
  111. rwsemtrace(sem, "Leaving __rwsem_do_wake");
  112. return sem;
  113. /* undo the change to count, but check for a transition 1->0 */
  114. undo:
  115. if (rwsem_atomic_update(-RWSEM_ACTIVE_BIAS, sem) != 0)
  116. goto out;
  117. goto try_again;
  118. }
  119. /*
  120. * wait for a lock to be granted
  121. */
  122. static inline struct rw_semaphore *
  123. rwsem_down_failed_common(struct rw_semaphore *sem,
  124. struct rwsem_waiter *waiter, signed long adjustment)
  125. {
  126. struct task_struct *tsk = current;
  127. signed long count;
  128. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  129. /* set up my own style of waitqueue */
  130. spin_lock_irq(&sem->wait_lock);
  131. waiter->task = tsk;
  132. get_task_struct(tsk);
  133. list_add_tail(&waiter->list, &sem->wait_list);
  134. /* we're now waiting on the lock, but no longer actively read-locking */
  135. count = rwsem_atomic_update(adjustment, sem);
  136. /* if there are no active locks, wake the front queued process(es) up */
  137. if (!(count & RWSEM_ACTIVE_MASK))
  138. sem = __rwsem_do_wake(sem, 0);
  139. spin_unlock_irq(&sem->wait_lock);
  140. /* wait to be given the lock */
  141. for (;;) {
  142. if (!waiter->task)
  143. break;
  144. schedule();
  145. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  146. }
  147. tsk->state = TASK_RUNNING;
  148. return sem;
  149. }
  150. /*
  151. * wait for the read lock to be granted
  152. */
  153. struct rw_semaphore fastcall __sched *
  154. rwsem_down_read_failed(struct rw_semaphore *sem)
  155. {
  156. struct rwsem_waiter waiter;
  157. rwsemtrace(sem, "Entering rwsem_down_read_failed");
  158. waiter.flags = RWSEM_WAITING_FOR_READ;
  159. rwsem_down_failed_common(sem, &waiter,
  160. RWSEM_WAITING_BIAS - RWSEM_ACTIVE_BIAS);
  161. rwsemtrace(sem, "Leaving rwsem_down_read_failed");
  162. return sem;
  163. }
  164. /*
  165. * wait for the write lock to be granted
  166. */
  167. struct rw_semaphore fastcall __sched *
  168. rwsem_down_write_failed(struct rw_semaphore *sem)
  169. {
  170. struct rwsem_waiter waiter;
  171. rwsemtrace(sem, "Entering rwsem_down_write_failed");
  172. waiter.flags = RWSEM_WAITING_FOR_WRITE;
  173. rwsem_down_failed_common(sem, &waiter, -RWSEM_ACTIVE_BIAS);
  174. rwsemtrace(sem, "Leaving rwsem_down_write_failed");
  175. return sem;
  176. }
  177. /*
  178. * handle waking up a waiter on the semaphore
  179. * - up_read/up_write has decremented the active part of count if we come here
  180. */
  181. struct rw_semaphore fastcall *rwsem_wake(struct rw_semaphore *sem)
  182. {
  183. unsigned long flags;
  184. rwsemtrace(sem, "Entering rwsem_wake");
  185. spin_lock_irqsave(&sem->wait_lock, flags);
  186. /* do nothing if list empty */
  187. if (!list_empty(&sem->wait_list))
  188. sem = __rwsem_do_wake(sem, 0);
  189. spin_unlock_irqrestore(&sem->wait_lock, flags);
  190. rwsemtrace(sem, "Leaving rwsem_wake");
  191. return sem;
  192. }
  193. /*
  194. * downgrade a write lock into a read lock
  195. * - caller incremented waiting part of count and discovered it still negative
  196. * - just wake up any readers at the front of the queue
  197. */
  198. struct rw_semaphore fastcall *rwsem_downgrade_wake(struct rw_semaphore *sem)
  199. {
  200. unsigned long flags;
  201. rwsemtrace(sem, "Entering rwsem_downgrade_wake");
  202. spin_lock_irqsave(&sem->wait_lock, flags);
  203. /* do nothing if list empty */
  204. if (!list_empty(&sem->wait_list))
  205. sem = __rwsem_do_wake(sem, 1);
  206. spin_unlock_irqrestore(&sem->wait_lock, flags);
  207. rwsemtrace(sem, "Leaving rwsem_downgrade_wake");
  208. return sem;
  209. }
  210. EXPORT_SYMBOL(rwsem_down_read_failed);
  211. EXPORT_SYMBOL(rwsem_down_write_failed);
  212. EXPORT_SYMBOL(rwsem_wake);
  213. EXPORT_SYMBOL(rwsem_downgrade_wake);
  214. #if RWSEM_DEBUG
  215. EXPORT_SYMBOL(rwsemtrace);
  216. #endif