Linux Audio

Check our new training course

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