workqueue.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * workqueue.h --- work queue handling for Linux.
  4. */
  5. #ifndef _LINUX_WORKQUEUE_H
  6. #define _LINUX_WORKQUEUE_H
  7. #include <linux/timer.h>
  8. #include <linux/linkage.h>
  9. #include <linux/bitops.h>
  10. #include <linux/lockdep.h>
  11. #include <linux/threads.h>
  12. #include <linux/atomic.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/android_kabi.h>
  16. struct workqueue_struct;
  17. struct work_struct;
  18. typedef void (*work_func_t)(struct work_struct *work);
  19. void delayed_work_timer_fn(struct timer_list *t);
  20. /*
  21. * The first word is the work queue pointer and the flags rolled into
  22. * one
  23. */
  24. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  25. enum {
  26. WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
  27. WORK_STRUCT_INACTIVE_BIT= 1, /* work item is inactive */
  28. WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */
  29. WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */
  30. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  31. WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */
  32. WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */
  33. #else
  34. WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */
  35. #endif
  36. WORK_STRUCT_COLOR_BITS = 4,
  37. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  38. WORK_STRUCT_INACTIVE = 1 << WORK_STRUCT_INACTIVE_BIT,
  39. WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT,
  40. WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
  41. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  42. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  43. #else
  44. WORK_STRUCT_STATIC = 0,
  45. #endif
  46. WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS),
  47. /* not bound to any CPU, prefer the local CPU */
  48. WORK_CPU_UNBOUND = NR_CPUS,
  49. /*
  50. * Reserve 8 bits off of pwq pointer w/ debugobjects turned off.
  51. * This makes pwqs aligned to 256 bytes and allows 16 workqueue
  52. * flush colors.
  53. */
  54. WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
  55. WORK_STRUCT_COLOR_BITS,
  56. /* data contains off-queue information when !WORK_STRUCT_PWQ */
  57. WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT,
  58. __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE,
  59. /*
  60. * When a work item is off queue, its high bits point to the last
  61. * pool it was on. Cap at 31 bits and use the highest number to
  62. * indicate that no pool is associated.
  63. */
  64. WORK_OFFQ_FLAG_BITS = 1,
  65. WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
  66. WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
  67. WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
  68. /* bit mask for work_busy() return values */
  69. WORK_BUSY_PENDING = 1 << 0,
  70. WORK_BUSY_RUNNING = 1 << 1,
  71. /* maximum string length for set_worker_desc() */
  72. WORKER_DESC_LEN = 24,
  73. };
  74. /* Convenience constants - of type 'unsigned long', not 'enum'! */
  75. #define WORK_OFFQ_CANCELING (1ul << __WORK_OFFQ_CANCELING)
  76. #define WORK_OFFQ_POOL_NONE ((1ul << WORK_OFFQ_POOL_BITS) - 1)
  77. #define WORK_STRUCT_NO_POOL (WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT)
  78. #define WORK_STRUCT_FLAG_MASK ((1ul << WORK_STRUCT_FLAG_BITS) - 1)
  79. #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
  80. struct work_struct {
  81. atomic_long_t data;
  82. struct list_head entry;
  83. work_func_t func;
  84. #ifdef CONFIG_LOCKDEP
  85. struct lockdep_map lockdep_map;
  86. #endif
  87. ANDROID_KABI_RESERVE(1);
  88. ANDROID_KABI_RESERVE(2);
  89. };
  90. #define WORK_DATA_INIT() ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
  91. #define WORK_DATA_STATIC_INIT() \
  92. ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
  93. struct delayed_work {
  94. struct work_struct work;
  95. struct timer_list timer;
  96. /* target workqueue and CPU ->timer uses to queue ->work */
  97. struct workqueue_struct *wq;
  98. int cpu;
  99. ANDROID_KABI_RESERVE(1);
  100. ANDROID_KABI_RESERVE(2);
  101. };
  102. struct rcu_work {
  103. struct work_struct work;
  104. struct rcu_head rcu;
  105. /* target workqueue ->rcu uses to queue ->work */
  106. struct workqueue_struct *wq;
  107. };
  108. /**
  109. * struct workqueue_attrs - A struct for workqueue attributes.
  110. *
  111. * This can be used to change attributes of an unbound workqueue.
  112. */
  113. struct workqueue_attrs {
  114. /**
  115. * @nice: nice level
  116. */
  117. int nice;
  118. /**
  119. * @cpumask: allowed CPUs
  120. */
  121. cpumask_var_t cpumask;
  122. /**
  123. * @no_numa: disable NUMA affinity
  124. *
  125. * Unlike other fields, ``no_numa`` isn't a property of a worker_pool. It
  126. * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus
  127. * doesn't participate in pool hash calculations or equality comparisons.
  128. */
  129. bool no_numa;
  130. };
  131. static inline struct delayed_work *to_delayed_work(struct work_struct *work)
  132. {
  133. return container_of(work, struct delayed_work, work);
  134. }
  135. static inline struct rcu_work *to_rcu_work(struct work_struct *work)
  136. {
  137. return container_of(work, struct rcu_work, work);
  138. }
  139. struct execute_work {
  140. struct work_struct work;
  141. };
  142. #ifdef CONFIG_LOCKDEP
  143. /*
  144. * NB: because we have to copy the lockdep_map, setting _key
  145. * here is required, otherwise it could get initialised to the
  146. * copy of the lockdep_map!
  147. */
  148. #define __WORK_INIT_LOCKDEP_MAP(n, k) \
  149. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
  150. #else
  151. #define __WORK_INIT_LOCKDEP_MAP(n, k)
  152. #endif
  153. #define __WORK_INITIALIZER(n, f) { \
  154. .data = WORK_DATA_STATIC_INIT(), \
  155. .entry = { &(n).entry, &(n).entry }, \
  156. .func = (f), \
  157. __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
  158. }
  159. #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \
  160. .work = __WORK_INITIALIZER((n).work, (f)), \
  161. .timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\
  162. (tflags) | TIMER_IRQSAFE), \
  163. }
  164. #define DECLARE_WORK(n, f) \
  165. struct work_struct n = __WORK_INITIALIZER(n, f)
  166. #define DECLARE_DELAYED_WORK(n, f) \
  167. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
  168. #define DECLARE_DEFERRABLE_WORK(n, f) \
  169. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
  170. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  171. extern void __init_work(struct work_struct *work, int onstack);
  172. extern void destroy_work_on_stack(struct work_struct *work);
  173. extern void destroy_delayed_work_on_stack(struct delayed_work *work);
  174. static inline unsigned int work_static(struct work_struct *work)
  175. {
  176. return *work_data_bits(work) & WORK_STRUCT_STATIC;
  177. }
  178. #else
  179. static inline void __init_work(struct work_struct *work, int onstack) { }
  180. static inline void destroy_work_on_stack(struct work_struct *work) { }
  181. static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { }
  182. static inline unsigned int work_static(struct work_struct *work) { return 0; }
  183. #endif
  184. /*
  185. * initialize all of a work item in one go
  186. *
  187. * NOTE! No point in using "atomic_long_set()": using a direct
  188. * assignment of the work data initializer allows the compiler
  189. * to generate better code.
  190. */
  191. #ifdef CONFIG_LOCKDEP
  192. #define __INIT_WORK(_work, _func, _onstack) \
  193. do { \
  194. static struct lock_class_key __key; \
  195. \
  196. __init_work((_work), _onstack); \
  197. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  198. lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
  199. INIT_LIST_HEAD(&(_work)->entry); \
  200. (_work)->func = (_func); \
  201. } while (0)
  202. #else
  203. #define __INIT_WORK(_work, _func, _onstack) \
  204. do { \
  205. __init_work((_work), _onstack); \
  206. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  207. INIT_LIST_HEAD(&(_work)->entry); \
  208. (_work)->func = (_func); \
  209. } while (0)
  210. #endif
  211. #define INIT_WORK(_work, _func) \
  212. __INIT_WORK((_work), (_func), 0)
  213. #define INIT_WORK_ONSTACK(_work, _func) \
  214. __INIT_WORK((_work), (_func), 1)
  215. #define __INIT_DELAYED_WORK(_work, _func, _tflags) \
  216. do { \
  217. INIT_WORK(&(_work)->work, (_func)); \
  218. __init_timer(&(_work)->timer, \
  219. delayed_work_timer_fn, \
  220. (_tflags) | TIMER_IRQSAFE); \
  221. } while (0)
  222. #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \
  223. do { \
  224. INIT_WORK_ONSTACK(&(_work)->work, (_func)); \
  225. __init_timer_on_stack(&(_work)->timer, \
  226. delayed_work_timer_fn, \
  227. (_tflags) | TIMER_IRQSAFE); \
  228. } while (0)
  229. #define INIT_DELAYED_WORK(_work, _func) \
  230. __INIT_DELAYED_WORK(_work, _func, 0)
  231. #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \
  232. __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
  233. #define INIT_DEFERRABLE_WORK(_work, _func) \
  234. __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
  235. #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \
  236. __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
  237. #define INIT_RCU_WORK(_work, _func) \
  238. INIT_WORK(&(_work)->work, (_func))
  239. #define INIT_RCU_WORK_ONSTACK(_work, _func) \
  240. INIT_WORK_ONSTACK(&(_work)->work, (_func))
  241. /**
  242. * work_pending - Find out whether a work item is currently pending
  243. * @work: The work item in question
  244. */
  245. #define work_pending(work) \
  246. test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  247. /**
  248. * delayed_work_pending - Find out whether a delayable work item is currently
  249. * pending
  250. * @w: The work item in question
  251. */
  252. #define delayed_work_pending(w) \
  253. work_pending(&(w)->work)
  254. /*
  255. * Workqueue flags and constants. For details, please refer to
  256. * Documentation/core-api/workqueue.rst.
  257. */
  258. enum {
  259. WQ_UNBOUND = 1 << 1, /* not bound to any cpu */
  260. WQ_FREEZABLE = 1 << 2, /* freeze during suspend */
  261. WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */
  262. WQ_HIGHPRI = 1 << 4, /* high priority */
  263. WQ_CPU_INTENSIVE = 1 << 5, /* cpu intensive workqueue */
  264. WQ_SYSFS = 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
  265. /*
  266. * Per-cpu workqueues are generally preferred because they tend to
  267. * show better performance thanks to cache locality. Per-cpu
  268. * workqueues exclude the scheduler from choosing the CPU to
  269. * execute the worker threads, which has an unfortunate side effect
  270. * of increasing power consumption.
  271. *
  272. * The scheduler considers a CPU idle if it doesn't have any task
  273. * to execute and tries to keep idle cores idle to conserve power;
  274. * however, for example, a per-cpu work item scheduled from an
  275. * interrupt handler on an idle CPU will force the scheduler to
  276. * execute the work item on that CPU breaking the idleness, which in
  277. * turn may lead to more scheduling choices which are sub-optimal
  278. * in terms of power consumption.
  279. *
  280. * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default
  281. * but become unbound if workqueue.power_efficient kernel param is
  282. * specified. Per-cpu workqueues which are identified to
  283. * contribute significantly to power-consumption are identified and
  284. * marked with this flag and enabling the power_efficient mode
  285. * leads to noticeable power saving at the cost of small
  286. * performance disadvantage.
  287. *
  288. * http://thread.gmane.org/gmane.linux.kernel/1480396
  289. */
  290. WQ_POWER_EFFICIENT = 1 << 7,
  291. __WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */
  292. __WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */
  293. __WQ_LEGACY = 1 << 18, /* internal: create*_workqueue() */
  294. __WQ_ORDERED_EXPLICIT = 1 << 19, /* internal: alloc_ordered_workqueue() */
  295. WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */
  296. WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */
  297. WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2,
  298. };
  299. /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
  300. #define WQ_UNBOUND_MAX_ACTIVE \
  301. max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
  302. /*
  303. * System-wide workqueues which are always present.
  304. *
  305. * system_wq is the one used by schedule[_delayed]_work[_on]().
  306. * Multi-CPU multi-threaded. There are users which expect relatively
  307. * short queue flush time. Don't queue works which can run for too
  308. * long.
  309. *
  310. * system_highpri_wq is similar to system_wq but for work items which
  311. * require WQ_HIGHPRI.
  312. *
  313. * system_long_wq is similar to system_wq but may host long running
  314. * works. Queue flushing might take relatively long.
  315. *
  316. * system_unbound_wq is unbound workqueue. Workers are not bound to
  317. * any specific CPU, not concurrency managed, and all queued works are
  318. * executed immediately as long as max_active limit is not reached and
  319. * resources are available.
  320. *
  321. * system_freezable_wq is equivalent to system_wq except that it's
  322. * freezable.
  323. *
  324. * *_power_efficient_wq are inclined towards saving power and converted
  325. * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise,
  326. * they are same as their non-power-efficient counterparts - e.g.
  327. * system_power_efficient_wq is identical to system_wq if
  328. * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info.
  329. */
  330. extern struct workqueue_struct *system_wq;
  331. extern struct workqueue_struct *system_highpri_wq;
  332. extern struct workqueue_struct *system_long_wq;
  333. extern struct workqueue_struct *system_unbound_wq;
  334. extern struct workqueue_struct *system_freezable_wq;
  335. extern struct workqueue_struct *system_power_efficient_wq;
  336. extern struct workqueue_struct *system_freezable_power_efficient_wq;
  337. /**
  338. * alloc_workqueue - allocate a workqueue
  339. * @fmt: printf format for the name of the workqueue
  340. * @flags: WQ_* flags
  341. * @max_active: max in-flight work items, 0 for default
  342. * remaining args: args for @fmt
  343. *
  344. * Allocate a workqueue with the specified parameters. For detailed
  345. * information on WQ_* flags, please refer to
  346. * Documentation/core-api/workqueue.rst.
  347. *
  348. * RETURNS:
  349. * Pointer to the allocated workqueue on success, %NULL on failure.
  350. */
  351. __printf(1, 4) struct workqueue_struct *
  352. alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...);
  353. /**
  354. * alloc_ordered_workqueue - allocate an ordered workqueue
  355. * @fmt: printf format for the name of the workqueue
  356. * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  357. * @args: args for @fmt
  358. *
  359. * Allocate an ordered workqueue. An ordered workqueue executes at
  360. * most one work item at any given time in the queued order. They are
  361. * implemented as unbound workqueues with @max_active of one.
  362. *
  363. * RETURNS:
  364. * Pointer to the allocated workqueue on success, %NULL on failure.
  365. */
  366. #define alloc_ordered_workqueue(fmt, flags, args...) \
  367. alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | \
  368. __WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
  369. #define create_workqueue(name) \
  370. alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
  371. #define create_freezable_workqueue(name) \
  372. alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND | \
  373. WQ_MEM_RECLAIM, 1, (name))
  374. #define create_singlethread_workqueue(name) \
  375. alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
  376. extern void destroy_workqueue(struct workqueue_struct *wq);
  377. struct workqueue_attrs *alloc_workqueue_attrs(void);
  378. void free_workqueue_attrs(struct workqueue_attrs *attrs);
  379. int apply_workqueue_attrs(struct workqueue_struct *wq,
  380. const struct workqueue_attrs *attrs);
  381. int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
  382. extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
  383. struct work_struct *work);
  384. extern bool queue_work_node(int node, struct workqueue_struct *wq,
  385. struct work_struct *work);
  386. extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  387. struct delayed_work *work, unsigned long delay);
  388. extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  389. struct delayed_work *dwork, unsigned long delay);
  390. extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
  391. extern void __flush_workqueue(struct workqueue_struct *wq);
  392. extern void drain_workqueue(struct workqueue_struct *wq);
  393. extern int schedule_on_each_cpu(work_func_t func);
  394. int execute_in_process_context(work_func_t fn, struct execute_work *);
  395. extern bool flush_work(struct work_struct *work);
  396. extern bool cancel_work(struct work_struct *work);
  397. extern bool cancel_work_sync(struct work_struct *work);
  398. extern bool flush_delayed_work(struct delayed_work *dwork);
  399. extern bool cancel_delayed_work(struct delayed_work *dwork);
  400. extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
  401. extern bool flush_rcu_work(struct rcu_work *rwork);
  402. extern void workqueue_set_max_active(struct workqueue_struct *wq,
  403. int max_active);
  404. extern struct work_struct *current_work(void);
  405. extern bool current_is_workqueue_rescuer(void);
  406. extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
  407. extern unsigned int work_busy(struct work_struct *work);
  408. extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
  409. extern void print_worker_info(const char *log_lvl, struct task_struct *task);
  410. extern void show_all_workqueues(void);
  411. extern void show_one_workqueue(struct workqueue_struct *wq);
  412. extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task);
  413. /**
  414. * queue_work - queue work on a workqueue
  415. * @wq: workqueue to use
  416. * @work: work to queue
  417. *
  418. * Returns %false if @work was already on a queue, %true otherwise.
  419. *
  420. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  421. * it can be processed by another CPU.
  422. *
  423. * Memory-ordering properties: If it returns %true, guarantees that all stores
  424. * preceding the call to queue_work() in the program order will be visible from
  425. * the CPU which will execute @work by the time such work executes, e.g.,
  426. *
  427. * { x is initially 0 }
  428. *
  429. * CPU0 CPU1
  430. *
  431. * WRITE_ONCE(x, 1); [ @work is being executed ]
  432. * r0 = queue_work(wq, work); r1 = READ_ONCE(x);
  433. *
  434. * Forbids: r0 == true && r1 == 0
  435. */
  436. static inline bool queue_work(struct workqueue_struct *wq,
  437. struct work_struct *work)
  438. {
  439. return queue_work_on(WORK_CPU_UNBOUND, wq, work);
  440. }
  441. /**
  442. * queue_delayed_work - queue work on a workqueue after delay
  443. * @wq: workqueue to use
  444. * @dwork: delayable work to queue
  445. * @delay: number of jiffies to wait before queueing
  446. *
  447. * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
  448. */
  449. static inline bool queue_delayed_work(struct workqueue_struct *wq,
  450. struct delayed_work *dwork,
  451. unsigned long delay)
  452. {
  453. return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
  454. }
  455. /**
  456. * mod_delayed_work - modify delay of or queue a delayed work
  457. * @wq: workqueue to use
  458. * @dwork: work to queue
  459. * @delay: number of jiffies to wait before queueing
  460. *
  461. * mod_delayed_work_on() on local CPU.
  462. */
  463. static inline bool mod_delayed_work(struct workqueue_struct *wq,
  464. struct delayed_work *dwork,
  465. unsigned long delay)
  466. {
  467. return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
  468. }
  469. /**
  470. * schedule_work_on - put work task on a specific cpu
  471. * @cpu: cpu to put the work task on
  472. * @work: job to be done
  473. *
  474. * This puts a job on a specific cpu
  475. */
  476. static inline bool schedule_work_on(int cpu, struct work_struct *work)
  477. {
  478. return queue_work_on(cpu, system_wq, work);
  479. }
  480. /**
  481. * schedule_work - put work task in global workqueue
  482. * @work: job to be done
  483. *
  484. * Returns %false if @work was already on the kernel-global workqueue and
  485. * %true otherwise.
  486. *
  487. * This puts a job in the kernel-global workqueue if it was not already
  488. * queued and leaves it in the same position on the kernel-global
  489. * workqueue otherwise.
  490. *
  491. * Shares the same memory-ordering properties of queue_work(), cf. the
  492. * DocBook header of queue_work().
  493. */
  494. static inline bool schedule_work(struct work_struct *work)
  495. {
  496. return queue_work(system_wq, work);
  497. }
  498. /*
  499. * Detect attempt to flush system-wide workqueues at compile time when possible.
  500. *
  501. * See https://lkml.kernel.org/r/[email protected]
  502. * for reasons and steps for converting system-wide workqueues into local workqueues.
  503. */
  504. extern void __warn_flushing_systemwide_wq(void)
  505. __compiletime_warning("Please avoid flushing system-wide workqueues.");
  506. /**
  507. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  508. *
  509. * Forces execution of the kernel-global workqueue and blocks until its
  510. * completion.
  511. *
  512. * It's very easy to get into trouble if you don't take great care.
  513. * Either of the following situations will lead to deadlock:
  514. *
  515. * One of the work items currently on the workqueue needs to acquire
  516. * a lock held by your code or its caller.
  517. *
  518. * Your code is running in the context of a work routine.
  519. *
  520. * They will be detected by lockdep when they occur, but the first might not
  521. * occur very often. It depends on what work items are on the workqueue and
  522. * what locks they need, which you have no control over.
  523. *
  524. * In most situations flushing the entire workqueue is overkill; you merely
  525. * need to know that a particular work item isn't queued and isn't running.
  526. * In such cases you should use cancel_delayed_work_sync() or
  527. * cancel_work_sync() instead.
  528. *
  529. * Please stop calling this function! A conversion to stop flushing system-wide
  530. * workqueues is in progress. This function will be removed after all in-tree
  531. * users stopped calling this function.
  532. */
  533. /*
  534. * The background of commit 771c035372a036f8 ("deprecate the
  535. * '__deprecated' attribute warnings entirely and for good") is that,
  536. * since Linus builds all modules between every single pull he does,
  537. * the standard kernel build needs to be _clean_ in order to be able to
  538. * notice when new problems happen. Therefore, don't emit warning while
  539. * there are in-tree users.
  540. */
  541. #define flush_scheduled_work() \
  542. ({ \
  543. if (0) \
  544. __warn_flushing_systemwide_wq(); \
  545. __flush_workqueue(system_wq); \
  546. })
  547. /*
  548. * Although there is no longer in-tree caller, for now just emit warning
  549. * in order to give out-of-tree callers time to update.
  550. */
  551. #define flush_workqueue(wq) \
  552. ({ \
  553. struct workqueue_struct *_wq = (wq); \
  554. \
  555. if ((__builtin_constant_p(_wq == system_wq) && \
  556. _wq == system_wq) || \
  557. (__builtin_constant_p(_wq == system_highpri_wq) && \
  558. _wq == system_highpri_wq) || \
  559. (__builtin_constant_p(_wq == system_long_wq) && \
  560. _wq == system_long_wq) || \
  561. (__builtin_constant_p(_wq == system_unbound_wq) && \
  562. _wq == system_unbound_wq) || \
  563. (__builtin_constant_p(_wq == system_freezable_wq) && \
  564. _wq == system_freezable_wq) || \
  565. (__builtin_constant_p(_wq == system_power_efficient_wq) && \
  566. _wq == system_power_efficient_wq) || \
  567. (__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \
  568. _wq == system_freezable_power_efficient_wq)) \
  569. __warn_flushing_systemwide_wq(); \
  570. __flush_workqueue(_wq); \
  571. })
  572. /**
  573. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  574. * @cpu: cpu to use
  575. * @dwork: job to be done
  576. * @delay: number of jiffies to wait
  577. *
  578. * After waiting for a given time this puts a job in the kernel-global
  579. * workqueue on the specified CPU.
  580. */
  581. static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
  582. unsigned long delay)
  583. {
  584. return queue_delayed_work_on(cpu, system_wq, dwork, delay);
  585. }
  586. /**
  587. * schedule_delayed_work - put work task in global workqueue after delay
  588. * @dwork: job to be done
  589. * @delay: number of jiffies to wait or 0 for immediate execution
  590. *
  591. * After waiting for a given time this puts a job in the kernel-global
  592. * workqueue.
  593. */
  594. static inline bool schedule_delayed_work(struct delayed_work *dwork,
  595. unsigned long delay)
  596. {
  597. return queue_delayed_work(system_wq, dwork, delay);
  598. }
  599. #ifndef CONFIG_SMP
  600. static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
  601. {
  602. return fn(arg);
  603. }
  604. static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
  605. {
  606. return fn(arg);
  607. }
  608. #else
  609. long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
  610. long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
  611. #endif /* CONFIG_SMP */
  612. #ifdef CONFIG_FREEZER
  613. extern void freeze_workqueues_begin(void);
  614. extern bool freeze_workqueues_busy(void);
  615. extern void thaw_workqueues(void);
  616. #endif /* CONFIG_FREEZER */
  617. #ifdef CONFIG_SYSFS
  618. int workqueue_sysfs_register(struct workqueue_struct *wq);
  619. #else /* CONFIG_SYSFS */
  620. static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
  621. { return 0; }
  622. #endif /* CONFIG_SYSFS */
  623. #ifdef CONFIG_WQ_WATCHDOG
  624. void wq_watchdog_touch(int cpu);
  625. #else /* CONFIG_WQ_WATCHDOG */
  626. static inline void wq_watchdog_touch(int cpu) { }
  627. #endif /* CONFIG_WQ_WATCHDOG */
  628. #ifdef CONFIG_SMP
  629. int workqueue_prepare_cpu(unsigned int cpu);
  630. int workqueue_online_cpu(unsigned int cpu);
  631. int workqueue_offline_cpu(unsigned int cpu);
  632. #endif
  633. void __init workqueue_init_early(void);
  634. void __init workqueue_init(void);
  635. #endif