Blame view

include/linux/rwsem.h 6.17 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
  /* rwsem.h: R/W semaphores, public interface
   *
   * Written by David Howells (dhowells@redhat.com).
   * Derived from asm-i386/semaphore.h
   */
  
  #ifndef _LINUX_RWSEM_H
  #define _LINUX_RWSEM_H
  
  #include <linux/linkage.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  #include <linux/types.h>
  #include <linux/kernel.h>
c16a87ce0   Thomas Gleixner   rwsem: Cleanup in...
14
15
  #include <linux/list.h>
  #include <linux/spinlock.h>
60063497a   Arun Sharma   atomic: use <linu...
16
  #include <linux/atomic.h>
d47996082   Michal Hocko   locking/rwsem: In...
17
  #include <linux/err.h>
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
18
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
90631822c   Jason Low   locking/spinlocks...
19
  #include <linux/osq_lock.h>
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
20
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

364f784f0   Waiman Long   locking/rwsem: Op...
22
23
24
25
26
27
28
29
30
31
32
33
  /*
   * For an uncontended rwsem, count and owner are the only fields a task
   * needs to touch when acquiring the rwsem. So they are put next to each
   * other to increase the chance that they will share the same cacheline.
   *
   * In a contended rwsem, the owner is likely the most frequently accessed
   * field in the structure as the optimistic waiter that holds the osq lock
   * will spin on owner. For an embedded rwsem, other hot fields in the
   * containing structure should be moved further away from the rwsem to
   * reduce the chance that they will share the same cacheline causing
   * cacheline bouncing problem.
   */
1c8ed640d   Thomas Gleixner   rwsem: Move dupli...
34
  struct rw_semaphore {
8ee62b187   Jason Low   locking/rwsem: Co...
35
  	atomic_long_t count;
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
36
  	/*
94a9717b3   Waiman Long   locking/rwsem: Ma...
37
38
39
  	 * Write owner or one of the read owners as well flags regarding
  	 * the current state of the rwsem. Can be used as a speculative
  	 * check to see if the write owner is running on the cpu.
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
40
  	 */
94a9717b3   Waiman Long   locking/rwsem: Ma...
41
  	atomic_long_t owner;
c71fd893f   Waiman Long   locking/rwsem: Ma...
42
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
364f784f0   Waiman Long   locking/rwsem: Op...
43
  	struct optimistic_spin_queue osq; /* spinner MCS lock */
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
44
  #endif
364f784f0   Waiman Long   locking/rwsem: Op...
45
46
  	raw_spinlock_t wait_lock;
  	struct list_head wait_list;
fce45cd41   Davidlohr Bueso   locking/rwsem: Ch...
47
48
49
  #ifdef CONFIG_DEBUG_RWSEMS
  	void *magic;
  #endif
1c8ed640d   Thomas Gleixner   rwsem: Move dupli...
50
51
52
53
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  	struct lockdep_map	dep_map;
  #endif
  };
5a817641f   Waiman Long   locking/percpu-rw...
54
  /*
02f1082b0   Waiman Long   locking/rwsem: Cl...
55
   * Setting all bits of the owner field except bit 0 will indicate
5a817641f   Waiman Long   locking/percpu-rw...
56
57
   * that the rwsem is writer-owned with an unknown owner.
   */
94a9717b3   Waiman Long   locking/rwsem: Ma...
58
  #define RWSEM_OWNER_UNKNOWN	(-2L)
5a817641f   Waiman Long   locking/percpu-rw...
59

41e5887fa   Thomas Gleixner   rwsem: Unify the ...
60
61
62
  /* In all implementations count != 0 means locked */
  static inline int rwsem_is_locked(struct rw_semaphore *sem)
  {
8ee62b187   Jason Low   locking/rwsem: Co...
63
  	return atomic_long_read(&sem->count) != 0;
41e5887fa   Thomas Gleixner   rwsem: Unify the ...
64
  }
46ad0840b   Waiman Long   locking/rwsem: Re...
65
  #define RWSEM_UNLOCKED_VALUE		0L
8ee62b187   Jason Low   locking/rwsem: Co...
66
  #define __RWSEM_INIT_COUNT(name)	.count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67

12249b344   Thomas Gleixner   rwsem: Move dupli...
68
69
70
71
72
73
74
  /* Common initializer macros and functions */
  
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  #else
  # define __RWSEM_DEP_MAP_INIT(lockname)
  #endif
fce45cd41   Davidlohr Bueso   locking/rwsem: Ch...
75
76
77
78
79
  #ifdef CONFIG_DEBUG_RWSEMS
  # define __DEBUG_RWSEM_INITIALIZER(lockname) , .magic = &lockname
  #else
  # define __DEBUG_RWSEM_INITIALIZER(lockname)
  #endif
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
80
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
c71fd893f   Waiman Long   locking/rwsem: Ma...
81
  #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
82
  #else
ce069fc92   Jason Low   locking/rwsem: Re...
83
  #define __RWSEM_OPT_INIT(lockname)
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
84
  #endif
12249b344   Thomas Gleixner   rwsem: Move dupli...
85

ce069fc92   Jason Low   locking/rwsem: Re...
86
  #define __RWSEM_INITIALIZER(name)				\
