]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/rwsem.h
locking/arch, x86: Add __down_read_killable()
[mirror_ubuntu-bionic-kernel.git] / include / linux / rwsem.h
CommitLineData
1da177e4
LT
1/* 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
1da177e4
LT
12#include <linux/types.h>
13#include <linux/kernel.h>
c16a87ce
TG
14#include <linux/list.h>
15#include <linux/spinlock.h>
60063497 16#include <linux/atomic.h>
d4799608 17#include <linux/err.h>
5db6c6fe 18#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
90631822 19#include <linux/osq_lock.h>
5db6c6fe 20#endif
1da177e4
LT
21
22struct rw_semaphore;
23
24#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
25#include <linux/rwsem-spinlock.h> /* use a generic implementation */
8ee62b18 26#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
1da177e4 27#else
1c8ed640
TG
28/* All arch specific implementations share the same struct */
29struct rw_semaphore {
8ee62b18 30 atomic_long_t count;
4fc828e2 31 struct list_head wait_list;
ce069fc9 32 raw_spinlock_t wait_lock;
5db6c6fe 33#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
ce069fc9 34 struct optimistic_spin_queue osq; /* spinner MCS lock */
4fc828e2
DB
35 /*
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;
4fc828e2 40#endif
1c8ed640
TG
41#ifdef CONFIG_DEBUG_LOCK_ALLOC
42 struct lockdep_map dep_map;
43#endif
44};
45
d1233754 46extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
83ced169 47extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
d1233754 48extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
d4799608 49extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
d1233754
TG
50extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
51extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
aac72277 52
1c8ed640
TG
53/* Include the arch specific part */
54#include <asm/rwsem.h>
41e5887f
TG
55
56/* In all implementations count != 0 means locked */
57static inline int rwsem_is_locked(struct rw_semaphore *sem)
58{
8ee62b18 59 return atomic_long_read(&sem->count) != 0;
41e5887f
TG
60}
61
8ee62b18 62#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
1da177e4
LT
63#endif
64
12249b34
TG
65/* 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
5db6c6fe 73#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
ce069fc9 74#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
4fc828e2 75#else
ce069fc9 76#define __RWSEM_OPT_INIT(lockname)
4fc828e2 77#endif
12249b34 78
ce069fc9 79#define __RWSEM_INITIALIZER(name) \
8ee62b18 80 { __RWSEM_INIT_COUNT(name), \
ce069fc9
JL
81 .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
12249b34
TG
86#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
4a444b1f
JB
99/*
100 * 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
1da177e4
LT
110/*
111 * lock for reading
112 */
4ea2176d 113extern void down_read(struct rw_semaphore *sem);
1da177e4
LT
114
115/*
116 * trylock for reading -- returns 1 if successful, 0 if contention
117 */
4ea2176d 118extern int down_read_trylock(struct rw_semaphore *sem);
1da177e4
LT
119
120/*
121 * lock for writing
122 */
4ea2176d 123extern void down_write(struct rw_semaphore *sem);
916633a4 124extern int __must_check down_write_killable(struct rw_semaphore *sem);
1da177e4
LT
125
126/*
127 * trylock for writing -- returns 1 if successful, 0 if contention
128 */
4ea2176d 129extern int down_write_trylock(struct rw_semaphore *sem);
1da177e4
LT
130
131/*
132 * release a read lock
133 */
4ea2176d 134extern void up_read(struct rw_semaphore *sem);
1da177e4
LT
135
136/*
137 * release a write lock
138 */
4ea2176d 139extern void up_write(struct rw_semaphore *sem);
1da177e4
LT
140
141/*
142 * downgrade write lock to read lock
143 */
4ea2176d
IM
144extern void downgrade_write(struct rw_semaphore *sem);
145
146#ifdef CONFIG_DEBUG_LOCK_ALLOC
147/*
5fca80e8
IM
148 * nested locking. NOTE: rwsems are not allowed to recurse
149 * (which occurs if the same task tries to acquire the same
150 * lock instance multiple times), but multiple locks of the
151 * same lock class might be taken, if the order of the locks
152 * is always the same. This ordering rule can be expressed
153 * to lockdep via the _nested() APIs, but enumerating the
154 * subclasses that are used. (If the nesting relationship is
155 * static then another method for expressing nested locking is
156 * the explicit definition of lock class keys and the use of
157 * lockdep_set_class() at lock initialization time.
214e0aed 158 * See Documentation/locking/lockdep-design.txt for more details.)
4ea2176d
IM
159 */
160extern void down_read_nested(struct rw_semaphore *sem, int subclass);
161extern void down_write_nested(struct rw_semaphore *sem, int subclass);
887bddfa 162extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
1b963c81
JK
163extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
164
165# define down_write_nest_lock(sem, nest_lock) \
166do { \
167 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
168 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
169} while (0);
170
84759c6d
KO
171/*
172 * Take/release a lock when not the owner will release it.
173 *
174 * [ This API should be avoided as much as possible - the
175 * proper abstraction for this case is completions. ]
176 */
177extern void down_read_non_owner(struct rw_semaphore *sem);
178extern void up_read_non_owner(struct rw_semaphore *sem);
4ea2176d
IM
179#else
180# define down_read_nested(sem, subclass) down_read(sem)
e65b9ad2 181# define down_write_nest_lock(sem, nest_lock) down_write(sem)
4ea2176d 182# define down_write_nested(sem, subclass) down_write(sem)
887bddfa 183# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
84759c6d
KO
184# define down_read_non_owner(sem) down_read(sem)
185# define up_read_non_owner(sem) up_read(sem)
4ea2176d 186#endif
1da177e4 187
1da177e4 188#endif /* _LINUX_RWSEM_H */