Commit 2fc391112fb6f3424435a3aa2fda887497b5f807

Authored by Peter Zijlstra
Committed by Ingo Molnar
1 parent beda2c7ea2

locking, sched: Give waitqueue spinlocks their own lockdep classes

Give waitqueue spinlocks their own lockdep classes when they
are initialised from init_waitqueue_head().  This means that
struct wait_queue::func functions can operate other waitqueues.

This is used by CacheFiles to catch the page from a backing fs
being unlocked and to wake up another thread to take a copy of
it.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Takashi Iwai <tiwai@suse.de>
Cc: linux-cachefs@redhat.com
Cc: torvalds@osdl.org
Cc: akpm@linux-foundation.org
LKML-Reference: <20090810113305.17284.81508.stgit@warthog.procyon.org.uk>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

Showing 2 changed files with 11 additions and 3 deletions Inline Diff

include/linux/wait.h
1 #ifndef _LINUX_WAIT_H 1 #ifndef _LINUX_WAIT_H
2 #define _LINUX_WAIT_H 2 #define _LINUX_WAIT_H
3 3
4 #define WNOHANG 0x00000001 4 #define WNOHANG 0x00000001
5 #define WUNTRACED 0x00000002 5 #define WUNTRACED 0x00000002
6 #define WSTOPPED WUNTRACED 6 #define WSTOPPED WUNTRACED
7 #define WEXITED 0x00000004 7 #define WEXITED 0x00000004
8 #define WCONTINUED 0x00000008 8 #define WCONTINUED 0x00000008
9 #define WNOWAIT 0x01000000 /* Don't reap, just poll status. */ 9 #define WNOWAIT 0x01000000 /* Don't reap, just poll status. */
10 10
11 #define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */ 11 #define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */
12 #define __WALL 0x40000000 /* Wait on all children, regardless of type */ 12 #define __WALL 0x40000000 /* Wait on all children, regardless of type */
13 #define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */ 13 #define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */
14 14
15 /* First argument to waitid: */ 15 /* First argument to waitid: */
16 #define P_ALL 0 16 #define P_ALL 0
17 #define P_PID 1 17 #define P_PID 1
18 #define P_PGID 2 18 #define P_PGID 2
19 19
20 #ifdef __KERNEL__ 20 #ifdef __KERNEL__
21 21
22 #include <linux/list.h> 22 #include <linux/list.h>
23 #include <linux/stddef.h> 23 #include <linux/stddef.h>
24 #include <linux/spinlock.h> 24 #include <linux/spinlock.h>
25 #include <asm/system.h> 25 #include <asm/system.h>
26 #include <asm/current.h> 26 #include <asm/current.h>
27 27
28 typedef struct __wait_queue wait_queue_t; 28 typedef struct __wait_queue wait_queue_t;
29 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key); 29 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
31 31
32 struct __wait_queue { 32 struct __wait_queue {
33 unsigned int flags; 33 unsigned int flags;
34 #define WQ_FLAG_EXCLUSIVE 0x01 34 #define WQ_FLAG_EXCLUSIVE 0x01
35 void *private; 35 void *private;
36 wait_queue_func_t func; 36 wait_queue_func_t func;
37 struct list_head task_list; 37 struct list_head task_list;
38 }; 38 };
39 39
40 struct wait_bit_key { 40 struct wait_bit_key {
41 void *flags; 41 void *flags;
42 int bit_nr; 42 int bit_nr;
43 }; 43 };
44 44
45 struct wait_bit_queue { 45 struct wait_bit_queue {
46 struct wait_bit_key key; 46 struct wait_bit_key key;
47 wait_queue_t wait; 47 wait_queue_t wait;
48 }; 48 };
49 49
50 struct __wait_queue_head { 50 struct __wait_queue_head {
51 spinlock_t lock; 51 spinlock_t lock;
52 struct list_head task_list; 52 struct list_head task_list;
53 }; 53 };
54 typedef struct __wait_queue_head wait_queue_head_t; 54 typedef struct __wait_queue_head wait_queue_head_t;
55 55
56 struct task_struct; 56 struct task_struct;
57 57
58 /* 58 /*
59 * Macros for declaration and initialisaton of the datatypes 59 * Macros for declaration and initialisaton of the datatypes
60 */ 60 */
61 61
62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \ 62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
63 .private = tsk, \ 63 .private = tsk, \
64 .func = default_wake_function, \ 64 .func = default_wake_function, \
65 .task_list = { NULL, NULL } } 65 .task_list = { NULL, NULL } }
66 66
67 #define DECLARE_WAITQUEUE(name, tsk) \ 67 #define DECLARE_WAITQUEUE(name, tsk) \
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk) 68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
69 69
70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ 70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ 71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
72 .task_list = { &(name).task_list, &(name).task_list } } 72 .task_list = { &(name).task_list, &(name).task_list } }
73 73
74 #define DECLARE_WAIT_QUEUE_HEAD(name) \ 74 #define DECLARE_WAIT_QUEUE_HEAD(name) \
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name) 75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
76 76
77 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \ 77 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
78 { .flags = word, .bit_nr = bit, } 78 { .flags = word, .bit_nr = bit, }
79 79
80 extern void init_waitqueue_head(wait_queue_head_t *q); 80 extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);
81
82 #define init_waitqueue_head(q) \
83 do { \
84 static struct lock_class_key __key; \
85 \
86 __init_waitqueue_head((q), &__key); \
87 } while (0)
81 88
82 #ifdef CONFIG_LOCKDEP 89 #ifdef CONFIG_LOCKDEP
83 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ 90 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
84 ({ init_waitqueue_head(&name); name; }) 91 ({ init_waitqueue_head(&name); name; })
85 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ 92 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) 93 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
87 #else 94 #else
88 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) 95 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
89 #endif 96 #endif
90 97
91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p) 98 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
92 { 99 {
93 q->flags = 0; 100 q->flags = 0;
94 q->private = p; 101 q->private = p;
95 q->func = default_wake_function; 102 q->func = default_wake_function;
96 } 103 }
97 104
98 static inline void init_waitqueue_func_entry(wait_queue_t *q, 105 static inline void init_waitqueue_func_entry(wait_queue_t *q,
99 wait_queue_func_t func) 106 wait_queue_func_t func)
100 { 107 {
101 q->flags = 0; 108 q->flags = 0;
102 q->private = NULL; 109 q->private = NULL;
103 q->func = func; 110 q->func = func;
104 } 111 }
105 112
106 static inline int waitqueue_active(wait_queue_head_t *q) 113 static inline int waitqueue_active(wait_queue_head_t *q)
107 { 114 {
108 return !list_empty(&q->task_list); 115 return !list_empty(&q->task_list);
109 } 116 }
110 117
111 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); 118 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
112 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait); 119 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
113 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); 120 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
114 121
115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) 122 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
116 { 123 {
117 list_add(&new->task_list, &head->task_list); 124 list_add(&new->task_list, &head->task_list);
118 } 125 }
119 126
120 /* 127 /*
121 * Used for wake-one threads: 128 * Used for wake-one threads:
122 */ 129 */
123 static inline void __add_wait_queue_tail(wait_queue_head_t *head, 130 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
124 wait_queue_t *new) 131 wait_queue_t *new)
125 { 132 {
126 list_add_tail(&new->task_list, &head->task_list); 133 list_add_tail(&new->task_list, &head->task_list);
127 } 134 }
128 135
129 static inline void __remove_wait_queue(wait_queue_head_t *head, 136 static inline void __remove_wait_queue(wait_queue_head_t *head,
130 wait_queue_t *old) 137 wait_queue_t *old)
131 { 138 {
132 list_del(&old->task_list); 139 list_del(&old->task_list);
133 } 140 }
134 141
135 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); 142 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
136 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); 143 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
137 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, 144 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
138 void *key); 145 void *key);
139 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode); 146 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
140 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); 147 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
141 void __wake_up_bit(wait_queue_head_t *, void *, int); 148 void __wake_up_bit(wait_queue_head_t *, void *, int);
142 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); 149 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
143 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); 150 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
144 void wake_up_bit(void *, int); 151 void wake_up_bit(void *, int);
145 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); 152 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
146 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); 153 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
147 wait_queue_head_t *bit_waitqueue(void *, int); 154 wait_queue_head_t *bit_waitqueue(void *, int);
148 155
149 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) 156 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
150 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) 157 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
151 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL) 158 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
152 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL) 159 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
153 160
154 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) 161 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
155 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) 162 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
156 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL) 163 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
157 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1) 164 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
158 165
159 /* 166 /*
160 * Wakeup macros to be used to report events to the targets. 167 * Wakeup macros to be used to report events to the targets.
161 */ 168 */
162 #define wake_up_poll(x, m) \ 169 #define wake_up_poll(x, m) \
163 __wake_up(x, TASK_NORMAL, 1, (void *) (m)) 170 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
164 #define wake_up_locked_poll(x, m) \ 171 #define wake_up_locked_poll(x, m) \
165 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m)) 172 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
166 #define wake_up_interruptible_poll(x, m) \ 173 #define wake_up_interruptible_poll(x, m) \
167 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m)) 174 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
168 #define wake_up_interruptible_sync_poll(x, m) \ 175 #define wake_up_interruptible_sync_poll(x, m) \
169 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) 176 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
170 177
171 #define __wait_event(wq, condition) \ 178 #define __wait_event(wq, condition) \
172 do { \ 179 do { \
173 DEFINE_WAIT(__wait); \ 180 DEFINE_WAIT(__wait); \
174 \ 181 \
175 for (;;) { \ 182 for (;;) { \
176 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ 183 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
177 if (condition) \ 184 if (condition) \
178 break; \ 185 break; \
179 schedule(); \ 186 schedule(); \
180 } \ 187 } \
181 finish_wait(&wq, &__wait); \ 188 finish_wait(&wq, &__wait); \
182 } while (0) 189 } while (0)
183 190
184 /** 191 /**
185 * wait_event - sleep until a condition gets true 192 * wait_event - sleep until a condition gets true
186 * @wq: the waitqueue to wait on 193 * @wq: the waitqueue to wait on
187 * @condition: a C expression for the event to wait for 194 * @condition: a C expression for the event to wait for
188 * 195 *
189 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the 196 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
190 * @condition evaluates to true. The @condition is checked each time 197 * @condition evaluates to true. The @condition is checked each time
191 * the waitqueue @wq is woken up. 198 * the waitqueue @wq is woken up.
192 * 199 *
193 * wake_up() has to be called after changing any variable that could 200 * wake_up() has to be called after changing any variable that could
194 * change the result of the wait condition. 201 * change the result of the wait condition.
195 */ 202 */
196 #define wait_event(wq, condition) \ 203 #define wait_event(wq, condition) \
197 do { \ 204 do { \
198 if (condition) \ 205 if (condition) \
199 break; \ 206 break; \
200 __wait_event(wq, condition); \ 207 __wait_event(wq, condition); \
201 } while (0) 208 } while (0)
202 209
203 #define __wait_event_timeout(wq, condition, ret) \ 210 #define __wait_event_timeout(wq, condition, ret) \
204 do { \ 211 do { \
205 DEFINE_WAIT(__wait); \ 212 DEFINE_WAIT(__wait); \
206 \ 213 \
207 for (;;) { \ 214 for (;;) { \
208 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ 215 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
209 if (condition) \ 216 if (condition) \
210 break; \ 217 break; \
211 ret = schedule_timeout(ret); \ 218 ret = schedule_timeout(ret); \
212 if (!ret) \ 219 if (!ret) \
213 break; \ 220 break; \
214 } \ 221 } \
215 finish_wait(&wq, &__wait); \ 222 finish_wait(&wq, &__wait); \
216 } while (0) 223 } while (0)
217 224
218 /** 225 /**
219 * wait_event_timeout - sleep until a condition gets true or a timeout elapses 226 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
220 * @wq: the waitqueue to wait on 227 * @wq: the waitqueue to wait on
221 * @condition: a C expression for the event to wait for 228 * @condition: a C expression for the event to wait for
222 * @timeout: timeout, in jiffies 229 * @timeout: timeout, in jiffies
223 * 230 *
224 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the 231 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
225 * @condition evaluates to true. The @condition is checked each time 232 * @condition evaluates to true. The @condition is checked each time
226 * the waitqueue @wq is woken up. 233 * the waitqueue @wq is woken up.
227 * 234 *
228 * wake_up() has to be called after changing any variable that could 235 * wake_up() has to be called after changing any variable that could
229 * change the result of the wait condition. 236 * change the result of the wait condition.
230 * 237 *
231 * The function returns 0 if the @timeout elapsed, and the remaining 238 * The function returns 0 if the @timeout elapsed, and the remaining
232 * jiffies if the condition evaluated to true before the timeout elapsed. 239 * jiffies if the condition evaluated to true before the timeout elapsed.
233 */ 240 */
234 #define wait_event_timeout(wq, condition, timeout) \ 241 #define wait_event_timeout(wq, condition, timeout) \
235 ({ \ 242 ({ \
236 long __ret = timeout; \ 243 long __ret = timeout; \
237 if (!(condition)) \ 244 if (!(condition)) \
238 __wait_event_timeout(wq, condition, __ret); \ 245 __wait_event_timeout(wq, condition, __ret); \
239 __ret; \ 246 __ret; \
240 }) 247 })
241 248
242 #define __wait_event_interruptible(wq, condition, ret) \ 249 #define __wait_event_interruptible(wq, condition, ret) \
243 do { \ 250 do { \
244 DEFINE_WAIT(__wait); \ 251 DEFINE_WAIT(__wait); \
245 \ 252 \
246 for (;;) { \ 253 for (;;) { \
247 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 254 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
248 if (condition) \ 255 if (condition) \
249 break; \ 256 break; \
250 if (!signal_pending(current)) { \ 257 if (!signal_pending(current)) { \
251 schedule(); \ 258 schedule(); \
252 continue; \ 259 continue; \
253 } \ 260 } \
254 ret = -ERESTARTSYS; \ 261 ret = -ERESTARTSYS; \
255 break; \ 262 break; \
256 } \ 263 } \
257 finish_wait(&wq, &__wait); \ 264 finish_wait(&wq, &__wait); \
258 } while (0) 265 } while (0)
259 266
260 /** 267 /**
261 * wait_event_interruptible - sleep until a condition gets true 268 * wait_event_interruptible - sleep until a condition gets true
262 * @wq: the waitqueue to wait on 269 * @wq: the waitqueue to wait on
263 * @condition: a C expression for the event to wait for 270 * @condition: a C expression for the event to wait for
264 * 271 *
265 * The process is put to sleep (TASK_INTERRUPTIBLE) until the 272 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
266 * @condition evaluates to true or a signal is received. 273 * @condition evaluates to true or a signal is received.
267 * The @condition is checked each time the waitqueue @wq is woken up. 274 * The @condition is checked each time the waitqueue @wq is woken up.
268 * 275 *
269 * wake_up() has to be called after changing any variable that could 276 * wake_up() has to be called after changing any variable that could
270 * change the result of the wait condition. 277 * change the result of the wait condition.
271 * 278 *
272 * The function will return -ERESTARTSYS if it was interrupted by a 279 * The function will return -ERESTARTSYS if it was interrupted by a
273 * signal and 0 if @condition evaluated to true. 280 * signal and 0 if @condition evaluated to true.
274 */ 281 */
275 #define wait_event_interruptible(wq, condition) \ 282 #define wait_event_interruptible(wq, condition) \
276 ({ \ 283 ({ \
277 int __ret = 0; \ 284 int __ret = 0; \
278 if (!(condition)) \ 285 if (!(condition)) \
279 __wait_event_interruptible(wq, condition, __ret); \ 286 __wait_event_interruptible(wq, condition, __ret); \
280 __ret; \ 287 __ret; \
281 }) 288 })
282 289
283 #define __wait_event_interruptible_timeout(wq, condition, ret) \ 290 #define __wait_event_interruptible_timeout(wq, condition, ret) \
284 do { \ 291 do { \
285 DEFINE_WAIT(__wait); \ 292 DEFINE_WAIT(__wait); \
286 \ 293 \
287 for (;;) { \ 294 for (;;) { \
288 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 295 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
289 if (condition) \ 296 if (condition) \
290 break; \ 297 break; \
291 if (!signal_pending(current)) { \ 298 if (!signal_pending(current)) { \
292 ret = schedule_timeout(ret); \ 299 ret = schedule_timeout(ret); \
293 if (!ret) \ 300 if (!ret) \
294 break; \ 301 break; \
295 continue; \ 302 continue; \
296 } \ 303 } \
297 ret = -ERESTARTSYS; \ 304 ret = -ERESTARTSYS; \
298 break; \ 305 break; \
299 } \ 306 } \
300 finish_wait(&wq, &__wait); \ 307 finish_wait(&wq, &__wait); \
301 } while (0) 308 } while (0)
302 309
303 /** 310 /**
304 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses 311 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
305 * @wq: the waitqueue to wait on 312 * @wq: the waitqueue to wait on
306 * @condition: a C expression for the event to wait for 313 * @condition: a C expression for the event to wait for
307 * @timeout: timeout, in jiffies 314 * @timeout: timeout, in jiffies
308 * 315 *
309 * The process is put to sleep (TASK_INTERRUPTIBLE) until the 316 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
310 * @condition evaluates to true or a signal is received. 317 * @condition evaluates to true or a signal is received.
311 * The @condition is checked each time the waitqueue @wq is woken up. 318 * The @condition is checked each time the waitqueue @wq is woken up.
312 * 319 *
313 * wake_up() has to be called after changing any variable that could 320 * wake_up() has to be called after changing any variable that could
314 * change the result of the wait condition. 321 * change the result of the wait condition.
315 * 322 *
316 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it 323 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
317 * was interrupted by a signal, and the remaining jiffies otherwise 324 * was interrupted by a signal, and the remaining jiffies otherwise
318 * if the condition evaluated to true before the timeout elapsed. 325 * if the condition evaluated to true before the timeout elapsed.
319 */ 326 */
320 #define wait_event_interruptible_timeout(wq, condition, timeout) \ 327 #define wait_event_interruptible_timeout(wq, condition, timeout) \
321 ({ \ 328 ({ \
322 long __ret = timeout; \ 329 long __ret = timeout; \
323 if (!(condition)) \ 330 if (!(condition)) \
324 __wait_event_interruptible_timeout(wq, condition, __ret); \ 331 __wait_event_interruptible_timeout(wq, condition, __ret); \
325 __ret; \ 332 __ret; \
326 }) 333 })
327 334
328 #define __wait_event_interruptible_exclusive(wq, condition, ret) \ 335 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
329 do { \ 336 do { \
330 DEFINE_WAIT(__wait); \ 337 DEFINE_WAIT(__wait); \
331 \ 338 \
332 for (;;) { \ 339 for (;;) { \
333 prepare_to_wait_exclusive(&wq, &__wait, \ 340 prepare_to_wait_exclusive(&wq, &__wait, \
334 TASK_INTERRUPTIBLE); \ 341 TASK_INTERRUPTIBLE); \
335 if (condition) { \ 342 if (condition) { \
336 finish_wait(&wq, &__wait); \ 343 finish_wait(&wq, &__wait); \
337 break; \ 344 break; \
338 } \ 345 } \
339 if (!signal_pending(current)) { \ 346 if (!signal_pending(current)) { \
340 schedule(); \ 347 schedule(); \
341 continue; \ 348 continue; \
342 } \ 349 } \
343 ret = -ERESTARTSYS; \ 350 ret = -ERESTARTSYS; \
344 abort_exclusive_wait(&wq, &__wait, \ 351 abort_exclusive_wait(&wq, &__wait, \
345 TASK_INTERRUPTIBLE, NULL); \ 352 TASK_INTERRUPTIBLE, NULL); \
346 break; \ 353 break; \
347 } \ 354 } \
348 } while (0) 355 } while (0)
349 356
350 #define wait_event_interruptible_exclusive(wq, condition) \ 357 #define wait_event_interruptible_exclusive(wq, condition) \
351 ({ \ 358 ({ \
352 int __ret = 0; \ 359 int __ret = 0; \
353 if (!(condition)) \ 360 if (!(condition)) \
354 __wait_event_interruptible_exclusive(wq, condition, __ret);\ 361 __wait_event_interruptible_exclusive(wq, condition, __ret);\
355 __ret; \ 362 __ret; \
356 }) 363 })
357 364
358 #define __wait_event_killable(wq, condition, ret) \ 365 #define __wait_event_killable(wq, condition, ret) \
359 do { \ 366 do { \
360 DEFINE_WAIT(__wait); \ 367 DEFINE_WAIT(__wait); \
361 \ 368 \
362 for (;;) { \ 369 for (;;) { \
363 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \ 370 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
364 if (condition) \ 371 if (condition) \
365 break; \ 372 break; \
366 if (!fatal_signal_pending(current)) { \ 373 if (!fatal_signal_pending(current)) { \
367 schedule(); \ 374 schedule(); \
368 continue; \ 375 continue; \
369 } \ 376 } \
370 ret = -ERESTARTSYS; \ 377 ret = -ERESTARTSYS; \
371 break; \ 378 break; \
372 } \ 379 } \
373 finish_wait(&wq, &__wait); \ 380 finish_wait(&wq, &__wait); \
374 } while (0) 381 } while (0)
375 382
376 /** 383 /**
377 * wait_event_killable - sleep until a condition gets true 384 * wait_event_killable - sleep until a condition gets true
378 * @wq: the waitqueue to wait on 385 * @wq: the waitqueue to wait on
379 * @condition: a C expression for the event to wait for 386 * @condition: a C expression for the event to wait for
380 * 387 *
381 * The process is put to sleep (TASK_KILLABLE) until the 388 * The process is put to sleep (TASK_KILLABLE) until the
382 * @condition evaluates to true or a signal is received. 389 * @condition evaluates to true or a signal is received.
383 * The @condition is checked each time the waitqueue @wq is woken up. 390 * The @condition is checked each time the waitqueue @wq is woken up.
384 * 391 *
385 * wake_up() has to be called after changing any variable that could 392 * wake_up() has to be called after changing any variable that could
386 * change the result of the wait condition. 393 * change the result of the wait condition.
387 * 394 *
388 * The function will return -ERESTARTSYS if it was interrupted by a 395 * The function will return -ERESTARTSYS if it was interrupted by a
389 * signal and 0 if @condition evaluated to true. 396 * signal and 0 if @condition evaluated to true.
390 */ 397 */
391 #define wait_event_killable(wq, condition) \ 398 #define wait_event_killable(wq, condition) \
392 ({ \ 399 ({ \
393 int __ret = 0; \ 400 int __ret = 0; \
394 if (!(condition)) \ 401 if (!(condition)) \
395 __wait_event_killable(wq, condition, __ret); \ 402 __wait_event_killable(wq, condition, __ret); \
396 __ret; \ 403 __ret; \
397 }) 404 })
398 405
399 /* 406 /*
400 * Must be called with the spinlock in the wait_queue_head_t held. 407 * Must be called with the spinlock in the wait_queue_head_t held.
401 */ 408 */
402 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q, 409 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
403 wait_queue_t * wait) 410 wait_queue_t * wait)
404 { 411 {
405 wait->flags |= WQ_FLAG_EXCLUSIVE; 412 wait->flags |= WQ_FLAG_EXCLUSIVE;
406 __add_wait_queue_tail(q, wait); 413 __add_wait_queue_tail(q, wait);
407 } 414 }
408 415
409 /* 416 /*
410 * Must be called with the spinlock in the wait_queue_head_t held. 417 * Must be called with the spinlock in the wait_queue_head_t held.
411 */ 418 */
412 static inline void remove_wait_queue_locked(wait_queue_head_t *q, 419 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
413 wait_queue_t * wait) 420 wait_queue_t * wait)
414 { 421 {
415 __remove_wait_queue(q, wait); 422 __remove_wait_queue(q, wait);
416 } 423 }
417 424
418 /* 425 /*
419 * These are the old interfaces to sleep waiting for an event. 426 * These are the old interfaces to sleep waiting for an event.
420 * They are racy. DO NOT use them, use the wait_event* interfaces above. 427 * They are racy. DO NOT use them, use the wait_event* interfaces above.
421 * We plan to remove these interfaces. 428 * We plan to remove these interfaces.
422 */ 429 */
423 extern void sleep_on(wait_queue_head_t *q); 430 extern void sleep_on(wait_queue_head_t *q);
424 extern long sleep_on_timeout(wait_queue_head_t *q, 431 extern long sleep_on_timeout(wait_queue_head_t *q,
425 signed long timeout); 432 signed long timeout);
426 extern void interruptible_sleep_on(wait_queue_head_t *q); 433 extern void interruptible_sleep_on(wait_queue_head_t *q);
427 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, 434 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
428 signed long timeout); 435 signed long timeout);
429 436
430 /* 437 /*
431 * Waitqueues which are removed from the waitqueue_head at wakeup time 438 * Waitqueues which are removed from the waitqueue_head at wakeup time
432 */ 439 */
433 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); 440 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
434 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state); 441 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
435 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); 442 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
436 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, 443 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
437 unsigned int mode, void *key); 444 unsigned int mode, void *key);
438 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 445 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
439 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 446 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
440 447
441 #define DEFINE_WAIT_FUNC(name, function) \ 448 #define DEFINE_WAIT_FUNC(name, function) \
442 wait_queue_t name = { \ 449 wait_queue_t name = { \
443 .private = current, \ 450 .private = current, \
444 .func = function, \ 451 .func = function, \
445 .task_list = LIST_HEAD_INIT((name).task_list), \ 452 .task_list = LIST_HEAD_INIT((name).task_list), \
446 } 453 }
447 454
448 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) 455 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
449 456
450 #define DEFINE_WAIT_BIT(name, word, bit) \ 457 #define DEFINE_WAIT_BIT(name, word, bit) \
451 struct wait_bit_queue name = { \ 458 struct wait_bit_queue name = { \
452 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \ 459 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
453 .wait = { \ 460 .wait = { \
454 .private = current, \ 461 .private = current, \
455 .func = wake_bit_function, \ 462 .func = wake_bit_function, \
456 .task_list = \ 463 .task_list = \
457 LIST_HEAD_INIT((name).wait.task_list), \ 464 LIST_HEAD_INIT((name).wait.task_list), \
458 }, \ 465 }, \
459 } 466 }
460 467
461 #define init_wait(wait) \ 468 #define init_wait(wait) \
462 do { \ 469 do { \
463 (wait)->private = current; \ 470 (wait)->private = current; \
464 (wait)->func = autoremove_wake_function; \ 471 (wait)->func = autoremove_wake_function; \
465 INIT_LIST_HEAD(&(wait)->task_list); \ 472 INIT_LIST_HEAD(&(wait)->task_list); \
466 } while (0) 473 } while (0)
467 474
468 /** 475 /**
469 * wait_on_bit - wait for a bit to be cleared 476 * wait_on_bit - wait for a bit to be cleared
470 * @word: the word being waited on, a kernel virtual address 477 * @word: the word being waited on, a kernel virtual address
471 * @bit: the bit of the word being waited on 478 * @bit: the bit of the word being waited on
472 * @action: the function used to sleep, which may take special actions 479 * @action: the function used to sleep, which may take special actions
473 * @mode: the task state to sleep in 480 * @mode: the task state to sleep in
474 * 481 *
475 * There is a standard hashed waitqueue table for generic use. This 482 * There is a standard hashed waitqueue table for generic use. This
476 * is the part of the hashtable's accessor API that waits on a bit. 483 * is the part of the hashtable's accessor API that waits on a bit.
477 * For instance, if one were to have waiters on a bitflag, one would 484 * For instance, if one were to have waiters on a bitflag, one would
478 * call wait_on_bit() in threads waiting for the bit to clear. 485 * call wait_on_bit() in threads waiting for the bit to clear.
479 * One uses wait_on_bit() where one is waiting for the bit to clear, 486 * One uses wait_on_bit() where one is waiting for the bit to clear,
480 * but has no intention of setting it. 487 * but has no intention of setting it.
481 */ 488 */
482 static inline int wait_on_bit(void *word, int bit, 489 static inline int wait_on_bit(void *word, int bit,
483 int (*action)(void *), unsigned mode) 490 int (*action)(void *), unsigned mode)
484 { 491 {
485 if (!test_bit(bit, word)) 492 if (!test_bit(bit, word))
486 return 0; 493 return 0;
487 return out_of_line_wait_on_bit(word, bit, action, mode); 494 return out_of_line_wait_on_bit(word, bit, action, mode);
488 } 495 }
489 496
490 /** 497 /**
491 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it 498 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
492 * @word: the word being waited on, a kernel virtual address 499 * @word: the word being waited on, a kernel virtual address
493 * @bit: the bit of the word being waited on 500 * @bit: the bit of the word being waited on
494 * @action: the function used to sleep, which may take special actions 501 * @action: the function used to sleep, which may take special actions
495 * @mode: the task state to sleep in 502 * @mode: the task state to sleep in
496 * 503 *
497 * There is a standard hashed waitqueue table for generic use. This 504 * There is a standard hashed waitqueue table for generic use. This
498 * is the part of the hashtable's accessor API that waits on a bit 505 * is the part of the hashtable's accessor API that waits on a bit
499 * when one intends to set it, for instance, trying to lock bitflags. 506 * when one intends to set it, for instance, trying to lock bitflags.
500 * For instance, if one were to have waiters trying to set bitflag 507 * For instance, if one were to have waiters trying to set bitflag
501 * and waiting for it to clear before setting it, one would call 508 * and waiting for it to clear before setting it, one would call
502 * wait_on_bit() in threads waiting to be able to set the bit. 509 * wait_on_bit() in threads waiting to be able to set the bit.
503 * One uses wait_on_bit_lock() where one is waiting for the bit to 510 * One uses wait_on_bit_lock() where one is waiting for the bit to
504 * clear with the intention of setting it, and when done, clearing it. 511 * clear with the intention of setting it, and when done, clearing it.
505 */ 512 */
506 static inline int wait_on_bit_lock(void *word, int bit, 513 static inline int wait_on_bit_lock(void *word, int bit,
507 int (*action)(void *), unsigned mode) 514 int (*action)(void *), unsigned mode)
508 { 515 {
509 if (!test_and_set_bit(bit, word)) 516 if (!test_and_set_bit(bit, word))
510 return 0; 517 return 0;
511 return out_of_line_wait_on_bit_lock(word, bit, action, mode); 518 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
512 } 519 }
513 520
514 #endif /* __KERNEL__ */ 521 #endif /* __KERNEL__ */
515 522
516 #endif 523 #endif
517 524
1 /* 1 /*
2 * Generic waiting primitives. 2 * Generic waiting primitives.
3 * 3 *
4 * (C) 2004 William Irwin, Oracle 4 * (C) 2004 William Irwin, Oracle
5 */ 5 */
6 #include <linux/init.h> 6 #include <linux/init.h>
7 #include <linux/module.h> 7 #include <linux/module.h>
8 #include <linux/sched.h> 8 #include <linux/sched.h>
9 #include <linux/mm.h> 9 #include <linux/mm.h>
10 #include <linux/wait.h> 10 #include <linux/wait.h>
11 #include <linux/hash.h> 11 #include <linux/hash.h>
12 12
13 void init_waitqueue_head(wait_queue_head_t *q) 13 void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *key)
14 { 14 {
15 spin_lock_init(&q->lock); 15 spin_lock_init(&q->lock);
16 lockdep_set_class(&q->lock, key);
16 INIT_LIST_HEAD(&q->task_list); 17 INIT_LIST_HEAD(&q->task_list);
17 } 18 }
18 19
19 EXPORT_SYMBOL(init_waitqueue_head); 20 EXPORT_SYMBOL(__init_waitqueue_head);
20 21
21 void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait) 22 void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
22 { 23 {
23 unsigned long flags; 24 unsigned long flags;
24 25
25 wait->flags &= ~WQ_FLAG_EXCLUSIVE; 26 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
26 spin_lock_irqsave(&q->lock, flags); 27 spin_lock_irqsave(&q->lock, flags);
27 __add_wait_queue(q, wait); 28 __add_wait_queue(q, wait);
28 spin_unlock_irqrestore(&q->lock, flags); 29 spin_unlock_irqrestore(&q->lock, flags);
29 } 30 }
30 EXPORT_SYMBOL(add_wait_queue); 31 EXPORT_SYMBOL(add_wait_queue);
31 32
32 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait) 33 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
33 { 34 {
34 unsigned long flags; 35 unsigned long flags;
35 36
36 wait->flags |= WQ_FLAG_EXCLUSIVE; 37 wait->flags |= WQ_FLAG_EXCLUSIVE;
37 spin_lock_irqsave(&q->lock, flags); 38 spin_lock_irqsave(&q->lock, flags);
38 __add_wait_queue_tail(q, wait); 39 __add_wait_queue_tail(q, wait);
39 spin_unlock_irqrestore(&q->lock, flags); 40 spin_unlock_irqrestore(&q->lock, flags);
40 } 41 }
41 EXPORT_SYMBOL(add_wait_queue_exclusive); 42 EXPORT_SYMBOL(add_wait_queue_exclusive);
42 43
43 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait) 44 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
44 { 45 {
45 unsigned long flags; 46 unsigned long flags;
46 47
47 spin_lock_irqsave(&q->lock, flags); 48 spin_lock_irqsave(&q->lock, flags);
48 __remove_wait_queue(q, wait); 49 __remove_wait_queue(q, wait);
49 spin_unlock_irqrestore(&q->lock, flags); 50 spin_unlock_irqrestore(&q->lock, flags);
50 } 51 }
51 EXPORT_SYMBOL(remove_wait_queue); 52 EXPORT_SYMBOL(remove_wait_queue);
52 53
53 54
54 /* 55 /*
55 * Note: we use "set_current_state()" _after_ the wait-queue add, 56 * Note: we use "set_current_state()" _after_ the wait-queue add,
56 * because we need a memory barrier there on SMP, so that any 57 * because we need a memory barrier there on SMP, so that any
57 * wake-function that tests for the wait-queue being active 58 * wake-function that tests for the wait-queue being active
58 * will be guaranteed to see waitqueue addition _or_ subsequent 59 * will be guaranteed to see waitqueue addition _or_ subsequent
59 * tests in this thread will see the wakeup having taken place. 60 * tests in this thread will see the wakeup having taken place.
60 * 61 *
61 * The spin_unlock() itself is semi-permeable and only protects 62 * The spin_unlock() itself is semi-permeable and only protects
62 * one way (it only protects stuff inside the critical region and 63 * one way (it only protects stuff inside the critical region and
63 * stops them from bleeding out - it would still allow subsequent 64 * stops them from bleeding out - it would still allow subsequent
64 * loads to move into the critical region). 65 * loads to move into the critical region).
65 */ 66 */
66 void 67 void
67 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state) 68 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
68 { 69 {
69 unsigned long flags; 70 unsigned long flags;
70 71
71 wait->flags &= ~WQ_FLAG_EXCLUSIVE; 72 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
72 spin_lock_irqsave(&q->lock, flags); 73 spin_lock_irqsave(&q->lock, flags);
73 if (list_empty(&wait->task_list)) 74 if (list_empty(&wait->task_list))
74 __add_wait_queue(q, wait); 75 __add_wait_queue(q, wait);
75 set_current_state(state); 76 set_current_state(state);
76 spin_unlock_irqrestore(&q->lock, flags); 77 spin_unlock_irqrestore(&q->lock, flags);
77 } 78 }
78 EXPORT_SYMBOL(prepare_to_wait); 79 EXPORT_SYMBOL(prepare_to_wait);
79 80
80 void 81 void
81 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state) 82 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
82 { 83 {
83 unsigned long flags; 84 unsigned long flags;
84 85
85 wait->flags |= WQ_FLAG_EXCLUSIVE; 86 wait->flags |= WQ_FLAG_EXCLUSIVE;
86 spin_lock_irqsave(&q->lock, flags); 87 spin_lock_irqsave(&q->lock, flags);
87 if (list_empty(&wait->task_list)) 88 if (list_empty(&wait->task_list))
88 __add_wait_queue_tail(q, wait); 89 __add_wait_queue_tail(q, wait);
89 set_current_state(state); 90 set_current_state(state);
90 spin_unlock_irqrestore(&q->lock, flags); 91 spin_unlock_irqrestore(&q->lock, flags);
91 } 92 }
92 EXPORT_SYMBOL(prepare_to_wait_exclusive); 93 EXPORT_SYMBOL(prepare_to_wait_exclusive);
93 94
94 /* 95 /*
95 * finish_wait - clean up after waiting in a queue 96 * finish_wait - clean up after waiting in a queue
96 * @q: waitqueue waited on 97 * @q: waitqueue waited on
97 * @wait: wait descriptor 98 * @wait: wait descriptor
98 * 99 *
99 * Sets current thread back to running state and removes 100 * Sets current thread back to running state and removes
100 * the wait descriptor from the given waitqueue if still 101 * the wait descriptor from the given waitqueue if still
101 * queued. 102 * queued.
102 */ 103 */
103 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait) 104 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
104 { 105 {
105 unsigned long flags; 106 unsigned long flags;
106 107
107 __set_current_state(TASK_RUNNING); 108 __set_current_state(TASK_RUNNING);
108 /* 109 /*
109 * We can check for list emptiness outside the lock 110 * We can check for list emptiness outside the lock
110 * IFF: 111 * IFF:
111 * - we use the "careful" check that verifies both 112 * - we use the "careful" check that verifies both
112 * the next and prev pointers, so that there cannot 113 * the next and prev pointers, so that there cannot
113 * be any half-pending updates in progress on other 114 * be any half-pending updates in progress on other
114 * CPU's that we haven't seen yet (and that might 115 * CPU's that we haven't seen yet (and that might
115 * still change the stack area. 116 * still change the stack area.
116 * and 117 * and
117 * - all other users take the lock (ie we can only 118 * - all other users take the lock (ie we can only
118 * have _one_ other CPU that looks at or modifies 119 * have _one_ other CPU that looks at or modifies
119 * the list). 120 * the list).
120 */ 121 */
121 if (!list_empty_careful(&wait->task_list)) { 122 if (!list_empty_careful(&wait->task_list)) {
122 spin_lock_irqsave(&q->lock, flags); 123 spin_lock_irqsave(&q->lock, flags);
123 list_del_init(&wait->task_list); 124 list_del_init(&wait->task_list);
124 spin_unlock_irqrestore(&q->lock, flags); 125 spin_unlock_irqrestore(&q->lock, flags);
125 } 126 }
126 } 127 }
127 EXPORT_SYMBOL(finish_wait); 128 EXPORT_SYMBOL(finish_wait);
128 129
129 /* 130 /*
130 * abort_exclusive_wait - abort exclusive waiting in a queue 131 * abort_exclusive_wait - abort exclusive waiting in a queue
131 * @q: waitqueue waited on 132 * @q: waitqueue waited on
132 * @wait: wait descriptor 133 * @wait: wait descriptor
133 * @state: runstate of the waiter to be woken 134 * @state: runstate of the waiter to be woken
134 * @key: key to identify a wait bit queue or %NULL 135 * @key: key to identify a wait bit queue or %NULL
135 * 136 *
136 * Sets current thread back to running state and removes 137 * Sets current thread back to running state and removes
137 * the wait descriptor from the given waitqueue if still 138 * the wait descriptor from the given waitqueue if still
138 * queued. 139 * queued.
139 * 140 *
140 * Wakes up the next waiter if the caller is concurrently 141 * Wakes up the next waiter if the caller is concurrently
141 * woken up through the queue. 142 * woken up through the queue.
142 * 143 *
143 * This prevents waiter starvation where an exclusive waiter 144 * This prevents waiter starvation where an exclusive waiter
144 * aborts and is woken up concurrently and noone wakes up 145 * aborts and is woken up concurrently and noone wakes up
145 * the next waiter. 146 * the next waiter.
146 */ 147 */
147 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, 148 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
148 unsigned int mode, void *key) 149 unsigned int mode, void *key)
149 { 150 {
150 unsigned long flags; 151 unsigned long flags;
151 152
152 __set_current_state(TASK_RUNNING); 153 __set_current_state(TASK_RUNNING);
153 spin_lock_irqsave(&q->lock, flags); 154 spin_lock_irqsave(&q->lock, flags);
154 if (!list_empty(&wait->task_list)) 155 if (!list_empty(&wait->task_list))
155 list_del_init(&wait->task_list); 156 list_del_init(&wait->task_list);
156 else if (waitqueue_active(q)) 157 else if (waitqueue_active(q))
157 __wake_up_locked_key(q, mode, key); 158 __wake_up_locked_key(q, mode, key);
158 spin_unlock_irqrestore(&q->lock, flags); 159 spin_unlock_irqrestore(&q->lock, flags);
159 } 160 }
160 EXPORT_SYMBOL(abort_exclusive_wait); 161 EXPORT_SYMBOL(abort_exclusive_wait);
161 162
162 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key) 163 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
163 { 164 {
164 int ret = default_wake_function(wait, mode, sync, key); 165 int ret = default_wake_function(wait, mode, sync, key);
165 166
166 if (ret) 167 if (ret)
167 list_del_init(&wait->task_list); 168 list_del_init(&wait->task_list);
168 return ret; 169 return ret;
169 } 170 }
170 EXPORT_SYMBOL(autoremove_wake_function); 171 EXPORT_SYMBOL(autoremove_wake_function);
171 172
172 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg) 173 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
173 { 174 {
174 struct wait_bit_key *key = arg; 175 struct wait_bit_key *key = arg;
175 struct wait_bit_queue *wait_bit 176 struct wait_bit_queue *wait_bit
176 = container_of(wait, struct wait_bit_queue, wait); 177 = container_of(wait, struct wait_bit_queue, wait);
177 178
178 if (wait_bit->key.flags != key->flags || 179 if (wait_bit->key.flags != key->flags ||
179 wait_bit->key.bit_nr != key->bit_nr || 180 wait_bit->key.bit_nr != key->bit_nr ||
180 test_bit(key->bit_nr, key->flags)) 181 test_bit(key->bit_nr, key->flags))
181 return 0; 182 return 0;
182 else 183 else
183 return autoremove_wake_function(wait, mode, sync, key); 184 return autoremove_wake_function(wait, mode, sync, key);
184 } 185 }
185 EXPORT_SYMBOL(wake_bit_function); 186 EXPORT_SYMBOL(wake_bit_function);
186 187
187 /* 188 /*
188 * To allow interruptible waiting and asynchronous (i.e. nonblocking) 189 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
189 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are 190 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
190 * permitted return codes. Nonzero return codes halt waiting and return. 191 * permitted return codes. Nonzero return codes halt waiting and return.
191 */ 192 */
192 int __sched 193 int __sched
193 __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q, 194 __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
194 int (*action)(void *), unsigned mode) 195 int (*action)(void *), unsigned mode)
195 { 196 {
196 int ret = 0; 197 int ret = 0;
197 198
198 do { 199 do {
199 prepare_to_wait(wq, &q->wait, mode); 200 prepare_to_wait(wq, &q->wait, mode);
200 if (test_bit(q->key.bit_nr, q->key.flags)) 201 if (test_bit(q->key.bit_nr, q->key.flags))
201 ret = (*action)(q->key.flags); 202 ret = (*action)(q->key.flags);
202 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret); 203 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
203 finish_wait(wq, &q->wait); 204 finish_wait(wq, &q->wait);
204 return ret; 205 return ret;
205 } 206 }
206 EXPORT_SYMBOL(__wait_on_bit); 207 EXPORT_SYMBOL(__wait_on_bit);
207 208
208 int __sched out_of_line_wait_on_bit(void *word, int bit, 209 int __sched out_of_line_wait_on_bit(void *word, int bit,
209 int (*action)(void *), unsigned mode) 210 int (*action)(void *), unsigned mode)
210 { 211 {
211 wait_queue_head_t *wq = bit_waitqueue(word, bit); 212 wait_queue_head_t *wq = bit_waitqueue(word, bit);
212 DEFINE_WAIT_BIT(wait, word, bit); 213 DEFINE_WAIT_BIT(wait, word, bit);
213 214
214 return __wait_on_bit(wq, &wait, action, mode); 215 return __wait_on_bit(wq, &wait, action, mode);
215 } 216 }
216 EXPORT_SYMBOL(out_of_line_wait_on_bit); 217 EXPORT_SYMBOL(out_of_line_wait_on_bit);
217 218
218 int __sched 219 int __sched
219 __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q, 220 __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
220 int (*action)(void *), unsigned mode) 221 int (*action)(void *), unsigned mode)
221 { 222 {
222 do { 223 do {
223 int ret; 224 int ret;
224 225
225 prepare_to_wait_exclusive(wq, &q->wait, mode); 226 prepare_to_wait_exclusive(wq, &q->wait, mode);
226 if (!test_bit(q->key.bit_nr, q->key.flags)) 227 if (!test_bit(q->key.bit_nr, q->key.flags))
227 continue; 228 continue;
228 ret = action(q->key.flags); 229 ret = action(q->key.flags);
229 if (!ret) 230 if (!ret)
230 continue; 231 continue;
231 abort_exclusive_wait(wq, &q->wait, mode, &q->key); 232 abort_exclusive_wait(wq, &q->wait, mode, &q->key);
232 return ret; 233 return ret;
233 } while (test_and_set_bit(q->key.bit_nr, q->key.flags)); 234 } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
234 finish_wait(wq, &q->wait); 235 finish_wait(wq, &q->wait);
235 return 0; 236 return 0;
236 } 237 }
237 EXPORT_SYMBOL(__wait_on_bit_lock); 238 EXPORT_SYMBOL(__wait_on_bit_lock);
238 239
239 int __sched out_of_line_wait_on_bit_lock(void *word, int bit, 240 int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
240 int (*action)(void *), unsigned mode) 241 int (*action)(void *), unsigned mode)
241 { 242 {
242 wait_queue_head_t *wq = bit_waitqueue(word, bit); 243 wait_queue_head_t *wq = bit_waitqueue(word, bit);
243 DEFINE_WAIT_BIT(wait, word, bit); 244 DEFINE_WAIT_BIT(wait, word, bit);
244 245
245 return __wait_on_bit_lock(wq, &wait, action, mode); 246 return __wait_on_bit_lock(wq, &wait, action, mode);
246 } 247 }
247 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock); 248 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
248 249
249 void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit) 250 void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
250 { 251 {
251 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit); 252 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
252 if (waitqueue_active(wq)) 253 if (waitqueue_active(wq))
253 __wake_up(wq, TASK_NORMAL, 1, &key); 254 __wake_up(wq, TASK_NORMAL, 1, &key);
254 } 255 }
255 EXPORT_SYMBOL(__wake_up_bit); 256 EXPORT_SYMBOL(__wake_up_bit);
256 257
257 /** 258 /**
258 * wake_up_bit - wake up a waiter on a bit 259 * wake_up_bit - wake up a waiter on a bit
259 * @word: the word being waited on, a kernel virtual address 260 * @word: the word being waited on, a kernel virtual address
260 * @bit: the bit of the word being waited on 261 * @bit: the bit of the word being waited on
261 * 262 *
262 * There is a standard hashed waitqueue table for generic use. This 263 * There is a standard hashed waitqueue table for generic use. This
263 * is the part of the hashtable's accessor API that wakes up waiters 264 * is the part of the hashtable's accessor API that wakes up waiters
264 * on a bit. For instance, if one were to have waiters on a bitflag, 265 * on a bit. For instance, if one were to have waiters on a bitflag,
265 * one would call wake_up_bit() after clearing the bit. 266 * one would call wake_up_bit() after clearing the bit.
266 * 267 *
267 * In order for this to function properly, as it uses waitqueue_active() 268 * In order for this to function properly, as it uses waitqueue_active()
268 * internally, some kind of memory barrier must be done prior to calling 269 * internally, some kind of memory barrier must be done prior to calling
269 * this. Typically, this will be smp_mb__after_clear_bit(), but in some 270 * this. Typically, this will be smp_mb__after_clear_bit(), but in some
270 * cases where bitflags are manipulated non-atomically under a lock, one 271 * cases where bitflags are manipulated non-atomically under a lock, one
271 * may need to use a less regular barrier, such fs/inode.c's smp_mb(), 272 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
272 * because spin_unlock() does not guarantee a memory barrier. 273 * because spin_unlock() does not guarantee a memory barrier.
273 */ 274 */
274 void wake_up_bit(void *word, int bit) 275 void wake_up_bit(void *word, int bit)
275 { 276 {
276 __wake_up_bit(bit_waitqueue(word, bit), word, bit); 277 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
277 } 278 }
278 EXPORT_SYMBOL(wake_up_bit); 279 EXPORT_SYMBOL(wake_up_bit);
279 280
280 wait_queue_head_t *bit_waitqueue(void *word, int bit) 281 wait_queue_head_t *bit_waitqueue(void *word, int bit)
281 { 282 {
282 const int shift = BITS_PER_LONG == 32 ? 5 : 6; 283 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
283 const struct zone *zone = page_zone(virt_to_page(word)); 284 const struct zone *zone = page_zone(virt_to_page(word));
284 unsigned long val = (unsigned long)word << shift | bit; 285 unsigned long val = (unsigned long)word << shift | bit;
285 286
286 return &zone->wait_table[hash_long(val, zone->wait_table_bits)]; 287 return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
287 } 288 }
288 EXPORT_SYMBOL(bit_waitqueue); 289 EXPORT_SYMBOL(bit_waitqueue);
289 290