11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * workqueue.h --- work queue handling for Linux. 31da177e4SLinus Torvalds */ 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #ifndef _LINUX_WORKQUEUE_H 61da177e4SLinus Torvalds #define _LINUX_WORKQUEUE_H 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/timer.h> 91da177e4SLinus Torvalds #include <linux/linkage.h> 101da177e4SLinus Torvalds #include <linux/bitops.h> 114e6045f1SJohannes Berg #include <linux/lockdep.h> 127a22ad75STejun Heo #include <linux/threads.h> 1360063497SArun Sharma #include <linux/atomic.h> 147a4e344cSTejun Heo #include <linux/cpumask.h> 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds struct workqueue_struct; 171da177e4SLinus Torvalds 1865f27f38SDavid Howells struct work_struct; 1965f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work); 20d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data); 216bb49e59SDavid Howells 22a08727baSLinus Torvalds /* 23a08727baSLinus Torvalds * The first word is the work queue pointer and the flags rolled into 24a08727baSLinus Torvalds * one 25a08727baSLinus Torvalds */ 26a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data)) 27a08727baSLinus Torvalds 2822df02bbSTejun Heo enum { 2922df02bbSTejun Heo WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */ 308a2e8e5dSTejun Heo WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */ 31112202d9STejun Heo WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */ 328a2e8e5dSTejun Heo WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */ 3322df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 348a2e8e5dSTejun Heo WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */ 358a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */ 360f900049STejun Heo #else 378a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */ 3822df02bbSTejun Heo #endif 3922df02bbSTejun Heo 4073f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS = 4, 4173f53c4aSTejun Heo 4222df02bbSTejun Heo WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT, 438a2e8e5dSTejun Heo WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT, 44112202d9STejun Heo WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT, 45affee4b2STejun Heo WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT, 4622df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 4722df02bbSTejun Heo WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT, 4822df02bbSTejun Heo #else 4922df02bbSTejun Heo WORK_STRUCT_STATIC = 0, 5022df02bbSTejun Heo #endif 5122df02bbSTejun Heo 5273f53c4aSTejun Heo /* 5373f53c4aSTejun Heo * The last color is no color used for works which don't 5473f53c4aSTejun Heo * participate in workqueue flushing. 5573f53c4aSTejun Heo */ 5673f53c4aSTejun Heo WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1, 5773f53c4aSTejun Heo WORK_NO_COLOR = WORK_NR_COLORS, 5873f53c4aSTejun Heo 59bdbc5dd7STejun Heo /* special cpu IDs */ 60f3421797STejun Heo WORK_CPU_UNBOUND = NR_CPUS, 616be19588SLai Jiangshan WORK_CPU_END = NR_CPUS + 1, 62bdbc5dd7STejun Heo 6373f53c4aSTejun Heo /* 64112202d9STejun Heo * Reserve 7 bits off of pwq pointer w/ debugobjects turned off. 65112202d9STejun Heo * This makes pwqs aligned to 256 bytes and allows 15 workqueue 66112202d9STejun Heo * flush colors. 6773f53c4aSTejun Heo */ 6873f53c4aSTejun Heo WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT + 6973f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS, 7073f53c4aSTejun Heo 71112202d9STejun Heo /* data contains off-queue information when !WORK_STRUCT_PWQ */ 7245d9550aSLai Jiangshan WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT, 73bbb68dfaSTejun Heo 74bbb68dfaSTejun Heo WORK_OFFQ_CANCELING = (1 << WORK_OFFQ_FLAG_BASE), 75bbb68dfaSTejun Heo 76715b06b8STejun Heo /* 77715b06b8STejun Heo * When a work item is off queue, its high bits point to the last 787c3eed5cSTejun Heo * pool it was on. Cap at 31 bits and use the highest number to 797c3eed5cSTejun Heo * indicate that no pool is associated. 80715b06b8STejun Heo */ 81bbb68dfaSTejun Heo WORK_OFFQ_FLAG_BITS = 1, 827c3eed5cSTejun Heo WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS, 837c3eed5cSTejun Heo WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT, 847c3eed5cSTejun Heo WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31, 857c3eed5cSTejun Heo WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1, 86b5490077STejun Heo 87b5490077STejun Heo /* convenience constants */ 880f900049STejun Heo WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1, 8922df02bbSTejun Heo WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK, 907c3eed5cSTejun Heo WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT, 91dcd989cbSTejun Heo 92dcd989cbSTejun Heo /* bit mask for work_busy() return values */ 93dcd989cbSTejun Heo WORK_BUSY_PENDING = 1 << 0, 94dcd989cbSTejun Heo WORK_BUSY_RUNNING = 1 << 1, 9522df02bbSTejun Heo }; 9622df02bbSTejun Heo 971da177e4SLinus Torvalds struct work_struct { 98a08727baSLinus Torvalds atomic_long_t data; 991da177e4SLinus Torvalds struct list_head entry; 1006bb49e59SDavid Howells work_func_t func; 1014e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1024e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1034e6045f1SJohannes Berg #endif 10452bad64dSDavid Howells }; 10552bad64dSDavid Howells 1067c3eed5cSTejun Heo #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL) 1077a22ad75STejun Heo #define WORK_DATA_STATIC_INIT() \ 1087c3eed5cSTejun Heo ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC) 109a08727baSLinus Torvalds 11052bad64dSDavid Howells struct delayed_work { 11152bad64dSDavid Howells struct work_struct work; 1121da177e4SLinus Torvalds struct timer_list timer; 11360c057bcSLai Jiangshan 11460c057bcSLai Jiangshan /* target workqueue and CPU ->timer uses to queue ->work */ 11560c057bcSLai Jiangshan struct workqueue_struct *wq; 1161265057fSTejun Heo int cpu; 1171da177e4SLinus Torvalds }; 1181da177e4SLinus Torvalds 1197a4e344cSTejun Heo /* 1207a4e344cSTejun Heo * A struct for workqueue attributes. This can be used to change 1217a4e344cSTejun Heo * attributes of an unbound workqueue. 1227a4e344cSTejun Heo */ 1237a4e344cSTejun Heo struct workqueue_attrs { 1247a4e344cSTejun Heo int nice; /* nice level */ 1257a4e344cSTejun Heo cpumask_var_t cpumask; /* allowed CPUs */ 1267a4e344cSTejun Heo }; 1277a4e344cSTejun Heo 128bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work) 129bf6aede7SJean Delvare { 130bf6aede7SJean Delvare return container_of(work, struct delayed_work, work); 131bf6aede7SJean Delvare } 132bf6aede7SJean Delvare 1331fa44ecaSJames Bottomley struct execute_work { 1341fa44ecaSJames Bottomley struct work_struct work; 1351fa44ecaSJames Bottomley }; 1361fa44ecaSJames Bottomley 1374e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1384e6045f1SJohannes Berg /* 1394e6045f1SJohannes Berg * NB: because we have to copy the lockdep_map, setting _key 1404e6045f1SJohannes Berg * here is required, otherwise it could get initialised to the 1414e6045f1SJohannes Berg * copy of the lockdep_map! 1424e6045f1SJohannes Berg */ 1434e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \ 1444e6045f1SJohannes Berg .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), 1454e6045f1SJohannes Berg #else 1464e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) 1474e6045f1SJohannes Berg #endif 1484e6045f1SJohannes Berg 14965f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) { \ 150dc186ad7SThomas Gleixner .data = WORK_DATA_STATIC_INIT(), \ 15165f27f38SDavid Howells .entry = { &(n).entry, &(n).entry }, \ 15265f27f38SDavid Howells .func = (f), \ 1534e6045f1SJohannes Berg __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ 15465f27f38SDavid Howells } 15565f27f38SDavid Howells 156f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \ 15765f27f38SDavid Howells .work = __WORK_INITIALIZER((n).work, (f)), \ 158f991b318STejun Heo .timer = __TIMER_INITIALIZER(delayed_work_timer_fn, \ 159e0aecdd8STejun Heo 0, (unsigned long)&(n), \ 160e0aecdd8STejun Heo (tflags) | TIMER_IRQSAFE), \ 161dd6414b5SPhil Carmody } 162dd6414b5SPhil Carmody 16365f27f38SDavid Howells #define DECLARE_WORK(n, f) \ 16465f27f38SDavid Howells struct work_struct n = __WORK_INITIALIZER(n, f) 16565f27f38SDavid Howells 16665f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f) \ 167f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0) 16865f27f38SDavid Howells 169203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f) \ 170f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE) 171dd6414b5SPhil Carmody 1721da177e4SLinus Torvalds /* 17365f27f38SDavid Howells * initialize a work item's function pointer 1741da177e4SLinus Torvalds */ 17565f27f38SDavid Howells #define PREPARE_WORK(_work, _func) \ 1761da177e4SLinus Torvalds do { \ 17752bad64dSDavid Howells (_work)->func = (_func); \ 1781da177e4SLinus Torvalds } while (0) 1791da177e4SLinus Torvalds 18065f27f38SDavid Howells #define PREPARE_DELAYED_WORK(_work, _func) \ 18165f27f38SDavid Howells PREPARE_WORK(&(_work)->work, (_func)) 18252bad64dSDavid Howells 183dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 184dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack); 185dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work); 1864690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) 1874690c4abSTejun Heo { 18822df02bbSTejun Heo return *work_data_bits(work) & WORK_STRUCT_STATIC; 1894690c4abSTejun Heo } 190dc186ad7SThomas Gleixner #else 191dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { } 192dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { } 1934690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; } 194dc186ad7SThomas Gleixner #endif 195dc186ad7SThomas Gleixner 1961da177e4SLinus Torvalds /* 19752bad64dSDavid Howells * initialize all of a work item in one go 198a08727baSLinus Torvalds * 199b9049df5SDmitri Vorobiev * NOTE! No point in using "atomic_long_set()": using a direct 200a08727baSLinus Torvalds * assignment of the work data initializer allows the compiler 201a08727baSLinus Torvalds * to generate better code. 2021da177e4SLinus Torvalds */ 2034e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 204dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2054e6045f1SJohannes Berg do { \ 2064e6045f1SJohannes Berg static struct lock_class_key __key; \ 2074e6045f1SJohannes Berg \ 208dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 2094e6045f1SJohannes Berg (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 2104e6045f1SJohannes Berg lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0); \ 2114e6045f1SJohannes Berg INIT_LIST_HEAD(&(_work)->entry); \ 2124e6045f1SJohannes Berg PREPARE_WORK((_work), (_func)); \ 2134e6045f1SJohannes Berg } while (0) 2144e6045f1SJohannes Berg #else 215dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2161da177e4SLinus Torvalds do { \ 217dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 21823b2e599SOleg Nesterov (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 21965f27f38SDavid Howells INIT_LIST_HEAD(&(_work)->entry); \ 22065f27f38SDavid Howells PREPARE_WORK((_work), (_func)); \ 22165f27f38SDavid Howells } while (0) 2224e6045f1SJohannes Berg #endif 22365f27f38SDavid Howells 224dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func) \ 225dc186ad7SThomas Gleixner do { \ 226dc186ad7SThomas Gleixner __INIT_WORK((_work), (_func), 0); \ 227dc186ad7SThomas Gleixner } while (0) 228dc186ad7SThomas Gleixner 229ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func) \ 230dc186ad7SThomas Gleixner do { \ 231dc186ad7SThomas Gleixner __INIT_WORK((_work), (_func), 1); \ 232dc186ad7SThomas Gleixner } while (0) 233dc186ad7SThomas Gleixner 234f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags) \ 23565f27f38SDavid Howells do { \ 23665f27f38SDavid Howells INIT_WORK(&(_work)->work, (_func)); \ 237f991b318STejun Heo __setup_timer(&(_work)->timer, delayed_work_timer_fn, \ 238e0aecdd8STejun Heo (unsigned long)(_work), \ 239e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 24065f27f38SDavid Howells } while (0) 24165f27f38SDavid Howells 242f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \ 2436d612b0fSPeter Zijlstra do { \ 244ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&(_work)->work, (_func)); \ 245f991b318STejun Heo __setup_timer_on_stack(&(_work)->timer, \ 246f991b318STejun Heo delayed_work_timer_fn, \ 247f991b318STejun Heo (unsigned long)(_work), \ 248e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 2496d612b0fSPeter Zijlstra } while (0) 2506d612b0fSPeter Zijlstra 251f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func) \ 252f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, 0) 253f991b318STejun Heo 254f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \ 255f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0) 256f991b318STejun Heo 257203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func) \ 258f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE) 259f991b318STejun Heo 260f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \ 261f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE) 26228287033SVenki Pallipadi 263365970a1SDavid Howells /** 264365970a1SDavid Howells * work_pending - Find out whether a work item is currently pending 265365970a1SDavid Howells * @work: The work item in question 266365970a1SDavid Howells */ 267365970a1SDavid Howells #define work_pending(work) \ 26822df02bbSTejun Heo test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 269365970a1SDavid Howells 270365970a1SDavid Howells /** 271365970a1SDavid Howells * delayed_work_pending - Find out whether a delayable work item is currently 272365970a1SDavid Howells * pending 273365970a1SDavid Howells * @work: The work item in question 274365970a1SDavid Howells */ 2750221872aSLinus Torvalds #define delayed_work_pending(w) \ 2760221872aSLinus Torvalds work_pending(&(w)->work) 277365970a1SDavid Howells 27865f27f38SDavid Howells /** 27923b2e599SOleg Nesterov * work_clear_pending - for internal use only, mark a work item as not pending 28023b2e599SOleg Nesterov * @work: The work item in question 28165f27f38SDavid Howells */ 28223b2e599SOleg Nesterov #define work_clear_pending(work) \ 28322df02bbSTejun Heo clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 28465f27f38SDavid Howells 285c54fce6eSTejun Heo /* 286c54fce6eSTejun Heo * Workqueue flags and constants. For details, please refer to 287c54fce6eSTejun Heo * Documentation/workqueue.txt. 288c54fce6eSTejun Heo */ 28997e37d7bSTejun Heo enum { 290bdbc5dd7STejun Heo WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */ 291c7fc77f7STejun Heo WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 29258a69cb4STejun Heo WQ_FREEZABLE = 1 << 2, /* freeze during suspend */ 2936370a6adSTejun Heo WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 294649027d7STejun Heo WQ_HIGHPRI = 1 << 4, /* high priority */ 295fb0e7bebSTejun Heo WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */ 296b71ab8c2STejun Heo 297618b01ebSTejun Heo __WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */ 298e41e704bSTejun Heo 299b71ab8c2STejun Heo WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 300f3421797STejun Heo WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */ 301b71ab8c2STejun Heo WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2, 30297e37d7bSTejun Heo }; 30352bad64dSDavid Howells 304f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */ 305f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE \ 306f3421797STejun Heo max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU) 307f3421797STejun Heo 308d320c038STejun Heo /* 309d320c038STejun Heo * System-wide workqueues which are always present. 310d320c038STejun Heo * 311d320c038STejun Heo * system_wq is the one used by schedule[_delayed]_work[_on](). 312d320c038STejun Heo * Multi-CPU multi-threaded. There are users which expect relatively 313d320c038STejun Heo * short queue flush time. Don't queue works which can run for too 314d320c038STejun Heo * long. 315d320c038STejun Heo * 316d320c038STejun Heo * system_long_wq is similar to system_wq but may host long running 317d320c038STejun Heo * works. Queue flushing might take relatively long. 318d320c038STejun Heo * 319f3421797STejun Heo * system_unbound_wq is unbound workqueue. Workers are not bound to 320f3421797STejun Heo * any specific CPU, not concurrency managed, and all queued works are 321f3421797STejun Heo * executed immediately as long as max_active limit is not reached and 322f3421797STejun Heo * resources are available. 3234149efb2STejun Heo * 32424d51addSTejun Heo * system_freezable_wq is equivalent to system_wq except that it's 32524d51addSTejun Heo * freezable. 326d320c038STejun Heo */ 327d320c038STejun Heo extern struct workqueue_struct *system_wq; 328d320c038STejun Heo extern struct workqueue_struct *system_long_wq; 329f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq; 33024d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq; 331ae930e0fSTejun Heo 3323b07e9caSTejun Heo static inline struct workqueue_struct * __deprecated __system_nrt_wq(void) 333ae930e0fSTejun Heo { 334ae930e0fSTejun Heo return system_wq; 335ae930e0fSTejun Heo } 336ae930e0fSTejun Heo 3373b07e9caSTejun Heo static inline struct workqueue_struct * __deprecated __system_nrt_freezable_wq(void) 338ae930e0fSTejun Heo { 339ae930e0fSTejun Heo return system_freezable_wq; 340ae930e0fSTejun Heo } 341ae930e0fSTejun Heo 342ae930e0fSTejun Heo /* equivlalent to system_wq and system_freezable_wq, deprecated */ 343ae930e0fSTejun Heo #define system_nrt_wq __system_nrt_wq() 344ae930e0fSTejun Heo #define system_nrt_freezable_wq __system_nrt_freezable_wq() 3451da177e4SLinus Torvalds 3464e6045f1SJohannes Berg extern struct workqueue_struct * 347b196be89STejun Heo __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active, 348b196be89STejun Heo struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6); 3494e6045f1SJohannes Berg 350b196be89STejun Heo /** 351b196be89STejun Heo * alloc_workqueue - allocate a workqueue 352b196be89STejun Heo * @fmt: printf format for the name of the workqueue 353b196be89STejun Heo * @flags: WQ_* flags 354b196be89STejun Heo * @max_active: max in-flight work items, 0 for default 355b196be89STejun Heo * @args: args for @fmt 356b196be89STejun Heo * 357b196be89STejun Heo * Allocate a workqueue with the specified parameters. For detailed 358b196be89STejun Heo * information on WQ_* flags, please refer to Documentation/workqueue.txt. 359b196be89STejun Heo * 360b196be89STejun Heo * The __lock_name macro dance is to guarantee that single lock_class_key 361b196be89STejun Heo * doesn't end up with different namesm, which isn't allowed by lockdep. 362b196be89STejun Heo * 363b196be89STejun Heo * RETURNS: 364b196be89STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 365b196be89STejun Heo */ 3664e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 367b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 3684e6045f1SJohannes Berg ({ \ 3694e6045f1SJohannes Berg static struct lock_class_key __key; \ 370eb13ba87SJohannes Berg const char *__lock_name; \ 371eb13ba87SJohannes Berg \ 372b196be89STejun Heo if (__builtin_constant_p(fmt)) \ 373b196be89STejun Heo __lock_name = (fmt); \ 374eb13ba87SJohannes Berg else \ 375b196be89STejun Heo __lock_name = #fmt; \ 3764e6045f1SJohannes Berg \ 377b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 378b196be89STejun Heo &__key, __lock_name, ##args); \ 3794e6045f1SJohannes Berg }) 3804e6045f1SJohannes Berg #else 381b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 382b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 383b196be89STejun Heo NULL, NULL, ##args) 3844e6045f1SJohannes Berg #endif 3854e6045f1SJohannes Berg 38681dcaf65STejun Heo /** 38781dcaf65STejun Heo * alloc_ordered_workqueue - allocate an ordered workqueue 388b196be89STejun Heo * @fmt: printf format for the name of the workqueue 38958a69cb4STejun Heo * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) 390b196be89STejun Heo * @args: args for @fmt 39181dcaf65STejun Heo * 39281dcaf65STejun Heo * Allocate an ordered workqueue. An ordered workqueue executes at 39381dcaf65STejun Heo * most one work item at any given time in the queued order. They are 39481dcaf65STejun Heo * implemented as unbound workqueues with @max_active of one. 39581dcaf65STejun Heo * 39681dcaf65STejun Heo * RETURNS: 39781dcaf65STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 39881dcaf65STejun Heo */ 399b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...) \ 400b196be89STejun Heo alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args) 40181dcaf65STejun Heo 40297e37d7bSTejun Heo #define create_workqueue(name) \ 4036370a6adSTejun Heo alloc_workqueue((name), WQ_MEM_RECLAIM, 1) 40458a69cb4STejun Heo #define create_freezable_workqueue(name) \ 40558a69cb4STejun Heo alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 40697e37d7bSTejun Heo #define create_singlethread_workqueue(name) \ 4076370a6adSTejun Heo alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq); 4101da177e4SLinus Torvalds 4117a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask); 4127a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs); 4139e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 4149e8cd2f5STejun Heo const struct workqueue_attrs *attrs); 4157a4e344cSTejun Heo 416d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq, 417c1a220e7SZhang Rui struct work_struct *work); 418d4283e93STejun Heo extern bool queue_work(struct workqueue_struct *wq, struct work_struct *work); 419d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 42052bad64dSDavid Howells struct delayed_work *work, unsigned long delay); 421d4283e93STejun Heo extern bool queue_delayed_work(struct workqueue_struct *wq, 4220a13c00eSTejun Heo struct delayed_work *work, unsigned long delay); 4238376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 4248376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 4258376fe22STejun Heo extern bool mod_delayed_work(struct workqueue_struct *wq, 4268376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 42728e53bddSOleg Nesterov 428b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq); 4299c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq); 43028e53bddSOleg Nesterov extern void flush_scheduled_work(void); 4311da177e4SLinus Torvalds 432d4283e93STejun Heo extern bool schedule_work_on(int cpu, struct work_struct *work); 433d4283e93STejun Heo extern bool schedule_work(struct work_struct *work); 434d4283e93STejun Heo extern bool schedule_delayed_work_on(int cpu, struct delayed_work *work, 43528e53bddSOleg Nesterov unsigned long delay); 436d4283e93STejun Heo extern bool schedule_delayed_work(struct delayed_work *work, 4370a13c00eSTejun Heo unsigned long delay); 43865f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func); 4391da177e4SLinus Torvalds extern int keventd_up(void); 4401da177e4SLinus Torvalds 44165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *); 4421da177e4SLinus Torvalds 443401a8d04STejun Heo extern bool flush_work(struct work_struct *work); 444401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work); 445401a8d04STejun Heo 446401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork); 44757b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork); 448401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork); 44928e53bddSOleg Nesterov 450dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq, 451dcd989cbSTejun Heo int max_active); 452d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq); 453dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work); 454dcd989cbSTejun Heo 4551da177e4SLinus Torvalds /* 4564e49627bSOleg Nesterov * Like above, but uses del_timer() instead of del_timer_sync(). This means, 4574e49627bSOleg Nesterov * if it returns 0 the timer function may be running and the queueing is in 4584e49627bSOleg Nesterov * progress. 4594e49627bSOleg Nesterov */ 460136b5721STejun Heo static inline bool __deprecated __cancel_delayed_work(struct delayed_work *work) 4614e49627bSOleg Nesterov { 462401a8d04STejun Heo bool ret; 4634e49627bSOleg Nesterov 4644e49627bSOleg Nesterov ret = del_timer(&work->timer); 4654e49627bSOleg Nesterov if (ret) 4664e49627bSOleg Nesterov work_clear_pending(&work->work); 4674e49627bSOleg Nesterov return ret; 4684e49627bSOleg Nesterov } 4694e49627bSOleg Nesterov 470606a5020STejun Heo /* used to be different but now identical to flush_work(), deprecated */ 47143829731STejun Heo static inline bool __deprecated flush_work_sync(struct work_struct *work) 472606a5020STejun Heo { 473606a5020STejun Heo return flush_work(work); 474606a5020STejun Heo } 475606a5020STejun Heo 476606a5020STejun Heo /* used to be different but now identical to flush_delayed_work(), deprecated */ 47743829731STejun Heo static inline bool __deprecated flush_delayed_work_sync(struct delayed_work *dwork) 478606a5020STejun Heo { 479606a5020STejun Heo return flush_delayed_work(dwork); 480606a5020STejun Heo } 481606a5020STejun Heo 4822d3854a3SRusty Russell #ifndef CONFIG_SMP 483d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 4842d3854a3SRusty Russell { 4852d3854a3SRusty Russell return fn(arg); 4862d3854a3SRusty Russell } 4872d3854a3SRusty Russell #else 488d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg); 4892d3854a3SRusty Russell #endif /* CONFIG_SMP */ 490a25909a4SPaul E. McKenney 491a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 492a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void); 493a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void); 494a0a1a5fdSTejun Heo extern void thaw_workqueues(void); 495a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 496a0a1a5fdSTejun Heo 4971da177e4SLinus Torvalds #endif 498