1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_SWAIT_H 2 #ifndef _LINUX_SWAIT_H 3 #define _LINUX_SWAIT_H 3 #define _LINUX_SWAIT_H 4 4 5 #include <linux/list.h> 5 #include <linux/list.h> 6 #include <linux/stddef.h> 6 #include <linux/stddef.h> 7 #include <linux/spinlock.h> 7 #include <linux/spinlock.h> 8 #include <linux/wait.h> 8 #include <linux/wait.h> 9 #include <asm/current.h> 9 #include <asm/current.h> 10 10 11 /* 11 /* 12 * Simple waitqueues are semantically very dif !! 12 * BROKEN wait-queues. 13 * (wait.h). The most important difference is !! 13 * 14 * for deterministic behaviour -- IOW it has s !! 14 * These "simple" wait-queues are broken garbage, and should never be 15 * times. !! 15 * used. The comments below claim that they are "similar" to regular >> 16 * wait-queues, but the semantics are actually completely different, and >> 17 * every single user we have ever had has been buggy (or pointless). >> 18 * >> 19 * A "swake_up_one()" only wakes up _one_ waiter, which is not at all what >> 20 * "wake_up()" does, and has led to problems. In other cases, it has >> 21 * been fine, because there's only ever one waiter (kvm), but in that >> 22 * case gthe whole "simple" wait-queue is just pointless to begin with, >> 23 * since there is no "queue". Use "wake_up_process()" with a direct >> 24 * pointer instead. >> 25 * >> 26 * While these are very similar to regular wait queues (wait.h) the most >> 27 * important difference is that the simple waitqueue allows for deterministic >> 28 * behaviour -- IOW it has strictly bounded IRQ and lock hold times. 16 * 29 * 17 * Mainly, this is accomplished by two things. 30 * Mainly, this is accomplished by two things. Firstly not allowing swake_up_all 18 * from IRQ disabled, and dropping the lock up 31 * from IRQ disabled, and dropping the lock upon every wakeup, giving a higher 19 * priority task a chance to run. 32 * priority task a chance to run. 20 * 33 * 21 * Secondly, we had to drop a fair number of f 34 * Secondly, we had to drop a fair number of features of the other waitqueue 22 * code; notably: 35 * code; notably: 23 * 36 * 24 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE 37 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue; 25 * all wakeups are TASK_NORMAL in order to 38 * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right 26 * sleeper state. 39 * sleeper state. 27 * 40 * 28 * - the !exclusive mode; because that leads 41 * - the !exclusive mode; because that leads to O(n) wakeups, everything is 29 * exclusive. As such swake_up_one will onl !! 42 * exclusive. 30 * 43 * 31 * - custom wake callback functions; because 44 * - custom wake callback functions; because you cannot give any guarantees 32 * about random code. This also allows swai 45 * about random code. This also allows swait to be used in RT, such that 33 * raw spinlock can be used for the swait q 46 * raw spinlock can be used for the swait queue head. 34 * 47 * 35 * As a side effect of these; the data structu 48 * As a side effect of these; the data structures are slimmer albeit more ad-hoc. 36 * For all the above, note that simple wait qu 49 * For all the above, note that simple wait queues should _only_ be used under 37 * very specific realtime constraints -- it is 50 * very specific realtime constraints -- it is best to stick with the regular 38 * wait queues in most cases. 51 * wait queues in most cases. 39 */ 52 */ 40 53 41 struct task_struct; 54 struct task_struct; 42 55 43 struct swait_queue_head { 56 struct swait_queue_head { 44 raw_spinlock_t lock; 57 raw_spinlock_t lock; 45 struct list_head task_list; 58 struct list_head task_list; 46 }; 59 }; 47 60 48 struct swait_queue { 61 struct swait_queue { 49 struct task_struct *task; 62 struct task_struct *task; 50 struct list_head task_list; 63 struct list_head task_list; 51 }; 64 }; 52 65 53 #define __SWAITQUEUE_INITIALIZER(name) { 66 #define __SWAITQUEUE_INITIALIZER(name) { \ 54 .task = current, 67 .task = current, \ 55 .task_list = LIST_HEAD_INIT((name 68 .task_list = LIST_HEAD_INIT((name).task_list), \ 56 } 69 } 57 70 58 #define DECLARE_SWAITQUEUE(name) 71 #define DECLARE_SWAITQUEUE(name) \ 59 struct swait_queue name = __SWAITQUEUE 72 struct swait_queue name = __SWAITQUEUE_INITIALIZER(name) 60 73 61 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { 74 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \ 62 .lock = __RAW_SPIN_LOCK_UNLO 75 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ 63 .task_list = LIST_HEAD_INIT((name 76 .task_list = LIST_HEAD_INIT((name).task_list), \ 64 } 77 } 65 78 66 #define DECLARE_SWAIT_QUEUE_HEAD(name) 79 #define DECLARE_SWAIT_QUEUE_HEAD(name) \ 67 struct swait_queue_head name = __SWAIT 80 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name) 68 81 69 extern void __init_swait_queue_head(struct swa 82 extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name, 70 struct loc 83 struct lock_class_key *key); 71 84 72 #define init_swait_queue_head(q) 85 #define init_swait_queue_head(q) \ 73 do { 86 do { \ 74 static struct lock_class_key _ 87 static struct lock_class_key __key; \ 75 __init_swait_queue_head((q), # 88 __init_swait_queue_head((q), #q, &__key); \ 76 } while (0) 89 } while (0) 77 90 78 #ifdef CONFIG_LOCKDEP 91 #ifdef CONFIG_LOCKDEP 79 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) 92 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ 80 ({ init_swait_queue_head(&name); name; 93 ({ init_swait_queue_head(&name); name; }) 81 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name 94 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \ 82 struct swait_queue_head name = __SWAIT 95 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) 83 #else 96 #else 84 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name 97 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \ 85 DECLARE_SWAIT_QUEUE_HEAD(name) 98 DECLARE_SWAIT_QUEUE_HEAD(name) 86 #endif 99 #endif 87 100 88 /** 101 /** 89 * swait_active -- locklessly test for waiters 102 * swait_active -- locklessly test for waiters on the queue 90 * @wq: the waitqueue to test for waiters 103 * @wq: the waitqueue to test for waiters 91 * 104 * 92 * returns true if the wait list is not empty 105 * returns true if the wait list is not empty 93 * 106 * 94 * NOTE: this function is lockless and require 107 * NOTE: this function is lockless and requires care, incorrect usage _will_ 95 * lead to sporadic and non-obvious failure. 108 * lead to sporadic and non-obvious failure. 96 * 109 * 97 * NOTE2: this function has the same above imp 110 * NOTE2: this function has the same above implications as regular waitqueues. 98 * 111 * 99 * Use either while holding swait_queue_head:: 112 * Use either while holding swait_queue_head::lock or when used for wakeups 100 * with an extra smp_mb() like: 113 * with an extra smp_mb() like: 101 * 114 * 102 * CPU0 - waker CPU1 - 115 * CPU0 - waker CPU1 - waiter 103 * 116 * 104 * for (; 117 * for (;;) { 105 * @cond = true; prep 118 * @cond = true; prepare_to_swait_exclusive(&wq_head, &wait, state); 106 * smp_mb(); // s 119 * smp_mb(); // smp_mb() from set_current_state() 107 * if (swait_active(wq_head)) if ( 120 * if (swait_active(wq_head)) if (@cond) 108 * wake_up(wq_head); 121 * wake_up(wq_head); break; 109 * sche 122 * schedule(); 110 * } 123 * } 111 * finish 124 * finish_swait(&wq_head, &wait); 112 * 125 * 113 * Because without the explicit smp_mb() it's 126 * Because without the explicit smp_mb() it's possible for the 114 * swait_active() load to get hoisted over the 127 * swait_active() load to get hoisted over the @cond store such that we'll 115 * observe an empty wait list while the waiter 128 * observe an empty wait list while the waiter might not observe @cond. 116 * This, in turn, can trigger missing wakeups. 129 * This, in turn, can trigger missing wakeups. 117 * 130 * 118 * Also note that this 'optimization' trades a 131 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(), 119 * which (when the lock is uncontended) are of 132 * which (when the lock is uncontended) are of roughly equal cost. 120 */ 133 */ 121 static inline int swait_active(struct swait_qu 134 static inline int swait_active(struct swait_queue_head *wq) 122 { 135 { 123 return !list_empty(&wq->task_list); 136 return !list_empty(&wq->task_list); 124 } 137 } 125 138 126 /** 139 /** 127 * swq_has_sleeper - check if there are any wa 140 * swq_has_sleeper - check if there are any waiting processes 128 * @wq: the waitqueue to test for waiters 141 * @wq: the waitqueue to test for waiters 129 * 142 * 130 * Returns true if @wq has waiting processes 143 * Returns true if @wq has waiting processes 131 * 144 * 132 * Please refer to the comment for swait_activ 145 * Please refer to the comment for swait_active. 133 */ 146 */ 134 static inline bool swq_has_sleeper(struct swai 147 static inline bool swq_has_sleeper(struct swait_queue_head *wq) 135 { 148 { 136 /* 149 /* 137 * We need to be sure we are in sync w 150 * We need to be sure we are in sync with the list_add() 138 * modifications to the wait queue (ta 151 * modifications to the wait queue (task_list). 139 * 152 * 140 * This memory barrier should be paire 153 * This memory barrier should be paired with one on the 141 * waiting side. 154 * waiting side. 142 */ 155 */ 143 smp_mb(); 156 smp_mb(); 144 return swait_active(wq); 157 return swait_active(wq); 145 } 158 } 146 159 147 extern void swake_up_one(struct swait_queue_he 160 extern void swake_up_one(struct swait_queue_head *q); 148 extern void swake_up_all(struct swait_queue_he 161 extern void swake_up_all(struct swait_queue_head *q); 149 extern void swake_up_locked(struct swait_queue !! 162 extern void swake_up_locked(struct swait_queue_head *q); 150 163 151 extern void prepare_to_swait_exclusive(struct 164 extern void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state); 152 extern long prepare_to_swait_event(struct swai 165 extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state); 153 166 154 extern void __finish_swait(struct swait_queue_ 167 extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait); 155 extern void finish_swait(struct swait_queue_he 168 extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait); 156 169 157 /* as per ___wait_event() but for swait, there 170 /* as per ___wait_event() but for swait, therefore "exclusive == 1" */ 158 #define ___swait_event(wq, condition, state, r 171 #define ___swait_event(wq, condition, state, ret, cmd) \ 159 ({ 172 ({ \ 160 __label__ __out; 173 __label__ __out; \ 161 struct swait_queue __wait; 174 struct swait_queue __wait; \ 162 long __ret = ret; 175 long __ret = ret; \ 163 176 \ 164 INIT_LIST_HEAD(&__wait.task_list); 177 INIT_LIST_HEAD(&__wait.task_list); \ 165 for (;;) { 178 for (;;) { \ 166 long __int = prepare_to_swait_ 179 long __int = prepare_to_swait_event(&wq, &__wait, state);\ 167 180 \ 168 if (condition) 181 if (condition) \ 169 break; 182 break; \ 170 183 \ 171 if (___wait_is_interruptible(s 184 if (___wait_is_interruptible(state) && __int) { \ 172 __ret = __int; 185 __ret = __int; \ 173 goto __out; 186 goto __out; \ 174 } 187 } \ 175 188 \ 176 cmd; 189 cmd; \ 177 } 190 } \ 178 finish_swait(&wq, &__wait); 191 finish_swait(&wq, &__wait); \ 179 __out: __ret; 192 __out: __ret; \ 180 }) 193 }) 181 194 182 #define __swait_event(wq, condition) 195 #define __swait_event(wq, condition) \ 183 (void)___swait_event(wq, condition, TA 196 (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \ 184 schedule()) 197 schedule()) 185 198 186 #define swait_event_exclusive(wq, condition) 199 #define swait_event_exclusive(wq, condition) \ 187 do { 200 do { \ 188 if (condition) 201 if (condition) \ 189 break; 202 break; \ 190 __swait_event(wq, condition); 203 __swait_event(wq, condition); \ 191 } while (0) 204 } while (0) 192 205 193 #define __swait_event_timeout(wq, condition, t 206 #define __swait_event_timeout(wq, condition, timeout) \ 194 ___swait_event(wq, ___wait_cond_timeou 207 ___swait_event(wq, ___wait_cond_timeout(condition), \ 195 TASK_UNINTERRUPTIBLE, ti 208 TASK_UNINTERRUPTIBLE, timeout, \ 196 __ret = schedule_timeout 209 __ret = schedule_timeout(__ret)) 197 210 198 #define swait_event_timeout_exclusive(wq, cond 211 #define swait_event_timeout_exclusive(wq, condition, timeout) \ 199 ({ 212 ({ \ 200 long __ret = timeout; 213 long __ret = timeout; \ 201 if (!___wait_cond_timeout(condition)) 214 if (!___wait_cond_timeout(condition)) \ 202 __ret = __swait_event_timeout( 215 __ret = __swait_event_timeout(wq, condition, timeout); \ 203 __ret; 216 __ret; \ 204 }) 217 }) 205 218 206 #define __swait_event_interruptible(wq, condit 219 #define __swait_event_interruptible(wq, condition) \ 207 ___swait_event(wq, condition, TASK_INT 220 ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \ 208 schedule()) 221 schedule()) 209 222 210 #define swait_event_interruptible_exclusive(wq 223 #define swait_event_interruptible_exclusive(wq, condition) \ 211 ({ 224 ({ \ 212 int __ret = 0; 225 int __ret = 0; \ 213 if (!(condition)) 226 if (!(condition)) \ 214 __ret = __swait_event_interrup 227 __ret = __swait_event_interruptible(wq, condition); \ 215 __ret; 228 __ret; \ 216 }) 229 }) 217 230 218 #define __swait_event_interruptible_timeout(wq 231 #define __swait_event_interruptible_timeout(wq, condition, timeout) \ 219 ___swait_event(wq, ___wait_cond_timeou 232 ___swait_event(wq, ___wait_cond_timeout(condition), \ 220 TASK_INTERRUPTIBLE, time 233 TASK_INTERRUPTIBLE, timeout, \ 221 __ret = schedule_timeout 234 __ret = schedule_timeout(__ret)) 222 235 223 #define swait_event_interruptible_timeout_excl 236 #define swait_event_interruptible_timeout_exclusive(wq, condition, timeout)\ 224 ({ 237 ({ \ 225 long __ret = timeout; 238 long __ret = timeout; \ 226 if (!___wait_cond_timeout(condition)) 239 if (!___wait_cond_timeout(condition)) \ 227 __ret = __swait_event_interrup 240 __ret = __swait_event_interruptible_timeout(wq, \ 228 241 condition, timeout); \ 229 __ret; 242 __ret; \ 230 }) 243 }) 231 244 232 #define __swait_event_idle(wq, condition) 245 #define __swait_event_idle(wq, condition) \ 233 (void)___swait_event(wq, condition, TA 246 (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule()) 234 247 235 /** 248 /** 236 * swait_event_idle_exclusive - wait without s 249 * swait_event_idle_exclusive - wait without system load contribution 237 * @wq: the waitqueue to wait on 250 * @wq: the waitqueue to wait on 238 * @condition: a C expression for the event to 251 * @condition: a C expression for the event to wait for 239 * 252 * 240 * The process is put to sleep (TASK_IDLE) unt 253 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to 241 * true. The @condition is checked each time t 254 * true. The @condition is checked each time the waitqueue @wq is woken up. 242 * 255 * 243 * This function is mostly used when a kthread 256 * This function is mostly used when a kthread or workqueue waits for some 244 * condition and doesn't want to contribute to 257 * condition and doesn't want to contribute to system load. Signals are 245 * ignored. 258 * ignored. 246 */ 259 */ 247 #define swait_event_idle_exclusive(wq, conditi 260 #define swait_event_idle_exclusive(wq, condition) \ 248 do { 261 do { \ 249 if (condition) 262 if (condition) \ 250 break; 263 break; \ 251 __swait_event_idle(wq, condition); 264 __swait_event_idle(wq, condition); \ 252 } while (0) 265 } while (0) 253 266 254 #define __swait_event_idle_timeout(wq, conditi 267 #define __swait_event_idle_timeout(wq, condition, timeout) \ 255 ___swait_event(wq, ___wait_cond_timeou 268 ___swait_event(wq, ___wait_cond_timeout(condition), \ 256 TASK_IDLE, timeout, 269 TASK_IDLE, timeout, \ 257 __ret = schedule_timeou 270 __ret = schedule_timeout(__ret)) 258 271 259 /** 272 /** 260 * swait_event_idle_timeout_exclusive - wait u 273 * swait_event_idle_timeout_exclusive - wait up to timeout without load contribution 261 * @wq: the waitqueue to wait on 274 * @wq: the waitqueue to wait on 262 * @condition: a C expression for the event to 275 * @condition: a C expression for the event to wait for 263 * @timeout: timeout at which we'll give up in 276 * @timeout: timeout at which we'll give up in jiffies 264 * 277 * 265 * The process is put to sleep (TASK_IDLE) unt 278 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to 266 * true. The @condition is checked each time t 279 * true. The @condition is checked each time the waitqueue @wq is woken up. 267 * 280 * 268 * This function is mostly used when a kthread 281 * This function is mostly used when a kthread or workqueue waits for some 269 * condition and doesn't want to contribute to 282 * condition and doesn't want to contribute to system load. Signals are 270 * ignored. 283 * ignored. 271 * 284 * 272 * Returns: 285 * Returns: 273 * 0 if the @condition evaluated to %false aft 286 * 0 if the @condition evaluated to %false after the @timeout elapsed, 274 * 1 if the @condition evaluated to %true afte 287 * 1 if the @condition evaluated to %true after the @timeout elapsed, 275 * or the remaining jiffies (at least 1) if th 288 * or the remaining jiffies (at least 1) if the @condition evaluated 276 * to %true before the @timeout elapsed. 289 * to %true before the @timeout elapsed. 277 */ 290 */ 278 #define swait_event_idle_timeout_exclusive(wq, 291 #define swait_event_idle_timeout_exclusive(wq, condition, timeout) \ 279 ({ 292 ({ \ 280 long __ret = timeout; 293 long __ret = timeout; \ 281 if (!___wait_cond_timeout(condition)) 294 if (!___wait_cond_timeout(condition)) \ 282 __ret = __swait_event_idle_tim 295 __ret = __swait_event_idle_timeout(wq, \ 283 296 condition, timeout); \ 284 __ret; 297 __ret; \ 285 }) 298 }) 286 299 287 #endif /* _LINUX_SWAIT_H */ 300 #endif /* _LINUX_SWAIT_H */ 288 301
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.