async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. Goals and Theory of Operation
  14. The primary goal of this feature is to reduce the kernel boot time,
  15. by doing various independent hardware delays and discovery operations
  16. decoupled and not strictly serialized.
  17. More specifically, the asynchronous function call concept allows
  18. certain operations (primarily during system boot) to happen
  19. asynchronously, out of order, while these operations still
  20. have their externally visible parts happen sequentially and in-order.
  21. (not unlike how out-of-order CPUs retire their instructions in order)
  22. Key to the asynchronous function call implementation is the concept of
  23. a "sequence cookie" (which, although it has an abstracted type, can be
  24. thought of as a monotonically incrementing number).
  25. The async core will assign each scheduled event such a sequence cookie and
  26. pass this to the called functions.
  27. The asynchronously called function should before doing a globally visible
  28. operation, such as registering device numbers, call the
  29. async_synchronize_cookie() function and pass in its own cookie. The
  30. async_synchronize_cookie() function will make sure that all asynchronous
  31. operations that were scheduled prior to the operation corresponding with the
  32. cookie have completed.
  33. Subsystem/driver initialization code that scheduled asynchronous probe
  34. functions, but which shares global resources with other drivers/subsystems
  35. that do not use the asynchronous call feature, need to do a full
  36. synchronization with the async_synchronize_full() function, before returning
  37. from their init function. This is to maintain strict ordering between the
  38. asynchronous and synchronous parts of the kernel.
  39. */
  40. #include <linux/async.h>
  41. #include <linux/bug.h>
  42. #include <linux/module.h>
  43. #include <linux/wait.h>
  44. #include <linux/sched.h>
  45. #include <linux/init.h>
  46. #include <linux/kthread.h>
  47. #include <linux/delay.h>
  48. #include <asm/atomic.h>
  49. static async_cookie_t next_cookie = 1;
  50. #define MAX_THREADS 256
  51. #define MAX_WORK 32768
  52. static LIST_HEAD(async_pending);
  53. static LIST_HEAD(async_running);
  54. static DEFINE_SPINLOCK(async_lock);
  55. static int async_enabled = 0;
  56. struct async_entry {
  57. struct list_head list;
  58. async_cookie_t cookie;
  59. async_func_ptr *func;
  60. void *data;
  61. struct list_head *running;
  62. };
  63. static DECLARE_WAIT_QUEUE_HEAD(async_done);
  64. static DECLARE_WAIT_QUEUE_HEAD(async_new);
  65. static atomic_t entry_count;
  66. static atomic_t thread_count;
  67. extern int initcall_debug;
  68. /*
  69. * MUST be called with the lock held!
  70. */
  71. static async_cookie_t __lowest_in_progress(struct list_head *running)
  72. {
  73. struct async_entry *entry;
  74. if (!list_empty(running)) {
  75. entry = list_first_entry(running,
  76. struct async_entry, list);
  77. return entry->cookie;
  78. }
  79. list_for_each_entry(entry, &async_pending, list)
  80. if (entry->running == running)
  81. return entry->cookie;
  82. return next_cookie; /* "infinity" value */
  83. }
  84. static async_cookie_t lowest_in_progress(struct list_head *running)
  85. {
  86. unsigned long flags;
  87. async_cookie_t ret;
  88. spin_lock_irqsave(&async_lock, flags);
  89. ret = __lowest_in_progress(running);
  90. spin_unlock_irqrestore(&async_lock, flags);
  91. return ret;
  92. }
  93. /*
  94. * pick the first pending entry and run it
  95. */
  96. static void run_one_entry(void)
  97. {
  98. unsigned long flags;
  99. struct async_entry *entry;
  100. ktime_t calltime, delta, rettime;
  101. /* 1) pick one task from the pending queue */
  102. spin_lock_irqsave(&async_lock, flags);
  103. if (list_empty(&async_pending))
  104. goto out;
  105. entry = list_first_entry(&async_pending, struct async_entry, list);
  106. /* 2) move it to the running queue */
  107. list_move_tail(&entry->list, entry->running);
  108. spin_unlock_irqrestore(&async_lock, flags);
  109. /* 3) run it (and print duration)*/
  110. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  111. printk("calling %lli_%pF @ %i\n", (long long)entry->cookie,
  112. entry->func, task_pid_nr(current));
  113. calltime = ktime_get();
  114. }
  115. entry->func(entry->data, entry->cookie);
  116. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  117. rettime = ktime_get();
  118. delta = ktime_sub(rettime, calltime);
  119. printk("initcall %lli_%pF returned 0 after %lld usecs\n",
  120. (long long)entry->cookie,
  121. entry->func,
  122. (long long)ktime_to_ns(delta) >> 10);
  123. }
  124. /* 4) remove it from the running queue */
  125. spin_lock_irqsave(&async_lock, flags);
  126. list_del(&entry->list);
  127. /* 5) free the entry */
  128. kfree(entry);
  129. atomic_dec(&entry_count);
  130. spin_unlock_irqrestore(&async_lock, flags);
  131. /* 6) wake up any waiters. */
  132. wake_up(&async_done);
  133. return;
  134. out:
  135. spin_unlock_irqrestore(&async_lock, flags);
  136. }
  137. static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
  138. {
  139. struct async_entry *entry;
  140. unsigned long flags;
  141. async_cookie_t newcookie;
  142. /* allow irq-off callers */
  143. entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
  144. /*
  145. * If we're out of memory or if there's too much work
  146. * pending already, we execute synchronously.
  147. */
  148. if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) {
  149. kfree(entry);
  150. spin_lock_irqsave(&async_lock, flags);
  151. newcookie = next_cookie++;
  152. spin_unlock_irqrestore(&async_lock, flags);
  153. /* low on memory.. run synchronously */
  154. ptr(data, newcookie);
  155. return newcookie;
  156. }
  157. entry->func = ptr;
  158. entry->data = data;
  159. entry->running = running;
  160. spin_lock_irqsave(&async_lock, flags);
  161. newcookie = entry->cookie = next_cookie++;
  162. list_add_tail(&entry->list, &async_pending);
  163. atomic_inc(&entry_count);
  164. spin_unlock_irqrestore(&async_lock, flags);
  165. wake_up(&async_new);
  166. return newcookie;
  167. }
  168. /**
  169. * async_schedule - schedule a function for asynchronous execution
  170. * @ptr: function to execute asynchronously
  171. * @data: data pointer to pass to the function
  172. *
  173. * Returns an async_cookie_t that may be used for checkpointing later.
  174. * Note: This function may be called from atomic or non-atomic contexts.
  175. */
  176. async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
  177. {
  178. return __async_schedule(ptr, data, &async_running);
  179. }
  180. EXPORT_SYMBOL_GPL(async_schedule);
  181. /**
  182. * async_schedule_domain - schedule a function for asynchronous execution within a certain domain
  183. * @ptr: function to execute asynchronously
  184. * @data: data pointer to pass to the function
  185. * @running: running list for the domain
  186. *
  187. * Returns an async_cookie_t that may be used for checkpointing later.
  188. * @running may be used in the async_synchronize_*_domain() functions
  189. * to wait within a certain synchronization domain rather than globally.
  190. * A synchronization domain is specified via the running queue @running to use.
  191. * Note: This function may be called from atomic or non-atomic contexts.
  192. */
  193. async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
  194. struct list_head *running)
  195. {
  196. return __async_schedule(ptr, data, running);
  197. }
  198. EXPORT_SYMBOL_GPL(async_schedule_domain);
  199. /**
  200. * async_synchronize_full - synchronize all asynchronous function calls
  201. *
  202. * This function waits until all asynchronous function calls have been done.
  203. */
  204. void async_synchronize_full(void)
  205. {
  206. do {
  207. async_synchronize_cookie(next_cookie);
  208. } while (!list_empty(&async_running) || !list_empty(&async_pending));
  209. }
  210. EXPORT_SYMBOL_GPL(async_synchronize_full);
  211. /**
  212. * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain
  213. * @list: running list to synchronize on
  214. *
  215. * This function waits until all asynchronous function calls for the
  216. * synchronization domain specified by the running list @list have been done.
  217. */
  218. void async_synchronize_full_domain(struct list_head *list)
  219. {
  220. async_synchronize_cookie_domain(next_cookie, list);
  221. }
  222. EXPORT_SYMBOL_GPL(async_synchronize_full_domain);
  223. /**
  224. * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing
  225. * @cookie: async_cookie_t to use as checkpoint
  226. * @running: running list to synchronize on
  227. *
  228. * This function waits until all asynchronous function calls for the
  229. * synchronization domain specified by the running list @list submitted
  230. * prior to @cookie have been done.
  231. */
  232. void async_synchronize_cookie_domain(async_cookie_t cookie,
  233. struct list_head *running)
  234. {
  235. ktime_t starttime, delta, endtime;
  236. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  237. printk("async_waiting @ %i\n", task_pid_nr(current));
  238. starttime = ktime_get();
  239. }
  240. wait_event(async_done, lowest_in_progress(running) >= cookie);
  241. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  242. endtime = ktime_get();
  243. delta = ktime_sub(endtime, starttime);
  244. printk("async_continuing @ %i after %lli usec\n",
  245. task_pid_nr(current),
  246. (long long)ktime_to_ns(delta) >> 10);
  247. }
  248. }
  249. EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain);
  250. /**
  251. * async_synchronize_cookie - synchronize asynchronous function calls with cookie checkpointing
  252. * @cookie: async_cookie_t to use as checkpoint
  253. *
  254. * This function waits until all asynchronous function calls prior to @cookie
  255. * have been done.
  256. */
  257. void async_synchronize_cookie(async_cookie_t cookie)
  258. {
  259. async_synchronize_cookie_domain(cookie, &async_running);
  260. }
  261. EXPORT_SYMBOL_GPL(async_synchronize_cookie);
  262. static int async_thread(void *unused)
  263. {
  264. DECLARE_WAITQUEUE(wq, current);
  265. add_wait_queue(&async_new, &wq);
  266. while (!kthread_should_stop()) {
  267. int ret = HZ;
  268. set_current_state(TASK_INTERRUPTIBLE);
  269. /*
  270. * check the list head without lock.. false positives
  271. * are dealt with inside run_one_entry() while holding
  272. * the lock.
  273. */
  274. rmb();
  275. if (!list_empty(&async_pending))
  276. run_one_entry();
  277. else
  278. ret = schedule_timeout(HZ);
  279. if (ret == 0) {
  280. /*
  281. * we timed out, this means we as thread are redundant.
  282. * we sign off and die, but we to avoid any races there
  283. * is a last-straw check to see if work snuck in.
  284. */
  285. atomic_dec(&thread_count);
  286. wmb(); /* manager must see our departure first */
  287. if (list_empty(&async_pending))
  288. break;
  289. /*
  290. * woops work came in between us timing out and us
  291. * signing off; we need to stay alive and keep working.
  292. */
  293. atomic_inc(&thread_count);
  294. }
  295. }
  296. remove_wait_queue(&async_new, &wq);
  297. return 0;
  298. }
  299. static int async_manager_thread(void *unused)
  300. {
  301. DECLARE_WAITQUEUE(wq, current);
  302. add_wait_queue(&async_new, &wq);
  303. while (!kthread_should_stop()) {
  304. int tc, ec;
  305. set_current_state(TASK_INTERRUPTIBLE);
  306. tc = atomic_read(&thread_count);
  307. rmb();
  308. ec = atomic_read(&entry_count);
  309. while (tc < ec && tc < MAX_THREADS) {
  310. if (IS_ERR(kthread_run(async_thread, NULL, "async/%i",
  311. tc))) {
  312. msleep(100);
  313. continue;
  314. }
  315. atomic_inc(&thread_count);
  316. tc++;
  317. }
  318. schedule();
  319. }
  320. remove_wait_queue(&async_new, &wq);
  321. return 0;
  322. }
  323. static int __init async_init(void)
  324. {
  325. async_enabled =
  326. !IS_ERR(kthread_run(async_manager_thread, NULL, "async/mgr"));
  327. WARN_ON(!async_enabled);
  328. return 0;
  329. }
  330. core_initcall(async_init);