| 1 | |
| 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 |
|
| 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 |
|
| 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
|
| 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 |
|
| 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 |
|
| 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
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 | struct wait_bit_key *__var_wake_key(struct wait_queue_entry *wq_entry, void *arg)
|
| 171 | {
|
| 172 | struct wait_bit_key *key = arg;
|
| 173 | struct wait_bit_queue_entry *wbq_entry =
|
| 174 | container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
|
| 175 |
|
| 176 | if (wbq_entry->key.flags != key->flags ||
|
| 177 | wbq_entry->key.bit_nr != key->bit_nr)
|
| 178 | return NULL;
|
| 179 |
|
| 180 | return key;
|
| 181 | }
|
| 182 |
|
| 183 | static int var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
|
| 184 | int sync, void *arg)
|
| 185 | {
|
| 186 | struct wait_bit_key *key = __var_wake_key(wq_entry, arg);
|
| 187 | if (!key)
|
| 188 | return 0;
|
| 189 |
|
| 190 | return autoremove_wake_function(wq_entry, mode, sync, key);
|
| 191 | }
|
| 192 |
|
| 193 | void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags)
|
| 194 | {
|
| 195 | *wbq_entry = (struct wait_bit_queue_entry){
|
| 196 | .key = {
|
| 197 | .flags = (var),
|
| 198 | .bit_nr = -1,
|
| 199 | },
|
| 200 | .wq_entry = {
|
| 201 | .flags = flags,
|
| 202 | .private = current,
|
| 203 | .func = var_wake_function,
|
| 204 | .entry = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
|
| 205 | },
|
| 206 | };
|
| 207 | }
|
| 208 | EXPORT_SYMBOL(init_wait_var_entry);
|
| 209 |
|
| 210 | |
| 211 | * wake_up_var - wake up waiters on a variable (kernel address)
|
| 212 | * @var: the address of the variable being waited on
|
| 213 | *
|
| 214 | * Wake up any process waiting in wait_var_event() or similar for the
|
| 215 | * given variable to change. wait_var_event() can be waiting for an
|
| 216 | * arbitrary condition to be true and associates that condition with an
|
| 217 | * address. Calling wake_up_var() suggests that the condition has been
|
| 218 | * made true, but does not strictly require the condtion to use the
|
| 219 | * address given.
|
| 220 | *
|
| 221 | * The wake-up is sent to tasks in a waitqueue selected by hash from a
|
| 222 | * shared pool. Only those tasks on that queue which have requested
|
| 223 | * wake_up on this specific address will be woken.
|
| 224 | *
|
| 225 | * In order for this to function properly there must be a full memory
|
| 226 | * barrier after the variable is updated (or more accurately, after the
|
| 227 | * condition waited on has been made to be true) and before this function
|
| 228 | * is called. If the variable was updated atomically, such as a by
|
| 229 | * atomic_dec() then smb_mb__after_atomic() can be used. If the
|
| 230 | * variable was updated by a fully ordered operation such as
|
| 231 | * atomic_dec_and_test() then no extra barrier is required. Otherwise
|
| 232 | * smb_mb() is needed.
|
| 233 | *
|
| 234 | * Normally the variable should be updated (the condition should be made
|
| 235 | * to be true) by an operation with RELEASE semantics such as
|
| 236 | * smp_store_release() so that any changes to memory made before the
|
| 237 | * variable was updated are guaranteed to be visible after the matching
|
| 238 | * wait_var_event() completes.
|
| 239 |
|
| 240 | void wake_up_var(void *var)
|
| 241 | {
|
| 242 | __wake_up_bit(__var_waitqueue(var), var, -1);
|
| 243 | }
|
| 244 | EXPORT_SYMBOL(wake_up_var);
|
| 245 |
|
| 246 | __sched int bit_wait(struct wait_bit_key *word, int mode)
|
| 247 | {
|
| 248 | schedule();
|
| 249 | if (signal_pending_state(mode, current))
|
| 250 | return -EINTR;
|
| 251 |
|
| 252 | return 0;
|
| 253 | }
|
| 254 | EXPORT_SYMBOL(bit_wait);
|
| 255 |
|
| 256 | __sched int bit_wait_io(struct wait_bit_key *word, int mode)
|
| 257 | {
|
| 258 | io_schedule();
|
| 259 | if (signal_pending_state(mode, current))
|
| 260 | return -EINTR;
|
| 261 |
|
| 262 | return 0;
|
| 263 | }
|
| 264 | EXPORT_SYMBOL(bit_wait_io);
|
| 265 |
|
| 266 | __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
|
| 267 | {
|
| 268 | unsigned long now = READ_ONCE(jiffies);
|
| 269 |
|
| 270 | if (time_after_eq(now, word->timeout))
|
| 271 | return -EAGAIN;
|
| 272 | schedule_timeout(word->timeout - now);
|
| 273 | if (signal_pending_state(mode, current))
|
| 274 | return -EINTR;
|
| 275 |
|
| 276 | return 0;
|
| 277 | }
|
| 278 | EXPORT_SYMBOL_GPL(bit_wait_timeout);
|
| 279 |
|
| 280 | void __init wait_bit_init(void)
|
| 281 | {
|
| 282 | int i;
|
| 283 |
|
| 284 | for (i = 0; i < WAIT_TABLE_SIZE; i++)
|
| 285 | init_waitqueue_head(bit_wait_table + i);
|
| 286 | }
|