1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * workqueue.h --- work queue handling for Linux. 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #ifndef _LINUX_WORKQUEUE_H 71da177e4SLinus Torvalds #define _LINUX_WORKQUEUE_H 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/timer.h> 101da177e4SLinus Torvalds #include <linux/linkage.h> 111da177e4SLinus Torvalds #include <linux/bitops.h> 124e6045f1SJohannes Berg #include <linux/lockdep.h> 137a22ad75STejun Heo #include <linux/threads.h> 1460063497SArun Sharma #include <linux/atomic.h> 157a4e344cSTejun Heo #include <linux/cpumask.h> 1605f0fe6bSTejun Heo #include <linux/rcupdate.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds struct workqueue_struct; 191da177e4SLinus Torvalds 2065f27f38SDavid Howells struct work_struct; 2165f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work); 228c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t); 236bb49e59SDavid Howells 24a08727baSLinus Torvalds /* 25a08727baSLinus Torvalds * The first word is the work queue pointer and the flags rolled into 26a08727baSLinus Torvalds * one 27a08727baSLinus Torvalds */ 28a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data)) 29a08727baSLinus Torvalds 3022df02bbSTejun Heo enum { 3122df02bbSTejun Heo WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */ 32f97a4a1aSLai Jiangshan WORK_STRUCT_INACTIVE_BIT= 1, /* work item is inactive */ 33112202d9STejun Heo WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */ 348a2e8e5dSTejun Heo WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */ 3522df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 368a2e8e5dSTejun Heo WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */ 378a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */ 380f900049STejun Heo #else 398a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */ 4022df02bbSTejun Heo #endif 4122df02bbSTejun Heo 4273f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS = 4, 4373f53c4aSTejun Heo 4422df02bbSTejun Heo WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT, 45f97a4a1aSLai Jiangshan WORK_STRUCT_INACTIVE = 1 << WORK_STRUCT_INACTIVE_BIT, 46112202d9STejun Heo WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT, 47affee4b2STejun Heo WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT, 4822df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 4922df02bbSTejun Heo WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT, 5022df02bbSTejun Heo #else 5122df02bbSTejun Heo WORK_STRUCT_STATIC = 0, 5222df02bbSTejun Heo #endif 5322df02bbSTejun Heo 54bdb0a654SLai Jiangshan WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS), 5573f53c4aSTejun Heo 5679bc251fSLai Jiangshan /* not bound to any CPU, prefer the local CPU */ 57f3421797STejun Heo WORK_CPU_UNBOUND = NR_CPUS, 58bdbc5dd7STejun Heo 5973f53c4aSTejun Heo /* 60c39ba6b3SLai Jiangshan * Reserve 8 bits off of pwq pointer w/ debugobjects turned off. 61bdb0a654SLai Jiangshan * This makes pwqs aligned to 256 bytes and allows 16 workqueue 62112202d9STejun Heo * flush colors. 6373f53c4aSTejun Heo */ 6473f53c4aSTejun Heo WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT + 6573f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS, 6673f53c4aSTejun Heo 67112202d9STejun Heo /* data contains off-queue information when !WORK_STRUCT_PWQ */ 6845d9550aSLai Jiangshan WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT, 69bbb68dfaSTejun Heo 708603e1b3STejun Heo __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE, 71bbb68dfaSTejun Heo 72715b06b8STejun Heo /* 73715b06b8STejun Heo * When a work item is off queue, its high bits point to the last 747c3eed5cSTejun Heo * pool it was on. Cap at 31 bits and use the highest number to 757c3eed5cSTejun Heo * indicate that no pool is associated. 76715b06b8STejun Heo */ 77bbb68dfaSTejun Heo WORK_OFFQ_FLAG_BITS = 1, 787c3eed5cSTejun Heo WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS, 797c3eed5cSTejun Heo WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT, 807c3eed5cSTejun Heo WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31, 81dcd989cbSTejun Heo 82dcd989cbSTejun Heo /* bit mask for work_busy() return values */ 83dcd989cbSTejun Heo WORK_BUSY_PENDING = 1 << 0, 84dcd989cbSTejun Heo WORK_BUSY_RUNNING = 1 << 1, 853d1cb205STejun Heo 863d1cb205STejun Heo /* maximum string length for set_worker_desc() */ 873d1cb205STejun Heo WORKER_DESC_LEN = 24, 8822df02bbSTejun Heo }; 8922df02bbSTejun Heo 90afa4bb77SLinus Torvalds /* Convenience constants - of type 'unsigned long', not 'enum'! */ 91afa4bb77SLinus Torvalds #define WORK_OFFQ_CANCELING (1ul << __WORK_OFFQ_CANCELING) 92afa4bb77SLinus Torvalds #define WORK_OFFQ_POOL_NONE ((1ul << WORK_OFFQ_POOL_BITS) - 1) 93afa4bb77SLinus Torvalds #define WORK_STRUCT_NO_POOL (WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT) 94afa4bb77SLinus Torvalds 95afa4bb77SLinus Torvalds #define WORK_STRUCT_FLAG_MASK ((1ul << WORK_STRUCT_FLAG_BITS) - 1) 96afa4bb77SLinus Torvalds #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK) 97afa4bb77SLinus Torvalds 981da177e4SLinus Torvalds struct work_struct { 99a08727baSLinus Torvalds atomic_long_t data; 1001da177e4SLinus Torvalds struct list_head entry; 1016bb49e59SDavid Howells work_func_t func; 1024e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1034e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1044e6045f1SJohannes Berg #endif 10552bad64dSDavid Howells }; 10652bad64dSDavid Howells 107a45463cbSArnd Bergmann #define WORK_DATA_INIT() ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL) 1087a22ad75STejun Heo #define WORK_DATA_STATIC_INIT() \ 109a45463cbSArnd Bergmann ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC)) 110a08727baSLinus Torvalds 11152bad64dSDavid Howells struct delayed_work { 11252bad64dSDavid Howells struct work_struct work; 1131da177e4SLinus Torvalds struct timer_list timer; 11460c057bcSLai Jiangshan 11560c057bcSLai Jiangshan /* target workqueue and CPU ->timer uses to queue ->work */ 11660c057bcSLai Jiangshan struct workqueue_struct *wq; 1171265057fSTejun Heo int cpu; 1181da177e4SLinus Torvalds }; 1191da177e4SLinus Torvalds 12005f0fe6bSTejun Heo struct rcu_work { 12105f0fe6bSTejun Heo struct work_struct work; 12205f0fe6bSTejun Heo struct rcu_head rcu; 12305f0fe6bSTejun Heo 12405f0fe6bSTejun Heo /* target workqueue ->rcu uses to queue ->work */ 12505f0fe6bSTejun Heo struct workqueue_struct *wq; 12605f0fe6bSTejun Heo }; 12705f0fe6bSTejun Heo 12842412c3aSSilvio Fricke /** 12942412c3aSSilvio Fricke * struct workqueue_attrs - A struct for workqueue attributes. 130d55262c4STejun Heo * 13142412c3aSSilvio Fricke * This can be used to change attributes of an unbound workqueue. 1327a4e344cSTejun Heo */ 1337a4e344cSTejun Heo struct workqueue_attrs { 13442412c3aSSilvio Fricke /** 13542412c3aSSilvio Fricke * @nice: nice level 13642412c3aSSilvio Fricke */ 13742412c3aSSilvio Fricke int nice; 13842412c3aSSilvio Fricke 13942412c3aSSilvio Fricke /** 14042412c3aSSilvio Fricke * @cpumask: allowed CPUs 14142412c3aSSilvio Fricke */ 14242412c3aSSilvio Fricke cpumask_var_t cpumask; 14342412c3aSSilvio Fricke 14442412c3aSSilvio Fricke /** 145*af73f5c9STejun Heo * @ordered: work items must be executed one by one in queueing order 14642412c3aSSilvio Fricke * 147*af73f5c9STejun Heo * Unlike other fields, ``ordered`` isn't a property of a worker_pool. It 14842412c3aSSilvio Fricke * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus 14942412c3aSSilvio Fricke * doesn't participate in pool hash calculations or equality comparisons. 15042412c3aSSilvio Fricke */ 151*af73f5c9STejun Heo bool ordered; 1527a4e344cSTejun Heo }; 1537a4e344cSTejun Heo 154bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work) 155bf6aede7SJean Delvare { 156bf6aede7SJean Delvare return container_of(work, struct delayed_work, work); 157bf6aede7SJean Delvare } 158bf6aede7SJean Delvare 15905f0fe6bSTejun Heo static inline struct rcu_work *to_rcu_work(struct work_struct *work) 16005f0fe6bSTejun Heo { 16105f0fe6bSTejun Heo return container_of(work, struct rcu_work, work); 16205f0fe6bSTejun Heo } 16305f0fe6bSTejun Heo 1641fa44ecaSJames Bottomley struct execute_work { 1651fa44ecaSJames Bottomley struct work_struct work; 1661fa44ecaSJames Bottomley }; 1671fa44ecaSJames Bottomley 1684e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1694e6045f1SJohannes Berg /* 1704e6045f1SJohannes Berg * NB: because we have to copy the lockdep_map, setting _key 1714e6045f1SJohannes Berg * here is required, otherwise it could get initialised to the 1724e6045f1SJohannes Berg * copy of the lockdep_map! 1734e6045f1SJohannes Berg */ 1744e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \ 1754e6045f1SJohannes Berg .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), 1764e6045f1SJohannes Berg #else 1774e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) 1784e6045f1SJohannes Berg #endif 1794e6045f1SJohannes Berg 18065f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) { \ 181dc186ad7SThomas Gleixner .data = WORK_DATA_STATIC_INIT(), \ 18265f27f38SDavid Howells .entry = { &(n).entry, &(n).entry }, \ 18365f27f38SDavid Howells .func = (f), \ 1844e6045f1SJohannes Berg __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ 18565f27f38SDavid Howells } 18665f27f38SDavid Howells 187f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \ 18865f27f38SDavid Howells .work = __WORK_INITIALIZER((n).work, (f)), \ 189841b86f3SKees Cook .timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\ 190e0aecdd8STejun Heo (tflags) | TIMER_IRQSAFE), \ 191dd6414b5SPhil Carmody } 192dd6414b5SPhil Carmody 19365f27f38SDavid Howells #define DECLARE_WORK(n, f) \ 19465f27f38SDavid Howells struct work_struct n = __WORK_INITIALIZER(n, f) 19565f27f38SDavid Howells 19665f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f) \ 197f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0) 19865f27f38SDavid Howells 199203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f) \ 200f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE) 201dd6414b5SPhil Carmody 202dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 203dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack); 204dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work); 205ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work); 2064690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) 2074690c4abSTejun Heo { 20822df02bbSTejun Heo return *work_data_bits(work) & WORK_STRUCT_STATIC; 2094690c4abSTejun Heo } 210dc186ad7SThomas Gleixner #else 211dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { } 212dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { } 213ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { } 2144690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; } 215dc186ad7SThomas Gleixner #endif 216dc186ad7SThomas Gleixner 2171da177e4SLinus Torvalds /* 21852bad64dSDavid Howells * initialize all of a work item in one go 219a08727baSLinus Torvalds * 220b9049df5SDmitri Vorobiev * NOTE! No point in using "atomic_long_set()": using a direct 221a08727baSLinus Torvalds * assignment of the work data initializer allows the compiler 222a08727baSLinus Torvalds * to generate better code. 2231da177e4SLinus Torvalds */ 2244e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 225dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2264e6045f1SJohannes Berg do { \ 2274e6045f1SJohannes Berg static struct lock_class_key __key; \ 2284e6045f1SJohannes Berg \ 229dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 2304e6045f1SJohannes Berg (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 231fd1a5b04SByungchul Park lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \ 2324e6045f1SJohannes Berg INIT_LIST_HEAD(&(_work)->entry); \ 233f073f922STejun Heo (_work)->func = (_func); \ 2344e6045f1SJohannes Berg } while (0) 2354e6045f1SJohannes Berg #else 236dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2371da177e4SLinus Torvalds do { \ 238dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 23923b2e599SOleg Nesterov (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 24065f27f38SDavid Howells INIT_LIST_HEAD(&(_work)->entry); \ 241f073f922STejun Heo (_work)->func = (_func); \ 24265f27f38SDavid Howells } while (0) 2434e6045f1SJohannes Berg #endif 24465f27f38SDavid Howells 245dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func) \ 2469da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 0) 247dc186ad7SThomas Gleixner 248ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func) \ 2499da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 1) 250dc186ad7SThomas Gleixner 251f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags) \ 25265f27f38SDavid Howells do { \ 25365f27f38SDavid Howells INIT_WORK(&(_work)->work, (_func)); \ 254919b250fSKees Cook __init_timer(&(_work)->timer, \ 255919b250fSKees Cook delayed_work_timer_fn, \ 256e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 25765f27f38SDavid Howells } while (0) 25865f27f38SDavid Howells 259f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \ 2606d612b0fSPeter Zijlstra do { \ 261ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&(_work)->work, (_func)); \ 262919b250fSKees Cook __init_timer_on_stack(&(_work)->timer, \ 263919b250fSKees Cook delayed_work_timer_fn, \ 264e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 2656d612b0fSPeter Zijlstra } while (0) 2666d612b0fSPeter Zijlstra 267f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func) \ 268f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, 0) 269f991b318STejun Heo 270f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \ 271f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0) 272f991b318STejun Heo 273203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func) \ 274f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE) 275f991b318STejun Heo 276f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \ 277f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE) 27828287033SVenki Pallipadi 27905f0fe6bSTejun Heo #define INIT_RCU_WORK(_work, _func) \ 28005f0fe6bSTejun Heo INIT_WORK(&(_work)->work, (_func)) 28105f0fe6bSTejun Heo 28205f0fe6bSTejun Heo #define INIT_RCU_WORK_ONSTACK(_work, _func) \ 28305f0fe6bSTejun Heo INIT_WORK_ONSTACK(&(_work)->work, (_func)) 28405f0fe6bSTejun Heo 285365970a1SDavid Howells /** 286365970a1SDavid Howells * work_pending - Find out whether a work item is currently pending 287365970a1SDavid Howells * @work: The work item in question 288365970a1SDavid Howells */ 289365970a1SDavid Howells #define work_pending(work) \ 29022df02bbSTejun Heo test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 291365970a1SDavid Howells 292365970a1SDavid Howells /** 293365970a1SDavid Howells * delayed_work_pending - Find out whether a delayable work item is currently 294365970a1SDavid Howells * pending 295355c0663SJonathan Corbet * @w: The work item in question 296365970a1SDavid Howells */ 2970221872aSLinus Torvalds #define delayed_work_pending(w) \ 2980221872aSLinus Torvalds work_pending(&(w)->work) 299365970a1SDavid Howells 300c54fce6eSTejun Heo /* 301c54fce6eSTejun Heo * Workqueue flags and constants. For details, please refer to 30242412c3aSSilvio Fricke * Documentation/core-api/workqueue.rst. 303c54fce6eSTejun Heo */ 30497e37d7bSTejun Heo enum { 305c7fc77f7STejun Heo WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 30658a69cb4STejun Heo WQ_FREEZABLE = 1 << 2, /* freeze during suspend */ 3076370a6adSTejun Heo WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 308649027d7STejun Heo WQ_HIGHPRI = 1 << 4, /* high priority */ 30941f50094SGeert Uytterhoeven WQ_CPU_INTENSIVE = 1 << 5, /* cpu intensive workqueue */ 31093e86295SMenglong Dong WQ_SYSFS = 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */ 311b71ab8c2STejun Heo 312cee22a15SViresh Kumar /* 313cee22a15SViresh Kumar * Per-cpu workqueues are generally preferred because they tend to 314cee22a15SViresh Kumar * show better performance thanks to cache locality. Per-cpu 315cee22a15SViresh Kumar * workqueues exclude the scheduler from choosing the CPU to 316cee22a15SViresh Kumar * execute the worker threads, which has an unfortunate side effect 317cee22a15SViresh Kumar * of increasing power consumption. 318cee22a15SViresh Kumar * 319cee22a15SViresh Kumar * The scheduler considers a CPU idle if it doesn't have any task 320cee22a15SViresh Kumar * to execute and tries to keep idle cores idle to conserve power; 321cee22a15SViresh Kumar * however, for example, a per-cpu work item scheduled from an 322cee22a15SViresh Kumar * interrupt handler on an idle CPU will force the scheduler to 32367dc8325SCai Huoqing * execute the work item on that CPU breaking the idleness, which in 324cee22a15SViresh Kumar * turn may lead to more scheduling choices which are sub-optimal 325cee22a15SViresh Kumar * in terms of power consumption. 326cee22a15SViresh Kumar * 327cee22a15SViresh Kumar * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default 328cee22a15SViresh Kumar * but become unbound if workqueue.power_efficient kernel param is 329cee22a15SViresh Kumar * specified. Per-cpu workqueues which are identified to 330cee22a15SViresh Kumar * contribute significantly to power-consumption are identified and 331cee22a15SViresh Kumar * marked with this flag and enabling the power_efficient mode 332cee22a15SViresh Kumar * leads to noticeable power saving at the cost of small 333cee22a15SViresh Kumar * performance disadvantage. 334cee22a15SViresh Kumar * 335cee22a15SViresh Kumar * http://thread.gmane.org/gmane.linux.kernel/1480396 336cee22a15SViresh Kumar */ 337cee22a15SViresh Kumar WQ_POWER_EFFICIENT = 1 << 7, 338cee22a15SViresh Kumar 33933e3f0a3SRichard Clark __WQ_DESTROYING = 1 << 15, /* internal: workqueue is destroying */ 340618b01ebSTejun Heo __WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */ 3418719dceaSTejun Heo __WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */ 34223d11a58STejun Heo __WQ_LEGACY = 1 << 18, /* internal: create*_workqueue() */ 343fbf1c41fSBen Hutchings __WQ_ORDERED_EXPLICIT = 1 << 19, /* internal: alloc_ordered_workqueue() */ 344e41e704bSTejun Heo 345b71ab8c2STejun Heo WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 346636b927eSTejun Heo WQ_UNBOUND_MAX_ACTIVE = WQ_MAX_ACTIVE, 347b71ab8c2STejun Heo WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2, 34897e37d7bSTejun Heo }; 34952bad64dSDavid Howells 350d320c038STejun Heo /* 351d320c038STejun Heo * System-wide workqueues which are always present. 352d320c038STejun Heo * 353d320c038STejun Heo * system_wq is the one used by schedule[_delayed]_work[_on](). 354d320c038STejun Heo * Multi-CPU multi-threaded. There are users which expect relatively 355d320c038STejun Heo * short queue flush time. Don't queue works which can run for too 356d320c038STejun Heo * long. 357d320c038STejun Heo * 35873e43544SLai Jiangshan * system_highpri_wq is similar to system_wq but for work items which 35973e43544SLai Jiangshan * require WQ_HIGHPRI. 36073e43544SLai Jiangshan * 361d320c038STejun Heo * system_long_wq is similar to system_wq but may host long running 362d320c038STejun Heo * works. Queue flushing might take relatively long. 363d320c038STejun Heo * 364f3421797STejun Heo * system_unbound_wq is unbound workqueue. Workers are not bound to 365f3421797STejun Heo * any specific CPU, not concurrency managed, and all queued works are 366f3421797STejun Heo * executed immediately as long as max_active limit is not reached and 367f3421797STejun Heo * resources are available. 3684149efb2STejun Heo * 36924d51addSTejun Heo * system_freezable_wq is equivalent to system_wq except that it's 37024d51addSTejun Heo * freezable. 3710668106cSViresh Kumar * 3720668106cSViresh Kumar * *_power_efficient_wq are inclined towards saving power and converted 3730668106cSViresh Kumar * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise, 3740668106cSViresh Kumar * they are same as their non-power-efficient counterparts - e.g. 3750668106cSViresh Kumar * system_power_efficient_wq is identical to system_wq if 3760668106cSViresh Kumar * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info. 377d320c038STejun Heo */ 378d320c038STejun Heo extern struct workqueue_struct *system_wq; 37973e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq; 380d320c038STejun Heo extern struct workqueue_struct *system_long_wq; 381f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq; 38224d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq; 3830668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq; 3840668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq; 385ae930e0fSTejun Heo 386b196be89STejun Heo /** 387b196be89STejun Heo * alloc_workqueue - allocate a workqueue 388b196be89STejun Heo * @fmt: printf format for the name of the workqueue 389b196be89STejun Heo * @flags: WQ_* flags 390636b927eSTejun Heo * @max_active: max in-flight work items per CPU, 0 for default 391669de8bdSBart Van Assche * remaining args: args for @fmt 392b196be89STejun Heo * 393b196be89STejun Heo * Allocate a workqueue with the specified parameters. For detailed 39442412c3aSSilvio Fricke * information on WQ_* flags, please refer to 39542412c3aSSilvio Fricke * Documentation/core-api/workqueue.rst. 396b196be89STejun Heo * 397b196be89STejun Heo * RETURNS: 398b196be89STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 399b196be89STejun Heo */ 40080f0a1f9SRolf Eike Beer __printf(1, 4) struct workqueue_struct * 40180f0a1f9SRolf Eike Beer alloc_workqueue(const char *fmt, unsigned int flags, int max_active, ...); 4024e6045f1SJohannes Berg 40381dcaf65STejun Heo /** 40481dcaf65STejun Heo * alloc_ordered_workqueue - allocate an ordered workqueue 405b196be89STejun Heo * @fmt: printf format for the name of the workqueue 40658a69cb4STejun Heo * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) 4078bee9dd9SJonathan Neuschäfer * @args: args for @fmt 40881dcaf65STejun Heo * 40981dcaf65STejun Heo * Allocate an ordered workqueue. An ordered workqueue executes at 41081dcaf65STejun Heo * most one work item at any given time in the queued order. They are 41181dcaf65STejun Heo * implemented as unbound workqueues with @max_active of one. 41281dcaf65STejun Heo * 41381dcaf65STejun Heo * RETURNS: 41481dcaf65STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 41581dcaf65STejun Heo */ 416b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...) \ 4170a94efb5STejun Heo alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | \ 4180a94efb5STejun Heo __WQ_ORDERED_EXPLICIT | (flags), 1, ##args) 41981dcaf65STejun Heo 42097e37d7bSTejun Heo #define create_workqueue(name) \ 42123d11a58STejun Heo alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name)) 42258a69cb4STejun Heo #define create_freezable_workqueue(name) \ 42323d11a58STejun Heo alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND | \ 42423d11a58STejun Heo WQ_MEM_RECLAIM, 1, (name)) 42597e37d7bSTejun Heo #define create_singlethread_workqueue(name) \ 42623d11a58STejun Heo alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name) 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq); 4291da177e4SLinus Torvalds 430513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void); 431513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs); 432513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 433513c98d0SDaniel Jordan const struct workqueue_attrs *attrs); 434042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask); 4357a4e344cSTejun Heo 436d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq, 437c1a220e7SZhang Rui struct work_struct *work); 4388204e0c1SAlexander Duyck extern bool queue_work_node(int node, struct workqueue_struct *wq, 4398204e0c1SAlexander Duyck struct work_struct *work); 440d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 44152bad64dSDavid Howells struct delayed_work *work, unsigned long delay); 4428376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 4438376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 44405f0fe6bSTejun Heo extern bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork); 44528e53bddSOleg Nesterov 446c4f135d6STetsuo Handa extern void __flush_workqueue(struct workqueue_struct *wq); 4479c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq); 4481da177e4SLinus Torvalds 44965f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func); 4501da177e4SLinus Torvalds 45165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *); 4521da177e4SLinus Torvalds 453401a8d04STejun Heo extern bool flush_work(struct work_struct *work); 45473b4b532SAndrey Grodzovsky extern bool cancel_work(struct work_struct *work); 455401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work); 456401a8d04STejun Heo 457401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork); 45857b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork); 459401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork); 46028e53bddSOleg Nesterov 46105f0fe6bSTejun Heo extern bool flush_rcu_work(struct rcu_work *rwork); 46205f0fe6bSTejun Heo 463dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq, 464dcd989cbSTejun Heo int max_active); 46527d4ee03SLukas Wunner extern struct work_struct *current_work(void); 466e6267616STejun Heo extern bool current_is_workqueue_rescuer(void); 467d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq); 468dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work); 4693d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...); 4703d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task); 47155df0933SImran Khan extern void show_all_workqueues(void); 472704bc669SJungseung Lee extern void show_freezable_workqueues(void); 47355df0933SImran Khan extern void show_one_workqueue(struct workqueue_struct *wq); 4746b59808bSTejun Heo extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task); 475dcd989cbSTejun Heo 4768425e3d5STejun Heo /** 4778425e3d5STejun Heo * queue_work - queue work on a workqueue 4788425e3d5STejun Heo * @wq: workqueue to use 4798425e3d5STejun Heo * @work: work to queue 4808425e3d5STejun Heo * 4818425e3d5STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 4828425e3d5STejun Heo * 4838425e3d5STejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 4848425e3d5STejun Heo * it can be processed by another CPU. 485dbb92f88SAndrea Parri * 486dbb92f88SAndrea Parri * Memory-ordering properties: If it returns %true, guarantees that all stores 487dbb92f88SAndrea Parri * preceding the call to queue_work() in the program order will be visible from 488dbb92f88SAndrea Parri * the CPU which will execute @work by the time such work executes, e.g., 489dbb92f88SAndrea Parri * 490dbb92f88SAndrea Parri * { x is initially 0 } 491dbb92f88SAndrea Parri * 492dbb92f88SAndrea Parri * CPU0 CPU1 493dbb92f88SAndrea Parri * 494dbb92f88SAndrea Parri * WRITE_ONCE(x, 1); [ @work is being executed ] 495dbb92f88SAndrea Parri * r0 = queue_work(wq, work); r1 = READ_ONCE(x); 496dbb92f88SAndrea Parri * 497dbb92f88SAndrea Parri * Forbids: r0 == true && r1 == 0 4988425e3d5STejun Heo */ 4998425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq, 5008425e3d5STejun Heo struct work_struct *work) 5018425e3d5STejun Heo { 5028425e3d5STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 5038425e3d5STejun Heo } 5048425e3d5STejun Heo 5058425e3d5STejun Heo /** 5068425e3d5STejun Heo * queue_delayed_work - queue work on a workqueue after delay 5078425e3d5STejun Heo * @wq: workqueue to use 5088425e3d5STejun Heo * @dwork: delayable work to queue 5098425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 5108425e3d5STejun Heo * 5118425e3d5STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 5128425e3d5STejun Heo */ 5138425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq, 5148425e3d5STejun Heo struct delayed_work *dwork, 5158425e3d5STejun Heo unsigned long delay) 5168425e3d5STejun Heo { 5178425e3d5STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 5188425e3d5STejun Heo } 5198425e3d5STejun Heo 5208425e3d5STejun Heo /** 5218425e3d5STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 5228425e3d5STejun Heo * @wq: workqueue to use 5238425e3d5STejun Heo * @dwork: work to queue 5248425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 5258425e3d5STejun Heo * 5268425e3d5STejun Heo * mod_delayed_work_on() on local CPU. 5278425e3d5STejun Heo */ 5288425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq, 5298425e3d5STejun Heo struct delayed_work *dwork, 5308425e3d5STejun Heo unsigned long delay) 5318425e3d5STejun Heo { 5328425e3d5STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 5338425e3d5STejun Heo } 5348425e3d5STejun Heo 5358425e3d5STejun Heo /** 5368425e3d5STejun Heo * schedule_work_on - put work task on a specific cpu 5378425e3d5STejun Heo * @cpu: cpu to put the work task on 5388425e3d5STejun Heo * @work: job to be done 5398425e3d5STejun Heo * 5408425e3d5STejun Heo * This puts a job on a specific cpu 5418425e3d5STejun Heo */ 5428425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work) 5438425e3d5STejun Heo { 5448425e3d5STejun Heo return queue_work_on(cpu, system_wq, work); 5458425e3d5STejun Heo } 5468425e3d5STejun Heo 5478425e3d5STejun Heo /** 5488425e3d5STejun Heo * schedule_work - put work task in global workqueue 5498425e3d5STejun Heo * @work: job to be done 5508425e3d5STejun Heo * 5518425e3d5STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 5528425e3d5STejun Heo * %true otherwise. 5538425e3d5STejun Heo * 5548425e3d5STejun Heo * This puts a job in the kernel-global workqueue if it was not already 5558425e3d5STejun Heo * queued and leaves it in the same position on the kernel-global 5568425e3d5STejun Heo * workqueue otherwise. 557dbb92f88SAndrea Parri * 558dbb92f88SAndrea Parri * Shares the same memory-ordering properties of queue_work(), cf. the 559dbb92f88SAndrea Parri * DocBook header of queue_work(). 5608425e3d5STejun Heo */ 5618425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work) 5628425e3d5STejun Heo { 5638425e3d5STejun Heo return queue_work(system_wq, work); 5648425e3d5STejun Heo } 5658425e3d5STejun Heo 566c4f135d6STetsuo Handa /* 567c4f135d6STetsuo Handa * Detect attempt to flush system-wide workqueues at compile time when possible. 56820bdedafSTetsuo Handa * Warn attempt to flush system-wide workqueues at runtime. 569c4f135d6STetsuo Handa * 570c4f135d6STetsuo Handa * See https://lkml.kernel.org/r/49925af7-78a8-a3dd-bce6-cfc02e1a9236@I-love.SAKURA.ne.jp 571c4f135d6STetsuo Handa * for reasons and steps for converting system-wide workqueues into local workqueues. 572c4f135d6STetsuo Handa */ 573c4f135d6STetsuo Handa extern void __warn_flushing_systemwide_wq(void) 574c4f135d6STetsuo Handa __compiletime_warning("Please avoid flushing system-wide workqueues."); 575c4f135d6STetsuo Handa 57620bdedafSTetsuo Handa /* Please stop using this function, for this function will be removed in near future. */ 577c4f135d6STetsuo Handa #define flush_scheduled_work() \ 578c4f135d6STetsuo Handa ({ \ 579c4f135d6STetsuo Handa __warn_flushing_systemwide_wq(); \ 580c4f135d6STetsuo Handa __flush_workqueue(system_wq); \ 581c4f135d6STetsuo Handa }) 582c4f135d6STetsuo Handa 583c4f135d6STetsuo Handa #define flush_workqueue(wq) \ 584c4f135d6STetsuo Handa ({ \ 585c4f135d6STetsuo Handa struct workqueue_struct *_wq = (wq); \ 586c4f135d6STetsuo Handa \ 587c4f135d6STetsuo Handa if ((__builtin_constant_p(_wq == system_wq) && \ 588c4f135d6STetsuo Handa _wq == system_wq) || \ 589c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_highpri_wq) && \ 590c4f135d6STetsuo Handa _wq == system_highpri_wq) || \ 591c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_long_wq) && \ 592c4f135d6STetsuo Handa _wq == system_long_wq) || \ 593c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_unbound_wq) && \ 594c4f135d6STetsuo Handa _wq == system_unbound_wq) || \ 595c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_freezable_wq) && \ 596c4f135d6STetsuo Handa _wq == system_freezable_wq) || \ 597c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_power_efficient_wq) && \ 598c4f135d6STetsuo Handa _wq == system_power_efficient_wq) || \ 599c4f135d6STetsuo Handa (__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \ 600c4f135d6STetsuo Handa _wq == system_freezable_power_efficient_wq)) \ 601c4f135d6STetsuo Handa __warn_flushing_systemwide_wq(); \ 602c4f135d6STetsuo Handa __flush_workqueue(_wq); \ 603c4f135d6STetsuo Handa }) 60437b1ef31SLai Jiangshan 60537b1ef31SLai Jiangshan /** 6068425e3d5STejun Heo * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 6078425e3d5STejun Heo * @cpu: cpu to use 6088425e3d5STejun Heo * @dwork: job to be done 6098425e3d5STejun Heo * @delay: number of jiffies to wait 6108425e3d5STejun Heo * 6118425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 6128425e3d5STejun Heo * workqueue on the specified CPU. 6138425e3d5STejun Heo */ 6148425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 6158425e3d5STejun Heo unsigned long delay) 6168425e3d5STejun Heo { 6178425e3d5STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 6188425e3d5STejun Heo } 6198425e3d5STejun Heo 6208425e3d5STejun Heo /** 6218425e3d5STejun Heo * schedule_delayed_work - put work task in global workqueue after delay 6228425e3d5STejun Heo * @dwork: job to be done 6238425e3d5STejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 6248425e3d5STejun Heo * 6258425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 6268425e3d5STejun Heo * workqueue. 6278425e3d5STejun Heo */ 6288425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork, 6298425e3d5STejun Heo unsigned long delay) 6308425e3d5STejun Heo { 6318425e3d5STejun Heo return queue_delayed_work(system_wq, dwork, delay); 6328425e3d5STejun Heo } 6338425e3d5STejun Heo 6342d3854a3SRusty Russell #ifndef CONFIG_SMP 635d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 6362d3854a3SRusty Russell { 6372d3854a3SRusty Russell return fn(arg); 6382d3854a3SRusty Russell } 6390e8d6a93SThomas Gleixner static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 6400e8d6a93SThomas Gleixner { 6410e8d6a93SThomas Gleixner return fn(arg); 6420e8d6a93SThomas Gleixner } 6432d3854a3SRusty Russell #else 644d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg); 6450e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg); 6462d3854a3SRusty Russell #endif /* CONFIG_SMP */ 647a25909a4SPaul E. McKenney 648a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 649a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void); 650a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void); 651a0a1a5fdSTejun Heo extern void thaw_workqueues(void); 652a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 653a0a1a5fdSTejun Heo 654226223abSTejun Heo #ifdef CONFIG_SYSFS 655226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq); 656226223abSTejun Heo #else /* CONFIG_SYSFS */ 657226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq) 658226223abSTejun Heo { return 0; } 659226223abSTejun Heo #endif /* CONFIG_SYSFS */ 660226223abSTejun Heo 66182607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 66282607adcSTejun Heo void wq_watchdog_touch(int cpu); 66382607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 66482607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { } 66582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 66682607adcSTejun Heo 6677ee681b2SThomas Gleixner #ifdef CONFIG_SMP 6687ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu); 6697ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu); 6707ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu); 6717ee681b2SThomas Gleixner #endif 6727ee681b2SThomas Gleixner 6732333e829SYu Chen void __init workqueue_init_early(void); 6742333e829SYu Chen void __init workqueue_init(void); 6753347fa09STejun Heo 6761da177e4SLinus Torvalds #endif 677