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