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
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>
21 void __sched
down_read(struct rw_semaphore
*sem
)
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
)
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_
);
42 rwsem_set_reader_owned(sem
);
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
);
56 rwsem_acquire_read(&sem
->dep_map
, 0, 1, _RET_IP_
);
57 rwsem_set_reader_owned(sem
);
62 EXPORT_SYMBOL(down_read_trylock
);
67 void __sched
down_write(struct rw_semaphore
*sem
)
70 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
72 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
76 EXPORT_SYMBOL(down_write
);
81 int __sched
down_write_killable(struct rw_semaphore
*sem
)
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_
);
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
);
105 rwsem_acquire(&sem
->dep_map
, 0, 1, _RET_IP_
);
106 rwsem_set_owner(sem
);
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_
);
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
);
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
)
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
)
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
)
183 EXPORT_SYMBOL(down_read_non_owner
);
185 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
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
)
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_
);
206 rwsem_set_owner(sem
);
210 EXPORT_SYMBOL(down_write_killable_nested
);
212 void up_read_non_owner(struct rw_semaphore
*sem
)
217 EXPORT_SYMBOL(up_read_non_owner
);