rwsem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* kernel/rwsem.c: R/W semaphores, public implementation
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. * Derived from asm-i386/semaphore.h
  5. */
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/export.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/atomic.h>
  13. #include "rwsem.h"
  14. /*
  15. * lock for reading
  16. */
  17. void __sched down_read(struct rw_semaphore *sem)
  18. {
  19. might_sleep();
  20. rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
  21. LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
  22. rwsem_set_reader_owned(sem);
  23. }
  24. EXPORT_SYMBOL(down_read);
  25. /*
  26. * trylock for reading -- returns 1 if successful, 0 if contention
  27. */
  28. int down_read_trylock(struct rw_semaphore *sem)
  29. {
  30. int ret = __down_read_trylock(sem);
  31. if (ret == 1) {
  32. rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
  33. rwsem_set_reader_owned(sem);
  34. }
  35. return ret;
  36. }
  37. EXPORT_SYMBOL(down_read_trylock);
  38. /*
  39. * lock for writing
  40. */
  41. void __sched down_write(struct rw_semaphore *sem)
  42. {
  43. might_sleep();
  44. rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
  45. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  46. rwsem_set_owner(sem);
  47. }
  48. EXPORT_SYMBOL(down_write);
  49. /*
  50. * lock for writing
  51. */
  52. int __sched down_write_killable(struct rw_semaphore *sem)
  53. {
  54. might_sleep();
  55. rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
  56. if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
  57. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  58. return -EINTR;
  59. }
  60. rwsem_set_owner(sem);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(down_write_killable);
  64. /*
  65. * trylock for writing -- returns 1 if successful, 0 if contention
  66. */
  67. int down_write_trylock(struct rw_semaphore *sem)
  68. {
  69. int ret = __down_write_trylock(sem);
  70. if (ret == 1) {
  71. rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
  72. rwsem_set_owner(sem);
  73. }
  74. return ret;
  75. }
  76. EXPORT_SYMBOL(down_write_trylock);
  77. /*
  78. * release a read lock
  79. */
  80. void up_read(struct rw_semaphore *sem)
  81. {
  82. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  83. __up_read(sem);
  84. }
  85. EXPORT_SYMBOL(up_read);
  86. /*
  87. * release a write lock
  88. */
  89. void up_write(struct rw_semaphore *sem)
  90. {
  91. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  92. rwsem_clear_owner(sem);
  93. __up_write(sem);
  94. }
  95. EXPORT_SYMBOL(up_write);
  96. /*
  97. * downgrade write lock to read lock
  98. */
  99. void downgrade_write(struct rw_semaphore *sem)
  100. {
  101. lock_downgrade(&sem->dep_map, _RET_IP_);
  102. rwsem_set_reader_owned(sem);
  103. __downgrade_write(sem);
  104. }
  105. EXPORT_SYMBOL(downgrade_write);
  106. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  107. void down_read_nested(struct rw_semaphore *sem, int subclass)
  108. {
  109. might_sleep();
  110. rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
  111. LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
  112. rwsem_set_reader_owned(sem);
  113. }
  114. EXPORT_SYMBOL(down_read_nested);
  115. void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
  116. {
  117. might_sleep();
  118. rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
  119. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  120. rwsem_set_owner(sem);
  121. }
  122. EXPORT_SYMBOL(_down_write_nest_lock);
  123. void down_read_non_owner(struct rw_semaphore *sem)
  124. {
  125. might_sleep();
  126. __down_read(sem);
  127. }
  128. EXPORT_SYMBOL(down_read_non_owner);
  129. void down_write_nested(struct rw_semaphore *sem, int subclass)
  130. {
  131. might_sleep();
  132. rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
  133. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  134. rwsem_set_owner(sem);
  135. }
  136. EXPORT_SYMBOL(down_write_nested);
  137. int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
  138. {
  139. might_sleep();
  140. rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
  141. if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
  142. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  143. return -EINTR;
  144. }
  145. rwsem_set_owner(sem);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(down_write_killable_nested);
  149. void up_read_non_owner(struct rw_semaphore *sem)
  150. {
  151. __up_read(sem);
  152. }
  153. EXPORT_SYMBOL(up_read_non_owner);
  154. #endif