]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/sched/completion.c
Merge tag 'mvebu-fixes-4.14-1' of git://git.infradead.org/linux-mvebu into fixes
[mirror_ubuntu-bionic-kernel.git] / kernel / sched / completion.c
CommitLineData
b8a21626
PZ
1/*
2 * Generic wait-for-completion handler;
3 *
4 * It differs from semaphores in that their default case is the opposite,
5 * wait_for_completion default blocks whereas semaphore default non-block. The
6 * interface also makes it easy to 'complete' multiple waiting threads,
7 * something which isn't entirely natural for semaphores.
8 *
9 * But more importantly, the primitive documents the usage. Semaphores would
10 * typically be used for exclusion which gives rise to priority inversion.
11 * Waiting for completion is a typically sync point, but not an exclusion point.
12 */
13
174cd4b1 14#include <linux/sched/signal.h>
b17b0153 15#include <linux/sched/debug.h>
b8a21626
PZ
16#include <linux/completion.h>
17
18/**
19 * complete: - signals a single thread waiting on this completion
20 * @x: holds the state of this particular completion
21 *
22 * This will wake up a single thread waiting on this completion. Threads will be
23 * awakened in the same order in which they were queued.
24 *
25 * See also complete_all(), wait_for_completion() and related routines.
26 *
27 * It may be assumed that this function implies a write memory barrier before
28 * changing the task state if and only if any tasks are woken up.
29 */
30void complete(struct completion *x)
31{
32 unsigned long flags;
33
34 spin_lock_irqsave(&x->wait.lock, flags);
cd8084f9
BP
35
36 /*
37 * Perform commit of crossrelease here.
38 */
39 complete_release_commit(x);
40
da9647e0
PZ
41 if (x->done != UINT_MAX)
42 x->done++;
b8a21626
PZ
43 __wake_up_locked(&x->wait, TASK_NORMAL, 1);
44 spin_unlock_irqrestore(&x->wait.lock, flags);
45}
46EXPORT_SYMBOL(complete);
47
48/**
49 * complete_all: - signals all threads waiting on this completion
50 * @x: holds the state of this particular completion
51 *
52 * This will wake up all threads waiting on this particular completion event.
53 *
54 * It may be assumed that this function implies a write memory barrier before
55 * changing the task state if and only if any tasks are woken up.
9c878320
SR
56 *
57 * Since complete_all() sets the completion of @x permanently to done
58 * to allow multiple waiters to finish, a call to reinit_completion()
59 * must be used on @x if @x is to be used again. The code must make
60 * sure that all waiters have woken and finished before reinitializing
61 * @x. Also note that the function completion_done() can not be used
62 * to know if there are still waiters after complete_all() has been called.
b8a21626
PZ
63 */
64void complete_all(struct completion *x)
65{
66 unsigned long flags;
67
68 spin_lock_irqsave(&x->wait.lock, flags);
da9647e0 69 x->done = UINT_MAX;
b8a21626
PZ
70 __wake_up_locked(&x->wait, TASK_NORMAL, 0);
71 spin_unlock_irqrestore(&x->wait.lock, flags);
72}
73EXPORT_SYMBOL(complete_all);
74
75static inline long __sched
76do_wait_for_common(struct completion *x,
77 long (*action)(long), long timeout, int state)
78{
79 if (!x->done) {
80 DECLARE_WAITQUEUE(wait, current);
81
ac6424b9 82 __add_wait_queue_entry_tail_exclusive(&x->wait, &wait);
b8a21626
PZ
83 do {
84 if (signal_pending_state(state, current)) {
85 timeout = -ERESTARTSYS;
86 break;
87 }
88 __set_current_state(state);
89 spin_unlock_irq(&x->wait.lock);
90 timeout = action(timeout);
91 spin_lock_irq(&x->wait.lock);
92 } while (!x->done && timeout);
93 __remove_wait_queue(&x->wait, &wait);
94 if (!x->done)
95 return timeout;
96 }
da9647e0
PZ
97 if (x->done != UINT_MAX)
98 x->done--;
b8a21626
PZ
99 return timeout ?: 1;
100}
101
102static inline long __sched
103__wait_for_common(struct completion *x,
104 long (*action)(long), long timeout, int state)
105{
106 might_sleep();
107
cd8084f9
BP
108 complete_acquire(x);
109
b8a21626
PZ
110 spin_lock_irq(&x->wait.lock);
111 timeout = do_wait_for_common(x, action, timeout, state);
112 spin_unlock_irq(&x->wait.lock);
cd8084f9
BP
113
114 complete_release(x);
115
b8a21626
PZ
116 return timeout;
117}
118
119static long __sched
120wait_for_common(struct completion *x, long timeout, int state)
121{
122 return __wait_for_common(x, schedule_timeout, timeout, state);
123}
124
125static long __sched
126wait_for_common_io(struct completion *x, long timeout, int state)
127{
128 return __wait_for_common(x, io_schedule_timeout, timeout, state);
129}
130
131/**
132 * wait_for_completion: - waits for completion of a task
133 * @x: holds the state of this particular completion
134 *
135 * This waits to be signaled for completion of a specific task. It is NOT
136 * interruptible and there is no timeout.
137 *
138 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
139 * and interrupt capability. Also see complete().
140 */
141void __sched wait_for_completion(struct completion *x)
142{
143 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
144}
145EXPORT_SYMBOL(wait_for_completion);
146
147/**
148 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
149 * @x: holds the state of this particular completion
150 * @timeout: timeout value in jiffies
151 *
152 * This waits for either a completion of a specific task to be signaled or for a
153 * specified timeout to expire. The timeout is in jiffies. It is not
154 * interruptible.
155 *
156 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
157 * till timeout) if completed.
158 */
159unsigned long __sched
160wait_for_completion_timeout(struct completion *x, unsigned long timeout)
161{
162 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
163}
164EXPORT_SYMBOL(wait_for_completion_timeout);
165
166/**
167 * wait_for_completion_io: - waits for completion of a task
168 * @x: holds the state of this particular completion
169 *
170 * This waits to be signaled for completion of a specific task. It is NOT
171 * interruptible and there is no timeout. The caller is accounted as waiting
a1bd5373 172 * for IO (which traditionally means blkio only).
b8a21626
PZ
173 */
174void __sched wait_for_completion_io(struct completion *x)
175{
176 wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
177}
178EXPORT_SYMBOL(wait_for_completion_io);
179
180/**
181 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
182 * @x: holds the state of this particular completion
183 * @timeout: timeout value in jiffies
184 *
185 * This waits for either a completion of a specific task to be signaled or for a
186 * specified timeout to expire. The timeout is in jiffies. It is not
a1bd5373
WS
187 * interruptible. The caller is accounted as waiting for IO (which traditionally
188 * means blkio only).
b8a21626
PZ
189 *
190 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
191 * till timeout) if completed.
192 */
193unsigned long __sched
194wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
195{
196 return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
197}
198EXPORT_SYMBOL(wait_for_completion_io_timeout);
199
200/**
201 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
202 * @x: holds the state of this particular completion
203 *
204 * This waits for completion of a specific task to be signaled. It is
205 * interruptible.
206 *
207 * Return: -ERESTARTSYS if interrupted, 0 if completed.
208 */
209int __sched wait_for_completion_interruptible(struct completion *x)
210{
211 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
212 if (t == -ERESTARTSYS)
213 return t;
214 return 0;
215}
216EXPORT_SYMBOL(wait_for_completion_interruptible);
217
218/**
219 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
220 * @x: holds the state of this particular completion
221 * @timeout: timeout value in jiffies
222 *
223 * This waits for either a completion of a specific task to be signaled or for a
224 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
225 *
226 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
227 * or number of jiffies left till timeout) if completed.
228 */
229long __sched
230wait_for_completion_interruptible_timeout(struct completion *x,
231 unsigned long timeout)
232{
233 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
234}
235EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
236
237/**
238 * wait_for_completion_killable: - waits for completion of a task (killable)
239 * @x: holds the state of this particular completion
240 *
241 * This waits to be signaled for completion of a specific task. It can be
242 * interrupted by a kill signal.
243 *
244 * Return: -ERESTARTSYS if interrupted, 0 if completed.
245 */
246int __sched wait_for_completion_killable(struct completion *x)
247{
248 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
249 if (t == -ERESTARTSYS)
250 return t;
251 return 0;
252}
253EXPORT_SYMBOL(wait_for_completion_killable);
254
255/**
256 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
257 * @x: holds the state of this particular completion
258 * @timeout: timeout value in jiffies
259 *
260 * This waits for either a completion of a specific task to be
261 * signaled or for a specified timeout to expire. It can be
262 * interrupted by a kill signal. The timeout is in jiffies.
263 *
264 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
265 * or number of jiffies left till timeout) if completed.
266 */
267long __sched
268wait_for_completion_killable_timeout(struct completion *x,
269 unsigned long timeout)
270{
271 return wait_for_common(x, timeout, TASK_KILLABLE);
272}
273EXPORT_SYMBOL(wait_for_completion_killable_timeout);
274
275/**
276 * try_wait_for_completion - try to decrement a completion without blocking
277 * @x: completion structure
278 *
279 * Return: 0 if a decrement cannot be done without blocking
280 * 1 if a decrement succeeded.
281 *
282 * If a completion is being used as a counting completion,
283 * attempt to decrement the counter without blocking. This
284 * enables us to avoid waiting if the resource the completion
285 * is protecting is not available.
286 */
287bool try_wait_for_completion(struct completion *x)
288{
289 unsigned long flags;
290 int ret = 1;
291
7c34e318
NMG
292 /*
293 * Since x->done will need to be locked only
294 * in the non-blocking case, we check x->done
295 * first without taking the lock so we can
296 * return early in the blocking case.
297 */
bc956015 298 if (!READ_ONCE(x->done))
7c34e318
NMG
299 return 0;
300
b8a21626
PZ
301 spin_lock_irqsave(&x->wait.lock, flags);
302 if (!x->done)
303 ret = 0;
da9647e0 304 else if (x->done != UINT_MAX)
b8a21626
PZ
305 x->done--;
306 spin_unlock_irqrestore(&x->wait.lock, flags);
307 return ret;
308}
309EXPORT_SYMBOL(try_wait_for_completion);
310
311/**
312 * completion_done - Test to see if a completion has any waiters
313 * @x: completion structure
314 *
315 * Return: 0 if there are waiters (wait_for_completion() in progress)
316 * 1 if there are no waiters.
317 *
9c878320 318 * Note, this will always return true if complete_all() was called on @X.
b8a21626
PZ
319 */
320bool completion_done(struct completion *x)
321{
dec13c42
PM
322 unsigned long flags;
323
bc956015
ON
324 if (!READ_ONCE(x->done))
325 return false;
326
327 /*
328 * If ->done, we need to wait for complete() to release ->wait.lock
329 * otherwise we can end up freeing the completion before complete()
330 * is done referencing it.
bc956015 331 */
dec13c42
PM
332 spin_lock_irqsave(&x->wait.lock, flags);
333 spin_unlock_irqrestore(&x->wait.lock, flags);
bc956015 334 return true;
b8a21626
PZ
335}
336EXPORT_SYMBOL(completion_done);