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