async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/atomic.h>
  42. #include <linux/ktime.h>
  43. #include <linux/export.h>
  44. #include <linux/wait.h>
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/workqueue.h>
  48. #include "workqueue_internal.h"
  49. static async_cookie_t next_cookie = 1;
  50. #define MAX_WORK 32768
  51. static LIST_HEAD(async_pending);
  52. static ASYNC_DOMAIN(async_running);
  53. static LIST_HEAD(async_domains);
  54. static DEFINE_SPINLOCK(async_lock);
  55. static DEFINE_MUTEX(async_register_mutex);
  56. struct async_entry {
  57. struct list_head list;
  58. struct work_struct work;
  59. async_cookie_t cookie;
  60. async_func_ptr *func;
  61. void *data;
  62. struct async_domain *running;
  63. };
  64. static DECLARE_WAIT_QUEUE_HEAD(async_done);
  65. static atomic_t entry_count;
  66. /*
  67. * MUST be called with the lock held!
  68. */
  69. static async_cookie_t __lowest_in_progress(struct async_domain *running)
  70. {
  71. struct async_entry *entry;
  72. if (!list_empty(&running->domain)) {
  73. entry = list_first_entry(&running->domain, typeof(*entry), list);
  74. return entry->cookie;
  75. }
  76. list_for_each_entry(entry, &async_pending, list)
  77. if (entry->running == running)
  78. return entry->cookie;
  79. return next_cookie; /* "infinity" value */
  80. }
  81. static async_cookie_t lowest_in_progress(struct async_domain *running)
  82. {
  83. unsigned long flags;
  84. async_cookie_t ret;
  85. spin_lock_irqsave(&async_lock, flags);
  86. ret = __lowest_in_progress(running);
  87. spin_unlock_irqrestore(&async_lock, flags);
  88. return ret;
  89. }
  90. /*
  91. * pick the first pending entry and run it
  92. */
  93. static void async_run_entry_fn(struct work_struct *work)
  94. {
  95. struct async_entry *entry =
  96. container_of(work, struct async_entry, work);
  97. unsigned long flags;
  98. ktime_t uninitialized_var(calltime), delta, rettime;
  99. struct async_domain *running = entry->running;
  100. /* 1) move self to the running queue */
  101. spin_lock_irqsave(&async_lock, flags);
  102. list_move_tail(&entry->list, &running->domain);
  103. spin_unlock_irqrestore(&async_lock, flags);
  104. /* 2) run (and print duration) */
  105. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  106. printk(KERN_DEBUG "calling %lli_%pF @ %i\n",
  107. (long long)entry->cookie,
  108. entry->func, task_pid_nr(current));
  109. calltime = ktime_get();
  110. }
  111. entry->func(entry->data, entry->cookie);
  112. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  113. rettime = ktime_get();
  114. delta = ktime_sub(rettime, calltime);
  115. printk(KERN_DEBUG "initcall %lli_%pF returned 0 after %lld usecs\n",
  116. (long long)entry->cookie,
  117. entry->func,
  118. (long long)ktime_to_ns(delta) >> 10);
  119. }
  120. /* 3) remove self from the running queue */
  121. spin_lock_irqsave(&async_lock, flags);
  122. list_del(&entry->list);
  123. if (running->registered && --running->count == 0)
  124. list_del_init(&running->node);
  125. /* 4) free the entry */
  126. kfree(entry);
  127. atomic_dec(&entry_count);
  128. spin_unlock_irqrestore(&async_lock, flags);
  129. /* 5) wake up any waiters */
  130. wake_up(&async_done);
  131. }
  132. static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct async_domain *running)
  133. {
  134. struct async_entry *entry;
  135. unsigned long flags;
  136. async_cookie_t newcookie;
  137. /* allow irq-off callers */
  138. entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
  139. /*
  140. * If we're out of memory or if there's too much work
  141. * pending already, we execute synchronously.
  142. */
  143. if (!entry || atomic_read(&entry_count) > MAX_WORK) {
  144. kfree(entry);
  145. spin_lock_irqsave(&async_lock, flags);
  146. newcookie = next_cookie++;
  147. spin_unlock_irqrestore(&async_lock, flags);
  148. /* low on memory.. run synchronously */
  149. ptr(data, newcookie);
  150. return newcookie;
  151. }
  152. INIT_WORK(&entry->work, async_run_entry_fn);
  153. entry->func = ptr;
  154. entry->data = data;
  155. entry->running = running;
  156. spin_lock_irqsave(&async_lock, flags);
  157. newcookie = entry->cookie = next_cookie++;
  158. list_add_tail(&entry->list, &async_pending);
  159. if (running->registered && running->count++ == 0)
  160. list_add_tail(&running->node, &async_domains);
  161. atomic_inc(&entry_count);
  162. spin_unlock_irqrestore(&async_lock, flags);
  163. /* schedule for execution */
  164. queue_work(system_unbound_wq, &entry->work);
  165. return newcookie;
  166. }
  167. /**
  168. * async_schedule - schedule a function for asynchronous execution
  169. * @ptr: function to execute asynchronously
  170. * @data: data pointer to pass to the function
  171. *
  172. * Returns an async_cookie_t that may be used for checkpointing later.
  173. * Note: This function may be called from atomic or non-atomic contexts.
  174. */
  175. async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
  176. {
  177. return __async_schedule(ptr, data, &async_running);
  178. }
  179. EXPORT_SYMBOL_GPL(async_schedule);
  180. /**
  181. * async_schedule_domain - schedule a function for asynchronous execution within a certain domain
  182. * @ptr: function to execute asynchronously
  183. * @data: data pointer to pass to the function
  184. * @running: running list for the domain
  185. *
  186. * Returns an async_cookie_t that may be used for checkpointing later.
  187. * @running may be used in the async_synchronize_*_domain() functions
  188. * to wait within a certain synchronization domain rather than globally.
  189. * A synchronization domain is specified via the running queue @running to use.
  190. * Note: This function may be called from atomic or non-atomic contexts.
  191. */
  192. async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
  193. struct async_domain *running)
  194. {
  195. return __async_schedule(ptr, data, running);
  196. }
  197. EXPORT_SYMBOL_GPL(async_schedule_domain);
  198. /**
  199. * async_synchronize_full - synchronize all asynchronous function calls
  200. *
  201. * This function waits until all asynchronous function calls have been done.
  202. */
  203. void async_synchronize_full(void)
  204. {
  205. mutex_lock(&async_register_mutex);
  206. do {
  207. struct async_domain *domain = NULL;
  208. spin_lock_irq(&async_lock);
  209. if (!list_empty(&async_domains))
  210. domain = list_first_entry(&async_domains, typeof(*domain), node);
  211. spin_unlock_irq(&async_lock);
  212. async_synchronize_cookie_domain(next_cookie, domain);
  213. } while (!list_empty(&async_domains));
  214. mutex_unlock(&async_register_mutex);
  215. }
  216. EXPORT_SYMBOL_GPL(async_synchronize_full);
  217. /**
  218. * async_unregister_domain - ensure no more anonymous waiters on this domain
  219. * @domain: idle domain to flush out of any async_synchronize_full instances
  220. *
  221. * async_synchronize_{cookie|full}_domain() are not flushed since callers
  222. * of these routines should know the lifetime of @domain
  223. *
  224. * Prefer ASYNC_DOMAIN_EXCLUSIVE() declarations over flushing
  225. */
  226. void async_unregister_domain(struct async_domain *domain)
  227. {
  228. mutex_lock(&async_register_mutex);
  229. spin_lock_irq(&async_lock);
  230. WARN_ON(!domain->registered || !list_empty(&domain->node) ||
  231. !list_empty(&domain->domain));
  232. domain->registered = 0;
  233. spin_unlock_irq(&async_lock);
  234. mutex_unlock(&async_register_mutex);
  235. }
  236. EXPORT_SYMBOL_GPL(async_unregister_domain);
  237. /**
  238. * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain
  239. * @domain: running list to synchronize on
  240. *
  241. * This function waits until all asynchronous function calls for the
  242. * synchronization domain specified by the running list @domain have been done.
  243. */
  244. void async_synchronize_full_domain(struct async_domain *domain)
  245. {
  246. async_synchronize_cookie_domain(next_cookie, domain);
  247. }
  248. EXPORT_SYMBOL_GPL(async_synchronize_full_domain);
  249. /**
  250. * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing
  251. * @cookie: async_cookie_t to use as checkpoint
  252. * @running: running list to synchronize on
  253. *
  254. * This function waits until all asynchronous function calls for the
  255. * synchronization domain specified by running list @running submitted
  256. * prior to @cookie have been done.
  257. */
  258. void async_synchronize_cookie_domain(async_cookie_t cookie, struct async_domain *running)
  259. {
  260. ktime_t uninitialized_var(starttime), delta, endtime;
  261. if (!running)
  262. return;
  263. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  264. printk(KERN_DEBUG "async_waiting @ %i\n", task_pid_nr(current));
  265. starttime = ktime_get();
  266. }
  267. wait_event(async_done, lowest_in_progress(running) >= cookie);
  268. if (initcall_debug && system_state == SYSTEM_BOOTING) {
  269. endtime = ktime_get();
  270. delta = ktime_sub(endtime, starttime);
  271. printk(KERN_DEBUG "async_continuing @ %i after %lli usec\n",
  272. task_pid_nr(current),
  273. (long long)ktime_to_ns(delta) >> 10);
  274. }
  275. }
  276. EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain);
  277. /**
  278. * async_synchronize_cookie - synchronize asynchronous function calls with cookie checkpointing
  279. * @cookie: async_cookie_t to use as checkpoint
  280. *
  281. * This function waits until all asynchronous function calls prior to @cookie
  282. * have been done.
  283. */
  284. void async_synchronize_cookie(async_cookie_t cookie)
  285. {
  286. async_synchronize_cookie_domain(cookie, &async_running);
  287. }
  288. EXPORT_SYMBOL_GPL(async_synchronize_cookie);
  289. /**
  290. * current_is_async - is %current an async worker task?
  291. *
  292. * Returns %true if %current is an async worker task.
  293. */
  294. bool current_is_async(void)
  295. {
  296. struct worker *worker = current_wq_worker();
  297. return worker && worker->current_func == async_run_entry_fn;
  298. }