11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19c54fce6eSTejun Heo * automatically managed. There is one worker pool for each CPU and 20c54fce6eSTejun Heo * one extra for works which are better served by workers which are 21c54fce6eSTejun Heo * not bound to any specific CPU. 22c54fce6eSTejun Heo * 23c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 269984de1aSPaul Gortmaker #include <linux/export.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds #include <linux/signal.h> 311da177e4SLinus Torvalds #include <linux/completion.h> 321da177e4SLinus Torvalds #include <linux/workqueue.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/cpu.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/kthread.h> 371fa44ecaSJames Bottomley #include <linux/hardirq.h> 3846934023SChristoph Lameter #include <linux/mempolicy.h> 39341a5958SRafael J. Wysocki #include <linux/freezer.h> 40d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 4442f8570fSSasha Levin #include <linux/hashtable.h> 45e22bee78STejun Heo 46ea138446STejun Heo #include "workqueue_internal.h" 471da177e4SLinus Torvalds 48c8e55f36STejun Heo enum { 49bc2ae0f5STejun Heo /* 5024647570STejun Heo * worker_pool flags 51bc2ae0f5STejun Heo * 5224647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 53bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 54bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 55bc2ae0f5STejun Heo * is in effect. 56bc2ae0f5STejun Heo * 57bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 58bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 5924647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 60bc2ae0f5STejun Heo * 61bc2ae0f5STejun Heo * Note that DISASSOCIATED can be flipped only while holding 6224647570STejun Heo * assoc_mutex to avoid changing binding state while 6324647570STejun Heo * create_worker() is in progress. 64bc2ae0f5STejun Heo */ 6511ebea50STejun Heo POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ 66552a37e9SLai Jiangshan POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */ 6724647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 6835b6bb63STejun Heo POOL_FREEZING = 1 << 3, /* freeze in progress */ 69db7bccf4STejun Heo 70c8e55f36STejun Heo /* worker flags */ 71c8e55f36STejun Heo WORKER_STARTED = 1 << 0, /* started */ 72c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 73c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 74e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 75fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 76f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 77e22bee78STejun Heo 785f7dabfdSLai Jiangshan WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND | 79403c821dSTejun Heo WORKER_CPU_INTENSIVE, 80db7bccf4STejun Heo 81e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 824ce62e9eSTejun Heo 83c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 84db7bccf4STejun Heo 85e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 86e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 87e22bee78STejun Heo 883233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 893233cdbdSTejun Heo /* call for help after 10ms 903233cdbdSTejun Heo (min two ticks) */ 91e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 92e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* 95e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 96e22bee78STejun Heo * all cpus. Give -20. 97e22bee78STejun Heo */ 98e22bee78STejun Heo RESCUER_NICE_LEVEL = -20, 993270476aSTejun Heo HIGHPRI_NICE_LEVEL = -20, 100c8e55f36STejun Heo }; 101c8e55f36STejun Heo 1021da177e4SLinus Torvalds /* 1034690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1044690c4abSTejun Heo * 105e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 106e41e704bSTejun Heo * everyone else. 1074690c4abSTejun Heo * 108e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 109e22bee78STejun Heo * only be modified and accessed from the local cpu. 110e22bee78STejun Heo * 111d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1124690c4abSTejun Heo * 113d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 114d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 115d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 116d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 117e22bee78STejun Heo * 11873f53c4aSTejun Heo * F: wq->flush_mutex protected. 11973f53c4aSTejun Heo * 1204690c4abSTejun Heo * W: workqueue_lock protected. 1214690c4abSTejun Heo */ 1224690c4abSTejun Heo 1232eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 124c34056a3STejun Heo 125bd7bdd43STejun Heo struct worker_pool { 126d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 127d84ff051STejun Heo int cpu; /* I: the associated cpu */ 1289daf9e67STejun Heo int id; /* I: pool ID */ 12911ebea50STejun Heo unsigned int flags; /* X: flags */ 130bd7bdd43STejun Heo 131bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 132bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 133ea1abd61SLai Jiangshan 134ea1abd61SLai Jiangshan /* nr_idle includes the ones off idle_list for rebinding */ 135bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 136bd7bdd43STejun Heo 137bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 138bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 139bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 140bd7bdd43STejun Heo 141c9e7cf27STejun Heo /* workers are chained either in busy_hash or idle_list */ 142c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 143c9e7cf27STejun Heo /* L: hash of busy workers */ 144c9e7cf27STejun Heo 14524647570STejun Heo struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */ 146bd7bdd43STejun Heo struct ida worker_ida; /* L: for worker IDs */ 147e19e397aSTejun Heo 148e19e397aSTejun Heo /* 149e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 150e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 151e19e397aSTejun Heo * cacheline. 152e19e397aSTejun Heo */ 153e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 1548b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1558b03ae3cSTejun Heo 1568b03ae3cSTejun Heo /* 157112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 158112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 159112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 160112202d9STejun Heo * number of flag bits. 1611da177e4SLinus Torvalds */ 162112202d9STejun Heo struct pool_workqueue { 163bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1644690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 16573f53c4aSTejun Heo int work_color; /* L: current color */ 16673f53c4aSTejun Heo int flush_color; /* L: flushing color */ 16773f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 16873f53c4aSTejun Heo /* L: nr of in_flight works */ 1691e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 170a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 1711e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 17230cdf249STejun Heo struct list_head pwqs_node; /* I: node on wq->pwqs */ 173493a1724STejun Heo struct list_head mayday_node; /* W: node on wq->maydays */ 174e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* 17773f53c4aSTejun Heo * Structure used to wait for workqueue flush. 17873f53c4aSTejun Heo */ 17973f53c4aSTejun Heo struct wq_flusher { 18073f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 18173f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 18273f53c4aSTejun Heo struct completion done; /* flush completion */ 18373f53c4aSTejun Heo }; 1841da177e4SLinus Torvalds 18573f53c4aSTejun Heo /* 1861da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 1871da177e4SLinus Torvalds * per-CPU workqueues: 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds struct workqueue_struct { 1909c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 191*420c0ddbSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */ 19230cdf249STejun Heo struct list_head pwqs; /* I: all pwqs of this wq */ 1934690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 19473f53c4aSTejun Heo 19573f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 19673f53c4aSTejun Heo int work_color; /* F: current work color */ 19773f53c4aSTejun Heo int flush_color; /* F: current flush color */ 198112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 19973f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 20073f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 20173f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 20273f53c4aSTejun Heo 203493a1724STejun Heo struct list_head maydays; /* W: pwqs requesting rescue */ 204e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 205e22bee78STejun Heo 2069c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 207112202d9STejun Heo int saved_max_active; /* W: saved pwq max_active */ 2084e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2094e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2104e6045f1SJohannes Berg #endif 211b196be89STejun Heo char name[]; /* I: workqueue name */ 2121da177e4SLinus Torvalds }; 2131da177e4SLinus Torvalds 214e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 215e904e6c2STejun Heo 216d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 217d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 218044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 2191aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 220044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 221d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 222044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 223f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 224044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 22524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 226d320c038STejun Heo 22797bd2347STejun Heo #define CREATE_TRACE_POINTS 22897bd2347STejun Heo #include <trace/events/workqueue.h> 22997bd2347STejun Heo 23038db41d9STejun Heo #define for_each_std_worker_pool(pool, cpu) \ 231a60dc39cSTejun Heo for ((pool) = &std_worker_pools(cpu)[0]; \ 232a60dc39cSTejun Heo (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++) 2334ce62e9eSTejun Heo 234b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool) \ 235b67bfe0dSSasha Levin hash_for_each(pool->busy_hash, i, worker, hentry) 236db7bccf4STejun Heo 237706026c2STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 238f3421797STejun Heo unsigned int sw) 239f3421797STejun Heo { 240f3421797STejun Heo if (cpu < nr_cpu_ids) { 241f3421797STejun Heo if (sw & 1) { 242f3421797STejun Heo cpu = cpumask_next(cpu, mask); 243f3421797STejun Heo if (cpu < nr_cpu_ids) 244f3421797STejun Heo return cpu; 245f3421797STejun Heo } 246f3421797STejun Heo if (sw & 2) 247f3421797STejun Heo return WORK_CPU_UNBOUND; 248f3421797STejun Heo } 2496be19588SLai Jiangshan return WORK_CPU_END; 250f3421797STejun Heo } 251f3421797STejun Heo 25209884951STejun Heo /* 25309884951STejun Heo * CPU iterators 25409884951STejun Heo * 255706026c2STejun Heo * An extra cpu number is defined using an invalid cpu number 25609884951STejun Heo * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 257706026c2STejun Heo * specific CPU. The following iterators are similar to for_each_*_cpu() 258706026c2STejun Heo * iterators but also considers the unbound CPU. 25909884951STejun Heo * 260706026c2STejun Heo * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND 261706026c2STejun Heo * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND 26209884951STejun Heo */ 263706026c2STejun Heo #define for_each_wq_cpu(cpu) \ 264706026c2STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \ 2656be19588SLai Jiangshan (cpu) < WORK_CPU_END; \ 266706026c2STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3)) 267f3421797STejun Heo 268706026c2STejun Heo #define for_each_online_wq_cpu(cpu) \ 269706026c2STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \ 2706be19588SLai Jiangshan (cpu) < WORK_CPU_END; \ 271706026c2STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3)) 272f3421797STejun Heo 27349e3cf44STejun Heo /** 27417116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 27517116969STejun Heo * @pool: iteration cursor 27617116969STejun Heo * @id: integer used for iteration 27717116969STejun Heo */ 27817116969STejun Heo #define for_each_pool(pool, id) \ 27917116969STejun Heo idr_for_each_entry(&worker_pool_idr, pool, id) 28017116969STejun Heo 28117116969STejun Heo /** 28249e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 28349e3cf44STejun Heo * @pwq: iteration cursor 28449e3cf44STejun Heo * @wq: the target workqueue 28549e3cf44STejun Heo */ 28649e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 28749e3cf44STejun Heo list_for_each_entry((pwq), &(wq)->pwqs, pwqs_node) 288f3421797STejun Heo 289dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 290dc186ad7SThomas Gleixner 291dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 292dc186ad7SThomas Gleixner 29399777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 29499777288SStanislaw Gruszka { 29599777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 29699777288SStanislaw Gruszka } 29799777288SStanislaw Gruszka 298dc186ad7SThomas Gleixner /* 299dc186ad7SThomas Gleixner * fixup_init is called when: 300dc186ad7SThomas Gleixner * - an active object is initialized 301dc186ad7SThomas Gleixner */ 302dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 303dc186ad7SThomas Gleixner { 304dc186ad7SThomas Gleixner struct work_struct *work = addr; 305dc186ad7SThomas Gleixner 306dc186ad7SThomas Gleixner switch (state) { 307dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 308dc186ad7SThomas Gleixner cancel_work_sync(work); 309dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 310dc186ad7SThomas Gleixner return 1; 311dc186ad7SThomas Gleixner default: 312dc186ad7SThomas Gleixner return 0; 313dc186ad7SThomas Gleixner } 314dc186ad7SThomas Gleixner } 315dc186ad7SThomas Gleixner 316dc186ad7SThomas Gleixner /* 317dc186ad7SThomas Gleixner * fixup_activate is called when: 318dc186ad7SThomas Gleixner * - an active object is activated 319dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 320dc186ad7SThomas Gleixner */ 321dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 322dc186ad7SThomas Gleixner { 323dc186ad7SThomas Gleixner struct work_struct *work = addr; 324dc186ad7SThomas Gleixner 325dc186ad7SThomas Gleixner switch (state) { 326dc186ad7SThomas Gleixner 327dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 328dc186ad7SThomas Gleixner /* 329dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 330dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 331dc186ad7SThomas Gleixner * is tracked in the object tracker. 332dc186ad7SThomas Gleixner */ 33322df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 334dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 335dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 336dc186ad7SThomas Gleixner return 0; 337dc186ad7SThomas Gleixner } 338dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 339dc186ad7SThomas Gleixner return 0; 340dc186ad7SThomas Gleixner 341dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 342dc186ad7SThomas Gleixner WARN_ON(1); 343dc186ad7SThomas Gleixner 344dc186ad7SThomas Gleixner default: 345dc186ad7SThomas Gleixner return 0; 346dc186ad7SThomas Gleixner } 347dc186ad7SThomas Gleixner } 348dc186ad7SThomas Gleixner 349dc186ad7SThomas Gleixner /* 350dc186ad7SThomas Gleixner * fixup_free is called when: 351dc186ad7SThomas Gleixner * - an active object is freed 352dc186ad7SThomas Gleixner */ 353dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 354dc186ad7SThomas Gleixner { 355dc186ad7SThomas Gleixner struct work_struct *work = addr; 356dc186ad7SThomas Gleixner 357dc186ad7SThomas Gleixner switch (state) { 358dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 359dc186ad7SThomas Gleixner cancel_work_sync(work); 360dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 361dc186ad7SThomas Gleixner return 1; 362dc186ad7SThomas Gleixner default: 363dc186ad7SThomas Gleixner return 0; 364dc186ad7SThomas Gleixner } 365dc186ad7SThomas Gleixner } 366dc186ad7SThomas Gleixner 367dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 368dc186ad7SThomas Gleixner .name = "work_struct", 36999777288SStanislaw Gruszka .debug_hint = work_debug_hint, 370dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 371dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 372dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 373dc186ad7SThomas Gleixner }; 374dc186ad7SThomas Gleixner 375dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 376dc186ad7SThomas Gleixner { 377dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 378dc186ad7SThomas Gleixner } 379dc186ad7SThomas Gleixner 380dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 381dc186ad7SThomas Gleixner { 382dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 383dc186ad7SThomas Gleixner } 384dc186ad7SThomas Gleixner 385dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 386dc186ad7SThomas Gleixner { 387dc186ad7SThomas Gleixner if (onstack) 388dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 389dc186ad7SThomas Gleixner else 390dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 391dc186ad7SThomas Gleixner } 392dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 393dc186ad7SThomas Gleixner 394dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 395dc186ad7SThomas Gleixner { 396dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 397dc186ad7SThomas Gleixner } 398dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 399dc186ad7SThomas Gleixner 400dc186ad7SThomas Gleixner #else 401dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 402dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 403dc186ad7SThomas Gleixner #endif 404dc186ad7SThomas Gleixner 40595402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 40695402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4071da177e4SLinus Torvalds static LIST_HEAD(workqueues); 408a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4091da177e4SLinus Torvalds 41014441960SOleg Nesterov /* 411e19e397aSTejun Heo * The CPU and unbound standard worker pools. The unbound ones have 412e19e397aSTejun Heo * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set. 41314441960SOleg Nesterov */ 414e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 415a60dc39cSTejun Heo cpu_std_worker_pools); 416a60dc39cSTejun Heo static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS]; 417f3421797STejun Heo 4189daf9e67STejun Heo /* idr of all pools */ 4199daf9e67STejun Heo static DEFINE_MUTEX(worker_pool_idr_mutex); 4209daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr); 4219daf9e67STejun Heo 422c34056a3STejun Heo static int worker_thread(void *__worker); 4231da177e4SLinus Torvalds 424a60dc39cSTejun Heo static struct worker_pool *std_worker_pools(int cpu) 4251da177e4SLinus Torvalds { 426f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 427a60dc39cSTejun Heo return per_cpu(cpu_std_worker_pools, cpu); 428f3421797STejun Heo else 429a60dc39cSTejun Heo return unbound_std_worker_pools; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 4324e8f0a60STejun Heo static int std_worker_pool_pri(struct worker_pool *pool) 4334e8f0a60STejun Heo { 434a60dc39cSTejun Heo return pool - std_worker_pools(pool->cpu); 4354e8f0a60STejun Heo } 4364e8f0a60STejun Heo 4379daf9e67STejun Heo /* allocate ID and assign it to @pool */ 4389daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 4399daf9e67STejun Heo { 4409daf9e67STejun Heo int ret; 4419daf9e67STejun Heo 4429daf9e67STejun Heo mutex_lock(&worker_pool_idr_mutex); 4439daf9e67STejun Heo idr_pre_get(&worker_pool_idr, GFP_KERNEL); 4449daf9e67STejun Heo ret = idr_get_new(&worker_pool_idr, pool, &pool->id); 4459daf9e67STejun Heo mutex_unlock(&worker_pool_idr_mutex); 4469daf9e67STejun Heo 4479daf9e67STejun Heo return ret; 4489daf9e67STejun Heo } 4499daf9e67STejun Heo 4507c3eed5cSTejun Heo /* 4517c3eed5cSTejun Heo * Lookup worker_pool by id. The idr currently is built during boot and 4527c3eed5cSTejun Heo * never modified. Don't worry about locking for now. 4537c3eed5cSTejun Heo */ 4547c3eed5cSTejun Heo static struct worker_pool *worker_pool_by_id(int pool_id) 4557c3eed5cSTejun Heo { 4567c3eed5cSTejun Heo return idr_find(&worker_pool_idr, pool_id); 4577c3eed5cSTejun Heo } 4587c3eed5cSTejun Heo 459d565ed63STejun Heo static struct worker_pool *get_std_worker_pool(int cpu, bool highpri) 460d565ed63STejun Heo { 461a60dc39cSTejun Heo struct worker_pool *pools = std_worker_pools(cpu); 462d565ed63STejun Heo 463a60dc39cSTejun Heo return &pools[highpri]; 464d565ed63STejun Heo } 465d565ed63STejun Heo 466d84ff051STejun Heo static struct pool_workqueue *get_pwq(int cpu, struct workqueue_struct *wq) 467a848e3b6SOleg Nesterov { 468f3421797STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 469e06ffa1eSLai Jiangshan if (likely(cpu < nr_cpu_ids)) 470*420c0ddbSTejun Heo return per_cpu_ptr(wq->cpu_pwqs, cpu); 471*420c0ddbSTejun Heo } else if (likely(cpu == WORK_CPU_UNBOUND)) { 472*420c0ddbSTejun Heo return list_first_entry(&wq->pwqs, struct pool_workqueue, 473*420c0ddbSTejun Heo pwqs_node); 474*420c0ddbSTejun Heo } 475f3421797STejun Heo return NULL; 476f3421797STejun Heo } 477a848e3b6SOleg Nesterov 47873f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 47973f53c4aSTejun Heo { 48073f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 48173f53c4aSTejun Heo } 48273f53c4aSTejun Heo 48373f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 48473f53c4aSTejun Heo { 48573f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 48673f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 48773f53c4aSTejun Heo } 48873f53c4aSTejun Heo 48973f53c4aSTejun Heo static int work_next_color(int color) 49073f53c4aSTejun Heo { 49173f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 492b1f4ec17SOleg Nesterov } 493b1f4ec17SOleg Nesterov 4944594bf15SDavid Howells /* 495112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 496112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 4977c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 4987a22ad75STejun Heo * 499112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 500112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 501bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 502bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 5037a22ad75STejun Heo * 504112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 5057c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 506112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 5077c3eed5cSTejun Heo * available only while the work item is queued. 508bbb68dfaSTejun Heo * 509bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 510bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 511bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 512bbb68dfaSTejun Heo * try to steal the PENDING bit. 5134594bf15SDavid Howells */ 5147a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5157a22ad75STejun Heo unsigned long flags) 5167a22ad75STejun Heo { 5176183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 5187a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5197a22ad75STejun Heo } 5207a22ad75STejun Heo 521112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 5224690c4abSTejun Heo unsigned long extra_flags) 523365970a1SDavid Howells { 524112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 525112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 526365970a1SDavid Howells } 527365970a1SDavid Howells 5284468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 5294468a00fSLai Jiangshan int pool_id) 5304468a00fSLai Jiangshan { 5314468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 5324468a00fSLai Jiangshan WORK_STRUCT_PENDING); 5334468a00fSLai Jiangshan } 5344468a00fSLai Jiangshan 5357c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 5367c3eed5cSTejun Heo int pool_id) 5374d707b9fSOleg Nesterov { 53823657bb1STejun Heo /* 53923657bb1STejun Heo * The following wmb is paired with the implied mb in 54023657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 54123657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 54223657bb1STejun Heo * owner. 54323657bb1STejun Heo */ 54423657bb1STejun Heo smp_wmb(); 5457c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 5464d707b9fSOleg Nesterov } 5474d707b9fSOleg Nesterov 5487a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 549365970a1SDavid Howells { 5507c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 5517c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 5527a22ad75STejun Heo } 5537a22ad75STejun Heo 554112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 5557a22ad75STejun Heo { 556e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5577a22ad75STejun Heo 558112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 559e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 560e120153dSTejun Heo else 561e120153dSTejun Heo return NULL; 5627a22ad75STejun Heo } 5637a22ad75STejun Heo 5647c3eed5cSTejun Heo /** 5657c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 5667c3eed5cSTejun Heo * @work: the work item of interest 5677c3eed5cSTejun Heo * 5687c3eed5cSTejun Heo * Return the worker_pool @work was last associated with. %NULL if none. 5697c3eed5cSTejun Heo */ 5707c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 5717a22ad75STejun Heo { 572e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5737c3eed5cSTejun Heo struct worker_pool *pool; 5747c3eed5cSTejun Heo int pool_id; 5757a22ad75STejun Heo 576112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 577112202d9STejun Heo return ((struct pool_workqueue *) 5787c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 5797a22ad75STejun Heo 5807c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 5817c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 5827a22ad75STejun Heo return NULL; 5837a22ad75STejun Heo 5847c3eed5cSTejun Heo pool = worker_pool_by_id(pool_id); 5857c3eed5cSTejun Heo WARN_ON_ONCE(!pool); 5867c3eed5cSTejun Heo return pool; 5877c3eed5cSTejun Heo } 5887c3eed5cSTejun Heo 5897c3eed5cSTejun Heo /** 5907c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 5917c3eed5cSTejun Heo * @work: the work item of interest 5927c3eed5cSTejun Heo * 5937c3eed5cSTejun Heo * Return the worker_pool ID @work was last associated with. 5947c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 5957c3eed5cSTejun Heo */ 5967c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 5977c3eed5cSTejun Heo { 59854d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 5997c3eed5cSTejun Heo 600112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 601112202d9STejun Heo return ((struct pool_workqueue *) 60254d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 60354d5b7d0SLai Jiangshan 60454d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 6057c3eed5cSTejun Heo } 6067c3eed5cSTejun Heo 607bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 608bbb68dfaSTejun Heo { 6097c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 610bbb68dfaSTejun Heo 6117c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 6127c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 613bbb68dfaSTejun Heo } 614bbb68dfaSTejun Heo 615bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 616bbb68dfaSTejun Heo { 617bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 618bbb68dfaSTejun Heo 619112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 620bbb68dfaSTejun Heo } 621bbb68dfaSTejun Heo 622e22bee78STejun Heo /* 6233270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6243270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 625d565ed63STejun Heo * they're being called with pool->lock held. 626e22bee78STejun Heo */ 627e22bee78STejun Heo 62863d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 629649027d7STejun Heo { 630e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 631649027d7STejun Heo } 632649027d7STejun Heo 633e22bee78STejun Heo /* 634e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 635e22bee78STejun Heo * running workers. 636974271c4STejun Heo * 637974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 638706026c2STejun Heo * function will always return %true for unbound pools as long as the 639974271c4STejun Heo * worklist isn't empty. 640e22bee78STejun Heo */ 64163d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 642e22bee78STejun Heo { 64363d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 644e22bee78STejun Heo } 645e22bee78STejun Heo 646e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 64763d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 648e22bee78STejun Heo { 64963d95a91STejun Heo return pool->nr_idle; 650e22bee78STejun Heo } 651e22bee78STejun Heo 652e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 65363d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 654e22bee78STejun Heo { 655e19e397aSTejun Heo return !list_empty(&pool->worklist) && 656e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 657e22bee78STejun Heo } 658e22bee78STejun Heo 659e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 66063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 661e22bee78STejun Heo { 66263d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 663e22bee78STejun Heo } 664e22bee78STejun Heo 665e22bee78STejun Heo /* Do I need to be the manager? */ 66663d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 667e22bee78STejun Heo { 66863d95a91STejun Heo return need_to_create_worker(pool) || 66911ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 670e22bee78STejun Heo } 671e22bee78STejun Heo 672e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 67363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 674e22bee78STejun Heo { 675552a37e9SLai Jiangshan bool managing = pool->flags & POOL_MANAGING_WORKERS; 67663d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 67763d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 678e22bee78STejun Heo 679ea1abd61SLai Jiangshan /* 680ea1abd61SLai Jiangshan * nr_idle and idle_list may disagree if idle rebinding is in 681ea1abd61SLai Jiangshan * progress. Never return %true if idle_list is empty. 682ea1abd61SLai Jiangshan */ 683ea1abd61SLai Jiangshan if (list_empty(&pool->idle_list)) 684ea1abd61SLai Jiangshan return false; 685ea1abd61SLai Jiangshan 686e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 687e22bee78STejun Heo } 688e22bee78STejun Heo 689e22bee78STejun Heo /* 690e22bee78STejun Heo * Wake up functions. 691e22bee78STejun Heo */ 692e22bee78STejun Heo 6937e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 69463d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6957e11629dSTejun Heo { 69663d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6977e11629dSTejun Heo return NULL; 6987e11629dSTejun Heo 69963d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 7007e11629dSTejun Heo } 7017e11629dSTejun Heo 7027e11629dSTejun Heo /** 7037e11629dSTejun Heo * wake_up_worker - wake up an idle worker 70463d95a91STejun Heo * @pool: worker pool to wake worker from 7057e11629dSTejun Heo * 70663d95a91STejun Heo * Wake up the first idle worker of @pool. 7077e11629dSTejun Heo * 7087e11629dSTejun Heo * CONTEXT: 709d565ed63STejun Heo * spin_lock_irq(pool->lock). 7107e11629dSTejun Heo */ 71163d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 7127e11629dSTejun Heo { 71363d95a91STejun Heo struct worker *worker = first_worker(pool); 7147e11629dSTejun Heo 7157e11629dSTejun Heo if (likely(worker)) 7167e11629dSTejun Heo wake_up_process(worker->task); 7177e11629dSTejun Heo } 7187e11629dSTejun Heo 7194690c4abSTejun Heo /** 720e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 721e22bee78STejun Heo * @task: task waking up 722e22bee78STejun Heo * @cpu: CPU @task is waking up to 723e22bee78STejun Heo * 724e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 725e22bee78STejun Heo * being awoken. 726e22bee78STejun Heo * 727e22bee78STejun Heo * CONTEXT: 728e22bee78STejun Heo * spin_lock_irq(rq->lock) 729e22bee78STejun Heo */ 730d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 731e22bee78STejun Heo { 732e22bee78STejun Heo struct worker *worker = kthread_data(task); 733e22bee78STejun Heo 73436576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 735ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 736e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 737e22bee78STejun Heo } 73836576000SJoonsoo Kim } 739e22bee78STejun Heo 740e22bee78STejun Heo /** 741e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 742e22bee78STejun Heo * @task: task going to sleep 743e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 744e22bee78STejun Heo * 745e22bee78STejun Heo * This function is called during schedule() when a busy worker is 746e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 747e22bee78STejun Heo * returning pointer to its task. 748e22bee78STejun Heo * 749e22bee78STejun Heo * CONTEXT: 750e22bee78STejun Heo * spin_lock_irq(rq->lock) 751e22bee78STejun Heo * 752e22bee78STejun Heo * RETURNS: 753e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 754e22bee78STejun Heo */ 755d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) 756e22bee78STejun Heo { 757e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 758111c225aSTejun Heo struct worker_pool *pool; 759e22bee78STejun Heo 760111c225aSTejun Heo /* 761111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 762111c225aSTejun Heo * workers, also reach here, let's not access anything before 763111c225aSTejun Heo * checking NOT_RUNNING. 764111c225aSTejun Heo */ 7652d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 766e22bee78STejun Heo return NULL; 767e22bee78STejun Heo 768111c225aSTejun Heo pool = worker->pool; 769111c225aSTejun Heo 770e22bee78STejun Heo /* this can only happen on the local cpu */ 7716183c009STejun Heo if (WARN_ON_ONCE(cpu != raw_smp_processor_id())) 7726183c009STejun Heo return NULL; 773e22bee78STejun Heo 774e22bee78STejun Heo /* 775e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 776e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 777e22bee78STejun Heo * Please read comment there. 778e22bee78STejun Heo * 779628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 780628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 781628c78e7STejun Heo * disabled, which in turn means that none else could be 782d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 783628c78e7STejun Heo * lock is safe. 784e22bee78STejun Heo */ 785e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 786e19e397aSTejun Heo !list_empty(&pool->worklist)) 78763d95a91STejun Heo to_wakeup = first_worker(pool); 788e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 789e22bee78STejun Heo } 790e22bee78STejun Heo 791e22bee78STejun Heo /** 792e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 793cb444766STejun Heo * @worker: self 794d302f017STejun Heo * @flags: flags to set 795d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 796d302f017STejun Heo * 797e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 798e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 799e22bee78STejun Heo * woken up. 800d302f017STejun Heo * 801cb444766STejun Heo * CONTEXT: 802d565ed63STejun Heo * spin_lock_irq(pool->lock) 803d302f017STejun Heo */ 804d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 805d302f017STejun Heo bool wakeup) 806d302f017STejun Heo { 807bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 808e22bee78STejun Heo 809cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 810cb444766STejun Heo 811e22bee78STejun Heo /* 812e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 813e22bee78STejun Heo * wake up an idle worker as necessary if requested by 814e22bee78STejun Heo * @wakeup. 815e22bee78STejun Heo */ 816e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 817e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 818e22bee78STejun Heo if (wakeup) { 819e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 820bd7bdd43STejun Heo !list_empty(&pool->worklist)) 82163d95a91STejun Heo wake_up_worker(pool); 822e22bee78STejun Heo } else 823e19e397aSTejun Heo atomic_dec(&pool->nr_running); 824e22bee78STejun Heo } 825e22bee78STejun Heo 826d302f017STejun Heo worker->flags |= flags; 827d302f017STejun Heo } 828d302f017STejun Heo 829d302f017STejun Heo /** 830e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 831cb444766STejun Heo * @worker: self 832d302f017STejun Heo * @flags: flags to clear 833d302f017STejun Heo * 834e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 835d302f017STejun Heo * 836cb444766STejun Heo * CONTEXT: 837d565ed63STejun Heo * spin_lock_irq(pool->lock) 838d302f017STejun Heo */ 839d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 840d302f017STejun Heo { 84163d95a91STejun Heo struct worker_pool *pool = worker->pool; 842e22bee78STejun Heo unsigned int oflags = worker->flags; 843e22bee78STejun Heo 844cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 845cb444766STejun Heo 846d302f017STejun Heo worker->flags &= ~flags; 847e22bee78STejun Heo 84842c025f3STejun Heo /* 84942c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 85042c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 85142c025f3STejun Heo * of multiple flags, not a single flag. 85242c025f3STejun Heo */ 853e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 854e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 855e19e397aSTejun Heo atomic_inc(&pool->nr_running); 856d302f017STejun Heo } 857d302f017STejun Heo 858d302f017STejun Heo /** 8598cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 860c9e7cf27STejun Heo * @pool: pool of interest 8618cca0eeaSTejun Heo * @work: work to find worker for 8628cca0eeaSTejun Heo * 863c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 864c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 865a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 866a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 867a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 868a2c1c57bSTejun Heo * being executed. 869a2c1c57bSTejun Heo * 870a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 871a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 872a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 873a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 874a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 875a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 876a2c1c57bSTejun Heo * 877a2c1c57bSTejun Heo * This function checks the work item address, work function and workqueue 878a2c1c57bSTejun Heo * to avoid false positives. Note that this isn't complete as one may 879a2c1c57bSTejun Heo * construct a work function which can introduce dependency onto itself 880a2c1c57bSTejun Heo * through a recycled work item. Well, if somebody wants to shoot oneself 881a2c1c57bSTejun Heo * in the foot that badly, there's only so much we can do, and if such 882a2c1c57bSTejun Heo * deadlock actually occurs, it should be easy to locate the culprit work 883a2c1c57bSTejun Heo * function. 8848cca0eeaSTejun Heo * 8858cca0eeaSTejun Heo * CONTEXT: 886d565ed63STejun Heo * spin_lock_irq(pool->lock). 8878cca0eeaSTejun Heo * 8888cca0eeaSTejun Heo * RETURNS: 8898cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8908cca0eeaSTejun Heo * otherwise. 8918cca0eeaSTejun Heo */ 892c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 8938cca0eeaSTejun Heo struct work_struct *work) 8948cca0eeaSTejun Heo { 89542f8570fSSasha Levin struct worker *worker; 89642f8570fSSasha Levin 897b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 898a2c1c57bSTejun Heo (unsigned long)work) 899a2c1c57bSTejun Heo if (worker->current_work == work && 900a2c1c57bSTejun Heo worker->current_func == work->func) 90142f8570fSSasha Levin return worker; 90242f8570fSSasha Levin 90342f8570fSSasha Levin return NULL; 9048cca0eeaSTejun Heo } 9058cca0eeaSTejun Heo 9068cca0eeaSTejun Heo /** 907bf4ede01STejun Heo * move_linked_works - move linked works to a list 908bf4ede01STejun Heo * @work: start of series of works to be scheduled 909bf4ede01STejun Heo * @head: target list to append @work to 910bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 911bf4ede01STejun Heo * 912bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 913bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 914bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 915bf4ede01STejun Heo * 916bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 917bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 918bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 919bf4ede01STejun Heo * 920bf4ede01STejun Heo * CONTEXT: 921d565ed63STejun Heo * spin_lock_irq(pool->lock). 922bf4ede01STejun Heo */ 923bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 924bf4ede01STejun Heo struct work_struct **nextp) 925bf4ede01STejun Heo { 926bf4ede01STejun Heo struct work_struct *n; 927bf4ede01STejun Heo 928bf4ede01STejun Heo /* 929bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 930bf4ede01STejun Heo * use NULL for list head. 931bf4ede01STejun Heo */ 932bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 933bf4ede01STejun Heo list_move_tail(&work->entry, head); 934bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 935bf4ede01STejun Heo break; 936bf4ede01STejun Heo } 937bf4ede01STejun Heo 938bf4ede01STejun Heo /* 939bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 940bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 941bf4ede01STejun Heo * needs to be updated. 942bf4ede01STejun Heo */ 943bf4ede01STejun Heo if (nextp) 944bf4ede01STejun Heo *nextp = n; 945bf4ede01STejun Heo } 946bf4ede01STejun Heo 947112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 948bf4ede01STejun Heo { 949112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 950bf4ede01STejun Heo 951bf4ede01STejun Heo trace_workqueue_activate_work(work); 952112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 953bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 954112202d9STejun Heo pwq->nr_active++; 955bf4ede01STejun Heo } 956bf4ede01STejun Heo 957112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 9583aa62497SLai Jiangshan { 959112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 9603aa62497SLai Jiangshan struct work_struct, entry); 9613aa62497SLai Jiangshan 962112202d9STejun Heo pwq_activate_delayed_work(work); 9633aa62497SLai Jiangshan } 9643aa62497SLai Jiangshan 965bf4ede01STejun Heo /** 966112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 967112202d9STejun Heo * @pwq: pwq of interest 968bf4ede01STejun Heo * @color: color of work which left the queue 969bf4ede01STejun Heo * 970bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 971112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 972bf4ede01STejun Heo * 973bf4ede01STejun Heo * CONTEXT: 974d565ed63STejun Heo * spin_lock_irq(pool->lock). 975bf4ede01STejun Heo */ 976112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 977bf4ede01STejun Heo { 978bf4ede01STejun Heo /* ignore uncolored works */ 979bf4ede01STejun Heo if (color == WORK_NO_COLOR) 980bf4ede01STejun Heo return; 981bf4ede01STejun Heo 982112202d9STejun Heo pwq->nr_in_flight[color]--; 983bf4ede01STejun Heo 984112202d9STejun Heo pwq->nr_active--; 985112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 986bf4ede01STejun Heo /* one down, submit a delayed one */ 987112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 988112202d9STejun Heo pwq_activate_first_delayed(pwq); 989bf4ede01STejun Heo } 990bf4ede01STejun Heo 991bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 992112202d9STejun Heo if (likely(pwq->flush_color != color)) 993bf4ede01STejun Heo return; 994bf4ede01STejun Heo 995bf4ede01STejun Heo /* are there still in-flight works? */ 996112202d9STejun Heo if (pwq->nr_in_flight[color]) 997bf4ede01STejun Heo return; 998bf4ede01STejun Heo 999112202d9STejun Heo /* this pwq is done, clear flush_color */ 1000112202d9STejun Heo pwq->flush_color = -1; 1001bf4ede01STejun Heo 1002bf4ede01STejun Heo /* 1003112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1004bf4ede01STejun Heo * will handle the rest. 1005bf4ede01STejun Heo */ 1006112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1007112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 1008bf4ede01STejun Heo } 1009bf4ede01STejun Heo 101036e227d2STejun Heo /** 1011bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 101236e227d2STejun Heo * @work: work item to steal 101336e227d2STejun Heo * @is_dwork: @work is a delayed_work 1014bbb68dfaSTejun Heo * @flags: place to store irq state 101536e227d2STejun Heo * 101636e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 101736e227d2STejun Heo * stable state - idle, on timer or on worklist. Return values are 101836e227d2STejun Heo * 101936e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 102036e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 102136e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1022bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1023bbb68dfaSTejun Heo * for arbitrarily long 102436e227d2STejun Heo * 1025bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1026e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1027e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1028e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1029bbb68dfaSTejun Heo * 1030bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1031bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1032bbb68dfaSTejun Heo * 1033e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1034bf4ede01STejun Heo */ 1035bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1036bbb68dfaSTejun Heo unsigned long *flags) 1037bf4ede01STejun Heo { 1038d565ed63STejun Heo struct worker_pool *pool; 1039112202d9STejun Heo struct pool_workqueue *pwq; 1040bf4ede01STejun Heo 1041bbb68dfaSTejun Heo local_irq_save(*flags); 1042bbb68dfaSTejun Heo 104336e227d2STejun Heo /* try to steal the timer if it exists */ 104436e227d2STejun Heo if (is_dwork) { 104536e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 104636e227d2STejun Heo 1047e0aecdd8STejun Heo /* 1048e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1049e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1050e0aecdd8STejun Heo * running on the local CPU. 1051e0aecdd8STejun Heo */ 105236e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 105336e227d2STejun Heo return 1; 105436e227d2STejun Heo } 105536e227d2STejun Heo 105636e227d2STejun Heo /* try to claim PENDING the normal way */ 1057bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1058bf4ede01STejun Heo return 0; 1059bf4ede01STejun Heo 1060bf4ede01STejun Heo /* 1061bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1062bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1063bf4ede01STejun Heo */ 1064d565ed63STejun Heo pool = get_work_pool(work); 1065d565ed63STejun Heo if (!pool) 1066bbb68dfaSTejun Heo goto fail; 1067bf4ede01STejun Heo 1068d565ed63STejun Heo spin_lock(&pool->lock); 1069bf4ede01STejun Heo /* 1070112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1071112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1072112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1073112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1074112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 10750b3dae68SLai Jiangshan * item is currently queued on that pool. 1076bf4ede01STejun Heo */ 1077112202d9STejun Heo pwq = get_work_pwq(work); 1078112202d9STejun Heo if (pwq && pwq->pool == pool) { 1079bf4ede01STejun Heo debug_work_deactivate(work); 10803aa62497SLai Jiangshan 10813aa62497SLai Jiangshan /* 108216062836STejun Heo * A delayed work item cannot be grabbed directly because 108316062836STejun Heo * it might have linked NO_COLOR work items which, if left 1084112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 108516062836STejun Heo * management later on and cause stall. Make sure the work 108616062836STejun Heo * item is activated before grabbing. 10873aa62497SLai Jiangshan */ 10883aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1089112202d9STejun Heo pwq_activate_delayed_work(work); 10903aa62497SLai Jiangshan 1091bf4ede01STejun Heo list_del_init(&work->entry); 1092112202d9STejun Heo pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work)); 109336e227d2STejun Heo 1094112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 10954468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 10964468a00fSLai Jiangshan 1097d565ed63STejun Heo spin_unlock(&pool->lock); 109836e227d2STejun Heo return 1; 1099bf4ede01STejun Heo } 1100d565ed63STejun Heo spin_unlock(&pool->lock); 1101bbb68dfaSTejun Heo fail: 1102bbb68dfaSTejun Heo local_irq_restore(*flags); 1103bbb68dfaSTejun Heo if (work_is_canceling(work)) 1104bbb68dfaSTejun Heo return -ENOENT; 1105bbb68dfaSTejun Heo cpu_relax(); 110636e227d2STejun Heo return -EAGAIN; 1107bf4ede01STejun Heo } 1108bf4ede01STejun Heo 1109bf4ede01STejun Heo /** 1110706026c2STejun Heo * insert_work - insert a work into a pool 1111112202d9STejun Heo * @pwq: pwq @work belongs to 11124690c4abSTejun Heo * @work: work to insert 11134690c4abSTejun Heo * @head: insertion point 11144690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 11154690c4abSTejun Heo * 1116112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1117706026c2STejun Heo * work_struct flags. 11184690c4abSTejun Heo * 11194690c4abSTejun Heo * CONTEXT: 1120d565ed63STejun Heo * spin_lock_irq(pool->lock). 1121365970a1SDavid Howells */ 1122112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1123112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1124b89deed3SOleg Nesterov { 1125112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1126e1d8aa9fSFrederic Weisbecker 11274690c4abSTejun Heo /* we own @work, set data and link */ 1128112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 11291a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1130e22bee78STejun Heo 1131e22bee78STejun Heo /* 1132e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1133e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1134e22bee78STejun Heo * lying around lazily while there are works to be processed. 1135e22bee78STejun Heo */ 1136e22bee78STejun Heo smp_mb(); 1137e22bee78STejun Heo 113863d95a91STejun Heo if (__need_more_worker(pool)) 113963d95a91STejun Heo wake_up_worker(pool); 1140b89deed3SOleg Nesterov } 1141b89deed3SOleg Nesterov 1142c8efcc25STejun Heo /* 1143c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 11448d03ecfeSTejun Heo * same workqueue. 1145c8efcc25STejun Heo */ 1146c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1147c8efcc25STejun Heo { 1148c8efcc25STejun Heo struct worker *worker; 1149c8efcc25STejun Heo 11508d03ecfeSTejun Heo worker = current_wq_worker(); 1151c8efcc25STejun Heo /* 11528d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 11538d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1154c8efcc25STejun Heo */ 1155112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1156c8efcc25STejun Heo } 1157c8efcc25STejun Heo 1158d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 11591da177e4SLinus Torvalds struct work_struct *work) 11601da177e4SLinus Torvalds { 1161112202d9STejun Heo struct pool_workqueue *pwq; 11621e19ffc6STejun Heo struct list_head *worklist; 11638a2e8e5dSTejun Heo unsigned int work_flags; 1164b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 11658930cabaSTejun Heo 11668930cabaSTejun Heo /* 11678930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11688930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11698930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11708930cabaSTejun Heo * happen with IRQ disabled. 11718930cabaSTejun Heo */ 11728930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 11731da177e4SLinus Torvalds 1174dc186ad7SThomas Gleixner debug_work_activate(work); 11751e19ffc6STejun Heo 1176c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 11779c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1178c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1179e41e704bSTejun Heo return; 1180e41e704bSTejun Heo 1181112202d9STejun Heo /* determine the pwq to use */ 1182c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1183c9e7cf27STejun Heo struct worker_pool *last_pool; 1184c7fc77f7STejun Heo 118557469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1186f3421797STejun Heo cpu = raw_smp_processor_id(); 1187f3421797STejun Heo 118818aa9effSTejun Heo /* 1189dbf2576eSTejun Heo * It's multi cpu. If @work was previously on a different 1190dbf2576eSTejun Heo * cpu, it might still be running there, in which case the 1191dbf2576eSTejun Heo * work needs to be queued on that cpu to guarantee 1192dbf2576eSTejun Heo * non-reentrancy. 119318aa9effSTejun Heo */ 1194112202d9STejun Heo pwq = get_pwq(cpu, wq); 1195c9e7cf27STejun Heo last_pool = get_work_pool(work); 1196dbf2576eSTejun Heo 1197112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 119818aa9effSTejun Heo struct worker *worker; 119918aa9effSTejun Heo 1200d565ed63STejun Heo spin_lock(&last_pool->lock); 120118aa9effSTejun Heo 1202c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 120318aa9effSTejun Heo 1204112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1205112202d9STejun Heo pwq = get_pwq(last_pool->cpu, wq); 12068594fadeSLai Jiangshan } else { 120718aa9effSTejun Heo /* meh... not running there, queue here */ 1208d565ed63STejun Heo spin_unlock(&last_pool->lock); 1209112202d9STejun Heo spin_lock(&pwq->pool->lock); 121018aa9effSTejun Heo } 12118930cabaSTejun Heo } else { 1212112202d9STejun Heo spin_lock(&pwq->pool->lock); 12138930cabaSTejun Heo } 1214f3421797STejun Heo } else { 1215112202d9STejun Heo pwq = get_pwq(WORK_CPU_UNBOUND, wq); 1216112202d9STejun Heo spin_lock(&pwq->pool->lock); 1217502ca9d8STejun Heo } 1218502ca9d8STejun Heo 1219112202d9STejun Heo /* pwq determined, queue */ 1220112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1221502ca9d8STejun Heo 1222f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1223112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1224f5b2552bSDan Carpenter return; 1225f5b2552bSDan Carpenter } 12261e19ffc6STejun Heo 1227112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1228112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 12291e19ffc6STejun Heo 1230112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1231cdadf009STejun Heo trace_workqueue_activate_work(work); 1232112202d9STejun Heo pwq->nr_active++; 1233112202d9STejun Heo worklist = &pwq->pool->worklist; 12348a2e8e5dSTejun Heo } else { 12358a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1236112202d9STejun Heo worklist = &pwq->delayed_works; 12378a2e8e5dSTejun Heo } 12381e19ffc6STejun Heo 1239112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 12401e19ffc6STejun Heo 1241112202d9STejun Heo spin_unlock(&pwq->pool->lock); 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12440fcb78c2SRolf Eike Beer /** 1245c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1246c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1247c1a220e7SZhang Rui * @wq: workqueue to use 1248c1a220e7SZhang Rui * @work: work to queue 1249c1a220e7SZhang Rui * 1250d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1251c1a220e7SZhang Rui * 1252c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1253c1a220e7SZhang Rui * can't go away. 1254c1a220e7SZhang Rui */ 1255d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1256d4283e93STejun Heo struct work_struct *work) 1257c1a220e7SZhang Rui { 1258d4283e93STejun Heo bool ret = false; 12598930cabaSTejun Heo unsigned long flags; 12608930cabaSTejun Heo 12618930cabaSTejun Heo local_irq_save(flags); 1262c1a220e7SZhang Rui 126322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12644690c4abSTejun Heo __queue_work(cpu, wq, work); 1265d4283e93STejun Heo ret = true; 1266c1a220e7SZhang Rui } 12678930cabaSTejun Heo 12688930cabaSTejun Heo local_irq_restore(flags); 1269c1a220e7SZhang Rui return ret; 1270c1a220e7SZhang Rui } 1271c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1272c1a220e7SZhang Rui 12730a13c00eSTejun Heo /** 12740a13c00eSTejun Heo * queue_work - queue work on a workqueue 12750a13c00eSTejun Heo * @wq: workqueue to use 12760a13c00eSTejun Heo * @work: work to queue 12770a13c00eSTejun Heo * 1278d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 12790a13c00eSTejun Heo * 12800a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 12810a13c00eSTejun Heo * it can be processed by another CPU. 12820a13c00eSTejun Heo */ 1283d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 12840a13c00eSTejun Heo { 128557469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 12860a13c00eSTejun Heo } 12870a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 12880a13c00eSTejun Heo 1289d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 12901da177e4SLinus Torvalds { 129152bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 12921da177e4SLinus Torvalds 1293e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 129460c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 12951da177e4SLinus Torvalds } 12961438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 12971da177e4SLinus Torvalds 12987beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 129952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13001da177e4SLinus Torvalds { 13017beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 13027beb2edfSTejun Heo struct work_struct *work = &dwork->work; 13031da177e4SLinus Torvalds 13047beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 13057beb2edfSTejun Heo timer->data != (unsigned long)dwork); 1306fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1307fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 13087beb2edfSTejun Heo 13098852aac2STejun Heo /* 13108852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 13118852aac2STejun Heo * both optimization and correctness. The earliest @timer can 13128852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 13138852aac2STejun Heo * on that there's no such delay when @delay is 0. 13148852aac2STejun Heo */ 13158852aac2STejun Heo if (!delay) { 13168852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 13178852aac2STejun Heo return; 13188852aac2STejun Heo } 13198852aac2STejun Heo 13207beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 13217beb2edfSTejun Heo 132260c057bcSLai Jiangshan dwork->wq = wq; 13231265057fSTejun Heo dwork->cpu = cpu; 13247beb2edfSTejun Heo timer->expires = jiffies + delay; 13257beb2edfSTejun Heo 13267beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13277beb2edfSTejun Heo add_timer_on(timer, cpu); 13287beb2edfSTejun Heo else 13297beb2edfSTejun Heo add_timer(timer); 13307beb2edfSTejun Heo } 13311da177e4SLinus Torvalds 13320fcb78c2SRolf Eike Beer /** 13330fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 13340fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 13350fcb78c2SRolf Eike Beer * @wq: workqueue to use 1336af9997e4SRandy Dunlap * @dwork: work to queue 13370fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 13380fcb78c2SRolf Eike Beer * 1339715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1340715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1341715f1300STejun Heo * execution. 13420fcb78c2SRolf Eike Beer */ 1343d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 134452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13457a6bc1cdSVenkatesh Pallipadi { 134652bad64dSDavid Howells struct work_struct *work = &dwork->work; 1347d4283e93STejun Heo bool ret = false; 13488930cabaSTejun Heo unsigned long flags; 13498930cabaSTejun Heo 13508930cabaSTejun Heo /* read the comment in __queue_work() */ 13518930cabaSTejun Heo local_irq_save(flags); 13527a6bc1cdSVenkatesh Pallipadi 135322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 13547beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1355d4283e93STejun Heo ret = true; 13567a6bc1cdSVenkatesh Pallipadi } 13578930cabaSTejun Heo 13588930cabaSTejun Heo local_irq_restore(flags); 13597a6bc1cdSVenkatesh Pallipadi return ret; 13607a6bc1cdSVenkatesh Pallipadi } 1361ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 13621da177e4SLinus Torvalds 1363c8e55f36STejun Heo /** 13640a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 13650a13c00eSTejun Heo * @wq: workqueue to use 13660a13c00eSTejun Heo * @dwork: delayable work to queue 13670a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 13680a13c00eSTejun Heo * 1369715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 13700a13c00eSTejun Heo */ 1371d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 13720a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 13730a13c00eSTejun Heo { 137457469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 13750a13c00eSTejun Heo } 13760a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 13770a13c00eSTejun Heo 13780a13c00eSTejun Heo /** 13798376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 13808376fe22STejun Heo * @cpu: CPU number to execute work on 13818376fe22STejun Heo * @wq: workqueue to use 13828376fe22STejun Heo * @dwork: work to queue 13838376fe22STejun Heo * @delay: number of jiffies to wait before queueing 13848376fe22STejun Heo * 13858376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 13868376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 13878376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 13888376fe22STejun Heo * current state. 13898376fe22STejun Heo * 13908376fe22STejun Heo * Returns %false if @dwork was idle and queued, %true if @dwork was 13918376fe22STejun Heo * pending and its timer was modified. 13928376fe22STejun Heo * 1393e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 13948376fe22STejun Heo * See try_to_grab_pending() for details. 13958376fe22STejun Heo */ 13968376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 13978376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 13988376fe22STejun Heo { 13998376fe22STejun Heo unsigned long flags; 14008376fe22STejun Heo int ret; 14018376fe22STejun Heo 14028376fe22STejun Heo do { 14038376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 14048376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 14058376fe22STejun Heo 14068376fe22STejun Heo if (likely(ret >= 0)) { 14078376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 14088376fe22STejun Heo local_irq_restore(flags); 14098376fe22STejun Heo } 14108376fe22STejun Heo 14118376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 14128376fe22STejun Heo return ret; 14138376fe22STejun Heo } 14148376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 14158376fe22STejun Heo 14168376fe22STejun Heo /** 14178376fe22STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 14188376fe22STejun Heo * @wq: workqueue to use 14198376fe22STejun Heo * @dwork: work to queue 14208376fe22STejun Heo * @delay: number of jiffies to wait before queueing 14218376fe22STejun Heo * 14228376fe22STejun Heo * mod_delayed_work_on() on local CPU. 14238376fe22STejun Heo */ 14248376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, 14258376fe22STejun Heo unsigned long delay) 14268376fe22STejun Heo { 14278376fe22STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 14288376fe22STejun Heo } 14298376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work); 14308376fe22STejun Heo 14318376fe22STejun Heo /** 1432c8e55f36STejun Heo * worker_enter_idle - enter idle state 1433c8e55f36STejun Heo * @worker: worker which is entering idle state 1434c8e55f36STejun Heo * 1435c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1436c8e55f36STejun Heo * necessary. 1437c8e55f36STejun Heo * 1438c8e55f36STejun Heo * LOCKING: 1439d565ed63STejun Heo * spin_lock_irq(pool->lock). 1440c8e55f36STejun Heo */ 1441c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 14421da177e4SLinus Torvalds { 1443bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1444c8e55f36STejun Heo 14456183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 14466183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 14476183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 14486183c009STejun Heo return; 1449c8e55f36STejun Heo 1450cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1451cb444766STejun Heo worker->flags |= WORKER_IDLE; 1452bd7bdd43STejun Heo pool->nr_idle++; 1453e22bee78STejun Heo worker->last_active = jiffies; 1454c8e55f36STejun Heo 1455c8e55f36STejun Heo /* idle_list is LIFO */ 1456bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1457db7bccf4STejun Heo 145863d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1459628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1460cb444766STejun Heo 1461544ecf31STejun Heo /* 1462706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1463d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1464628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1465628c78e7STejun Heo * unbind is not in progress. 1466544ecf31STejun Heo */ 146724647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1468bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1469e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1470c8e55f36STejun Heo } 1471c8e55f36STejun Heo 1472c8e55f36STejun Heo /** 1473c8e55f36STejun Heo * worker_leave_idle - leave idle state 1474c8e55f36STejun Heo * @worker: worker which is leaving idle state 1475c8e55f36STejun Heo * 1476c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1477c8e55f36STejun Heo * 1478c8e55f36STejun Heo * LOCKING: 1479d565ed63STejun Heo * spin_lock_irq(pool->lock). 1480c8e55f36STejun Heo */ 1481c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1482c8e55f36STejun Heo { 1483bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1484c8e55f36STejun Heo 14856183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 14866183c009STejun Heo return; 1487d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1488bd7bdd43STejun Heo pool->nr_idle--; 1489c8e55f36STejun Heo list_del_init(&worker->entry); 1490c8e55f36STejun Heo } 1491c8e55f36STejun Heo 1492e22bee78STejun Heo /** 1493f36dc67bSLai Jiangshan * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it 1494f36dc67bSLai Jiangshan * @pool: target worker_pool 1495f36dc67bSLai Jiangshan * 1496f36dc67bSLai Jiangshan * Bind %current to the cpu of @pool if it is associated and lock @pool. 1497e22bee78STejun Heo * 1498e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1499e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1500e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1501e22bee78STejun Heo * guaranteed to execute on the cpu. 1502e22bee78STejun Heo * 1503f5faa077SLai Jiangshan * This function is to be used by unbound workers and rescuers to bind 1504e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1505e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1506e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1507706026c2STejun Heo * verbatim as it's best effort and blocking and pool may be 1508e22bee78STejun Heo * [dis]associated in the meantime. 1509e22bee78STejun Heo * 1510706026c2STejun Heo * This function tries set_cpus_allowed() and locks pool and verifies the 151124647570STejun Heo * binding against %POOL_DISASSOCIATED which is set during 1512f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1513f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1514f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1515e22bee78STejun Heo * 1516e22bee78STejun Heo * CONTEXT: 1517d565ed63STejun Heo * Might sleep. Called without any lock but returns with pool->lock 1518e22bee78STejun Heo * held. 1519e22bee78STejun Heo * 1520e22bee78STejun Heo * RETURNS: 1521706026c2STejun Heo * %true if the associated pool is online (@worker is successfully 1522e22bee78STejun Heo * bound), %false if offline. 1523e22bee78STejun Heo */ 1524f36dc67bSLai Jiangshan static bool worker_maybe_bind_and_lock(struct worker_pool *pool) 1525d565ed63STejun Heo __acquires(&pool->lock) 1526e22bee78STejun Heo { 1527e22bee78STejun Heo while (true) { 1528e22bee78STejun Heo /* 1529e22bee78STejun Heo * The following call may fail, succeed or succeed 1530e22bee78STejun Heo * without actually migrating the task to the cpu if 1531e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 153224647570STejun Heo * against POOL_DISASSOCIATED. 1533e22bee78STejun Heo */ 153424647570STejun Heo if (!(pool->flags & POOL_DISASSOCIATED)) 1535f5faa077SLai Jiangshan set_cpus_allowed_ptr(current, get_cpu_mask(pool->cpu)); 1536e22bee78STejun Heo 1537d565ed63STejun Heo spin_lock_irq(&pool->lock); 153824647570STejun Heo if (pool->flags & POOL_DISASSOCIATED) 1539e22bee78STejun Heo return false; 1540f5faa077SLai Jiangshan if (task_cpu(current) == pool->cpu && 1541e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1542ec22ca5eSTejun Heo get_cpu_mask(pool->cpu))) 1543e22bee78STejun Heo return true; 1544d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1545e22bee78STejun Heo 15465035b20fSTejun Heo /* 15475035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 15485035b20fSTejun Heo * and retry migration. cond_resched() is required here; 15495035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 15505035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 15515035b20fSTejun Heo */ 1552e22bee78STejun Heo cpu_relax(); 15535035b20fSTejun Heo cond_resched(); 1554e22bee78STejun Heo } 1555e22bee78STejun Heo } 1556e22bee78STejun Heo 1557e22bee78STejun Heo /* 1558ea1abd61SLai Jiangshan * Rebind an idle @worker to its CPU. worker_thread() will test 15595f7dabfdSLai Jiangshan * list_empty(@worker->entry) before leaving idle and call this function. 156025511a47STejun Heo */ 156125511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 156225511a47STejun Heo { 15635f7dabfdSLai Jiangshan /* CPU may go down again inbetween, clear UNBOUND only on success */ 1564f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(worker->pool)) 15655f7dabfdSLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 156625511a47STejun Heo 1567ea1abd61SLai Jiangshan /* rebind complete, become available again */ 1568ea1abd61SLai Jiangshan list_add(&worker->entry, &worker->pool->idle_list); 1569d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 157025511a47STejun Heo } 157125511a47STejun Heo 157225511a47STejun Heo /* 157325511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1574403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1575403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1576403c821dSTejun Heo * executed twice without intervening cpu down. 1577e22bee78STejun Heo */ 157825511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1579e22bee78STejun Heo { 1580e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1581e22bee78STejun Heo 1582f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(worker->pool)) 1583eab6d828SLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 1584e22bee78STejun Heo 1585d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 1586e22bee78STejun Heo } 1587e22bee78STejun Heo 158825511a47STejun Heo /** 158994cf58bbSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 159094cf58bbSTejun Heo * @pool: pool of interest 159125511a47STejun Heo * 159294cf58bbSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding 159325511a47STejun Heo * is different for idle and busy ones. 159425511a47STejun Heo * 1595ea1abd61SLai Jiangshan * Idle ones will be removed from the idle_list and woken up. They will 1596ea1abd61SLai Jiangshan * add themselves back after completing rebind. This ensures that the 1597ea1abd61SLai Jiangshan * idle_list doesn't contain any unbound workers when re-bound busy workers 1598ea1abd61SLai Jiangshan * try to perform local wake-ups for concurrency management. 159925511a47STejun Heo * 1600ea1abd61SLai Jiangshan * Busy workers can rebind after they finish their current work items. 1601ea1abd61SLai Jiangshan * Queueing the rebind work item at the head of the scheduled list is 1602ea1abd61SLai Jiangshan * enough. Note that nr_running will be properly bumped as busy workers 1603ea1abd61SLai Jiangshan * rebind. 160425511a47STejun Heo * 1605ea1abd61SLai Jiangshan * On return, all non-manager workers are scheduled for rebind - see 1606ea1abd61SLai Jiangshan * manage_workers() for the manager special case. Any idle worker 1607ea1abd61SLai Jiangshan * including the manager will not appear on @idle_list until rebind is 1608ea1abd61SLai Jiangshan * complete, making local wake-ups safe. 160925511a47STejun Heo */ 161094cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool) 161125511a47STejun Heo { 1612ea1abd61SLai Jiangshan struct worker *worker, *n; 161325511a47STejun Heo int i; 161425511a47STejun Heo 1615b2eb83d1SLai Jiangshan lockdep_assert_held(&pool->assoc_mutex); 1616d565ed63STejun Heo lockdep_assert_held(&pool->lock); 161725511a47STejun Heo 16185f7dabfdSLai Jiangshan /* dequeue and kick idle ones */ 1619ea1abd61SLai Jiangshan list_for_each_entry_safe(worker, n, &pool->idle_list, entry) { 1620ea1abd61SLai Jiangshan /* 162194cf58bbSTejun Heo * idle workers should be off @pool->idle_list until rebind 162294cf58bbSTejun Heo * is complete to avoid receiving premature local wake-ups. 1623ea1abd61SLai Jiangshan */ 1624ea1abd61SLai Jiangshan list_del_init(&worker->entry); 162525511a47STejun Heo 162625511a47STejun Heo /* 162794cf58bbSTejun Heo * worker_thread() will see the above dequeuing and call 162894cf58bbSTejun Heo * idle_worker_rebind(). 162925511a47STejun Heo */ 163025511a47STejun Heo wake_up_process(worker->task); 163125511a47STejun Heo } 163225511a47STejun Heo 1633ea1abd61SLai Jiangshan /* rebind busy workers */ 1634b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) { 163525511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 1636e2b6a6d5SJoonsoo Kim struct workqueue_struct *wq; 163725511a47STejun Heo 163825511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 163925511a47STejun Heo work_data_bits(rebind_work))) 164025511a47STejun Heo continue; 164125511a47STejun Heo 164225511a47STejun Heo debug_work_activate(rebind_work); 164390beca5dSTejun Heo 164490beca5dSTejun Heo /* 164594cf58bbSTejun Heo * wq doesn't really matter but let's keep @worker->pool 1646112202d9STejun Heo * and @pwq->pool consistent for sanity. 164790beca5dSTejun Heo */ 1648e34cdddbSTejun Heo if (std_worker_pool_pri(worker->pool)) 1649e2b6a6d5SJoonsoo Kim wq = system_highpri_wq; 1650e2b6a6d5SJoonsoo Kim else 1651e2b6a6d5SJoonsoo Kim wq = system_wq; 1652ec58815aSTejun Heo 1653112202d9STejun Heo insert_work(get_pwq(pool->cpu, wq), rebind_work, 165425511a47STejun Heo worker->scheduled.next, 165525511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 1656ec58815aSTejun Heo } 165725511a47STejun Heo } 165825511a47STejun Heo 1659c34056a3STejun Heo static struct worker *alloc_worker(void) 1660c34056a3STejun Heo { 1661c34056a3STejun Heo struct worker *worker; 1662c34056a3STejun Heo 1663c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1664c8e55f36STejun Heo if (worker) { 1665c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1666affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 166725511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1668e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1669e22bee78STejun Heo worker->flags = WORKER_PREP; 1670c8e55f36STejun Heo } 1671c34056a3STejun Heo return worker; 1672c34056a3STejun Heo } 1673c34056a3STejun Heo 1674c34056a3STejun Heo /** 1675c34056a3STejun Heo * create_worker - create a new workqueue worker 167663d95a91STejun Heo * @pool: pool the new worker will belong to 1677c34056a3STejun Heo * 167863d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1679c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1680c34056a3STejun Heo * destroy_worker(). 1681c34056a3STejun Heo * 1682c34056a3STejun Heo * CONTEXT: 1683c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1684c34056a3STejun Heo * 1685c34056a3STejun Heo * RETURNS: 1686c34056a3STejun Heo * Pointer to the newly created worker. 1687c34056a3STejun Heo */ 1688bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1689c34056a3STejun Heo { 1690e34cdddbSTejun Heo const char *pri = std_worker_pool_pri(pool) ? "H" : ""; 1691c34056a3STejun Heo struct worker *worker = NULL; 1692f3421797STejun Heo int id = -1; 1693c34056a3STejun Heo 1694d565ed63STejun Heo spin_lock_irq(&pool->lock); 1695bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 1696d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1697bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1698c34056a3STejun Heo goto fail; 1699d565ed63STejun Heo spin_lock_irq(&pool->lock); 1700c34056a3STejun Heo } 1701d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1702c34056a3STejun Heo 1703c34056a3STejun Heo worker = alloc_worker(); 1704c34056a3STejun Heo if (!worker) 1705c34056a3STejun Heo goto fail; 1706c34056a3STejun Heo 1707bd7bdd43STejun Heo worker->pool = pool; 1708c34056a3STejun Heo worker->id = id; 1709c34056a3STejun Heo 1710ec22ca5eSTejun Heo if (pool->cpu != WORK_CPU_UNBOUND) 171194dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 1712ec22ca5eSTejun Heo worker, cpu_to_node(pool->cpu), 1713d84ff051STejun Heo "kworker/%d:%d%s", pool->cpu, id, pri); 1714f3421797STejun Heo else 1715f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 17163270476aSTejun Heo "kworker/u:%d%s", id, pri); 1717c34056a3STejun Heo if (IS_ERR(worker->task)) 1718c34056a3STejun Heo goto fail; 1719c34056a3STejun Heo 1720e34cdddbSTejun Heo if (std_worker_pool_pri(pool)) 17213270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 17223270476aSTejun Heo 1723db7bccf4STejun Heo /* 1724bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 172524647570STejun Heo * %POOL_DISASSOCIATED. The caller is responsible for ensuring the 1726bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1727bc2ae0f5STejun Heo * above the flag definition for details. 1728bc2ae0f5STejun Heo * 1729bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1730bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1731db7bccf4STejun Heo */ 173224647570STejun Heo if (!(pool->flags & POOL_DISASSOCIATED)) { 1733ec22ca5eSTejun Heo kthread_bind(worker->task, pool->cpu); 1734bc2ae0f5STejun Heo } else { 1735db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1736f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1737f3421797STejun Heo } 1738c34056a3STejun Heo 1739c34056a3STejun Heo return worker; 1740c34056a3STejun Heo fail: 1741c34056a3STejun Heo if (id >= 0) { 1742d565ed63STejun Heo spin_lock_irq(&pool->lock); 1743bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1744d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1745c34056a3STejun Heo } 1746c34056a3STejun Heo kfree(worker); 1747c34056a3STejun Heo return NULL; 1748c34056a3STejun Heo } 1749c34056a3STejun Heo 1750c34056a3STejun Heo /** 1751c34056a3STejun Heo * start_worker - start a newly created worker 1752c34056a3STejun Heo * @worker: worker to start 1753c34056a3STejun Heo * 1754706026c2STejun Heo * Make the pool aware of @worker and start it. 1755c34056a3STejun Heo * 1756c34056a3STejun Heo * CONTEXT: 1757d565ed63STejun Heo * spin_lock_irq(pool->lock). 1758c34056a3STejun Heo */ 1759c34056a3STejun Heo static void start_worker(struct worker *worker) 1760c34056a3STejun Heo { 1761cb444766STejun Heo worker->flags |= WORKER_STARTED; 1762bd7bdd43STejun Heo worker->pool->nr_workers++; 1763c8e55f36STejun Heo worker_enter_idle(worker); 1764c34056a3STejun Heo wake_up_process(worker->task); 1765c34056a3STejun Heo } 1766c34056a3STejun Heo 1767c34056a3STejun Heo /** 1768c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1769c34056a3STejun Heo * @worker: worker to be destroyed 1770c34056a3STejun Heo * 1771706026c2STejun Heo * Destroy @worker and adjust @pool stats accordingly. 1772c8e55f36STejun Heo * 1773c8e55f36STejun Heo * CONTEXT: 1774d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 1775c34056a3STejun Heo */ 1776c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1777c34056a3STejun Heo { 1778bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1779c34056a3STejun Heo int id = worker->id; 1780c34056a3STejun Heo 1781c34056a3STejun Heo /* sanity check frenzy */ 17826183c009STejun Heo if (WARN_ON(worker->current_work) || 17836183c009STejun Heo WARN_ON(!list_empty(&worker->scheduled))) 17846183c009STejun Heo return; 1785c34056a3STejun Heo 1786c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1787bd7bdd43STejun Heo pool->nr_workers--; 1788c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1789bd7bdd43STejun Heo pool->nr_idle--; 1790c8e55f36STejun Heo 1791c8e55f36STejun Heo list_del_init(&worker->entry); 1792cb444766STejun Heo worker->flags |= WORKER_DIE; 1793c8e55f36STejun Heo 1794d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1795c8e55f36STejun Heo 1796c34056a3STejun Heo kthread_stop(worker->task); 1797c34056a3STejun Heo kfree(worker); 1798c34056a3STejun Heo 1799d565ed63STejun Heo spin_lock_irq(&pool->lock); 1800bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1801c34056a3STejun Heo } 1802c34056a3STejun Heo 180363d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1804e22bee78STejun Heo { 180563d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1806e22bee78STejun Heo 1807d565ed63STejun Heo spin_lock_irq(&pool->lock); 1808e22bee78STejun Heo 180963d95a91STejun Heo if (too_many_workers(pool)) { 1810e22bee78STejun Heo struct worker *worker; 1811e22bee78STejun Heo unsigned long expires; 1812e22bee78STejun Heo 1813e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 181463d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1815e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1816e22bee78STejun Heo 1817e22bee78STejun Heo if (time_before(jiffies, expires)) 181863d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1819e22bee78STejun Heo else { 1820e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 182111ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 182263d95a91STejun Heo wake_up_worker(pool); 1823e22bee78STejun Heo } 1824e22bee78STejun Heo } 1825e22bee78STejun Heo 1826d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1827e22bee78STejun Heo } 1828e22bee78STejun Heo 1829493a1724STejun Heo static void send_mayday(struct work_struct *work) 1830e22bee78STejun Heo { 1831112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1832112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1833493a1724STejun Heo 1834493a1724STejun Heo lockdep_assert_held(&workqueue_lock); 1835e22bee78STejun Heo 1836e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1837493a1724STejun Heo return; 1838e22bee78STejun Heo 1839e22bee78STejun Heo /* mayday mayday mayday */ 1840493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 1841493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1842e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1843493a1724STejun Heo } 1844e22bee78STejun Heo } 1845e22bee78STejun Heo 1846706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1847e22bee78STejun Heo { 184863d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1849e22bee78STejun Heo struct work_struct *work; 1850e22bee78STejun Heo 1851493a1724STejun Heo spin_lock_irq(&workqueue_lock); /* for wq->maydays */ 1852493a1724STejun Heo spin_lock(&pool->lock); 1853e22bee78STejun Heo 185463d95a91STejun Heo if (need_to_create_worker(pool)) { 1855e22bee78STejun Heo /* 1856e22bee78STejun Heo * We've been trying to create a new worker but 1857e22bee78STejun Heo * haven't been successful. We might be hitting an 1858e22bee78STejun Heo * allocation deadlock. Send distress signals to 1859e22bee78STejun Heo * rescuers. 1860e22bee78STejun Heo */ 186163d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1862e22bee78STejun Heo send_mayday(work); 1863e22bee78STejun Heo } 1864e22bee78STejun Heo 1865493a1724STejun Heo spin_unlock(&pool->lock); 1866493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 1867e22bee78STejun Heo 186863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1869e22bee78STejun Heo } 1870e22bee78STejun Heo 1871e22bee78STejun Heo /** 1872e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 187363d95a91STejun Heo * @pool: pool to create a new worker for 1874e22bee78STejun Heo * 187563d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1876e22bee78STejun Heo * have at least one idle worker on return from this function. If 1877e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 187863d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1879e22bee78STejun Heo * possible allocation deadlock. 1880e22bee78STejun Heo * 1881e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1882e22bee78STejun Heo * may_start_working() true. 1883e22bee78STejun Heo * 1884e22bee78STejun Heo * LOCKING: 1885d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1886e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1887e22bee78STejun Heo * manager. 1888e22bee78STejun Heo * 1889e22bee78STejun Heo * RETURNS: 1890d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1891e22bee78STejun Heo * otherwise. 1892e22bee78STejun Heo */ 189363d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 1894d565ed63STejun Heo __releases(&pool->lock) 1895d565ed63STejun Heo __acquires(&pool->lock) 1896e22bee78STejun Heo { 189763d95a91STejun Heo if (!need_to_create_worker(pool)) 1898e22bee78STejun Heo return false; 1899e22bee78STejun Heo restart: 1900d565ed63STejun Heo spin_unlock_irq(&pool->lock); 19019f9c2364STejun Heo 1902e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 190363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1904e22bee78STejun Heo 1905e22bee78STejun Heo while (true) { 1906e22bee78STejun Heo struct worker *worker; 1907e22bee78STejun Heo 1908bc2ae0f5STejun Heo worker = create_worker(pool); 1909e22bee78STejun Heo if (worker) { 191063d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1911d565ed63STejun Heo spin_lock_irq(&pool->lock); 1912e22bee78STejun Heo start_worker(worker); 19136183c009STejun Heo if (WARN_ON_ONCE(need_to_create_worker(pool))) 19146183c009STejun Heo goto restart; 1915e22bee78STejun Heo return true; 1916e22bee78STejun Heo } 1917e22bee78STejun Heo 191863d95a91STejun Heo if (!need_to_create_worker(pool)) 1919e22bee78STejun Heo break; 1920e22bee78STejun Heo 1921e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1922e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19239f9c2364STejun Heo 192463d95a91STejun Heo if (!need_to_create_worker(pool)) 1925e22bee78STejun Heo break; 1926e22bee78STejun Heo } 1927e22bee78STejun Heo 192863d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1929d565ed63STejun Heo spin_lock_irq(&pool->lock); 193063d95a91STejun Heo if (need_to_create_worker(pool)) 1931e22bee78STejun Heo goto restart; 1932e22bee78STejun Heo return true; 1933e22bee78STejun Heo } 1934e22bee78STejun Heo 1935e22bee78STejun Heo /** 1936e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 193763d95a91STejun Heo * @pool: pool to destroy workers for 1938e22bee78STejun Heo * 193963d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1940e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1941e22bee78STejun Heo * 1942e22bee78STejun Heo * LOCKING: 1943d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1944e22bee78STejun Heo * multiple times. Called only from manager. 1945e22bee78STejun Heo * 1946e22bee78STejun Heo * RETURNS: 1947d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1948e22bee78STejun Heo * otherwise. 1949e22bee78STejun Heo */ 195063d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1951e22bee78STejun Heo { 1952e22bee78STejun Heo bool ret = false; 1953e22bee78STejun Heo 195463d95a91STejun Heo while (too_many_workers(pool)) { 1955e22bee78STejun Heo struct worker *worker; 1956e22bee78STejun Heo unsigned long expires; 1957e22bee78STejun Heo 195863d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1959e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1960e22bee78STejun Heo 1961e22bee78STejun Heo if (time_before(jiffies, expires)) { 196263d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1963e22bee78STejun Heo break; 1964e22bee78STejun Heo } 1965e22bee78STejun Heo 1966e22bee78STejun Heo destroy_worker(worker); 1967e22bee78STejun Heo ret = true; 1968e22bee78STejun Heo } 1969e22bee78STejun Heo 1970e22bee78STejun Heo return ret; 1971e22bee78STejun Heo } 1972e22bee78STejun Heo 1973e22bee78STejun Heo /** 1974e22bee78STejun Heo * manage_workers - manage worker pool 1975e22bee78STejun Heo * @worker: self 1976e22bee78STejun Heo * 1977706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1978e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1979706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1980e22bee78STejun Heo * 1981e22bee78STejun Heo * The caller can safely start processing works on false return. On 1982e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1983e22bee78STejun Heo * and may_start_working() is true. 1984e22bee78STejun Heo * 1985e22bee78STejun Heo * CONTEXT: 1986d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1987e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1988e22bee78STejun Heo * 1989e22bee78STejun Heo * RETURNS: 1990d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1991d565ed63STejun Heo * multiple times. Does GFP_KERNEL allocations. 1992e22bee78STejun Heo */ 1993e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1994e22bee78STejun Heo { 199563d95a91STejun Heo struct worker_pool *pool = worker->pool; 1996e22bee78STejun Heo bool ret = false; 1997e22bee78STejun Heo 1998ee378aa4SLai Jiangshan if (pool->flags & POOL_MANAGING_WORKERS) 1999e22bee78STejun Heo return ret; 2000e22bee78STejun Heo 2001552a37e9SLai Jiangshan pool->flags |= POOL_MANAGING_WORKERS; 2002ee378aa4SLai Jiangshan 2003ee378aa4SLai Jiangshan /* 2004ee378aa4SLai Jiangshan * To simplify both worker management and CPU hotplug, hold off 2005ee378aa4SLai Jiangshan * management while hotplug is in progress. CPU hotplug path can't 2006ee378aa4SLai Jiangshan * grab %POOL_MANAGING_WORKERS to achieve this because that can 2007ee378aa4SLai Jiangshan * lead to idle worker depletion (all become busy thinking someone 2008ee378aa4SLai Jiangshan * else is managing) which in turn can result in deadlock under 2009b2eb83d1SLai Jiangshan * extreme circumstances. Use @pool->assoc_mutex to synchronize 2010ee378aa4SLai Jiangshan * manager against CPU hotplug. 2011ee378aa4SLai Jiangshan * 2012b2eb83d1SLai Jiangshan * assoc_mutex would always be free unless CPU hotplug is in 2013d565ed63STejun Heo * progress. trylock first without dropping @pool->lock. 2014ee378aa4SLai Jiangshan */ 2015b2eb83d1SLai Jiangshan if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { 2016d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2017b2eb83d1SLai Jiangshan mutex_lock(&pool->assoc_mutex); 2018ee378aa4SLai Jiangshan /* 2019ee378aa4SLai Jiangshan * CPU hotplug could have happened while we were waiting 2020b2eb83d1SLai Jiangshan * for assoc_mutex. Hotplug itself can't handle us 2021ee378aa4SLai Jiangshan * because manager isn't either on idle or busy list, and 2022706026c2STejun Heo * @pool's state and ours could have deviated. 2023ee378aa4SLai Jiangshan * 2024b2eb83d1SLai Jiangshan * As hotplug is now excluded via assoc_mutex, we can 2025ee378aa4SLai Jiangshan * simply try to bind. It will succeed or fail depending 2026706026c2STejun Heo * on @pool's current state. Try it and adjust 2027ee378aa4SLai Jiangshan * %WORKER_UNBOUND accordingly. 2028ee378aa4SLai Jiangshan */ 2029f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(pool)) 2030ee378aa4SLai Jiangshan worker->flags &= ~WORKER_UNBOUND; 2031ee378aa4SLai Jiangshan else 2032ee378aa4SLai Jiangshan worker->flags |= WORKER_UNBOUND; 2033ee378aa4SLai Jiangshan 2034ee378aa4SLai Jiangshan ret = true; 2035ee378aa4SLai Jiangshan } 2036ee378aa4SLai Jiangshan 203711ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 2038e22bee78STejun Heo 2039e22bee78STejun Heo /* 2040e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 2041e22bee78STejun Heo * on return. 2042e22bee78STejun Heo */ 204363d95a91STejun Heo ret |= maybe_destroy_workers(pool); 204463d95a91STejun Heo ret |= maybe_create_worker(pool); 2045e22bee78STejun Heo 2046552a37e9SLai Jiangshan pool->flags &= ~POOL_MANAGING_WORKERS; 2047b2eb83d1SLai Jiangshan mutex_unlock(&pool->assoc_mutex); 2048e22bee78STejun Heo return ret; 2049e22bee78STejun Heo } 2050e22bee78STejun Heo 2051a62428c0STejun Heo /** 2052a62428c0STejun Heo * process_one_work - process single work 2053c34056a3STejun Heo * @worker: self 2054a62428c0STejun Heo * @work: work to process 2055a62428c0STejun Heo * 2056a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2057a62428c0STejun Heo * process a single work including synchronization against and 2058a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2059a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2060a62428c0STejun Heo * call this function to process a work. 2061a62428c0STejun Heo * 2062a62428c0STejun Heo * CONTEXT: 2063d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2064a62428c0STejun Heo */ 2065c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2066d565ed63STejun Heo __releases(&pool->lock) 2067d565ed63STejun Heo __acquires(&pool->lock) 20681da177e4SLinus Torvalds { 2069112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2070bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2071112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 207273f53c4aSTejun Heo int work_color; 20737e11629dSTejun Heo struct worker *collision; 20744e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20754e6045f1SJohannes Berg /* 2076a62428c0STejun Heo * It is permissible to free the struct work_struct from 2077a62428c0STejun Heo * inside the function that is called from it, this we need to 2078a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2079a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2080a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20814e6045f1SJohannes Berg */ 20824d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20834d82a1deSPeter Zijlstra 20844d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20854e6045f1SJohannes Berg #endif 20866fec10a1STejun Heo /* 20876fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20886fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 208924647570STejun Heo * unbound or a disassociated pool. 20906fec10a1STejun Heo */ 20915f7dabfdSLai Jiangshan WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) && 209224647570STejun Heo !(pool->flags & POOL_DISASSOCIATED) && 2093ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 209425511a47STejun Heo 20957e11629dSTejun Heo /* 20967e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20977e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20987e11629dSTejun Heo * already processing the work. If so, defer the work to the 20997e11629dSTejun Heo * currently executing one. 21007e11629dSTejun Heo */ 2101c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 21027e11629dSTejun Heo if (unlikely(collision)) { 21037e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 21047e11629dSTejun Heo return; 21057e11629dSTejun Heo } 21061da177e4SLinus Torvalds 21078930cabaSTejun Heo /* claim and dequeue */ 21081da177e4SLinus Torvalds debug_work_deactivate(work); 2109c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2110c34056a3STejun Heo worker->current_work = work; 2111a2c1c57bSTejun Heo worker->current_func = work->func; 2112112202d9STejun Heo worker->current_pwq = pwq; 211373f53c4aSTejun Heo work_color = get_work_color(work); 21147a22ad75STejun Heo 2115a62428c0STejun Heo list_del_init(&work->entry); 2116a62428c0STejun Heo 2117649027d7STejun Heo /* 2118fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2119fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2120fb0e7bebSTejun Heo */ 2121fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2122fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2123fb0e7bebSTejun Heo 2124974271c4STejun Heo /* 2125d565ed63STejun Heo * Unbound pool isn't concurrency managed and work items should be 2126974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2127974271c4STejun Heo */ 212863d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 212963d95a91STejun Heo wake_up_worker(pool); 2130974271c4STejun Heo 21318930cabaSTejun Heo /* 21327c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2133d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 213423657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 213523657bb1STejun Heo * disabled. 21368930cabaSTejun Heo */ 21377c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 21381da177e4SLinus Torvalds 2139d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2140365970a1SDavid Howells 2141112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 21423295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2143e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2144a2c1c57bSTejun Heo worker->current_func(work); 2145e36c886aSArjan van de Ven /* 2146e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2147e36c886aSArjan van de Ven * point will only record its address. 2148e36c886aSArjan van de Ven */ 2149e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21503295f0efSIngo Molnar lock_map_release(&lockdep_map); 2151112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21521da177e4SLinus Torvalds 2153d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2154044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2155044c782cSValentin Ilie " last function: %pf\n", 2156a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2157a2c1c57bSTejun Heo worker->current_func); 2158d5abe669SPeter Zijlstra debug_show_held_locks(current); 2159d5abe669SPeter Zijlstra dump_stack(); 2160d5abe669SPeter Zijlstra } 2161d5abe669SPeter Zijlstra 2162d565ed63STejun Heo spin_lock_irq(&pool->lock); 2163a62428c0STejun Heo 2164fb0e7bebSTejun Heo /* clear cpu intensive status */ 2165fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2166fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2167fb0e7bebSTejun Heo 2168a62428c0STejun Heo /* we're done with it, release */ 216942f8570fSSasha Levin hash_del(&worker->hentry); 2170c34056a3STejun Heo worker->current_work = NULL; 2171a2c1c57bSTejun Heo worker->current_func = NULL; 2172112202d9STejun Heo worker->current_pwq = NULL; 2173112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 2176affee4b2STejun Heo /** 2177affee4b2STejun Heo * process_scheduled_works - process scheduled works 2178affee4b2STejun Heo * @worker: self 2179affee4b2STejun Heo * 2180affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2181affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2182affee4b2STejun Heo * fetches a work from the top and executes it. 2183affee4b2STejun Heo * 2184affee4b2STejun Heo * CONTEXT: 2185d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2186affee4b2STejun Heo * multiple times. 2187affee4b2STejun Heo */ 2188affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21891da177e4SLinus Torvalds { 2190affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2191affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2192a62428c0STejun Heo struct work_struct, entry); 2193c34056a3STejun Heo process_one_work(worker, work); 2194a62428c0STejun Heo } 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds 21974690c4abSTejun Heo /** 21984690c4abSTejun Heo * worker_thread - the worker thread function 2199c34056a3STejun Heo * @__worker: self 22004690c4abSTejun Heo * 2201706026c2STejun Heo * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2202706026c2STejun Heo * of these per each cpu. These workers process all works regardless of 2203e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2204e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2205e22bee78STejun Heo * rescuer_thread(). 22064690c4abSTejun Heo */ 2207c34056a3STejun Heo static int worker_thread(void *__worker) 22081da177e4SLinus Torvalds { 2209c34056a3STejun Heo struct worker *worker = __worker; 2210bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 22111da177e4SLinus Torvalds 2212e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2213e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2214c8e55f36STejun Heo woke_up: 2215d565ed63STejun Heo spin_lock_irq(&pool->lock); 2216affee4b2STejun Heo 22175f7dabfdSLai Jiangshan /* we are off idle list if destruction or rebind is requested */ 22185f7dabfdSLai Jiangshan if (unlikely(list_empty(&worker->entry))) { 2219d565ed63STejun Heo spin_unlock_irq(&pool->lock); 222025511a47STejun Heo 22215f7dabfdSLai Jiangshan /* if DIE is set, destruction is requested */ 222225511a47STejun Heo if (worker->flags & WORKER_DIE) { 2223e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2224c8e55f36STejun Heo return 0; 2225c8e55f36STejun Heo } 2226c8e55f36STejun Heo 22275f7dabfdSLai Jiangshan /* otherwise, rebind */ 222825511a47STejun Heo idle_worker_rebind(worker); 222925511a47STejun Heo goto woke_up; 223025511a47STejun Heo } 223125511a47STejun Heo 2232c8e55f36STejun Heo worker_leave_idle(worker); 2233db7bccf4STejun Heo recheck: 2234e22bee78STejun Heo /* no more worker necessary? */ 223563d95a91STejun Heo if (!need_more_worker(pool)) 2236e22bee78STejun Heo goto sleep; 2237e22bee78STejun Heo 2238e22bee78STejun Heo /* do we need to manage? */ 223963d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2240e22bee78STejun Heo goto recheck; 2241e22bee78STejun Heo 2242c8e55f36STejun Heo /* 2243c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2244c8e55f36STejun Heo * preparing to process a work or actually processing it. 2245c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2246c8e55f36STejun Heo */ 22476183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2248c8e55f36STejun Heo 2249e22bee78STejun Heo /* 2250e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2251e22bee78STejun Heo * at least one idle worker or that someone else has already 2252e22bee78STejun Heo * assumed the manager role. 2253e22bee78STejun Heo */ 2254e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2255e22bee78STejun Heo 2256e22bee78STejun Heo do { 2257affee4b2STejun Heo struct work_struct *work = 2258bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2259affee4b2STejun Heo struct work_struct, entry); 2260affee4b2STejun Heo 2261c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2262affee4b2STejun Heo /* optimization path, not strictly necessary */ 2263affee4b2STejun Heo process_one_work(worker, work); 2264affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2265affee4b2STejun Heo process_scheduled_works(worker); 2266affee4b2STejun Heo } else { 2267c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2268affee4b2STejun Heo process_scheduled_works(worker); 2269affee4b2STejun Heo } 227063d95a91STejun Heo } while (keep_working(pool)); 2271affee4b2STejun Heo 2272e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2273d313dd85STejun Heo sleep: 227463d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2275e22bee78STejun Heo goto recheck; 2276d313dd85STejun Heo 2277c8e55f36STejun Heo /* 2278d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2279d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2280d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2281d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2282d565ed63STejun Heo * event. 2283c8e55f36STejun Heo */ 2284c8e55f36STejun Heo worker_enter_idle(worker); 2285c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2286d565ed63STejun Heo spin_unlock_irq(&pool->lock); 22871da177e4SLinus Torvalds schedule(); 2288c8e55f36STejun Heo goto woke_up; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 2291e22bee78STejun Heo /** 2292e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2293111c225aSTejun Heo * @__rescuer: self 2294e22bee78STejun Heo * 2295e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2296e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2297e22bee78STejun Heo * 2298706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2299e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2300e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2301e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2302e22bee78STejun Heo * the problem rescuer solves. 2303e22bee78STejun Heo * 2304706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2305706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2306e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2307e22bee78STejun Heo * 2308e22bee78STejun Heo * This should happen rarely. 2309e22bee78STejun Heo */ 2310111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2311e22bee78STejun Heo { 2312111c225aSTejun Heo struct worker *rescuer = __rescuer; 2313111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2314e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2315e22bee78STejun Heo 2316e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2317111c225aSTejun Heo 2318111c225aSTejun Heo /* 2319111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2320111c225aSTejun Heo * doesn't participate in concurrency management. 2321111c225aSTejun Heo */ 2322111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2323e22bee78STejun Heo repeat: 2324e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 23251da177e4SLinus Torvalds 2326412d32e6SMike Galbraith if (kthread_should_stop()) { 2327412d32e6SMike Galbraith __set_current_state(TASK_RUNNING); 2328111c225aSTejun Heo rescuer->task->flags &= ~PF_WQ_WORKER; 2329e22bee78STejun Heo return 0; 2330412d32e6SMike Galbraith } 23311da177e4SLinus Torvalds 2332493a1724STejun Heo /* see whether any pwq is asking for help */ 2333493a1724STejun Heo spin_lock_irq(&workqueue_lock); 2334493a1724STejun Heo 2335493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2336493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2337493a1724STejun Heo struct pool_workqueue, mayday_node); 2338112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2339e22bee78STejun Heo struct work_struct *work, *n; 2340e22bee78STejun Heo 2341e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2342493a1724STejun Heo list_del_init(&pwq->mayday_node); 2343493a1724STejun Heo 2344493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2345e22bee78STejun Heo 2346e22bee78STejun Heo /* migrate to the target cpu if possible */ 2347f36dc67bSLai Jiangshan worker_maybe_bind_and_lock(pool); 2348b3104104SLai Jiangshan rescuer->pool = pool; 2349e22bee78STejun Heo 2350e22bee78STejun Heo /* 2351e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2352e22bee78STejun Heo * process'em. 2353e22bee78STejun Heo */ 23546183c009STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 2355bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2356112202d9STejun Heo if (get_work_pwq(work) == pwq) 2357e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2358e22bee78STejun Heo 2359e22bee78STejun Heo process_scheduled_works(rescuer); 23607576958aSTejun Heo 23617576958aSTejun Heo /* 2362d565ed63STejun Heo * Leave this pool. If keep_working() is %true, notify a 23637576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23647576958aSTejun Heo * and stalling the execution. 23657576958aSTejun Heo */ 236663d95a91STejun Heo if (keep_working(pool)) 236763d95a91STejun Heo wake_up_worker(pool); 23687576958aSTejun Heo 2369b3104104SLai Jiangshan rescuer->pool = NULL; 2370493a1724STejun Heo spin_unlock(&pool->lock); 2371493a1724STejun Heo spin_lock(&workqueue_lock); 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 2374493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2375493a1724STejun Heo 2376111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2377111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2378e22bee78STejun Heo schedule(); 2379e22bee78STejun Heo goto repeat; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 2382fc2e4d70SOleg Nesterov struct wq_barrier { 2383fc2e4d70SOleg Nesterov struct work_struct work; 2384fc2e4d70SOleg Nesterov struct completion done; 2385fc2e4d70SOleg Nesterov }; 2386fc2e4d70SOleg Nesterov 2387fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2388fc2e4d70SOleg Nesterov { 2389fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2390fc2e4d70SOleg Nesterov complete(&barr->done); 2391fc2e4d70SOleg Nesterov } 2392fc2e4d70SOleg Nesterov 23934690c4abSTejun Heo /** 23944690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2395112202d9STejun Heo * @pwq: pwq to insert barrier into 23964690c4abSTejun Heo * @barr: wq_barrier to insert 2397affee4b2STejun Heo * @target: target work to attach @barr to 2398affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23994690c4abSTejun Heo * 2400affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2401affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2402affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2403affee4b2STejun Heo * cpu. 2404affee4b2STejun Heo * 2405affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2406affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2407affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2408affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2409affee4b2STejun Heo * after a work with LINKED flag set. 2410affee4b2STejun Heo * 2411affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2412112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24134690c4abSTejun Heo * 24144690c4abSTejun Heo * CONTEXT: 2415d565ed63STejun Heo * spin_lock_irq(pool->lock). 24164690c4abSTejun Heo */ 2417112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2418affee4b2STejun Heo struct wq_barrier *barr, 2419affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2420fc2e4d70SOleg Nesterov { 2421affee4b2STejun Heo struct list_head *head; 2422affee4b2STejun Heo unsigned int linked = 0; 2423affee4b2STejun Heo 2424dc186ad7SThomas Gleixner /* 2425d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2426dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2427dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2428dc186ad7SThomas Gleixner * might deadlock. 2429dc186ad7SThomas Gleixner */ 2430ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 243122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2432fc2e4d70SOleg Nesterov init_completion(&barr->done); 243383c22520SOleg Nesterov 2434affee4b2STejun Heo /* 2435affee4b2STejun Heo * If @target is currently being executed, schedule the 2436affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2437affee4b2STejun Heo */ 2438affee4b2STejun Heo if (worker) 2439affee4b2STejun Heo head = worker->scheduled.next; 2440affee4b2STejun Heo else { 2441affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2442affee4b2STejun Heo 2443affee4b2STejun Heo head = target->entry.next; 2444affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2445affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2446affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2447affee4b2STejun Heo } 2448affee4b2STejun Heo 2449dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2450112202d9STejun Heo insert_work(pwq, &barr->work, head, 2451affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2452fc2e4d70SOleg Nesterov } 2453fc2e4d70SOleg Nesterov 245473f53c4aSTejun Heo /** 2455112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 245673f53c4aSTejun Heo * @wq: workqueue being flushed 245773f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 245873f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 245973f53c4aSTejun Heo * 2460112202d9STejun Heo * Prepare pwqs for workqueue flushing. 246173f53c4aSTejun Heo * 2462112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2463112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2464112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2465112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2466112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 246773f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 246873f53c4aSTejun Heo * 246973f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 247073f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 247173f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 247273f53c4aSTejun Heo * is returned. 247373f53c4aSTejun Heo * 2474112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 247573f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 247673f53c4aSTejun Heo * advanced to @work_color. 247773f53c4aSTejun Heo * 247873f53c4aSTejun Heo * CONTEXT: 247973f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 248073f53c4aSTejun Heo * 248173f53c4aSTejun Heo * RETURNS: 248273f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 248373f53c4aSTejun Heo * otherwise. 248473f53c4aSTejun Heo */ 2485112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 248673f53c4aSTejun Heo int flush_color, int work_color) 24871da177e4SLinus Torvalds { 248873f53c4aSTejun Heo bool wait = false; 248949e3cf44STejun Heo struct pool_workqueue *pwq; 24901da177e4SLinus Torvalds 249173f53c4aSTejun Heo if (flush_color >= 0) { 24926183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2493112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2494dc186ad7SThomas Gleixner } 249514441960SOleg Nesterov 249649e3cf44STejun Heo for_each_pwq(pwq, wq) { 2497112202d9STejun Heo struct worker_pool *pool = pwq->pool; 24981da177e4SLinus Torvalds 2499d565ed63STejun Heo spin_lock_irq(&pool->lock); 250073f53c4aSTejun Heo 250173f53c4aSTejun Heo if (flush_color >= 0) { 25026183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 250373f53c4aSTejun Heo 2504112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2505112202d9STejun Heo pwq->flush_color = flush_color; 2506112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 250773f53c4aSTejun Heo wait = true; 25081da177e4SLinus Torvalds } 250973f53c4aSTejun Heo } 251073f53c4aSTejun Heo 251173f53c4aSTejun Heo if (work_color >= 0) { 25126183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2513112202d9STejun Heo pwq->work_color = work_color; 251473f53c4aSTejun Heo } 251573f53c4aSTejun Heo 2516d565ed63STejun Heo spin_unlock_irq(&pool->lock); 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds 2519112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 252073f53c4aSTejun Heo complete(&wq->first_flusher->done); 252173f53c4aSTejun Heo 252273f53c4aSTejun Heo return wait; 252383c22520SOleg Nesterov } 25241da177e4SLinus Torvalds 25250fcb78c2SRolf Eike Beer /** 25261da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25270fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25281da177e4SLinus Torvalds * 25291da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 25301da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 25311da177e4SLinus Torvalds * 2532fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2533fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 25341da177e4SLinus Torvalds */ 25357ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25361da177e4SLinus Torvalds { 253773f53c4aSTejun Heo struct wq_flusher this_flusher = { 253873f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 253973f53c4aSTejun Heo .flush_color = -1, 254073f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 254173f53c4aSTejun Heo }; 254273f53c4aSTejun Heo int next_color; 2543b1f4ec17SOleg Nesterov 25443295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25453295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 254673f53c4aSTejun Heo 254773f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 254873f53c4aSTejun Heo 254973f53c4aSTejun Heo /* 255073f53c4aSTejun Heo * Start-to-wait phase 255173f53c4aSTejun Heo */ 255273f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 255373f53c4aSTejun Heo 255473f53c4aSTejun Heo if (next_color != wq->flush_color) { 255573f53c4aSTejun Heo /* 255673f53c4aSTejun Heo * Color space is not full. The current work_color 255773f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 255873f53c4aSTejun Heo * by one. 255973f53c4aSTejun Heo */ 25606183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 256173f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 256273f53c4aSTejun Heo wq->work_color = next_color; 256373f53c4aSTejun Heo 256473f53c4aSTejun Heo if (!wq->first_flusher) { 256573f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 25666183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 256773f53c4aSTejun Heo 256873f53c4aSTejun Heo wq->first_flusher = &this_flusher; 256973f53c4aSTejun Heo 2570112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 257173f53c4aSTejun Heo wq->work_color)) { 257273f53c4aSTejun Heo /* nothing to flush, done */ 257373f53c4aSTejun Heo wq->flush_color = next_color; 257473f53c4aSTejun Heo wq->first_flusher = NULL; 257573f53c4aSTejun Heo goto out_unlock; 257673f53c4aSTejun Heo } 257773f53c4aSTejun Heo } else { 257873f53c4aSTejun Heo /* wait in queue */ 25796183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 258073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2581112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 258273f53c4aSTejun Heo } 258373f53c4aSTejun Heo } else { 258473f53c4aSTejun Heo /* 258573f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 258673f53c4aSTejun Heo * The next flush completion will assign us 258773f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 258873f53c4aSTejun Heo */ 258973f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 259073f53c4aSTejun Heo } 259173f53c4aSTejun Heo 259273f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 259373f53c4aSTejun Heo 259473f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 259573f53c4aSTejun Heo 259673f53c4aSTejun Heo /* 259773f53c4aSTejun Heo * Wake-up-and-cascade phase 259873f53c4aSTejun Heo * 259973f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 260073f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 260173f53c4aSTejun Heo */ 260273f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 260373f53c4aSTejun Heo return; 260473f53c4aSTejun Heo 260573f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 260673f53c4aSTejun Heo 26074ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26084ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26094ce48b37STejun Heo goto out_unlock; 26104ce48b37STejun Heo 261173f53c4aSTejun Heo wq->first_flusher = NULL; 261273f53c4aSTejun Heo 26136183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 26146183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 261573f53c4aSTejun Heo 261673f53c4aSTejun Heo while (true) { 261773f53c4aSTejun Heo struct wq_flusher *next, *tmp; 261873f53c4aSTejun Heo 261973f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 262073f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 262173f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 262273f53c4aSTejun Heo break; 262373f53c4aSTejun Heo list_del_init(&next->list); 262473f53c4aSTejun Heo complete(&next->done); 262573f53c4aSTejun Heo } 262673f53c4aSTejun Heo 26276183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 262873f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 262973f53c4aSTejun Heo 263073f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 263173f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 263273f53c4aSTejun Heo 263373f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 263473f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 263573f53c4aSTejun Heo /* 263673f53c4aSTejun Heo * Assign the same color to all overflowed 263773f53c4aSTejun Heo * flushers, advance work_color and append to 263873f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 263973f53c4aSTejun Heo * phase for these overflowed flushers. 264073f53c4aSTejun Heo */ 264173f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 264273f53c4aSTejun Heo tmp->flush_color = wq->work_color; 264373f53c4aSTejun Heo 264473f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 264573f53c4aSTejun Heo 264673f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 264773f53c4aSTejun Heo &wq->flusher_queue); 2648112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 264973f53c4aSTejun Heo } 265073f53c4aSTejun Heo 265173f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 26526183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 265373f53c4aSTejun Heo break; 265473f53c4aSTejun Heo } 265573f53c4aSTejun Heo 265673f53c4aSTejun Heo /* 265773f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2658112202d9STejun Heo * the new first flusher and arm pwqs. 265973f53c4aSTejun Heo */ 26606183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 26616183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 266273f53c4aSTejun Heo 266373f53c4aSTejun Heo list_del_init(&next->list); 266473f53c4aSTejun Heo wq->first_flusher = next; 266573f53c4aSTejun Heo 2666112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 266773f53c4aSTejun Heo break; 266873f53c4aSTejun Heo 266973f53c4aSTejun Heo /* 267073f53c4aSTejun Heo * Meh... this color is already done, clear first 267173f53c4aSTejun Heo * flusher and repeat cascading. 267273f53c4aSTejun Heo */ 267373f53c4aSTejun Heo wq->first_flusher = NULL; 267473f53c4aSTejun Heo } 267573f53c4aSTejun Heo 267673f53c4aSTejun Heo out_unlock: 267773f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26781da177e4SLinus Torvalds } 2679ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26801da177e4SLinus Torvalds 26819c5a2ba7STejun Heo /** 26829c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26839c5a2ba7STejun Heo * @wq: workqueue to drain 26849c5a2ba7STejun Heo * 26859c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26869c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26879c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26889c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26899c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26909c5a2ba7STejun Heo * takes too long. 26919c5a2ba7STejun Heo */ 26929c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26939c5a2ba7STejun Heo { 26949c5a2ba7STejun Heo unsigned int flush_cnt = 0; 269549e3cf44STejun Heo struct pool_workqueue *pwq; 26969c5a2ba7STejun Heo 26979c5a2ba7STejun Heo /* 26989c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26999c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 27009c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 27019c5a2ba7STejun Heo */ 2702e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 27039c5a2ba7STejun Heo if (!wq->nr_drainers++) 27049c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 2705e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 27069c5a2ba7STejun Heo reflush: 27079c5a2ba7STejun Heo flush_workqueue(wq); 27089c5a2ba7STejun Heo 270949e3cf44STejun Heo for_each_pwq(pwq, wq) { 2710fa2563e4SThomas Tuttle bool drained; 27119c5a2ba7STejun Heo 2712112202d9STejun Heo spin_lock_irq(&pwq->pool->lock); 2713112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2714112202d9STejun Heo spin_unlock_irq(&pwq->pool->lock); 2715fa2563e4SThomas Tuttle 2716fa2563e4SThomas Tuttle if (drained) 27179c5a2ba7STejun Heo continue; 27189c5a2ba7STejun Heo 27199c5a2ba7STejun Heo if (++flush_cnt == 10 || 27209c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2721044c782cSValentin Ilie pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 27229c5a2ba7STejun Heo wq->name, flush_cnt); 27239c5a2ba7STejun Heo goto reflush; 27249c5a2ba7STejun Heo } 27259c5a2ba7STejun Heo 2726e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 27279c5a2ba7STejun Heo if (!--wq->nr_drainers) 27289c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 2729e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 27309c5a2ba7STejun Heo } 27319c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27329c5a2ba7STejun Heo 2733606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2734baf59022STejun Heo { 2735baf59022STejun Heo struct worker *worker = NULL; 2736c9e7cf27STejun Heo struct worker_pool *pool; 2737112202d9STejun Heo struct pool_workqueue *pwq; 2738baf59022STejun Heo 2739baf59022STejun Heo might_sleep(); 2740c9e7cf27STejun Heo pool = get_work_pool(work); 2741c9e7cf27STejun Heo if (!pool) 2742baf59022STejun Heo return false; 2743baf59022STejun Heo 2744d565ed63STejun Heo spin_lock_irq(&pool->lock); 27450b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2746112202d9STejun Heo pwq = get_work_pwq(work); 2747112202d9STejun Heo if (pwq) { 2748112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2749baf59022STejun Heo goto already_gone; 2750606a5020STejun Heo } else { 2751c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2752baf59022STejun Heo if (!worker) 2753baf59022STejun Heo goto already_gone; 2754112202d9STejun Heo pwq = worker->current_pwq; 2755606a5020STejun Heo } 2756baf59022STejun Heo 2757112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2758d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2759baf59022STejun Heo 2760e159489bSTejun Heo /* 2761e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2762e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2763e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2764e159489bSTejun Heo * access. 2765e159489bSTejun Heo */ 2766112202d9STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2767112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2768e159489bSTejun Heo else 2769112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2770112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2771e159489bSTejun Heo 2772baf59022STejun Heo return true; 2773baf59022STejun Heo already_gone: 2774d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2775baf59022STejun Heo return false; 2776baf59022STejun Heo } 2777baf59022STejun Heo 2778db700897SOleg Nesterov /** 2779401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2780401a8d04STejun Heo * @work: the work to flush 2781db700897SOleg Nesterov * 2782606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2783606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2784401a8d04STejun Heo * 2785401a8d04STejun Heo * RETURNS: 2786401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2787401a8d04STejun Heo * %false if it was already idle. 2788db700897SOleg Nesterov */ 2789401a8d04STejun Heo bool flush_work(struct work_struct *work) 2790db700897SOleg Nesterov { 2791db700897SOleg Nesterov struct wq_barrier barr; 2792db700897SOleg Nesterov 27930976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 27940976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 27950976dfc1SStephen Boyd 2796606a5020STejun Heo if (start_flush_work(work, &barr)) { 2797db700897SOleg Nesterov wait_for_completion(&barr.done); 2798dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2799401a8d04STejun Heo return true; 2800606a5020STejun Heo } else { 2801401a8d04STejun Heo return false; 2802db700897SOleg Nesterov } 2803606a5020STejun Heo } 2804db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2805db700897SOleg Nesterov 280636e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2807401a8d04STejun Heo { 2808bbb68dfaSTejun Heo unsigned long flags; 28091f1f642eSOleg Nesterov int ret; 28101f1f642eSOleg Nesterov 28111f1f642eSOleg Nesterov do { 2812bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2813bbb68dfaSTejun Heo /* 2814bbb68dfaSTejun Heo * If someone else is canceling, wait for the same event it 2815bbb68dfaSTejun Heo * would be waiting for before retrying. 2816bbb68dfaSTejun Heo */ 2817bbb68dfaSTejun Heo if (unlikely(ret == -ENOENT)) 2818606a5020STejun Heo flush_work(work); 28191f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28201f1f642eSOleg Nesterov 2821bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2822bbb68dfaSTejun Heo mark_work_canceling(work); 2823bbb68dfaSTejun Heo local_irq_restore(flags); 2824bbb68dfaSTejun Heo 2825606a5020STejun Heo flush_work(work); 28267a22ad75STejun Heo clear_work_data(work); 28271f1f642eSOleg Nesterov return ret; 28281f1f642eSOleg Nesterov } 28291f1f642eSOleg Nesterov 28306e84d644SOleg Nesterov /** 2831401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2832401a8d04STejun Heo * @work: the work to cancel 28336e84d644SOleg Nesterov * 2834401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2835401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2836401a8d04STejun Heo * another workqueue. On return from this function, @work is 2837401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28381f1f642eSOleg Nesterov * 2839401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2840401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28416e84d644SOleg Nesterov * 2842401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28436e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2844401a8d04STejun Heo * 2845401a8d04STejun Heo * RETURNS: 2846401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28476e84d644SOleg Nesterov */ 2848401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28496e84d644SOleg Nesterov { 285036e227d2STejun Heo return __cancel_work_timer(work, false); 2851b89deed3SOleg Nesterov } 285228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2853b89deed3SOleg Nesterov 28546e84d644SOleg Nesterov /** 2855401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2856401a8d04STejun Heo * @dwork: the delayed work to flush 28576e84d644SOleg Nesterov * 2858401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2859401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2860401a8d04STejun Heo * considers the last queueing instance of @dwork. 28611f1f642eSOleg Nesterov * 2862401a8d04STejun Heo * RETURNS: 2863401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2864401a8d04STejun Heo * %false if it was already idle. 28656e84d644SOleg Nesterov */ 2866401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2867401a8d04STejun Heo { 28688930cabaSTejun Heo local_irq_disable(); 2869401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 287060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 28718930cabaSTejun Heo local_irq_enable(); 2872401a8d04STejun Heo return flush_work(&dwork->work); 2873401a8d04STejun Heo } 2874401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2875401a8d04STejun Heo 2876401a8d04STejun Heo /** 287757b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 287857b30ae7STejun Heo * @dwork: delayed_work to cancel 287909383498STejun Heo * 288057b30ae7STejun Heo * Kill off a pending delayed_work. Returns %true if @dwork was pending 288157b30ae7STejun Heo * and canceled; %false if wasn't pending. Note that the work callback 288257b30ae7STejun Heo * function may still be running on return, unless it returns %true and the 288357b30ae7STejun Heo * work doesn't re-arm itself. Explicitly flush or use 288457b30ae7STejun Heo * cancel_delayed_work_sync() to wait on it. 288509383498STejun Heo * 288657b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 288709383498STejun Heo */ 288857b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 288909383498STejun Heo { 289057b30ae7STejun Heo unsigned long flags; 289157b30ae7STejun Heo int ret; 289257b30ae7STejun Heo 289357b30ae7STejun Heo do { 289457b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 289557b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 289657b30ae7STejun Heo 289757b30ae7STejun Heo if (unlikely(ret < 0)) 289857b30ae7STejun Heo return false; 289957b30ae7STejun Heo 29007c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 29017c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 290257b30ae7STejun Heo local_irq_restore(flags); 2903c0158ca6SDan Magenheimer return ret; 290409383498STejun Heo } 290557b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 290609383498STejun Heo 290709383498STejun Heo /** 2908401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2909401a8d04STejun Heo * @dwork: the delayed work cancel 2910401a8d04STejun Heo * 2911401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2912401a8d04STejun Heo * 2913401a8d04STejun Heo * RETURNS: 2914401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2915401a8d04STejun Heo */ 2916401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29176e84d644SOleg Nesterov { 291836e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29196e84d644SOleg Nesterov } 2920f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29211da177e4SLinus Torvalds 29220fcb78c2SRolf Eike Beer /** 2923c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2924c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2925c1a220e7SZhang Rui * @work: job to be done 2926c1a220e7SZhang Rui * 2927c1a220e7SZhang Rui * This puts a job on a specific cpu 2928c1a220e7SZhang Rui */ 2929d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 2930c1a220e7SZhang Rui { 2931d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2932c1a220e7SZhang Rui } 2933c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2934c1a220e7SZhang Rui 29350fcb78c2SRolf Eike Beer /** 2936ae90dd5dSDave Jones * schedule_work - put work task in global workqueue 29371da177e4SLinus Torvalds * @work: job to be done 29381da177e4SLinus Torvalds * 2939d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2940d4283e93STejun Heo * %true otherwise. 294152bad64dSDavid Howells * 29420fcb78c2SRolf Eike Beer * This puts a job in the kernel-global workqueue if it was not already 29430fcb78c2SRolf Eike Beer * queued and leaves it in the same position on the kernel-global 29440fcb78c2SRolf Eike Beer * workqueue otherwise. 29451da177e4SLinus Torvalds */ 2946d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29471da177e4SLinus Torvalds { 29480fcb78c2SRolf Eike Beer return queue_work(system_wq, work); 29491da177e4SLinus Torvalds } 29500fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work); 29511da177e4SLinus Torvalds 29520fcb78c2SRolf Eike Beer /** 29530fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29540fcb78c2SRolf Eike Beer * @cpu: cpu to use 29550fcb78c2SRolf Eike Beer * @dwork: job to be done 29560fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29570fcb78c2SRolf Eike Beer * 29580fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29590fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29600fcb78c2SRolf Eike Beer */ 2961d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2962d4283e93STejun Heo unsigned long delay) 29631da177e4SLinus Torvalds { 2964d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29651da177e4SLinus Torvalds } 2966ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29671da177e4SLinus Torvalds 2968b6136773SAndrew Morton /** 29690a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29700a13c00eSTejun Heo * @dwork: job to be done 29710a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29720a13c00eSTejun Heo * 29730a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29740a13c00eSTejun Heo * workqueue. 29750a13c00eSTejun Heo */ 2976d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29770a13c00eSTejun Heo { 29780a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29790a13c00eSTejun Heo } 29800a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29810a13c00eSTejun Heo 29820a13c00eSTejun Heo /** 298331ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2984b6136773SAndrew Morton * @func: the function to call 2985b6136773SAndrew Morton * 298631ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 298731ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2988b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 298931ddd871STejun Heo * 299031ddd871STejun Heo * RETURNS: 299131ddd871STejun Heo * 0 on success, -errno on failure. 2992b6136773SAndrew Morton */ 299365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 299415316ba8SChristoph Lameter { 299515316ba8SChristoph Lameter int cpu; 299638f51568SNamhyung Kim struct work_struct __percpu *works; 299715316ba8SChristoph Lameter 2998b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2999b6136773SAndrew Morton if (!works) 300015316ba8SChristoph Lameter return -ENOMEM; 3001b6136773SAndrew Morton 300295402b38SGautham R Shenoy get_online_cpus(); 300393981800STejun Heo 300415316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30059bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30069bfb1839SIngo Molnar 30079bfb1839SIngo Molnar INIT_WORK(work, func); 30088de6d308SOleg Nesterov schedule_work_on(cpu, work); 300915316ba8SChristoph Lameter } 301093981800STejun Heo 301193981800STejun Heo for_each_online_cpu(cpu) 30128616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 301393981800STejun Heo 301495402b38SGautham R Shenoy put_online_cpus(); 3015b6136773SAndrew Morton free_percpu(works); 301615316ba8SChristoph Lameter return 0; 301715316ba8SChristoph Lameter } 301815316ba8SChristoph Lameter 3019eef6a7d5SAlan Stern /** 3020eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3021eef6a7d5SAlan Stern * 3022eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3023eef6a7d5SAlan Stern * completion. 3024eef6a7d5SAlan Stern * 3025eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3026eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3027eef6a7d5SAlan Stern * will lead to deadlock: 3028eef6a7d5SAlan Stern * 3029eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3030eef6a7d5SAlan Stern * a lock held by your code or its caller. 3031eef6a7d5SAlan Stern * 3032eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3033eef6a7d5SAlan Stern * 3034eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3035eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3036eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3037eef6a7d5SAlan Stern * 3038eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3039eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3040eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3041eef6a7d5SAlan Stern * cancel_work_sync() instead. 3042eef6a7d5SAlan Stern */ 30431da177e4SLinus Torvalds void flush_scheduled_work(void) 30441da177e4SLinus Torvalds { 3045d320c038STejun Heo flush_workqueue(system_wq); 30461da177e4SLinus Torvalds } 3047ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds /** 30501fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30511fa44ecaSJames Bottomley * @fn: the function to execute 30521fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30531fa44ecaSJames Bottomley * be available when the work executes) 30541fa44ecaSJames Bottomley * 30551fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30561fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30571fa44ecaSJames Bottomley * 30581fa44ecaSJames Bottomley * Returns: 0 - function was executed 30591fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30601fa44ecaSJames Bottomley */ 306165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30621fa44ecaSJames Bottomley { 30631fa44ecaSJames Bottomley if (!in_interrupt()) { 306465f27f38SDavid Howells fn(&ew->work); 30651fa44ecaSJames Bottomley return 0; 30661fa44ecaSJames Bottomley } 30671fa44ecaSJames Bottomley 306865f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30691fa44ecaSJames Bottomley schedule_work(&ew->work); 30701fa44ecaSJames Bottomley 30711fa44ecaSJames Bottomley return 1; 30721fa44ecaSJames Bottomley } 30731fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30741fa44ecaSJames Bottomley 30751da177e4SLinus Torvalds int keventd_up(void) 30761da177e4SLinus Torvalds { 3077d320c038STejun Heo return system_wq != NULL; 30781da177e4SLinus Torvalds } 30791da177e4SLinus Torvalds 308030cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 30811da177e4SLinus Torvalds { 308249e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 308330cdf249STejun Heo int cpu; 3084e1d8aa9fSFrederic Weisbecker 308530cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3086*420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3087*420c0ddbSTejun Heo if (!wq->cpu_pwqs) 308830cdf249STejun Heo return -ENOMEM; 308930cdf249STejun Heo 309030cdf249STejun Heo for_each_possible_cpu(cpu) { 309130cdf249STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 309230cdf249STejun Heo 309349e3cf44STejun Heo pwq->pool = get_std_worker_pool(cpu, highpri); 309430cdf249STejun Heo list_add_tail(&pwq->pwqs_node, &wq->pwqs); 309530cdf249STejun Heo } 309630cdf249STejun Heo } else { 309730cdf249STejun Heo struct pool_workqueue *pwq; 309830cdf249STejun Heo 309930cdf249STejun Heo pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL); 310030cdf249STejun Heo if (!pwq) 310130cdf249STejun Heo return -ENOMEM; 310230cdf249STejun Heo 310349e3cf44STejun Heo pwq->pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri); 310430cdf249STejun Heo list_add_tail(&pwq->pwqs_node, &wq->pwqs); 310530cdf249STejun Heo } 310630cdf249STejun Heo 310730cdf249STejun Heo return 0; 31080f900049STejun Heo } 31090f900049STejun Heo 3110112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq) 311106ba38a9SOleg Nesterov { 3112e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3113*420c0ddbSTejun Heo free_percpu(wq->cpu_pwqs); 3114*420c0ddbSTejun Heo else if (!list_empty(&wq->pwqs)) 3115*420c0ddbSTejun Heo kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs, 3116*420c0ddbSTejun Heo struct pool_workqueue, pwqs_node)); 311706ba38a9SOleg Nesterov } 311806ba38a9SOleg Nesterov 3119f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3120f3421797STejun Heo const char *name) 3121b71ab8c2STejun Heo { 3122f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3123f3421797STejun Heo 3124f3421797STejun Heo if (max_active < 1 || max_active > lim) 3125044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3126f3421797STejun Heo max_active, name, 1, lim); 3127b71ab8c2STejun Heo 3128f3421797STejun Heo return clamp_val(max_active, 1, lim); 3129b71ab8c2STejun Heo } 3130b71ab8c2STejun Heo 3131b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 313297e37d7bSTejun Heo unsigned int flags, 31331e19ffc6STejun Heo int max_active, 3134eb13ba87SJohannes Berg struct lock_class_key *key, 3135b196be89STejun Heo const char *lock_name, ...) 31363af24433SOleg Nesterov { 3137b196be89STejun Heo va_list args, args1; 31383af24433SOleg Nesterov struct workqueue_struct *wq; 313949e3cf44STejun Heo struct pool_workqueue *pwq; 3140b196be89STejun Heo size_t namelen; 3141b196be89STejun Heo 3142b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3143b196be89STejun Heo va_start(args, lock_name); 3144b196be89STejun Heo va_copy(args1, args); 3145b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3146b196be89STejun Heo 3147b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3148b196be89STejun Heo if (!wq) 3149b196be89STejun Heo goto err; 3150b196be89STejun Heo 3151b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3152b196be89STejun Heo va_end(args); 3153b196be89STejun Heo va_end(args1); 31543af24433SOleg Nesterov 3155f3421797STejun Heo /* 31566370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31576370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31586370a6adSTejun Heo */ 31596370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31606370a6adSTejun Heo flags |= WQ_RESCUER; 31616370a6adSTejun Heo 3162d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3163b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31643af24433SOleg Nesterov 3165b196be89STejun Heo /* init wq */ 316697e37d7bSTejun Heo wq->flags = flags; 3167a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 316873f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 3169112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 317030cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 317173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 317273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3173493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 31743af24433SOleg Nesterov 3175eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3176cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31773af24433SOleg Nesterov 317830cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 3179bdbc5dd7STejun Heo goto err; 3180bdbc5dd7STejun Heo 318149e3cf44STejun Heo for_each_pwq(pwq, wq) { 3182112202d9STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3183112202d9STejun Heo pwq->wq = wq; 3184112202d9STejun Heo pwq->flush_color = -1; 3185112202d9STejun Heo pwq->max_active = max_active; 3186112202d9STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 3187493a1724STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 3188e22bee78STejun Heo } 31891537663fSTejun Heo 3190e22bee78STejun Heo if (flags & WQ_RESCUER) { 3191e22bee78STejun Heo struct worker *rescuer; 3192e22bee78STejun Heo 3193e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3194e22bee78STejun Heo if (!rescuer) 3195e22bee78STejun Heo goto err; 3196e22bee78STejun Heo 3197111c225aSTejun Heo rescuer->rescue_wq = wq; 3198111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3199b196be89STejun Heo wq->name); 3200e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3201e22bee78STejun Heo goto err; 3202e22bee78STejun Heo 3203e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3204e22bee78STejun Heo wake_up_process(rescuer->task); 32053af24433SOleg Nesterov } 32061537663fSTejun Heo 32073af24433SOleg Nesterov /* 3208a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3209a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3210a0a1a5fdSTejun Heo * workqueue to workqueues list. 32113af24433SOleg Nesterov */ 3212e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3213a0a1a5fdSTejun Heo 321458a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 321549e3cf44STejun Heo for_each_pwq(pwq, wq) 321649e3cf44STejun Heo pwq->max_active = 0; 3217a0a1a5fdSTejun Heo 32183af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3219a0a1a5fdSTejun Heo 3220e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 32213af24433SOleg Nesterov 32223af24433SOleg Nesterov return wq; 32234690c4abSTejun Heo err: 32244690c4abSTejun Heo if (wq) { 3225112202d9STejun Heo free_pwqs(wq); 3226e22bee78STejun Heo kfree(wq->rescuer); 32274690c4abSTejun Heo kfree(wq); 32283af24433SOleg Nesterov } 32294690c4abSTejun Heo return NULL; 32301da177e4SLinus Torvalds } 3231d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32321da177e4SLinus Torvalds 32333af24433SOleg Nesterov /** 32343af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32353af24433SOleg Nesterov * @wq: target workqueue 32363af24433SOleg Nesterov * 32373af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32383af24433SOleg Nesterov */ 32393af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32403af24433SOleg Nesterov { 324149e3cf44STejun Heo struct pool_workqueue *pwq; 32423af24433SOleg Nesterov 32439c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32449c5a2ba7STejun Heo drain_workqueue(wq); 3245c8efcc25STejun Heo 32466183c009STejun Heo /* sanity checks */ 324749e3cf44STejun Heo for_each_pwq(pwq, wq) { 32486183c009STejun Heo int i; 32496183c009STejun Heo 32506183c009STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 32516183c009STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) 32526183c009STejun Heo return; 32536183c009STejun Heo if (WARN_ON(pwq->nr_active) || 32546183c009STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) 32556183c009STejun Heo return; 32566183c009STejun Heo } 32576183c009STejun Heo 3258a0a1a5fdSTejun Heo /* 3259a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3260a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3261a0a1a5fdSTejun Heo */ 3262e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 32633af24433SOleg Nesterov list_del(&wq->list); 3264e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 32653af24433SOleg Nesterov 3266e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3267e22bee78STejun Heo kthread_stop(wq->rescuer->task); 32688d9df9f0SXiaotian Feng kfree(wq->rescuer); 3269e22bee78STejun Heo } 3270e22bee78STejun Heo 3271112202d9STejun Heo free_pwqs(wq); 32723af24433SOleg Nesterov kfree(wq); 32733af24433SOleg Nesterov } 32743af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32753af24433SOleg Nesterov 3276dcd989cbSTejun Heo /** 3277112202d9STejun Heo * pwq_set_max_active - adjust max_active of a pwq 3278112202d9STejun Heo * @pwq: target pool_workqueue 32799f4bd4cdSLai Jiangshan * @max_active: new max_active value. 32809f4bd4cdSLai Jiangshan * 3281112202d9STejun Heo * Set @pwq->max_active to @max_active and activate delayed works if 32829f4bd4cdSLai Jiangshan * increased. 32839f4bd4cdSLai Jiangshan * 32849f4bd4cdSLai Jiangshan * CONTEXT: 3285d565ed63STejun Heo * spin_lock_irq(pool->lock). 32869f4bd4cdSLai Jiangshan */ 3287112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 32889f4bd4cdSLai Jiangshan { 3289112202d9STejun Heo pwq->max_active = max_active; 32909f4bd4cdSLai Jiangshan 3291112202d9STejun Heo while (!list_empty(&pwq->delayed_works) && 3292112202d9STejun Heo pwq->nr_active < pwq->max_active) 3293112202d9STejun Heo pwq_activate_first_delayed(pwq); 32949f4bd4cdSLai Jiangshan } 32959f4bd4cdSLai Jiangshan 32969f4bd4cdSLai Jiangshan /** 3297dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3298dcd989cbSTejun Heo * @wq: target workqueue 3299dcd989cbSTejun Heo * @max_active: new max_active value. 3300dcd989cbSTejun Heo * 3301dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3302dcd989cbSTejun Heo * 3303dcd989cbSTejun Heo * CONTEXT: 3304dcd989cbSTejun Heo * Don't call from IRQ context. 3305dcd989cbSTejun Heo */ 3306dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3307dcd989cbSTejun Heo { 330849e3cf44STejun Heo struct pool_workqueue *pwq; 3309dcd989cbSTejun Heo 3310f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3311dcd989cbSTejun Heo 3312e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3313dcd989cbSTejun Heo 3314dcd989cbSTejun Heo wq->saved_max_active = max_active; 3315dcd989cbSTejun Heo 331649e3cf44STejun Heo for_each_pwq(pwq, wq) { 3317112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3318dcd989cbSTejun Heo 3319e98d5b16STejun Heo spin_lock(&pool->lock); 3320dcd989cbSTejun Heo 332158a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 332235b6bb63STejun Heo !(pool->flags & POOL_FREEZING)) 3323112202d9STejun Heo pwq_set_max_active(pwq, max_active); 3324dcd989cbSTejun Heo 3325e98d5b16STejun Heo spin_unlock(&pool->lock); 3326dcd989cbSTejun Heo } 3327dcd989cbSTejun Heo 3328e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3329dcd989cbSTejun Heo } 3330dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3331dcd989cbSTejun Heo 3332dcd989cbSTejun Heo /** 3333dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3334dcd989cbSTejun Heo * @cpu: CPU in question 3335dcd989cbSTejun Heo * @wq: target workqueue 3336dcd989cbSTejun Heo * 3337dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3338dcd989cbSTejun Heo * no synchronization around this function and the test result is 3339dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3340dcd989cbSTejun Heo * 3341dcd989cbSTejun Heo * RETURNS: 3342dcd989cbSTejun Heo * %true if congested, %false otherwise. 3343dcd989cbSTejun Heo */ 3344d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 3345dcd989cbSTejun Heo { 3346112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3347dcd989cbSTejun Heo 3348112202d9STejun Heo return !list_empty(&pwq->delayed_works); 3349dcd989cbSTejun Heo } 3350dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3351dcd989cbSTejun Heo 3352dcd989cbSTejun Heo /** 3353dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3354dcd989cbSTejun Heo * @work: the work to be tested 3355dcd989cbSTejun Heo * 3356dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3357dcd989cbSTejun Heo * synchronization around this function and the test result is 3358dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3359dcd989cbSTejun Heo * 3360dcd989cbSTejun Heo * RETURNS: 3361dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3362dcd989cbSTejun Heo */ 3363dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3364dcd989cbSTejun Heo { 3365c9e7cf27STejun Heo struct worker_pool *pool = get_work_pool(work); 3366dcd989cbSTejun Heo unsigned long flags; 3367dcd989cbSTejun Heo unsigned int ret = 0; 3368dcd989cbSTejun Heo 3369dcd989cbSTejun Heo if (work_pending(work)) 3370dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3371038366c5SLai Jiangshan 3372038366c5SLai Jiangshan if (pool) { 3373038366c5SLai Jiangshan spin_lock_irqsave(&pool->lock, flags); 3374c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 3375dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3376d565ed63STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 3377038366c5SLai Jiangshan } 3378dcd989cbSTejun Heo 3379dcd989cbSTejun Heo return ret; 3380dcd989cbSTejun Heo } 3381dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3382dcd989cbSTejun Heo 3383db7bccf4STejun Heo /* 3384db7bccf4STejun Heo * CPU hotplug. 3385db7bccf4STejun Heo * 3386e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3387112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 3388706026c2STejun Heo * pool which make migrating pending and scheduled works very 3389e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 339094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 3391e22bee78STejun Heo * blocked draining impractical. 3392e22bee78STejun Heo * 339324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 3394628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3395628c78e7STejun Heo * cpu comes back online. 3396db7bccf4STejun Heo */ 3397db7bccf4STejun Heo 3398706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 3399db7bccf4STejun Heo { 340038db41d9STejun Heo int cpu = smp_processor_id(); 34014ce62e9eSTejun Heo struct worker_pool *pool; 3402db7bccf4STejun Heo struct worker *worker; 3403db7bccf4STejun Heo int i; 3404db7bccf4STejun Heo 340538db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 34066183c009STejun Heo WARN_ON_ONCE(cpu != smp_processor_id()); 3407db7bccf4STejun Heo 340894cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 340994cf58bbSTejun Heo spin_lock_irq(&pool->lock); 3410e22bee78STejun Heo 3411f2d5a0eeSTejun Heo /* 341294cf58bbSTejun Heo * We've claimed all manager positions. Make all workers 341394cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 341494cf58bbSTejun Heo * except for the ones which are still executing works from 341594cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 341694cf58bbSTejun Heo * this, they may become diasporas. 3417f2d5a0eeSTejun Heo */ 34184ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3419403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3420db7bccf4STejun Heo 3421b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) 3422403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3423db7bccf4STejun Heo 342424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 3425f2d5a0eeSTejun Heo 342694cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 342794cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 342894cf58bbSTejun Heo } 3429e22bee78STejun Heo 3430e22bee78STejun Heo /* 3431628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3432628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3433628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3434628c78e7STejun Heo */ 3435628c78e7STejun Heo schedule(); 3436628c78e7STejun Heo 3437628c78e7STejun Heo /* 3438628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3439628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 344038db41d9STejun Heo * are always true as long as the worklist is not empty. Pools on 344138db41d9STejun Heo * @cpu now behave as unbound (in terms of concurrency management) 344238db41d9STejun Heo * pools which are served by workers tied to the CPU. 3443628c78e7STejun Heo * 3444628c78e7STejun Heo * On return from this function, the current worker would trigger 3445628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3446628c78e7STejun Heo * didn't already. 3447e22bee78STejun Heo */ 344838db41d9STejun Heo for_each_std_worker_pool(pool, cpu) 3449e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 3450db7bccf4STejun Heo } 3451db7bccf4STejun Heo 34528db25e78STejun Heo /* 34538db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34548db25e78STejun Heo * This will be registered high priority CPU notifier. 34558db25e78STejun Heo */ 34569fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34571da177e4SLinus Torvalds unsigned long action, 34581da177e4SLinus Torvalds void *hcpu) 34591da177e4SLinus Torvalds { 3460d84ff051STejun Heo int cpu = (unsigned long)hcpu; 34614ce62e9eSTejun Heo struct worker_pool *pool; 34621da177e4SLinus Torvalds 34638db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34643af24433SOleg Nesterov case CPU_UP_PREPARE: 346538db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 34663ce63377STejun Heo struct worker *worker; 34673ce63377STejun Heo 34683ce63377STejun Heo if (pool->nr_workers) 34693ce63377STejun Heo continue; 34703ce63377STejun Heo 34713ce63377STejun Heo worker = create_worker(pool); 34723ce63377STejun Heo if (!worker) 34733ce63377STejun Heo return NOTIFY_BAD; 34743ce63377STejun Heo 3475d565ed63STejun Heo spin_lock_irq(&pool->lock); 34763ce63377STejun Heo start_worker(worker); 3477d565ed63STejun Heo spin_unlock_irq(&pool->lock); 34783af24433SOleg Nesterov } 34791da177e4SLinus Torvalds break; 34801da177e4SLinus Torvalds 348165758202STejun Heo case CPU_DOWN_FAILED: 348265758202STejun Heo case CPU_ONLINE: 348338db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 348494cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 348594cf58bbSTejun Heo spin_lock_irq(&pool->lock); 348694cf58bbSTejun Heo 348724647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 348894cf58bbSTejun Heo rebind_workers(pool); 348994cf58bbSTejun Heo 349094cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 349194cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 349294cf58bbSTejun Heo } 34938db25e78STejun Heo break; 349465758202STejun Heo } 349565758202STejun Heo return NOTIFY_OK; 349665758202STejun Heo } 349765758202STejun Heo 349865758202STejun Heo /* 349965758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 350065758202STejun Heo * This will be registered as low priority CPU notifier. 350165758202STejun Heo */ 35029fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, 350365758202STejun Heo unsigned long action, 350465758202STejun Heo void *hcpu) 350565758202STejun Heo { 3506d84ff051STejun Heo int cpu = (unsigned long)hcpu; 35078db25e78STejun Heo struct work_struct unbind_work; 35088db25e78STejun Heo 350965758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 351065758202STejun Heo case CPU_DOWN_PREPARE: 35118db25e78STejun Heo /* unbinding should happen on the local CPU */ 3512706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 35137635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 35148db25e78STejun Heo flush_work(&unbind_work); 35158db25e78STejun Heo break; 351665758202STejun Heo } 351765758202STejun Heo return NOTIFY_OK; 351865758202STejun Heo } 351965758202STejun Heo 35202d3854a3SRusty Russell #ifdef CONFIG_SMP 35218ccad40dSRusty Russell 35222d3854a3SRusty Russell struct work_for_cpu { 3523ed48ece2STejun Heo struct work_struct work; 35242d3854a3SRusty Russell long (*fn)(void *); 35252d3854a3SRusty Russell void *arg; 35262d3854a3SRusty Russell long ret; 35272d3854a3SRusty Russell }; 35282d3854a3SRusty Russell 3529ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 35302d3854a3SRusty Russell { 3531ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 3532ed48ece2STejun Heo 35332d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35342d3854a3SRusty Russell } 35352d3854a3SRusty Russell 35362d3854a3SRusty Russell /** 35372d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35382d3854a3SRusty Russell * @cpu: the cpu to run on 35392d3854a3SRusty Russell * @fn: the function to run 35402d3854a3SRusty Russell * @arg: the function arg 35412d3854a3SRusty Russell * 354231ad9081SRusty Russell * This will return the value @fn returns. 354331ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35446b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35452d3854a3SRusty Russell */ 3546d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 35472d3854a3SRusty Russell { 3548ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 35492d3854a3SRusty Russell 3550ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 3551ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 3552ed48ece2STejun Heo flush_work(&wfc.work); 35532d3854a3SRusty Russell return wfc.ret; 35542d3854a3SRusty Russell } 35552d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35562d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35572d3854a3SRusty Russell 3558a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3559e7577c50SRusty Russell 3560a0a1a5fdSTejun Heo /** 3561a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3562a0a1a5fdSTejun Heo * 356358a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 356458a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 3565706026c2STejun Heo * pool->worklist. 3566a0a1a5fdSTejun Heo * 3567a0a1a5fdSTejun Heo * CONTEXT: 3568d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3569a0a1a5fdSTejun Heo */ 3570a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3571a0a1a5fdSTejun Heo { 357217116969STejun Heo struct worker_pool *pool; 357324b8a847STejun Heo struct workqueue_struct *wq; 357424b8a847STejun Heo struct pool_workqueue *pwq; 357517116969STejun Heo int id; 3576a0a1a5fdSTejun Heo 3577e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3578a0a1a5fdSTejun Heo 35796183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 3580a0a1a5fdSTejun Heo workqueue_freezing = true; 3581a0a1a5fdSTejun Heo 358224b8a847STejun Heo /* set FREEZING */ 358317116969STejun Heo for_each_pool(pool, id) { 3584e98d5b16STejun Heo spin_lock(&pool->lock); 358535b6bb63STejun Heo WARN_ON_ONCE(pool->flags & POOL_FREEZING); 358635b6bb63STejun Heo pool->flags |= POOL_FREEZING; 358724b8a847STejun Heo spin_unlock(&pool->lock); 35881da177e4SLinus Torvalds } 35898b03ae3cSTejun Heo 359024b8a847STejun Heo /* suppress further executions by setting max_active to zero */ 359124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 359224b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 359324b8a847STejun Heo continue; 359424b8a847STejun Heo 359524b8a847STejun Heo for_each_pwq(pwq, wq) { 359624b8a847STejun Heo spin_lock(&pwq->pool->lock); 359724b8a847STejun Heo pwq->max_active = 0; 359824b8a847STejun Heo spin_unlock(&pwq->pool->lock); 359924b8a847STejun Heo } 3600a1056305STejun Heo } 3601a0a1a5fdSTejun Heo 3602e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3603a0a1a5fdSTejun Heo } 3604a0a1a5fdSTejun Heo 3605a0a1a5fdSTejun Heo /** 360658a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3607a0a1a5fdSTejun Heo * 3608a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3609a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3610a0a1a5fdSTejun Heo * 3611a0a1a5fdSTejun Heo * CONTEXT: 3612a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3613a0a1a5fdSTejun Heo * 3614a0a1a5fdSTejun Heo * RETURNS: 361558a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 361658a69cb4STejun Heo * is complete. 3617a0a1a5fdSTejun Heo */ 3618a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3619a0a1a5fdSTejun Heo { 3620a0a1a5fdSTejun Heo bool busy = false; 362124b8a847STejun Heo struct workqueue_struct *wq; 362224b8a847STejun Heo struct pool_workqueue *pwq; 3623a0a1a5fdSTejun Heo 3624e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3625a0a1a5fdSTejun Heo 36266183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 3627a0a1a5fdSTejun Heo 362824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 362924b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 363024b8a847STejun Heo continue; 3631a0a1a5fdSTejun Heo /* 3632a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3633a0a1a5fdSTejun Heo * to peek without lock. 3634a0a1a5fdSTejun Heo */ 363524b8a847STejun Heo for_each_pwq(pwq, wq) { 36366183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 3637112202d9STejun Heo if (pwq->nr_active) { 3638a0a1a5fdSTejun Heo busy = true; 3639a0a1a5fdSTejun Heo goto out_unlock; 3640a0a1a5fdSTejun Heo } 3641a0a1a5fdSTejun Heo } 3642a0a1a5fdSTejun Heo } 3643a0a1a5fdSTejun Heo out_unlock: 3644e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3645a0a1a5fdSTejun Heo return busy; 3646a0a1a5fdSTejun Heo } 3647a0a1a5fdSTejun Heo 3648a0a1a5fdSTejun Heo /** 3649a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3650a0a1a5fdSTejun Heo * 3651a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 3652706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 3653a0a1a5fdSTejun Heo * 3654a0a1a5fdSTejun Heo * CONTEXT: 3655d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3656a0a1a5fdSTejun Heo */ 3657a0a1a5fdSTejun Heo void thaw_workqueues(void) 3658a0a1a5fdSTejun Heo { 365924b8a847STejun Heo struct workqueue_struct *wq; 366024b8a847STejun Heo struct pool_workqueue *pwq; 366124b8a847STejun Heo struct worker_pool *pool; 366224b8a847STejun Heo int id; 3663a0a1a5fdSTejun Heo 3664e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3665a0a1a5fdSTejun Heo 3666a0a1a5fdSTejun Heo if (!workqueue_freezing) 3667a0a1a5fdSTejun Heo goto out_unlock; 3668a0a1a5fdSTejun Heo 366924b8a847STejun Heo /* clear FREEZING */ 367024b8a847STejun Heo for_each_pool(pool, id) { 3671e98d5b16STejun Heo spin_lock(&pool->lock); 367235b6bb63STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 367335b6bb63STejun Heo pool->flags &= ~POOL_FREEZING; 3674e98d5b16STejun Heo spin_unlock(&pool->lock); 3675d565ed63STejun Heo } 367624b8a847STejun Heo 367724b8a847STejun Heo /* restore max_active and repopulate worklist */ 367824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 367924b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 368024b8a847STejun Heo continue; 368124b8a847STejun Heo 368224b8a847STejun Heo for_each_pwq(pwq, wq) { 368324b8a847STejun Heo spin_lock(&pwq->pool->lock); 368424b8a847STejun Heo pwq_set_max_active(pwq, wq->saved_max_active); 368524b8a847STejun Heo spin_unlock(&pwq->pool->lock); 368624b8a847STejun Heo } 368724b8a847STejun Heo } 368824b8a847STejun Heo 368924b8a847STejun Heo /* kick workers */ 369024b8a847STejun Heo for_each_pool(pool, id) { 369124b8a847STejun Heo spin_lock(&pool->lock); 369224b8a847STejun Heo wake_up_worker(pool); 369324b8a847STejun Heo spin_unlock(&pool->lock); 3694a0a1a5fdSTejun Heo } 3695a0a1a5fdSTejun Heo 3696a0a1a5fdSTejun Heo workqueue_freezing = false; 3697a0a1a5fdSTejun Heo out_unlock: 3698e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3699a0a1a5fdSTejun Heo } 3700a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3701a0a1a5fdSTejun Heo 37026ee0578bSSuresh Siddha static int __init init_workqueues(void) 37031da177e4SLinus Torvalds { 3704d84ff051STejun Heo int cpu; 3705c34056a3STejun Heo 37067c3eed5cSTejun Heo /* make sure we have enough bits for OFFQ pool ID */ 37077c3eed5cSTejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 37086be19588SLai Jiangshan WORK_CPU_END * NR_STD_WORKER_POOLS); 3709b5490077STejun Heo 3710e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 3711e904e6c2STejun Heo 3712e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 3713e904e6c2STejun Heo 371465758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 3715a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37168b03ae3cSTejun Heo 3717706026c2STejun Heo /* initialize CPU pools */ 3718706026c2STejun Heo for_each_wq_cpu(cpu) { 37194ce62e9eSTejun Heo struct worker_pool *pool; 37208b03ae3cSTejun Heo 372138db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 3722d565ed63STejun Heo spin_lock_init(&pool->lock); 3723ec22ca5eSTejun Heo pool->cpu = cpu; 372424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 37254ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37264ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3727c9e7cf27STejun Heo hash_init(pool->busy_hash); 3728e22bee78STejun Heo 37294ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37304ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37314ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3732e22bee78STejun Heo 3733706026c2STejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 37344ce62e9eSTejun Heo (unsigned long)pool); 37354ce62e9eSTejun Heo 3736b2eb83d1SLai Jiangshan mutex_init(&pool->assoc_mutex); 37374ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37389daf9e67STejun Heo 37399daf9e67STejun Heo /* alloc pool ID */ 37409daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 37414ce62e9eSTejun Heo } 37428b03ae3cSTejun Heo } 37438b03ae3cSTejun Heo 3744e22bee78STejun Heo /* create the initial worker */ 3745706026c2STejun Heo for_each_online_wq_cpu(cpu) { 37464ce62e9eSTejun Heo struct worker_pool *pool; 3747e22bee78STejun Heo 374838db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 37494ce62e9eSTejun Heo struct worker *worker; 37504ce62e9eSTejun Heo 375124647570STejun Heo if (cpu != WORK_CPU_UNBOUND) 375224647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 375324647570STejun Heo 3754bc2ae0f5STejun Heo worker = create_worker(pool); 3755e22bee78STejun Heo BUG_ON(!worker); 3756d565ed63STejun Heo spin_lock_irq(&pool->lock); 3757e22bee78STejun Heo start_worker(worker); 3758d565ed63STejun Heo spin_unlock_irq(&pool->lock); 3759e22bee78STejun Heo } 37604ce62e9eSTejun Heo } 3761e22bee78STejun Heo 3762d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 37631aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 3764d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3765f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3766f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 376724d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 376824d51addSTejun Heo WQ_FREEZABLE, 0); 37691aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 3770ae930e0fSTejun Heo !system_unbound_wq || !system_freezable_wq); 37716ee0578bSSuresh Siddha return 0; 37721da177e4SLinus Torvalds } 37736ee0578bSSuresh Siddha early_initcall(init_workqueues); 3774