mm/hmm.c: remove superfluous RCU protection around radix tree lookup
[linux/fpc-iii.git] / kernel / locking / rwsem.c
blob30465a2f2b6cf8e072ee797d1b58219f0799cb66
1 // SPDX-License-Identifier: GPL-2.0
2 /* kernel/rwsem.c: R/W semaphores, public implementation
4 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
6 */
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/sched/debug.h>
12 #include <linux/export.h>
13 #include <linux/rwsem.h>
14 #include <linux/atomic.h>
16 #include "rwsem.h"
19 * lock for reading
21 void __sched down_read(struct rw_semaphore *sem)
23 might_sleep();
24 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
26 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
27 rwsem_set_reader_owned(sem);
30 EXPORT_SYMBOL(down_read);
32 int __sched down_read_killable(struct rw_semaphore *sem)
34 might_sleep();
35 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
37 if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
38 rwsem_release(&sem->dep_map, 1, _RET_IP_);
39 return -EINTR;
42 rwsem_set_reader_owned(sem);
43 return 0;
46 EXPORT_SYMBOL(down_read_killable);
49 * trylock for reading -- returns 1 if successful, 0 if contention
51 int down_read_trylock(struct rw_semaphore *sem)
53 int ret = __down_read_trylock(sem);
55 if (ret == 1) {
56 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
57 rwsem_set_reader_owned(sem);
59 return ret;
62 EXPORT_SYMBOL(down_read_trylock);
65 * lock for writing
67 void __sched down_write(struct rw_semaphore *sem)
69 might_sleep();
70 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
72 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
73 rwsem_set_owner(sem);
76 EXPORT_SYMBOL(down_write);
79 * lock for writing
81 int __sched down_write_killable(struct rw_semaphore *sem)
83 might_sleep();
84 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
86 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
87 rwsem_release(&sem->dep_map, 1, _RET_IP_);
88 return -EINTR;
91 rwsem_set_owner(sem);
92 return 0;
95 EXPORT_SYMBOL(down_write_killable);
98 * trylock for writing -- returns 1 if successful, 0 if contention
100 int down_write_trylock(struct rw_semaphore *sem)
102 int ret = __down_write_trylock(sem);
104 if (ret == 1) {
105 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
106 rwsem_set_owner(sem);
109 return ret;
112 EXPORT_SYMBOL(down_write_trylock);
115 * release a read lock
117 void up_read(struct rw_semaphore *sem)
119 rwsem_release(&sem->dep_map, 1, _RET_IP_);
120 DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
122 __up_read(sem);
125 EXPORT_SYMBOL(up_read);
128 * release a write lock
130 void up_write(struct rw_semaphore *sem)
132 rwsem_release(&sem->dep_map, 1, _RET_IP_);
133 DEBUG_RWSEMS_WARN_ON(sem->owner != current);
135 rwsem_clear_owner(sem);
136 __up_write(sem);
139 EXPORT_SYMBOL(up_write);
142 * downgrade write lock to read lock
144 void downgrade_write(struct rw_semaphore *sem)
146 lock_downgrade(&sem->dep_map, _RET_IP_);
147 DEBUG_RWSEMS_WARN_ON(sem->owner != current);
149 rwsem_set_reader_owned(sem);
150 __downgrade_write(sem);
153 EXPORT_SYMBOL(downgrade_write);
155 #ifdef CONFIG_DEBUG_LOCK_ALLOC
157 void down_read_nested(struct rw_semaphore *sem, int subclass)
159 might_sleep();
160 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
162 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
163 rwsem_set_reader_owned(sem);
166 EXPORT_SYMBOL(down_read_nested);
168 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
170 might_sleep();
171 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
173 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
174 rwsem_set_owner(sem);
177 EXPORT_SYMBOL(_down_write_nest_lock);
179 void down_read_non_owner(struct rw_semaphore *sem)
181 might_sleep();
183 __down_read(sem);
186 EXPORT_SYMBOL(down_read_non_owner);
188 void down_write_nested(struct rw_semaphore *sem, int subclass)
190 might_sleep();
191 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
193 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
194 rwsem_set_owner(sem);
197 EXPORT_SYMBOL(down_write_nested);
199 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
201 might_sleep();
202 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
204 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
205 rwsem_release(&sem->dep_map, 1, _RET_IP_);
206 return -EINTR;
209 rwsem_set_owner(sem);
210 return 0;
213 EXPORT_SYMBOL(down_write_killable_nested);
215 void up_read_non_owner(struct rw_semaphore *sem)
217 DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
218 __up_read(sem);
221 EXPORT_SYMBOL(up_read_non_owner);
223 #endif