async.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/module.h>
  42. #include <linux/wait.h>
  43. #include <linux/sched.h>
  44. #include <linux/init.h>
  45. #include <linux/kthread.h>
  46. #include <asm/atomic.h>
  47. static async_cookie_t next_cookie = 1;
  48. #define MAX_THREADS 256
  49. #define MAX_WORK 32768
  50. static LIST_HEAD(async_pending);
  51. static LIST_HEAD(async_running);
  52. static DEFINE_SPINLOCK(async_lock);
  53. struct async_entry {
  54. struct list_head list;
  55. async_cookie_t cookie;
  56. async_func_ptr *func;
  57. void *data;
  58. struct list_head *running;
  59. };
  60. static DECLARE_WAIT_QUEUE_HEAD(async_done);
  61. static DECLARE_WAIT_QUEUE_HEAD(async_new);
  62. static atomic_t entry_count;
  63. static atomic_t thread_count;
  64. extern int initcall_debug;
  65. /*
  66. * MUST be called with the lock held!
  67. */
  68. static async_cookie_t __lowest_in_progress(struct list_head *running)
  69. {
  70. struct async_entry *entry;
  71. if (!list_empty(&async_pending)) {
  72. entry = list_first_entry(&async_pending,
  73. struct async_entry, list);
  74. return entry->cookie;
  75. } else if (!list_empty(running)) {
  76. entry = list_first_entry(running,
  77. struct async_entry, list);
  78. return entry->cookie;
  79. } else {
  80. /* nothing in progress... next_cookie is "infinity" */
  81. return next_cookie;
  82. }
  83. }
  84. /*
  85. * pick the first pending entry and run it
  86. */
  87. static void run_one_entry(void)
  88. {
  89. unsigned long flags;
  90. struct async_entry *entry;
  91. ktime_t calltime, delta, rettime;
  92. /* 1) pick one task from the pending queue */
  93. spin_lock_irqsave(&async_lock, flags);
  94. if (list_empty(&async_pending))
  95. goto out;
  96. entry = list_first_entry(&async_pending, struct async_entry, list);
  97. /* 2) move it to the running queue */
  98. list_del(&entry->list);
  99. list_add_tail(&entry->list, &async_running);
  100. spin_unlock_irqrestore(&async_lock, flags);
  101. /* 3) run it (and print duration)*/
  102. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  103. printk("calling %lli_%pF @ %i\n", entry->cookie, entry->func, task_pid_nr(current));
  104. calltime = ktime_get();
  105. }
  106. entry->func(entry->data, entry->cookie);
  107. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  108. rettime = ktime_get();
  109. delta = ktime_sub(rettime, calltime);
  110. printk("initcall %lli_%pF returned 0 after %lld usecs\n", entry->cookie,
  111. entry->func, ktime_to_ns(delta) >> 10);
  112. }
  113. /* 4) remove it from the running queue */
  114. spin_lock_irqsave(&async_lock, flags);
  115. list_del(&entry->list);
  116. /* 5) free the entry */
  117. kfree(entry);
  118. atomic_dec(&entry_count);
  119. spin_unlock_irqrestore(&async_lock, flags);
  120. /* 6) wake up any waiters. */
  121. wake_up(&async_done);
  122. return;
  123. out:
  124. spin_unlock_irqrestore(&async_lock, flags);
  125. }
  126. static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
  127. {
  128. struct async_entry *entry;
  129. unsigned long flags;
  130. async_cookie_t newcookie;
  131. /* allow irq-off callers */
  132. entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
  133. /*
  134. * If we're out of memory or if there's too much work
  135. * pending already, we execute synchronously.
  136. */
  137. if (!entry || atomic_read(&entry_count) > MAX_WORK) {
  138. kfree(entry);
  139. spin_lock_irqsave(&async_lock, flags);
  140. newcookie = next_cookie++;
  141. spin_unlock_irqrestore(&async_lock, flags);
  142. /* low on memory.. run synchronously */
  143. ptr(data, newcookie);
  144. return newcookie;
  145. }
  146. entry->func = ptr;
  147. entry->data = data;
  148. entry->running = running;
  149. spin_lock_irqsave(&async_lock, flags);
  150. newcookie = entry->cookie = next_cookie++;
  151. list_add_tail(&entry->list, &async_pending);
  152. atomic_inc(&entry_count);
  153. spin_unlock_irqrestore(&async_lock, flags);
  154. wake_up(&async_new);
  155. return newcookie;
  156. }
  157. async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
  158. {
  159. return __async_schedule(ptr, data, &async_pending);
  160. }
  161. EXPORT_SYMBOL_GPL(async_schedule);
  162. async_cookie_t async_schedule_special(async_func_ptr *ptr, void *data, struct list_head *running)
  163. {
  164. return __async_schedule(ptr, data, running);
  165. }
  166. EXPORT_SYMBOL_GPL(async_schedule_special);
  167. void async_synchronize_full(void)
  168. {
  169. async_synchronize_cookie(next_cookie);
  170. }
  171. EXPORT_SYMBOL_GPL(async_synchronize_full);
  172. void async_synchronize_full_special(struct list_head *list)
  173. {
  174. async_synchronize_cookie_special(next_cookie, list);
  175. }
  176. EXPORT_SYMBOL_GPL(async_synchronize_full_special);
  177. void async_synchronize_cookie_special(async_cookie_t cookie, struct list_head *running)
  178. {
  179. ktime_t starttime, delta, endtime;
  180. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  181. printk("async_waiting @ %i\n", task_pid_nr(current));
  182. starttime = ktime_get();
  183. }
  184. wait_event(async_done, __lowest_in_progress(running) >= cookie);
  185. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  186. endtime = ktime_get();
  187. delta = ktime_sub(endtime, starttime);
  188. printk("async_continuing @ %i after %lli usec\n",
  189. task_pid_nr(current), ktime_to_ns(delta) >> 10);
  190. }
  191. }
  192. EXPORT_SYMBOL_GPL(async_synchronize_cookie_special);
  193. void async_synchronize_cookie(async_cookie_t cookie)
  194. {
  195. async_synchronize_cookie_special(cookie, &async_running);
  196. }
  197. EXPORT_SYMBOL_GPL(async_synchronize_cookie);
  198. static int async_thread(void *unused)
  199. {
  200. DECLARE_WAITQUEUE(wq, current);
  201. add_wait_queue(&async_new, &wq);
  202. while (!kthread_should_stop()) {
  203. int ret = HZ;
  204. set_current_state(TASK_INTERRUPTIBLE);
  205. /*
  206. * check the list head without lock.. false positives
  207. * are dealt with inside run_one_entry() while holding
  208. * the lock.
  209. */
  210. rmb();
  211. if (!list_empty(&async_pending))
  212. run_one_entry();
  213. else
  214. ret = schedule_timeout(HZ);
  215. if (ret == 0) {
  216. /*
  217. * we timed out, this means we as thread are redundant.
  218. * we sign off and die, but we to avoid any races there
  219. * is a last-straw check to see if work snuck in.
  220. */
  221. atomic_dec(&thread_count);
  222. wmb(); /* manager must see our departure first */
  223. if (list_empty(&async_pending))
  224. break;
  225. /*
  226. * woops work came in between us timing out and us
  227. * signing off; we need to stay alive and keep working.
  228. */
  229. atomic_inc(&thread_count);
  230. }
  231. }
  232. remove_wait_queue(&async_new, &wq);
  233. return 0;
  234. }
  235. static int async_manager_thread(void *unused)
  236. {
  237. DECLARE_WAITQUEUE(wq, current);
  238. add_wait_queue(&async_new, &wq);
  239. while (!kthread_should_stop()) {
  240. int tc, ec;
  241. set_current_state(TASK_INTERRUPTIBLE);
  242. tc = atomic_read(&thread_count);
  243. rmb();
  244. ec = atomic_read(&entry_count);
  245. while (tc < ec && tc < MAX_THREADS) {
  246. kthread_run(async_thread, NULL, "async/%i", tc);
  247. atomic_inc(&thread_count);
  248. tc++;
  249. }
  250. schedule();
  251. }
  252. remove_wait_queue(&async_new, &wq);
  253. return 0;
  254. }
  255. static int __init async_init(void)
  256. {
  257. kthread_run(async_manager_thread, NULL, "async/mgr");
  258. return 0;
  259. }
  260. core_initcall(async_init);