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> 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds struct workqueue_struct; 161da177e4SLinus Torvalds 1765f27f38SDavid Howells struct work_struct; 1865f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work); 19d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data); 206bb49e59SDavid Howells 21a08727baSLinus Torvalds /* 22a08727baSLinus Torvalds * The first word is the work queue pointer and the flags rolled into 23a08727baSLinus Torvalds * one 24a08727baSLinus Torvalds */ 25a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data)) 26a08727baSLinus Torvalds 2722df02bbSTejun Heo enum { 2822df02bbSTejun Heo WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */ 298a2e8e5dSTejun Heo WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */ 308a2e8e5dSTejun Heo WORK_STRUCT_CWQ_BIT = 2, /* data points to cwq */ 318a2e8e5dSTejun Heo WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */ 3222df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 338a2e8e5dSTejun Heo WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */ 348a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */ 350f900049STejun Heo #else 368a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */ 3722df02bbSTejun Heo #endif 3822df02bbSTejun Heo 3973f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS = 4, 4073f53c4aSTejun Heo 4122df02bbSTejun Heo WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT, 428a2e8e5dSTejun Heo WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT, 43e120153dSTejun Heo WORK_STRUCT_CWQ = 1 << WORK_STRUCT_CWQ_BIT, 44affee4b2STejun Heo WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT, 4522df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 4622df02bbSTejun Heo WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT, 4722df02bbSTejun Heo #else 4822df02bbSTejun Heo WORK_STRUCT_STATIC = 0, 4922df02bbSTejun Heo #endif 5022df02bbSTejun Heo 5173f53c4aSTejun Heo /* 5273f53c4aSTejun Heo * The last color is no color used for works which don't 5373f53c4aSTejun Heo * participate in workqueue flushing. 5473f53c4aSTejun Heo */ 5573f53c4aSTejun Heo WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1, 5673f53c4aSTejun Heo WORK_NO_COLOR = WORK_NR_COLORS, 5773f53c4aSTejun Heo 58bdbc5dd7STejun Heo /* special cpu IDs */ 59f3421797STejun Heo WORK_CPU_UNBOUND = NR_CPUS, 606be19588SLai Jiangshan WORK_CPU_END = NR_CPUS + 1, 61bdbc5dd7STejun Heo 6273f53c4aSTejun Heo /* 63e120153dSTejun Heo * Reserve 7 bits off of cwq pointer w/ debugobjects turned 648a2e8e5dSTejun Heo * off. This makes cwqs aligned to 256 bytes and allows 15 658a2e8e5dSTejun Heo * workqueue flush colors. 6673f53c4aSTejun Heo */ 6773f53c4aSTejun Heo WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT + 6873f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS, 6973f53c4aSTejun Heo 70b5490077STejun Heo /* data contains off-queue information when !WORK_STRUCT_CWQ */ 71b5490077STejun Heo WORK_OFFQ_FLAG_BASE = WORK_STRUCT_FLAG_BITS, 72bbb68dfaSTejun Heo 73bbb68dfaSTejun Heo WORK_OFFQ_CANCELING = (1 << WORK_OFFQ_FLAG_BASE), 74bbb68dfaSTejun Heo 75715b06b8STejun Heo /* 76715b06b8STejun Heo * When a work item is off queue, its high bits point to the last 777c3eed5cSTejun Heo * pool it was on. Cap at 31 bits and use the highest number to 787c3eed5cSTejun Heo * indicate that no pool is associated. 79715b06b8STejun Heo */ 80bbb68dfaSTejun Heo WORK_OFFQ_FLAG_BITS = 1, 817c3eed5cSTejun Heo WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS, 827c3eed5cSTejun Heo WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT, 837c3eed5cSTejun Heo WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31, 847c3eed5cSTejun Heo WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1, 85b5490077STejun Heo 86b5490077STejun Heo /* convenience constants */ 870f900049STejun Heo WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1, 8822df02bbSTejun Heo WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK, 897c3eed5cSTejun Heo WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT, 90dcd989cbSTejun Heo 91dcd989cbSTejun Heo /* bit mask for work_busy() return values */ 92dcd989cbSTejun Heo WORK_BUSY_PENDING = 1 << 0, 93dcd989cbSTejun Heo WORK_BUSY_RUNNING = 1 << 1, 9422df02bbSTejun Heo }; 9522df02bbSTejun Heo 961da177e4SLinus Torvalds struct work_struct { 97a08727baSLinus Torvalds atomic_long_t data; 981da177e4SLinus Torvalds struct list_head entry; 996bb49e59SDavid Howells work_func_t func; 1004e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1014e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1024e6045f1SJohannes Berg #endif 10352bad64dSDavid Howells }; 10452bad64dSDavid Howells 1057c3eed5cSTejun Heo #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL) 1067a22ad75STejun Heo #define WORK_DATA_STATIC_INIT() \ 1077c3eed5cSTejun Heo ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC) 108a08727baSLinus Torvalds 10952bad64dSDavid Howells struct delayed_work { 11052bad64dSDavid Howells struct work_struct work; 1111da177e4SLinus Torvalds struct timer_list timer; 11260c057bcSLai Jiangshan 11360c057bcSLai Jiangshan /* target workqueue and CPU ->timer uses to queue ->work */ 11460c057bcSLai Jiangshan struct workqueue_struct *wq; 1151265057fSTejun Heo int cpu; 1161da177e4SLinus Torvalds }; 1171da177e4SLinus Torvalds 118bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work) 119bf6aede7SJean Delvare { 120bf6aede7SJean Delvare return container_of(work, struct delayed_work, work); 121bf6aede7SJean Delvare } 122bf6aede7SJean Delvare 1231fa44ecaSJames Bottomley struct execute_work { 1241fa44ecaSJames Bottomley struct work_struct work; 1251fa44ecaSJames Bottomley }; 1261fa44ecaSJames Bottomley 1274e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1284e6045f1SJohannes Berg /* 1294e6045f1SJohannes Berg * NB: because we have to copy the lockdep_map, setting _key 1304e6045f1SJohannes Berg * here is required, otherwise it could get initialised to the 1314e6045f1SJohannes Berg * copy of the lockdep_map! 1324e6045f1SJohannes Berg */ 1334e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \ 1344e6045f1SJohannes Berg .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), 1354e6045f1SJohannes Berg #else 1364e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) 1374e6045f1SJohannes Berg #endif 1384e6045f1SJohannes Berg 13965f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) { \ 140dc186ad7SThomas Gleixner .data = WORK_DATA_STATIC_INIT(), \ 14165f27f38SDavid Howells .entry = { &(n).entry, &(n).entry }, \ 14265f27f38SDavid Howells .func = (f), \ 1434e6045f1SJohannes Berg __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ 14465f27f38SDavid Howells } 14565f27f38SDavid Howells 146f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \ 14765f27f38SDavid Howells .work = __WORK_INITIALIZER((n).work, (f)), \ 148f991b318STejun Heo .timer = __TIMER_INITIALIZER(delayed_work_timer_fn, \ 149e0aecdd8STejun Heo 0, (unsigned long)&(n), \ 150e0aecdd8STejun Heo (tflags) | TIMER_IRQSAFE), \ 151dd6414b5SPhil Carmody } 152dd6414b5SPhil Carmody 15365f27f38SDavid Howells #define DECLARE_WORK(n, f) \ 15465f27f38SDavid Howells struct work_struct n = __WORK_INITIALIZER(n, f) 15565f27f38SDavid Howells 15665f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f) \ 157f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0) 15865f27f38SDavid Howells 159203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f) \ 160f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE) 161dd6414b5SPhil Carmody 1621da177e4SLinus Torvalds /* 16365f27f38SDavid Howells * initialize a work item's function pointer 1641da177e4SLinus Torvalds */ 16565f27f38SDavid Howells #define PREPARE_WORK(_work, _func) \ 1661da177e4SLinus Torvalds do { \ 16752bad64dSDavid Howells (_work)->func = (_func); \ 1681da177e4SLinus Torvalds } while (0) 1691da177e4SLinus Torvalds 17065f27f38SDavid Howells #define PREPARE_DELAYED_WORK(_work, _func) \ 17165f27f38SDavid Howells PREPARE_WORK(&(_work)->work, (_func)) 17252bad64dSDavid Howells 173dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 174dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack); 175dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work); 1764690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) 1774690c4abSTejun Heo { 17822df02bbSTejun Heo return *work_data_bits(work) & WORK_STRUCT_STATIC; 1794690c4abSTejun Heo } 180dc186ad7SThomas Gleixner #else 181dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { } 182dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { } 1834690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; } 184dc186ad7SThomas Gleixner #endif 185dc186ad7SThomas Gleixner 1861da177e4SLinus Torvalds /* 18752bad64dSDavid Howells * initialize all of a work item in one go 188a08727baSLinus Torvalds * 189b9049df5SDmitri Vorobiev * NOTE! No point in using "atomic_long_set()": using a direct 190a08727baSLinus Torvalds * assignment of the work data initializer allows the compiler 191a08727baSLinus Torvalds * to generate better code. 1921da177e4SLinus Torvalds */ 1934e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 194dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 1954e6045f1SJohannes Berg do { \ 1964e6045f1SJohannes Berg static struct lock_class_key __key; \ 1974e6045f1SJohannes Berg \ 198dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 1994e6045f1SJohannes Berg (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 2004e6045f1SJohannes Berg lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0); \ 2014e6045f1SJohannes Berg INIT_LIST_HEAD(&(_work)->entry); \ 2024e6045f1SJohannes Berg PREPARE_WORK((_work), (_func)); \ 2034e6045f1SJohannes Berg } while (0) 2044e6045f1SJohannes Berg #else 205dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2061da177e4SLinus Torvalds do { \ 207dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 20823b2e599SOleg Nesterov (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 20965f27f38SDavid Howells INIT_LIST_HEAD(&(_work)->entry); \ 21065f27f38SDavid Howells PREPARE_WORK((_work), (_func)); \ 21165f27f38SDavid Howells } while (0) 2124e6045f1SJohannes Berg #endif 21365f27f38SDavid Howells 214dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func) \ 215dc186ad7SThomas Gleixner do { \ 216dc186ad7SThomas Gleixner __INIT_WORK((_work), (_func), 0); \ 217dc186ad7SThomas Gleixner } while (0) 218dc186ad7SThomas Gleixner 219ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func) \ 220dc186ad7SThomas Gleixner do { \ 221dc186ad7SThomas Gleixner __INIT_WORK((_work), (_func), 1); \ 222dc186ad7SThomas Gleixner } while (0) 223dc186ad7SThomas Gleixner 224f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags) \ 22565f27f38SDavid Howells do { \ 22665f27f38SDavid Howells INIT_WORK(&(_work)->work, (_func)); \ 227f991b318STejun Heo __setup_timer(&(_work)->timer, delayed_work_timer_fn, \ 228e0aecdd8STejun Heo (unsigned long)(_work), \ 229e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 23065f27f38SDavid Howells } while (0) 23165f27f38SDavid Howells 232f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \ 2336d612b0fSPeter Zijlstra do { \ 234ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&(_work)->work, (_func)); \ 235f991b318STejun Heo __setup_timer_on_stack(&(_work)->timer, \ 236f991b318STejun Heo delayed_work_timer_fn, \ 237f991b318STejun Heo (unsigned long)(_work), \ 238e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 2396d612b0fSPeter Zijlstra } while (0) 2406d612b0fSPeter Zijlstra 241f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func) \ 242f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, 0) 243f991b318STejun Heo 244f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \ 245f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0) 246f991b318STejun Heo 247203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func) \ 248f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE) 249f991b318STejun Heo 250f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \ 251f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE) 25228287033SVenki Pallipadi 253365970a1SDavid Howells /** 254365970a1SDavid Howells * work_pending - Find out whether a work item is currently pending 255365970a1SDavid Howells * @work: The work item in question 256365970a1SDavid Howells */ 257365970a1SDavid Howells #define work_pending(work) \ 25822df02bbSTejun Heo test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 259365970a1SDavid Howells 260365970a1SDavid Howells /** 261365970a1SDavid Howells * delayed_work_pending - Find out whether a delayable work item is currently 262365970a1SDavid Howells * pending 263365970a1SDavid Howells * @work: The work item in question 264365970a1SDavid Howells */ 2650221872aSLinus Torvalds #define delayed_work_pending(w) \ 2660221872aSLinus Torvalds work_pending(&(w)->work) 267365970a1SDavid Howells 26865f27f38SDavid Howells /** 26923b2e599SOleg Nesterov * work_clear_pending - for internal use only, mark a work item as not pending 27023b2e599SOleg Nesterov * @work: The work item in question 27165f27f38SDavid Howells */ 27223b2e599SOleg Nesterov #define work_clear_pending(work) \ 27322df02bbSTejun Heo clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 27465f27f38SDavid Howells 275c54fce6eSTejun Heo /* 276c54fce6eSTejun Heo * Workqueue flags and constants. For details, please refer to 277c54fce6eSTejun Heo * Documentation/workqueue.txt. 278c54fce6eSTejun Heo */ 27997e37d7bSTejun Heo enum { 280bdbc5dd7STejun Heo WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */ 281c7fc77f7STejun Heo WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 28258a69cb4STejun Heo WQ_FREEZABLE = 1 << 2, /* freeze during suspend */ 2836370a6adSTejun Heo WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 284649027d7STejun Heo WQ_HIGHPRI = 1 << 4, /* high priority */ 285fb0e7bebSTejun Heo WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */ 286b71ab8c2STejun Heo 2879c5a2ba7STejun Heo WQ_DRAINING = 1 << 6, /* internal: workqueue is draining */ 2886370a6adSTejun Heo WQ_RESCUER = 1 << 7, /* internal: workqueue has rescuer */ 289e41e704bSTejun Heo 290b71ab8c2STejun Heo WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 291f3421797STejun Heo WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */ 292b71ab8c2STejun Heo WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2, 29397e37d7bSTejun Heo }; 29452bad64dSDavid Howells 295f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */ 296f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE \ 297f3421797STejun Heo max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU) 298f3421797STejun Heo 299d320c038STejun Heo /* 300d320c038STejun Heo * System-wide workqueues which are always present. 301d320c038STejun Heo * 302d320c038STejun Heo * system_wq is the one used by schedule[_delayed]_work[_on](). 303d320c038STejun Heo * Multi-CPU multi-threaded. There are users which expect relatively 304d320c038STejun Heo * short queue flush time. Don't queue works which can run for too 305d320c038STejun Heo * long. 306d320c038STejun Heo * 307d320c038STejun Heo * system_long_wq is similar to system_wq but may host long running 308d320c038STejun Heo * works. Queue flushing might take relatively long. 309d320c038STejun Heo * 310f3421797STejun Heo * system_unbound_wq is unbound workqueue. Workers are not bound to 311f3421797STejun Heo * any specific CPU, not concurrency managed, and all queued works are 312f3421797STejun Heo * executed immediately as long as max_active limit is not reached and 313f3421797STejun Heo * resources are available. 3144149efb2STejun Heo * 31524d51addSTejun Heo * system_freezable_wq is equivalent to system_wq except that it's 31624d51addSTejun Heo * freezable. 317d320c038STejun Heo */ 318d320c038STejun Heo extern struct workqueue_struct *system_wq; 319d320c038STejun Heo extern struct workqueue_struct *system_long_wq; 320f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq; 32124d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq; 322ae930e0fSTejun Heo 3233b07e9caSTejun Heo static inline struct workqueue_struct * __deprecated __system_nrt_wq(void) 324ae930e0fSTejun Heo { 325ae930e0fSTejun Heo return system_wq; 326ae930e0fSTejun Heo } 327ae930e0fSTejun Heo 3283b07e9caSTejun Heo static inline struct workqueue_struct * __deprecated __system_nrt_freezable_wq(void) 329ae930e0fSTejun Heo { 330ae930e0fSTejun Heo return system_freezable_wq; 331ae930e0fSTejun Heo } 332ae930e0fSTejun Heo 333ae930e0fSTejun Heo /* equivlalent to system_wq and system_freezable_wq, deprecated */ 334ae930e0fSTejun Heo #define system_nrt_wq __system_nrt_wq() 335ae930e0fSTejun Heo #define system_nrt_freezable_wq __system_nrt_freezable_wq() 3361da177e4SLinus Torvalds 3374e6045f1SJohannes Berg extern struct workqueue_struct * 338b196be89STejun Heo __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active, 339b196be89STejun Heo struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6); 3404e6045f1SJohannes Berg 341b196be89STejun Heo /** 342b196be89STejun Heo * alloc_workqueue - allocate a workqueue 343b196be89STejun Heo * @fmt: printf format for the name of the workqueue 344b196be89STejun Heo * @flags: WQ_* flags 345b196be89STejun Heo * @max_active: max in-flight work items, 0 for default 346b196be89STejun Heo * @args: args for @fmt 347b196be89STejun Heo * 348b196be89STejun Heo * Allocate a workqueue with the specified parameters. For detailed 349b196be89STejun Heo * information on WQ_* flags, please refer to Documentation/workqueue.txt. 350b196be89STejun Heo * 351b196be89STejun Heo * The __lock_name macro dance is to guarantee that single lock_class_key 352b196be89STejun Heo * doesn't end up with different namesm, which isn't allowed by lockdep. 353b196be89STejun Heo * 354b196be89STejun Heo * RETURNS: 355b196be89STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 356b196be89STejun Heo */ 3574e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 358b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 3594e6045f1SJohannes Berg ({ \ 3604e6045f1SJohannes Berg static struct lock_class_key __key; \ 361eb13ba87SJohannes Berg const char *__lock_name; \ 362eb13ba87SJohannes Berg \ 363b196be89STejun Heo if (__builtin_constant_p(fmt)) \ 364b196be89STejun Heo __lock_name = (fmt); \ 365eb13ba87SJohannes Berg else \ 366b196be89STejun Heo __lock_name = #fmt; \ 3674e6045f1SJohannes Berg \ 368b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 369b196be89STejun Heo &__key, __lock_name, ##args); \ 3704e6045f1SJohannes Berg }) 3714e6045f1SJohannes Berg #else 372b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 373b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 374b196be89STejun Heo NULL, NULL, ##args) 3754e6045f1SJohannes Berg #endif 3764e6045f1SJohannes Berg 37781dcaf65STejun Heo /** 37881dcaf65STejun Heo * alloc_ordered_workqueue - allocate an ordered workqueue 379b196be89STejun Heo * @fmt: printf format for the name of the workqueue 38058a69cb4STejun Heo * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) 381b196be89STejun Heo * @args: args for @fmt 38281dcaf65STejun Heo * 38381dcaf65STejun Heo * Allocate an ordered workqueue. An ordered workqueue executes at 38481dcaf65STejun Heo * most one work item at any given time in the queued order. They are 38581dcaf65STejun Heo * implemented as unbound workqueues with @max_active of one. 38681dcaf65STejun Heo * 38781dcaf65STejun Heo * RETURNS: 38881dcaf65STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 38981dcaf65STejun Heo */ 390b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...) \ 391b196be89STejun Heo alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args) 39281dcaf65STejun Heo 39397e37d7bSTejun Heo #define create_workqueue(name) \ 3946370a6adSTejun Heo alloc_workqueue((name), WQ_MEM_RECLAIM, 1) 39558a69cb4STejun Heo #define create_freezable_workqueue(name) \ 39658a69cb4STejun Heo alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 39797e37d7bSTejun Heo #define create_singlethread_workqueue(name) \ 3986370a6adSTejun Heo alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1) 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq); 4011da177e4SLinus Torvalds 402d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq, 403c1a220e7SZhang Rui struct work_struct *work); 404d4283e93STejun Heo extern bool queue_work(struct workqueue_struct *wq, struct work_struct *work); 405d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 40652bad64dSDavid Howells struct delayed_work *work, unsigned long delay); 407d4283e93STejun Heo extern bool queue_delayed_work(struct workqueue_struct *wq, 4080a13c00eSTejun Heo struct delayed_work *work, unsigned long delay); 4098376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 4108376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 4118376fe22STejun Heo extern bool mod_delayed_work(struct workqueue_struct *wq, 4128376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 41328e53bddSOleg Nesterov 414b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq); 4159c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq); 41628e53bddSOleg Nesterov extern void flush_scheduled_work(void); 4171da177e4SLinus Torvalds 418d4283e93STejun Heo extern bool schedule_work_on(int cpu, struct work_struct *work); 419d4283e93STejun Heo extern bool schedule_work(struct work_struct *work); 420d4283e93STejun Heo extern bool schedule_delayed_work_on(int cpu, struct delayed_work *work, 42128e53bddSOleg Nesterov unsigned long delay); 422d4283e93STejun Heo extern bool schedule_delayed_work(struct delayed_work *work, 4230a13c00eSTejun Heo unsigned long delay); 42465f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func); 4251da177e4SLinus Torvalds extern int keventd_up(void); 4261da177e4SLinus Torvalds 42765f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *); 4281da177e4SLinus Torvalds 429401a8d04STejun Heo extern bool flush_work(struct work_struct *work); 430401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work); 431401a8d04STejun Heo 432401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork); 43357b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork); 434401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork); 43528e53bddSOleg Nesterov 436dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq, 437dcd989cbSTejun Heo int max_active); 438dcd989cbSTejun Heo extern bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq); 439dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work); 440dcd989cbSTejun Heo 4411da177e4SLinus Torvalds /* 4424e49627bSOleg Nesterov * Like above, but uses del_timer() instead of del_timer_sync(). This means, 4434e49627bSOleg Nesterov * if it returns 0 the timer function may be running and the queueing is in 4444e49627bSOleg Nesterov * progress. 4454e49627bSOleg Nesterov */ 446136b5721STejun Heo static inline bool __deprecated __cancel_delayed_work(struct delayed_work *work) 4474e49627bSOleg Nesterov { 448401a8d04STejun Heo bool ret; 4494e49627bSOleg Nesterov 4504e49627bSOleg Nesterov ret = del_timer(&work->timer); 4514e49627bSOleg Nesterov if (ret) 4524e49627bSOleg Nesterov work_clear_pending(&work->work); 4534e49627bSOleg Nesterov return ret; 4544e49627bSOleg Nesterov } 4554e49627bSOleg Nesterov 456606a5020STejun Heo /* used to be different but now identical to flush_work(), deprecated */ 45743829731STejun Heo static inline bool __deprecated flush_work_sync(struct work_struct *work) 458606a5020STejun Heo { 459606a5020STejun Heo return flush_work(work); 460606a5020STejun Heo } 461606a5020STejun Heo 462606a5020STejun Heo /* used to be different but now identical to flush_delayed_work(), deprecated */ 46343829731STejun Heo static inline bool __deprecated flush_delayed_work_sync(struct delayed_work *dwork) 464606a5020STejun Heo { 465606a5020STejun Heo return flush_delayed_work(dwork); 466606a5020STejun Heo } 467606a5020STejun Heo 4682d3854a3SRusty Russell #ifndef CONFIG_SMP 4692d3854a3SRusty Russell static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 4702d3854a3SRusty Russell { 4712d3854a3SRusty Russell return fn(arg); 4722d3854a3SRusty Russell } 4732d3854a3SRusty Russell #else 4742d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg); 4752d3854a3SRusty Russell #endif /* CONFIG_SMP */ 476a25909a4SPaul E. McKenney 477a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 478a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void); 479a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void); 480a0a1a5fdSTejun Heo extern void thaw_workqueues(void); 481a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 482a0a1a5fdSTejun Heo 4831da177e4SLinus Torvalds #endif 484