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 <dwmw2@infradead.org> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 101da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 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 <tj@kernel.org> 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 */ 127ec22ca5eSTejun Heo unsigned 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 */ 1720f900049STejun Heo }; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 17573f53c4aSTejun Heo * Structure used to wait for workqueue flush. 17673f53c4aSTejun Heo */ 17773f53c4aSTejun Heo struct wq_flusher { 17873f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 17973f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 18073f53c4aSTejun Heo struct completion done; /* flush completion */ 18173f53c4aSTejun Heo }; 1821da177e4SLinus Torvalds 18373f53c4aSTejun Heo /* 184f2e005aaSTejun Heo * All cpumasks are assumed to be always set on UP and thus can't be 185f2e005aaSTejun Heo * used to determine whether there's something to be done. 186f2e005aaSTejun Heo */ 187f2e005aaSTejun Heo #ifdef CONFIG_SMP 188f2e005aaSTejun Heo typedef cpumask_var_t mayday_mask_t; 189f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) \ 190f2e005aaSTejun Heo cpumask_test_and_set_cpu((cpu), (mask)) 191f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) 192f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) 1939c37547aSTejun Heo #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp)) 194f2e005aaSTejun Heo #define free_mayday_mask(mask) free_cpumask_var((mask)) 195f2e005aaSTejun Heo #else 196f2e005aaSTejun Heo typedef unsigned long mayday_mask_t; 197f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask)) 198f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask)) 199f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask)) 200f2e005aaSTejun Heo #define alloc_mayday_mask(maskp, gfp) true 201f2e005aaSTejun Heo #define free_mayday_mask(mask) do { } while (0) 202f2e005aaSTejun Heo #endif 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /* 2051da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 2061da177e4SLinus Torvalds * per-CPU workqueues: 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds struct workqueue_struct { 2099c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 210bdbc5dd7STejun Heo union { 211112202d9STejun Heo struct pool_workqueue __percpu *pcpu; 212112202d9STejun Heo struct pool_workqueue *single; 213bdbc5dd7STejun Heo unsigned long v; 214112202d9STejun Heo } pool_wq; /* I: pwq's */ 2154690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 21673f53c4aSTejun Heo 21773f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 21873f53c4aSTejun Heo int work_color; /* F: current work color */ 21973f53c4aSTejun Heo int flush_color; /* F: current flush color */ 220112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 22173f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 22273f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 22373f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 22473f53c4aSTejun Heo 225f2e005aaSTejun Heo mayday_mask_t mayday_mask; /* cpus requesting rescue */ 226e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 227e22bee78STejun Heo 2289c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 229112202d9STejun Heo int saved_max_active; /* W: saved pwq max_active */ 2304e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2314e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2324e6045f1SJohannes Berg #endif 233b196be89STejun Heo char name[]; /* I: workqueue name */ 2341da177e4SLinus Torvalds }; 2351da177e4SLinus Torvalds 236d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 237d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 238044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 2391aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 240044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 241d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 242044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 243f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 244044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 24524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 246d320c038STejun Heo 24797bd2347STejun Heo #define CREATE_TRACE_POINTS 24897bd2347STejun Heo #include <trace/events/workqueue.h> 24997bd2347STejun Heo 25038db41d9STejun Heo #define for_each_std_worker_pool(pool, cpu) \ 251a60dc39cSTejun Heo for ((pool) = &std_worker_pools(cpu)[0]; \ 252a60dc39cSTejun Heo (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++) 2534ce62e9eSTejun Heo 254b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool) \ 255b67bfe0dSSasha Levin hash_for_each(pool->busy_hash, i, worker, hentry) 256db7bccf4STejun Heo 257706026c2STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 258f3421797STejun Heo unsigned int sw) 259f3421797STejun Heo { 260f3421797STejun Heo if (cpu < nr_cpu_ids) { 261f3421797STejun Heo if (sw & 1) { 262f3421797STejun Heo cpu = cpumask_next(cpu, mask); 263f3421797STejun Heo if (cpu < nr_cpu_ids) 264f3421797STejun Heo return cpu; 265f3421797STejun Heo } 266f3421797STejun Heo if (sw & 2) 267f3421797STejun Heo return WORK_CPU_UNBOUND; 268f3421797STejun Heo } 2696be19588SLai Jiangshan return WORK_CPU_END; 270f3421797STejun Heo } 271f3421797STejun Heo 272112202d9STejun Heo static inline int __next_pwq_cpu(int cpu, const struct cpumask *mask, 273f3421797STejun Heo struct workqueue_struct *wq) 274f3421797STejun Heo { 275706026c2STejun Heo return __next_wq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2); 276f3421797STejun Heo } 277f3421797STejun Heo 27809884951STejun Heo /* 27909884951STejun Heo * CPU iterators 28009884951STejun Heo * 281706026c2STejun Heo * An extra cpu number is defined using an invalid cpu number 28209884951STejun Heo * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 283706026c2STejun Heo * specific CPU. The following iterators are similar to for_each_*_cpu() 284706026c2STejun Heo * iterators but also considers the unbound CPU. 28509884951STejun Heo * 286706026c2STejun Heo * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND 287706026c2STejun Heo * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND 288112202d9STejun Heo * for_each_pwq_cpu() : possible CPUs for bound workqueues, 28909884951STejun Heo * WORK_CPU_UNBOUND for unbound workqueues 29009884951STejun Heo */ 291706026c2STejun Heo #define for_each_wq_cpu(cpu) \ 292706026c2STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \ 2936be19588SLai Jiangshan (cpu) < WORK_CPU_END; \ 294706026c2STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3)) 295f3421797STejun Heo 296706026c2STejun Heo #define for_each_online_wq_cpu(cpu) \ 297706026c2STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \ 2986be19588SLai Jiangshan (cpu) < WORK_CPU_END; \ 299706026c2STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3)) 300f3421797STejun Heo 301112202d9STejun Heo #define for_each_pwq_cpu(cpu, wq) \ 302112202d9STejun Heo for ((cpu) = __next_pwq_cpu(-1, cpu_possible_mask, (wq)); \ 3036be19588SLai Jiangshan (cpu) < WORK_CPU_END; \ 304112202d9STejun Heo (cpu) = __next_pwq_cpu((cpu), cpu_possible_mask, (wq))) 305f3421797STejun Heo 306dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 307dc186ad7SThomas Gleixner 308dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 309dc186ad7SThomas Gleixner 31099777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 31199777288SStanislaw Gruszka { 31299777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 31399777288SStanislaw Gruszka } 31499777288SStanislaw Gruszka 315dc186ad7SThomas Gleixner /* 316dc186ad7SThomas Gleixner * fixup_init is called when: 317dc186ad7SThomas Gleixner * - an active object is initialized 318dc186ad7SThomas Gleixner */ 319dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 320dc186ad7SThomas Gleixner { 321dc186ad7SThomas Gleixner struct work_struct *work = addr; 322dc186ad7SThomas Gleixner 323dc186ad7SThomas Gleixner switch (state) { 324dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 325dc186ad7SThomas Gleixner cancel_work_sync(work); 326dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 327dc186ad7SThomas Gleixner return 1; 328dc186ad7SThomas Gleixner default: 329dc186ad7SThomas Gleixner return 0; 330dc186ad7SThomas Gleixner } 331dc186ad7SThomas Gleixner } 332dc186ad7SThomas Gleixner 333dc186ad7SThomas Gleixner /* 334dc186ad7SThomas Gleixner * fixup_activate is called when: 335dc186ad7SThomas Gleixner * - an active object is activated 336dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 337dc186ad7SThomas Gleixner */ 338dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 339dc186ad7SThomas Gleixner { 340dc186ad7SThomas Gleixner struct work_struct *work = addr; 341dc186ad7SThomas Gleixner 342dc186ad7SThomas Gleixner switch (state) { 343dc186ad7SThomas Gleixner 344dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 345dc186ad7SThomas Gleixner /* 346dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 347dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 348dc186ad7SThomas Gleixner * is tracked in the object tracker. 349dc186ad7SThomas Gleixner */ 35022df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 351dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 352dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 353dc186ad7SThomas Gleixner return 0; 354dc186ad7SThomas Gleixner } 355dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 356dc186ad7SThomas Gleixner return 0; 357dc186ad7SThomas Gleixner 358dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 359dc186ad7SThomas Gleixner WARN_ON(1); 360dc186ad7SThomas Gleixner 361dc186ad7SThomas Gleixner default: 362dc186ad7SThomas Gleixner return 0; 363dc186ad7SThomas Gleixner } 364dc186ad7SThomas Gleixner } 365dc186ad7SThomas Gleixner 366dc186ad7SThomas Gleixner /* 367dc186ad7SThomas Gleixner * fixup_free is called when: 368dc186ad7SThomas Gleixner * - an active object is freed 369dc186ad7SThomas Gleixner */ 370dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 371dc186ad7SThomas Gleixner { 372dc186ad7SThomas Gleixner struct work_struct *work = addr; 373dc186ad7SThomas Gleixner 374dc186ad7SThomas Gleixner switch (state) { 375dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 376dc186ad7SThomas Gleixner cancel_work_sync(work); 377dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 378dc186ad7SThomas Gleixner return 1; 379dc186ad7SThomas Gleixner default: 380dc186ad7SThomas Gleixner return 0; 381dc186ad7SThomas Gleixner } 382dc186ad7SThomas Gleixner } 383dc186ad7SThomas Gleixner 384dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 385dc186ad7SThomas Gleixner .name = "work_struct", 38699777288SStanislaw Gruszka .debug_hint = work_debug_hint, 387dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 388dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 389dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 390dc186ad7SThomas Gleixner }; 391dc186ad7SThomas Gleixner 392dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 393dc186ad7SThomas Gleixner { 394dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 395dc186ad7SThomas Gleixner } 396dc186ad7SThomas Gleixner 397dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 398dc186ad7SThomas Gleixner { 399dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 400dc186ad7SThomas Gleixner } 401dc186ad7SThomas Gleixner 402dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 403dc186ad7SThomas Gleixner { 404dc186ad7SThomas Gleixner if (onstack) 405dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 406dc186ad7SThomas Gleixner else 407dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 408dc186ad7SThomas Gleixner } 409dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 410dc186ad7SThomas Gleixner 411dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 412dc186ad7SThomas Gleixner { 413dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 414dc186ad7SThomas Gleixner } 415dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 416dc186ad7SThomas Gleixner 417dc186ad7SThomas Gleixner #else 418dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 419dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 420dc186ad7SThomas Gleixner #endif 421dc186ad7SThomas Gleixner 42295402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 42395402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4241da177e4SLinus Torvalds static LIST_HEAD(workqueues); 425a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4261da177e4SLinus Torvalds 42714441960SOleg Nesterov /* 428e19e397aSTejun Heo * The CPU and unbound standard worker pools. The unbound ones have 429e19e397aSTejun Heo * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set. 43014441960SOleg Nesterov */ 431e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 432a60dc39cSTejun Heo cpu_std_worker_pools); 433a60dc39cSTejun Heo static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS]; 434f3421797STejun Heo 4359daf9e67STejun Heo /* idr of all pools */ 4369daf9e67STejun Heo static DEFINE_MUTEX(worker_pool_idr_mutex); 4379daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr); 4389daf9e67STejun Heo 439c34056a3STejun Heo static int worker_thread(void *__worker); 4401da177e4SLinus Torvalds 441a60dc39cSTejun Heo static struct worker_pool *std_worker_pools(int cpu) 4421da177e4SLinus Torvalds { 443f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 444a60dc39cSTejun Heo return per_cpu(cpu_std_worker_pools, cpu); 445f3421797STejun Heo else 446a60dc39cSTejun Heo return unbound_std_worker_pools; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4494e8f0a60STejun Heo static int std_worker_pool_pri(struct worker_pool *pool) 4504e8f0a60STejun Heo { 451a60dc39cSTejun Heo return pool - std_worker_pools(pool->cpu); 4524e8f0a60STejun Heo } 4534e8f0a60STejun Heo 4549daf9e67STejun Heo /* allocate ID and assign it to @pool */ 4559daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 4569daf9e67STejun Heo { 4579daf9e67STejun Heo int ret; 4589daf9e67STejun Heo 4599daf9e67STejun Heo mutex_lock(&worker_pool_idr_mutex); 460e68035fbSTejun Heo ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL); 461e68035fbSTejun Heo if (ret >= 0) 462e68035fbSTejun Heo pool->id = ret; 4639daf9e67STejun Heo mutex_unlock(&worker_pool_idr_mutex); 4649daf9e67STejun Heo 465e68035fbSTejun Heo return ret < 0 ? ret : 0; 4669daf9e67STejun Heo } 4679daf9e67STejun Heo 4687c3eed5cSTejun Heo /* 4697c3eed5cSTejun Heo * Lookup worker_pool by id. The idr currently is built during boot and 4707c3eed5cSTejun Heo * never modified. Don't worry about locking for now. 4717c3eed5cSTejun Heo */ 4727c3eed5cSTejun Heo static struct worker_pool *worker_pool_by_id(int pool_id) 4737c3eed5cSTejun Heo { 4747c3eed5cSTejun Heo return idr_find(&worker_pool_idr, pool_id); 4757c3eed5cSTejun Heo } 4767c3eed5cSTejun Heo 477d565ed63STejun Heo static struct worker_pool *get_std_worker_pool(int cpu, bool highpri) 478d565ed63STejun Heo { 479a60dc39cSTejun Heo struct worker_pool *pools = std_worker_pools(cpu); 480d565ed63STejun Heo 481a60dc39cSTejun Heo return &pools[highpri]; 482d565ed63STejun Heo } 483d565ed63STejun Heo 484112202d9STejun Heo static struct pool_workqueue *get_pwq(unsigned int cpu, 4854690c4abSTejun Heo struct workqueue_struct *wq) 486a848e3b6SOleg Nesterov { 487f3421797STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 488e06ffa1eSLai Jiangshan if (likely(cpu < nr_cpu_ids)) 489112202d9STejun Heo return per_cpu_ptr(wq->pool_wq.pcpu, cpu); 490f3421797STejun Heo } else if (likely(cpu == WORK_CPU_UNBOUND)) 491112202d9STejun Heo return wq->pool_wq.single; 492f3421797STejun Heo return NULL; 493f3421797STejun Heo } 494a848e3b6SOleg Nesterov 49573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 49673f53c4aSTejun Heo { 49773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 49873f53c4aSTejun Heo } 49973f53c4aSTejun Heo 50073f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 50173f53c4aSTejun Heo { 50273f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 50373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 50473f53c4aSTejun Heo } 50573f53c4aSTejun Heo 50673f53c4aSTejun Heo static int work_next_color(int color) 50773f53c4aSTejun Heo { 50873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 509b1f4ec17SOleg Nesterov } 510b1f4ec17SOleg Nesterov 5114594bf15SDavid Howells /* 512112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 513112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5147c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5157a22ad75STejun Heo * 516112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 517112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 518bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 519bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 5207a22ad75STejun Heo * 521112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 5227c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 523112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 5247c3eed5cSTejun Heo * available only while the work item is queued. 525bbb68dfaSTejun Heo * 526bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 527bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 528bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 529bbb68dfaSTejun Heo * try to steal the PENDING bit. 5304594bf15SDavid Howells */ 5317a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5327a22ad75STejun Heo unsigned long flags) 5337a22ad75STejun Heo { 5347a22ad75STejun Heo BUG_ON(!work_pending(work)); 5357a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5367a22ad75STejun Heo } 5377a22ad75STejun Heo 538112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 5394690c4abSTejun Heo unsigned long extra_flags) 540365970a1SDavid Howells { 541112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 542112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 543365970a1SDavid Howells } 544365970a1SDavid Howells 5454468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 5464468a00fSLai Jiangshan int pool_id) 5474468a00fSLai Jiangshan { 5484468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 5494468a00fSLai Jiangshan WORK_STRUCT_PENDING); 5504468a00fSLai Jiangshan } 5514468a00fSLai Jiangshan 5527c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 5537c3eed5cSTejun Heo int pool_id) 5544d707b9fSOleg Nesterov { 55523657bb1STejun Heo /* 55623657bb1STejun Heo * The following wmb is paired with the implied mb in 55723657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 55823657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 55923657bb1STejun Heo * owner. 56023657bb1STejun Heo */ 56123657bb1STejun Heo smp_wmb(); 5627c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 5634d707b9fSOleg Nesterov } 5644d707b9fSOleg Nesterov 5657a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 566365970a1SDavid Howells { 5677c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 5687c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 5697a22ad75STejun Heo } 5707a22ad75STejun Heo 571112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 5727a22ad75STejun Heo { 573e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5747a22ad75STejun Heo 575112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 576e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 577e120153dSTejun Heo else 578e120153dSTejun Heo return NULL; 5797a22ad75STejun Heo } 5807a22ad75STejun Heo 5817c3eed5cSTejun Heo /** 5827c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 5837c3eed5cSTejun Heo * @work: the work item of interest 5847c3eed5cSTejun Heo * 5857c3eed5cSTejun Heo * Return the worker_pool @work was last associated with. %NULL if none. 5867c3eed5cSTejun Heo */ 5877c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 5887a22ad75STejun Heo { 589e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5907c3eed5cSTejun Heo struct worker_pool *pool; 5917c3eed5cSTejun Heo int pool_id; 5927a22ad75STejun Heo 593112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 594112202d9STejun Heo return ((struct pool_workqueue *) 5957c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 5967a22ad75STejun Heo 5977c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 5987c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 5997a22ad75STejun Heo return NULL; 6007a22ad75STejun Heo 6017c3eed5cSTejun Heo pool = worker_pool_by_id(pool_id); 6027c3eed5cSTejun Heo WARN_ON_ONCE(!pool); 6037c3eed5cSTejun Heo return pool; 6047c3eed5cSTejun Heo } 6057c3eed5cSTejun Heo 6067c3eed5cSTejun Heo /** 6077c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 6087c3eed5cSTejun Heo * @work: the work item of interest 6097c3eed5cSTejun Heo * 6107c3eed5cSTejun Heo * Return the worker_pool ID @work was last associated with. 6117c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 6127c3eed5cSTejun Heo */ 6137c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 6147c3eed5cSTejun Heo { 61554d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 6167c3eed5cSTejun Heo 617112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 618112202d9STejun Heo return ((struct pool_workqueue *) 61954d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 62054d5b7d0SLai Jiangshan 62154d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 6227c3eed5cSTejun Heo } 6237c3eed5cSTejun Heo 624bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 625bbb68dfaSTejun Heo { 6267c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 627bbb68dfaSTejun Heo 6287c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 6297c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 630bbb68dfaSTejun Heo } 631bbb68dfaSTejun Heo 632bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 633bbb68dfaSTejun Heo { 634bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 635bbb68dfaSTejun Heo 636112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 637bbb68dfaSTejun Heo } 638bbb68dfaSTejun Heo 639e22bee78STejun Heo /* 6403270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6413270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 642d565ed63STejun Heo * they're being called with pool->lock held. 643e22bee78STejun Heo */ 644e22bee78STejun Heo 64563d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 646649027d7STejun Heo { 647e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 648649027d7STejun Heo } 649649027d7STejun Heo 650e22bee78STejun Heo /* 651e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 652e22bee78STejun Heo * running workers. 653974271c4STejun Heo * 654974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 655706026c2STejun Heo * function will always return %true for unbound pools as long as the 656974271c4STejun Heo * worklist isn't empty. 657e22bee78STejun Heo */ 65863d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 659e22bee78STejun Heo { 66063d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 661e22bee78STejun Heo } 662e22bee78STejun Heo 663e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 66463d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 665e22bee78STejun Heo { 66663d95a91STejun Heo return pool->nr_idle; 667e22bee78STejun Heo } 668e22bee78STejun Heo 669e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 67063d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 671e22bee78STejun Heo { 672e19e397aSTejun Heo return !list_empty(&pool->worklist) && 673e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 674e22bee78STejun Heo } 675e22bee78STejun Heo 676e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 67763d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 678e22bee78STejun Heo { 67963d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 680e22bee78STejun Heo } 681e22bee78STejun Heo 682e22bee78STejun Heo /* Do I need to be the manager? */ 68363d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 684e22bee78STejun Heo { 68563d95a91STejun Heo return need_to_create_worker(pool) || 68611ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 687e22bee78STejun Heo } 688e22bee78STejun Heo 689e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 69063d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 691e22bee78STejun Heo { 692552a37e9SLai Jiangshan bool managing = pool->flags & POOL_MANAGING_WORKERS; 69363d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 69463d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 695e22bee78STejun Heo 696ea1abd61SLai Jiangshan /* 697ea1abd61SLai Jiangshan * nr_idle and idle_list may disagree if idle rebinding is in 698ea1abd61SLai Jiangshan * progress. Never return %true if idle_list is empty. 699ea1abd61SLai Jiangshan */ 700ea1abd61SLai Jiangshan if (list_empty(&pool->idle_list)) 701ea1abd61SLai Jiangshan return false; 702ea1abd61SLai Jiangshan 703e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 704e22bee78STejun Heo } 705e22bee78STejun Heo 706e22bee78STejun Heo /* 707e22bee78STejun Heo * Wake up functions. 708e22bee78STejun Heo */ 709e22bee78STejun Heo 7107e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 71163d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 7127e11629dSTejun Heo { 71363d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 7147e11629dSTejun Heo return NULL; 7157e11629dSTejun Heo 71663d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 7177e11629dSTejun Heo } 7187e11629dSTejun Heo 7197e11629dSTejun Heo /** 7207e11629dSTejun Heo * wake_up_worker - wake up an idle worker 72163d95a91STejun Heo * @pool: worker pool to wake worker from 7227e11629dSTejun Heo * 72363d95a91STejun Heo * Wake up the first idle worker of @pool. 7247e11629dSTejun Heo * 7257e11629dSTejun Heo * CONTEXT: 726d565ed63STejun Heo * spin_lock_irq(pool->lock). 7277e11629dSTejun Heo */ 72863d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 7297e11629dSTejun Heo { 73063d95a91STejun Heo struct worker *worker = first_worker(pool); 7317e11629dSTejun Heo 7327e11629dSTejun Heo if (likely(worker)) 7337e11629dSTejun Heo wake_up_process(worker->task); 7347e11629dSTejun Heo } 7357e11629dSTejun Heo 7364690c4abSTejun Heo /** 737e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 738e22bee78STejun Heo * @task: task waking up 739e22bee78STejun Heo * @cpu: CPU @task is waking up to 740e22bee78STejun Heo * 741e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 742e22bee78STejun Heo * being awoken. 743e22bee78STejun Heo * 744e22bee78STejun Heo * CONTEXT: 745e22bee78STejun Heo * spin_lock_irq(rq->lock) 746e22bee78STejun Heo */ 747e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 748e22bee78STejun Heo { 749e22bee78STejun Heo struct worker *worker = kthread_data(task); 750e22bee78STejun Heo 75136576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 752ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 753e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 754e22bee78STejun Heo } 75536576000SJoonsoo Kim } 756e22bee78STejun Heo 757e22bee78STejun Heo /** 758e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 759e22bee78STejun Heo * @task: task going to sleep 760e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 761e22bee78STejun Heo * 762e22bee78STejun Heo * This function is called during schedule() when a busy worker is 763e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 764e22bee78STejun Heo * returning pointer to its task. 765e22bee78STejun Heo * 766e22bee78STejun Heo * CONTEXT: 767e22bee78STejun Heo * spin_lock_irq(rq->lock) 768e22bee78STejun Heo * 769e22bee78STejun Heo * RETURNS: 770e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 771e22bee78STejun Heo */ 772e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 773e22bee78STejun Heo unsigned int cpu) 774e22bee78STejun Heo { 775e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 776111c225aSTejun Heo struct worker_pool *pool; 777e22bee78STejun Heo 778111c225aSTejun Heo /* 779111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 780111c225aSTejun Heo * workers, also reach here, let's not access anything before 781111c225aSTejun Heo * checking NOT_RUNNING. 782111c225aSTejun Heo */ 7832d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 784e22bee78STejun Heo return NULL; 785e22bee78STejun Heo 786111c225aSTejun Heo pool = worker->pool; 787111c225aSTejun Heo 788e22bee78STejun Heo /* this can only happen on the local cpu */ 789e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 790e22bee78STejun Heo 791e22bee78STejun Heo /* 792e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 793e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 794e22bee78STejun Heo * Please read comment there. 795e22bee78STejun Heo * 796628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 797628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 798628c78e7STejun Heo * disabled, which in turn means that none else could be 799d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 800628c78e7STejun Heo * lock is safe. 801e22bee78STejun Heo */ 802e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 803e19e397aSTejun Heo !list_empty(&pool->worklist)) 80463d95a91STejun Heo to_wakeup = first_worker(pool); 805e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 806e22bee78STejun Heo } 807e22bee78STejun Heo 808e22bee78STejun Heo /** 809e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 810cb444766STejun Heo * @worker: self 811d302f017STejun Heo * @flags: flags to set 812d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 813d302f017STejun Heo * 814e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 815e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 816e22bee78STejun Heo * woken up. 817d302f017STejun Heo * 818cb444766STejun Heo * CONTEXT: 819d565ed63STejun Heo * spin_lock_irq(pool->lock) 820d302f017STejun Heo */ 821d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 822d302f017STejun Heo bool wakeup) 823d302f017STejun Heo { 824bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 825e22bee78STejun Heo 826cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 827cb444766STejun Heo 828e22bee78STejun Heo /* 829e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 830e22bee78STejun Heo * wake up an idle worker as necessary if requested by 831e22bee78STejun Heo * @wakeup. 832e22bee78STejun Heo */ 833e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 834e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 835e22bee78STejun Heo if (wakeup) { 836e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 837bd7bdd43STejun Heo !list_empty(&pool->worklist)) 83863d95a91STejun Heo wake_up_worker(pool); 839e22bee78STejun Heo } else 840e19e397aSTejun Heo atomic_dec(&pool->nr_running); 841e22bee78STejun Heo } 842e22bee78STejun Heo 843d302f017STejun Heo worker->flags |= flags; 844d302f017STejun Heo } 845d302f017STejun Heo 846d302f017STejun Heo /** 847e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 848cb444766STejun Heo * @worker: self 849d302f017STejun Heo * @flags: flags to clear 850d302f017STejun Heo * 851e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 852d302f017STejun Heo * 853cb444766STejun Heo * CONTEXT: 854d565ed63STejun Heo * spin_lock_irq(pool->lock) 855d302f017STejun Heo */ 856d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 857d302f017STejun Heo { 85863d95a91STejun Heo struct worker_pool *pool = worker->pool; 859e22bee78STejun Heo unsigned int oflags = worker->flags; 860e22bee78STejun Heo 861cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 862cb444766STejun Heo 863d302f017STejun Heo worker->flags &= ~flags; 864e22bee78STejun Heo 86542c025f3STejun Heo /* 86642c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 86742c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 86842c025f3STejun Heo * of multiple flags, not a single flag. 86942c025f3STejun Heo */ 870e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 871e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 872e19e397aSTejun Heo atomic_inc(&pool->nr_running); 873d302f017STejun Heo } 874d302f017STejun Heo 875d302f017STejun Heo /** 8768cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 877c9e7cf27STejun Heo * @pool: pool of interest 8788cca0eeaSTejun Heo * @work: work to find worker for 8798cca0eeaSTejun Heo * 880c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 881c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 882a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 883a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 884a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 885a2c1c57bSTejun Heo * being executed. 886a2c1c57bSTejun Heo * 887a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 888a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 889a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 890a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 891a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 892a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 893a2c1c57bSTejun Heo * 894a2c1c57bSTejun Heo * This function checks the work item address, work function and workqueue 895a2c1c57bSTejun Heo * to avoid false positives. Note that this isn't complete as one may 896a2c1c57bSTejun Heo * construct a work function which can introduce dependency onto itself 897a2c1c57bSTejun Heo * through a recycled work item. Well, if somebody wants to shoot oneself 898a2c1c57bSTejun Heo * in the foot that badly, there's only so much we can do, and if such 899a2c1c57bSTejun Heo * deadlock actually occurs, it should be easy to locate the culprit work 900a2c1c57bSTejun Heo * function. 9018cca0eeaSTejun Heo * 9028cca0eeaSTejun Heo * CONTEXT: 903d565ed63STejun Heo * spin_lock_irq(pool->lock). 9048cca0eeaSTejun Heo * 9058cca0eeaSTejun Heo * RETURNS: 9068cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 9078cca0eeaSTejun Heo * otherwise. 9088cca0eeaSTejun Heo */ 909c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 9108cca0eeaSTejun Heo struct work_struct *work) 9118cca0eeaSTejun Heo { 91242f8570fSSasha Levin struct worker *worker; 91342f8570fSSasha Levin 914b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 915a2c1c57bSTejun Heo (unsigned long)work) 916a2c1c57bSTejun Heo if (worker->current_work == work && 917a2c1c57bSTejun Heo worker->current_func == work->func) 91842f8570fSSasha Levin return worker; 91942f8570fSSasha Levin 92042f8570fSSasha Levin return NULL; 9218cca0eeaSTejun Heo } 9228cca0eeaSTejun Heo 9238cca0eeaSTejun Heo /** 924bf4ede01STejun Heo * move_linked_works - move linked works to a list 925bf4ede01STejun Heo * @work: start of series of works to be scheduled 926bf4ede01STejun Heo * @head: target list to append @work to 927bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 928bf4ede01STejun Heo * 929bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 930bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 931bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 932bf4ede01STejun Heo * 933bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 934bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 935bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 936bf4ede01STejun Heo * 937bf4ede01STejun Heo * CONTEXT: 938d565ed63STejun Heo * spin_lock_irq(pool->lock). 939bf4ede01STejun Heo */ 940bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 941bf4ede01STejun Heo struct work_struct **nextp) 942bf4ede01STejun Heo { 943bf4ede01STejun Heo struct work_struct *n; 944bf4ede01STejun Heo 945bf4ede01STejun Heo /* 946bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 947bf4ede01STejun Heo * use NULL for list head. 948bf4ede01STejun Heo */ 949bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 950bf4ede01STejun Heo list_move_tail(&work->entry, head); 951bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 952bf4ede01STejun Heo break; 953bf4ede01STejun Heo } 954bf4ede01STejun Heo 955bf4ede01STejun Heo /* 956bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 957bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 958bf4ede01STejun Heo * needs to be updated. 959bf4ede01STejun Heo */ 960bf4ede01STejun Heo if (nextp) 961bf4ede01STejun Heo *nextp = n; 962bf4ede01STejun Heo } 963bf4ede01STejun Heo 964112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 965bf4ede01STejun Heo { 966112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 967bf4ede01STejun Heo 968bf4ede01STejun Heo trace_workqueue_activate_work(work); 969112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 970bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 971112202d9STejun Heo pwq->nr_active++; 972bf4ede01STejun Heo } 973bf4ede01STejun Heo 974112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 9753aa62497SLai Jiangshan { 976112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 9773aa62497SLai Jiangshan struct work_struct, entry); 9783aa62497SLai Jiangshan 979112202d9STejun Heo pwq_activate_delayed_work(work); 9803aa62497SLai Jiangshan } 9813aa62497SLai Jiangshan 982bf4ede01STejun Heo /** 983112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 984112202d9STejun Heo * @pwq: pwq of interest 985bf4ede01STejun Heo * @color: color of work which left the queue 986bf4ede01STejun Heo * 987bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 988112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 989bf4ede01STejun Heo * 990bf4ede01STejun Heo * CONTEXT: 991d565ed63STejun Heo * spin_lock_irq(pool->lock). 992bf4ede01STejun Heo */ 993112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 994bf4ede01STejun Heo { 995bf4ede01STejun Heo /* ignore uncolored works */ 996bf4ede01STejun Heo if (color == WORK_NO_COLOR) 997bf4ede01STejun Heo return; 998bf4ede01STejun Heo 999112202d9STejun Heo pwq->nr_in_flight[color]--; 1000bf4ede01STejun Heo 1001112202d9STejun Heo pwq->nr_active--; 1002112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1003bf4ede01STejun Heo /* one down, submit a delayed one */ 1004112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1005112202d9STejun Heo pwq_activate_first_delayed(pwq); 1006bf4ede01STejun Heo } 1007bf4ede01STejun Heo 1008bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1009112202d9STejun Heo if (likely(pwq->flush_color != color)) 1010bf4ede01STejun Heo return; 1011bf4ede01STejun Heo 1012bf4ede01STejun Heo /* are there still in-flight works? */ 1013112202d9STejun Heo if (pwq->nr_in_flight[color]) 1014bf4ede01STejun Heo return; 1015bf4ede01STejun Heo 1016112202d9STejun Heo /* this pwq is done, clear flush_color */ 1017112202d9STejun Heo pwq->flush_color = -1; 1018bf4ede01STejun Heo 1019bf4ede01STejun Heo /* 1020112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1021bf4ede01STejun Heo * will handle the rest. 1022bf4ede01STejun Heo */ 1023112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1024112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 1025bf4ede01STejun Heo } 1026bf4ede01STejun Heo 102736e227d2STejun Heo /** 1028bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 102936e227d2STejun Heo * @work: work item to steal 103036e227d2STejun Heo * @is_dwork: @work is a delayed_work 1031bbb68dfaSTejun Heo * @flags: place to store irq state 103236e227d2STejun Heo * 103336e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 103436e227d2STejun Heo * stable state - idle, on timer or on worklist. Return values are 103536e227d2STejun Heo * 103636e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 103736e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 103836e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1039bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1040bbb68dfaSTejun Heo * for arbitrarily long 104136e227d2STejun Heo * 1042bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1043e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1044e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1045e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1046bbb68dfaSTejun Heo * 1047bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1048bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1049bbb68dfaSTejun Heo * 1050e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1051bf4ede01STejun Heo */ 1052bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1053bbb68dfaSTejun Heo unsigned long *flags) 1054bf4ede01STejun Heo { 1055d565ed63STejun Heo struct worker_pool *pool; 1056112202d9STejun Heo struct pool_workqueue *pwq; 1057bf4ede01STejun Heo 1058bbb68dfaSTejun Heo local_irq_save(*flags); 1059bbb68dfaSTejun Heo 106036e227d2STejun Heo /* try to steal the timer if it exists */ 106136e227d2STejun Heo if (is_dwork) { 106236e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 106336e227d2STejun Heo 1064e0aecdd8STejun Heo /* 1065e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1066e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1067e0aecdd8STejun Heo * running on the local CPU. 1068e0aecdd8STejun Heo */ 106936e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 107036e227d2STejun Heo return 1; 107136e227d2STejun Heo } 107236e227d2STejun Heo 107336e227d2STejun Heo /* try to claim PENDING the normal way */ 1074bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1075bf4ede01STejun Heo return 0; 1076bf4ede01STejun Heo 1077bf4ede01STejun Heo /* 1078bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1079bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1080bf4ede01STejun Heo */ 1081d565ed63STejun Heo pool = get_work_pool(work); 1082d565ed63STejun Heo if (!pool) 1083bbb68dfaSTejun Heo goto fail; 1084bf4ede01STejun Heo 1085d565ed63STejun Heo spin_lock(&pool->lock); 1086bf4ede01STejun Heo /* 1087112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1088112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1089112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1090112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1091112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 10920b3dae68SLai Jiangshan * item is currently queued on that pool. 1093bf4ede01STejun Heo */ 1094112202d9STejun Heo pwq = get_work_pwq(work); 1095112202d9STejun Heo if (pwq && pwq->pool == pool) { 1096bf4ede01STejun Heo debug_work_deactivate(work); 10973aa62497SLai Jiangshan 10983aa62497SLai Jiangshan /* 109916062836STejun Heo * A delayed work item cannot be grabbed directly because 110016062836STejun Heo * it might have linked NO_COLOR work items which, if left 1101112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 110216062836STejun Heo * management later on and cause stall. Make sure the work 110316062836STejun Heo * item is activated before grabbing. 11043aa62497SLai Jiangshan */ 11053aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1106112202d9STejun Heo pwq_activate_delayed_work(work); 11073aa62497SLai Jiangshan 1108bf4ede01STejun Heo list_del_init(&work->entry); 1109112202d9STejun Heo pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work)); 111036e227d2STejun Heo 1111112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 11124468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 11134468a00fSLai Jiangshan 1114d565ed63STejun Heo spin_unlock(&pool->lock); 111536e227d2STejun Heo return 1; 1116bf4ede01STejun Heo } 1117d565ed63STejun Heo spin_unlock(&pool->lock); 1118bbb68dfaSTejun Heo fail: 1119bbb68dfaSTejun Heo local_irq_restore(*flags); 1120bbb68dfaSTejun Heo if (work_is_canceling(work)) 1121bbb68dfaSTejun Heo return -ENOENT; 1122bbb68dfaSTejun Heo cpu_relax(); 112336e227d2STejun Heo return -EAGAIN; 1124bf4ede01STejun Heo } 1125bf4ede01STejun Heo 1126bf4ede01STejun Heo /** 1127706026c2STejun Heo * insert_work - insert a work into a pool 1128112202d9STejun Heo * @pwq: pwq @work belongs to 11294690c4abSTejun Heo * @work: work to insert 11304690c4abSTejun Heo * @head: insertion point 11314690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 11324690c4abSTejun Heo * 1133112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1134706026c2STejun Heo * work_struct flags. 11354690c4abSTejun Heo * 11364690c4abSTejun Heo * CONTEXT: 1137d565ed63STejun Heo * spin_lock_irq(pool->lock). 1138365970a1SDavid Howells */ 1139112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1140112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1141b89deed3SOleg Nesterov { 1142112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1143e1d8aa9fSFrederic Weisbecker 11444690c4abSTejun Heo /* we own @work, set data and link */ 1145112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 11461a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1147e22bee78STejun Heo 1148e22bee78STejun Heo /* 1149e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1150e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1151e22bee78STejun Heo * lying around lazily while there are works to be processed. 1152e22bee78STejun Heo */ 1153e22bee78STejun Heo smp_mb(); 1154e22bee78STejun Heo 115563d95a91STejun Heo if (__need_more_worker(pool)) 115663d95a91STejun Heo wake_up_worker(pool); 1157b89deed3SOleg Nesterov } 1158b89deed3SOleg Nesterov 1159c8efcc25STejun Heo /* 1160c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 11618d03ecfeSTejun Heo * same workqueue. 1162c8efcc25STejun Heo */ 1163c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1164c8efcc25STejun Heo { 1165c8efcc25STejun Heo struct worker *worker; 1166c8efcc25STejun Heo 11678d03ecfeSTejun Heo worker = current_wq_worker(); 1168c8efcc25STejun Heo /* 11698d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 11708d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1171c8efcc25STejun Heo */ 1172112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1173c8efcc25STejun Heo } 1174c8efcc25STejun Heo 11754690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 11761da177e4SLinus Torvalds struct work_struct *work) 11771da177e4SLinus Torvalds { 1178112202d9STejun Heo struct pool_workqueue *pwq; 11791e19ffc6STejun Heo struct list_head *worklist; 11808a2e8e5dSTejun Heo unsigned int work_flags; 1181b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 11828930cabaSTejun Heo 11838930cabaSTejun Heo /* 11848930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11858930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11868930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11878930cabaSTejun Heo * happen with IRQ disabled. 11888930cabaSTejun Heo */ 11898930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 11901da177e4SLinus Torvalds 1191dc186ad7SThomas Gleixner debug_work_activate(work); 11921e19ffc6STejun Heo 1193c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 11949c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1195c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1196e41e704bSTejun Heo return; 1197e41e704bSTejun Heo 1198112202d9STejun Heo /* determine the pwq to use */ 1199c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1200c9e7cf27STejun Heo struct worker_pool *last_pool; 1201c7fc77f7STejun Heo 120257469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1203f3421797STejun Heo cpu = raw_smp_processor_id(); 1204f3421797STejun Heo 120518aa9effSTejun Heo /* 1206dbf2576eSTejun Heo * It's multi cpu. If @work was previously on a different 1207dbf2576eSTejun Heo * cpu, it might still be running there, in which case the 1208dbf2576eSTejun Heo * work needs to be queued on that cpu to guarantee 1209dbf2576eSTejun Heo * non-reentrancy. 121018aa9effSTejun Heo */ 1211112202d9STejun Heo pwq = get_pwq(cpu, wq); 1212c9e7cf27STejun Heo last_pool = get_work_pool(work); 1213dbf2576eSTejun Heo 1214112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 121518aa9effSTejun Heo struct worker *worker; 121618aa9effSTejun Heo 1217d565ed63STejun Heo spin_lock(&last_pool->lock); 121818aa9effSTejun Heo 1219c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 122018aa9effSTejun Heo 1221112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1222112202d9STejun Heo pwq = get_pwq(last_pool->cpu, wq); 12238594fadeSLai Jiangshan } else { 122418aa9effSTejun Heo /* meh... not running there, queue here */ 1225d565ed63STejun Heo spin_unlock(&last_pool->lock); 1226112202d9STejun Heo spin_lock(&pwq->pool->lock); 122718aa9effSTejun Heo } 12288930cabaSTejun Heo } else { 1229112202d9STejun Heo spin_lock(&pwq->pool->lock); 12308930cabaSTejun Heo } 1231f3421797STejun Heo } else { 1232112202d9STejun Heo pwq = get_pwq(WORK_CPU_UNBOUND, wq); 1233112202d9STejun Heo spin_lock(&pwq->pool->lock); 1234502ca9d8STejun Heo } 1235502ca9d8STejun Heo 1236112202d9STejun Heo /* pwq determined, queue */ 1237112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1238502ca9d8STejun Heo 1239f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1240112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1241f5b2552bSDan Carpenter return; 1242f5b2552bSDan Carpenter } 12431e19ffc6STejun Heo 1244112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1245112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 12461e19ffc6STejun Heo 1247112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1248cdadf009STejun Heo trace_workqueue_activate_work(work); 1249112202d9STejun Heo pwq->nr_active++; 1250112202d9STejun Heo worklist = &pwq->pool->worklist; 12518a2e8e5dSTejun Heo } else { 12528a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1253112202d9STejun Heo worklist = &pwq->delayed_works; 12548a2e8e5dSTejun Heo } 12551e19ffc6STejun Heo 1256112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 12571e19ffc6STejun Heo 1258112202d9STejun Heo spin_unlock(&pwq->pool->lock); 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12610fcb78c2SRolf Eike Beer /** 1262c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1263c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1264c1a220e7SZhang Rui * @wq: workqueue to use 1265c1a220e7SZhang Rui * @work: work to queue 1266c1a220e7SZhang Rui * 1267d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1268c1a220e7SZhang Rui * 1269c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1270c1a220e7SZhang Rui * can't go away. 1271c1a220e7SZhang Rui */ 1272d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1273d4283e93STejun Heo struct work_struct *work) 1274c1a220e7SZhang Rui { 1275d4283e93STejun Heo bool ret = false; 12768930cabaSTejun Heo unsigned long flags; 12778930cabaSTejun Heo 12788930cabaSTejun Heo local_irq_save(flags); 1279c1a220e7SZhang Rui 128022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12814690c4abSTejun Heo __queue_work(cpu, wq, work); 1282d4283e93STejun Heo ret = true; 1283c1a220e7SZhang Rui } 12848930cabaSTejun Heo 12858930cabaSTejun Heo local_irq_restore(flags); 1286c1a220e7SZhang Rui return ret; 1287c1a220e7SZhang Rui } 1288c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1289c1a220e7SZhang Rui 12900a13c00eSTejun Heo /** 12910a13c00eSTejun Heo * queue_work - queue work on a workqueue 12920a13c00eSTejun Heo * @wq: workqueue to use 12930a13c00eSTejun Heo * @work: work to queue 12940a13c00eSTejun Heo * 1295d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 12960a13c00eSTejun Heo * 12970a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 12980a13c00eSTejun Heo * it can be processed by another CPU. 12990a13c00eSTejun Heo */ 1300d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 13010a13c00eSTejun Heo { 130257469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 13030a13c00eSTejun Heo } 13040a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 13050a13c00eSTejun Heo 1306d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 13071da177e4SLinus Torvalds { 130852bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 13091da177e4SLinus Torvalds 1310e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 131160c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 13121da177e4SLinus Torvalds } 13131438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 13141da177e4SLinus Torvalds 13157beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 131652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13171da177e4SLinus Torvalds { 13187beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 13197beb2edfSTejun Heo struct work_struct *work = &dwork->work; 13201da177e4SLinus Torvalds 13217beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 13227beb2edfSTejun Heo timer->data != (unsigned long)dwork); 1323fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1324fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 13257beb2edfSTejun Heo 13268852aac2STejun Heo /* 13278852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 13288852aac2STejun Heo * both optimization and correctness. The earliest @timer can 13298852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 13308852aac2STejun Heo * on that there's no such delay when @delay is 0. 13318852aac2STejun Heo */ 13328852aac2STejun Heo if (!delay) { 13338852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 13348852aac2STejun Heo return; 13358852aac2STejun Heo } 13368852aac2STejun Heo 13377beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 13387beb2edfSTejun Heo 133960c057bcSLai Jiangshan dwork->wq = wq; 13401265057fSTejun Heo dwork->cpu = cpu; 13417beb2edfSTejun Heo timer->expires = jiffies + delay; 13427beb2edfSTejun Heo 13437beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13447beb2edfSTejun Heo add_timer_on(timer, cpu); 13457beb2edfSTejun Heo else 13467beb2edfSTejun Heo add_timer(timer); 13477beb2edfSTejun Heo } 13481da177e4SLinus Torvalds 13490fcb78c2SRolf Eike Beer /** 13500fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 13510fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 13520fcb78c2SRolf Eike Beer * @wq: workqueue to use 1353af9997e4SRandy Dunlap * @dwork: work to queue 13540fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 13550fcb78c2SRolf Eike Beer * 1356715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1357715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1358715f1300STejun Heo * execution. 13590fcb78c2SRolf Eike Beer */ 1360d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 136152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13627a6bc1cdSVenkatesh Pallipadi { 136352bad64dSDavid Howells struct work_struct *work = &dwork->work; 1364d4283e93STejun Heo bool ret = false; 13658930cabaSTejun Heo unsigned long flags; 13668930cabaSTejun Heo 13678930cabaSTejun Heo /* read the comment in __queue_work() */ 13688930cabaSTejun Heo local_irq_save(flags); 13697a6bc1cdSVenkatesh Pallipadi 137022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 13717beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1372d4283e93STejun Heo ret = true; 13737a6bc1cdSVenkatesh Pallipadi } 13748930cabaSTejun Heo 13758930cabaSTejun Heo local_irq_restore(flags); 13767a6bc1cdSVenkatesh Pallipadi return ret; 13777a6bc1cdSVenkatesh Pallipadi } 1378ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 13791da177e4SLinus Torvalds 1380c8e55f36STejun Heo /** 13810a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 13820a13c00eSTejun Heo * @wq: workqueue to use 13830a13c00eSTejun Heo * @dwork: delayable work to queue 13840a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 13850a13c00eSTejun Heo * 1386715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 13870a13c00eSTejun Heo */ 1388d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 13890a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 13900a13c00eSTejun Heo { 139157469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 13920a13c00eSTejun Heo } 13930a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 13940a13c00eSTejun Heo 13950a13c00eSTejun Heo /** 13968376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 13978376fe22STejun Heo * @cpu: CPU number to execute work on 13988376fe22STejun Heo * @wq: workqueue to use 13998376fe22STejun Heo * @dwork: work to queue 14008376fe22STejun Heo * @delay: number of jiffies to wait before queueing 14018376fe22STejun Heo * 14028376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 14038376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 14048376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 14058376fe22STejun Heo * current state. 14068376fe22STejun Heo * 14078376fe22STejun Heo * Returns %false if @dwork was idle and queued, %true if @dwork was 14088376fe22STejun Heo * pending and its timer was modified. 14098376fe22STejun Heo * 1410e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 14118376fe22STejun Heo * See try_to_grab_pending() for details. 14128376fe22STejun Heo */ 14138376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 14148376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 14158376fe22STejun Heo { 14168376fe22STejun Heo unsigned long flags; 14178376fe22STejun Heo int ret; 14188376fe22STejun Heo 14198376fe22STejun Heo do { 14208376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 14218376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 14228376fe22STejun Heo 14238376fe22STejun Heo if (likely(ret >= 0)) { 14248376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 14258376fe22STejun Heo local_irq_restore(flags); 14268376fe22STejun Heo } 14278376fe22STejun Heo 14288376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 14298376fe22STejun Heo return ret; 14308376fe22STejun Heo } 14318376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 14328376fe22STejun Heo 14338376fe22STejun Heo /** 14348376fe22STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 14358376fe22STejun Heo * @wq: workqueue to use 14368376fe22STejun Heo * @dwork: work to queue 14378376fe22STejun Heo * @delay: number of jiffies to wait before queueing 14388376fe22STejun Heo * 14398376fe22STejun Heo * mod_delayed_work_on() on local CPU. 14408376fe22STejun Heo */ 14418376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, 14428376fe22STejun Heo unsigned long delay) 14438376fe22STejun Heo { 14448376fe22STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 14458376fe22STejun Heo } 14468376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work); 14478376fe22STejun Heo 14488376fe22STejun Heo /** 1449c8e55f36STejun Heo * worker_enter_idle - enter idle state 1450c8e55f36STejun Heo * @worker: worker which is entering idle state 1451c8e55f36STejun Heo * 1452c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1453c8e55f36STejun Heo * necessary. 1454c8e55f36STejun Heo * 1455c8e55f36STejun Heo * LOCKING: 1456d565ed63STejun Heo * spin_lock_irq(pool->lock). 1457c8e55f36STejun Heo */ 1458c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 14591da177e4SLinus Torvalds { 1460bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1461c8e55f36STejun Heo 1462c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1463c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1464c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1465c8e55f36STejun Heo 1466cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1467cb444766STejun Heo worker->flags |= WORKER_IDLE; 1468bd7bdd43STejun Heo pool->nr_idle++; 1469e22bee78STejun Heo worker->last_active = jiffies; 1470c8e55f36STejun Heo 1471c8e55f36STejun Heo /* idle_list is LIFO */ 1472bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1473db7bccf4STejun Heo 147463d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1475628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1476cb444766STejun Heo 1477544ecf31STejun Heo /* 1478706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1479d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1480628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1481628c78e7STejun Heo * unbind is not in progress. 1482544ecf31STejun Heo */ 148324647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1484bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1485e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1486c8e55f36STejun Heo } 1487c8e55f36STejun Heo 1488c8e55f36STejun Heo /** 1489c8e55f36STejun Heo * worker_leave_idle - leave idle state 1490c8e55f36STejun Heo * @worker: worker which is leaving idle state 1491c8e55f36STejun Heo * 1492c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1493c8e55f36STejun Heo * 1494c8e55f36STejun Heo * LOCKING: 1495d565ed63STejun Heo * spin_lock_irq(pool->lock). 1496c8e55f36STejun Heo */ 1497c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1498c8e55f36STejun Heo { 1499bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1500c8e55f36STejun Heo 1501c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1502d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1503bd7bdd43STejun Heo pool->nr_idle--; 1504c8e55f36STejun Heo list_del_init(&worker->entry); 1505c8e55f36STejun Heo } 1506c8e55f36STejun Heo 1507e22bee78STejun Heo /** 1508706026c2STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock pool 1509e22bee78STejun Heo * @worker: self 1510e22bee78STejun Heo * 1511e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1512e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1513e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1514e22bee78STejun Heo * guaranteed to execute on the cpu. 1515e22bee78STejun Heo * 1516e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1517e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1518e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1519e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1520706026c2STejun Heo * verbatim as it's best effort and blocking and pool may be 1521e22bee78STejun Heo * [dis]associated in the meantime. 1522e22bee78STejun Heo * 1523706026c2STejun Heo * This function tries set_cpus_allowed() and locks pool and verifies the 152424647570STejun Heo * binding against %POOL_DISASSOCIATED which is set during 1525f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1526f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1527f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1528e22bee78STejun Heo * 1529e22bee78STejun Heo * CONTEXT: 1530d565ed63STejun Heo * Might sleep. Called without any lock but returns with pool->lock 1531e22bee78STejun Heo * held. 1532e22bee78STejun Heo * 1533e22bee78STejun Heo * RETURNS: 1534706026c2STejun Heo * %true if the associated pool is online (@worker is successfully 1535e22bee78STejun Heo * bound), %false if offline. 1536e22bee78STejun Heo */ 1537e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1538d565ed63STejun Heo __acquires(&pool->lock) 1539e22bee78STejun Heo { 154024647570STejun Heo struct worker_pool *pool = worker->pool; 1541e22bee78STejun Heo struct task_struct *task = worker->task; 1542e22bee78STejun Heo 1543e22bee78STejun Heo while (true) { 1544e22bee78STejun Heo /* 1545e22bee78STejun Heo * The following call may fail, succeed or succeed 1546e22bee78STejun Heo * without actually migrating the task to the cpu if 1547e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 154824647570STejun Heo * against POOL_DISASSOCIATED. 1549e22bee78STejun Heo */ 155024647570STejun Heo if (!(pool->flags & POOL_DISASSOCIATED)) 1551ec22ca5eSTejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(pool->cpu)); 1552e22bee78STejun Heo 1553d565ed63STejun Heo spin_lock_irq(&pool->lock); 155424647570STejun Heo if (pool->flags & POOL_DISASSOCIATED) 1555e22bee78STejun Heo return false; 1556ec22ca5eSTejun Heo if (task_cpu(task) == pool->cpu && 1557e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1558ec22ca5eSTejun Heo get_cpu_mask(pool->cpu))) 1559e22bee78STejun Heo return true; 1560d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1561e22bee78STejun Heo 15625035b20fSTejun Heo /* 15635035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 15645035b20fSTejun Heo * and retry migration. cond_resched() is required here; 15655035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 15665035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 15675035b20fSTejun Heo */ 1568e22bee78STejun Heo cpu_relax(); 15695035b20fSTejun Heo cond_resched(); 1570e22bee78STejun Heo } 1571e22bee78STejun Heo } 1572e22bee78STejun Heo 1573e22bee78STejun Heo /* 1574ea1abd61SLai Jiangshan * Rebind an idle @worker to its CPU. worker_thread() will test 15755f7dabfdSLai Jiangshan * list_empty(@worker->entry) before leaving idle and call this function. 157625511a47STejun Heo */ 157725511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 157825511a47STejun Heo { 15795f7dabfdSLai Jiangshan /* CPU may go down again inbetween, clear UNBOUND only on success */ 15805f7dabfdSLai Jiangshan if (worker_maybe_bind_and_lock(worker)) 15815f7dabfdSLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 158225511a47STejun Heo 1583ea1abd61SLai Jiangshan /* rebind complete, become available again */ 1584ea1abd61SLai Jiangshan list_add(&worker->entry, &worker->pool->idle_list); 1585d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 158625511a47STejun Heo } 158725511a47STejun Heo 158825511a47STejun Heo /* 158925511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1590403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1591403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1592403c821dSTejun Heo * executed twice without intervening cpu down. 1593e22bee78STejun Heo */ 159425511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1595e22bee78STejun Heo { 1596e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1597e22bee78STejun Heo 1598eab6d828SLai Jiangshan if (worker_maybe_bind_and_lock(worker)) 1599eab6d828SLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 1600e22bee78STejun Heo 1601d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 1602e22bee78STejun Heo } 1603e22bee78STejun Heo 160425511a47STejun Heo /** 160594cf58bbSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 160694cf58bbSTejun Heo * @pool: pool of interest 160725511a47STejun Heo * 160894cf58bbSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding 160925511a47STejun Heo * is different for idle and busy ones. 161025511a47STejun Heo * 1611ea1abd61SLai Jiangshan * Idle ones will be removed from the idle_list and woken up. They will 1612ea1abd61SLai Jiangshan * add themselves back after completing rebind. This ensures that the 1613ea1abd61SLai Jiangshan * idle_list doesn't contain any unbound workers when re-bound busy workers 1614ea1abd61SLai Jiangshan * try to perform local wake-ups for concurrency management. 161525511a47STejun Heo * 1616ea1abd61SLai Jiangshan * Busy workers can rebind after they finish their current work items. 1617ea1abd61SLai Jiangshan * Queueing the rebind work item at the head of the scheduled list is 1618ea1abd61SLai Jiangshan * enough. Note that nr_running will be properly bumped as busy workers 1619ea1abd61SLai Jiangshan * rebind. 162025511a47STejun Heo * 1621ea1abd61SLai Jiangshan * On return, all non-manager workers are scheduled for rebind - see 1622ea1abd61SLai Jiangshan * manage_workers() for the manager special case. Any idle worker 1623ea1abd61SLai Jiangshan * including the manager will not appear on @idle_list until rebind is 1624ea1abd61SLai Jiangshan * complete, making local wake-ups safe. 162525511a47STejun Heo */ 162694cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool) 162725511a47STejun Heo { 1628ea1abd61SLai Jiangshan struct worker *worker, *n; 162925511a47STejun Heo int i; 163025511a47STejun Heo 1631b2eb83d1SLai Jiangshan lockdep_assert_held(&pool->assoc_mutex); 1632d565ed63STejun Heo lockdep_assert_held(&pool->lock); 163325511a47STejun Heo 16345f7dabfdSLai Jiangshan /* dequeue and kick idle ones */ 1635ea1abd61SLai Jiangshan list_for_each_entry_safe(worker, n, &pool->idle_list, entry) { 1636ea1abd61SLai Jiangshan /* 163794cf58bbSTejun Heo * idle workers should be off @pool->idle_list until rebind 163894cf58bbSTejun Heo * is complete to avoid receiving premature local wake-ups. 1639ea1abd61SLai Jiangshan */ 1640ea1abd61SLai Jiangshan list_del_init(&worker->entry); 164125511a47STejun Heo 164225511a47STejun Heo /* 164394cf58bbSTejun Heo * worker_thread() will see the above dequeuing and call 164494cf58bbSTejun Heo * idle_worker_rebind(). 164525511a47STejun Heo */ 164625511a47STejun Heo wake_up_process(worker->task); 164725511a47STejun Heo } 164825511a47STejun Heo 1649ea1abd61SLai Jiangshan /* rebind busy workers */ 1650b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) { 165125511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 1652e2b6a6d5SJoonsoo Kim struct workqueue_struct *wq; 165325511a47STejun Heo 165425511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 165525511a47STejun Heo work_data_bits(rebind_work))) 165625511a47STejun Heo continue; 165725511a47STejun Heo 165825511a47STejun Heo debug_work_activate(rebind_work); 165990beca5dSTejun Heo 166090beca5dSTejun Heo /* 166194cf58bbSTejun Heo * wq doesn't really matter but let's keep @worker->pool 1662112202d9STejun Heo * and @pwq->pool consistent for sanity. 166390beca5dSTejun Heo */ 1664e34cdddbSTejun Heo if (std_worker_pool_pri(worker->pool)) 1665e2b6a6d5SJoonsoo Kim wq = system_highpri_wq; 1666e2b6a6d5SJoonsoo Kim else 1667e2b6a6d5SJoonsoo Kim wq = system_wq; 1668ec58815aSTejun Heo 1669112202d9STejun Heo insert_work(get_pwq(pool->cpu, wq), rebind_work, 167025511a47STejun Heo worker->scheduled.next, 167125511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 1672ec58815aSTejun Heo } 167325511a47STejun Heo } 167425511a47STejun Heo 1675c34056a3STejun Heo static struct worker *alloc_worker(void) 1676c34056a3STejun Heo { 1677c34056a3STejun Heo struct worker *worker; 1678c34056a3STejun Heo 1679c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1680c8e55f36STejun Heo if (worker) { 1681c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1682affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 168325511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1684e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1685e22bee78STejun Heo worker->flags = WORKER_PREP; 1686c8e55f36STejun Heo } 1687c34056a3STejun Heo return worker; 1688c34056a3STejun Heo } 1689c34056a3STejun Heo 1690c34056a3STejun Heo /** 1691c34056a3STejun Heo * create_worker - create a new workqueue worker 169263d95a91STejun Heo * @pool: pool the new worker will belong to 1693c34056a3STejun Heo * 169463d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1695c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1696c34056a3STejun Heo * destroy_worker(). 1697c34056a3STejun Heo * 1698c34056a3STejun Heo * CONTEXT: 1699c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1700c34056a3STejun Heo * 1701c34056a3STejun Heo * RETURNS: 1702c34056a3STejun Heo * Pointer to the newly created worker. 1703c34056a3STejun Heo */ 1704bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1705c34056a3STejun Heo { 1706e34cdddbSTejun Heo const char *pri = std_worker_pool_pri(pool) ? "H" : ""; 1707c34056a3STejun Heo struct worker *worker = NULL; 1708f3421797STejun Heo int id = -1; 1709c34056a3STejun Heo 1710d565ed63STejun Heo spin_lock_irq(&pool->lock); 1711bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 1712d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1713bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1714c34056a3STejun Heo goto fail; 1715d565ed63STejun Heo spin_lock_irq(&pool->lock); 1716c34056a3STejun Heo } 1717d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1718c34056a3STejun Heo 1719c34056a3STejun Heo worker = alloc_worker(); 1720c34056a3STejun Heo if (!worker) 1721c34056a3STejun Heo goto fail; 1722c34056a3STejun Heo 1723bd7bdd43STejun Heo worker->pool = pool; 1724c34056a3STejun Heo worker->id = id; 1725c34056a3STejun Heo 1726ec22ca5eSTejun Heo if (pool->cpu != WORK_CPU_UNBOUND) 172794dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 1728ec22ca5eSTejun Heo worker, cpu_to_node(pool->cpu), 1729ec22ca5eSTejun Heo "kworker/%u:%d%s", pool->cpu, id, pri); 1730f3421797STejun Heo else 1731f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 17323270476aSTejun Heo "kworker/u:%d%s", id, pri); 1733c34056a3STejun Heo if (IS_ERR(worker->task)) 1734c34056a3STejun Heo goto fail; 1735c34056a3STejun Heo 1736e34cdddbSTejun Heo if (std_worker_pool_pri(pool)) 17373270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 17383270476aSTejun Heo 1739db7bccf4STejun Heo /* 1740bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 174124647570STejun Heo * %POOL_DISASSOCIATED. The caller is responsible for ensuring the 1742bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1743bc2ae0f5STejun Heo * above the flag definition for details. 1744bc2ae0f5STejun Heo * 1745bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1746bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1747db7bccf4STejun Heo */ 174824647570STejun Heo if (!(pool->flags & POOL_DISASSOCIATED)) { 1749ec22ca5eSTejun Heo kthread_bind(worker->task, pool->cpu); 1750bc2ae0f5STejun Heo } else { 1751db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1752f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1753f3421797STejun Heo } 1754c34056a3STejun Heo 1755c34056a3STejun Heo return worker; 1756c34056a3STejun Heo fail: 1757c34056a3STejun Heo if (id >= 0) { 1758d565ed63STejun Heo spin_lock_irq(&pool->lock); 1759bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1760d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1761c34056a3STejun Heo } 1762c34056a3STejun Heo kfree(worker); 1763c34056a3STejun Heo return NULL; 1764c34056a3STejun Heo } 1765c34056a3STejun Heo 1766c34056a3STejun Heo /** 1767c34056a3STejun Heo * start_worker - start a newly created worker 1768c34056a3STejun Heo * @worker: worker to start 1769c34056a3STejun Heo * 1770706026c2STejun Heo * Make the pool aware of @worker and start it. 1771c34056a3STejun Heo * 1772c34056a3STejun Heo * CONTEXT: 1773d565ed63STejun Heo * spin_lock_irq(pool->lock). 1774c34056a3STejun Heo */ 1775c34056a3STejun Heo static void start_worker(struct worker *worker) 1776c34056a3STejun Heo { 1777cb444766STejun Heo worker->flags |= WORKER_STARTED; 1778bd7bdd43STejun Heo worker->pool->nr_workers++; 1779c8e55f36STejun Heo worker_enter_idle(worker); 1780c34056a3STejun Heo wake_up_process(worker->task); 1781c34056a3STejun Heo } 1782c34056a3STejun Heo 1783c34056a3STejun Heo /** 1784c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1785c34056a3STejun Heo * @worker: worker to be destroyed 1786c34056a3STejun Heo * 1787706026c2STejun Heo * Destroy @worker and adjust @pool stats accordingly. 1788c8e55f36STejun Heo * 1789c8e55f36STejun Heo * CONTEXT: 1790d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 1791c34056a3STejun Heo */ 1792c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1793c34056a3STejun Heo { 1794bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1795c34056a3STejun Heo int id = worker->id; 1796c34056a3STejun Heo 1797c34056a3STejun Heo /* sanity check frenzy */ 1798c34056a3STejun Heo BUG_ON(worker->current_work); 1799affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1800c34056a3STejun Heo 1801c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1802bd7bdd43STejun Heo pool->nr_workers--; 1803c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1804bd7bdd43STejun Heo pool->nr_idle--; 1805c8e55f36STejun Heo 1806c8e55f36STejun Heo list_del_init(&worker->entry); 1807cb444766STejun Heo worker->flags |= WORKER_DIE; 1808c8e55f36STejun Heo 1809d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1810c8e55f36STejun Heo 1811c34056a3STejun Heo kthread_stop(worker->task); 1812c34056a3STejun Heo kfree(worker); 1813c34056a3STejun Heo 1814d565ed63STejun Heo spin_lock_irq(&pool->lock); 1815bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1816c34056a3STejun Heo } 1817c34056a3STejun Heo 181863d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1819e22bee78STejun Heo { 182063d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1821e22bee78STejun Heo 1822d565ed63STejun Heo spin_lock_irq(&pool->lock); 1823e22bee78STejun Heo 182463d95a91STejun Heo if (too_many_workers(pool)) { 1825e22bee78STejun Heo struct worker *worker; 1826e22bee78STejun Heo unsigned long expires; 1827e22bee78STejun Heo 1828e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 182963d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1830e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1831e22bee78STejun Heo 1832e22bee78STejun Heo if (time_before(jiffies, expires)) 183363d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1834e22bee78STejun Heo else { 1835e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 183611ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 183763d95a91STejun Heo wake_up_worker(pool); 1838e22bee78STejun Heo } 1839e22bee78STejun Heo } 1840e22bee78STejun Heo 1841d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1842e22bee78STejun Heo } 1843e22bee78STejun Heo 1844e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1845e22bee78STejun Heo { 1846112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1847112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1848f3421797STejun Heo unsigned int cpu; 1849e22bee78STejun Heo 1850e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1851e22bee78STejun Heo return false; 1852e22bee78STejun Heo 1853e22bee78STejun Heo /* mayday mayday mayday */ 1854112202d9STejun Heo cpu = pwq->pool->cpu; 1855f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1856f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1857f3421797STejun Heo cpu = 0; 1858f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1859e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1860e22bee78STejun Heo return true; 1861e22bee78STejun Heo } 1862e22bee78STejun Heo 1863706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1864e22bee78STejun Heo { 186563d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1866e22bee78STejun Heo struct work_struct *work; 1867e22bee78STejun Heo 1868d565ed63STejun Heo spin_lock_irq(&pool->lock); 1869e22bee78STejun Heo 187063d95a91STejun Heo if (need_to_create_worker(pool)) { 1871e22bee78STejun Heo /* 1872e22bee78STejun Heo * We've been trying to create a new worker but 1873e22bee78STejun Heo * haven't been successful. We might be hitting an 1874e22bee78STejun Heo * allocation deadlock. Send distress signals to 1875e22bee78STejun Heo * rescuers. 1876e22bee78STejun Heo */ 187763d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1878e22bee78STejun Heo send_mayday(work); 1879e22bee78STejun Heo } 1880e22bee78STejun Heo 1881d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1882e22bee78STejun Heo 188363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1884e22bee78STejun Heo } 1885e22bee78STejun Heo 1886e22bee78STejun Heo /** 1887e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 188863d95a91STejun Heo * @pool: pool to create a new worker for 1889e22bee78STejun Heo * 189063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1891e22bee78STejun Heo * have at least one idle worker on return from this function. If 1892e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 189363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1894e22bee78STejun Heo * possible allocation deadlock. 1895e22bee78STejun Heo * 1896e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1897e22bee78STejun Heo * may_start_working() true. 1898e22bee78STejun Heo * 1899e22bee78STejun Heo * LOCKING: 1900d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1901e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1902e22bee78STejun Heo * manager. 1903e22bee78STejun Heo * 1904e22bee78STejun Heo * RETURNS: 1905d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1906e22bee78STejun Heo * otherwise. 1907e22bee78STejun Heo */ 190863d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 1909d565ed63STejun Heo __releases(&pool->lock) 1910d565ed63STejun Heo __acquires(&pool->lock) 1911e22bee78STejun Heo { 191263d95a91STejun Heo if (!need_to_create_worker(pool)) 1913e22bee78STejun Heo return false; 1914e22bee78STejun Heo restart: 1915d565ed63STejun Heo spin_unlock_irq(&pool->lock); 19169f9c2364STejun Heo 1917e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 191863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1919e22bee78STejun Heo 1920e22bee78STejun Heo while (true) { 1921e22bee78STejun Heo struct worker *worker; 1922e22bee78STejun Heo 1923bc2ae0f5STejun Heo worker = create_worker(pool); 1924e22bee78STejun Heo if (worker) { 192563d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1926d565ed63STejun Heo spin_lock_irq(&pool->lock); 1927e22bee78STejun Heo start_worker(worker); 192863d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1929e22bee78STejun Heo return true; 1930e22bee78STejun Heo } 1931e22bee78STejun Heo 193263d95a91STejun Heo if (!need_to_create_worker(pool)) 1933e22bee78STejun Heo break; 1934e22bee78STejun Heo 1935e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1936e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19379f9c2364STejun Heo 193863d95a91STejun Heo if (!need_to_create_worker(pool)) 1939e22bee78STejun Heo break; 1940e22bee78STejun Heo } 1941e22bee78STejun Heo 194263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1943d565ed63STejun Heo spin_lock_irq(&pool->lock); 194463d95a91STejun Heo if (need_to_create_worker(pool)) 1945e22bee78STejun Heo goto restart; 1946e22bee78STejun Heo return true; 1947e22bee78STejun Heo } 1948e22bee78STejun Heo 1949e22bee78STejun Heo /** 1950e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 195163d95a91STejun Heo * @pool: pool to destroy workers for 1952e22bee78STejun Heo * 195363d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1954e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1955e22bee78STejun Heo * 1956e22bee78STejun Heo * LOCKING: 1957d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1958e22bee78STejun Heo * multiple times. Called only from manager. 1959e22bee78STejun Heo * 1960e22bee78STejun Heo * RETURNS: 1961d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1962e22bee78STejun Heo * otherwise. 1963e22bee78STejun Heo */ 196463d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1965e22bee78STejun Heo { 1966e22bee78STejun Heo bool ret = false; 1967e22bee78STejun Heo 196863d95a91STejun Heo while (too_many_workers(pool)) { 1969e22bee78STejun Heo struct worker *worker; 1970e22bee78STejun Heo unsigned long expires; 1971e22bee78STejun Heo 197263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1973e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1974e22bee78STejun Heo 1975e22bee78STejun Heo if (time_before(jiffies, expires)) { 197663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1977e22bee78STejun Heo break; 1978e22bee78STejun Heo } 1979e22bee78STejun Heo 1980e22bee78STejun Heo destroy_worker(worker); 1981e22bee78STejun Heo ret = true; 1982e22bee78STejun Heo } 1983e22bee78STejun Heo 1984e22bee78STejun Heo return ret; 1985e22bee78STejun Heo } 1986e22bee78STejun Heo 1987e22bee78STejun Heo /** 1988e22bee78STejun Heo * manage_workers - manage worker pool 1989e22bee78STejun Heo * @worker: self 1990e22bee78STejun Heo * 1991706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1992e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1993706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1994e22bee78STejun Heo * 1995e22bee78STejun Heo * The caller can safely start processing works on false return. On 1996e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1997e22bee78STejun Heo * and may_start_working() is true. 1998e22bee78STejun Heo * 1999e22bee78STejun Heo * CONTEXT: 2000d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2001e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2002e22bee78STejun Heo * 2003e22bee78STejun Heo * RETURNS: 2004d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2005d565ed63STejun Heo * multiple times. Does GFP_KERNEL allocations. 2006e22bee78STejun Heo */ 2007e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2008e22bee78STejun Heo { 200963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2010e22bee78STejun Heo bool ret = false; 2011e22bee78STejun Heo 2012ee378aa4SLai Jiangshan if (pool->flags & POOL_MANAGING_WORKERS) 2013e22bee78STejun Heo return ret; 2014e22bee78STejun Heo 2015552a37e9SLai Jiangshan pool->flags |= POOL_MANAGING_WORKERS; 2016ee378aa4SLai Jiangshan 2017ee378aa4SLai Jiangshan /* 2018ee378aa4SLai Jiangshan * To simplify both worker management and CPU hotplug, hold off 2019ee378aa4SLai Jiangshan * management while hotplug is in progress. CPU hotplug path can't 2020ee378aa4SLai Jiangshan * grab %POOL_MANAGING_WORKERS to achieve this because that can 2021ee378aa4SLai Jiangshan * lead to idle worker depletion (all become busy thinking someone 2022ee378aa4SLai Jiangshan * else is managing) which in turn can result in deadlock under 2023b2eb83d1SLai Jiangshan * extreme circumstances. Use @pool->assoc_mutex to synchronize 2024ee378aa4SLai Jiangshan * manager against CPU hotplug. 2025ee378aa4SLai Jiangshan * 2026b2eb83d1SLai Jiangshan * assoc_mutex would always be free unless CPU hotplug is in 2027d565ed63STejun Heo * progress. trylock first without dropping @pool->lock. 2028ee378aa4SLai Jiangshan */ 2029b2eb83d1SLai Jiangshan if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { 2030d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2031b2eb83d1SLai Jiangshan mutex_lock(&pool->assoc_mutex); 2032ee378aa4SLai Jiangshan /* 2033ee378aa4SLai Jiangshan * CPU hotplug could have happened while we were waiting 2034b2eb83d1SLai Jiangshan * for assoc_mutex. Hotplug itself can't handle us 2035ee378aa4SLai Jiangshan * because manager isn't either on idle or busy list, and 2036706026c2STejun Heo * @pool's state and ours could have deviated. 2037ee378aa4SLai Jiangshan * 2038b2eb83d1SLai Jiangshan * As hotplug is now excluded via assoc_mutex, we can 2039ee378aa4SLai Jiangshan * simply try to bind. It will succeed or fail depending 2040706026c2STejun Heo * on @pool's current state. Try it and adjust 2041ee378aa4SLai Jiangshan * %WORKER_UNBOUND accordingly. 2042ee378aa4SLai Jiangshan */ 2043ee378aa4SLai Jiangshan if (worker_maybe_bind_and_lock(worker)) 2044ee378aa4SLai Jiangshan worker->flags &= ~WORKER_UNBOUND; 2045ee378aa4SLai Jiangshan else 2046ee378aa4SLai Jiangshan worker->flags |= WORKER_UNBOUND; 2047ee378aa4SLai Jiangshan 2048ee378aa4SLai Jiangshan ret = true; 2049ee378aa4SLai Jiangshan } 2050ee378aa4SLai Jiangshan 205111ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 2052e22bee78STejun Heo 2053e22bee78STejun Heo /* 2054e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 2055e22bee78STejun Heo * on return. 2056e22bee78STejun Heo */ 205763d95a91STejun Heo ret |= maybe_destroy_workers(pool); 205863d95a91STejun Heo ret |= maybe_create_worker(pool); 2059e22bee78STejun Heo 2060552a37e9SLai Jiangshan pool->flags &= ~POOL_MANAGING_WORKERS; 2061b2eb83d1SLai Jiangshan mutex_unlock(&pool->assoc_mutex); 2062e22bee78STejun Heo return ret; 2063e22bee78STejun Heo } 2064e22bee78STejun Heo 2065a62428c0STejun Heo /** 2066a62428c0STejun Heo * process_one_work - process single work 2067c34056a3STejun Heo * @worker: self 2068a62428c0STejun Heo * @work: work to process 2069a62428c0STejun Heo * 2070a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2071a62428c0STejun Heo * process a single work including synchronization against and 2072a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2073a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2074a62428c0STejun Heo * call this function to process a work. 2075a62428c0STejun Heo * 2076a62428c0STejun Heo * CONTEXT: 2077d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2078a62428c0STejun Heo */ 2079c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2080d565ed63STejun Heo __releases(&pool->lock) 2081d565ed63STejun Heo __acquires(&pool->lock) 20821da177e4SLinus Torvalds { 2083112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2084bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2085112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 208673f53c4aSTejun Heo int work_color; 20877e11629dSTejun Heo struct worker *collision; 20884e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20894e6045f1SJohannes Berg /* 2090a62428c0STejun Heo * It is permissible to free the struct work_struct from 2091a62428c0STejun Heo * inside the function that is called from it, this we need to 2092a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2093a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2094a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20954e6045f1SJohannes Berg */ 20964d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20974d82a1deSPeter Zijlstra 20984d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20994e6045f1SJohannes Berg #endif 21006fec10a1STejun Heo /* 21016fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 21026fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 210324647570STejun Heo * unbound or a disassociated pool. 21046fec10a1STejun Heo */ 21055f7dabfdSLai Jiangshan WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) && 210624647570STejun Heo !(pool->flags & POOL_DISASSOCIATED) && 2107ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 210825511a47STejun Heo 21097e11629dSTejun Heo /* 21107e11629dSTejun Heo * A single work shouldn't be executed concurrently by 21117e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 21127e11629dSTejun Heo * already processing the work. If so, defer the work to the 21137e11629dSTejun Heo * currently executing one. 21147e11629dSTejun Heo */ 2115c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 21167e11629dSTejun Heo if (unlikely(collision)) { 21177e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 21187e11629dSTejun Heo return; 21197e11629dSTejun Heo } 21201da177e4SLinus Torvalds 21218930cabaSTejun Heo /* claim and dequeue */ 21221da177e4SLinus Torvalds debug_work_deactivate(work); 2123c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2124c34056a3STejun Heo worker->current_work = work; 2125a2c1c57bSTejun Heo worker->current_func = work->func; 2126112202d9STejun Heo worker->current_pwq = pwq; 212773f53c4aSTejun Heo work_color = get_work_color(work); 21287a22ad75STejun Heo 2129a62428c0STejun Heo list_del_init(&work->entry); 2130a62428c0STejun Heo 2131649027d7STejun Heo /* 2132fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2133fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2134fb0e7bebSTejun Heo */ 2135fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2136fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2137fb0e7bebSTejun Heo 2138974271c4STejun Heo /* 2139d565ed63STejun Heo * Unbound pool isn't concurrency managed and work items should be 2140974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2141974271c4STejun Heo */ 214263d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 214363d95a91STejun Heo wake_up_worker(pool); 2144974271c4STejun Heo 21458930cabaSTejun Heo /* 21467c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2147d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 214823657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 214923657bb1STejun Heo * disabled. 21508930cabaSTejun Heo */ 21517c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 21521da177e4SLinus Torvalds 2153d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2154365970a1SDavid Howells 2155112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 21563295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2157e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2158a2c1c57bSTejun Heo worker->current_func(work); 2159e36c886aSArjan van de Ven /* 2160e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2161e36c886aSArjan van de Ven * point will only record its address. 2162e36c886aSArjan van de Ven */ 2163e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21643295f0efSIngo Molnar lock_map_release(&lockdep_map); 2165112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21661da177e4SLinus Torvalds 2167d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2168044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2169044c782cSValentin Ilie " last function: %pf\n", 2170a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2171a2c1c57bSTejun Heo worker->current_func); 2172d5abe669SPeter Zijlstra debug_show_held_locks(current); 2173d5abe669SPeter Zijlstra dump_stack(); 2174d5abe669SPeter Zijlstra } 2175d5abe669SPeter Zijlstra 2176d565ed63STejun Heo spin_lock_irq(&pool->lock); 2177a62428c0STejun Heo 2178fb0e7bebSTejun Heo /* clear cpu intensive status */ 2179fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2180fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2181fb0e7bebSTejun Heo 2182a62428c0STejun Heo /* we're done with it, release */ 218342f8570fSSasha Levin hash_del(&worker->hentry); 2184c34056a3STejun Heo worker->current_work = NULL; 2185a2c1c57bSTejun Heo worker->current_func = NULL; 2186112202d9STejun Heo worker->current_pwq = NULL; 2187112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 2190affee4b2STejun Heo /** 2191affee4b2STejun Heo * process_scheduled_works - process scheduled works 2192affee4b2STejun Heo * @worker: self 2193affee4b2STejun Heo * 2194affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2195affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2196affee4b2STejun Heo * fetches a work from the top and executes it. 2197affee4b2STejun Heo * 2198affee4b2STejun Heo * CONTEXT: 2199d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2200affee4b2STejun Heo * multiple times. 2201affee4b2STejun Heo */ 2202affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 22031da177e4SLinus Torvalds { 2204affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2205affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2206a62428c0STejun Heo struct work_struct, entry); 2207c34056a3STejun Heo process_one_work(worker, work); 2208a62428c0STejun Heo } 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 22114690c4abSTejun Heo /** 22124690c4abSTejun Heo * worker_thread - the worker thread function 2213c34056a3STejun Heo * @__worker: self 22144690c4abSTejun Heo * 2215706026c2STejun Heo * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2216706026c2STejun Heo * of these per each cpu. These workers process all works regardless of 2217e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2218e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2219e22bee78STejun Heo * rescuer_thread(). 22204690c4abSTejun Heo */ 2221c34056a3STejun Heo static int worker_thread(void *__worker) 22221da177e4SLinus Torvalds { 2223c34056a3STejun Heo struct worker *worker = __worker; 2224bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 22251da177e4SLinus Torvalds 2226e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2227e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2228c8e55f36STejun Heo woke_up: 2229d565ed63STejun Heo spin_lock_irq(&pool->lock); 2230affee4b2STejun Heo 22315f7dabfdSLai Jiangshan /* we are off idle list if destruction or rebind is requested */ 22325f7dabfdSLai Jiangshan if (unlikely(list_empty(&worker->entry))) { 2233d565ed63STejun Heo spin_unlock_irq(&pool->lock); 223425511a47STejun Heo 22355f7dabfdSLai Jiangshan /* if DIE is set, destruction is requested */ 223625511a47STejun Heo if (worker->flags & WORKER_DIE) { 2237e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2238c8e55f36STejun Heo return 0; 2239c8e55f36STejun Heo } 2240c8e55f36STejun Heo 22415f7dabfdSLai Jiangshan /* otherwise, rebind */ 224225511a47STejun Heo idle_worker_rebind(worker); 224325511a47STejun Heo goto woke_up; 224425511a47STejun Heo } 224525511a47STejun Heo 2246c8e55f36STejun Heo worker_leave_idle(worker); 2247db7bccf4STejun Heo recheck: 2248e22bee78STejun Heo /* no more worker necessary? */ 224963d95a91STejun Heo if (!need_more_worker(pool)) 2250e22bee78STejun Heo goto sleep; 2251e22bee78STejun Heo 2252e22bee78STejun Heo /* do we need to manage? */ 225363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2254e22bee78STejun Heo goto recheck; 2255e22bee78STejun Heo 2256c8e55f36STejun Heo /* 2257c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2258c8e55f36STejun Heo * preparing to process a work or actually processing it. 2259c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2260c8e55f36STejun Heo */ 2261c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2262c8e55f36STejun Heo 2263e22bee78STejun Heo /* 2264e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2265e22bee78STejun Heo * at least one idle worker or that someone else has already 2266e22bee78STejun Heo * assumed the manager role. 2267e22bee78STejun Heo */ 2268e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2269e22bee78STejun Heo 2270e22bee78STejun Heo do { 2271affee4b2STejun Heo struct work_struct *work = 2272bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2273affee4b2STejun Heo struct work_struct, entry); 2274affee4b2STejun Heo 2275c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2276affee4b2STejun Heo /* optimization path, not strictly necessary */ 2277affee4b2STejun Heo process_one_work(worker, work); 2278affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2279affee4b2STejun Heo process_scheduled_works(worker); 2280affee4b2STejun Heo } else { 2281c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2282affee4b2STejun Heo process_scheduled_works(worker); 2283affee4b2STejun Heo } 228463d95a91STejun Heo } while (keep_working(pool)); 2285affee4b2STejun Heo 2286e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2287d313dd85STejun Heo sleep: 228863d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2289e22bee78STejun Heo goto recheck; 2290d313dd85STejun Heo 2291c8e55f36STejun Heo /* 2292d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2293d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2294d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2295d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2296d565ed63STejun Heo * event. 2297c8e55f36STejun Heo */ 2298c8e55f36STejun Heo worker_enter_idle(worker); 2299c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2300d565ed63STejun Heo spin_unlock_irq(&pool->lock); 23011da177e4SLinus Torvalds schedule(); 2302c8e55f36STejun Heo goto woke_up; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 2305e22bee78STejun Heo /** 2306e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2307111c225aSTejun Heo * @__rescuer: self 2308e22bee78STejun Heo * 2309e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2310e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2311e22bee78STejun Heo * 2312706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2313e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2314e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2315e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2316e22bee78STejun Heo * the problem rescuer solves. 2317e22bee78STejun Heo * 2318706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2319706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2320e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2321e22bee78STejun Heo * 2322e22bee78STejun Heo * This should happen rarely. 2323e22bee78STejun Heo */ 2324111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2325e22bee78STejun Heo { 2326111c225aSTejun Heo struct worker *rescuer = __rescuer; 2327111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2328e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2329f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2330e22bee78STejun Heo unsigned int cpu; 2331e22bee78STejun Heo 2332e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2333111c225aSTejun Heo 2334111c225aSTejun Heo /* 2335111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2336111c225aSTejun Heo * doesn't participate in concurrency management. 2337111c225aSTejun Heo */ 2338111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2339e22bee78STejun Heo repeat: 2340e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 23411da177e4SLinus Torvalds 2342412d32e6SMike Galbraith if (kthread_should_stop()) { 2343412d32e6SMike Galbraith __set_current_state(TASK_RUNNING); 2344111c225aSTejun Heo rescuer->task->flags &= ~PF_WQ_WORKER; 2345e22bee78STejun Heo return 0; 2346412d32e6SMike Galbraith } 23471da177e4SLinus Torvalds 2348f3421797STejun Heo /* 2349f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2350f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2351f3421797STejun Heo */ 2352f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2353f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2354112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(tcpu, wq); 2355112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2356e22bee78STejun Heo struct work_struct *work, *n; 2357e22bee78STejun Heo 2358e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2359f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2360e22bee78STejun Heo 2361e22bee78STejun Heo /* migrate to the target cpu if possible */ 2362bd7bdd43STejun Heo rescuer->pool = pool; 2363e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2364e22bee78STejun Heo 2365e22bee78STejun Heo /* 2366e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2367e22bee78STejun Heo * process'em. 2368e22bee78STejun Heo */ 2369e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2370bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2371112202d9STejun Heo if (get_work_pwq(work) == pwq) 2372e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2373e22bee78STejun Heo 2374e22bee78STejun Heo process_scheduled_works(rescuer); 23757576958aSTejun Heo 23767576958aSTejun Heo /* 2377d565ed63STejun Heo * Leave this pool. If keep_working() is %true, notify a 23787576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23797576958aSTejun Heo * and stalling the execution. 23807576958aSTejun Heo */ 238163d95a91STejun Heo if (keep_working(pool)) 238263d95a91STejun Heo wake_up_worker(pool); 23837576958aSTejun Heo 2384d565ed63STejun Heo spin_unlock_irq(&pool->lock); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 2387111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2388111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2389e22bee78STejun Heo schedule(); 2390e22bee78STejun Heo goto repeat; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 2393fc2e4d70SOleg Nesterov struct wq_barrier { 2394fc2e4d70SOleg Nesterov struct work_struct work; 2395fc2e4d70SOleg Nesterov struct completion done; 2396fc2e4d70SOleg Nesterov }; 2397fc2e4d70SOleg Nesterov 2398fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2399fc2e4d70SOleg Nesterov { 2400fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2401fc2e4d70SOleg Nesterov complete(&barr->done); 2402fc2e4d70SOleg Nesterov } 2403fc2e4d70SOleg Nesterov 24044690c4abSTejun Heo /** 24054690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2406112202d9STejun Heo * @pwq: pwq to insert barrier into 24074690c4abSTejun Heo * @barr: wq_barrier to insert 2408affee4b2STejun Heo * @target: target work to attach @barr to 2409affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 24104690c4abSTejun Heo * 2411affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2412affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2413affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2414affee4b2STejun Heo * cpu. 2415affee4b2STejun Heo * 2416affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2417affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2418affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2419affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2420affee4b2STejun Heo * after a work with LINKED flag set. 2421affee4b2STejun Heo * 2422affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2423112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24244690c4abSTejun Heo * 24254690c4abSTejun Heo * CONTEXT: 2426d565ed63STejun Heo * spin_lock_irq(pool->lock). 24274690c4abSTejun Heo */ 2428112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2429affee4b2STejun Heo struct wq_barrier *barr, 2430affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2431fc2e4d70SOleg Nesterov { 2432affee4b2STejun Heo struct list_head *head; 2433affee4b2STejun Heo unsigned int linked = 0; 2434affee4b2STejun Heo 2435dc186ad7SThomas Gleixner /* 2436d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2437dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2438dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2439dc186ad7SThomas Gleixner * might deadlock. 2440dc186ad7SThomas Gleixner */ 2441ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 244222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2443fc2e4d70SOleg Nesterov init_completion(&barr->done); 244483c22520SOleg Nesterov 2445affee4b2STejun Heo /* 2446affee4b2STejun Heo * If @target is currently being executed, schedule the 2447affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2448affee4b2STejun Heo */ 2449affee4b2STejun Heo if (worker) 2450affee4b2STejun Heo head = worker->scheduled.next; 2451affee4b2STejun Heo else { 2452affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2453affee4b2STejun Heo 2454affee4b2STejun Heo head = target->entry.next; 2455affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2456affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2457affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2458affee4b2STejun Heo } 2459affee4b2STejun Heo 2460dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2461112202d9STejun Heo insert_work(pwq, &barr->work, head, 2462affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2463fc2e4d70SOleg Nesterov } 2464fc2e4d70SOleg Nesterov 246573f53c4aSTejun Heo /** 2466112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 246773f53c4aSTejun Heo * @wq: workqueue being flushed 246873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 246973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 247073f53c4aSTejun Heo * 2471112202d9STejun Heo * Prepare pwqs for workqueue flushing. 247273f53c4aSTejun Heo * 2473112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2474112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2475112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2476112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2477112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 247873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 247973f53c4aSTejun Heo * 248073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 248173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 248273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 248373f53c4aSTejun Heo * is returned. 248473f53c4aSTejun Heo * 2485112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 248673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 248773f53c4aSTejun Heo * advanced to @work_color. 248873f53c4aSTejun Heo * 248973f53c4aSTejun Heo * CONTEXT: 249073f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 249173f53c4aSTejun Heo * 249273f53c4aSTejun Heo * RETURNS: 249373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 249473f53c4aSTejun Heo * otherwise. 249573f53c4aSTejun Heo */ 2496112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 249773f53c4aSTejun Heo int flush_color, int work_color) 24981da177e4SLinus Torvalds { 249973f53c4aSTejun Heo bool wait = false; 250073f53c4aSTejun Heo unsigned int cpu; 25011da177e4SLinus Torvalds 250273f53c4aSTejun Heo if (flush_color >= 0) { 2503112202d9STejun Heo BUG_ON(atomic_read(&wq->nr_pwqs_to_flush)); 2504112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2505dc186ad7SThomas Gleixner } 250614441960SOleg Nesterov 2507112202d9STejun Heo for_each_pwq_cpu(cpu, wq) { 2508112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 2509112202d9STejun Heo struct worker_pool *pool = pwq->pool; 25101da177e4SLinus Torvalds 2511d565ed63STejun Heo spin_lock_irq(&pool->lock); 251273f53c4aSTejun Heo 251373f53c4aSTejun Heo if (flush_color >= 0) { 2514112202d9STejun Heo BUG_ON(pwq->flush_color != -1); 251573f53c4aSTejun Heo 2516112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2517112202d9STejun Heo pwq->flush_color = flush_color; 2518112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 251973f53c4aSTejun Heo wait = true; 25201da177e4SLinus Torvalds } 252173f53c4aSTejun Heo } 252273f53c4aSTejun Heo 252373f53c4aSTejun Heo if (work_color >= 0) { 2524112202d9STejun Heo BUG_ON(work_color != work_next_color(pwq->work_color)); 2525112202d9STejun Heo pwq->work_color = work_color; 252673f53c4aSTejun Heo } 252773f53c4aSTejun Heo 2528d565ed63STejun Heo spin_unlock_irq(&pool->lock); 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 2531112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 253273f53c4aSTejun Heo complete(&wq->first_flusher->done); 253373f53c4aSTejun Heo 253473f53c4aSTejun Heo return wait; 253583c22520SOleg Nesterov } 25361da177e4SLinus Torvalds 25370fcb78c2SRolf Eike Beer /** 25381da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25390fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25401da177e4SLinus Torvalds * 25411da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 25421da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 25431da177e4SLinus Torvalds * 2544fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2545fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 25461da177e4SLinus Torvalds */ 25477ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25481da177e4SLinus Torvalds { 254973f53c4aSTejun Heo struct wq_flusher this_flusher = { 255073f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 255173f53c4aSTejun Heo .flush_color = -1, 255273f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 255373f53c4aSTejun Heo }; 255473f53c4aSTejun Heo int next_color; 2555b1f4ec17SOleg Nesterov 25563295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25573295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 255873f53c4aSTejun Heo 255973f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 256073f53c4aSTejun Heo 256173f53c4aSTejun Heo /* 256273f53c4aSTejun Heo * Start-to-wait phase 256373f53c4aSTejun Heo */ 256473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 256573f53c4aSTejun Heo 256673f53c4aSTejun Heo if (next_color != wq->flush_color) { 256773f53c4aSTejun Heo /* 256873f53c4aSTejun Heo * Color space is not full. The current work_color 256973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 257073f53c4aSTejun Heo * by one. 257173f53c4aSTejun Heo */ 257273f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 257373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 257473f53c4aSTejun Heo wq->work_color = next_color; 257573f53c4aSTejun Heo 257673f53c4aSTejun Heo if (!wq->first_flusher) { 257773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 257873f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 257973f53c4aSTejun Heo 258073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 258173f53c4aSTejun Heo 2582112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 258373f53c4aSTejun Heo wq->work_color)) { 258473f53c4aSTejun Heo /* nothing to flush, done */ 258573f53c4aSTejun Heo wq->flush_color = next_color; 258673f53c4aSTejun Heo wq->first_flusher = NULL; 258773f53c4aSTejun Heo goto out_unlock; 258873f53c4aSTejun Heo } 258973f53c4aSTejun Heo } else { 259073f53c4aSTejun Heo /* wait in queue */ 259173f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 259273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2593112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 259473f53c4aSTejun Heo } 259573f53c4aSTejun Heo } else { 259673f53c4aSTejun Heo /* 259773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 259873f53c4aSTejun Heo * The next flush completion will assign us 259973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 260073f53c4aSTejun Heo */ 260173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 260273f53c4aSTejun Heo } 260373f53c4aSTejun Heo 260473f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 260573f53c4aSTejun Heo 260673f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 260773f53c4aSTejun Heo 260873f53c4aSTejun Heo /* 260973f53c4aSTejun Heo * Wake-up-and-cascade phase 261073f53c4aSTejun Heo * 261173f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 261273f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 261373f53c4aSTejun Heo */ 261473f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 261573f53c4aSTejun Heo return; 261673f53c4aSTejun Heo 261773f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 261873f53c4aSTejun Heo 26194ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26204ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26214ce48b37STejun Heo goto out_unlock; 26224ce48b37STejun Heo 262373f53c4aSTejun Heo wq->first_flusher = NULL; 262473f53c4aSTejun Heo 262573f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 262673f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 262773f53c4aSTejun Heo 262873f53c4aSTejun Heo while (true) { 262973f53c4aSTejun Heo struct wq_flusher *next, *tmp; 263073f53c4aSTejun Heo 263173f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 263273f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 263373f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 263473f53c4aSTejun Heo break; 263573f53c4aSTejun Heo list_del_init(&next->list); 263673f53c4aSTejun Heo complete(&next->done); 263773f53c4aSTejun Heo } 263873f53c4aSTejun Heo 263973f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 264073f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 264173f53c4aSTejun Heo 264273f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 264373f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 264473f53c4aSTejun Heo 264573f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 264673f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 264773f53c4aSTejun Heo /* 264873f53c4aSTejun Heo * Assign the same color to all overflowed 264973f53c4aSTejun Heo * flushers, advance work_color and append to 265073f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 265173f53c4aSTejun Heo * phase for these overflowed flushers. 265273f53c4aSTejun Heo */ 265373f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 265473f53c4aSTejun Heo tmp->flush_color = wq->work_color; 265573f53c4aSTejun Heo 265673f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 265773f53c4aSTejun Heo 265873f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 265973f53c4aSTejun Heo &wq->flusher_queue); 2660112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 266173f53c4aSTejun Heo } 266273f53c4aSTejun Heo 266373f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 266473f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 266573f53c4aSTejun Heo break; 266673f53c4aSTejun Heo } 266773f53c4aSTejun Heo 266873f53c4aSTejun Heo /* 266973f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2670112202d9STejun Heo * the new first flusher and arm pwqs. 267173f53c4aSTejun Heo */ 267273f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 267373f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 267473f53c4aSTejun Heo 267573f53c4aSTejun Heo list_del_init(&next->list); 267673f53c4aSTejun Heo wq->first_flusher = next; 267773f53c4aSTejun Heo 2678112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 267973f53c4aSTejun Heo break; 268073f53c4aSTejun Heo 268173f53c4aSTejun Heo /* 268273f53c4aSTejun Heo * Meh... this color is already done, clear first 268373f53c4aSTejun Heo * flusher and repeat cascading. 268473f53c4aSTejun Heo */ 268573f53c4aSTejun Heo wq->first_flusher = NULL; 268673f53c4aSTejun Heo } 268773f53c4aSTejun Heo 268873f53c4aSTejun Heo out_unlock: 268973f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26901da177e4SLinus Torvalds } 2691ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26921da177e4SLinus Torvalds 26939c5a2ba7STejun Heo /** 26949c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26959c5a2ba7STejun Heo * @wq: workqueue to drain 26969c5a2ba7STejun Heo * 26979c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26989c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26999c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 27009c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 27019c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 27029c5a2ba7STejun Heo * takes too long. 27039c5a2ba7STejun Heo */ 27049c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 27059c5a2ba7STejun Heo { 27069c5a2ba7STejun Heo unsigned int flush_cnt = 0; 27079c5a2ba7STejun Heo unsigned int cpu; 27089c5a2ba7STejun Heo 27099c5a2ba7STejun Heo /* 27109c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 27119c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 27129c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 27139c5a2ba7STejun Heo */ 27149c5a2ba7STejun Heo spin_lock(&workqueue_lock); 27159c5a2ba7STejun Heo if (!wq->nr_drainers++) 27169c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 27179c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 27189c5a2ba7STejun Heo reflush: 27199c5a2ba7STejun Heo flush_workqueue(wq); 27209c5a2ba7STejun Heo 2721112202d9STejun Heo for_each_pwq_cpu(cpu, wq) { 2722112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 2723fa2563e4SThomas Tuttle bool drained; 27249c5a2ba7STejun Heo 2725112202d9STejun Heo spin_lock_irq(&pwq->pool->lock); 2726112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2727112202d9STejun Heo spin_unlock_irq(&pwq->pool->lock); 2728fa2563e4SThomas Tuttle 2729fa2563e4SThomas Tuttle if (drained) 27309c5a2ba7STejun Heo continue; 27319c5a2ba7STejun Heo 27329c5a2ba7STejun Heo if (++flush_cnt == 10 || 27339c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2734044c782cSValentin Ilie pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 27359c5a2ba7STejun Heo wq->name, flush_cnt); 27369c5a2ba7STejun Heo goto reflush; 27379c5a2ba7STejun Heo } 27389c5a2ba7STejun Heo 27399c5a2ba7STejun Heo spin_lock(&workqueue_lock); 27409c5a2ba7STejun Heo if (!--wq->nr_drainers) 27419c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 27429c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 27439c5a2ba7STejun Heo } 27449c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27459c5a2ba7STejun Heo 2746606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2747baf59022STejun Heo { 2748baf59022STejun Heo struct worker *worker = NULL; 2749c9e7cf27STejun Heo struct worker_pool *pool; 2750112202d9STejun Heo struct pool_workqueue *pwq; 2751baf59022STejun Heo 2752baf59022STejun Heo might_sleep(); 2753c9e7cf27STejun Heo pool = get_work_pool(work); 2754c9e7cf27STejun Heo if (!pool) 2755baf59022STejun Heo return false; 2756baf59022STejun Heo 2757d565ed63STejun Heo spin_lock_irq(&pool->lock); 27580b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2759112202d9STejun Heo pwq = get_work_pwq(work); 2760112202d9STejun Heo if (pwq) { 2761112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2762baf59022STejun Heo goto already_gone; 2763606a5020STejun Heo } else { 2764c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2765baf59022STejun Heo if (!worker) 2766baf59022STejun Heo goto already_gone; 2767112202d9STejun Heo pwq = worker->current_pwq; 2768606a5020STejun Heo } 2769baf59022STejun Heo 2770112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2771d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2772baf59022STejun Heo 2773e159489bSTejun Heo /* 2774e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2775e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2776e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2777e159489bSTejun Heo * access. 2778e159489bSTejun Heo */ 2779112202d9STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2780112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2781e159489bSTejun Heo else 2782112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2783112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2784e159489bSTejun Heo 2785baf59022STejun Heo return true; 2786baf59022STejun Heo already_gone: 2787d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2788baf59022STejun Heo return false; 2789baf59022STejun Heo } 2790baf59022STejun Heo 2791db700897SOleg Nesterov /** 2792401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2793401a8d04STejun Heo * @work: the work to flush 2794db700897SOleg Nesterov * 2795606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2796606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2797401a8d04STejun Heo * 2798401a8d04STejun Heo * RETURNS: 2799401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2800401a8d04STejun Heo * %false if it was already idle. 2801db700897SOleg Nesterov */ 2802401a8d04STejun Heo bool flush_work(struct work_struct *work) 2803db700897SOleg Nesterov { 2804db700897SOleg Nesterov struct wq_barrier barr; 2805db700897SOleg Nesterov 28060976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 28070976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 28080976dfc1SStephen Boyd 2809606a5020STejun Heo if (start_flush_work(work, &barr)) { 2810db700897SOleg Nesterov wait_for_completion(&barr.done); 2811dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2812401a8d04STejun Heo return true; 2813606a5020STejun Heo } else { 2814401a8d04STejun Heo return false; 2815db700897SOleg Nesterov } 2816606a5020STejun Heo } 2817db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2818db700897SOleg Nesterov 281936e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2820401a8d04STejun Heo { 2821bbb68dfaSTejun Heo unsigned long flags; 28221f1f642eSOleg Nesterov int ret; 28231f1f642eSOleg Nesterov 28241f1f642eSOleg Nesterov do { 2825bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2826bbb68dfaSTejun Heo /* 2827bbb68dfaSTejun Heo * If someone else is canceling, wait for the same event it 2828bbb68dfaSTejun Heo * would be waiting for before retrying. 2829bbb68dfaSTejun Heo */ 2830bbb68dfaSTejun Heo if (unlikely(ret == -ENOENT)) 2831606a5020STejun Heo flush_work(work); 28321f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28331f1f642eSOleg Nesterov 2834bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2835bbb68dfaSTejun Heo mark_work_canceling(work); 2836bbb68dfaSTejun Heo local_irq_restore(flags); 2837bbb68dfaSTejun Heo 2838606a5020STejun Heo flush_work(work); 28397a22ad75STejun Heo clear_work_data(work); 28401f1f642eSOleg Nesterov return ret; 28411f1f642eSOleg Nesterov } 28421f1f642eSOleg Nesterov 28436e84d644SOleg Nesterov /** 2844401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2845401a8d04STejun Heo * @work: the work to cancel 28466e84d644SOleg Nesterov * 2847401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2848401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2849401a8d04STejun Heo * another workqueue. On return from this function, @work is 2850401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28511f1f642eSOleg Nesterov * 2852401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2853401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28546e84d644SOleg Nesterov * 2855401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28566e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2857401a8d04STejun Heo * 2858401a8d04STejun Heo * RETURNS: 2859401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28606e84d644SOleg Nesterov */ 2861401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28626e84d644SOleg Nesterov { 286336e227d2STejun Heo return __cancel_work_timer(work, false); 2864b89deed3SOleg Nesterov } 286528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2866b89deed3SOleg Nesterov 28676e84d644SOleg Nesterov /** 2868401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2869401a8d04STejun Heo * @dwork: the delayed work to flush 28706e84d644SOleg Nesterov * 2871401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2872401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2873401a8d04STejun Heo * considers the last queueing instance of @dwork. 28741f1f642eSOleg Nesterov * 2875401a8d04STejun Heo * RETURNS: 2876401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2877401a8d04STejun Heo * %false if it was already idle. 28786e84d644SOleg Nesterov */ 2879401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2880401a8d04STejun Heo { 28818930cabaSTejun Heo local_irq_disable(); 2882401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 288360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 28848930cabaSTejun Heo local_irq_enable(); 2885401a8d04STejun Heo return flush_work(&dwork->work); 2886401a8d04STejun Heo } 2887401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2888401a8d04STejun Heo 2889401a8d04STejun Heo /** 289057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 289157b30ae7STejun Heo * @dwork: delayed_work to cancel 289209383498STejun Heo * 289357b30ae7STejun Heo * Kill off a pending delayed_work. Returns %true if @dwork was pending 289457b30ae7STejun Heo * and canceled; %false if wasn't pending. Note that the work callback 289557b30ae7STejun Heo * function may still be running on return, unless it returns %true and the 289657b30ae7STejun Heo * work doesn't re-arm itself. Explicitly flush or use 289757b30ae7STejun Heo * cancel_delayed_work_sync() to wait on it. 289809383498STejun Heo * 289957b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 290009383498STejun Heo */ 290157b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 290209383498STejun Heo { 290357b30ae7STejun Heo unsigned long flags; 290457b30ae7STejun Heo int ret; 290557b30ae7STejun Heo 290657b30ae7STejun Heo do { 290757b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 290857b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 290957b30ae7STejun Heo 291057b30ae7STejun Heo if (unlikely(ret < 0)) 291157b30ae7STejun Heo return false; 291257b30ae7STejun Heo 29137c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 29147c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 291557b30ae7STejun Heo local_irq_restore(flags); 2916c0158ca6SDan Magenheimer return ret; 291709383498STejun Heo } 291857b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 291909383498STejun Heo 292009383498STejun Heo /** 2921401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2922401a8d04STejun Heo * @dwork: the delayed work cancel 2923401a8d04STejun Heo * 2924401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2925401a8d04STejun Heo * 2926401a8d04STejun Heo * RETURNS: 2927401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2928401a8d04STejun Heo */ 2929401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29306e84d644SOleg Nesterov { 293136e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29326e84d644SOleg Nesterov } 2933f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29341da177e4SLinus Torvalds 29350fcb78c2SRolf Eike Beer /** 2936c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2937c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2938c1a220e7SZhang Rui * @work: job to be done 2939c1a220e7SZhang Rui * 2940c1a220e7SZhang Rui * This puts a job on a specific cpu 2941c1a220e7SZhang Rui */ 2942d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 2943c1a220e7SZhang Rui { 2944d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2945c1a220e7SZhang Rui } 2946c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2947c1a220e7SZhang Rui 29480fcb78c2SRolf Eike Beer /** 2949ae90dd5dSDave Jones * schedule_work - put work task in global workqueue 29501da177e4SLinus Torvalds * @work: job to be done 29511da177e4SLinus Torvalds * 2952d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2953d4283e93STejun Heo * %true otherwise. 295452bad64dSDavid Howells * 29550fcb78c2SRolf Eike Beer * This puts a job in the kernel-global workqueue if it was not already 29560fcb78c2SRolf Eike Beer * queued and leaves it in the same position on the kernel-global 29570fcb78c2SRolf Eike Beer * workqueue otherwise. 29581da177e4SLinus Torvalds */ 2959d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29601da177e4SLinus Torvalds { 29610fcb78c2SRolf Eike Beer return queue_work(system_wq, work); 29621da177e4SLinus Torvalds } 29630fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work); 29641da177e4SLinus Torvalds 29650fcb78c2SRolf Eike Beer /** 29660fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29670fcb78c2SRolf Eike Beer * @cpu: cpu to use 29680fcb78c2SRolf Eike Beer * @dwork: job to be done 29690fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29700fcb78c2SRolf Eike Beer * 29710fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29720fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29730fcb78c2SRolf Eike Beer */ 2974d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2975d4283e93STejun Heo unsigned long delay) 29761da177e4SLinus Torvalds { 2977d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29781da177e4SLinus Torvalds } 2979ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29801da177e4SLinus Torvalds 2981b6136773SAndrew Morton /** 29820a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29830a13c00eSTejun Heo * @dwork: job to be done 29840a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29850a13c00eSTejun Heo * 29860a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29870a13c00eSTejun Heo * workqueue. 29880a13c00eSTejun Heo */ 2989d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29900a13c00eSTejun Heo { 29910a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29920a13c00eSTejun Heo } 29930a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29940a13c00eSTejun Heo 29950a13c00eSTejun Heo /** 299631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2997b6136773SAndrew Morton * @func: the function to call 2998b6136773SAndrew Morton * 299931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 300031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3001b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 300231ddd871STejun Heo * 300331ddd871STejun Heo * RETURNS: 300431ddd871STejun Heo * 0 on success, -errno on failure. 3005b6136773SAndrew Morton */ 300665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 300715316ba8SChristoph Lameter { 300815316ba8SChristoph Lameter int cpu; 300938f51568SNamhyung Kim struct work_struct __percpu *works; 301015316ba8SChristoph Lameter 3011b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3012b6136773SAndrew Morton if (!works) 301315316ba8SChristoph Lameter return -ENOMEM; 3014b6136773SAndrew Morton 301595402b38SGautham R Shenoy get_online_cpus(); 301693981800STejun Heo 301715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30189bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30199bfb1839SIngo Molnar 30209bfb1839SIngo Molnar INIT_WORK(work, func); 30218de6d308SOleg Nesterov schedule_work_on(cpu, work); 302215316ba8SChristoph Lameter } 302393981800STejun Heo 302493981800STejun Heo for_each_online_cpu(cpu) 30258616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 302693981800STejun Heo 302795402b38SGautham R Shenoy put_online_cpus(); 3028b6136773SAndrew Morton free_percpu(works); 302915316ba8SChristoph Lameter return 0; 303015316ba8SChristoph Lameter } 303115316ba8SChristoph Lameter 3032eef6a7d5SAlan Stern /** 3033eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3034eef6a7d5SAlan Stern * 3035eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3036eef6a7d5SAlan Stern * completion. 3037eef6a7d5SAlan Stern * 3038eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3039eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3040eef6a7d5SAlan Stern * will lead to deadlock: 3041eef6a7d5SAlan Stern * 3042eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3043eef6a7d5SAlan Stern * a lock held by your code or its caller. 3044eef6a7d5SAlan Stern * 3045eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3046eef6a7d5SAlan Stern * 3047eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3048eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3049eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3050eef6a7d5SAlan Stern * 3051eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3052eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3053eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3054eef6a7d5SAlan Stern * cancel_work_sync() instead. 3055eef6a7d5SAlan Stern */ 30561da177e4SLinus Torvalds void flush_scheduled_work(void) 30571da177e4SLinus Torvalds { 3058d320c038STejun Heo flush_workqueue(system_wq); 30591da177e4SLinus Torvalds } 3060ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds /** 30631fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30641fa44ecaSJames Bottomley * @fn: the function to execute 30651fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30661fa44ecaSJames Bottomley * be available when the work executes) 30671fa44ecaSJames Bottomley * 30681fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30691fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30701fa44ecaSJames Bottomley * 30711fa44ecaSJames Bottomley * Returns: 0 - function was executed 30721fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30731fa44ecaSJames Bottomley */ 307465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30751fa44ecaSJames Bottomley { 30761fa44ecaSJames Bottomley if (!in_interrupt()) { 307765f27f38SDavid Howells fn(&ew->work); 30781fa44ecaSJames Bottomley return 0; 30791fa44ecaSJames Bottomley } 30801fa44ecaSJames Bottomley 308165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30821fa44ecaSJames Bottomley schedule_work(&ew->work); 30831fa44ecaSJames Bottomley 30841fa44ecaSJames Bottomley return 1; 30851fa44ecaSJames Bottomley } 30861fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30871fa44ecaSJames Bottomley 30881da177e4SLinus Torvalds int keventd_up(void) 30891da177e4SLinus Torvalds { 3090d320c038STejun Heo return system_wq != NULL; 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds 3093112202d9STejun Heo static int alloc_pwqs(struct workqueue_struct *wq) 30941da177e4SLinus Torvalds { 30953af24433SOleg Nesterov /* 3096112202d9STejun Heo * pwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30970f900049STejun Heo * Make sure that the alignment isn't lower than that of 30980f900049STejun Heo * unsigned long long. 30993af24433SOleg Nesterov */ 3100112202d9STejun Heo const size_t size = sizeof(struct pool_workqueue); 31010f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 31020f900049STejun Heo __alignof__(unsigned long long)); 31033af24433SOleg Nesterov 3104e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3105112202d9STejun Heo wq->pool_wq.pcpu = __alloc_percpu(size, align); 3106931ac77eSTejun Heo else { 31070f900049STejun Heo void *ptr; 3108e1d8aa9fSFrederic Weisbecker 31090f900049STejun Heo /* 3110112202d9STejun Heo * Allocate enough room to align pwq and put an extra 3111f3421797STejun Heo * pointer at the end pointing back to the originally 3112f3421797STejun Heo * allocated pointer which will be used for free. 31130f900049STejun Heo */ 3114bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3115bdbc5dd7STejun Heo if (ptr) { 3116112202d9STejun Heo wq->pool_wq.single = PTR_ALIGN(ptr, align); 3117112202d9STejun Heo *(void **)(wq->pool_wq.single + 1) = ptr; 3118bdbc5dd7STejun Heo } 31193af24433SOleg Nesterov } 31203af24433SOleg Nesterov 31210415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3122112202d9STejun Heo BUG_ON(!IS_ALIGNED(wq->pool_wq.v, align)); 3123112202d9STejun Heo return wq->pool_wq.v ? 0 : -ENOMEM; 31240f900049STejun Heo } 31250f900049STejun Heo 3126112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq) 312706ba38a9SOleg Nesterov { 3128e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3129112202d9STejun Heo free_percpu(wq->pool_wq.pcpu); 3130112202d9STejun Heo else if (wq->pool_wq.single) { 3131112202d9STejun Heo /* the pointer to free is stored right after the pwq */ 3132112202d9STejun Heo kfree(*(void **)(wq->pool_wq.single + 1)); 313306ba38a9SOleg Nesterov } 313406ba38a9SOleg Nesterov } 313506ba38a9SOleg Nesterov 3136f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3137f3421797STejun Heo const char *name) 3138b71ab8c2STejun Heo { 3139f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3140f3421797STejun Heo 3141f3421797STejun Heo if (max_active < 1 || max_active > lim) 3142044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3143f3421797STejun Heo max_active, name, 1, lim); 3144b71ab8c2STejun Heo 3145f3421797STejun Heo return clamp_val(max_active, 1, lim); 3146b71ab8c2STejun Heo } 3147b71ab8c2STejun Heo 3148b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 314997e37d7bSTejun Heo unsigned int flags, 31501e19ffc6STejun Heo int max_active, 3151eb13ba87SJohannes Berg struct lock_class_key *key, 3152b196be89STejun Heo const char *lock_name, ...) 31533af24433SOleg Nesterov { 3154b196be89STejun Heo va_list args, args1; 31553af24433SOleg Nesterov struct workqueue_struct *wq; 3156c34056a3STejun Heo unsigned int cpu; 3157b196be89STejun Heo size_t namelen; 3158b196be89STejun Heo 3159b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3160b196be89STejun Heo va_start(args, lock_name); 3161b196be89STejun Heo va_copy(args1, args); 3162b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3163b196be89STejun Heo 3164b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3165b196be89STejun Heo if (!wq) 3166b196be89STejun Heo goto err; 3167b196be89STejun Heo 3168b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3169b196be89STejun Heo va_end(args); 3170b196be89STejun Heo va_end(args1); 31713af24433SOleg Nesterov 3172f3421797STejun Heo /* 31736370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31746370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31756370a6adSTejun Heo */ 31766370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31776370a6adSTejun Heo flags |= WQ_RESCUER; 31786370a6adSTejun Heo 3179d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3180b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31813af24433SOleg Nesterov 3182b196be89STejun Heo /* init wq */ 318397e37d7bSTejun Heo wq->flags = flags; 3184a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 318573f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 3186112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 318773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 318873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31893af24433SOleg Nesterov 3190eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3191cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31923af24433SOleg Nesterov 3193112202d9STejun Heo if (alloc_pwqs(wq) < 0) 3194bdbc5dd7STejun Heo goto err; 3195bdbc5dd7STejun Heo 3196112202d9STejun Heo for_each_pwq_cpu(cpu, wq) { 3197112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 31981537663fSTejun Heo 3199112202d9STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3200112202d9STejun Heo pwq->pool = get_std_worker_pool(cpu, flags & WQ_HIGHPRI); 3201112202d9STejun Heo pwq->wq = wq; 3202112202d9STejun Heo pwq->flush_color = -1; 3203112202d9STejun Heo pwq->max_active = max_active; 3204112202d9STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 3205e22bee78STejun Heo } 32061537663fSTejun Heo 3207e22bee78STejun Heo if (flags & WQ_RESCUER) { 3208e22bee78STejun Heo struct worker *rescuer; 3209e22bee78STejun Heo 3210f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3211e22bee78STejun Heo goto err; 3212e22bee78STejun Heo 3213e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3214e22bee78STejun Heo if (!rescuer) 3215e22bee78STejun Heo goto err; 3216e22bee78STejun Heo 3217111c225aSTejun Heo rescuer->rescue_wq = wq; 3218111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3219b196be89STejun Heo wq->name); 3220e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3221e22bee78STejun Heo goto err; 3222e22bee78STejun Heo 3223e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3224e22bee78STejun Heo wake_up_process(rescuer->task); 32253af24433SOleg Nesterov } 32261537663fSTejun Heo 32273af24433SOleg Nesterov /* 3228a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3229a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3230a0a1a5fdSTejun Heo * workqueue to workqueues list. 32313af24433SOleg Nesterov */ 32323af24433SOleg Nesterov spin_lock(&workqueue_lock); 3233a0a1a5fdSTejun Heo 323458a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3235112202d9STejun Heo for_each_pwq_cpu(cpu, wq) 3236112202d9STejun Heo get_pwq(cpu, wq)->max_active = 0; 3237a0a1a5fdSTejun Heo 32383af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3239a0a1a5fdSTejun Heo 32403af24433SOleg Nesterov spin_unlock(&workqueue_lock); 32413af24433SOleg Nesterov 32423af24433SOleg Nesterov return wq; 32434690c4abSTejun Heo err: 32444690c4abSTejun Heo if (wq) { 3245112202d9STejun Heo free_pwqs(wq); 3246f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3247e22bee78STejun Heo kfree(wq->rescuer); 32484690c4abSTejun Heo kfree(wq); 32493af24433SOleg Nesterov } 32504690c4abSTejun Heo return NULL; 32511da177e4SLinus Torvalds } 3252d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32531da177e4SLinus Torvalds 32543af24433SOleg Nesterov /** 32553af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32563af24433SOleg Nesterov * @wq: target workqueue 32573af24433SOleg Nesterov * 32583af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32593af24433SOleg Nesterov */ 32603af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32613af24433SOleg Nesterov { 3262c8e55f36STejun Heo unsigned int cpu; 32633af24433SOleg Nesterov 32649c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32659c5a2ba7STejun Heo drain_workqueue(wq); 3266c8efcc25STejun Heo 3267a0a1a5fdSTejun Heo /* 3268a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3269a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3270a0a1a5fdSTejun Heo */ 327195402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32723af24433SOleg Nesterov list_del(&wq->list); 327395402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32743af24433SOleg Nesterov 3275e22bee78STejun Heo /* sanity check */ 3276112202d9STejun Heo for_each_pwq_cpu(cpu, wq) { 3277112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 327873f53c4aSTejun Heo int i; 32793af24433SOleg Nesterov 328073f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 3281112202d9STejun Heo BUG_ON(pwq->nr_in_flight[i]); 3282112202d9STejun Heo BUG_ON(pwq->nr_active); 3283112202d9STejun Heo BUG_ON(!list_empty(&pwq->delayed_works)); 328473f53c4aSTejun Heo } 32851537663fSTejun Heo 3286e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3287e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3288f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32898d9df9f0SXiaotian Feng kfree(wq->rescuer); 3290e22bee78STejun Heo } 3291e22bee78STejun Heo 3292112202d9STejun Heo free_pwqs(wq); 32933af24433SOleg Nesterov kfree(wq); 32943af24433SOleg Nesterov } 32953af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32963af24433SOleg Nesterov 3297dcd989cbSTejun Heo /** 3298112202d9STejun Heo * pwq_set_max_active - adjust max_active of a pwq 3299112202d9STejun Heo * @pwq: target pool_workqueue 33009f4bd4cdSLai Jiangshan * @max_active: new max_active value. 33019f4bd4cdSLai Jiangshan * 3302112202d9STejun Heo * Set @pwq->max_active to @max_active and activate delayed works if 33039f4bd4cdSLai Jiangshan * increased. 33049f4bd4cdSLai Jiangshan * 33059f4bd4cdSLai Jiangshan * CONTEXT: 3306d565ed63STejun Heo * spin_lock_irq(pool->lock). 33079f4bd4cdSLai Jiangshan */ 3308112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 33099f4bd4cdSLai Jiangshan { 3310112202d9STejun Heo pwq->max_active = max_active; 33119f4bd4cdSLai Jiangshan 3312112202d9STejun Heo while (!list_empty(&pwq->delayed_works) && 3313112202d9STejun Heo pwq->nr_active < pwq->max_active) 3314112202d9STejun Heo pwq_activate_first_delayed(pwq); 33159f4bd4cdSLai Jiangshan } 33169f4bd4cdSLai Jiangshan 33179f4bd4cdSLai Jiangshan /** 3318dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3319dcd989cbSTejun Heo * @wq: target workqueue 3320dcd989cbSTejun Heo * @max_active: new max_active value. 3321dcd989cbSTejun Heo * 3322dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3323dcd989cbSTejun Heo * 3324dcd989cbSTejun Heo * CONTEXT: 3325dcd989cbSTejun Heo * Don't call from IRQ context. 3326dcd989cbSTejun Heo */ 3327dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3328dcd989cbSTejun Heo { 3329dcd989cbSTejun Heo unsigned int cpu; 3330dcd989cbSTejun Heo 3331f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3332dcd989cbSTejun Heo 3333dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3334dcd989cbSTejun Heo 3335dcd989cbSTejun Heo wq->saved_max_active = max_active; 3336dcd989cbSTejun Heo 3337112202d9STejun Heo for_each_pwq_cpu(cpu, wq) { 3338112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3339112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3340dcd989cbSTejun Heo 3341d565ed63STejun Heo spin_lock_irq(&pool->lock); 3342dcd989cbSTejun Heo 334358a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 334435b6bb63STejun Heo !(pool->flags & POOL_FREEZING)) 3345112202d9STejun Heo pwq_set_max_active(pwq, max_active); 3346dcd989cbSTejun Heo 3347d565ed63STejun Heo spin_unlock_irq(&pool->lock); 3348dcd989cbSTejun Heo } 3349dcd989cbSTejun Heo 3350dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3351dcd989cbSTejun Heo } 3352dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3353dcd989cbSTejun Heo 3354dcd989cbSTejun Heo /** 3355dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3356dcd989cbSTejun Heo * @cpu: CPU in question 3357dcd989cbSTejun Heo * @wq: target workqueue 3358dcd989cbSTejun Heo * 3359dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3360dcd989cbSTejun Heo * no synchronization around this function and the test result is 3361dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3362dcd989cbSTejun Heo * 3363dcd989cbSTejun Heo * RETURNS: 3364dcd989cbSTejun Heo * %true if congested, %false otherwise. 3365dcd989cbSTejun Heo */ 3366dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3367dcd989cbSTejun Heo { 3368112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3369dcd989cbSTejun Heo 3370112202d9STejun Heo return !list_empty(&pwq->delayed_works); 3371dcd989cbSTejun Heo } 3372dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3373dcd989cbSTejun Heo 3374dcd989cbSTejun Heo /** 3375dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3376dcd989cbSTejun Heo * @work: the work to be tested 3377dcd989cbSTejun Heo * 3378dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3379dcd989cbSTejun Heo * synchronization around this function and the test result is 3380dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3381dcd989cbSTejun Heo * 3382dcd989cbSTejun Heo * RETURNS: 3383dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3384dcd989cbSTejun Heo */ 3385dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3386dcd989cbSTejun Heo { 3387c9e7cf27STejun Heo struct worker_pool *pool = get_work_pool(work); 3388dcd989cbSTejun Heo unsigned long flags; 3389dcd989cbSTejun Heo unsigned int ret = 0; 3390dcd989cbSTejun Heo 3391dcd989cbSTejun Heo if (work_pending(work)) 3392dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3393038366c5SLai Jiangshan 3394038366c5SLai Jiangshan if (pool) { 3395038366c5SLai Jiangshan spin_lock_irqsave(&pool->lock, flags); 3396c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 3397dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3398d565ed63STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 3399038366c5SLai Jiangshan } 3400dcd989cbSTejun Heo 3401dcd989cbSTejun Heo return ret; 3402dcd989cbSTejun Heo } 3403dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3404dcd989cbSTejun Heo 3405db7bccf4STejun Heo /* 3406db7bccf4STejun Heo * CPU hotplug. 3407db7bccf4STejun Heo * 3408e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3409112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 3410706026c2STejun Heo * pool which make migrating pending and scheduled works very 3411e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 341294cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 3413e22bee78STejun Heo * blocked draining impractical. 3414e22bee78STejun Heo * 341524647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 3416628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3417628c78e7STejun Heo * cpu comes back online. 3418db7bccf4STejun Heo */ 3419db7bccf4STejun Heo 3420706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 3421db7bccf4STejun Heo { 342238db41d9STejun Heo int cpu = smp_processor_id(); 34234ce62e9eSTejun Heo struct worker_pool *pool; 3424db7bccf4STejun Heo struct worker *worker; 3425db7bccf4STejun Heo int i; 3426db7bccf4STejun Heo 342738db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 342838db41d9STejun Heo BUG_ON(cpu != smp_processor_id()); 3429db7bccf4STejun Heo 343094cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 343194cf58bbSTejun Heo spin_lock_irq(&pool->lock); 3432e22bee78STejun Heo 3433f2d5a0eeSTejun Heo /* 343494cf58bbSTejun Heo * We've claimed all manager positions. Make all workers 343594cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 343694cf58bbSTejun Heo * except for the ones which are still executing works from 343794cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 343894cf58bbSTejun Heo * this, they may become diasporas. 3439f2d5a0eeSTejun Heo */ 34404ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3441403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3442db7bccf4STejun Heo 3443b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) 3444403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3445db7bccf4STejun Heo 344624647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 3447f2d5a0eeSTejun Heo 344894cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 344994cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 345094cf58bbSTejun Heo } 3451e22bee78STejun Heo 3452e22bee78STejun Heo /* 3453628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3454628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3455628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3456628c78e7STejun Heo */ 3457628c78e7STejun Heo schedule(); 3458628c78e7STejun Heo 3459628c78e7STejun Heo /* 3460628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3461628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 346238db41d9STejun Heo * are always true as long as the worklist is not empty. Pools on 346338db41d9STejun Heo * @cpu now behave as unbound (in terms of concurrency management) 346438db41d9STejun Heo * pools which are served by workers tied to the CPU. 3465628c78e7STejun Heo * 3466628c78e7STejun Heo * On return from this function, the current worker would trigger 3467628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3468628c78e7STejun Heo * didn't already. 3469e22bee78STejun Heo */ 347038db41d9STejun Heo for_each_std_worker_pool(pool, cpu) 3471e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 3472db7bccf4STejun Heo } 3473db7bccf4STejun Heo 34748db25e78STejun Heo /* 34758db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34768db25e78STejun Heo * This will be registered high priority CPU notifier. 34778db25e78STejun Heo */ 34789fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34791da177e4SLinus Torvalds unsigned long action, 34801da177e4SLinus Torvalds void *hcpu) 34811da177e4SLinus Torvalds { 34823af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 34834ce62e9eSTejun Heo struct worker_pool *pool; 34841da177e4SLinus Torvalds 34858db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34863af24433SOleg Nesterov case CPU_UP_PREPARE: 348738db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 34883ce63377STejun Heo struct worker *worker; 34893ce63377STejun Heo 34903ce63377STejun Heo if (pool->nr_workers) 34913ce63377STejun Heo continue; 34923ce63377STejun Heo 34933ce63377STejun Heo worker = create_worker(pool); 34943ce63377STejun Heo if (!worker) 34953ce63377STejun Heo return NOTIFY_BAD; 34963ce63377STejun Heo 3497d565ed63STejun Heo spin_lock_irq(&pool->lock); 34983ce63377STejun Heo start_worker(worker); 3499d565ed63STejun Heo spin_unlock_irq(&pool->lock); 35003af24433SOleg Nesterov } 35011da177e4SLinus Torvalds break; 35021da177e4SLinus Torvalds 350365758202STejun Heo case CPU_DOWN_FAILED: 350465758202STejun Heo case CPU_ONLINE: 350538db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 350694cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 350794cf58bbSTejun Heo spin_lock_irq(&pool->lock); 350894cf58bbSTejun Heo 350924647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 351094cf58bbSTejun Heo rebind_workers(pool); 351194cf58bbSTejun Heo 351294cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 351394cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 351494cf58bbSTejun Heo } 35158db25e78STejun Heo break; 351665758202STejun Heo } 351765758202STejun Heo return NOTIFY_OK; 351865758202STejun Heo } 351965758202STejun Heo 352065758202STejun Heo /* 352165758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 352265758202STejun Heo * This will be registered as low priority CPU notifier. 352365758202STejun Heo */ 35249fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, 352565758202STejun Heo unsigned long action, 352665758202STejun Heo void *hcpu) 352765758202STejun Heo { 35288db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35298db25e78STejun Heo struct work_struct unbind_work; 35308db25e78STejun Heo 353165758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 353265758202STejun Heo case CPU_DOWN_PREPARE: 35338db25e78STejun Heo /* unbinding should happen on the local CPU */ 3534706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 35357635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 35368db25e78STejun Heo flush_work(&unbind_work); 35378db25e78STejun Heo break; 353865758202STejun Heo } 353965758202STejun Heo return NOTIFY_OK; 354065758202STejun Heo } 354165758202STejun Heo 35422d3854a3SRusty Russell #ifdef CONFIG_SMP 35438ccad40dSRusty Russell 35442d3854a3SRusty Russell struct work_for_cpu { 3545ed48ece2STejun Heo struct work_struct work; 35462d3854a3SRusty Russell long (*fn)(void *); 35472d3854a3SRusty Russell void *arg; 35482d3854a3SRusty Russell long ret; 35492d3854a3SRusty Russell }; 35502d3854a3SRusty Russell 3551ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 35522d3854a3SRusty Russell { 3553ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 3554ed48ece2STejun Heo 35552d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35562d3854a3SRusty Russell } 35572d3854a3SRusty Russell 35582d3854a3SRusty Russell /** 35592d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35602d3854a3SRusty Russell * @cpu: the cpu to run on 35612d3854a3SRusty Russell * @fn: the function to run 35622d3854a3SRusty Russell * @arg: the function arg 35632d3854a3SRusty Russell * 356431ad9081SRusty Russell * This will return the value @fn returns. 356531ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35666b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35672d3854a3SRusty Russell */ 35682d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35692d3854a3SRusty Russell { 3570ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 35712d3854a3SRusty Russell 3572ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 3573ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 3574ed48ece2STejun Heo flush_work(&wfc.work); 35752d3854a3SRusty Russell return wfc.ret; 35762d3854a3SRusty Russell } 35772d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35782d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35792d3854a3SRusty Russell 3580a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3581e7577c50SRusty Russell 3582a0a1a5fdSTejun Heo /** 3583a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3584a0a1a5fdSTejun Heo * 358558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 358658a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 3587706026c2STejun Heo * pool->worklist. 3588a0a1a5fdSTejun Heo * 3589a0a1a5fdSTejun Heo * CONTEXT: 3590d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3591a0a1a5fdSTejun Heo */ 3592a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3593a0a1a5fdSTejun Heo { 3594a0a1a5fdSTejun Heo unsigned int cpu; 3595a0a1a5fdSTejun Heo 3596a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3597a0a1a5fdSTejun Heo 3598a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3599a0a1a5fdSTejun Heo workqueue_freezing = true; 3600a0a1a5fdSTejun Heo 3601706026c2STejun Heo for_each_wq_cpu(cpu) { 360235b6bb63STejun Heo struct worker_pool *pool; 3603bdbc5dd7STejun Heo struct workqueue_struct *wq; 36048b03ae3cSTejun Heo 360538db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 3606a1056305STejun Heo spin_lock_irq(&pool->lock); 3607d565ed63STejun Heo 360835b6bb63STejun Heo WARN_ON_ONCE(pool->flags & POOL_FREEZING); 360935b6bb63STejun Heo pool->flags |= POOL_FREEZING; 3610db7bccf4STejun Heo 3611a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3612112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3613a0a1a5fdSTejun Heo 3614112202d9STejun Heo if (pwq && pwq->pool == pool && 3615a1056305STejun Heo (wq->flags & WQ_FREEZABLE)) 3616112202d9STejun Heo pwq->max_active = 0; 36171da177e4SLinus Torvalds } 36188b03ae3cSTejun Heo 3619a1056305STejun Heo spin_unlock_irq(&pool->lock); 3620a1056305STejun Heo } 3621a0a1a5fdSTejun Heo } 3622a0a1a5fdSTejun Heo 3623a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3624a0a1a5fdSTejun Heo } 3625a0a1a5fdSTejun Heo 3626a0a1a5fdSTejun Heo /** 362758a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3628a0a1a5fdSTejun Heo * 3629a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3630a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3631a0a1a5fdSTejun Heo * 3632a0a1a5fdSTejun Heo * CONTEXT: 3633a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3634a0a1a5fdSTejun Heo * 3635a0a1a5fdSTejun Heo * RETURNS: 363658a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 363758a69cb4STejun Heo * is complete. 3638a0a1a5fdSTejun Heo */ 3639a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3640a0a1a5fdSTejun Heo { 3641a0a1a5fdSTejun Heo unsigned int cpu; 3642a0a1a5fdSTejun Heo bool busy = false; 3643a0a1a5fdSTejun Heo 3644a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3645a0a1a5fdSTejun Heo 3646a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3647a0a1a5fdSTejun Heo 3648706026c2STejun Heo for_each_wq_cpu(cpu) { 3649bdbc5dd7STejun Heo struct workqueue_struct *wq; 3650a0a1a5fdSTejun Heo /* 3651a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3652a0a1a5fdSTejun Heo * to peek without lock. 3653a0a1a5fdSTejun Heo */ 3654a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3655112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3656a0a1a5fdSTejun Heo 3657112202d9STejun Heo if (!pwq || !(wq->flags & WQ_FREEZABLE)) 3658a0a1a5fdSTejun Heo continue; 3659a0a1a5fdSTejun Heo 3660112202d9STejun Heo BUG_ON(pwq->nr_active < 0); 3661112202d9STejun Heo if (pwq->nr_active) { 3662a0a1a5fdSTejun Heo busy = true; 3663a0a1a5fdSTejun Heo goto out_unlock; 3664a0a1a5fdSTejun Heo } 3665a0a1a5fdSTejun Heo } 3666a0a1a5fdSTejun Heo } 3667a0a1a5fdSTejun Heo out_unlock: 3668a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3669a0a1a5fdSTejun Heo return busy; 3670a0a1a5fdSTejun Heo } 3671a0a1a5fdSTejun Heo 3672a0a1a5fdSTejun Heo /** 3673a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3674a0a1a5fdSTejun Heo * 3675a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 3676706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 3677a0a1a5fdSTejun Heo * 3678a0a1a5fdSTejun Heo * CONTEXT: 3679d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3680a0a1a5fdSTejun Heo */ 3681a0a1a5fdSTejun Heo void thaw_workqueues(void) 3682a0a1a5fdSTejun Heo { 3683a0a1a5fdSTejun Heo unsigned int cpu; 3684a0a1a5fdSTejun Heo 3685a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3686a0a1a5fdSTejun Heo 3687a0a1a5fdSTejun Heo if (!workqueue_freezing) 3688a0a1a5fdSTejun Heo goto out_unlock; 3689a0a1a5fdSTejun Heo 3690706026c2STejun Heo for_each_wq_cpu(cpu) { 36914ce62e9eSTejun Heo struct worker_pool *pool; 3692bdbc5dd7STejun Heo struct workqueue_struct *wq; 36938b03ae3cSTejun Heo 369438db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 3695a1056305STejun Heo spin_lock_irq(&pool->lock); 3696d565ed63STejun Heo 369735b6bb63STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 369835b6bb63STejun Heo pool->flags &= ~POOL_FREEZING; 3699db7bccf4STejun Heo 3700a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3701112202d9STejun Heo struct pool_workqueue *pwq = get_pwq(cpu, wq); 3702a0a1a5fdSTejun Heo 3703112202d9STejun Heo if (!pwq || pwq->pool != pool || 3704a1056305STejun Heo !(wq->flags & WQ_FREEZABLE)) 3705a0a1a5fdSTejun Heo continue; 3706a0a1a5fdSTejun Heo 3707a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3708112202d9STejun Heo pwq_set_max_active(pwq, wq->saved_max_active); 3709a0a1a5fdSTejun Heo } 37108b03ae3cSTejun Heo 37114ce62e9eSTejun Heo wake_up_worker(pool); 3712a1056305STejun Heo 3713a1056305STejun Heo spin_unlock_irq(&pool->lock); 3714d565ed63STejun Heo } 3715a0a1a5fdSTejun Heo } 3716a0a1a5fdSTejun Heo 3717a0a1a5fdSTejun Heo workqueue_freezing = false; 3718a0a1a5fdSTejun Heo out_unlock: 3719a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3720a0a1a5fdSTejun Heo } 3721a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3722a0a1a5fdSTejun Heo 37236ee0578bSSuresh Siddha static int __init init_workqueues(void) 37241da177e4SLinus Torvalds { 3725c34056a3STejun Heo unsigned int cpu; 3726c34056a3STejun Heo 37277c3eed5cSTejun Heo /* make sure we have enough bits for OFFQ pool ID */ 37287c3eed5cSTejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 37296be19588SLai Jiangshan WORK_CPU_END * NR_STD_WORKER_POOLS); 3730b5490077STejun Heo 373165758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 3732a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37338b03ae3cSTejun Heo 3734706026c2STejun Heo /* initialize CPU pools */ 3735706026c2STejun Heo for_each_wq_cpu(cpu) { 37364ce62e9eSTejun Heo struct worker_pool *pool; 37378b03ae3cSTejun Heo 373838db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 3739d565ed63STejun Heo spin_lock_init(&pool->lock); 3740ec22ca5eSTejun Heo pool->cpu = cpu; 374124647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 37424ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37434ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3744c9e7cf27STejun Heo hash_init(pool->busy_hash); 3745e22bee78STejun Heo 37464ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37474ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37484ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3749e22bee78STejun Heo 3750706026c2STejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 37514ce62e9eSTejun Heo (unsigned long)pool); 37524ce62e9eSTejun Heo 3753b2eb83d1SLai Jiangshan mutex_init(&pool->assoc_mutex); 37544ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37559daf9e67STejun Heo 37569daf9e67STejun Heo /* alloc pool ID */ 37579daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 37584ce62e9eSTejun Heo } 37598b03ae3cSTejun Heo } 37608b03ae3cSTejun Heo 3761e22bee78STejun Heo /* create the initial worker */ 3762706026c2STejun Heo for_each_online_wq_cpu(cpu) { 37634ce62e9eSTejun Heo struct worker_pool *pool; 3764e22bee78STejun Heo 376538db41d9STejun Heo for_each_std_worker_pool(pool, cpu) { 37664ce62e9eSTejun Heo struct worker *worker; 37674ce62e9eSTejun Heo 376824647570STejun Heo if (cpu != WORK_CPU_UNBOUND) 376924647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 377024647570STejun Heo 3771bc2ae0f5STejun Heo worker = create_worker(pool); 3772e22bee78STejun Heo BUG_ON(!worker); 3773d565ed63STejun Heo spin_lock_irq(&pool->lock); 3774e22bee78STejun Heo start_worker(worker); 3775d565ed63STejun Heo spin_unlock_irq(&pool->lock); 3776e22bee78STejun Heo } 37774ce62e9eSTejun Heo } 3778e22bee78STejun Heo 3779d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 37801aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 3781d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3782f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3783f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 378424d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 378524d51addSTejun Heo WQ_FREEZABLE, 0); 37861aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 3787ae930e0fSTejun Heo !system_unbound_wq || !system_freezable_wq); 37886ee0578bSSuresh Siddha return 0; 37891da177e4SLinus Torvalds } 37906ee0578bSSuresh Siddha early_initcall(init_workqueues); 3791