Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 2 | /* kernel/rwsem.c: R/W semaphores, public implementation |
| 3 | * |
| 4 | * Written by David Howells (dhowells@redhat.com). |
| 5 | * Derived from asm-i386/semaphore.h |
| 6 | */ |
| 7 | |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/kernel.h> |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 10 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 11 | #include <linux/sched/debug.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 13 | #include <linux/rwsem.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 14 | #include <linux/atomic.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 15 | |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 16 | #include "rwsem.h" |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 17 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 18 | /* |
| 19 | * lock for reading |
| 20 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 21 | void __sched down_read(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 22 | { |
| 23 | might_sleep(); |
| 24 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 25 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 26 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 27 | rwsem_set_reader_owned(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | EXPORT_SYMBOL(down_read); |
| 31 | |
Kirill Tkhai | 76f8507 | 2017-09-29 19:06:38 +0300 | [diff] [blame] | 32 | int __sched down_read_killable(struct rw_semaphore *sem) |
| 33 | { |
| 34 | might_sleep(); |
| 35 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 36 | |
| 37 | if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) { |
| 38 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 39 | return -EINTR; |
| 40 | } |
| 41 | |
| 42 | rwsem_set_reader_owned(sem); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | EXPORT_SYMBOL(down_read_killable); |
| 47 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 48 | /* |
| 49 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 50 | */ |
| 51 | int down_read_trylock(struct rw_semaphore *sem) |
| 52 | { |
| 53 | int ret = __down_read_trylock(sem); |
| 54 | |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 55 | if (ret == 1) { |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 56 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 57 | rwsem_set_reader_owned(sem); |
| 58 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | EXPORT_SYMBOL(down_read_trylock); |
| 63 | |
| 64 | /* |
| 65 | * lock for writing |
| 66 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 67 | void __sched down_write(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 68 | { |
| 69 | might_sleep(); |
| 70 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 71 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 72 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 73 | rwsem_set_owner(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | EXPORT_SYMBOL(down_write); |
| 77 | |
| 78 | /* |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 79 | * lock for writing |
| 80 | */ |
| 81 | int __sched down_write_killable(struct rw_semaphore *sem) |
| 82 | { |
| 83 | might_sleep(); |
| 84 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 85 | |
| 86 | if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) { |
| 87 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 88 | return -EINTR; |
| 89 | } |
| 90 | |
| 91 | rwsem_set_owner(sem); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | EXPORT_SYMBOL(down_write_killable); |
| 96 | |
| 97 | /* |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 98 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 99 | */ |
| 100 | int down_write_trylock(struct rw_semaphore *sem) |
| 101 | { |
| 102 | int ret = __down_write_trylock(sem); |
| 103 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 104 | if (ret == 1) { |
Pavel Emelianov | 428e6ce | 2007-05-08 00:29:10 -0700 | [diff] [blame] | 105 | rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 106 | rwsem_set_owner(sem); |
| 107 | } |
| 108 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | EXPORT_SYMBOL(down_write_trylock); |
| 113 | |
| 114 | /* |
| 115 | * release a read lock |
| 116 | */ |
| 117 | void up_read(struct rw_semaphore *sem) |
| 118 | { |
| 119 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame^] | 120 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED)); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 121 | |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame^] | 122 | rwsem_clear_reader_owned(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 123 | __up_read(sem); |
| 124 | } |
| 125 | |
| 126 | EXPORT_SYMBOL(up_read); |
| 127 | |
| 128 | /* |
| 129 | * release a write lock |
| 130 | */ |
| 131 | void up_write(struct rw_semaphore *sem) |
| 132 | { |
| 133 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
Waiman Long | 5149cba | 2018-03-30 17:27:58 -0400 | [diff] [blame] | 134 | DEBUG_RWSEMS_WARN_ON(sem->owner != current); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 135 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 136 | rwsem_clear_owner(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 137 | __up_write(sem); |
| 138 | } |
| 139 | |
| 140 | EXPORT_SYMBOL(up_write); |
| 141 | |
| 142 | /* |
| 143 | * downgrade write lock to read lock |
| 144 | */ |
| 145 | void downgrade_write(struct rw_semaphore *sem) |
| 146 | { |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 147 | lock_downgrade(&sem->dep_map, _RET_IP_); |
Waiman Long | 5149cba | 2018-03-30 17:27:58 -0400 | [diff] [blame] | 148 | DEBUG_RWSEMS_WARN_ON(sem->owner != current); |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 149 | |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 150 | rwsem_set_reader_owned(sem); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 151 | __downgrade_write(sem); |
| 152 | } |
| 153 | |
| 154 | EXPORT_SYMBOL(downgrade_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 155 | |
| 156 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 157 | |
| 158 | void down_read_nested(struct rw_semaphore *sem, int subclass) |
| 159 | { |
| 160 | might_sleep(); |
| 161 | rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_); |
| 162 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 163 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 164 | rwsem_set_reader_owned(sem); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | EXPORT_SYMBOL(down_read_nested); |
| 168 | |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 169 | void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest) |
| 170 | { |
| 171 | might_sleep(); |
| 172 | rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_); |
| 173 | |
| 174 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 175 | rwsem_set_owner(sem); |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | EXPORT_SYMBOL(_down_write_nest_lock); |
| 179 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 180 | void down_read_non_owner(struct rw_semaphore *sem) |
| 181 | { |
| 182 | might_sleep(); |
| 183 | |
| 184 | __down_read(sem); |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame^] | 185 | __rwsem_set_reader_owned(sem, NULL); |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | EXPORT_SYMBOL(down_read_non_owner); |
| 189 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 190 | void down_write_nested(struct rw_semaphore *sem, int subclass) |
| 191 | { |
| 192 | might_sleep(); |
| 193 | rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_); |
| 194 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 195 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 196 | rwsem_set_owner(sem); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | EXPORT_SYMBOL(down_write_nested); |
| 200 | |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 201 | int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass) |
| 202 | { |
| 203 | might_sleep(); |
| 204 | rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_); |
| 205 | |
| 206 | if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) { |
| 207 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 208 | return -EINTR; |
| 209 | } |
| 210 | |
| 211 | rwsem_set_owner(sem); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | EXPORT_SYMBOL(down_write_killable_nested); |
| 216 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 217 | void up_read_non_owner(struct rw_semaphore *sem) |
| 218 | { |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame^] | 219 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED)); |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 220 | __up_read(sem); |
| 221 | } |
| 222 | |
| 223 | EXPORT_SYMBOL(up_read_non_owner); |
| 224 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 225 | #endif |