Linux 4.15.10
[linux/fpc-iii.git] / kernel / locking / rwsem.c
blobf549c552dbf1e376a412efc1cb7a95a7029f2f7a
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_);
121 __up_read(sem);
124 EXPORT_SYMBOL(up_read);
127 * release a write lock
129 void up_write(struct rw_semaphore *sem)
131 rwsem_release(&sem->dep_map, 1, _RET_IP_);
133 rwsem_clear_owner(sem);
134 __up_write(sem);
137 EXPORT_SYMBOL(up_write);
140 * downgrade write lock to read lock
142 void downgrade_write(struct rw_semaphore *sem)
144 lock_downgrade(&sem->dep_map, _RET_IP_);
146 rwsem_set_reader_owned(sem);
147 __downgrade_write(sem);
150 EXPORT_SYMBOL(downgrade_write);
152 #ifdef CONFIG_DEBUG_LOCK_ALLOC
154 void down_read_nested(struct rw_semaphore *sem, int subclass)
156 might_sleep();
157 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
159 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
160 rwsem_set_reader_owned(sem);
163 EXPORT_SYMBOL(down_read_nested);
165 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
167 might_sleep();
168 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
170 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
171 rwsem_set_owner(sem);
174 EXPORT_SYMBOL(_down_write_nest_lock);
176 void down_read_non_owner(struct rw_semaphore *sem)
178 might_sleep();
180 __down_read(sem);
183 EXPORT_SYMBOL(down_read_non_owner);
185 void down_write_nested(struct rw_semaphore *sem, int subclass)
187 might_sleep();
188 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
190 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
191 rwsem_set_owner(sem);
194 EXPORT_SYMBOL(down_write_nested);
196 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
198 might_sleep();
199 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
201 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
202 rwsem_release(&sem->dep_map, 1, _RET_IP_);
203 return -EINTR;
206 rwsem_set_owner(sem);
207 return 0;
210 EXPORT_SYMBOL(down_write_killable_nested);
212 void up_read_non_owner(struct rw_semaphore *sem)
214 __up_read(sem);
217 EXPORT_SYMBOL(up_read_non_owner);
219 #endif