workqueue.h 21 KB

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