Loading...
1/*
2 * The implementation of the wait_bit*() and related waiting APIs:
3 */
4#include "sched.h"
5
6#define WAIT_TABLE_BITS 8
7#define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
8
9static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;
10
11wait_queue_head_t *bit_waitqueue(void *word, int bit)
12{
13 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
14 unsigned long val = (unsigned long)word << shift | bit;
15
16 return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
17}
18EXPORT_SYMBOL(bit_waitqueue);
19
20int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg)
21{
22 struct wait_bit_key *key = arg;
23 struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
24
25 if (wait_bit->key.flags != key->flags ||
26 wait_bit->key.bit_nr != key->bit_nr ||
27 test_bit(key->bit_nr, key->flags))
28 return 0;
29
30 return autoremove_wake_function(wq_entry, mode, sync, key);
31}
32EXPORT_SYMBOL(wake_bit_function);
33
34/*
35 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
36 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
37 * permitted return codes. Nonzero return codes halt waiting and return.
38 */
39int __sched
40__wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
41 wait_bit_action_f *action, unsigned mode)
42{
43 int ret = 0;
44
45 do {
46 prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
47 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags))
48 ret = (*action)(&wbq_entry->key, mode);
49 } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret);
50
51 finish_wait(wq_head, &wbq_entry->wq_entry);
52
53 return ret;
54}
55EXPORT_SYMBOL(__wait_on_bit);
56
57int __sched out_of_line_wait_on_bit(void *word, int bit,
58 wait_bit_action_f *action, unsigned mode)
59{
60 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
61 DEFINE_WAIT_BIT(wq_entry, word, bit);
62
63 return __wait_on_bit(wq_head, &wq_entry, action, mode);
64}
65EXPORT_SYMBOL(out_of_line_wait_on_bit);
66
67int __sched out_of_line_wait_on_bit_timeout(
68 void *word, int bit, wait_bit_action_f *action,
69 unsigned mode, unsigned long timeout)
70{
71 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
72 DEFINE_WAIT_BIT(wq_entry, word, bit);
73
74 wq_entry.key.timeout = jiffies + timeout;
75
76 return __wait_on_bit(wq_head, &wq_entry, action, mode);
77}
78EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
79
80int __sched
81__wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
82 wait_bit_action_f *action, unsigned mode)
83{
84 int ret = 0;
85
86 for (;;) {
87 prepare_to_wait_exclusive(wq_head, &wbq_entry->wq_entry, mode);
88 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
89 ret = action(&wbq_entry->key, mode);
90 /*
91 * See the comment in prepare_to_wait_event().
92 * finish_wait() does not necessarily takes wwq_head->lock,
93 * but test_and_set_bit() implies mb() which pairs with
94 * smp_mb__after_atomic() before wake_up_page().
95 */
96 if (ret)
97 finish_wait(wq_head, &wbq_entry->wq_entry);
98 }
99 if (!test_and_set_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
100 if (!ret)
101 finish_wait(wq_head, &wbq_entry->wq_entry);
102 return 0;
103 } else if (ret) {
104 return ret;
105 }
106 }
107}
108EXPORT_SYMBOL(__wait_on_bit_lock);
109
110int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
111 wait_bit_action_f *action, unsigned mode)
112{
113 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
114 DEFINE_WAIT_BIT(wq_entry, word, bit);
115
116 return __wait_on_bit_lock(wq_head, &wq_entry, action, mode);
117}
118EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
119
120void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit)
121{
122 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
123
124 if (waitqueue_active(wq_head))
125 __wake_up(wq_head, TASK_NORMAL, 1, &key);
126}
127EXPORT_SYMBOL(__wake_up_bit);
128
129/**
130 * wake_up_bit - wake up a waiter on a bit
131 * @word: the word being waited on, a kernel virtual address
132 * @bit: the bit of the word being waited on
133 *
134 * There is a standard hashed waitqueue table for generic use. This
135 * is the part of the hashtable's accessor API that wakes up waiters
136 * on a bit. For instance, if one were to have waiters on a bitflag,
137 * one would call wake_up_bit() after clearing the bit.
138 *
139 * In order for this to function properly, as it uses waitqueue_active()
140 * internally, some kind of memory barrier must be done prior to calling
141 * this. Typically, this will be smp_mb__after_atomic(), but in some
142 * cases where bitflags are manipulated non-atomically under a lock, one
143 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
144 * because spin_unlock() does not guarantee a memory barrier.
145 */
146void wake_up_bit(void *word, int bit)
147{
148 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
149}
150EXPORT_SYMBOL(wake_up_bit);
151
152wait_queue_head_t *__var_waitqueue(void *p)
153{
154 return bit_wait_table + hash_ptr(p, WAIT_TABLE_BITS);
155}
156EXPORT_SYMBOL(__var_waitqueue);
157
158static int
159var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
160 int sync, void *arg)
161{
162 struct wait_bit_key *key = arg;
163 struct wait_bit_queue_entry *wbq_entry =
164 container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
165
166 if (wbq_entry->key.flags != key->flags ||
167 wbq_entry->key.bit_nr != key->bit_nr)
168 return 0;
169
170 return autoremove_wake_function(wq_entry, mode, sync, key);
171}
172
173void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags)
174{
175 *wbq_entry = (struct wait_bit_queue_entry){
176 .key = {
177 .flags = (var),
178 .bit_nr = -1,
179 },
180 .wq_entry = {
181 .private = current,
182 .func = var_wake_function,
183 .entry = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
184 },
185 };
186}
187EXPORT_SYMBOL(init_wait_var_entry);
188
189void wake_up_var(void *var)
190{
191 __wake_up_bit(__var_waitqueue(var), var, -1);
192}
193EXPORT_SYMBOL(wake_up_var);
194
195__sched int bit_wait(struct wait_bit_key *word, int mode)
196{
197 schedule();
198 if (signal_pending_state(mode, current))
199 return -EINTR;
200
201 return 0;
202}
203EXPORT_SYMBOL(bit_wait);
204
205__sched int bit_wait_io(struct wait_bit_key *word, int mode)
206{
207 io_schedule();
208 if (signal_pending_state(mode, current))
209 return -EINTR;
210
211 return 0;
212}
213EXPORT_SYMBOL(bit_wait_io);
214
215__sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
216{
217 unsigned long now = READ_ONCE(jiffies);
218
219 if (time_after_eq(now, word->timeout))
220 return -EAGAIN;
221 schedule_timeout(word->timeout - now);
222 if (signal_pending_state(mode, current))
223 return -EINTR;
224
225 return 0;
226}
227EXPORT_SYMBOL_GPL(bit_wait_timeout);
228
229__sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
230{
231 unsigned long now = READ_ONCE(jiffies);
232
233 if (time_after_eq(now, word->timeout))
234 return -EAGAIN;
235 io_schedule_timeout(word->timeout - now);
236 if (signal_pending_state(mode, current))
237 return -EINTR;
238
239 return 0;
240}
241EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
242
243void __init wait_bit_init(void)
244{
245 int i;
246
247 for (i = 0; i < WAIT_TABLE_SIZE; i++)
248 init_waitqueue_head(bit_wait_table + i);
249}
1// SPDX-License-Identifier: GPL-2.0-only
2
3/*
4 * The implementation of the wait_bit*() and related waiting APIs:
5 */
6
7#define WAIT_TABLE_BITS 8
8#define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
9
10static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;
11
12wait_queue_head_t *bit_waitqueue(unsigned long *word, int bit)
13{
14 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
15 unsigned long val = (unsigned long)word << shift | bit;
16
17 return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
18}
19EXPORT_SYMBOL(bit_waitqueue);
20
21int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg)
22{
23 struct wait_bit_key *key = arg;
24 struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
25
26 if (wait_bit->key.flags != key->flags ||
27 wait_bit->key.bit_nr != key->bit_nr ||
28 test_bit(key->bit_nr, key->flags))
29 return 0;
30
31 return autoremove_wake_function(wq_entry, mode, sync, key);
32}
33EXPORT_SYMBOL(wake_bit_function);
34
35/*
36 * To allow interruptible waiting and asynchronous (i.e. non-blocking)
37 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
38 * permitted return codes. Nonzero return codes halt waiting and return.
39 */
40int __sched
41__wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
42 wait_bit_action_f *action, unsigned mode)
43{
44 int ret = 0;
45
46 do {
47 prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
48 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags))
49 ret = (*action)(&wbq_entry->key, mode);
50 } while (test_bit_acquire(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret);
51
52 finish_wait(wq_head, &wbq_entry->wq_entry);
53
54 return ret;
55}
56EXPORT_SYMBOL(__wait_on_bit);
57
58int __sched out_of_line_wait_on_bit(unsigned long *word, int bit,
59 wait_bit_action_f *action, unsigned mode)
60{
61 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
62 DEFINE_WAIT_BIT(wq_entry, word, bit);
63
64 return __wait_on_bit(wq_head, &wq_entry, action, mode);
65}
66EXPORT_SYMBOL(out_of_line_wait_on_bit);
67
68int __sched out_of_line_wait_on_bit_timeout(
69 unsigned long *word, int bit, wait_bit_action_f *action,
70 unsigned mode, unsigned long timeout)
71{
72 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
73 DEFINE_WAIT_BIT(wq_entry, word, bit);
74
75 wq_entry.key.timeout = jiffies + timeout;
76
77 return __wait_on_bit(wq_head, &wq_entry, action, mode);
78}
79EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
80
81int __sched
82__wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
83 wait_bit_action_f *action, unsigned mode)
84{
85 int ret = 0;
86
87 for (;;) {
88 prepare_to_wait_exclusive(wq_head, &wbq_entry->wq_entry, mode);
89 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
90 ret = action(&wbq_entry->key, mode);
91 /*
92 * See the comment in prepare_to_wait_event().
93 * finish_wait() does not necessarily takes wwq_head->lock,
94 * but test_and_set_bit() implies mb() which pairs with
95 * smp_mb__after_atomic() before wake_up_page().
96 */
97 if (ret)
98 finish_wait(wq_head, &wbq_entry->wq_entry);
99 }
100 if (!test_and_set_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
101 if (!ret)
102 finish_wait(wq_head, &wbq_entry->wq_entry);
103 return 0;
104 } else if (ret) {
105 return ret;
106 }
107 }
108}
109EXPORT_SYMBOL(__wait_on_bit_lock);
110
111int __sched out_of_line_wait_on_bit_lock(unsigned long *word, int bit,
112 wait_bit_action_f *action, unsigned mode)
113{
114 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
115 DEFINE_WAIT_BIT(wq_entry, word, bit);
116
117 return __wait_on_bit_lock(wq_head, &wq_entry, action, mode);
118}
119EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
120
121void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit)
122{
123 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
124
125 if (waitqueue_active(wq_head))
126 __wake_up(wq_head, TASK_NORMAL, 1, &key);
127}
128EXPORT_SYMBOL(__wake_up_bit);
129
130/**
131 * wake_up_bit - wake up waiters on a bit
132 * @word: the address containing the bit being waited on
133 * @bit: the bit at that address being waited on
134 *
135 * Wake up any process waiting in wait_on_bit() or similar for the
136 * given bit to be cleared.
137 *
138 * The wake-up is sent to tasks in a waitqueue selected by hash from a
139 * shared pool. Only those tasks on that queue which have requested
140 * wake_up on this specific address and bit will be woken, and only if the
141 * bit is clear.
142 *
143 * In order for this to function properly there must be a full memory
144 * barrier after the bit is cleared and before this function is called.
145 * If the bit was cleared atomically, such as a by clear_bit() then
146 * smb_mb__after_atomic() can be used, othwewise smb_mb() is needed.
147 * If the bit was cleared with a fully-ordered operation, no further
148 * barrier is required.
149 *
150 * Normally the bit should be cleared by an operation with RELEASE
151 * semantics so that any changes to memory made before the bit is
152 * cleared are guaranteed to be visible after the matching wait_on_bit()
153 * completes.
154 */
155void wake_up_bit(unsigned long *word, int bit)
156{
157 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
158}
159EXPORT_SYMBOL(wake_up_bit);
160
161wait_queue_head_t *__var_waitqueue(void *p)
162{
163 return bit_wait_table + hash_ptr(p, WAIT_TABLE_BITS);
164}
165EXPORT_SYMBOL(__var_waitqueue);
166
167static int
168var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
169 int sync, void *arg)
170{
171 struct wait_bit_key *key = arg;
172 struct wait_bit_queue_entry *wbq_entry =
173 container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
174
175 if (wbq_entry->key.flags != key->flags ||
176 wbq_entry->key.bit_nr != key->bit_nr)
177 return 0;
178
179 return autoremove_wake_function(wq_entry, mode, sync, key);
180}
181
182void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags)
183{
184 *wbq_entry = (struct wait_bit_queue_entry){
185 .key = {
186 .flags = (var),
187 .bit_nr = -1,
188 },
189 .wq_entry = {
190 .flags = flags,
191 .private = current,
192 .func = var_wake_function,
193 .entry = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
194 },
195 };
196}
197EXPORT_SYMBOL(init_wait_var_entry);
198
199/**
200 * wake_up_var - wake up waiters on a variable (kernel address)
201 * @var: the address of the variable being waited on
202 *
203 * Wake up any process waiting in wait_var_event() or similar for the
204 * given variable to change. wait_var_event() can be waiting for an
205 * arbitrary condition to be true and associates that condition with an
206 * address. Calling wake_up_var() suggests that the condition has been
207 * made true, but does not strictly require the condtion to use the
208 * address given.
209 *
210 * The wake-up is sent to tasks in a waitqueue selected by hash from a
211 * shared pool. Only those tasks on that queue which have requested
212 * wake_up on this specific address will be woken.
213 *
214 * In order for this to function properly there must be a full memory
215 * barrier after the variable is updated (or more accurately, after the
216 * condition waited on has been made to be true) and before this function
217 * is called. If the variable was updated atomically, such as a by
218 * atomic_dec() then smb_mb__after_atomic() can be used. If the
219 * variable was updated by a fully ordered operation such as
220 * atomic_dec_and_test() then no extra barrier is required. Otherwise
221 * smb_mb() is needed.
222 *
223 * Normally the variable should be updated (the condition should be made
224 * to be true) by an operation with RELEASE semantics such as
225 * smp_store_release() so that any changes to memory made before the
226 * variable was updated are guaranteed to be visible after the matching
227 * wait_var_event() completes.
228 */
229void wake_up_var(void *var)
230{
231 __wake_up_bit(__var_waitqueue(var), var, -1);
232}
233EXPORT_SYMBOL(wake_up_var);
234
235__sched int bit_wait(struct wait_bit_key *word, int mode)
236{
237 schedule();
238 if (signal_pending_state(mode, current))
239 return -EINTR;
240
241 return 0;
242}
243EXPORT_SYMBOL(bit_wait);
244
245__sched int bit_wait_io(struct wait_bit_key *word, int mode)
246{
247 io_schedule();
248 if (signal_pending_state(mode, current))
249 return -EINTR;
250
251 return 0;
252}
253EXPORT_SYMBOL(bit_wait_io);
254
255__sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
256{
257 unsigned long now = READ_ONCE(jiffies);
258
259 if (time_after_eq(now, word->timeout))
260 return -EAGAIN;
261 schedule_timeout(word->timeout - now);
262 if (signal_pending_state(mode, current))
263 return -EINTR;
264
265 return 0;
266}
267EXPORT_SYMBOL_GPL(bit_wait_timeout);
268
269void __init wait_bit_init(void)
270{
271 int i;
272
273 for (i = 0; i < WAIT_TABLE_SIZE; i++)
274 init_waitqueue_head(bit_wait_table + i);
275}