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_
);
120 DEBUG_RWSEMS_WARN_ON(sem
->owner
!= RWSEM_READER_OWNED
);
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
);
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
)
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
)
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
)
186 EXPORT_SYMBOL(down_read_non_owner
);
188 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
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
)
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_
);
209 rwsem_set_owner(sem
);
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
);
221 EXPORT_SYMBOL(up_read_non_owner
);