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
);
29 EXPORT_SYMBOL(down_read
);
31 int __sched
down_read_killable(struct rw_semaphore
*sem
)
34 rwsem_acquire_read(&sem
->dep_map
, 0, 0, _RET_IP_
);
36 if (LOCK_CONTENDED_RETURN(sem
, __down_read_trylock
, __down_read_killable
)) {
37 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
44 EXPORT_SYMBOL(down_read_killable
);
47 * trylock for reading -- returns 1 if successful, 0 if contention
49 int down_read_trylock(struct rw_semaphore
*sem
)
51 int ret
= __down_read_trylock(sem
);
54 rwsem_acquire_read(&sem
->dep_map
, 0, 1, _RET_IP_
);
58 EXPORT_SYMBOL(down_read_trylock
);
63 void __sched
down_write(struct rw_semaphore
*sem
)
66 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
68 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
71 EXPORT_SYMBOL(down_write
);
76 int __sched
down_write_killable(struct rw_semaphore
*sem
)
79 rwsem_acquire(&sem
->dep_map
, 0, 0, _RET_IP_
);
81 if (LOCK_CONTENDED_RETURN(sem
, __down_write_trylock
, __down_write_killable
)) {
82 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
89 EXPORT_SYMBOL(down_write_killable
);
92 * trylock for writing -- returns 1 if successful, 0 if contention
94 int down_write_trylock(struct rw_semaphore
*sem
)
96 int ret
= __down_write_trylock(sem
);
99 rwsem_acquire(&sem
->dep_map
, 0, 1, _RET_IP_
);
104 EXPORT_SYMBOL(down_write_trylock
);
107 * release a read lock
109 void up_read(struct rw_semaphore
*sem
)
111 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
116 EXPORT_SYMBOL(up_read
);
119 * release a write lock
121 void up_write(struct rw_semaphore
*sem
)
123 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
128 EXPORT_SYMBOL(up_write
);
131 * downgrade write lock to read lock
133 void downgrade_write(struct rw_semaphore
*sem
)
135 lock_downgrade(&sem
->dep_map
, _RET_IP_
);
137 __downgrade_write(sem
);
140 EXPORT_SYMBOL(downgrade_write
);
142 #ifdef CONFIG_DEBUG_LOCK_ALLOC
144 void down_read_nested(struct rw_semaphore
*sem
, int subclass
)
147 rwsem_acquire_read(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
149 LOCK_CONTENDED(sem
, __down_read_trylock
, __down_read
);
152 EXPORT_SYMBOL(down_read_nested
);
154 void _down_write_nest_lock(struct rw_semaphore
*sem
, struct lockdep_map
*nest
)
157 rwsem_acquire_nest(&sem
->dep_map
, 0, 0, nest
, _RET_IP_
);
159 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
162 EXPORT_SYMBOL(_down_write_nest_lock
);
164 void down_read_non_owner(struct rw_semaphore
*sem
)
169 __rwsem_set_reader_owned(sem
, NULL
);
172 EXPORT_SYMBOL(down_read_non_owner
);
174 void down_write_nested(struct rw_semaphore
*sem
, int subclass
)
177 rwsem_acquire(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
179 LOCK_CONTENDED(sem
, __down_write_trylock
, __down_write
);
182 EXPORT_SYMBOL(down_write_nested
);
184 int __sched
down_write_killable_nested(struct rw_semaphore
*sem
, int subclass
)
187 rwsem_acquire(&sem
->dep_map
, subclass
, 0, _RET_IP_
);
189 if (LOCK_CONTENDED_RETURN(sem
, __down_write_trylock
, __down_write_killable
)) {
190 rwsem_release(&sem
->dep_map
, 1, _RET_IP_
);
197 EXPORT_SYMBOL(down_write_killable_nested
);
199 void up_read_non_owner(struct rw_semaphore
*sem
)
201 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem
->owner
& RWSEM_READER_OWNED
),
206 EXPORT_SYMBOL(up_read_non_owner
);