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 5979bc251fSLai Jiangshan /* not bound to any CPU, prefer the local CPU */ 60f3421797STejun Heo WORK_CPU_UNBOUND = NR_CPUS, 61bdbc5dd7STejun Heo 6273f53c4aSTejun Heo /* 63112202d9STejun Heo * Reserve 7 bits off of pwq pointer w/ debugobjects turned off. 64112202d9STejun Heo * This makes pwqs aligned to 256 bytes and allows 15 workqueue 65112202d9STejun Heo * flush colors. 6673f53c4aSTejun Heo */ 6773f53c4aSTejun Heo WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT + 6873f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS, 6973f53c4aSTejun Heo 70112202d9STejun Heo /* data contains off-queue information when !WORK_STRUCT_PWQ */ 7145d9550aSLai Jiangshan WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT, 72bbb68dfaSTejun Heo 738603e1b3STejun Heo __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE, 748603e1b3STejun Heo WORK_OFFQ_CANCELING = (1 << __WORK_OFFQ_CANCELING), 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, 953d1cb205STejun Heo 963d1cb205STejun Heo /* maximum string length for set_worker_desc() */ 973d1cb205STejun Heo WORKER_DESC_LEN = 24, 9822df02bbSTejun Heo }; 9922df02bbSTejun Heo 1001da177e4SLinus Torvalds struct work_struct { 101a08727baSLinus Torvalds atomic_long_t data; 1021da177e4SLinus Torvalds struct list_head entry; 1036bb49e59SDavid Howells work_func_t func; 1044e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1054e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1064e6045f1SJohannes Berg #endif 10752bad64dSDavid Howells }; 10852bad64dSDavid Howells 1097c3eed5cSTejun Heo #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL) 1107a22ad75STejun Heo #define WORK_DATA_STATIC_INIT() \ 1117c3eed5cSTejun Heo ATOMIC_LONG_INIT(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC) 112a08727baSLinus Torvalds 11352bad64dSDavid Howells struct delayed_work { 11452bad64dSDavid Howells struct work_struct work; 1151da177e4SLinus Torvalds struct timer_list timer; 11660c057bcSLai Jiangshan 11760c057bcSLai Jiangshan /* target workqueue and CPU ->timer uses to queue ->work */ 11860c057bcSLai Jiangshan struct workqueue_struct *wq; 1191265057fSTejun Heo int cpu; 1201da177e4SLinus Torvalds }; 1211da177e4SLinus Torvalds 1227a4e344cSTejun Heo /* 1237a4e344cSTejun Heo * A struct for workqueue attributes. This can be used to change 1247a4e344cSTejun Heo * attributes of an unbound workqueue. 125d55262c4STejun Heo * 126d55262c4STejun Heo * Unlike other fields, ->no_numa isn't a property of a worker_pool. It 127d55262c4STejun Heo * only modifies how apply_workqueue_attrs() select pools and thus doesn't 128d55262c4STejun Heo * participate in pool hash calculations or equality comparisons. 1297a4e344cSTejun Heo */ 1307a4e344cSTejun Heo struct workqueue_attrs { 1317a4e344cSTejun Heo int nice; /* nice level */ 1327a4e344cSTejun Heo cpumask_var_t cpumask; /* allowed CPUs */ 133d55262c4STejun Heo bool no_numa; /* disable NUMA affinity */ 1347a4e344cSTejun Heo }; 1357a4e344cSTejun Heo 136bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work) 137bf6aede7SJean Delvare { 138bf6aede7SJean Delvare return container_of(work, struct delayed_work, work); 139bf6aede7SJean Delvare } 140bf6aede7SJean Delvare 1411fa44ecaSJames Bottomley struct execute_work { 1421fa44ecaSJames Bottomley struct work_struct work; 1431fa44ecaSJames Bottomley }; 1441fa44ecaSJames Bottomley 1454e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1464e6045f1SJohannes Berg /* 1474e6045f1SJohannes Berg * NB: because we have to copy the lockdep_map, setting _key 1484e6045f1SJohannes Berg * here is required, otherwise it could get initialised to the 1494e6045f1SJohannes Berg * copy of the lockdep_map! 1504e6045f1SJohannes Berg */ 1514e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \ 1524e6045f1SJohannes Berg .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), 1534e6045f1SJohannes Berg #else 1544e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) 1554e6045f1SJohannes Berg #endif 1564e6045f1SJohannes Berg 15765f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) { \ 158dc186ad7SThomas Gleixner .data = WORK_DATA_STATIC_INIT(), \ 15965f27f38SDavid Howells .entry = { &(n).entry, &(n).entry }, \ 16065f27f38SDavid Howells .func = (f), \ 1614e6045f1SJohannes Berg __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ 16265f27f38SDavid Howells } 16365f27f38SDavid Howells 164f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \ 16565f27f38SDavid Howells .work = __WORK_INITIALIZER((n).work, (f)), \ 166f991b318STejun Heo .timer = __TIMER_INITIALIZER(delayed_work_timer_fn, \ 167e0aecdd8STejun Heo 0, (unsigned long)&(n), \ 168e0aecdd8STejun Heo (tflags) | TIMER_IRQSAFE), \ 169dd6414b5SPhil Carmody } 170dd6414b5SPhil Carmody 17165f27f38SDavid Howells #define DECLARE_WORK(n, f) \ 17265f27f38SDavid Howells struct work_struct n = __WORK_INITIALIZER(n, f) 17365f27f38SDavid Howells 17465f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f) \ 175f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0) 17665f27f38SDavid Howells 177203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f) \ 178f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE) 179dd6414b5SPhil Carmody 180dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 181dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack); 182dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work); 183ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work); 1844690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) 1854690c4abSTejun Heo { 18622df02bbSTejun Heo return *work_data_bits(work) & WORK_STRUCT_STATIC; 1874690c4abSTejun Heo } 188dc186ad7SThomas Gleixner #else 189dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { } 190dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { } 191ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { } 1924690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; } 193dc186ad7SThomas Gleixner #endif 194dc186ad7SThomas Gleixner 1951da177e4SLinus Torvalds /* 19652bad64dSDavid Howells * initialize all of a work item in one go 197a08727baSLinus Torvalds * 198b9049df5SDmitri Vorobiev * NOTE! No point in using "atomic_long_set()": using a direct 199a08727baSLinus Torvalds * assignment of the work data initializer allows the compiler 200a08727baSLinus Torvalds * to generate better code. 2011da177e4SLinus Torvalds */ 2024e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 203dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2044e6045f1SJohannes Berg do { \ 2054e6045f1SJohannes Berg static struct lock_class_key __key; \ 2064e6045f1SJohannes Berg \ 207dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 2084e6045f1SJohannes Berg (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 2094e6045f1SJohannes Berg lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0); \ 2104e6045f1SJohannes Berg INIT_LIST_HEAD(&(_work)->entry); \ 211f073f922STejun Heo (_work)->func = (_func); \ 2124e6045f1SJohannes Berg } while (0) 2134e6045f1SJohannes Berg #else 214dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2151da177e4SLinus Torvalds do { \ 216dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 21723b2e599SOleg Nesterov (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 21865f27f38SDavid Howells INIT_LIST_HEAD(&(_work)->entry); \ 219f073f922STejun Heo (_work)->func = (_func); \ 22065f27f38SDavid Howells } while (0) 2214e6045f1SJohannes Berg #endif 22265f27f38SDavid Howells 223dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func) \ 2249da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 0) 225dc186ad7SThomas Gleixner 226ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func) \ 2279da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 1) 228dc186ad7SThomas Gleixner 229f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags) \ 23065f27f38SDavid Howells do { \ 23165f27f38SDavid Howells INIT_WORK(&(_work)->work, (_func)); \ 232f991b318STejun Heo __setup_timer(&(_work)->timer, delayed_work_timer_fn, \ 233e0aecdd8STejun Heo (unsigned long)(_work), \ 234e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 23565f27f38SDavid Howells } while (0) 23665f27f38SDavid Howells 237f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \ 2386d612b0fSPeter Zijlstra do { \ 239ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&(_work)->work, (_func)); \ 240f991b318STejun Heo __setup_timer_on_stack(&(_work)->timer, \ 241f991b318STejun Heo delayed_work_timer_fn, \ 242f991b318STejun Heo (unsigned long)(_work), \ 243e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 2446d612b0fSPeter Zijlstra } while (0) 2456d612b0fSPeter Zijlstra 246f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func) \ 247f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, 0) 248f991b318STejun Heo 249f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \ 250f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0) 251f991b318STejun Heo 252203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func) \ 253f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE) 254f991b318STejun Heo 255f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \ 256f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE) 25728287033SVenki Pallipadi 258365970a1SDavid Howells /** 259365970a1SDavid Howells * work_pending - Find out whether a work item is currently pending 260365970a1SDavid Howells * @work: The work item in question 261365970a1SDavid Howells */ 262365970a1SDavid Howells #define work_pending(work) \ 26322df02bbSTejun Heo test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 264365970a1SDavid Howells 265365970a1SDavid Howells /** 266365970a1SDavid Howells * delayed_work_pending - Find out whether a delayable work item is currently 267365970a1SDavid Howells * pending 268355c0663SJonathan Corbet * @w: The work item in question 269365970a1SDavid Howells */ 2700221872aSLinus Torvalds #define delayed_work_pending(w) \ 2710221872aSLinus Torvalds work_pending(&(w)->work) 272365970a1SDavid Howells 273c54fce6eSTejun Heo /* 274c54fce6eSTejun Heo * Workqueue flags and constants. For details, please refer to 275c54fce6eSTejun Heo * Documentation/workqueue.txt. 276c54fce6eSTejun Heo */ 27797e37d7bSTejun Heo enum { 278c7fc77f7STejun Heo WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 27958a69cb4STejun Heo WQ_FREEZABLE = 1 << 2, /* freeze during suspend */ 2806370a6adSTejun Heo WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 281649027d7STejun Heo WQ_HIGHPRI = 1 << 4, /* high priority */ 28241f50094SGeert Uytterhoeven WQ_CPU_INTENSIVE = 1 << 5, /* cpu intensive workqueue */ 283226223abSTejun Heo WQ_SYSFS = 1 << 6, /* visible in sysfs, see wq_sysfs_register() */ 284b71ab8c2STejun Heo 285cee22a15SViresh Kumar /* 286cee22a15SViresh Kumar * Per-cpu workqueues are generally preferred because they tend to 287cee22a15SViresh Kumar * show better performance thanks to cache locality. Per-cpu 288cee22a15SViresh Kumar * workqueues exclude the scheduler from choosing the CPU to 289cee22a15SViresh Kumar * execute the worker threads, which has an unfortunate side effect 290cee22a15SViresh Kumar * of increasing power consumption. 291cee22a15SViresh Kumar * 292cee22a15SViresh Kumar * The scheduler considers a CPU idle if it doesn't have any task 293cee22a15SViresh Kumar * to execute and tries to keep idle cores idle to conserve power; 294cee22a15SViresh Kumar * however, for example, a per-cpu work item scheduled from an 295cee22a15SViresh Kumar * interrupt handler on an idle CPU will force the scheduler to 296cee22a15SViresh Kumar * excute the work item on that CPU breaking the idleness, which in 297cee22a15SViresh Kumar * turn may lead to more scheduling choices which are sub-optimal 298cee22a15SViresh Kumar * in terms of power consumption. 299cee22a15SViresh Kumar * 300cee22a15SViresh Kumar * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default 301cee22a15SViresh Kumar * but become unbound if workqueue.power_efficient kernel param is 302cee22a15SViresh Kumar * specified. Per-cpu workqueues which are identified to 303cee22a15SViresh Kumar * contribute significantly to power-consumption are identified and 304cee22a15SViresh Kumar * marked with this flag and enabling the power_efficient mode 305cee22a15SViresh Kumar * leads to noticeable power saving at the cost of small 306cee22a15SViresh Kumar * performance disadvantage. 307cee22a15SViresh Kumar * 308cee22a15SViresh Kumar * http://thread.gmane.org/gmane.linux.kernel/1480396 309cee22a15SViresh Kumar */ 310cee22a15SViresh Kumar WQ_POWER_EFFICIENT = 1 << 7, 311cee22a15SViresh Kumar 312618b01ebSTejun Heo __WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */ 3138719dceaSTejun Heo __WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */ 314e41e704bSTejun Heo 315b71ab8c2STejun Heo WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 316f3421797STejun Heo WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */ 317b71ab8c2STejun Heo WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2, 31897e37d7bSTejun Heo }; 31952bad64dSDavid Howells 320f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */ 321f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE \ 322f3421797STejun Heo max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU) 323f3421797STejun Heo 324d320c038STejun Heo /* 325d320c038STejun Heo * System-wide workqueues which are always present. 326d320c038STejun Heo * 327d320c038STejun Heo * system_wq is the one used by schedule[_delayed]_work[_on](). 328d320c038STejun Heo * Multi-CPU multi-threaded. There are users which expect relatively 329d320c038STejun Heo * short queue flush time. Don't queue works which can run for too 330d320c038STejun Heo * long. 331d320c038STejun Heo * 33273e43544SLai Jiangshan * system_highpri_wq is similar to system_wq but for work items which 33373e43544SLai Jiangshan * require WQ_HIGHPRI. 33473e43544SLai Jiangshan * 335d320c038STejun Heo * system_long_wq is similar to system_wq but may host long running 336d320c038STejun Heo * works. Queue flushing might take relatively long. 337d320c038STejun Heo * 338f3421797STejun Heo * system_unbound_wq is unbound workqueue. Workers are not bound to 339f3421797STejun Heo * any specific CPU, not concurrency managed, and all queued works are 340f3421797STejun Heo * executed immediately as long as max_active limit is not reached and 341f3421797STejun Heo * resources are available. 3424149efb2STejun Heo * 34324d51addSTejun Heo * system_freezable_wq is equivalent to system_wq except that it's 34424d51addSTejun Heo * freezable. 3450668106cSViresh Kumar * 3460668106cSViresh Kumar * *_power_efficient_wq are inclined towards saving power and converted 3470668106cSViresh Kumar * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise, 3480668106cSViresh Kumar * they are same as their non-power-efficient counterparts - e.g. 3490668106cSViresh Kumar * system_power_efficient_wq is identical to system_wq if 3500668106cSViresh Kumar * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info. 351d320c038STejun Heo */ 352d320c038STejun Heo extern struct workqueue_struct *system_wq; 35373e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq; 354d320c038STejun Heo extern struct workqueue_struct *system_long_wq; 355f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq; 35624d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq; 3570668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq; 3580668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq; 359ae930e0fSTejun Heo 3604e6045f1SJohannes Berg extern struct workqueue_struct * 361b196be89STejun Heo __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active, 362b196be89STejun Heo struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6); 3634e6045f1SJohannes Berg 364b196be89STejun Heo /** 365b196be89STejun Heo * alloc_workqueue - allocate a workqueue 366b196be89STejun Heo * @fmt: printf format for the name of the workqueue 367b196be89STejun Heo * @flags: WQ_* flags 368b196be89STejun Heo * @max_active: max in-flight work items, 0 for default 369355c0663SJonathan Corbet * @args...: args for @fmt 370b196be89STejun Heo * 371b196be89STejun Heo * Allocate a workqueue with the specified parameters. For detailed 372b196be89STejun Heo * information on WQ_* flags, please refer to Documentation/workqueue.txt. 373b196be89STejun Heo * 374b196be89STejun Heo * The __lock_name macro dance is to guarantee that single lock_class_key 375b196be89STejun Heo * doesn't end up with different namesm, which isn't allowed by lockdep. 376b196be89STejun Heo * 377b196be89STejun Heo * RETURNS: 378b196be89STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 379b196be89STejun Heo */ 3804e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 381b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 3824e6045f1SJohannes Berg ({ \ 3834e6045f1SJohannes Berg static struct lock_class_key __key; \ 384eb13ba87SJohannes Berg const char *__lock_name; \ 385eb13ba87SJohannes Berg \ 386fada94eeSLi Zhong __lock_name = #fmt#args; \ 3874e6045f1SJohannes Berg \ 388b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 389b196be89STejun Heo &__key, __lock_name, ##args); \ 3904e6045f1SJohannes Berg }) 3914e6045f1SJohannes Berg #else 392b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 393b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 394b196be89STejun Heo NULL, NULL, ##args) 3954e6045f1SJohannes Berg #endif 3964e6045f1SJohannes Berg 39781dcaf65STejun Heo /** 39881dcaf65STejun Heo * alloc_ordered_workqueue - allocate an ordered workqueue 399b196be89STejun Heo * @fmt: printf format for the name of the workqueue 40058a69cb4STejun Heo * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) 401355c0663SJonathan Corbet * @args...: args for @fmt 40281dcaf65STejun Heo * 40381dcaf65STejun Heo * Allocate an ordered workqueue. An ordered workqueue executes at 40481dcaf65STejun Heo * most one work item at any given time in the queued order. They are 40581dcaf65STejun Heo * implemented as unbound workqueues with @max_active of one. 40681dcaf65STejun Heo * 40781dcaf65STejun Heo * RETURNS: 40881dcaf65STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 40981dcaf65STejun Heo */ 410b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...) \ 4118719dceaSTejun Heo alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args) 41281dcaf65STejun Heo 41397e37d7bSTejun Heo #define create_workqueue(name) \ 414d8537548SKees Cook alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, (name)) 41558a69cb4STejun Heo #define create_freezable_workqueue(name) \ 416d8537548SKees Cook alloc_workqueue("%s", WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, \ 417d8537548SKees Cook 1, (name)) 41897e37d7bSTejun Heo #define create_singlethread_workqueue(name) \ 419e09c2c29STejun Heo alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, name) 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq); 4221da177e4SLinus Torvalds 4237a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask); 4247a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs); 4259e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 4269e8cd2f5STejun Heo const struct workqueue_attrs *attrs); 427042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask); 4287a4e344cSTejun Heo 429d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq, 430c1a220e7SZhang Rui struct work_struct *work); 431d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 43252bad64dSDavid Howells struct delayed_work *work, unsigned long delay); 4338376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 4348376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 43528e53bddSOleg Nesterov 436b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq); 4379c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq); 4381da177e4SLinus Torvalds 43965f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func); 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); 452e6267616STejun Heo extern bool current_is_workqueue_rescuer(void); 453d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq); 454dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work); 4553d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...); 4563d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task); 4573494fc30STejun Heo extern void show_workqueue_state(void); 458dcd989cbSTejun Heo 4598425e3d5STejun Heo /** 4608425e3d5STejun Heo * queue_work - queue work on a workqueue 4618425e3d5STejun Heo * @wq: workqueue to use 4628425e3d5STejun Heo * @work: work to queue 4638425e3d5STejun Heo * 4648425e3d5STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 4658425e3d5STejun Heo * 4668425e3d5STejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 4678425e3d5STejun Heo * it can be processed by another CPU. 4688425e3d5STejun Heo */ 4698425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq, 4708425e3d5STejun Heo struct work_struct *work) 4718425e3d5STejun Heo { 4728425e3d5STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 4738425e3d5STejun Heo } 4748425e3d5STejun Heo 4758425e3d5STejun Heo /** 4768425e3d5STejun Heo * queue_delayed_work - queue work on a workqueue after delay 4778425e3d5STejun Heo * @wq: workqueue to use 4788425e3d5STejun Heo * @dwork: delayable work to queue 4798425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 4808425e3d5STejun Heo * 4818425e3d5STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 4828425e3d5STejun Heo */ 4838425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq, 4848425e3d5STejun Heo struct delayed_work *dwork, 4858425e3d5STejun Heo unsigned long delay) 4868425e3d5STejun Heo { 4878425e3d5STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 4888425e3d5STejun Heo } 4898425e3d5STejun Heo 4908425e3d5STejun Heo /** 4918425e3d5STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 4928425e3d5STejun Heo * @wq: workqueue to use 4938425e3d5STejun Heo * @dwork: work to queue 4948425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 4958425e3d5STejun Heo * 4968425e3d5STejun Heo * mod_delayed_work_on() on local CPU. 4978425e3d5STejun Heo */ 4988425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq, 4998425e3d5STejun Heo struct delayed_work *dwork, 5008425e3d5STejun Heo unsigned long delay) 5018425e3d5STejun Heo { 5028425e3d5STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 5038425e3d5STejun Heo } 5048425e3d5STejun Heo 5058425e3d5STejun Heo /** 5068425e3d5STejun Heo * schedule_work_on - put work task on a specific cpu 5078425e3d5STejun Heo * @cpu: cpu to put the work task on 5088425e3d5STejun Heo * @work: job to be done 5098425e3d5STejun Heo * 5108425e3d5STejun Heo * This puts a job on a specific cpu 5118425e3d5STejun Heo */ 5128425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work) 5138425e3d5STejun Heo { 5148425e3d5STejun Heo return queue_work_on(cpu, system_wq, work); 5158425e3d5STejun Heo } 5168425e3d5STejun Heo 5178425e3d5STejun Heo /** 5188425e3d5STejun Heo * schedule_work - put work task in global workqueue 5198425e3d5STejun Heo * @work: job to be done 5208425e3d5STejun Heo * 5218425e3d5STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 5228425e3d5STejun Heo * %true otherwise. 5238425e3d5STejun Heo * 5248425e3d5STejun Heo * This puts a job in the kernel-global workqueue if it was not already 5258425e3d5STejun Heo * queued and leaves it in the same position on the kernel-global 5268425e3d5STejun Heo * workqueue otherwise. 5278425e3d5STejun Heo */ 5288425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work) 5298425e3d5STejun Heo { 5308425e3d5STejun Heo return queue_work(system_wq, work); 5318425e3d5STejun Heo } 5328425e3d5STejun Heo 5338425e3d5STejun Heo /** 53437b1ef31SLai Jiangshan * flush_scheduled_work - ensure that any scheduled work has run to completion. 53537b1ef31SLai Jiangshan * 53637b1ef31SLai Jiangshan * Forces execution of the kernel-global workqueue and blocks until its 53737b1ef31SLai Jiangshan * completion. 53837b1ef31SLai Jiangshan * 53937b1ef31SLai Jiangshan * Think twice before calling this function! It's very easy to get into 54037b1ef31SLai Jiangshan * trouble if you don't take great care. Either of the following situations 54137b1ef31SLai Jiangshan * will lead to deadlock: 54237b1ef31SLai Jiangshan * 54337b1ef31SLai Jiangshan * One of the work items currently on the workqueue needs to acquire 54437b1ef31SLai Jiangshan * a lock held by your code or its caller. 54537b1ef31SLai Jiangshan * 54637b1ef31SLai Jiangshan * Your code is running in the context of a work routine. 54737b1ef31SLai Jiangshan * 54837b1ef31SLai Jiangshan * They will be detected by lockdep when they occur, but the first might not 54937b1ef31SLai Jiangshan * occur very often. It depends on what work items are on the workqueue and 55037b1ef31SLai Jiangshan * what locks they need, which you have no control over. 55137b1ef31SLai Jiangshan * 55237b1ef31SLai Jiangshan * In most situations flushing the entire workqueue is overkill; you merely 55337b1ef31SLai Jiangshan * need to know that a particular work item isn't queued and isn't running. 55437b1ef31SLai Jiangshan * In such cases you should use cancel_delayed_work_sync() or 55537b1ef31SLai Jiangshan * cancel_work_sync() instead. 55637b1ef31SLai Jiangshan */ 55737b1ef31SLai Jiangshan static inline void flush_scheduled_work(void) 55837b1ef31SLai Jiangshan { 55937b1ef31SLai Jiangshan flush_workqueue(system_wq); 56037b1ef31SLai Jiangshan } 56137b1ef31SLai Jiangshan 56237b1ef31SLai Jiangshan /** 5638425e3d5STejun Heo * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 5648425e3d5STejun Heo * @cpu: cpu to use 5658425e3d5STejun Heo * @dwork: job to be done 5668425e3d5STejun Heo * @delay: number of jiffies to wait 5678425e3d5STejun Heo * 5688425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 5698425e3d5STejun Heo * workqueue on the specified CPU. 5708425e3d5STejun Heo */ 5718425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 5728425e3d5STejun Heo unsigned long delay) 5738425e3d5STejun Heo { 5748425e3d5STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 5758425e3d5STejun Heo } 5768425e3d5STejun Heo 5778425e3d5STejun Heo /** 5788425e3d5STejun Heo * schedule_delayed_work - put work task in global workqueue after delay 5798425e3d5STejun Heo * @dwork: job to be done 5808425e3d5STejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 5818425e3d5STejun Heo * 5828425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 5838425e3d5STejun Heo * workqueue. 5848425e3d5STejun Heo */ 5858425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork, 5868425e3d5STejun Heo unsigned long delay) 5878425e3d5STejun Heo { 5888425e3d5STejun Heo return queue_delayed_work(system_wq, dwork, delay); 5898425e3d5STejun Heo } 5908425e3d5STejun Heo 5918425e3d5STejun Heo /** 5928425e3d5STejun Heo * keventd_up - is workqueue initialized yet? 5938425e3d5STejun Heo */ 5948425e3d5STejun Heo static inline bool keventd_up(void) 5958425e3d5STejun Heo { 5968425e3d5STejun Heo return system_wq != NULL; 5978425e3d5STejun Heo } 5988425e3d5STejun Heo 5992d3854a3SRusty Russell #ifndef CONFIG_SMP 600d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 6012d3854a3SRusty Russell { 6022d3854a3SRusty Russell return fn(arg); 6032d3854a3SRusty Russell } 6042d3854a3SRusty Russell #else 605d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg); 6062d3854a3SRusty Russell #endif /* CONFIG_SMP */ 607a25909a4SPaul E. McKenney 608a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 609a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void); 610a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void); 611a0a1a5fdSTejun Heo extern void thaw_workqueues(void); 612a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 613a0a1a5fdSTejun Heo 614226223abSTejun Heo #ifdef CONFIG_SYSFS 615226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq); 616226223abSTejun Heo #else /* CONFIG_SYSFS */ 617226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq) 618226223abSTejun Heo { return 0; } 619226223abSTejun Heo #endif /* CONFIG_SYSFS */ 620226223abSTejun Heo 62182607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 62282607adcSTejun Heo void wq_watchdog_touch(int cpu); 62382607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 62482607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { } 62582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 62682607adcSTejun Heo 6271da177e4SLinus Torvalds #endif 628