blob: 6ac8ee5f15ddf67977c5c32e9d45799ec8920f7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* rwsem.h: R/W semaphores, public interface
2 *
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
5 */
6
7#ifndef _LINUX_RWSEM_H
8#define _LINUX_RWSEM_H
9
10#include <linux/linkage.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/kernel.h>
Thomas Gleixnerc16a87c2011-01-26 20:05:50 +000014#include <linux/list.h>
15#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Michal Hockod47996082016-04-07 17:12:26 +020017#include <linux/err.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070018#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Low90631822014-07-14 10:27:49 -070019#include <linux/osq_lock.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct rw_semaphore;
23
24#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
25#include <linux/rwsem-spinlock.h> /* use a generic implementation */
Jason Low8ee62b12016-06-03 22:26:02 -070026#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000028/* All arch specific implementations share the same struct */
29struct rw_semaphore {
Jason Low8ee62b12016-06-03 22:26:02 -070030 atomic_long_t count;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070031 struct list_head wait_list;
Jason Lowce069fc2014-07-14 10:27:52 -070032 raw_spinlock_t wait_lock;
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070033#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070034 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070035 /*
36 * Write owner. Used as a speculative check to see
37 * if the owner is running on the cpu.
38 */
39 struct task_struct *owner;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070040#endif
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000041#ifdef CONFIG_DEBUG_LOCK_ALLOC
42 struct lockdep_map dep_map;
43#endif
44};
45
Thomas Gleixnerd1233752011-01-26 21:32:01 +010046extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
Kirill Tkhai83ced162017-06-19 21:02:26 +030047extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
Thomas Gleixnerd1233752011-01-26 21:32:01 +010048extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
Michal Hockod47996082016-04-07 17:12:26 +020049extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
Thomas Gleixnerd1233752011-01-26 21:32:01 +010050extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
51extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000052
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000053/* Include the arch specific part */
54#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000055
56/* In all implementations count != 0 means locked */
57static inline int rwsem_is_locked(struct rw_semaphore *sem)
58{
Jason Low8ee62b12016-06-03 22:26:02 -070059 return atomic_long_read(&sem->count) != 0;
Thomas Gleixner41e58872011-01-26 20:06:03 +000060}
61
Jason Low8ee62b12016-06-03 22:26:02 -070062#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Thomas Gleixner12249b32011-01-26 20:06:00 +000065/* Common initializer macros and functions */
66
67#ifdef CONFIG_DEBUG_LOCK_ALLOC
68# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
69#else
70# define __RWSEM_DEP_MAP_INIT(lockname)
71#endif
72
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070073#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070074#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070075#else
Jason Lowce069fc2014-07-14 10:27:52 -070076#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070077#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000078
Jason Lowce069fc2014-07-14 10:27:52 -070079#define __RWSEM_INITIALIZER(name) \
Jason Low8ee62b12016-06-03 22:26:02 -070080 { __RWSEM_INIT_COUNT(name), \
Jason Lowce069fc2014-07-14 10:27:52 -070081 .wait_list = LIST_HEAD_INIT((name).wait_list), \
82 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
83 __RWSEM_OPT_INIT(name) \
84 __RWSEM_DEP_MAP_INIT(name) }
85
Thomas Gleixner12249b32011-01-26 20:06:00 +000086#define DECLARE_RWSEM(name) \
87 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
88
89extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
90 struct lock_class_key *key);
91
92#define init_rwsem(sem) \
93do { \
94 static struct lock_class_key __key; \
95 \
96 __init_rwsem((sem), #sem, &__key); \
97} while (0)
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Josef Bacik4a444b12013-08-30 10:05:22 -0400100 * This is the same regardless of which rwsem implementation that is being used.
101 * It is just a heuristic meant to be called by somebody alreadying holding the
102 * rwsem to see if somebody from an incompatible type is wanting access to the
103 * lock.
104 */
105static inline int rwsem_is_contended(struct rw_semaphore *sem)
106{
107 return !list_empty(&sem->wait_list);
108}
109
110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * lock for reading
112 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700113extern void down_read(struct rw_semaphore *sem);
Kirill Tkhai76f85072017-09-29 19:06:38 +0300114extern int __must_check down_read_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * trylock for reading -- returns 1 if successful, 0 if contention
118 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700119extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * lock for writing
123 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700124extern void down_write(struct rw_semaphore *sem);
Michal Hocko916633a2016-04-07 17:12:31 +0200125extern int __must_check down_write_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * trylock for writing -- returns 1 if successful, 0 if contention
129 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700130extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*
133 * release a read lock
134 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700135extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*
138 * release a write lock
139 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700140extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/*
143 * downgrade write lock to read lock
144 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700145extern void downgrade_write(struct rw_semaphore *sem);
146
147#ifdef CONFIG_DEBUG_LOCK_ALLOC
148/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700149 * nested locking. NOTE: rwsems are not allowed to recurse
150 * (which occurs if the same task tries to acquire the same
151 * lock instance multiple times), but multiple locks of the
152 * same lock class might be taken, if the order of the locks
153 * is always the same. This ordering rule can be expressed
154 * to lockdep via the _nested() APIs, but enumerating the
155 * subclasses that are used. (If the nesting relationship is
156 * static then another method for expressing nested locking is
157 * the explicit definition of lock class keys and the use of
158 * lockdep_set_class() at lock initialization time.
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700159 * See Documentation/locking/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700160 */
161extern void down_read_nested(struct rw_semaphore *sem, int subclass);
162extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Al Viro887bddf2016-05-26 00:04:58 -0400163extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800164extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
165
166# define down_write_nest_lock(sem, nest_lock) \
167do { \
168 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
169 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
170} while (0);
171
Kent Overstreet84759c62011-09-21 21:43:05 -0700172/*
173 * Take/release a lock when not the owner will release it.
174 *
175 * [ This API should be avoided as much as possible - the
176 * proper abstraction for this case is completions. ]
177 */
178extern void down_read_non_owner(struct rw_semaphore *sem);
179extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700180#else
181# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100182# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700183# define down_write_nested(sem, subclass) down_write(sem)
Al Viro887bddf2016-05-26 00:04:58 -0400184# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700185# define down_read_non_owner(sem) down_read(sem)
186# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif /* _LINUX_RWSEM_H */