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 /** 9077e11629dSTejun Heo * insert_work - insert a work into gcwq 9084690c4abSTejun Heo * @cwq: cwq @work belongs to 9094690c4abSTejun Heo * @work: work to insert 9104690c4abSTejun Heo * @head: insertion point 9114690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 9124690c4abSTejun Heo * 9137e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 9147e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 9154690c4abSTejun Heo * 9164690c4abSTejun Heo * CONTEXT: 9178b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 9181da177e4SLinus Torvalds */ 919b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 9204690c4abSTejun Heo struct work_struct *work, struct list_head *head, 9214690c4abSTejun Heo unsigned int extra_flags) 922b89deed3SOleg Nesterov { 92363d95a91STejun Heo struct worker_pool *pool = cwq->pool; 924e1d8aa9fSFrederic Weisbecker 9254690c4abSTejun Heo /* we own @work, set data and link */ 9267a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 9274690c4abSTejun Heo 9286e84d644SOleg Nesterov /* 9296e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 9306e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 9316e84d644SOleg Nesterov */ 9326e84d644SOleg Nesterov smp_wmb(); 9334690c4abSTejun Heo 9341a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 935e22bee78STejun Heo 936e22bee78STejun Heo /* 937e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 938e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 939e22bee78STejun Heo * lying around lazily while there are works to be processed. 940e22bee78STejun Heo */ 941e22bee78STejun Heo smp_mb(); 942e22bee78STejun Heo 94363d95a91STejun Heo if (__need_more_worker(pool)) 94463d95a91STejun Heo wake_up_worker(pool); 945b89deed3SOleg Nesterov } 946b89deed3SOleg Nesterov 947c8efcc25STejun Heo /* 948c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 949c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 950c8efcc25STejun Heo * cold paths. 951c8efcc25STejun Heo */ 952c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 953c8efcc25STejun Heo { 954c8efcc25STejun Heo unsigned long flags; 955c8efcc25STejun Heo unsigned int cpu; 956c8efcc25STejun Heo 957c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 958c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 959c8efcc25STejun Heo struct worker *worker; 960c8efcc25STejun Heo struct hlist_node *pos; 961c8efcc25STejun Heo int i; 962c8efcc25STejun Heo 963c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 964c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 965c8efcc25STejun Heo if (worker->task != current) 966c8efcc25STejun Heo continue; 967c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 968c8efcc25STejun Heo /* 969c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 970c8efcc25STejun Heo * is headed to the same workqueue. 971c8efcc25STejun Heo */ 972c8efcc25STejun Heo return worker->current_cwq->wq == wq; 973c8efcc25STejun Heo } 974c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 975c8efcc25STejun Heo } 976c8efcc25STejun Heo return false; 977c8efcc25STejun Heo } 978c8efcc25STejun Heo 9794690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 9801da177e4SLinus Torvalds struct work_struct *work) 9811da177e4SLinus Torvalds { 982502ca9d8STejun Heo struct global_cwq *gcwq; 983502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 9841e19ffc6STejun Heo struct list_head *worklist; 9858a2e8e5dSTejun Heo unsigned int work_flags; 9868930cabaSTejun Heo 9878930cabaSTejun Heo /* 9888930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 9898930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 9908930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 9918930cabaSTejun Heo * happen with IRQ disabled. 9928930cabaSTejun Heo */ 9938930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 9941da177e4SLinus Torvalds 995dc186ad7SThomas Gleixner debug_work_activate(work); 9961e19ffc6STejun Heo 997c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 9989c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 999c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1000e41e704bSTejun Heo return; 1001e41e704bSTejun Heo 1002c7fc77f7STejun Heo /* determine gcwq to use */ 1003c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1004c7fc77f7STejun Heo struct global_cwq *last_gcwq; 1005c7fc77f7STejun Heo 1006f3421797STejun Heo if (unlikely(cpu == WORK_CPU_UNBOUND)) 1007f3421797STejun Heo cpu = raw_smp_processor_id(); 1008f3421797STejun Heo 100918aa9effSTejun Heo /* 101018aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 101118aa9effSTejun Heo * was previously on a different cpu, it might still 101218aa9effSTejun Heo * be running there, in which case the work needs to 101318aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 101418aa9effSTejun Heo */ 1015502ca9d8STejun Heo gcwq = get_gcwq(cpu); 101618aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 101718aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 101818aa9effSTejun Heo struct worker *worker; 101918aa9effSTejun Heo 10208930cabaSTejun Heo spin_lock(&last_gcwq->lock); 102118aa9effSTejun Heo 102218aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 102318aa9effSTejun Heo 102418aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 102518aa9effSTejun Heo gcwq = last_gcwq; 102618aa9effSTejun Heo else { 102718aa9effSTejun Heo /* meh... not running there, queue here */ 10288930cabaSTejun Heo spin_unlock(&last_gcwq->lock); 10298930cabaSTejun Heo spin_lock(&gcwq->lock); 103018aa9effSTejun Heo } 10318930cabaSTejun Heo } else { 10328930cabaSTejun Heo spin_lock(&gcwq->lock); 10338930cabaSTejun Heo } 1034f3421797STejun Heo } else { 1035f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 10368930cabaSTejun Heo spin_lock(&gcwq->lock); 1037502ca9d8STejun Heo } 1038502ca9d8STejun Heo 1039502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1040502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1041cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1042502ca9d8STejun Heo 1043f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 10448930cabaSTejun Heo spin_unlock(&gcwq->lock); 1045f5b2552bSDan Carpenter return; 1046f5b2552bSDan Carpenter } 10471e19ffc6STejun Heo 104873f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 10498a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 10501e19ffc6STejun Heo 10511e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1052cdadf009STejun Heo trace_workqueue_activate_work(work); 10531e19ffc6STejun Heo cwq->nr_active++; 10543270476aSTejun Heo worklist = &cwq->pool->worklist; 10558a2e8e5dSTejun Heo } else { 10568a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 10571e19ffc6STejun Heo worklist = &cwq->delayed_works; 10588a2e8e5dSTejun Heo } 10591e19ffc6STejun Heo 10608a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 10611e19ffc6STejun Heo 10628930cabaSTejun Heo spin_unlock(&gcwq->lock); 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10650fcb78c2SRolf Eike Beer /** 1066c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1067c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1068c1a220e7SZhang Rui * @wq: workqueue to use 1069c1a220e7SZhang Rui * @work: work to queue 1070c1a220e7SZhang Rui * 1071d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1072c1a220e7SZhang Rui * 1073c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1074c1a220e7SZhang Rui * can't go away. 1075c1a220e7SZhang Rui */ 1076d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1077d4283e93STejun Heo struct work_struct *work) 1078c1a220e7SZhang Rui { 1079d4283e93STejun Heo bool ret = false; 10808930cabaSTejun Heo unsigned long flags; 10818930cabaSTejun Heo 10828930cabaSTejun Heo local_irq_save(flags); 1083c1a220e7SZhang Rui 108422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 10854690c4abSTejun Heo __queue_work(cpu, wq, work); 1086d4283e93STejun Heo ret = true; 1087c1a220e7SZhang Rui } 10888930cabaSTejun Heo 10898930cabaSTejun Heo local_irq_restore(flags); 1090c1a220e7SZhang Rui return ret; 1091c1a220e7SZhang Rui } 1092c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1093c1a220e7SZhang Rui 10940a13c00eSTejun Heo /** 10950a13c00eSTejun Heo * queue_work - queue work on a workqueue 10960a13c00eSTejun Heo * @wq: workqueue to use 10970a13c00eSTejun Heo * @work: work to queue 10980a13c00eSTejun Heo * 1099d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 11000a13c00eSTejun Heo * 11010a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 11020a13c00eSTejun Heo * it can be processed by another CPU. 11030a13c00eSTejun Heo */ 1104d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 11050a13c00eSTejun Heo { 1106d4283e93STejun Heo bool ret; 11070a13c00eSTejun Heo 11080a13c00eSTejun Heo ret = queue_work_on(get_cpu(), wq, work); 11090a13c00eSTejun Heo put_cpu(); 11100a13c00eSTejun Heo 11110a13c00eSTejun Heo return ret; 11120a13c00eSTejun Heo } 11130a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 11140a13c00eSTejun Heo 11156d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 11161da177e4SLinus Torvalds { 111752bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 11187a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 11191da177e4SLinus Torvalds 11208930cabaSTejun Heo local_irq_disable(); 11214690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 11228930cabaSTejun Heo local_irq_enable(); 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11250fcb78c2SRolf Eike Beer /** 11260fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 11270fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 11280fcb78c2SRolf Eike Beer * @wq: workqueue to use 1129af9997e4SRandy Dunlap * @dwork: work to queue 11300fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11310fcb78c2SRolf Eike Beer * 1132d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 11330fcb78c2SRolf Eike Beer */ 1134d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 113552bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11367a6bc1cdSVenkatesh Pallipadi { 113752bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 113852bad64dSDavid Howells struct work_struct *work = &dwork->work; 1139d4283e93STejun Heo bool ret = false; 11408930cabaSTejun Heo unsigned long flags; 11418930cabaSTejun Heo 11428930cabaSTejun Heo /* read the comment in __queue_work() */ 11438930cabaSTejun Heo local_irq_save(flags); 11447a6bc1cdSVenkatesh Pallipadi 114522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1146c7fc77f7STejun Heo unsigned int lcpu; 11477a22ad75STejun Heo 11487a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 11497a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 11507a6bc1cdSVenkatesh Pallipadi 11518a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 11528a3e77ccSAndrew Liu 11537a22ad75STejun Heo /* 11547a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 11557a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 11567a22ad75STejun Heo * reentrance detection for delayed works. 11577a22ad75STejun Heo */ 1158c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1159c7fc77f7STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 1160c7fc77f7STejun Heo 1161c7fc77f7STejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 1162c7fc77f7STejun Heo lcpu = gcwq->cpu; 1163c7fc77f7STejun Heo else 1164c7fc77f7STejun Heo lcpu = raw_smp_processor_id(); 1165c7fc77f7STejun Heo } else 1166c7fc77f7STejun Heo lcpu = WORK_CPU_UNBOUND; 1167c7fc77f7STejun Heo 11687a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 1169c7fc77f7STejun Heo 11707a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 117152bad64dSDavid Howells timer->data = (unsigned long)dwork; 11727a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 117363bc0362SOleg Nesterov 117463bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 11757a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 117663bc0362SOleg Nesterov else 117763bc0362SOleg Nesterov add_timer(timer); 1178d4283e93STejun Heo ret = true; 11797a6bc1cdSVenkatesh Pallipadi } 11808930cabaSTejun Heo 11818930cabaSTejun Heo local_irq_restore(flags); 11827a6bc1cdSVenkatesh Pallipadi return ret; 11837a6bc1cdSVenkatesh Pallipadi } 1184ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 11851da177e4SLinus Torvalds 1186c8e55f36STejun Heo /** 11870a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 11880a13c00eSTejun Heo * @wq: workqueue to use 11890a13c00eSTejun Heo * @dwork: delayable work to queue 11900a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 11910a13c00eSTejun Heo * 1192d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 11930a13c00eSTejun Heo */ 1194d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 11950a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 11960a13c00eSTejun Heo { 11970a13c00eSTejun Heo if (delay == 0) 11980a13c00eSTejun Heo return queue_work(wq, &dwork->work); 11990a13c00eSTejun Heo 12000a13c00eSTejun Heo return queue_delayed_work_on(-1, wq, dwork, delay); 12010a13c00eSTejun Heo } 12020a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 12030a13c00eSTejun Heo 12040a13c00eSTejun Heo /** 1205c8e55f36STejun Heo * worker_enter_idle - enter idle state 1206c8e55f36STejun Heo * @worker: worker which is entering idle state 1207c8e55f36STejun Heo * 1208c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1209c8e55f36STejun Heo * necessary. 1210c8e55f36STejun Heo * 1211c8e55f36STejun Heo * LOCKING: 1212c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1213c8e55f36STejun Heo */ 1214c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 12151da177e4SLinus Torvalds { 1216bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1217bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1218c8e55f36STejun Heo 1219c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1220c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1221c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1222c8e55f36STejun Heo 1223cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1224cb444766STejun Heo worker->flags |= WORKER_IDLE; 1225bd7bdd43STejun Heo pool->nr_idle++; 1226e22bee78STejun Heo worker->last_active = jiffies; 1227c8e55f36STejun Heo 1228c8e55f36STejun Heo /* idle_list is LIFO */ 1229bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1230db7bccf4STejun Heo 123163d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1232628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1233cb444766STejun Heo 1234544ecf31STejun Heo /* 1235628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1236628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1237628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1238628c78e7STejun Heo * unbind is not in progress. 1239544ecf31STejun Heo */ 1240628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1241bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 124263d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1243c8e55f36STejun Heo } 1244c8e55f36STejun Heo 1245c8e55f36STejun Heo /** 1246c8e55f36STejun Heo * worker_leave_idle - leave idle state 1247c8e55f36STejun Heo * @worker: worker which is leaving idle state 1248c8e55f36STejun Heo * 1249c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1250c8e55f36STejun Heo * 1251c8e55f36STejun Heo * LOCKING: 1252c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1253c8e55f36STejun Heo */ 1254c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1255c8e55f36STejun Heo { 1256bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1257c8e55f36STejun Heo 1258c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1259d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1260bd7bdd43STejun Heo pool->nr_idle--; 1261c8e55f36STejun Heo list_del_init(&worker->entry); 1262c8e55f36STejun Heo } 1263c8e55f36STejun Heo 1264e22bee78STejun Heo /** 1265e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1266e22bee78STejun Heo * @worker: self 1267e22bee78STejun Heo * 1268e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1269e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1270e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1271e22bee78STejun Heo * guaranteed to execute on the cpu. 1272e22bee78STejun Heo * 1273e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1274e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1275e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1276e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1277e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1278e22bee78STejun Heo * [dis]associated in the meantime. 1279e22bee78STejun Heo * 1280f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1281f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1282f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1283f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1284f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1285e22bee78STejun Heo * 1286e22bee78STejun Heo * CONTEXT: 1287e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1288e22bee78STejun Heo * held. 1289e22bee78STejun Heo * 1290e22bee78STejun Heo * RETURNS: 1291e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1292e22bee78STejun Heo * bound), %false if offline. 1293e22bee78STejun Heo */ 1294e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1295972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1296e22bee78STejun Heo { 1297bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1298e22bee78STejun Heo struct task_struct *task = worker->task; 1299e22bee78STejun Heo 1300e22bee78STejun Heo while (true) { 1301e22bee78STejun Heo /* 1302e22bee78STejun Heo * The following call may fail, succeed or succeed 1303e22bee78STejun Heo * without actually migrating the task to the cpu if 1304e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1305e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1306e22bee78STejun Heo */ 1307f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1308e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1309e22bee78STejun Heo 1310e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1311e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1312e22bee78STejun Heo return false; 1313e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1314e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1315e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1316e22bee78STejun Heo return true; 1317e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1318e22bee78STejun Heo 13195035b20fSTejun Heo /* 13205035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 13215035b20fSTejun Heo * and retry migration. cond_resched() is required here; 13225035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 13235035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 13245035b20fSTejun Heo */ 1325e22bee78STejun Heo cpu_relax(); 13265035b20fSTejun Heo cond_resched(); 1327e22bee78STejun Heo } 1328e22bee78STejun Heo } 1329e22bee78STejun Heo 133025511a47STejun Heo struct idle_rebind { 133125511a47STejun Heo int cnt; /* # workers to be rebound */ 133225511a47STejun Heo struct completion done; /* all workers rebound */ 133325511a47STejun Heo }; 133425511a47STejun Heo 1335e22bee78STejun Heo /* 133625511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 133725511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 133825511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 133925511a47STejun Heo */ 134025511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 134125511a47STejun Heo { 134225511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 134325511a47STejun Heo 134425511a47STejun Heo /* CPU must be online at this point */ 134525511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 134625511a47STejun Heo if (!--worker->idle_rebind->cnt) 134725511a47STejun Heo complete(&worker->idle_rebind->done); 134825511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 134925511a47STejun Heo 135025511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 135125511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 135225511a47STejun Heo } 135325511a47STejun Heo 135425511a47STejun Heo /* 135525511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1356403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1357403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1358403c821dSTejun Heo * executed twice without intervening cpu down. 1359e22bee78STejun Heo */ 136025511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1361e22bee78STejun Heo { 1362e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1363bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1364e22bee78STejun Heo 1365e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1366e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1367e22bee78STejun Heo 1368e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1369e22bee78STejun Heo } 1370e22bee78STejun Heo 137125511a47STejun Heo /** 137225511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 137325511a47STejun Heo * @gcwq: gcwq of interest 137425511a47STejun Heo * 137525511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 137625511a47STejun Heo * is different for idle and busy ones. 137725511a47STejun Heo * 137825511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 137925511a47STejun Heo * be complete before any worker starts executing work items with 138025511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 138125511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 138225511a47STejun Heo * 138325511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 138425511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 138525511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 138625511a47STejun Heo * 138725511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 138825511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 138925511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 139025511a47STejun Heo * be properbly bumped as busy workers rebind. 139125511a47STejun Heo * 139225511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 139325511a47STejun Heo * work item scheduled. 139425511a47STejun Heo */ 139525511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 139625511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 139725511a47STejun Heo { 139825511a47STejun Heo struct idle_rebind idle_rebind; 139925511a47STejun Heo struct worker_pool *pool; 140025511a47STejun Heo struct worker *worker; 140125511a47STejun Heo struct hlist_node *pos; 140225511a47STejun Heo int i; 140325511a47STejun Heo 140425511a47STejun Heo lockdep_assert_held(&gcwq->lock); 140525511a47STejun Heo 140625511a47STejun Heo for_each_worker_pool(pool, gcwq) 140725511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 140825511a47STejun Heo 140925511a47STejun Heo /* 141025511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 141125511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 141225511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 141325511a47STejun Heo */ 141425511a47STejun Heo init_completion(&idle_rebind.done); 141525511a47STejun Heo retry: 141625511a47STejun Heo idle_rebind.cnt = 1; 141725511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 141825511a47STejun Heo 141925511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 142025511a47STejun Heo for_each_worker_pool(pool, gcwq) { 142125511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 142225511a47STejun Heo if (worker->flags & WORKER_REBIND) 142325511a47STejun Heo continue; 142425511a47STejun Heo 142525511a47STejun Heo /* morph UNBOUND to REBIND */ 142625511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 142725511a47STejun Heo worker->flags |= WORKER_REBIND; 142825511a47STejun Heo 142925511a47STejun Heo idle_rebind.cnt++; 143025511a47STejun Heo worker->idle_rebind = &idle_rebind; 143125511a47STejun Heo 143225511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 143325511a47STejun Heo wake_up_process(worker->task); 143425511a47STejun Heo } 143525511a47STejun Heo } 143625511a47STejun Heo 143725511a47STejun Heo if (--idle_rebind.cnt) { 143825511a47STejun Heo spin_unlock_irq(&gcwq->lock); 143925511a47STejun Heo wait_for_completion(&idle_rebind.done); 144025511a47STejun Heo spin_lock_irq(&gcwq->lock); 144125511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 144225511a47STejun Heo goto retry; 144325511a47STejun Heo } 144425511a47STejun Heo 144525511a47STejun Heo /* 144625511a47STejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 144725511a47STejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 144825511a47STejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 144925511a47STejun Heo * because these workers are still guaranteed to be idle. 145025511a47STejun Heo */ 145125511a47STejun Heo for_each_worker_pool(pool, gcwq) 145225511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 145325511a47STejun Heo worker->flags &= ~WORKER_REBIND; 145425511a47STejun Heo 145525511a47STejun Heo wake_up_all(&gcwq->rebind_hold); 145625511a47STejun Heo 145725511a47STejun Heo /* rebind busy workers */ 145825511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 145925511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 146025511a47STejun Heo 146125511a47STejun Heo /* morph UNBOUND to REBIND */ 146225511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 146325511a47STejun Heo worker->flags |= WORKER_REBIND; 146425511a47STejun Heo 146525511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 146625511a47STejun Heo work_data_bits(rebind_work))) 146725511a47STejun Heo continue; 146825511a47STejun Heo 146925511a47STejun Heo /* wq doesn't matter, use the default one */ 147025511a47STejun Heo debug_work_activate(rebind_work); 147125511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 147225511a47STejun Heo worker->scheduled.next, 147325511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 147425511a47STejun Heo } 147525511a47STejun Heo } 147625511a47STejun Heo 1477c34056a3STejun Heo static struct worker *alloc_worker(void) 1478c34056a3STejun Heo { 1479c34056a3STejun Heo struct worker *worker; 1480c34056a3STejun Heo 1481c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1482c8e55f36STejun Heo if (worker) { 1483c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1484affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 148525511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1486e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1487e22bee78STejun Heo worker->flags = WORKER_PREP; 1488c8e55f36STejun Heo } 1489c34056a3STejun Heo return worker; 1490c34056a3STejun Heo } 1491c34056a3STejun Heo 1492c34056a3STejun Heo /** 1493c34056a3STejun Heo * create_worker - create a new workqueue worker 149463d95a91STejun Heo * @pool: pool the new worker will belong to 1495c34056a3STejun Heo * 149663d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1497c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1498c34056a3STejun Heo * destroy_worker(). 1499c34056a3STejun Heo * 1500c34056a3STejun Heo * CONTEXT: 1501c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1502c34056a3STejun Heo * 1503c34056a3STejun Heo * RETURNS: 1504c34056a3STejun Heo * Pointer to the newly created worker. 1505c34056a3STejun Heo */ 1506bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1507c34056a3STejun Heo { 150863d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 15093270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1510c34056a3STejun Heo struct worker *worker = NULL; 1511f3421797STejun Heo int id = -1; 1512c34056a3STejun Heo 15138b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1514bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 15158b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1516bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1517c34056a3STejun Heo goto fail; 15188b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1519c34056a3STejun Heo } 15208b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1521c34056a3STejun Heo 1522c34056a3STejun Heo worker = alloc_worker(); 1523c34056a3STejun Heo if (!worker) 1524c34056a3STejun Heo goto fail; 1525c34056a3STejun Heo 1526bd7bdd43STejun Heo worker->pool = pool; 1527c34056a3STejun Heo worker->id = id; 1528c34056a3STejun Heo 1529bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 153094dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 15313270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 15323270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1533f3421797STejun Heo else 1534f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 15353270476aSTejun Heo "kworker/u:%d%s", id, pri); 1536c34056a3STejun Heo if (IS_ERR(worker->task)) 1537c34056a3STejun Heo goto fail; 1538c34056a3STejun Heo 15393270476aSTejun Heo if (worker_pool_pri(pool)) 15403270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 15413270476aSTejun Heo 1542db7bccf4STejun Heo /* 1543bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1544bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1545bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1546bc2ae0f5STejun Heo * above the flag definition for details. 1547bc2ae0f5STejun Heo * 1548bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1549bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1550db7bccf4STejun Heo */ 1551bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 15528b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1553bc2ae0f5STejun Heo } else { 1554db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1555f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1556f3421797STejun Heo } 1557c34056a3STejun Heo 1558c34056a3STejun Heo return worker; 1559c34056a3STejun Heo fail: 1560c34056a3STejun Heo if (id >= 0) { 15618b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1562bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 15638b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1564c34056a3STejun Heo } 1565c34056a3STejun Heo kfree(worker); 1566c34056a3STejun Heo return NULL; 1567c34056a3STejun Heo } 1568c34056a3STejun Heo 1569c34056a3STejun Heo /** 1570c34056a3STejun Heo * start_worker - start a newly created worker 1571c34056a3STejun Heo * @worker: worker to start 1572c34056a3STejun Heo * 1573c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1574c34056a3STejun Heo * 1575c34056a3STejun Heo * CONTEXT: 15768b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1577c34056a3STejun Heo */ 1578c34056a3STejun Heo static void start_worker(struct worker *worker) 1579c34056a3STejun Heo { 1580cb444766STejun Heo worker->flags |= WORKER_STARTED; 1581bd7bdd43STejun Heo worker->pool->nr_workers++; 1582c8e55f36STejun Heo worker_enter_idle(worker); 1583c34056a3STejun Heo wake_up_process(worker->task); 1584c34056a3STejun Heo } 1585c34056a3STejun Heo 1586c34056a3STejun Heo /** 1587c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1588c34056a3STejun Heo * @worker: worker to be destroyed 1589c34056a3STejun Heo * 1590c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1591c8e55f36STejun Heo * 1592c8e55f36STejun Heo * CONTEXT: 1593c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1594c34056a3STejun Heo */ 1595c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1596c34056a3STejun Heo { 1597bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1598bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1599c34056a3STejun Heo int id = worker->id; 1600c34056a3STejun Heo 1601c34056a3STejun Heo /* sanity check frenzy */ 1602c34056a3STejun Heo BUG_ON(worker->current_work); 1603affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1604c34056a3STejun Heo 1605c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1606bd7bdd43STejun Heo pool->nr_workers--; 1607c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1608bd7bdd43STejun Heo pool->nr_idle--; 1609c8e55f36STejun Heo 1610c8e55f36STejun Heo list_del_init(&worker->entry); 1611cb444766STejun Heo worker->flags |= WORKER_DIE; 1612c8e55f36STejun Heo 1613c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1614c8e55f36STejun Heo 1615c34056a3STejun Heo kthread_stop(worker->task); 1616c34056a3STejun Heo kfree(worker); 1617c34056a3STejun Heo 16188b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1619bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1620c34056a3STejun Heo } 1621c34056a3STejun Heo 162263d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1623e22bee78STejun Heo { 162463d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 162563d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1626e22bee78STejun Heo 1627e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1628e22bee78STejun Heo 162963d95a91STejun Heo if (too_many_workers(pool)) { 1630e22bee78STejun Heo struct worker *worker; 1631e22bee78STejun Heo unsigned long expires; 1632e22bee78STejun Heo 1633e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 163463d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1635e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1636e22bee78STejun Heo 1637e22bee78STejun Heo if (time_before(jiffies, expires)) 163863d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1639e22bee78STejun Heo else { 1640e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 164111ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 164263d95a91STejun Heo wake_up_worker(pool); 1643e22bee78STejun Heo } 1644e22bee78STejun Heo } 1645e22bee78STejun Heo 1646e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1647e22bee78STejun Heo } 1648e22bee78STejun Heo 1649e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1650e22bee78STejun Heo { 1651e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1652e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1653f3421797STejun Heo unsigned int cpu; 1654e22bee78STejun Heo 1655e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1656e22bee78STejun Heo return false; 1657e22bee78STejun Heo 1658e22bee78STejun Heo /* mayday mayday mayday */ 1659bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1660f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1661f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1662f3421797STejun Heo cpu = 0; 1663f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1664e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1665e22bee78STejun Heo return true; 1666e22bee78STejun Heo } 1667e22bee78STejun Heo 166863d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1669e22bee78STejun Heo { 167063d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 167163d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1672e22bee78STejun Heo struct work_struct *work; 1673e22bee78STejun Heo 1674e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1675e22bee78STejun Heo 167663d95a91STejun Heo if (need_to_create_worker(pool)) { 1677e22bee78STejun Heo /* 1678e22bee78STejun Heo * We've been trying to create a new worker but 1679e22bee78STejun Heo * haven't been successful. We might be hitting an 1680e22bee78STejun Heo * allocation deadlock. Send distress signals to 1681e22bee78STejun Heo * rescuers. 1682e22bee78STejun Heo */ 168363d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1684e22bee78STejun Heo send_mayday(work); 1685e22bee78STejun Heo } 1686e22bee78STejun Heo 1687e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1688e22bee78STejun Heo 168963d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1690e22bee78STejun Heo } 1691e22bee78STejun Heo 1692e22bee78STejun Heo /** 1693e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 169463d95a91STejun Heo * @pool: pool to create a new worker for 1695e22bee78STejun Heo * 169663d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1697e22bee78STejun Heo * have at least one idle worker on return from this function. If 1698e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 169963d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1700e22bee78STejun Heo * possible allocation deadlock. 1701e22bee78STejun Heo * 1702e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1703e22bee78STejun Heo * may_start_working() true. 1704e22bee78STejun Heo * 1705e22bee78STejun Heo * LOCKING: 1706e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1707e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1708e22bee78STejun Heo * manager. 1709e22bee78STejun Heo * 1710e22bee78STejun Heo * RETURNS: 1711e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1712e22bee78STejun Heo * otherwise. 1713e22bee78STejun Heo */ 171463d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 171506bd6ebfSNamhyung Kim __releases(&gcwq->lock) 171606bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1717e22bee78STejun Heo { 171863d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 171963d95a91STejun Heo 172063d95a91STejun Heo if (!need_to_create_worker(pool)) 1721e22bee78STejun Heo return false; 1722e22bee78STejun Heo restart: 17239f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 17249f9c2364STejun Heo 1725e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 172663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1727e22bee78STejun Heo 1728e22bee78STejun Heo while (true) { 1729e22bee78STejun Heo struct worker *worker; 1730e22bee78STejun Heo 1731bc2ae0f5STejun Heo worker = create_worker(pool); 1732e22bee78STejun Heo if (worker) { 173363d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1734e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1735e22bee78STejun Heo start_worker(worker); 173663d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1737e22bee78STejun Heo return true; 1738e22bee78STejun Heo } 1739e22bee78STejun Heo 174063d95a91STejun Heo if (!need_to_create_worker(pool)) 1741e22bee78STejun Heo break; 1742e22bee78STejun Heo 1743e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1744e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 17459f9c2364STejun Heo 174663d95a91STejun Heo if (!need_to_create_worker(pool)) 1747e22bee78STejun Heo break; 1748e22bee78STejun Heo } 1749e22bee78STejun Heo 175063d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1751e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 175263d95a91STejun Heo if (need_to_create_worker(pool)) 1753e22bee78STejun Heo goto restart; 1754e22bee78STejun Heo return true; 1755e22bee78STejun Heo } 1756e22bee78STejun Heo 1757e22bee78STejun Heo /** 1758e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 175963d95a91STejun Heo * @pool: pool to destroy workers for 1760e22bee78STejun Heo * 176163d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1762e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1763e22bee78STejun Heo * 1764e22bee78STejun Heo * LOCKING: 1765e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1766e22bee78STejun Heo * multiple times. Called only from manager. 1767e22bee78STejun Heo * 1768e22bee78STejun Heo * RETURNS: 1769e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1770e22bee78STejun Heo * otherwise. 1771e22bee78STejun Heo */ 177263d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1773e22bee78STejun Heo { 1774e22bee78STejun Heo bool ret = false; 1775e22bee78STejun Heo 177663d95a91STejun Heo while (too_many_workers(pool)) { 1777e22bee78STejun Heo struct worker *worker; 1778e22bee78STejun Heo unsigned long expires; 1779e22bee78STejun Heo 178063d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1781e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1782e22bee78STejun Heo 1783e22bee78STejun Heo if (time_before(jiffies, expires)) { 178463d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1785e22bee78STejun Heo break; 1786e22bee78STejun Heo } 1787e22bee78STejun Heo 1788e22bee78STejun Heo destroy_worker(worker); 1789e22bee78STejun Heo ret = true; 1790e22bee78STejun Heo } 1791e22bee78STejun Heo 1792e22bee78STejun Heo return ret; 1793e22bee78STejun Heo } 1794e22bee78STejun Heo 1795e22bee78STejun Heo /** 1796e22bee78STejun Heo * manage_workers - manage worker pool 1797e22bee78STejun Heo * @worker: self 1798e22bee78STejun Heo * 1799e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1800e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1801e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1802e22bee78STejun Heo * 1803e22bee78STejun Heo * The caller can safely start processing works on false return. On 1804e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1805e22bee78STejun Heo * and may_start_working() is true. 1806e22bee78STejun Heo * 1807e22bee78STejun Heo * CONTEXT: 1808e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1809e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1810e22bee78STejun Heo * 1811e22bee78STejun Heo * RETURNS: 1812e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1813e22bee78STejun Heo * some action was taken. 1814e22bee78STejun Heo */ 1815e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1816e22bee78STejun Heo { 181763d95a91STejun Heo struct worker_pool *pool = worker->pool; 1818e22bee78STejun Heo bool ret = false; 1819e22bee78STejun Heo 182060373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1821e22bee78STejun Heo return ret; 1822e22bee78STejun Heo 182311ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1824e22bee78STejun Heo 1825e22bee78STejun Heo /* 1826e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1827e22bee78STejun Heo * on return. 1828e22bee78STejun Heo */ 182963d95a91STejun Heo ret |= maybe_destroy_workers(pool); 183063d95a91STejun Heo ret |= maybe_create_worker(pool); 1831e22bee78STejun Heo 183260373152STejun Heo mutex_unlock(&pool->manager_mutex); 1833e22bee78STejun Heo return ret; 1834e22bee78STejun Heo } 1835e22bee78STejun Heo 1836a62428c0STejun Heo /** 1837affee4b2STejun Heo * move_linked_works - move linked works to a list 1838affee4b2STejun Heo * @work: start of series of works to be scheduled 1839affee4b2STejun Heo * @head: target list to append @work to 1840affee4b2STejun Heo * @nextp: out paramter for nested worklist walking 1841affee4b2STejun Heo * 1842affee4b2STejun Heo * Schedule linked works starting from @work to @head. Work series to 1843affee4b2STejun Heo * be scheduled starts at @work and includes any consecutive work with 1844affee4b2STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1845affee4b2STejun Heo * 1846affee4b2STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1847affee4b2STejun Heo * the last scheduled work. This allows move_linked_works() to be 1848affee4b2STejun Heo * nested inside outer list_for_each_entry_safe(). 1849affee4b2STejun Heo * 1850affee4b2STejun Heo * CONTEXT: 18518b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1852affee4b2STejun Heo */ 1853affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1854affee4b2STejun Heo struct work_struct **nextp) 1855affee4b2STejun Heo { 1856affee4b2STejun Heo struct work_struct *n; 1857affee4b2STejun Heo 1858affee4b2STejun Heo /* 1859affee4b2STejun Heo * Linked worklist will always end before the end of the list, 1860affee4b2STejun Heo * use NULL for list head. 1861affee4b2STejun Heo */ 1862affee4b2STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1863affee4b2STejun Heo list_move_tail(&work->entry, head); 1864affee4b2STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1865affee4b2STejun Heo break; 1866affee4b2STejun Heo } 1867affee4b2STejun Heo 1868affee4b2STejun Heo /* 1869affee4b2STejun Heo * If we're already inside safe list traversal and have moved 1870affee4b2STejun Heo * multiple works to the scheduled queue, the next position 1871affee4b2STejun Heo * needs to be updated. 1872affee4b2STejun Heo */ 1873affee4b2STejun Heo if (nextp) 1874affee4b2STejun Heo *nextp = n; 1875affee4b2STejun Heo } 1876affee4b2STejun Heo 18771e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 18781e19ffc6STejun Heo { 18791e19ffc6STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 18801da177e4SLinus Torvalds struct work_struct, entry); 18811e19ffc6STejun Heo 1882cdadf009STejun Heo trace_workqueue_activate_work(work); 18833270476aSTejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 18848a2e8e5dSTejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 18851e19ffc6STejun Heo cwq->nr_active++; 18861e19ffc6STejun Heo } 18871e19ffc6STejun Heo 1888affee4b2STejun Heo /** 188973f53c4aSTejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 189073f53c4aSTejun Heo * @cwq: cwq of interest 189173f53c4aSTejun Heo * @color: color of work which left the queue 18928a2e8e5dSTejun Heo * @delayed: for a delayed work 189373f53c4aSTejun Heo * 189473f53c4aSTejun Heo * A work either has completed or is removed from pending queue, 189573f53c4aSTejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 189673f53c4aSTejun Heo * 189773f53c4aSTejun Heo * CONTEXT: 18988b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 189973f53c4aSTejun Heo */ 19008a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 19018a2e8e5dSTejun Heo bool delayed) 190273f53c4aSTejun Heo { 190373f53c4aSTejun Heo /* ignore uncolored works */ 190473f53c4aSTejun Heo if (color == WORK_NO_COLOR) 190573f53c4aSTejun Heo return; 190673f53c4aSTejun Heo 190773f53c4aSTejun Heo cwq->nr_in_flight[color]--; 19081e19ffc6STejun Heo 19098a2e8e5dSTejun Heo if (!delayed) { 19108a2e8e5dSTejun Heo cwq->nr_active--; 1911502ca9d8STejun Heo if (!list_empty(&cwq->delayed_works)) { 19121e19ffc6STejun Heo /* one down, submit a delayed one */ 1913502ca9d8STejun Heo if (cwq->nr_active < cwq->max_active) 19141e19ffc6STejun Heo cwq_activate_first_delayed(cwq); 1915502ca9d8STejun Heo } 19168a2e8e5dSTejun Heo } 191773f53c4aSTejun Heo 191873f53c4aSTejun Heo /* is flush in progress and are we at the flushing tip? */ 191973f53c4aSTejun Heo if (likely(cwq->flush_color != color)) 192073f53c4aSTejun Heo return; 192173f53c4aSTejun Heo 192273f53c4aSTejun Heo /* are there still in-flight works? */ 192373f53c4aSTejun Heo if (cwq->nr_in_flight[color]) 192473f53c4aSTejun Heo return; 192573f53c4aSTejun Heo 192673f53c4aSTejun Heo /* this cwq is done, clear flush_color */ 192773f53c4aSTejun Heo cwq->flush_color = -1; 192873f53c4aSTejun Heo 192973f53c4aSTejun Heo /* 193073f53c4aSTejun Heo * If this was the last cwq, wake up the first flusher. It 193173f53c4aSTejun Heo * will handle the rest. 193273f53c4aSTejun Heo */ 193373f53c4aSTejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 193473f53c4aSTejun Heo complete(&cwq->wq->first_flusher->done); 193573f53c4aSTejun Heo } 193673f53c4aSTejun Heo 193773f53c4aSTejun Heo /** 1938a62428c0STejun Heo * process_one_work - process single work 1939c34056a3STejun Heo * @worker: self 1940a62428c0STejun Heo * @work: work to process 1941a62428c0STejun Heo * 1942a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1943a62428c0STejun Heo * process a single work including synchronization against and 1944a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1945a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1946a62428c0STejun Heo * call this function to process a work. 1947a62428c0STejun Heo * 1948a62428c0STejun Heo * CONTEXT: 19498b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1950a62428c0STejun Heo */ 1951c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 195206bd6ebfSNamhyung Kim __releases(&gcwq->lock) 195306bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 19541da177e4SLinus Torvalds { 19557e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1956bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1957bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1958c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 1959fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 19606bb49e59SDavid Howells work_func_t f = work->func; 196173f53c4aSTejun Heo int work_color; 19627e11629dSTejun Heo struct worker *collision; 19634e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 19644e6045f1SJohannes Berg /* 1965a62428c0STejun Heo * It is permissible to free the struct work_struct from 1966a62428c0STejun Heo * inside the function that is called from it, this we need to 1967a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1968a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1969a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 19704e6045f1SJohannes Berg */ 19714d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 19724d82a1deSPeter Zijlstra 19734d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 19744e6045f1SJohannes Berg #endif 19756fec10a1STejun Heo /* 19766fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 19776fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 19786fec10a1STejun Heo * unbound or a disassociated gcwq. 19796fec10a1STejun Heo */ 198025511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 19816fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 198225511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 198325511a47STejun Heo 19847e11629dSTejun Heo /* 19857e11629dSTejun Heo * A single work shouldn't be executed concurrently by 19867e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 19877e11629dSTejun Heo * already processing the work. If so, defer the work to the 19887e11629dSTejun Heo * currently executing one. 19897e11629dSTejun Heo */ 19907e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 19917e11629dSTejun Heo if (unlikely(collision)) { 19927e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 19937e11629dSTejun Heo return; 19947e11629dSTejun Heo } 19951da177e4SLinus Torvalds 19968930cabaSTejun Heo /* claim and dequeue */ 19971da177e4SLinus Torvalds debug_work_deactivate(work); 1998c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 1999c34056a3STejun Heo worker->current_work = work; 20008cca0eeaSTejun Heo worker->current_cwq = cwq; 200173f53c4aSTejun Heo work_color = get_work_color(work); 20027a22ad75STejun Heo 2003a62428c0STejun Heo list_del_init(&work->entry); 2004a62428c0STejun Heo 2005649027d7STejun Heo /* 2006fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2007fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2008fb0e7bebSTejun Heo */ 2009fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2010fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2011fb0e7bebSTejun Heo 2012974271c4STejun Heo /* 2013974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 2014974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2015974271c4STejun Heo */ 201663d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 201763d95a91STejun Heo wake_up_worker(pool); 2018974271c4STejun Heo 20198930cabaSTejun Heo /* 20208930cabaSTejun Heo * Record the last CPU and clear PENDING. The following wmb is 20218930cabaSTejun Heo * paired with the implied mb in test_and_set_bit(PENDING) and 20228930cabaSTejun Heo * ensures all updates to @work made here are visible to and 20238930cabaSTejun Heo * precede any updates by the next PENDING owner. Also, clear 20248930cabaSTejun Heo * PENDING inside @gcwq->lock so that PENDING and queued state 20258930cabaSTejun Heo * changes happen together while IRQ is disabled. 20268930cabaSTejun Heo */ 20278930cabaSTejun Heo smp_wmb(); 20288930cabaSTejun Heo set_work_cpu_and_clear_pending(work, gcwq->cpu); 20291da177e4SLinus Torvalds 20308930cabaSTejun Heo spin_unlock_irq(&gcwq->lock); 2031959d1af8STejun Heo 2032e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 20333295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2034e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 203565f27f38SDavid Howells f(work); 2036e36c886aSArjan van de Ven /* 2037e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2038e36c886aSArjan van de Ven * point will only record its address. 2039e36c886aSArjan van de Ven */ 2040e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20413295f0efSIngo Molnar lock_map_release(&lockdep_map); 20423295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 20431da177e4SLinus Torvalds 2044d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2045d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2046d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2047a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2048d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2049d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2050d5abe669SPeter Zijlstra debug_show_held_locks(current); 2051d5abe669SPeter Zijlstra dump_stack(); 2052d5abe669SPeter Zijlstra } 2053d5abe669SPeter Zijlstra 20548b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2055a62428c0STejun Heo 2056fb0e7bebSTejun Heo /* clear cpu intensive status */ 2057fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2058fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2059fb0e7bebSTejun Heo 2060a62428c0STejun Heo /* we're done with it, release */ 2061c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2062c34056a3STejun Heo worker->current_work = NULL; 20638cca0eeaSTejun Heo worker->current_cwq = NULL; 20648a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 2067affee4b2STejun Heo /** 2068affee4b2STejun Heo * process_scheduled_works - process scheduled works 2069affee4b2STejun Heo * @worker: self 2070affee4b2STejun Heo * 2071affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2072affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2073affee4b2STejun Heo * fetches a work from the top and executes it. 2074affee4b2STejun Heo * 2075affee4b2STejun Heo * CONTEXT: 20768b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2077affee4b2STejun Heo * multiple times. 2078affee4b2STejun Heo */ 2079affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 20801da177e4SLinus Torvalds { 2081affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2082affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2083a62428c0STejun Heo struct work_struct, entry); 2084c34056a3STejun Heo process_one_work(worker, work); 2085a62428c0STejun Heo } 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 20884690c4abSTejun Heo /** 20894690c4abSTejun Heo * worker_thread - the worker thread function 2090c34056a3STejun Heo * @__worker: self 20914690c4abSTejun Heo * 2092e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2093e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2094e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2095e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2096e22bee78STejun Heo * rescuer_thread(). 20974690c4abSTejun Heo */ 2098c34056a3STejun Heo static int worker_thread(void *__worker) 20991da177e4SLinus Torvalds { 2100c34056a3STejun Heo struct worker *worker = __worker; 2101bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2102bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 21031da177e4SLinus Torvalds 2104e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2105e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2106c8e55f36STejun Heo woke_up: 21078b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2108affee4b2STejun Heo 210925511a47STejun Heo /* 211025511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 211125511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 211225511a47STejun Heo */ 211325511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2114c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 211525511a47STejun Heo 211625511a47STejun Heo if (worker->flags & WORKER_DIE) { 2117e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2118c8e55f36STejun Heo return 0; 2119c8e55f36STejun Heo } 2120c8e55f36STejun Heo 212125511a47STejun Heo idle_worker_rebind(worker); 212225511a47STejun Heo goto woke_up; 212325511a47STejun Heo } 212425511a47STejun Heo 2125c8e55f36STejun Heo worker_leave_idle(worker); 2126db7bccf4STejun Heo recheck: 2127e22bee78STejun Heo /* no more worker necessary? */ 212863d95a91STejun Heo if (!need_more_worker(pool)) 2129e22bee78STejun Heo goto sleep; 2130e22bee78STejun Heo 2131e22bee78STejun Heo /* do we need to manage? */ 213263d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2133e22bee78STejun Heo goto recheck; 2134e22bee78STejun Heo 2135c8e55f36STejun Heo /* 2136c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2137c8e55f36STejun Heo * preparing to process a work or actually processing it. 2138c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2139c8e55f36STejun Heo */ 2140c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2141c8e55f36STejun Heo 2142e22bee78STejun Heo /* 2143e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2144e22bee78STejun Heo * at least one idle worker or that someone else has already 2145e22bee78STejun Heo * assumed the manager role. 2146e22bee78STejun Heo */ 2147e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2148e22bee78STejun Heo 2149e22bee78STejun Heo do { 2150affee4b2STejun Heo struct work_struct *work = 2151bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2152affee4b2STejun Heo struct work_struct, entry); 2153affee4b2STejun Heo 2154c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2155affee4b2STejun Heo /* optimization path, not strictly necessary */ 2156affee4b2STejun Heo process_one_work(worker, work); 2157affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2158affee4b2STejun Heo process_scheduled_works(worker); 2159affee4b2STejun Heo } else { 2160c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2161affee4b2STejun Heo process_scheduled_works(worker); 2162affee4b2STejun Heo } 216363d95a91STejun Heo } while (keep_working(pool)); 2164affee4b2STejun Heo 2165e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2166d313dd85STejun Heo sleep: 216763d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2168e22bee78STejun Heo goto recheck; 2169d313dd85STejun Heo 2170c8e55f36STejun Heo /* 2171e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2172e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2173e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2174e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2175e22bee78STejun Heo * prevent losing any event. 2176c8e55f36STejun Heo */ 2177c8e55f36STejun Heo worker_enter_idle(worker); 2178c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 21798b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 21801da177e4SLinus Torvalds schedule(); 2181c8e55f36STejun Heo goto woke_up; 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 2184e22bee78STejun Heo /** 2185e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2186e22bee78STejun Heo * @__wq: the associated workqueue 2187e22bee78STejun Heo * 2188e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2189e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2190e22bee78STejun Heo * 2191e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2192e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2193e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2194e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2195e22bee78STejun Heo * the problem rescuer solves. 2196e22bee78STejun Heo * 2197e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2198e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2199e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2200e22bee78STejun Heo * 2201e22bee78STejun Heo * This should happen rarely. 2202e22bee78STejun Heo */ 2203e22bee78STejun Heo static int rescuer_thread(void *__wq) 2204e22bee78STejun Heo { 2205e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2206e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2207e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2208f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2209e22bee78STejun Heo unsigned int cpu; 2210e22bee78STejun Heo 2211e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2212e22bee78STejun Heo repeat: 2213e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds if (kthread_should_stop()) 2216e22bee78STejun Heo return 0; 22171da177e4SLinus Torvalds 2218f3421797STejun Heo /* 2219f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2220f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2221f3421797STejun Heo */ 2222f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2223f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2224f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2225bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2226bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2227e22bee78STejun Heo struct work_struct *work, *n; 2228e22bee78STejun Heo 2229e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2230f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2231e22bee78STejun Heo 2232e22bee78STejun Heo /* migrate to the target cpu if possible */ 2233bd7bdd43STejun Heo rescuer->pool = pool; 2234e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2235e22bee78STejun Heo 2236e22bee78STejun Heo /* 2237e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2238e22bee78STejun Heo * process'em. 2239e22bee78STejun Heo */ 2240e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2241bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2242e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2243e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2244e22bee78STejun Heo 2245e22bee78STejun Heo process_scheduled_works(rescuer); 22467576958aSTejun Heo 22477576958aSTejun Heo /* 22487576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 22497576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22507576958aSTejun Heo * and stalling the execution. 22517576958aSTejun Heo */ 225263d95a91STejun Heo if (keep_working(pool)) 225363d95a91STejun Heo wake_up_worker(pool); 22547576958aSTejun Heo 2255e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 2258e22bee78STejun Heo schedule(); 2259e22bee78STejun Heo goto repeat; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 2262fc2e4d70SOleg Nesterov struct wq_barrier { 2263fc2e4d70SOleg Nesterov struct work_struct work; 2264fc2e4d70SOleg Nesterov struct completion done; 2265fc2e4d70SOleg Nesterov }; 2266fc2e4d70SOleg Nesterov 2267fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2268fc2e4d70SOleg Nesterov { 2269fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2270fc2e4d70SOleg Nesterov complete(&barr->done); 2271fc2e4d70SOleg Nesterov } 2272fc2e4d70SOleg Nesterov 22734690c4abSTejun Heo /** 22744690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 22754690c4abSTejun Heo * @cwq: cwq to insert barrier into 22764690c4abSTejun Heo * @barr: wq_barrier to insert 2277affee4b2STejun Heo * @target: target work to attach @barr to 2278affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 22794690c4abSTejun Heo * 2280affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2281affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2282affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2283affee4b2STejun Heo * cpu. 2284affee4b2STejun Heo * 2285affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2286affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2287affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2288affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2289affee4b2STejun Heo * after a work with LINKED flag set. 2290affee4b2STejun Heo * 2291affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2292affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 22934690c4abSTejun Heo * 22944690c4abSTejun Heo * CONTEXT: 22958b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 22964690c4abSTejun Heo */ 229783c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2298affee4b2STejun Heo struct wq_barrier *barr, 2299affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2300fc2e4d70SOleg Nesterov { 2301affee4b2STejun Heo struct list_head *head; 2302affee4b2STejun Heo unsigned int linked = 0; 2303affee4b2STejun Heo 2304dc186ad7SThomas Gleixner /* 23058b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2306dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2307dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2308dc186ad7SThomas Gleixner * might deadlock. 2309dc186ad7SThomas Gleixner */ 2310ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 231122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2312fc2e4d70SOleg Nesterov init_completion(&barr->done); 231383c22520SOleg Nesterov 2314affee4b2STejun Heo /* 2315affee4b2STejun Heo * If @target is currently being executed, schedule the 2316affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2317affee4b2STejun Heo */ 2318affee4b2STejun Heo if (worker) 2319affee4b2STejun Heo head = worker->scheduled.next; 2320affee4b2STejun Heo else { 2321affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2322affee4b2STejun Heo 2323affee4b2STejun Heo head = target->entry.next; 2324affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2325affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2326affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2327affee4b2STejun Heo } 2328affee4b2STejun Heo 2329dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2330affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2331affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2332fc2e4d70SOleg Nesterov } 2333fc2e4d70SOleg Nesterov 233473f53c4aSTejun Heo /** 233573f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 233673f53c4aSTejun Heo * @wq: workqueue being flushed 233773f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 233873f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 233973f53c4aSTejun Heo * 234073f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 234173f53c4aSTejun Heo * 234273f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 234373f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 234473f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 234573f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 234673f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 234773f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 234873f53c4aSTejun Heo * 234973f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 235073f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 235173f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 235273f53c4aSTejun Heo * is returned. 235373f53c4aSTejun Heo * 235473f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 235573f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 235673f53c4aSTejun Heo * advanced to @work_color. 235773f53c4aSTejun Heo * 235873f53c4aSTejun Heo * CONTEXT: 235973f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 236073f53c4aSTejun Heo * 236173f53c4aSTejun Heo * RETURNS: 236273f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 236373f53c4aSTejun Heo * otherwise. 236473f53c4aSTejun Heo */ 236573f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 236673f53c4aSTejun Heo int flush_color, int work_color) 23671da177e4SLinus Torvalds { 236873f53c4aSTejun Heo bool wait = false; 236973f53c4aSTejun Heo unsigned int cpu; 23701da177e4SLinus Torvalds 237173f53c4aSTejun Heo if (flush_color >= 0) { 237273f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 237373f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2374dc186ad7SThomas Gleixner } 237514441960SOleg Nesterov 2376f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 237773f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2378bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 23791da177e4SLinus Torvalds 23808b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 238173f53c4aSTejun Heo 238273f53c4aSTejun Heo if (flush_color >= 0) { 238373f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 238473f53c4aSTejun Heo 238573f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 238673f53c4aSTejun Heo cwq->flush_color = flush_color; 238773f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 238873f53c4aSTejun Heo wait = true; 23891da177e4SLinus Torvalds } 239073f53c4aSTejun Heo } 239173f53c4aSTejun Heo 239273f53c4aSTejun Heo if (work_color >= 0) { 239373f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 239473f53c4aSTejun Heo cwq->work_color = work_color; 239573f53c4aSTejun Heo } 239673f53c4aSTejun Heo 23978b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds 240073f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 240173f53c4aSTejun Heo complete(&wq->first_flusher->done); 240273f53c4aSTejun Heo 240373f53c4aSTejun Heo return wait; 240483c22520SOleg Nesterov } 24051da177e4SLinus Torvalds 24060fcb78c2SRolf Eike Beer /** 24071da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 24080fcb78c2SRolf Eike Beer * @wq: workqueue to flush 24091da177e4SLinus Torvalds * 24101da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 24111da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 24121da177e4SLinus Torvalds * 2413fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2414fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 24151da177e4SLinus Torvalds */ 24167ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 24171da177e4SLinus Torvalds { 241873f53c4aSTejun Heo struct wq_flusher this_flusher = { 241973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 242073f53c4aSTejun Heo .flush_color = -1, 242173f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 242273f53c4aSTejun Heo }; 242373f53c4aSTejun Heo int next_color; 2424b1f4ec17SOleg Nesterov 24253295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24263295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 242773f53c4aSTejun Heo 242873f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 242973f53c4aSTejun Heo 243073f53c4aSTejun Heo /* 243173f53c4aSTejun Heo * Start-to-wait phase 243273f53c4aSTejun Heo */ 243373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 243473f53c4aSTejun Heo 243573f53c4aSTejun Heo if (next_color != wq->flush_color) { 243673f53c4aSTejun Heo /* 243773f53c4aSTejun Heo * Color space is not full. The current work_color 243873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 243973f53c4aSTejun Heo * by one. 244073f53c4aSTejun Heo */ 244173f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 244273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 244373f53c4aSTejun Heo wq->work_color = next_color; 244473f53c4aSTejun Heo 244573f53c4aSTejun Heo if (!wq->first_flusher) { 244673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 244773f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 244873f53c4aSTejun Heo 244973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 245073f53c4aSTejun Heo 245173f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 245273f53c4aSTejun Heo wq->work_color)) { 245373f53c4aSTejun Heo /* nothing to flush, done */ 245473f53c4aSTejun Heo wq->flush_color = next_color; 245573f53c4aSTejun Heo wq->first_flusher = NULL; 245673f53c4aSTejun Heo goto out_unlock; 245773f53c4aSTejun Heo } 245873f53c4aSTejun Heo } else { 245973f53c4aSTejun Heo /* wait in queue */ 246073f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 246173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 246273f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 246373f53c4aSTejun Heo } 246473f53c4aSTejun Heo } else { 246573f53c4aSTejun Heo /* 246673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 246773f53c4aSTejun Heo * The next flush completion will assign us 246873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 246973f53c4aSTejun Heo */ 247073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 247173f53c4aSTejun Heo } 247273f53c4aSTejun Heo 247373f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 247473f53c4aSTejun Heo 247573f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 247673f53c4aSTejun Heo 247773f53c4aSTejun Heo /* 247873f53c4aSTejun Heo * Wake-up-and-cascade phase 247973f53c4aSTejun Heo * 248073f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 248173f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 248273f53c4aSTejun Heo */ 248373f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 248473f53c4aSTejun Heo return; 248573f53c4aSTejun Heo 248673f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 248773f53c4aSTejun Heo 24884ce48b37STejun Heo /* we might have raced, check again with mutex held */ 24894ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 24904ce48b37STejun Heo goto out_unlock; 24914ce48b37STejun Heo 249273f53c4aSTejun Heo wq->first_flusher = NULL; 249373f53c4aSTejun Heo 249473f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 249573f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 249673f53c4aSTejun Heo 249773f53c4aSTejun Heo while (true) { 249873f53c4aSTejun Heo struct wq_flusher *next, *tmp; 249973f53c4aSTejun Heo 250073f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 250173f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 250273f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 250373f53c4aSTejun Heo break; 250473f53c4aSTejun Heo list_del_init(&next->list); 250573f53c4aSTejun Heo complete(&next->done); 250673f53c4aSTejun Heo } 250773f53c4aSTejun Heo 250873f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 250973f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 251073f53c4aSTejun Heo 251173f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 251273f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 251373f53c4aSTejun Heo 251473f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 251573f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 251673f53c4aSTejun Heo /* 251773f53c4aSTejun Heo * Assign the same color to all overflowed 251873f53c4aSTejun Heo * flushers, advance work_color and append to 251973f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 252073f53c4aSTejun Heo * phase for these overflowed flushers. 252173f53c4aSTejun Heo */ 252273f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 252373f53c4aSTejun Heo tmp->flush_color = wq->work_color; 252473f53c4aSTejun Heo 252573f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 252673f53c4aSTejun Heo 252773f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 252873f53c4aSTejun Heo &wq->flusher_queue); 252973f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 253073f53c4aSTejun Heo } 253173f53c4aSTejun Heo 253273f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 253373f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 253473f53c4aSTejun Heo break; 253573f53c4aSTejun Heo } 253673f53c4aSTejun Heo 253773f53c4aSTejun Heo /* 253873f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 253973f53c4aSTejun Heo * the new first flusher and arm cwqs. 254073f53c4aSTejun Heo */ 254173f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 254273f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 254373f53c4aSTejun Heo 254473f53c4aSTejun Heo list_del_init(&next->list); 254573f53c4aSTejun Heo wq->first_flusher = next; 254673f53c4aSTejun Heo 254773f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 254873f53c4aSTejun Heo break; 254973f53c4aSTejun Heo 255073f53c4aSTejun Heo /* 255173f53c4aSTejun Heo * Meh... this color is already done, clear first 255273f53c4aSTejun Heo * flusher and repeat cascading. 255373f53c4aSTejun Heo */ 255473f53c4aSTejun Heo wq->first_flusher = NULL; 255573f53c4aSTejun Heo } 255673f53c4aSTejun Heo 255773f53c4aSTejun Heo out_unlock: 255873f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 25591da177e4SLinus Torvalds } 2560ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 25611da177e4SLinus Torvalds 25629c5a2ba7STejun Heo /** 25639c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 25649c5a2ba7STejun Heo * @wq: workqueue to drain 25659c5a2ba7STejun Heo * 25669c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 25679c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 25689c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 25699c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 25709c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 25719c5a2ba7STejun Heo * takes too long. 25729c5a2ba7STejun Heo */ 25739c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 25749c5a2ba7STejun Heo { 25759c5a2ba7STejun Heo unsigned int flush_cnt = 0; 25769c5a2ba7STejun Heo unsigned int cpu; 25779c5a2ba7STejun Heo 25789c5a2ba7STejun Heo /* 25799c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 25809c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 25819c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 25829c5a2ba7STejun Heo */ 25839c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25849c5a2ba7STejun Heo if (!wq->nr_drainers++) 25859c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 25869c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25879c5a2ba7STejun Heo reflush: 25889c5a2ba7STejun Heo flush_workqueue(wq); 25899c5a2ba7STejun Heo 25909c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 25919c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2592fa2563e4SThomas Tuttle bool drained; 25939c5a2ba7STejun Heo 2594bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2595fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2596bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2597fa2563e4SThomas Tuttle 2598fa2563e4SThomas Tuttle if (drained) 25999c5a2ba7STejun Heo continue; 26009c5a2ba7STejun Heo 26019c5a2ba7STejun Heo if (++flush_cnt == 10 || 26029c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 26039c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 26049c5a2ba7STejun Heo wq->name, flush_cnt); 26059c5a2ba7STejun Heo goto reflush; 26069c5a2ba7STejun Heo } 26079c5a2ba7STejun Heo 26089c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26099c5a2ba7STejun Heo if (!--wq->nr_drainers) 26109c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 26119c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26129c5a2ba7STejun Heo } 26139c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 26149c5a2ba7STejun Heo 2615baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2616baf59022STejun Heo bool wait_executing) 2617baf59022STejun Heo { 2618baf59022STejun Heo struct worker *worker = NULL; 2619baf59022STejun Heo struct global_cwq *gcwq; 2620baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2621baf59022STejun Heo 2622baf59022STejun Heo might_sleep(); 2623baf59022STejun Heo gcwq = get_work_gcwq(work); 2624baf59022STejun Heo if (!gcwq) 2625baf59022STejun Heo return false; 2626baf59022STejun Heo 2627baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2628baf59022STejun Heo if (!list_empty(&work->entry)) { 2629baf59022STejun Heo /* 2630baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2631baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2632baf59022STejun Heo * are not going to wait. 2633baf59022STejun Heo */ 2634baf59022STejun Heo smp_rmb(); 2635baf59022STejun Heo cwq = get_work_cwq(work); 2636bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2637baf59022STejun Heo goto already_gone; 2638baf59022STejun Heo } else if (wait_executing) { 2639baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2640baf59022STejun Heo if (!worker) 2641baf59022STejun Heo goto already_gone; 2642baf59022STejun Heo cwq = worker->current_cwq; 2643baf59022STejun Heo } else 2644baf59022STejun Heo goto already_gone; 2645baf59022STejun Heo 2646baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2647baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2648baf59022STejun Heo 2649e159489bSTejun Heo /* 2650e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2651e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2652e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2653e159489bSTejun Heo * access. 2654e159489bSTejun Heo */ 2655e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2656baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2657e159489bSTejun Heo else 2658e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2659baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2660e159489bSTejun Heo 2661baf59022STejun Heo return true; 2662baf59022STejun Heo already_gone: 2663baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2664baf59022STejun Heo return false; 2665baf59022STejun Heo } 2666baf59022STejun Heo 2667db700897SOleg Nesterov /** 2668401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2669401a8d04STejun Heo * @work: the work to flush 2670db700897SOleg Nesterov * 2671401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2672401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2673401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2674401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2675401a8d04STejun Heo * some of the CPUs from earlier queueing. 2676a67da70dSOleg Nesterov * 2677401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2678401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2679401a8d04STejun Heo * been requeued since flush started. 2680401a8d04STejun Heo * 2681401a8d04STejun Heo * RETURNS: 2682401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2683401a8d04STejun Heo * %false if it was already idle. 2684db700897SOleg Nesterov */ 2685401a8d04STejun Heo bool flush_work(struct work_struct *work) 2686db700897SOleg Nesterov { 2687db700897SOleg Nesterov struct wq_barrier barr; 2688db700897SOleg Nesterov 26890976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 26900976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 26910976dfc1SStephen Boyd 2692baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2693db700897SOleg Nesterov wait_for_completion(&barr.done); 2694dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2695401a8d04STejun Heo return true; 2696baf59022STejun Heo } else 2697401a8d04STejun Heo return false; 2698db700897SOleg Nesterov } 2699db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2700db700897SOleg Nesterov 2701401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2702401a8d04STejun Heo { 2703401a8d04STejun Heo struct wq_barrier barr; 2704401a8d04STejun Heo struct worker *worker; 2705401a8d04STejun Heo 2706401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2707401a8d04STejun Heo 2708401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2709401a8d04STejun Heo if (unlikely(worker)) 2710401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2711401a8d04STejun Heo 2712401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2713401a8d04STejun Heo 2714401a8d04STejun Heo if (unlikely(worker)) { 2715401a8d04STejun Heo wait_for_completion(&barr.done); 2716401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2717401a8d04STejun Heo return true; 2718401a8d04STejun Heo } else 2719401a8d04STejun Heo return false; 2720401a8d04STejun Heo } 2721401a8d04STejun Heo 2722401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2723401a8d04STejun Heo { 2724401a8d04STejun Heo bool ret = false; 2725401a8d04STejun Heo int cpu; 2726401a8d04STejun Heo 2727401a8d04STejun Heo might_sleep(); 2728401a8d04STejun Heo 2729401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2730401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2731401a8d04STejun Heo 2732401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2733401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2734401a8d04STejun Heo return ret; 2735401a8d04STejun Heo } 2736401a8d04STejun Heo 273709383498STejun Heo /** 273809383498STejun Heo * flush_work_sync - wait until a work has finished execution 273909383498STejun Heo * @work: the work to flush 274009383498STejun Heo * 274109383498STejun Heo * Wait until @work has finished execution. On return, it's 274209383498STejun Heo * guaranteed that all queueing instances of @work which happened 274309383498STejun Heo * before this function is called are finished. In other words, if 274409383498STejun Heo * @work hasn't been requeued since this function was called, @work is 274509383498STejun Heo * guaranteed to be idle on return. 274609383498STejun Heo * 274709383498STejun Heo * RETURNS: 274809383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 274909383498STejun Heo * %false if it was already idle. 275009383498STejun Heo */ 275109383498STejun Heo bool flush_work_sync(struct work_struct *work) 275209383498STejun Heo { 275309383498STejun Heo struct wq_barrier barr; 275409383498STejun Heo bool pending, waited; 275509383498STejun Heo 275609383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 275709383498STejun Heo pending = start_flush_work(work, &barr, false); 275809383498STejun Heo 275909383498STejun Heo /* wait for executions to finish */ 276009383498STejun Heo waited = wait_on_work(work); 276109383498STejun Heo 276209383498STejun Heo /* wait for the pending one */ 276309383498STejun Heo if (pending) { 276409383498STejun Heo wait_for_completion(&barr.done); 276509383498STejun Heo destroy_work_on_stack(&barr.work); 276609383498STejun Heo } 276709383498STejun Heo 276809383498STejun Heo return pending || waited; 276909383498STejun Heo } 277009383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 277109383498STejun Heo 27726e84d644SOleg Nesterov /* 27731f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 27746e84d644SOleg Nesterov * so this work can't be re-armed in any way. 27756e84d644SOleg Nesterov */ 27766e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 27776e84d644SOleg Nesterov { 27788b03ae3cSTejun Heo struct global_cwq *gcwq; 27791f1f642eSOleg Nesterov int ret = -1; 27806e84d644SOleg Nesterov 278122df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 27821f1f642eSOleg Nesterov return 0; 27836e84d644SOleg Nesterov 27846e84d644SOleg Nesterov /* 27856e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 27866e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 27876e84d644SOleg Nesterov */ 27887a22ad75STejun Heo gcwq = get_work_gcwq(work); 27897a22ad75STejun Heo if (!gcwq) 27906e84d644SOleg Nesterov return ret; 27916e84d644SOleg Nesterov 27928b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 27936e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 27946e84d644SOleg Nesterov /* 27957a22ad75STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 27966e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 27976e84d644SOleg Nesterov * insert_work()->wmb(). 27986e84d644SOleg Nesterov */ 27996e84d644SOleg Nesterov smp_rmb(); 28007a22ad75STejun Heo if (gcwq == get_work_gcwq(work)) { 2801dc186ad7SThomas Gleixner debug_work_deactivate(work); 28026e84d644SOleg Nesterov list_del_init(&work->entry); 28037a22ad75STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 28048a2e8e5dSTejun Heo get_work_color(work), 28058a2e8e5dSTejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 28066e84d644SOleg Nesterov ret = 1; 28076e84d644SOleg Nesterov } 28086e84d644SOleg Nesterov } 28098b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 28106e84d644SOleg Nesterov 28116e84d644SOleg Nesterov return ret; 28126e84d644SOleg Nesterov } 28136e84d644SOleg Nesterov 2814401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work, 28151f1f642eSOleg Nesterov struct timer_list* timer) 28161f1f642eSOleg Nesterov { 28171f1f642eSOleg Nesterov int ret; 28181f1f642eSOleg Nesterov 28191f1f642eSOleg Nesterov do { 28201f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 28211f1f642eSOleg Nesterov if (!ret) 28221f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 28231f1f642eSOleg Nesterov wait_on_work(work); 28241f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28251f1f642eSOleg Nesterov 28267a22ad75STejun Heo clear_work_data(work); 28271f1f642eSOleg Nesterov return ret; 28281f1f642eSOleg Nesterov } 28291f1f642eSOleg Nesterov 28306e84d644SOleg Nesterov /** 2831401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2832401a8d04STejun Heo * @work: the work to cancel 28336e84d644SOleg Nesterov * 2834401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2835401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2836401a8d04STejun Heo * another workqueue. On return from this function, @work is 2837401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28381f1f642eSOleg Nesterov * 2839401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2840401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28416e84d644SOleg Nesterov * 2842401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28436e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2844401a8d04STejun Heo * 2845401a8d04STejun Heo * RETURNS: 2846401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28476e84d644SOleg Nesterov */ 2848401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28496e84d644SOleg Nesterov { 28501f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 2851b89deed3SOleg Nesterov } 285228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2853b89deed3SOleg Nesterov 28546e84d644SOleg Nesterov /** 2855401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2856401a8d04STejun Heo * @dwork: the delayed work to flush 28576e84d644SOleg Nesterov * 2858401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2859401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2860401a8d04STejun Heo * considers the last queueing instance of @dwork. 28611f1f642eSOleg Nesterov * 2862401a8d04STejun Heo * RETURNS: 2863401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2864401a8d04STejun Heo * %false if it was already idle. 28656e84d644SOleg Nesterov */ 2866401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2867401a8d04STejun Heo { 28688930cabaSTejun Heo local_irq_disable(); 2869401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 2870401a8d04STejun Heo __queue_work(raw_smp_processor_id(), 2871401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 28728930cabaSTejun Heo local_irq_enable(); 2873401a8d04STejun Heo return flush_work(&dwork->work); 2874401a8d04STejun Heo } 2875401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2876401a8d04STejun Heo 2877401a8d04STejun Heo /** 287809383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 287909383498STejun Heo * @dwork: the delayed work to flush 288009383498STejun Heo * 288109383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 288209383498STejun Heo * execution immediately. Other than timer handling, its behavior 288309383498STejun Heo * is identical to flush_work_sync(). 288409383498STejun Heo * 288509383498STejun Heo * RETURNS: 288609383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 288709383498STejun Heo * %false if it was already idle. 288809383498STejun Heo */ 288909383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 289009383498STejun Heo { 28918930cabaSTejun Heo local_irq_disable(); 289209383498STejun Heo if (del_timer_sync(&dwork->timer)) 289309383498STejun Heo __queue_work(raw_smp_processor_id(), 289409383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 28958930cabaSTejun Heo local_irq_enable(); 289609383498STejun Heo return flush_work_sync(&dwork->work); 289709383498STejun Heo } 289809383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 289909383498STejun Heo 290009383498STejun Heo /** 2901401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2902401a8d04STejun Heo * @dwork: the delayed work cancel 2903401a8d04STejun Heo * 2904401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2905401a8d04STejun Heo * 2906401a8d04STejun Heo * RETURNS: 2907401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2908401a8d04STejun Heo */ 2909401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29106e84d644SOleg Nesterov { 29111f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 29126e84d644SOleg Nesterov } 2913f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29141da177e4SLinus Torvalds 2915d4283e93STejun Heo /** 29160a13c00eSTejun Heo * schedule_work_on - put work task on a specific cpu 29170a13c00eSTejun Heo * @cpu: cpu to put the work task on 29180a13c00eSTejun Heo * @work: job to be done 29190a13c00eSTejun Heo * 29200a13c00eSTejun Heo * This puts a job on a specific cpu 29210a13c00eSTejun Heo */ 2922d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 29230a13c00eSTejun Heo { 29240a13c00eSTejun Heo return queue_work_on(cpu, system_wq, work); 29250a13c00eSTejun Heo } 29260a13c00eSTejun Heo EXPORT_SYMBOL(schedule_work_on); 29270a13c00eSTejun Heo 29280fcb78c2SRolf Eike Beer /** 29290fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 29300fcb78c2SRolf Eike Beer * @work: job to be done 29310fcb78c2SRolf Eike Beer * 2932d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2933d4283e93STejun Heo * %true otherwise. 29345b0f437dSBart Van Assche * 29355b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 29365b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 29375b0f437dSBart Van Assche * workqueue otherwise. 29380fcb78c2SRolf Eike Beer */ 2939d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29401da177e4SLinus Torvalds { 2941d320c038STejun Heo return queue_work(system_wq, work); 29421da177e4SLinus Torvalds } 2943ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 29441da177e4SLinus Torvalds 29450fcb78c2SRolf Eike Beer /** 29460fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29470fcb78c2SRolf Eike Beer * @cpu: cpu to use 294852bad64dSDavid Howells * @dwork: job to be done 29490fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29500fcb78c2SRolf Eike Beer * 29510fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29520fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29530fcb78c2SRolf Eike Beer */ 2954d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2955d4283e93STejun Heo unsigned long delay) 29561da177e4SLinus Torvalds { 2957d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29581da177e4SLinus Torvalds } 2959ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29601da177e4SLinus Torvalds 2961b6136773SAndrew Morton /** 29620a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29630a13c00eSTejun Heo * @dwork: job to be done 29640a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29650a13c00eSTejun Heo * 29660a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29670a13c00eSTejun Heo * workqueue. 29680a13c00eSTejun Heo */ 2969d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29700a13c00eSTejun Heo { 29710a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29720a13c00eSTejun Heo } 29730a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29740a13c00eSTejun Heo 29750a13c00eSTejun Heo /** 297631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2977b6136773SAndrew Morton * @func: the function to call 2978b6136773SAndrew Morton * 297931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 298031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2981b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 298231ddd871STejun Heo * 298331ddd871STejun Heo * RETURNS: 298431ddd871STejun Heo * 0 on success, -errno on failure. 2985b6136773SAndrew Morton */ 298665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 298715316ba8SChristoph Lameter { 298815316ba8SChristoph Lameter int cpu; 298938f51568SNamhyung Kim struct work_struct __percpu *works; 299015316ba8SChristoph Lameter 2991b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2992b6136773SAndrew Morton if (!works) 299315316ba8SChristoph Lameter return -ENOMEM; 2994b6136773SAndrew Morton 299595402b38SGautham R Shenoy get_online_cpus(); 299693981800STejun Heo 299715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29989bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29999bfb1839SIngo Molnar 30009bfb1839SIngo Molnar INIT_WORK(work, func); 30018de6d308SOleg Nesterov schedule_work_on(cpu, work); 300215316ba8SChristoph Lameter } 300393981800STejun Heo 300493981800STejun Heo for_each_online_cpu(cpu) 30058616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 300693981800STejun Heo 300795402b38SGautham R Shenoy put_online_cpus(); 3008b6136773SAndrew Morton free_percpu(works); 300915316ba8SChristoph Lameter return 0; 301015316ba8SChristoph Lameter } 301115316ba8SChristoph Lameter 3012eef6a7d5SAlan Stern /** 3013eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3014eef6a7d5SAlan Stern * 3015eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3016eef6a7d5SAlan Stern * completion. 3017eef6a7d5SAlan Stern * 3018eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3019eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3020eef6a7d5SAlan Stern * will lead to deadlock: 3021eef6a7d5SAlan Stern * 3022eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3023eef6a7d5SAlan Stern * a lock held by your code or its caller. 3024eef6a7d5SAlan Stern * 3025eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3026eef6a7d5SAlan Stern * 3027eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3028eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3029eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3030eef6a7d5SAlan Stern * 3031eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3032eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3033eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3034eef6a7d5SAlan Stern * cancel_work_sync() instead. 3035eef6a7d5SAlan Stern */ 30361da177e4SLinus Torvalds void flush_scheduled_work(void) 30371da177e4SLinus Torvalds { 3038d320c038STejun Heo flush_workqueue(system_wq); 30391da177e4SLinus Torvalds } 3040ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds /** 30431fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30441fa44ecaSJames Bottomley * @fn: the function to execute 30451fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30461fa44ecaSJames Bottomley * be available when the work executes) 30471fa44ecaSJames Bottomley * 30481fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30491fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30501fa44ecaSJames Bottomley * 30511fa44ecaSJames Bottomley * Returns: 0 - function was executed 30521fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30531fa44ecaSJames Bottomley */ 305465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30551fa44ecaSJames Bottomley { 30561fa44ecaSJames Bottomley if (!in_interrupt()) { 305765f27f38SDavid Howells fn(&ew->work); 30581fa44ecaSJames Bottomley return 0; 30591fa44ecaSJames Bottomley } 30601fa44ecaSJames Bottomley 306165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30621fa44ecaSJames Bottomley schedule_work(&ew->work); 30631fa44ecaSJames Bottomley 30641fa44ecaSJames Bottomley return 1; 30651fa44ecaSJames Bottomley } 30661fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30671fa44ecaSJames Bottomley 30681da177e4SLinus Torvalds int keventd_up(void) 30691da177e4SLinus Torvalds { 3070d320c038STejun Heo return system_wq != NULL; 30711da177e4SLinus Torvalds } 30721da177e4SLinus Torvalds 3073bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30741da177e4SLinus Torvalds { 30753af24433SOleg Nesterov /* 30760f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30770f900049STejun Heo * Make sure that the alignment isn't lower than that of 30780f900049STejun Heo * unsigned long long. 30793af24433SOleg Nesterov */ 30800f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 30810f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 30820f900049STejun Heo __alignof__(unsigned long long)); 30833af24433SOleg Nesterov 3084e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3085f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3086931ac77eSTejun Heo else { 30870f900049STejun Heo void *ptr; 3088e1d8aa9fSFrederic Weisbecker 30890f900049STejun Heo /* 3090f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3091f3421797STejun Heo * pointer at the end pointing back to the originally 3092f3421797STejun Heo * allocated pointer which will be used for free. 30930f900049STejun Heo */ 3094bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3095bdbc5dd7STejun Heo if (ptr) { 3096bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3097bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3098bdbc5dd7STejun Heo } 30993af24433SOleg Nesterov } 31003af24433SOleg Nesterov 31010415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3102bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3103bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 31040f900049STejun Heo } 31050f900049STejun Heo 3106bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 310706ba38a9SOleg Nesterov { 3108e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3109bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3110f3421797STejun Heo else if (wq->cpu_wq.single) { 3111f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3112f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 311306ba38a9SOleg Nesterov } 311406ba38a9SOleg Nesterov } 311506ba38a9SOleg Nesterov 3116f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3117f3421797STejun Heo const char *name) 3118b71ab8c2STejun Heo { 3119f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3120f3421797STejun Heo 3121f3421797STejun Heo if (max_active < 1 || max_active > lim) 3122b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3123b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3124f3421797STejun Heo max_active, name, 1, lim); 3125b71ab8c2STejun Heo 3126f3421797STejun Heo return clamp_val(max_active, 1, lim); 3127b71ab8c2STejun Heo } 3128b71ab8c2STejun Heo 3129b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 313097e37d7bSTejun Heo unsigned int flags, 31311e19ffc6STejun Heo int max_active, 3132eb13ba87SJohannes Berg struct lock_class_key *key, 3133b196be89STejun Heo const char *lock_name, ...) 31343af24433SOleg Nesterov { 3135b196be89STejun Heo va_list args, args1; 31363af24433SOleg Nesterov struct workqueue_struct *wq; 3137c34056a3STejun Heo unsigned int cpu; 3138b196be89STejun Heo size_t namelen; 3139b196be89STejun Heo 3140b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3141b196be89STejun Heo va_start(args, lock_name); 3142b196be89STejun Heo va_copy(args1, args); 3143b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3144b196be89STejun Heo 3145b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3146b196be89STejun Heo if (!wq) 3147b196be89STejun Heo goto err; 3148b196be89STejun Heo 3149b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3150b196be89STejun Heo va_end(args); 3151b196be89STejun Heo va_end(args1); 31523af24433SOleg Nesterov 3153f3421797STejun Heo /* 31546370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31556370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31566370a6adSTejun Heo */ 31576370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31586370a6adSTejun Heo flags |= WQ_RESCUER; 31596370a6adSTejun Heo 3160d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3161b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31623af24433SOleg Nesterov 3163b196be89STejun Heo /* init wq */ 316497e37d7bSTejun Heo wq->flags = flags; 3165a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 316673f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 316773f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 316873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 316973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31703af24433SOleg Nesterov 3171eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3172cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31733af24433SOleg Nesterov 3174bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3175bdbc5dd7STejun Heo goto err; 3176bdbc5dd7STejun Heo 3177f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 31781537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 31798b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 31803270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 31811537663fSTejun Heo 31820f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 31833270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3184c34056a3STejun Heo cwq->wq = wq; 318573f53c4aSTejun Heo cwq->flush_color = -1; 31861e19ffc6STejun Heo cwq->max_active = max_active; 31871e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3188e22bee78STejun Heo } 31891537663fSTejun Heo 3190e22bee78STejun Heo if (flags & WQ_RESCUER) { 3191e22bee78STejun Heo struct worker *rescuer; 3192e22bee78STejun Heo 3193f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3194e22bee78STejun Heo goto err; 3195e22bee78STejun Heo 3196e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3197e22bee78STejun Heo if (!rescuer) 3198e22bee78STejun Heo goto err; 3199e22bee78STejun Heo 3200b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3201b196be89STejun Heo wq->name); 3202e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3203e22bee78STejun Heo goto err; 3204e22bee78STejun Heo 3205e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3206e22bee78STejun Heo wake_up_process(rescuer->task); 32073af24433SOleg Nesterov } 32081537663fSTejun Heo 32093af24433SOleg Nesterov /* 3210a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3211a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3212a0a1a5fdSTejun Heo * workqueue to workqueues list. 32133af24433SOleg Nesterov */ 32143af24433SOleg Nesterov spin_lock(&workqueue_lock); 3215a0a1a5fdSTejun Heo 321658a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3217f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3218a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3219a0a1a5fdSTejun Heo 32203af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3221a0a1a5fdSTejun Heo 32223af24433SOleg Nesterov spin_unlock(&workqueue_lock); 32233af24433SOleg Nesterov 32243af24433SOleg Nesterov return wq; 32254690c4abSTejun Heo err: 32264690c4abSTejun Heo if (wq) { 3227bdbc5dd7STejun Heo free_cwqs(wq); 3228f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3229e22bee78STejun Heo kfree(wq->rescuer); 32304690c4abSTejun Heo kfree(wq); 32313af24433SOleg Nesterov } 32324690c4abSTejun Heo return NULL; 32331da177e4SLinus Torvalds } 3234d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32351da177e4SLinus Torvalds 32363af24433SOleg Nesterov /** 32373af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32383af24433SOleg Nesterov * @wq: target workqueue 32393af24433SOleg Nesterov * 32403af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32413af24433SOleg Nesterov */ 32423af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32433af24433SOleg Nesterov { 3244c8e55f36STejun Heo unsigned int cpu; 32453af24433SOleg Nesterov 32469c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32479c5a2ba7STejun Heo drain_workqueue(wq); 3248c8efcc25STejun Heo 3249a0a1a5fdSTejun Heo /* 3250a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3251a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3252a0a1a5fdSTejun Heo */ 325395402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32543af24433SOleg Nesterov list_del(&wq->list); 325595402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32563af24433SOleg Nesterov 3257e22bee78STejun Heo /* sanity check */ 3258f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 325973f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 326073f53c4aSTejun Heo int i; 32613af24433SOleg Nesterov 326273f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 326373f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32641e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32651e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 326673f53c4aSTejun Heo } 32671537663fSTejun Heo 3268e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3269e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3270f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32718d9df9f0SXiaotian Feng kfree(wq->rescuer); 3272e22bee78STejun Heo } 3273e22bee78STejun Heo 3274bdbc5dd7STejun Heo free_cwqs(wq); 32753af24433SOleg Nesterov kfree(wq); 32763af24433SOleg Nesterov } 32773af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32783af24433SOleg Nesterov 3279dcd989cbSTejun Heo /** 3280dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3281dcd989cbSTejun Heo * @wq: target workqueue 3282dcd989cbSTejun Heo * @max_active: new max_active value. 3283dcd989cbSTejun Heo * 3284dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3285dcd989cbSTejun Heo * 3286dcd989cbSTejun Heo * CONTEXT: 3287dcd989cbSTejun Heo * Don't call from IRQ context. 3288dcd989cbSTejun Heo */ 3289dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3290dcd989cbSTejun Heo { 3291dcd989cbSTejun Heo unsigned int cpu; 3292dcd989cbSTejun Heo 3293f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3294dcd989cbSTejun Heo 3295dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3296dcd989cbSTejun Heo 3297dcd989cbSTejun Heo wq->saved_max_active = max_active; 3298dcd989cbSTejun Heo 3299f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3300dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3301dcd989cbSTejun Heo 3302dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3303dcd989cbSTejun Heo 330458a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3305dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3306dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3307dcd989cbSTejun Heo 3308dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3309dcd989cbSTejun Heo } 3310dcd989cbSTejun Heo 3311dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3312dcd989cbSTejun Heo } 3313dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3314dcd989cbSTejun Heo 3315dcd989cbSTejun Heo /** 3316dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3317dcd989cbSTejun Heo * @cpu: CPU in question 3318dcd989cbSTejun Heo * @wq: target workqueue 3319dcd989cbSTejun Heo * 3320dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3321dcd989cbSTejun Heo * no synchronization around this function and the test result is 3322dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3323dcd989cbSTejun Heo * 3324dcd989cbSTejun Heo * RETURNS: 3325dcd989cbSTejun Heo * %true if congested, %false otherwise. 3326dcd989cbSTejun Heo */ 3327dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3328dcd989cbSTejun Heo { 3329dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3330dcd989cbSTejun Heo 3331dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3332dcd989cbSTejun Heo } 3333dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3334dcd989cbSTejun Heo 3335dcd989cbSTejun Heo /** 3336dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3337dcd989cbSTejun Heo * @work: the work of interest 3338dcd989cbSTejun Heo * 3339dcd989cbSTejun Heo * RETURNS: 3340bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3341dcd989cbSTejun Heo */ 3342dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3343dcd989cbSTejun Heo { 3344dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3345dcd989cbSTejun Heo 3346bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3347dcd989cbSTejun Heo } 3348dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3349dcd989cbSTejun Heo 3350dcd989cbSTejun Heo /** 3351dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3352dcd989cbSTejun Heo * @work: the work to be tested 3353dcd989cbSTejun Heo * 3354dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3355dcd989cbSTejun Heo * synchronization around this function and the test result is 3356dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3357dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3358dcd989cbSTejun Heo * running state. 3359dcd989cbSTejun Heo * 3360dcd989cbSTejun Heo * RETURNS: 3361dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3362dcd989cbSTejun Heo */ 3363dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3364dcd989cbSTejun Heo { 3365dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3366dcd989cbSTejun Heo unsigned long flags; 3367dcd989cbSTejun Heo unsigned int ret = 0; 3368dcd989cbSTejun Heo 3369dcd989cbSTejun Heo if (!gcwq) 3370dcd989cbSTejun Heo return false; 3371dcd989cbSTejun Heo 3372dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3373dcd989cbSTejun Heo 3374dcd989cbSTejun Heo if (work_pending(work)) 3375dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3376dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3377dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3378dcd989cbSTejun Heo 3379dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3380dcd989cbSTejun Heo 3381dcd989cbSTejun Heo return ret; 3382dcd989cbSTejun Heo } 3383dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3384dcd989cbSTejun Heo 3385db7bccf4STejun Heo /* 3386db7bccf4STejun Heo * CPU hotplug. 3387db7bccf4STejun Heo * 3388e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3389e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3390e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3391e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3392e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3393e22bee78STejun Heo * blocked draining impractical. 3394e22bee78STejun Heo * 3395628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3396628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3397628c78e7STejun Heo * cpu comes back online. 3398db7bccf4STejun Heo */ 3399db7bccf4STejun Heo 340060373152STejun Heo /* claim manager positions of all pools */ 34018db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 340260373152STejun Heo { 340360373152STejun Heo struct worker_pool *pool; 340460373152STejun Heo 340560373152STejun Heo for_each_worker_pool(pool, gcwq) 340660373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 34078db25e78STejun Heo spin_lock_irq(&gcwq->lock); 340860373152STejun Heo } 340960373152STejun Heo 341060373152STejun Heo /* release manager positions */ 34118db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 341260373152STejun Heo { 341360373152STejun Heo struct worker_pool *pool; 341460373152STejun Heo 34158db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 341660373152STejun Heo for_each_worker_pool(pool, gcwq) 341760373152STejun Heo mutex_unlock(&pool->manager_mutex); 341860373152STejun Heo } 341960373152STejun Heo 3420628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3421db7bccf4STejun Heo { 3422628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 34234ce62e9eSTejun Heo struct worker_pool *pool; 3424db7bccf4STejun Heo struct worker *worker; 3425db7bccf4STejun Heo struct hlist_node *pos; 3426db7bccf4STejun Heo int i; 3427db7bccf4STejun Heo 3428db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3429db7bccf4STejun Heo 34308db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3431e22bee78STejun Heo 3432f2d5a0eeSTejun Heo /* 3433f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3434f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3435f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3436f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3437f2d5a0eeSTejun Heo */ 343860373152STejun Heo for_each_worker_pool(pool, gcwq) 34394ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3440403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3441db7bccf4STejun Heo 3442db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3443403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3444db7bccf4STejun Heo 3445f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3446f2d5a0eeSTejun Heo 34478db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3448e22bee78STejun Heo 3449e22bee78STejun Heo /* 3450628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3451628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3452628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3453628c78e7STejun Heo */ 3454628c78e7STejun Heo schedule(); 3455628c78e7STejun Heo 3456628c78e7STejun Heo /* 3457628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3458628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3459628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3460628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3461628c78e7STejun Heo * which is served by workers tied to the CPU. 3462628c78e7STejun Heo * 3463628c78e7STejun Heo * On return from this function, the current worker would trigger 3464628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3465628c78e7STejun Heo * didn't already. 3466e22bee78STejun Heo */ 34674ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34684ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3469db7bccf4STejun Heo } 3470db7bccf4STejun Heo 34718db25e78STejun Heo /* 34728db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34738db25e78STejun Heo * This will be registered high priority CPU notifier. 34748db25e78STejun Heo */ 34758db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34761da177e4SLinus Torvalds unsigned long action, 34771da177e4SLinus Torvalds void *hcpu) 34781da177e4SLinus Torvalds { 34793af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3480db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 34814ce62e9eSTejun Heo struct worker_pool *pool; 34821da177e4SLinus Torvalds 34838db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34843af24433SOleg Nesterov case CPU_UP_PREPARE: 34854ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 34863ce63377STejun Heo struct worker *worker; 34873ce63377STejun Heo 34883ce63377STejun Heo if (pool->nr_workers) 34893ce63377STejun Heo continue; 34903ce63377STejun Heo 34913ce63377STejun Heo worker = create_worker(pool); 34923ce63377STejun Heo if (!worker) 34933ce63377STejun Heo return NOTIFY_BAD; 34943ce63377STejun Heo 34953ce63377STejun Heo spin_lock_irq(&gcwq->lock); 34963ce63377STejun Heo start_worker(worker); 34973ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 34983af24433SOleg Nesterov } 34991da177e4SLinus Torvalds break; 35001da177e4SLinus Torvalds 350165758202STejun Heo case CPU_DOWN_FAILED: 350265758202STejun Heo case CPU_ONLINE: 35038db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 35048db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 35058db25e78STejun Heo rebind_workers(gcwq); 35068db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 35078db25e78STejun Heo break; 350865758202STejun Heo } 350965758202STejun Heo return NOTIFY_OK; 351065758202STejun Heo } 351165758202STejun Heo 351265758202STejun Heo /* 351365758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 351465758202STejun Heo * This will be registered as low priority CPU notifier. 351565758202STejun Heo */ 351665758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 351765758202STejun Heo unsigned long action, 351865758202STejun Heo void *hcpu) 351965758202STejun Heo { 35208db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35218db25e78STejun Heo struct work_struct unbind_work; 35228db25e78STejun Heo 352365758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 352465758202STejun Heo case CPU_DOWN_PREPARE: 35258db25e78STejun Heo /* unbinding should happen on the local CPU */ 35268db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 35278db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 35288db25e78STejun Heo flush_work(&unbind_work); 35298db25e78STejun Heo break; 353065758202STejun Heo } 353165758202STejun Heo return NOTIFY_OK; 353265758202STejun Heo } 353365758202STejun Heo 35342d3854a3SRusty Russell #ifdef CONFIG_SMP 35358ccad40dSRusty Russell 35362d3854a3SRusty Russell struct work_for_cpu { 35376b44003eSAndrew Morton struct completion completion; 35382d3854a3SRusty Russell long (*fn)(void *); 35392d3854a3SRusty Russell void *arg; 35402d3854a3SRusty Russell long ret; 35412d3854a3SRusty Russell }; 35422d3854a3SRusty Russell 35436b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35442d3854a3SRusty Russell { 35456b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35462d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35476b44003eSAndrew Morton complete(&wfc->completion); 35486b44003eSAndrew Morton return 0; 35492d3854a3SRusty Russell } 35502d3854a3SRusty Russell 35512d3854a3SRusty Russell /** 35522d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35532d3854a3SRusty Russell * @cpu: the cpu to run on 35542d3854a3SRusty Russell * @fn: the function to run 35552d3854a3SRusty Russell * @arg: the function arg 35562d3854a3SRusty Russell * 355731ad9081SRusty Russell * This will return the value @fn returns. 355831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35596b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35602d3854a3SRusty Russell */ 35612d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35622d3854a3SRusty Russell { 35636b44003eSAndrew Morton struct task_struct *sub_thread; 35646b44003eSAndrew Morton struct work_for_cpu wfc = { 35656b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35666b44003eSAndrew Morton .fn = fn, 35676b44003eSAndrew Morton .arg = arg, 35686b44003eSAndrew Morton }; 35692d3854a3SRusty Russell 35706b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35716b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35726b44003eSAndrew Morton return PTR_ERR(sub_thread); 35736b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35746b44003eSAndrew Morton wake_up_process(sub_thread); 35756b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35762d3854a3SRusty Russell return wfc.ret; 35772d3854a3SRusty Russell } 35782d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35792d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35802d3854a3SRusty Russell 3581a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3582e7577c50SRusty Russell 3583a0a1a5fdSTejun Heo /** 3584a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3585a0a1a5fdSTejun Heo * 358658a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 358758a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 358858a69cb4STejun Heo * gcwq->worklist. 3589a0a1a5fdSTejun Heo * 3590a0a1a5fdSTejun Heo * CONTEXT: 35918b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3592a0a1a5fdSTejun Heo */ 3593a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3594a0a1a5fdSTejun Heo { 3595a0a1a5fdSTejun Heo unsigned int cpu; 3596a0a1a5fdSTejun Heo 3597a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3598a0a1a5fdSTejun Heo 3599a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3600a0a1a5fdSTejun Heo workqueue_freezing = true; 3601a0a1a5fdSTejun Heo 3602f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36038b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3604bdbc5dd7STejun Heo struct workqueue_struct *wq; 36058b03ae3cSTejun Heo 36068b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36078b03ae3cSTejun Heo 3608db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3609db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3610db7bccf4STejun Heo 3611a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3612a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3613a0a1a5fdSTejun Heo 361458a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3615a0a1a5fdSTejun Heo cwq->max_active = 0; 36161da177e4SLinus Torvalds } 36178b03ae3cSTejun Heo 36188b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3619a0a1a5fdSTejun Heo } 3620a0a1a5fdSTejun Heo 3621a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3622a0a1a5fdSTejun Heo } 3623a0a1a5fdSTejun Heo 3624a0a1a5fdSTejun Heo /** 362558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3626a0a1a5fdSTejun Heo * 3627a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3628a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3629a0a1a5fdSTejun Heo * 3630a0a1a5fdSTejun Heo * CONTEXT: 3631a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3632a0a1a5fdSTejun Heo * 3633a0a1a5fdSTejun Heo * RETURNS: 363458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 363558a69cb4STejun Heo * is complete. 3636a0a1a5fdSTejun Heo */ 3637a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3638a0a1a5fdSTejun Heo { 3639a0a1a5fdSTejun Heo unsigned int cpu; 3640a0a1a5fdSTejun Heo bool busy = false; 3641a0a1a5fdSTejun Heo 3642a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3643a0a1a5fdSTejun Heo 3644a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3645a0a1a5fdSTejun Heo 3646f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3647bdbc5dd7STejun Heo struct workqueue_struct *wq; 3648a0a1a5fdSTejun Heo /* 3649a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3650a0a1a5fdSTejun Heo * to peek without lock. 3651a0a1a5fdSTejun Heo */ 3652a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3653a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3654a0a1a5fdSTejun Heo 365558a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3656a0a1a5fdSTejun Heo continue; 3657a0a1a5fdSTejun Heo 3658a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3659a0a1a5fdSTejun Heo if (cwq->nr_active) { 3660a0a1a5fdSTejun Heo busy = true; 3661a0a1a5fdSTejun Heo goto out_unlock; 3662a0a1a5fdSTejun Heo } 3663a0a1a5fdSTejun Heo } 3664a0a1a5fdSTejun Heo } 3665a0a1a5fdSTejun Heo out_unlock: 3666a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3667a0a1a5fdSTejun Heo return busy; 3668a0a1a5fdSTejun Heo } 3669a0a1a5fdSTejun Heo 3670a0a1a5fdSTejun Heo /** 3671a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3672a0a1a5fdSTejun Heo * 3673a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36747e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3675a0a1a5fdSTejun Heo * 3676a0a1a5fdSTejun Heo * CONTEXT: 36778b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3678a0a1a5fdSTejun Heo */ 3679a0a1a5fdSTejun Heo void thaw_workqueues(void) 3680a0a1a5fdSTejun Heo { 3681a0a1a5fdSTejun Heo unsigned int cpu; 3682a0a1a5fdSTejun Heo 3683a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3684a0a1a5fdSTejun Heo 3685a0a1a5fdSTejun Heo if (!workqueue_freezing) 3686a0a1a5fdSTejun Heo goto out_unlock; 3687a0a1a5fdSTejun Heo 3688f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36898b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 36904ce62e9eSTejun Heo struct worker_pool *pool; 3691bdbc5dd7STejun Heo struct workqueue_struct *wq; 36928b03ae3cSTejun Heo 36938b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36948b03ae3cSTejun Heo 3695db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3696db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3697db7bccf4STejun Heo 3698a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3699a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3700a0a1a5fdSTejun Heo 370158a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3702a0a1a5fdSTejun Heo continue; 3703a0a1a5fdSTejun Heo 3704a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3705a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3706a0a1a5fdSTejun Heo 3707a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3708a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3709a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3710a0a1a5fdSTejun Heo } 37118b03ae3cSTejun Heo 37124ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37134ce62e9eSTejun Heo wake_up_worker(pool); 3714e22bee78STejun Heo 37158b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3716a0a1a5fdSTejun Heo } 3717a0a1a5fdSTejun Heo 3718a0a1a5fdSTejun Heo workqueue_freezing = false; 3719a0a1a5fdSTejun Heo out_unlock: 3720a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3721a0a1a5fdSTejun Heo } 3722a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3723a0a1a5fdSTejun Heo 37246ee0578bSSuresh Siddha static int __init init_workqueues(void) 37251da177e4SLinus Torvalds { 3726c34056a3STejun Heo unsigned int cpu; 3727c8e55f36STejun Heo int i; 3728c34056a3STejun Heo 372965758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 373065758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37318b03ae3cSTejun Heo 37328b03ae3cSTejun Heo /* initialize gcwqs */ 3733f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37348b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37354ce62e9eSTejun Heo struct worker_pool *pool; 37368b03ae3cSTejun Heo 37378b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37388b03ae3cSTejun Heo gcwq->cpu = cpu; 3739f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37408b03ae3cSTejun Heo 3741c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3742c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3743c8e55f36STejun Heo 37444ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37454ce62e9eSTejun Heo pool->gcwq = gcwq; 37464ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37474ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3748e22bee78STejun Heo 37494ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37504ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37514ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3752e22bee78STejun Heo 37534ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37544ce62e9eSTejun Heo (unsigned long)pool); 37554ce62e9eSTejun Heo 375660373152STejun Heo mutex_init(&pool->manager_mutex); 37574ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37584ce62e9eSTejun Heo } 3759db7bccf4STejun Heo 376025511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37618b03ae3cSTejun Heo } 37628b03ae3cSTejun Heo 3763e22bee78STejun Heo /* create the initial worker */ 3764f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3765e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37664ce62e9eSTejun Heo struct worker_pool *pool; 3767e22bee78STejun Heo 3768477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3769477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37704ce62e9eSTejun Heo 37714ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37724ce62e9eSTejun Heo struct worker *worker; 37734ce62e9eSTejun Heo 3774bc2ae0f5STejun Heo worker = create_worker(pool); 3775e22bee78STejun Heo BUG_ON(!worker); 3776e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3777e22bee78STejun Heo start_worker(worker); 3778e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3779e22bee78STejun Heo } 37804ce62e9eSTejun Heo } 3781e22bee78STejun Heo 3782d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3783d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3784d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3785f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3786f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 378724d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 378824d51addSTejun Heo WQ_FREEZABLE, 0); 378962d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 379062d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3791e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 379262d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 379362d3c543SAlan Stern !system_nrt_freezable_wq); 37946ee0578bSSuresh Siddha return 0; 37951da177e4SLinus Torvalds } 37966ee0578bSSuresh Siddha early_initcall(init_workqueues); 3797