Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / kernel / wait.c
1 /*
2  * Generic waiting primitives.
3  *
4  * (C) 2004 William Irwin, Oracle
5  */
6 #include <linux/config.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/wait.h>
12 #include <linux/hash.h>
13
14 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
15 {
16         unsigned long flags;
17
18         wait->flags &= ~WQ_FLAG_EXCLUSIVE;
19         spin_lock_irqsave(&q->lock, flags);
20         __add_wait_queue(q, wait);
21         spin_unlock_irqrestore(&q->lock, flags);
22 }
23 EXPORT_SYMBOL(add_wait_queue);
24
25 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
26 {
27         unsigned long flags;
28
29         wait->flags |= WQ_FLAG_EXCLUSIVE;
30         spin_lock_irqsave(&q->lock, flags);
31         __add_wait_queue_tail(q, wait);
32         spin_unlock_irqrestore(&q->lock, flags);
33 }
34 EXPORT_SYMBOL(add_wait_queue_exclusive);
35
36 int fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
37 {
38         unsigned long flags;
39         struct list_head *list;
40         int seen, retval;
41
42         spin_lock_irqsave(&q->lock, flags);
43         list = &q->task_list;
44         seen = 0;
45         retval = -1;
46
47         do {
48                 struct list_head *next;
49                 if (list == &wait->task_list)
50                         seen++;
51                 next = list->next;
52                 if (next->prev != list) {
53                         seen += 2;
54                         break;
55                 }
56                 list = next;
57         } while (list != &q->task_list);
58
59         if (seen == 1) {
60                 __remove_wait_queue(q, wait);
61                 retval = 0;
62         }
63         spin_unlock_irqrestore(&q->lock, flags);
64         return retval;
65 }
66 EXPORT_SYMBOL(remove_wait_queue);
67
68
69 /*
70  * Note: we use "set_current_state()" _after_ the wait-queue add,
71  * because we need a memory barrier there on SMP, so that any
72  * wake-function that tests for the wait-queue being active
73  * will be guaranteed to see waitqueue addition _or_ subsequent
74  * tests in this thread will see the wakeup having taken place.
75  *
76  * The spin_unlock() itself is semi-permeable and only protects
77  * one way (it only protects stuff inside the critical region and
78  * stops them from bleeding out - it would still allow subsequent
79  * loads to move into the the critical region).
80  */
81 void fastcall
82 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
83 {
84         unsigned long flags;
85
86         wait->flags &= ~WQ_FLAG_EXCLUSIVE;
87         spin_lock_irqsave(&q->lock, flags);
88         if (list_empty(&wait->task_list))
89                 __add_wait_queue(q, wait);
90         /*
91          * don't alter the task state if this is just going to
92          * queue an async wait queue callback
93          */
94         if (is_sync_wait(wait))
95                 set_current_state(state);
96         spin_unlock_irqrestore(&q->lock, flags);
97 }
98 EXPORT_SYMBOL(prepare_to_wait);
99
100 void fastcall
101 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
102 {
103         unsigned long flags;
104
105         wait->flags |= WQ_FLAG_EXCLUSIVE;
106         spin_lock_irqsave(&q->lock, flags);
107         if (list_empty(&wait->task_list))
108                 __add_wait_queue_tail(q, wait);
109         /*
110          * don't alter the task state if this is just going to
111          * queue an async wait queue callback
112          */
113         if (is_sync_wait(wait))
114                 set_current_state(state);
115         spin_unlock_irqrestore(&q->lock, flags);
116 }
117 EXPORT_SYMBOL(prepare_to_wait_exclusive);
118
119 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
120 {
121         unsigned long flags;
122
123         __set_current_state(TASK_RUNNING);
124         /*
125          * We can check for list emptiness outside the lock
126          * IFF:
127          *  - we use the "careful" check that verifies both
128          *    the next and prev pointers, so that there cannot
129          *    be any half-pending updates in progress on other
130          *    CPU's that we haven't seen yet (and that might
131          *    still change the stack area.
132          * and
133          *  - all other users take the lock (ie we can only
134          *    have _one_ other CPU that looks at or modifies
135          *    the list).
136          */
137         if (!list_empty_careful(&wait->task_list)) {
138                 spin_lock_irqsave(&q->lock, flags);
139                 list_del_init(&wait->task_list);
140                 spin_unlock_irqrestore(&q->lock, flags);
141         }
142 }
143 EXPORT_SYMBOL(finish_wait);
144
145 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
146 {
147         int ret = default_wake_function(wait, mode, sync, key);
148
149         if (ret)
150                 list_del_init(&wait->task_list);
151         return ret;
152 }
153 EXPORT_SYMBOL(autoremove_wake_function);
154
155 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
156 {
157         struct wait_bit_key *key = arg;
158         struct wait_bit_queue *wait_bit
159                 = container_of(wait, struct wait_bit_queue, wait);
160
161         if (wait_bit->key.flags != key->flags ||
162                         wait_bit->key.bit_nr != key->bit_nr ||
163                         test_bit(key->bit_nr, key->flags))
164                 return 0;
165         else
166                 return autoremove_wake_function(wait, mode, sync, key);
167 }
168 EXPORT_SYMBOL(wake_bit_function);
169
170 /*
171  * To allow interruptible waiting and asynchronous (i.e. nonblocking)
172  * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
173  * permitted return codes. Nonzero return codes halt waiting and return.
174  */
175 int __sched fastcall
176 __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
177                         int (*action)(void *), unsigned mode)
178 {
179         int ret = 0;
180
181         do {
182                 prepare_to_wait(wq, &q->wait, mode);
183                 if (test_bit(q->key.bit_nr, q->key.flags))
184                         ret = (*action)(q->key.flags);
185         } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
186         finish_wait(wq, &q->wait);
187         return ret;
188 }
189 EXPORT_SYMBOL(__wait_on_bit);
190
191 int __sched fastcall out_of_line_wait_on_bit(void *word, int bit,
192                                         int (*action)(void *), unsigned mode)
193 {
194         wait_queue_head_t *wq = bit_waitqueue(word, bit);
195         DEFINE_WAIT_BIT(wait, word, bit);
196
197         return __wait_on_bit(wq, &wait, action, mode);
198 }
199 EXPORT_SYMBOL(out_of_line_wait_on_bit);
200
201 int __sched fastcall
202 __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
203                         int (*action)(void *), unsigned mode)
204 {
205         int ret = 0;
206
207         do {
208                 prepare_to_wait_exclusive(wq, &q->wait, mode);
209                 if (test_bit(q->key.bit_nr, q->key.flags)) {
210                         if ((ret = (*action)(q->key.flags)))
211                                 break;
212                 }
213         } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
214         finish_wait(wq, &q->wait);
215         return ret;
216 }
217 EXPORT_SYMBOL(__wait_on_bit_lock);
218
219 int __sched fastcall out_of_line_wait_on_bit_lock(void *word, int bit,
220                                         int (*action)(void *), unsigned mode)
221 {
222         wait_queue_head_t *wq = bit_waitqueue(word, bit);
223         DEFINE_WAIT_BIT(wait, word, bit);
224
225         return __wait_on_bit_lock(wq, &wait, action, mode);
226 }
227 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
228
229 void fastcall __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
230 {
231         struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
232         if (waitqueue_active(wq))
233                 __wake_up(wq, TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE, 1, &key);
234 }
235 EXPORT_SYMBOL(__wake_up_bit);
236
237 /**
238  * wake_up_bit - wake up a waiter on a bit
239  * @word: the word being waited on, a kernel virtual address
240  * @bit: the bit of the word being waited on
241  *
242  * There is a standard hashed waitqueue table for generic use. This
243  * is the part of the hashtable's accessor API that wakes up waiters
244  * on a bit. For instance, if one were to have waiters on a bitflag,
245  * one would call wake_up_bit() after clearing the bit.
246  *
247  * In order for this to function properly, as it uses waitqueue_active()
248  * internally, some kind of memory barrier must be done prior to calling
249  * this. Typically, this will be smp_mb__after_clear_bit(), but in some
250  * cases where bitflags are manipulated non-atomically under a lock, one
251  * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
252  * because spin_unlock() does not guarantee a memory barrier.
253  */
254 void fastcall wake_up_bit(void *word, int bit)
255 {
256         __wake_up_bit(bit_waitqueue(word, bit), word, bit);
257 }
258 EXPORT_SYMBOL(wake_up_bit);
259
260 fastcall wait_queue_head_t *bit_waitqueue(void *word, int bit)
261 {
262         const int shift = BITS_PER_LONG == 32 ? 5 : 6;
263         const struct zone *zone = page_zone(virt_to_page(word));
264         unsigned long val = (unsigned long)word << shift | bit;
265
266         return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
267 }
268 EXPORT_SYMBOL(bit_waitqueue);