async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/export.h>
  39. #include <linux/ktime.h>
  40. #include <linux/pid.h>
  41. #include <linux/sched.h>
  42. #include <linux/slab.h>
  43. #include <linux/wait.h>
  44. #include <linux/workqueue.h>
  45. #include "workqueue_internal.h"
  46. static async_cookie_t next_cookie = 1;
  47. #define MAX_WORK 32768
  48. #define ASYNC_COOKIE_MAX ULLONG_MAX /* infinity cookie */
  49. static LIST_HEAD(async_global_pending); /* pending from all registered doms */
  50. static ASYNC_DOMAIN(async_dfl_domain);
  51. static DEFINE_SPINLOCK(async_lock);
  52. static struct workqueue_struct *async_wq;
  53. struct async_entry {
  54. struct list_head domain_list;
  55. struct list_head global_list;
  56. struct work_struct work;
  57. async_cookie_t cookie;
  58. async_func_t func;
  59. void *data;
  60. struct async_domain *domain;
  61. };
  62. static DECLARE_WAIT_QUEUE_HEAD(async_done);
  63. static atomic_t entry_count;
  64. static long long microseconds_since(ktime_t start)
  65. {
  66. ktime_t now = ktime_get();
  67. return ktime_to_ns(ktime_sub(now, start)) >> 10;
  68. }
  69. static async_cookie_t lowest_in_progress(struct async_domain *domain)
  70. {
  71. struct async_entry *first = NULL;
  72. async_cookie_t ret = ASYNC_COOKIE_MAX;
  73. unsigned long flags;
  74. spin_lock_irqsave(&async_lock, flags);
  75. if (domain) {
  76. if (!list_empty(&domain->pending))
  77. first = list_first_entry(&domain->pending,
  78. struct async_entry, domain_list);
  79. } else {
  80. if (!list_empty(&async_global_pending))
  81. first = list_first_entry(&async_global_pending,
  82. struct async_entry, global_list);
  83. }
  84. if (first)
  85. ret = first->cookie;
  86. spin_unlock_irqrestore(&async_lock, flags);
  87. return ret;
  88. }
  89. /*
  90. * pick the first pending entry and run it
  91. */
  92. static void async_run_entry_fn(struct work_struct *work)
  93. {
  94. struct async_entry *entry =
  95. container_of(work, struct async_entry, work);
  96. unsigned long flags;
  97. ktime_t calltime;
  98. /* 1) run (and print duration) */
  99. pr_debug("calling %lli_%pS @ %i\n", (long long)entry->cookie,
  100. entry->func, task_pid_nr(current));
  101. calltime = ktime_get();
  102. entry->func(entry->data, entry->cookie);
  103. pr_debug("initcall %lli_%pS returned after %lld usecs\n",
  104. (long long)entry->cookie, entry->func,
  105. microseconds_since(calltime));
  106. /* 2) remove self from the pending queues */
  107. spin_lock_irqsave(&async_lock, flags);
  108. list_del_init(&entry->domain_list);
  109. list_del_init(&entry->global_list);
  110. /* 3) free the entry */
  111. kfree(entry);
  112. atomic_dec(&entry_count);
  113. spin_unlock_irqrestore(&async_lock, flags);
  114. /* 4) wake up any waiters */
  115. wake_up(&async_done);
  116. }
  117. static async_cookie_t __async_schedule_node_domain(async_func_t func,
  118. void *data, int node,
  119. struct async_domain *domain,
  120. struct async_entry *entry)
  121. {
  122. async_cookie_t newcookie;
  123. unsigned long flags;
  124. INIT_LIST_HEAD(&entry->domain_list);
  125. INIT_LIST_HEAD(&entry->global_list);
  126. INIT_WORK(&entry->work, async_run_entry_fn);
  127. entry->func = func;
  128. entry->data = data;
  129. entry->domain = domain;
  130. spin_lock_irqsave(&async_lock, flags);
  131. /* allocate cookie and queue */
  132. newcookie = entry->cookie = next_cookie++;
  133. list_add_tail(&entry->domain_list, &domain->pending);
  134. if (domain->registered)
  135. list_add_tail(&entry->global_list, &async_global_pending);
  136. atomic_inc(&entry_count);
  137. spin_unlock_irqrestore(&async_lock, flags);
  138. /* schedule for execution */
  139. queue_work_node(node, async_wq, &entry->work);
  140. return newcookie;
  141. }
  142. /**
  143. * async_schedule_node_domain - NUMA specific version of async_schedule_domain
  144. * @func: function to execute asynchronously
  145. * @data: data pointer to pass to the function
  146. * @node: NUMA node that we want to schedule this on or close to
  147. * @domain: the domain
  148. *
  149. * Returns an async_cookie_t that may be used for checkpointing later.
  150. * @domain may be used in the async_synchronize_*_domain() functions to
  151. * wait within a certain synchronization domain rather than globally.
  152. *
  153. * Note: This function may be called from atomic or non-atomic contexts.
  154. *
  155. * The node requested will be honored on a best effort basis. If the node
  156. * has no CPUs associated with it then the work is distributed among all
  157. * available CPUs.
  158. */
  159. async_cookie_t async_schedule_node_domain(async_func_t func, void *data,
  160. int node, struct async_domain *domain)
  161. {
  162. struct async_entry *entry;
  163. unsigned long flags;
  164. async_cookie_t newcookie;
  165. /* allow irq-off callers */
  166. entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
  167. /*
  168. * If we're out of memory or if there's too much work
  169. * pending already, we execute synchronously.
  170. */
  171. if (!entry || atomic_read(&entry_count) > MAX_WORK) {
  172. kfree(entry);
  173. spin_lock_irqsave(&async_lock, flags);
  174. newcookie = next_cookie++;
  175. spin_unlock_irqrestore(&async_lock, flags);
  176. /* low on memory.. run synchronously */
  177. func(data, newcookie);
  178. return newcookie;
  179. }
  180. return __async_schedule_node_domain(func, data, node, domain, entry);
  181. }
  182. EXPORT_SYMBOL_GPL(async_schedule_node_domain);
  183. /**
  184. * async_schedule_node - NUMA specific version of async_schedule
  185. * @func: function to execute asynchronously
  186. * @data: data pointer to pass to the function
  187. * @node: NUMA node that we want to schedule this on or close to
  188. *
  189. * Returns an async_cookie_t that may be used for checkpointing later.
  190. * Note: This function may be called from atomic or non-atomic contexts.
  191. *
  192. * The node requested will be honored on a best effort basis. If the node
  193. * has no CPUs associated with it then the work is distributed among all
  194. * available CPUs.
  195. */
  196. async_cookie_t async_schedule_node(async_func_t func, void *data, int node)
  197. {
  198. return async_schedule_node_domain(func, data, node, &async_dfl_domain);
  199. }
  200. EXPORT_SYMBOL_GPL(async_schedule_node);
  201. /**
  202. * async_schedule_dev_nocall - A simplified variant of async_schedule_dev()
  203. * @func: function to execute asynchronously
  204. * @dev: device argument to be passed to function
  205. *
  206. * @dev is used as both the argument for the function and to provide NUMA
  207. * context for where to run the function.
  208. *
  209. * If the asynchronous execution of @func is scheduled successfully, return
  210. * true. Otherwise, do nothing and return false, unlike async_schedule_dev()
  211. * that will run the function synchronously then.
  212. */
  213. bool async_schedule_dev_nocall(async_func_t func, struct device *dev)
  214. {
  215. struct async_entry *entry;
  216. entry = kzalloc(sizeof(struct async_entry), GFP_KERNEL);
  217. /* Give up if there is no memory or too much work. */
  218. if (!entry || atomic_read(&entry_count) > MAX_WORK) {
  219. kfree(entry);
  220. return false;
  221. }
  222. __async_schedule_node_domain(func, dev, dev_to_node(dev),
  223. &async_dfl_domain, entry);
  224. return true;
  225. }
  226. /**
  227. * async_synchronize_full - synchronize all asynchronous function calls
  228. *
  229. * This function waits until all asynchronous function calls have been done.
  230. */
  231. void async_synchronize_full(void)
  232. {
  233. async_synchronize_full_domain(NULL);
  234. }
  235. EXPORT_SYMBOL_GPL(async_synchronize_full);
  236. /**
  237. * async_synchronize_full_domain - synchronize all asynchronous function within a certain domain
  238. * @domain: the domain to synchronize
  239. *
  240. * This function waits until all asynchronous function calls for the
  241. * synchronization domain specified by @domain have been done.
  242. */
  243. void async_synchronize_full_domain(struct async_domain *domain)
  244. {
  245. async_synchronize_cookie_domain(ASYNC_COOKIE_MAX, domain);
  246. }
  247. EXPORT_SYMBOL_GPL(async_synchronize_full_domain);
  248. /**
  249. * async_synchronize_cookie_domain - synchronize asynchronous function calls within a certain domain with cookie checkpointing
  250. * @cookie: async_cookie_t to use as checkpoint
  251. * @domain: the domain to synchronize (%NULL for all registered domains)
  252. *
  253. * This function waits until all asynchronous function calls for the
  254. * synchronization domain specified by @domain submitted prior to @cookie
  255. * have been done.
  256. */
  257. void async_synchronize_cookie_domain(async_cookie_t cookie, struct async_domain *domain)
  258. {
  259. ktime_t starttime;
  260. pr_debug("async_waiting @ %i\n", task_pid_nr(current));
  261. starttime = ktime_get();
  262. wait_event(async_done, lowest_in_progress(domain) >= cookie);
  263. pr_debug("async_continuing @ %i after %lli usec\n", task_pid_nr(current),
  264. microseconds_since(starttime));
  265. }
  266. EXPORT_SYMBOL_GPL(async_synchronize_cookie_domain);
  267. /**
  268. * async_synchronize_cookie - synchronize asynchronous function calls with cookie checkpointing
  269. * @cookie: async_cookie_t to use as checkpoint
  270. *
  271. * This function waits until all asynchronous function calls prior to @cookie
  272. * have been done.
  273. */
  274. void async_synchronize_cookie(async_cookie_t cookie)
  275. {
  276. async_synchronize_cookie_domain(cookie, &async_dfl_domain);
  277. }
  278. EXPORT_SYMBOL_GPL(async_synchronize_cookie);
  279. /**
  280. * current_is_async - is %current an async worker task?
  281. *
  282. * Returns %true if %current is an async worker task.
  283. */
  284. bool current_is_async(void)
  285. {
  286. struct worker *worker = current_wq_worker();
  287. return worker && worker->current_func == async_run_entry_fn;
  288. }
  289. EXPORT_SYMBOL_GPL(current_is_async);
  290. void __init async_init(void)
  291. {
  292. /*
  293. * Async can schedule a number of interdependent work items. However,
  294. * unbound workqueues can handle only upto min_active interdependent
  295. * work items. The default min_active of 8 isn't sufficient for async
  296. * and can lead to stalls. Let's use a dedicated workqueue with raised
  297. * min_active.
  298. */
  299. async_wq = alloc_workqueue("async", WQ_UNBOUND, 0);
  300. BUG_ON(!async_wq);
  301. workqueue_set_min_active(async_wq, WQ_DFL_ACTIVE);
  302. }