workqueue.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * workqueue.h --- work queue handling for Linux.
  3. */
  4. #ifndef _LINUX_WORKQUEUE_H
  5. #define _LINUX_WORKQUEUE_H
  6. #include <linux/timer.h>
  7. #include <linux/linkage.h>
  8. #include <linux/bitops.h>
  9. #include <linux/lockdep.h>
  10. #include <linux/threads.h>
  11. #include <linux/atomic.h>
  12. struct workqueue_struct;
  13. struct work_struct;
  14. typedef void (*work_func_t)(struct work_struct *work);
  15. void delayed_work_timer_fn(unsigned long __data);
  16. /*
  17. * The first word is the work queue pointer and the flags rolled into
  18. * one
  19. */
  20. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  21. enum {
  22. WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
  23. WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */
  24. WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */
  25. WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */
  26. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  27. WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */
  28. WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */
  29. #else
  30. WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */
  31. #endif
  32. WORK_STRUCT_COLOR_BITS = 4,
  33. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  34. WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT,
  35. WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT,
  36. WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
  37. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  38. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  39. #else
  40. WORK_STRUCT_STATIC = 0,
  41. #endif
  42. /*
  43. * The last color is no color used for works which don't
  44. * participate in workqueue flushing.
  45. */
  46. WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1,
  47. WORK_NO_COLOR = WORK_NR_COLORS,
  48. /* special cpu IDs */
  49. WORK_CPU_UNBOUND = NR_CPUS,
  50. WORK_CPU_END = NR_CPUS + 1,
  51. /*
  52. * Reserve 7 bits off of pwq pointer w/ debugobjects turned off.
  53. * This makes pwqs aligned to 256 bytes and allows 15 workqueue
  54. * flush colors.
  55. */
  56. WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
  57. WORK_STRUCT_COLOR_BITS,
  58. /* data contains off-queue information when !WORK_STRUCT_PWQ */
  59. WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT,
  60. WORK_OFFQ_CANCELING = (1 << WORK_OFFQ_FLAG_BASE),
  61. /*
  62. * When a work item is off queue, its high bits point to the last
  63. * pool it was on. Cap at 31 bits and use the highest number to
  64. * indicate that no pool is associated.
  65. */
  66. WORK_OFFQ_FLAG_BITS = 1,
  67. WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS,
  68. WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT,
  69. WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31,
  70. WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1,
  71. /* convenience constants */
  72. WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
  73. WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
  74. WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT,
  75. /* bit mask for work_busy() return values */
  76. WORK_BUSY_PENDING = 1 << 0,
  77. WORK_BUSY_RUNNING = 1 << 1,
  78. };
  79. struct work_struct {
  80. atomic_long_t data;
  81. struct list_head entry;
  82. work_func_t func;
  83. #ifdef CONFIG_LOCKDEP
  84. struct lockdep_map lockdep_map;
  85. #endif
  86. };
  87. #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL)
  88. #define WORK_DATA_STATIC_INIT() \
  89. ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC)
  90. struct delayed_work {
  91. struct work_struct work;
  92. struct timer_list timer;
  93. /* target workqueue and CPU ->timer uses to queue ->work */
  94. struct workqueue_struct *wq;
  95. int cpu;
  96. };
  97. static inline struct delayed_work *to_delayed_work(struct work_struct *work)
  98. {
  99. return container_of(work, struct delayed_work, work);
  100. }
  101. struct execute_work {
  102. struct work_struct work;
  103. };
  104. #ifdef CONFIG_LOCKDEP
  105. /*
  106. * NB: because we have to copy the lockdep_map, setting _key
  107. * here is required, otherwise it could get initialised to the
  108. * copy of the lockdep_map!
  109. */
  110. #define __WORK_INIT_LOCKDEP_MAP(n, k) \
  111. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
  112. #else
  113. #define __WORK_INIT_LOCKDEP_MAP(n, k)
  114. #endif
  115. #define __WORK_INITIALIZER(n, f) { \
  116. .data = WORK_DATA_STATIC_INIT(), \
  117. .entry = { &(n).entry, &(n).entry }, \
  118. .func = (f), \
  119. __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
  120. }
  121. #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \
  122. .work = __WORK_INITIALIZER((n).work, (f)), \
  123. .timer = __TIMER_INITIALIZER(delayed_work_timer_fn, \
  124. 0, (unsigned long)&(n), \
  125. (tflags) | TIMER_IRQSAFE), \
  126. }
  127. #define DECLARE_WORK(n, f) \
  128. struct work_struct n = __WORK_INITIALIZER(n, f)
  129. #define DECLARE_DELAYED_WORK(n, f) \
  130. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0)
  131. #define DECLARE_DEFERRABLE_WORK(n, f) \
  132. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE)
  133. /*
  134. * initialize a work item's function pointer
  135. */
  136. #define PREPARE_WORK(_work, _func) \
  137. do { \
  138. (_work)->func = (_func); \
  139. } while (0)
  140. #define PREPARE_DELAYED_WORK(_work, _func) \
  141. PREPARE_WORK(&(_work)->work, (_func))
  142. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  143. extern void __init_work(struct work_struct *work, int onstack);
  144. extern void destroy_work_on_stack(struct work_struct *work);
  145. static inline unsigned int work_static(struct work_struct *work)
  146. {
  147. return *work_data_bits(work) & WORK_STRUCT_STATIC;
  148. }
  149. #else
  150. static inline void __init_work(struct work_struct *work, int onstack) { }
  151. static inline void destroy_work_on_stack(struct work_struct *work) { }
  152. static inline unsigned int work_static(struct work_struct *work) { return 0; }
  153. #endif
  154. /*
  155. * initialize all of a work item in one go
  156. *
  157. * NOTE! No point in using "atomic_long_set()": using a direct
  158. * assignment of the work data initializer allows the compiler
  159. * to generate better code.
  160. */
  161. #ifdef CONFIG_LOCKDEP
  162. #define __INIT_WORK(_work, _func, _onstack) \
  163. do { \
  164. static struct lock_class_key __key; \
  165. \
  166. __init_work((_work), _onstack); \
  167. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  168. lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0); \
  169. INIT_LIST_HEAD(&(_work)->entry); \
  170. PREPARE_WORK((_work), (_func)); \
  171. } while (0)
  172. #else
  173. #define __INIT_WORK(_work, _func, _onstack) \
  174. do { \
  175. __init_work((_work), _onstack); \
  176. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  177. INIT_LIST_HEAD(&(_work)->entry); \
  178. PREPARE_WORK((_work), (_func)); \
  179. } while (0)
  180. #endif
  181. #define INIT_WORK(_work, _func) \
  182. do { \
  183. __INIT_WORK((_work), (_func), 0); \
  184. } while (0)
  185. #define INIT_WORK_ONSTACK(_work, _func) \
  186. do { \
  187. __INIT_WORK((_work), (_func), 1); \
  188. } while (0)
  189. #define __INIT_DELAYED_WORK(_work, _func, _tflags) \
  190. do { \
  191. INIT_WORK(&(_work)->work, (_func)); \
  192. __setup_timer(&(_work)->timer, delayed_work_timer_fn, \
  193. (unsigned long)(_work), \
  194. (_tflags) | TIMER_IRQSAFE); \
  195. } while (0)
  196. #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \
  197. do { \
  198. INIT_WORK_ONSTACK(&(_work)->work, (_func)); \
  199. __setup_timer_on_stack(&(_work)->timer, \
  200. delayed_work_timer_fn, \
  201. (unsigned long)(_work), \
  202. (_tflags) | TIMER_IRQSAFE); \
  203. } while (0)
  204. #define INIT_DELAYED_WORK(_work, _func) \
  205. __INIT_DELAYED_WORK(_work, _func, 0)
  206. #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \
  207. __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0)
  208. #define INIT_DEFERRABLE_WORK(_work, _func) \
  209. __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE)
  210. #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \
  211. __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE)
  212. /**
  213. * work_pending - Find out whether a work item is currently pending
  214. * @work: The work item in question
  215. */
  216. #define work_pending(work) \
  217. test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  218. /**
  219. * delayed_work_pending - Find out whether a delayable work item is currently
  220. * pending
  221. * @work: The work item in question
  222. */
  223. #define delayed_work_pending(w) \
  224. work_pending(&(w)->work)
  225. /**
  226. * work_clear_pending - for internal use only, mark a work item as not pending
  227. * @work: The work item in question
  228. */
  229. #define work_clear_pending(work) \
  230. clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  231. /*
  232. * Workqueue flags and constants. For details, please refer to
  233. * Documentation/workqueue.txt.
  234. */
  235. enum {
  236. WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */
  237. WQ_UNBOUND = 1 << 1, /* not bound to any cpu */
  238. WQ_FREEZABLE = 1 << 2, /* freeze during suspend */
  239. WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */
  240. WQ_HIGHPRI = 1 << 4, /* high priority */
  241. WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */
  242. WQ_DRAINING = 1 << 6, /* internal: workqueue is draining */
  243. WQ_RESCUER = 1 << 7, /* internal: workqueue has rescuer */
  244. WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */
  245. WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */
  246. WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2,
  247. };
  248. /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
  249. #define WQ_UNBOUND_MAX_ACTIVE \
  250. max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
  251. /*
  252. * System-wide workqueues which are always present.
  253. *
  254. * system_wq is the one used by schedule[_delayed]_work[_on]().
  255. * Multi-CPU multi-threaded. There are users which expect relatively
  256. * short queue flush time. Don't queue works which can run for too
  257. * long.
  258. *
  259. * system_long_wq is similar to system_wq but may host long running
  260. * works. Queue flushing might take relatively long.
  261. *
  262. * system_unbound_wq is unbound workqueue. Workers are not bound to
  263. * any specific CPU, not concurrency managed, and all queued works are
  264. * executed immediately as long as max_active limit is not reached and
  265. * resources are available.
  266. *
  267. * system_freezable_wq is equivalent to system_wq except that it's
  268. * freezable.
  269. */
  270. extern struct workqueue_struct *system_wq;
  271. extern struct workqueue_struct *system_long_wq;
  272. extern struct workqueue_struct *system_unbound_wq;
  273. extern struct workqueue_struct *system_freezable_wq;
  274. static inline struct workqueue_struct * __deprecated __system_nrt_wq(void)
  275. {
  276. return system_wq;
  277. }
  278. static inline struct workqueue_struct * __deprecated __system_nrt_freezable_wq(void)
  279. {
  280. return system_freezable_wq;
  281. }
  282. /* equivlalent to system_wq and system_freezable_wq, deprecated */
  283. #define system_nrt_wq __system_nrt_wq()
  284. #define system_nrt_freezable_wq __system_nrt_freezable_wq()
  285. extern struct workqueue_struct *
  286. __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active,
  287. struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6);
  288. /**
  289. * alloc_workqueue - allocate a workqueue
  290. * @fmt: printf format for the name of the workqueue
  291. * @flags: WQ_* flags
  292. * @max_active: max in-flight work items, 0 for default
  293. * @args: args for @fmt
  294. *
  295. * Allocate a workqueue with the specified parameters. For detailed
  296. * information on WQ_* flags, please refer to Documentation/workqueue.txt.
  297. *
  298. * The __lock_name macro dance is to guarantee that single lock_class_key
  299. * doesn't end up with different namesm, which isn't allowed by lockdep.
  300. *
  301. * RETURNS:
  302. * Pointer to the allocated workqueue on success, %NULL on failure.
  303. */
  304. #ifdef CONFIG_LOCKDEP
  305. #define alloc_workqueue(fmt, flags, max_active, args...) \
  306. ({ \
  307. static struct lock_class_key __key; \
  308. const char *__lock_name; \
  309. \
  310. if (__builtin_constant_p(fmt)) \
  311. __lock_name = (fmt); \
  312. else \
  313. __lock_name = #fmt; \
  314. \
  315. __alloc_workqueue_key((fmt), (flags), (max_active), \
  316. &__key, __lock_name, ##args); \
  317. })
  318. #else
  319. #define alloc_workqueue(fmt, flags, max_active, args...) \
  320. __alloc_workqueue_key((fmt), (flags), (max_active), \
  321. NULL, NULL, ##args)
  322. #endif
  323. /**
  324. * alloc_ordered_workqueue - allocate an ordered workqueue
  325. * @fmt: printf format for the name of the workqueue
  326. * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  327. * @args: args for @fmt
  328. *
  329. * Allocate an ordered workqueue. An ordered workqueue executes at
  330. * most one work item at any given time in the queued order. They are
  331. * implemented as unbound workqueues with @max_active of one.
  332. *
  333. * RETURNS:
  334. * Pointer to the allocated workqueue on success, %NULL on failure.
  335. */
  336. #define alloc_ordered_workqueue(fmt, flags, args...) \
  337. alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args)
  338. #define create_workqueue(name) \
  339. alloc_workqueue((name), WQ_MEM_RECLAIM, 1)
  340. #define create_freezable_workqueue(name) \
  341. alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
  342. #define create_singlethread_workqueue(name) \
  343. alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
  344. extern void destroy_workqueue(struct workqueue_struct *wq);
  345. extern bool queue_work_on(int cpu, struct workqueue_struct *wq,
  346. struct work_struct *work);
  347. extern bool queue_work(struct workqueue_struct *wq, struct work_struct *work);
  348. extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  349. struct delayed_work *work, unsigned long delay);
  350. extern bool queue_delayed_work(struct workqueue_struct *wq,
  351. struct delayed_work *work, unsigned long delay);
  352. extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  353. struct delayed_work *dwork, unsigned long delay);
  354. extern bool mod_delayed_work(struct workqueue_struct *wq,
  355. struct delayed_work *dwork, unsigned long delay);
  356. extern void flush_workqueue(struct workqueue_struct *wq);
  357. extern void drain_workqueue(struct workqueue_struct *wq);
  358. extern void flush_scheduled_work(void);
  359. extern bool schedule_work_on(int cpu, struct work_struct *work);
  360. extern bool schedule_work(struct work_struct *work);
  361. extern bool schedule_delayed_work_on(int cpu, struct delayed_work *work,
  362. unsigned long delay);
  363. extern bool schedule_delayed_work(struct delayed_work *work,
  364. unsigned long delay);
  365. extern int schedule_on_each_cpu(work_func_t func);
  366. extern int keventd_up(void);
  367. int execute_in_process_context(work_func_t fn, struct execute_work *);
  368. extern bool flush_work(struct work_struct *work);
  369. extern bool cancel_work_sync(struct work_struct *work);
  370. extern bool flush_delayed_work(struct delayed_work *dwork);
  371. extern bool cancel_delayed_work(struct delayed_work *dwork);
  372. extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
  373. extern void workqueue_set_max_active(struct workqueue_struct *wq,
  374. int max_active);
  375. extern bool workqueue_congested(int cpu, struct workqueue_struct *wq);
  376. extern unsigned int work_busy(struct work_struct *work);
  377. /*
  378. * Like above, but uses del_timer() instead of del_timer_sync(). This means,
  379. * if it returns 0 the timer function may be running and the queueing is in
  380. * progress.
  381. */
  382. static inline bool __deprecated __cancel_delayed_work(struct delayed_work *work)
  383. {
  384. bool ret;
  385. ret = del_timer(&work->timer);
  386. if (ret)
  387. work_clear_pending(&work->work);
  388. return ret;
  389. }
  390. /* used to be different but now identical to flush_work(), deprecated */
  391. static inline bool __deprecated flush_work_sync(struct work_struct *work)
  392. {
  393. return flush_work(work);
  394. }
  395. /* used to be different but now identical to flush_delayed_work(), deprecated */
  396. static inline bool __deprecated flush_delayed_work_sync(struct delayed_work *dwork)
  397. {
  398. return flush_delayed_work(dwork);
  399. }
  400. #ifndef CONFIG_SMP
  401. static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
  402. {
  403. return fn(arg);
  404. }
  405. #else
  406. long work_on_cpu(int cpu, long (*fn)(void *), void *arg);
  407. #endif /* CONFIG_SMP */
  408. #ifdef CONFIG_FREEZER
  409. extern void freeze_workqueues_begin(void);
  410. extern bool freeze_workqueues_busy(void);
  411. extern void thaw_workqueues(void);
  412. #endif /* CONFIG_FREEZER */
  413. #endif