async.c 10 KB

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