async.c 10 KB

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