]>
git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blob - include/linux/swait.h
1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/list.h>
6 #include <linux/stddef.h>
7 #include <linux/spinlock.h>
8 #include <asm/current.h>
13 * While these are very similar to the other/complex wait queues (wait.h) the
14 * most important difference is that the simple waitqueue allows for
15 * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
18 * In order to make this so, we had to drop a fair number of features of the
19 * other waitqueue code; notably:
21 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
22 * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
25 * - the exclusive mode; because this requires preserving the list order
28 * - custom wake functions; because you cannot give any guarantees about
31 * As a side effect of this; the data structures are slimmer.
33 * One would recommend using this wait queue where possible.
38 struct swait_queue_head
{
40 struct list_head task_list
;
44 struct task_struct
*task
;
45 struct list_head task_list
;
48 #define __SWAITQUEUE_INITIALIZER(name) { \
50 .task_list = LIST_HEAD_INIT((name).task_list), \
53 #define DECLARE_SWAITQUEUE(name) \
54 struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
56 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
57 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
58 .task_list = LIST_HEAD_INIT((name).task_list), \
61 #define DECLARE_SWAIT_QUEUE_HEAD(name) \
62 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
64 extern void __init_swait_queue_head(struct swait_queue_head
*q
, const char *name
,
65 struct lock_class_key
*key
);
67 #define init_swait_queue_head(q) \
69 static struct lock_class_key __key; \
70 __init_swait_queue_head((q), #q, &__key); \
74 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
75 ({ init_swait_queue_head(&name); name; })
76 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
77 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
79 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
80 DECLARE_SWAIT_QUEUE_HEAD(name)
84 * swait_active -- locklessly test for waiters on the queue
85 * @wq: the waitqueue to test for waiters
87 * returns true if the wait list is not empty
89 * NOTE: this function is lockless and requires care, incorrect usage _will_
90 * lead to sporadic and non-obvious failure.
92 * NOTE2: this function has the same above implications as regular waitqueues.
94 * Use either while holding swait_queue_head::lock or when used for wakeups
95 * with an extra smp_mb() like:
97 * CPU0 - waker CPU1 - waiter
100 * @cond = true; prepare_to_swait(&wq_head, &wait, state);
101 * smp_mb(); // smp_mb() from set_current_state()
102 * if (swait_active(wq_head)) if (@cond)
103 * wake_up(wq_head); break;
106 * finish_swait(&wq_head, &wait);
108 * Because without the explicit smp_mb() it's possible for the
109 * swait_active() load to get hoisted over the @cond store such that we'll
110 * observe an empty wait list while the waiter might not observe @cond.
111 * This, in turn, can trigger missing wakeups.
113 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
114 * which (when the lock is uncontended) are of roughly equal cost.
116 static inline int swait_active(struct swait_queue_head
*wq
)
118 return !list_empty(&wq
->task_list
);
122 * swq_has_sleeper - check if there are any waiting processes
123 * @wq: the waitqueue to test for waiters
125 * Returns true if @wq has waiting processes
127 * Please refer to the comment for swait_active.
129 static inline bool swq_has_sleeper(struct swait_queue_head
*wq
)
132 * We need to be sure we are in sync with the list_add()
133 * modifications to the wait queue (task_list).
135 * This memory barrier should be paired with one on the
139 return swait_active(wq
);
142 extern void swake_up(struct swait_queue_head
*q
);
143 extern void swake_up_all(struct swait_queue_head
*q
);
144 extern void swake_up_locked(struct swait_queue_head
*q
);
146 extern void __prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
147 extern void prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
);
148 extern long prepare_to_swait_event(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
);
150 extern void __finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
151 extern void finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
153 /* as per ___wait_event() but for swait, therefore "exclusive == 0" */
154 #define ___swait_event(wq, condition, state, ret, cmd) \
156 struct swait_queue __wait; \
159 INIT_LIST_HEAD(&__wait.task_list); \
161 long __int = prepare_to_swait_event(&wq, &__wait, state);\
166 if (___wait_is_interruptible(state) && __int) { \
173 finish_swait(&wq, &__wait); \
177 #define __swait_event(wq, condition) \
178 (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
181 #define swait_event(wq, condition) \
185 __swait_event(wq, condition); \
188 #define __swait_event_timeout(wq, condition, timeout) \
189 ___swait_event(wq, ___wait_cond_timeout(condition), \
190 TASK_UNINTERRUPTIBLE, timeout, \
191 __ret = schedule_timeout(__ret))
193 #define swait_event_timeout(wq, condition, timeout) \
195 long __ret = timeout; \
196 if (!___wait_cond_timeout(condition)) \
197 __ret = __swait_event_timeout(wq, condition, timeout); \
201 #define __swait_event_interruptible(wq, condition) \
202 ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
205 #define swait_event_interruptible(wq, condition) \
209 __ret = __swait_event_interruptible(wq, condition); \
213 #define __swait_event_interruptible_timeout(wq, condition, timeout) \
214 ___swait_event(wq, ___wait_cond_timeout(condition), \
215 TASK_INTERRUPTIBLE, timeout, \
216 __ret = schedule_timeout(__ret))
218 #define swait_event_interruptible_timeout(wq, condition, timeout) \
220 long __ret = timeout; \
221 if (!___wait_cond_timeout(condition)) \
222 __ret = __swait_event_interruptible_timeout(wq, \
223 condition, timeout); \
227 #define __swait_event_idle(wq, condition) \
228 (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule())
231 * swait_event_idle - wait without system load contribution
232 * @wq: the waitqueue to wait on
233 * @condition: a C expression for the event to wait for
235 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
236 * true. The @condition is checked each time the waitqueue @wq is woken up.
238 * This function is mostly used when a kthread or workqueue waits for some
239 * condition and doesn't want to contribute to system load. Signals are
242 #define swait_event_idle(wq, condition) \
246 __swait_event_idle(wq, condition); \
249 #define __swait_event_idle_timeout(wq, condition, timeout) \
250 ___swait_event(wq, ___wait_cond_timeout(condition), \
251 TASK_IDLE, timeout, \
252 __ret = schedule_timeout(__ret))
255 * swait_event_idle_timeout - wait up to timeout without load contribution
256 * @wq: the waitqueue to wait on
257 * @condition: a C expression for the event to wait for
258 * @timeout: timeout at which we'll give up in jiffies
260 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
261 * true. The @condition is checked each time the waitqueue @wq is woken up.
263 * This function is mostly used when a kthread or workqueue waits for some
264 * condition and doesn't want to contribute to system load. Signals are
268 * 0 if the @condition evaluated to %false after the @timeout elapsed,
269 * 1 if the @condition evaluated to %true after the @timeout elapsed,
270 * or the remaining jiffies (at least 1) if the @condition evaluated
271 * to %true before the @timeout elapsed.
273 #define swait_event_idle_timeout(wq, condition, timeout) \
275 long __ret = timeout; \
276 if (!___wait_cond_timeout(condition)) \
277 __ret = __swait_event_idle_timeout(wq, \
278 condition, timeout); \
282 #endif /* _LINUX_SWAIT_H */