xref: /linux/kernel/locking/rwsem.c (revision ef69f8d2ff09518657c3ecaf2db8408c16549829)
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 	rwsem_set_reader_owned(sem);
28 }
29 
30 EXPORT_SYMBOL(down_read);
31 
32 int __sched down_read_killable(struct rw_semaphore *sem)
33 {
34 	might_sleep();
35 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
36 
37 	if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
38 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
39 		return -EINTR;
40 	}
41 
42 	rwsem_set_reader_owned(sem);
43 	return 0;
44 }
45 
46 EXPORT_SYMBOL(down_read_killable);
47 
48 /*
49  * trylock for reading -- returns 1 if successful, 0 if contention
50  */
51 int down_read_trylock(struct rw_semaphore *sem)
52 {
53 	int ret = __down_read_trylock(sem);
54 
55 	if (ret == 1) {
56 		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
57 		rwsem_set_reader_owned(sem);
58 	}
59 	return ret;
60 }
61 
62 EXPORT_SYMBOL(down_read_trylock);
63 
64 /*
65  * lock for writing
66  */
67 void __sched down_write(struct rw_semaphore *sem)
68 {
69 	might_sleep();
70 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
71 
72 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
73 	rwsem_set_owner(sem);
74 }
75 
76 EXPORT_SYMBOL(down_write);
77 
78 /*
79  * lock for writing
80  */
81 int __sched down_write_killable(struct rw_semaphore *sem)
82 {
83 	might_sleep();
84 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
85 
86 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
87 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
88 		return -EINTR;
89 	}
90 
91 	rwsem_set_owner(sem);
92 	return 0;
93 }
94 
95 EXPORT_SYMBOL(down_write_killable);
96 
97 /*
98  * trylock for writing -- returns 1 if successful, 0 if contention
99  */
100 int down_write_trylock(struct rw_semaphore *sem)
101 {
102 	int ret = __down_write_trylock(sem);
103 
104 	if (ret == 1) {
105 		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
106 		rwsem_set_owner(sem);
107 	}
108 
109 	return ret;
110 }
111 
112 EXPORT_SYMBOL(down_write_trylock);
113 
114 /*
115  * release a read lock
116  */
117 void up_read(struct rw_semaphore *sem)
118 {
119 	rwsem_release(&sem->dep_map, 1, _RET_IP_);
120 
121 	__up_read(sem);
122 }
123 
124 EXPORT_SYMBOL(up_read);
125 
126 /*
127  * release a write lock
128  */
129 void up_write(struct rw_semaphore *sem)
130 {
131 	rwsem_release(&sem->dep_map, 1, _RET_IP_);
132 
133 	rwsem_clear_owner(sem);
134 	__up_write(sem);
135 }
136 
137 EXPORT_SYMBOL(up_write);
138 
139 /*
140  * downgrade write lock to read lock
141  */
142 void downgrade_write(struct rw_semaphore *sem)
143 {
144 	lock_downgrade(&sem->dep_map, _RET_IP_);
145 
146 	rwsem_set_reader_owned(sem);
147 	__downgrade_write(sem);
148 }
149 
150 EXPORT_SYMBOL(downgrade_write);
151 
152 #ifdef CONFIG_DEBUG_LOCK_ALLOC
153 
154 void down_read_nested(struct rw_semaphore *sem, int subclass)
155 {
156 	might_sleep();
157 	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
158 
159 	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
160 	rwsem_set_reader_owned(sem);
161 }
162 
163 EXPORT_SYMBOL(down_read_nested);
164 
165 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
166 {
167 	might_sleep();
168 	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
169 
170 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
171 	rwsem_set_owner(sem);
172 }
173 
174 EXPORT_SYMBOL(_down_write_nest_lock);
175 
176 void down_read_non_owner(struct rw_semaphore *sem)
177 {
178 	might_sleep();
179 
180 	__down_read(sem);
181 }
182 
183 EXPORT_SYMBOL(down_read_non_owner);
184 
185 void down_write_nested(struct rw_semaphore *sem, int subclass)
186 {
187 	might_sleep();
188 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
189 
190 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
191 	rwsem_set_owner(sem);
192 }
193 
194 EXPORT_SYMBOL(down_write_nested);
195 
196 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
197 {
198 	might_sleep();
199 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
200 
201 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
202 		rwsem_release(&sem->dep_map, 1, _RET_IP_);
203 		return -EINTR;
204 	}
205 
206 	rwsem_set_owner(sem);
207 	return 0;
208 }
209 
210 EXPORT_SYMBOL(down_write_killable_nested);
211 
212 void up_read_non_owner(struct rw_semaphore *sem)
213 {
214 	__up_read(sem);
215 }
216 
217 EXPORT_SYMBOL(up_read_non_owner);
218 
219 #endif
220 
221 
222