Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 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 | |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/kernel.h> |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 9 | #include <linux/sched.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 11 | #include <linux/rwsem.h> |
| 12 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 14 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 15 | #if defined(CONFIG_SMP) && defined(CONFIG_RWSEM_XCHGADD_ALGORITHM) |
| 16 | static inline void rwsem_set_owner(struct rw_semaphore *sem) |
| 17 | { |
| 18 | sem->owner = current; |
| 19 | } |
| 20 | |
| 21 | static inline void rwsem_clear_owner(struct rw_semaphore *sem) |
| 22 | { |
| 23 | sem->owner = NULL; |
| 24 | } |
| 25 | |
| 26 | #else |
| 27 | static inline void rwsem_set_owner(struct rw_semaphore *sem) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | static inline void rwsem_clear_owner(struct rw_semaphore *sem) |
| 32 | { |
| 33 | } |
| 34 | #endif |
| 35 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 36 | /* |
| 37 | * lock for reading |
| 38 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 39 | void __sched down_read(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 40 | { |
| 41 | might_sleep(); |
| 42 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 43 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 44 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | EXPORT_SYMBOL(down_read); |
| 48 | |
| 49 | /* |
| 50 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 51 | */ |
| 52 | int down_read_trylock(struct rw_semaphore *sem) |
| 53 | { |
| 54 | int ret = __down_read_trylock(sem); |
| 55 | |
| 56 | if (ret == 1) |
| 57 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | EXPORT_SYMBOL(down_read_trylock); |
| 62 | |
| 63 | /* |
| 64 | * lock for writing |
| 65 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 66 | void __sched down_write(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 67 | { |
| 68 | might_sleep(); |
| 69 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 70 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 71 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 72 | rwsem_set_owner(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | EXPORT_SYMBOL(down_write); |
| 76 | |
| 77 | /* |
| 78 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 79 | */ |
| 80 | int down_write_trylock(struct rw_semaphore *sem) |
| 81 | { |
| 82 | int ret = __down_write_trylock(sem); |
| 83 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 84 | if (ret == 1) { |
Pavel Emelianov | 428e6ce | 2007-05-08 00:29:10 -0700 | [diff] [blame] | 85 | rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 86 | rwsem_set_owner(sem); |
| 87 | } |
| 88 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | EXPORT_SYMBOL(down_write_trylock); |
| 93 | |
| 94 | /* |
| 95 | * release a read lock |
| 96 | */ |
| 97 | void up_read(struct rw_semaphore *sem) |
| 98 | { |
| 99 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 100 | |
| 101 | __up_read(sem); |
| 102 | } |
| 103 | |
| 104 | EXPORT_SYMBOL(up_read); |
| 105 | |
| 106 | /* |
| 107 | * release a write lock |
| 108 | */ |
| 109 | void up_write(struct rw_semaphore *sem) |
| 110 | { |
| 111 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 112 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 113 | rwsem_clear_owner(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 114 | __up_write(sem); |
| 115 | } |
| 116 | |
| 117 | EXPORT_SYMBOL(up_write); |
| 118 | |
| 119 | /* |
| 120 | * downgrade write lock to read lock |
| 121 | */ |
| 122 | void downgrade_write(struct rw_semaphore *sem) |
| 123 | { |
| 124 | /* |
| 125 | * lockdep: a downgraded write will live on as a write |
| 126 | * dependency. |
| 127 | */ |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 128 | rwsem_clear_owner(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 129 | __downgrade_write(sem); |
| 130 | } |
| 131 | |
| 132 | EXPORT_SYMBOL(downgrade_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 133 | |
| 134 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 135 | |
| 136 | void down_read_nested(struct rw_semaphore *sem, int subclass) |
| 137 | { |
| 138 | might_sleep(); |
| 139 | rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_); |
| 140 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 141 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | EXPORT_SYMBOL(down_read_nested); |
| 145 | |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 146 | void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest) |
| 147 | { |
| 148 | might_sleep(); |
| 149 | rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_); |
| 150 | |
| 151 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 152 | rwsem_set_owner(sem); |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | EXPORT_SYMBOL(_down_write_nest_lock); |
| 156 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 157 | void down_read_non_owner(struct rw_semaphore *sem) |
| 158 | { |
| 159 | might_sleep(); |
| 160 | |
| 161 | __down_read(sem); |
| 162 | } |
| 163 | |
| 164 | EXPORT_SYMBOL(down_read_non_owner); |
| 165 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 166 | void down_write_nested(struct rw_semaphore *sem, int subclass) |
| 167 | { |
| 168 | might_sleep(); |
| 169 | rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_); |
| 170 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 171 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame^] | 172 | rwsem_set_owner(sem); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | EXPORT_SYMBOL(down_write_nested); |
| 176 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 177 | void up_read_non_owner(struct rw_semaphore *sem) |
| 178 | { |
| 179 | __up_read(sem); |
| 180 | } |
| 181 | |
| 182 | EXPORT_SYMBOL(up_read_non_owner); |
| 183 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | |