blob: e586f0d03ad38f51539be76c4f042642cdd89200 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarc4e05112006-07-03 00:24:29 -07002/* 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>
Livio Soaresc7af77b2007-12-18 15:21:13 +010010#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010011#include <linux/sched/debug.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040012#include <linux/export.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070013#include <linux/rwsem.h>
Arun Sharma600634972011-07-26 16:09:06 -070014#include <linux/atomic.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070015
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080016#include "rwsem.h"
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070017
Ingo Molnarc4e05112006-07-03 00:24:29 -070018/*
19 * lock for reading
20 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010021void __sched down_read(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070022{
23 might_sleep();
24 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
25
Peter Zijlstra4fe87742007-07-19 01:48:58 -070026 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Waiman Long19c5d692016-05-17 21:26:19 -040027 rwsem_set_reader_owned(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -070028}
29
30EXPORT_SYMBOL(down_read);
31
Kirill Tkhai76f85072017-09-29 19:06:38 +030032int __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
46EXPORT_SYMBOL(down_read_killable);
47
Ingo Molnarc4e05112006-07-03 00:24:29 -070048/*
49 * trylock for reading -- returns 1 if successful, 0 if contention
50 */
51int down_read_trylock(struct rw_semaphore *sem)
52{
53 int ret = __down_read_trylock(sem);
54
Waiman Long19c5d692016-05-17 21:26:19 -040055 if (ret == 1) {
Ingo Molnarc4e05112006-07-03 00:24:29 -070056 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
Waiman Long19c5d692016-05-17 21:26:19 -040057 rwsem_set_reader_owned(sem);
58 }
Ingo Molnarc4e05112006-07-03 00:24:29 -070059 return ret;
60}
61
62EXPORT_SYMBOL(down_read_trylock);
63
64/*
65 * lock for writing
66 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010067void __sched down_write(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070068{
69 might_sleep();
70 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
71
Peter Zijlstra4fe87742007-07-19 01:48:58 -070072 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070073 rwsem_set_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -070074}
75
76EXPORT_SYMBOL(down_write);
77
78/*
Michal Hocko916633a2016-04-07 17:12:31 +020079 * lock for writing
80 */
81int __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
95EXPORT_SYMBOL(down_write_killable);
96
97/*
Ingo Molnarc4e05112006-07-03 00:24:29 -070098 * trylock for writing -- returns 1 if successful, 0 if contention
99 */
100int down_write_trylock(struct rw_semaphore *sem)
101{
102 int ret = __down_write_trylock(sem);
103
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700104 if (ret == 1) {
Pavel Emelianov428e6ce2007-05-08 00:29:10 -0700105 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700106 rwsem_set_owner(sem);
107 }
108
Ingo Molnarc4e05112006-07-03 00:24:29 -0700109 return ret;
110}
111
112EXPORT_SYMBOL(down_write_trylock);
113
114/*
115 * release a read lock
116 */
117void up_read(struct rw_semaphore *sem)
118{
119 rwsem_release(&sem->dep_map, 1, _RET_IP_);
Waiman Long925b9cd2018-09-06 16:18:34 -0400120 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED));
Ingo Molnarc4e05112006-07-03 00:24:29 -0700121
Waiman Long925b9cd2018-09-06 16:18:34 -0400122 rwsem_clear_reader_owned(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700123 __up_read(sem);
124}
125
126EXPORT_SYMBOL(up_read);
127
128/*
129 * release a write lock
130 */
131void up_write(struct rw_semaphore *sem)
132{
133 rwsem_release(&sem->dep_map, 1, _RET_IP_);
Waiman Long5149cba2018-03-30 17:27:58 -0400134 DEBUG_RWSEMS_WARN_ON(sem->owner != current);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700135
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700136 rwsem_clear_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700137 __up_write(sem);
138}
139
140EXPORT_SYMBOL(up_write);
141
142/*
143 * downgrade write lock to read lock
144 */
145void downgrade_write(struct rw_semaphore *sem)
146{
J. R. Okajima6419c4a2017-02-03 01:38:17 +0900147 lock_downgrade(&sem->dep_map, _RET_IP_);
Waiman Long5149cba2018-03-30 17:27:58 -0400148 DEBUG_RWSEMS_WARN_ON(sem->owner != current);
J. R. Okajima6419c4a2017-02-03 01:38:17 +0900149
Waiman Long19c5d692016-05-17 21:26:19 -0400150 rwsem_set_reader_owned(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700151 __downgrade_write(sem);
152}
153
154EXPORT_SYMBOL(downgrade_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700155
156#ifdef CONFIG_DEBUG_LOCK_ALLOC
157
158void down_read_nested(struct rw_semaphore *sem, int subclass)
159{
160 might_sleep();
161 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
162
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700163 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Waiman Long19c5d692016-05-17 21:26:19 -0400164 rwsem_set_reader_owned(sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700165}
166
167EXPORT_SYMBOL(down_read_nested);
168
Jiri Kosina1b963c82013-01-11 14:31:56 -0800169void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
170{
171 might_sleep();
172 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
173
174 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700175 rwsem_set_owner(sem);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800176}
177
178EXPORT_SYMBOL(_down_write_nest_lock);
179
Kent Overstreet84759c62011-09-21 21:43:05 -0700180void down_read_non_owner(struct rw_semaphore *sem)
181{
182 might_sleep();
183
184 __down_read(sem);
Waiman Long925b9cd2018-09-06 16:18:34 -0400185 __rwsem_set_reader_owned(sem, NULL);
Kent Overstreet84759c62011-09-21 21:43:05 -0700186}
187
188EXPORT_SYMBOL(down_read_non_owner);
189
Ingo Molnar4ea21762006-07-03 00:24:53 -0700190void down_write_nested(struct rw_semaphore *sem, int subclass)
191{
192 might_sleep();
193 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
194
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700195 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700196 rwsem_set_owner(sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700197}
198
199EXPORT_SYMBOL(down_write_nested);
200
Al Viro887bddf2016-05-26 00:04:58 -0400201int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
202{
203 might_sleep();
204 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
205
206 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
207 rwsem_release(&sem->dep_map, 1, _RET_IP_);
208 return -EINTR;
209 }
210
211 rwsem_set_owner(sem);
212 return 0;
213}
214
215EXPORT_SYMBOL(down_write_killable_nested);
216
Kent Overstreet84759c62011-09-21 21:43:05 -0700217void up_read_non_owner(struct rw_semaphore *sem)
218{
Waiman Long925b9cd2018-09-06 16:18:34 -0400219 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED));
Kent Overstreet84759c62011-09-21 21:43:05 -0700220 __up_read(sem);
221}
222
223EXPORT_SYMBOL(up_read_non_owner);
224
Ingo Molnar4ea21762006-07-03 00:24:53 -0700225#endif