async.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/slab.h>
  45. #include <linux/workqueue.h>
  46. #include <asm/atomic.h>
  47. static async_cookie_t next_cookie = 1;
  48. #define MAX_WORK 32768
  49. static LIST_HEAD(async_pending);
  50. static LIST_HEAD(async_running);
  51. static DEFINE_SPINLOCK(async_lock);
  52. struct async_entry {
  53. struct list_head list;
  54. struct work_struct work;
  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 atomic_t entry_count;
  62. extern int initcall_debug;
  63. /*
  64. * MUST be called with the lock held!
  65. */
  66. static async_cookie_t __lowest_in_progress(struct list_head *running)
  67. {
  68. struct async_entry *entry;
  69. if (!list_empty(running)) {
  70. entry = list_first_entry(running,
  71. struct async_entry, list);
  72. return entry->cookie;
  73. }
  74. list_for_each_entry(entry, &async_pending, list)
  75. if (entry->running == running)
  76. return entry->cookie;
  77. return next_cookie; /* "infinity" value */
  78. }
  79. static async_cookie_t lowest_in_progress(struct list_head *running)
  80. {
  81. unsigned long flags;
  82. async_cookie_t ret;
  83. spin_lock_irqsave(&async_lock, flags);
  84. ret = __lowest_in_progress(running);
  85. spin_unlock_irqrestore(&async_lock, flags);
  86. return ret;
  87. }
  88. /*
  89. * pick the first pending entry and run it
  90. */
  91. static void async_run_entry_fn(struct work_struct *work)
  92. {
  93. struct async_entry *entry =
  94. container_of(work, struct async_entry, work);
  95. unsigned long flags;
  96. ktime_t calltime, delta, rettime;
  97. /* 1) move self to the running queue */
  98. spin_lock_irqsave(&async_lock, flags);
  99. list_move_tail(&entry->list, entry->running);
  100. spin_unlock_irqrestore(&async_lock, flags);
  101. /* 2) run (and print duration) */
  102. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  103. printk("calling %lli_%pF @ %i\n", (long long)entry->cookie,
  104. entry->func, task_pid_nr(current));
  105. calltime = ktime_get();
  106. }
  107. entry->func(entry->data, entry->cookie);
  108. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  109. rettime = ktime_get();
  110. delta = ktime_sub(rettime, calltime);
  111. printk("initcall %lli_%pF returned 0 after %lld usecs\n",
  112. (long long)entry->cookie,
  113. entry->func,
  114. (long long)ktime_to_ns(delta) >> 10);
  115. }
  116. /* 3) remove self from the running queue */
  117. spin_lock_irqsave(&async_lock, flags);
  118. list_del(&entry->list);
  119. /* 4) free the entry */
  120. kfree(entry);
  121. atomic_dec(&entry_count);
  122. spin_unlock_irqrestore(&async_lock, flags);
  123. /* 5) wake up any waiters */
  124. wake_up(&async_done);
  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. INIT_WORK(&entry->work, async_run_entry_fn);
  147. entry->func = ptr;
  148. entry->data = data;
  149. entry->running = running;
  150. spin_lock_irqsave(&async_lock, flags);
  151. newcookie = entry->cookie = next_cookie++;
  152. list_add_tail(&entry->list, &async_pending);
  153. atomic_inc(&entry_count);
  154. spin_unlock_irqrestore(&async_lock, flags);
  155. /* schedule for execution */
  156. queue_work(system_unbound_wq, &entry->work);
  157. return newcookie;
  158. }
  159. /**
  160. * async_schedule - schedule a function for asynchronous execution
  161. * @ptr: function to execute asynchronously
  162. * @data: data pointer to pass to the function
  163. *
  164. * Returns an async_cookie_t that may be used for checkpointing later.
  165. * Note: This function may be called from atomic or non-atomic contexts.
  166. */
  167. async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
  168. {
  169. return __async_schedule(ptr, data, &async_running);
  170. }
  171. EXPORT_SYMBOL_GPL(async_schedule);
  172. /**
  173. * async_schedule_domain - schedule a function for asynchronous execution within a certain domain
  174. * @ptr: function to execute asynchronously
  175. * @data: data pointer to pass to the function
  176. * @running: running list for the domain
  177. *
  178. * Returns an async_cookie_t that may be used for checkpointing later.
  179. * @running may be used in the async_synchronize_*_domain() functions
  180. * to wait within a certain synchronization domain rather than globally.
  181. * A synchronization domain is specified via the running queue @running to use.
  182. * Note: This function may be called from atomic or non-atomic contexts.
  183. */
  184. async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
  185. struct list_head *running)
  186. {
  187. return __async_schedule(ptr, data, running);
  188. }
  189. EXPORT_SYMBOL_GPL(async_schedule_domain);
  190. /**
  191. * async_synchronize_full - synchronize all asynchronous function calls
  192. *
  193. * This function waits until all asynchronous function calls have been done.
  194. */
  195. void async_synchronize_full(void)
  196. {
  197. do {
  198. async_synchronize_cookie(next_cookie);
  199. } while (!list_empty(&async_running) || !list_empty(&async_pending));
  200. }
  201. EXPORT_SYMBOL_GPL(async_synchronize_full);
  202. /**
  203. * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain
  204. * @list: running list to synchronize on
  205. *
  206. * This function waits until all asynchronous function calls for the
  207. * synchronization domain specified by the running list @list have been done.
  208. */
  209. void async_synchronize_full_domain(struct list_head *list)
  210. {
  211. async_synchronize_cookie_domain(next_cookie, list);
  212. }
  213. EXPORT_SYMBOL_GPL(async_synchronize_full_domain);
  214. /**
  215. * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing
  216. * @cookie: async_cookie_t to use as checkpoint
  217. * @running: running list to synchronize on
  218. *
  219. * This function waits until all asynchronous function calls for the
  220. * synchronization domain specified by the running list @list submitted
  221. * prior to @cookie have been done.
  222. */
  223. void async_synchronize_cookie_domain(async_cookie_t cookie,
  224. struct list_head *running)
  225. {
  226. 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();
  230. }
  231. 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);
  235. printk("async_continuing @ %i after %lli usec\n",
  236. task_pid_nr(current),
  237. (long long)ktime_to_ns(delta) >> 10);
  238. }
  239. }
  240. EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain);
  241. /**
  242. * async_synchronize_cookie - synchronize asynchronous function calls with cookie checkpointing
  243. * @cookie: async_cookie_t to use as checkpoint
  244. *
  245. * This function waits until all asynchronous function calls prior to @cookie
  246. * have been done.
  247. */
  248. void async_synchronize_cookie(async_cookie_t cookie)
  249. {
  250. async_synchronize_cookie_domain(cookie, &async_running);
  251. }
  252. EXPORT_SYMBOL_GPL(async_synchronize_cookie);