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> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds struct workqueue_struct; 181da177e4SLinus Torvalds 1965f27f38SDavid Howells struct work_struct; 2065f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work); 218c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t); 226bb49e59SDavid Howells 23a08727baSLinus Torvalds /* 24a08727baSLinus Torvalds * The first word is the work queue pointer and the flags rolled into 25a08727baSLinus Torvalds * one 26a08727baSLinus Torvalds */ 27a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data)) 28a08727baSLinus Torvalds 2922df02bbSTejun Heo enum { 3022df02bbSTejun Heo WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */ 318a2e8e5dSTejun Heo WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */ 32112202d9STejun Heo WORK_STRUCT_PWQ_BIT = 2, /* data points to pwq */ 338a2e8e5dSTejun Heo WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */ 3422df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 358a2e8e5dSTejun Heo WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */ 368a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */ 370f900049STejun Heo #else 388a2e8e5dSTejun Heo WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */ 3922df02bbSTejun Heo #endif 4022df02bbSTejun Heo 4173f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS = 4, 4273f53c4aSTejun Heo 4322df02bbSTejun Heo WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT, 448a2e8e5dSTejun Heo WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT, 45112202d9STejun Heo WORK_STRUCT_PWQ = 1 << WORK_STRUCT_PWQ_BIT, 46affee4b2STejun Heo WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT, 4722df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK 4822df02bbSTejun Heo WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT, 4922df02bbSTejun Heo #else 5022df02bbSTejun Heo WORK_STRUCT_STATIC = 0, 5122df02bbSTejun Heo #endif 5222df02bbSTejun Heo 5373f53c4aSTejun Heo /* 5473f53c4aSTejun Heo * The last color is no color used for works which don't 5573f53c4aSTejun Heo * participate in workqueue flushing. 5673f53c4aSTejun Heo */ 5773f53c4aSTejun Heo WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1, 5873f53c4aSTejun Heo WORK_NO_COLOR = WORK_NR_COLORS, 5973f53c4aSTejun Heo 6079bc251fSLai Jiangshan /* not bound to any CPU, prefer the local CPU */ 61f3421797STejun Heo WORK_CPU_UNBOUND = NR_CPUS, 62bdbc5dd7STejun Heo 6373f53c4aSTejun Heo /* 64112202d9STejun Heo * Reserve 7 bits off of pwq pointer w/ debugobjects turned off. 65112202d9STejun Heo * This makes pwqs aligned to 256 bytes and allows 15 workqueue 66112202d9STejun Heo * flush colors. 6773f53c4aSTejun Heo */ 6873f53c4aSTejun Heo WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT + 6973f53c4aSTejun Heo WORK_STRUCT_COLOR_BITS, 7073f53c4aSTejun Heo 71112202d9STejun Heo /* data contains off-queue information when !WORK_STRUCT_PWQ */ 7245d9550aSLai Jiangshan WORK_OFFQ_FLAG_BASE = WORK_STRUCT_COLOR_SHIFT, 73bbb68dfaSTejun Heo 748603e1b3STejun Heo __WORK_OFFQ_CANCELING = WORK_OFFQ_FLAG_BASE, 758603e1b3STejun Heo WORK_OFFQ_CANCELING = (1 << __WORK_OFFQ_CANCELING), 76bbb68dfaSTejun Heo 77715b06b8STejun Heo /* 78715b06b8STejun Heo * When a work item is off queue, its high bits point to the last 797c3eed5cSTejun Heo * pool it was on. Cap at 31 bits and use the highest number to 807c3eed5cSTejun Heo * indicate that no pool is associated. 81715b06b8STejun Heo */ 82bbb68dfaSTejun Heo WORK_OFFQ_FLAG_BITS = 1, 837c3eed5cSTejun Heo WORK_OFFQ_POOL_SHIFT = WORK_OFFQ_FLAG_BASE + WORK_OFFQ_FLAG_BITS, 847c3eed5cSTejun Heo WORK_OFFQ_LEFT = BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT, 857c3eed5cSTejun Heo WORK_OFFQ_POOL_BITS = WORK_OFFQ_LEFT <= 31 ? WORK_OFFQ_LEFT : 31, 867c3eed5cSTejun Heo WORK_OFFQ_POOL_NONE = (1LU << WORK_OFFQ_POOL_BITS) - 1, 87b5490077STejun Heo 88b5490077STejun Heo /* convenience constants */ 890f900049STejun Heo WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1, 9022df02bbSTejun Heo WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK, 917c3eed5cSTejun Heo WORK_STRUCT_NO_POOL = (unsigned long)WORK_OFFQ_POOL_NONE << WORK_OFFQ_POOL_SHIFT, 92dcd989cbSTejun Heo 93dcd989cbSTejun Heo /* bit mask for work_busy() return values */ 94dcd989cbSTejun Heo WORK_BUSY_PENDING = 1 << 0, 95dcd989cbSTejun Heo WORK_BUSY_RUNNING = 1 << 1, 963d1cb205STejun Heo 973d1cb205STejun Heo /* maximum string length for set_worker_desc() */ 983d1cb205STejun Heo WORKER_DESC_LEN = 24, 9922df02bbSTejun Heo }; 10022df02bbSTejun Heo 1011da177e4SLinus Torvalds struct work_struct { 102a08727baSLinus Torvalds atomic_long_t data; 1031da177e4SLinus Torvalds struct list_head entry; 1046bb49e59SDavid Howells work_func_t func; 1054e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1064e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1074e6045f1SJohannes Berg #endif 10852bad64dSDavid Howells }; 10952bad64dSDavid Howells 110a45463cbSArnd Bergmann #define WORK_DATA_INIT() ATOMIC_LONG_INIT((unsigned long)WORK_STRUCT_NO_POOL) 1117a22ad75STejun Heo #define WORK_DATA_STATIC_INIT() \ 112a45463cbSArnd Bergmann ATOMIC_LONG_INIT((unsigned long)(WORK_STRUCT_NO_POOL | WORK_STRUCT_STATIC)) 113a08727baSLinus Torvalds 11452bad64dSDavid Howells struct delayed_work { 11552bad64dSDavid Howells struct work_struct work; 1161da177e4SLinus Torvalds struct timer_list timer; 11760c057bcSLai Jiangshan 11860c057bcSLai Jiangshan /* target workqueue and CPU ->timer uses to queue ->work */ 11960c057bcSLai Jiangshan struct workqueue_struct *wq; 1201265057fSTejun Heo int cpu; 1211da177e4SLinus Torvalds }; 1221da177e4SLinus Torvalds 12342412c3aSSilvio Fricke /** 12442412c3aSSilvio Fricke * struct workqueue_attrs - A struct for workqueue attributes. 125d55262c4STejun Heo * 12642412c3aSSilvio Fricke * This can be used to change attributes of an unbound workqueue. 1277a4e344cSTejun Heo */ 1287a4e344cSTejun Heo struct workqueue_attrs { 12942412c3aSSilvio Fricke /** 13042412c3aSSilvio Fricke * @nice: nice level 13142412c3aSSilvio Fricke */ 13242412c3aSSilvio Fricke int nice; 13342412c3aSSilvio Fricke 13442412c3aSSilvio Fricke /** 13542412c3aSSilvio Fricke * @cpumask: allowed CPUs 13642412c3aSSilvio Fricke */ 13742412c3aSSilvio Fricke cpumask_var_t cpumask; 13842412c3aSSilvio Fricke 13942412c3aSSilvio Fricke /** 14042412c3aSSilvio Fricke * @no_numa: disable NUMA affinity 14142412c3aSSilvio Fricke * 14242412c3aSSilvio Fricke * Unlike other fields, ``no_numa`` isn't a property of a worker_pool. It 14342412c3aSSilvio Fricke * only modifies how :c:func:`apply_workqueue_attrs` select pools and thus 14442412c3aSSilvio Fricke * doesn't participate in pool hash calculations or equality comparisons. 14542412c3aSSilvio Fricke */ 14642412c3aSSilvio Fricke bool no_numa; 1477a4e344cSTejun Heo }; 1487a4e344cSTejun Heo 149bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work) 150bf6aede7SJean Delvare { 151bf6aede7SJean Delvare return container_of(work, struct delayed_work, work); 152bf6aede7SJean Delvare } 153bf6aede7SJean Delvare 1541fa44ecaSJames Bottomley struct execute_work { 1551fa44ecaSJames Bottomley struct work_struct work; 1561fa44ecaSJames Bottomley }; 1571fa44ecaSJames Bottomley 1584e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1594e6045f1SJohannes Berg /* 1604e6045f1SJohannes Berg * NB: because we have to copy the lockdep_map, setting _key 1614e6045f1SJohannes Berg * here is required, otherwise it could get initialised to the 1624e6045f1SJohannes Berg * copy of the lockdep_map! 1634e6045f1SJohannes Berg */ 1644e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \ 1654e6045f1SJohannes Berg .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), 1664e6045f1SJohannes Berg #else 1674e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) 1684e6045f1SJohannes Berg #endif 1694e6045f1SJohannes Berg 17065f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) { \ 171dc186ad7SThomas Gleixner .data = WORK_DATA_STATIC_INIT(), \ 17265f27f38SDavid Howells .entry = { &(n).entry, &(n).entry }, \ 17365f27f38SDavid Howells .func = (f), \ 1744e6045f1SJohannes Berg __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ 17565f27f38SDavid Howells } 17665f27f38SDavid Howells 177f991b318STejun Heo #define __DELAYED_WORK_INITIALIZER(n, f, tflags) { \ 17865f27f38SDavid Howells .work = __WORK_INITIALIZER((n).work, (f)), \ 179841b86f3SKees Cook .timer = __TIMER_INITIALIZER(delayed_work_timer_fn,\ 180e0aecdd8STejun Heo (tflags) | TIMER_IRQSAFE), \ 181dd6414b5SPhil Carmody } 182dd6414b5SPhil Carmody 18365f27f38SDavid Howells #define DECLARE_WORK(n, f) \ 18465f27f38SDavid Howells struct work_struct n = __WORK_INITIALIZER(n, f) 18565f27f38SDavid Howells 18665f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f) \ 187f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, 0) 18865f27f38SDavid Howells 189203b42f7STejun Heo #define DECLARE_DEFERRABLE_WORK(n, f) \ 190f991b318STejun Heo struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, TIMER_DEFERRABLE) 191dd6414b5SPhil Carmody 192dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 193dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack); 194dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work); 195ea2e64f2SThomas Gleixner extern void destroy_delayed_work_on_stack(struct delayed_work *work); 1964690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) 1974690c4abSTejun Heo { 19822df02bbSTejun Heo return *work_data_bits(work) & WORK_STRUCT_STATIC; 1994690c4abSTejun Heo } 200dc186ad7SThomas Gleixner #else 201dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { } 202dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { } 203ea2e64f2SThomas Gleixner static inline void destroy_delayed_work_on_stack(struct delayed_work *work) { } 2044690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; } 205dc186ad7SThomas Gleixner #endif 206dc186ad7SThomas Gleixner 2071da177e4SLinus Torvalds /* 20852bad64dSDavid Howells * initialize all of a work item in one go 209a08727baSLinus Torvalds * 210b9049df5SDmitri Vorobiev * NOTE! No point in using "atomic_long_set()": using a direct 211a08727baSLinus Torvalds * assignment of the work data initializer allows the compiler 212a08727baSLinus Torvalds * to generate better code. 2131da177e4SLinus Torvalds */ 2144e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 215dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2164e6045f1SJohannes Berg do { \ 2174e6045f1SJohannes Berg static struct lock_class_key __key; \ 2184e6045f1SJohannes Berg \ 219dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 2204e6045f1SJohannes Berg (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 221fd1a5b04SByungchul Park lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \ 2224e6045f1SJohannes Berg INIT_LIST_HEAD(&(_work)->entry); \ 223f073f922STejun Heo (_work)->func = (_func); \ 2244e6045f1SJohannes Berg } while (0) 2254e6045f1SJohannes Berg #else 226dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack) \ 2271da177e4SLinus Torvalds do { \ 228dc186ad7SThomas Gleixner __init_work((_work), _onstack); \ 22923b2e599SOleg Nesterov (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ 23065f27f38SDavid Howells INIT_LIST_HEAD(&(_work)->entry); \ 231f073f922STejun Heo (_work)->func = (_func); \ 23265f27f38SDavid Howells } while (0) 2334e6045f1SJohannes Berg #endif 23465f27f38SDavid Howells 235dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func) \ 2369da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 0) 237dc186ad7SThomas Gleixner 238ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func) \ 2399da7dae9SValentin Rothberg __INIT_WORK((_work), (_func), 1) 240dc186ad7SThomas Gleixner 241f991b318STejun Heo #define __INIT_DELAYED_WORK(_work, _func, _tflags) \ 24265f27f38SDavid Howells do { \ 24365f27f38SDavid Howells INIT_WORK(&(_work)->work, (_func)); \ 244919b250fSKees Cook __init_timer(&(_work)->timer, \ 245919b250fSKees Cook delayed_work_timer_fn, \ 246e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 24765f27f38SDavid Howells } while (0) 24865f27f38SDavid Howells 249f991b318STejun Heo #define __INIT_DELAYED_WORK_ONSTACK(_work, _func, _tflags) \ 2506d612b0fSPeter Zijlstra do { \ 251ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&(_work)->work, (_func)); \ 252919b250fSKees Cook __init_timer_on_stack(&(_work)->timer, \ 253919b250fSKees Cook delayed_work_timer_fn, \ 254e0aecdd8STejun Heo (_tflags) | TIMER_IRQSAFE); \ 2556d612b0fSPeter Zijlstra } while (0) 2566d612b0fSPeter Zijlstra 257f991b318STejun Heo #define INIT_DELAYED_WORK(_work, _func) \ 258f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, 0) 259f991b318STejun Heo 260f991b318STejun Heo #define INIT_DELAYED_WORK_ONSTACK(_work, _func) \ 261f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, 0) 262f991b318STejun Heo 263203b42f7STejun Heo #define INIT_DEFERRABLE_WORK(_work, _func) \ 264f991b318STejun Heo __INIT_DELAYED_WORK(_work, _func, TIMER_DEFERRABLE) 265f991b318STejun Heo 266f991b318STejun Heo #define INIT_DEFERRABLE_WORK_ONSTACK(_work, _func) \ 267f991b318STejun Heo __INIT_DELAYED_WORK_ONSTACK(_work, _func, TIMER_DEFERRABLE) 26828287033SVenki Pallipadi 269365970a1SDavid Howells /** 270365970a1SDavid Howells * work_pending - Find out whether a work item is currently pending 271365970a1SDavid Howells * @work: The work item in question 272365970a1SDavid Howells */ 273365970a1SDavid Howells #define work_pending(work) \ 27422df02bbSTejun Heo test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) 275365970a1SDavid Howells 276365970a1SDavid Howells /** 277365970a1SDavid Howells * delayed_work_pending - Find out whether a delayable work item is currently 278365970a1SDavid Howells * pending 279355c0663SJonathan Corbet * @w: The work item in question 280365970a1SDavid Howells */ 2810221872aSLinus Torvalds #define delayed_work_pending(w) \ 2820221872aSLinus Torvalds work_pending(&(w)->work) 283365970a1SDavid Howells 284c54fce6eSTejun Heo /* 285c54fce6eSTejun Heo * Workqueue flags and constants. For details, please refer to 28642412c3aSSilvio Fricke * Documentation/core-api/workqueue.rst. 287c54fce6eSTejun Heo */ 28897e37d7bSTejun Heo enum { 289c7fc77f7STejun Heo WQ_UNBOUND = 1 << 1, /* not bound to any cpu */ 29058a69cb4STejun Heo WQ_FREEZABLE = 1 << 2, /* freeze during suspend */ 2916370a6adSTejun Heo WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */ 292649027d7STejun Heo WQ_HIGHPRI = 1 << 4, /* high priority */ 29341f50094SGeert Uytterhoeven WQ_CPU_INTENSIVE = 1 << 5, /* cpu intensive workqueue */ 294226223abSTejun Heo WQ_SYSFS = 1 << 6, /* visible in sysfs, see wq_sysfs_register() */ 295b71ab8c2STejun Heo 296cee22a15SViresh Kumar /* 297cee22a15SViresh Kumar * Per-cpu workqueues are generally preferred because they tend to 298cee22a15SViresh Kumar * show better performance thanks to cache locality. Per-cpu 299cee22a15SViresh Kumar * workqueues exclude the scheduler from choosing the CPU to 300cee22a15SViresh Kumar * execute the worker threads, which has an unfortunate side effect 301cee22a15SViresh Kumar * of increasing power consumption. 302cee22a15SViresh Kumar * 303cee22a15SViresh Kumar * The scheduler considers a CPU idle if it doesn't have any task 304cee22a15SViresh Kumar * to execute and tries to keep idle cores idle to conserve power; 305cee22a15SViresh Kumar * however, for example, a per-cpu work item scheduled from an 306cee22a15SViresh Kumar * interrupt handler on an idle CPU will force the scheduler to 307cee22a15SViresh Kumar * excute the work item on that CPU breaking the idleness, which in 308cee22a15SViresh Kumar * turn may lead to more scheduling choices which are sub-optimal 309cee22a15SViresh Kumar * in terms of power consumption. 310cee22a15SViresh Kumar * 311cee22a15SViresh Kumar * Workqueues marked with WQ_POWER_EFFICIENT are per-cpu by default 312cee22a15SViresh Kumar * but become unbound if workqueue.power_efficient kernel param is 313cee22a15SViresh Kumar * specified. Per-cpu workqueues which are identified to 314cee22a15SViresh Kumar * contribute significantly to power-consumption are identified and 315cee22a15SViresh Kumar * marked with this flag and enabling the power_efficient mode 316cee22a15SViresh Kumar * leads to noticeable power saving at the cost of small 317cee22a15SViresh Kumar * performance disadvantage. 318cee22a15SViresh Kumar * 319cee22a15SViresh Kumar * http://thread.gmane.org/gmane.linux.kernel/1480396 320cee22a15SViresh Kumar */ 321cee22a15SViresh Kumar WQ_POWER_EFFICIENT = 1 << 7, 322cee22a15SViresh Kumar 323618b01ebSTejun Heo __WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */ 3248719dceaSTejun Heo __WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */ 32523d11a58STejun Heo __WQ_LEGACY = 1 << 18, /* internal: create*_workqueue() */ 326fbf1c41fSBen Hutchings __WQ_ORDERED_EXPLICIT = 1 << 19, /* internal: alloc_ordered_workqueue() */ 327e41e704bSTejun Heo 328b71ab8c2STejun Heo WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */ 329f3421797STejun Heo WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */ 330b71ab8c2STejun Heo WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2, 33197e37d7bSTejun Heo }; 33252bad64dSDavid Howells 333f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */ 334f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE \ 335f3421797STejun Heo max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU) 336f3421797STejun Heo 337d320c038STejun Heo /* 338d320c038STejun Heo * System-wide workqueues which are always present. 339d320c038STejun Heo * 340d320c038STejun Heo * system_wq is the one used by schedule[_delayed]_work[_on](). 341d320c038STejun Heo * Multi-CPU multi-threaded. There are users which expect relatively 342d320c038STejun Heo * short queue flush time. Don't queue works which can run for too 343d320c038STejun Heo * long. 344d320c038STejun Heo * 34573e43544SLai Jiangshan * system_highpri_wq is similar to system_wq but for work items which 34673e43544SLai Jiangshan * require WQ_HIGHPRI. 34773e43544SLai Jiangshan * 348d320c038STejun Heo * system_long_wq is similar to system_wq but may host long running 349d320c038STejun Heo * works. Queue flushing might take relatively long. 350d320c038STejun Heo * 351f3421797STejun Heo * system_unbound_wq is unbound workqueue. Workers are not bound to 352f3421797STejun Heo * any specific CPU, not concurrency managed, and all queued works are 353f3421797STejun Heo * executed immediately as long as max_active limit is not reached and 354f3421797STejun Heo * resources are available. 3554149efb2STejun Heo * 35624d51addSTejun Heo * system_freezable_wq is equivalent to system_wq except that it's 35724d51addSTejun Heo * freezable. 3580668106cSViresh Kumar * 3590668106cSViresh Kumar * *_power_efficient_wq are inclined towards saving power and converted 3600668106cSViresh Kumar * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise, 3610668106cSViresh Kumar * they are same as their non-power-efficient counterparts - e.g. 3620668106cSViresh Kumar * system_power_efficient_wq is identical to system_wq if 3630668106cSViresh Kumar * 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info. 364d320c038STejun Heo */ 365d320c038STejun Heo extern struct workqueue_struct *system_wq; 36673e43544SLai Jiangshan extern struct workqueue_struct *system_highpri_wq; 367d320c038STejun Heo extern struct workqueue_struct *system_long_wq; 368f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq; 36924d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq; 3700668106cSViresh Kumar extern struct workqueue_struct *system_power_efficient_wq; 3710668106cSViresh Kumar extern struct workqueue_struct *system_freezable_power_efficient_wq; 372ae930e0fSTejun Heo 3734e6045f1SJohannes Berg extern struct workqueue_struct * 374b196be89STejun Heo __alloc_workqueue_key(const char *fmt, unsigned int flags, int max_active, 375b196be89STejun Heo struct lock_class_key *key, const char *lock_name, ...) __printf(1, 6); 3764e6045f1SJohannes Berg 377b196be89STejun Heo /** 378b196be89STejun Heo * alloc_workqueue - allocate a workqueue 379b196be89STejun Heo * @fmt: printf format for the name of the workqueue 380b196be89STejun Heo * @flags: WQ_* flags 381b196be89STejun Heo * @max_active: max in-flight work items, 0 for default 382355c0663SJonathan Corbet * @args...: args for @fmt 383b196be89STejun Heo * 384b196be89STejun Heo * Allocate a workqueue with the specified parameters. For detailed 38542412c3aSSilvio Fricke * information on WQ_* flags, please refer to 38642412c3aSSilvio Fricke * Documentation/core-api/workqueue.rst. 387b196be89STejun Heo * 388b196be89STejun Heo * The __lock_name macro dance is to guarantee that single lock_class_key 389b196be89STejun Heo * doesn't end up with different namesm, which isn't allowed by lockdep. 390b196be89STejun Heo * 391b196be89STejun Heo * RETURNS: 392b196be89STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 393b196be89STejun Heo */ 3944e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 395b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 3964e6045f1SJohannes Berg ({ \ 3974e6045f1SJohannes Berg static struct lock_class_key __key; \ 398eb13ba87SJohannes Berg const char *__lock_name; \ 399eb13ba87SJohannes Berg \ 400fd1a5b04SByungchul Park __lock_name = "(wq_completion)"#fmt#args; \ 4014e6045f1SJohannes Berg \ 402b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 403b196be89STejun Heo &__key, __lock_name, ##args); \ 4044e6045f1SJohannes Berg }) 4054e6045f1SJohannes Berg #else 406b196be89STejun Heo #define alloc_workqueue(fmt, flags, max_active, args...) \ 407b196be89STejun Heo __alloc_workqueue_key((fmt), (flags), (max_active), \ 408b196be89STejun Heo NULL, NULL, ##args) 4094e6045f1SJohannes Berg #endif 4104e6045f1SJohannes Berg 41181dcaf65STejun Heo /** 41281dcaf65STejun Heo * alloc_ordered_workqueue - allocate an ordered workqueue 413b196be89STejun Heo * @fmt: printf format for the name of the workqueue 41458a69cb4STejun Heo * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful) 415355c0663SJonathan Corbet * @args...: args for @fmt 41681dcaf65STejun Heo * 41781dcaf65STejun Heo * Allocate an ordered workqueue. An ordered workqueue executes at 41881dcaf65STejun Heo * most one work item at any given time in the queued order. They are 41981dcaf65STejun Heo * implemented as unbound workqueues with @max_active of one. 42081dcaf65STejun Heo * 42181dcaf65STejun Heo * RETURNS: 42281dcaf65STejun Heo * Pointer to the allocated workqueue on success, %NULL on failure. 42381dcaf65STejun Heo */ 424b196be89STejun Heo #define alloc_ordered_workqueue(fmt, flags, args...) \ 4250a94efb5STejun Heo alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | \ 4260a94efb5STejun Heo __WQ_ORDERED_EXPLICIT | (flags), 1, ##args) 42781dcaf65STejun Heo 42897e37d7bSTejun Heo #define create_workqueue(name) \ 42923d11a58STejun Heo alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name)) 43058a69cb4STejun Heo #define create_freezable_workqueue(name) \ 43123d11a58STejun Heo alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND | \ 43223d11a58STejun Heo WQ_MEM_RECLAIM, 1, (name)) 43397e37d7bSTejun Heo #define create_singlethread_workqueue(name) \ 43423d11a58STejun Heo alloc_ordered_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, name) 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq); 4371da177e4SLinus Torvalds 4387a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask); 4397a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs); 4409e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 4419e8cd2f5STejun Heo const struct workqueue_attrs *attrs); 442042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask); 4437a4e344cSTejun Heo 444d4283e93STejun Heo extern bool queue_work_on(int cpu, struct workqueue_struct *wq, 445c1a220e7SZhang Rui struct work_struct *work); 446d4283e93STejun Heo extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 44752bad64dSDavid Howells struct delayed_work *work, unsigned long delay); 4488376fe22STejun Heo extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 4498376fe22STejun Heo struct delayed_work *dwork, unsigned long delay); 45028e53bddSOleg Nesterov 451b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq); 4529c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq); 4531da177e4SLinus Torvalds 45465f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func); 4551da177e4SLinus Torvalds 45665f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *); 4571da177e4SLinus Torvalds 458401a8d04STejun Heo extern bool flush_work(struct work_struct *work); 459f72b8792SJens Axboe extern bool cancel_work(struct work_struct *work); 460401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work); 461401a8d04STejun Heo 462401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork); 46357b30ae7STejun Heo extern bool cancel_delayed_work(struct delayed_work *dwork); 464401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork); 46528e53bddSOleg Nesterov 466dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq, 467dcd989cbSTejun Heo int max_active); 46827d4ee03SLukas Wunner extern struct work_struct *current_work(void); 469e6267616STejun Heo extern bool current_is_workqueue_rescuer(void); 470d84ff051STejun Heo extern bool workqueue_congested(int cpu, struct workqueue_struct *wq); 471dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work); 4723d1cb205STejun Heo extern __printf(1, 2) void set_worker_desc(const char *fmt, ...); 4733d1cb205STejun Heo extern void print_worker_info(const char *log_lvl, struct task_struct *task); 4743494fc30STejun Heo extern void show_workqueue_state(void); 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. 4858425e3d5STejun Heo */ 4868425e3d5STejun Heo static inline bool queue_work(struct workqueue_struct *wq, 4878425e3d5STejun Heo struct work_struct *work) 4888425e3d5STejun Heo { 4898425e3d5STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 4908425e3d5STejun Heo } 4918425e3d5STejun Heo 4928425e3d5STejun Heo /** 4938425e3d5STejun Heo * queue_delayed_work - queue work on a workqueue after delay 4948425e3d5STejun Heo * @wq: workqueue to use 4958425e3d5STejun Heo * @dwork: delayable work to queue 4968425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 4978425e3d5STejun Heo * 4988425e3d5STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 4998425e3d5STejun Heo */ 5008425e3d5STejun Heo static inline bool queue_delayed_work(struct workqueue_struct *wq, 5018425e3d5STejun Heo struct delayed_work *dwork, 5028425e3d5STejun Heo unsigned long delay) 5038425e3d5STejun Heo { 5048425e3d5STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 5058425e3d5STejun Heo } 5068425e3d5STejun Heo 5078425e3d5STejun Heo /** 5088425e3d5STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 5098425e3d5STejun Heo * @wq: workqueue to use 5108425e3d5STejun Heo * @dwork: work to queue 5118425e3d5STejun Heo * @delay: number of jiffies to wait before queueing 5128425e3d5STejun Heo * 5138425e3d5STejun Heo * mod_delayed_work_on() on local CPU. 5148425e3d5STejun Heo */ 5158425e3d5STejun Heo static inline bool mod_delayed_work(struct workqueue_struct *wq, 5168425e3d5STejun Heo struct delayed_work *dwork, 5178425e3d5STejun Heo unsigned long delay) 5188425e3d5STejun Heo { 5198425e3d5STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 5208425e3d5STejun Heo } 5218425e3d5STejun Heo 5228425e3d5STejun Heo /** 5238425e3d5STejun Heo * schedule_work_on - put work task on a specific cpu 5248425e3d5STejun Heo * @cpu: cpu to put the work task on 5258425e3d5STejun Heo * @work: job to be done 5268425e3d5STejun Heo * 5278425e3d5STejun Heo * This puts a job on a specific cpu 5288425e3d5STejun Heo */ 5298425e3d5STejun Heo static inline bool schedule_work_on(int cpu, struct work_struct *work) 5308425e3d5STejun Heo { 5318425e3d5STejun Heo return queue_work_on(cpu, system_wq, work); 5328425e3d5STejun Heo } 5338425e3d5STejun Heo 5348425e3d5STejun Heo /** 5358425e3d5STejun Heo * schedule_work - put work task in global workqueue 5368425e3d5STejun Heo * @work: job to be done 5378425e3d5STejun Heo * 5388425e3d5STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 5398425e3d5STejun Heo * %true otherwise. 5408425e3d5STejun Heo * 5418425e3d5STejun Heo * This puts a job in the kernel-global workqueue if it was not already 5428425e3d5STejun Heo * queued and leaves it in the same position on the kernel-global 5438425e3d5STejun Heo * workqueue otherwise. 5448425e3d5STejun Heo */ 5458425e3d5STejun Heo static inline bool schedule_work(struct work_struct *work) 5468425e3d5STejun Heo { 5478425e3d5STejun Heo return queue_work(system_wq, work); 5488425e3d5STejun Heo } 5498425e3d5STejun Heo 5508425e3d5STejun Heo /** 55137b1ef31SLai Jiangshan * flush_scheduled_work - ensure that any scheduled work has run to completion. 55237b1ef31SLai Jiangshan * 55337b1ef31SLai Jiangshan * Forces execution of the kernel-global workqueue and blocks until its 55437b1ef31SLai Jiangshan * completion. 55537b1ef31SLai Jiangshan * 55637b1ef31SLai Jiangshan * Think twice before calling this function! It's very easy to get into 55737b1ef31SLai Jiangshan * trouble if you don't take great care. Either of the following situations 55837b1ef31SLai Jiangshan * will lead to deadlock: 55937b1ef31SLai Jiangshan * 56037b1ef31SLai Jiangshan * One of the work items currently on the workqueue needs to acquire 56137b1ef31SLai Jiangshan * a lock held by your code or its caller. 56237b1ef31SLai Jiangshan * 56337b1ef31SLai Jiangshan * Your code is running in the context of a work routine. 56437b1ef31SLai Jiangshan * 56537b1ef31SLai Jiangshan * They will be detected by lockdep when they occur, but the first might not 56637b1ef31SLai Jiangshan * occur very often. It depends on what work items are on the workqueue and 56737b1ef31SLai Jiangshan * what locks they need, which you have no control over. 56837b1ef31SLai Jiangshan * 56937b1ef31SLai Jiangshan * In most situations flushing the entire workqueue is overkill; you merely 57037b1ef31SLai Jiangshan * need to know that a particular work item isn't queued and isn't running. 57137b1ef31SLai Jiangshan * In such cases you should use cancel_delayed_work_sync() or 57237b1ef31SLai Jiangshan * cancel_work_sync() instead. 57337b1ef31SLai Jiangshan */ 57437b1ef31SLai Jiangshan static inline void flush_scheduled_work(void) 57537b1ef31SLai Jiangshan { 57637b1ef31SLai Jiangshan flush_workqueue(system_wq); 57737b1ef31SLai Jiangshan } 57837b1ef31SLai Jiangshan 57937b1ef31SLai Jiangshan /** 5808425e3d5STejun Heo * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 5818425e3d5STejun Heo * @cpu: cpu to use 5828425e3d5STejun Heo * @dwork: job to be done 5838425e3d5STejun Heo * @delay: number of jiffies to wait 5848425e3d5STejun Heo * 5858425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 5868425e3d5STejun Heo * workqueue on the specified CPU. 5878425e3d5STejun Heo */ 5888425e3d5STejun Heo static inline bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 5898425e3d5STejun Heo unsigned long delay) 5908425e3d5STejun Heo { 5918425e3d5STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 5928425e3d5STejun Heo } 5938425e3d5STejun Heo 5948425e3d5STejun Heo /** 5958425e3d5STejun Heo * schedule_delayed_work - put work task in global workqueue after delay 5968425e3d5STejun Heo * @dwork: job to be done 5978425e3d5STejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 5988425e3d5STejun Heo * 5998425e3d5STejun Heo * After waiting for a given time this puts a job in the kernel-global 6008425e3d5STejun Heo * workqueue. 6018425e3d5STejun Heo */ 6028425e3d5STejun Heo static inline bool schedule_delayed_work(struct delayed_work *dwork, 6038425e3d5STejun Heo unsigned long delay) 6048425e3d5STejun Heo { 6058425e3d5STejun Heo return queue_delayed_work(system_wq, dwork, delay); 6068425e3d5STejun Heo } 6078425e3d5STejun Heo 6082d3854a3SRusty Russell #ifndef CONFIG_SMP 609d84ff051STejun Heo static inline long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 6102d3854a3SRusty Russell { 6112d3854a3SRusty Russell return fn(arg); 6122d3854a3SRusty Russell } 6130e8d6a93SThomas Gleixner static inline long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 6140e8d6a93SThomas Gleixner { 6150e8d6a93SThomas Gleixner return fn(arg); 6160e8d6a93SThomas Gleixner } 6172d3854a3SRusty Russell #else 618d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg); 6190e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg); 6202d3854a3SRusty Russell #endif /* CONFIG_SMP */ 621a25909a4SPaul E. McKenney 622a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 623a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void); 624a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void); 625a0a1a5fdSTejun Heo extern void thaw_workqueues(void); 626a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 627a0a1a5fdSTejun Heo 628226223abSTejun Heo #ifdef CONFIG_SYSFS 629226223abSTejun Heo int workqueue_sysfs_register(struct workqueue_struct *wq); 630226223abSTejun Heo #else /* CONFIG_SYSFS */ 631226223abSTejun Heo static inline int workqueue_sysfs_register(struct workqueue_struct *wq) 632226223abSTejun Heo { return 0; } 633226223abSTejun Heo #endif /* CONFIG_SYSFS */ 634226223abSTejun Heo 63582607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 63682607adcSTejun Heo void wq_watchdog_touch(int cpu); 63782607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 63882607adcSTejun Heo static inline void wq_watchdog_touch(int cpu) { } 63982607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 64082607adcSTejun Heo 6417ee681b2SThomas Gleixner #ifdef CONFIG_SMP 6427ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu); 6437ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu); 6447ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu); 6457ee681b2SThomas Gleixner #endif 6467ee681b2SThomas Gleixner 6473347fa09STejun Heo int __init workqueue_init_early(void); 6483347fa09STejun Heo int __init workqueue_init(void); 6493347fa09STejun Heo 6501da177e4SLinus Torvalds #endif 651