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