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