blob: 2e853ad93a3a0f27ac3ce676ebe637ad8e03199e [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>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070011#include <linux/rwsem.h>
Arun Sharma600634972011-07-26 16:09:06 -070012#include <linux/atomic.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070013
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080014#include "rwsem.h"
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070015
Ingo Molnarc4e05112006-07-03 00:24:29 -070016/*
17 * lock for reading
18 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010019void __sched down_read(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070020{
21 might_sleep();
22 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
23
Peter Zijlstra4fe87742007-07-19 01:48:58 -070024 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnarc4e05112006-07-03 00:24:29 -070025}
26
27EXPORT_SYMBOL(down_read);
28
29/*
30 * trylock for reading -- returns 1 if successful, 0 if contention
31 */
32int down_read_trylock(struct rw_semaphore *sem)
33{
34 int ret = __down_read_trylock(sem);
35
36 if (ret == 1)
37 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
38 return ret;
39}
40
41EXPORT_SYMBOL(down_read_trylock);
42
43/*
44 * lock for writing
45 */
Livio Soaresc7af77b2007-12-18 15:21:13 +010046void __sched down_write(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -070047{
48 might_sleep();
49 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
50
Peter Zijlstra4fe87742007-07-19 01:48:58 -070051 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070052 rwsem_set_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -070053}
54
55EXPORT_SYMBOL(down_write);
56
57/*
Michal Hocko916633a2016-04-07 17:12:31 +020058 * lock for writing
59 */
60int __sched down_write_killable(struct rw_semaphore *sem)
61{
62 might_sleep();
63 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
64
65 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
66 rwsem_release(&sem->dep_map, 1, _RET_IP_);
67 return -EINTR;
68 }
69
70 rwsem_set_owner(sem);
71 return 0;
72}
73
74EXPORT_SYMBOL(down_write_killable);
75
76/*
Ingo Molnarc4e05112006-07-03 00:24:29 -070077 * trylock for writing -- returns 1 if successful, 0 if contention
78 */
79int down_write_trylock(struct rw_semaphore *sem)
80{
81 int ret = __down_write_trylock(sem);
82
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070083 if (ret == 1) {
Pavel Emelianov428e6ce2007-05-08 00:29:10 -070084 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070085 rwsem_set_owner(sem);
86 }
87
Ingo Molnarc4e05112006-07-03 00:24:29 -070088 return ret;
89}
90
91EXPORT_SYMBOL(down_write_trylock);
92
93/*
94 * release a read lock
95 */
96void up_read(struct rw_semaphore *sem)
97{
98 rwsem_release(&sem->dep_map, 1, _RET_IP_);
99
100 __up_read(sem);
101}
102
103EXPORT_SYMBOL(up_read);
104
105/*
106 * release a write lock
107 */
108void up_write(struct rw_semaphore *sem)
109{
110 rwsem_release(&sem->dep_map, 1, _RET_IP_);
111
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700112 rwsem_clear_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700113 __up_write(sem);
114}
115
116EXPORT_SYMBOL(up_write);
117
118/*
119 * downgrade write lock to read lock
120 */
121void downgrade_write(struct rw_semaphore *sem)
122{
123 /*
124 * lockdep: a downgraded write will live on as a write
125 * dependency.
126 */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700127 rwsem_clear_owner(sem);
Ingo Molnarc4e05112006-07-03 00:24:29 -0700128 __downgrade_write(sem);
129}
130
131EXPORT_SYMBOL(downgrade_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700132
133#ifdef CONFIG_DEBUG_LOCK_ALLOC
134
135void down_read_nested(struct rw_semaphore *sem, int subclass)
136{
137 might_sleep();
138 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
139
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700140 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700141}
142
143EXPORT_SYMBOL(down_read_nested);
144
Jiri Kosina1b963c82013-01-11 14:31:56 -0800145void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
146{
147 might_sleep();
148 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
149
150 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700151 rwsem_set_owner(sem);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800152}
153
154EXPORT_SYMBOL(_down_write_nest_lock);
155
Kent Overstreet84759c62011-09-21 21:43:05 -0700156void down_read_non_owner(struct rw_semaphore *sem)
157{
158 might_sleep();
159
160 __down_read(sem);
161}
162
163EXPORT_SYMBOL(down_read_non_owner);
164
Ingo Molnar4ea21762006-07-03 00:24:53 -0700165void down_write_nested(struct rw_semaphore *sem, int subclass)
166{
167 might_sleep();
168 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
169
Peter Zijlstra4fe87742007-07-19 01:48:58 -0700170 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700171 rwsem_set_owner(sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700172}
173
174EXPORT_SYMBOL(down_write_nested);
175
Al Viro887bddf2016-05-26 00:04:58 -0400176int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
177{
178 might_sleep();
179 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
180
181 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
182 rwsem_release(&sem->dep_map, 1, _RET_IP_);
183 return -EINTR;
184 }
185
186 rwsem_set_owner(sem);
187 return 0;
188}
189
190EXPORT_SYMBOL(down_write_killable_nested);
191
Kent Overstreet84759c62011-09-21 21:43:05 -0700192void up_read_non_owner(struct rw_semaphore *sem)
193{
194 __up_read(sem);
195}
196
197EXPORT_SYMBOL(up_read_non_owner);
198
Ingo Molnar4ea21762006-07-03 00:24:53 -0700199#endif
200
201