2 * async.c: Asynchronous function calls for boot performance
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Arjan van de Ven <arjan@linux.intel.com>
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
16 Goals and Theory of Operation
18 The primary goal of this feature is to reduce the kernel boot time,
19 by doing various independent hardware delays and discovery operations
20 decoupled and not strictly serialized.
22 More specifically, the asynchronous function call concept allows
23 certain operations (primarily during system boot) to happen
24 asynchronously, out of order, while these operations still
25 have their externally visible parts happen sequentially and in-order.
26 (not unlike how out-of-order CPUs retire their instructions in order)
28 Key to the asynchronous function call implementation is the concept of
29 a "sequence cookie" (which, although it has an abstracted type, can be
30 thought of as a monotonically incrementing number).
32 The async core will assign each scheduled event such a sequence cookie and
33 pass this to the called functions.
35 The asynchronously called function should before doing a globally visible
36 operation, such as registering device numbers, call the
37 async_synchronize_cookie() function and pass in its own cookie. The
38 async_synchronize_cookie() function will make sure that all asynchronous
39 operations that were scheduled prior to the operation corresponding with the
40 cookie have completed.
42 Subsystem/driver initialization code that scheduled asynchronous probe
43 functions, but which shares global resources with other drivers/subsystems
44 that do not use the asynchronous call feature, need to do a full
45 synchronization with the async_synchronize_full() function, before returning
46 from their init function. This is to maintain strict ordering between the
47 asynchronous and synchronous parts of the kernel.
51 #include <linux/async.h>
52 #include <linux/module.h>
53 #include <linux/wait.h>
54 #include <linux/sched.h>
55 #include <linux/init.h>
56 #include <linux/kthread.h>
57 #include <asm/atomic.h>
59 static async_cookie_t next_cookie
= 1;
61 #define MAX_THREADS 256
62 #define MAX_WORK 32768
64 static LIST_HEAD(async_pending
);
65 static LIST_HEAD(async_running
);
66 static DEFINE_SPINLOCK(async_lock
);
68 static int async_enabled
= 0;
71 struct list_head list
;
72 async_cookie_t cookie
;
75 struct list_head
*running
;
78 static DECLARE_WAIT_QUEUE_HEAD(async_done
);
79 static DECLARE_WAIT_QUEUE_HEAD(async_new
);
81 static atomic_t entry_count
;
82 static atomic_t thread_count
;
84 extern int initcall_debug
;
88 * MUST be called with the lock held!
90 static async_cookie_t
__lowest_in_progress(struct list_head
*running
)
92 struct async_entry
*entry
;
93 if (!list_empty(&async_pending
)) {
94 entry
= list_first_entry(&async_pending
,
95 struct async_entry
, list
);
97 } else if (!list_empty(running
)) {
98 entry
= list_first_entry(running
,
99 struct async_entry
, list
);
100 return entry
->cookie
;
102 /* nothing in progress... next_cookie is "infinity" */
108 * pick the first pending entry and run it
110 static void run_one_entry(void)
113 struct async_entry
*entry
;
114 ktime_t calltime
, delta
, rettime
;
116 /* 1) pick one task from the pending queue */
118 spin_lock_irqsave(&async_lock
, flags
);
119 if (list_empty(&async_pending
))
121 entry
= list_first_entry(&async_pending
, struct async_entry
, list
);
123 /* 2) move it to the running queue */
124 list_del(&entry
->list
);
125 list_add_tail(&entry
->list
, &async_running
);
126 spin_unlock_irqrestore(&async_lock
, flags
);
128 /* 3) run it (and print duration)*/
129 if (initcall_debug
&& system_state
== SYSTEM_BOOTING
) {
130 printk("calling %lli_%pF @ %i\n", entry
->cookie
, entry
->func
, task_pid_nr(current
));
131 calltime
= ktime_get();
133 entry
->func(entry
->data
, entry
->cookie
);
134 if (initcall_debug
&& system_state
== SYSTEM_BOOTING
) {
135 rettime
= ktime_get();
136 delta
= ktime_sub(rettime
, calltime
);
137 printk("initcall %lli_%pF returned 0 after %lld usecs\n", entry
->cookie
,
138 entry
->func
, ktime_to_ns(delta
) >> 10);
141 /* 4) remove it from the running queue */
142 spin_lock_irqsave(&async_lock
, flags
);
143 list_del(&entry
->list
);
145 /* 5) free the entry */
147 atomic_dec(&entry_count
);
149 spin_unlock_irqrestore(&async_lock
, flags
);
151 /* 6) wake up any waiters. */
152 wake_up(&async_done
);
156 spin_unlock_irqrestore(&async_lock
, flags
);
160 static async_cookie_t
__async_schedule(async_func_ptr
*ptr
, void *data
, struct list_head
*running
)
162 struct async_entry
*entry
;
164 async_cookie_t newcookie
;
167 /* allow irq-off callers */
168 entry
= kzalloc(sizeof(struct async_entry
), GFP_ATOMIC
);
171 * If we're out of memory or if there's too much work
172 * pending already, we execute synchronously.
174 if (!async_enabled
|| !entry
|| atomic_read(&entry_count
) > MAX_WORK
) {
176 spin_lock_irqsave(&async_lock
, flags
);
177 newcookie
= next_cookie
++;
178 spin_unlock_irqrestore(&async_lock
, flags
);
180 /* low on memory.. run synchronously */
181 ptr(data
, newcookie
);
186 entry
->running
= running
;
188 spin_lock_irqsave(&async_lock
, flags
);
189 newcookie
= entry
->cookie
= next_cookie
++;
190 list_add_tail(&entry
->list
, &async_pending
);
191 atomic_inc(&entry_count
);
192 spin_unlock_irqrestore(&async_lock
, flags
);
197 async_cookie_t
async_schedule(async_func_ptr
*ptr
, void *data
)
199 return __async_schedule(ptr
, data
, &async_pending
);
201 EXPORT_SYMBOL_GPL(async_schedule
);
203 async_cookie_t
async_schedule_special(async_func_ptr
*ptr
, void *data
, struct list_head
*running
)
205 return __async_schedule(ptr
, data
, running
);
207 EXPORT_SYMBOL_GPL(async_schedule_special
);
209 void async_synchronize_full(void)
212 async_synchronize_cookie(next_cookie
);
213 } while (!list_empty(&async_running
) || !list_empty(&async_pending
));
215 EXPORT_SYMBOL_GPL(async_synchronize_full
);
217 void async_synchronize_full_special(struct list_head
*list
)
219 async_synchronize_cookie_special(next_cookie
, list
);
221 EXPORT_SYMBOL_GPL(async_synchronize_full_special
);
223 void async_synchronize_cookie_special(async_cookie_t cookie
, struct list_head
*running
)
225 ktime_t starttime
, delta
, endtime
;
227 if (initcall_debug
&& system_state
== SYSTEM_BOOTING
) {
228 printk("async_waiting @ %i\n", task_pid_nr(current
));
229 starttime
= ktime_get();
232 wait_event(async_done
, __lowest_in_progress(running
) >= cookie
);
234 if (initcall_debug
&& system_state
== SYSTEM_BOOTING
) {
235 endtime
= ktime_get();
236 delta
= ktime_sub(endtime
, starttime
);
238 printk("async_continuing @ %i after %lli usec\n",
239 task_pid_nr(current
), ktime_to_ns(delta
) >> 10);
242 EXPORT_SYMBOL_GPL(async_synchronize_cookie_special
);
244 void async_synchronize_cookie(async_cookie_t cookie
)
246 async_synchronize_cookie_special(cookie
, &async_running
);
248 EXPORT_SYMBOL_GPL(async_synchronize_cookie
);
251 static int async_thread(void *unused
)
253 DECLARE_WAITQUEUE(wq
, current
);
254 add_wait_queue(&async_new
, &wq
);
256 while (!kthread_should_stop()) {
258 set_current_state(TASK_INTERRUPTIBLE
);
260 * check the list head without lock.. false positives
261 * are dealt with inside run_one_entry() while holding
265 if (!list_empty(&async_pending
))
268 ret
= schedule_timeout(HZ
);
272 * we timed out, this means we as thread are redundant.
273 * we sign off and die, but we to avoid any races there
274 * is a last-straw check to see if work snuck in.
276 atomic_dec(&thread_count
);
277 wmb(); /* manager must see our departure first */
278 if (list_empty(&async_pending
))
281 * woops work came in between us timing out and us
282 * signing off; we need to stay alive and keep working.
284 atomic_inc(&thread_count
);
287 remove_wait_queue(&async_new
, &wq
);
292 static int async_manager_thread(void *unused
)
294 DECLARE_WAITQUEUE(wq
, current
);
295 add_wait_queue(&async_new
, &wq
);
297 while (!kthread_should_stop()) {
300 set_current_state(TASK_INTERRUPTIBLE
);
302 tc
= atomic_read(&thread_count
);
304 ec
= atomic_read(&entry_count
);
306 while (tc
< ec
&& tc
< MAX_THREADS
) {
307 kthread_run(async_thread
, NULL
, "async/%i", tc
);
308 atomic_inc(&thread_count
);
314 remove_wait_queue(&async_new
, &wq
);
319 static int __init
async_init(void)
322 kthread_run(async_manager_thread
, NULL
, "async/mgr");
326 static int __init
setup_async(char *str
)
332 __setup("fastboot", setup_async
);
335 core_initcall(async_init
);