11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19c54fce6eSTejun Heo * automatically managed. There is one worker pool for each CPU and 20c54fce6eSTejun Heo * one extra for works which are better served by workers which are 21c54fce6eSTejun Heo * not bound to any specific CPU. 22c54fce6eSTejun Heo * 23c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 269984de1aSPaul Gortmaker #include <linux/export.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds #include <linux/signal.h> 311da177e4SLinus Torvalds #include <linux/completion.h> 321da177e4SLinus Torvalds #include <linux/workqueue.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/cpu.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/kthread.h> 371fa44ecaSJames Bottomley #include <linux/hardirq.h> 3846934023SChristoph Lameter #include <linux/mempolicy.h> 39341a5958SRafael J. Wysocki #include <linux/freezer.h> 40d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 44e22bee78STejun Heo 45e22bee78STejun Heo #include "workqueue_sched.h" 461da177e4SLinus Torvalds 47c8e55f36STejun Heo enum { 48bc2ae0f5STejun Heo /* 49bc2ae0f5STejun Heo * global_cwq flags 50bc2ae0f5STejun Heo * 51bc2ae0f5STejun Heo * A bound gcwq is either associated or disassociated with its CPU. 52bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 53bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 54bc2ae0f5STejun Heo * is in effect. 55bc2ae0f5STejun Heo * 56bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 57bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 58bc2ae0f5STejun Heo * be executing on any CPU. The gcwq behaves as an unbound one. 59bc2ae0f5STejun Heo * 60bc2ae0f5STejun Heo * Note that DISASSOCIATED can be flipped only while holding 61bc2ae0f5STejun Heo * managership of all pools on the gcwq to avoid changing binding 62bc2ae0f5STejun Heo * state while create_worker() is in progress. 63bc2ae0f5STejun Heo */ 6411ebea50STejun Heo GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */ 6511ebea50STejun Heo GCWQ_FREEZING = 1 << 1, /* freeze in progress */ 6611ebea50STejun Heo 6711ebea50STejun Heo /* pool flags */ 6811ebea50STejun Heo POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ 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 */ 75e22bee78STejun Heo WORKER_REBIND = 1 << 5, /* mom is home, come back */ 76fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 77f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 78e22bee78STejun Heo 79403c821dSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND | 80403c821dSTejun Heo WORKER_CPU_INTENSIVE, 81db7bccf4STejun Heo 823270476aSTejun Heo NR_WORKER_POOLS = 2, /* # worker pools per gcwq */ 834ce62e9eSTejun Heo 84c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 85c8e55f36STejun Heo BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER, 86c8e55f36STejun Heo BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1, 87db7bccf4STejun Heo 88e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 89e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 90e22bee78STejun Heo 913233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 923233cdbdSTejun Heo /* call for help after 10ms 933233cdbdSTejun Heo (min two ticks) */ 94e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 95e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 98e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 99e22bee78STejun Heo * all cpus. Give -20. 100e22bee78STejun Heo */ 101e22bee78STejun Heo RESCUER_NICE_LEVEL = -20, 1023270476aSTejun Heo HIGHPRI_NICE_LEVEL = -20, 103c8e55f36STejun Heo }; 104c8e55f36STejun Heo 1051da177e4SLinus Torvalds /* 1064690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1074690c4abSTejun Heo * 108e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 109e41e704bSTejun Heo * everyone else. 1104690c4abSTejun Heo * 111e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 112e22bee78STejun Heo * only be modified and accessed from the local cpu. 113e22bee78STejun Heo * 1148b03ae3cSTejun Heo * L: gcwq->lock protected. Access with gcwq->lock held. 1154690c4abSTejun Heo * 116e22bee78STejun Heo * X: During normal operation, modification requires gcwq->lock and 117e22bee78STejun Heo * should be done only from local cpu. Either disabling preemption 118e22bee78STejun Heo * on local cpu or grabbing gcwq->lock is enough for read access. 119f3421797STejun Heo * If GCWQ_DISASSOCIATED is set, it's identical to L. 120e22bee78STejun Heo * 12173f53c4aSTejun Heo * F: wq->flush_mutex protected. 12273f53c4aSTejun Heo * 1234690c4abSTejun Heo * W: workqueue_lock protected. 1244690c4abSTejun Heo */ 1254690c4abSTejun Heo 1268b03ae3cSTejun Heo struct global_cwq; 127bd7bdd43STejun Heo struct worker_pool; 12825511a47STejun Heo struct idle_rebind; 129c34056a3STejun Heo 130e22bee78STejun Heo /* 131e22bee78STejun Heo * The poor guys doing the actual heavy lifting. All on-duty workers 132e22bee78STejun Heo * are either serving the manager role, on idle list or on busy hash. 133e22bee78STejun Heo */ 134c34056a3STejun Heo struct worker { 135c8e55f36STejun Heo /* on idle list while idle, on busy hash table while busy */ 136c8e55f36STejun Heo union { 137c8e55f36STejun Heo struct list_head entry; /* L: while idle */ 138c8e55f36STejun Heo struct hlist_node hentry; /* L: while busy */ 139c8e55f36STejun Heo }; 140c8e55f36STejun Heo 141c34056a3STejun Heo struct work_struct *current_work; /* L: work being processed */ 1428cca0eeaSTejun Heo struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */ 143affee4b2STejun Heo struct list_head scheduled; /* L: scheduled works */ 144c34056a3STejun Heo struct task_struct *task; /* I: worker task */ 145bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 146e22bee78STejun Heo /* 64 bytes boundary on 64bit, 32 on 32bit */ 147e22bee78STejun Heo unsigned long last_active; /* L: last active timestamp */ 148e22bee78STejun Heo unsigned int flags; /* X: flags */ 149c34056a3STejun Heo int id; /* I: worker id */ 15025511a47STejun Heo 15125511a47STejun Heo /* for rebinding worker to CPU */ 15225511a47STejun Heo struct idle_rebind *idle_rebind; /* L: for idle worker */ 15325511a47STejun Heo struct work_struct rebind_work; /* L: for busy worker */ 154c34056a3STejun Heo }; 155c34056a3STejun Heo 156bd7bdd43STejun Heo struct worker_pool { 157bd7bdd43STejun Heo struct global_cwq *gcwq; /* I: the owning gcwq */ 15811ebea50STejun Heo unsigned int flags; /* X: flags */ 159bd7bdd43STejun Heo 160bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 161bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 162bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 163bd7bdd43STejun Heo 164bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 165bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 166bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 167bd7bdd43STejun Heo 16860373152STejun Heo struct mutex manager_mutex; /* mutex manager should hold */ 169bd7bdd43STejun Heo struct ida worker_ida; /* L: for worker IDs */ 170bd7bdd43STejun Heo }; 171bd7bdd43STejun Heo 1724690c4abSTejun Heo /* 173e22bee78STejun Heo * Global per-cpu workqueue. There's one and only one for each cpu 174e22bee78STejun Heo * and all works are queued and processed here regardless of their 175e22bee78STejun Heo * target workqueues. 1768b03ae3cSTejun Heo */ 1778b03ae3cSTejun Heo struct global_cwq { 1788b03ae3cSTejun Heo spinlock_t lock; /* the gcwq lock */ 1798b03ae3cSTejun Heo unsigned int cpu; /* I: the associated cpu */ 180db7bccf4STejun Heo unsigned int flags; /* L: GCWQ_* flags */ 181c8e55f36STejun Heo 182bd7bdd43STejun Heo /* workers are chained either in busy_hash or pool idle_list */ 183c8e55f36STejun Heo struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE]; 184c8e55f36STejun Heo /* L: hash of busy workers */ 185c8e55f36STejun Heo 1863270476aSTejun Heo struct worker_pool pools[2]; /* normal and highpri pools */ 187db7bccf4STejun Heo 18825511a47STejun Heo wait_queue_head_t rebind_hold; /* rebind hold wait */ 1898b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1908b03ae3cSTejun Heo 1918b03ae3cSTejun Heo /* 192502ca9d8STejun Heo * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of 1930f900049STejun Heo * work_struct->data are used for flags and thus cwqs need to be 1940f900049STejun Heo * aligned at two's power of the number of flag bits. 1951da177e4SLinus Torvalds */ 1961da177e4SLinus Torvalds struct cpu_workqueue_struct { 197bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1984690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 19973f53c4aSTejun Heo int work_color; /* L: current color */ 20073f53c4aSTejun Heo int flush_color; /* L: flushing color */ 20173f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20273f53c4aSTejun Heo /* L: nr of in_flight works */ 2031e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 204a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2051e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2060f900049STejun Heo }; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds /* 20973f53c4aSTejun Heo * Structure used to wait for workqueue flush. 21073f53c4aSTejun Heo */ 21173f53c4aSTejun Heo struct wq_flusher { 21273f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 21373f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 21473f53c4aSTejun Heo struct completion done; /* flush completion */ 21573f53c4aSTejun Heo }; 2161da177e4SLinus Torvalds 21773f53c4aSTejun Heo /* 218f2e005aaSTejun Heo * All cpumasks are assumed to be always set on UP and thus can't be 219f2e005aaSTejun Heo * used to determine whether there's something to be done. 220f2e005aaSTejun Heo */ 221f2e005aaSTejun Heo #ifdef CONFIG_SMP 222f2e005aaSTejun Heo typedef cpumask_var_t mayday_mask_t; 223f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) \ 224f2e005aaSTejun Heo cpumask_test_and_set_cpu((cpu), (mask)) 225f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) 226f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) 2279c37547aSTejun Heo #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp)) 228f2e005aaSTejun Heo #define free_mayday_mask(mask) free_cpumask_var((mask)) 229f2e005aaSTejun Heo #else 230f2e005aaSTejun Heo typedef unsigned long mayday_mask_t; 231f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask)) 232f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask)) 233f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask)) 234f2e005aaSTejun Heo #define alloc_mayday_mask(maskp, gfp) true 235f2e005aaSTejun Heo #define free_mayday_mask(mask) do { } while (0) 236f2e005aaSTejun Heo #endif 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds /* 2391da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 2401da177e4SLinus Torvalds * per-CPU workqueues: 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds struct workqueue_struct { 2439c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 244bdbc5dd7STejun Heo union { 245bdbc5dd7STejun Heo struct cpu_workqueue_struct __percpu *pcpu; 246bdbc5dd7STejun Heo struct cpu_workqueue_struct *single; 247bdbc5dd7STejun Heo unsigned long v; 248bdbc5dd7STejun Heo } cpu_wq; /* I: cwq's */ 2494690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 25073f53c4aSTejun Heo 25173f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 25273f53c4aSTejun Heo int work_color; /* F: current work color */ 25373f53c4aSTejun Heo int flush_color; /* F: current flush color */ 25473f53c4aSTejun Heo atomic_t nr_cwqs_to_flush; /* flush in progress */ 25573f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 25673f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 25773f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 25873f53c4aSTejun Heo 259f2e005aaSTejun Heo mayday_mask_t mayday_mask; /* cpus requesting rescue */ 260e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 261e22bee78STejun Heo 2629c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 263dcd989cbSTejun Heo int saved_max_active; /* W: saved cwq max_active */ 2644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2654e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2664e6045f1SJohannes Berg #endif 267b196be89STejun Heo char name[]; /* I: workqueue name */ 2681da177e4SLinus Torvalds }; 2691da177e4SLinus Torvalds 270d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 271d320c038STejun Heo struct workqueue_struct *system_long_wq __read_mostly; 272d320c038STejun Heo struct workqueue_struct *system_nrt_wq __read_mostly; 273f3421797STejun Heo struct workqueue_struct *system_unbound_wq __read_mostly; 27424d51addSTejun Heo struct workqueue_struct *system_freezable_wq __read_mostly; 27562d3c543SAlan Stern struct workqueue_struct *system_nrt_freezable_wq __read_mostly; 276d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 277d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 278d320c038STejun Heo EXPORT_SYMBOL_GPL(system_nrt_wq); 279f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 28024d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 28162d3c543SAlan Stern EXPORT_SYMBOL_GPL(system_nrt_freezable_wq); 282d320c038STejun Heo 28397bd2347STejun Heo #define CREATE_TRACE_POINTS 28497bd2347STejun Heo #include <trace/events/workqueue.h> 28597bd2347STejun Heo 2864ce62e9eSTejun Heo #define for_each_worker_pool(pool, gcwq) \ 2873270476aSTejun Heo for ((pool) = &(gcwq)->pools[0]; \ 2883270476aSTejun Heo (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++) 2894ce62e9eSTejun Heo 290db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq) \ 291db7bccf4STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ 292db7bccf4STejun Heo hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) 293db7bccf4STejun Heo 294f3421797STejun Heo static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask, 295f3421797STejun Heo unsigned int sw) 296f3421797STejun Heo { 297f3421797STejun Heo if (cpu < nr_cpu_ids) { 298f3421797STejun Heo if (sw & 1) { 299f3421797STejun Heo cpu = cpumask_next(cpu, mask); 300f3421797STejun Heo if (cpu < nr_cpu_ids) 301f3421797STejun Heo return cpu; 302f3421797STejun Heo } 303f3421797STejun Heo if (sw & 2) 304f3421797STejun Heo return WORK_CPU_UNBOUND; 305f3421797STejun Heo } 306f3421797STejun Heo return WORK_CPU_NONE; 307f3421797STejun Heo } 308f3421797STejun Heo 309f3421797STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 310f3421797STejun Heo struct workqueue_struct *wq) 311f3421797STejun Heo { 312f3421797STejun Heo return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2); 313f3421797STejun Heo } 314f3421797STejun Heo 31509884951STejun Heo /* 31609884951STejun Heo * CPU iterators 31709884951STejun Heo * 31809884951STejun Heo * An extra gcwq is defined for an invalid cpu number 31909884951STejun Heo * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 32009884951STejun Heo * specific CPU. The following iterators are similar to 32109884951STejun Heo * for_each_*_cpu() iterators but also considers the unbound gcwq. 32209884951STejun Heo * 32309884951STejun Heo * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND 32409884951STejun Heo * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND 32509884951STejun Heo * for_each_cwq_cpu() : possible CPUs for bound workqueues, 32609884951STejun Heo * WORK_CPU_UNBOUND for unbound workqueues 32709884951STejun Heo */ 328f3421797STejun Heo #define for_each_gcwq_cpu(cpu) \ 329f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \ 330f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 331f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3)) 332f3421797STejun Heo 333f3421797STejun Heo #define for_each_online_gcwq_cpu(cpu) \ 334f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \ 335f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 336f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3)) 337f3421797STejun Heo 338f3421797STejun Heo #define for_each_cwq_cpu(cpu, wq) \ 339f3421797STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \ 340f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 341f3421797STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) 342f3421797STejun Heo 343dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 344dc186ad7SThomas Gleixner 345dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 346dc186ad7SThomas Gleixner 34799777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 34899777288SStanislaw Gruszka { 34999777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 35099777288SStanislaw Gruszka } 35199777288SStanislaw Gruszka 352dc186ad7SThomas Gleixner /* 353dc186ad7SThomas Gleixner * fixup_init is called when: 354dc186ad7SThomas Gleixner * - an active object is initialized 355dc186ad7SThomas Gleixner */ 356dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 357dc186ad7SThomas Gleixner { 358dc186ad7SThomas Gleixner struct work_struct *work = addr; 359dc186ad7SThomas Gleixner 360dc186ad7SThomas Gleixner switch (state) { 361dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 362dc186ad7SThomas Gleixner cancel_work_sync(work); 363dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 364dc186ad7SThomas Gleixner return 1; 365dc186ad7SThomas Gleixner default: 366dc186ad7SThomas Gleixner return 0; 367dc186ad7SThomas Gleixner } 368dc186ad7SThomas Gleixner } 369dc186ad7SThomas Gleixner 370dc186ad7SThomas Gleixner /* 371dc186ad7SThomas Gleixner * fixup_activate is called when: 372dc186ad7SThomas Gleixner * - an active object is activated 373dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 374dc186ad7SThomas Gleixner */ 375dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 376dc186ad7SThomas Gleixner { 377dc186ad7SThomas Gleixner struct work_struct *work = addr; 378dc186ad7SThomas Gleixner 379dc186ad7SThomas Gleixner switch (state) { 380dc186ad7SThomas Gleixner 381dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 382dc186ad7SThomas Gleixner /* 383dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 384dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 385dc186ad7SThomas Gleixner * is tracked in the object tracker. 386dc186ad7SThomas Gleixner */ 38722df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 388dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 389dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 390dc186ad7SThomas Gleixner return 0; 391dc186ad7SThomas Gleixner } 392dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 393dc186ad7SThomas Gleixner return 0; 394dc186ad7SThomas Gleixner 395dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 396dc186ad7SThomas Gleixner WARN_ON(1); 397dc186ad7SThomas Gleixner 398dc186ad7SThomas Gleixner default: 399dc186ad7SThomas Gleixner return 0; 400dc186ad7SThomas Gleixner } 401dc186ad7SThomas Gleixner } 402dc186ad7SThomas Gleixner 403dc186ad7SThomas Gleixner /* 404dc186ad7SThomas Gleixner * fixup_free is called when: 405dc186ad7SThomas Gleixner * - an active object is freed 406dc186ad7SThomas Gleixner */ 407dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 408dc186ad7SThomas Gleixner { 409dc186ad7SThomas Gleixner struct work_struct *work = addr; 410dc186ad7SThomas Gleixner 411dc186ad7SThomas Gleixner switch (state) { 412dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 413dc186ad7SThomas Gleixner cancel_work_sync(work); 414dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 415dc186ad7SThomas Gleixner return 1; 416dc186ad7SThomas Gleixner default: 417dc186ad7SThomas Gleixner return 0; 418dc186ad7SThomas Gleixner } 419dc186ad7SThomas Gleixner } 420dc186ad7SThomas Gleixner 421dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 422dc186ad7SThomas Gleixner .name = "work_struct", 42399777288SStanislaw Gruszka .debug_hint = work_debug_hint, 424dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 425dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 426dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 427dc186ad7SThomas Gleixner }; 428dc186ad7SThomas Gleixner 429dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 430dc186ad7SThomas Gleixner { 431dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 432dc186ad7SThomas Gleixner } 433dc186ad7SThomas Gleixner 434dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 435dc186ad7SThomas Gleixner { 436dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 437dc186ad7SThomas Gleixner } 438dc186ad7SThomas Gleixner 439dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 440dc186ad7SThomas Gleixner { 441dc186ad7SThomas Gleixner if (onstack) 442dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 443dc186ad7SThomas Gleixner else 444dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 445dc186ad7SThomas Gleixner } 446dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 447dc186ad7SThomas Gleixner 448dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 449dc186ad7SThomas Gleixner { 450dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 451dc186ad7SThomas Gleixner } 452dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 453dc186ad7SThomas Gleixner 454dc186ad7SThomas Gleixner #else 455dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 456dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 457dc186ad7SThomas Gleixner #endif 458dc186ad7SThomas Gleixner 45995402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 46095402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4611da177e4SLinus Torvalds static LIST_HEAD(workqueues); 462a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4631da177e4SLinus Torvalds 46414441960SOleg Nesterov /* 465e22bee78STejun Heo * The almighty global cpu workqueues. nr_running is the only field 466e22bee78STejun Heo * which is expected to be used frequently by other cpus via 467e22bee78STejun Heo * try_to_wake_up(). Put it in a separate cacheline. 46814441960SOleg Nesterov */ 4698b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq); 4704ce62e9eSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]); 471f756d5e2SNathan Lynch 472f3421797STejun Heo /* 473f3421797STejun Heo * Global cpu workqueue and nr_running counter for unbound gcwq. The 474f3421797STejun Heo * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its 475f3421797STejun Heo * workers have WORKER_UNBOUND set. 476f3421797STejun Heo */ 477f3421797STejun Heo static struct global_cwq unbound_global_cwq; 4784ce62e9eSTejun Heo static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = { 4794ce62e9eSTejun Heo [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */ 4804ce62e9eSTejun Heo }; 481f3421797STejun Heo 482c34056a3STejun Heo static int worker_thread(void *__worker); 4831da177e4SLinus Torvalds 4843270476aSTejun Heo static int worker_pool_pri(struct worker_pool *pool) 4853270476aSTejun Heo { 4863270476aSTejun Heo return pool - pool->gcwq->pools; 4873270476aSTejun Heo } 4883270476aSTejun Heo 4898b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu) 4901da177e4SLinus Torvalds { 491f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 4928b03ae3cSTejun Heo return &per_cpu(global_cwq, cpu); 493f3421797STejun Heo else 494f3421797STejun Heo return &unbound_global_cwq; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 49763d95a91STejun Heo static atomic_t *get_pool_nr_running(struct worker_pool *pool) 498b1f4ec17SOleg Nesterov { 49963d95a91STejun Heo int cpu = pool->gcwq->cpu; 5003270476aSTejun Heo int idx = worker_pool_pri(pool); 50163d95a91STejun Heo 502f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 5034ce62e9eSTejun Heo return &per_cpu(pool_nr_running, cpu)[idx]; 504f3421797STejun Heo else 5054ce62e9eSTejun Heo return &unbound_pool_nr_running[idx]; 506b1f4ec17SOleg Nesterov } 507b1f4ec17SOleg Nesterov 5084690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 5094690c4abSTejun Heo struct workqueue_struct *wq) 510a848e3b6SOleg Nesterov { 511f3421797STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 512e06ffa1eSLai Jiangshan if (likely(cpu < nr_cpu_ids)) 513bdbc5dd7STejun Heo return per_cpu_ptr(wq->cpu_wq.pcpu, cpu); 514f3421797STejun Heo } else if (likely(cpu == WORK_CPU_UNBOUND)) 515f3421797STejun Heo return wq->cpu_wq.single; 516f3421797STejun Heo return NULL; 517f3421797STejun Heo } 518a848e3b6SOleg Nesterov 51973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 52073f53c4aSTejun Heo { 52173f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 52273f53c4aSTejun Heo } 52373f53c4aSTejun Heo 52473f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 52573f53c4aSTejun Heo { 52673f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 52773f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 52873f53c4aSTejun Heo } 52973f53c4aSTejun Heo 53073f53c4aSTejun Heo static int work_next_color(int color) 53173f53c4aSTejun Heo { 53273f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5354594bf15SDavid Howells /* 536b5490077STejun Heo * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data 537b5490077STejun Heo * contain the pointer to the queued cwq. Once execution starts, the flag 538b5490077STejun Heo * is cleared and the high bits contain OFFQ flags and CPU number. 5397a22ad75STejun Heo * 5408930cabaSTejun Heo * set_work_cwq(), set_work_cpu_and_clear_pending() and clear_work_data() 5418930cabaSTejun Heo * can be used to set the cwq, cpu or clear work->data. These functions 5428930cabaSTejun Heo * should only be called while the work is owned - ie. while the PENDING 5438930cabaSTejun Heo * bit is set. 5447a22ad75STejun Heo * 5457a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 5467a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 5477a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 5487a22ad75STejun Heo * queueing until execution starts. 5494594bf15SDavid Howells */ 5507a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5517a22ad75STejun Heo unsigned long flags) 5527a22ad75STejun Heo { 5537a22ad75STejun Heo BUG_ON(!work_pending(work)); 5547a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5557a22ad75STejun Heo } 5567a22ad75STejun Heo 5577a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5584690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5594690c4abSTejun Heo unsigned long extra_flags) 560365970a1SDavid Howells { 5617a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 562e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 563365970a1SDavid Howells } 564365970a1SDavid Howells 5658930cabaSTejun Heo static void set_work_cpu_and_clear_pending(struct work_struct *work, 5668930cabaSTejun Heo unsigned int cpu) 5674d707b9fSOleg Nesterov { 568b5490077STejun Heo set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0); 5694d707b9fSOleg Nesterov } 5704d707b9fSOleg Nesterov 5717a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 572365970a1SDavid Howells { 5737a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5747a22ad75STejun Heo } 5757a22ad75STejun Heo 5767a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5777a22ad75STejun Heo { 578e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5797a22ad75STejun Heo 580e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 581e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 582e120153dSTejun Heo else 583e120153dSTejun Heo return NULL; 5847a22ad75STejun Heo } 5857a22ad75STejun Heo 5867a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5877a22ad75STejun Heo { 588e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5897a22ad75STejun Heo unsigned int cpu; 5907a22ad75STejun Heo 591e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 592e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 593bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5947a22ad75STejun Heo 595b5490077STejun Heo cpu = data >> WORK_OFFQ_CPU_SHIFT; 596bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 5977a22ad75STejun Heo return NULL; 5987a22ad75STejun Heo 599f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 6007a22ad75STejun Heo return get_gcwq(cpu); 601365970a1SDavid Howells } 602365970a1SDavid Howells 603e22bee78STejun Heo /* 6043270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6053270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6063270476aSTejun Heo * they're being called with gcwq->lock held. 607e22bee78STejun Heo */ 608e22bee78STejun Heo 60963d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 610649027d7STejun Heo { 6113270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 612649027d7STejun Heo } 613649027d7STejun Heo 614e22bee78STejun Heo /* 615e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 616e22bee78STejun Heo * running workers. 617974271c4STejun Heo * 618974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 619974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 620974271c4STejun Heo * worklist isn't empty. 621e22bee78STejun Heo */ 62263d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 623e22bee78STejun Heo { 62463d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 625e22bee78STejun Heo } 626e22bee78STejun Heo 627e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 62863d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 629e22bee78STejun Heo { 63063d95a91STejun Heo return pool->nr_idle; 631e22bee78STejun Heo } 632e22bee78STejun Heo 633e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 63463d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 635e22bee78STejun Heo { 63663d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 637e22bee78STejun Heo 6383270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 639e22bee78STejun Heo } 640e22bee78STejun Heo 641e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 64263d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 643e22bee78STejun Heo { 64463d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 645e22bee78STejun Heo } 646e22bee78STejun Heo 647e22bee78STejun Heo /* Do I need to be the manager? */ 64863d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 649e22bee78STejun Heo { 65063d95a91STejun Heo return need_to_create_worker(pool) || 65111ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 652e22bee78STejun Heo } 653e22bee78STejun Heo 654e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 65563d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 656e22bee78STejun Heo { 65760373152STejun Heo bool managing = mutex_is_locked(&pool->manager_mutex); 65863d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 65963d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 660e22bee78STejun Heo 661e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 662e22bee78STejun Heo } 663e22bee78STejun Heo 664e22bee78STejun Heo /* 665e22bee78STejun Heo * Wake up functions. 666e22bee78STejun Heo */ 667e22bee78STejun Heo 6687e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 66963d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6707e11629dSTejun Heo { 67163d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6727e11629dSTejun Heo return NULL; 6737e11629dSTejun Heo 67463d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6757e11629dSTejun Heo } 6767e11629dSTejun Heo 6777e11629dSTejun Heo /** 6787e11629dSTejun Heo * wake_up_worker - wake up an idle worker 67963d95a91STejun Heo * @pool: worker pool to wake worker from 6807e11629dSTejun Heo * 68163d95a91STejun Heo * Wake up the first idle worker of @pool. 6827e11629dSTejun Heo * 6837e11629dSTejun Heo * CONTEXT: 6847e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 6857e11629dSTejun Heo */ 68663d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 6877e11629dSTejun Heo { 68863d95a91STejun Heo struct worker *worker = first_worker(pool); 6897e11629dSTejun Heo 6907e11629dSTejun Heo if (likely(worker)) 6917e11629dSTejun Heo wake_up_process(worker->task); 6927e11629dSTejun Heo } 6937e11629dSTejun Heo 6944690c4abSTejun Heo /** 695e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 696e22bee78STejun Heo * @task: task waking up 697e22bee78STejun Heo * @cpu: CPU @task is waking up to 698e22bee78STejun Heo * 699e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 700e22bee78STejun Heo * being awoken. 701e22bee78STejun Heo * 702e22bee78STejun Heo * CONTEXT: 703e22bee78STejun Heo * spin_lock_irq(rq->lock) 704e22bee78STejun Heo */ 705e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 706e22bee78STejun Heo { 707e22bee78STejun Heo struct worker *worker = kthread_data(task); 708e22bee78STejun Heo 7092d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 71063d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 711e22bee78STejun Heo } 712e22bee78STejun Heo 713e22bee78STejun Heo /** 714e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 715e22bee78STejun Heo * @task: task going to sleep 716e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 717e22bee78STejun Heo * 718e22bee78STejun Heo * This function is called during schedule() when a busy worker is 719e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 720e22bee78STejun Heo * returning pointer to its task. 721e22bee78STejun Heo * 722e22bee78STejun Heo * CONTEXT: 723e22bee78STejun Heo * spin_lock_irq(rq->lock) 724e22bee78STejun Heo * 725e22bee78STejun Heo * RETURNS: 726e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 727e22bee78STejun Heo */ 728e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 729e22bee78STejun Heo unsigned int cpu) 730e22bee78STejun Heo { 731e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 732bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 73363d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 734e22bee78STejun Heo 7352d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 736e22bee78STejun Heo return NULL; 737e22bee78STejun Heo 738e22bee78STejun Heo /* this can only happen on the local cpu */ 739e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 740e22bee78STejun Heo 741e22bee78STejun Heo /* 742e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 743e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 744e22bee78STejun Heo * Please read comment there. 745e22bee78STejun Heo * 746628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 747628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 748628c78e7STejun Heo * disabled, which in turn means that none else could be 749628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 750628c78e7STejun Heo * lock is safe. 751e22bee78STejun Heo */ 752bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 75363d95a91STejun Heo to_wakeup = first_worker(pool); 754e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 755e22bee78STejun Heo } 756e22bee78STejun Heo 757e22bee78STejun Heo /** 758e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 759cb444766STejun Heo * @worker: self 760d302f017STejun Heo * @flags: flags to set 761d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 762d302f017STejun Heo * 763e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 764e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 765e22bee78STejun Heo * woken up. 766d302f017STejun Heo * 767cb444766STejun Heo * CONTEXT: 768cb444766STejun Heo * spin_lock_irq(gcwq->lock) 769d302f017STejun Heo */ 770d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 771d302f017STejun Heo bool wakeup) 772d302f017STejun Heo { 773bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 774e22bee78STejun Heo 775cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 776cb444766STejun Heo 777e22bee78STejun Heo /* 778e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 779e22bee78STejun Heo * wake up an idle worker as necessary if requested by 780e22bee78STejun Heo * @wakeup. 781e22bee78STejun Heo */ 782e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 783e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 78463d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 785e22bee78STejun Heo 786e22bee78STejun Heo if (wakeup) { 787e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 788bd7bdd43STejun Heo !list_empty(&pool->worklist)) 78963d95a91STejun Heo wake_up_worker(pool); 790e22bee78STejun Heo } else 791e22bee78STejun Heo atomic_dec(nr_running); 792e22bee78STejun Heo } 793e22bee78STejun Heo 794d302f017STejun Heo worker->flags |= flags; 795d302f017STejun Heo } 796d302f017STejun Heo 797d302f017STejun Heo /** 798e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 799cb444766STejun Heo * @worker: self 800d302f017STejun Heo * @flags: flags to clear 801d302f017STejun Heo * 802e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 803d302f017STejun Heo * 804cb444766STejun Heo * CONTEXT: 805cb444766STejun Heo * spin_lock_irq(gcwq->lock) 806d302f017STejun Heo */ 807d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 808d302f017STejun Heo { 80963d95a91STejun Heo struct worker_pool *pool = worker->pool; 810e22bee78STejun Heo unsigned int oflags = worker->flags; 811e22bee78STejun Heo 812cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 813cb444766STejun Heo 814d302f017STejun Heo worker->flags &= ~flags; 815e22bee78STejun Heo 81642c025f3STejun Heo /* 81742c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 81842c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 81942c025f3STejun Heo * of multiple flags, not a single flag. 82042c025f3STejun Heo */ 821e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 822e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 82363d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 824d302f017STejun Heo } 825d302f017STejun Heo 826d302f017STejun Heo /** 827c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 828c8e55f36STejun Heo * @gcwq: gcwq of interest 829c8e55f36STejun Heo * @work: work to be hashed 830c8e55f36STejun Heo * 831c8e55f36STejun Heo * Return hash head of @gcwq for @work. 832c8e55f36STejun Heo * 833c8e55f36STejun Heo * CONTEXT: 834c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 835c8e55f36STejun Heo * 836c8e55f36STejun Heo * RETURNS: 837c8e55f36STejun Heo * Pointer to the hash head. 838c8e55f36STejun Heo */ 839c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 840c8e55f36STejun Heo struct work_struct *work) 841c8e55f36STejun Heo { 842c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 843c8e55f36STejun Heo unsigned long v = (unsigned long)work; 844c8e55f36STejun Heo 845c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 846c8e55f36STejun Heo v >>= base_shift; 847c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 848c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 849c8e55f36STejun Heo 850c8e55f36STejun Heo return &gcwq->busy_hash[v]; 851c8e55f36STejun Heo } 852c8e55f36STejun Heo 853c8e55f36STejun Heo /** 8548cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8558cca0eeaSTejun Heo * @gcwq: gcwq of interest 8568cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8578cca0eeaSTejun Heo * @work: work to find worker for 8588cca0eeaSTejun Heo * 8598cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8608cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8618cca0eeaSTejun Heo * work. 8628cca0eeaSTejun Heo * 8638cca0eeaSTejun Heo * CONTEXT: 8648cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8658cca0eeaSTejun Heo * 8668cca0eeaSTejun Heo * RETURNS: 8678cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8688cca0eeaSTejun Heo * otherwise. 8698cca0eeaSTejun Heo */ 8708cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8718cca0eeaSTejun Heo struct hlist_head *bwh, 8728cca0eeaSTejun Heo struct work_struct *work) 8738cca0eeaSTejun Heo { 8748cca0eeaSTejun Heo struct worker *worker; 8758cca0eeaSTejun Heo struct hlist_node *tmp; 8768cca0eeaSTejun Heo 8778cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8788cca0eeaSTejun Heo if (worker->current_work == work) 8798cca0eeaSTejun Heo return worker; 8808cca0eeaSTejun Heo return NULL; 8818cca0eeaSTejun Heo } 8828cca0eeaSTejun Heo 8838cca0eeaSTejun Heo /** 8848cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 8858cca0eeaSTejun Heo * @gcwq: gcwq of interest 8868cca0eeaSTejun Heo * @work: work to find worker for 8878cca0eeaSTejun Heo * 8888cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 8898cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 8908cca0eeaSTejun Heo * function calculates @bwh itself. 8918cca0eeaSTejun Heo * 8928cca0eeaSTejun Heo * CONTEXT: 8938cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8948cca0eeaSTejun Heo * 8958cca0eeaSTejun Heo * RETURNS: 8968cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8978cca0eeaSTejun Heo * otherwise. 8988cca0eeaSTejun Heo */ 8998cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 9008cca0eeaSTejun Heo struct work_struct *work) 9018cca0eeaSTejun Heo { 9028cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9038cca0eeaSTejun Heo work); 9048cca0eeaSTejun Heo } 9058cca0eeaSTejun Heo 9068cca0eeaSTejun Heo /** 907bf4ede01STejun Heo * move_linked_works - move linked works to a list 908bf4ede01STejun Heo * @work: start of series of works to be scheduled 909bf4ede01STejun Heo * @head: target list to append @work to 910bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 911bf4ede01STejun Heo * 912bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 913bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 914bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 915bf4ede01STejun Heo * 916bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 917bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 918bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 919bf4ede01STejun Heo * 920bf4ede01STejun Heo * CONTEXT: 921bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 922bf4ede01STejun Heo */ 923bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 924bf4ede01STejun Heo struct work_struct **nextp) 925bf4ede01STejun Heo { 926bf4ede01STejun Heo struct work_struct *n; 927bf4ede01STejun Heo 928bf4ede01STejun Heo /* 929bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 930bf4ede01STejun Heo * use NULL for list head. 931bf4ede01STejun Heo */ 932bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 933bf4ede01STejun Heo list_move_tail(&work->entry, head); 934bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 935bf4ede01STejun Heo break; 936bf4ede01STejun Heo } 937bf4ede01STejun Heo 938bf4ede01STejun Heo /* 939bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 940bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 941bf4ede01STejun Heo * needs to be updated. 942bf4ede01STejun Heo */ 943bf4ede01STejun Heo if (nextp) 944bf4ede01STejun Heo *nextp = n; 945bf4ede01STejun Heo } 946bf4ede01STejun Heo 947bf4ede01STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 948bf4ede01STejun Heo { 949bf4ede01STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 950bf4ede01STejun Heo struct work_struct, entry); 951bf4ede01STejun Heo 952bf4ede01STejun Heo trace_workqueue_activate_work(work); 953bf4ede01STejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 954bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 955bf4ede01STejun Heo cwq->nr_active++; 956bf4ede01STejun Heo } 957bf4ede01STejun Heo 958bf4ede01STejun Heo /** 959bf4ede01STejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 960bf4ede01STejun Heo * @cwq: cwq of interest 961bf4ede01STejun Heo * @color: color of work which left the queue 962bf4ede01STejun Heo * @delayed: for a delayed work 963bf4ede01STejun Heo * 964bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 965bf4ede01STejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 966bf4ede01STejun Heo * 967bf4ede01STejun Heo * CONTEXT: 968bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 969bf4ede01STejun Heo */ 970bf4ede01STejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 971bf4ede01STejun Heo bool delayed) 972bf4ede01STejun Heo { 973bf4ede01STejun Heo /* ignore uncolored works */ 974bf4ede01STejun Heo if (color == WORK_NO_COLOR) 975bf4ede01STejun Heo return; 976bf4ede01STejun Heo 977bf4ede01STejun Heo cwq->nr_in_flight[color]--; 978bf4ede01STejun Heo 979bf4ede01STejun Heo if (!delayed) { 980bf4ede01STejun Heo cwq->nr_active--; 981bf4ede01STejun Heo if (!list_empty(&cwq->delayed_works)) { 982bf4ede01STejun Heo /* one down, submit a delayed one */ 983bf4ede01STejun Heo if (cwq->nr_active < cwq->max_active) 984bf4ede01STejun Heo cwq_activate_first_delayed(cwq); 985bf4ede01STejun Heo } 986bf4ede01STejun Heo } 987bf4ede01STejun Heo 988bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 989bf4ede01STejun Heo if (likely(cwq->flush_color != color)) 990bf4ede01STejun Heo return; 991bf4ede01STejun Heo 992bf4ede01STejun Heo /* are there still in-flight works? */ 993bf4ede01STejun Heo if (cwq->nr_in_flight[color]) 994bf4ede01STejun Heo return; 995bf4ede01STejun Heo 996bf4ede01STejun Heo /* this cwq is done, clear flush_color */ 997bf4ede01STejun Heo cwq->flush_color = -1; 998bf4ede01STejun Heo 999bf4ede01STejun Heo /* 1000bf4ede01STejun Heo * If this was the last cwq, wake up the first flusher. It 1001bf4ede01STejun Heo * will handle the rest. 1002bf4ede01STejun Heo */ 1003bf4ede01STejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 1004bf4ede01STejun Heo complete(&cwq->wq->first_flusher->done); 1005bf4ede01STejun Heo } 1006bf4ede01STejun Heo 1007*36e227d2STejun Heo /** 1008*36e227d2STejun Heo * try_to_grab_pending - steal work item from worklist 1009*36e227d2STejun Heo * @work: work item to steal 1010*36e227d2STejun Heo * @is_dwork: @work is a delayed_work 1011*36e227d2STejun Heo * 1012*36e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1013*36e227d2STejun Heo * stable state - idle, on timer or on worklist. Return values are 1014*36e227d2STejun Heo * 1015*36e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 1016*36e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 1017*36e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1018*36e227d2STejun Heo * 1019*36e227d2STejun Heo * On >= 0 return, the caller owns @work's PENDING bit. 1020bf4ede01STejun Heo */ 1021*36e227d2STejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork) 1022bf4ede01STejun Heo { 1023bf4ede01STejun Heo struct global_cwq *gcwq; 1024bf4ede01STejun Heo 1025*36e227d2STejun Heo /* try to steal the timer if it exists */ 1026*36e227d2STejun Heo if (is_dwork) { 1027*36e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 1028*36e227d2STejun Heo 1029*36e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 1030*36e227d2STejun Heo return 1; 1031*36e227d2STejun Heo } 1032*36e227d2STejun Heo 1033*36e227d2STejun Heo /* try to claim PENDING the normal way */ 1034bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1035bf4ede01STejun Heo return 0; 1036bf4ede01STejun Heo 1037bf4ede01STejun Heo /* 1038bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1039bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1040bf4ede01STejun Heo */ 1041bf4ede01STejun Heo gcwq = get_work_gcwq(work); 1042bf4ede01STejun Heo if (!gcwq) 1043*36e227d2STejun Heo return -EAGAIN; 1044bf4ede01STejun Heo 1045bf4ede01STejun Heo spin_lock_irq(&gcwq->lock); 1046bf4ede01STejun Heo if (!list_empty(&work->entry)) { 1047bf4ede01STejun Heo /* 1048bf4ede01STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 1049bf4ede01STejun Heo * In that case we must see the new value after rmb(), see 1050bf4ede01STejun Heo * insert_work()->wmb(). 1051bf4ede01STejun Heo */ 1052bf4ede01STejun Heo smp_rmb(); 1053bf4ede01STejun Heo if (gcwq == get_work_gcwq(work)) { 1054bf4ede01STejun Heo debug_work_deactivate(work); 1055bf4ede01STejun Heo list_del_init(&work->entry); 1056bf4ede01STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 1057bf4ede01STejun Heo get_work_color(work), 1058bf4ede01STejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 1059*36e227d2STejun Heo 1060*36e227d2STejun Heo spin_unlock_irq(&gcwq->lock); 1061*36e227d2STejun Heo return 1; 1062bf4ede01STejun Heo } 1063bf4ede01STejun Heo } 1064bf4ede01STejun Heo spin_unlock_irq(&gcwq->lock); 1065bf4ede01STejun Heo 1066*36e227d2STejun Heo return -EAGAIN; 1067bf4ede01STejun Heo } 1068bf4ede01STejun Heo 1069bf4ede01STejun Heo /** 10707e11629dSTejun Heo * insert_work - insert a work into gcwq 10714690c4abSTejun Heo * @cwq: cwq @work belongs to 10724690c4abSTejun Heo * @work: work to insert 10734690c4abSTejun Heo * @head: insertion point 10744690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 10754690c4abSTejun Heo * 10767e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 10777e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 10784690c4abSTejun Heo * 10794690c4abSTejun Heo * CONTEXT: 10808b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 10811da177e4SLinus Torvalds */ 1082b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 10834690c4abSTejun Heo struct work_struct *work, struct list_head *head, 10844690c4abSTejun Heo unsigned int extra_flags) 1085b89deed3SOleg Nesterov { 108663d95a91STejun Heo struct worker_pool *pool = cwq->pool; 1087e1d8aa9fSFrederic Weisbecker 10884690c4abSTejun Heo /* we own @work, set data and link */ 10897a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 10904690c4abSTejun Heo 10916e84d644SOleg Nesterov /* 10926e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 10936e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 10946e84d644SOleg Nesterov */ 10956e84d644SOleg Nesterov smp_wmb(); 10964690c4abSTejun Heo 10971a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1098e22bee78STejun Heo 1099e22bee78STejun Heo /* 1100e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1101e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1102e22bee78STejun Heo * lying around lazily while there are works to be processed. 1103e22bee78STejun Heo */ 1104e22bee78STejun Heo smp_mb(); 1105e22bee78STejun Heo 110663d95a91STejun Heo if (__need_more_worker(pool)) 110763d95a91STejun Heo wake_up_worker(pool); 1108b89deed3SOleg Nesterov } 1109b89deed3SOleg Nesterov 1110c8efcc25STejun Heo /* 1111c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 1112c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 1113c8efcc25STejun Heo * cold paths. 1114c8efcc25STejun Heo */ 1115c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1116c8efcc25STejun Heo { 1117c8efcc25STejun Heo unsigned long flags; 1118c8efcc25STejun Heo unsigned int cpu; 1119c8efcc25STejun Heo 1120c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 1121c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 1122c8efcc25STejun Heo struct worker *worker; 1123c8efcc25STejun Heo struct hlist_node *pos; 1124c8efcc25STejun Heo int i; 1125c8efcc25STejun Heo 1126c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1127c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 1128c8efcc25STejun Heo if (worker->task != current) 1129c8efcc25STejun Heo continue; 1130c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1131c8efcc25STejun Heo /* 1132c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 1133c8efcc25STejun Heo * is headed to the same workqueue. 1134c8efcc25STejun Heo */ 1135c8efcc25STejun Heo return worker->current_cwq->wq == wq; 1136c8efcc25STejun Heo } 1137c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1138c8efcc25STejun Heo } 1139c8efcc25STejun Heo return false; 1140c8efcc25STejun Heo } 1141c8efcc25STejun Heo 11424690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 11431da177e4SLinus Torvalds struct work_struct *work) 11441da177e4SLinus Torvalds { 1145502ca9d8STejun Heo struct global_cwq *gcwq; 1146502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 11471e19ffc6STejun Heo struct list_head *worklist; 11488a2e8e5dSTejun Heo unsigned int work_flags; 11498930cabaSTejun Heo 11508930cabaSTejun Heo /* 11518930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11528930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11538930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11548930cabaSTejun Heo * happen with IRQ disabled. 11558930cabaSTejun Heo */ 11568930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 11571da177e4SLinus Torvalds 1158dc186ad7SThomas Gleixner debug_work_activate(work); 11591e19ffc6STejun Heo 1160c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 11619c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1162c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1163e41e704bSTejun Heo return; 1164e41e704bSTejun Heo 1165c7fc77f7STejun Heo /* determine gcwq to use */ 1166c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1167c7fc77f7STejun Heo struct global_cwq *last_gcwq; 1168c7fc77f7STejun Heo 116957469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1170f3421797STejun Heo cpu = raw_smp_processor_id(); 1171f3421797STejun Heo 117218aa9effSTejun Heo /* 117318aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 117418aa9effSTejun Heo * was previously on a different cpu, it might still 117518aa9effSTejun Heo * be running there, in which case the work needs to 117618aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 117718aa9effSTejun Heo */ 1178502ca9d8STejun Heo gcwq = get_gcwq(cpu); 117918aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 118018aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 118118aa9effSTejun Heo struct worker *worker; 118218aa9effSTejun Heo 11838930cabaSTejun Heo spin_lock(&last_gcwq->lock); 118418aa9effSTejun Heo 118518aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 118618aa9effSTejun Heo 118718aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 118818aa9effSTejun Heo gcwq = last_gcwq; 118918aa9effSTejun Heo else { 119018aa9effSTejun Heo /* meh... not running there, queue here */ 11918930cabaSTejun Heo spin_unlock(&last_gcwq->lock); 11928930cabaSTejun Heo spin_lock(&gcwq->lock); 119318aa9effSTejun Heo } 11948930cabaSTejun Heo } else { 11958930cabaSTejun Heo spin_lock(&gcwq->lock); 11968930cabaSTejun Heo } 1197f3421797STejun Heo } else { 1198f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 11998930cabaSTejun Heo spin_lock(&gcwq->lock); 1200502ca9d8STejun Heo } 1201502ca9d8STejun Heo 1202502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1203502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1204cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1205502ca9d8STejun Heo 1206f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 12078930cabaSTejun Heo spin_unlock(&gcwq->lock); 1208f5b2552bSDan Carpenter return; 1209f5b2552bSDan Carpenter } 12101e19ffc6STejun Heo 121173f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 12128a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 12131e19ffc6STejun Heo 12141e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1215cdadf009STejun Heo trace_workqueue_activate_work(work); 12161e19ffc6STejun Heo cwq->nr_active++; 12173270476aSTejun Heo worklist = &cwq->pool->worklist; 12188a2e8e5dSTejun Heo } else { 12198a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 12201e19ffc6STejun Heo worklist = &cwq->delayed_works; 12218a2e8e5dSTejun Heo } 12221e19ffc6STejun Heo 12238a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 12241e19ffc6STejun Heo 12258930cabaSTejun Heo spin_unlock(&gcwq->lock); 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12280fcb78c2SRolf Eike Beer /** 1229c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1230c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1231c1a220e7SZhang Rui * @wq: workqueue to use 1232c1a220e7SZhang Rui * @work: work to queue 1233c1a220e7SZhang Rui * 1234d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1235c1a220e7SZhang Rui * 1236c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1237c1a220e7SZhang Rui * can't go away. 1238c1a220e7SZhang Rui */ 1239d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1240d4283e93STejun Heo struct work_struct *work) 1241c1a220e7SZhang Rui { 1242d4283e93STejun Heo bool ret = false; 12438930cabaSTejun Heo unsigned long flags; 12448930cabaSTejun Heo 12458930cabaSTejun Heo local_irq_save(flags); 1246c1a220e7SZhang Rui 124722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12484690c4abSTejun Heo __queue_work(cpu, wq, work); 1249d4283e93STejun Heo ret = true; 1250c1a220e7SZhang Rui } 12518930cabaSTejun Heo 12528930cabaSTejun Heo local_irq_restore(flags); 1253c1a220e7SZhang Rui return ret; 1254c1a220e7SZhang Rui } 1255c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1256c1a220e7SZhang Rui 12570a13c00eSTejun Heo /** 12580a13c00eSTejun Heo * queue_work - queue work on a workqueue 12590a13c00eSTejun Heo * @wq: workqueue to use 12600a13c00eSTejun Heo * @work: work to queue 12610a13c00eSTejun Heo * 1262d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 12630a13c00eSTejun Heo * 12640a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 12650a13c00eSTejun Heo * it can be processed by another CPU. 12660a13c00eSTejun Heo */ 1267d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 12680a13c00eSTejun Heo { 126957469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 12700a13c00eSTejun Heo } 12710a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 12720a13c00eSTejun Heo 1273d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 12741da177e4SLinus Torvalds { 127552bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 12767a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 12771da177e4SLinus Torvalds 12788930cabaSTejun Heo local_irq_disable(); 127957469821STejun Heo __queue_work(WORK_CPU_UNBOUND, cwq->wq, &dwork->work); 12808930cabaSTejun Heo local_irq_enable(); 12811da177e4SLinus Torvalds } 1282d8e794dfSTejun Heo EXPORT_SYMBOL_GPL(delayed_work_timer_fn); 12831da177e4SLinus Torvalds 12847beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 12857beb2edfSTejun Heo struct delayed_work *dwork, unsigned long delay) 12867beb2edfSTejun Heo { 12877beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 12887beb2edfSTejun Heo struct work_struct *work = &dwork->work; 12897beb2edfSTejun Heo unsigned int lcpu; 12907beb2edfSTejun Heo 12917beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 12927beb2edfSTejun Heo timer->data != (unsigned long)dwork); 12937beb2edfSTejun Heo BUG_ON(timer_pending(timer)); 12947beb2edfSTejun Heo BUG_ON(!list_empty(&work->entry)); 12957beb2edfSTejun Heo 12967beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 12977beb2edfSTejun Heo 12987beb2edfSTejun Heo /* 12997beb2edfSTejun Heo * This stores cwq for the moment, for the timer_fn. Note that the 13007beb2edfSTejun Heo * work's gcwq is preserved to allow reentrance detection for 13017beb2edfSTejun Heo * delayed works. 13027beb2edfSTejun Heo */ 13037beb2edfSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 13047beb2edfSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 13057beb2edfSTejun Heo 13067beb2edfSTejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 13077beb2edfSTejun Heo lcpu = gcwq->cpu; 13087beb2edfSTejun Heo else 13097beb2edfSTejun Heo lcpu = raw_smp_processor_id(); 13107beb2edfSTejun Heo } else { 13117beb2edfSTejun Heo lcpu = WORK_CPU_UNBOUND; 13127beb2edfSTejun Heo } 13137beb2edfSTejun Heo 13147beb2edfSTejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 13157beb2edfSTejun Heo 13167beb2edfSTejun Heo timer->expires = jiffies + delay; 13177beb2edfSTejun Heo 13187beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13197beb2edfSTejun Heo add_timer_on(timer, cpu); 13207beb2edfSTejun Heo else 13217beb2edfSTejun Heo add_timer(timer); 13227beb2edfSTejun Heo } 13237beb2edfSTejun Heo 13240fcb78c2SRolf Eike Beer /** 13250fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 13260fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 13270fcb78c2SRolf Eike Beer * @wq: workqueue to use 1328af9997e4SRandy Dunlap * @dwork: work to queue 13290fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 13300fcb78c2SRolf Eike Beer * 1331715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1332715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1333715f1300STejun Heo * execution. 13340fcb78c2SRolf Eike Beer */ 1335d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 133652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13377a6bc1cdSVenkatesh Pallipadi { 133852bad64dSDavid Howells struct work_struct *work = &dwork->work; 1339d4283e93STejun Heo bool ret = false; 13408930cabaSTejun Heo unsigned long flags; 13418930cabaSTejun Heo 1342715f1300STejun Heo if (!delay) 1343715f1300STejun Heo return queue_work_on(cpu, wq, &dwork->work); 1344715f1300STejun Heo 13458930cabaSTejun Heo /* read the comment in __queue_work() */ 13468930cabaSTejun Heo local_irq_save(flags); 13477a6bc1cdSVenkatesh Pallipadi 134822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 13497beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1350d4283e93STejun Heo ret = true; 13517a6bc1cdSVenkatesh Pallipadi } 13528930cabaSTejun Heo 13538930cabaSTejun Heo local_irq_restore(flags); 13547a6bc1cdSVenkatesh Pallipadi return ret; 13557a6bc1cdSVenkatesh Pallipadi } 1356ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 13571da177e4SLinus Torvalds 1358c8e55f36STejun Heo /** 13590a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 13600a13c00eSTejun Heo * @wq: workqueue to use 13610a13c00eSTejun Heo * @dwork: delayable work to queue 13620a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 13630a13c00eSTejun Heo * 1364715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 13650a13c00eSTejun Heo */ 1366d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 13670a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 13680a13c00eSTejun Heo { 136957469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 13700a13c00eSTejun Heo } 13710a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 13720a13c00eSTejun Heo 13730a13c00eSTejun Heo /** 1374c8e55f36STejun Heo * worker_enter_idle - enter idle state 1375c8e55f36STejun Heo * @worker: worker which is entering idle state 1376c8e55f36STejun Heo * 1377c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1378c8e55f36STejun Heo * necessary. 1379c8e55f36STejun Heo * 1380c8e55f36STejun Heo * LOCKING: 1381c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1382c8e55f36STejun Heo */ 1383c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 13841da177e4SLinus Torvalds { 1385bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1386bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1387c8e55f36STejun Heo 1388c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1389c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1390c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1391c8e55f36STejun Heo 1392cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1393cb444766STejun Heo worker->flags |= WORKER_IDLE; 1394bd7bdd43STejun Heo pool->nr_idle++; 1395e22bee78STejun Heo worker->last_active = jiffies; 1396c8e55f36STejun Heo 1397c8e55f36STejun Heo /* idle_list is LIFO */ 1398bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1399db7bccf4STejun Heo 140063d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1401628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1402cb444766STejun Heo 1403544ecf31STejun Heo /* 1404628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1405628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1406628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1407628c78e7STejun Heo * unbind is not in progress. 1408544ecf31STejun Heo */ 1409628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1410bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 141163d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1412c8e55f36STejun Heo } 1413c8e55f36STejun Heo 1414c8e55f36STejun Heo /** 1415c8e55f36STejun Heo * worker_leave_idle - leave idle state 1416c8e55f36STejun Heo * @worker: worker which is leaving idle state 1417c8e55f36STejun Heo * 1418c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1419c8e55f36STejun Heo * 1420c8e55f36STejun Heo * LOCKING: 1421c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1422c8e55f36STejun Heo */ 1423c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1424c8e55f36STejun Heo { 1425bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1426c8e55f36STejun Heo 1427c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1428d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1429bd7bdd43STejun Heo pool->nr_idle--; 1430c8e55f36STejun Heo list_del_init(&worker->entry); 1431c8e55f36STejun Heo } 1432c8e55f36STejun Heo 1433e22bee78STejun Heo /** 1434e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1435e22bee78STejun Heo * @worker: self 1436e22bee78STejun Heo * 1437e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1438e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1439e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1440e22bee78STejun Heo * guaranteed to execute on the cpu. 1441e22bee78STejun Heo * 1442e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1443e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1444e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1445e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1446e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1447e22bee78STejun Heo * [dis]associated in the meantime. 1448e22bee78STejun Heo * 1449f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1450f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1451f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1452f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1453f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1454e22bee78STejun Heo * 1455e22bee78STejun Heo * CONTEXT: 1456e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1457e22bee78STejun Heo * held. 1458e22bee78STejun Heo * 1459e22bee78STejun Heo * RETURNS: 1460e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1461e22bee78STejun Heo * bound), %false if offline. 1462e22bee78STejun Heo */ 1463e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1464972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1465e22bee78STejun Heo { 1466bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1467e22bee78STejun Heo struct task_struct *task = worker->task; 1468e22bee78STejun Heo 1469e22bee78STejun Heo while (true) { 1470e22bee78STejun Heo /* 1471e22bee78STejun Heo * The following call may fail, succeed or succeed 1472e22bee78STejun Heo * without actually migrating the task to the cpu if 1473e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1474e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1475e22bee78STejun Heo */ 1476f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1477e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1478e22bee78STejun Heo 1479e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1480e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1481e22bee78STejun Heo return false; 1482e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1483e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1484e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1485e22bee78STejun Heo return true; 1486e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1487e22bee78STejun Heo 14885035b20fSTejun Heo /* 14895035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 14905035b20fSTejun Heo * and retry migration. cond_resched() is required here; 14915035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 14925035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 14935035b20fSTejun Heo */ 1494e22bee78STejun Heo cpu_relax(); 14955035b20fSTejun Heo cond_resched(); 1496e22bee78STejun Heo } 1497e22bee78STejun Heo } 1498e22bee78STejun Heo 149925511a47STejun Heo struct idle_rebind { 150025511a47STejun Heo int cnt; /* # workers to be rebound */ 150125511a47STejun Heo struct completion done; /* all workers rebound */ 150225511a47STejun Heo }; 150325511a47STejun Heo 1504e22bee78STejun Heo /* 150525511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 150625511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 150725511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 150825511a47STejun Heo */ 150925511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 151025511a47STejun Heo { 151125511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 151225511a47STejun Heo 151325511a47STejun Heo /* CPU must be online at this point */ 151425511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 151525511a47STejun Heo if (!--worker->idle_rebind->cnt) 151625511a47STejun Heo complete(&worker->idle_rebind->done); 151725511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 151825511a47STejun Heo 151925511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 152025511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 152125511a47STejun Heo } 152225511a47STejun Heo 152325511a47STejun Heo /* 152425511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1525403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1526403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1527403c821dSTejun Heo * executed twice without intervening cpu down. 1528e22bee78STejun Heo */ 152925511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1530e22bee78STejun Heo { 1531e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1532bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1533e22bee78STejun Heo 1534e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1535e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1536e22bee78STejun Heo 1537e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1538e22bee78STejun Heo } 1539e22bee78STejun Heo 154025511a47STejun Heo /** 154125511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 154225511a47STejun Heo * @gcwq: gcwq of interest 154325511a47STejun Heo * 154425511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 154525511a47STejun Heo * is different for idle and busy ones. 154625511a47STejun Heo * 154725511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 154825511a47STejun Heo * be complete before any worker starts executing work items with 154925511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 155025511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 155125511a47STejun Heo * 155225511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 155325511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 155425511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 155525511a47STejun Heo * 155625511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 155725511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 155825511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 155925511a47STejun Heo * be properbly bumped as busy workers rebind. 156025511a47STejun Heo * 156125511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 156225511a47STejun Heo * work item scheduled. 156325511a47STejun Heo */ 156425511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 156525511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 156625511a47STejun Heo { 156725511a47STejun Heo struct idle_rebind idle_rebind; 156825511a47STejun Heo struct worker_pool *pool; 156925511a47STejun Heo struct worker *worker; 157025511a47STejun Heo struct hlist_node *pos; 157125511a47STejun Heo int i; 157225511a47STejun Heo 157325511a47STejun Heo lockdep_assert_held(&gcwq->lock); 157425511a47STejun Heo 157525511a47STejun Heo for_each_worker_pool(pool, gcwq) 157625511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 157725511a47STejun Heo 157825511a47STejun Heo /* 157925511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 158025511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 158125511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 158225511a47STejun Heo */ 158325511a47STejun Heo init_completion(&idle_rebind.done); 158425511a47STejun Heo retry: 158525511a47STejun Heo idle_rebind.cnt = 1; 158625511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 158725511a47STejun Heo 158825511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 158925511a47STejun Heo for_each_worker_pool(pool, gcwq) { 159025511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 159125511a47STejun Heo if (worker->flags & WORKER_REBIND) 159225511a47STejun Heo continue; 159325511a47STejun Heo 159425511a47STejun Heo /* morph UNBOUND to REBIND */ 159525511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 159625511a47STejun Heo worker->flags |= WORKER_REBIND; 159725511a47STejun Heo 159825511a47STejun Heo idle_rebind.cnt++; 159925511a47STejun Heo worker->idle_rebind = &idle_rebind; 160025511a47STejun Heo 160125511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 160225511a47STejun Heo wake_up_process(worker->task); 160325511a47STejun Heo } 160425511a47STejun Heo } 160525511a47STejun Heo 160625511a47STejun Heo if (--idle_rebind.cnt) { 160725511a47STejun Heo spin_unlock_irq(&gcwq->lock); 160825511a47STejun Heo wait_for_completion(&idle_rebind.done); 160925511a47STejun Heo spin_lock_irq(&gcwq->lock); 161025511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 161125511a47STejun Heo goto retry; 161225511a47STejun Heo } 161325511a47STejun Heo 161425511a47STejun Heo /* 161525511a47STejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 161625511a47STejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 161725511a47STejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 161825511a47STejun Heo * because these workers are still guaranteed to be idle. 161925511a47STejun Heo */ 162025511a47STejun Heo for_each_worker_pool(pool, gcwq) 162125511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 162225511a47STejun Heo worker->flags &= ~WORKER_REBIND; 162325511a47STejun Heo 162425511a47STejun Heo wake_up_all(&gcwq->rebind_hold); 162525511a47STejun Heo 162625511a47STejun Heo /* rebind busy workers */ 162725511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 162825511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 162925511a47STejun Heo 163025511a47STejun Heo /* morph UNBOUND to REBIND */ 163125511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 163225511a47STejun Heo worker->flags |= WORKER_REBIND; 163325511a47STejun Heo 163425511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 163525511a47STejun Heo work_data_bits(rebind_work))) 163625511a47STejun Heo continue; 163725511a47STejun Heo 163825511a47STejun Heo /* wq doesn't matter, use the default one */ 163925511a47STejun Heo debug_work_activate(rebind_work); 164025511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 164125511a47STejun Heo worker->scheduled.next, 164225511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 164325511a47STejun Heo } 164425511a47STejun Heo } 164525511a47STejun Heo 1646c34056a3STejun Heo static struct worker *alloc_worker(void) 1647c34056a3STejun Heo { 1648c34056a3STejun Heo struct worker *worker; 1649c34056a3STejun Heo 1650c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1651c8e55f36STejun Heo if (worker) { 1652c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1653affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 165425511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1655e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1656e22bee78STejun Heo worker->flags = WORKER_PREP; 1657c8e55f36STejun Heo } 1658c34056a3STejun Heo return worker; 1659c34056a3STejun Heo } 1660c34056a3STejun Heo 1661c34056a3STejun Heo /** 1662c34056a3STejun Heo * create_worker - create a new workqueue worker 166363d95a91STejun Heo * @pool: pool the new worker will belong to 1664c34056a3STejun Heo * 166563d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1666c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1667c34056a3STejun Heo * destroy_worker(). 1668c34056a3STejun Heo * 1669c34056a3STejun Heo * CONTEXT: 1670c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1671c34056a3STejun Heo * 1672c34056a3STejun Heo * RETURNS: 1673c34056a3STejun Heo * Pointer to the newly created worker. 1674c34056a3STejun Heo */ 1675bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1676c34056a3STejun Heo { 167763d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 16783270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1679c34056a3STejun Heo struct worker *worker = NULL; 1680f3421797STejun Heo int id = -1; 1681c34056a3STejun Heo 16828b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1683bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 16848b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1685bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1686c34056a3STejun Heo goto fail; 16878b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1688c34056a3STejun Heo } 16898b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1690c34056a3STejun Heo 1691c34056a3STejun Heo worker = alloc_worker(); 1692c34056a3STejun Heo if (!worker) 1693c34056a3STejun Heo goto fail; 1694c34056a3STejun Heo 1695bd7bdd43STejun Heo worker->pool = pool; 1696c34056a3STejun Heo worker->id = id; 1697c34056a3STejun Heo 1698bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 169994dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 17003270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 17013270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1702f3421797STejun Heo else 1703f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 17043270476aSTejun Heo "kworker/u:%d%s", id, pri); 1705c34056a3STejun Heo if (IS_ERR(worker->task)) 1706c34056a3STejun Heo goto fail; 1707c34056a3STejun Heo 17083270476aSTejun Heo if (worker_pool_pri(pool)) 17093270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 17103270476aSTejun Heo 1711db7bccf4STejun Heo /* 1712bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1713bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1714bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1715bc2ae0f5STejun Heo * above the flag definition for details. 1716bc2ae0f5STejun Heo * 1717bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1718bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1719db7bccf4STejun Heo */ 1720bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 17218b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1722bc2ae0f5STejun Heo } else { 1723db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1724f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1725f3421797STejun Heo } 1726c34056a3STejun Heo 1727c34056a3STejun Heo return worker; 1728c34056a3STejun Heo fail: 1729c34056a3STejun Heo if (id >= 0) { 17308b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1731bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 17328b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1733c34056a3STejun Heo } 1734c34056a3STejun Heo kfree(worker); 1735c34056a3STejun Heo return NULL; 1736c34056a3STejun Heo } 1737c34056a3STejun Heo 1738c34056a3STejun Heo /** 1739c34056a3STejun Heo * start_worker - start a newly created worker 1740c34056a3STejun Heo * @worker: worker to start 1741c34056a3STejun Heo * 1742c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1743c34056a3STejun Heo * 1744c34056a3STejun Heo * CONTEXT: 17458b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1746c34056a3STejun Heo */ 1747c34056a3STejun Heo static void start_worker(struct worker *worker) 1748c34056a3STejun Heo { 1749cb444766STejun Heo worker->flags |= WORKER_STARTED; 1750bd7bdd43STejun Heo worker->pool->nr_workers++; 1751c8e55f36STejun Heo worker_enter_idle(worker); 1752c34056a3STejun Heo wake_up_process(worker->task); 1753c34056a3STejun Heo } 1754c34056a3STejun Heo 1755c34056a3STejun Heo /** 1756c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1757c34056a3STejun Heo * @worker: worker to be destroyed 1758c34056a3STejun Heo * 1759c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1760c8e55f36STejun Heo * 1761c8e55f36STejun Heo * CONTEXT: 1762c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1763c34056a3STejun Heo */ 1764c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1765c34056a3STejun Heo { 1766bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1767bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1768c34056a3STejun Heo int id = worker->id; 1769c34056a3STejun Heo 1770c34056a3STejun Heo /* sanity check frenzy */ 1771c34056a3STejun Heo BUG_ON(worker->current_work); 1772affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1773c34056a3STejun Heo 1774c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1775bd7bdd43STejun Heo pool->nr_workers--; 1776c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1777bd7bdd43STejun Heo pool->nr_idle--; 1778c8e55f36STejun Heo 1779c8e55f36STejun Heo list_del_init(&worker->entry); 1780cb444766STejun Heo worker->flags |= WORKER_DIE; 1781c8e55f36STejun Heo 1782c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1783c8e55f36STejun Heo 1784c34056a3STejun Heo kthread_stop(worker->task); 1785c34056a3STejun Heo kfree(worker); 1786c34056a3STejun Heo 17878b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1788bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1789c34056a3STejun Heo } 1790c34056a3STejun Heo 179163d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1792e22bee78STejun Heo { 179363d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 179463d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1795e22bee78STejun Heo 1796e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1797e22bee78STejun Heo 179863d95a91STejun Heo if (too_many_workers(pool)) { 1799e22bee78STejun Heo struct worker *worker; 1800e22bee78STejun Heo unsigned long expires; 1801e22bee78STejun Heo 1802e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 180363d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1804e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1805e22bee78STejun Heo 1806e22bee78STejun Heo if (time_before(jiffies, expires)) 180763d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1808e22bee78STejun Heo else { 1809e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 181011ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 181163d95a91STejun Heo wake_up_worker(pool); 1812e22bee78STejun Heo } 1813e22bee78STejun Heo } 1814e22bee78STejun Heo 1815e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1816e22bee78STejun Heo } 1817e22bee78STejun Heo 1818e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1819e22bee78STejun Heo { 1820e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1821e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1822f3421797STejun Heo unsigned int cpu; 1823e22bee78STejun Heo 1824e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1825e22bee78STejun Heo return false; 1826e22bee78STejun Heo 1827e22bee78STejun Heo /* mayday mayday mayday */ 1828bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1829f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1830f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1831f3421797STejun Heo cpu = 0; 1832f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1833e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1834e22bee78STejun Heo return true; 1835e22bee78STejun Heo } 1836e22bee78STejun Heo 183763d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1838e22bee78STejun Heo { 183963d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 184063d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1841e22bee78STejun Heo struct work_struct *work; 1842e22bee78STejun Heo 1843e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1844e22bee78STejun Heo 184563d95a91STejun Heo if (need_to_create_worker(pool)) { 1846e22bee78STejun Heo /* 1847e22bee78STejun Heo * We've been trying to create a new worker but 1848e22bee78STejun Heo * haven't been successful. We might be hitting an 1849e22bee78STejun Heo * allocation deadlock. Send distress signals to 1850e22bee78STejun Heo * rescuers. 1851e22bee78STejun Heo */ 185263d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1853e22bee78STejun Heo send_mayday(work); 1854e22bee78STejun Heo } 1855e22bee78STejun Heo 1856e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1857e22bee78STejun Heo 185863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1859e22bee78STejun Heo } 1860e22bee78STejun Heo 1861e22bee78STejun Heo /** 1862e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 186363d95a91STejun Heo * @pool: pool to create a new worker for 1864e22bee78STejun Heo * 186563d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1866e22bee78STejun Heo * have at least one idle worker on return from this function. If 1867e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 186863d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1869e22bee78STejun Heo * possible allocation deadlock. 1870e22bee78STejun Heo * 1871e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1872e22bee78STejun Heo * may_start_working() true. 1873e22bee78STejun Heo * 1874e22bee78STejun Heo * LOCKING: 1875e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1876e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1877e22bee78STejun Heo * manager. 1878e22bee78STejun Heo * 1879e22bee78STejun Heo * RETURNS: 1880e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1881e22bee78STejun Heo * otherwise. 1882e22bee78STejun Heo */ 188363d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 188406bd6ebfSNamhyung Kim __releases(&gcwq->lock) 188506bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1886e22bee78STejun Heo { 188763d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 188863d95a91STejun Heo 188963d95a91STejun Heo if (!need_to_create_worker(pool)) 1890e22bee78STejun Heo return false; 1891e22bee78STejun Heo restart: 18929f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 18939f9c2364STejun Heo 1894e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 189563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1896e22bee78STejun Heo 1897e22bee78STejun Heo while (true) { 1898e22bee78STejun Heo struct worker *worker; 1899e22bee78STejun Heo 1900bc2ae0f5STejun Heo worker = create_worker(pool); 1901e22bee78STejun Heo if (worker) { 190263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1903e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1904e22bee78STejun Heo start_worker(worker); 190563d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1906e22bee78STejun Heo return true; 1907e22bee78STejun Heo } 1908e22bee78STejun Heo 190963d95a91STejun Heo if (!need_to_create_worker(pool)) 1910e22bee78STejun Heo break; 1911e22bee78STejun Heo 1912e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1913e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19149f9c2364STejun Heo 191563d95a91STejun Heo if (!need_to_create_worker(pool)) 1916e22bee78STejun Heo break; 1917e22bee78STejun Heo } 1918e22bee78STejun Heo 191963d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1920e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 192163d95a91STejun Heo if (need_to_create_worker(pool)) 1922e22bee78STejun Heo goto restart; 1923e22bee78STejun Heo return true; 1924e22bee78STejun Heo } 1925e22bee78STejun Heo 1926e22bee78STejun Heo /** 1927e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 192863d95a91STejun Heo * @pool: pool to destroy workers for 1929e22bee78STejun Heo * 193063d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1931e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1932e22bee78STejun Heo * 1933e22bee78STejun Heo * LOCKING: 1934e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1935e22bee78STejun Heo * multiple times. Called only from manager. 1936e22bee78STejun Heo * 1937e22bee78STejun Heo * RETURNS: 1938e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1939e22bee78STejun Heo * otherwise. 1940e22bee78STejun Heo */ 194163d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1942e22bee78STejun Heo { 1943e22bee78STejun Heo bool ret = false; 1944e22bee78STejun Heo 194563d95a91STejun Heo while (too_many_workers(pool)) { 1946e22bee78STejun Heo struct worker *worker; 1947e22bee78STejun Heo unsigned long expires; 1948e22bee78STejun Heo 194963d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1950e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1951e22bee78STejun Heo 1952e22bee78STejun Heo if (time_before(jiffies, expires)) { 195363d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1954e22bee78STejun Heo break; 1955e22bee78STejun Heo } 1956e22bee78STejun Heo 1957e22bee78STejun Heo destroy_worker(worker); 1958e22bee78STejun Heo ret = true; 1959e22bee78STejun Heo } 1960e22bee78STejun Heo 1961e22bee78STejun Heo return ret; 1962e22bee78STejun Heo } 1963e22bee78STejun Heo 1964e22bee78STejun Heo /** 1965e22bee78STejun Heo * manage_workers - manage worker pool 1966e22bee78STejun Heo * @worker: self 1967e22bee78STejun Heo * 1968e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1969e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1970e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1971e22bee78STejun Heo * 1972e22bee78STejun Heo * The caller can safely start processing works on false return. On 1973e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1974e22bee78STejun Heo * and may_start_working() is true. 1975e22bee78STejun Heo * 1976e22bee78STejun Heo * CONTEXT: 1977e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1978e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1979e22bee78STejun Heo * 1980e22bee78STejun Heo * RETURNS: 1981e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1982e22bee78STejun Heo * some action was taken. 1983e22bee78STejun Heo */ 1984e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1985e22bee78STejun Heo { 198663d95a91STejun Heo struct worker_pool *pool = worker->pool; 1987e22bee78STejun Heo bool ret = false; 1988e22bee78STejun Heo 198960373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1990e22bee78STejun Heo return ret; 1991e22bee78STejun Heo 199211ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1993e22bee78STejun Heo 1994e22bee78STejun Heo /* 1995e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1996e22bee78STejun Heo * on return. 1997e22bee78STejun Heo */ 199863d95a91STejun Heo ret |= maybe_destroy_workers(pool); 199963d95a91STejun Heo ret |= maybe_create_worker(pool); 2000e22bee78STejun Heo 200160373152STejun Heo mutex_unlock(&pool->manager_mutex); 2002e22bee78STejun Heo return ret; 2003e22bee78STejun Heo } 2004e22bee78STejun Heo 2005a62428c0STejun Heo /** 2006a62428c0STejun Heo * process_one_work - process single work 2007c34056a3STejun Heo * @worker: self 2008a62428c0STejun Heo * @work: work to process 2009a62428c0STejun Heo * 2010a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2011a62428c0STejun Heo * process a single work including synchronization against and 2012a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2013a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2014a62428c0STejun Heo * call this function to process a work. 2015a62428c0STejun Heo * 2016a62428c0STejun Heo * CONTEXT: 20178b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 2018a62428c0STejun Heo */ 2019c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 202006bd6ebfSNamhyung Kim __releases(&gcwq->lock) 202106bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 20221da177e4SLinus Torvalds { 20237e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 2024bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2025bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2026c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 2027fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 20286bb49e59SDavid Howells work_func_t f = work->func; 202973f53c4aSTejun Heo int work_color; 20307e11629dSTejun Heo struct worker *collision; 20314e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20324e6045f1SJohannes Berg /* 2033a62428c0STejun Heo * It is permissible to free the struct work_struct from 2034a62428c0STejun Heo * inside the function that is called from it, this we need to 2035a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2036a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2037a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20384e6045f1SJohannes Berg */ 20394d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20404d82a1deSPeter Zijlstra 20414d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20424e6045f1SJohannes Berg #endif 20436fec10a1STejun Heo /* 20446fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20456fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 20466fec10a1STejun Heo * unbound or a disassociated gcwq. 20476fec10a1STejun Heo */ 204825511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 20496fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 205025511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 205125511a47STejun Heo 20527e11629dSTejun Heo /* 20537e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20547e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20557e11629dSTejun Heo * already processing the work. If so, defer the work to the 20567e11629dSTejun Heo * currently executing one. 20577e11629dSTejun Heo */ 20587e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 20597e11629dSTejun Heo if (unlikely(collision)) { 20607e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20617e11629dSTejun Heo return; 20627e11629dSTejun Heo } 20631da177e4SLinus Torvalds 20648930cabaSTejun Heo /* claim and dequeue */ 20651da177e4SLinus Torvalds debug_work_deactivate(work); 2066c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 2067c34056a3STejun Heo worker->current_work = work; 20688cca0eeaSTejun Heo worker->current_cwq = cwq; 206973f53c4aSTejun Heo work_color = get_work_color(work); 20707a22ad75STejun Heo 2071a62428c0STejun Heo list_del_init(&work->entry); 2072a62428c0STejun Heo 2073649027d7STejun Heo /* 2074fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2075fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2076fb0e7bebSTejun Heo */ 2077fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2078fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2079fb0e7bebSTejun Heo 2080974271c4STejun Heo /* 2081974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 2082974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2083974271c4STejun Heo */ 208463d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 208563d95a91STejun Heo wake_up_worker(pool); 2086974271c4STejun Heo 20878930cabaSTejun Heo /* 20888930cabaSTejun Heo * Record the last CPU and clear PENDING. The following wmb is 20898930cabaSTejun Heo * paired with the implied mb in test_and_set_bit(PENDING) and 20908930cabaSTejun Heo * ensures all updates to @work made here are visible to and 20918930cabaSTejun Heo * precede any updates by the next PENDING owner. Also, clear 20928930cabaSTejun Heo * PENDING inside @gcwq->lock so that PENDING and queued state 20938930cabaSTejun Heo * changes happen together while IRQ is disabled. 20948930cabaSTejun Heo */ 20958930cabaSTejun Heo smp_wmb(); 20968930cabaSTejun Heo set_work_cpu_and_clear_pending(work, gcwq->cpu); 20971da177e4SLinus Torvalds 20988930cabaSTejun Heo spin_unlock_irq(&gcwq->lock); 2099959d1af8STejun Heo 2100e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 21013295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2102e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 210365f27f38SDavid Howells f(work); 2104e36c886aSArjan van de Ven /* 2105e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2106e36c886aSArjan van de Ven * point will only record its address. 2107e36c886aSArjan van de Ven */ 2108e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21093295f0efSIngo Molnar lock_map_release(&lockdep_map); 21103295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 21111da177e4SLinus Torvalds 2112d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2113d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2114d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2115a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2116d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2117d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2118d5abe669SPeter Zijlstra debug_show_held_locks(current); 2119d5abe669SPeter Zijlstra dump_stack(); 2120d5abe669SPeter Zijlstra } 2121d5abe669SPeter Zijlstra 21228b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2123a62428c0STejun Heo 2124fb0e7bebSTejun Heo /* clear cpu intensive status */ 2125fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2126fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2127fb0e7bebSTejun Heo 2128a62428c0STejun Heo /* we're done with it, release */ 2129c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2130c34056a3STejun Heo worker->current_work = NULL; 21318cca0eeaSTejun Heo worker->current_cwq = NULL; 21328a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 2135affee4b2STejun Heo /** 2136affee4b2STejun Heo * process_scheduled_works - process scheduled works 2137affee4b2STejun Heo * @worker: self 2138affee4b2STejun Heo * 2139affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2140affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2141affee4b2STejun Heo * fetches a work from the top and executes it. 2142affee4b2STejun Heo * 2143affee4b2STejun Heo * CONTEXT: 21448b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2145affee4b2STejun Heo * multiple times. 2146affee4b2STejun Heo */ 2147affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21481da177e4SLinus Torvalds { 2149affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2150affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2151a62428c0STejun Heo struct work_struct, entry); 2152c34056a3STejun Heo process_one_work(worker, work); 2153a62428c0STejun Heo } 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21564690c4abSTejun Heo /** 21574690c4abSTejun Heo * worker_thread - the worker thread function 2158c34056a3STejun Heo * @__worker: self 21594690c4abSTejun Heo * 2160e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2161e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2162e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2163e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2164e22bee78STejun Heo * rescuer_thread(). 21654690c4abSTejun Heo */ 2166c34056a3STejun Heo static int worker_thread(void *__worker) 21671da177e4SLinus Torvalds { 2168c34056a3STejun Heo struct worker *worker = __worker; 2169bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2170bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 21711da177e4SLinus Torvalds 2172e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2173e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2174c8e55f36STejun Heo woke_up: 21758b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2176affee4b2STejun Heo 217725511a47STejun Heo /* 217825511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 217925511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 218025511a47STejun Heo */ 218125511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2182c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 218325511a47STejun Heo 218425511a47STejun Heo if (worker->flags & WORKER_DIE) { 2185e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2186c8e55f36STejun Heo return 0; 2187c8e55f36STejun Heo } 2188c8e55f36STejun Heo 218925511a47STejun Heo idle_worker_rebind(worker); 219025511a47STejun Heo goto woke_up; 219125511a47STejun Heo } 219225511a47STejun Heo 2193c8e55f36STejun Heo worker_leave_idle(worker); 2194db7bccf4STejun Heo recheck: 2195e22bee78STejun Heo /* no more worker necessary? */ 219663d95a91STejun Heo if (!need_more_worker(pool)) 2197e22bee78STejun Heo goto sleep; 2198e22bee78STejun Heo 2199e22bee78STejun Heo /* do we need to manage? */ 220063d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2201e22bee78STejun Heo goto recheck; 2202e22bee78STejun Heo 2203c8e55f36STejun Heo /* 2204c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2205c8e55f36STejun Heo * preparing to process a work or actually processing it. 2206c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2207c8e55f36STejun Heo */ 2208c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2209c8e55f36STejun Heo 2210e22bee78STejun Heo /* 2211e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2212e22bee78STejun Heo * at least one idle worker or that someone else has already 2213e22bee78STejun Heo * assumed the manager role. 2214e22bee78STejun Heo */ 2215e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2216e22bee78STejun Heo 2217e22bee78STejun Heo do { 2218affee4b2STejun Heo struct work_struct *work = 2219bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2220affee4b2STejun Heo struct work_struct, entry); 2221affee4b2STejun Heo 2222c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2223affee4b2STejun Heo /* optimization path, not strictly necessary */ 2224affee4b2STejun Heo process_one_work(worker, work); 2225affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2226affee4b2STejun Heo process_scheduled_works(worker); 2227affee4b2STejun Heo } else { 2228c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2229affee4b2STejun Heo process_scheduled_works(worker); 2230affee4b2STejun Heo } 223163d95a91STejun Heo } while (keep_working(pool)); 2232affee4b2STejun Heo 2233e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2234d313dd85STejun Heo sleep: 223563d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2236e22bee78STejun Heo goto recheck; 2237d313dd85STejun Heo 2238c8e55f36STejun Heo /* 2239e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2240e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2241e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2242e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2243e22bee78STejun Heo * prevent losing any event. 2244c8e55f36STejun Heo */ 2245c8e55f36STejun Heo worker_enter_idle(worker); 2246c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 22478b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 22481da177e4SLinus Torvalds schedule(); 2249c8e55f36STejun Heo goto woke_up; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 2252e22bee78STejun Heo /** 2253e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2254e22bee78STejun Heo * @__wq: the associated workqueue 2255e22bee78STejun Heo * 2256e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2257e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2258e22bee78STejun Heo * 2259e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2260e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2261e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2262e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2263e22bee78STejun Heo * the problem rescuer solves. 2264e22bee78STejun Heo * 2265e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2266e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2267e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2268e22bee78STejun Heo * 2269e22bee78STejun Heo * This should happen rarely. 2270e22bee78STejun Heo */ 2271e22bee78STejun Heo static int rescuer_thread(void *__wq) 2272e22bee78STejun Heo { 2273e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2274e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2275e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2276f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2277e22bee78STejun Heo unsigned int cpu; 2278e22bee78STejun Heo 2279e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2280e22bee78STejun Heo repeat: 2281e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds if (kthread_should_stop()) 2284e22bee78STejun Heo return 0; 22851da177e4SLinus Torvalds 2286f3421797STejun Heo /* 2287f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2288f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2289f3421797STejun Heo */ 2290f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2291f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2292f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2293bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2294bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2295e22bee78STejun Heo struct work_struct *work, *n; 2296e22bee78STejun Heo 2297e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2298f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2299e22bee78STejun Heo 2300e22bee78STejun Heo /* migrate to the target cpu if possible */ 2301bd7bdd43STejun Heo rescuer->pool = pool; 2302e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2303e22bee78STejun Heo 2304e22bee78STejun Heo /* 2305e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2306e22bee78STejun Heo * process'em. 2307e22bee78STejun Heo */ 2308e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2309bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2310e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2311e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2312e22bee78STejun Heo 2313e22bee78STejun Heo process_scheduled_works(rescuer); 23147576958aSTejun Heo 23157576958aSTejun Heo /* 23167576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 23177576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23187576958aSTejun Heo * and stalling the execution. 23197576958aSTejun Heo */ 232063d95a91STejun Heo if (keep_working(pool)) 232163d95a91STejun Heo wake_up_worker(pool); 23227576958aSTejun Heo 2323e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 23241da177e4SLinus Torvalds } 23251da177e4SLinus Torvalds 2326e22bee78STejun Heo schedule(); 2327e22bee78STejun Heo goto repeat; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 2330fc2e4d70SOleg Nesterov struct wq_barrier { 2331fc2e4d70SOleg Nesterov struct work_struct work; 2332fc2e4d70SOleg Nesterov struct completion done; 2333fc2e4d70SOleg Nesterov }; 2334fc2e4d70SOleg Nesterov 2335fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2336fc2e4d70SOleg Nesterov { 2337fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2338fc2e4d70SOleg Nesterov complete(&barr->done); 2339fc2e4d70SOleg Nesterov } 2340fc2e4d70SOleg Nesterov 23414690c4abSTejun Heo /** 23424690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 23434690c4abSTejun Heo * @cwq: cwq to insert barrier into 23444690c4abSTejun Heo * @barr: wq_barrier to insert 2345affee4b2STejun Heo * @target: target work to attach @barr to 2346affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23474690c4abSTejun Heo * 2348affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2349affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2350affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2351affee4b2STejun Heo * cpu. 2352affee4b2STejun Heo * 2353affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2354affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2355affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2356affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2357affee4b2STejun Heo * after a work with LINKED flag set. 2358affee4b2STejun Heo * 2359affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2360affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 23614690c4abSTejun Heo * 23624690c4abSTejun Heo * CONTEXT: 23638b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 23644690c4abSTejun Heo */ 236583c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2366affee4b2STejun Heo struct wq_barrier *barr, 2367affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2368fc2e4d70SOleg Nesterov { 2369affee4b2STejun Heo struct list_head *head; 2370affee4b2STejun Heo unsigned int linked = 0; 2371affee4b2STejun Heo 2372dc186ad7SThomas Gleixner /* 23738b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2374dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2375dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2376dc186ad7SThomas Gleixner * might deadlock. 2377dc186ad7SThomas Gleixner */ 2378ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 237922df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2380fc2e4d70SOleg Nesterov init_completion(&barr->done); 238183c22520SOleg Nesterov 2382affee4b2STejun Heo /* 2383affee4b2STejun Heo * If @target is currently being executed, schedule the 2384affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2385affee4b2STejun Heo */ 2386affee4b2STejun Heo if (worker) 2387affee4b2STejun Heo head = worker->scheduled.next; 2388affee4b2STejun Heo else { 2389affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2390affee4b2STejun Heo 2391affee4b2STejun Heo head = target->entry.next; 2392affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2393affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2394affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2395affee4b2STejun Heo } 2396affee4b2STejun Heo 2397dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2398affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2399affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2400fc2e4d70SOleg Nesterov } 2401fc2e4d70SOleg Nesterov 240273f53c4aSTejun Heo /** 240373f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 240473f53c4aSTejun Heo * @wq: workqueue being flushed 240573f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 240673f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 240773f53c4aSTejun Heo * 240873f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 240973f53c4aSTejun Heo * 241073f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 241173f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 241273f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 241373f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 241473f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 241573f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 241673f53c4aSTejun Heo * 241773f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 241873f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 241973f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 242073f53c4aSTejun Heo * is returned. 242173f53c4aSTejun Heo * 242273f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 242373f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 242473f53c4aSTejun Heo * advanced to @work_color. 242573f53c4aSTejun Heo * 242673f53c4aSTejun Heo * CONTEXT: 242773f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 242873f53c4aSTejun Heo * 242973f53c4aSTejun Heo * RETURNS: 243073f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 243173f53c4aSTejun Heo * otherwise. 243273f53c4aSTejun Heo */ 243373f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 243473f53c4aSTejun Heo int flush_color, int work_color) 24351da177e4SLinus Torvalds { 243673f53c4aSTejun Heo bool wait = false; 243773f53c4aSTejun Heo unsigned int cpu; 24381da177e4SLinus Torvalds 243973f53c4aSTejun Heo if (flush_color >= 0) { 244073f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 244173f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2442dc186ad7SThomas Gleixner } 244314441960SOleg Nesterov 2444f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 244573f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2446bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 24471da177e4SLinus Torvalds 24488b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 244973f53c4aSTejun Heo 245073f53c4aSTejun Heo if (flush_color >= 0) { 245173f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 245273f53c4aSTejun Heo 245373f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 245473f53c4aSTejun Heo cwq->flush_color = flush_color; 245573f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 245673f53c4aSTejun Heo wait = true; 24571da177e4SLinus Torvalds } 245873f53c4aSTejun Heo } 245973f53c4aSTejun Heo 246073f53c4aSTejun Heo if (work_color >= 0) { 246173f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 246273f53c4aSTejun Heo cwq->work_color = work_color; 246373f53c4aSTejun Heo } 246473f53c4aSTejun Heo 24658b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 246873f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 246973f53c4aSTejun Heo complete(&wq->first_flusher->done); 247073f53c4aSTejun Heo 247173f53c4aSTejun Heo return wait; 247283c22520SOleg Nesterov } 24731da177e4SLinus Torvalds 24740fcb78c2SRolf Eike Beer /** 24751da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 24760fcb78c2SRolf Eike Beer * @wq: workqueue to flush 24771da177e4SLinus Torvalds * 24781da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 24791da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 24801da177e4SLinus Torvalds * 2481fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2482fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 24831da177e4SLinus Torvalds */ 24847ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 24851da177e4SLinus Torvalds { 248673f53c4aSTejun Heo struct wq_flusher this_flusher = { 248773f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 248873f53c4aSTejun Heo .flush_color = -1, 248973f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 249073f53c4aSTejun Heo }; 249173f53c4aSTejun Heo int next_color; 2492b1f4ec17SOleg Nesterov 24933295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24943295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 249573f53c4aSTejun Heo 249673f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 249773f53c4aSTejun Heo 249873f53c4aSTejun Heo /* 249973f53c4aSTejun Heo * Start-to-wait phase 250073f53c4aSTejun Heo */ 250173f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 250273f53c4aSTejun Heo 250373f53c4aSTejun Heo if (next_color != wq->flush_color) { 250473f53c4aSTejun Heo /* 250573f53c4aSTejun Heo * Color space is not full. The current work_color 250673f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 250773f53c4aSTejun Heo * by one. 250873f53c4aSTejun Heo */ 250973f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 251073f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 251173f53c4aSTejun Heo wq->work_color = next_color; 251273f53c4aSTejun Heo 251373f53c4aSTejun Heo if (!wq->first_flusher) { 251473f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 251573f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 251673f53c4aSTejun Heo 251773f53c4aSTejun Heo wq->first_flusher = &this_flusher; 251873f53c4aSTejun Heo 251973f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 252073f53c4aSTejun Heo wq->work_color)) { 252173f53c4aSTejun Heo /* nothing to flush, done */ 252273f53c4aSTejun Heo wq->flush_color = next_color; 252373f53c4aSTejun Heo wq->first_flusher = NULL; 252473f53c4aSTejun Heo goto out_unlock; 252573f53c4aSTejun Heo } 252673f53c4aSTejun Heo } else { 252773f53c4aSTejun Heo /* wait in queue */ 252873f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 252973f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 253073f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 253173f53c4aSTejun Heo } 253273f53c4aSTejun Heo } else { 253373f53c4aSTejun Heo /* 253473f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 253573f53c4aSTejun Heo * The next flush completion will assign us 253673f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 253773f53c4aSTejun Heo */ 253873f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 253973f53c4aSTejun Heo } 254073f53c4aSTejun Heo 254173f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 254273f53c4aSTejun Heo 254373f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 254473f53c4aSTejun Heo 254573f53c4aSTejun Heo /* 254673f53c4aSTejun Heo * Wake-up-and-cascade phase 254773f53c4aSTejun Heo * 254873f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 254973f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 255073f53c4aSTejun Heo */ 255173f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 255273f53c4aSTejun Heo return; 255373f53c4aSTejun Heo 255473f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 255573f53c4aSTejun Heo 25564ce48b37STejun Heo /* we might have raced, check again with mutex held */ 25574ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 25584ce48b37STejun Heo goto out_unlock; 25594ce48b37STejun Heo 256073f53c4aSTejun Heo wq->first_flusher = NULL; 256173f53c4aSTejun Heo 256273f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 256373f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 256473f53c4aSTejun Heo 256573f53c4aSTejun Heo while (true) { 256673f53c4aSTejun Heo struct wq_flusher *next, *tmp; 256773f53c4aSTejun Heo 256873f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 256973f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 257073f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 257173f53c4aSTejun Heo break; 257273f53c4aSTejun Heo list_del_init(&next->list); 257373f53c4aSTejun Heo complete(&next->done); 257473f53c4aSTejun Heo } 257573f53c4aSTejun Heo 257673f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 257773f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 257873f53c4aSTejun Heo 257973f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 258073f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 258173f53c4aSTejun Heo 258273f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 258373f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 258473f53c4aSTejun Heo /* 258573f53c4aSTejun Heo * Assign the same color to all overflowed 258673f53c4aSTejun Heo * flushers, advance work_color and append to 258773f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 258873f53c4aSTejun Heo * phase for these overflowed flushers. 258973f53c4aSTejun Heo */ 259073f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 259173f53c4aSTejun Heo tmp->flush_color = wq->work_color; 259273f53c4aSTejun Heo 259373f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 259473f53c4aSTejun Heo 259573f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 259673f53c4aSTejun Heo &wq->flusher_queue); 259773f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 259873f53c4aSTejun Heo } 259973f53c4aSTejun Heo 260073f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 260173f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 260273f53c4aSTejun Heo break; 260373f53c4aSTejun Heo } 260473f53c4aSTejun Heo 260573f53c4aSTejun Heo /* 260673f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 260773f53c4aSTejun Heo * the new first flusher and arm cwqs. 260873f53c4aSTejun Heo */ 260973f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 261073f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 261173f53c4aSTejun Heo 261273f53c4aSTejun Heo list_del_init(&next->list); 261373f53c4aSTejun Heo wq->first_flusher = next; 261473f53c4aSTejun Heo 261573f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 261673f53c4aSTejun Heo break; 261773f53c4aSTejun Heo 261873f53c4aSTejun Heo /* 261973f53c4aSTejun Heo * Meh... this color is already done, clear first 262073f53c4aSTejun Heo * flusher and repeat cascading. 262173f53c4aSTejun Heo */ 262273f53c4aSTejun Heo wq->first_flusher = NULL; 262373f53c4aSTejun Heo } 262473f53c4aSTejun Heo 262573f53c4aSTejun Heo out_unlock: 262673f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26271da177e4SLinus Torvalds } 2628ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26291da177e4SLinus Torvalds 26309c5a2ba7STejun Heo /** 26319c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26329c5a2ba7STejun Heo * @wq: workqueue to drain 26339c5a2ba7STejun Heo * 26349c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26359c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26369c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26379c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26389c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26399c5a2ba7STejun Heo * takes too long. 26409c5a2ba7STejun Heo */ 26419c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26429c5a2ba7STejun Heo { 26439c5a2ba7STejun Heo unsigned int flush_cnt = 0; 26449c5a2ba7STejun Heo unsigned int cpu; 26459c5a2ba7STejun Heo 26469c5a2ba7STejun Heo /* 26479c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26489c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 26499c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 26509c5a2ba7STejun Heo */ 26519c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26529c5a2ba7STejun Heo if (!wq->nr_drainers++) 26539c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 26549c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26559c5a2ba7STejun Heo reflush: 26569c5a2ba7STejun Heo flush_workqueue(wq); 26579c5a2ba7STejun Heo 26589c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 26599c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2660fa2563e4SThomas Tuttle bool drained; 26619c5a2ba7STejun Heo 2662bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2663fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2664bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2665fa2563e4SThomas Tuttle 2666fa2563e4SThomas Tuttle if (drained) 26679c5a2ba7STejun Heo continue; 26689c5a2ba7STejun Heo 26699c5a2ba7STejun Heo if (++flush_cnt == 10 || 26709c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 26719c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 26729c5a2ba7STejun Heo wq->name, flush_cnt); 26739c5a2ba7STejun Heo goto reflush; 26749c5a2ba7STejun Heo } 26759c5a2ba7STejun Heo 26769c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26779c5a2ba7STejun Heo if (!--wq->nr_drainers) 26789c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 26799c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26809c5a2ba7STejun Heo } 26819c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 26829c5a2ba7STejun Heo 2683baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2684baf59022STejun Heo bool wait_executing) 2685baf59022STejun Heo { 2686baf59022STejun Heo struct worker *worker = NULL; 2687baf59022STejun Heo struct global_cwq *gcwq; 2688baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2689baf59022STejun Heo 2690baf59022STejun Heo might_sleep(); 2691baf59022STejun Heo gcwq = get_work_gcwq(work); 2692baf59022STejun Heo if (!gcwq) 2693baf59022STejun Heo return false; 2694baf59022STejun Heo 2695baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2696baf59022STejun Heo if (!list_empty(&work->entry)) { 2697baf59022STejun Heo /* 2698baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2699baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2700baf59022STejun Heo * are not going to wait. 2701baf59022STejun Heo */ 2702baf59022STejun Heo smp_rmb(); 2703baf59022STejun Heo cwq = get_work_cwq(work); 2704bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2705baf59022STejun Heo goto already_gone; 2706baf59022STejun Heo } else if (wait_executing) { 2707baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2708baf59022STejun Heo if (!worker) 2709baf59022STejun Heo goto already_gone; 2710baf59022STejun Heo cwq = worker->current_cwq; 2711baf59022STejun Heo } else 2712baf59022STejun Heo goto already_gone; 2713baf59022STejun Heo 2714baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2715baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2716baf59022STejun Heo 2717e159489bSTejun Heo /* 2718e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2719e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2720e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2721e159489bSTejun Heo * access. 2722e159489bSTejun Heo */ 2723e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2724baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2725e159489bSTejun Heo else 2726e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2727baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2728e159489bSTejun Heo 2729baf59022STejun Heo return true; 2730baf59022STejun Heo already_gone: 2731baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2732baf59022STejun Heo return false; 2733baf59022STejun Heo } 2734baf59022STejun Heo 2735db700897SOleg Nesterov /** 2736401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2737401a8d04STejun Heo * @work: the work to flush 2738db700897SOleg Nesterov * 2739401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2740401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2741401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2742401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2743401a8d04STejun Heo * some of the CPUs from earlier queueing. 2744a67da70dSOleg Nesterov * 2745401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2746401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2747401a8d04STejun Heo * been requeued since flush started. 2748401a8d04STejun Heo * 2749401a8d04STejun Heo * RETURNS: 2750401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2751401a8d04STejun Heo * %false if it was already idle. 2752db700897SOleg Nesterov */ 2753401a8d04STejun Heo bool flush_work(struct work_struct *work) 2754db700897SOleg Nesterov { 2755db700897SOleg Nesterov struct wq_barrier barr; 2756db700897SOleg Nesterov 27570976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 27580976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 27590976dfc1SStephen Boyd 2760baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2761db700897SOleg Nesterov wait_for_completion(&barr.done); 2762dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2763401a8d04STejun Heo return true; 2764baf59022STejun Heo } else 2765401a8d04STejun Heo return false; 2766db700897SOleg Nesterov } 2767db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2768db700897SOleg Nesterov 2769401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2770401a8d04STejun Heo { 2771401a8d04STejun Heo struct wq_barrier barr; 2772401a8d04STejun Heo struct worker *worker; 2773401a8d04STejun Heo 2774401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2775401a8d04STejun Heo 2776401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2777401a8d04STejun Heo if (unlikely(worker)) 2778401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2779401a8d04STejun Heo 2780401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2781401a8d04STejun Heo 2782401a8d04STejun Heo if (unlikely(worker)) { 2783401a8d04STejun Heo wait_for_completion(&barr.done); 2784401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2785401a8d04STejun Heo return true; 2786401a8d04STejun Heo } else 2787401a8d04STejun Heo return false; 2788401a8d04STejun Heo } 2789401a8d04STejun Heo 2790401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2791401a8d04STejun Heo { 2792401a8d04STejun Heo bool ret = false; 2793401a8d04STejun Heo int cpu; 2794401a8d04STejun Heo 2795401a8d04STejun Heo might_sleep(); 2796401a8d04STejun Heo 2797401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2798401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2799401a8d04STejun Heo 2800401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2801401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2802401a8d04STejun Heo return ret; 2803401a8d04STejun Heo } 2804401a8d04STejun Heo 280509383498STejun Heo /** 280609383498STejun Heo * flush_work_sync - wait until a work has finished execution 280709383498STejun Heo * @work: the work to flush 280809383498STejun Heo * 280909383498STejun Heo * Wait until @work has finished execution. On return, it's 281009383498STejun Heo * guaranteed that all queueing instances of @work which happened 281109383498STejun Heo * before this function is called are finished. In other words, if 281209383498STejun Heo * @work hasn't been requeued since this function was called, @work is 281309383498STejun Heo * guaranteed to be idle on return. 281409383498STejun Heo * 281509383498STejun Heo * RETURNS: 281609383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 281709383498STejun Heo * %false if it was already idle. 281809383498STejun Heo */ 281909383498STejun Heo bool flush_work_sync(struct work_struct *work) 282009383498STejun Heo { 282109383498STejun Heo struct wq_barrier barr; 282209383498STejun Heo bool pending, waited; 282309383498STejun Heo 282409383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 282509383498STejun Heo pending = start_flush_work(work, &barr, false); 282609383498STejun Heo 282709383498STejun Heo /* wait for executions to finish */ 282809383498STejun Heo waited = wait_on_work(work); 282909383498STejun Heo 283009383498STejun Heo /* wait for the pending one */ 283109383498STejun Heo if (pending) { 283209383498STejun Heo wait_for_completion(&barr.done); 283309383498STejun Heo destroy_work_on_stack(&barr.work); 283409383498STejun Heo } 283509383498STejun Heo 283609383498STejun Heo return pending || waited; 283709383498STejun Heo } 283809383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 283909383498STejun Heo 2840*36e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 28411f1f642eSOleg Nesterov { 28421f1f642eSOleg Nesterov int ret; 28431f1f642eSOleg Nesterov 28441f1f642eSOleg Nesterov do { 2845*36e227d2STejun Heo ret = try_to_grab_pending(work, is_dwork); 28461f1f642eSOleg Nesterov wait_on_work(work); 28471f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28481f1f642eSOleg Nesterov 28497a22ad75STejun Heo clear_work_data(work); 28501f1f642eSOleg Nesterov return ret; 28511f1f642eSOleg Nesterov } 28521f1f642eSOleg Nesterov 28536e84d644SOleg Nesterov /** 2854401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2855401a8d04STejun Heo * @work: the work to cancel 28566e84d644SOleg Nesterov * 2857401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2858401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2859401a8d04STejun Heo * another workqueue. On return from this function, @work is 2860401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28611f1f642eSOleg Nesterov * 2862401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2863401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28646e84d644SOleg Nesterov * 2865401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28666e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2867401a8d04STejun Heo * 2868401a8d04STejun Heo * RETURNS: 2869401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28706e84d644SOleg Nesterov */ 2871401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28726e84d644SOleg Nesterov { 2873*36e227d2STejun Heo return __cancel_work_timer(work, false); 2874b89deed3SOleg Nesterov } 287528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2876b89deed3SOleg Nesterov 28776e84d644SOleg Nesterov /** 2878401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2879401a8d04STejun Heo * @dwork: the delayed work to flush 28806e84d644SOleg Nesterov * 2881401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2882401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2883401a8d04STejun Heo * considers the last queueing instance of @dwork. 28841f1f642eSOleg Nesterov * 2885401a8d04STejun Heo * RETURNS: 2886401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2887401a8d04STejun Heo * %false if it was already idle. 28886e84d644SOleg Nesterov */ 2889401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2890401a8d04STejun Heo { 28918930cabaSTejun Heo local_irq_disable(); 2892401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 289357469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 2894401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 28958930cabaSTejun Heo local_irq_enable(); 2896401a8d04STejun Heo return flush_work(&dwork->work); 2897401a8d04STejun Heo } 2898401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2899401a8d04STejun Heo 2900401a8d04STejun Heo /** 290109383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 290209383498STejun Heo * @dwork: the delayed work to flush 290309383498STejun Heo * 290409383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 290509383498STejun Heo * execution immediately. Other than timer handling, its behavior 290609383498STejun Heo * is identical to flush_work_sync(). 290709383498STejun Heo * 290809383498STejun Heo * RETURNS: 290909383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 291009383498STejun Heo * %false if it was already idle. 291109383498STejun Heo */ 291209383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 291309383498STejun Heo { 29148930cabaSTejun Heo local_irq_disable(); 291509383498STejun Heo if (del_timer_sync(&dwork->timer)) 291657469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 291709383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 29188930cabaSTejun Heo local_irq_enable(); 291909383498STejun Heo return flush_work_sync(&dwork->work); 292009383498STejun Heo } 292109383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 292209383498STejun Heo 292309383498STejun Heo /** 2924401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2925401a8d04STejun Heo * @dwork: the delayed work cancel 2926401a8d04STejun Heo * 2927401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2928401a8d04STejun Heo * 2929401a8d04STejun Heo * RETURNS: 2930401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2931401a8d04STejun Heo */ 2932401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29336e84d644SOleg Nesterov { 2934*36e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29356e84d644SOleg Nesterov } 2936f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29371da177e4SLinus Torvalds 2938d4283e93STejun Heo /** 29390a13c00eSTejun Heo * schedule_work_on - put work task on a specific cpu 29400a13c00eSTejun Heo * @cpu: cpu to put the work task on 29410a13c00eSTejun Heo * @work: job to be done 29420a13c00eSTejun Heo * 29430a13c00eSTejun Heo * This puts a job on a specific cpu 29440a13c00eSTejun Heo */ 2945d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 29460a13c00eSTejun Heo { 29470a13c00eSTejun Heo return queue_work_on(cpu, system_wq, work); 29480a13c00eSTejun Heo } 29490a13c00eSTejun Heo EXPORT_SYMBOL(schedule_work_on); 29500a13c00eSTejun Heo 29510fcb78c2SRolf Eike Beer /** 29520fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 29530fcb78c2SRolf Eike Beer * @work: job to be done 29540fcb78c2SRolf Eike Beer * 2955d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2956d4283e93STejun Heo * %true otherwise. 29575b0f437dSBart Van Assche * 29585b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 29595b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 29605b0f437dSBart Van Assche * workqueue otherwise. 29610fcb78c2SRolf Eike Beer */ 2962d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29631da177e4SLinus Torvalds { 2964d320c038STejun Heo return queue_work(system_wq, work); 29651da177e4SLinus Torvalds } 2966ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 29671da177e4SLinus Torvalds 29680fcb78c2SRolf Eike Beer /** 29690fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29700fcb78c2SRolf Eike Beer * @cpu: cpu to use 297152bad64dSDavid Howells * @dwork: job to be done 29720fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29730fcb78c2SRolf Eike Beer * 29740fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29750fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29760fcb78c2SRolf Eike Beer */ 2977d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2978d4283e93STejun Heo unsigned long delay) 29791da177e4SLinus Torvalds { 2980d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29811da177e4SLinus Torvalds } 2982ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29831da177e4SLinus Torvalds 2984b6136773SAndrew Morton /** 29850a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29860a13c00eSTejun Heo * @dwork: job to be done 29870a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29880a13c00eSTejun Heo * 29890a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29900a13c00eSTejun Heo * workqueue. 29910a13c00eSTejun Heo */ 2992d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29930a13c00eSTejun Heo { 29940a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29950a13c00eSTejun Heo } 29960a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29970a13c00eSTejun Heo 29980a13c00eSTejun Heo /** 299931ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3000b6136773SAndrew Morton * @func: the function to call 3001b6136773SAndrew Morton * 300231ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 300331ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3004b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 300531ddd871STejun Heo * 300631ddd871STejun Heo * RETURNS: 300731ddd871STejun Heo * 0 on success, -errno on failure. 3008b6136773SAndrew Morton */ 300965f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 301015316ba8SChristoph Lameter { 301115316ba8SChristoph Lameter int cpu; 301238f51568SNamhyung Kim struct work_struct __percpu *works; 301315316ba8SChristoph Lameter 3014b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3015b6136773SAndrew Morton if (!works) 301615316ba8SChristoph Lameter return -ENOMEM; 3017b6136773SAndrew Morton 301895402b38SGautham R Shenoy get_online_cpus(); 301993981800STejun Heo 302015316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30219bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30229bfb1839SIngo Molnar 30239bfb1839SIngo Molnar INIT_WORK(work, func); 30248de6d308SOleg Nesterov schedule_work_on(cpu, work); 302515316ba8SChristoph Lameter } 302693981800STejun Heo 302793981800STejun Heo for_each_online_cpu(cpu) 30288616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 302993981800STejun Heo 303095402b38SGautham R Shenoy put_online_cpus(); 3031b6136773SAndrew Morton free_percpu(works); 303215316ba8SChristoph Lameter return 0; 303315316ba8SChristoph Lameter } 303415316ba8SChristoph Lameter 3035eef6a7d5SAlan Stern /** 3036eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3037eef6a7d5SAlan Stern * 3038eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3039eef6a7d5SAlan Stern * completion. 3040eef6a7d5SAlan Stern * 3041eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3042eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3043eef6a7d5SAlan Stern * will lead to deadlock: 3044eef6a7d5SAlan Stern * 3045eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3046eef6a7d5SAlan Stern * a lock held by your code or its caller. 3047eef6a7d5SAlan Stern * 3048eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3049eef6a7d5SAlan Stern * 3050eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3051eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3052eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3053eef6a7d5SAlan Stern * 3054eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3055eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3056eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3057eef6a7d5SAlan Stern * cancel_work_sync() instead. 3058eef6a7d5SAlan Stern */ 30591da177e4SLinus Torvalds void flush_scheduled_work(void) 30601da177e4SLinus Torvalds { 3061d320c038STejun Heo flush_workqueue(system_wq); 30621da177e4SLinus Torvalds } 3063ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds /** 30661fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30671fa44ecaSJames Bottomley * @fn: the function to execute 30681fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30691fa44ecaSJames Bottomley * be available when the work executes) 30701fa44ecaSJames Bottomley * 30711fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30721fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30731fa44ecaSJames Bottomley * 30741fa44ecaSJames Bottomley * Returns: 0 - function was executed 30751fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30761fa44ecaSJames Bottomley */ 307765f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30781fa44ecaSJames Bottomley { 30791fa44ecaSJames Bottomley if (!in_interrupt()) { 308065f27f38SDavid Howells fn(&ew->work); 30811fa44ecaSJames Bottomley return 0; 30821fa44ecaSJames Bottomley } 30831fa44ecaSJames Bottomley 308465f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30851fa44ecaSJames Bottomley schedule_work(&ew->work); 30861fa44ecaSJames Bottomley 30871fa44ecaSJames Bottomley return 1; 30881fa44ecaSJames Bottomley } 30891fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30901fa44ecaSJames Bottomley 30911da177e4SLinus Torvalds int keventd_up(void) 30921da177e4SLinus Torvalds { 3093d320c038STejun Heo return system_wq != NULL; 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 3096bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30971da177e4SLinus Torvalds { 30983af24433SOleg Nesterov /* 30990f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 31000f900049STejun Heo * Make sure that the alignment isn't lower than that of 31010f900049STejun Heo * unsigned long long. 31023af24433SOleg Nesterov */ 31030f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 31040f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 31050f900049STejun Heo __alignof__(unsigned long long)); 31063af24433SOleg Nesterov 3107e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3108f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3109931ac77eSTejun Heo else { 31100f900049STejun Heo void *ptr; 3111e1d8aa9fSFrederic Weisbecker 31120f900049STejun Heo /* 3113f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3114f3421797STejun Heo * pointer at the end pointing back to the originally 3115f3421797STejun Heo * allocated pointer which will be used for free. 31160f900049STejun Heo */ 3117bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3118bdbc5dd7STejun Heo if (ptr) { 3119bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3120bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3121bdbc5dd7STejun Heo } 31223af24433SOleg Nesterov } 31233af24433SOleg Nesterov 31240415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3125bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3126bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 31270f900049STejun Heo } 31280f900049STejun Heo 3129bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 313006ba38a9SOleg Nesterov { 3131e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3132bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3133f3421797STejun Heo else if (wq->cpu_wq.single) { 3134f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3135f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 313606ba38a9SOleg Nesterov } 313706ba38a9SOleg Nesterov } 313806ba38a9SOleg Nesterov 3139f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3140f3421797STejun Heo const char *name) 3141b71ab8c2STejun Heo { 3142f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3143f3421797STejun Heo 3144f3421797STejun Heo if (max_active < 1 || max_active > lim) 3145b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3146b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3147f3421797STejun Heo max_active, name, 1, lim); 3148b71ab8c2STejun Heo 3149f3421797STejun Heo return clamp_val(max_active, 1, lim); 3150b71ab8c2STejun Heo } 3151b71ab8c2STejun Heo 3152b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 315397e37d7bSTejun Heo unsigned int flags, 31541e19ffc6STejun Heo int max_active, 3155eb13ba87SJohannes Berg struct lock_class_key *key, 3156b196be89STejun Heo const char *lock_name, ...) 31573af24433SOleg Nesterov { 3158b196be89STejun Heo va_list args, args1; 31593af24433SOleg Nesterov struct workqueue_struct *wq; 3160c34056a3STejun Heo unsigned int cpu; 3161b196be89STejun Heo size_t namelen; 3162b196be89STejun Heo 3163b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3164b196be89STejun Heo va_start(args, lock_name); 3165b196be89STejun Heo va_copy(args1, args); 3166b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3167b196be89STejun Heo 3168b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3169b196be89STejun Heo if (!wq) 3170b196be89STejun Heo goto err; 3171b196be89STejun Heo 3172b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3173b196be89STejun Heo va_end(args); 3174b196be89STejun Heo va_end(args1); 31753af24433SOleg Nesterov 3176f3421797STejun Heo /* 31776370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31786370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31796370a6adSTejun Heo */ 31806370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31816370a6adSTejun Heo flags |= WQ_RESCUER; 31826370a6adSTejun Heo 3183d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3184b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31853af24433SOleg Nesterov 3186b196be89STejun Heo /* init wq */ 318797e37d7bSTejun Heo wq->flags = flags; 3188a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 318973f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 319073f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 319173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 319273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31933af24433SOleg Nesterov 3194eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3195cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31963af24433SOleg Nesterov 3197bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3198bdbc5dd7STejun Heo goto err; 3199bdbc5dd7STejun Heo 3200f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 32011537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 32028b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 32033270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 32041537663fSTejun Heo 32050f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 32063270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3207c34056a3STejun Heo cwq->wq = wq; 320873f53c4aSTejun Heo cwq->flush_color = -1; 32091e19ffc6STejun Heo cwq->max_active = max_active; 32101e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3211e22bee78STejun Heo } 32121537663fSTejun Heo 3213e22bee78STejun Heo if (flags & WQ_RESCUER) { 3214e22bee78STejun Heo struct worker *rescuer; 3215e22bee78STejun Heo 3216f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3217e22bee78STejun Heo goto err; 3218e22bee78STejun Heo 3219e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3220e22bee78STejun Heo if (!rescuer) 3221e22bee78STejun Heo goto err; 3222e22bee78STejun Heo 3223b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3224b196be89STejun Heo wq->name); 3225e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3226e22bee78STejun Heo goto err; 3227e22bee78STejun Heo 3228e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3229e22bee78STejun Heo wake_up_process(rescuer->task); 32303af24433SOleg Nesterov } 32311537663fSTejun Heo 32323af24433SOleg Nesterov /* 3233a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3234a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3235a0a1a5fdSTejun Heo * workqueue to workqueues list. 32363af24433SOleg Nesterov */ 32373af24433SOleg Nesterov spin_lock(&workqueue_lock); 3238a0a1a5fdSTejun Heo 323958a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3240f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3241a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3242a0a1a5fdSTejun Heo 32433af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3244a0a1a5fdSTejun Heo 32453af24433SOleg Nesterov spin_unlock(&workqueue_lock); 32463af24433SOleg Nesterov 32473af24433SOleg Nesterov return wq; 32484690c4abSTejun Heo err: 32494690c4abSTejun Heo if (wq) { 3250bdbc5dd7STejun Heo free_cwqs(wq); 3251f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3252e22bee78STejun Heo kfree(wq->rescuer); 32534690c4abSTejun Heo kfree(wq); 32543af24433SOleg Nesterov } 32554690c4abSTejun Heo return NULL; 32561da177e4SLinus Torvalds } 3257d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32581da177e4SLinus Torvalds 32593af24433SOleg Nesterov /** 32603af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32613af24433SOleg Nesterov * @wq: target workqueue 32623af24433SOleg Nesterov * 32633af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32643af24433SOleg Nesterov */ 32653af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32663af24433SOleg Nesterov { 3267c8e55f36STejun Heo unsigned int cpu; 32683af24433SOleg Nesterov 32699c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32709c5a2ba7STejun Heo drain_workqueue(wq); 3271c8efcc25STejun Heo 3272a0a1a5fdSTejun Heo /* 3273a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3274a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3275a0a1a5fdSTejun Heo */ 327695402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32773af24433SOleg Nesterov list_del(&wq->list); 327895402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32793af24433SOleg Nesterov 3280e22bee78STejun Heo /* sanity check */ 3281f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 328273f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 328373f53c4aSTejun Heo int i; 32843af24433SOleg Nesterov 328573f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 328673f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32871e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32881e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 328973f53c4aSTejun Heo } 32901537663fSTejun Heo 3291e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3292e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3293f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32948d9df9f0SXiaotian Feng kfree(wq->rescuer); 3295e22bee78STejun Heo } 3296e22bee78STejun Heo 3297bdbc5dd7STejun Heo free_cwqs(wq); 32983af24433SOleg Nesterov kfree(wq); 32993af24433SOleg Nesterov } 33003af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 33013af24433SOleg Nesterov 3302dcd989cbSTejun Heo /** 3303dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3304dcd989cbSTejun Heo * @wq: target workqueue 3305dcd989cbSTejun Heo * @max_active: new max_active value. 3306dcd989cbSTejun Heo * 3307dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3308dcd989cbSTejun Heo * 3309dcd989cbSTejun Heo * CONTEXT: 3310dcd989cbSTejun Heo * Don't call from IRQ context. 3311dcd989cbSTejun Heo */ 3312dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3313dcd989cbSTejun Heo { 3314dcd989cbSTejun Heo unsigned int cpu; 3315dcd989cbSTejun Heo 3316f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3317dcd989cbSTejun Heo 3318dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3319dcd989cbSTejun Heo 3320dcd989cbSTejun Heo wq->saved_max_active = max_active; 3321dcd989cbSTejun Heo 3322f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3323dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3324dcd989cbSTejun Heo 3325dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3326dcd989cbSTejun Heo 332758a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3328dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3329dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3330dcd989cbSTejun Heo 3331dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3332dcd989cbSTejun Heo } 3333dcd989cbSTejun Heo 3334dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3335dcd989cbSTejun Heo } 3336dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3337dcd989cbSTejun Heo 3338dcd989cbSTejun Heo /** 3339dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3340dcd989cbSTejun Heo * @cpu: CPU in question 3341dcd989cbSTejun Heo * @wq: target workqueue 3342dcd989cbSTejun Heo * 3343dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3344dcd989cbSTejun Heo * no synchronization around this function and the test result is 3345dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3346dcd989cbSTejun Heo * 3347dcd989cbSTejun Heo * RETURNS: 3348dcd989cbSTejun Heo * %true if congested, %false otherwise. 3349dcd989cbSTejun Heo */ 3350dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3351dcd989cbSTejun Heo { 3352dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3353dcd989cbSTejun Heo 3354dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3355dcd989cbSTejun Heo } 3356dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3357dcd989cbSTejun Heo 3358dcd989cbSTejun Heo /** 3359dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3360dcd989cbSTejun Heo * @work: the work of interest 3361dcd989cbSTejun Heo * 3362dcd989cbSTejun Heo * RETURNS: 3363bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3364dcd989cbSTejun Heo */ 3365dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3366dcd989cbSTejun Heo { 3367dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3368dcd989cbSTejun Heo 3369bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3370dcd989cbSTejun Heo } 3371dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3372dcd989cbSTejun Heo 3373dcd989cbSTejun Heo /** 3374dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3375dcd989cbSTejun Heo * @work: the work to be tested 3376dcd989cbSTejun Heo * 3377dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3378dcd989cbSTejun Heo * synchronization around this function and the test result is 3379dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3380dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3381dcd989cbSTejun Heo * running state. 3382dcd989cbSTejun Heo * 3383dcd989cbSTejun Heo * RETURNS: 3384dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3385dcd989cbSTejun Heo */ 3386dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3387dcd989cbSTejun Heo { 3388dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3389dcd989cbSTejun Heo unsigned long flags; 3390dcd989cbSTejun Heo unsigned int ret = 0; 3391dcd989cbSTejun Heo 3392dcd989cbSTejun Heo if (!gcwq) 3393dcd989cbSTejun Heo return false; 3394dcd989cbSTejun Heo 3395dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3396dcd989cbSTejun Heo 3397dcd989cbSTejun Heo if (work_pending(work)) 3398dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3399dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3400dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3401dcd989cbSTejun Heo 3402dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3403dcd989cbSTejun Heo 3404dcd989cbSTejun Heo return ret; 3405dcd989cbSTejun Heo } 3406dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3407dcd989cbSTejun Heo 3408db7bccf4STejun Heo /* 3409db7bccf4STejun Heo * CPU hotplug. 3410db7bccf4STejun Heo * 3411e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3412e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3413e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3414e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3415e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3416e22bee78STejun Heo * blocked draining impractical. 3417e22bee78STejun Heo * 3418628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3419628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3420628c78e7STejun Heo * cpu comes back online. 3421db7bccf4STejun Heo */ 3422db7bccf4STejun Heo 342360373152STejun Heo /* claim manager positions of all pools */ 34248db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 342560373152STejun Heo { 342660373152STejun Heo struct worker_pool *pool; 342760373152STejun Heo 342860373152STejun Heo for_each_worker_pool(pool, gcwq) 342960373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 34308db25e78STejun Heo spin_lock_irq(&gcwq->lock); 343160373152STejun Heo } 343260373152STejun Heo 343360373152STejun Heo /* release manager positions */ 34348db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 343560373152STejun Heo { 343660373152STejun Heo struct worker_pool *pool; 343760373152STejun Heo 34388db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 343960373152STejun Heo for_each_worker_pool(pool, gcwq) 344060373152STejun Heo mutex_unlock(&pool->manager_mutex); 344160373152STejun Heo } 344260373152STejun Heo 3443628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3444db7bccf4STejun Heo { 3445628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 34464ce62e9eSTejun Heo struct worker_pool *pool; 3447db7bccf4STejun Heo struct worker *worker; 3448db7bccf4STejun Heo struct hlist_node *pos; 3449db7bccf4STejun Heo int i; 3450db7bccf4STejun Heo 3451db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3452db7bccf4STejun Heo 34538db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3454e22bee78STejun Heo 3455f2d5a0eeSTejun Heo /* 3456f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3457f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3458f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3459f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3460f2d5a0eeSTejun Heo */ 346160373152STejun Heo for_each_worker_pool(pool, gcwq) 34624ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3463403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3464db7bccf4STejun Heo 3465db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3466403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3467db7bccf4STejun Heo 3468f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3469f2d5a0eeSTejun Heo 34708db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3471e22bee78STejun Heo 3472e22bee78STejun Heo /* 3473628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3474628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3475628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3476628c78e7STejun Heo */ 3477628c78e7STejun Heo schedule(); 3478628c78e7STejun Heo 3479628c78e7STejun Heo /* 3480628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3481628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3482628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3483628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3484628c78e7STejun Heo * which is served by workers tied to the CPU. 3485628c78e7STejun Heo * 3486628c78e7STejun Heo * On return from this function, the current worker would trigger 3487628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3488628c78e7STejun Heo * didn't already. 3489e22bee78STejun Heo */ 34904ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34914ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3492db7bccf4STejun Heo } 3493db7bccf4STejun Heo 34948db25e78STejun Heo /* 34958db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34968db25e78STejun Heo * This will be registered high priority CPU notifier. 34978db25e78STejun Heo */ 34988db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34991da177e4SLinus Torvalds unsigned long action, 35001da177e4SLinus Torvalds void *hcpu) 35011da177e4SLinus Torvalds { 35023af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3503db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 35044ce62e9eSTejun Heo struct worker_pool *pool; 35051da177e4SLinus Torvalds 35068db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 35073af24433SOleg Nesterov case CPU_UP_PREPARE: 35084ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 35093ce63377STejun Heo struct worker *worker; 35103ce63377STejun Heo 35113ce63377STejun Heo if (pool->nr_workers) 35123ce63377STejun Heo continue; 35133ce63377STejun Heo 35143ce63377STejun Heo worker = create_worker(pool); 35153ce63377STejun Heo if (!worker) 35163ce63377STejun Heo return NOTIFY_BAD; 35173ce63377STejun Heo 35183ce63377STejun Heo spin_lock_irq(&gcwq->lock); 35193ce63377STejun Heo start_worker(worker); 35203ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 35213af24433SOleg Nesterov } 35221da177e4SLinus Torvalds break; 35231da177e4SLinus Torvalds 352465758202STejun Heo case CPU_DOWN_FAILED: 352565758202STejun Heo case CPU_ONLINE: 35268db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 35278db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 35288db25e78STejun Heo rebind_workers(gcwq); 35298db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 35308db25e78STejun Heo break; 353165758202STejun Heo } 353265758202STejun Heo return NOTIFY_OK; 353365758202STejun Heo } 353465758202STejun Heo 353565758202STejun Heo /* 353665758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 353765758202STejun Heo * This will be registered as low priority CPU notifier. 353865758202STejun Heo */ 353965758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 354065758202STejun Heo unsigned long action, 354165758202STejun Heo void *hcpu) 354265758202STejun Heo { 35438db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35448db25e78STejun Heo struct work_struct unbind_work; 35458db25e78STejun Heo 354665758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 354765758202STejun Heo case CPU_DOWN_PREPARE: 35488db25e78STejun Heo /* unbinding should happen on the local CPU */ 35498db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 35508db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 35518db25e78STejun Heo flush_work(&unbind_work); 35528db25e78STejun Heo break; 355365758202STejun Heo } 355465758202STejun Heo return NOTIFY_OK; 355565758202STejun Heo } 355665758202STejun Heo 35572d3854a3SRusty Russell #ifdef CONFIG_SMP 35588ccad40dSRusty Russell 35592d3854a3SRusty Russell struct work_for_cpu { 35606b44003eSAndrew Morton struct completion completion; 35612d3854a3SRusty Russell long (*fn)(void *); 35622d3854a3SRusty Russell void *arg; 35632d3854a3SRusty Russell long ret; 35642d3854a3SRusty Russell }; 35652d3854a3SRusty Russell 35666b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35672d3854a3SRusty Russell { 35686b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35692d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35706b44003eSAndrew Morton complete(&wfc->completion); 35716b44003eSAndrew Morton return 0; 35722d3854a3SRusty Russell } 35732d3854a3SRusty Russell 35742d3854a3SRusty Russell /** 35752d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35762d3854a3SRusty Russell * @cpu: the cpu to run on 35772d3854a3SRusty Russell * @fn: the function to run 35782d3854a3SRusty Russell * @arg: the function arg 35792d3854a3SRusty Russell * 358031ad9081SRusty Russell * This will return the value @fn returns. 358131ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35826b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35832d3854a3SRusty Russell */ 35842d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35852d3854a3SRusty Russell { 35866b44003eSAndrew Morton struct task_struct *sub_thread; 35876b44003eSAndrew Morton struct work_for_cpu wfc = { 35886b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35896b44003eSAndrew Morton .fn = fn, 35906b44003eSAndrew Morton .arg = arg, 35916b44003eSAndrew Morton }; 35922d3854a3SRusty Russell 35936b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35946b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35956b44003eSAndrew Morton return PTR_ERR(sub_thread); 35966b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35976b44003eSAndrew Morton wake_up_process(sub_thread); 35986b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35992d3854a3SRusty Russell return wfc.ret; 36002d3854a3SRusty Russell } 36012d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 36022d3854a3SRusty Russell #endif /* CONFIG_SMP */ 36032d3854a3SRusty Russell 3604a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3605e7577c50SRusty Russell 3606a0a1a5fdSTejun Heo /** 3607a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3608a0a1a5fdSTejun Heo * 360958a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 361058a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 361158a69cb4STejun Heo * gcwq->worklist. 3612a0a1a5fdSTejun Heo * 3613a0a1a5fdSTejun Heo * CONTEXT: 36148b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3615a0a1a5fdSTejun Heo */ 3616a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3617a0a1a5fdSTejun Heo { 3618a0a1a5fdSTejun Heo unsigned int cpu; 3619a0a1a5fdSTejun Heo 3620a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3621a0a1a5fdSTejun Heo 3622a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3623a0a1a5fdSTejun Heo workqueue_freezing = true; 3624a0a1a5fdSTejun Heo 3625f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36268b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3627bdbc5dd7STejun Heo struct workqueue_struct *wq; 36288b03ae3cSTejun Heo 36298b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36308b03ae3cSTejun Heo 3631db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3632db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3633db7bccf4STejun Heo 3634a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3635a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3636a0a1a5fdSTejun Heo 363758a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3638a0a1a5fdSTejun Heo cwq->max_active = 0; 36391da177e4SLinus Torvalds } 36408b03ae3cSTejun Heo 36418b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3642a0a1a5fdSTejun Heo } 3643a0a1a5fdSTejun Heo 3644a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3645a0a1a5fdSTejun Heo } 3646a0a1a5fdSTejun Heo 3647a0a1a5fdSTejun Heo /** 364858a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3649a0a1a5fdSTejun Heo * 3650a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3651a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3652a0a1a5fdSTejun Heo * 3653a0a1a5fdSTejun Heo * CONTEXT: 3654a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3655a0a1a5fdSTejun Heo * 3656a0a1a5fdSTejun Heo * RETURNS: 365758a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 365858a69cb4STejun Heo * is complete. 3659a0a1a5fdSTejun Heo */ 3660a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3661a0a1a5fdSTejun Heo { 3662a0a1a5fdSTejun Heo unsigned int cpu; 3663a0a1a5fdSTejun Heo bool busy = false; 3664a0a1a5fdSTejun Heo 3665a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3666a0a1a5fdSTejun Heo 3667a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3668a0a1a5fdSTejun Heo 3669f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3670bdbc5dd7STejun Heo struct workqueue_struct *wq; 3671a0a1a5fdSTejun Heo /* 3672a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3673a0a1a5fdSTejun Heo * to peek without lock. 3674a0a1a5fdSTejun Heo */ 3675a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3676a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3677a0a1a5fdSTejun Heo 367858a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3679a0a1a5fdSTejun Heo continue; 3680a0a1a5fdSTejun Heo 3681a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3682a0a1a5fdSTejun Heo if (cwq->nr_active) { 3683a0a1a5fdSTejun Heo busy = true; 3684a0a1a5fdSTejun Heo goto out_unlock; 3685a0a1a5fdSTejun Heo } 3686a0a1a5fdSTejun Heo } 3687a0a1a5fdSTejun Heo } 3688a0a1a5fdSTejun Heo out_unlock: 3689a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3690a0a1a5fdSTejun Heo return busy; 3691a0a1a5fdSTejun Heo } 3692a0a1a5fdSTejun Heo 3693a0a1a5fdSTejun Heo /** 3694a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3695a0a1a5fdSTejun Heo * 3696a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36977e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3698a0a1a5fdSTejun Heo * 3699a0a1a5fdSTejun Heo * CONTEXT: 37008b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3701a0a1a5fdSTejun Heo */ 3702a0a1a5fdSTejun Heo void thaw_workqueues(void) 3703a0a1a5fdSTejun Heo { 3704a0a1a5fdSTejun Heo unsigned int cpu; 3705a0a1a5fdSTejun Heo 3706a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3707a0a1a5fdSTejun Heo 3708a0a1a5fdSTejun Heo if (!workqueue_freezing) 3709a0a1a5fdSTejun Heo goto out_unlock; 3710a0a1a5fdSTejun Heo 3711f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37128b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37134ce62e9eSTejun Heo struct worker_pool *pool; 3714bdbc5dd7STejun Heo struct workqueue_struct *wq; 37158b03ae3cSTejun Heo 37168b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 37178b03ae3cSTejun Heo 3718db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3719db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3720db7bccf4STejun Heo 3721a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3722a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3723a0a1a5fdSTejun Heo 372458a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3725a0a1a5fdSTejun Heo continue; 3726a0a1a5fdSTejun Heo 3727a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3728a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3729a0a1a5fdSTejun Heo 3730a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3731a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3732a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3733a0a1a5fdSTejun Heo } 37348b03ae3cSTejun Heo 37354ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37364ce62e9eSTejun Heo wake_up_worker(pool); 3737e22bee78STejun Heo 37388b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3739a0a1a5fdSTejun Heo } 3740a0a1a5fdSTejun Heo 3741a0a1a5fdSTejun Heo workqueue_freezing = false; 3742a0a1a5fdSTejun Heo out_unlock: 3743a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3744a0a1a5fdSTejun Heo } 3745a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3746a0a1a5fdSTejun Heo 37476ee0578bSSuresh Siddha static int __init init_workqueues(void) 37481da177e4SLinus Torvalds { 3749c34056a3STejun Heo unsigned int cpu; 3750c8e55f36STejun Heo int i; 3751c34056a3STejun Heo 3752b5490077STejun Heo /* make sure we have enough bits for OFFQ CPU number */ 3753b5490077STejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) < 3754b5490077STejun Heo WORK_CPU_LAST); 3755b5490077STejun Heo 375665758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 375765758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37588b03ae3cSTejun Heo 37598b03ae3cSTejun Heo /* initialize gcwqs */ 3760f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37618b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37624ce62e9eSTejun Heo struct worker_pool *pool; 37638b03ae3cSTejun Heo 37648b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37658b03ae3cSTejun Heo gcwq->cpu = cpu; 3766f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37678b03ae3cSTejun Heo 3768c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3769c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3770c8e55f36STejun Heo 37714ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37724ce62e9eSTejun Heo pool->gcwq = gcwq; 37734ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37744ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3775e22bee78STejun Heo 37764ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37774ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37784ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3779e22bee78STejun Heo 37804ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37814ce62e9eSTejun Heo (unsigned long)pool); 37824ce62e9eSTejun Heo 378360373152STejun Heo mutex_init(&pool->manager_mutex); 37844ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37854ce62e9eSTejun Heo } 3786db7bccf4STejun Heo 378725511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37888b03ae3cSTejun Heo } 37898b03ae3cSTejun Heo 3790e22bee78STejun Heo /* create the initial worker */ 3791f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3792e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37934ce62e9eSTejun Heo struct worker_pool *pool; 3794e22bee78STejun Heo 3795477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3796477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37974ce62e9eSTejun Heo 37984ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37994ce62e9eSTejun Heo struct worker *worker; 38004ce62e9eSTejun Heo 3801bc2ae0f5STejun Heo worker = create_worker(pool); 3802e22bee78STejun Heo BUG_ON(!worker); 3803e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3804e22bee78STejun Heo start_worker(worker); 3805e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3806e22bee78STejun Heo } 38074ce62e9eSTejun Heo } 3808e22bee78STejun Heo 3809d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3810d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3811d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3812f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3813f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 381424d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 381524d51addSTejun Heo WQ_FREEZABLE, 0); 381662d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 381762d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3818e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 381962d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 382062d3c543SAlan Stern !system_nrt_freezable_wq); 38216ee0578bSSuresh Siddha return 0; 38221da177e4SLinus Torvalds } 38236ee0578bSSuresh Siddha early_initcall(init_workqueues); 3824