clk: samsung: Add bus clock for GPU/G3D on Exynos4412
[linux/fpc-iii.git] / kernel / locking / rwsem.c
blobccbf18f560ff1d85f0835ff5cadc8044cae00bfc
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);
29 EXPORT_SYMBOL(down_read);
31 int __sched down_read_killable(struct rw_semaphore *sem)
33 might_sleep();
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_);
38 return -EINTR;
41 return 0;
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);
53 if (ret == 1)
54 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
55 return ret;
58 EXPORT_SYMBOL(down_read_trylock);
61 * lock for writing
63 void __sched down_write(struct rw_semaphore *sem)
65 might_sleep();
66 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
68 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
71 EXPORT_SYMBOL(down_write);
74 * lock for writing
76 int __sched down_write_killable(struct rw_semaphore *sem)
78 might_sleep();
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_);
83 return -EINTR;
86 return 0;
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);
98 if (ret == 1)
99 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
101 return ret;
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_);
113 __up_read(sem);
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_);
125 __up_write(sem);
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)
146 might_sleep();
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)
156 might_sleep();
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)
166 might_sleep();
168 __down_read(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)
176 might_sleep();
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)
186 might_sleep();
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_);
191 return -EINTR;
194 return 0;
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),
202 sem);
203 __up_read(sem);
206 EXPORT_SYMBOL(up_read_non_owner);
208 #endif