xref: /openbmc/linux/kernel/locking/rwsem.c (revision 4e95bc26)
1 // SPDX-License-Identifier: GPL-2.0
2 /* kernel/rwsem.c: R/W semaphores, public implementation
3  *
4  * Written by David Howells (dhowells@redhat.com).
5  * Derived from asm-i386/semaphore.h
6  */
7 
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>
15 
16 #include "rwsem.h"
17 
18 /*
19  * lock for reading
20  */
21 void __sched down_read(struct rw_semaphore *sem)
22 {
23 	might_sleep();
24 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
25 
26 	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
27 }
28 
29 EXPORT_SYMBOL(down_read);
30 
31 int __sched down_read_killable(struct rw_semaphore *sem)
32 {
33 	might_sleep();
34 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
35 
36 	if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
37 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
38 		return -EINTR;
39 	}
40 
41 	return 0;
42 }
43 
44 EXPORT_SYMBOL(down_read_killable);
45 
46 /*
47  * trylock for reading -- returns 1 if successful, 0 if contention
48  */
49 int down_read_trylock(struct rw_semaphore *sem)
50 {
51 	int ret = __down_read_trylock(sem);
52 
53 	if (ret == 1)
54 		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
55 	return ret;
56 }
57 
58 EXPORT_SYMBOL(down_read_trylock);
59 
60 /*
61  * lock for writing
62  */
63 void __sched down_write(struct rw_semaphore *sem)
64 {
65 	might_sleep();
66 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
67 
68 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
69 }
70 
71 EXPORT_SYMBOL(down_write);
72 
73 /*
74  * lock for writing
75  */
76 int __sched down_write_killable(struct rw_semaphore *sem)
77 {
78 	might_sleep();
79 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
80 
81 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
82 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
83 		return -EINTR;
84 	}
85 
86 	return 0;
87 }
88 
89 EXPORT_SYMBOL(down_write_killable);
90 
91 /*
92  * trylock for writing -- returns 1 if successful, 0 if contention
93  */
94 int down_write_trylock(struct rw_semaphore *sem)
95 {
96 	int ret = __down_write_trylock(sem);
97 
98 	if (ret == 1)
99 		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
100 
101 	return ret;
102 }
103 
104 EXPORT_SYMBOL(down_write_trylock);
105 
106 /*
107  * release a read lock
108  */
109 void up_read(struct rw_semaphore *sem)
110 {
111 	rwsem_release(&sem->dep_map, 1, _RET_IP_);
112 
113 	__up_read(sem);
114 }
115 
116 EXPORT_SYMBOL(up_read);
117 
118 /*
119  * release a write lock
120  */
121 void up_write(struct rw_semaphore *sem)
122 {
123 	rwsem_release(&sem->dep_map, 1, _RET_IP_);
124 
125 	__up_write(sem);
126 }
127 
128 EXPORT_SYMBOL(up_write);
129 
130 /*
131  * downgrade write lock to read lock
132  */
133 void downgrade_write(struct rw_semaphore *sem)
134 {
135 	lock_downgrade(&sem->dep_map, _RET_IP_);
136 
137 	__downgrade_write(sem);
138 }
139 
140 EXPORT_SYMBOL(downgrade_write);
141 
142 #ifdef CONFIG_DEBUG_LOCK_ALLOC
143 
144 void down_read_nested(struct rw_semaphore *sem, int subclass)
145 {
146 	might_sleep();
147 	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
148 
149 	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
150 }
151 
152 EXPORT_SYMBOL(down_read_nested);
153 
154 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
155 {
156 	might_sleep();
157 	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
158 
159 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
160 }
161 
162 EXPORT_SYMBOL(_down_write_nest_lock);
163 
164 void down_read_non_owner(struct rw_semaphore *sem)
165 {
166 	might_sleep();
167 
168 	__down_read(sem);
169 	__rwsem_set_reader_owned(sem, NULL);
170 }
171 
172 EXPORT_SYMBOL(down_read_non_owner);
173 
174 void down_write_nested(struct rw_semaphore *sem, int subclass)
175 {
176 	might_sleep();
177 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
178 
179 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
180 }
181 
182 EXPORT_SYMBOL(down_write_nested);
183 
184 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
185 {
186 	might_sleep();
187 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
188 
189 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
190 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
191 		return -EINTR;
192 	}
193 
194 	return 0;
195 }
196 
197 EXPORT_SYMBOL(down_write_killable_nested);
198 
199 void up_read_non_owner(struct rw_semaphore *sem)
200 {
201 	DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED),
202 				sem);
203 	__up_read(sem);
204 }
205 
206 EXPORT_SYMBOL(up_read_non_owner);
207 
208 #endif
209