blob: 50540301ed0f2d5308ab15a012715bd0b53f0a04 [file] [log] [blame]
Arjan van de Ven22a9d642009-01-07 08:45:46 -08001/*
2 * async.c: Asynchronous function calls for boot performance
3 *
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Arjan van de Ven <arjan@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 */
12
13
14/*
15
16Goals and Theory of Operation
17
18The primary goal of this feature is to reduce the kernel boot time,
19by doing various independent hardware delays and discovery operations
20decoupled and not strictly serialized.
21
22More specifically, the asynchronous function call concept allows
23certain operations (primarily during system boot) to happen
24asynchronously, out of order, while these operations still
25have their externally visible parts happen sequentially and in-order.
26(not unlike how out-of-order CPUs retire their instructions in order)
27
28Key to the asynchronous function call implementation is the concept of
29a "sequence cookie" (which, although it has an abstracted type, can be
30thought of as a monotonically incrementing number).
31
32The async core will assign each scheduled event such a sequence cookie and
33pass this to the called functions.
34
35The asynchronously called function should before doing a globally visible
36operation, such as registering device numbers, call the
37async_synchronize_cookie() function and pass in its own cookie. The
38async_synchronize_cookie() function will make sure that all asynchronous
39operations that were scheduled prior to the operation corresponding with the
40cookie have completed.
41
42Subsystem/driver initialization code that scheduled asynchronous probe
43functions, but which shares global resources with other drivers/subsystems
44that do not use the asynchronous call feature, need to do a full
45synchronization with the async_synchronize_full() function, before returning
46from their init function. This is to maintain strict ordering between the
47asynchronous and synchronous parts of the kernel.
48
49*/
50
51#include <linux/async.h>
Arjan van de Ven97107942009-03-15 11:11:44 -070052#include <linux/bug.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080053#include <linux/module.h>
54#include <linux/wait.h>
55#include <linux/sched.h>
56#include <linux/init.h>
57#include <linux/kthread.h>
Cornelia Huck86532d82009-01-19 13:45:31 +010058#include <linux/delay.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080059#include <asm/atomic.h>
60
61static async_cookie_t next_cookie = 1;
62
63#define MAX_THREADS 256
64#define MAX_WORK 32768
65
66static LIST_HEAD(async_pending);
67static LIST_HEAD(async_running);
68static DEFINE_SPINLOCK(async_lock);
69
Arjan van de Vencdb80f62009-01-09 13:23:45 -080070static int async_enabled = 0;
71
Arjan van de Ven22a9d642009-01-07 08:45:46 -080072struct async_entry {
73 struct list_head list;
74 async_cookie_t cookie;
75 async_func_ptr *func;
76 void *data;
77 struct list_head *running;
78};
79
80static DECLARE_WAIT_QUEUE_HEAD(async_done);
81static DECLARE_WAIT_QUEUE_HEAD(async_new);
82
83static atomic_t entry_count;
84static atomic_t thread_count;
85
86extern int initcall_debug;
87
88
89/*
90 * MUST be called with the lock held!
91 */
92static async_cookie_t __lowest_in_progress(struct list_head *running)
93{
94 struct async_entry *entry;
James Bottomleyd5a877e2009-05-24 13:03:43 -070095 async_cookie_t ret = next_cookie; /* begin with "infinity" value */
96
Arjan van de Ven37a76bd2009-01-11 15:35:01 +000097 if (!list_empty(running)) {
98 entry = list_first_entry(running,
Arjan van de Ven22a9d642009-01-07 08:45:46 -080099 struct async_entry, list);
James Bottomleyd5a877e2009-05-24 13:03:43 -0700100 ret = entry->cookie;
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800101 }
102
James Bottomleyd5a877e2009-05-24 13:03:43 -0700103 if (!list_empty(&async_pending)) {
104 list_for_each_entry(entry, &async_pending, list)
105 if (entry->running == running) {
106 ret = entry->cookie;
107 break;
108 }
109 }
110
111 return ret;
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800112}
Arjan van de Ven37a76bd2009-01-11 15:35:01 +0000113
114static async_cookie_t lowest_in_progress(struct list_head *running)
115{
116 unsigned long flags;
117 async_cookie_t ret;
118
119 spin_lock_irqsave(&async_lock, flags);
120 ret = __lowest_in_progress(running);
121 spin_unlock_irqrestore(&async_lock, flags);
122 return ret;
123}
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800124/*
125 * pick the first pending entry and run it
126 */
127static void run_one_entry(void)
128{
129 unsigned long flags;
130 struct async_entry *entry;
131 ktime_t calltime, delta, rettime;
132
133 /* 1) pick one task from the pending queue */
134
135 spin_lock_irqsave(&async_lock, flags);
136 if (list_empty(&async_pending))
137 goto out;
138 entry = list_first_entry(&async_pending, struct async_entry, list);
139
140 /* 2) move it to the running queue */
Stefan Richterf7de7621f2009-02-02 13:24:34 +0100141 list_move_tail(&entry->list, entry->running);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800142 spin_unlock_irqrestore(&async_lock, flags);
143
144 /* 3) run it (and print duration)*/
Arjan van de Venad160d22009-01-07 09:28:53 -0800145 if (initcall_debug && system_state == SYSTEM_BOOTING) {
Andrew Morton58763a22009-02-04 15:11:58 -0800146 printk("calling %lli_%pF @ %i\n", (long long)entry->cookie,
147 entry->func, task_pid_nr(current));
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800148 calltime = ktime_get();
149 }
150 entry->func(entry->data, entry->cookie);
Arjan van de Venad160d22009-01-07 09:28:53 -0800151 if (initcall_debug && system_state == SYSTEM_BOOTING) {
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800152 rettime = ktime_get();
153 delta = ktime_sub(rettime, calltime);
Andrew Morton58763a22009-02-04 15:11:58 -0800154 printk("initcall %lli_%pF returned 0 after %lld usecs\n",
155 (long long)entry->cookie,
156 entry->func,
157 (long long)ktime_to_ns(delta) >> 10);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800158 }
159
160 /* 4) remove it from the running queue */
161 spin_lock_irqsave(&async_lock, flags);
162 list_del(&entry->list);
163
164 /* 5) free the entry */
165 kfree(entry);
166 atomic_dec(&entry_count);
167
168 spin_unlock_irqrestore(&async_lock, flags);
169
170 /* 6) wake up any waiters. */
171 wake_up(&async_done);
172 return;
173
174out:
175 spin_unlock_irqrestore(&async_lock, flags);
176}
177
178
179static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
180{
181 struct async_entry *entry;
182 unsigned long flags;
183 async_cookie_t newcookie;
184
185
186 /* allow irq-off callers */
187 entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
188
189 /*
190 * If we're out of memory or if there's too much work
191 * pending already, we execute synchronously.
192 */
Arjan van de Vencdb80f62009-01-09 13:23:45 -0800193 if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) {
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800194 kfree(entry);
195 spin_lock_irqsave(&async_lock, flags);
196 newcookie = next_cookie++;
197 spin_unlock_irqrestore(&async_lock, flags);
198
199 /* low on memory.. run synchronously */
200 ptr(data, newcookie);
201 return newcookie;
202 }
203 entry->func = ptr;
204 entry->data = data;
205 entry->running = running;
206
207 spin_lock_irqsave(&async_lock, flags);
208 newcookie = entry->cookie = next_cookie++;
209 list_add_tail(&entry->list, &async_pending);
210 atomic_inc(&entry_count);
211 spin_unlock_irqrestore(&async_lock, flags);
212 wake_up(&async_new);
213 return newcookie;
214}
215
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100216/**
217 * async_schedule - schedule a function for asynchronous execution
218 * @ptr: function to execute asynchronously
219 * @data: data pointer to pass to the function
220 *
221 * Returns an async_cookie_t that may be used for checkpointing later.
222 * Note: This function may be called from atomic or non-atomic contexts.
223 */
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800224async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
225{
Cornelia Huck7a89bbc2009-01-19 13:45:28 +0100226 return __async_schedule(ptr, data, &async_running);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800227}
228EXPORT_SYMBOL_GPL(async_schedule);
229
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100230/**
Cornelia Huck766ccb92009-01-20 15:31:31 +0100231 * async_schedule_domain - schedule a function for asynchronous execution within a certain domain
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100232 * @ptr: function to execute asynchronously
233 * @data: data pointer to pass to the function
Cornelia Huck766ccb92009-01-20 15:31:31 +0100234 * @running: running list for the domain
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100235 *
236 * Returns an async_cookie_t that may be used for checkpointing later.
Cornelia Huck766ccb92009-01-20 15:31:31 +0100237 * @running may be used in the async_synchronize_*_domain() functions
238 * to wait within a certain synchronization domain rather than globally.
239 * A synchronization domain is specified via the running queue @running to use.
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100240 * Note: This function may be called from atomic or non-atomic contexts.
241 */
Cornelia Huck766ccb92009-01-20 15:31:31 +0100242async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
243 struct list_head *running)
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800244{
245 return __async_schedule(ptr, data, running);
246}
Cornelia Huck766ccb92009-01-20 15:31:31 +0100247EXPORT_SYMBOL_GPL(async_schedule_domain);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800248
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100249/**
250 * async_synchronize_full - synchronize all asynchronous function calls
251 *
252 * This function waits until all asynchronous function calls have been done.
253 */
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800254void async_synchronize_full(void)
255{
Arjan van de Ven33b04b92009-01-08 12:35:11 -0800256 do {
257 async_synchronize_cookie(next_cookie);
258 } while (!list_empty(&async_running) || !list_empty(&async_pending));
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800259}
260EXPORT_SYMBOL_GPL(async_synchronize_full);
261
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100262/**
Cornelia Huck766ccb92009-01-20 15:31:31 +0100263 * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100264 * @list: running list to synchronize on
265 *
Cornelia Huck766ccb92009-01-20 15:31:31 +0100266 * This function waits until all asynchronous function calls for the
267 * synchronization domain specified by the running list @list have been done.
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100268 */
Cornelia Huck766ccb92009-01-20 15:31:31 +0100269void async_synchronize_full_domain(struct list_head *list)
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800270{
Cornelia Huck766ccb92009-01-20 15:31:31 +0100271 async_synchronize_cookie_domain(next_cookie, list);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800272}
Cornelia Huck766ccb92009-01-20 15:31:31 +0100273EXPORT_SYMBOL_GPL(async_synchronize_full_domain);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800274
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100275/**
Cornelia Huck766ccb92009-01-20 15:31:31 +0100276 * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100277 * @cookie: async_cookie_t to use as checkpoint
278 * @running: running list to synchronize on
279 *
Cornelia Huck766ccb92009-01-20 15:31:31 +0100280 * This function waits until all asynchronous function calls for the
281 * synchronization domain specified by the running list @list submitted
282 * prior to @cookie have been done.
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100283 */
Cornelia Huck766ccb92009-01-20 15:31:31 +0100284void async_synchronize_cookie_domain(async_cookie_t cookie,
285 struct list_head *running)
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800286{
287 ktime_t starttime, delta, endtime;
288
Arjan van de Venad160d22009-01-07 09:28:53 -0800289 if (initcall_debug && system_state == SYSTEM_BOOTING) {
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800290 printk("async_waiting @ %i\n", task_pid_nr(current));
291 starttime = ktime_get();
292 }
293
Arjan van de Ven37a76bd2009-01-11 15:35:01 +0000294 wait_event(async_done, lowest_in_progress(running) >= cookie);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800295
Arjan van de Venad160d22009-01-07 09:28:53 -0800296 if (initcall_debug && system_state == SYSTEM_BOOTING) {
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800297 endtime = ktime_get();
298 delta = ktime_sub(endtime, starttime);
299
300 printk("async_continuing @ %i after %lli usec\n",
Andrew Morton58763a22009-02-04 15:11:58 -0800301 task_pid_nr(current),
302 (long long)ktime_to_ns(delta) >> 10);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800303 }
304}
Cornelia Huck766ccb92009-01-20 15:31:31 +0100305EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800306
Cornelia Huckf30d5b32009-01-19 13:45:33 +0100307/**
308 * async_synchronize_cookie - synchronize asynchronous function calls with cookie checkpointing
309 * @cookie: async_cookie_t to use as checkpoint
310 *
311 * This function waits until all asynchronous function calls prior to @cookie
312 * have been done.
313 */
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800314void async_synchronize_cookie(async_cookie_t cookie)
315{
Cornelia Huck766ccb92009-01-20 15:31:31 +0100316 async_synchronize_cookie_domain(cookie, &async_running);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800317}
318EXPORT_SYMBOL_GPL(async_synchronize_cookie);
319
320
321static int async_thread(void *unused)
322{
323 DECLARE_WAITQUEUE(wq, current);
324 add_wait_queue(&async_new, &wq);
325
326 while (!kthread_should_stop()) {
327 int ret = HZ;
328 set_current_state(TASK_INTERRUPTIBLE);
329 /*
330 * check the list head without lock.. false positives
331 * are dealt with inside run_one_entry() while holding
332 * the lock.
333 */
334 rmb();
335 if (!list_empty(&async_pending))
336 run_one_entry();
337 else
338 ret = schedule_timeout(HZ);
339
340 if (ret == 0) {
341 /*
342 * we timed out, this means we as thread are redundant.
343 * we sign off and die, but we to avoid any races there
344 * is a last-straw check to see if work snuck in.
345 */
346 atomic_dec(&thread_count);
347 wmb(); /* manager must see our departure first */
348 if (list_empty(&async_pending))
349 break;
350 /*
351 * woops work came in between us timing out and us
352 * signing off; we need to stay alive and keep working.
353 */
354 atomic_inc(&thread_count);
355 }
356 }
357 remove_wait_queue(&async_new, &wq);
358
359 return 0;
360}
361
362static int async_manager_thread(void *unused)
363{
364 DECLARE_WAITQUEUE(wq, current);
365 add_wait_queue(&async_new, &wq);
366
367 while (!kthread_should_stop()) {
368 int tc, ec;
369
370 set_current_state(TASK_INTERRUPTIBLE);
371
372 tc = atomic_read(&thread_count);
373 rmb();
374 ec = atomic_read(&entry_count);
375
376 while (tc < ec && tc < MAX_THREADS) {
Cornelia Huck86532d82009-01-19 13:45:31 +0100377 if (IS_ERR(kthread_run(async_thread, NULL, "async/%i",
378 tc))) {
379 msleep(100);
380 continue;
381 }
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800382 atomic_inc(&thread_count);
383 tc++;
384 }
385
386 schedule();
387 }
388 remove_wait_queue(&async_new, &wq);
389
390 return 0;
391}
392
393static int __init async_init(void)
394{
Arjan van de Ven97107942009-03-15 11:11:44 -0700395 async_enabled =
396 !IS_ERR(kthread_run(async_manager_thread, NULL, "async/mgr"));
397
398 WARN_ON(!async_enabled);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800399 return 0;
400}
401
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800402core_initcall(async_init);