blob: 9f039b4f4b09beb227ea8b48183801674de6e6f4 [file] [log] [blame]
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -03001================================================
Ingo Molnar0c373342018-10-11 10:36:23 +02002Completions - "wait for completion" barrier APIs
3================================================
Nicholas Mc Guire202799b2015-01-30 08:01:52 +01004
5Introduction:
6-------------
7
Ingo Molnar0c373342018-10-11 10:36:23 +02008If you have one or more threads that must wait for some kernel activity
Jonathan Corbet7085f6c2015-03-27 10:16:35 -06009to have reached a point or a specific state, completions can provide a
10race-free solution to this problem. Semantically they are somewhat like a
Ingo Molnar0c373342018-10-11 10:36:23 +020011pthread_barrier() and have similar use-cases.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010012
Jonathan Corbet7085f6c2015-03-27 10:16:35 -060013Completions are a code synchronization mechanism which is preferable to any
Ingo Molnar0c373342018-10-11 10:36:23 +020014misuse of locks/semaphores and busy-loops. Any time you think of using
15yield() or some quirky msleep(1) loop to allow something else to proceed,
16you probably want to look into using one of the wait_for_completion*()
17calls and complete() instead.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010018
Ingo Molnar0c373342018-10-11 10:36:23 +020019The advantage of using completions is that they have a well defined, focused
20purpose which makes it very easy to see the intent of the code, but they
21also result in more efficient code as all threads can continue execution
22until the result is actually needed, and both the waiting and the signalling
23is highly efficient using low level scheduler sleep/wakeup facilities.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010024
Ingo Molnar0c373342018-10-11 10:36:23 +020025Completions are built on top of the waitqueue and wakeup infrastructure of
26the Linux scheduler. The event the threads on the waitqueue are waiting for
27is reduced to a simple flag in 'struct completion', appropriately called "done".
28
29As completions are scheduling related, the code can be found in
Brian Norrisdc927262016-11-15 14:42:14 -080030kernel/sched/completion.c.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010031
32
33Usage:
34------
35
Ingo Molnar0c373342018-10-11 10:36:23 +020036There are three main parts to using completions:
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010037
Ingo Molnar0c373342018-10-11 10:36:23 +020038 - the initialization of the 'struct completion' synchronization object
39 - the waiting part through a call to one of the variants of wait_for_completion(),
40 - the signaling side through a call to complete() or complete_all().
41
42There are also some helper functions for checking the state of completions.
43Note that while initialization must happen first, the waiting and signaling
44part can happen in any order. I.e. it's entirely normal for a thread
45to have marked a completion as 'done' before another thread checks whether
46it has to wait for it.
47
48To use completions you need to #include <linux/completion.h> and
49create a static or dynamic variable of type 'struct completion',
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -030050which has only two fields::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010051
52 struct completion {
53 unsigned int done;
54 wait_queue_head_t wait;
55 };
56
Ingo Molnar0c373342018-10-11 10:36:23 +020057This provides the ->wait waitqueue to place tasks on for waiting (if any), and
58the ->done completion flag for indicating whether it's completed or not.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010059
Ingo Molnar0c373342018-10-11 10:36:23 +020060Completions should be named to refer to the event that is being synchronized on.
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -030061A good example is::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010062
63 wait_for_completion(&early_console_added);
64
65 complete(&early_console_added);
66
Ingo Molnar0c373342018-10-11 10:36:23 +020067Good, intuitive naming (as always) helps code readability. Naming a completion
68'complete' is not helpful unless the purpose is super obvious...
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010069
70
71Initializing completions:
72-------------------------
73
Nicholas Mc Guire11e13692018-10-16 15:45:39 +020074Dynamically allocated completion objects should preferably be embedded in data
75structures that are assured to be alive for the life-time of the function/driver,
76to prevent races with asynchronous complete() calls from occurring.
77
78Particular care should be taken when using the _timeout() or _killable()/_interruptible()
79variants of wait_for_completion(), as it must be assured that memory de-allocation
80does not happen until all related activities (complete() or reinit_completion())
81have taken place, even if these wait functions return prematurely due to a timeout
82or a signal triggering.
83
84Initializing of dynamically allocated completion objects is done via a call to
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -030085init_completion()::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010086
Ingo Molnar0c373342018-10-11 10:36:23 +020087 init_completion(&dynamic_object->done);
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010088
Ingo Molnar0c373342018-10-11 10:36:23 +020089In this call we initialize the waitqueue and set ->done to 0, i.e. "not completed"
90or "not done".
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010091
92The re-initialization function, reinit_completion(), simply resets the
Ingo Molnar0c373342018-10-11 10:36:23 +020093->done field to 0 ("not done"), without touching the waitqueue.
94Callers of this function must make sure that there are no racy
95wait_for_completion() calls going on in parallel.
96
97Calling init_completion() on the same completion object twice is
Nicholas Mc Guire202799b2015-01-30 08:01:52 +010098most likely a bug as it re-initializes the queue to an empty queue and
Ingo Molnar0c373342018-10-11 10:36:23 +020099enqueued tasks could get "lost" - use reinit_completion() in that case,
100but be aware of other races.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100101
Ingo Molnar0c373342018-10-11 10:36:23 +0200102For static declaration and initialization, macros are available.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100103
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300104For static (or global) declarations in file scope you can use
105DECLARE_COMPLETION()::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100106
Ingo Molnar0c373342018-10-11 10:36:23 +0200107 static DECLARE_COMPLETION(setup_done);
108 DECLARE_COMPLETION(setup_done);
109
110Note that in this case the completion is boot time (or module load time)
111initialized to 'not done' and doesn't require an init_completion() call.
112
113When a completion is declared as a local variable within a function,
Nicholas Mc Guire11e13692018-10-16 15:45:39 +0200114then the initialization should always use DECLARE_COMPLETION_ONSTACK()
115explicitly, not just to make lockdep happy, but also to make it clear
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300116that limited scope had been considered and is intentional::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100117
118 DECLARE_COMPLETION_ONSTACK(setup_done)
119
Ingo Molnar0c373342018-10-11 10:36:23 +0200120Note that when using completion objects as local variables you must be
Nicholas Mc Guire11e13692018-10-16 15:45:39 +0200121acutely aware of the short life time of the function stack: the function
122must not return to a calling context until all activities (such as waiting
123threads) have ceased and the completion object is completely unused.
124
125To emphasise this again: in particular when using some of the waiting API variants
126with more complex outcomes, such as the timeout or signalling (_timeout(),
127_killable() and _interruptible()) variants, the wait might complete
128prematurely while the object might still be in use by another thread - and a return
129from the wait_on_completion*() caller function will deallocate the function
130stack and cause subtle data corruption if a complete() is done in some
131other thread. Simple testing might not trigger these kinds of races.
132
133If unsure, use dynamically allocated completion objects, preferably embedded
134in some other long lived object that has a boringly long life time which
135exceeds the life time of any helper threads using the completion object,
136or has a lock or other synchronization mechanism to make sure complete()
137is not called on a freed object.
138
139A naive DECLARE_COMPLETION() on the stack triggers a lockdep warning.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100140
141Waiting for completions:
142------------------------
143
Ingo Molnar0c373342018-10-11 10:36:23 +0200144For a thread to wait for some concurrent activity to finish, it
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300145calls wait_for_completion() on the initialized completion structure::
Ingo Molnar0c373342018-10-11 10:36:23 +0200146
147 void wait_for_completion(struct completion *done)
148
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300149A typical usage scenario is::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100150
Ingo Molnar0c373342018-10-11 10:36:23 +0200151 CPU#1 CPU#2
152
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600153 struct completion setup_done;
Ingo Molnar0c373342018-10-11 10:36:23 +0200154
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100155 init_completion(&setup_done);
Ingo Molnar0c373342018-10-11 10:36:23 +0200156 initialize_work(...,&setup_done,...);
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100157
Ingo Molnar0c373342018-10-11 10:36:23 +0200158 /* run non-dependent code */ /* do setup */
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100159
Ingo Molnar0c373342018-10-11 10:36:23 +0200160 wait_for_completion(&setup_done); complete(setup_done);
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100161
Ingo Molnar0c373342018-10-11 10:36:23 +0200162This is not implying any particular order between wait_for_completion() and
163the call to complete() - if the call to complete() happened before the call
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100164to wait_for_completion() then the waiting side simply will continue
John Garry7b6abce2018-10-10 22:56:32 +0800165immediately as all dependencies are satisfied; if not, it will block until
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500166completion is signaled by complete().
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100167
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600168Note that wait_for_completion() is calling spin_lock_irq()/spin_unlock_irq(),
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100169so it can only be called safely when you know that interrupts are enabled.
Ingo Molnar0c373342018-10-11 10:36:23 +0200170Calling it from IRQs-off atomic contexts will result in hard-to-detect
171spurious enabling of interrupts.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100172
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600173The default behavior is to wait without a timeout and to mark the task as
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100174uninterruptible. wait_for_completion() and its variants are only safe
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500175in process context (as they can sleep) but not in atomic context,
Ingo Molnar0c373342018-10-11 10:36:23 +0200176interrupt context, with disabled IRQs, or preemption is disabled - see also
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500177try_wait_for_completion() below for handling completion in atomic/interrupt
178context.
179
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100180As all variants of wait_for_completion() can (obviously) block for a long
Ingo Molnar0c373342018-10-11 10:36:23 +0200181time depending on the nature of the activity they are waiting for, so in
182most cases you probably don't want to call this with held mutexes.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100183
184
Ingo Molnar0c373342018-10-11 10:36:23 +0200185wait_for_completion*() variants available:
186------------------------------------------
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100187
188The below variants all return status and this status should be checked in
189most(/all) cases - in cases where the status is deliberately not checked you
190probably want to make a note explaining this (e.g. see
191arch/arm/kernel/smp.c:__cpu_up()).
192
193A common problem that occurs is to have unclean assignment of return types,
Ingo Molnar0c373342018-10-11 10:36:23 +0200194so take care to assign return-values to variables of the proper type.
195
196Checking for the specific meaning of return values also has been found
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300197to be quite inaccurate, e.g. constructs like::
Ingo Molnar0c373342018-10-11 10:36:23 +0200198
199 if (!wait_for_completion_interruptible_timeout(...))
200
201... would execute the same code path for successful completion and for the
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300202interrupted case - which is probably not what you want::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100203
204 int wait_for_completion_interruptible(struct completion *done)
205
Ingo Molnar0c373342018-10-11 10:36:23 +0200206This function marks the task TASK_INTERRUPTIBLE while it is waiting.
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300207If a signal was received while waiting it will return -ERESTARTSYS; 0 otherwise::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100208
Ingo Molnar0c373342018-10-11 10:36:23 +0200209 unsigned long wait_for_completion_timeout(struct completion *done, unsigned long timeout)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100210
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500211The task is marked as TASK_UNINTERRUPTIBLE and will wait at most 'timeout'
Ingo Molnar0c373342018-10-11 10:36:23 +0200212jiffies. If a timeout occurs it returns 0, else the remaining time in
213jiffies (but at least 1).
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100214
Ingo Molnar0c373342018-10-11 10:36:23 +0200215Timeouts are preferably calculated with msecs_to_jiffies() or usecs_to_jiffies(),
216to make the code largely HZ-invariant.
217
218If the returned timeout value is deliberately ignored a comment should probably explain
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300219why (e.g. see drivers/mfd/wm8350-core.c wm8350_read_auxadc())::
Ingo Molnar0c373342018-10-11 10:36:23 +0200220
221 long wait_for_completion_interruptible_timeout(struct completion *done, unsigned long timeout)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100222
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600223This function passes a timeout in jiffies and marks the task as
224TASK_INTERRUPTIBLE. If a signal was received it will return -ERESTARTSYS;
Ingo Molnar0c373342018-10-11 10:36:23 +0200225otherwise it returns 0 if the completion timed out, or the remaining time in
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600226jiffies if completion occurred.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100227
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600228Further variants include _killable which uses TASK_KILLABLE as the
Ingo Molnar0c373342018-10-11 10:36:23 +0200229designated tasks state and will return -ERESTARTSYS if it is interrupted,
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300230or 0 if completion was achieved. There is a _timeout variant as well::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100231
232 long wait_for_completion_killable(struct completion *done)
Ingo Molnar0c373342018-10-11 10:36:23 +0200233 long wait_for_completion_killable_timeout(struct completion *done, unsigned long timeout)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100234
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500235The _io variants wait_for_completion_io() behave the same as the non-_io
Ingo Molnar0c373342018-10-11 10:36:23 +0200236variants, except for accounting waiting time as 'waiting on IO', which has
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300237an impact on how the task is accounted in scheduling/IO stats::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100238
239 void wait_for_completion_io(struct completion *done)
Ingo Molnar0c373342018-10-11 10:36:23 +0200240 unsigned long wait_for_completion_io_timeout(struct completion *done, unsigned long timeout)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100241
242
243Signaling completions:
244----------------------
245
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500246A thread that wants to signal that the conditions for continuation have been
247achieved calls complete() to signal exactly one of the waiters that it can
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300248continue::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100249
250 void complete(struct completion *done)
251
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300252... or calls complete_all() to signal all current and future waiters::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100253
254 void complete_all(struct completion *done)
255
256The signaling will work as expected even if completions are signaled before
257a thread starts waiting. This is achieved by the waiter "consuming"
Ingo Molnar0c373342018-10-11 10:36:23 +0200258(decrementing) the done field of 'struct completion'. Waiting threads
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100259wakeup order is the same in which they were enqueued (FIFO order).
260
261If complete() is called multiple times then this will allow for that number
262of waiters to continue - each call to complete() will simply increment the
Ingo Molnar0c373342018-10-11 10:36:23 +0200263done field. Calling complete_all() multiple times is a bug though. Both
264complete() and complete_all() can be called in IRQ/atomic context safely.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100265
Ingo Molnar0c373342018-10-11 10:36:23 +0200266There can only be one thread calling complete() or complete_all() on a
267particular 'struct completion' at any time - serialized through the wait
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100268queue spinlock. Any such concurrent calls to complete() or complete_all()
269probably are a design bug.
270
Ingo Molnar0c373342018-10-11 10:36:23 +0200271Signaling completion from IRQ context is fine as it will appropriately
Linus Torvalds01aa9d52018-10-24 18:01:11 +0100272lock with spin_lock_irqsave()/spin_unlock_irqrestore() and it will never
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300273sleep.
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100274
275
276try_wait_for_completion()/completion_done():
277--------------------------------------------
278
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500279The try_wait_for_completion() function will not put the thread on the wait
280queue but rather returns false if it would need to enqueue (block) the thread,
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300281else it consumes one posted completion and returns true::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100282
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500283 bool try_wait_for_completion(struct completion *done)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100284
Ingo Molnar0c373342018-10-11 10:36:23 +0200285Finally, to check the state of a completion without changing it in any way,
Jonathan Corbet7085f6c2015-03-27 10:16:35 -0600286call completion_done(), which returns false if there are no posted
287completions that were not yet consumed by waiters (implying that there are
Mauro Carvalho Chehabd6a3b242019-06-12 14:53:03 -0300288waiters) and true otherwise::
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100289
Nicholas Mc Guire4988aaa2015-02-20 12:28:48 -0500290 bool completion_done(struct completion *done)
Nicholas Mc Guire202799b2015-01-30 08:01:52 +0100291
292Both try_wait_for_completion() and completion_done() are safe to be called in
Ingo Molnar0c373342018-10-11 10:36:23 +0200293IRQ or atomic context.