blob: 4d48b1c4870dda0b33c7f93e1aed01fe8b6ceb61 [file] [log] [blame]
Ingo Molnarc4e05112006-07-03 00:24:29 -07001/* 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>
Livio Soaresc7af77b2007-12-18 15:21:13 +01009#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010010#include <linux/sched/debug.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040011#include <linux/export.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070012#include <linux/rwsem.h>
Arun Sharma600634972011-07-26 16:09:06 -070013#include <linux/atomic.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070014
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080015#include "rwsem.h"
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070016
Ingo Molnarc4e05112006-07-03 00:24:29 -070017/*
18 * lock for reading
19 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010020void __sched down_read(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070021{
22 might_sleep();
23 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
24
Peter Zijlstra4fe87742007-07-19 01:48:58 -070025 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Waiman Long19c5d692016-05-17 21:26:19 -040026 rwsem_set_reader_owned(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -070027}
28
29EXPORT_SYMBOL(down_read);
30
31/*
32 * trylock for reading -- returns 1 if successful, 0 if contention
33 */
34int down_read_trylock(struct rw_semaphore *sem)
35{
36 int ret = __down_read_trylock(sem);
37
Waiman Long19c5d692016-05-17 21:26:19 -040038 if (ret == 1) {
Ingo Molnarc4e05112006-07-03 00:24:29 -070039 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
Waiman Long19c5d692016-05-17 21:26:19 -040040 rwsem_set_reader_owned(sem);
41 }
Ingo Molnarc4e05112006-07-03 00:24:29 -070042 return ret;
43}
44
45EXPORT_SYMBOL(down_read_trylock);
46
47/*
48 * lock for writing
49 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010050void __sched down_write(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070051{
52 might_sleep();
53 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
54
Peter Zijlstra4fe87742007-07-19 01:48:58 -070055 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070056 rwsem_set_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -070057}
58
59EXPORT_SYMBOL(down_write);
60
61/*
Michal Hocko916633a2016-04-07 17:12:31 +020062 * lock for writing
63 */
64int __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
78EXPORT_SYMBOL(down_write_killable);
79
80/*
Ingo Molnarc4e05112006-07-03 00:24:29 -070081 * trylock for writing -- returns 1 if successful, 0 if contention
82 */
83int down_write_trylock(struct rw_semaphore *sem)
84{
85 int ret = __down_write_trylock(sem);
86
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070087 if (ret == 1) {
Pavel Emelianov428e6ce2007-05-08 00:29:10 -070088 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070089 rwsem_set_owner(sem);
90 }
91
Ingo Molnarc4e05112006-07-03 00:24:29 -070092 return ret;
93}
94
95EXPORT_SYMBOL(down_write_trylock);
96
97/*
98 * release a read lock
99 */
100void up_read(struct rw_semaphore *sem)
101{
102 rwsem_release(&sem->dep_map, 1, _RET_IP_);
103
104 __up_read(sem);
105}
106
107EXPORT_SYMBOL(up_read);
108
109/*
110 * release a write lock
111 */
112void up_write(struct rw_semaphore *sem)
113{
114 rwsem_release(&sem->dep_map, 1, _RET_IP_);
115
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700116 rwsem_clear_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700117 __up_write(sem);
118}
119
120EXPORT_SYMBOL(up_write);
121
122/*
123 * downgrade write lock to read lock
124 */
125void downgrade_write(struct rw_semaphore *sem)
126{
J. R. Okajima6419c4a2017-02-03 01:38:17 +0900127 lock_downgrade(&sem->dep_map, _RET_IP_);
128
Waiman Long19c5d692016-05-17 21:26:19 -0400129 rwsem_set_reader_owned(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700130 __downgrade_write(sem);
131}
132
133EXPORT_SYMBOL(downgrade_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700134
135#ifdef CONFIG_DEBUG_LOCK_ALLOC
136
137void 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
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700142 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Waiman Long19c5d692016-05-17 21:26:19 -0400143 rwsem_set_reader_owned(sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700144}
145
146EXPORT_SYMBOL(down_read_nested);
147
Jiri Kosina1b963c82013-01-11 14:31:56 -0800148void _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);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700154 rwsem_set_owner(sem);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800155}
156
157EXPORT_SYMBOL(_down_write_nest_lock);
158
Kent Overstreet84759c62011-09-21 21:43:05 -0700159void down_read_non_owner(struct rw_semaphore *sem)
160{
161 might_sleep();
162
163 __down_read(sem);
164}
165
166EXPORT_SYMBOL(down_read_non_owner);
167
Ingo Molnar4ea21762006-07-03 00:24:53 -0700168void down_write_nested(struct rw_semaphore *sem, int subclass)
169{
170 might_sleep();
171 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
172
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700173 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700174 rwsem_set_owner(sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700175}
176
177EXPORT_SYMBOL(down_write_nested);
178
Al Viro887bddf2016-05-26 00:04:58 -0400179int __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
193EXPORT_SYMBOL(down_write_killable_nested);
194
Kent Overstreet84759c62011-09-21 21:43:05 -0700195void up_read_non_owner(struct rw_semaphore *sem)
196{
197 __up_read(sem);
198}
199
200EXPORT_SYMBOL(up_read_non_owner);
201
Ingo Molnar4ea21762006-07-03 00:24:53 -0700202#endif
203
204