rwsem-spinlock.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* rwsem-spinlock.c: R/W semaphores: contention handling functions for
  2. * generic spinlock implementation
  3. *
  4. * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  5. * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  6. * - Derived also from comments by Linus
  7. */
  8. #include <linux/rwsem.h>
  9. #include <linux/sched.h>
  10. #include <linux/export.h>
  11. struct rwsem_waiter {
  12. struct list_head list;
  13. struct task_struct *task;
  14. unsigned int flags;
  15. #define RWSEM_WAITING_FOR_READ 0x00000001
  16. #define RWSEM_WAITING_FOR_WRITE 0x00000002
  17. };
  18. int rwsem_is_locked(struct rw_semaphore *sem)
  19. {
  20. int ret = 1;
  21. unsigned long flags;
  22. if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
  23. ret = (sem->activity != 0);
  24. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  25. }
  26. return ret;
  27. }
  28. EXPORT_SYMBOL(rwsem_is_locked);
  29. /*
  30. * initialise the semaphore
  31. */
  32. void __init_rwsem(struct rw_semaphore *sem, const char *name,
  33. struct lock_class_key *key)
  34. {
  35. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  36. /*
  37. * Make sure we are not reinitializing a held semaphore:
  38. */
  39. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  40. lockdep_init_map(&sem->dep_map, name, key, 0);
  41. #endif
  42. sem->activity = 0;
  43. raw_spin_lock_init(&sem->wait_lock);
  44. INIT_LIST_HEAD(&sem->wait_list);
  45. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  46. sem->owner = NULL;
  47. sem->owner_pid = 0;
  48. sem->owner_comm[0] = 'N';
  49. sem->owner_comm[1] = 'U';
  50. sem->owner_comm[2] = 'L';
  51. sem->owner_comm[3] = 'L';
  52. sem->caller = NULL;
  53. #endif
  54. }
  55. EXPORT_SYMBOL(__init_rwsem);
  56. /*
  57. * handle the lock release when processes blocked on it that can now run
  58. * - if we come here, then:
  59. * - the 'active count' _reached_ zero
  60. * - the 'waiting count' is non-zero
  61. * - the spinlock must be held by the caller
  62. * - woken process blocks are discarded from the list after having task zeroed
  63. * - writers are only woken if wakewrite is non-zero
  64. */
  65. static inline struct rw_semaphore *
  66. __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
  67. {
  68. struct rwsem_waiter *waiter;
  69. struct task_struct *tsk;
  70. int woken;
  71. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  72. if (!wakewrite) {
  73. if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
  74. goto out;
  75. goto dont_wake_writers;
  76. }
  77. /* if we are allowed to wake writers try to grant a single write lock
  78. * if there's a writer at the front of the queue
  79. * - we leave the 'waiting count' incremented to signify potential
  80. * contention
  81. */
  82. if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
  83. sem->activity = -1;
  84. list_del(&waiter->list);
  85. tsk = waiter->task;
  86. /* Don't touch waiter after ->task has been NULLed */
  87. smp_mb();
  88. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  89. sem->owner = waiter->task;
  90. sem->owner_pid = waiter->task->pid;
  91. memcpy(sem->owner_comm ,waiter->task->comm,16);
  92. sem->caller = __builtin_return_address(0);
  93. #endif
  94. waiter->task = NULL;
  95. wake_up_process(tsk);
  96. put_task_struct(tsk);
  97. goto out;
  98. }
  99. /* grant an infinite number of read locks to the front of the queue */
  100. dont_wake_writers:
  101. woken = 0;
  102. while (waiter->flags & RWSEM_WAITING_FOR_READ) {
  103. struct list_head *next = waiter->list.next;
  104. list_del(&waiter->list);
  105. tsk = waiter->task;
  106. smp_mb();
  107. waiter->task = NULL;
  108. wake_up_process(tsk);
  109. put_task_struct(tsk);
  110. woken++;
  111. if (list_empty(&sem->wait_list))
  112. break;
  113. waiter = list_entry(next, struct rwsem_waiter, list);
  114. }
  115. sem->activity += woken;
  116. out:
  117. return sem;
  118. }
  119. /*
  120. * wake a single writer
  121. */
  122. static inline struct rw_semaphore *
  123. __rwsem_wake_one_writer(struct rw_semaphore *sem)
  124. {
  125. struct rwsem_waiter *waiter;
  126. struct task_struct *tsk;
  127. sem->activity = -1;
  128. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  129. list_del(&waiter->list);
  130. tsk = waiter->task;
  131. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  132. sem->owner = waiter->task;
  133. sem->owner_pid = waiter->task->pid;
  134. memcpy(sem->owner_comm ,waiter->task->comm,16);
  135. sem->caller = __builtin_return_address(0);
  136. #endif
  137. smp_mb();
  138. waiter->task = NULL;
  139. wake_up_process(tsk);
  140. put_task_struct(tsk);
  141. return sem;
  142. }
  143. /*
  144. * get a read lock on the semaphore
  145. */
  146. void __sched __down_read(struct rw_semaphore *sem)
  147. {
  148. struct rwsem_waiter waiter;
  149. struct task_struct *tsk;
  150. unsigned long flags;
  151. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  152. if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
  153. /* granted */
  154. sem->activity++;
  155. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  156. goto out;
  157. }
  158. tsk = current;
  159. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  160. /* set up my own style of waitqueue */
  161. waiter.task = tsk;
  162. waiter.flags = RWSEM_WAITING_FOR_READ;
  163. get_task_struct(tsk);
  164. list_add_tail(&waiter.list, &sem->wait_list);
  165. /* we don't need to touch the semaphore struct anymore */
  166. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  167. /* wait to be given the lock */
  168. for (;;) {
  169. if (!waiter.task)
  170. break;
  171. schedule();
  172. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  173. }
  174. tsk->state = TASK_RUNNING;
  175. out:
  176. ;
  177. }
  178. /*
  179. * trylock for reading -- returns 1 if successful, 0 if contention
  180. */
  181. int __down_read_trylock(struct rw_semaphore *sem)
  182. {
  183. unsigned long flags;
  184. int ret = 0;
  185. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  186. if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
  187. /* granted */
  188. sem->activity++;
  189. ret = 1;
  190. }
  191. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  192. return ret;
  193. }
  194. /*
  195. * get a write lock on the semaphore
  196. * - we increment the waiting count anyway to indicate an exclusive lock
  197. */
  198. void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
  199. {
  200. struct rwsem_waiter waiter;
  201. struct task_struct *tsk;
  202. unsigned long flags;
  203. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  204. if (sem->activity == 0 && list_empty(&sem->wait_list)) {
  205. /* granted */
  206. sem->activity = -1;
  207. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  208. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  209. sem->owner = current;
  210. sem->owner_pid = current->pid;
  211. memcpy(sem->owner_comm ,current->comm,16);
  212. sem->caller = __builtin_return_address(0);
  213. #endif
  214. goto out;
  215. }
  216. tsk = current;
  217. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  218. /* set up my own style of waitqueue */
  219. waiter.task = tsk;
  220. waiter.flags = RWSEM_WAITING_FOR_WRITE;
  221. get_task_struct(tsk);
  222. list_add_tail(&waiter.list, &sem->wait_list);
  223. /* we don't need to touch the semaphore struct anymore */
  224. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  225. /* wait to be given the lock */
  226. for (;;) {
  227. if (!waiter.task)
  228. break;
  229. schedule();
  230. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  231. }
  232. tsk->state = TASK_RUNNING;
  233. out:
  234. ;
  235. }
  236. void __sched __down_write(struct rw_semaphore *sem)
  237. {
  238. __down_write_nested(sem, 0);
  239. }
  240. /*
  241. * trylock for writing -- returns 1 if successful, 0 if contention
  242. */
  243. int __down_write_trylock(struct rw_semaphore *sem)
  244. {
  245. unsigned long flags;
  246. int ret = 0;
  247. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  248. if (sem->activity == 0 && list_empty(&sem->wait_list)) {
  249. /* granted */
  250. sem->activity = -1;
  251. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  252. sem->owner = current;
  253. sem->owner_pid = current->pid;
  254. memcpy(sem->owner_comm ,current->comm,16);
  255. sem->caller = __builtin_return_address(0);
  256. #endif
  257. ret = 1;
  258. }
  259. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  260. return ret;
  261. }
  262. /*
  263. * release a read lock on the semaphore
  264. */
  265. void __up_read(struct rw_semaphore *sem)
  266. {
  267. unsigned long flags;
  268. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  269. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  270. if(sem->activity == 0)
  271. panic("Reader is making sem as writer locked state by mistake");
  272. #endif
  273. if (--sem->activity == 0 && !list_empty(&sem->wait_list))
  274. sem = __rwsem_wake_one_writer(sem);
  275. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  276. }
  277. /*
  278. * release a write lock on the semaphore
  279. */
  280. void __up_write(struct rw_semaphore *sem)
  281. {
  282. unsigned long flags;
  283. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  284. sem->activity = 0;
  285. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  286. sem->owner = current;
  287. sem->owner_pid = current->pid;
  288. memcpy(sem->owner_comm ,current->comm,16);
  289. sem->caller = __builtin_return_address(0);
  290. #endif
  291. if (!list_empty(&sem->wait_list))
  292. sem = __rwsem_do_wake(sem, 1);
  293. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  294. }
  295. /*
  296. * downgrade a write lock into a read lock
  297. * - just wake up any readers at the front of the queue
  298. */
  299. void __downgrade_write(struct rw_semaphore *sem)
  300. {
  301. unsigned long flags;
  302. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  303. sem->activity = 1;
  304. #ifdef CONFIG_SEC_FORKHANG_DEBUG
  305. sem->owner = current;
  306. sem->owner_pid = current->pid;
  307. memcpy(sem->owner_comm ,current->comm,16);
  308. sem->caller = __builtin_return_address(0);
  309. #endif
  310. if (!list_empty(&sem->wait_list))
  311. sem = __rwsem_do_wake(sem, 0);
  312. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  313. }