]>
Commit | Line | Data |
---|---|---|
22a9d645 AV |
1 | /* |
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 | ||
16 | Goals and Theory of Operation | |
17 | ||
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. | |
21 | ||
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) | |
27 | ||
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). | |
31 | ||
32 | The async core will assign each scheduled event such a sequence cookie and | |
33 | pass this to the called functions. | |
34 | ||
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. | |
41 | ||
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. | |
48 | ||
49 | */ | |
50 | ||
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> | |
58 | ||
59 | static async_cookie_t next_cookie = 1; | |
60 | ||
61 | #define MAX_THREADS 256 | |
62 | #define MAX_WORK 32768 | |
63 | ||
64 | static LIST_HEAD(async_pending); | |
65 | static LIST_HEAD(async_running); | |
66 | static DEFINE_SPINLOCK(async_lock); | |
67 | ||
cdb80f63 AV |
68 | static int async_enabled = 0; |
69 | ||
22a9d645 AV |
70 | struct async_entry { |
71 | struct list_head list; | |
72 | async_cookie_t cookie; | |
73 | async_func_ptr *func; | |
74 | void *data; | |
75 | struct list_head *running; | |
76 | }; | |
77 | ||
78 | static DECLARE_WAIT_QUEUE_HEAD(async_done); | |
79 | static DECLARE_WAIT_QUEUE_HEAD(async_new); | |
80 | ||
81 | static atomic_t entry_count; | |
82 | static atomic_t thread_count; | |
83 | ||
84 | extern int initcall_debug; | |
85 | ||
86 | ||
87 | /* | |
88 | * MUST be called with the lock held! | |
89 | */ | |
90 | static async_cookie_t __lowest_in_progress(struct list_head *running) | |
91 | { | |
92 | struct async_entry *entry; | |
37a76bd4 AV |
93 | if (!list_empty(running)) { |
94 | entry = list_first_entry(running, | |
22a9d645 AV |
95 | struct async_entry, list); |
96 | return entry->cookie; | |
37a76bd4 AV |
97 | } else if (!list_empty(&async_pending)) { |
98 | entry = list_first_entry(&async_pending, | |
22a9d645 AV |
99 | struct async_entry, list); |
100 | return entry->cookie; | |
101 | } else { | |
102 | /* nothing in progress... next_cookie is "infinity" */ | |
103 | return next_cookie; | |
104 | } | |
105 | ||
106 | } | |
37a76bd4 AV |
107 | |
108 | static async_cookie_t lowest_in_progress(struct list_head *running) | |
109 | { | |
110 | unsigned long flags; | |
111 | async_cookie_t ret; | |
112 | ||
113 | spin_lock_irqsave(&async_lock, flags); | |
114 | ret = __lowest_in_progress(running); | |
115 | spin_unlock_irqrestore(&async_lock, flags); | |
116 | return ret; | |
117 | } | |
22a9d645 AV |
118 | /* |
119 | * pick the first pending entry and run it | |
120 | */ | |
121 | static void run_one_entry(void) | |
122 | { | |
123 | unsigned long flags; | |
124 | struct async_entry *entry; | |
125 | ktime_t calltime, delta, rettime; | |
126 | ||
127 | /* 1) pick one task from the pending queue */ | |
128 | ||
129 | spin_lock_irqsave(&async_lock, flags); | |
130 | if (list_empty(&async_pending)) | |
131 | goto out; | |
132 | entry = list_first_entry(&async_pending, struct async_entry, list); | |
133 | ||
134 | /* 2) move it to the running queue */ | |
135 | list_del(&entry->list); | |
136 | list_add_tail(&entry->list, &async_running); | |
137 | spin_unlock_irqrestore(&async_lock, flags); | |
138 | ||
139 | /* 3) run it (and print duration)*/ | |
ad160d23 | 140 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
58763a29 AM |
141 | printk("calling %lli_%pF @ %i\n", (long long)entry->cookie, |
142 | entry->func, task_pid_nr(current)); | |
22a9d645 AV |
143 | calltime = ktime_get(); |
144 | } | |
145 | entry->func(entry->data, entry->cookie); | |
ad160d23 | 146 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
22a9d645 AV |
147 | rettime = ktime_get(); |
148 | delta = ktime_sub(rettime, calltime); | |
58763a29 AM |
149 | printk("initcall %lli_%pF returned 0 after %lld usecs\n", |
150 | (long long)entry->cookie, | |
151 | entry->func, | |
152 | (long long)ktime_to_ns(delta) >> 10); | |
22a9d645 AV |
153 | } |
154 | ||
155 | /* 4) remove it from the running queue */ | |
156 | spin_lock_irqsave(&async_lock, flags); | |
157 | list_del(&entry->list); | |
158 | ||
159 | /* 5) free the entry */ | |
160 | kfree(entry); | |
161 | atomic_dec(&entry_count); | |
162 | ||
163 | spin_unlock_irqrestore(&async_lock, flags); | |
164 | ||
165 | /* 6) wake up any waiters. */ | |
166 | wake_up(&async_done); | |
167 | return; | |
168 | ||
169 | out: | |
170 | spin_unlock_irqrestore(&async_lock, flags); | |
171 | } | |
172 | ||
173 | ||
174 | static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running) | |
175 | { | |
176 | struct async_entry *entry; | |
177 | unsigned long flags; | |
178 | async_cookie_t newcookie; | |
179 | ||
180 | ||
181 | /* allow irq-off callers */ | |
182 | entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC); | |
183 | ||
184 | /* | |
185 | * If we're out of memory or if there's too much work | |
186 | * pending already, we execute synchronously. | |
187 | */ | |
cdb80f63 | 188 | if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) { |
22a9d645 AV |
189 | kfree(entry); |
190 | spin_lock_irqsave(&async_lock, flags); | |
191 | newcookie = next_cookie++; | |
192 | spin_unlock_irqrestore(&async_lock, flags); | |
193 | ||
194 | /* low on memory.. run synchronously */ | |
195 | ptr(data, newcookie); | |
196 | return newcookie; | |
197 | } | |
198 | entry->func = ptr; | |
199 | entry->data = data; | |
200 | entry->running = running; | |
201 | ||
202 | spin_lock_irqsave(&async_lock, flags); | |
203 | newcookie = entry->cookie = next_cookie++; | |
204 | list_add_tail(&entry->list, &async_pending); | |
205 | atomic_inc(&entry_count); | |
206 | spin_unlock_irqrestore(&async_lock, flags); | |
207 | wake_up(&async_new); | |
208 | return newcookie; | |
209 | } | |
210 | ||
211 | async_cookie_t async_schedule(async_func_ptr *ptr, void *data) | |
212 | { | |
213 | return __async_schedule(ptr, data, &async_pending); | |
214 | } | |
215 | EXPORT_SYMBOL_GPL(async_schedule); | |
216 | ||
217 | async_cookie_t async_schedule_special(async_func_ptr *ptr, void *data, struct list_head *running) | |
218 | { | |
219 | return __async_schedule(ptr, data, running); | |
220 | } | |
221 | EXPORT_SYMBOL_GPL(async_schedule_special); | |
222 | ||
223 | void async_synchronize_full(void) | |
224 | { | |
33b04b93 AV |
225 | do { |
226 | async_synchronize_cookie(next_cookie); | |
227 | } while (!list_empty(&async_running) || !list_empty(&async_pending)); | |
22a9d645 AV |
228 | } |
229 | EXPORT_SYMBOL_GPL(async_synchronize_full); | |
230 | ||
231 | void async_synchronize_full_special(struct list_head *list) | |
232 | { | |
233 | async_synchronize_cookie_special(next_cookie, list); | |
234 | } | |
235 | EXPORT_SYMBOL_GPL(async_synchronize_full_special); | |
236 | ||
237 | void async_synchronize_cookie_special(async_cookie_t cookie, struct list_head *running) | |
238 | { | |
239 | ktime_t starttime, delta, endtime; | |
240 | ||
ad160d23 | 241 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
22a9d645 AV |
242 | printk("async_waiting @ %i\n", task_pid_nr(current)); |
243 | starttime = ktime_get(); | |
244 | } | |
245 | ||
37a76bd4 | 246 | wait_event(async_done, lowest_in_progress(running) >= cookie); |
22a9d645 | 247 | |
ad160d23 | 248 | if (initcall_debug && system_state == SYSTEM_BOOTING) { |
22a9d645 AV |
249 | endtime = ktime_get(); |
250 | delta = ktime_sub(endtime, starttime); | |
251 | ||
252 | printk("async_continuing @ %i after %lli usec\n", | |
58763a29 AM |
253 | task_pid_nr(current), |
254 | (long long)ktime_to_ns(delta) >> 10); | |
22a9d645 AV |
255 | } |
256 | } | |
257 | EXPORT_SYMBOL_GPL(async_synchronize_cookie_special); | |
258 | ||
259 | void async_synchronize_cookie(async_cookie_t cookie) | |
260 | { | |
261 | async_synchronize_cookie_special(cookie, &async_running); | |
262 | } | |
263 | EXPORT_SYMBOL_GPL(async_synchronize_cookie); | |
264 | ||
265 | ||
266 | static int async_thread(void *unused) | |
267 | { | |
268 | DECLARE_WAITQUEUE(wq, current); | |
269 | add_wait_queue(&async_new, &wq); | |
270 | ||
271 | while (!kthread_should_stop()) { | |
272 | int ret = HZ; | |
273 | set_current_state(TASK_INTERRUPTIBLE); | |
274 | /* | |
275 | * check the list head without lock.. false positives | |
276 | * are dealt with inside run_one_entry() while holding | |
277 | * the lock. | |
278 | */ | |
279 | rmb(); | |
280 | if (!list_empty(&async_pending)) | |
281 | run_one_entry(); | |
282 | else | |
283 | ret = schedule_timeout(HZ); | |
284 | ||
285 | if (ret == 0) { | |
286 | /* | |
287 | * we timed out, this means we as thread are redundant. | |
288 | * we sign off and die, but we to avoid any races there | |
289 | * is a last-straw check to see if work snuck in. | |
290 | */ | |
291 | atomic_dec(&thread_count); | |
292 | wmb(); /* manager must see our departure first */ | |
293 | if (list_empty(&async_pending)) | |
294 | break; | |
295 | /* | |
296 | * woops work came in between us timing out and us | |
297 | * signing off; we need to stay alive and keep working. | |
298 | */ | |
299 | atomic_inc(&thread_count); | |
300 | } | |
301 | } | |
302 | remove_wait_queue(&async_new, &wq); | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | static int async_manager_thread(void *unused) | |
308 | { | |
309 | DECLARE_WAITQUEUE(wq, current); | |
310 | add_wait_queue(&async_new, &wq); | |
311 | ||
312 | while (!kthread_should_stop()) { | |
313 | int tc, ec; | |
314 | ||
315 | set_current_state(TASK_INTERRUPTIBLE); | |
316 | ||
317 | tc = atomic_read(&thread_count); | |
318 | rmb(); | |
319 | ec = atomic_read(&entry_count); | |
320 | ||
321 | while (tc < ec && tc < MAX_THREADS) { | |
322 | kthread_run(async_thread, NULL, "async/%i", tc); | |
323 | atomic_inc(&thread_count); | |
324 | tc++; | |
325 | } | |
326 | ||
327 | schedule(); | |
328 | } | |
329 | remove_wait_queue(&async_new, &wq); | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
334 | static int __init async_init(void) | |
335 | { | |
cdb80f63 AV |
336 | if (async_enabled) |
337 | kthread_run(async_manager_thread, NULL, "async/mgr"); | |
22a9d645 AV |
338 | return 0; |
339 | } | |
340 | ||
cdb80f63 AV |
341 | static int __init setup_async(char *str) |
342 | { | |
343 | async_enabled = 1; | |
344 | return 1; | |
345 | } | |
346 | ||
347 | __setup("fastboot", setup_async); | |
348 | ||
349 | ||
22a9d645 | 350 | core_initcall(async_init); |