0001
0002
0003
0004
0005
0006
0007 #define WAIT_TABLE_BITS 8
0008 #define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
0009
0010 static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;
0011
0012 wait_queue_head_t *bit_waitqueue(void *word, int bit)
0013 {
0014 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
0015 unsigned long val = (unsigned long)word << shift | bit;
0016
0017 return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
0018 }
0019 EXPORT_SYMBOL(bit_waitqueue);
0020
0021 int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg)
0022 {
0023 struct wait_bit_key *key = arg;
0024 struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
0025
0026 if (wait_bit->key.flags != key->flags ||
0027 wait_bit->key.bit_nr != key->bit_nr ||
0028 test_bit(key->bit_nr, key->flags))
0029 return 0;
0030
0031 return autoremove_wake_function(wq_entry, mode, sync, key);
0032 }
0033 EXPORT_SYMBOL(wake_bit_function);
0034
0035
0036
0037
0038
0039
0040 int __sched
0041 __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
0042 wait_bit_action_f *action, unsigned mode)
0043 {
0044 int ret = 0;
0045
0046 do {
0047 prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
0048 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags))
0049 ret = (*action)(&wbq_entry->key, mode);
0050 } while (test_bit_acquire(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret);
0051
0052 finish_wait(wq_head, &wbq_entry->wq_entry);
0053
0054 return ret;
0055 }
0056 EXPORT_SYMBOL(__wait_on_bit);
0057
0058 int __sched out_of_line_wait_on_bit(void *word, int bit,
0059 wait_bit_action_f *action, unsigned mode)
0060 {
0061 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
0062 DEFINE_WAIT_BIT(wq_entry, word, bit);
0063
0064 return __wait_on_bit(wq_head, &wq_entry, action, mode);
0065 }
0066 EXPORT_SYMBOL(out_of_line_wait_on_bit);
0067
0068 int __sched out_of_line_wait_on_bit_timeout(
0069 void *word, int bit, wait_bit_action_f *action,
0070 unsigned mode, unsigned long timeout)
0071 {
0072 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
0073 DEFINE_WAIT_BIT(wq_entry, word, bit);
0074
0075 wq_entry.key.timeout = jiffies + timeout;
0076
0077 return __wait_on_bit(wq_head, &wq_entry, action, mode);
0078 }
0079 EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
0080
0081 int __sched
0082 __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
0083 wait_bit_action_f *action, unsigned mode)
0084 {
0085 int ret = 0;
0086
0087 for (;;) {
0088 prepare_to_wait_exclusive(wq_head, &wbq_entry->wq_entry, mode);
0089 if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
0090 ret = action(&wbq_entry->key, mode);
0091
0092
0093
0094
0095
0096
0097 if (ret)
0098 finish_wait(wq_head, &wbq_entry->wq_entry);
0099 }
0100 if (!test_and_set_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
0101 if (!ret)
0102 finish_wait(wq_head, &wbq_entry->wq_entry);
0103 return 0;
0104 } else if (ret) {
0105 return ret;
0106 }
0107 }
0108 }
0109 EXPORT_SYMBOL(__wait_on_bit_lock);
0110
0111 int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
0112 wait_bit_action_f *action, unsigned mode)
0113 {
0114 struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
0115 DEFINE_WAIT_BIT(wq_entry, word, bit);
0116
0117 return __wait_on_bit_lock(wq_head, &wq_entry, action, mode);
0118 }
0119 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
0120
0121 void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit)
0122 {
0123 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
0124
0125 if (waitqueue_active(wq_head))
0126 __wake_up(wq_head, TASK_NORMAL, 1, &key);
0127 }
0128 EXPORT_SYMBOL(__wake_up_bit);
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 void wake_up_bit(void *word, int bit)
0148 {
0149 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
0150 }
0151 EXPORT_SYMBOL(wake_up_bit);
0152
0153 wait_queue_head_t *__var_waitqueue(void *p)
0154 {
0155 return bit_wait_table + hash_ptr(p, WAIT_TABLE_BITS);
0156 }
0157 EXPORT_SYMBOL(__var_waitqueue);
0158
0159 static int
0160 var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
0161 int sync, void *arg)
0162 {
0163 struct wait_bit_key *key = arg;
0164 struct wait_bit_queue_entry *wbq_entry =
0165 container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
0166
0167 if (wbq_entry->key.flags != key->flags ||
0168 wbq_entry->key.bit_nr != key->bit_nr)
0169 return 0;
0170
0171 return autoremove_wake_function(wq_entry, mode, sync, key);
0172 }
0173
0174 void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags)
0175 {
0176 *wbq_entry = (struct wait_bit_queue_entry){
0177 .key = {
0178 .flags = (var),
0179 .bit_nr = -1,
0180 },
0181 .wq_entry = {
0182 .flags = flags,
0183 .private = current,
0184 .func = var_wake_function,
0185 .entry = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
0186 },
0187 };
0188 }
0189 EXPORT_SYMBOL(init_wait_var_entry);
0190
0191 void wake_up_var(void *var)
0192 {
0193 __wake_up_bit(__var_waitqueue(var), var, -1);
0194 }
0195 EXPORT_SYMBOL(wake_up_var);
0196
0197 __sched int bit_wait(struct wait_bit_key *word, int mode)
0198 {
0199 schedule();
0200 if (signal_pending_state(mode, current))
0201 return -EINTR;
0202
0203 return 0;
0204 }
0205 EXPORT_SYMBOL(bit_wait);
0206
0207 __sched int bit_wait_io(struct wait_bit_key *word, int mode)
0208 {
0209 io_schedule();
0210 if (signal_pending_state(mode, current))
0211 return -EINTR;
0212
0213 return 0;
0214 }
0215 EXPORT_SYMBOL(bit_wait_io);
0216
0217 __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
0218 {
0219 unsigned long now = READ_ONCE(jiffies);
0220
0221 if (time_after_eq(now, word->timeout))
0222 return -EAGAIN;
0223 schedule_timeout(word->timeout - now);
0224 if (signal_pending_state(mode, current))
0225 return -EINTR;
0226
0227 return 0;
0228 }
0229 EXPORT_SYMBOL_GPL(bit_wait_timeout);
0230
0231 __sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
0232 {
0233 unsigned long now = READ_ONCE(jiffies);
0234
0235 if (time_after_eq(now, word->timeout))
0236 return -EAGAIN;
0237 io_schedule_timeout(word->timeout - now);
0238 if (signal_pending_state(mode, current))
0239 return -EINTR;
0240
0241 return 0;
0242 }
0243 EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
0244
0245 void __init wait_bit_init(void)
0246 {
0247 int i;
0248
0249 for (i = 0; i < WAIT_TABLE_SIZE; i++)
0250 init_waitqueue_head(bit_wait_table + i);
0251 }