async.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * async.c: Asynchronous function calls for boot performance
  4. *
  5. * (C) Copyright 2009 Intel Corporation
  6. * Author: Arjan van de Ven <arjan@linux.intel.com>
  7. */
  8. /*
  9. Goals and Theory of Operation
  10. The primary goal of this feature is to reduce the kernel boot time,
  11. by doing various independent hardware delays and discovery operations
  12. decoupled and not strictly serialized.
  13. More specifically, the asynchronous function call concept allows
  14. certain operations (primarily during system boot) to happen
  15. asynchronously, out of order, while these operations still
  16. have their externally visible parts happen sequentially and in-order.
  17. (not unlike how out-of-order CPUs retire their instructions in order)
  18. Key to the asynchronous function call implementation is the concept of
  19. a "sequence cookie" (which, although it has an abstracted type, can be
  20. thought of as a monotonically incrementing number).
  21. The async core will assign each scheduled event such a sequence cookie and
  22. pass this to the called functions.
  23. The asynchronously called function should before doing a globally visible
  24. operation, such as registering device numbers, call the
  25. async_synchronize_cookie() function and pass in its own cookie. The
  26. async_synchronize_cookie() function will make sure that all asynchronous
  27. operations that were scheduled prior to the operation corresponding with the
  28. cookie have completed.
  29. Subsystem/driver initialization code that scheduled asynchronous probe
  30. functions, but which shares global resources with other drivers/subsystems
  31. that do not use the asynchronous call feature, need to do a full
  32. synchronization with the async_synchronize_full() function, before returning
  33. from their init function. This is to maintain strict ordering between the
  34. asynchronous and synchronous parts of the kernel.
  35. */
  36. #include <linux/async.h>
  37. #include <linux/atomic.h>
  38. #include <linux/ktime.h>
  39. #include <linux/export.h>
  40. #include <linux/wait.h>
  41. #include <linux/sched.h>
  42. #include <linux/slab.h>
  43. #include <linux/workqueue.h>
  44. #include "workqueue_internal.h"
  45. static async_cookie_t next_cookie = 1;
  46. #define MAX_WORK 32768
  47. #define ASYNC_COOKIE_MAX ULLONG_MAX /* infinity cookie */
  48. static LIST_HEAD(async_global_pending); /* pending from all registered doms */
  49. static ASYNC_DOMAIN(async_dfl_domain);
  50. static DEFINE_SPINLOCK(async_lock);
  51. struct async_entry {
  52. struct list_head domain_list;
  53. struct list_head global_list;
  54. struct work_struct work;
  55. async_cookie_t cookie;
  56. async_func_t func;
  57. void *data;
  58. struct async_domain *domain;
  59. };
  60. static DECLARE_WAIT_QUEUE_HEAD(async_done);
  61. static atomic_t entry_count;
  62. static async_cookie_t lowest_in_progress(struct async_domain *domain)
  63. {
  64. struct async_entry *first = NULL;
  65. async_cookie_t ret = ASYNC_COOKIE_MAX;
  66. unsigned long flags;
  67. spin_lock_irqsave(&async_lock, flags);
  68. if (domain) {
  69. if (!list_empty(&domain->pending))
  70. first = list_first_entry(&domain->pending,
  71. struct async_entry, domain_list);
  72. } else {
  73. if (!list_empty(&async_global_pending))
  74. first = list_first_entry(&async_global_pending,
  75. struct async_entry, global_list);
  76. }
  77. if (first)
  78. ret = first->cookie;
  79. spin_unlock_irqrestore(&async_lock, flags);
  80. return ret;
  81. }
  82. /*
  83. * pick the first pending entry and run it
  84. */
  85. static void async_run_entry_fn(struct work_struct *work)
  86. {
  87. struct async_entry *entry =
  88. container_of(work, struct async_entry, work);
  89. unsigned long flags;
  90. ktime_t calltime, delta, rettime;
  91. /* 1) run (and print duration) */
  92. if (initcall_debug && system_state < SYSTEM_RUNNING) {
  93. pr_debug("calling %lli_%pS @ %i\n",
  94. (long long)entry->cookie,
  95. entry->func, task_pid_nr(current));
  96. calltime = ktime_get();
  97. }
  98. entry->func(entry->data, entry->cookie);
  99. if (initcall_debug && system_state < SYSTEM_RUNNING) {
  100. rettime = ktime_get();
  101. delta = ktime_sub(rettime, calltime);
  102. pr_debug("initcall %lli_%pS returned 0 after %lld usecs\n",
  103. (long long)entry->cookie,
  104. entry->func,
  105. (long long)ktime_to_ns(delta) >> 10);
  106. }
  107. /* 2) remove self from the pending queues */
  108. spin_lock_irqsave(&async_lock, flags);
  109. list_del_init(&entry->domain_list);
  110. list_del_init(&entry->global_list);
  111. /* 3) free the entry */
  112. kfree(entry);
  113. atomic_dec(&entry_count);
  114. spin_unlock_irqrestore(&async_lock, flags);
  115. /* 4) wake up any waiters */
  116. wake_up(&async_done);
  117. }
  118. /**
  119. * async_schedule_node_domain - NUMA specific version of async_schedule_domain
  120. * @func: function to execute asynchronously
  121. * @data: data pointer to pass to the function
  122. * @node: NUMA node that we want to schedule this on or close to
  123. * @domain: the domain
  124. *
  125. * Returns an async_cookie_t that may be used for checkpointing later.
  126. * @domain may be used in the async_synchronize_*_domain() functions to
  127. * wait within a certain synchronization domain rather than globally.
  128. *
  129. * Note: This function may be called from atomic or non-atomic contexts.
  130. *
  131. * The node requested will be honored on a best effort basis. If the node
  132. * has no CPUs associated with it then the work is distributed among all
  133. * available CPUs.
  134. */
  135. async_cookie_t async_schedule_node_domain(async_func_t func, void *data,
  136. int node, struct async_domain *domain)
  137. {
  138. struct async_entry *entry;
  139. unsigned long flags;
  140. async_cookie_t newcookie;
  141. /* allow irq-off callers */
  142. entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
  143. /*
  144. * If we're out of memory or if there's too much work
  145. * pending already, we execute synchronously.
  146. */
  147. if (!entry || atomic_read(&entry_count) > MAX_WORK) {
  148. kfree(entry);
  149. spin_lock_irqsave(&async_lock, flags);
  150. newcookie = next_cookie++;
  151. spin_unlock_irqrestore(&async_lock, flags);
  152. /* low on memory.. run synchronously */
  153. func(data, newcookie);
  154. return newcookie;
  155. }
  156. INIT_LIST_HEAD(&entry->domain_list);
  157. INIT_LIST_HEAD(&entry->global_list);
  158. INIT_WORK(&entry->work, async_run_entry_fn);
  159. entry->func = func;
  160. entry->data = data;
  161. entry->domain = domain;
  162. spin_lock_irqsave(&async_lock, flags);
  163. /* allocate cookie and queue */
  164. newcookie = entry->cookie = next_cookie++;
  165. list_add_tail(&entry->domain_list, &domain->pending);
  166. if (domain->registered)
  167. list_add_tail(&entry->global_list, &async_global_pending);
  168. atomic_inc(&entry_count);
  169. spin_unlock_irqrestore(&async_lock, flags);
  170. /* schedule for execution */
  171. queue_work_node(node, system_unbound_wq, &entry->work);
  172. return newcookie;
  173. }
  174. EXPORT_SYMBOL_GPL(async_schedule_node_domain);
  175. /**
  176. * async_schedule_node - NUMA specific version of async_schedule
  177. * @func: function to execute asynchronously
  178. * @data: data pointer to pass to the function
  179. * @node: NUMA node that we want to schedule this on or close to
  180. *
  181. * Returns an async_cookie_t that may be used for checkpointing later.
  182. * Note: This function may be called from atomic or non-atomic contexts.
  183. *
  184. * The node requested will be honored on a best effort basis. If the node
  185. * has no CPUs associated with it then the work is distributed among all
  186. * available CPUs.
  187. */
  188. async_cookie_t async_schedule_node(async_func_t func, void *data, int node)
  189. {
  190. return async_schedule_node_domain(func, data, node, &async_dfl_domain);
  191. }
  192. EXPORT_SYMBOL_GPL(async_schedule_node);
  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 starttime, delta, endtime;
  244. if (initcall_debug && system_state < SYSTEM_RUNNING) {
  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_RUNNING) {
  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);