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);
69 struct list_head list;
70 async_cookie_t cookie;
73 struct list_head *running;
76 static DECLARE_WAIT_QUEUE_HEAD(async_done);
77 static DECLARE_WAIT_QUEUE_HEAD(async_new);
79 static atomic_t entry_count;
80 static atomic_t thread_count;
82 extern int initcall_debug;
86 * MUST be called with the lock held!
88 static async_cookie_t __lowest_in_progress(struct list_head *running)
90 struct async_entry *entry;
91 if (!list_empty(&async_pending)) {
92 entry = list_first_entry(&async_pending,
93 struct async_entry, list);
95 } else if (!list_empty(running)) {
96 entry = list_first_entry(running,
97 struct async_entry, list);
100 /* nothing in progress... next_cookie is "infinity" */
106 * pick the first pending entry and run it
108 static void run_one_entry(void)
111 struct async_entry *entry;
112 ktime_t calltime, delta, rettime;
114 /* 1) pick one task from the pending queue */
116 spin_lock_irqsave(&async_lock, flags);
117 if (list_empty(&async_pending))
119 entry = list_first_entry(&async_pending, struct async_entry, list);
121 /* 2) move it to the running queue */
122 list_del(&entry->list);
123 list_add_tail(&entry->list, &async_running);
124 spin_unlock_irqrestore(&async_lock, flags);
126 /* 3) run it (and print duration)*/
127 if (initcall_debug && system_state == SYSTEM_BOOTING) {
128 printk("calling %lli_%pF @ %i\n", entry->cookie, entry->func, task_pid_nr(current));
129 calltime = ktime_get();
131 entry->func(entry->data, entry->cookie);
132 if (initcall_debug && system_state == SYSTEM_BOOTING) {
133 rettime = ktime_get();
134 delta = ktime_sub(rettime, calltime);
135 printk("initcall %lli_%pF returned 0 after %lld usecs\n", entry->cookie,
136 entry->func, ktime_to_ns(delta) >> 10);
139 /* 4) remove it from the running queue */
140 spin_lock_irqsave(&async_lock, flags);
141 list_del(&entry->list);
143 /* 5) free the entry */
145 atomic_dec(&entry_count);
147 spin_unlock_irqrestore(&async_lock, flags);
149 /* 6) wake up any waiters. */
150 wake_up(&async_done);
154 spin_unlock_irqrestore(&async_lock, flags);
158 static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
160 struct async_entry *entry;
162 async_cookie_t newcookie;
165 /* allow irq-off callers */
166 entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
169 * If we're out of memory or if there's too much work
170 * pending already, we execute synchronously.
172 if (!entry || atomic_read(&entry_count) > MAX_WORK) {
174 spin_lock_irqsave(&async_lock, flags);
175 newcookie = next_cookie++;
176 spin_unlock_irqrestore(&async_lock, flags);
178 /* low on memory.. run synchronously */
179 ptr(data, newcookie);
184 entry->running = running;
186 spin_lock_irqsave(&async_lock, flags);
187 newcookie = entry->cookie = next_cookie++;
188 list_add_tail(&entry->list, &async_pending);
189 atomic_inc(&entry_count);
190 spin_unlock_irqrestore(&async_lock, flags);
195 async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
197 return __async_schedule(ptr, data, &async_pending);
199 EXPORT_SYMBOL_GPL(async_schedule);
201 async_cookie_t async_schedule_special(async_func_ptr *ptr, void *data, struct list_head *running)
203 return __async_schedule(ptr, data, running);
205 EXPORT_SYMBOL_GPL(async_schedule_special);
207 void async_synchronize_full(void)
210 async_synchronize_cookie(next_cookie);
211 } while (!list_empty(&async_running) || !list_empty(&async_pending));
213 EXPORT_SYMBOL_GPL(async_synchronize_full);
215 void async_synchronize_full_special(struct list_head *list)
217 async_synchronize_cookie_special(next_cookie, list);
219 EXPORT_SYMBOL_GPL(async_synchronize_full_special);
221 void async_synchronize_cookie_special(async_cookie_t cookie, struct list_head *running)
223 ktime_t starttime, delta, endtime;
225 if (initcall_debug && system_state == SYSTEM_BOOTING) {
226 printk("async_waiting @ %i\n", task_pid_nr(current));
227 starttime = ktime_get();
230 wait_event(async_done, __lowest_in_progress(running) >= cookie);
232 if (initcall_debug && system_state == SYSTEM_BOOTING) {
233 endtime = ktime_get();
234 delta = ktime_sub(endtime, starttime);
236 printk("async_continuing @ %i after %lli usec\n",
237 task_pid_nr(current), ktime_to_ns(delta) >> 10);
240 EXPORT_SYMBOL_GPL(async_synchronize_cookie_special);
242 void async_synchronize_cookie(async_cookie_t cookie)
244 async_synchronize_cookie_special(cookie, &async_running);
246 EXPORT_SYMBOL_GPL(async_synchronize_cookie);
249 static int async_thread(void *unused)
251 DECLARE_WAITQUEUE(wq, current);
252 add_wait_queue(&async_new, &wq);
254 while (!kthread_should_stop()) {
256 set_current_state(TASK_INTERRUPTIBLE);
258 * check the list head without lock.. false positives
259 * are dealt with inside run_one_entry() while holding
263 if (!list_empty(&async_pending))
266 ret = schedule_timeout(HZ);
270 * we timed out, this means we as thread are redundant.
271 * we sign off and die, but we to avoid any races there
272 * is a last-straw check to see if work snuck in.
274 atomic_dec(&thread_count);
275 wmb(); /* manager must see our departure first */
276 if (list_empty(&async_pending))
279 * woops work came in between us timing out and us
280 * signing off; we need to stay alive and keep working.
282 atomic_inc(&thread_count);
285 remove_wait_queue(&async_new, &wq);
290 static int async_manager_thread(void *unused)
292 DECLARE_WAITQUEUE(wq, current);
293 add_wait_queue(&async_new, &wq);
295 while (!kthread_should_stop()) {
298 set_current_state(TASK_INTERRUPTIBLE);
300 tc = atomic_read(&thread_count);
302 ec = atomic_read(&entry_count);
304 while (tc < ec && tc < MAX_THREADS) {
305 kthread_run(async_thread, NULL, "async/%i", tc);
306 atomic_inc(&thread_count);
312 remove_wait_queue(&async_new, &wq);
317 static int __init async_init(void)
319 kthread_run(async_manager_thread, NULL, "async/mgr");
323 core_initcall(async_init);