11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 101da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19c54fce6eSTejun Heo * automatically managed. There is one worker pool for each CPU and 20c54fce6eSTejun Heo * one extra for works which are better served by workers which are 21c54fce6eSTejun Heo * not bound to any specific CPU. 22c54fce6eSTejun Heo * 23c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 269984de1aSPaul Gortmaker #include <linux/export.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds #include <linux/signal.h> 311da177e4SLinus Torvalds #include <linux/completion.h> 321da177e4SLinus Torvalds #include <linux/workqueue.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/cpu.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/kthread.h> 371fa44ecaSJames Bottomley #include <linux/hardirq.h> 3846934023SChristoph Lameter #include <linux/mempolicy.h> 39341a5958SRafael J. Wysocki #include <linux/freezer.h> 40d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 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 /* 536e120153dSTejun Heo * A work's data points to the cwq with WORK_STRUCT_CWQ set while the 537e120153dSTejun Heo * work is on queue. Once execution starts, WORK_STRUCT_CWQ is 538e120153dSTejun Heo * cleared and the work data contains the cpu number it was last on. 5397a22ad75STejun Heo * 5407a22ad75STejun Heo * set_work_{cwq|cpu}() and clear_work_data() can be used to set the 5417a22ad75STejun Heo * cwq, cpu or clear work->data. These functions should only be 5427a22ad75STejun Heo * called while the work is owned - ie. while the PENDING bit is set. 5437a22ad75STejun Heo * 5447a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 5457a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 5467a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 5477a22ad75STejun Heo * queueing until execution starts. 5484594bf15SDavid Howells */ 5497a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5507a22ad75STejun Heo unsigned long flags) 5517a22ad75STejun Heo { 5527a22ad75STejun Heo BUG_ON(!work_pending(work)); 5537a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5547a22ad75STejun Heo } 5557a22ad75STejun Heo 5567a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5574690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5584690c4abSTejun Heo unsigned long extra_flags) 559365970a1SDavid Howells { 5607a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 561e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 562365970a1SDavid Howells } 563365970a1SDavid Howells 5647a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu) 5654d707b9fSOleg Nesterov { 5667a22ad75STejun Heo set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING); 5674d707b9fSOleg Nesterov } 5684d707b9fSOleg Nesterov 5697a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 570365970a1SDavid Howells { 5717a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5727a22ad75STejun Heo } 5737a22ad75STejun Heo 5747a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5757a22ad75STejun Heo { 576e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5777a22ad75STejun Heo 578e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 579e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 580e120153dSTejun Heo else 581e120153dSTejun Heo return NULL; 5827a22ad75STejun Heo } 5837a22ad75STejun Heo 5847a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5857a22ad75STejun Heo { 586e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5877a22ad75STejun Heo unsigned int cpu; 5887a22ad75STejun Heo 589e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 590e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 591bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5927a22ad75STejun Heo 5937a22ad75STejun Heo cpu = data >> WORK_STRUCT_FLAG_BITS; 594bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 5957a22ad75STejun Heo return NULL; 5967a22ad75STejun Heo 597f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 5987a22ad75STejun Heo return get_gcwq(cpu); 599365970a1SDavid Howells } 600365970a1SDavid Howells 601e22bee78STejun Heo /* 6023270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6033270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6043270476aSTejun Heo * they're being called with gcwq->lock held. 605e22bee78STejun Heo */ 606e22bee78STejun Heo 60763d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 608649027d7STejun Heo { 6093270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 610649027d7STejun Heo } 611649027d7STejun Heo 612e22bee78STejun Heo /* 613e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 614e22bee78STejun Heo * running workers. 615974271c4STejun Heo * 616974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 617974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 618974271c4STejun Heo * worklist isn't empty. 619e22bee78STejun Heo */ 62063d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 621e22bee78STejun Heo { 62263d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 623e22bee78STejun Heo } 624e22bee78STejun Heo 625e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 62663d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 627e22bee78STejun Heo { 62863d95a91STejun Heo return pool->nr_idle; 629e22bee78STejun Heo } 630e22bee78STejun Heo 631e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 63263d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 633e22bee78STejun Heo { 63463d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 635e22bee78STejun Heo 6363270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 637e22bee78STejun Heo } 638e22bee78STejun Heo 639e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 64063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 641e22bee78STejun Heo { 64263d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 643e22bee78STejun Heo } 644e22bee78STejun Heo 645e22bee78STejun Heo /* Do I need to be the manager? */ 64663d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 647e22bee78STejun Heo { 64863d95a91STejun Heo return need_to_create_worker(pool) || 64911ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 650e22bee78STejun Heo } 651e22bee78STejun Heo 652e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 65363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 654e22bee78STejun Heo { 65560373152STejun Heo bool managing = mutex_is_locked(&pool->manager_mutex); 65663d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 65763d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 658e22bee78STejun Heo 659e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 660e22bee78STejun Heo } 661e22bee78STejun Heo 662e22bee78STejun Heo /* 663e22bee78STejun Heo * Wake up functions. 664e22bee78STejun Heo */ 665e22bee78STejun Heo 6667e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 66763d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6687e11629dSTejun Heo { 66963d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6707e11629dSTejun Heo return NULL; 6717e11629dSTejun Heo 67263d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6737e11629dSTejun Heo } 6747e11629dSTejun Heo 6757e11629dSTejun Heo /** 6767e11629dSTejun Heo * wake_up_worker - wake up an idle worker 67763d95a91STejun Heo * @pool: worker pool to wake worker from 6787e11629dSTejun Heo * 67963d95a91STejun Heo * Wake up the first idle worker of @pool. 6807e11629dSTejun Heo * 6817e11629dSTejun Heo * CONTEXT: 6827e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 6837e11629dSTejun Heo */ 68463d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 6857e11629dSTejun Heo { 68663d95a91STejun Heo struct worker *worker = first_worker(pool); 6877e11629dSTejun Heo 6887e11629dSTejun Heo if (likely(worker)) 6897e11629dSTejun Heo wake_up_process(worker->task); 6907e11629dSTejun Heo } 6917e11629dSTejun Heo 6924690c4abSTejun Heo /** 693e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 694e22bee78STejun Heo * @task: task waking up 695e22bee78STejun Heo * @cpu: CPU @task is waking up to 696e22bee78STejun Heo * 697e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 698e22bee78STejun Heo * being awoken. 699e22bee78STejun Heo * 700e22bee78STejun Heo * CONTEXT: 701e22bee78STejun Heo * spin_lock_irq(rq->lock) 702e22bee78STejun Heo */ 703e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 704e22bee78STejun Heo { 705e22bee78STejun Heo struct worker *worker = kthread_data(task); 706e22bee78STejun Heo 7072d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 70863d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 709e22bee78STejun Heo } 710e22bee78STejun Heo 711e22bee78STejun Heo /** 712e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 713e22bee78STejun Heo * @task: task going to sleep 714e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 715e22bee78STejun Heo * 716e22bee78STejun Heo * This function is called during schedule() when a busy worker is 717e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 718e22bee78STejun Heo * returning pointer to its task. 719e22bee78STejun Heo * 720e22bee78STejun Heo * CONTEXT: 721e22bee78STejun Heo * spin_lock_irq(rq->lock) 722e22bee78STejun Heo * 723e22bee78STejun Heo * RETURNS: 724e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 725e22bee78STejun Heo */ 726e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 727e22bee78STejun Heo unsigned int cpu) 728e22bee78STejun Heo { 729e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 730bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 73163d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 732e22bee78STejun Heo 7332d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 734e22bee78STejun Heo return NULL; 735e22bee78STejun Heo 736e22bee78STejun Heo /* this can only happen on the local cpu */ 737e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 738e22bee78STejun Heo 739e22bee78STejun Heo /* 740e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 741e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 742e22bee78STejun Heo * Please read comment there. 743e22bee78STejun Heo * 744628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 745628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 746628c78e7STejun Heo * disabled, which in turn means that none else could be 747628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 748628c78e7STejun Heo * lock is safe. 749e22bee78STejun Heo */ 750bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 75163d95a91STejun Heo to_wakeup = first_worker(pool); 752e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 753e22bee78STejun Heo } 754e22bee78STejun Heo 755e22bee78STejun Heo /** 756e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 757cb444766STejun Heo * @worker: self 758d302f017STejun Heo * @flags: flags to set 759d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 760d302f017STejun Heo * 761e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 762e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 763e22bee78STejun Heo * woken up. 764d302f017STejun Heo * 765cb444766STejun Heo * CONTEXT: 766cb444766STejun Heo * spin_lock_irq(gcwq->lock) 767d302f017STejun Heo */ 768d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 769d302f017STejun Heo bool wakeup) 770d302f017STejun Heo { 771bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 772e22bee78STejun Heo 773cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 774cb444766STejun Heo 775e22bee78STejun Heo /* 776e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 777e22bee78STejun Heo * wake up an idle worker as necessary if requested by 778e22bee78STejun Heo * @wakeup. 779e22bee78STejun Heo */ 780e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 781e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 78263d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 783e22bee78STejun Heo 784e22bee78STejun Heo if (wakeup) { 785e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 786bd7bdd43STejun Heo !list_empty(&pool->worklist)) 78763d95a91STejun Heo wake_up_worker(pool); 788e22bee78STejun Heo } else 789e22bee78STejun Heo atomic_dec(nr_running); 790e22bee78STejun Heo } 791e22bee78STejun Heo 792d302f017STejun Heo worker->flags |= flags; 793d302f017STejun Heo } 794d302f017STejun Heo 795d302f017STejun Heo /** 796e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 797cb444766STejun Heo * @worker: self 798d302f017STejun Heo * @flags: flags to clear 799d302f017STejun Heo * 800e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 801d302f017STejun Heo * 802cb444766STejun Heo * CONTEXT: 803cb444766STejun Heo * spin_lock_irq(gcwq->lock) 804d302f017STejun Heo */ 805d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 806d302f017STejun Heo { 80763d95a91STejun Heo struct worker_pool *pool = worker->pool; 808e22bee78STejun Heo unsigned int oflags = worker->flags; 809e22bee78STejun Heo 810cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 811cb444766STejun Heo 812d302f017STejun Heo worker->flags &= ~flags; 813e22bee78STejun Heo 81442c025f3STejun Heo /* 81542c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 81642c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 81742c025f3STejun Heo * of multiple flags, not a single flag. 81842c025f3STejun Heo */ 819e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 820e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 82163d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 822d302f017STejun Heo } 823d302f017STejun Heo 824d302f017STejun Heo /** 825c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 826c8e55f36STejun Heo * @gcwq: gcwq of interest 827c8e55f36STejun Heo * @work: work to be hashed 828c8e55f36STejun Heo * 829c8e55f36STejun Heo * Return hash head of @gcwq for @work. 830c8e55f36STejun Heo * 831c8e55f36STejun Heo * CONTEXT: 832c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 833c8e55f36STejun Heo * 834c8e55f36STejun Heo * RETURNS: 835c8e55f36STejun Heo * Pointer to the hash head. 836c8e55f36STejun Heo */ 837c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 838c8e55f36STejun Heo struct work_struct *work) 839c8e55f36STejun Heo { 840c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 841c8e55f36STejun Heo unsigned long v = (unsigned long)work; 842c8e55f36STejun Heo 843c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 844c8e55f36STejun Heo v >>= base_shift; 845c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 846c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 847c8e55f36STejun Heo 848c8e55f36STejun Heo return &gcwq->busy_hash[v]; 849c8e55f36STejun Heo } 850c8e55f36STejun Heo 851c8e55f36STejun Heo /** 8528cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8538cca0eeaSTejun Heo * @gcwq: gcwq of interest 8548cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8558cca0eeaSTejun Heo * @work: work to find worker for 8568cca0eeaSTejun Heo * 8578cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8588cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8598cca0eeaSTejun Heo * work. 8608cca0eeaSTejun Heo * 8618cca0eeaSTejun Heo * CONTEXT: 8628cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8638cca0eeaSTejun Heo * 8648cca0eeaSTejun Heo * RETURNS: 8658cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8668cca0eeaSTejun Heo * otherwise. 8678cca0eeaSTejun Heo */ 8688cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8698cca0eeaSTejun Heo struct hlist_head *bwh, 8708cca0eeaSTejun Heo struct work_struct *work) 8718cca0eeaSTejun Heo { 8728cca0eeaSTejun Heo struct worker *worker; 8738cca0eeaSTejun Heo struct hlist_node *tmp; 8748cca0eeaSTejun Heo 8758cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8768cca0eeaSTejun Heo if (worker->current_work == work) 8778cca0eeaSTejun Heo return worker; 8788cca0eeaSTejun Heo return NULL; 8798cca0eeaSTejun Heo } 8808cca0eeaSTejun Heo 8818cca0eeaSTejun Heo /** 8828cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 8838cca0eeaSTejun Heo * @gcwq: gcwq of interest 8848cca0eeaSTejun Heo * @work: work to find worker for 8858cca0eeaSTejun Heo * 8868cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 8878cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 8888cca0eeaSTejun Heo * function calculates @bwh itself. 8898cca0eeaSTejun Heo * 8908cca0eeaSTejun Heo * CONTEXT: 8918cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8928cca0eeaSTejun Heo * 8938cca0eeaSTejun Heo * RETURNS: 8948cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8958cca0eeaSTejun Heo * otherwise. 8968cca0eeaSTejun Heo */ 8978cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 8988cca0eeaSTejun Heo struct work_struct *work) 8998cca0eeaSTejun Heo { 9008cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9018cca0eeaSTejun Heo work); 9028cca0eeaSTejun Heo } 9038cca0eeaSTejun Heo 9048cca0eeaSTejun Heo /** 9057e11629dSTejun Heo * insert_work - insert a work into gcwq 9064690c4abSTejun Heo * @cwq: cwq @work belongs to 9074690c4abSTejun Heo * @work: work to insert 9084690c4abSTejun Heo * @head: insertion point 9094690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 9104690c4abSTejun Heo * 9117e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 9127e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 9134690c4abSTejun Heo * 9144690c4abSTejun Heo * CONTEXT: 9158b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 9161da177e4SLinus Torvalds */ 917b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 9184690c4abSTejun Heo struct work_struct *work, struct list_head *head, 9194690c4abSTejun Heo unsigned int extra_flags) 920b89deed3SOleg Nesterov { 92163d95a91STejun Heo struct worker_pool *pool = cwq->pool; 922e1d8aa9fSFrederic Weisbecker 9234690c4abSTejun Heo /* we own @work, set data and link */ 9247a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 9254690c4abSTejun Heo 9266e84d644SOleg Nesterov /* 9276e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 9286e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 9296e84d644SOleg Nesterov */ 9306e84d644SOleg Nesterov smp_wmb(); 9314690c4abSTejun Heo 9321a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 933e22bee78STejun Heo 934e22bee78STejun Heo /* 935e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 936e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 937e22bee78STejun Heo * lying around lazily while there are works to be processed. 938e22bee78STejun Heo */ 939e22bee78STejun Heo smp_mb(); 940e22bee78STejun Heo 94163d95a91STejun Heo if (__need_more_worker(pool)) 94263d95a91STejun Heo wake_up_worker(pool); 943b89deed3SOleg Nesterov } 944b89deed3SOleg Nesterov 945c8efcc25STejun Heo /* 946c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 947c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 948c8efcc25STejun Heo * cold paths. 949c8efcc25STejun Heo */ 950c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 951c8efcc25STejun Heo { 952c8efcc25STejun Heo unsigned long flags; 953c8efcc25STejun Heo unsigned int cpu; 954c8efcc25STejun Heo 955c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 956c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 957c8efcc25STejun Heo struct worker *worker; 958c8efcc25STejun Heo struct hlist_node *pos; 959c8efcc25STejun Heo int i; 960c8efcc25STejun Heo 961c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 962c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 963c8efcc25STejun Heo if (worker->task != current) 964c8efcc25STejun Heo continue; 965c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 966c8efcc25STejun Heo /* 967c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 968c8efcc25STejun Heo * is headed to the same workqueue. 969c8efcc25STejun Heo */ 970c8efcc25STejun Heo return worker->current_cwq->wq == wq; 971c8efcc25STejun Heo } 972c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 973c8efcc25STejun Heo } 974c8efcc25STejun Heo return false; 975c8efcc25STejun Heo } 976c8efcc25STejun Heo 9774690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 9781da177e4SLinus Torvalds struct work_struct *work) 9791da177e4SLinus Torvalds { 980502ca9d8STejun Heo struct global_cwq *gcwq; 981502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 9821e19ffc6STejun Heo struct list_head *worklist; 9838a2e8e5dSTejun Heo unsigned int work_flags; 9841da177e4SLinus Torvalds unsigned long flags; 9851da177e4SLinus Torvalds 986dc186ad7SThomas Gleixner debug_work_activate(work); 9871e19ffc6STejun Heo 988c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 9899c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 990c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 991e41e704bSTejun Heo return; 992e41e704bSTejun Heo 993c7fc77f7STejun Heo /* determine gcwq to use */ 994c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 995c7fc77f7STejun Heo struct global_cwq *last_gcwq; 996c7fc77f7STejun Heo 997f3421797STejun Heo if (unlikely(cpu == WORK_CPU_UNBOUND)) 998f3421797STejun Heo cpu = raw_smp_processor_id(); 999f3421797STejun Heo 100018aa9effSTejun Heo /* 100118aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 100218aa9effSTejun Heo * was previously on a different cpu, it might still 100318aa9effSTejun Heo * be running there, in which case the work needs to 100418aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 100518aa9effSTejun Heo */ 1006502ca9d8STejun Heo gcwq = get_gcwq(cpu); 100718aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 100818aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 100918aa9effSTejun Heo struct worker *worker; 101018aa9effSTejun Heo 101118aa9effSTejun Heo spin_lock_irqsave(&last_gcwq->lock, flags); 101218aa9effSTejun Heo 101318aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 101418aa9effSTejun Heo 101518aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 101618aa9effSTejun Heo gcwq = last_gcwq; 101718aa9effSTejun Heo else { 101818aa9effSTejun Heo /* meh... not running there, queue here */ 101918aa9effSTejun Heo spin_unlock_irqrestore(&last_gcwq->lock, flags); 102018aa9effSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 102118aa9effSTejun Heo } 102218aa9effSTejun Heo } else 10238b03ae3cSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1024f3421797STejun Heo } else { 1025f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 1026f3421797STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1027502ca9d8STejun Heo } 1028502ca9d8STejun Heo 1029502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1030502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1031cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1032502ca9d8STejun Heo 1033f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1034f5b2552bSDan Carpenter spin_unlock_irqrestore(&gcwq->lock, flags); 1035f5b2552bSDan Carpenter return; 1036f5b2552bSDan Carpenter } 10371e19ffc6STejun Heo 103873f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 10398a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 10401e19ffc6STejun Heo 10411e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1042cdadf009STejun Heo trace_workqueue_activate_work(work); 10431e19ffc6STejun Heo cwq->nr_active++; 10443270476aSTejun Heo worklist = &cwq->pool->worklist; 10458a2e8e5dSTejun Heo } else { 10468a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 10471e19ffc6STejun Heo worklist = &cwq->delayed_works; 10488a2e8e5dSTejun Heo } 10491e19ffc6STejun Heo 10508a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 10511e19ffc6STejun Heo 10528b03ae3cSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10550fcb78c2SRolf Eike Beer /** 10560fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 10570fcb78c2SRolf Eike Beer * @wq: workqueue to use 10580fcb78c2SRolf Eike Beer * @work: work to queue 10590fcb78c2SRolf Eike Beer * 1060057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 10611da177e4SLinus Torvalds * 106200dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 106300dfcaf7SOleg Nesterov * it can be processed by another CPU. 10641da177e4SLinus Torvalds */ 10657ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 10661da177e4SLinus Torvalds { 1067ef1ca236SOleg Nesterov int ret; 10681da177e4SLinus Torvalds 1069ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 1070a848e3b6SOleg Nesterov put_cpu(); 1071ef1ca236SOleg Nesterov 10721da177e4SLinus Torvalds return ret; 10731da177e4SLinus Torvalds } 1074ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 10751da177e4SLinus Torvalds 1076c1a220e7SZhang Rui /** 1077c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1078c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1079c1a220e7SZhang Rui * @wq: workqueue to use 1080c1a220e7SZhang Rui * @work: work to queue 1081c1a220e7SZhang Rui * 1082c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 1083c1a220e7SZhang Rui * 1084c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1085c1a220e7SZhang Rui * can't go away. 1086c1a220e7SZhang Rui */ 1087c1a220e7SZhang Rui int 1088c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 1089c1a220e7SZhang Rui { 1090c1a220e7SZhang Rui int ret = 0; 1091c1a220e7SZhang Rui 109222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 10934690c4abSTejun Heo __queue_work(cpu, wq, work); 1094c1a220e7SZhang Rui ret = 1; 1095c1a220e7SZhang Rui } 1096c1a220e7SZhang Rui return ret; 1097c1a220e7SZhang Rui } 1098c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1099c1a220e7SZhang Rui 11006d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 11011da177e4SLinus Torvalds { 110252bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 11037a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 11041da177e4SLinus Torvalds 11054690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11080fcb78c2SRolf Eike Beer /** 11090fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 11100fcb78c2SRolf Eike Beer * @wq: workqueue to use 1111af9997e4SRandy Dunlap * @dwork: delayable work to queue 11120fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11130fcb78c2SRolf Eike Beer * 1114057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11150fcb78c2SRolf Eike Beer */ 11167ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 111752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11181da177e4SLinus Torvalds { 111952bad64dSDavid Howells if (delay == 0) 112063bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 11211da177e4SLinus Torvalds 112263bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 11231da177e4SLinus Torvalds } 1124ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 11251da177e4SLinus Torvalds 11260fcb78c2SRolf Eike Beer /** 11270fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 11280fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 11290fcb78c2SRolf Eike Beer * @wq: workqueue to use 1130af9997e4SRandy Dunlap * @dwork: work to queue 11310fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11320fcb78c2SRolf Eike Beer * 1133057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11340fcb78c2SRolf Eike Beer */ 11357a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 113652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11377a6bc1cdSVenkatesh Pallipadi { 11387a6bc1cdSVenkatesh Pallipadi int ret = 0; 113952bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 114052bad64dSDavid Howells struct work_struct *work = &dwork->work; 11417a6bc1cdSVenkatesh Pallipadi 114222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1143c7fc77f7STejun Heo unsigned int lcpu; 11447a22ad75STejun Heo 11457a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 11467a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 11477a6bc1cdSVenkatesh Pallipadi 11488a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 11498a3e77ccSAndrew Liu 11507a22ad75STejun Heo /* 11517a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 11527a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 11537a22ad75STejun Heo * reentrance detection for delayed works. 11547a22ad75STejun Heo */ 1155c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1156c7fc77f7STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 1157c7fc77f7STejun Heo 1158c7fc77f7STejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 1159c7fc77f7STejun Heo lcpu = gcwq->cpu; 1160c7fc77f7STejun Heo else 1161c7fc77f7STejun Heo lcpu = raw_smp_processor_id(); 1162c7fc77f7STejun Heo } else 1163c7fc77f7STejun Heo lcpu = WORK_CPU_UNBOUND; 1164c7fc77f7STejun Heo 11657a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 1166c7fc77f7STejun Heo 11677a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 116852bad64dSDavid Howells timer->data = (unsigned long)dwork; 11697a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 117063bc0362SOleg Nesterov 117163bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 11727a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 117363bc0362SOleg Nesterov else 117463bc0362SOleg Nesterov add_timer(timer); 11757a6bc1cdSVenkatesh Pallipadi ret = 1; 11767a6bc1cdSVenkatesh Pallipadi } 11777a6bc1cdSVenkatesh Pallipadi return ret; 11787a6bc1cdSVenkatesh Pallipadi } 1179ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 11801da177e4SLinus Torvalds 1181c8e55f36STejun Heo /** 1182c8e55f36STejun Heo * worker_enter_idle - enter idle state 1183c8e55f36STejun Heo * @worker: worker which is entering idle state 1184c8e55f36STejun Heo * 1185c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1186c8e55f36STejun Heo * necessary. 1187c8e55f36STejun Heo * 1188c8e55f36STejun Heo * LOCKING: 1189c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1190c8e55f36STejun Heo */ 1191c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 11921da177e4SLinus Torvalds { 1193bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1194bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1195c8e55f36STejun Heo 1196c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1197c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1198c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1199c8e55f36STejun Heo 1200cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1201cb444766STejun Heo worker->flags |= WORKER_IDLE; 1202bd7bdd43STejun Heo pool->nr_idle++; 1203e22bee78STejun Heo worker->last_active = jiffies; 1204c8e55f36STejun Heo 1205c8e55f36STejun Heo /* idle_list is LIFO */ 1206bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1207db7bccf4STejun Heo 120863d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1209628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1210cb444766STejun Heo 1211544ecf31STejun Heo /* 1212628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1213628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1214628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1215628c78e7STejun Heo * unbind is not in progress. 1216544ecf31STejun Heo */ 1217628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1218bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 121963d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1220c8e55f36STejun Heo } 1221c8e55f36STejun Heo 1222c8e55f36STejun Heo /** 1223c8e55f36STejun Heo * worker_leave_idle - leave idle state 1224c8e55f36STejun Heo * @worker: worker which is leaving idle state 1225c8e55f36STejun Heo * 1226c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1227c8e55f36STejun Heo * 1228c8e55f36STejun Heo * LOCKING: 1229c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1230c8e55f36STejun Heo */ 1231c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1232c8e55f36STejun Heo { 1233bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1234c8e55f36STejun Heo 1235c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1236d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1237bd7bdd43STejun Heo pool->nr_idle--; 1238c8e55f36STejun Heo list_del_init(&worker->entry); 1239c8e55f36STejun Heo } 1240c8e55f36STejun Heo 1241e22bee78STejun Heo /** 1242e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1243e22bee78STejun Heo * @worker: self 1244e22bee78STejun Heo * 1245e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1246e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1247e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1248e22bee78STejun Heo * guaranteed to execute on the cpu. 1249e22bee78STejun Heo * 1250e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1251e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1252e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1253e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1254e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1255e22bee78STejun Heo * [dis]associated in the meantime. 1256e22bee78STejun Heo * 1257f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1258f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1259f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1260f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1261f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1262e22bee78STejun Heo * 1263e22bee78STejun Heo * CONTEXT: 1264e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1265e22bee78STejun Heo * held. 1266e22bee78STejun Heo * 1267e22bee78STejun Heo * RETURNS: 1268e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1269e22bee78STejun Heo * bound), %false if offline. 1270e22bee78STejun Heo */ 1271e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1272972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1273e22bee78STejun Heo { 1274bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1275e22bee78STejun Heo struct task_struct *task = worker->task; 1276e22bee78STejun Heo 1277e22bee78STejun Heo while (true) { 1278e22bee78STejun Heo /* 1279e22bee78STejun Heo * The following call may fail, succeed or succeed 1280e22bee78STejun Heo * without actually migrating the task to the cpu if 1281e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1282e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1283e22bee78STejun Heo */ 1284f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1285e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1286e22bee78STejun Heo 1287e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1288e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1289e22bee78STejun Heo return false; 1290e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1291e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1292e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1293e22bee78STejun Heo return true; 1294e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1295e22bee78STejun Heo 12965035b20fSTejun Heo /* 12975035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 12985035b20fSTejun Heo * and retry migration. cond_resched() is required here; 12995035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 13005035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 13015035b20fSTejun Heo */ 1302e22bee78STejun Heo cpu_relax(); 13035035b20fSTejun Heo cond_resched(); 1304e22bee78STejun Heo } 1305e22bee78STejun Heo } 1306e22bee78STejun Heo 130725511a47STejun Heo struct idle_rebind { 130825511a47STejun Heo int cnt; /* # workers to be rebound */ 130925511a47STejun Heo struct completion done; /* all workers rebound */ 131025511a47STejun Heo }; 131125511a47STejun Heo 1312e22bee78STejun Heo /* 131325511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 131425511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 131525511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 131625511a47STejun Heo */ 131725511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 131825511a47STejun Heo { 131925511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 132025511a47STejun Heo 132125511a47STejun Heo /* CPU must be online at this point */ 132225511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 132325511a47STejun Heo if (!--worker->idle_rebind->cnt) 132425511a47STejun Heo complete(&worker->idle_rebind->done); 132525511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 132625511a47STejun Heo 132725511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 132825511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 132925511a47STejun Heo } 133025511a47STejun Heo 133125511a47STejun Heo /* 133225511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1333403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1334403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1335403c821dSTejun Heo * executed twice without intervening cpu down. 1336e22bee78STejun Heo */ 133725511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1338e22bee78STejun Heo { 1339e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1340bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1341e22bee78STejun Heo 1342e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1343e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1344e22bee78STejun Heo 1345e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1346e22bee78STejun Heo } 1347e22bee78STejun Heo 134825511a47STejun Heo /** 134925511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 135025511a47STejun Heo * @gcwq: gcwq of interest 135125511a47STejun Heo * 135225511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 135325511a47STejun Heo * is different for idle and busy ones. 135425511a47STejun Heo * 135525511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 135625511a47STejun Heo * be complete before any worker starts executing work items with 135725511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 135825511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 135925511a47STejun Heo * 136025511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 136125511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 136225511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 136325511a47STejun Heo * 136425511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 136525511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 136625511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 136725511a47STejun Heo * be properbly bumped as busy workers rebind. 136825511a47STejun Heo * 136925511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 137025511a47STejun Heo * work item scheduled. 137125511a47STejun Heo */ 137225511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 137325511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 137425511a47STejun Heo { 137525511a47STejun Heo struct idle_rebind idle_rebind; 137625511a47STejun Heo struct worker_pool *pool; 137725511a47STejun Heo struct worker *worker; 137825511a47STejun Heo struct hlist_node *pos; 137925511a47STejun Heo int i; 138025511a47STejun Heo 138125511a47STejun Heo lockdep_assert_held(&gcwq->lock); 138225511a47STejun Heo 138325511a47STejun Heo for_each_worker_pool(pool, gcwq) 138425511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 138525511a47STejun Heo 138625511a47STejun Heo /* 138725511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 138825511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 138925511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 139025511a47STejun Heo */ 139125511a47STejun Heo init_completion(&idle_rebind.done); 139225511a47STejun Heo retry: 139325511a47STejun Heo idle_rebind.cnt = 1; 139425511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 139525511a47STejun Heo 139625511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 139725511a47STejun Heo for_each_worker_pool(pool, gcwq) { 139825511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 139925511a47STejun Heo if (worker->flags & WORKER_REBIND) 140025511a47STejun Heo continue; 140125511a47STejun Heo 140225511a47STejun Heo /* morph UNBOUND to REBIND */ 140325511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 140425511a47STejun Heo worker->flags |= WORKER_REBIND; 140525511a47STejun Heo 140625511a47STejun Heo idle_rebind.cnt++; 140725511a47STejun Heo worker->idle_rebind = &idle_rebind; 140825511a47STejun Heo 140925511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 141025511a47STejun Heo wake_up_process(worker->task); 141125511a47STejun Heo } 141225511a47STejun Heo } 141325511a47STejun Heo 141425511a47STejun Heo if (--idle_rebind.cnt) { 141525511a47STejun Heo spin_unlock_irq(&gcwq->lock); 141625511a47STejun Heo wait_for_completion(&idle_rebind.done); 141725511a47STejun Heo spin_lock_irq(&gcwq->lock); 141825511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 141925511a47STejun Heo goto retry; 142025511a47STejun Heo } 142125511a47STejun Heo 142225511a47STejun Heo /* 142325511a47STejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 142425511a47STejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 142525511a47STejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 142625511a47STejun Heo * because these workers are still guaranteed to be idle. 142725511a47STejun Heo */ 142825511a47STejun Heo for_each_worker_pool(pool, gcwq) 142925511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 143025511a47STejun Heo worker->flags &= ~WORKER_REBIND; 143125511a47STejun Heo 143225511a47STejun Heo wake_up_all(&gcwq->rebind_hold); 143325511a47STejun Heo 143425511a47STejun Heo /* rebind busy workers */ 143525511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 143625511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 143725511a47STejun Heo 143825511a47STejun Heo /* morph UNBOUND to REBIND */ 143925511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 144025511a47STejun Heo worker->flags |= WORKER_REBIND; 144125511a47STejun Heo 144225511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 144325511a47STejun Heo work_data_bits(rebind_work))) 144425511a47STejun Heo continue; 144525511a47STejun Heo 144625511a47STejun Heo /* wq doesn't matter, use the default one */ 144725511a47STejun Heo debug_work_activate(rebind_work); 144825511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 144925511a47STejun Heo worker->scheduled.next, 145025511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 145125511a47STejun Heo } 145225511a47STejun Heo } 145325511a47STejun Heo 1454c34056a3STejun Heo static struct worker *alloc_worker(void) 1455c34056a3STejun Heo { 1456c34056a3STejun Heo struct worker *worker; 1457c34056a3STejun Heo 1458c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1459c8e55f36STejun Heo if (worker) { 1460c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1461affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 146225511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1463e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1464e22bee78STejun Heo worker->flags = WORKER_PREP; 1465c8e55f36STejun Heo } 1466c34056a3STejun Heo return worker; 1467c34056a3STejun Heo } 1468c34056a3STejun Heo 1469c34056a3STejun Heo /** 1470c34056a3STejun Heo * create_worker - create a new workqueue worker 147163d95a91STejun Heo * @pool: pool the new worker will belong to 1472c34056a3STejun Heo * 147363d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1474c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1475c34056a3STejun Heo * destroy_worker(). 1476c34056a3STejun Heo * 1477c34056a3STejun Heo * CONTEXT: 1478c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1479c34056a3STejun Heo * 1480c34056a3STejun Heo * RETURNS: 1481c34056a3STejun Heo * Pointer to the newly created worker. 1482c34056a3STejun Heo */ 1483bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1484c34056a3STejun Heo { 148563d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 14863270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1487c34056a3STejun Heo struct worker *worker = NULL; 1488f3421797STejun Heo int id = -1; 1489c34056a3STejun Heo 14908b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1491bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 14928b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1493bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1494c34056a3STejun Heo goto fail; 14958b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1496c34056a3STejun Heo } 14978b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1498c34056a3STejun Heo 1499c34056a3STejun Heo worker = alloc_worker(); 1500c34056a3STejun Heo if (!worker) 1501c34056a3STejun Heo goto fail; 1502c34056a3STejun Heo 1503bd7bdd43STejun Heo worker->pool = pool; 1504c34056a3STejun Heo worker->id = id; 1505c34056a3STejun Heo 1506bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 150794dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 15083270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 15093270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1510f3421797STejun Heo else 1511f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 15123270476aSTejun Heo "kworker/u:%d%s", id, pri); 1513c34056a3STejun Heo if (IS_ERR(worker->task)) 1514c34056a3STejun Heo goto fail; 1515c34056a3STejun Heo 15163270476aSTejun Heo if (worker_pool_pri(pool)) 15173270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 15183270476aSTejun Heo 1519db7bccf4STejun Heo /* 1520bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1521bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1522bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1523bc2ae0f5STejun Heo * above the flag definition for details. 1524bc2ae0f5STejun Heo * 1525bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1526bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1527db7bccf4STejun Heo */ 1528bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 15298b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1530bc2ae0f5STejun Heo } else { 1531db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1532f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1533f3421797STejun Heo } 1534c34056a3STejun Heo 1535c34056a3STejun Heo return worker; 1536c34056a3STejun Heo fail: 1537c34056a3STejun Heo if (id >= 0) { 15388b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1539bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 15408b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1541c34056a3STejun Heo } 1542c34056a3STejun Heo kfree(worker); 1543c34056a3STejun Heo return NULL; 1544c34056a3STejun Heo } 1545c34056a3STejun Heo 1546c34056a3STejun Heo /** 1547c34056a3STejun Heo * start_worker - start a newly created worker 1548c34056a3STejun Heo * @worker: worker to start 1549c34056a3STejun Heo * 1550c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1551c34056a3STejun Heo * 1552c34056a3STejun Heo * CONTEXT: 15538b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1554c34056a3STejun Heo */ 1555c34056a3STejun Heo static void start_worker(struct worker *worker) 1556c34056a3STejun Heo { 1557cb444766STejun Heo worker->flags |= WORKER_STARTED; 1558bd7bdd43STejun Heo worker->pool->nr_workers++; 1559c8e55f36STejun Heo worker_enter_idle(worker); 1560c34056a3STejun Heo wake_up_process(worker->task); 1561c34056a3STejun Heo } 1562c34056a3STejun Heo 1563c34056a3STejun Heo /** 1564c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1565c34056a3STejun Heo * @worker: worker to be destroyed 1566c34056a3STejun Heo * 1567c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1568c8e55f36STejun Heo * 1569c8e55f36STejun Heo * CONTEXT: 1570c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1571c34056a3STejun Heo */ 1572c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1573c34056a3STejun Heo { 1574bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1575bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1576c34056a3STejun Heo int id = worker->id; 1577c34056a3STejun Heo 1578c34056a3STejun Heo /* sanity check frenzy */ 1579c34056a3STejun Heo BUG_ON(worker->current_work); 1580affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1581c34056a3STejun Heo 1582c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1583bd7bdd43STejun Heo pool->nr_workers--; 1584c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1585bd7bdd43STejun Heo pool->nr_idle--; 1586c8e55f36STejun Heo 1587c8e55f36STejun Heo list_del_init(&worker->entry); 1588cb444766STejun Heo worker->flags |= WORKER_DIE; 1589c8e55f36STejun Heo 1590c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1591c8e55f36STejun Heo 1592c34056a3STejun Heo kthread_stop(worker->task); 1593c34056a3STejun Heo kfree(worker); 1594c34056a3STejun Heo 15958b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1596bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1597c34056a3STejun Heo } 1598c34056a3STejun Heo 159963d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1600e22bee78STejun Heo { 160163d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 160263d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1603e22bee78STejun Heo 1604e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1605e22bee78STejun Heo 160663d95a91STejun Heo if (too_many_workers(pool)) { 1607e22bee78STejun Heo struct worker *worker; 1608e22bee78STejun Heo unsigned long expires; 1609e22bee78STejun Heo 1610e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 161163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1612e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1613e22bee78STejun Heo 1614e22bee78STejun Heo if (time_before(jiffies, expires)) 161563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1616e22bee78STejun Heo else { 1617e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 161811ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 161963d95a91STejun Heo wake_up_worker(pool); 1620e22bee78STejun Heo } 1621e22bee78STejun Heo } 1622e22bee78STejun Heo 1623e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1624e22bee78STejun Heo } 1625e22bee78STejun Heo 1626e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1627e22bee78STejun Heo { 1628e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1629e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1630f3421797STejun Heo unsigned int cpu; 1631e22bee78STejun Heo 1632e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1633e22bee78STejun Heo return false; 1634e22bee78STejun Heo 1635e22bee78STejun Heo /* mayday mayday mayday */ 1636bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1637f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1638f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1639f3421797STejun Heo cpu = 0; 1640f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1641e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1642e22bee78STejun Heo return true; 1643e22bee78STejun Heo } 1644e22bee78STejun Heo 164563d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1646e22bee78STejun Heo { 164763d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 164863d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1649e22bee78STejun Heo struct work_struct *work; 1650e22bee78STejun Heo 1651e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1652e22bee78STejun Heo 165363d95a91STejun Heo if (need_to_create_worker(pool)) { 1654e22bee78STejun Heo /* 1655e22bee78STejun Heo * We've been trying to create a new worker but 1656e22bee78STejun Heo * haven't been successful. We might be hitting an 1657e22bee78STejun Heo * allocation deadlock. Send distress signals to 1658e22bee78STejun Heo * rescuers. 1659e22bee78STejun Heo */ 166063d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1661e22bee78STejun Heo send_mayday(work); 1662e22bee78STejun Heo } 1663e22bee78STejun Heo 1664e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1665e22bee78STejun Heo 166663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1667e22bee78STejun Heo } 1668e22bee78STejun Heo 1669e22bee78STejun Heo /** 1670e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 167163d95a91STejun Heo * @pool: pool to create a new worker for 1672e22bee78STejun Heo * 167363d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1674e22bee78STejun Heo * have at least one idle worker on return from this function. If 1675e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 167663d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1677e22bee78STejun Heo * possible allocation deadlock. 1678e22bee78STejun Heo * 1679e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1680e22bee78STejun Heo * may_start_working() true. 1681e22bee78STejun Heo * 1682e22bee78STejun Heo * LOCKING: 1683e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1684e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1685e22bee78STejun Heo * manager. 1686e22bee78STejun Heo * 1687e22bee78STejun Heo * RETURNS: 1688e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1689e22bee78STejun Heo * otherwise. 1690e22bee78STejun Heo */ 169163d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 169206bd6ebfSNamhyung Kim __releases(&gcwq->lock) 169306bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1694e22bee78STejun Heo { 169563d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 169663d95a91STejun Heo 169763d95a91STejun Heo if (!need_to_create_worker(pool)) 1698e22bee78STejun Heo return false; 1699e22bee78STejun Heo restart: 17009f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 17019f9c2364STejun Heo 1702e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 170363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1704e22bee78STejun Heo 1705e22bee78STejun Heo while (true) { 1706e22bee78STejun Heo struct worker *worker; 1707e22bee78STejun Heo 1708bc2ae0f5STejun Heo worker = create_worker(pool); 1709e22bee78STejun Heo if (worker) { 171063d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1711e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1712e22bee78STejun Heo start_worker(worker); 171363d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1714e22bee78STejun Heo return true; 1715e22bee78STejun Heo } 1716e22bee78STejun Heo 171763d95a91STejun Heo if (!need_to_create_worker(pool)) 1718e22bee78STejun Heo break; 1719e22bee78STejun Heo 1720e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1721e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 17229f9c2364STejun Heo 172363d95a91STejun Heo if (!need_to_create_worker(pool)) 1724e22bee78STejun Heo break; 1725e22bee78STejun Heo } 1726e22bee78STejun Heo 172763d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1728e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 172963d95a91STejun Heo if (need_to_create_worker(pool)) 1730e22bee78STejun Heo goto restart; 1731e22bee78STejun Heo return true; 1732e22bee78STejun Heo } 1733e22bee78STejun Heo 1734e22bee78STejun Heo /** 1735e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 173663d95a91STejun Heo * @pool: pool to destroy workers for 1737e22bee78STejun Heo * 173863d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1739e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1740e22bee78STejun Heo * 1741e22bee78STejun Heo * LOCKING: 1742e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1743e22bee78STejun Heo * multiple times. Called only from manager. 1744e22bee78STejun Heo * 1745e22bee78STejun Heo * RETURNS: 1746e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1747e22bee78STejun Heo * otherwise. 1748e22bee78STejun Heo */ 174963d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1750e22bee78STejun Heo { 1751e22bee78STejun Heo bool ret = false; 1752e22bee78STejun Heo 175363d95a91STejun Heo while (too_many_workers(pool)) { 1754e22bee78STejun Heo struct worker *worker; 1755e22bee78STejun Heo unsigned long expires; 1756e22bee78STejun Heo 175763d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1758e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1759e22bee78STejun Heo 1760e22bee78STejun Heo if (time_before(jiffies, expires)) { 176163d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1762e22bee78STejun Heo break; 1763e22bee78STejun Heo } 1764e22bee78STejun Heo 1765e22bee78STejun Heo destroy_worker(worker); 1766e22bee78STejun Heo ret = true; 1767e22bee78STejun Heo } 1768e22bee78STejun Heo 1769e22bee78STejun Heo return ret; 1770e22bee78STejun Heo } 1771e22bee78STejun Heo 1772e22bee78STejun Heo /** 1773e22bee78STejun Heo * manage_workers - manage worker pool 1774e22bee78STejun Heo * @worker: self 1775e22bee78STejun Heo * 1776e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1777e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1778e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1779e22bee78STejun Heo * 1780e22bee78STejun Heo * The caller can safely start processing works on false return. On 1781e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1782e22bee78STejun Heo * and may_start_working() is true. 1783e22bee78STejun Heo * 1784e22bee78STejun Heo * CONTEXT: 1785e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1786e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1787e22bee78STejun Heo * 1788e22bee78STejun Heo * RETURNS: 1789e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1790e22bee78STejun Heo * some action was taken. 1791e22bee78STejun Heo */ 1792e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1793e22bee78STejun Heo { 179463d95a91STejun Heo struct worker_pool *pool = worker->pool; 1795e22bee78STejun Heo bool ret = false; 1796e22bee78STejun Heo 179760373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1798e22bee78STejun Heo return ret; 1799e22bee78STejun Heo 180011ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1801e22bee78STejun Heo 1802e22bee78STejun Heo /* 1803e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1804e22bee78STejun Heo * on return. 1805e22bee78STejun Heo */ 180663d95a91STejun Heo ret |= maybe_destroy_workers(pool); 180763d95a91STejun Heo ret |= maybe_create_worker(pool); 1808e22bee78STejun Heo 180960373152STejun Heo mutex_unlock(&pool->manager_mutex); 1810e22bee78STejun Heo return ret; 1811e22bee78STejun Heo } 1812e22bee78STejun Heo 1813a62428c0STejun Heo /** 1814affee4b2STejun Heo * move_linked_works - move linked works to a list 1815affee4b2STejun Heo * @work: start of series of works to be scheduled 1816affee4b2STejun Heo * @head: target list to append @work to 1817affee4b2STejun Heo * @nextp: out paramter for nested worklist walking 1818affee4b2STejun Heo * 1819affee4b2STejun Heo * Schedule linked works starting from @work to @head. Work series to 1820affee4b2STejun Heo * be scheduled starts at @work and includes any consecutive work with 1821affee4b2STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1822affee4b2STejun Heo * 1823affee4b2STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1824affee4b2STejun Heo * the last scheduled work. This allows move_linked_works() to be 1825affee4b2STejun Heo * nested inside outer list_for_each_entry_safe(). 1826affee4b2STejun Heo * 1827affee4b2STejun Heo * CONTEXT: 18288b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1829affee4b2STejun Heo */ 1830affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1831affee4b2STejun Heo struct work_struct **nextp) 1832affee4b2STejun Heo { 1833affee4b2STejun Heo struct work_struct *n; 1834affee4b2STejun Heo 1835affee4b2STejun Heo /* 1836affee4b2STejun Heo * Linked worklist will always end before the end of the list, 1837affee4b2STejun Heo * use NULL for list head. 1838affee4b2STejun Heo */ 1839affee4b2STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1840affee4b2STejun Heo list_move_tail(&work->entry, head); 1841affee4b2STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1842affee4b2STejun Heo break; 1843affee4b2STejun Heo } 1844affee4b2STejun Heo 1845affee4b2STejun Heo /* 1846affee4b2STejun Heo * If we're already inside safe list traversal and have moved 1847affee4b2STejun Heo * multiple works to the scheduled queue, the next position 1848affee4b2STejun Heo * needs to be updated. 1849affee4b2STejun Heo */ 1850affee4b2STejun Heo if (nextp) 1851affee4b2STejun Heo *nextp = n; 1852affee4b2STejun Heo } 1853affee4b2STejun Heo 18541e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 18551e19ffc6STejun Heo { 18561e19ffc6STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 18571da177e4SLinus Torvalds struct work_struct, entry); 18581e19ffc6STejun Heo 1859cdadf009STejun Heo trace_workqueue_activate_work(work); 18603270476aSTejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 18618a2e8e5dSTejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 18621e19ffc6STejun Heo cwq->nr_active++; 18631e19ffc6STejun Heo } 18641e19ffc6STejun Heo 1865affee4b2STejun Heo /** 186673f53c4aSTejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 186773f53c4aSTejun Heo * @cwq: cwq of interest 186873f53c4aSTejun Heo * @color: color of work which left the queue 18698a2e8e5dSTejun Heo * @delayed: for a delayed work 187073f53c4aSTejun Heo * 187173f53c4aSTejun Heo * A work either has completed or is removed from pending queue, 187273f53c4aSTejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 187373f53c4aSTejun Heo * 187473f53c4aSTejun Heo * CONTEXT: 18758b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 187673f53c4aSTejun Heo */ 18778a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 18788a2e8e5dSTejun Heo bool delayed) 187973f53c4aSTejun Heo { 188073f53c4aSTejun Heo /* ignore uncolored works */ 188173f53c4aSTejun Heo if (color == WORK_NO_COLOR) 188273f53c4aSTejun Heo return; 188373f53c4aSTejun Heo 188473f53c4aSTejun Heo cwq->nr_in_flight[color]--; 18851e19ffc6STejun Heo 18868a2e8e5dSTejun Heo if (!delayed) { 18878a2e8e5dSTejun Heo cwq->nr_active--; 1888502ca9d8STejun Heo if (!list_empty(&cwq->delayed_works)) { 18891e19ffc6STejun Heo /* one down, submit a delayed one */ 1890502ca9d8STejun Heo if (cwq->nr_active < cwq->max_active) 18911e19ffc6STejun Heo cwq_activate_first_delayed(cwq); 1892502ca9d8STejun Heo } 18938a2e8e5dSTejun Heo } 189473f53c4aSTejun Heo 189573f53c4aSTejun Heo /* is flush in progress and are we at the flushing tip? */ 189673f53c4aSTejun Heo if (likely(cwq->flush_color != color)) 189773f53c4aSTejun Heo return; 189873f53c4aSTejun Heo 189973f53c4aSTejun Heo /* are there still in-flight works? */ 190073f53c4aSTejun Heo if (cwq->nr_in_flight[color]) 190173f53c4aSTejun Heo return; 190273f53c4aSTejun Heo 190373f53c4aSTejun Heo /* this cwq is done, clear flush_color */ 190473f53c4aSTejun Heo cwq->flush_color = -1; 190573f53c4aSTejun Heo 190673f53c4aSTejun Heo /* 190773f53c4aSTejun Heo * If this was the last cwq, wake up the first flusher. It 190873f53c4aSTejun Heo * will handle the rest. 190973f53c4aSTejun Heo */ 191073f53c4aSTejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 191173f53c4aSTejun Heo complete(&cwq->wq->first_flusher->done); 191273f53c4aSTejun Heo } 191373f53c4aSTejun Heo 191473f53c4aSTejun Heo /** 1915a62428c0STejun Heo * process_one_work - process single work 1916c34056a3STejun Heo * @worker: self 1917a62428c0STejun Heo * @work: work to process 1918a62428c0STejun Heo * 1919a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1920a62428c0STejun Heo * process a single work including synchronization against and 1921a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1922a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1923a62428c0STejun Heo * call this function to process a work. 1924a62428c0STejun Heo * 1925a62428c0STejun Heo * CONTEXT: 19268b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1927a62428c0STejun Heo */ 1928c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 192906bd6ebfSNamhyung Kim __releases(&gcwq->lock) 193006bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 19311da177e4SLinus Torvalds { 19327e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1933bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1934bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1935c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 1936fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 19376bb49e59SDavid Howells work_func_t f = work->func; 193873f53c4aSTejun Heo int work_color; 19397e11629dSTejun Heo struct worker *collision; 19404e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 19414e6045f1SJohannes Berg /* 1942a62428c0STejun Heo * It is permissible to free the struct work_struct from 1943a62428c0STejun Heo * inside the function that is called from it, this we need to 1944a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1945a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1946a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 19474e6045f1SJohannes Berg */ 19484d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 19494d82a1deSPeter Zijlstra 19504d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 19514e6045f1SJohannes Berg #endif 195225511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 195325511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 195425511a47STejun Heo 19557e11629dSTejun Heo /* 19567e11629dSTejun Heo * A single work shouldn't be executed concurrently by 19577e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 19587e11629dSTejun Heo * already processing the work. If so, defer the work to the 19597e11629dSTejun Heo * currently executing one. 19607e11629dSTejun Heo */ 19617e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 19627e11629dSTejun Heo if (unlikely(collision)) { 19637e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 19647e11629dSTejun Heo return; 19657e11629dSTejun Heo } 19661da177e4SLinus Torvalds 1967a62428c0STejun Heo /* claim and process */ 19681da177e4SLinus Torvalds debug_work_deactivate(work); 1969c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 1970c34056a3STejun Heo worker->current_work = work; 19718cca0eeaSTejun Heo worker->current_cwq = cwq; 197273f53c4aSTejun Heo work_color = get_work_color(work); 19737a22ad75STejun Heo 19747a22ad75STejun Heo /* record the current cpu number in the work data and dequeue */ 19757a22ad75STejun Heo set_work_cpu(work, gcwq->cpu); 1976a62428c0STejun Heo list_del_init(&work->entry); 1977a62428c0STejun Heo 1978649027d7STejun Heo /* 1979fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 1980fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 1981fb0e7bebSTejun Heo */ 1982fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 1983fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 1984fb0e7bebSTejun Heo 1985974271c4STejun Heo /* 1986974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 1987974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 1988974271c4STejun Heo */ 198963d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 199063d95a91STejun Heo wake_up_worker(pool); 1991974271c4STejun Heo 19928b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 19931da177e4SLinus Torvalds 199423b2e599SOleg Nesterov work_clear_pending(work); 1995e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 19963295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 1997e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 199865f27f38SDavid Howells f(work); 1999e36c886aSArjan van de Ven /* 2000e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2001e36c886aSArjan van de Ven * point will only record its address. 2002e36c886aSArjan van de Ven */ 2003e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20043295f0efSIngo Molnar lock_map_release(&lockdep_map); 20053295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 20061da177e4SLinus Torvalds 2007d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2008d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2009d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2010a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2011d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2012d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2013d5abe669SPeter Zijlstra debug_show_held_locks(current); 2014d5abe669SPeter Zijlstra dump_stack(); 2015d5abe669SPeter Zijlstra } 2016d5abe669SPeter Zijlstra 20178b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2018a62428c0STejun Heo 2019fb0e7bebSTejun Heo /* clear cpu intensive status */ 2020fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2021fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2022fb0e7bebSTejun Heo 2023a62428c0STejun Heo /* we're done with it, release */ 2024c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2025c34056a3STejun Heo worker->current_work = NULL; 20268cca0eeaSTejun Heo worker->current_cwq = NULL; 20278a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 2030affee4b2STejun Heo /** 2031affee4b2STejun Heo * process_scheduled_works - process scheduled works 2032affee4b2STejun Heo * @worker: self 2033affee4b2STejun Heo * 2034affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2035affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2036affee4b2STejun Heo * fetches a work from the top and executes it. 2037affee4b2STejun Heo * 2038affee4b2STejun Heo * CONTEXT: 20398b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2040affee4b2STejun Heo * multiple times. 2041affee4b2STejun Heo */ 2042affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 20431da177e4SLinus Torvalds { 2044affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2045affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2046a62428c0STejun Heo struct work_struct, entry); 2047c34056a3STejun Heo process_one_work(worker, work); 2048a62428c0STejun Heo } 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20514690c4abSTejun Heo /** 20524690c4abSTejun Heo * worker_thread - the worker thread function 2053c34056a3STejun Heo * @__worker: self 20544690c4abSTejun Heo * 2055e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2056e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2057e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2058e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2059e22bee78STejun Heo * rescuer_thread(). 20604690c4abSTejun Heo */ 2061c34056a3STejun Heo static int worker_thread(void *__worker) 20621da177e4SLinus Torvalds { 2063c34056a3STejun Heo struct worker *worker = __worker; 2064bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2065bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 20661da177e4SLinus Torvalds 2067e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2068e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2069c8e55f36STejun Heo woke_up: 20708b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2071affee4b2STejun Heo 207225511a47STejun Heo /* 207325511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 207425511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 207525511a47STejun Heo */ 207625511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2077c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 207825511a47STejun Heo 207925511a47STejun Heo if (worker->flags & WORKER_DIE) { 2080e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2081c8e55f36STejun Heo return 0; 2082c8e55f36STejun Heo } 2083c8e55f36STejun Heo 208425511a47STejun Heo idle_worker_rebind(worker); 208525511a47STejun Heo goto woke_up; 208625511a47STejun Heo } 208725511a47STejun Heo 2088c8e55f36STejun Heo worker_leave_idle(worker); 2089db7bccf4STejun Heo recheck: 2090e22bee78STejun Heo /* no more worker necessary? */ 209163d95a91STejun Heo if (!need_more_worker(pool)) 2092e22bee78STejun Heo goto sleep; 2093e22bee78STejun Heo 2094e22bee78STejun Heo /* do we need to manage? */ 209563d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2096e22bee78STejun Heo goto recheck; 2097e22bee78STejun Heo 2098c8e55f36STejun Heo /* 2099c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2100c8e55f36STejun Heo * preparing to process a work or actually processing it. 2101c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2102c8e55f36STejun Heo */ 2103c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2104c8e55f36STejun Heo 2105e22bee78STejun Heo /* 2106e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2107e22bee78STejun Heo * at least one idle worker or that someone else has already 2108e22bee78STejun Heo * assumed the manager role. 2109e22bee78STejun Heo */ 2110e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2111e22bee78STejun Heo 2112e22bee78STejun Heo do { 2113affee4b2STejun Heo struct work_struct *work = 2114bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2115affee4b2STejun Heo struct work_struct, entry); 2116affee4b2STejun Heo 2117c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2118affee4b2STejun Heo /* optimization path, not strictly necessary */ 2119affee4b2STejun Heo process_one_work(worker, work); 2120affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2121affee4b2STejun Heo process_scheduled_works(worker); 2122affee4b2STejun Heo } else { 2123c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2124affee4b2STejun Heo process_scheduled_works(worker); 2125affee4b2STejun Heo } 212663d95a91STejun Heo } while (keep_working(pool)); 2127affee4b2STejun Heo 2128e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2129d313dd85STejun Heo sleep: 213063d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2131e22bee78STejun Heo goto recheck; 2132d313dd85STejun Heo 2133c8e55f36STejun Heo /* 2134e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2135e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2136e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2137e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2138e22bee78STejun Heo * prevent losing any event. 2139c8e55f36STejun Heo */ 2140c8e55f36STejun Heo worker_enter_idle(worker); 2141c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 21428b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 21431da177e4SLinus Torvalds schedule(); 2144c8e55f36STejun Heo goto woke_up; 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 2147e22bee78STejun Heo /** 2148e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2149e22bee78STejun Heo * @__wq: the associated workqueue 2150e22bee78STejun Heo * 2151e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2152e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2153e22bee78STejun Heo * 2154e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2155e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2156e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2157e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2158e22bee78STejun Heo * the problem rescuer solves. 2159e22bee78STejun Heo * 2160e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2161e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2162e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2163e22bee78STejun Heo * 2164e22bee78STejun Heo * This should happen rarely. 2165e22bee78STejun Heo */ 2166e22bee78STejun Heo static int rescuer_thread(void *__wq) 2167e22bee78STejun Heo { 2168e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2169e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2170e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2171f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2172e22bee78STejun Heo unsigned int cpu; 2173e22bee78STejun Heo 2174e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2175e22bee78STejun Heo repeat: 2176e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (kthread_should_stop()) 2179e22bee78STejun Heo return 0; 21801da177e4SLinus Torvalds 2181f3421797STejun Heo /* 2182f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2183f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2184f3421797STejun Heo */ 2185f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2186f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2187f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2188bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2189bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2190e22bee78STejun Heo struct work_struct *work, *n; 2191e22bee78STejun Heo 2192e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2193f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2194e22bee78STejun Heo 2195e22bee78STejun Heo /* migrate to the target cpu if possible */ 2196bd7bdd43STejun Heo rescuer->pool = pool; 2197e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2198e22bee78STejun Heo 2199e22bee78STejun Heo /* 2200e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2201e22bee78STejun Heo * process'em. 2202e22bee78STejun Heo */ 2203e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2204bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2205e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2206e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2207e22bee78STejun Heo 2208e22bee78STejun Heo process_scheduled_works(rescuer); 22097576958aSTejun Heo 22107576958aSTejun Heo /* 22117576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 22127576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22137576958aSTejun Heo * and stalling the execution. 22147576958aSTejun Heo */ 221563d95a91STejun Heo if (keep_working(pool)) 221663d95a91STejun Heo wake_up_worker(pool); 22177576958aSTejun Heo 2218e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 2221e22bee78STejun Heo schedule(); 2222e22bee78STejun Heo goto repeat; 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 2225fc2e4d70SOleg Nesterov struct wq_barrier { 2226fc2e4d70SOleg Nesterov struct work_struct work; 2227fc2e4d70SOleg Nesterov struct completion done; 2228fc2e4d70SOleg Nesterov }; 2229fc2e4d70SOleg Nesterov 2230fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2231fc2e4d70SOleg Nesterov { 2232fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2233fc2e4d70SOleg Nesterov complete(&barr->done); 2234fc2e4d70SOleg Nesterov } 2235fc2e4d70SOleg Nesterov 22364690c4abSTejun Heo /** 22374690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 22384690c4abSTejun Heo * @cwq: cwq to insert barrier into 22394690c4abSTejun Heo * @barr: wq_barrier to insert 2240affee4b2STejun Heo * @target: target work to attach @barr to 2241affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 22424690c4abSTejun Heo * 2243affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2244affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2245affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2246affee4b2STejun Heo * cpu. 2247affee4b2STejun Heo * 2248affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2249affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2250affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2251affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2252affee4b2STejun Heo * after a work with LINKED flag set. 2253affee4b2STejun Heo * 2254affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2255affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 22564690c4abSTejun Heo * 22574690c4abSTejun Heo * CONTEXT: 22588b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 22594690c4abSTejun Heo */ 226083c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2261affee4b2STejun Heo struct wq_barrier *barr, 2262affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2263fc2e4d70SOleg Nesterov { 2264affee4b2STejun Heo struct list_head *head; 2265affee4b2STejun Heo unsigned int linked = 0; 2266affee4b2STejun Heo 2267dc186ad7SThomas Gleixner /* 22688b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2269dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2270dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2271dc186ad7SThomas Gleixner * might deadlock. 2272dc186ad7SThomas Gleixner */ 2273ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 227422df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2275fc2e4d70SOleg Nesterov init_completion(&barr->done); 227683c22520SOleg Nesterov 2277affee4b2STejun Heo /* 2278affee4b2STejun Heo * If @target is currently being executed, schedule the 2279affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2280affee4b2STejun Heo */ 2281affee4b2STejun Heo if (worker) 2282affee4b2STejun Heo head = worker->scheduled.next; 2283affee4b2STejun Heo else { 2284affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2285affee4b2STejun Heo 2286affee4b2STejun Heo head = target->entry.next; 2287affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2288affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2289affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2290affee4b2STejun Heo } 2291affee4b2STejun Heo 2292dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2293affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2294affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2295fc2e4d70SOleg Nesterov } 2296fc2e4d70SOleg Nesterov 229773f53c4aSTejun Heo /** 229873f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 229973f53c4aSTejun Heo * @wq: workqueue being flushed 230073f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 230173f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 230273f53c4aSTejun Heo * 230373f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 230473f53c4aSTejun Heo * 230573f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 230673f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 230773f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 230873f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 230973f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 231073f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 231173f53c4aSTejun Heo * 231273f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 231373f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 231473f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 231573f53c4aSTejun Heo * is returned. 231673f53c4aSTejun Heo * 231773f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 231873f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 231973f53c4aSTejun Heo * advanced to @work_color. 232073f53c4aSTejun Heo * 232173f53c4aSTejun Heo * CONTEXT: 232273f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 232373f53c4aSTejun Heo * 232473f53c4aSTejun Heo * RETURNS: 232573f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 232673f53c4aSTejun Heo * otherwise. 232773f53c4aSTejun Heo */ 232873f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 232973f53c4aSTejun Heo int flush_color, int work_color) 23301da177e4SLinus Torvalds { 233173f53c4aSTejun Heo bool wait = false; 233273f53c4aSTejun Heo unsigned int cpu; 23331da177e4SLinus Torvalds 233473f53c4aSTejun Heo if (flush_color >= 0) { 233573f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 233673f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2337dc186ad7SThomas Gleixner } 233814441960SOleg Nesterov 2339f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 234073f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2341bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 23421da177e4SLinus Torvalds 23438b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 234473f53c4aSTejun Heo 234573f53c4aSTejun Heo if (flush_color >= 0) { 234673f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 234773f53c4aSTejun Heo 234873f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 234973f53c4aSTejun Heo cwq->flush_color = flush_color; 235073f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 235173f53c4aSTejun Heo wait = true; 23521da177e4SLinus Torvalds } 235373f53c4aSTejun Heo } 235473f53c4aSTejun Heo 235573f53c4aSTejun Heo if (work_color >= 0) { 235673f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 235773f53c4aSTejun Heo cwq->work_color = work_color; 235873f53c4aSTejun Heo } 235973f53c4aSTejun Heo 23608b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 236373f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 236473f53c4aSTejun Heo complete(&wq->first_flusher->done); 236573f53c4aSTejun Heo 236673f53c4aSTejun Heo return wait; 236783c22520SOleg Nesterov } 23681da177e4SLinus Torvalds 23690fcb78c2SRolf Eike Beer /** 23701da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 23710fcb78c2SRolf Eike Beer * @wq: workqueue to flush 23721da177e4SLinus Torvalds * 23731da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 23741da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 23751da177e4SLinus Torvalds * 2376fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2377fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 23781da177e4SLinus Torvalds */ 23797ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 23801da177e4SLinus Torvalds { 238173f53c4aSTejun Heo struct wq_flusher this_flusher = { 238273f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 238373f53c4aSTejun Heo .flush_color = -1, 238473f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 238573f53c4aSTejun Heo }; 238673f53c4aSTejun Heo int next_color; 2387b1f4ec17SOleg Nesterov 23883295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 23893295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 239073f53c4aSTejun Heo 239173f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 239273f53c4aSTejun Heo 239373f53c4aSTejun Heo /* 239473f53c4aSTejun Heo * Start-to-wait phase 239573f53c4aSTejun Heo */ 239673f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 239773f53c4aSTejun Heo 239873f53c4aSTejun Heo if (next_color != wq->flush_color) { 239973f53c4aSTejun Heo /* 240073f53c4aSTejun Heo * Color space is not full. The current work_color 240173f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 240273f53c4aSTejun Heo * by one. 240373f53c4aSTejun Heo */ 240473f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 240573f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 240673f53c4aSTejun Heo wq->work_color = next_color; 240773f53c4aSTejun Heo 240873f53c4aSTejun Heo if (!wq->first_flusher) { 240973f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 241073f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 241173f53c4aSTejun Heo 241273f53c4aSTejun Heo wq->first_flusher = &this_flusher; 241373f53c4aSTejun Heo 241473f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 241573f53c4aSTejun Heo wq->work_color)) { 241673f53c4aSTejun Heo /* nothing to flush, done */ 241773f53c4aSTejun Heo wq->flush_color = next_color; 241873f53c4aSTejun Heo wq->first_flusher = NULL; 241973f53c4aSTejun Heo goto out_unlock; 242073f53c4aSTejun Heo } 242173f53c4aSTejun Heo } else { 242273f53c4aSTejun Heo /* wait in queue */ 242373f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 242473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 242573f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 242673f53c4aSTejun Heo } 242773f53c4aSTejun Heo } else { 242873f53c4aSTejun Heo /* 242973f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 243073f53c4aSTejun Heo * The next flush completion will assign us 243173f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 243273f53c4aSTejun Heo */ 243373f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 243473f53c4aSTejun Heo } 243573f53c4aSTejun Heo 243673f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 243773f53c4aSTejun Heo 243873f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 243973f53c4aSTejun Heo 244073f53c4aSTejun Heo /* 244173f53c4aSTejun Heo * Wake-up-and-cascade phase 244273f53c4aSTejun Heo * 244373f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 244473f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 244573f53c4aSTejun Heo */ 244673f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 244773f53c4aSTejun Heo return; 244873f53c4aSTejun Heo 244973f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 245073f53c4aSTejun Heo 24514ce48b37STejun Heo /* we might have raced, check again with mutex held */ 24524ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 24534ce48b37STejun Heo goto out_unlock; 24544ce48b37STejun Heo 245573f53c4aSTejun Heo wq->first_flusher = NULL; 245673f53c4aSTejun Heo 245773f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 245873f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 245973f53c4aSTejun Heo 246073f53c4aSTejun Heo while (true) { 246173f53c4aSTejun Heo struct wq_flusher *next, *tmp; 246273f53c4aSTejun Heo 246373f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 246473f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 246573f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 246673f53c4aSTejun Heo break; 246773f53c4aSTejun Heo list_del_init(&next->list); 246873f53c4aSTejun Heo complete(&next->done); 246973f53c4aSTejun Heo } 247073f53c4aSTejun Heo 247173f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 247273f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 247373f53c4aSTejun Heo 247473f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 247573f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 247673f53c4aSTejun Heo 247773f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 247873f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 247973f53c4aSTejun Heo /* 248073f53c4aSTejun Heo * Assign the same color to all overflowed 248173f53c4aSTejun Heo * flushers, advance work_color and append to 248273f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 248373f53c4aSTejun Heo * phase for these overflowed flushers. 248473f53c4aSTejun Heo */ 248573f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 248673f53c4aSTejun Heo tmp->flush_color = wq->work_color; 248773f53c4aSTejun Heo 248873f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 248973f53c4aSTejun Heo 249073f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 249173f53c4aSTejun Heo &wq->flusher_queue); 249273f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 249373f53c4aSTejun Heo } 249473f53c4aSTejun Heo 249573f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 249673f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 249773f53c4aSTejun Heo break; 249873f53c4aSTejun Heo } 249973f53c4aSTejun Heo 250073f53c4aSTejun Heo /* 250173f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 250273f53c4aSTejun Heo * the new first flusher and arm cwqs. 250373f53c4aSTejun Heo */ 250473f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 250573f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 250673f53c4aSTejun Heo 250773f53c4aSTejun Heo list_del_init(&next->list); 250873f53c4aSTejun Heo wq->first_flusher = next; 250973f53c4aSTejun Heo 251073f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 251173f53c4aSTejun Heo break; 251273f53c4aSTejun Heo 251373f53c4aSTejun Heo /* 251473f53c4aSTejun Heo * Meh... this color is already done, clear first 251573f53c4aSTejun Heo * flusher and repeat cascading. 251673f53c4aSTejun Heo */ 251773f53c4aSTejun Heo wq->first_flusher = NULL; 251873f53c4aSTejun Heo } 251973f53c4aSTejun Heo 252073f53c4aSTejun Heo out_unlock: 252173f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 25221da177e4SLinus Torvalds } 2523ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 25241da177e4SLinus Torvalds 25259c5a2ba7STejun Heo /** 25269c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 25279c5a2ba7STejun Heo * @wq: workqueue to drain 25289c5a2ba7STejun Heo * 25299c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 25309c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 25319c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 25329c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 25339c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 25349c5a2ba7STejun Heo * takes too long. 25359c5a2ba7STejun Heo */ 25369c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 25379c5a2ba7STejun Heo { 25389c5a2ba7STejun Heo unsigned int flush_cnt = 0; 25399c5a2ba7STejun Heo unsigned int cpu; 25409c5a2ba7STejun Heo 25419c5a2ba7STejun Heo /* 25429c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 25439c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 25449c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 25459c5a2ba7STejun Heo */ 25469c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25479c5a2ba7STejun Heo if (!wq->nr_drainers++) 25489c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 25499c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25509c5a2ba7STejun Heo reflush: 25519c5a2ba7STejun Heo flush_workqueue(wq); 25529c5a2ba7STejun Heo 25539c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 25549c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2555fa2563e4SThomas Tuttle bool drained; 25569c5a2ba7STejun Heo 2557bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2558fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2559bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2560fa2563e4SThomas Tuttle 2561fa2563e4SThomas Tuttle if (drained) 25629c5a2ba7STejun Heo continue; 25639c5a2ba7STejun Heo 25649c5a2ba7STejun Heo if (++flush_cnt == 10 || 25659c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 25669c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 25679c5a2ba7STejun Heo wq->name, flush_cnt); 25689c5a2ba7STejun Heo goto reflush; 25699c5a2ba7STejun Heo } 25709c5a2ba7STejun Heo 25719c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25729c5a2ba7STejun Heo if (!--wq->nr_drainers) 25739c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 25749c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25759c5a2ba7STejun Heo } 25769c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 25779c5a2ba7STejun Heo 2578baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2579baf59022STejun Heo bool wait_executing) 2580baf59022STejun Heo { 2581baf59022STejun Heo struct worker *worker = NULL; 2582baf59022STejun Heo struct global_cwq *gcwq; 2583baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2584baf59022STejun Heo 2585baf59022STejun Heo might_sleep(); 2586baf59022STejun Heo gcwq = get_work_gcwq(work); 2587baf59022STejun Heo if (!gcwq) 2588baf59022STejun Heo return false; 2589baf59022STejun Heo 2590baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2591baf59022STejun Heo if (!list_empty(&work->entry)) { 2592baf59022STejun Heo /* 2593baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2594baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2595baf59022STejun Heo * are not going to wait. 2596baf59022STejun Heo */ 2597baf59022STejun Heo smp_rmb(); 2598baf59022STejun Heo cwq = get_work_cwq(work); 2599bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2600baf59022STejun Heo goto already_gone; 2601baf59022STejun Heo } else if (wait_executing) { 2602baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2603baf59022STejun Heo if (!worker) 2604baf59022STejun Heo goto already_gone; 2605baf59022STejun Heo cwq = worker->current_cwq; 2606baf59022STejun Heo } else 2607baf59022STejun Heo goto already_gone; 2608baf59022STejun Heo 2609baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2610baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2611baf59022STejun Heo 2612e159489bSTejun Heo /* 2613e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2614e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2615e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2616e159489bSTejun Heo * access. 2617e159489bSTejun Heo */ 2618e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2619baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2620e159489bSTejun Heo else 2621e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2622baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2623e159489bSTejun Heo 2624baf59022STejun Heo return true; 2625baf59022STejun Heo already_gone: 2626baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2627baf59022STejun Heo return false; 2628baf59022STejun Heo } 2629baf59022STejun Heo 2630db700897SOleg Nesterov /** 2631401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2632401a8d04STejun Heo * @work: the work to flush 2633db700897SOleg Nesterov * 2634401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2635401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2636401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2637401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2638401a8d04STejun Heo * some of the CPUs from earlier queueing. 2639a67da70dSOleg Nesterov * 2640401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2641401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2642401a8d04STejun Heo * been requeued since flush started. 2643401a8d04STejun Heo * 2644401a8d04STejun Heo * RETURNS: 2645401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2646401a8d04STejun Heo * %false if it was already idle. 2647db700897SOleg Nesterov */ 2648401a8d04STejun Heo bool flush_work(struct work_struct *work) 2649db700897SOleg Nesterov { 2650db700897SOleg Nesterov struct wq_barrier barr; 2651db700897SOleg Nesterov 26520976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 26530976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 26540976dfc1SStephen Boyd 2655baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2656db700897SOleg Nesterov wait_for_completion(&barr.done); 2657dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2658401a8d04STejun Heo return true; 2659baf59022STejun Heo } else 2660401a8d04STejun Heo return false; 2661db700897SOleg Nesterov } 2662db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2663db700897SOleg Nesterov 2664401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2665401a8d04STejun Heo { 2666401a8d04STejun Heo struct wq_barrier barr; 2667401a8d04STejun Heo struct worker *worker; 2668401a8d04STejun Heo 2669401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2670401a8d04STejun Heo 2671401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2672401a8d04STejun Heo if (unlikely(worker)) 2673401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2674401a8d04STejun Heo 2675401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2676401a8d04STejun Heo 2677401a8d04STejun Heo if (unlikely(worker)) { 2678401a8d04STejun Heo wait_for_completion(&barr.done); 2679401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2680401a8d04STejun Heo return true; 2681401a8d04STejun Heo } else 2682401a8d04STejun Heo return false; 2683401a8d04STejun Heo } 2684401a8d04STejun Heo 2685401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2686401a8d04STejun Heo { 2687401a8d04STejun Heo bool ret = false; 2688401a8d04STejun Heo int cpu; 2689401a8d04STejun Heo 2690401a8d04STejun Heo might_sleep(); 2691401a8d04STejun Heo 2692401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2693401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2694401a8d04STejun Heo 2695401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2696401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2697401a8d04STejun Heo return ret; 2698401a8d04STejun Heo } 2699401a8d04STejun Heo 270009383498STejun Heo /** 270109383498STejun Heo * flush_work_sync - wait until a work has finished execution 270209383498STejun Heo * @work: the work to flush 270309383498STejun Heo * 270409383498STejun Heo * Wait until @work has finished execution. On return, it's 270509383498STejun Heo * guaranteed that all queueing instances of @work which happened 270609383498STejun Heo * before this function is called are finished. In other words, if 270709383498STejun Heo * @work hasn't been requeued since this function was called, @work is 270809383498STejun Heo * guaranteed to be idle on return. 270909383498STejun Heo * 271009383498STejun Heo * RETURNS: 271109383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 271209383498STejun Heo * %false if it was already idle. 271309383498STejun Heo */ 271409383498STejun Heo bool flush_work_sync(struct work_struct *work) 271509383498STejun Heo { 271609383498STejun Heo struct wq_barrier barr; 271709383498STejun Heo bool pending, waited; 271809383498STejun Heo 271909383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 272009383498STejun Heo pending = start_flush_work(work, &barr, false); 272109383498STejun Heo 272209383498STejun Heo /* wait for executions to finish */ 272309383498STejun Heo waited = wait_on_work(work); 272409383498STejun Heo 272509383498STejun Heo /* wait for the pending one */ 272609383498STejun Heo if (pending) { 272709383498STejun Heo wait_for_completion(&barr.done); 272809383498STejun Heo destroy_work_on_stack(&barr.work); 272909383498STejun Heo } 273009383498STejun Heo 273109383498STejun Heo return pending || waited; 273209383498STejun Heo } 273309383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 273409383498STejun Heo 27356e84d644SOleg Nesterov /* 27361f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 27376e84d644SOleg Nesterov * so this work can't be re-armed in any way. 27386e84d644SOleg Nesterov */ 27396e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 27406e84d644SOleg Nesterov { 27418b03ae3cSTejun Heo struct global_cwq *gcwq; 27421f1f642eSOleg Nesterov int ret = -1; 27436e84d644SOleg Nesterov 274422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 27451f1f642eSOleg Nesterov return 0; 27466e84d644SOleg Nesterov 27476e84d644SOleg Nesterov /* 27486e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 27496e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 27506e84d644SOleg Nesterov */ 27517a22ad75STejun Heo gcwq = get_work_gcwq(work); 27527a22ad75STejun Heo if (!gcwq) 27536e84d644SOleg Nesterov return ret; 27546e84d644SOleg Nesterov 27558b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 27566e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 27576e84d644SOleg Nesterov /* 27587a22ad75STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 27596e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 27606e84d644SOleg Nesterov * insert_work()->wmb(). 27616e84d644SOleg Nesterov */ 27626e84d644SOleg Nesterov smp_rmb(); 27637a22ad75STejun Heo if (gcwq == get_work_gcwq(work)) { 2764dc186ad7SThomas Gleixner debug_work_deactivate(work); 27656e84d644SOleg Nesterov list_del_init(&work->entry); 27667a22ad75STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 27678a2e8e5dSTejun Heo get_work_color(work), 27688a2e8e5dSTejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 27696e84d644SOleg Nesterov ret = 1; 27706e84d644SOleg Nesterov } 27716e84d644SOleg Nesterov } 27728b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 27736e84d644SOleg Nesterov 27746e84d644SOleg Nesterov return ret; 27756e84d644SOleg Nesterov } 27766e84d644SOleg Nesterov 2777401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work, 27781f1f642eSOleg Nesterov struct timer_list* timer) 27791f1f642eSOleg Nesterov { 27801f1f642eSOleg Nesterov int ret; 27811f1f642eSOleg Nesterov 27821f1f642eSOleg Nesterov do { 27831f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 27841f1f642eSOleg Nesterov if (!ret) 27851f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 27861f1f642eSOleg Nesterov wait_on_work(work); 27871f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 27881f1f642eSOleg Nesterov 27897a22ad75STejun Heo clear_work_data(work); 27901f1f642eSOleg Nesterov return ret; 27911f1f642eSOleg Nesterov } 27921f1f642eSOleg Nesterov 27936e84d644SOleg Nesterov /** 2794401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2795401a8d04STejun Heo * @work: the work to cancel 27966e84d644SOleg Nesterov * 2797401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2798401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2799401a8d04STejun Heo * another workqueue. On return from this function, @work is 2800401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28011f1f642eSOleg Nesterov * 2802401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2803401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28046e84d644SOleg Nesterov * 2805401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28066e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2807401a8d04STejun Heo * 2808401a8d04STejun Heo * RETURNS: 2809401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28106e84d644SOleg Nesterov */ 2811401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28126e84d644SOleg Nesterov { 28131f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 2814b89deed3SOleg Nesterov } 281528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2816b89deed3SOleg Nesterov 28176e84d644SOleg Nesterov /** 2818401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2819401a8d04STejun Heo * @dwork: the delayed work to flush 28206e84d644SOleg Nesterov * 2821401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2822401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2823401a8d04STejun Heo * considers the last queueing instance of @dwork. 28241f1f642eSOleg Nesterov * 2825401a8d04STejun Heo * RETURNS: 2826401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2827401a8d04STejun Heo * %false if it was already idle. 28286e84d644SOleg Nesterov */ 2829401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2830401a8d04STejun Heo { 2831401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 2832401a8d04STejun Heo __queue_work(raw_smp_processor_id(), 2833401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 2834401a8d04STejun Heo return flush_work(&dwork->work); 2835401a8d04STejun Heo } 2836401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2837401a8d04STejun Heo 2838401a8d04STejun Heo /** 283909383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 284009383498STejun Heo * @dwork: the delayed work to flush 284109383498STejun Heo * 284209383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 284309383498STejun Heo * execution immediately. Other than timer handling, its behavior 284409383498STejun Heo * is identical to flush_work_sync(). 284509383498STejun Heo * 284609383498STejun Heo * RETURNS: 284709383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 284809383498STejun Heo * %false if it was already idle. 284909383498STejun Heo */ 285009383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 285109383498STejun Heo { 285209383498STejun Heo if (del_timer_sync(&dwork->timer)) 285309383498STejun Heo __queue_work(raw_smp_processor_id(), 285409383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 285509383498STejun Heo return flush_work_sync(&dwork->work); 285609383498STejun Heo } 285709383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 285809383498STejun Heo 285909383498STejun Heo /** 2860401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2861401a8d04STejun Heo * @dwork: the delayed work cancel 2862401a8d04STejun Heo * 2863401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2864401a8d04STejun Heo * 2865401a8d04STejun Heo * RETURNS: 2866401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2867401a8d04STejun Heo */ 2868401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 28696e84d644SOleg Nesterov { 28701f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 28716e84d644SOleg Nesterov } 2872f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 28731da177e4SLinus Torvalds 28740fcb78c2SRolf Eike Beer /** 28750fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 28760fcb78c2SRolf Eike Beer * @work: job to be done 28770fcb78c2SRolf Eike Beer * 28785b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 28795b0f437dSBart Van Assche * non-zero otherwise. 28805b0f437dSBart Van Assche * 28815b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 28825b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 28835b0f437dSBart Van Assche * workqueue otherwise. 28840fcb78c2SRolf Eike Beer */ 28857ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 28861da177e4SLinus Torvalds { 2887d320c038STejun Heo return queue_work(system_wq, work); 28881da177e4SLinus Torvalds } 2889ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 28901da177e4SLinus Torvalds 2891c1a220e7SZhang Rui /* 2892c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2893c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2894c1a220e7SZhang Rui * @work: job to be done 2895c1a220e7SZhang Rui * 2896c1a220e7SZhang Rui * This puts a job on a specific cpu 2897c1a220e7SZhang Rui */ 2898c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 2899c1a220e7SZhang Rui { 2900d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2901c1a220e7SZhang Rui } 2902c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2903c1a220e7SZhang Rui 29040fcb78c2SRolf Eike Beer /** 29050fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 290652bad64dSDavid Howells * @dwork: job to be done 290752bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 29080fcb78c2SRolf Eike Beer * 29090fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29100fcb78c2SRolf Eike Beer * workqueue. 29110fcb78c2SRolf Eike Beer */ 29127ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 291382f67cd9SIngo Molnar unsigned long delay) 29141da177e4SLinus Torvalds { 2915d320c038STejun Heo return queue_delayed_work(system_wq, dwork, delay); 29161da177e4SLinus Torvalds } 2917ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 29181da177e4SLinus Torvalds 29190fcb78c2SRolf Eike Beer /** 29200fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29210fcb78c2SRolf Eike Beer * @cpu: cpu to use 292252bad64dSDavid Howells * @dwork: job to be done 29230fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29240fcb78c2SRolf Eike Beer * 29250fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29260fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29270fcb78c2SRolf Eike Beer */ 29281da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 292952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 29301da177e4SLinus Torvalds { 2931d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29321da177e4SLinus Torvalds } 2933ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29341da177e4SLinus Torvalds 2935b6136773SAndrew Morton /** 293631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2937b6136773SAndrew Morton * @func: the function to call 2938b6136773SAndrew Morton * 293931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 294031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2941b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 294231ddd871STejun Heo * 294331ddd871STejun Heo * RETURNS: 294431ddd871STejun Heo * 0 on success, -errno on failure. 2945b6136773SAndrew Morton */ 294665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 294715316ba8SChristoph Lameter { 294815316ba8SChristoph Lameter int cpu; 294938f51568SNamhyung Kim struct work_struct __percpu *works; 295015316ba8SChristoph Lameter 2951b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2952b6136773SAndrew Morton if (!works) 295315316ba8SChristoph Lameter return -ENOMEM; 2954b6136773SAndrew Morton 295595402b38SGautham R Shenoy get_online_cpus(); 295693981800STejun Heo 295715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29589bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29599bfb1839SIngo Molnar 29609bfb1839SIngo Molnar INIT_WORK(work, func); 29618de6d308SOleg Nesterov schedule_work_on(cpu, work); 296215316ba8SChristoph Lameter } 296393981800STejun Heo 296493981800STejun Heo for_each_online_cpu(cpu) 29658616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 296693981800STejun Heo 296795402b38SGautham R Shenoy put_online_cpus(); 2968b6136773SAndrew Morton free_percpu(works); 296915316ba8SChristoph Lameter return 0; 297015316ba8SChristoph Lameter } 297115316ba8SChristoph Lameter 2972eef6a7d5SAlan Stern /** 2973eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 2974eef6a7d5SAlan Stern * 2975eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 2976eef6a7d5SAlan Stern * completion. 2977eef6a7d5SAlan Stern * 2978eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 2979eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 2980eef6a7d5SAlan Stern * will lead to deadlock: 2981eef6a7d5SAlan Stern * 2982eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 2983eef6a7d5SAlan Stern * a lock held by your code or its caller. 2984eef6a7d5SAlan Stern * 2985eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 2986eef6a7d5SAlan Stern * 2987eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 2988eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 2989eef6a7d5SAlan Stern * what locks they need, which you have no control over. 2990eef6a7d5SAlan Stern * 2991eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 2992eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 2993eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 2994eef6a7d5SAlan Stern * cancel_work_sync() instead. 2995eef6a7d5SAlan Stern */ 29961da177e4SLinus Torvalds void flush_scheduled_work(void) 29971da177e4SLinus Torvalds { 2998d320c038STejun Heo flush_workqueue(system_wq); 29991da177e4SLinus Torvalds } 3000ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds /** 30031fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30041fa44ecaSJames Bottomley * @fn: the function to execute 30051fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30061fa44ecaSJames Bottomley * be available when the work executes) 30071fa44ecaSJames Bottomley * 30081fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30091fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30101fa44ecaSJames Bottomley * 30111fa44ecaSJames Bottomley * Returns: 0 - function was executed 30121fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30131fa44ecaSJames Bottomley */ 301465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30151fa44ecaSJames Bottomley { 30161fa44ecaSJames Bottomley if (!in_interrupt()) { 301765f27f38SDavid Howells fn(&ew->work); 30181fa44ecaSJames Bottomley return 0; 30191fa44ecaSJames Bottomley } 30201fa44ecaSJames Bottomley 302165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30221fa44ecaSJames Bottomley schedule_work(&ew->work); 30231fa44ecaSJames Bottomley 30241fa44ecaSJames Bottomley return 1; 30251fa44ecaSJames Bottomley } 30261fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30271fa44ecaSJames Bottomley 30281da177e4SLinus Torvalds int keventd_up(void) 30291da177e4SLinus Torvalds { 3030d320c038STejun Heo return system_wq != NULL; 30311da177e4SLinus Torvalds } 30321da177e4SLinus Torvalds 3033bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30341da177e4SLinus Torvalds { 30353af24433SOleg Nesterov /* 30360f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30370f900049STejun Heo * Make sure that the alignment isn't lower than that of 30380f900049STejun Heo * unsigned long long. 30393af24433SOleg Nesterov */ 30400f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 30410f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 30420f900049STejun Heo __alignof__(unsigned long long)); 30433af24433SOleg Nesterov 3044e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3045f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3046931ac77eSTejun Heo else { 30470f900049STejun Heo void *ptr; 3048e1d8aa9fSFrederic Weisbecker 30490f900049STejun Heo /* 3050f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3051f3421797STejun Heo * pointer at the end pointing back to the originally 3052f3421797STejun Heo * allocated pointer which will be used for free. 30530f900049STejun Heo */ 3054bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3055bdbc5dd7STejun Heo if (ptr) { 3056bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3057bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3058bdbc5dd7STejun Heo } 30593af24433SOleg Nesterov } 30603af24433SOleg Nesterov 30610415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3062bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3063bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 30640f900049STejun Heo } 30650f900049STejun Heo 3066bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 306706ba38a9SOleg Nesterov { 3068e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3069bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3070f3421797STejun Heo else if (wq->cpu_wq.single) { 3071f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3072f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 307306ba38a9SOleg Nesterov } 307406ba38a9SOleg Nesterov } 307506ba38a9SOleg Nesterov 3076f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3077f3421797STejun Heo const char *name) 3078b71ab8c2STejun Heo { 3079f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3080f3421797STejun Heo 3081f3421797STejun Heo if (max_active < 1 || max_active > lim) 3082b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3083b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3084f3421797STejun Heo max_active, name, 1, lim); 3085b71ab8c2STejun Heo 3086f3421797STejun Heo return clamp_val(max_active, 1, lim); 3087b71ab8c2STejun Heo } 3088b71ab8c2STejun Heo 3089b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 309097e37d7bSTejun Heo unsigned int flags, 30911e19ffc6STejun Heo int max_active, 3092eb13ba87SJohannes Berg struct lock_class_key *key, 3093b196be89STejun Heo const char *lock_name, ...) 30943af24433SOleg Nesterov { 3095b196be89STejun Heo va_list args, args1; 30963af24433SOleg Nesterov struct workqueue_struct *wq; 3097c34056a3STejun Heo unsigned int cpu; 3098b196be89STejun Heo size_t namelen; 3099b196be89STejun Heo 3100b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3101b196be89STejun Heo va_start(args, lock_name); 3102b196be89STejun Heo va_copy(args1, args); 3103b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3104b196be89STejun Heo 3105b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3106b196be89STejun Heo if (!wq) 3107b196be89STejun Heo goto err; 3108b196be89STejun Heo 3109b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3110b196be89STejun Heo va_end(args); 3111b196be89STejun Heo va_end(args1); 31123af24433SOleg Nesterov 3113f3421797STejun Heo /* 31146370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31156370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31166370a6adSTejun Heo */ 31176370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31186370a6adSTejun Heo flags |= WQ_RESCUER; 31196370a6adSTejun Heo 3120d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3121b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31223af24433SOleg Nesterov 3123b196be89STejun Heo /* init wq */ 312497e37d7bSTejun Heo wq->flags = flags; 3125a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 312673f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 312773f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 312873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 312973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31303af24433SOleg Nesterov 3131eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3132cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31333af24433SOleg Nesterov 3134bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3135bdbc5dd7STejun Heo goto err; 3136bdbc5dd7STejun Heo 3137f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 31381537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 31398b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 31403270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 31411537663fSTejun Heo 31420f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 31433270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3144c34056a3STejun Heo cwq->wq = wq; 314573f53c4aSTejun Heo cwq->flush_color = -1; 31461e19ffc6STejun Heo cwq->max_active = max_active; 31471e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3148e22bee78STejun Heo } 31491537663fSTejun Heo 3150e22bee78STejun Heo if (flags & WQ_RESCUER) { 3151e22bee78STejun Heo struct worker *rescuer; 3152e22bee78STejun Heo 3153f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3154e22bee78STejun Heo goto err; 3155e22bee78STejun Heo 3156e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3157e22bee78STejun Heo if (!rescuer) 3158e22bee78STejun Heo goto err; 3159e22bee78STejun Heo 3160b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3161b196be89STejun Heo wq->name); 3162e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3163e22bee78STejun Heo goto err; 3164e22bee78STejun Heo 3165e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3166e22bee78STejun Heo wake_up_process(rescuer->task); 31673af24433SOleg Nesterov } 31681537663fSTejun Heo 31693af24433SOleg Nesterov /* 3170a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3171a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3172a0a1a5fdSTejun Heo * workqueue to workqueues list. 31733af24433SOleg Nesterov */ 31743af24433SOleg Nesterov spin_lock(&workqueue_lock); 3175a0a1a5fdSTejun Heo 317658a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3177f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3178a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3179a0a1a5fdSTejun Heo 31803af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3181a0a1a5fdSTejun Heo 31823af24433SOleg Nesterov spin_unlock(&workqueue_lock); 31833af24433SOleg Nesterov 31843af24433SOleg Nesterov return wq; 31854690c4abSTejun Heo err: 31864690c4abSTejun Heo if (wq) { 3187bdbc5dd7STejun Heo free_cwqs(wq); 3188f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3189e22bee78STejun Heo kfree(wq->rescuer); 31904690c4abSTejun Heo kfree(wq); 31913af24433SOleg Nesterov } 31924690c4abSTejun Heo return NULL; 31931da177e4SLinus Torvalds } 3194d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 31951da177e4SLinus Torvalds 31963af24433SOleg Nesterov /** 31973af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 31983af24433SOleg Nesterov * @wq: target workqueue 31993af24433SOleg Nesterov * 32003af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32013af24433SOleg Nesterov */ 32023af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32033af24433SOleg Nesterov { 3204c8e55f36STejun Heo unsigned int cpu; 32053af24433SOleg Nesterov 32069c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32079c5a2ba7STejun Heo drain_workqueue(wq); 3208c8efcc25STejun Heo 3209a0a1a5fdSTejun Heo /* 3210a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3211a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3212a0a1a5fdSTejun Heo */ 321395402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32143af24433SOleg Nesterov list_del(&wq->list); 321595402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32163af24433SOleg Nesterov 3217e22bee78STejun Heo /* sanity check */ 3218f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 321973f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 322073f53c4aSTejun Heo int i; 32213af24433SOleg Nesterov 322273f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 322373f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32241e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32251e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 322673f53c4aSTejun Heo } 32271537663fSTejun Heo 3228e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3229e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3230f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32318d9df9f0SXiaotian Feng kfree(wq->rescuer); 3232e22bee78STejun Heo } 3233e22bee78STejun Heo 3234bdbc5dd7STejun Heo free_cwqs(wq); 32353af24433SOleg Nesterov kfree(wq); 32363af24433SOleg Nesterov } 32373af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32383af24433SOleg Nesterov 3239dcd989cbSTejun Heo /** 3240dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3241dcd989cbSTejun Heo * @wq: target workqueue 3242dcd989cbSTejun Heo * @max_active: new max_active value. 3243dcd989cbSTejun Heo * 3244dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3245dcd989cbSTejun Heo * 3246dcd989cbSTejun Heo * CONTEXT: 3247dcd989cbSTejun Heo * Don't call from IRQ context. 3248dcd989cbSTejun Heo */ 3249dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3250dcd989cbSTejun Heo { 3251dcd989cbSTejun Heo unsigned int cpu; 3252dcd989cbSTejun Heo 3253f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3254dcd989cbSTejun Heo 3255dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3256dcd989cbSTejun Heo 3257dcd989cbSTejun Heo wq->saved_max_active = max_active; 3258dcd989cbSTejun Heo 3259f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3260dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3261dcd989cbSTejun Heo 3262dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3263dcd989cbSTejun Heo 326458a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3265dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3266dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3267dcd989cbSTejun Heo 3268dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3269dcd989cbSTejun Heo } 3270dcd989cbSTejun Heo 3271dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3272dcd989cbSTejun Heo } 3273dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3274dcd989cbSTejun Heo 3275dcd989cbSTejun Heo /** 3276dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3277dcd989cbSTejun Heo * @cpu: CPU in question 3278dcd989cbSTejun Heo * @wq: target workqueue 3279dcd989cbSTejun Heo * 3280dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3281dcd989cbSTejun Heo * no synchronization around this function and the test result is 3282dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3283dcd989cbSTejun Heo * 3284dcd989cbSTejun Heo * RETURNS: 3285dcd989cbSTejun Heo * %true if congested, %false otherwise. 3286dcd989cbSTejun Heo */ 3287dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3288dcd989cbSTejun Heo { 3289dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3290dcd989cbSTejun Heo 3291dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3292dcd989cbSTejun Heo } 3293dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3294dcd989cbSTejun Heo 3295dcd989cbSTejun Heo /** 3296dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3297dcd989cbSTejun Heo * @work: the work of interest 3298dcd989cbSTejun Heo * 3299dcd989cbSTejun Heo * RETURNS: 3300bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3301dcd989cbSTejun Heo */ 3302dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3303dcd989cbSTejun Heo { 3304dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3305dcd989cbSTejun Heo 3306bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3307dcd989cbSTejun Heo } 3308dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3309dcd989cbSTejun Heo 3310dcd989cbSTejun Heo /** 3311dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3312dcd989cbSTejun Heo * @work: the work to be tested 3313dcd989cbSTejun Heo * 3314dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3315dcd989cbSTejun Heo * synchronization around this function and the test result is 3316dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3317dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3318dcd989cbSTejun Heo * running state. 3319dcd989cbSTejun Heo * 3320dcd989cbSTejun Heo * RETURNS: 3321dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3322dcd989cbSTejun Heo */ 3323dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3324dcd989cbSTejun Heo { 3325dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3326dcd989cbSTejun Heo unsigned long flags; 3327dcd989cbSTejun Heo unsigned int ret = 0; 3328dcd989cbSTejun Heo 3329dcd989cbSTejun Heo if (!gcwq) 3330dcd989cbSTejun Heo return false; 3331dcd989cbSTejun Heo 3332dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3333dcd989cbSTejun Heo 3334dcd989cbSTejun Heo if (work_pending(work)) 3335dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3336dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3337dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3338dcd989cbSTejun Heo 3339dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3340dcd989cbSTejun Heo 3341dcd989cbSTejun Heo return ret; 3342dcd989cbSTejun Heo } 3343dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3344dcd989cbSTejun Heo 3345db7bccf4STejun Heo /* 3346db7bccf4STejun Heo * CPU hotplug. 3347db7bccf4STejun Heo * 3348e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3349e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3350e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3351e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3352e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3353e22bee78STejun Heo * blocked draining impractical. 3354e22bee78STejun Heo * 3355628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3356628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3357628c78e7STejun Heo * cpu comes back online. 3358db7bccf4STejun Heo */ 3359db7bccf4STejun Heo 336060373152STejun Heo /* claim manager positions of all pools */ 336160373152STejun Heo static void gcwq_claim_management(struct global_cwq *gcwq) 336260373152STejun Heo { 336360373152STejun Heo struct worker_pool *pool; 336460373152STejun Heo 336560373152STejun Heo for_each_worker_pool(pool, gcwq) 336660373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 336760373152STejun Heo } 336860373152STejun Heo 336960373152STejun Heo /* release manager positions */ 337060373152STejun Heo static void gcwq_release_management(struct global_cwq *gcwq) 337160373152STejun Heo { 337260373152STejun Heo struct worker_pool *pool; 337360373152STejun Heo 337460373152STejun Heo for_each_worker_pool(pool, gcwq) 337560373152STejun Heo mutex_unlock(&pool->manager_mutex); 337660373152STejun Heo } 337760373152STejun Heo 3378628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3379db7bccf4STejun Heo { 3380628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 33814ce62e9eSTejun Heo struct worker_pool *pool; 3382db7bccf4STejun Heo struct worker *worker; 3383db7bccf4STejun Heo struct hlist_node *pos; 3384db7bccf4STejun Heo int i; 3385db7bccf4STejun Heo 3386db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3387db7bccf4STejun Heo 338860373152STejun Heo gcwq_claim_management(gcwq); 3389db7bccf4STejun Heo spin_lock_irq(&gcwq->lock); 3390e22bee78STejun Heo 3391f2d5a0eeSTejun Heo /* 3392f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3393f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3394f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3395f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3396f2d5a0eeSTejun Heo */ 339760373152STejun Heo for_each_worker_pool(pool, gcwq) 33984ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3399403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3400db7bccf4STejun Heo 3401db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3402403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3403db7bccf4STejun Heo 3404f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3405f2d5a0eeSTejun Heo 3406e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3407628c78e7STejun Heo gcwq_release_management(gcwq); 3408e22bee78STejun Heo 3409e22bee78STejun Heo /* 3410628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3411628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3412628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3413628c78e7STejun Heo */ 3414628c78e7STejun Heo schedule(); 3415628c78e7STejun Heo 3416628c78e7STejun Heo /* 3417628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3418628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3419628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3420628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3421628c78e7STejun Heo * which is served by workers tied to the CPU. 3422628c78e7STejun Heo * 3423628c78e7STejun Heo * On return from this function, the current worker would trigger 3424628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3425628c78e7STejun Heo * didn't already. 3426e22bee78STejun Heo */ 34274ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34284ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3429db7bccf4STejun Heo } 3430db7bccf4STejun Heo 34319c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 34321da177e4SLinus Torvalds unsigned long action, 34331da177e4SLinus Torvalds void *hcpu) 34341da177e4SLinus Torvalds { 34353af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3436db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 34374ce62e9eSTejun Heo struct worker_pool *pool; 3438628c78e7STejun Heo struct work_struct unbind_work; 3439db7bccf4STejun Heo unsigned long flags; 34401da177e4SLinus Torvalds 34418bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 34428bb78442SRafael J. Wysocki 34431da177e4SLinus Torvalds switch (action) { 3444db7bccf4STejun Heo case CPU_DOWN_PREPARE: 3445628c78e7STejun Heo /* unbinding should happen on the local CPU */ 3446628c78e7STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 3447628c78e7STejun Heo schedule_work_on(cpu, &unbind_work); 3448628c78e7STejun Heo flush_work(&unbind_work); 34493ce63377STejun Heo break; 34503ce63377STejun Heo 34513af24433SOleg Nesterov case CPU_UP_PREPARE: 34524ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 34533ce63377STejun Heo struct worker *worker; 34543ce63377STejun Heo 34553ce63377STejun Heo if (pool->nr_workers) 34563ce63377STejun Heo continue; 34573ce63377STejun Heo 34583ce63377STejun Heo worker = create_worker(pool); 34593ce63377STejun Heo if (!worker) 34603ce63377STejun Heo return NOTIFY_BAD; 34613ce63377STejun Heo 34623ce63377STejun Heo spin_lock_irq(&gcwq->lock); 34633ce63377STejun Heo start_worker(worker); 34643ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 34653af24433SOleg Nesterov } 3466db7bccf4STejun Heo } 34671537663fSTejun Heo 3468db7bccf4STejun Heo /* some are called w/ irq disabled, don't disturb irq status */ 3469db7bccf4STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 34703af24433SOleg Nesterov 34713af24433SOleg Nesterov switch (action) { 3472db7bccf4STejun Heo case CPU_DOWN_FAILED: 34731da177e4SLinus Torvalds case CPU_ONLINE: 347425511a47STejun Heo spin_unlock_irq(&gcwq->lock); 347525511a47STejun Heo gcwq_claim_management(gcwq); 347625511a47STejun Heo spin_lock_irq(&gcwq->lock); 347725511a47STejun Heo 3478bc2ae0f5STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 3479bc2ae0f5STejun Heo 348025511a47STejun Heo rebind_workers(gcwq); 348125511a47STejun Heo 348225511a47STejun Heo gcwq_release_management(gcwq); 34831da177e4SLinus Torvalds break; 34841da177e4SLinus Torvalds } 34851da177e4SLinus Torvalds 3486db7bccf4STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 348700dfcaf7SOleg Nesterov 34881537663fSTejun Heo return notifier_from_errno(0); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds 349165758202STejun Heo /* 349265758202STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 349365758202STejun Heo * This will be registered high priority CPU notifier. 349465758202STejun Heo */ 349565758202STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 349665758202STejun Heo unsigned long action, 349765758202STejun Heo void *hcpu) 349865758202STejun Heo { 349965758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 350065758202STejun Heo case CPU_UP_PREPARE: 350165758202STejun Heo case CPU_DOWN_FAILED: 350265758202STejun Heo case CPU_ONLINE: 350365758202STejun Heo return workqueue_cpu_callback(nfb, action, hcpu); 350465758202STejun Heo } 350565758202STejun Heo return NOTIFY_OK; 350665758202STejun Heo } 350765758202STejun Heo 350865758202STejun Heo /* 350965758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 351065758202STejun Heo * This will be registered as low priority CPU notifier. 351165758202STejun Heo */ 351265758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 351365758202STejun Heo unsigned long action, 351465758202STejun Heo void *hcpu) 351565758202STejun Heo { 351665758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 351765758202STejun Heo case CPU_DOWN_PREPARE: 351865758202STejun Heo return workqueue_cpu_callback(nfb, action, hcpu); 351965758202STejun Heo } 352065758202STejun Heo return NOTIFY_OK; 352165758202STejun Heo } 352265758202STejun Heo 35232d3854a3SRusty Russell #ifdef CONFIG_SMP 35248ccad40dSRusty Russell 35252d3854a3SRusty Russell struct work_for_cpu { 35266b44003eSAndrew Morton struct completion completion; 35272d3854a3SRusty Russell long (*fn)(void *); 35282d3854a3SRusty Russell void *arg; 35292d3854a3SRusty Russell long ret; 35302d3854a3SRusty Russell }; 35312d3854a3SRusty Russell 35326b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35332d3854a3SRusty Russell { 35346b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35352d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35366b44003eSAndrew Morton complete(&wfc->completion); 35376b44003eSAndrew Morton return 0; 35382d3854a3SRusty Russell } 35392d3854a3SRusty Russell 35402d3854a3SRusty Russell /** 35412d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35422d3854a3SRusty Russell * @cpu: the cpu to run on 35432d3854a3SRusty Russell * @fn: the function to run 35442d3854a3SRusty Russell * @arg: the function arg 35452d3854a3SRusty Russell * 354631ad9081SRusty Russell * This will return the value @fn returns. 354731ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35486b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35492d3854a3SRusty Russell */ 35502d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35512d3854a3SRusty Russell { 35526b44003eSAndrew Morton struct task_struct *sub_thread; 35536b44003eSAndrew Morton struct work_for_cpu wfc = { 35546b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35556b44003eSAndrew Morton .fn = fn, 35566b44003eSAndrew Morton .arg = arg, 35576b44003eSAndrew Morton }; 35582d3854a3SRusty Russell 35596b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35606b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35616b44003eSAndrew Morton return PTR_ERR(sub_thread); 35626b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35636b44003eSAndrew Morton wake_up_process(sub_thread); 35646b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35652d3854a3SRusty Russell return wfc.ret; 35662d3854a3SRusty Russell } 35672d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35682d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35692d3854a3SRusty Russell 3570a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3571e7577c50SRusty Russell 3572a0a1a5fdSTejun Heo /** 3573a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3574a0a1a5fdSTejun Heo * 357558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 357658a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 357758a69cb4STejun Heo * gcwq->worklist. 3578a0a1a5fdSTejun Heo * 3579a0a1a5fdSTejun Heo * CONTEXT: 35808b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3581a0a1a5fdSTejun Heo */ 3582a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3583a0a1a5fdSTejun Heo { 3584a0a1a5fdSTejun Heo unsigned int cpu; 3585a0a1a5fdSTejun Heo 3586a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3587a0a1a5fdSTejun Heo 3588a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3589a0a1a5fdSTejun Heo workqueue_freezing = true; 3590a0a1a5fdSTejun Heo 3591f3421797STejun Heo for_each_gcwq_cpu(cpu) { 35928b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3593bdbc5dd7STejun Heo struct workqueue_struct *wq; 35948b03ae3cSTejun Heo 35958b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 35968b03ae3cSTejun Heo 3597db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3598db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3599db7bccf4STejun Heo 3600a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3601a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3602a0a1a5fdSTejun Heo 360358a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3604a0a1a5fdSTejun Heo cwq->max_active = 0; 36051da177e4SLinus Torvalds } 36068b03ae3cSTejun Heo 36078b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3608a0a1a5fdSTejun Heo } 3609a0a1a5fdSTejun Heo 3610a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3611a0a1a5fdSTejun Heo } 3612a0a1a5fdSTejun Heo 3613a0a1a5fdSTejun Heo /** 361458a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3615a0a1a5fdSTejun Heo * 3616a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3617a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3618a0a1a5fdSTejun Heo * 3619a0a1a5fdSTejun Heo * CONTEXT: 3620a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3621a0a1a5fdSTejun Heo * 3622a0a1a5fdSTejun Heo * RETURNS: 362358a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 362458a69cb4STejun Heo * is complete. 3625a0a1a5fdSTejun Heo */ 3626a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3627a0a1a5fdSTejun Heo { 3628a0a1a5fdSTejun Heo unsigned int cpu; 3629a0a1a5fdSTejun Heo bool busy = false; 3630a0a1a5fdSTejun Heo 3631a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3632a0a1a5fdSTejun Heo 3633a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3634a0a1a5fdSTejun Heo 3635f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3636bdbc5dd7STejun Heo struct workqueue_struct *wq; 3637a0a1a5fdSTejun Heo /* 3638a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3639a0a1a5fdSTejun Heo * to peek without lock. 3640a0a1a5fdSTejun Heo */ 3641a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3642a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3643a0a1a5fdSTejun Heo 364458a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3645a0a1a5fdSTejun Heo continue; 3646a0a1a5fdSTejun Heo 3647a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3648a0a1a5fdSTejun Heo if (cwq->nr_active) { 3649a0a1a5fdSTejun Heo busy = true; 3650a0a1a5fdSTejun Heo goto out_unlock; 3651a0a1a5fdSTejun Heo } 3652a0a1a5fdSTejun Heo } 3653a0a1a5fdSTejun Heo } 3654a0a1a5fdSTejun Heo out_unlock: 3655a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3656a0a1a5fdSTejun Heo return busy; 3657a0a1a5fdSTejun Heo } 3658a0a1a5fdSTejun Heo 3659a0a1a5fdSTejun Heo /** 3660a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3661a0a1a5fdSTejun Heo * 3662a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36637e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3664a0a1a5fdSTejun Heo * 3665a0a1a5fdSTejun Heo * CONTEXT: 36668b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3667a0a1a5fdSTejun Heo */ 3668a0a1a5fdSTejun Heo void thaw_workqueues(void) 3669a0a1a5fdSTejun Heo { 3670a0a1a5fdSTejun Heo unsigned int cpu; 3671a0a1a5fdSTejun Heo 3672a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3673a0a1a5fdSTejun Heo 3674a0a1a5fdSTejun Heo if (!workqueue_freezing) 3675a0a1a5fdSTejun Heo goto out_unlock; 3676a0a1a5fdSTejun Heo 3677f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36788b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 36794ce62e9eSTejun Heo struct worker_pool *pool; 3680bdbc5dd7STejun Heo struct workqueue_struct *wq; 36818b03ae3cSTejun Heo 36828b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36838b03ae3cSTejun Heo 3684db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3685db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3686db7bccf4STejun Heo 3687a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3688a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3689a0a1a5fdSTejun Heo 369058a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3691a0a1a5fdSTejun Heo continue; 3692a0a1a5fdSTejun Heo 3693a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3694a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3695a0a1a5fdSTejun Heo 3696a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3697a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3698a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3699a0a1a5fdSTejun Heo } 37008b03ae3cSTejun Heo 37014ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37024ce62e9eSTejun Heo wake_up_worker(pool); 3703e22bee78STejun Heo 37048b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3705a0a1a5fdSTejun Heo } 3706a0a1a5fdSTejun Heo 3707a0a1a5fdSTejun Heo workqueue_freezing = false; 3708a0a1a5fdSTejun Heo out_unlock: 3709a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3710a0a1a5fdSTejun Heo } 3711a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3712a0a1a5fdSTejun Heo 37136ee0578bSSuresh Siddha static int __init init_workqueues(void) 37141da177e4SLinus Torvalds { 3715c34056a3STejun Heo unsigned int cpu; 3716c8e55f36STejun Heo int i; 3717c34056a3STejun Heo 371865758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 371965758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37208b03ae3cSTejun Heo 37218b03ae3cSTejun Heo /* initialize gcwqs */ 3722f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37238b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37244ce62e9eSTejun Heo struct worker_pool *pool; 37258b03ae3cSTejun Heo 37268b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37278b03ae3cSTejun Heo gcwq->cpu = cpu; 3728f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37298b03ae3cSTejun Heo 3730c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3731c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3732c8e55f36STejun Heo 37334ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37344ce62e9eSTejun Heo pool->gcwq = gcwq; 37354ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37364ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3737e22bee78STejun Heo 37384ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37394ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37404ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3741e22bee78STejun Heo 37424ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37434ce62e9eSTejun Heo (unsigned long)pool); 37444ce62e9eSTejun Heo 374560373152STejun Heo mutex_init(&pool->manager_mutex); 37464ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37474ce62e9eSTejun Heo } 3748db7bccf4STejun Heo 374925511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37508b03ae3cSTejun Heo } 37518b03ae3cSTejun Heo 3752e22bee78STejun Heo /* create the initial worker */ 3753f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3754e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37554ce62e9eSTejun Heo struct worker_pool *pool; 3756e22bee78STejun Heo 3757477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3758477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37594ce62e9eSTejun Heo 37604ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37614ce62e9eSTejun Heo struct worker *worker; 37624ce62e9eSTejun Heo 3763bc2ae0f5STejun Heo worker = create_worker(pool); 3764e22bee78STejun Heo BUG_ON(!worker); 3765e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3766e22bee78STejun Heo start_worker(worker); 3767e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3768e22bee78STejun Heo } 37694ce62e9eSTejun Heo } 3770e22bee78STejun Heo 3771d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3772d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3773d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3774f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3775f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 377624d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 377724d51addSTejun Heo WQ_FREEZABLE, 0); 377862d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 377962d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3780e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 378162d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 378262d3c543SAlan Stern !system_nrt_freezable_wq); 37836ee0578bSSuresh Siddha return 0; 37841da177e4SLinus Torvalds } 37856ee0578bSSuresh Siddha early_initcall(init_workqueues); 3786