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 * 5408930cabaSTejun Heo * set_work_cwq(), set_work_cpu_and_clear_pending() and clear_work_data() 5418930cabaSTejun Heo * can be used to set the cwq, cpu or clear work->data. These functions 5428930cabaSTejun Heo * should only be called while the work is owned - ie. while the PENDING 5438930cabaSTejun Heo * bit is set. 5447a22ad75STejun Heo * 5457a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 5467a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 5477a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 5487a22ad75STejun Heo * queueing until execution starts. 5494594bf15SDavid Howells */ 5507a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5517a22ad75STejun Heo unsigned long flags) 5527a22ad75STejun Heo { 5537a22ad75STejun Heo BUG_ON(!work_pending(work)); 5547a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5557a22ad75STejun Heo } 5567a22ad75STejun Heo 5577a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5584690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5594690c4abSTejun Heo unsigned long extra_flags) 560365970a1SDavid Howells { 5617a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 562e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 563365970a1SDavid Howells } 564365970a1SDavid Howells 5658930cabaSTejun Heo static void set_work_cpu_and_clear_pending(struct work_struct *work, 5668930cabaSTejun Heo unsigned int cpu) 5674d707b9fSOleg Nesterov { 5688930cabaSTejun Heo set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, 0); 5694d707b9fSOleg Nesterov } 5704d707b9fSOleg Nesterov 5717a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 572365970a1SDavid Howells { 5737a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5747a22ad75STejun Heo } 5757a22ad75STejun Heo 5767a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5777a22ad75STejun Heo { 578e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5797a22ad75STejun Heo 580e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 581e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 582e120153dSTejun Heo else 583e120153dSTejun Heo return NULL; 5847a22ad75STejun Heo } 5857a22ad75STejun Heo 5867a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5877a22ad75STejun Heo { 588e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5897a22ad75STejun Heo unsigned int cpu; 5907a22ad75STejun Heo 591e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 592e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 593bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5947a22ad75STejun Heo 5957a22ad75STejun Heo cpu = data >> WORK_STRUCT_FLAG_BITS; 596bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 5977a22ad75STejun Heo return NULL; 5987a22ad75STejun Heo 599f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 6007a22ad75STejun Heo return get_gcwq(cpu); 601365970a1SDavid Howells } 602365970a1SDavid Howells 603e22bee78STejun Heo /* 6043270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6053270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6063270476aSTejun Heo * they're being called with gcwq->lock held. 607e22bee78STejun Heo */ 608e22bee78STejun Heo 60963d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 610649027d7STejun Heo { 6113270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 612649027d7STejun Heo } 613649027d7STejun Heo 614e22bee78STejun Heo /* 615e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 616e22bee78STejun Heo * running workers. 617974271c4STejun Heo * 618974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 619974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 620974271c4STejun Heo * worklist isn't empty. 621e22bee78STejun Heo */ 62263d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 623e22bee78STejun Heo { 62463d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 625e22bee78STejun Heo } 626e22bee78STejun Heo 627e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 62863d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 629e22bee78STejun Heo { 63063d95a91STejun Heo return pool->nr_idle; 631e22bee78STejun Heo } 632e22bee78STejun Heo 633e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 63463d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 635e22bee78STejun Heo { 63663d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 637e22bee78STejun Heo 6383270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 639e22bee78STejun Heo } 640e22bee78STejun Heo 641e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 64263d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 643e22bee78STejun Heo { 64463d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 645e22bee78STejun Heo } 646e22bee78STejun Heo 647e22bee78STejun Heo /* Do I need to be the manager? */ 64863d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 649e22bee78STejun Heo { 65063d95a91STejun Heo return need_to_create_worker(pool) || 65111ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 652e22bee78STejun Heo } 653e22bee78STejun Heo 654e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 65563d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 656e22bee78STejun Heo { 65760373152STejun Heo bool managing = mutex_is_locked(&pool->manager_mutex); 65863d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 65963d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 660e22bee78STejun Heo 661e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 662e22bee78STejun Heo } 663e22bee78STejun Heo 664e22bee78STejun Heo /* 665e22bee78STejun Heo * Wake up functions. 666e22bee78STejun Heo */ 667e22bee78STejun Heo 6687e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 66963d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6707e11629dSTejun Heo { 67163d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6727e11629dSTejun Heo return NULL; 6737e11629dSTejun Heo 67463d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6757e11629dSTejun Heo } 6767e11629dSTejun Heo 6777e11629dSTejun Heo /** 6787e11629dSTejun Heo * wake_up_worker - wake up an idle worker 67963d95a91STejun Heo * @pool: worker pool to wake worker from 6807e11629dSTejun Heo * 68163d95a91STejun Heo * Wake up the first idle worker of @pool. 6827e11629dSTejun Heo * 6837e11629dSTejun Heo * CONTEXT: 6847e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 6857e11629dSTejun Heo */ 68663d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 6877e11629dSTejun Heo { 68863d95a91STejun Heo struct worker *worker = first_worker(pool); 6897e11629dSTejun Heo 6907e11629dSTejun Heo if (likely(worker)) 6917e11629dSTejun Heo wake_up_process(worker->task); 6927e11629dSTejun Heo } 6937e11629dSTejun Heo 6944690c4abSTejun Heo /** 695e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 696e22bee78STejun Heo * @task: task waking up 697e22bee78STejun Heo * @cpu: CPU @task is waking up to 698e22bee78STejun Heo * 699e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 700e22bee78STejun Heo * being awoken. 701e22bee78STejun Heo * 702e22bee78STejun Heo * CONTEXT: 703e22bee78STejun Heo * spin_lock_irq(rq->lock) 704e22bee78STejun Heo */ 705e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 706e22bee78STejun Heo { 707e22bee78STejun Heo struct worker *worker = kthread_data(task); 708e22bee78STejun Heo 7092d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 71063d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 711e22bee78STejun Heo } 712e22bee78STejun Heo 713e22bee78STejun Heo /** 714e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 715e22bee78STejun Heo * @task: task going to sleep 716e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 717e22bee78STejun Heo * 718e22bee78STejun Heo * This function is called during schedule() when a busy worker is 719e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 720e22bee78STejun Heo * returning pointer to its task. 721e22bee78STejun Heo * 722e22bee78STejun Heo * CONTEXT: 723e22bee78STejun Heo * spin_lock_irq(rq->lock) 724e22bee78STejun Heo * 725e22bee78STejun Heo * RETURNS: 726e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 727e22bee78STejun Heo */ 728e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 729e22bee78STejun Heo unsigned int cpu) 730e22bee78STejun Heo { 731e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 732bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 73363d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 734e22bee78STejun Heo 7352d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 736e22bee78STejun Heo return NULL; 737e22bee78STejun Heo 738e22bee78STejun Heo /* this can only happen on the local cpu */ 739e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 740e22bee78STejun Heo 741e22bee78STejun Heo /* 742e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 743e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 744e22bee78STejun Heo * Please read comment there. 745e22bee78STejun Heo * 746628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 747628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 748628c78e7STejun Heo * disabled, which in turn means that none else could be 749628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 750628c78e7STejun Heo * lock is safe. 751e22bee78STejun Heo */ 752bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 75363d95a91STejun Heo to_wakeup = first_worker(pool); 754e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 755e22bee78STejun Heo } 756e22bee78STejun Heo 757e22bee78STejun Heo /** 758e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 759cb444766STejun Heo * @worker: self 760d302f017STejun Heo * @flags: flags to set 761d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 762d302f017STejun Heo * 763e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 764e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 765e22bee78STejun Heo * woken up. 766d302f017STejun Heo * 767cb444766STejun Heo * CONTEXT: 768cb444766STejun Heo * spin_lock_irq(gcwq->lock) 769d302f017STejun Heo */ 770d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 771d302f017STejun Heo bool wakeup) 772d302f017STejun Heo { 773bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 774e22bee78STejun Heo 775cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 776cb444766STejun Heo 777e22bee78STejun Heo /* 778e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 779e22bee78STejun Heo * wake up an idle worker as necessary if requested by 780e22bee78STejun Heo * @wakeup. 781e22bee78STejun Heo */ 782e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 783e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 78463d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 785e22bee78STejun Heo 786e22bee78STejun Heo if (wakeup) { 787e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 788bd7bdd43STejun Heo !list_empty(&pool->worklist)) 78963d95a91STejun Heo wake_up_worker(pool); 790e22bee78STejun Heo } else 791e22bee78STejun Heo atomic_dec(nr_running); 792e22bee78STejun Heo } 793e22bee78STejun Heo 794d302f017STejun Heo worker->flags |= flags; 795d302f017STejun Heo } 796d302f017STejun Heo 797d302f017STejun Heo /** 798e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 799cb444766STejun Heo * @worker: self 800d302f017STejun Heo * @flags: flags to clear 801d302f017STejun Heo * 802e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 803d302f017STejun Heo * 804cb444766STejun Heo * CONTEXT: 805cb444766STejun Heo * spin_lock_irq(gcwq->lock) 806d302f017STejun Heo */ 807d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 808d302f017STejun Heo { 80963d95a91STejun Heo struct worker_pool *pool = worker->pool; 810e22bee78STejun Heo unsigned int oflags = worker->flags; 811e22bee78STejun Heo 812cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 813cb444766STejun Heo 814d302f017STejun Heo worker->flags &= ~flags; 815e22bee78STejun Heo 81642c025f3STejun Heo /* 81742c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 81842c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 81942c025f3STejun Heo * of multiple flags, not a single flag. 82042c025f3STejun Heo */ 821e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 822e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 82363d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 824d302f017STejun Heo } 825d302f017STejun Heo 826d302f017STejun Heo /** 827c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 828c8e55f36STejun Heo * @gcwq: gcwq of interest 829c8e55f36STejun Heo * @work: work to be hashed 830c8e55f36STejun Heo * 831c8e55f36STejun Heo * Return hash head of @gcwq for @work. 832c8e55f36STejun Heo * 833c8e55f36STejun Heo * CONTEXT: 834c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 835c8e55f36STejun Heo * 836c8e55f36STejun Heo * RETURNS: 837c8e55f36STejun Heo * Pointer to the hash head. 838c8e55f36STejun Heo */ 839c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 840c8e55f36STejun Heo struct work_struct *work) 841c8e55f36STejun Heo { 842c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 843c8e55f36STejun Heo unsigned long v = (unsigned long)work; 844c8e55f36STejun Heo 845c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 846c8e55f36STejun Heo v >>= base_shift; 847c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 848c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 849c8e55f36STejun Heo 850c8e55f36STejun Heo return &gcwq->busy_hash[v]; 851c8e55f36STejun Heo } 852c8e55f36STejun Heo 853c8e55f36STejun Heo /** 8548cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8558cca0eeaSTejun Heo * @gcwq: gcwq of interest 8568cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8578cca0eeaSTejun Heo * @work: work to find worker for 8588cca0eeaSTejun Heo * 8598cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8608cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8618cca0eeaSTejun Heo * work. 8628cca0eeaSTejun Heo * 8638cca0eeaSTejun Heo * CONTEXT: 8648cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8658cca0eeaSTejun Heo * 8668cca0eeaSTejun Heo * RETURNS: 8678cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8688cca0eeaSTejun Heo * otherwise. 8698cca0eeaSTejun Heo */ 8708cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8718cca0eeaSTejun Heo struct hlist_head *bwh, 8728cca0eeaSTejun Heo struct work_struct *work) 8738cca0eeaSTejun Heo { 8748cca0eeaSTejun Heo struct worker *worker; 8758cca0eeaSTejun Heo struct hlist_node *tmp; 8768cca0eeaSTejun Heo 8778cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8788cca0eeaSTejun Heo if (worker->current_work == work) 8798cca0eeaSTejun Heo return worker; 8808cca0eeaSTejun Heo return NULL; 8818cca0eeaSTejun Heo } 8828cca0eeaSTejun Heo 8838cca0eeaSTejun Heo /** 8848cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 8858cca0eeaSTejun Heo * @gcwq: gcwq of interest 8868cca0eeaSTejun Heo * @work: work to find worker for 8878cca0eeaSTejun Heo * 8888cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 8898cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 8908cca0eeaSTejun Heo * function calculates @bwh itself. 8918cca0eeaSTejun Heo * 8928cca0eeaSTejun Heo * CONTEXT: 8938cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8948cca0eeaSTejun Heo * 8958cca0eeaSTejun Heo * RETURNS: 8968cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8978cca0eeaSTejun Heo * otherwise. 8988cca0eeaSTejun Heo */ 8998cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 9008cca0eeaSTejun Heo struct work_struct *work) 9018cca0eeaSTejun Heo { 9028cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9038cca0eeaSTejun Heo work); 9048cca0eeaSTejun Heo } 9058cca0eeaSTejun Heo 9068cca0eeaSTejun Heo /** 907bf4ede01STejun Heo * move_linked_works - move linked works to a list 908bf4ede01STejun Heo * @work: start of series of works to be scheduled 909bf4ede01STejun Heo * @head: target list to append @work to 910bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 911bf4ede01STejun Heo * 912bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 913bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 914bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 915bf4ede01STejun Heo * 916bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 917bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 918bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 919bf4ede01STejun Heo * 920bf4ede01STejun Heo * CONTEXT: 921bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 922bf4ede01STejun Heo */ 923bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 924bf4ede01STejun Heo struct work_struct **nextp) 925bf4ede01STejun Heo { 926bf4ede01STejun Heo struct work_struct *n; 927bf4ede01STejun Heo 928bf4ede01STejun Heo /* 929bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 930bf4ede01STejun Heo * use NULL for list head. 931bf4ede01STejun Heo */ 932bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 933bf4ede01STejun Heo list_move_tail(&work->entry, head); 934bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 935bf4ede01STejun Heo break; 936bf4ede01STejun Heo } 937bf4ede01STejun Heo 938bf4ede01STejun Heo /* 939bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 940bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 941bf4ede01STejun Heo * needs to be updated. 942bf4ede01STejun Heo */ 943bf4ede01STejun Heo if (nextp) 944bf4ede01STejun Heo *nextp = n; 945bf4ede01STejun Heo } 946bf4ede01STejun Heo 947bf4ede01STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 948bf4ede01STejun Heo { 949bf4ede01STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 950bf4ede01STejun Heo struct work_struct, entry); 951bf4ede01STejun Heo 952bf4ede01STejun Heo trace_workqueue_activate_work(work); 953bf4ede01STejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 954bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 955bf4ede01STejun Heo cwq->nr_active++; 956bf4ede01STejun Heo } 957bf4ede01STejun Heo 958bf4ede01STejun Heo /** 959bf4ede01STejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 960bf4ede01STejun Heo * @cwq: cwq of interest 961bf4ede01STejun Heo * @color: color of work which left the queue 962bf4ede01STejun Heo * @delayed: for a delayed work 963bf4ede01STejun Heo * 964bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 965bf4ede01STejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 966bf4ede01STejun Heo * 967bf4ede01STejun Heo * CONTEXT: 968bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 969bf4ede01STejun Heo */ 970bf4ede01STejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 971bf4ede01STejun Heo bool delayed) 972bf4ede01STejun Heo { 973bf4ede01STejun Heo /* ignore uncolored works */ 974bf4ede01STejun Heo if (color == WORK_NO_COLOR) 975bf4ede01STejun Heo return; 976bf4ede01STejun Heo 977bf4ede01STejun Heo cwq->nr_in_flight[color]--; 978bf4ede01STejun Heo 979bf4ede01STejun Heo if (!delayed) { 980bf4ede01STejun Heo cwq->nr_active--; 981bf4ede01STejun Heo if (!list_empty(&cwq->delayed_works)) { 982bf4ede01STejun Heo /* one down, submit a delayed one */ 983bf4ede01STejun Heo if (cwq->nr_active < cwq->max_active) 984bf4ede01STejun Heo cwq_activate_first_delayed(cwq); 985bf4ede01STejun Heo } 986bf4ede01STejun Heo } 987bf4ede01STejun Heo 988bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 989bf4ede01STejun Heo if (likely(cwq->flush_color != color)) 990bf4ede01STejun Heo return; 991bf4ede01STejun Heo 992bf4ede01STejun Heo /* are there still in-flight works? */ 993bf4ede01STejun Heo if (cwq->nr_in_flight[color]) 994bf4ede01STejun Heo return; 995bf4ede01STejun Heo 996bf4ede01STejun Heo /* this cwq is done, clear flush_color */ 997bf4ede01STejun Heo cwq->flush_color = -1; 998bf4ede01STejun Heo 999bf4ede01STejun Heo /* 1000bf4ede01STejun Heo * If this was the last cwq, wake up the first flusher. It 1001bf4ede01STejun Heo * will handle the rest. 1002bf4ede01STejun Heo */ 1003bf4ede01STejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 1004bf4ede01STejun Heo complete(&cwq->wq->first_flusher->done); 1005bf4ede01STejun Heo } 1006bf4ede01STejun Heo 1007bf4ede01STejun Heo /* 1008bf4ede01STejun Heo * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 1009bf4ede01STejun Heo * so this work can't be re-armed in any way. 1010bf4ede01STejun Heo */ 1011bf4ede01STejun Heo static int try_to_grab_pending(struct work_struct *work) 1012bf4ede01STejun Heo { 1013bf4ede01STejun Heo struct global_cwq *gcwq; 1014bf4ede01STejun Heo int ret = -1; 1015bf4ede01STejun Heo 1016bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1017bf4ede01STejun Heo return 0; 1018bf4ede01STejun Heo 1019bf4ede01STejun Heo /* 1020bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1021bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1022bf4ede01STejun Heo */ 1023bf4ede01STejun Heo gcwq = get_work_gcwq(work); 1024bf4ede01STejun Heo if (!gcwq) 1025bf4ede01STejun Heo return ret; 1026bf4ede01STejun Heo 1027bf4ede01STejun Heo spin_lock_irq(&gcwq->lock); 1028bf4ede01STejun Heo if (!list_empty(&work->entry)) { 1029bf4ede01STejun Heo /* 1030bf4ede01STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 1031bf4ede01STejun Heo * In that case we must see the new value after rmb(), see 1032bf4ede01STejun Heo * insert_work()->wmb(). 1033bf4ede01STejun Heo */ 1034bf4ede01STejun Heo smp_rmb(); 1035bf4ede01STejun Heo if (gcwq == get_work_gcwq(work)) { 1036bf4ede01STejun Heo debug_work_deactivate(work); 1037bf4ede01STejun Heo list_del_init(&work->entry); 1038bf4ede01STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 1039bf4ede01STejun Heo get_work_color(work), 1040bf4ede01STejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 1041bf4ede01STejun Heo ret = 1; 1042bf4ede01STejun Heo } 1043bf4ede01STejun Heo } 1044bf4ede01STejun Heo spin_unlock_irq(&gcwq->lock); 1045bf4ede01STejun Heo 1046bf4ede01STejun Heo return ret; 1047bf4ede01STejun Heo } 1048bf4ede01STejun Heo 1049bf4ede01STejun Heo /** 10507e11629dSTejun Heo * insert_work - insert a work into gcwq 10514690c4abSTejun Heo * @cwq: cwq @work belongs to 10524690c4abSTejun Heo * @work: work to insert 10534690c4abSTejun Heo * @head: insertion point 10544690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 10554690c4abSTejun Heo * 10567e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 10577e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 10584690c4abSTejun Heo * 10594690c4abSTejun Heo * CONTEXT: 10608b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 10611da177e4SLinus Torvalds */ 1062b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 10634690c4abSTejun Heo struct work_struct *work, struct list_head *head, 10644690c4abSTejun Heo unsigned int extra_flags) 1065b89deed3SOleg Nesterov { 106663d95a91STejun Heo struct worker_pool *pool = cwq->pool; 1067e1d8aa9fSFrederic Weisbecker 10684690c4abSTejun Heo /* we own @work, set data and link */ 10697a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 10704690c4abSTejun Heo 10716e84d644SOleg Nesterov /* 10726e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 10736e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 10746e84d644SOleg Nesterov */ 10756e84d644SOleg Nesterov smp_wmb(); 10764690c4abSTejun Heo 10771a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1078e22bee78STejun Heo 1079e22bee78STejun Heo /* 1080e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1081e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1082e22bee78STejun Heo * lying around lazily while there are works to be processed. 1083e22bee78STejun Heo */ 1084e22bee78STejun Heo smp_mb(); 1085e22bee78STejun Heo 108663d95a91STejun Heo if (__need_more_worker(pool)) 108763d95a91STejun Heo wake_up_worker(pool); 1088b89deed3SOleg Nesterov } 1089b89deed3SOleg Nesterov 1090c8efcc25STejun Heo /* 1091c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 1092c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 1093c8efcc25STejun Heo * cold paths. 1094c8efcc25STejun Heo */ 1095c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1096c8efcc25STejun Heo { 1097c8efcc25STejun Heo unsigned long flags; 1098c8efcc25STejun Heo unsigned int cpu; 1099c8efcc25STejun Heo 1100c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 1101c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 1102c8efcc25STejun Heo struct worker *worker; 1103c8efcc25STejun Heo struct hlist_node *pos; 1104c8efcc25STejun Heo int i; 1105c8efcc25STejun Heo 1106c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1107c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 1108c8efcc25STejun Heo if (worker->task != current) 1109c8efcc25STejun Heo continue; 1110c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1111c8efcc25STejun Heo /* 1112c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 1113c8efcc25STejun Heo * is headed to the same workqueue. 1114c8efcc25STejun Heo */ 1115c8efcc25STejun Heo return worker->current_cwq->wq == wq; 1116c8efcc25STejun Heo } 1117c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1118c8efcc25STejun Heo } 1119c8efcc25STejun Heo return false; 1120c8efcc25STejun Heo } 1121c8efcc25STejun Heo 11224690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 11231da177e4SLinus Torvalds struct work_struct *work) 11241da177e4SLinus Torvalds { 1125502ca9d8STejun Heo struct global_cwq *gcwq; 1126502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 11271e19ffc6STejun Heo struct list_head *worklist; 11288a2e8e5dSTejun Heo unsigned int work_flags; 11298930cabaSTejun Heo 11308930cabaSTejun Heo /* 11318930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11328930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11338930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11348930cabaSTejun Heo * happen with IRQ disabled. 11358930cabaSTejun Heo */ 11368930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 11371da177e4SLinus Torvalds 1138dc186ad7SThomas Gleixner debug_work_activate(work); 11391e19ffc6STejun Heo 1140c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 11419c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1142c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1143e41e704bSTejun Heo return; 1144e41e704bSTejun Heo 1145c7fc77f7STejun Heo /* determine gcwq to use */ 1146c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1147c7fc77f7STejun Heo struct global_cwq *last_gcwq; 1148c7fc77f7STejun Heo 114957469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1150f3421797STejun Heo cpu = raw_smp_processor_id(); 1151f3421797STejun Heo 115218aa9effSTejun Heo /* 115318aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 115418aa9effSTejun Heo * was previously on a different cpu, it might still 115518aa9effSTejun Heo * be running there, in which case the work needs to 115618aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 115718aa9effSTejun Heo */ 1158502ca9d8STejun Heo gcwq = get_gcwq(cpu); 115918aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 116018aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 116118aa9effSTejun Heo struct worker *worker; 116218aa9effSTejun Heo 11638930cabaSTejun Heo spin_lock(&last_gcwq->lock); 116418aa9effSTejun Heo 116518aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 116618aa9effSTejun Heo 116718aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 116818aa9effSTejun Heo gcwq = last_gcwq; 116918aa9effSTejun Heo else { 117018aa9effSTejun Heo /* meh... not running there, queue here */ 11718930cabaSTejun Heo spin_unlock(&last_gcwq->lock); 11728930cabaSTejun Heo spin_lock(&gcwq->lock); 117318aa9effSTejun Heo } 11748930cabaSTejun Heo } else { 11758930cabaSTejun Heo spin_lock(&gcwq->lock); 11768930cabaSTejun Heo } 1177f3421797STejun Heo } else { 1178f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 11798930cabaSTejun Heo spin_lock(&gcwq->lock); 1180502ca9d8STejun Heo } 1181502ca9d8STejun Heo 1182502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1183502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1184cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1185502ca9d8STejun Heo 1186f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 11878930cabaSTejun Heo spin_unlock(&gcwq->lock); 1188f5b2552bSDan Carpenter return; 1189f5b2552bSDan Carpenter } 11901e19ffc6STejun Heo 119173f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 11928a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 11931e19ffc6STejun Heo 11941e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1195cdadf009STejun Heo trace_workqueue_activate_work(work); 11961e19ffc6STejun Heo cwq->nr_active++; 11973270476aSTejun Heo worklist = &cwq->pool->worklist; 11988a2e8e5dSTejun Heo } else { 11998a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 12001e19ffc6STejun Heo worklist = &cwq->delayed_works; 12018a2e8e5dSTejun Heo } 12021e19ffc6STejun Heo 12038a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 12041e19ffc6STejun Heo 12058930cabaSTejun Heo spin_unlock(&gcwq->lock); 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12080fcb78c2SRolf Eike Beer /** 1209c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1210c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1211c1a220e7SZhang Rui * @wq: workqueue to use 1212c1a220e7SZhang Rui * @work: work to queue 1213c1a220e7SZhang Rui * 1214d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1215c1a220e7SZhang Rui * 1216c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1217c1a220e7SZhang Rui * can't go away. 1218c1a220e7SZhang Rui */ 1219d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1220d4283e93STejun Heo struct work_struct *work) 1221c1a220e7SZhang Rui { 1222d4283e93STejun Heo bool ret = false; 12238930cabaSTejun Heo unsigned long flags; 12248930cabaSTejun Heo 12258930cabaSTejun Heo local_irq_save(flags); 1226c1a220e7SZhang Rui 122722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12284690c4abSTejun Heo __queue_work(cpu, wq, work); 1229d4283e93STejun Heo ret = true; 1230c1a220e7SZhang Rui } 12318930cabaSTejun Heo 12328930cabaSTejun Heo local_irq_restore(flags); 1233c1a220e7SZhang Rui return ret; 1234c1a220e7SZhang Rui } 1235c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1236c1a220e7SZhang Rui 12370a13c00eSTejun Heo /** 12380a13c00eSTejun Heo * queue_work - queue work on a workqueue 12390a13c00eSTejun Heo * @wq: workqueue to use 12400a13c00eSTejun Heo * @work: work to queue 12410a13c00eSTejun Heo * 1242d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 12430a13c00eSTejun Heo * 12440a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 12450a13c00eSTejun Heo * it can be processed by another CPU. 12460a13c00eSTejun Heo */ 1247d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 12480a13c00eSTejun Heo { 124957469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 12500a13c00eSTejun Heo } 12510a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 12520a13c00eSTejun Heo 1253d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 12541da177e4SLinus Torvalds { 125552bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 12567a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 12571da177e4SLinus Torvalds 12588930cabaSTejun Heo local_irq_disable(); 125957469821STejun Heo __queue_work(WORK_CPU_UNBOUND, cwq->wq, &dwork->work); 12608930cabaSTejun Heo local_irq_enable(); 12611da177e4SLinus Torvalds } 1262d8e794dfSTejun Heo EXPORT_SYMBOL_GPL(delayed_work_timer_fn); 12631da177e4SLinus Torvalds 12640fcb78c2SRolf Eike Beer /** 12650fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 12660fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 12670fcb78c2SRolf Eike Beer * @wq: workqueue to use 1268af9997e4SRandy Dunlap * @dwork: work to queue 12690fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 12700fcb78c2SRolf Eike Beer * 1271715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1272715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1273715f1300STejun Heo * execution. 12740fcb78c2SRolf Eike Beer */ 1275d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 127652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 12777a6bc1cdSVenkatesh Pallipadi { 127852bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 127952bad64dSDavid Howells struct work_struct *work = &dwork->work; 1280d4283e93STejun Heo bool ret = false; 12818930cabaSTejun Heo unsigned long flags; 12828930cabaSTejun Heo 1283715f1300STejun Heo if (!delay) 1284715f1300STejun Heo return queue_work_on(cpu, wq, &dwork->work); 1285715f1300STejun Heo 12868930cabaSTejun Heo /* read the comment in __queue_work() */ 12878930cabaSTejun Heo local_irq_save(flags); 12887a6bc1cdSVenkatesh Pallipadi 128922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1290c7fc77f7STejun Heo unsigned int lcpu; 12917a22ad75STejun Heo 1292d8e794dfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 1293d8e794dfSTejun Heo timer->data != (unsigned long)dwork); 12947a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 12957a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 12967a6bc1cdSVenkatesh Pallipadi 12978a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 12988a3e77ccSAndrew Liu 12997a22ad75STejun Heo /* 13007a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 13017a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 13027a22ad75STejun Heo * reentrance detection for delayed works. 13037a22ad75STejun Heo */ 1304c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1305c7fc77f7STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 1306c7fc77f7STejun Heo 1307c7fc77f7STejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 1308c7fc77f7STejun Heo lcpu = gcwq->cpu; 1309c7fc77f7STejun Heo else 1310c7fc77f7STejun Heo lcpu = raw_smp_processor_id(); 1311c7fc77f7STejun Heo } else 1312c7fc77f7STejun Heo lcpu = WORK_CPU_UNBOUND; 1313c7fc77f7STejun Heo 13147a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 1315c7fc77f7STejun Heo 13167a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 131763bc0362SOleg Nesterov 131857469821STejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13197a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 132063bc0362SOleg Nesterov else 132163bc0362SOleg Nesterov add_timer(timer); 1322d4283e93STejun Heo ret = true; 13237a6bc1cdSVenkatesh Pallipadi } 13248930cabaSTejun Heo 13258930cabaSTejun Heo local_irq_restore(flags); 13267a6bc1cdSVenkatesh Pallipadi return ret; 13277a6bc1cdSVenkatesh Pallipadi } 1328ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 13291da177e4SLinus Torvalds 1330c8e55f36STejun Heo /** 13310a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 13320a13c00eSTejun Heo * @wq: workqueue to use 13330a13c00eSTejun Heo * @dwork: delayable work to queue 13340a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 13350a13c00eSTejun Heo * 1336715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 13370a13c00eSTejun Heo */ 1338d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 13390a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 13400a13c00eSTejun Heo { 134157469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 13420a13c00eSTejun Heo } 13430a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 13440a13c00eSTejun Heo 13450a13c00eSTejun Heo /** 1346c8e55f36STejun Heo * worker_enter_idle - enter idle state 1347c8e55f36STejun Heo * @worker: worker which is entering idle state 1348c8e55f36STejun Heo * 1349c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1350c8e55f36STejun Heo * necessary. 1351c8e55f36STejun Heo * 1352c8e55f36STejun Heo * LOCKING: 1353c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1354c8e55f36STejun Heo */ 1355c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 13561da177e4SLinus Torvalds { 1357bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1358bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1359c8e55f36STejun Heo 1360c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1361c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1362c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1363c8e55f36STejun Heo 1364cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1365cb444766STejun Heo worker->flags |= WORKER_IDLE; 1366bd7bdd43STejun Heo pool->nr_idle++; 1367e22bee78STejun Heo worker->last_active = jiffies; 1368c8e55f36STejun Heo 1369c8e55f36STejun Heo /* idle_list is LIFO */ 1370bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1371db7bccf4STejun Heo 137263d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1373628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1374cb444766STejun Heo 1375544ecf31STejun Heo /* 1376628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1377628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1378628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1379628c78e7STejun Heo * unbind is not in progress. 1380544ecf31STejun Heo */ 1381628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1382bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 138363d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1384c8e55f36STejun Heo } 1385c8e55f36STejun Heo 1386c8e55f36STejun Heo /** 1387c8e55f36STejun Heo * worker_leave_idle - leave idle state 1388c8e55f36STejun Heo * @worker: worker which is leaving idle state 1389c8e55f36STejun Heo * 1390c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1391c8e55f36STejun Heo * 1392c8e55f36STejun Heo * LOCKING: 1393c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1394c8e55f36STejun Heo */ 1395c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1396c8e55f36STejun Heo { 1397bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1398c8e55f36STejun Heo 1399c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1400d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1401bd7bdd43STejun Heo pool->nr_idle--; 1402c8e55f36STejun Heo list_del_init(&worker->entry); 1403c8e55f36STejun Heo } 1404c8e55f36STejun Heo 1405e22bee78STejun Heo /** 1406e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1407e22bee78STejun Heo * @worker: self 1408e22bee78STejun Heo * 1409e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1410e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1411e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1412e22bee78STejun Heo * guaranteed to execute on the cpu. 1413e22bee78STejun Heo * 1414e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1415e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1416e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1417e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1418e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1419e22bee78STejun Heo * [dis]associated in the meantime. 1420e22bee78STejun Heo * 1421f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1422f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1423f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1424f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1425f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1426e22bee78STejun Heo * 1427e22bee78STejun Heo * CONTEXT: 1428e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1429e22bee78STejun Heo * held. 1430e22bee78STejun Heo * 1431e22bee78STejun Heo * RETURNS: 1432e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1433e22bee78STejun Heo * bound), %false if offline. 1434e22bee78STejun Heo */ 1435e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1436972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1437e22bee78STejun Heo { 1438bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1439e22bee78STejun Heo struct task_struct *task = worker->task; 1440e22bee78STejun Heo 1441e22bee78STejun Heo while (true) { 1442e22bee78STejun Heo /* 1443e22bee78STejun Heo * The following call may fail, succeed or succeed 1444e22bee78STejun Heo * without actually migrating the task to the cpu if 1445e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1446e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1447e22bee78STejun Heo */ 1448f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1449e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1450e22bee78STejun Heo 1451e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1452e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1453e22bee78STejun Heo return false; 1454e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1455e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1456e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1457e22bee78STejun Heo return true; 1458e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1459e22bee78STejun Heo 14605035b20fSTejun Heo /* 14615035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 14625035b20fSTejun Heo * and retry migration. cond_resched() is required here; 14635035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 14645035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 14655035b20fSTejun Heo */ 1466e22bee78STejun Heo cpu_relax(); 14675035b20fSTejun Heo cond_resched(); 1468e22bee78STejun Heo } 1469e22bee78STejun Heo } 1470e22bee78STejun Heo 147125511a47STejun Heo struct idle_rebind { 147225511a47STejun Heo int cnt; /* # workers to be rebound */ 147325511a47STejun Heo struct completion done; /* all workers rebound */ 147425511a47STejun Heo }; 147525511a47STejun Heo 1476e22bee78STejun Heo /* 147725511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 147825511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 147925511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 148025511a47STejun Heo */ 148125511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 148225511a47STejun Heo { 148325511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 148425511a47STejun Heo 148525511a47STejun Heo /* CPU must be online at this point */ 148625511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 148725511a47STejun Heo if (!--worker->idle_rebind->cnt) 148825511a47STejun Heo complete(&worker->idle_rebind->done); 148925511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 149025511a47STejun Heo 149125511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 149225511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 149325511a47STejun Heo } 149425511a47STejun Heo 149525511a47STejun Heo /* 149625511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1497403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1498403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1499403c821dSTejun Heo * executed twice without intervening cpu down. 1500e22bee78STejun Heo */ 150125511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1502e22bee78STejun Heo { 1503e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1504bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1505e22bee78STejun Heo 1506e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1507e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1508e22bee78STejun Heo 1509e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1510e22bee78STejun Heo } 1511e22bee78STejun Heo 151225511a47STejun Heo /** 151325511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 151425511a47STejun Heo * @gcwq: gcwq of interest 151525511a47STejun Heo * 151625511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 151725511a47STejun Heo * is different for idle and busy ones. 151825511a47STejun Heo * 151925511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 152025511a47STejun Heo * be complete before any worker starts executing work items with 152125511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 152225511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 152325511a47STejun Heo * 152425511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 152525511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 152625511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 152725511a47STejun Heo * 152825511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 152925511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 153025511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 153125511a47STejun Heo * be properbly bumped as busy workers rebind. 153225511a47STejun Heo * 153325511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 153425511a47STejun Heo * work item scheduled. 153525511a47STejun Heo */ 153625511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 153725511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 153825511a47STejun Heo { 153925511a47STejun Heo struct idle_rebind idle_rebind; 154025511a47STejun Heo struct worker_pool *pool; 154125511a47STejun Heo struct worker *worker; 154225511a47STejun Heo struct hlist_node *pos; 154325511a47STejun Heo int i; 154425511a47STejun Heo 154525511a47STejun Heo lockdep_assert_held(&gcwq->lock); 154625511a47STejun Heo 154725511a47STejun Heo for_each_worker_pool(pool, gcwq) 154825511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 154925511a47STejun Heo 155025511a47STejun Heo /* 155125511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 155225511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 155325511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 155425511a47STejun Heo */ 155525511a47STejun Heo init_completion(&idle_rebind.done); 155625511a47STejun Heo retry: 155725511a47STejun Heo idle_rebind.cnt = 1; 155825511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 155925511a47STejun Heo 156025511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 156125511a47STejun Heo for_each_worker_pool(pool, gcwq) { 156225511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 156325511a47STejun Heo if (worker->flags & WORKER_REBIND) 156425511a47STejun Heo continue; 156525511a47STejun Heo 156625511a47STejun Heo /* morph UNBOUND to REBIND */ 156725511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 156825511a47STejun Heo worker->flags |= WORKER_REBIND; 156925511a47STejun Heo 157025511a47STejun Heo idle_rebind.cnt++; 157125511a47STejun Heo worker->idle_rebind = &idle_rebind; 157225511a47STejun Heo 157325511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 157425511a47STejun Heo wake_up_process(worker->task); 157525511a47STejun Heo } 157625511a47STejun Heo } 157725511a47STejun Heo 157825511a47STejun Heo if (--idle_rebind.cnt) { 157925511a47STejun Heo spin_unlock_irq(&gcwq->lock); 158025511a47STejun Heo wait_for_completion(&idle_rebind.done); 158125511a47STejun Heo spin_lock_irq(&gcwq->lock); 158225511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 158325511a47STejun Heo goto retry; 158425511a47STejun Heo } 158525511a47STejun Heo 158625511a47STejun Heo /* 158725511a47STejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 158825511a47STejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 158925511a47STejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 159025511a47STejun Heo * because these workers are still guaranteed to be idle. 159125511a47STejun Heo */ 159225511a47STejun Heo for_each_worker_pool(pool, gcwq) 159325511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 159425511a47STejun Heo worker->flags &= ~WORKER_REBIND; 159525511a47STejun Heo 159625511a47STejun Heo wake_up_all(&gcwq->rebind_hold); 159725511a47STejun Heo 159825511a47STejun Heo /* rebind busy workers */ 159925511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 160025511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 160125511a47STejun Heo 160225511a47STejun Heo /* morph UNBOUND to REBIND */ 160325511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 160425511a47STejun Heo worker->flags |= WORKER_REBIND; 160525511a47STejun Heo 160625511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 160725511a47STejun Heo work_data_bits(rebind_work))) 160825511a47STejun Heo continue; 160925511a47STejun Heo 161025511a47STejun Heo /* wq doesn't matter, use the default one */ 161125511a47STejun Heo debug_work_activate(rebind_work); 161225511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 161325511a47STejun Heo worker->scheduled.next, 161425511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 161525511a47STejun Heo } 161625511a47STejun Heo } 161725511a47STejun Heo 1618c34056a3STejun Heo static struct worker *alloc_worker(void) 1619c34056a3STejun Heo { 1620c34056a3STejun Heo struct worker *worker; 1621c34056a3STejun Heo 1622c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1623c8e55f36STejun Heo if (worker) { 1624c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1625affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 162625511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1627e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1628e22bee78STejun Heo worker->flags = WORKER_PREP; 1629c8e55f36STejun Heo } 1630c34056a3STejun Heo return worker; 1631c34056a3STejun Heo } 1632c34056a3STejun Heo 1633c34056a3STejun Heo /** 1634c34056a3STejun Heo * create_worker - create a new workqueue worker 163563d95a91STejun Heo * @pool: pool the new worker will belong to 1636c34056a3STejun Heo * 163763d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1638c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1639c34056a3STejun Heo * destroy_worker(). 1640c34056a3STejun Heo * 1641c34056a3STejun Heo * CONTEXT: 1642c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1643c34056a3STejun Heo * 1644c34056a3STejun Heo * RETURNS: 1645c34056a3STejun Heo * Pointer to the newly created worker. 1646c34056a3STejun Heo */ 1647bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1648c34056a3STejun Heo { 164963d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 16503270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1651c34056a3STejun Heo struct worker *worker = NULL; 1652f3421797STejun Heo int id = -1; 1653c34056a3STejun Heo 16548b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1655bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 16568b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1657bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1658c34056a3STejun Heo goto fail; 16598b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1660c34056a3STejun Heo } 16618b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1662c34056a3STejun Heo 1663c34056a3STejun Heo worker = alloc_worker(); 1664c34056a3STejun Heo if (!worker) 1665c34056a3STejun Heo goto fail; 1666c34056a3STejun Heo 1667bd7bdd43STejun Heo worker->pool = pool; 1668c34056a3STejun Heo worker->id = id; 1669c34056a3STejun Heo 1670bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 167194dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 16723270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 16733270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1674f3421797STejun Heo else 1675f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 16763270476aSTejun Heo "kworker/u:%d%s", id, pri); 1677c34056a3STejun Heo if (IS_ERR(worker->task)) 1678c34056a3STejun Heo goto fail; 1679c34056a3STejun Heo 16803270476aSTejun Heo if (worker_pool_pri(pool)) 16813270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 16823270476aSTejun Heo 1683db7bccf4STejun Heo /* 1684bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1685bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1686bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1687bc2ae0f5STejun Heo * above the flag definition for details. 1688bc2ae0f5STejun Heo * 1689bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1690bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1691db7bccf4STejun Heo */ 1692bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 16938b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1694bc2ae0f5STejun Heo } else { 1695db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1696f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1697f3421797STejun Heo } 1698c34056a3STejun Heo 1699c34056a3STejun Heo return worker; 1700c34056a3STejun Heo fail: 1701c34056a3STejun Heo if (id >= 0) { 17028b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1703bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 17048b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1705c34056a3STejun Heo } 1706c34056a3STejun Heo kfree(worker); 1707c34056a3STejun Heo return NULL; 1708c34056a3STejun Heo } 1709c34056a3STejun Heo 1710c34056a3STejun Heo /** 1711c34056a3STejun Heo * start_worker - start a newly created worker 1712c34056a3STejun Heo * @worker: worker to start 1713c34056a3STejun Heo * 1714c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1715c34056a3STejun Heo * 1716c34056a3STejun Heo * CONTEXT: 17178b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1718c34056a3STejun Heo */ 1719c34056a3STejun Heo static void start_worker(struct worker *worker) 1720c34056a3STejun Heo { 1721cb444766STejun Heo worker->flags |= WORKER_STARTED; 1722bd7bdd43STejun Heo worker->pool->nr_workers++; 1723c8e55f36STejun Heo worker_enter_idle(worker); 1724c34056a3STejun Heo wake_up_process(worker->task); 1725c34056a3STejun Heo } 1726c34056a3STejun Heo 1727c34056a3STejun Heo /** 1728c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1729c34056a3STejun Heo * @worker: worker to be destroyed 1730c34056a3STejun Heo * 1731c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1732c8e55f36STejun Heo * 1733c8e55f36STejun Heo * CONTEXT: 1734c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1735c34056a3STejun Heo */ 1736c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1737c34056a3STejun Heo { 1738bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1739bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1740c34056a3STejun Heo int id = worker->id; 1741c34056a3STejun Heo 1742c34056a3STejun Heo /* sanity check frenzy */ 1743c34056a3STejun Heo BUG_ON(worker->current_work); 1744affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1745c34056a3STejun Heo 1746c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1747bd7bdd43STejun Heo pool->nr_workers--; 1748c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1749bd7bdd43STejun Heo pool->nr_idle--; 1750c8e55f36STejun Heo 1751c8e55f36STejun Heo list_del_init(&worker->entry); 1752cb444766STejun Heo worker->flags |= WORKER_DIE; 1753c8e55f36STejun Heo 1754c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1755c8e55f36STejun Heo 1756c34056a3STejun Heo kthread_stop(worker->task); 1757c34056a3STejun Heo kfree(worker); 1758c34056a3STejun Heo 17598b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1760bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1761c34056a3STejun Heo } 1762c34056a3STejun Heo 176363d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1764e22bee78STejun Heo { 176563d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 176663d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1767e22bee78STejun Heo 1768e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1769e22bee78STejun Heo 177063d95a91STejun Heo if (too_many_workers(pool)) { 1771e22bee78STejun Heo struct worker *worker; 1772e22bee78STejun Heo unsigned long expires; 1773e22bee78STejun Heo 1774e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 177563d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1776e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1777e22bee78STejun Heo 1778e22bee78STejun Heo if (time_before(jiffies, expires)) 177963d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1780e22bee78STejun Heo else { 1781e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 178211ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 178363d95a91STejun Heo wake_up_worker(pool); 1784e22bee78STejun Heo } 1785e22bee78STejun Heo } 1786e22bee78STejun Heo 1787e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1788e22bee78STejun Heo } 1789e22bee78STejun Heo 1790e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1791e22bee78STejun Heo { 1792e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1793e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1794f3421797STejun Heo unsigned int cpu; 1795e22bee78STejun Heo 1796e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1797e22bee78STejun Heo return false; 1798e22bee78STejun Heo 1799e22bee78STejun Heo /* mayday mayday mayday */ 1800bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1801f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1802f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1803f3421797STejun Heo cpu = 0; 1804f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1805e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1806e22bee78STejun Heo return true; 1807e22bee78STejun Heo } 1808e22bee78STejun Heo 180963d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1810e22bee78STejun Heo { 181163d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 181263d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1813e22bee78STejun Heo struct work_struct *work; 1814e22bee78STejun Heo 1815e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1816e22bee78STejun Heo 181763d95a91STejun Heo if (need_to_create_worker(pool)) { 1818e22bee78STejun Heo /* 1819e22bee78STejun Heo * We've been trying to create a new worker but 1820e22bee78STejun Heo * haven't been successful. We might be hitting an 1821e22bee78STejun Heo * allocation deadlock. Send distress signals to 1822e22bee78STejun Heo * rescuers. 1823e22bee78STejun Heo */ 182463d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1825e22bee78STejun Heo send_mayday(work); 1826e22bee78STejun Heo } 1827e22bee78STejun Heo 1828e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1829e22bee78STejun Heo 183063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1831e22bee78STejun Heo } 1832e22bee78STejun Heo 1833e22bee78STejun Heo /** 1834e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 183563d95a91STejun Heo * @pool: pool to create a new worker for 1836e22bee78STejun Heo * 183763d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1838e22bee78STejun Heo * have at least one idle worker on return from this function. If 1839e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 184063d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1841e22bee78STejun Heo * possible allocation deadlock. 1842e22bee78STejun Heo * 1843e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1844e22bee78STejun Heo * may_start_working() true. 1845e22bee78STejun Heo * 1846e22bee78STejun Heo * LOCKING: 1847e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1848e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1849e22bee78STejun Heo * manager. 1850e22bee78STejun Heo * 1851e22bee78STejun Heo * RETURNS: 1852e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1853e22bee78STejun Heo * otherwise. 1854e22bee78STejun Heo */ 185563d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 185606bd6ebfSNamhyung Kim __releases(&gcwq->lock) 185706bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1858e22bee78STejun Heo { 185963d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 186063d95a91STejun Heo 186163d95a91STejun Heo if (!need_to_create_worker(pool)) 1862e22bee78STejun Heo return false; 1863e22bee78STejun Heo restart: 18649f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 18659f9c2364STejun Heo 1866e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 186763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1868e22bee78STejun Heo 1869e22bee78STejun Heo while (true) { 1870e22bee78STejun Heo struct worker *worker; 1871e22bee78STejun Heo 1872bc2ae0f5STejun Heo worker = create_worker(pool); 1873e22bee78STejun Heo if (worker) { 187463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1875e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1876e22bee78STejun Heo start_worker(worker); 187763d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1878e22bee78STejun Heo return true; 1879e22bee78STejun Heo } 1880e22bee78STejun Heo 188163d95a91STejun Heo if (!need_to_create_worker(pool)) 1882e22bee78STejun Heo break; 1883e22bee78STejun Heo 1884e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1885e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 18869f9c2364STejun Heo 188763d95a91STejun Heo if (!need_to_create_worker(pool)) 1888e22bee78STejun Heo break; 1889e22bee78STejun Heo } 1890e22bee78STejun Heo 189163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1892e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 189363d95a91STejun Heo if (need_to_create_worker(pool)) 1894e22bee78STejun Heo goto restart; 1895e22bee78STejun Heo return true; 1896e22bee78STejun Heo } 1897e22bee78STejun Heo 1898e22bee78STejun Heo /** 1899e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 190063d95a91STejun Heo * @pool: pool to destroy workers for 1901e22bee78STejun Heo * 190263d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1903e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1904e22bee78STejun Heo * 1905e22bee78STejun Heo * LOCKING: 1906e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1907e22bee78STejun Heo * multiple times. Called only from manager. 1908e22bee78STejun Heo * 1909e22bee78STejun Heo * RETURNS: 1910e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1911e22bee78STejun Heo * otherwise. 1912e22bee78STejun Heo */ 191363d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1914e22bee78STejun Heo { 1915e22bee78STejun Heo bool ret = false; 1916e22bee78STejun Heo 191763d95a91STejun Heo while (too_many_workers(pool)) { 1918e22bee78STejun Heo struct worker *worker; 1919e22bee78STejun Heo unsigned long expires; 1920e22bee78STejun Heo 192163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1922e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1923e22bee78STejun Heo 1924e22bee78STejun Heo if (time_before(jiffies, expires)) { 192563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1926e22bee78STejun Heo break; 1927e22bee78STejun Heo } 1928e22bee78STejun Heo 1929e22bee78STejun Heo destroy_worker(worker); 1930e22bee78STejun Heo ret = true; 1931e22bee78STejun Heo } 1932e22bee78STejun Heo 1933e22bee78STejun Heo return ret; 1934e22bee78STejun Heo } 1935e22bee78STejun Heo 1936e22bee78STejun Heo /** 1937e22bee78STejun Heo * manage_workers - manage worker pool 1938e22bee78STejun Heo * @worker: self 1939e22bee78STejun Heo * 1940e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1941e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1942e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1943e22bee78STejun Heo * 1944e22bee78STejun Heo * The caller can safely start processing works on false return. On 1945e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1946e22bee78STejun Heo * and may_start_working() is true. 1947e22bee78STejun Heo * 1948e22bee78STejun Heo * CONTEXT: 1949e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1950e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1951e22bee78STejun Heo * 1952e22bee78STejun Heo * RETURNS: 1953e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1954e22bee78STejun Heo * some action was taken. 1955e22bee78STejun Heo */ 1956e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1957e22bee78STejun Heo { 195863d95a91STejun Heo struct worker_pool *pool = worker->pool; 1959e22bee78STejun Heo bool ret = false; 1960e22bee78STejun Heo 196160373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1962e22bee78STejun Heo return ret; 1963e22bee78STejun Heo 196411ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1965e22bee78STejun Heo 1966e22bee78STejun Heo /* 1967e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1968e22bee78STejun Heo * on return. 1969e22bee78STejun Heo */ 197063d95a91STejun Heo ret |= maybe_destroy_workers(pool); 197163d95a91STejun Heo ret |= maybe_create_worker(pool); 1972e22bee78STejun Heo 197360373152STejun Heo mutex_unlock(&pool->manager_mutex); 1974e22bee78STejun Heo return ret; 1975e22bee78STejun Heo } 1976e22bee78STejun Heo 1977a62428c0STejun Heo /** 1978a62428c0STejun Heo * process_one_work - process single work 1979c34056a3STejun Heo * @worker: self 1980a62428c0STejun Heo * @work: work to process 1981a62428c0STejun Heo * 1982a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1983a62428c0STejun Heo * process a single work including synchronization against and 1984a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1985a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1986a62428c0STejun Heo * call this function to process a work. 1987a62428c0STejun Heo * 1988a62428c0STejun Heo * CONTEXT: 19898b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1990a62428c0STejun Heo */ 1991c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 199206bd6ebfSNamhyung Kim __releases(&gcwq->lock) 199306bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 19941da177e4SLinus Torvalds { 19957e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1996bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1997bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1998c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 1999fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 20006bb49e59SDavid Howells work_func_t f = work->func; 200173f53c4aSTejun Heo int work_color; 20027e11629dSTejun Heo struct worker *collision; 20034e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20044e6045f1SJohannes Berg /* 2005a62428c0STejun Heo * It is permissible to free the struct work_struct from 2006a62428c0STejun Heo * inside the function that is called from it, this we need to 2007a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2008a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2009a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20104e6045f1SJohannes Berg */ 20114d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20124d82a1deSPeter Zijlstra 20134d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20144e6045f1SJohannes Berg #endif 20156fec10a1STejun Heo /* 20166fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20176fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 20186fec10a1STejun Heo * unbound or a disassociated gcwq. 20196fec10a1STejun Heo */ 202025511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 20216fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 202225511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 202325511a47STejun Heo 20247e11629dSTejun Heo /* 20257e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20267e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20277e11629dSTejun Heo * already processing the work. If so, defer the work to the 20287e11629dSTejun Heo * currently executing one. 20297e11629dSTejun Heo */ 20307e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 20317e11629dSTejun Heo if (unlikely(collision)) { 20327e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20337e11629dSTejun Heo return; 20347e11629dSTejun Heo } 20351da177e4SLinus Torvalds 20368930cabaSTejun Heo /* claim and dequeue */ 20371da177e4SLinus Torvalds debug_work_deactivate(work); 2038c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 2039c34056a3STejun Heo worker->current_work = work; 20408cca0eeaSTejun Heo worker->current_cwq = cwq; 204173f53c4aSTejun Heo work_color = get_work_color(work); 20427a22ad75STejun Heo 2043a62428c0STejun Heo list_del_init(&work->entry); 2044a62428c0STejun Heo 2045649027d7STejun Heo /* 2046fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2047fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2048fb0e7bebSTejun Heo */ 2049fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2050fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2051fb0e7bebSTejun Heo 2052974271c4STejun Heo /* 2053974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 2054974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2055974271c4STejun Heo */ 205663d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 205763d95a91STejun Heo wake_up_worker(pool); 2058974271c4STejun Heo 20598930cabaSTejun Heo /* 20608930cabaSTejun Heo * Record the last CPU and clear PENDING. The following wmb is 20618930cabaSTejun Heo * paired with the implied mb in test_and_set_bit(PENDING) and 20628930cabaSTejun Heo * ensures all updates to @work made here are visible to and 20638930cabaSTejun Heo * precede any updates by the next PENDING owner. Also, clear 20648930cabaSTejun Heo * PENDING inside @gcwq->lock so that PENDING and queued state 20658930cabaSTejun Heo * changes happen together while IRQ is disabled. 20668930cabaSTejun Heo */ 20678930cabaSTejun Heo smp_wmb(); 20688930cabaSTejun Heo set_work_cpu_and_clear_pending(work, gcwq->cpu); 20691da177e4SLinus Torvalds 20708930cabaSTejun Heo spin_unlock_irq(&gcwq->lock); 2071959d1af8STejun Heo 2072e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 20733295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2074e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 207565f27f38SDavid Howells f(work); 2076e36c886aSArjan van de Ven /* 2077e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2078e36c886aSArjan van de Ven * point will only record its address. 2079e36c886aSArjan van de Ven */ 2080e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20813295f0efSIngo Molnar lock_map_release(&lockdep_map); 20823295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 20831da177e4SLinus Torvalds 2084d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2085d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2086d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2087a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2088d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2089d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2090d5abe669SPeter Zijlstra debug_show_held_locks(current); 2091d5abe669SPeter Zijlstra dump_stack(); 2092d5abe669SPeter Zijlstra } 2093d5abe669SPeter Zijlstra 20948b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2095a62428c0STejun Heo 2096fb0e7bebSTejun Heo /* clear cpu intensive status */ 2097fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2098fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2099fb0e7bebSTejun Heo 2100a62428c0STejun Heo /* we're done with it, release */ 2101c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2102c34056a3STejun Heo worker->current_work = NULL; 21038cca0eeaSTejun Heo worker->current_cwq = NULL; 21048a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 2107affee4b2STejun Heo /** 2108affee4b2STejun Heo * process_scheduled_works - process scheduled works 2109affee4b2STejun Heo * @worker: self 2110affee4b2STejun Heo * 2111affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2112affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2113affee4b2STejun Heo * fetches a work from the top and executes it. 2114affee4b2STejun Heo * 2115affee4b2STejun Heo * CONTEXT: 21168b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2117affee4b2STejun Heo * multiple times. 2118affee4b2STejun Heo */ 2119affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21201da177e4SLinus Torvalds { 2121affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2122affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2123a62428c0STejun Heo struct work_struct, entry); 2124c34056a3STejun Heo process_one_work(worker, work); 2125a62428c0STejun Heo } 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21284690c4abSTejun Heo /** 21294690c4abSTejun Heo * worker_thread - the worker thread function 2130c34056a3STejun Heo * @__worker: self 21314690c4abSTejun Heo * 2132e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2133e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2134e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2135e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2136e22bee78STejun Heo * rescuer_thread(). 21374690c4abSTejun Heo */ 2138c34056a3STejun Heo static int worker_thread(void *__worker) 21391da177e4SLinus Torvalds { 2140c34056a3STejun Heo struct worker *worker = __worker; 2141bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2142bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 21431da177e4SLinus Torvalds 2144e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2145e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2146c8e55f36STejun Heo woke_up: 21478b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2148affee4b2STejun Heo 214925511a47STejun Heo /* 215025511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 215125511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 215225511a47STejun Heo */ 215325511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2154c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 215525511a47STejun Heo 215625511a47STejun Heo if (worker->flags & WORKER_DIE) { 2157e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2158c8e55f36STejun Heo return 0; 2159c8e55f36STejun Heo } 2160c8e55f36STejun Heo 216125511a47STejun Heo idle_worker_rebind(worker); 216225511a47STejun Heo goto woke_up; 216325511a47STejun Heo } 216425511a47STejun Heo 2165c8e55f36STejun Heo worker_leave_idle(worker); 2166db7bccf4STejun Heo recheck: 2167e22bee78STejun Heo /* no more worker necessary? */ 216863d95a91STejun Heo if (!need_more_worker(pool)) 2169e22bee78STejun Heo goto sleep; 2170e22bee78STejun Heo 2171e22bee78STejun Heo /* do we need to manage? */ 217263d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2173e22bee78STejun Heo goto recheck; 2174e22bee78STejun Heo 2175c8e55f36STejun Heo /* 2176c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2177c8e55f36STejun Heo * preparing to process a work or actually processing it. 2178c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2179c8e55f36STejun Heo */ 2180c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2181c8e55f36STejun Heo 2182e22bee78STejun Heo /* 2183e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2184e22bee78STejun Heo * at least one idle worker or that someone else has already 2185e22bee78STejun Heo * assumed the manager role. 2186e22bee78STejun Heo */ 2187e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2188e22bee78STejun Heo 2189e22bee78STejun Heo do { 2190affee4b2STejun Heo struct work_struct *work = 2191bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2192affee4b2STejun Heo struct work_struct, entry); 2193affee4b2STejun Heo 2194c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2195affee4b2STejun Heo /* optimization path, not strictly necessary */ 2196affee4b2STejun Heo process_one_work(worker, work); 2197affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2198affee4b2STejun Heo process_scheduled_works(worker); 2199affee4b2STejun Heo } else { 2200c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2201affee4b2STejun Heo process_scheduled_works(worker); 2202affee4b2STejun Heo } 220363d95a91STejun Heo } while (keep_working(pool)); 2204affee4b2STejun Heo 2205e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2206d313dd85STejun Heo sleep: 220763d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2208e22bee78STejun Heo goto recheck; 2209d313dd85STejun Heo 2210c8e55f36STejun Heo /* 2211e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2212e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2213e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2214e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2215e22bee78STejun Heo * prevent losing any event. 2216c8e55f36STejun Heo */ 2217c8e55f36STejun Heo worker_enter_idle(worker); 2218c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 22198b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 22201da177e4SLinus Torvalds schedule(); 2221c8e55f36STejun Heo goto woke_up; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 2224e22bee78STejun Heo /** 2225e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2226e22bee78STejun Heo * @__wq: the associated workqueue 2227e22bee78STejun Heo * 2228e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2229e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2230e22bee78STejun Heo * 2231e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2232e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2233e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2234e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2235e22bee78STejun Heo * the problem rescuer solves. 2236e22bee78STejun Heo * 2237e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2238e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2239e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2240e22bee78STejun Heo * 2241e22bee78STejun Heo * This should happen rarely. 2242e22bee78STejun Heo */ 2243e22bee78STejun Heo static int rescuer_thread(void *__wq) 2244e22bee78STejun Heo { 2245e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2246e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2247e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2248f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2249e22bee78STejun Heo unsigned int cpu; 2250e22bee78STejun Heo 2251e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2252e22bee78STejun Heo repeat: 2253e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds if (kthread_should_stop()) 2256e22bee78STejun Heo return 0; 22571da177e4SLinus Torvalds 2258f3421797STejun Heo /* 2259f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2260f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2261f3421797STejun Heo */ 2262f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2263f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2264f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2265bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2266bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2267e22bee78STejun Heo struct work_struct *work, *n; 2268e22bee78STejun Heo 2269e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2270f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2271e22bee78STejun Heo 2272e22bee78STejun Heo /* migrate to the target cpu if possible */ 2273bd7bdd43STejun Heo rescuer->pool = pool; 2274e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2275e22bee78STejun Heo 2276e22bee78STejun Heo /* 2277e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2278e22bee78STejun Heo * process'em. 2279e22bee78STejun Heo */ 2280e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2281bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2282e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2283e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2284e22bee78STejun Heo 2285e22bee78STejun Heo process_scheduled_works(rescuer); 22867576958aSTejun Heo 22877576958aSTejun Heo /* 22887576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 22897576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22907576958aSTejun Heo * and stalling the execution. 22917576958aSTejun Heo */ 229263d95a91STejun Heo if (keep_working(pool)) 229363d95a91STejun Heo wake_up_worker(pool); 22947576958aSTejun Heo 2295e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 2298e22bee78STejun Heo schedule(); 2299e22bee78STejun Heo goto repeat; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 2302fc2e4d70SOleg Nesterov struct wq_barrier { 2303fc2e4d70SOleg Nesterov struct work_struct work; 2304fc2e4d70SOleg Nesterov struct completion done; 2305fc2e4d70SOleg Nesterov }; 2306fc2e4d70SOleg Nesterov 2307fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2308fc2e4d70SOleg Nesterov { 2309fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2310fc2e4d70SOleg Nesterov complete(&barr->done); 2311fc2e4d70SOleg Nesterov } 2312fc2e4d70SOleg Nesterov 23134690c4abSTejun Heo /** 23144690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 23154690c4abSTejun Heo * @cwq: cwq to insert barrier into 23164690c4abSTejun Heo * @barr: wq_barrier to insert 2317affee4b2STejun Heo * @target: target work to attach @barr to 2318affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23194690c4abSTejun Heo * 2320affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2321affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2322affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2323affee4b2STejun Heo * cpu. 2324affee4b2STejun Heo * 2325affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2326affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2327affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2328affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2329affee4b2STejun Heo * after a work with LINKED flag set. 2330affee4b2STejun Heo * 2331affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2332affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 23334690c4abSTejun Heo * 23344690c4abSTejun Heo * CONTEXT: 23358b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 23364690c4abSTejun Heo */ 233783c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2338affee4b2STejun Heo struct wq_barrier *barr, 2339affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2340fc2e4d70SOleg Nesterov { 2341affee4b2STejun Heo struct list_head *head; 2342affee4b2STejun Heo unsigned int linked = 0; 2343affee4b2STejun Heo 2344dc186ad7SThomas Gleixner /* 23458b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2346dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2347dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2348dc186ad7SThomas Gleixner * might deadlock. 2349dc186ad7SThomas Gleixner */ 2350ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 235122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2352fc2e4d70SOleg Nesterov init_completion(&barr->done); 235383c22520SOleg Nesterov 2354affee4b2STejun Heo /* 2355affee4b2STejun Heo * If @target is currently being executed, schedule the 2356affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2357affee4b2STejun Heo */ 2358affee4b2STejun Heo if (worker) 2359affee4b2STejun Heo head = worker->scheduled.next; 2360affee4b2STejun Heo else { 2361affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2362affee4b2STejun Heo 2363affee4b2STejun Heo head = target->entry.next; 2364affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2365affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2366affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2367affee4b2STejun Heo } 2368affee4b2STejun Heo 2369dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2370affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2371affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2372fc2e4d70SOleg Nesterov } 2373fc2e4d70SOleg Nesterov 237473f53c4aSTejun Heo /** 237573f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 237673f53c4aSTejun Heo * @wq: workqueue being flushed 237773f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 237873f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 237973f53c4aSTejun Heo * 238073f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 238173f53c4aSTejun Heo * 238273f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 238373f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 238473f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 238573f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 238673f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 238773f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 238873f53c4aSTejun Heo * 238973f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 239073f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 239173f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 239273f53c4aSTejun Heo * is returned. 239373f53c4aSTejun Heo * 239473f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 239573f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 239673f53c4aSTejun Heo * advanced to @work_color. 239773f53c4aSTejun Heo * 239873f53c4aSTejun Heo * CONTEXT: 239973f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 240073f53c4aSTejun Heo * 240173f53c4aSTejun Heo * RETURNS: 240273f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 240373f53c4aSTejun Heo * otherwise. 240473f53c4aSTejun Heo */ 240573f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 240673f53c4aSTejun Heo int flush_color, int work_color) 24071da177e4SLinus Torvalds { 240873f53c4aSTejun Heo bool wait = false; 240973f53c4aSTejun Heo unsigned int cpu; 24101da177e4SLinus Torvalds 241173f53c4aSTejun Heo if (flush_color >= 0) { 241273f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 241373f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2414dc186ad7SThomas Gleixner } 241514441960SOleg Nesterov 2416f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 241773f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2418bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 24191da177e4SLinus Torvalds 24208b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 242173f53c4aSTejun Heo 242273f53c4aSTejun Heo if (flush_color >= 0) { 242373f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 242473f53c4aSTejun Heo 242573f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 242673f53c4aSTejun Heo cwq->flush_color = flush_color; 242773f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 242873f53c4aSTejun Heo wait = true; 24291da177e4SLinus Torvalds } 243073f53c4aSTejun Heo } 243173f53c4aSTejun Heo 243273f53c4aSTejun Heo if (work_color >= 0) { 243373f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 243473f53c4aSTejun Heo cwq->work_color = work_color; 243573f53c4aSTejun Heo } 243673f53c4aSTejun Heo 24378b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 244073f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 244173f53c4aSTejun Heo complete(&wq->first_flusher->done); 244273f53c4aSTejun Heo 244373f53c4aSTejun Heo return wait; 244483c22520SOleg Nesterov } 24451da177e4SLinus Torvalds 24460fcb78c2SRolf Eike Beer /** 24471da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 24480fcb78c2SRolf Eike Beer * @wq: workqueue to flush 24491da177e4SLinus Torvalds * 24501da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 24511da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 24521da177e4SLinus Torvalds * 2453fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2454fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 24551da177e4SLinus Torvalds */ 24567ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 24571da177e4SLinus Torvalds { 245873f53c4aSTejun Heo struct wq_flusher this_flusher = { 245973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 246073f53c4aSTejun Heo .flush_color = -1, 246173f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 246273f53c4aSTejun Heo }; 246373f53c4aSTejun Heo int next_color; 2464b1f4ec17SOleg Nesterov 24653295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24663295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 246773f53c4aSTejun Heo 246873f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 246973f53c4aSTejun Heo 247073f53c4aSTejun Heo /* 247173f53c4aSTejun Heo * Start-to-wait phase 247273f53c4aSTejun Heo */ 247373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 247473f53c4aSTejun Heo 247573f53c4aSTejun Heo if (next_color != wq->flush_color) { 247673f53c4aSTejun Heo /* 247773f53c4aSTejun Heo * Color space is not full. The current work_color 247873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 247973f53c4aSTejun Heo * by one. 248073f53c4aSTejun Heo */ 248173f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 248273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 248373f53c4aSTejun Heo wq->work_color = next_color; 248473f53c4aSTejun Heo 248573f53c4aSTejun Heo if (!wq->first_flusher) { 248673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 248773f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 248873f53c4aSTejun Heo 248973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 249073f53c4aSTejun Heo 249173f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 249273f53c4aSTejun Heo wq->work_color)) { 249373f53c4aSTejun Heo /* nothing to flush, done */ 249473f53c4aSTejun Heo wq->flush_color = next_color; 249573f53c4aSTejun Heo wq->first_flusher = NULL; 249673f53c4aSTejun Heo goto out_unlock; 249773f53c4aSTejun Heo } 249873f53c4aSTejun Heo } else { 249973f53c4aSTejun Heo /* wait in queue */ 250073f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 250173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 250273f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 250373f53c4aSTejun Heo } 250473f53c4aSTejun Heo } else { 250573f53c4aSTejun Heo /* 250673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 250773f53c4aSTejun Heo * The next flush completion will assign us 250873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 250973f53c4aSTejun Heo */ 251073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 251173f53c4aSTejun Heo } 251273f53c4aSTejun Heo 251373f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 251473f53c4aSTejun Heo 251573f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 251673f53c4aSTejun Heo 251773f53c4aSTejun Heo /* 251873f53c4aSTejun Heo * Wake-up-and-cascade phase 251973f53c4aSTejun Heo * 252073f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 252173f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 252273f53c4aSTejun Heo */ 252373f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 252473f53c4aSTejun Heo return; 252573f53c4aSTejun Heo 252673f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 252773f53c4aSTejun Heo 25284ce48b37STejun Heo /* we might have raced, check again with mutex held */ 25294ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 25304ce48b37STejun Heo goto out_unlock; 25314ce48b37STejun Heo 253273f53c4aSTejun Heo wq->first_flusher = NULL; 253373f53c4aSTejun Heo 253473f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 253573f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 253673f53c4aSTejun Heo 253773f53c4aSTejun Heo while (true) { 253873f53c4aSTejun Heo struct wq_flusher *next, *tmp; 253973f53c4aSTejun Heo 254073f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 254173f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 254273f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 254373f53c4aSTejun Heo break; 254473f53c4aSTejun Heo list_del_init(&next->list); 254573f53c4aSTejun Heo complete(&next->done); 254673f53c4aSTejun Heo } 254773f53c4aSTejun Heo 254873f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 254973f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 255073f53c4aSTejun Heo 255173f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 255273f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 255373f53c4aSTejun Heo 255473f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 255573f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 255673f53c4aSTejun Heo /* 255773f53c4aSTejun Heo * Assign the same color to all overflowed 255873f53c4aSTejun Heo * flushers, advance work_color and append to 255973f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 256073f53c4aSTejun Heo * phase for these overflowed flushers. 256173f53c4aSTejun Heo */ 256273f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 256373f53c4aSTejun Heo tmp->flush_color = wq->work_color; 256473f53c4aSTejun Heo 256573f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 256673f53c4aSTejun Heo 256773f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 256873f53c4aSTejun Heo &wq->flusher_queue); 256973f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 257073f53c4aSTejun Heo } 257173f53c4aSTejun Heo 257273f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 257373f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 257473f53c4aSTejun Heo break; 257573f53c4aSTejun Heo } 257673f53c4aSTejun Heo 257773f53c4aSTejun Heo /* 257873f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 257973f53c4aSTejun Heo * the new first flusher and arm cwqs. 258073f53c4aSTejun Heo */ 258173f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 258273f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 258373f53c4aSTejun Heo 258473f53c4aSTejun Heo list_del_init(&next->list); 258573f53c4aSTejun Heo wq->first_flusher = next; 258673f53c4aSTejun Heo 258773f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 258873f53c4aSTejun Heo break; 258973f53c4aSTejun Heo 259073f53c4aSTejun Heo /* 259173f53c4aSTejun Heo * Meh... this color is already done, clear first 259273f53c4aSTejun Heo * flusher and repeat cascading. 259373f53c4aSTejun Heo */ 259473f53c4aSTejun Heo wq->first_flusher = NULL; 259573f53c4aSTejun Heo } 259673f53c4aSTejun Heo 259773f53c4aSTejun Heo out_unlock: 259873f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 25991da177e4SLinus Torvalds } 2600ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26011da177e4SLinus Torvalds 26029c5a2ba7STejun Heo /** 26039c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26049c5a2ba7STejun Heo * @wq: workqueue to drain 26059c5a2ba7STejun Heo * 26069c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26079c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26089c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26099c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26109c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26119c5a2ba7STejun Heo * takes too long. 26129c5a2ba7STejun Heo */ 26139c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26149c5a2ba7STejun Heo { 26159c5a2ba7STejun Heo unsigned int flush_cnt = 0; 26169c5a2ba7STejun Heo unsigned int cpu; 26179c5a2ba7STejun Heo 26189c5a2ba7STejun Heo /* 26199c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26209c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 26219c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 26229c5a2ba7STejun Heo */ 26239c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26249c5a2ba7STejun Heo if (!wq->nr_drainers++) 26259c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 26269c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26279c5a2ba7STejun Heo reflush: 26289c5a2ba7STejun Heo flush_workqueue(wq); 26299c5a2ba7STejun Heo 26309c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 26319c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2632fa2563e4SThomas Tuttle bool drained; 26339c5a2ba7STejun Heo 2634bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2635fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2636bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2637fa2563e4SThomas Tuttle 2638fa2563e4SThomas Tuttle if (drained) 26399c5a2ba7STejun Heo continue; 26409c5a2ba7STejun Heo 26419c5a2ba7STejun Heo if (++flush_cnt == 10 || 26429c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 26439c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 26449c5a2ba7STejun Heo wq->name, flush_cnt); 26459c5a2ba7STejun Heo goto reflush; 26469c5a2ba7STejun Heo } 26479c5a2ba7STejun Heo 26489c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26499c5a2ba7STejun Heo if (!--wq->nr_drainers) 26509c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 26519c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26529c5a2ba7STejun Heo } 26539c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 26549c5a2ba7STejun Heo 2655baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2656baf59022STejun Heo bool wait_executing) 2657baf59022STejun Heo { 2658baf59022STejun Heo struct worker *worker = NULL; 2659baf59022STejun Heo struct global_cwq *gcwq; 2660baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2661baf59022STejun Heo 2662baf59022STejun Heo might_sleep(); 2663baf59022STejun Heo gcwq = get_work_gcwq(work); 2664baf59022STejun Heo if (!gcwq) 2665baf59022STejun Heo return false; 2666baf59022STejun Heo 2667baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2668baf59022STejun Heo if (!list_empty(&work->entry)) { 2669baf59022STejun Heo /* 2670baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2671baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2672baf59022STejun Heo * are not going to wait. 2673baf59022STejun Heo */ 2674baf59022STejun Heo smp_rmb(); 2675baf59022STejun Heo cwq = get_work_cwq(work); 2676bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2677baf59022STejun Heo goto already_gone; 2678baf59022STejun Heo } else if (wait_executing) { 2679baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2680baf59022STejun Heo if (!worker) 2681baf59022STejun Heo goto already_gone; 2682baf59022STejun Heo cwq = worker->current_cwq; 2683baf59022STejun Heo } else 2684baf59022STejun Heo goto already_gone; 2685baf59022STejun Heo 2686baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2687baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2688baf59022STejun Heo 2689e159489bSTejun Heo /* 2690e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2691e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2692e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2693e159489bSTejun Heo * access. 2694e159489bSTejun Heo */ 2695e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2696baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2697e159489bSTejun Heo else 2698e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2699baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2700e159489bSTejun Heo 2701baf59022STejun Heo return true; 2702baf59022STejun Heo already_gone: 2703baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2704baf59022STejun Heo return false; 2705baf59022STejun Heo } 2706baf59022STejun Heo 2707db700897SOleg Nesterov /** 2708401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2709401a8d04STejun Heo * @work: the work to flush 2710db700897SOleg Nesterov * 2711401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2712401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2713401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2714401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2715401a8d04STejun Heo * some of the CPUs from earlier queueing. 2716a67da70dSOleg Nesterov * 2717401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2718401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2719401a8d04STejun Heo * been requeued since flush started. 2720401a8d04STejun Heo * 2721401a8d04STejun Heo * RETURNS: 2722401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2723401a8d04STejun Heo * %false if it was already idle. 2724db700897SOleg Nesterov */ 2725401a8d04STejun Heo bool flush_work(struct work_struct *work) 2726db700897SOleg Nesterov { 2727db700897SOleg Nesterov struct wq_barrier barr; 2728db700897SOleg Nesterov 27290976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 27300976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 27310976dfc1SStephen Boyd 2732baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2733db700897SOleg Nesterov wait_for_completion(&barr.done); 2734dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2735401a8d04STejun Heo return true; 2736baf59022STejun Heo } else 2737401a8d04STejun Heo return false; 2738db700897SOleg Nesterov } 2739db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2740db700897SOleg Nesterov 2741401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2742401a8d04STejun Heo { 2743401a8d04STejun Heo struct wq_barrier barr; 2744401a8d04STejun Heo struct worker *worker; 2745401a8d04STejun Heo 2746401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2747401a8d04STejun Heo 2748401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2749401a8d04STejun Heo if (unlikely(worker)) 2750401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2751401a8d04STejun Heo 2752401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2753401a8d04STejun Heo 2754401a8d04STejun Heo if (unlikely(worker)) { 2755401a8d04STejun Heo wait_for_completion(&barr.done); 2756401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2757401a8d04STejun Heo return true; 2758401a8d04STejun Heo } else 2759401a8d04STejun Heo return false; 2760401a8d04STejun Heo } 2761401a8d04STejun Heo 2762401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2763401a8d04STejun Heo { 2764401a8d04STejun Heo bool ret = false; 2765401a8d04STejun Heo int cpu; 2766401a8d04STejun Heo 2767401a8d04STejun Heo might_sleep(); 2768401a8d04STejun Heo 2769401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2770401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2771401a8d04STejun Heo 2772401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2773401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2774401a8d04STejun Heo return ret; 2775401a8d04STejun Heo } 2776401a8d04STejun Heo 277709383498STejun Heo /** 277809383498STejun Heo * flush_work_sync - wait until a work has finished execution 277909383498STejun Heo * @work: the work to flush 278009383498STejun Heo * 278109383498STejun Heo * Wait until @work has finished execution. On return, it's 278209383498STejun Heo * guaranteed that all queueing instances of @work which happened 278309383498STejun Heo * before this function is called are finished. In other words, if 278409383498STejun Heo * @work hasn't been requeued since this function was called, @work is 278509383498STejun Heo * guaranteed to be idle on return. 278609383498STejun Heo * 278709383498STejun Heo * RETURNS: 278809383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 278909383498STejun Heo * %false if it was already idle. 279009383498STejun Heo */ 279109383498STejun Heo bool flush_work_sync(struct work_struct *work) 279209383498STejun Heo { 279309383498STejun Heo struct wq_barrier barr; 279409383498STejun Heo bool pending, waited; 279509383498STejun Heo 279609383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 279709383498STejun Heo pending = start_flush_work(work, &barr, false); 279809383498STejun Heo 279909383498STejun Heo /* wait for executions to finish */ 280009383498STejun Heo waited = wait_on_work(work); 280109383498STejun Heo 280209383498STejun Heo /* wait for the pending one */ 280309383498STejun Heo if (pending) { 280409383498STejun Heo wait_for_completion(&barr.done); 280509383498STejun Heo destroy_work_on_stack(&barr.work); 280609383498STejun Heo } 280709383498STejun Heo 280809383498STejun Heo return pending || waited; 280909383498STejun Heo } 281009383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 281109383498STejun Heo 2812401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work, 28131f1f642eSOleg Nesterov struct timer_list* timer) 28141f1f642eSOleg Nesterov { 28151f1f642eSOleg Nesterov int ret; 28161f1f642eSOleg Nesterov 28171f1f642eSOleg Nesterov do { 28181f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 28191f1f642eSOleg Nesterov if (!ret) 28201f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 28211f1f642eSOleg Nesterov wait_on_work(work); 28221f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28231f1f642eSOleg Nesterov 28247a22ad75STejun Heo clear_work_data(work); 28251f1f642eSOleg Nesterov return ret; 28261f1f642eSOleg Nesterov } 28271f1f642eSOleg Nesterov 28286e84d644SOleg Nesterov /** 2829401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2830401a8d04STejun Heo * @work: the work to cancel 28316e84d644SOleg Nesterov * 2832401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2833401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2834401a8d04STejun Heo * another workqueue. On return from this function, @work is 2835401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28361f1f642eSOleg Nesterov * 2837401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2838401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28396e84d644SOleg Nesterov * 2840401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28416e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2842401a8d04STejun Heo * 2843401a8d04STejun Heo * RETURNS: 2844401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28456e84d644SOleg Nesterov */ 2846401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28476e84d644SOleg Nesterov { 28481f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 2849b89deed3SOleg Nesterov } 285028e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2851b89deed3SOleg Nesterov 28526e84d644SOleg Nesterov /** 2853401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2854401a8d04STejun Heo * @dwork: the delayed work to flush 28556e84d644SOleg Nesterov * 2856401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2857401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2858401a8d04STejun Heo * considers the last queueing instance of @dwork. 28591f1f642eSOleg Nesterov * 2860401a8d04STejun Heo * RETURNS: 2861401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2862401a8d04STejun Heo * %false if it was already idle. 28636e84d644SOleg Nesterov */ 2864401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2865401a8d04STejun Heo { 28668930cabaSTejun Heo local_irq_disable(); 2867401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 286857469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 2869401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 28708930cabaSTejun Heo local_irq_enable(); 2871401a8d04STejun Heo return flush_work(&dwork->work); 2872401a8d04STejun Heo } 2873401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2874401a8d04STejun Heo 2875401a8d04STejun Heo /** 287609383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 287709383498STejun Heo * @dwork: the delayed work to flush 287809383498STejun Heo * 287909383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 288009383498STejun Heo * execution immediately. Other than timer handling, its behavior 288109383498STejun Heo * is identical to flush_work_sync(). 288209383498STejun Heo * 288309383498STejun Heo * RETURNS: 288409383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 288509383498STejun Heo * %false if it was already idle. 288609383498STejun Heo */ 288709383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 288809383498STejun Heo { 28898930cabaSTejun Heo local_irq_disable(); 289009383498STejun Heo if (del_timer_sync(&dwork->timer)) 289157469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 289209383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 28938930cabaSTejun Heo local_irq_enable(); 289409383498STejun Heo return flush_work_sync(&dwork->work); 289509383498STejun Heo } 289609383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 289709383498STejun Heo 289809383498STejun Heo /** 2899401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2900401a8d04STejun Heo * @dwork: the delayed work cancel 2901401a8d04STejun Heo * 2902401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2903401a8d04STejun Heo * 2904401a8d04STejun Heo * RETURNS: 2905401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2906401a8d04STejun Heo */ 2907401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29086e84d644SOleg Nesterov { 29091f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 29106e84d644SOleg Nesterov } 2911f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29121da177e4SLinus Torvalds 2913d4283e93STejun Heo /** 29140a13c00eSTejun Heo * schedule_work_on - put work task on a specific cpu 29150a13c00eSTejun Heo * @cpu: cpu to put the work task on 29160a13c00eSTejun Heo * @work: job to be done 29170a13c00eSTejun Heo * 29180a13c00eSTejun Heo * This puts a job on a specific cpu 29190a13c00eSTejun Heo */ 2920d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 29210a13c00eSTejun Heo { 29220a13c00eSTejun Heo return queue_work_on(cpu, system_wq, work); 29230a13c00eSTejun Heo } 29240a13c00eSTejun Heo EXPORT_SYMBOL(schedule_work_on); 29250a13c00eSTejun Heo 29260fcb78c2SRolf Eike Beer /** 29270fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 29280fcb78c2SRolf Eike Beer * @work: job to be done 29290fcb78c2SRolf Eike Beer * 2930d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2931d4283e93STejun Heo * %true otherwise. 29325b0f437dSBart Van Assche * 29335b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 29345b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 29355b0f437dSBart Van Assche * workqueue otherwise. 29360fcb78c2SRolf Eike Beer */ 2937d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29381da177e4SLinus Torvalds { 2939d320c038STejun Heo return queue_work(system_wq, work); 29401da177e4SLinus Torvalds } 2941ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 29421da177e4SLinus Torvalds 29430fcb78c2SRolf Eike Beer /** 29440fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29450fcb78c2SRolf Eike Beer * @cpu: cpu to use 294652bad64dSDavid Howells * @dwork: job to be done 29470fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29480fcb78c2SRolf Eike Beer * 29490fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29500fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29510fcb78c2SRolf Eike Beer */ 2952d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2953d4283e93STejun Heo unsigned long delay) 29541da177e4SLinus Torvalds { 2955d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29561da177e4SLinus Torvalds } 2957ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29581da177e4SLinus Torvalds 2959b6136773SAndrew Morton /** 29600a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29610a13c00eSTejun Heo * @dwork: job to be done 29620a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29630a13c00eSTejun Heo * 29640a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29650a13c00eSTejun Heo * workqueue. 29660a13c00eSTejun Heo */ 2967d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29680a13c00eSTejun Heo { 29690a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29700a13c00eSTejun Heo } 29710a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29720a13c00eSTejun Heo 29730a13c00eSTejun Heo /** 297431ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2975b6136773SAndrew Morton * @func: the function to call 2976b6136773SAndrew Morton * 297731ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 297831ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2979b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 298031ddd871STejun Heo * 298131ddd871STejun Heo * RETURNS: 298231ddd871STejun Heo * 0 on success, -errno on failure. 2983b6136773SAndrew Morton */ 298465f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 298515316ba8SChristoph Lameter { 298615316ba8SChristoph Lameter int cpu; 298738f51568SNamhyung Kim struct work_struct __percpu *works; 298815316ba8SChristoph Lameter 2989b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2990b6136773SAndrew Morton if (!works) 299115316ba8SChristoph Lameter return -ENOMEM; 2992b6136773SAndrew Morton 299395402b38SGautham R Shenoy get_online_cpus(); 299493981800STejun Heo 299515316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29969bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29979bfb1839SIngo Molnar 29989bfb1839SIngo Molnar INIT_WORK(work, func); 29998de6d308SOleg Nesterov schedule_work_on(cpu, work); 300015316ba8SChristoph Lameter } 300193981800STejun Heo 300293981800STejun Heo for_each_online_cpu(cpu) 30038616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 300493981800STejun Heo 300595402b38SGautham R Shenoy put_online_cpus(); 3006b6136773SAndrew Morton free_percpu(works); 300715316ba8SChristoph Lameter return 0; 300815316ba8SChristoph Lameter } 300915316ba8SChristoph Lameter 3010eef6a7d5SAlan Stern /** 3011eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3012eef6a7d5SAlan Stern * 3013eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3014eef6a7d5SAlan Stern * completion. 3015eef6a7d5SAlan Stern * 3016eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3017eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3018eef6a7d5SAlan Stern * will lead to deadlock: 3019eef6a7d5SAlan Stern * 3020eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3021eef6a7d5SAlan Stern * a lock held by your code or its caller. 3022eef6a7d5SAlan Stern * 3023eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3024eef6a7d5SAlan Stern * 3025eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3026eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3027eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3028eef6a7d5SAlan Stern * 3029eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3030eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3031eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3032eef6a7d5SAlan Stern * cancel_work_sync() instead. 3033eef6a7d5SAlan Stern */ 30341da177e4SLinus Torvalds void flush_scheduled_work(void) 30351da177e4SLinus Torvalds { 3036d320c038STejun Heo flush_workqueue(system_wq); 30371da177e4SLinus Torvalds } 3038ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds /** 30411fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30421fa44ecaSJames Bottomley * @fn: the function to execute 30431fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30441fa44ecaSJames Bottomley * be available when the work executes) 30451fa44ecaSJames Bottomley * 30461fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30471fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30481fa44ecaSJames Bottomley * 30491fa44ecaSJames Bottomley * Returns: 0 - function was executed 30501fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30511fa44ecaSJames Bottomley */ 305265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30531fa44ecaSJames Bottomley { 30541fa44ecaSJames Bottomley if (!in_interrupt()) { 305565f27f38SDavid Howells fn(&ew->work); 30561fa44ecaSJames Bottomley return 0; 30571fa44ecaSJames Bottomley } 30581fa44ecaSJames Bottomley 305965f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30601fa44ecaSJames Bottomley schedule_work(&ew->work); 30611fa44ecaSJames Bottomley 30621fa44ecaSJames Bottomley return 1; 30631fa44ecaSJames Bottomley } 30641fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30651fa44ecaSJames Bottomley 30661da177e4SLinus Torvalds int keventd_up(void) 30671da177e4SLinus Torvalds { 3068d320c038STejun Heo return system_wq != NULL; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 3071bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30721da177e4SLinus Torvalds { 30733af24433SOleg Nesterov /* 30740f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30750f900049STejun Heo * Make sure that the alignment isn't lower than that of 30760f900049STejun Heo * unsigned long long. 30773af24433SOleg Nesterov */ 30780f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 30790f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 30800f900049STejun Heo __alignof__(unsigned long long)); 30813af24433SOleg Nesterov 3082e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3083f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3084931ac77eSTejun Heo else { 30850f900049STejun Heo void *ptr; 3086e1d8aa9fSFrederic Weisbecker 30870f900049STejun Heo /* 3088f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3089f3421797STejun Heo * pointer at the end pointing back to the originally 3090f3421797STejun Heo * allocated pointer which will be used for free. 30910f900049STejun Heo */ 3092bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3093bdbc5dd7STejun Heo if (ptr) { 3094bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3095bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3096bdbc5dd7STejun Heo } 30973af24433SOleg Nesterov } 30983af24433SOleg Nesterov 30990415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3100bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3101bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 31020f900049STejun Heo } 31030f900049STejun Heo 3104bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 310506ba38a9SOleg Nesterov { 3106e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3107bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3108f3421797STejun Heo else if (wq->cpu_wq.single) { 3109f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3110f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 311106ba38a9SOleg Nesterov } 311206ba38a9SOleg Nesterov } 311306ba38a9SOleg Nesterov 3114f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3115f3421797STejun Heo const char *name) 3116b71ab8c2STejun Heo { 3117f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3118f3421797STejun Heo 3119f3421797STejun Heo if (max_active < 1 || max_active > lim) 3120b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3121b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3122f3421797STejun Heo max_active, name, 1, lim); 3123b71ab8c2STejun Heo 3124f3421797STejun Heo return clamp_val(max_active, 1, lim); 3125b71ab8c2STejun Heo } 3126b71ab8c2STejun Heo 3127b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 312897e37d7bSTejun Heo unsigned int flags, 31291e19ffc6STejun Heo int max_active, 3130eb13ba87SJohannes Berg struct lock_class_key *key, 3131b196be89STejun Heo const char *lock_name, ...) 31323af24433SOleg Nesterov { 3133b196be89STejun Heo va_list args, args1; 31343af24433SOleg Nesterov struct workqueue_struct *wq; 3135c34056a3STejun Heo unsigned int cpu; 3136b196be89STejun Heo size_t namelen; 3137b196be89STejun Heo 3138b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3139b196be89STejun Heo va_start(args, lock_name); 3140b196be89STejun Heo va_copy(args1, args); 3141b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3142b196be89STejun Heo 3143b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3144b196be89STejun Heo if (!wq) 3145b196be89STejun Heo goto err; 3146b196be89STejun Heo 3147b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3148b196be89STejun Heo va_end(args); 3149b196be89STejun Heo va_end(args1); 31503af24433SOleg Nesterov 3151f3421797STejun Heo /* 31526370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31536370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31546370a6adSTejun Heo */ 31556370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31566370a6adSTejun Heo flags |= WQ_RESCUER; 31576370a6adSTejun Heo 3158d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3159b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31603af24433SOleg Nesterov 3161b196be89STejun Heo /* init wq */ 316297e37d7bSTejun Heo wq->flags = flags; 3163a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 316473f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 316573f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 316673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 316773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31683af24433SOleg Nesterov 3169eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3170cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31713af24433SOleg Nesterov 3172bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3173bdbc5dd7STejun Heo goto err; 3174bdbc5dd7STejun Heo 3175f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 31761537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 31778b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 31783270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 31791537663fSTejun Heo 31800f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 31813270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3182c34056a3STejun Heo cwq->wq = wq; 318373f53c4aSTejun Heo cwq->flush_color = -1; 31841e19ffc6STejun Heo cwq->max_active = max_active; 31851e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3186e22bee78STejun Heo } 31871537663fSTejun Heo 3188e22bee78STejun Heo if (flags & WQ_RESCUER) { 3189e22bee78STejun Heo struct worker *rescuer; 3190e22bee78STejun Heo 3191f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3192e22bee78STejun Heo goto err; 3193e22bee78STejun Heo 3194e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3195e22bee78STejun Heo if (!rescuer) 3196e22bee78STejun Heo goto err; 3197e22bee78STejun Heo 3198b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3199b196be89STejun Heo wq->name); 3200e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3201e22bee78STejun Heo goto err; 3202e22bee78STejun Heo 3203e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3204e22bee78STejun Heo wake_up_process(rescuer->task); 32053af24433SOleg Nesterov } 32061537663fSTejun Heo 32073af24433SOleg Nesterov /* 3208a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3209a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3210a0a1a5fdSTejun Heo * workqueue to workqueues list. 32113af24433SOleg Nesterov */ 32123af24433SOleg Nesterov spin_lock(&workqueue_lock); 3213a0a1a5fdSTejun Heo 321458a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3215f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3216a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3217a0a1a5fdSTejun Heo 32183af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3219a0a1a5fdSTejun Heo 32203af24433SOleg Nesterov spin_unlock(&workqueue_lock); 32213af24433SOleg Nesterov 32223af24433SOleg Nesterov return wq; 32234690c4abSTejun Heo err: 32244690c4abSTejun Heo if (wq) { 3225bdbc5dd7STejun Heo free_cwqs(wq); 3226f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3227e22bee78STejun Heo kfree(wq->rescuer); 32284690c4abSTejun Heo kfree(wq); 32293af24433SOleg Nesterov } 32304690c4abSTejun Heo return NULL; 32311da177e4SLinus Torvalds } 3232d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32331da177e4SLinus Torvalds 32343af24433SOleg Nesterov /** 32353af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32363af24433SOleg Nesterov * @wq: target workqueue 32373af24433SOleg Nesterov * 32383af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32393af24433SOleg Nesterov */ 32403af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32413af24433SOleg Nesterov { 3242c8e55f36STejun Heo unsigned int cpu; 32433af24433SOleg Nesterov 32449c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32459c5a2ba7STejun Heo drain_workqueue(wq); 3246c8efcc25STejun Heo 3247a0a1a5fdSTejun Heo /* 3248a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3249a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3250a0a1a5fdSTejun Heo */ 325195402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32523af24433SOleg Nesterov list_del(&wq->list); 325395402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32543af24433SOleg Nesterov 3255e22bee78STejun Heo /* sanity check */ 3256f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 325773f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 325873f53c4aSTejun Heo int i; 32593af24433SOleg Nesterov 326073f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 326173f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32621e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32631e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 326473f53c4aSTejun Heo } 32651537663fSTejun Heo 3266e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3267e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3268f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32698d9df9f0SXiaotian Feng kfree(wq->rescuer); 3270e22bee78STejun Heo } 3271e22bee78STejun Heo 3272bdbc5dd7STejun Heo free_cwqs(wq); 32733af24433SOleg Nesterov kfree(wq); 32743af24433SOleg Nesterov } 32753af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32763af24433SOleg Nesterov 3277dcd989cbSTejun Heo /** 3278dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3279dcd989cbSTejun Heo * @wq: target workqueue 3280dcd989cbSTejun Heo * @max_active: new max_active value. 3281dcd989cbSTejun Heo * 3282dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3283dcd989cbSTejun Heo * 3284dcd989cbSTejun Heo * CONTEXT: 3285dcd989cbSTejun Heo * Don't call from IRQ context. 3286dcd989cbSTejun Heo */ 3287dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3288dcd989cbSTejun Heo { 3289dcd989cbSTejun Heo unsigned int cpu; 3290dcd989cbSTejun Heo 3291f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3292dcd989cbSTejun Heo 3293dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3294dcd989cbSTejun Heo 3295dcd989cbSTejun Heo wq->saved_max_active = max_active; 3296dcd989cbSTejun Heo 3297f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3298dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3299dcd989cbSTejun Heo 3300dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3301dcd989cbSTejun Heo 330258a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3303dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3304dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3305dcd989cbSTejun Heo 3306dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3307dcd989cbSTejun Heo } 3308dcd989cbSTejun Heo 3309dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3310dcd989cbSTejun Heo } 3311dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3312dcd989cbSTejun Heo 3313dcd989cbSTejun Heo /** 3314dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3315dcd989cbSTejun Heo * @cpu: CPU in question 3316dcd989cbSTejun Heo * @wq: target workqueue 3317dcd989cbSTejun Heo * 3318dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3319dcd989cbSTejun Heo * no synchronization around this function and the test result is 3320dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3321dcd989cbSTejun Heo * 3322dcd989cbSTejun Heo * RETURNS: 3323dcd989cbSTejun Heo * %true if congested, %false otherwise. 3324dcd989cbSTejun Heo */ 3325dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3326dcd989cbSTejun Heo { 3327dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3328dcd989cbSTejun Heo 3329dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3330dcd989cbSTejun Heo } 3331dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3332dcd989cbSTejun Heo 3333dcd989cbSTejun Heo /** 3334dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3335dcd989cbSTejun Heo * @work: the work of interest 3336dcd989cbSTejun Heo * 3337dcd989cbSTejun Heo * RETURNS: 3338bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3339dcd989cbSTejun Heo */ 3340dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3341dcd989cbSTejun Heo { 3342dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3343dcd989cbSTejun Heo 3344bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3345dcd989cbSTejun Heo } 3346dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3347dcd989cbSTejun Heo 3348dcd989cbSTejun Heo /** 3349dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3350dcd989cbSTejun Heo * @work: the work to be tested 3351dcd989cbSTejun Heo * 3352dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3353dcd989cbSTejun Heo * synchronization around this function and the test result is 3354dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3355dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3356dcd989cbSTejun Heo * running state. 3357dcd989cbSTejun Heo * 3358dcd989cbSTejun Heo * RETURNS: 3359dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3360dcd989cbSTejun Heo */ 3361dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3362dcd989cbSTejun Heo { 3363dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3364dcd989cbSTejun Heo unsigned long flags; 3365dcd989cbSTejun Heo unsigned int ret = 0; 3366dcd989cbSTejun Heo 3367dcd989cbSTejun Heo if (!gcwq) 3368dcd989cbSTejun Heo return false; 3369dcd989cbSTejun Heo 3370dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3371dcd989cbSTejun Heo 3372dcd989cbSTejun Heo if (work_pending(work)) 3373dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3374dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3375dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3376dcd989cbSTejun Heo 3377dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3378dcd989cbSTejun Heo 3379dcd989cbSTejun Heo return ret; 3380dcd989cbSTejun Heo } 3381dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3382dcd989cbSTejun Heo 3383db7bccf4STejun Heo /* 3384db7bccf4STejun Heo * CPU hotplug. 3385db7bccf4STejun Heo * 3386e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3387e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3388e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3389e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3390e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3391e22bee78STejun Heo * blocked draining impractical. 3392e22bee78STejun Heo * 3393628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3394628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3395628c78e7STejun Heo * cpu comes back online. 3396db7bccf4STejun Heo */ 3397db7bccf4STejun Heo 339860373152STejun Heo /* claim manager positions of all pools */ 33998db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 340060373152STejun Heo { 340160373152STejun Heo struct worker_pool *pool; 340260373152STejun Heo 340360373152STejun Heo for_each_worker_pool(pool, gcwq) 340460373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 34058db25e78STejun Heo spin_lock_irq(&gcwq->lock); 340660373152STejun Heo } 340760373152STejun Heo 340860373152STejun Heo /* release manager positions */ 34098db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 341060373152STejun Heo { 341160373152STejun Heo struct worker_pool *pool; 341260373152STejun Heo 34138db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 341460373152STejun Heo for_each_worker_pool(pool, gcwq) 341560373152STejun Heo mutex_unlock(&pool->manager_mutex); 341660373152STejun Heo } 341760373152STejun Heo 3418628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3419db7bccf4STejun Heo { 3420628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 34214ce62e9eSTejun Heo struct worker_pool *pool; 3422db7bccf4STejun Heo struct worker *worker; 3423db7bccf4STejun Heo struct hlist_node *pos; 3424db7bccf4STejun Heo int i; 3425db7bccf4STejun Heo 3426db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3427db7bccf4STejun Heo 34288db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3429e22bee78STejun Heo 3430f2d5a0eeSTejun Heo /* 3431f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3432f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3433f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3434f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3435f2d5a0eeSTejun Heo */ 343660373152STejun Heo for_each_worker_pool(pool, gcwq) 34374ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3438403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3439db7bccf4STejun Heo 3440db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3441403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3442db7bccf4STejun Heo 3443f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3444f2d5a0eeSTejun Heo 34458db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3446e22bee78STejun Heo 3447e22bee78STejun Heo /* 3448628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3449628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3450628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3451628c78e7STejun Heo */ 3452628c78e7STejun Heo schedule(); 3453628c78e7STejun Heo 3454628c78e7STejun Heo /* 3455628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3456628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3457628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3458628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3459628c78e7STejun Heo * which is served by workers tied to the CPU. 3460628c78e7STejun Heo * 3461628c78e7STejun Heo * On return from this function, the current worker would trigger 3462628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3463628c78e7STejun Heo * didn't already. 3464e22bee78STejun Heo */ 34654ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34664ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3467db7bccf4STejun Heo } 3468db7bccf4STejun Heo 34698db25e78STejun Heo /* 34708db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34718db25e78STejun Heo * This will be registered high priority CPU notifier. 34728db25e78STejun Heo */ 34738db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34741da177e4SLinus Torvalds unsigned long action, 34751da177e4SLinus Torvalds void *hcpu) 34761da177e4SLinus Torvalds { 34773af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3478db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 34794ce62e9eSTejun Heo struct worker_pool *pool; 34801da177e4SLinus Torvalds 34818db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34823af24433SOleg Nesterov case CPU_UP_PREPARE: 34834ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 34843ce63377STejun Heo struct worker *worker; 34853ce63377STejun Heo 34863ce63377STejun Heo if (pool->nr_workers) 34873ce63377STejun Heo continue; 34883ce63377STejun Heo 34893ce63377STejun Heo worker = create_worker(pool); 34903ce63377STejun Heo if (!worker) 34913ce63377STejun Heo return NOTIFY_BAD; 34923ce63377STejun Heo 34933ce63377STejun Heo spin_lock_irq(&gcwq->lock); 34943ce63377STejun Heo start_worker(worker); 34953ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 34963af24433SOleg Nesterov } 34971da177e4SLinus Torvalds break; 34981da177e4SLinus Torvalds 349965758202STejun Heo case CPU_DOWN_FAILED: 350065758202STejun Heo case CPU_ONLINE: 35018db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 35028db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 35038db25e78STejun Heo rebind_workers(gcwq); 35048db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 35058db25e78STejun Heo break; 350665758202STejun Heo } 350765758202STejun Heo return NOTIFY_OK; 350865758202STejun Heo } 350965758202STejun Heo 351065758202STejun Heo /* 351165758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 351265758202STejun Heo * This will be registered as low priority CPU notifier. 351365758202STejun Heo */ 351465758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 351565758202STejun Heo unsigned long action, 351665758202STejun Heo void *hcpu) 351765758202STejun Heo { 35188db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35198db25e78STejun Heo struct work_struct unbind_work; 35208db25e78STejun Heo 352165758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 352265758202STejun Heo case CPU_DOWN_PREPARE: 35238db25e78STejun Heo /* unbinding should happen on the local CPU */ 35248db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 35258db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 35268db25e78STejun Heo flush_work(&unbind_work); 35278db25e78STejun Heo break; 352865758202STejun Heo } 352965758202STejun Heo return NOTIFY_OK; 353065758202STejun Heo } 353165758202STejun Heo 35322d3854a3SRusty Russell #ifdef CONFIG_SMP 35338ccad40dSRusty Russell 35342d3854a3SRusty Russell struct work_for_cpu { 35356b44003eSAndrew Morton struct completion completion; 35362d3854a3SRusty Russell long (*fn)(void *); 35372d3854a3SRusty Russell void *arg; 35382d3854a3SRusty Russell long ret; 35392d3854a3SRusty Russell }; 35402d3854a3SRusty Russell 35416b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35422d3854a3SRusty Russell { 35436b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35442d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35456b44003eSAndrew Morton complete(&wfc->completion); 35466b44003eSAndrew Morton return 0; 35472d3854a3SRusty Russell } 35482d3854a3SRusty Russell 35492d3854a3SRusty Russell /** 35502d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35512d3854a3SRusty Russell * @cpu: the cpu to run on 35522d3854a3SRusty Russell * @fn: the function to run 35532d3854a3SRusty Russell * @arg: the function arg 35542d3854a3SRusty Russell * 355531ad9081SRusty Russell * This will return the value @fn returns. 355631ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35576b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35582d3854a3SRusty Russell */ 35592d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35602d3854a3SRusty Russell { 35616b44003eSAndrew Morton struct task_struct *sub_thread; 35626b44003eSAndrew Morton struct work_for_cpu wfc = { 35636b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35646b44003eSAndrew Morton .fn = fn, 35656b44003eSAndrew Morton .arg = arg, 35666b44003eSAndrew Morton }; 35672d3854a3SRusty Russell 35686b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35696b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35706b44003eSAndrew Morton return PTR_ERR(sub_thread); 35716b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35726b44003eSAndrew Morton wake_up_process(sub_thread); 35736b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35742d3854a3SRusty Russell return wfc.ret; 35752d3854a3SRusty Russell } 35762d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35772d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35782d3854a3SRusty Russell 3579a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3580e7577c50SRusty Russell 3581a0a1a5fdSTejun Heo /** 3582a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3583a0a1a5fdSTejun Heo * 358458a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 358558a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 358658a69cb4STejun Heo * gcwq->worklist. 3587a0a1a5fdSTejun Heo * 3588a0a1a5fdSTejun Heo * CONTEXT: 35898b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3590a0a1a5fdSTejun Heo */ 3591a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3592a0a1a5fdSTejun Heo { 3593a0a1a5fdSTejun Heo unsigned int cpu; 3594a0a1a5fdSTejun Heo 3595a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3596a0a1a5fdSTejun Heo 3597a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3598a0a1a5fdSTejun Heo workqueue_freezing = true; 3599a0a1a5fdSTejun Heo 3600f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36018b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3602bdbc5dd7STejun Heo struct workqueue_struct *wq; 36038b03ae3cSTejun Heo 36048b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36058b03ae3cSTejun Heo 3606db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3607db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3608db7bccf4STejun Heo 3609a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3610a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3611a0a1a5fdSTejun Heo 361258a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3613a0a1a5fdSTejun Heo cwq->max_active = 0; 36141da177e4SLinus Torvalds } 36158b03ae3cSTejun Heo 36168b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3617a0a1a5fdSTejun Heo } 3618a0a1a5fdSTejun Heo 3619a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3620a0a1a5fdSTejun Heo } 3621a0a1a5fdSTejun Heo 3622a0a1a5fdSTejun Heo /** 362358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3624a0a1a5fdSTejun Heo * 3625a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3626a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3627a0a1a5fdSTejun Heo * 3628a0a1a5fdSTejun Heo * CONTEXT: 3629a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3630a0a1a5fdSTejun Heo * 3631a0a1a5fdSTejun Heo * RETURNS: 363258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 363358a69cb4STejun Heo * is complete. 3634a0a1a5fdSTejun Heo */ 3635a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3636a0a1a5fdSTejun Heo { 3637a0a1a5fdSTejun Heo unsigned int cpu; 3638a0a1a5fdSTejun Heo bool busy = false; 3639a0a1a5fdSTejun Heo 3640a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3641a0a1a5fdSTejun Heo 3642a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3643a0a1a5fdSTejun Heo 3644f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3645bdbc5dd7STejun Heo struct workqueue_struct *wq; 3646a0a1a5fdSTejun Heo /* 3647a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3648a0a1a5fdSTejun Heo * to peek without lock. 3649a0a1a5fdSTejun Heo */ 3650a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3651a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3652a0a1a5fdSTejun Heo 365358a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3654a0a1a5fdSTejun Heo continue; 3655a0a1a5fdSTejun Heo 3656a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3657a0a1a5fdSTejun Heo if (cwq->nr_active) { 3658a0a1a5fdSTejun Heo busy = true; 3659a0a1a5fdSTejun Heo goto out_unlock; 3660a0a1a5fdSTejun Heo } 3661a0a1a5fdSTejun Heo } 3662a0a1a5fdSTejun Heo } 3663a0a1a5fdSTejun Heo out_unlock: 3664a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3665a0a1a5fdSTejun Heo return busy; 3666a0a1a5fdSTejun Heo } 3667a0a1a5fdSTejun Heo 3668a0a1a5fdSTejun Heo /** 3669a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3670a0a1a5fdSTejun Heo * 3671a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36727e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3673a0a1a5fdSTejun Heo * 3674a0a1a5fdSTejun Heo * CONTEXT: 36758b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3676a0a1a5fdSTejun Heo */ 3677a0a1a5fdSTejun Heo void thaw_workqueues(void) 3678a0a1a5fdSTejun Heo { 3679a0a1a5fdSTejun Heo unsigned int cpu; 3680a0a1a5fdSTejun Heo 3681a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3682a0a1a5fdSTejun Heo 3683a0a1a5fdSTejun Heo if (!workqueue_freezing) 3684a0a1a5fdSTejun Heo goto out_unlock; 3685a0a1a5fdSTejun Heo 3686f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36878b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 36884ce62e9eSTejun Heo struct worker_pool *pool; 3689bdbc5dd7STejun Heo struct workqueue_struct *wq; 36908b03ae3cSTejun Heo 36918b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36928b03ae3cSTejun Heo 3693db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3694db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3695db7bccf4STejun Heo 3696a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3697a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3698a0a1a5fdSTejun Heo 369958a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3700a0a1a5fdSTejun Heo continue; 3701a0a1a5fdSTejun Heo 3702a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3703a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3704a0a1a5fdSTejun Heo 3705a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3706a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3707a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3708a0a1a5fdSTejun Heo } 37098b03ae3cSTejun Heo 37104ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37114ce62e9eSTejun Heo wake_up_worker(pool); 3712e22bee78STejun Heo 37138b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3714a0a1a5fdSTejun Heo } 3715a0a1a5fdSTejun Heo 3716a0a1a5fdSTejun Heo workqueue_freezing = false; 3717a0a1a5fdSTejun Heo out_unlock: 3718a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3719a0a1a5fdSTejun Heo } 3720a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3721a0a1a5fdSTejun Heo 37226ee0578bSSuresh Siddha static int __init init_workqueues(void) 37231da177e4SLinus Torvalds { 3724c34056a3STejun Heo unsigned int cpu; 3725c8e55f36STejun Heo int i; 3726c34056a3STejun Heo 372765758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 372865758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37298b03ae3cSTejun Heo 37308b03ae3cSTejun Heo /* initialize gcwqs */ 3731f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37328b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37334ce62e9eSTejun Heo struct worker_pool *pool; 37348b03ae3cSTejun Heo 37358b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37368b03ae3cSTejun Heo gcwq->cpu = cpu; 3737f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37388b03ae3cSTejun Heo 3739c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3740c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3741c8e55f36STejun Heo 37424ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37434ce62e9eSTejun Heo pool->gcwq = gcwq; 37444ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37454ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3746e22bee78STejun Heo 37474ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37484ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37494ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3750e22bee78STejun Heo 37514ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37524ce62e9eSTejun Heo (unsigned long)pool); 37534ce62e9eSTejun Heo 375460373152STejun Heo mutex_init(&pool->manager_mutex); 37554ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37564ce62e9eSTejun Heo } 3757db7bccf4STejun Heo 375825511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37598b03ae3cSTejun Heo } 37608b03ae3cSTejun Heo 3761e22bee78STejun Heo /* create the initial worker */ 3762f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3763e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37644ce62e9eSTejun Heo struct worker_pool *pool; 3765e22bee78STejun Heo 3766477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3767477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37684ce62e9eSTejun Heo 37694ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37704ce62e9eSTejun Heo struct worker *worker; 37714ce62e9eSTejun Heo 3772bc2ae0f5STejun Heo worker = create_worker(pool); 3773e22bee78STejun Heo BUG_ON(!worker); 3774e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3775e22bee78STejun Heo start_worker(worker); 3776e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3777e22bee78STejun Heo } 37784ce62e9eSTejun Heo } 3779e22bee78STejun Heo 3780d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3781d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3782d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3783f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3784f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 378524d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 378624d51addSTejun Heo WQ_FREEZABLE, 0); 378762d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 378862d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3789e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 379062d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 379162d3c543SAlan Stern !system_nrt_freezable_wq); 37926ee0578bSSuresh Siddha return 0; 37931da177e4SLinus Torvalds } 37946ee0578bSSuresh Siddha early_initcall(init_workqueues); 3795