workqueue.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. struct workqueue_struct;
  16. struct work_struct;
  17. typedef void (*work_func_t)(struct work_struct *work);
  18. void delayed_work_timer_fn(struct timer_list *t);
  19. /*
  20. * The first word is the work queue pointer and the flags rolled into
  21. * one
  22. */
  23. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  24. enum {
  25. WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
  26. WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */
  27. WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */
  28. WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */
  29. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  30. WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */
  31. WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */
  32. #else
  33. WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */
  34. #endif
  35. WORK_STRUCT_COLOR_BITS = 4,
  36. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  37. WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT,
  38. WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT,
  39. WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
  40. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  41. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  42. #else
  43. WORK_STRUCT_STATIC = 0,
  44. #endif
  45. /*
  46. * The last color is no color used for works which don't
  47. * participate in workqueue flushing.
  48. */
  49. WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1,
  50. WORK_NO_COLOR = WORK_NR_COLORS,
  51. /* not bound to any CPU, prefer the local CPU */
  52. WORK_CPU_UNBOUND = NR_CPUS,
  53. /*
  54. * Reserve 7 bits off of pwq pointer w/ debugobjects turned off.
  55. * This makes pwqs aligned to 256 bytes and allows 15 workqueue
  56. * flush colors.
  57. */
  58. WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
  59. WORK_STRUCT_COLOR_BITS,
  60. /* data contains off-queue information when !WORK_STRUCT_PWQ */
  61. WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT,
  62. __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE,
  63. WORK_OFFQ_CANCELING = (1 << __WORK_OFFQ_CANCELING),
  64. /*
  65. * When a work item is off queue, its high bits point to the last
  66. * pool it was on. Cap at 31 bits and use the highest number to
  67. * indicate that no pool is associated.
  68. */
  69. WORK_OFFQ_FLAG_BITS = 1,
  70. WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
  71. WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
  72. WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
  73. WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1,
  74. /* convenience constants */
  75. WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
  76. WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
  77. WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT,
  78. /* bit mask for work_busy() return values */
  79. WORK_BUSY_PENDING = 1 << 0,
  80. WORK_BUSY_RUNNING = 1 << 1,
  81. /* maximum string length for set_worker_desc() */
  82. WORKER_DESC_LEN = 24,
  83. };
  84. struct work_struct {
  85. atomic_long_t data;
  86. struct list_head entry;
  87. work_func_t func;
  88. #ifdef CONFIG_LOCKDEP
  89. struct lockdep_map lockdep_map;
  90. #endif
  91. };
  92. #define WORK_DATA_INIT() ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL)
  93. #define WORK_DATA_STATIC_INIT() \
  94. ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC))
  95. struct delayed_work {
  96. struct work_struct work;
  97. struct timer_list timer;
  98. /* target workqueue and CPU ->timer uses to queue ->work */
  99. struct workqueue_struct *wq;
  100. int cpu;
  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 wq_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. * excute 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. extern struct workqueue_struct *
  338. __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active,
  339. struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6);
  340. /**
  341. * alloc_workqueue - allocate a workqueue
  342. * @fmt: printf format for the name of the workqueue
  343. * @flags: WQ_* flags
  344. * @max_active: max in-flight work items, 0 for default
  345. * @args...: args for @fmt
  346. *
  347. * Allocate a workqueue with the specified parameters. For detailed
  348. * information on WQ_* flags, please refer to
  349. * Documentation/core-api/workqueue.rst.
  350. *
  351. * The __lock_name macro dance is to guarantee that single lock_class_key
  352. * doesn't end up with different namesm, which isn't allowed by lockdep.
  353. *
  354. * RETURNS:
  355. * Pointer to the allocated workqueue on success, %NULL on failure.
  356. */
  357. #ifdef CONFIG_LOCKDEP
  358. #define alloc_workqueue(fmt, flags, max_active, args...) \
  359. ({ \
  360. static struct lock_class_key __key; \
  361. const char *__lock_name; \
  362. \
  363. __lock_name = "(wq_completion)"#fmt#args; \
  364. \
  365. __alloc_workqueue_key((fmt), (flags), (max_active), \
  366. &__key, __lock_name, ##args); \
  367. })
  368. #else
  369. #define alloc_workqueue(fmt, flags, max_active, args...) \
  370. __alloc_workqueue_key((fmt), (flags), (max_active), \
  371. NULL, NULL, ##args)
  372. #endif
  373. /**
  374. * alloc_ordered_workqueue - allocate an ordered workqueue
  375. * @fmt: printf format for the name of the workqueue
  376. * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  377. * @args...: args for @fmt
  378. *
  379. * Allocate an ordered workqueue. An ordered workqueue executes at
  380. * most one work item at any given time in the queued order. They are
  381. * implemented as unbound workqueues with @max_active of one.
  382. *
  383. * RETURNS:
  384. * Pointer to the allocated workqueue on success, %NULL on failure.
  385. */
  386. #define alloc_ordered_workqueue(fmt, flags, args...) \
  387. alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | \
  388. __WQ_ORDERED_EXPLICIT | (flags), 1, ##args)
  389. #define create_workqueue(name) \
  390. alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
  391. #define create_freezable_workqueue(name) \
  392. alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND | \
  393. WQ_MEM_RECLAIM, 1, (name))
  394. #define create_singlethread_workqueue(name) \
  395. alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name)
  396. extern void destroy_workqueue(struct workqueue_struct *wq);
  397. struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask);
  398. void free_workqueue_attrs(struct workqueue_attrs *attrs);
  399. int apply_workqueue_attrs(struct workqueue_struct *wq,
  400. const struct workqueue_attrs *attrs);
  401. int workqueue_set_unbound_cpumask(cpumask_var_t cpumask);
  402. extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
  403. struct work_struct *work);
  404. extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  405. struct delayed_work *work, unsigned long delay);
  406. extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  407. struct delayed_work *dwork, unsigned long delay);
  408. extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
  409. extern void flush_workqueue(struct workqueue_struct *wq);
  410. extern void drain_workqueue(struct workqueue_struct *wq);
  411. extern int schedule_on_each_cpu(work_func_t func);
  412. int execute_in_process_context(work_func_t fn, struct execute_work *);
  413. extern bool flush_work(struct work_struct *work);
  414. extern bool cancel_work_sync(struct work_struct *work);
  415. extern bool flush_delayed_work(struct delayed_work *dwork);
  416. extern bool cancel_delayed_work(struct delayed_work *dwork);
  417. extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
  418. extern bool flush_rcu_work(struct rcu_work *rwork);
  419. extern void workqueue_set_max_active(struct workqueue_struct *wq,
  420. int max_active);
  421. extern struct work_struct *current_work(void);
  422. extern bool current_is_workqueue_rescuer(void);
  423. extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
  424. extern unsigned int work_busy(struct work_struct *work);
  425. extern __printf(1, 2) void set_worker_desc(const char *fmt, ...);
  426. extern void print_worker_info(const char *log_lvl, struct task_struct *task);
  427. extern void show_workqueue_state(void);
  428. /**
  429. * queue_work - queue work on a workqueue
  430. * @wq: workqueue to use
  431. * @work: work to queue
  432. *
  433. * Returns %false if @work was already on a queue, %true otherwise.
  434. *
  435. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  436. * it can be processed by another CPU.
  437. */
  438. static inline bool queue_work(struct workqueue_struct *wq,
  439. struct work_struct *work)
  440. {
  441. return queue_work_on(WORK_CPU_UNBOUND, wq, work);
  442. }
  443. /**
  444. * queue_delayed_work - queue work on a workqueue after delay
  445. * @wq: workqueue to use
  446. * @dwork: delayable work to queue
  447. * @delay: number of jiffies to wait before queueing
  448. *
  449. * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
  450. */
  451. static inline bool queue_delayed_work(struct workqueue_struct *wq,
  452. struct delayed_work *dwork,
  453. unsigned long delay)
  454. {
  455. return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
  456. }
  457. /**
  458. * mod_delayed_work - modify delay of or queue a delayed work
  459. * @wq: workqueue to use
  460. * @dwork: work to queue
  461. * @delay: number of jiffies to wait before queueing
  462. *
  463. * mod_delayed_work_on() on local CPU.
  464. */
  465. static inline bool mod_delayed_work(struct workqueue_struct *wq,
  466. struct delayed_work *dwork,
  467. unsigned long delay)
  468. {
  469. return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
  470. }
  471. /**
  472. * schedule_work_on - put work task on a specific cpu
  473. * @cpu: cpu to put the work task on
  474. * @work: job to be done
  475. *
  476. * This puts a job on a specific cpu
  477. */
  478. static inline bool schedule_work_on(int cpu, struct work_struct *work)
  479. {
  480. return queue_work_on(cpu, system_wq, work);
  481. }
  482. /**
  483. * schedule_work - put work task in global workqueue
  484. * @work: job to be done
  485. *
  486. * Returns %false if @work was already on the kernel-global workqueue and
  487. * %true otherwise.
  488. *
  489. * This puts a job in the kernel-global workqueue if it was not already
  490. * queued and leaves it in the same position on the kernel-global
  491. * workqueue otherwise.
  492. */
  493. static inline bool schedule_work(struct work_struct *work)
  494. {
  495. return queue_work(system_wq, work);
  496. }
  497. /**
  498. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  499. *
  500. * Forces execution of the kernel-global workqueue and blocks until its
  501. * completion.
  502. *
  503. * Think twice before calling this function! It's very easy to get into
  504. * trouble if you don't take great care. Either of the following situations
  505. * will lead to deadlock:
  506. *
  507. * One of the work items currently on the workqueue needs to acquire
  508. * a lock held by your code or its caller.
  509. *
  510. * Your code is running in the context of a work routine.
  511. *
  512. * They will be detected by lockdep when they occur, but the first might not
  513. * occur very often. It depends on what work items are on the workqueue and
  514. * what locks they need, which you have no control over.
  515. *
  516. * In most situations flushing the entire workqueue is overkill; you merely
  517. * need to know that a particular work item isn't queued and isn't running.
  518. * In such cases you should use cancel_delayed_work_sync() or
  519. * cancel_work_sync() instead.
  520. */
  521. static inline void flush_scheduled_work(void)
  522. {
  523. flush_workqueue(system_wq);
  524. }
  525. /**
  526. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  527. * @cpu: cpu to use
  528. * @dwork: job to be done
  529. * @delay: number of jiffies to wait
  530. *
  531. * After waiting for a given time this puts a job in the kernel-global
  532. * workqueue on the specified CPU.
  533. */
  534. static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
  535. unsigned long delay)
  536. {
  537. return queue_delayed_work_on(cpu, system_wq, dwork, delay);
  538. }
  539. /**
  540. * schedule_delayed_work - put work task in global workqueue after delay
  541. * @dwork: job to be done
  542. * @delay: number of jiffies to wait or 0 for immediate execution
  543. *
  544. * After waiting for a given time this puts a job in the kernel-global
  545. * workqueue.
  546. */
  547. static inline bool schedule_delayed_work(struct delayed_work *dwork,
  548. unsigned long delay)
  549. {
  550. return queue_delayed_work(system_wq, dwork, delay);
  551. }
  552. #ifndef CONFIG_SMP
  553. static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
  554. {
  555. return fn(arg);
  556. }
  557. static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg)
  558. {
  559. return fn(arg);
  560. }
  561. #else
  562. long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
  563. long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg);
  564. #endif /* CONFIG_SMP */
  565. #ifdef CONFIG_FREEZER
  566. extern void freeze_workqueues_begin(void);
  567. extern bool freeze_workqueues_busy(void);
  568. extern void thaw_workqueues(void);
  569. #endif /* CONFIG_FREEZER */
  570. #ifdef CONFIG_SYSFS
  571. int workqueue_sysfs_register(struct workqueue_struct *wq);
  572. #else /* CONFIG_SYSFS */
  573. static inline int workqueue_sysfs_register(struct workqueue_struct *wq)
  574. { return 0; }
  575. #endif /* CONFIG_SYSFS */
  576. #ifdef CONFIG_WQ_WATCHDOG
  577. void wq_watchdog_touch(int cpu);
  578. #else /* CONFIG_WQ_WATCHDOG */
  579. static inline void wq_watchdog_touch(int cpu) { }
  580. #endif /* CONFIG_WQ_WATCHDOG */
  581. #ifdef CONFIG_SMP
  582. int workqueue_prepare_cpu(unsigned int cpu);
  583. int workqueue_online_cpu(unsigned int cpu);
  584. int workqueue_offline_cpu(unsigned int cpu);
  585. #endif
  586. int __init workqueue_init_early(void);
  587. int __init workqueue_init(void);
  588. #endif