8ee62b187   Jason Low   locking/rwsem: Co...
87
  	{ __RWSEM_INIT_COUNT(name),				\
94a9717b3   Waiman Long   locking/rwsem: Ma...
88
  	  .owner = ATOMIC_LONG_INIT(0),				\
ce069fc92   Jason Low   locking/rwsem: Re...
89
90
91
  	  .wait_list = LIST_HEAD_INIT((name).wait_list),	\
  	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock)	\
  	  __RWSEM_OPT_INIT(name)				\
fce45cd41   Davidlohr Bueso   locking/rwsem: Ch...
92
  	  __DEBUG_RWSEM_INITIALIZER(name)			\
ce069fc92   Jason Low   locking/rwsem: Re...
93
  	  __RWSEM_DEP_MAP_INIT(name) }
12249b344   Thomas Gleixner   rwsem: Move dupli...
94
95
96
97
98
99
100
101
102
103
104
105
  #define DECLARE_RWSEM(name) \
  	struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  
  extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  			 struct lock_class_key *key);
  
  #define init_rwsem(sem)						\
  do {								\
  	static struct lock_class_key __key;			\
  								\
  	__init_rwsem((sem), #sem, &__key);			\
  } while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  /*
4a444b1f0   Josef Bacik   rwsem: add rwsem_...
107
108
109
110
111
112
113
114
115
116
117
   * This is the same regardless of which rwsem implementation that is being used.
   * It is just a heuristic meant to be called by somebody alreadying holding the
   * rwsem to see if somebody from an incompatible type is wanting access to the
   * lock.
   */
  static inline int rwsem_is_contended(struct rw_semaphore *sem)
  {
  	return !list_empty(&sem->wait_list);
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
   * lock for reading
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
120
  extern void down_read(struct rw_semaphore *sem);
76f8507f7   Kirill Tkhai   locking/rwsem: Ad...
121
  extern int __must_check down_read_killable(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
  
  /*
   * trylock for reading -- returns 1 if successful, 0 if contention
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
126
  extern int down_read_trylock(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
  
  /*
   * lock for writing
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
131
  extern void down_write(struct rw_semaphore *sem);
916633a40   Michal Hocko   locking/rwsem: Pr...
132
  extern int __must_check down_write_killable(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
135
136
  
  /*
   * trylock for writing -- returns 1 if successful, 0 if contention
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
137
  extern int down_write_trylock(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
141
  
  /*
   * release a read lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
142
  extern void up_read(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
146
  
  /*
   * release a write lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
147
  extern void up_write(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
  
  /*
   * downgrade write lock to read lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
152
153
154
155
  extern void downgrade_write(struct rw_semaphore *sem);
  
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  /*
5fca80e8b   Ingo Molnar   [PATCH] lockdep: ...
156
157
158
159
160
161
162
163
164
165
   * nested locking. NOTE: rwsems are not allowed to recurse
   * (which occurs if the same task tries to acquire the same
   * lock instance multiple times), but multiple locks of the
   * same lock class might be taken, if the order of the locks
   * is always the same. This ordering rule can be expressed
   * to lockdep via the _nested() APIs, but enumerating the
   * subclasses that are used. (If the nesting relationship is
   * static then another method for expressing nested locking is
   * the explicit definition of lock class keys and the use of
   * lockdep_set_class() at lock initialization time.
387b14684   Mauro Carvalho Chehab   docs: locking: co...
166
   * See Documentation/locking/lockdep-design.rst for more details.)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
167
168
169
   */
  extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  extern void down_write_nested(struct rw_semaphore *sem, int subclass);
887bddfa9   Al Viro   add down_write_ki...
170
  extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
1b963c81b   Jiri Kosina   lockdep, rwsem: p...
171
172
173
174
175
176
177
  extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
  
  # define down_write_nest_lock(sem, nest_lock)			\
  do {								\
  	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
  	_down_write_nest_lock(sem, &(nest_lock)->dep_map);	\
  } while (0);
84759c6d1   Kent Overstreet   Revert "rw_semaph...
178
179
180
181
182
183
184
185
  /*
   * Take/release a lock when not the owner will release it.
   *
   * [ This API should be avoided as much as possible - the
   *   proper abstraction for this case is completions. ]
   */
  extern void down_read_non_owner(struct rw_semaphore *sem);
  extern void up_read_non_owner(struct rw_semaphore *sem);
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
186
187
  #else
  # define down_read_nested(sem, subclass)		down_read(sem)
e65b9ad22   Jiri Kosina   lockdep, rwsem: f...
188
  # define down_write_nest_lock(sem, nest_lock)	down_write(sem)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
189
  # define down_write_nested(sem, subclass)	down_write(sem)
887bddfa9   Al Viro   add down_write_ki...
190
  # define down_write_killable_nested(sem, subclass)	down_write_killable(sem)
84759c6d1   Kent Overstreet   Revert "rw_semaph...
191
192
  # define down_read_non_owner(sem)		down_read(sem)
  # define up_read_non_owner(sem)			up_read(sem)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
193
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
  #endif /* _LINUX_RWSEM_H */