11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19c54fce6eSTejun Heo * automatically managed. There is one worker pool for each CPU and 20c54fce6eSTejun Heo * one extra for works which are better served by workers which are 21c54fce6eSTejun Heo * not bound to any specific CPU. 22c54fce6eSTejun Heo * 23c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 269984de1aSPaul Gortmaker #include <linux/export.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds #include <linux/signal.h> 311da177e4SLinus Torvalds #include <linux/completion.h> 321da177e4SLinus Torvalds #include <linux/workqueue.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/cpu.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/kthread.h> 371fa44ecaSJames Bottomley #include <linux/hardirq.h> 3846934023SChristoph Lameter #include <linux/mempolicy.h> 39341a5958SRafael J. Wysocki #include <linux/freezer.h> 40d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 44e22bee78STejun Heo 45e22bee78STejun Heo #include "workqueue_sched.h" 461da177e4SLinus Torvalds 47c8e55f36STejun Heo enum { 48bc2ae0f5STejun Heo /* 49bc2ae0f5STejun Heo * global_cwq flags 50bc2ae0f5STejun Heo * 51bc2ae0f5STejun Heo * A bound gcwq is either associated or disassociated with its CPU. 52bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 53bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 54bc2ae0f5STejun Heo * is in effect. 55bc2ae0f5STejun Heo * 56bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 57bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 58bc2ae0f5STejun Heo * be executing on any CPU. The gcwq behaves as an unbound one. 59bc2ae0f5STejun Heo * 60bc2ae0f5STejun Heo * Note that DISASSOCIATED can be flipped only while holding 61bc2ae0f5STejun Heo * managership of all pools on the gcwq to avoid changing binding 62bc2ae0f5STejun Heo * state while create_worker() is in progress. 63bc2ae0f5STejun Heo */ 6411ebea50STejun Heo GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */ 6511ebea50STejun Heo GCWQ_FREEZING = 1 << 1, /* freeze in progress */ 6611ebea50STejun Heo 6711ebea50STejun Heo /* pool flags */ 6811ebea50STejun Heo POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ 69db7bccf4STejun Heo 70c8e55f36STejun Heo /* worker flags */ 71c8e55f36STejun Heo WORKER_STARTED = 1 << 0, /* started */ 72c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 73c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 74e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 75e22bee78STejun Heo WORKER_REBIND = 1 << 5, /* mom is home, come back */ 76fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 77f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 78e22bee78STejun Heo 79403c821dSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND | 80403c821dSTejun Heo WORKER_CPU_INTENSIVE, 81db7bccf4STejun Heo 823270476aSTejun Heo NR_WORKER_POOLS = 2, /* # worker pools per gcwq */ 834ce62e9eSTejun Heo 84c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 85c8e55f36STejun Heo BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER, 86c8e55f36STejun Heo BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1, 87db7bccf4STejun Heo 88e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 89e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 90e22bee78STejun Heo 913233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 923233cdbdSTejun Heo /* call for help after 10ms 933233cdbdSTejun Heo (min two ticks) */ 94e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 95e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 98e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 99e22bee78STejun Heo * all cpus. Give -20. 100e22bee78STejun Heo */ 101e22bee78STejun Heo RESCUER_NICE_LEVEL = -20, 1023270476aSTejun Heo HIGHPRI_NICE_LEVEL = -20, 103c8e55f36STejun Heo }; 104c8e55f36STejun Heo 1051da177e4SLinus Torvalds /* 1064690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1074690c4abSTejun Heo * 108e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 109e41e704bSTejun Heo * everyone else. 1104690c4abSTejun Heo * 111e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 112e22bee78STejun Heo * only be modified and accessed from the local cpu. 113e22bee78STejun Heo * 1148b03ae3cSTejun Heo * L: gcwq->lock protected. Access with gcwq->lock held. 1154690c4abSTejun Heo * 116e22bee78STejun Heo * X: During normal operation, modification requires gcwq->lock and 117e22bee78STejun Heo * should be done only from local cpu. Either disabling preemption 118e22bee78STejun Heo * on local cpu or grabbing gcwq->lock is enough for read access. 119f3421797STejun Heo * If GCWQ_DISASSOCIATED is set, it's identical to L. 120e22bee78STejun Heo * 12173f53c4aSTejun Heo * F: wq->flush_mutex protected. 12273f53c4aSTejun Heo * 1234690c4abSTejun Heo * W: workqueue_lock protected. 1244690c4abSTejun Heo */ 1254690c4abSTejun Heo 1268b03ae3cSTejun Heo struct global_cwq; 127bd7bdd43STejun Heo struct worker_pool; 12825511a47STejun Heo struct idle_rebind; 129c34056a3STejun Heo 130e22bee78STejun Heo /* 131e22bee78STejun Heo * The poor guys doing the actual heavy lifting. All on-duty workers 132e22bee78STejun Heo * are either serving the manager role, on idle list or on busy hash. 133e22bee78STejun Heo */ 134c34056a3STejun Heo struct worker { 135c8e55f36STejun Heo /* on idle list while idle, on busy hash table while busy */ 136c8e55f36STejun Heo union { 137c8e55f36STejun Heo struct list_head entry; /* L: while idle */ 138c8e55f36STejun Heo struct hlist_node hentry; /* L: while busy */ 139c8e55f36STejun Heo }; 140c8e55f36STejun Heo 141c34056a3STejun Heo struct work_struct *current_work; /* L: work being processed */ 1428cca0eeaSTejun Heo struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */ 143affee4b2STejun Heo struct list_head scheduled; /* L: scheduled works */ 144c34056a3STejun Heo struct task_struct *task; /* I: worker task */ 145bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 146e22bee78STejun Heo /* 64 bytes boundary on 64bit, 32 on 32bit */ 147e22bee78STejun Heo unsigned long last_active; /* L: last active timestamp */ 148e22bee78STejun Heo unsigned int flags; /* X: flags */ 149c34056a3STejun Heo int id; /* I: worker id */ 15025511a47STejun Heo 15125511a47STejun Heo /* for rebinding worker to CPU */ 15225511a47STejun Heo struct idle_rebind *idle_rebind; /* L: for idle worker */ 15325511a47STejun Heo struct work_struct rebind_work; /* L: for busy worker */ 154c34056a3STejun Heo }; 155c34056a3STejun Heo 156bd7bdd43STejun Heo struct worker_pool { 157bd7bdd43STejun Heo struct global_cwq *gcwq; /* I: the owning gcwq */ 15811ebea50STejun Heo unsigned int flags; /* X: flags */ 159bd7bdd43STejun Heo 160bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 161bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 162bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 163bd7bdd43STejun Heo 164bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 165bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 166bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 167bd7bdd43STejun Heo 16860373152STejun Heo struct mutex manager_mutex; /* mutex manager should hold */ 169bd7bdd43STejun Heo struct ida worker_ida; /* L: for worker IDs */ 170bd7bdd43STejun Heo }; 171bd7bdd43STejun Heo 1724690c4abSTejun Heo /* 173e22bee78STejun Heo * Global per-cpu workqueue. There's one and only one for each cpu 174e22bee78STejun Heo * and all works are queued and processed here regardless of their 175e22bee78STejun Heo * target workqueues. 1768b03ae3cSTejun Heo */ 1778b03ae3cSTejun Heo struct global_cwq { 1788b03ae3cSTejun Heo spinlock_t lock; /* the gcwq lock */ 1798b03ae3cSTejun Heo unsigned int cpu; /* I: the associated cpu */ 180db7bccf4STejun Heo unsigned int flags; /* L: GCWQ_* flags */ 181c8e55f36STejun Heo 182bd7bdd43STejun Heo /* workers are chained either in busy_hash or pool idle_list */ 183c8e55f36STejun Heo struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE]; 184c8e55f36STejun Heo /* L: hash of busy workers */ 185c8e55f36STejun Heo 1863270476aSTejun Heo struct worker_pool pools[2]; /* normal and highpri pools */ 187db7bccf4STejun Heo 18825511a47STejun Heo wait_queue_head_t rebind_hold; /* rebind hold wait */ 1898b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1908b03ae3cSTejun Heo 1918b03ae3cSTejun Heo /* 192502ca9d8STejun Heo * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of 1930f900049STejun Heo * work_struct->data are used for flags and thus cwqs need to be 1940f900049STejun Heo * aligned at two's power of the number of flag bits. 1951da177e4SLinus Torvalds */ 1961da177e4SLinus Torvalds struct cpu_workqueue_struct { 197bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1984690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 19973f53c4aSTejun Heo int work_color; /* L: current color */ 20073f53c4aSTejun Heo int flush_color; /* L: flushing color */ 20173f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20273f53c4aSTejun Heo /* L: nr of in_flight works */ 2031e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 204a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2051e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2060f900049STejun Heo }; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds /* 20973f53c4aSTejun Heo * Structure used to wait for workqueue flush. 21073f53c4aSTejun Heo */ 21173f53c4aSTejun Heo struct wq_flusher { 21273f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 21373f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 21473f53c4aSTejun Heo struct completion done; /* flush completion */ 21573f53c4aSTejun Heo }; 2161da177e4SLinus Torvalds 21773f53c4aSTejun Heo /* 218f2e005aaSTejun Heo * All cpumasks are assumed to be always set on UP and thus can't be 219f2e005aaSTejun Heo * used to determine whether there's something to be done. 220f2e005aaSTejun Heo */ 221f2e005aaSTejun Heo #ifdef CONFIG_SMP 222f2e005aaSTejun Heo typedef cpumask_var_t mayday_mask_t; 223f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) \ 224f2e005aaSTejun Heo cpumask_test_and_set_cpu((cpu), (mask)) 225f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) 226f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) 2279c37547aSTejun Heo #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp)) 228f2e005aaSTejun Heo #define free_mayday_mask(mask) free_cpumask_var((mask)) 229f2e005aaSTejun Heo #else 230f2e005aaSTejun Heo typedef unsigned long mayday_mask_t; 231f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask)) 232f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask)) 233f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask)) 234f2e005aaSTejun Heo #define alloc_mayday_mask(maskp, gfp) true 235f2e005aaSTejun Heo #define free_mayday_mask(mask) do { } while (0) 236f2e005aaSTejun Heo #endif 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds /* 2391da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 2401da177e4SLinus Torvalds * per-CPU workqueues: 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds struct workqueue_struct { 2439c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 244bdbc5dd7STejun Heo union { 245bdbc5dd7STejun Heo struct cpu_workqueue_struct __percpu *pcpu; 246bdbc5dd7STejun Heo struct cpu_workqueue_struct *single; 247bdbc5dd7STejun Heo unsigned long v; 248bdbc5dd7STejun Heo } cpu_wq; /* I: cwq's */ 2494690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 25073f53c4aSTejun Heo 25173f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 25273f53c4aSTejun Heo int work_color; /* F: current work color */ 25373f53c4aSTejun Heo int flush_color; /* F: current flush color */ 25473f53c4aSTejun Heo atomic_t nr_cwqs_to_flush; /* flush in progress */ 25573f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 25673f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 25773f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 25873f53c4aSTejun Heo 259f2e005aaSTejun Heo mayday_mask_t mayday_mask; /* cpus requesting rescue */ 260e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 261e22bee78STejun Heo 2629c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 263dcd989cbSTejun Heo int saved_max_active; /* W: saved cwq max_active */ 2644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2654e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2664e6045f1SJohannes Berg #endif 267b196be89STejun Heo char name[]; /* I: workqueue name */ 2681da177e4SLinus Torvalds }; 2691da177e4SLinus Torvalds 270d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 271d320c038STejun Heo struct workqueue_struct *system_long_wq __read_mostly; 272d320c038STejun Heo struct workqueue_struct *system_nrt_wq __read_mostly; 273f3421797STejun Heo struct workqueue_struct *system_unbound_wq __read_mostly; 27424d51addSTejun Heo struct workqueue_struct *system_freezable_wq __read_mostly; 27562d3c543SAlan Stern struct workqueue_struct *system_nrt_freezable_wq __read_mostly; 276d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 277d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 278d320c038STejun Heo EXPORT_SYMBOL_GPL(system_nrt_wq); 279f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 28024d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 28162d3c543SAlan Stern EXPORT_SYMBOL_GPL(system_nrt_freezable_wq); 282d320c038STejun Heo 28397bd2347STejun Heo #define CREATE_TRACE_POINTS 28497bd2347STejun Heo #include <trace/events/workqueue.h> 28597bd2347STejun Heo 2864ce62e9eSTejun Heo #define for_each_worker_pool(pool, gcwq) \ 2873270476aSTejun Heo for ((pool) = &(gcwq)->pools[0]; \ 2883270476aSTejun Heo (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++) 2894ce62e9eSTejun Heo 290db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq) \ 291db7bccf4STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ 292db7bccf4STejun Heo hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) 293db7bccf4STejun Heo 294f3421797STejun Heo static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask, 295f3421797STejun Heo unsigned int sw) 296f3421797STejun Heo { 297f3421797STejun Heo if (cpu < nr_cpu_ids) { 298f3421797STejun Heo if (sw & 1) { 299f3421797STejun Heo cpu = cpumask_next(cpu, mask); 300f3421797STejun Heo if (cpu < nr_cpu_ids) 301f3421797STejun Heo return cpu; 302f3421797STejun Heo } 303f3421797STejun Heo if (sw & 2) 304f3421797STejun Heo return WORK_CPU_UNBOUND; 305f3421797STejun Heo } 306f3421797STejun Heo return WORK_CPU_NONE; 307f3421797STejun Heo } 308f3421797STejun Heo 309f3421797STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 310f3421797STejun Heo struct workqueue_struct *wq) 311f3421797STejun Heo { 312f3421797STejun Heo return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2); 313f3421797STejun Heo } 314f3421797STejun Heo 31509884951STejun Heo /* 31609884951STejun Heo * CPU iterators 31709884951STejun Heo * 31809884951STejun Heo * An extra gcwq is defined for an invalid cpu number 31909884951STejun Heo * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 32009884951STejun Heo * specific CPU. The following iterators are similar to 32109884951STejun Heo * for_each_*_cpu() iterators but also considers the unbound gcwq. 32209884951STejun Heo * 32309884951STejun Heo * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND 32409884951STejun Heo * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND 32509884951STejun Heo * for_each_cwq_cpu() : possible CPUs for bound workqueues, 32609884951STejun Heo * WORK_CPU_UNBOUND for unbound workqueues 32709884951STejun Heo */ 328f3421797STejun Heo #define for_each_gcwq_cpu(cpu) \ 329f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \ 330f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 331f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3)) 332f3421797STejun Heo 333f3421797STejun Heo #define for_each_online_gcwq_cpu(cpu) \ 334f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \ 335f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 336f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3)) 337f3421797STejun Heo 338f3421797STejun Heo #define for_each_cwq_cpu(cpu, wq) \ 339f3421797STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \ 340f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 341f3421797STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) 342f3421797STejun Heo 343dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 344dc186ad7SThomas Gleixner 345dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 346dc186ad7SThomas Gleixner 34799777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 34899777288SStanislaw Gruszka { 34999777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 35099777288SStanislaw Gruszka } 35199777288SStanislaw Gruszka 352dc186ad7SThomas Gleixner /* 353dc186ad7SThomas Gleixner * fixup_init is called when: 354dc186ad7SThomas Gleixner * - an active object is initialized 355dc186ad7SThomas Gleixner */ 356dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 357dc186ad7SThomas Gleixner { 358dc186ad7SThomas Gleixner struct work_struct *work = addr; 359dc186ad7SThomas Gleixner 360dc186ad7SThomas Gleixner switch (state) { 361dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 362dc186ad7SThomas Gleixner cancel_work_sync(work); 363dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 364dc186ad7SThomas Gleixner return 1; 365dc186ad7SThomas Gleixner default: 366dc186ad7SThomas Gleixner return 0; 367dc186ad7SThomas Gleixner } 368dc186ad7SThomas Gleixner } 369dc186ad7SThomas Gleixner 370dc186ad7SThomas Gleixner /* 371dc186ad7SThomas Gleixner * fixup_activate is called when: 372dc186ad7SThomas Gleixner * - an active object is activated 373dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 374dc186ad7SThomas Gleixner */ 375dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 376dc186ad7SThomas Gleixner { 377dc186ad7SThomas Gleixner struct work_struct *work = addr; 378dc186ad7SThomas Gleixner 379dc186ad7SThomas Gleixner switch (state) { 380dc186ad7SThomas Gleixner 381dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 382dc186ad7SThomas Gleixner /* 383dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 384dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 385dc186ad7SThomas Gleixner * is tracked in the object tracker. 386dc186ad7SThomas Gleixner */ 38722df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 388dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 389dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 390dc186ad7SThomas Gleixner return 0; 391dc186ad7SThomas Gleixner } 392dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 393dc186ad7SThomas Gleixner return 0; 394dc186ad7SThomas Gleixner 395dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 396dc186ad7SThomas Gleixner WARN_ON(1); 397dc186ad7SThomas Gleixner 398dc186ad7SThomas Gleixner default: 399dc186ad7SThomas Gleixner return 0; 400dc186ad7SThomas Gleixner } 401dc186ad7SThomas Gleixner } 402dc186ad7SThomas Gleixner 403dc186ad7SThomas Gleixner /* 404dc186ad7SThomas Gleixner * fixup_free is called when: 405dc186ad7SThomas Gleixner * - an active object is freed 406dc186ad7SThomas Gleixner */ 407dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 408dc186ad7SThomas Gleixner { 409dc186ad7SThomas Gleixner struct work_struct *work = addr; 410dc186ad7SThomas Gleixner 411dc186ad7SThomas Gleixner switch (state) { 412dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 413dc186ad7SThomas Gleixner cancel_work_sync(work); 414dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 415dc186ad7SThomas Gleixner return 1; 416dc186ad7SThomas Gleixner default: 417dc186ad7SThomas Gleixner return 0; 418dc186ad7SThomas Gleixner } 419dc186ad7SThomas Gleixner } 420dc186ad7SThomas Gleixner 421dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 422dc186ad7SThomas Gleixner .name = "work_struct", 42399777288SStanislaw Gruszka .debug_hint = work_debug_hint, 424dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 425dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 426dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 427dc186ad7SThomas Gleixner }; 428dc186ad7SThomas Gleixner 429dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 430dc186ad7SThomas Gleixner { 431dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 432dc186ad7SThomas Gleixner } 433dc186ad7SThomas Gleixner 434dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 435dc186ad7SThomas Gleixner { 436dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 437dc186ad7SThomas Gleixner } 438dc186ad7SThomas Gleixner 439dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 440dc186ad7SThomas Gleixner { 441dc186ad7SThomas Gleixner if (onstack) 442dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 443dc186ad7SThomas Gleixner else 444dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 445dc186ad7SThomas Gleixner } 446dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 447dc186ad7SThomas Gleixner 448dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 449dc186ad7SThomas Gleixner { 450dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 451dc186ad7SThomas Gleixner } 452dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 453dc186ad7SThomas Gleixner 454dc186ad7SThomas Gleixner #else 455dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 456dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 457dc186ad7SThomas Gleixner #endif 458dc186ad7SThomas Gleixner 45995402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 46095402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4611da177e4SLinus Torvalds static LIST_HEAD(workqueues); 462a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4631da177e4SLinus Torvalds 46414441960SOleg Nesterov /* 465e22bee78STejun Heo * The almighty global cpu workqueues. nr_running is the only field 466e22bee78STejun Heo * which is expected to be used frequently by other cpus via 467e22bee78STejun Heo * try_to_wake_up(). Put it in a separate cacheline. 46814441960SOleg Nesterov */ 4698b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq); 4704ce62e9eSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]); 471f756d5e2SNathan Lynch 472f3421797STejun Heo /* 473f3421797STejun Heo * Global cpu workqueue and nr_running counter for unbound gcwq. The 474f3421797STejun Heo * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its 475f3421797STejun Heo * workers have WORKER_UNBOUND set. 476f3421797STejun Heo */ 477f3421797STejun Heo static struct global_cwq unbound_global_cwq; 4784ce62e9eSTejun Heo static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = { 4794ce62e9eSTejun Heo [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */ 4804ce62e9eSTejun Heo }; 481f3421797STejun Heo 482c34056a3STejun Heo static int worker_thread(void *__worker); 4831da177e4SLinus Torvalds 4843270476aSTejun Heo static int worker_pool_pri(struct worker_pool *pool) 4853270476aSTejun Heo { 4863270476aSTejun Heo return pool - pool->gcwq->pools; 4873270476aSTejun Heo } 4883270476aSTejun Heo 4898b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu) 4901da177e4SLinus Torvalds { 491f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 4928b03ae3cSTejun Heo return &per_cpu(global_cwq, cpu); 493f3421797STejun Heo else 494f3421797STejun Heo return &unbound_global_cwq; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 49763d95a91STejun Heo static atomic_t *get_pool_nr_running(struct worker_pool *pool) 498b1f4ec17SOleg Nesterov { 49963d95a91STejun Heo int cpu = pool->gcwq->cpu; 5003270476aSTejun Heo int idx = worker_pool_pri(pool); 50163d95a91STejun Heo 502f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 5034ce62e9eSTejun Heo return &per_cpu(pool_nr_running, cpu)[idx]; 504f3421797STejun Heo else 5054ce62e9eSTejun Heo return &unbound_pool_nr_running[idx]; 506b1f4ec17SOleg Nesterov } 507b1f4ec17SOleg Nesterov 5084690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 5094690c4abSTejun Heo struct workqueue_struct *wq) 510a848e3b6SOleg Nesterov { 511f3421797STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 512e06ffa1eSLai Jiangshan if (likely(cpu < nr_cpu_ids)) 513bdbc5dd7STejun Heo return per_cpu_ptr(wq->cpu_wq.pcpu, cpu); 514f3421797STejun Heo } else if (likely(cpu == WORK_CPU_UNBOUND)) 515f3421797STejun Heo return wq->cpu_wq.single; 516f3421797STejun Heo return NULL; 517f3421797STejun Heo } 518a848e3b6SOleg Nesterov 51973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 52073f53c4aSTejun Heo { 52173f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 52273f53c4aSTejun Heo } 52373f53c4aSTejun Heo 52473f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 52573f53c4aSTejun Heo { 52673f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 52773f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 52873f53c4aSTejun Heo } 52973f53c4aSTejun Heo 53073f53c4aSTejun Heo static int work_next_color(int color) 53173f53c4aSTejun Heo { 53273f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5354594bf15SDavid Howells /* 536b5490077STejun Heo * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data 537b5490077STejun Heo * contain the pointer to the queued cwq. Once execution starts, the flag 538b5490077STejun Heo * is cleared and the high bits contain OFFQ flags and CPU number. 5397a22ad75STejun Heo * 540*bbb68dfaSTejun Heo * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling() 541*bbb68dfaSTejun Heo * and clear_work_data() can be used to set the cwq, cpu or clear 542*bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 543*bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 5447a22ad75STejun Heo * 545*bbb68dfaSTejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to 546*bbb68dfaSTejun Heo * a work. gcwq is available once the work has been queued anywhere after 547*bbb68dfaSTejun Heo * initialization until it is sync canceled. cwq is available only while 548*bbb68dfaSTejun Heo * the work item is queued. 549*bbb68dfaSTejun Heo * 550*bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 551*bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 552*bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 553*bbb68dfaSTejun Heo * try to steal the PENDING bit. 5544594bf15SDavid Howells */ 5557a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5567a22ad75STejun Heo unsigned long flags) 5577a22ad75STejun Heo { 5587a22ad75STejun Heo BUG_ON(!work_pending(work)); 5597a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5607a22ad75STejun Heo } 5617a22ad75STejun Heo 5627a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5634690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5644690c4abSTejun Heo unsigned long extra_flags) 565365970a1SDavid Howells { 5667a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 567e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 568365970a1SDavid Howells } 569365970a1SDavid Howells 5708930cabaSTejun Heo static void set_work_cpu_and_clear_pending(struct work_struct *work, 5718930cabaSTejun Heo unsigned int cpu) 5724d707b9fSOleg Nesterov { 573b5490077STejun Heo set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0); 5744d707b9fSOleg Nesterov } 5754d707b9fSOleg Nesterov 5767a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 577365970a1SDavid Howells { 5787a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5797a22ad75STejun Heo } 5807a22ad75STejun Heo 5817a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5827a22ad75STejun Heo { 583e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5847a22ad75STejun Heo 585e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 586e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 587e120153dSTejun Heo else 588e120153dSTejun Heo return NULL; 5897a22ad75STejun Heo } 5907a22ad75STejun Heo 5917a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5927a22ad75STejun Heo { 593e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5947a22ad75STejun Heo unsigned int cpu; 5957a22ad75STejun Heo 596e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 597e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 598bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5997a22ad75STejun Heo 600b5490077STejun Heo cpu = data >> WORK_OFFQ_CPU_SHIFT; 601bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 6027a22ad75STejun Heo return NULL; 6037a22ad75STejun Heo 604f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 6057a22ad75STejun Heo return get_gcwq(cpu); 606365970a1SDavid Howells } 607365970a1SDavid Howells 608*bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 609*bbb68dfaSTejun Heo { 610*bbb68dfaSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 611*bbb68dfaSTejun Heo unsigned long cpu = gcwq ? gcwq->cpu : WORK_CPU_NONE; 612*bbb68dfaSTejun Heo 613*bbb68dfaSTejun Heo set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING, 614*bbb68dfaSTejun Heo WORK_STRUCT_PENDING); 615*bbb68dfaSTejun Heo } 616*bbb68dfaSTejun Heo 617*bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 618*bbb68dfaSTejun Heo { 619*bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 620*bbb68dfaSTejun Heo 621*bbb68dfaSTejun Heo return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING); 622*bbb68dfaSTejun Heo } 623*bbb68dfaSTejun Heo 624e22bee78STejun Heo /* 6253270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6263270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6273270476aSTejun Heo * they're being called with gcwq->lock held. 628e22bee78STejun Heo */ 629e22bee78STejun Heo 63063d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 631649027d7STejun Heo { 6323270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 633649027d7STejun Heo } 634649027d7STejun Heo 635e22bee78STejun Heo /* 636e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 637e22bee78STejun Heo * running workers. 638974271c4STejun Heo * 639974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 640974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 641974271c4STejun Heo * worklist isn't empty. 642e22bee78STejun Heo */ 64363d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 644e22bee78STejun Heo { 64563d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 646e22bee78STejun Heo } 647e22bee78STejun Heo 648e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 64963d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 650e22bee78STejun Heo { 65163d95a91STejun Heo return pool->nr_idle; 652e22bee78STejun Heo } 653e22bee78STejun Heo 654e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 65563d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 656e22bee78STejun Heo { 65763d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 658e22bee78STejun Heo 6593270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 660e22bee78STejun Heo } 661e22bee78STejun Heo 662e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 66363d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 664e22bee78STejun Heo { 66563d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 666e22bee78STejun Heo } 667e22bee78STejun Heo 668e22bee78STejun Heo /* Do I need to be the manager? */ 66963d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 670e22bee78STejun Heo { 67163d95a91STejun Heo return need_to_create_worker(pool) || 67211ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 673e22bee78STejun Heo } 674e22bee78STejun Heo 675e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 67663d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 677e22bee78STejun Heo { 67860373152STejun Heo bool managing = mutex_is_locked(&pool->manager_mutex); 67963d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 68063d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 681e22bee78STejun Heo 682e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 683e22bee78STejun Heo } 684e22bee78STejun Heo 685e22bee78STejun Heo /* 686e22bee78STejun Heo * Wake up functions. 687e22bee78STejun Heo */ 688e22bee78STejun Heo 6897e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 69063d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6917e11629dSTejun Heo { 69263d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6937e11629dSTejun Heo return NULL; 6947e11629dSTejun Heo 69563d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6967e11629dSTejun Heo } 6977e11629dSTejun Heo 6987e11629dSTejun Heo /** 6997e11629dSTejun Heo * wake_up_worker - wake up an idle worker 70063d95a91STejun Heo * @pool: worker pool to wake worker from 7017e11629dSTejun Heo * 70263d95a91STejun Heo * Wake up the first idle worker of @pool. 7037e11629dSTejun Heo * 7047e11629dSTejun Heo * CONTEXT: 7057e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 7067e11629dSTejun Heo */ 70763d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 7087e11629dSTejun Heo { 70963d95a91STejun Heo struct worker *worker = first_worker(pool); 7107e11629dSTejun Heo 7117e11629dSTejun Heo if (likely(worker)) 7127e11629dSTejun Heo wake_up_process(worker->task); 7137e11629dSTejun Heo } 7147e11629dSTejun Heo 7154690c4abSTejun Heo /** 716e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 717e22bee78STejun Heo * @task: task waking up 718e22bee78STejun Heo * @cpu: CPU @task is waking up to 719e22bee78STejun Heo * 720e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 721e22bee78STejun Heo * being awoken. 722e22bee78STejun Heo * 723e22bee78STejun Heo * CONTEXT: 724e22bee78STejun Heo * spin_lock_irq(rq->lock) 725e22bee78STejun Heo */ 726e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 727e22bee78STejun Heo { 728e22bee78STejun Heo struct worker *worker = kthread_data(task); 729e22bee78STejun Heo 7302d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 73163d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 732e22bee78STejun Heo } 733e22bee78STejun Heo 734e22bee78STejun Heo /** 735e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 736e22bee78STejun Heo * @task: task going to sleep 737e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 738e22bee78STejun Heo * 739e22bee78STejun Heo * This function is called during schedule() when a busy worker is 740e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 741e22bee78STejun Heo * returning pointer to its task. 742e22bee78STejun Heo * 743e22bee78STejun Heo * CONTEXT: 744e22bee78STejun Heo * spin_lock_irq(rq->lock) 745e22bee78STejun Heo * 746e22bee78STejun Heo * RETURNS: 747e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 748e22bee78STejun Heo */ 749e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 750e22bee78STejun Heo unsigned int cpu) 751e22bee78STejun Heo { 752e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 753bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 75463d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 755e22bee78STejun Heo 7562d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 757e22bee78STejun Heo return NULL; 758e22bee78STejun Heo 759e22bee78STejun Heo /* this can only happen on the local cpu */ 760e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 761e22bee78STejun Heo 762e22bee78STejun Heo /* 763e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 764e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 765e22bee78STejun Heo * Please read comment there. 766e22bee78STejun Heo * 767628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 768628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 769628c78e7STejun Heo * disabled, which in turn means that none else could be 770628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 771628c78e7STejun Heo * lock is safe. 772e22bee78STejun Heo */ 773bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 77463d95a91STejun Heo to_wakeup = first_worker(pool); 775e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 776e22bee78STejun Heo } 777e22bee78STejun Heo 778e22bee78STejun Heo /** 779e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 780cb444766STejun Heo * @worker: self 781d302f017STejun Heo * @flags: flags to set 782d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 783d302f017STejun Heo * 784e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 785e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 786e22bee78STejun Heo * woken up. 787d302f017STejun Heo * 788cb444766STejun Heo * CONTEXT: 789cb444766STejun Heo * spin_lock_irq(gcwq->lock) 790d302f017STejun Heo */ 791d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 792d302f017STejun Heo bool wakeup) 793d302f017STejun Heo { 794bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 795e22bee78STejun Heo 796cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 797cb444766STejun Heo 798e22bee78STejun Heo /* 799e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 800e22bee78STejun Heo * wake up an idle worker as necessary if requested by 801e22bee78STejun Heo * @wakeup. 802e22bee78STejun Heo */ 803e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 804e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 80563d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 806e22bee78STejun Heo 807e22bee78STejun Heo if (wakeup) { 808e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 809bd7bdd43STejun Heo !list_empty(&pool->worklist)) 81063d95a91STejun Heo wake_up_worker(pool); 811e22bee78STejun Heo } else 812e22bee78STejun Heo atomic_dec(nr_running); 813e22bee78STejun Heo } 814e22bee78STejun Heo 815d302f017STejun Heo worker->flags |= flags; 816d302f017STejun Heo } 817d302f017STejun Heo 818d302f017STejun Heo /** 819e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 820cb444766STejun Heo * @worker: self 821d302f017STejun Heo * @flags: flags to clear 822d302f017STejun Heo * 823e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 824d302f017STejun Heo * 825cb444766STejun Heo * CONTEXT: 826cb444766STejun Heo * spin_lock_irq(gcwq->lock) 827d302f017STejun Heo */ 828d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 829d302f017STejun Heo { 83063d95a91STejun Heo struct worker_pool *pool = worker->pool; 831e22bee78STejun Heo unsigned int oflags = worker->flags; 832e22bee78STejun Heo 833cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 834cb444766STejun Heo 835d302f017STejun Heo worker->flags &= ~flags; 836e22bee78STejun Heo 83742c025f3STejun Heo /* 83842c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 83942c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 84042c025f3STejun Heo * of multiple flags, not a single flag. 84142c025f3STejun Heo */ 842e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 843e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 84463d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 845d302f017STejun Heo } 846d302f017STejun Heo 847d302f017STejun Heo /** 848c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 849c8e55f36STejun Heo * @gcwq: gcwq of interest 850c8e55f36STejun Heo * @work: work to be hashed 851c8e55f36STejun Heo * 852c8e55f36STejun Heo * Return hash head of @gcwq for @work. 853c8e55f36STejun Heo * 854c8e55f36STejun Heo * CONTEXT: 855c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 856c8e55f36STejun Heo * 857c8e55f36STejun Heo * RETURNS: 858c8e55f36STejun Heo * Pointer to the hash head. 859c8e55f36STejun Heo */ 860c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 861c8e55f36STejun Heo struct work_struct *work) 862c8e55f36STejun Heo { 863c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 864c8e55f36STejun Heo unsigned long v = (unsigned long)work; 865c8e55f36STejun Heo 866c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 867c8e55f36STejun Heo v >>= base_shift; 868c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 869c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 870c8e55f36STejun Heo 871c8e55f36STejun Heo return &gcwq->busy_hash[v]; 872c8e55f36STejun Heo } 873c8e55f36STejun Heo 874c8e55f36STejun Heo /** 8758cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8768cca0eeaSTejun Heo * @gcwq: gcwq of interest 8778cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8788cca0eeaSTejun Heo * @work: work to find worker for 8798cca0eeaSTejun Heo * 8808cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8818cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8828cca0eeaSTejun Heo * work. 8838cca0eeaSTejun Heo * 8848cca0eeaSTejun Heo * CONTEXT: 8858cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8868cca0eeaSTejun Heo * 8878cca0eeaSTejun Heo * RETURNS: 8888cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8898cca0eeaSTejun Heo * otherwise. 8908cca0eeaSTejun Heo */ 8918cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8928cca0eeaSTejun Heo struct hlist_head *bwh, 8938cca0eeaSTejun Heo struct work_struct *work) 8948cca0eeaSTejun Heo { 8958cca0eeaSTejun Heo struct worker *worker; 8968cca0eeaSTejun Heo struct hlist_node *tmp; 8978cca0eeaSTejun Heo 8988cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8998cca0eeaSTejun Heo if (worker->current_work == work) 9008cca0eeaSTejun Heo return worker; 9018cca0eeaSTejun Heo return NULL; 9028cca0eeaSTejun Heo } 9038cca0eeaSTejun Heo 9048cca0eeaSTejun Heo /** 9058cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 9068cca0eeaSTejun Heo * @gcwq: gcwq of interest 9078cca0eeaSTejun Heo * @work: work to find worker for 9088cca0eeaSTejun Heo * 9098cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 9108cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 9118cca0eeaSTejun Heo * function calculates @bwh itself. 9128cca0eeaSTejun Heo * 9138cca0eeaSTejun Heo * CONTEXT: 9148cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 9158cca0eeaSTejun Heo * 9168cca0eeaSTejun Heo * RETURNS: 9178cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 9188cca0eeaSTejun Heo * otherwise. 9198cca0eeaSTejun Heo */ 9208cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 9218cca0eeaSTejun Heo struct work_struct *work) 9228cca0eeaSTejun Heo { 9238cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9248cca0eeaSTejun Heo work); 9258cca0eeaSTejun Heo } 9268cca0eeaSTejun Heo 9278cca0eeaSTejun Heo /** 928bf4ede01STejun Heo * move_linked_works - move linked works to a list 929bf4ede01STejun Heo * @work: start of series of works to be scheduled 930bf4ede01STejun Heo * @head: target list to append @work to 931bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 932bf4ede01STejun Heo * 933bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 934bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 935bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 936bf4ede01STejun Heo * 937bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 938bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 939bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 940bf4ede01STejun Heo * 941bf4ede01STejun Heo * CONTEXT: 942bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 943bf4ede01STejun Heo */ 944bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 945bf4ede01STejun Heo struct work_struct **nextp) 946bf4ede01STejun Heo { 947bf4ede01STejun Heo struct work_struct *n; 948bf4ede01STejun Heo 949bf4ede01STejun Heo /* 950bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 951bf4ede01STejun Heo * use NULL for list head. 952bf4ede01STejun Heo */ 953bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 954bf4ede01STejun Heo list_move_tail(&work->entry, head); 955bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 956bf4ede01STejun Heo break; 957bf4ede01STejun Heo } 958bf4ede01STejun Heo 959bf4ede01STejun Heo /* 960bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 961bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 962bf4ede01STejun Heo * needs to be updated. 963bf4ede01STejun Heo */ 964bf4ede01STejun Heo if (nextp) 965bf4ede01STejun Heo *nextp = n; 966bf4ede01STejun Heo } 967bf4ede01STejun Heo 968bf4ede01STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 969bf4ede01STejun Heo { 970bf4ede01STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 971bf4ede01STejun Heo struct work_struct, entry); 972bf4ede01STejun Heo 973bf4ede01STejun Heo trace_workqueue_activate_work(work); 974bf4ede01STejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 975bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 976bf4ede01STejun Heo cwq->nr_active++; 977bf4ede01STejun Heo } 978bf4ede01STejun Heo 979bf4ede01STejun Heo /** 980bf4ede01STejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 981bf4ede01STejun Heo * @cwq: cwq of interest 982bf4ede01STejun Heo * @color: color of work which left the queue 983bf4ede01STejun Heo * @delayed: for a delayed work 984bf4ede01STejun Heo * 985bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 986bf4ede01STejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 987bf4ede01STejun Heo * 988bf4ede01STejun Heo * CONTEXT: 989bf4ede01STejun Heo * spin_lock_irq(gcwq->lock). 990bf4ede01STejun Heo */ 991bf4ede01STejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 992bf4ede01STejun Heo bool delayed) 993bf4ede01STejun Heo { 994bf4ede01STejun Heo /* ignore uncolored works */ 995bf4ede01STejun Heo if (color == WORK_NO_COLOR) 996bf4ede01STejun Heo return; 997bf4ede01STejun Heo 998bf4ede01STejun Heo cwq->nr_in_flight[color]--; 999bf4ede01STejun Heo 1000bf4ede01STejun Heo if (!delayed) { 1001bf4ede01STejun Heo cwq->nr_active--; 1002bf4ede01STejun Heo if (!list_empty(&cwq->delayed_works)) { 1003bf4ede01STejun Heo /* one down, submit a delayed one */ 1004bf4ede01STejun Heo if (cwq->nr_active < cwq->max_active) 1005bf4ede01STejun Heo cwq_activate_first_delayed(cwq); 1006bf4ede01STejun Heo } 1007bf4ede01STejun Heo } 1008bf4ede01STejun Heo 1009bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1010bf4ede01STejun Heo if (likely(cwq->flush_color != color)) 1011bf4ede01STejun Heo return; 1012bf4ede01STejun Heo 1013bf4ede01STejun Heo /* are there still in-flight works? */ 1014bf4ede01STejun Heo if (cwq->nr_in_flight[color]) 1015bf4ede01STejun Heo return; 1016bf4ede01STejun Heo 1017bf4ede01STejun Heo /* this cwq is done, clear flush_color */ 1018bf4ede01STejun Heo cwq->flush_color = -1; 1019bf4ede01STejun Heo 1020bf4ede01STejun Heo /* 1021bf4ede01STejun Heo * If this was the last cwq, wake up the first flusher. It 1022bf4ede01STejun Heo * will handle the rest. 1023bf4ede01STejun Heo */ 1024bf4ede01STejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 1025bf4ede01STejun Heo complete(&cwq->wq->first_flusher->done); 1026bf4ede01STejun Heo } 1027bf4ede01STejun Heo 102836e227d2STejun Heo /** 1029*bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 103036e227d2STejun Heo * @work: work item to steal 103136e227d2STejun Heo * @is_dwork: @work is a delayed_work 1032*bbb68dfaSTejun Heo * @flags: place to store irq state 103336e227d2STejun Heo * 103436e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 103536e227d2STejun Heo * stable state - idle, on timer or on worklist. Return values are 103636e227d2STejun Heo * 103736e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 103836e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 103936e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1040*bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1041*bbb68dfaSTejun Heo * for arbitrarily long 104236e227d2STejun Heo * 1043*bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1044*bbb68dfaSTejun Heo * preempted while holding PENDING and @work off queue, preemption must be 1045*bbb68dfaSTejun Heo * disabled on entry. This ensures that we don't return -EAGAIN while 1046*bbb68dfaSTejun Heo * another task is preempted in this function. 1047*bbb68dfaSTejun Heo * 1048*bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1049*bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1050*bbb68dfaSTejun Heo * 1051*bbb68dfaSTejun Heo * This function is safe to call from any context other than IRQ handler. 1052*bbb68dfaSTejun Heo * An IRQ handler may run on top of delayed_work_timer_fn() which can make 1053*bbb68dfaSTejun Heo * this function return -EAGAIN perpetually. 1054bf4ede01STejun Heo */ 1055*bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1056*bbb68dfaSTejun Heo unsigned long *flags) 1057bf4ede01STejun Heo { 1058bf4ede01STejun Heo struct global_cwq *gcwq; 1059bf4ede01STejun Heo 1060*bbb68dfaSTejun Heo WARN_ON_ONCE(in_irq()); 1061*bbb68dfaSTejun Heo 1062*bbb68dfaSTejun Heo local_irq_save(*flags); 1063*bbb68dfaSTejun Heo 106436e227d2STejun Heo /* try to steal the timer if it exists */ 106536e227d2STejun Heo if (is_dwork) { 106636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 106736e227d2STejun Heo 106836e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 106936e227d2STejun Heo return 1; 107036e227d2STejun Heo } 107136e227d2STejun Heo 107236e227d2STejun Heo /* try to claim PENDING the normal way */ 1073bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1074bf4ede01STejun Heo return 0; 1075bf4ede01STejun Heo 1076bf4ede01STejun Heo /* 1077bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1078bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1079bf4ede01STejun Heo */ 1080bf4ede01STejun Heo gcwq = get_work_gcwq(work); 1081bf4ede01STejun Heo if (!gcwq) 1082*bbb68dfaSTejun Heo goto fail; 1083bf4ede01STejun Heo 1084*bbb68dfaSTejun Heo spin_lock(&gcwq->lock); 1085bf4ede01STejun Heo if (!list_empty(&work->entry)) { 1086bf4ede01STejun Heo /* 1087bf4ede01STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 1088bf4ede01STejun Heo * In that case we must see the new value after rmb(), see 1089bf4ede01STejun Heo * insert_work()->wmb(). 1090bf4ede01STejun Heo */ 1091bf4ede01STejun Heo smp_rmb(); 1092bf4ede01STejun Heo if (gcwq == get_work_gcwq(work)) { 1093bf4ede01STejun Heo debug_work_deactivate(work); 1094bf4ede01STejun Heo list_del_init(&work->entry); 1095bf4ede01STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 1096bf4ede01STejun Heo get_work_color(work), 1097bf4ede01STejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 109836e227d2STejun Heo 1099*bbb68dfaSTejun Heo spin_unlock(&gcwq->lock); 110036e227d2STejun Heo return 1; 1101bf4ede01STejun Heo } 1102bf4ede01STejun Heo } 1103*bbb68dfaSTejun Heo spin_unlock(&gcwq->lock); 1104*bbb68dfaSTejun Heo fail: 1105*bbb68dfaSTejun Heo local_irq_restore(*flags); 1106*bbb68dfaSTejun Heo if (work_is_canceling(work)) 1107*bbb68dfaSTejun Heo return -ENOENT; 1108*bbb68dfaSTejun Heo cpu_relax(); 110936e227d2STejun Heo return -EAGAIN; 1110bf4ede01STejun Heo } 1111bf4ede01STejun Heo 1112bf4ede01STejun Heo /** 11137e11629dSTejun Heo * insert_work - insert a work into gcwq 11144690c4abSTejun Heo * @cwq: cwq @work belongs to 11154690c4abSTejun Heo * @work: work to insert 11164690c4abSTejun Heo * @head: insertion point 11174690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 11184690c4abSTejun Heo * 11197e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 11207e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 11214690c4abSTejun Heo * 11224690c4abSTejun Heo * CONTEXT: 11238b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 11241da177e4SLinus Torvalds */ 1125b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 11264690c4abSTejun Heo struct work_struct *work, struct list_head *head, 11274690c4abSTejun Heo unsigned int extra_flags) 1128b89deed3SOleg Nesterov { 112963d95a91STejun Heo struct worker_pool *pool = cwq->pool; 1130e1d8aa9fSFrederic Weisbecker 11314690c4abSTejun Heo /* we own @work, set data and link */ 11327a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 11334690c4abSTejun Heo 11346e84d644SOleg Nesterov /* 11356e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 11366e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 11376e84d644SOleg Nesterov */ 11386e84d644SOleg Nesterov smp_wmb(); 11394690c4abSTejun Heo 11401a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1141e22bee78STejun Heo 1142e22bee78STejun Heo /* 1143e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1144e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1145e22bee78STejun Heo * lying around lazily while there are works to be processed. 1146e22bee78STejun Heo */ 1147e22bee78STejun Heo smp_mb(); 1148e22bee78STejun Heo 114963d95a91STejun Heo if (__need_more_worker(pool)) 115063d95a91STejun Heo wake_up_worker(pool); 1151b89deed3SOleg Nesterov } 1152b89deed3SOleg Nesterov 1153c8efcc25STejun Heo /* 1154c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 1155c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 1156c8efcc25STejun Heo * cold paths. 1157c8efcc25STejun Heo */ 1158c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1159c8efcc25STejun Heo { 1160c8efcc25STejun Heo unsigned long flags; 1161c8efcc25STejun Heo unsigned int cpu; 1162c8efcc25STejun Heo 1163c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 1164c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 1165c8efcc25STejun Heo struct worker *worker; 1166c8efcc25STejun Heo struct hlist_node *pos; 1167c8efcc25STejun Heo int i; 1168c8efcc25STejun Heo 1169c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1170c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 1171c8efcc25STejun Heo if (worker->task != current) 1172c8efcc25STejun Heo continue; 1173c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1174c8efcc25STejun Heo /* 1175c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 1176c8efcc25STejun Heo * is headed to the same workqueue. 1177c8efcc25STejun Heo */ 1178c8efcc25STejun Heo return worker->current_cwq->wq == wq; 1179c8efcc25STejun Heo } 1180c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 1181c8efcc25STejun Heo } 1182c8efcc25STejun Heo return false; 1183c8efcc25STejun Heo } 1184c8efcc25STejun Heo 11854690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 11861da177e4SLinus Torvalds struct work_struct *work) 11871da177e4SLinus Torvalds { 1188502ca9d8STejun Heo struct global_cwq *gcwq; 1189502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 11901e19ffc6STejun Heo struct list_head *worklist; 11918a2e8e5dSTejun Heo unsigned int work_flags; 11928930cabaSTejun Heo 11938930cabaSTejun Heo /* 11948930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11958930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11968930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11978930cabaSTejun Heo * happen with IRQ disabled. 11988930cabaSTejun Heo */ 11998930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 12001da177e4SLinus Torvalds 1201dc186ad7SThomas Gleixner debug_work_activate(work); 12021e19ffc6STejun Heo 1203c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 12049c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1205c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1206e41e704bSTejun Heo return; 1207e41e704bSTejun Heo 1208c7fc77f7STejun Heo /* determine gcwq to use */ 1209c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1210c7fc77f7STejun Heo struct global_cwq *last_gcwq; 1211c7fc77f7STejun Heo 121257469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1213f3421797STejun Heo cpu = raw_smp_processor_id(); 1214f3421797STejun Heo 121518aa9effSTejun Heo /* 121618aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 121718aa9effSTejun Heo * was previously on a different cpu, it might still 121818aa9effSTejun Heo * be running there, in which case the work needs to 121918aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 122018aa9effSTejun Heo */ 1221502ca9d8STejun Heo gcwq = get_gcwq(cpu); 122218aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 122318aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 122418aa9effSTejun Heo struct worker *worker; 122518aa9effSTejun Heo 12268930cabaSTejun Heo spin_lock(&last_gcwq->lock); 122718aa9effSTejun Heo 122818aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 122918aa9effSTejun Heo 123018aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 123118aa9effSTejun Heo gcwq = last_gcwq; 123218aa9effSTejun Heo else { 123318aa9effSTejun Heo /* meh... not running there, queue here */ 12348930cabaSTejun Heo spin_unlock(&last_gcwq->lock); 12358930cabaSTejun Heo spin_lock(&gcwq->lock); 123618aa9effSTejun Heo } 12378930cabaSTejun Heo } else { 12388930cabaSTejun Heo spin_lock(&gcwq->lock); 12398930cabaSTejun Heo } 1240f3421797STejun Heo } else { 1241f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 12428930cabaSTejun Heo spin_lock(&gcwq->lock); 1243502ca9d8STejun Heo } 1244502ca9d8STejun Heo 1245502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1246502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1247cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1248502ca9d8STejun Heo 1249f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 12508930cabaSTejun Heo spin_unlock(&gcwq->lock); 1251f5b2552bSDan Carpenter return; 1252f5b2552bSDan Carpenter } 12531e19ffc6STejun Heo 125473f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 12558a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 12561e19ffc6STejun Heo 12571e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1258cdadf009STejun Heo trace_workqueue_activate_work(work); 12591e19ffc6STejun Heo cwq->nr_active++; 12603270476aSTejun Heo worklist = &cwq->pool->worklist; 12618a2e8e5dSTejun Heo } else { 12628a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 12631e19ffc6STejun Heo worklist = &cwq->delayed_works; 12648a2e8e5dSTejun Heo } 12651e19ffc6STejun Heo 12668a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 12671e19ffc6STejun Heo 12688930cabaSTejun Heo spin_unlock(&gcwq->lock); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12710fcb78c2SRolf Eike Beer /** 1272c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1273c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1274c1a220e7SZhang Rui * @wq: workqueue to use 1275c1a220e7SZhang Rui * @work: work to queue 1276c1a220e7SZhang Rui * 1277d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1278c1a220e7SZhang Rui * 1279c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1280c1a220e7SZhang Rui * can't go away. 1281c1a220e7SZhang Rui */ 1282d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1283d4283e93STejun Heo struct work_struct *work) 1284c1a220e7SZhang Rui { 1285d4283e93STejun Heo bool ret = false; 12868930cabaSTejun Heo unsigned long flags; 12878930cabaSTejun Heo 12888930cabaSTejun Heo local_irq_save(flags); 1289c1a220e7SZhang Rui 129022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12914690c4abSTejun Heo __queue_work(cpu, wq, work); 1292d4283e93STejun Heo ret = true; 1293c1a220e7SZhang Rui } 12948930cabaSTejun Heo 12958930cabaSTejun Heo local_irq_restore(flags); 1296c1a220e7SZhang Rui return ret; 1297c1a220e7SZhang Rui } 1298c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1299c1a220e7SZhang Rui 13000a13c00eSTejun Heo /** 13010a13c00eSTejun Heo * queue_work - queue work on a workqueue 13020a13c00eSTejun Heo * @wq: workqueue to use 13030a13c00eSTejun Heo * @work: work to queue 13040a13c00eSTejun Heo * 1305d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 13060a13c00eSTejun Heo * 13070a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 13080a13c00eSTejun Heo * it can be processed by another CPU. 13090a13c00eSTejun Heo */ 1310d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 13110a13c00eSTejun Heo { 131257469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 13130a13c00eSTejun Heo } 13140a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 13150a13c00eSTejun Heo 1316d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 13171da177e4SLinus Torvalds { 131852bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 13197a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 13201da177e4SLinus Torvalds 13218930cabaSTejun Heo local_irq_disable(); 132257469821STejun Heo __queue_work(WORK_CPU_UNBOUND, cwq->wq, &dwork->work); 13238930cabaSTejun Heo local_irq_enable(); 13241da177e4SLinus Torvalds } 1325d8e794dfSTejun Heo EXPORT_SYMBOL_GPL(delayed_work_timer_fn); 13261da177e4SLinus Torvalds 13277beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 13287beb2edfSTejun Heo struct delayed_work *dwork, unsigned long delay) 13297beb2edfSTejun Heo { 13307beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 13317beb2edfSTejun Heo struct work_struct *work = &dwork->work; 13327beb2edfSTejun Heo unsigned int lcpu; 13337beb2edfSTejun Heo 13347beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 13357beb2edfSTejun Heo timer->data != (unsigned long)dwork); 13367beb2edfSTejun Heo BUG_ON(timer_pending(timer)); 13377beb2edfSTejun Heo BUG_ON(!list_empty(&work->entry)); 13387beb2edfSTejun Heo 13397beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 13407beb2edfSTejun Heo 13417beb2edfSTejun Heo /* 13427beb2edfSTejun Heo * This stores cwq for the moment, for the timer_fn. Note that the 13437beb2edfSTejun Heo * work's gcwq is preserved to allow reentrance detection for 13447beb2edfSTejun Heo * delayed works. 13457beb2edfSTejun Heo */ 13467beb2edfSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 13477beb2edfSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 13487beb2edfSTejun Heo 13497beb2edfSTejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 13507beb2edfSTejun Heo lcpu = gcwq->cpu; 13517beb2edfSTejun Heo else 13527beb2edfSTejun Heo lcpu = raw_smp_processor_id(); 13537beb2edfSTejun Heo } else { 13547beb2edfSTejun Heo lcpu = WORK_CPU_UNBOUND; 13557beb2edfSTejun Heo } 13567beb2edfSTejun Heo 13577beb2edfSTejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 13587beb2edfSTejun Heo 13597beb2edfSTejun Heo timer->expires = jiffies + delay; 13607beb2edfSTejun Heo 13617beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13627beb2edfSTejun Heo add_timer_on(timer, cpu); 13637beb2edfSTejun Heo else 13647beb2edfSTejun Heo add_timer(timer); 13657beb2edfSTejun Heo } 13667beb2edfSTejun Heo 13670fcb78c2SRolf Eike Beer /** 13680fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 13690fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 13700fcb78c2SRolf Eike Beer * @wq: workqueue to use 1371af9997e4SRandy Dunlap * @dwork: work to queue 13720fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 13730fcb78c2SRolf Eike Beer * 1374715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1375715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1376715f1300STejun Heo * execution. 13770fcb78c2SRolf Eike Beer */ 1378d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 137952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13807a6bc1cdSVenkatesh Pallipadi { 138152bad64dSDavid Howells struct work_struct *work = &dwork->work; 1382d4283e93STejun Heo bool ret = false; 13838930cabaSTejun Heo unsigned long flags; 13848930cabaSTejun Heo 1385715f1300STejun Heo if (!delay) 1386715f1300STejun Heo return queue_work_on(cpu, wq, &dwork->work); 1387715f1300STejun Heo 13888930cabaSTejun Heo /* read the comment in __queue_work() */ 13898930cabaSTejun Heo local_irq_save(flags); 13907a6bc1cdSVenkatesh Pallipadi 139122df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 13927beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1393d4283e93STejun Heo ret = true; 13947a6bc1cdSVenkatesh Pallipadi } 13958930cabaSTejun Heo 13968930cabaSTejun Heo local_irq_restore(flags); 13977a6bc1cdSVenkatesh Pallipadi return ret; 13987a6bc1cdSVenkatesh Pallipadi } 1399ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 14001da177e4SLinus Torvalds 1401c8e55f36STejun Heo /** 14020a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 14030a13c00eSTejun Heo * @wq: workqueue to use 14040a13c00eSTejun Heo * @dwork: delayable work to queue 14050a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 14060a13c00eSTejun Heo * 1407715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 14080a13c00eSTejun Heo */ 1409d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 14100a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 14110a13c00eSTejun Heo { 141257469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 14130a13c00eSTejun Heo } 14140a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 14150a13c00eSTejun Heo 14160a13c00eSTejun Heo /** 1417c8e55f36STejun Heo * worker_enter_idle - enter idle state 1418c8e55f36STejun Heo * @worker: worker which is entering idle state 1419c8e55f36STejun Heo * 1420c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1421c8e55f36STejun Heo * necessary. 1422c8e55f36STejun Heo * 1423c8e55f36STejun Heo * LOCKING: 1424c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1425c8e55f36STejun Heo */ 1426c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 14271da177e4SLinus Torvalds { 1428bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1429bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1430c8e55f36STejun Heo 1431c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1432c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1433c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1434c8e55f36STejun Heo 1435cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1436cb444766STejun Heo worker->flags |= WORKER_IDLE; 1437bd7bdd43STejun Heo pool->nr_idle++; 1438e22bee78STejun Heo worker->last_active = jiffies; 1439c8e55f36STejun Heo 1440c8e55f36STejun Heo /* idle_list is LIFO */ 1441bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1442db7bccf4STejun Heo 144363d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1444628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1445cb444766STejun Heo 1446544ecf31STejun Heo /* 1447628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1448628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1449628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1450628c78e7STejun Heo * unbind is not in progress. 1451544ecf31STejun Heo */ 1452628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1453bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 145463d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1455c8e55f36STejun Heo } 1456c8e55f36STejun Heo 1457c8e55f36STejun Heo /** 1458c8e55f36STejun Heo * worker_leave_idle - leave idle state 1459c8e55f36STejun Heo * @worker: worker which is leaving idle state 1460c8e55f36STejun Heo * 1461c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1462c8e55f36STejun Heo * 1463c8e55f36STejun Heo * LOCKING: 1464c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1465c8e55f36STejun Heo */ 1466c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1467c8e55f36STejun Heo { 1468bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1469c8e55f36STejun Heo 1470c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1471d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1472bd7bdd43STejun Heo pool->nr_idle--; 1473c8e55f36STejun Heo list_del_init(&worker->entry); 1474c8e55f36STejun Heo } 1475c8e55f36STejun Heo 1476e22bee78STejun Heo /** 1477e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1478e22bee78STejun Heo * @worker: self 1479e22bee78STejun Heo * 1480e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1481e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1482e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1483e22bee78STejun Heo * guaranteed to execute on the cpu. 1484e22bee78STejun Heo * 1485e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1486e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1487e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1488e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1489e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1490e22bee78STejun Heo * [dis]associated in the meantime. 1491e22bee78STejun Heo * 1492f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1493f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1494f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1495f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1496f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1497e22bee78STejun Heo * 1498e22bee78STejun Heo * CONTEXT: 1499e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1500e22bee78STejun Heo * held. 1501e22bee78STejun Heo * 1502e22bee78STejun Heo * RETURNS: 1503e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1504e22bee78STejun Heo * bound), %false if offline. 1505e22bee78STejun Heo */ 1506e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1507972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1508e22bee78STejun Heo { 1509bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1510e22bee78STejun Heo struct task_struct *task = worker->task; 1511e22bee78STejun Heo 1512e22bee78STejun Heo while (true) { 1513e22bee78STejun Heo /* 1514e22bee78STejun Heo * The following call may fail, succeed or succeed 1515e22bee78STejun Heo * without actually migrating the task to the cpu if 1516e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1517e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1518e22bee78STejun Heo */ 1519f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1520e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1521e22bee78STejun Heo 1522e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1523e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1524e22bee78STejun Heo return false; 1525e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1526e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1527e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1528e22bee78STejun Heo return true; 1529e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1530e22bee78STejun Heo 15315035b20fSTejun Heo /* 15325035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 15335035b20fSTejun Heo * and retry migration. cond_resched() is required here; 15345035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 15355035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 15365035b20fSTejun Heo */ 1537e22bee78STejun Heo cpu_relax(); 15385035b20fSTejun Heo cond_resched(); 1539e22bee78STejun Heo } 1540e22bee78STejun Heo } 1541e22bee78STejun Heo 154225511a47STejun Heo struct idle_rebind { 154325511a47STejun Heo int cnt; /* # workers to be rebound */ 154425511a47STejun Heo struct completion done; /* all workers rebound */ 154525511a47STejun Heo }; 154625511a47STejun Heo 1547e22bee78STejun Heo /* 154825511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 154925511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 155025511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 155125511a47STejun Heo */ 155225511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 155325511a47STejun Heo { 155425511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 155525511a47STejun Heo 155625511a47STejun Heo /* CPU must be online at this point */ 155725511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 155825511a47STejun Heo if (!--worker->idle_rebind->cnt) 155925511a47STejun Heo complete(&worker->idle_rebind->done); 156025511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 156125511a47STejun Heo 156225511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 156325511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 156425511a47STejun Heo } 156525511a47STejun Heo 156625511a47STejun Heo /* 156725511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1568403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1569403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1570403c821dSTejun Heo * executed twice without intervening cpu down. 1571e22bee78STejun Heo */ 157225511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1573e22bee78STejun Heo { 1574e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1575bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1576e22bee78STejun Heo 1577e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1578e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1579e22bee78STejun Heo 1580e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1581e22bee78STejun Heo } 1582e22bee78STejun Heo 158325511a47STejun Heo /** 158425511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 158525511a47STejun Heo * @gcwq: gcwq of interest 158625511a47STejun Heo * 158725511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 158825511a47STejun Heo * is different for idle and busy ones. 158925511a47STejun Heo * 159025511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 159125511a47STejun Heo * be complete before any worker starts executing work items with 159225511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 159325511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 159425511a47STejun Heo * 159525511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 159625511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 159725511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 159825511a47STejun Heo * 159925511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 160025511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 160125511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 160225511a47STejun Heo * be properbly bumped as busy workers rebind. 160325511a47STejun Heo * 160425511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 160525511a47STejun Heo * work item scheduled. 160625511a47STejun Heo */ 160725511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 160825511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 160925511a47STejun Heo { 161025511a47STejun Heo struct idle_rebind idle_rebind; 161125511a47STejun Heo struct worker_pool *pool; 161225511a47STejun Heo struct worker *worker; 161325511a47STejun Heo struct hlist_node *pos; 161425511a47STejun Heo int i; 161525511a47STejun Heo 161625511a47STejun Heo lockdep_assert_held(&gcwq->lock); 161725511a47STejun Heo 161825511a47STejun Heo for_each_worker_pool(pool, gcwq) 161925511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 162025511a47STejun Heo 162125511a47STejun Heo /* 162225511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 162325511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 162425511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 162525511a47STejun Heo */ 162625511a47STejun Heo init_completion(&idle_rebind.done); 162725511a47STejun Heo retry: 162825511a47STejun Heo idle_rebind.cnt = 1; 162925511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 163025511a47STejun Heo 163125511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 163225511a47STejun Heo for_each_worker_pool(pool, gcwq) { 163325511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 163425511a47STejun Heo if (worker->flags & WORKER_REBIND) 163525511a47STejun Heo continue; 163625511a47STejun Heo 163725511a47STejun Heo /* morph UNBOUND to REBIND */ 163825511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 163925511a47STejun Heo worker->flags |= WORKER_REBIND; 164025511a47STejun Heo 164125511a47STejun Heo idle_rebind.cnt++; 164225511a47STejun Heo worker->idle_rebind = &idle_rebind; 164325511a47STejun Heo 164425511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 164525511a47STejun Heo wake_up_process(worker->task); 164625511a47STejun Heo } 164725511a47STejun Heo } 164825511a47STejun Heo 164925511a47STejun Heo if (--idle_rebind.cnt) { 165025511a47STejun Heo spin_unlock_irq(&gcwq->lock); 165125511a47STejun Heo wait_for_completion(&idle_rebind.done); 165225511a47STejun Heo spin_lock_irq(&gcwq->lock); 165325511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 165425511a47STejun Heo goto retry; 165525511a47STejun Heo } 165625511a47STejun Heo 165725511a47STejun Heo /* 165825511a47STejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 165925511a47STejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 166025511a47STejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 166125511a47STejun Heo * because these workers are still guaranteed to be idle. 166225511a47STejun Heo */ 166325511a47STejun Heo for_each_worker_pool(pool, gcwq) 166425511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 166525511a47STejun Heo worker->flags &= ~WORKER_REBIND; 166625511a47STejun Heo 166725511a47STejun Heo wake_up_all(&gcwq->rebind_hold); 166825511a47STejun Heo 166925511a47STejun Heo /* rebind busy workers */ 167025511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 167125511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 167225511a47STejun Heo 167325511a47STejun Heo /* morph UNBOUND to REBIND */ 167425511a47STejun Heo worker->flags &= ~WORKER_UNBOUND; 167525511a47STejun Heo worker->flags |= WORKER_REBIND; 167625511a47STejun Heo 167725511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 167825511a47STejun Heo work_data_bits(rebind_work))) 167925511a47STejun Heo continue; 168025511a47STejun Heo 168125511a47STejun Heo /* wq doesn't matter, use the default one */ 168225511a47STejun Heo debug_work_activate(rebind_work); 168325511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 168425511a47STejun Heo worker->scheduled.next, 168525511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 168625511a47STejun Heo } 168725511a47STejun Heo } 168825511a47STejun Heo 1689c34056a3STejun Heo static struct worker *alloc_worker(void) 1690c34056a3STejun Heo { 1691c34056a3STejun Heo struct worker *worker; 1692c34056a3STejun Heo 1693c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1694c8e55f36STejun Heo if (worker) { 1695c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1696affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 169725511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1698e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1699e22bee78STejun Heo worker->flags = WORKER_PREP; 1700c8e55f36STejun Heo } 1701c34056a3STejun Heo return worker; 1702c34056a3STejun Heo } 1703c34056a3STejun Heo 1704c34056a3STejun Heo /** 1705c34056a3STejun Heo * create_worker - create a new workqueue worker 170663d95a91STejun Heo * @pool: pool the new worker will belong to 1707c34056a3STejun Heo * 170863d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1709c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1710c34056a3STejun Heo * destroy_worker(). 1711c34056a3STejun Heo * 1712c34056a3STejun Heo * CONTEXT: 1713c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1714c34056a3STejun Heo * 1715c34056a3STejun Heo * RETURNS: 1716c34056a3STejun Heo * Pointer to the newly created worker. 1717c34056a3STejun Heo */ 1718bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1719c34056a3STejun Heo { 172063d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 17213270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1722c34056a3STejun Heo struct worker *worker = NULL; 1723f3421797STejun Heo int id = -1; 1724c34056a3STejun Heo 17258b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1726bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 17278b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1728bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1729c34056a3STejun Heo goto fail; 17308b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1731c34056a3STejun Heo } 17328b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1733c34056a3STejun Heo 1734c34056a3STejun Heo worker = alloc_worker(); 1735c34056a3STejun Heo if (!worker) 1736c34056a3STejun Heo goto fail; 1737c34056a3STejun Heo 1738bd7bdd43STejun Heo worker->pool = pool; 1739c34056a3STejun Heo worker->id = id; 1740c34056a3STejun Heo 1741bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 174294dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 17433270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 17443270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1745f3421797STejun Heo else 1746f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 17473270476aSTejun Heo "kworker/u:%d%s", id, pri); 1748c34056a3STejun Heo if (IS_ERR(worker->task)) 1749c34056a3STejun Heo goto fail; 1750c34056a3STejun Heo 17513270476aSTejun Heo if (worker_pool_pri(pool)) 17523270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 17533270476aSTejun Heo 1754db7bccf4STejun Heo /* 1755bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1756bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1757bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1758bc2ae0f5STejun Heo * above the flag definition for details. 1759bc2ae0f5STejun Heo * 1760bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1761bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1762db7bccf4STejun Heo */ 1763bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 17648b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1765bc2ae0f5STejun Heo } else { 1766db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1767f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1768f3421797STejun Heo } 1769c34056a3STejun Heo 1770c34056a3STejun Heo return worker; 1771c34056a3STejun Heo fail: 1772c34056a3STejun Heo if (id >= 0) { 17738b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1774bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 17758b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1776c34056a3STejun Heo } 1777c34056a3STejun Heo kfree(worker); 1778c34056a3STejun Heo return NULL; 1779c34056a3STejun Heo } 1780c34056a3STejun Heo 1781c34056a3STejun Heo /** 1782c34056a3STejun Heo * start_worker - start a newly created worker 1783c34056a3STejun Heo * @worker: worker to start 1784c34056a3STejun Heo * 1785c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1786c34056a3STejun Heo * 1787c34056a3STejun Heo * CONTEXT: 17888b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1789c34056a3STejun Heo */ 1790c34056a3STejun Heo static void start_worker(struct worker *worker) 1791c34056a3STejun Heo { 1792cb444766STejun Heo worker->flags |= WORKER_STARTED; 1793bd7bdd43STejun Heo worker->pool->nr_workers++; 1794c8e55f36STejun Heo worker_enter_idle(worker); 1795c34056a3STejun Heo wake_up_process(worker->task); 1796c34056a3STejun Heo } 1797c34056a3STejun Heo 1798c34056a3STejun Heo /** 1799c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1800c34056a3STejun Heo * @worker: worker to be destroyed 1801c34056a3STejun Heo * 1802c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1803c8e55f36STejun Heo * 1804c8e55f36STejun Heo * CONTEXT: 1805c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1806c34056a3STejun Heo */ 1807c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1808c34056a3STejun Heo { 1809bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1810bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1811c34056a3STejun Heo int id = worker->id; 1812c34056a3STejun Heo 1813c34056a3STejun Heo /* sanity check frenzy */ 1814c34056a3STejun Heo BUG_ON(worker->current_work); 1815affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1816c34056a3STejun Heo 1817c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1818bd7bdd43STejun Heo pool->nr_workers--; 1819c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1820bd7bdd43STejun Heo pool->nr_idle--; 1821c8e55f36STejun Heo 1822c8e55f36STejun Heo list_del_init(&worker->entry); 1823cb444766STejun Heo worker->flags |= WORKER_DIE; 1824c8e55f36STejun Heo 1825c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1826c8e55f36STejun Heo 1827c34056a3STejun Heo kthread_stop(worker->task); 1828c34056a3STejun Heo kfree(worker); 1829c34056a3STejun Heo 18308b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1831bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1832c34056a3STejun Heo } 1833c34056a3STejun Heo 183463d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1835e22bee78STejun Heo { 183663d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 183763d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1838e22bee78STejun Heo 1839e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1840e22bee78STejun Heo 184163d95a91STejun Heo if (too_many_workers(pool)) { 1842e22bee78STejun Heo struct worker *worker; 1843e22bee78STejun Heo unsigned long expires; 1844e22bee78STejun Heo 1845e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 184663d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1847e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1848e22bee78STejun Heo 1849e22bee78STejun Heo if (time_before(jiffies, expires)) 185063d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1851e22bee78STejun Heo else { 1852e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 185311ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 185463d95a91STejun Heo wake_up_worker(pool); 1855e22bee78STejun Heo } 1856e22bee78STejun Heo } 1857e22bee78STejun Heo 1858e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1859e22bee78STejun Heo } 1860e22bee78STejun Heo 1861e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1862e22bee78STejun Heo { 1863e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1864e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1865f3421797STejun Heo unsigned int cpu; 1866e22bee78STejun Heo 1867e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1868e22bee78STejun Heo return false; 1869e22bee78STejun Heo 1870e22bee78STejun Heo /* mayday mayday mayday */ 1871bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1872f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1873f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1874f3421797STejun Heo cpu = 0; 1875f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1876e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1877e22bee78STejun Heo return true; 1878e22bee78STejun Heo } 1879e22bee78STejun Heo 188063d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1881e22bee78STejun Heo { 188263d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 188363d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1884e22bee78STejun Heo struct work_struct *work; 1885e22bee78STejun Heo 1886e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1887e22bee78STejun Heo 188863d95a91STejun Heo if (need_to_create_worker(pool)) { 1889e22bee78STejun Heo /* 1890e22bee78STejun Heo * We've been trying to create a new worker but 1891e22bee78STejun Heo * haven't been successful. We might be hitting an 1892e22bee78STejun Heo * allocation deadlock. Send distress signals to 1893e22bee78STejun Heo * rescuers. 1894e22bee78STejun Heo */ 189563d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1896e22bee78STejun Heo send_mayday(work); 1897e22bee78STejun Heo } 1898e22bee78STejun Heo 1899e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1900e22bee78STejun Heo 190163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1902e22bee78STejun Heo } 1903e22bee78STejun Heo 1904e22bee78STejun Heo /** 1905e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 190663d95a91STejun Heo * @pool: pool to create a new worker for 1907e22bee78STejun Heo * 190863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1909e22bee78STejun Heo * have at least one idle worker on return from this function. If 1910e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 191163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1912e22bee78STejun Heo * possible allocation deadlock. 1913e22bee78STejun Heo * 1914e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1915e22bee78STejun Heo * may_start_working() true. 1916e22bee78STejun Heo * 1917e22bee78STejun Heo * LOCKING: 1918e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1919e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1920e22bee78STejun Heo * manager. 1921e22bee78STejun Heo * 1922e22bee78STejun Heo * RETURNS: 1923e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1924e22bee78STejun Heo * otherwise. 1925e22bee78STejun Heo */ 192663d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 192706bd6ebfSNamhyung Kim __releases(&gcwq->lock) 192806bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1929e22bee78STejun Heo { 193063d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 193163d95a91STejun Heo 193263d95a91STejun Heo if (!need_to_create_worker(pool)) 1933e22bee78STejun Heo return false; 1934e22bee78STejun Heo restart: 19359f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 19369f9c2364STejun Heo 1937e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 193863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1939e22bee78STejun Heo 1940e22bee78STejun Heo while (true) { 1941e22bee78STejun Heo struct worker *worker; 1942e22bee78STejun Heo 1943bc2ae0f5STejun Heo worker = create_worker(pool); 1944e22bee78STejun Heo if (worker) { 194563d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1946e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1947e22bee78STejun Heo start_worker(worker); 194863d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1949e22bee78STejun Heo return true; 1950e22bee78STejun Heo } 1951e22bee78STejun Heo 195263d95a91STejun Heo if (!need_to_create_worker(pool)) 1953e22bee78STejun Heo break; 1954e22bee78STejun Heo 1955e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1956e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19579f9c2364STejun Heo 195863d95a91STejun Heo if (!need_to_create_worker(pool)) 1959e22bee78STejun Heo break; 1960e22bee78STejun Heo } 1961e22bee78STejun Heo 196263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1963e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 196463d95a91STejun Heo if (need_to_create_worker(pool)) 1965e22bee78STejun Heo goto restart; 1966e22bee78STejun Heo return true; 1967e22bee78STejun Heo } 1968e22bee78STejun Heo 1969e22bee78STejun Heo /** 1970e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 197163d95a91STejun Heo * @pool: pool to destroy workers for 1972e22bee78STejun Heo * 197363d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1974e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1975e22bee78STejun Heo * 1976e22bee78STejun Heo * LOCKING: 1977e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1978e22bee78STejun Heo * multiple times. Called only from manager. 1979e22bee78STejun Heo * 1980e22bee78STejun Heo * RETURNS: 1981e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1982e22bee78STejun Heo * otherwise. 1983e22bee78STejun Heo */ 198463d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1985e22bee78STejun Heo { 1986e22bee78STejun Heo bool ret = false; 1987e22bee78STejun Heo 198863d95a91STejun Heo while (too_many_workers(pool)) { 1989e22bee78STejun Heo struct worker *worker; 1990e22bee78STejun Heo unsigned long expires; 1991e22bee78STejun Heo 199263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1993e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1994e22bee78STejun Heo 1995e22bee78STejun Heo if (time_before(jiffies, expires)) { 199663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1997e22bee78STejun Heo break; 1998e22bee78STejun Heo } 1999e22bee78STejun Heo 2000e22bee78STejun Heo destroy_worker(worker); 2001e22bee78STejun Heo ret = true; 2002e22bee78STejun Heo } 2003e22bee78STejun Heo 2004e22bee78STejun Heo return ret; 2005e22bee78STejun Heo } 2006e22bee78STejun Heo 2007e22bee78STejun Heo /** 2008e22bee78STejun Heo * manage_workers - manage worker pool 2009e22bee78STejun Heo * @worker: self 2010e22bee78STejun Heo * 2011e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 2012e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2013e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 2014e22bee78STejun Heo * 2015e22bee78STejun Heo * The caller can safely start processing works on false return. On 2016e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2017e22bee78STejun Heo * and may_start_working() is true. 2018e22bee78STejun Heo * 2019e22bee78STejun Heo * CONTEXT: 2020e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2021e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2022e22bee78STejun Heo * 2023e22bee78STejun Heo * RETURNS: 2024e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 2025e22bee78STejun Heo * some action was taken. 2026e22bee78STejun Heo */ 2027e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2028e22bee78STejun Heo { 202963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2030e22bee78STejun Heo bool ret = false; 2031e22bee78STejun Heo 203260373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 2033e22bee78STejun Heo return ret; 2034e22bee78STejun Heo 203511ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 2036e22bee78STejun Heo 2037e22bee78STejun Heo /* 2038e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 2039e22bee78STejun Heo * on return. 2040e22bee78STejun Heo */ 204163d95a91STejun Heo ret |= maybe_destroy_workers(pool); 204263d95a91STejun Heo ret |= maybe_create_worker(pool); 2043e22bee78STejun Heo 204460373152STejun Heo mutex_unlock(&pool->manager_mutex); 2045e22bee78STejun Heo return ret; 2046e22bee78STejun Heo } 2047e22bee78STejun Heo 2048a62428c0STejun Heo /** 2049a62428c0STejun Heo * process_one_work - process single work 2050c34056a3STejun Heo * @worker: self 2051a62428c0STejun Heo * @work: work to process 2052a62428c0STejun Heo * 2053a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2054a62428c0STejun Heo * process a single work including synchronization against and 2055a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2056a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2057a62428c0STejun Heo * call this function to process a work. 2058a62428c0STejun Heo * 2059a62428c0STejun Heo * CONTEXT: 20608b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 2061a62428c0STejun Heo */ 2062c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 206306bd6ebfSNamhyung Kim __releases(&gcwq->lock) 206406bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 20651da177e4SLinus Torvalds { 20667e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 2067bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2068bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2069c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 2070fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 20716bb49e59SDavid Howells work_func_t f = work->func; 207273f53c4aSTejun Heo int work_color; 20737e11629dSTejun Heo struct worker *collision; 20744e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20754e6045f1SJohannes Berg /* 2076a62428c0STejun Heo * It is permissible to free the struct work_struct from 2077a62428c0STejun Heo * inside the function that is called from it, this we need to 2078a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2079a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2080a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20814e6045f1SJohannes Berg */ 20824d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20834d82a1deSPeter Zijlstra 20844d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20854e6045f1SJohannes Berg #endif 20866fec10a1STejun Heo /* 20876fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20886fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 20896fec10a1STejun Heo * unbound or a disassociated gcwq. 20906fec10a1STejun Heo */ 209125511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 20926fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 209325511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 209425511a47STejun Heo 20957e11629dSTejun Heo /* 20967e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20977e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20987e11629dSTejun Heo * already processing the work. If so, defer the work to the 20997e11629dSTejun Heo * currently executing one. 21007e11629dSTejun Heo */ 21017e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 21027e11629dSTejun Heo if (unlikely(collision)) { 21037e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 21047e11629dSTejun Heo return; 21057e11629dSTejun Heo } 21061da177e4SLinus Torvalds 21078930cabaSTejun Heo /* claim and dequeue */ 21081da177e4SLinus Torvalds debug_work_deactivate(work); 2109c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 2110c34056a3STejun Heo worker->current_work = work; 21118cca0eeaSTejun Heo worker->current_cwq = cwq; 211273f53c4aSTejun Heo work_color = get_work_color(work); 21137a22ad75STejun Heo 2114a62428c0STejun Heo list_del_init(&work->entry); 2115a62428c0STejun Heo 2116649027d7STejun Heo /* 2117fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2118fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2119fb0e7bebSTejun Heo */ 2120fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2121fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2122fb0e7bebSTejun Heo 2123974271c4STejun Heo /* 2124974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 2125974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2126974271c4STejun Heo */ 212763d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 212863d95a91STejun Heo wake_up_worker(pool); 2129974271c4STejun Heo 21308930cabaSTejun Heo /* 21318930cabaSTejun Heo * Record the last CPU and clear PENDING. The following wmb is 21328930cabaSTejun Heo * paired with the implied mb in test_and_set_bit(PENDING) and 21338930cabaSTejun Heo * ensures all updates to @work made here are visible to and 21348930cabaSTejun Heo * precede any updates by the next PENDING owner. Also, clear 21358930cabaSTejun Heo * PENDING inside @gcwq->lock so that PENDING and queued state 21368930cabaSTejun Heo * changes happen together while IRQ is disabled. 21378930cabaSTejun Heo */ 21388930cabaSTejun Heo smp_wmb(); 21398930cabaSTejun Heo set_work_cpu_and_clear_pending(work, gcwq->cpu); 21401da177e4SLinus Torvalds 21418930cabaSTejun Heo spin_unlock_irq(&gcwq->lock); 2142959d1af8STejun Heo 2143e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 21443295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2145e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 214665f27f38SDavid Howells f(work); 2147e36c886aSArjan van de Ven /* 2148e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2149e36c886aSArjan van de Ven * point will only record its address. 2150e36c886aSArjan van de Ven */ 2151e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21523295f0efSIngo Molnar lock_map_release(&lockdep_map); 21533295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 21541da177e4SLinus Torvalds 2155d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2156d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2157d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2158a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2159d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2160d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2161d5abe669SPeter Zijlstra debug_show_held_locks(current); 2162d5abe669SPeter Zijlstra dump_stack(); 2163d5abe669SPeter Zijlstra } 2164d5abe669SPeter Zijlstra 21658b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2166a62428c0STejun Heo 2167fb0e7bebSTejun Heo /* clear cpu intensive status */ 2168fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2169fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2170fb0e7bebSTejun Heo 2171a62428c0STejun Heo /* we're done with it, release */ 2172c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2173c34056a3STejun Heo worker->current_work = NULL; 21748cca0eeaSTejun Heo worker->current_cwq = NULL; 21758a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds 2178affee4b2STejun Heo /** 2179affee4b2STejun Heo * process_scheduled_works - process scheduled works 2180affee4b2STejun Heo * @worker: self 2181affee4b2STejun Heo * 2182affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2183affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2184affee4b2STejun Heo * fetches a work from the top and executes it. 2185affee4b2STejun Heo * 2186affee4b2STejun Heo * CONTEXT: 21878b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2188affee4b2STejun Heo * multiple times. 2189affee4b2STejun Heo */ 2190affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21911da177e4SLinus Torvalds { 2192affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2193affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2194a62428c0STejun Heo struct work_struct, entry); 2195c34056a3STejun Heo process_one_work(worker, work); 2196a62428c0STejun Heo } 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21994690c4abSTejun Heo /** 22004690c4abSTejun Heo * worker_thread - the worker thread function 2201c34056a3STejun Heo * @__worker: self 22024690c4abSTejun Heo * 2203e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2204e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2205e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2206e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2207e22bee78STejun Heo * rescuer_thread(). 22084690c4abSTejun Heo */ 2209c34056a3STejun Heo static int worker_thread(void *__worker) 22101da177e4SLinus Torvalds { 2211c34056a3STejun Heo struct worker *worker = __worker; 2212bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2213bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 22141da177e4SLinus Torvalds 2215e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2216e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2217c8e55f36STejun Heo woke_up: 22188b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2219affee4b2STejun Heo 222025511a47STejun Heo /* 222125511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 222225511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 222325511a47STejun Heo */ 222425511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2225c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 222625511a47STejun Heo 222725511a47STejun Heo if (worker->flags & WORKER_DIE) { 2228e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2229c8e55f36STejun Heo return 0; 2230c8e55f36STejun Heo } 2231c8e55f36STejun Heo 223225511a47STejun Heo idle_worker_rebind(worker); 223325511a47STejun Heo goto woke_up; 223425511a47STejun Heo } 223525511a47STejun Heo 2236c8e55f36STejun Heo worker_leave_idle(worker); 2237db7bccf4STejun Heo recheck: 2238e22bee78STejun Heo /* no more worker necessary? */ 223963d95a91STejun Heo if (!need_more_worker(pool)) 2240e22bee78STejun Heo goto sleep; 2241e22bee78STejun Heo 2242e22bee78STejun Heo /* do we need to manage? */ 224363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2244e22bee78STejun Heo goto recheck; 2245e22bee78STejun Heo 2246c8e55f36STejun Heo /* 2247c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2248c8e55f36STejun Heo * preparing to process a work or actually processing it. 2249c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2250c8e55f36STejun Heo */ 2251c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2252c8e55f36STejun Heo 2253e22bee78STejun Heo /* 2254e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2255e22bee78STejun Heo * at least one idle worker or that someone else has already 2256e22bee78STejun Heo * assumed the manager role. 2257e22bee78STejun Heo */ 2258e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2259e22bee78STejun Heo 2260e22bee78STejun Heo do { 2261affee4b2STejun Heo struct work_struct *work = 2262bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2263affee4b2STejun Heo struct work_struct, entry); 2264affee4b2STejun Heo 2265c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2266affee4b2STejun Heo /* optimization path, not strictly necessary */ 2267affee4b2STejun Heo process_one_work(worker, work); 2268affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2269affee4b2STejun Heo process_scheduled_works(worker); 2270affee4b2STejun Heo } else { 2271c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2272affee4b2STejun Heo process_scheduled_works(worker); 2273affee4b2STejun Heo } 227463d95a91STejun Heo } while (keep_working(pool)); 2275affee4b2STejun Heo 2276e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2277d313dd85STejun Heo sleep: 227863d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2279e22bee78STejun Heo goto recheck; 2280d313dd85STejun Heo 2281c8e55f36STejun Heo /* 2282e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2283e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2284e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2285e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2286e22bee78STejun Heo * prevent losing any event. 2287c8e55f36STejun Heo */ 2288c8e55f36STejun Heo worker_enter_idle(worker); 2289c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 22908b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 22911da177e4SLinus Torvalds schedule(); 2292c8e55f36STejun Heo goto woke_up; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 2295e22bee78STejun Heo /** 2296e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2297e22bee78STejun Heo * @__wq: the associated workqueue 2298e22bee78STejun Heo * 2299e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2300e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2301e22bee78STejun Heo * 2302e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2303e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2304e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2305e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2306e22bee78STejun Heo * the problem rescuer solves. 2307e22bee78STejun Heo * 2308e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2309e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2310e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2311e22bee78STejun Heo * 2312e22bee78STejun Heo * This should happen rarely. 2313e22bee78STejun Heo */ 2314e22bee78STejun Heo static int rescuer_thread(void *__wq) 2315e22bee78STejun Heo { 2316e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2317e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2318e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2319f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2320e22bee78STejun Heo unsigned int cpu; 2321e22bee78STejun Heo 2322e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2323e22bee78STejun Heo repeat: 2324e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds if (kthread_should_stop()) 2327e22bee78STejun Heo return 0; 23281da177e4SLinus Torvalds 2329f3421797STejun Heo /* 2330f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2331f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2332f3421797STejun Heo */ 2333f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2334f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2335f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2336bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2337bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2338e22bee78STejun Heo struct work_struct *work, *n; 2339e22bee78STejun Heo 2340e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2341f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2342e22bee78STejun Heo 2343e22bee78STejun Heo /* migrate to the target cpu if possible */ 2344bd7bdd43STejun Heo rescuer->pool = pool; 2345e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2346e22bee78STejun Heo 2347e22bee78STejun Heo /* 2348e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2349e22bee78STejun Heo * process'em. 2350e22bee78STejun Heo */ 2351e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2352bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2353e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2354e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2355e22bee78STejun Heo 2356e22bee78STejun Heo process_scheduled_works(rescuer); 23577576958aSTejun Heo 23587576958aSTejun Heo /* 23597576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 23607576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23617576958aSTejun Heo * and stalling the execution. 23627576958aSTejun Heo */ 236363d95a91STejun Heo if (keep_working(pool)) 236463d95a91STejun Heo wake_up_worker(pool); 23657576958aSTejun Heo 2366e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 2369e22bee78STejun Heo schedule(); 2370e22bee78STejun Heo goto repeat; 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 2373fc2e4d70SOleg Nesterov struct wq_barrier { 2374fc2e4d70SOleg Nesterov struct work_struct work; 2375fc2e4d70SOleg Nesterov struct completion done; 2376fc2e4d70SOleg Nesterov }; 2377fc2e4d70SOleg Nesterov 2378fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2379fc2e4d70SOleg Nesterov { 2380fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2381fc2e4d70SOleg Nesterov complete(&barr->done); 2382fc2e4d70SOleg Nesterov } 2383fc2e4d70SOleg Nesterov 23844690c4abSTejun Heo /** 23854690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 23864690c4abSTejun Heo * @cwq: cwq to insert barrier into 23874690c4abSTejun Heo * @barr: wq_barrier to insert 2388affee4b2STejun Heo * @target: target work to attach @barr to 2389affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23904690c4abSTejun Heo * 2391affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2392affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2393affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2394affee4b2STejun Heo * cpu. 2395affee4b2STejun Heo * 2396affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2397affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2398affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2399affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2400affee4b2STejun Heo * after a work with LINKED flag set. 2401affee4b2STejun Heo * 2402affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2403affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 24044690c4abSTejun Heo * 24054690c4abSTejun Heo * CONTEXT: 24068b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 24074690c4abSTejun Heo */ 240883c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2409affee4b2STejun Heo struct wq_barrier *barr, 2410affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2411fc2e4d70SOleg Nesterov { 2412affee4b2STejun Heo struct list_head *head; 2413affee4b2STejun Heo unsigned int linked = 0; 2414affee4b2STejun Heo 2415dc186ad7SThomas Gleixner /* 24168b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2417dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2418dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2419dc186ad7SThomas Gleixner * might deadlock. 2420dc186ad7SThomas Gleixner */ 2421ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 242222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2423fc2e4d70SOleg Nesterov init_completion(&barr->done); 242483c22520SOleg Nesterov 2425affee4b2STejun Heo /* 2426affee4b2STejun Heo * If @target is currently being executed, schedule the 2427affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2428affee4b2STejun Heo */ 2429affee4b2STejun Heo if (worker) 2430affee4b2STejun Heo head = worker->scheduled.next; 2431affee4b2STejun Heo else { 2432affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2433affee4b2STejun Heo 2434affee4b2STejun Heo head = target->entry.next; 2435affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2436affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2437affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2438affee4b2STejun Heo } 2439affee4b2STejun Heo 2440dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2441affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2442affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2443fc2e4d70SOleg Nesterov } 2444fc2e4d70SOleg Nesterov 244573f53c4aSTejun Heo /** 244673f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 244773f53c4aSTejun Heo * @wq: workqueue being flushed 244873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 244973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 245073f53c4aSTejun Heo * 245173f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 245273f53c4aSTejun Heo * 245373f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 245473f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 245573f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 245673f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 245773f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 245873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 245973f53c4aSTejun Heo * 246073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 246173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 246273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 246373f53c4aSTejun Heo * is returned. 246473f53c4aSTejun Heo * 246573f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 246673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 246773f53c4aSTejun Heo * advanced to @work_color. 246873f53c4aSTejun Heo * 246973f53c4aSTejun Heo * CONTEXT: 247073f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 247173f53c4aSTejun Heo * 247273f53c4aSTejun Heo * RETURNS: 247373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 247473f53c4aSTejun Heo * otherwise. 247573f53c4aSTejun Heo */ 247673f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 247773f53c4aSTejun Heo int flush_color, int work_color) 24781da177e4SLinus Torvalds { 247973f53c4aSTejun Heo bool wait = false; 248073f53c4aSTejun Heo unsigned int cpu; 24811da177e4SLinus Torvalds 248273f53c4aSTejun Heo if (flush_color >= 0) { 248373f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 248473f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2485dc186ad7SThomas Gleixner } 248614441960SOleg Nesterov 2487f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 248873f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2489bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 24901da177e4SLinus Torvalds 24918b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 249273f53c4aSTejun Heo 249373f53c4aSTejun Heo if (flush_color >= 0) { 249473f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 249573f53c4aSTejun Heo 249673f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 249773f53c4aSTejun Heo cwq->flush_color = flush_color; 249873f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 249973f53c4aSTejun Heo wait = true; 25001da177e4SLinus Torvalds } 250173f53c4aSTejun Heo } 250273f53c4aSTejun Heo 250373f53c4aSTejun Heo if (work_color >= 0) { 250473f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 250573f53c4aSTejun Heo cwq->work_color = work_color; 250673f53c4aSTejun Heo } 250773f53c4aSTejun Heo 25088b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds 251173f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 251273f53c4aSTejun Heo complete(&wq->first_flusher->done); 251373f53c4aSTejun Heo 251473f53c4aSTejun Heo return wait; 251583c22520SOleg Nesterov } 25161da177e4SLinus Torvalds 25170fcb78c2SRolf Eike Beer /** 25181da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25190fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25201da177e4SLinus Torvalds * 25211da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 25221da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 25231da177e4SLinus Torvalds * 2524fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2525fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 25261da177e4SLinus Torvalds */ 25277ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25281da177e4SLinus Torvalds { 252973f53c4aSTejun Heo struct wq_flusher this_flusher = { 253073f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 253173f53c4aSTejun Heo .flush_color = -1, 253273f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 253373f53c4aSTejun Heo }; 253473f53c4aSTejun Heo int next_color; 2535b1f4ec17SOleg Nesterov 25363295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25373295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 253873f53c4aSTejun Heo 253973f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 254073f53c4aSTejun Heo 254173f53c4aSTejun Heo /* 254273f53c4aSTejun Heo * Start-to-wait phase 254373f53c4aSTejun Heo */ 254473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 254573f53c4aSTejun Heo 254673f53c4aSTejun Heo if (next_color != wq->flush_color) { 254773f53c4aSTejun Heo /* 254873f53c4aSTejun Heo * Color space is not full. The current work_color 254973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 255073f53c4aSTejun Heo * by one. 255173f53c4aSTejun Heo */ 255273f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 255373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 255473f53c4aSTejun Heo wq->work_color = next_color; 255573f53c4aSTejun Heo 255673f53c4aSTejun Heo if (!wq->first_flusher) { 255773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 255873f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 255973f53c4aSTejun Heo 256073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 256173f53c4aSTejun Heo 256273f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 256373f53c4aSTejun Heo wq->work_color)) { 256473f53c4aSTejun Heo /* nothing to flush, done */ 256573f53c4aSTejun Heo wq->flush_color = next_color; 256673f53c4aSTejun Heo wq->first_flusher = NULL; 256773f53c4aSTejun Heo goto out_unlock; 256873f53c4aSTejun Heo } 256973f53c4aSTejun Heo } else { 257073f53c4aSTejun Heo /* wait in queue */ 257173f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 257273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 257373f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 257473f53c4aSTejun Heo } 257573f53c4aSTejun Heo } else { 257673f53c4aSTejun Heo /* 257773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 257873f53c4aSTejun Heo * The next flush completion will assign us 257973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 258073f53c4aSTejun Heo */ 258173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 258273f53c4aSTejun Heo } 258373f53c4aSTejun Heo 258473f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 258573f53c4aSTejun Heo 258673f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 258773f53c4aSTejun Heo 258873f53c4aSTejun Heo /* 258973f53c4aSTejun Heo * Wake-up-and-cascade phase 259073f53c4aSTejun Heo * 259173f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 259273f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 259373f53c4aSTejun Heo */ 259473f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 259573f53c4aSTejun Heo return; 259673f53c4aSTejun Heo 259773f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 259873f53c4aSTejun Heo 25994ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26004ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26014ce48b37STejun Heo goto out_unlock; 26024ce48b37STejun Heo 260373f53c4aSTejun Heo wq->first_flusher = NULL; 260473f53c4aSTejun Heo 260573f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 260673f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 260773f53c4aSTejun Heo 260873f53c4aSTejun Heo while (true) { 260973f53c4aSTejun Heo struct wq_flusher *next, *tmp; 261073f53c4aSTejun Heo 261173f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 261273f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 261373f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 261473f53c4aSTejun Heo break; 261573f53c4aSTejun Heo list_del_init(&next->list); 261673f53c4aSTejun Heo complete(&next->done); 261773f53c4aSTejun Heo } 261873f53c4aSTejun Heo 261973f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 262073f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 262173f53c4aSTejun Heo 262273f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 262373f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 262473f53c4aSTejun Heo 262573f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 262673f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 262773f53c4aSTejun Heo /* 262873f53c4aSTejun Heo * Assign the same color to all overflowed 262973f53c4aSTejun Heo * flushers, advance work_color and append to 263073f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 263173f53c4aSTejun Heo * phase for these overflowed flushers. 263273f53c4aSTejun Heo */ 263373f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 263473f53c4aSTejun Heo tmp->flush_color = wq->work_color; 263573f53c4aSTejun Heo 263673f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 263773f53c4aSTejun Heo 263873f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 263973f53c4aSTejun Heo &wq->flusher_queue); 264073f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 264173f53c4aSTejun Heo } 264273f53c4aSTejun Heo 264373f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 264473f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 264573f53c4aSTejun Heo break; 264673f53c4aSTejun Heo } 264773f53c4aSTejun Heo 264873f53c4aSTejun Heo /* 264973f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 265073f53c4aSTejun Heo * the new first flusher and arm cwqs. 265173f53c4aSTejun Heo */ 265273f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 265373f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 265473f53c4aSTejun Heo 265573f53c4aSTejun Heo list_del_init(&next->list); 265673f53c4aSTejun Heo wq->first_flusher = next; 265773f53c4aSTejun Heo 265873f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 265973f53c4aSTejun Heo break; 266073f53c4aSTejun Heo 266173f53c4aSTejun Heo /* 266273f53c4aSTejun Heo * Meh... this color is already done, clear first 266373f53c4aSTejun Heo * flusher and repeat cascading. 266473f53c4aSTejun Heo */ 266573f53c4aSTejun Heo wq->first_flusher = NULL; 266673f53c4aSTejun Heo } 266773f53c4aSTejun Heo 266873f53c4aSTejun Heo out_unlock: 266973f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26701da177e4SLinus Torvalds } 2671ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26721da177e4SLinus Torvalds 26739c5a2ba7STejun Heo /** 26749c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26759c5a2ba7STejun Heo * @wq: workqueue to drain 26769c5a2ba7STejun Heo * 26779c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26789c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26799c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26809c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26819c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26829c5a2ba7STejun Heo * takes too long. 26839c5a2ba7STejun Heo */ 26849c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26859c5a2ba7STejun Heo { 26869c5a2ba7STejun Heo unsigned int flush_cnt = 0; 26879c5a2ba7STejun Heo unsigned int cpu; 26889c5a2ba7STejun Heo 26899c5a2ba7STejun Heo /* 26909c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26919c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 26929c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 26939c5a2ba7STejun Heo */ 26949c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26959c5a2ba7STejun Heo if (!wq->nr_drainers++) 26969c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 26979c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26989c5a2ba7STejun Heo reflush: 26999c5a2ba7STejun Heo flush_workqueue(wq); 27009c5a2ba7STejun Heo 27019c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 27029c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2703fa2563e4SThomas Tuttle bool drained; 27049c5a2ba7STejun Heo 2705bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2706fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2707bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2708fa2563e4SThomas Tuttle 2709fa2563e4SThomas Tuttle if (drained) 27109c5a2ba7STejun Heo continue; 27119c5a2ba7STejun Heo 27129c5a2ba7STejun Heo if (++flush_cnt == 10 || 27139c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 27149c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 27159c5a2ba7STejun Heo wq->name, flush_cnt); 27169c5a2ba7STejun Heo goto reflush; 27179c5a2ba7STejun Heo } 27189c5a2ba7STejun Heo 27199c5a2ba7STejun Heo spin_lock(&workqueue_lock); 27209c5a2ba7STejun Heo if (!--wq->nr_drainers) 27219c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 27229c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 27239c5a2ba7STejun Heo } 27249c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27259c5a2ba7STejun Heo 2726baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2727baf59022STejun Heo bool wait_executing) 2728baf59022STejun Heo { 2729baf59022STejun Heo struct worker *worker = NULL; 2730baf59022STejun Heo struct global_cwq *gcwq; 2731baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2732baf59022STejun Heo 2733baf59022STejun Heo might_sleep(); 2734baf59022STejun Heo gcwq = get_work_gcwq(work); 2735baf59022STejun Heo if (!gcwq) 2736baf59022STejun Heo return false; 2737baf59022STejun Heo 2738baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2739baf59022STejun Heo if (!list_empty(&work->entry)) { 2740baf59022STejun Heo /* 2741baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2742baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2743baf59022STejun Heo * are not going to wait. 2744baf59022STejun Heo */ 2745baf59022STejun Heo smp_rmb(); 2746baf59022STejun Heo cwq = get_work_cwq(work); 2747bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2748baf59022STejun Heo goto already_gone; 2749baf59022STejun Heo } else if (wait_executing) { 2750baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2751baf59022STejun Heo if (!worker) 2752baf59022STejun Heo goto already_gone; 2753baf59022STejun Heo cwq = worker->current_cwq; 2754baf59022STejun Heo } else 2755baf59022STejun Heo goto already_gone; 2756baf59022STejun Heo 2757baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2758baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2759baf59022STejun Heo 2760e159489bSTejun Heo /* 2761e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2762e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2763e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2764e159489bSTejun Heo * access. 2765e159489bSTejun Heo */ 2766e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2767baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2768e159489bSTejun Heo else 2769e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2770baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2771e159489bSTejun Heo 2772baf59022STejun Heo return true; 2773baf59022STejun Heo already_gone: 2774baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2775baf59022STejun Heo return false; 2776baf59022STejun Heo } 2777baf59022STejun Heo 2778db700897SOleg Nesterov /** 2779401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2780401a8d04STejun Heo * @work: the work to flush 2781db700897SOleg Nesterov * 2782401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2783401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2784401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2785401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2786401a8d04STejun Heo * some of the CPUs from earlier queueing. 2787a67da70dSOleg Nesterov * 2788401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2789401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2790401a8d04STejun Heo * been requeued since flush started. 2791401a8d04STejun Heo * 2792401a8d04STejun Heo * RETURNS: 2793401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2794401a8d04STejun Heo * %false if it was already idle. 2795db700897SOleg Nesterov */ 2796401a8d04STejun Heo bool flush_work(struct work_struct *work) 2797db700897SOleg Nesterov { 2798db700897SOleg Nesterov struct wq_barrier barr; 2799db700897SOleg Nesterov 28000976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 28010976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 28020976dfc1SStephen Boyd 2803baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2804db700897SOleg Nesterov wait_for_completion(&barr.done); 2805dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2806401a8d04STejun Heo return true; 2807baf59022STejun Heo } else 2808401a8d04STejun Heo return false; 2809db700897SOleg Nesterov } 2810db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2811db700897SOleg Nesterov 2812401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2813401a8d04STejun Heo { 2814401a8d04STejun Heo struct wq_barrier barr; 2815401a8d04STejun Heo struct worker *worker; 2816401a8d04STejun Heo 2817401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2818401a8d04STejun Heo 2819401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2820401a8d04STejun Heo if (unlikely(worker)) 2821401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2822401a8d04STejun Heo 2823401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2824401a8d04STejun Heo 2825401a8d04STejun Heo if (unlikely(worker)) { 2826401a8d04STejun Heo wait_for_completion(&barr.done); 2827401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2828401a8d04STejun Heo return true; 2829401a8d04STejun Heo } else 2830401a8d04STejun Heo return false; 2831401a8d04STejun Heo } 2832401a8d04STejun Heo 2833401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2834401a8d04STejun Heo { 2835401a8d04STejun Heo bool ret = false; 2836401a8d04STejun Heo int cpu; 2837401a8d04STejun Heo 2838401a8d04STejun Heo might_sleep(); 2839401a8d04STejun Heo 2840401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2841401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2842401a8d04STejun Heo 2843401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2844401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2845401a8d04STejun Heo return ret; 2846401a8d04STejun Heo } 2847401a8d04STejun Heo 284809383498STejun Heo /** 284909383498STejun Heo * flush_work_sync - wait until a work has finished execution 285009383498STejun Heo * @work: the work to flush 285109383498STejun Heo * 285209383498STejun Heo * Wait until @work has finished execution. On return, it's 285309383498STejun Heo * guaranteed that all queueing instances of @work which happened 285409383498STejun Heo * before this function is called are finished. In other words, if 285509383498STejun Heo * @work hasn't been requeued since this function was called, @work is 285609383498STejun Heo * guaranteed to be idle on return. 285709383498STejun Heo * 285809383498STejun Heo * RETURNS: 285909383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 286009383498STejun Heo * %false if it was already idle. 286109383498STejun Heo */ 286209383498STejun Heo bool flush_work_sync(struct work_struct *work) 286309383498STejun Heo { 286409383498STejun Heo struct wq_barrier barr; 286509383498STejun Heo bool pending, waited; 286609383498STejun Heo 286709383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 286809383498STejun Heo pending = start_flush_work(work, &barr, false); 286909383498STejun Heo 287009383498STejun Heo /* wait for executions to finish */ 287109383498STejun Heo waited = wait_on_work(work); 287209383498STejun Heo 287309383498STejun Heo /* wait for the pending one */ 287409383498STejun Heo if (pending) { 287509383498STejun Heo wait_for_completion(&barr.done); 287609383498STejun Heo destroy_work_on_stack(&barr.work); 287709383498STejun Heo } 287809383498STejun Heo 287909383498STejun Heo return pending || waited; 288009383498STejun Heo } 288109383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 288209383498STejun Heo 288336e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 28841f1f642eSOleg Nesterov { 2885*bbb68dfaSTejun Heo unsigned long flags; 28861f1f642eSOleg Nesterov int ret; 28871f1f642eSOleg Nesterov 28881f1f642eSOleg Nesterov do { 2889*bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2890*bbb68dfaSTejun Heo /* 2891*bbb68dfaSTejun Heo * If someone else is canceling, wait for the same event it 2892*bbb68dfaSTejun Heo * would be waiting for before retrying. 2893*bbb68dfaSTejun Heo */ 2894*bbb68dfaSTejun Heo if (unlikely(ret == -ENOENT)) 28951f1f642eSOleg Nesterov wait_on_work(work); 28961f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28971f1f642eSOleg Nesterov 2898*bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2899*bbb68dfaSTejun Heo mark_work_canceling(work); 2900*bbb68dfaSTejun Heo local_irq_restore(flags); 2901*bbb68dfaSTejun Heo 2902*bbb68dfaSTejun Heo wait_on_work(work); 29037a22ad75STejun Heo clear_work_data(work); 29041f1f642eSOleg Nesterov return ret; 29051f1f642eSOleg Nesterov } 29061f1f642eSOleg Nesterov 29076e84d644SOleg Nesterov /** 2908401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2909401a8d04STejun Heo * @work: the work to cancel 29106e84d644SOleg Nesterov * 2911401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2912401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2913401a8d04STejun Heo * another workqueue. On return from this function, @work is 2914401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 29151f1f642eSOleg Nesterov * 2916401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2917401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 29186e84d644SOleg Nesterov * 2919401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 29206e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2921401a8d04STejun Heo * 2922401a8d04STejun Heo * RETURNS: 2923401a8d04STejun Heo * %true if @work was pending, %false otherwise. 29246e84d644SOleg Nesterov */ 2925401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 29266e84d644SOleg Nesterov { 292736e227d2STejun Heo return __cancel_work_timer(work, false); 2928b89deed3SOleg Nesterov } 292928e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2930b89deed3SOleg Nesterov 29316e84d644SOleg Nesterov /** 2932401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2933401a8d04STejun Heo * @dwork: the delayed work to flush 29346e84d644SOleg Nesterov * 2935401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2936401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2937401a8d04STejun Heo * considers the last queueing instance of @dwork. 29381f1f642eSOleg Nesterov * 2939401a8d04STejun Heo * RETURNS: 2940401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2941401a8d04STejun Heo * %false if it was already idle. 29426e84d644SOleg Nesterov */ 2943401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2944401a8d04STejun Heo { 29458930cabaSTejun Heo local_irq_disable(); 2946401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 294757469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 2948401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 29498930cabaSTejun Heo local_irq_enable(); 2950401a8d04STejun Heo return flush_work(&dwork->work); 2951401a8d04STejun Heo } 2952401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2953401a8d04STejun Heo 2954401a8d04STejun Heo /** 295509383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 295609383498STejun Heo * @dwork: the delayed work to flush 295709383498STejun Heo * 295809383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 295909383498STejun Heo * execution immediately. Other than timer handling, its behavior 296009383498STejun Heo * is identical to flush_work_sync(). 296109383498STejun Heo * 296209383498STejun Heo * RETURNS: 296309383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 296409383498STejun Heo * %false if it was already idle. 296509383498STejun Heo */ 296609383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 296709383498STejun Heo { 29688930cabaSTejun Heo local_irq_disable(); 296909383498STejun Heo if (del_timer_sync(&dwork->timer)) 297057469821STejun Heo __queue_work(WORK_CPU_UNBOUND, 297109383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 29728930cabaSTejun Heo local_irq_enable(); 297309383498STejun Heo return flush_work_sync(&dwork->work); 297409383498STejun Heo } 297509383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 297609383498STejun Heo 297709383498STejun Heo /** 2978401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2979401a8d04STejun Heo * @dwork: the delayed work cancel 2980401a8d04STejun Heo * 2981401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2982401a8d04STejun Heo * 2983401a8d04STejun Heo * RETURNS: 2984401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2985401a8d04STejun Heo */ 2986401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29876e84d644SOleg Nesterov { 298836e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29896e84d644SOleg Nesterov } 2990f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29911da177e4SLinus Torvalds 2992d4283e93STejun Heo /** 29930a13c00eSTejun Heo * schedule_work_on - put work task on a specific cpu 29940a13c00eSTejun Heo * @cpu: cpu to put the work task on 29950a13c00eSTejun Heo * @work: job to be done 29960a13c00eSTejun Heo * 29970a13c00eSTejun Heo * This puts a job on a specific cpu 29980a13c00eSTejun Heo */ 2999d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 30000a13c00eSTejun Heo { 30010a13c00eSTejun Heo return queue_work_on(cpu, system_wq, work); 30020a13c00eSTejun Heo } 30030a13c00eSTejun Heo EXPORT_SYMBOL(schedule_work_on); 30040a13c00eSTejun Heo 30050fcb78c2SRolf Eike Beer /** 30060fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 30070fcb78c2SRolf Eike Beer * @work: job to be done 30080fcb78c2SRolf Eike Beer * 3009d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 3010d4283e93STejun Heo * %true otherwise. 30115b0f437dSBart Van Assche * 30125b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 30135b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 30145b0f437dSBart Van Assche * workqueue otherwise. 30150fcb78c2SRolf Eike Beer */ 3016d4283e93STejun Heo bool schedule_work(struct work_struct *work) 30171da177e4SLinus Torvalds { 3018d320c038STejun Heo return queue_work(system_wq, work); 30191da177e4SLinus Torvalds } 3020ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 30211da177e4SLinus Torvalds 30220fcb78c2SRolf Eike Beer /** 30230fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 30240fcb78c2SRolf Eike Beer * @cpu: cpu to use 302552bad64dSDavid Howells * @dwork: job to be done 30260fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 30270fcb78c2SRolf Eike Beer * 30280fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 30290fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 30300fcb78c2SRolf Eike Beer */ 3031d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 3032d4283e93STejun Heo unsigned long delay) 30331da177e4SLinus Torvalds { 3034d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 30351da177e4SLinus Torvalds } 3036ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 30371da177e4SLinus Torvalds 3038b6136773SAndrew Morton /** 30390a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 30400a13c00eSTejun Heo * @dwork: job to be done 30410a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 30420a13c00eSTejun Heo * 30430a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 30440a13c00eSTejun Heo * workqueue. 30450a13c00eSTejun Heo */ 3046d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 30470a13c00eSTejun Heo { 30480a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 30490a13c00eSTejun Heo } 30500a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 30510a13c00eSTejun Heo 30520a13c00eSTejun Heo /** 305331ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3054b6136773SAndrew Morton * @func: the function to call 3055b6136773SAndrew Morton * 305631ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 305731ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3058b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 305931ddd871STejun Heo * 306031ddd871STejun Heo * RETURNS: 306131ddd871STejun Heo * 0 on success, -errno on failure. 3062b6136773SAndrew Morton */ 306365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 306415316ba8SChristoph Lameter { 306515316ba8SChristoph Lameter int cpu; 306638f51568SNamhyung Kim struct work_struct __percpu *works; 306715316ba8SChristoph Lameter 3068b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3069b6136773SAndrew Morton if (!works) 307015316ba8SChristoph Lameter return -ENOMEM; 3071b6136773SAndrew Morton 307295402b38SGautham R Shenoy get_online_cpus(); 307393981800STejun Heo 307415316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30759bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30769bfb1839SIngo Molnar 30779bfb1839SIngo Molnar INIT_WORK(work, func); 30788de6d308SOleg Nesterov schedule_work_on(cpu, work); 307915316ba8SChristoph Lameter } 308093981800STejun Heo 308193981800STejun Heo for_each_online_cpu(cpu) 30828616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 308393981800STejun Heo 308495402b38SGautham R Shenoy put_online_cpus(); 3085b6136773SAndrew Morton free_percpu(works); 308615316ba8SChristoph Lameter return 0; 308715316ba8SChristoph Lameter } 308815316ba8SChristoph Lameter 3089eef6a7d5SAlan Stern /** 3090eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3091eef6a7d5SAlan Stern * 3092eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3093eef6a7d5SAlan Stern * completion. 3094eef6a7d5SAlan Stern * 3095eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3096eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3097eef6a7d5SAlan Stern * will lead to deadlock: 3098eef6a7d5SAlan Stern * 3099eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3100eef6a7d5SAlan Stern * a lock held by your code or its caller. 3101eef6a7d5SAlan Stern * 3102eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3103eef6a7d5SAlan Stern * 3104eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3105eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3106eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3107eef6a7d5SAlan Stern * 3108eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3109eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3110eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3111eef6a7d5SAlan Stern * cancel_work_sync() instead. 3112eef6a7d5SAlan Stern */ 31131da177e4SLinus Torvalds void flush_scheduled_work(void) 31141da177e4SLinus Torvalds { 3115d320c038STejun Heo flush_workqueue(system_wq); 31161da177e4SLinus Torvalds } 3117ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds /** 31201fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 31211fa44ecaSJames Bottomley * @fn: the function to execute 31221fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 31231fa44ecaSJames Bottomley * be available when the work executes) 31241fa44ecaSJames Bottomley * 31251fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 31261fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 31271fa44ecaSJames Bottomley * 31281fa44ecaSJames Bottomley * Returns: 0 - function was executed 31291fa44ecaSJames Bottomley * 1 - function was scheduled for execution 31301fa44ecaSJames Bottomley */ 313165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 31321fa44ecaSJames Bottomley { 31331fa44ecaSJames Bottomley if (!in_interrupt()) { 313465f27f38SDavid Howells fn(&ew->work); 31351fa44ecaSJames Bottomley return 0; 31361fa44ecaSJames Bottomley } 31371fa44ecaSJames Bottomley 313865f27f38SDavid Howells INIT_WORK(&ew->work, fn); 31391fa44ecaSJames Bottomley schedule_work(&ew->work); 31401fa44ecaSJames Bottomley 31411fa44ecaSJames Bottomley return 1; 31421fa44ecaSJames Bottomley } 31431fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 31441fa44ecaSJames Bottomley 31451da177e4SLinus Torvalds int keventd_up(void) 31461da177e4SLinus Torvalds { 3147d320c038STejun Heo return system_wq != NULL; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 3150bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 31511da177e4SLinus Torvalds { 31523af24433SOleg Nesterov /* 31530f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 31540f900049STejun Heo * Make sure that the alignment isn't lower than that of 31550f900049STejun Heo * unsigned long long. 31563af24433SOleg Nesterov */ 31570f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 31580f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 31590f900049STejun Heo __alignof__(unsigned long long)); 31603af24433SOleg Nesterov 3161e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3162f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3163931ac77eSTejun Heo else { 31640f900049STejun Heo void *ptr; 3165e1d8aa9fSFrederic Weisbecker 31660f900049STejun Heo /* 3167f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3168f3421797STejun Heo * pointer at the end pointing back to the originally 3169f3421797STejun Heo * allocated pointer which will be used for free. 31700f900049STejun Heo */ 3171bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3172bdbc5dd7STejun Heo if (ptr) { 3173bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3174bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3175bdbc5dd7STejun Heo } 31763af24433SOleg Nesterov } 31773af24433SOleg Nesterov 31780415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3179bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3180bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 31810f900049STejun Heo } 31820f900049STejun Heo 3183bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 318406ba38a9SOleg Nesterov { 3185e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3186bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3187f3421797STejun Heo else if (wq->cpu_wq.single) { 3188f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3189f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 319006ba38a9SOleg Nesterov } 319106ba38a9SOleg Nesterov } 319206ba38a9SOleg Nesterov 3193f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3194f3421797STejun Heo const char *name) 3195b71ab8c2STejun Heo { 3196f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3197f3421797STejun Heo 3198f3421797STejun Heo if (max_active < 1 || max_active > lim) 3199b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3200b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3201f3421797STejun Heo max_active, name, 1, lim); 3202b71ab8c2STejun Heo 3203f3421797STejun Heo return clamp_val(max_active, 1, lim); 3204b71ab8c2STejun Heo } 3205b71ab8c2STejun Heo 3206b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 320797e37d7bSTejun Heo unsigned int flags, 32081e19ffc6STejun Heo int max_active, 3209eb13ba87SJohannes Berg struct lock_class_key *key, 3210b196be89STejun Heo const char *lock_name, ...) 32113af24433SOleg Nesterov { 3212b196be89STejun Heo va_list args, args1; 32133af24433SOleg Nesterov struct workqueue_struct *wq; 3214c34056a3STejun Heo unsigned int cpu; 3215b196be89STejun Heo size_t namelen; 3216b196be89STejun Heo 3217b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3218b196be89STejun Heo va_start(args, lock_name); 3219b196be89STejun Heo va_copy(args1, args); 3220b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3221b196be89STejun Heo 3222b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3223b196be89STejun Heo if (!wq) 3224b196be89STejun Heo goto err; 3225b196be89STejun Heo 3226b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3227b196be89STejun Heo va_end(args); 3228b196be89STejun Heo va_end(args1); 32293af24433SOleg Nesterov 3230f3421797STejun Heo /* 32316370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 32326370a6adSTejun Heo * have a rescuer to guarantee forward progress. 32336370a6adSTejun Heo */ 32346370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 32356370a6adSTejun Heo flags |= WQ_RESCUER; 32366370a6adSTejun Heo 3237d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3238b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 32393af24433SOleg Nesterov 3240b196be89STejun Heo /* init wq */ 324197e37d7bSTejun Heo wq->flags = flags; 3242a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 324373f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 324473f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 324573f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 324673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 32473af24433SOleg Nesterov 3248eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3249cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 32503af24433SOleg Nesterov 3251bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3252bdbc5dd7STejun Heo goto err; 3253bdbc5dd7STejun Heo 3254f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 32551537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 32568b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 32573270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 32581537663fSTejun Heo 32590f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 32603270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3261c34056a3STejun Heo cwq->wq = wq; 326273f53c4aSTejun Heo cwq->flush_color = -1; 32631e19ffc6STejun Heo cwq->max_active = max_active; 32641e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3265e22bee78STejun Heo } 32661537663fSTejun Heo 3267e22bee78STejun Heo if (flags & WQ_RESCUER) { 3268e22bee78STejun Heo struct worker *rescuer; 3269e22bee78STejun Heo 3270f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3271e22bee78STejun Heo goto err; 3272e22bee78STejun Heo 3273e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3274e22bee78STejun Heo if (!rescuer) 3275e22bee78STejun Heo goto err; 3276e22bee78STejun Heo 3277b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3278b196be89STejun Heo wq->name); 3279e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3280e22bee78STejun Heo goto err; 3281e22bee78STejun Heo 3282e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3283e22bee78STejun Heo wake_up_process(rescuer->task); 32843af24433SOleg Nesterov } 32851537663fSTejun Heo 32863af24433SOleg Nesterov /* 3287a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3288a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3289a0a1a5fdSTejun Heo * workqueue to workqueues list. 32903af24433SOleg Nesterov */ 32913af24433SOleg Nesterov spin_lock(&workqueue_lock); 3292a0a1a5fdSTejun Heo 329358a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3294f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3295a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3296a0a1a5fdSTejun Heo 32973af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3298a0a1a5fdSTejun Heo 32993af24433SOleg Nesterov spin_unlock(&workqueue_lock); 33003af24433SOleg Nesterov 33013af24433SOleg Nesterov return wq; 33024690c4abSTejun Heo err: 33034690c4abSTejun Heo if (wq) { 3304bdbc5dd7STejun Heo free_cwqs(wq); 3305f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3306e22bee78STejun Heo kfree(wq->rescuer); 33074690c4abSTejun Heo kfree(wq); 33083af24433SOleg Nesterov } 33094690c4abSTejun Heo return NULL; 33101da177e4SLinus Torvalds } 3311d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 33121da177e4SLinus Torvalds 33133af24433SOleg Nesterov /** 33143af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 33153af24433SOleg Nesterov * @wq: target workqueue 33163af24433SOleg Nesterov * 33173af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 33183af24433SOleg Nesterov */ 33193af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 33203af24433SOleg Nesterov { 3321c8e55f36STejun Heo unsigned int cpu; 33223af24433SOleg Nesterov 33239c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 33249c5a2ba7STejun Heo drain_workqueue(wq); 3325c8efcc25STejun Heo 3326a0a1a5fdSTejun Heo /* 3327a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3328a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3329a0a1a5fdSTejun Heo */ 333095402b38SGautham R Shenoy spin_lock(&workqueue_lock); 33313af24433SOleg Nesterov list_del(&wq->list); 333295402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 33333af24433SOleg Nesterov 3334e22bee78STejun Heo /* sanity check */ 3335f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 333673f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 333773f53c4aSTejun Heo int i; 33383af24433SOleg Nesterov 333973f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 334073f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 33411e19ffc6STejun Heo BUG_ON(cwq->nr_active); 33421e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 334373f53c4aSTejun Heo } 33441537663fSTejun Heo 3345e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3346e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3347f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 33488d9df9f0SXiaotian Feng kfree(wq->rescuer); 3349e22bee78STejun Heo } 3350e22bee78STejun Heo 3351bdbc5dd7STejun Heo free_cwqs(wq); 33523af24433SOleg Nesterov kfree(wq); 33533af24433SOleg Nesterov } 33543af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 33553af24433SOleg Nesterov 3356dcd989cbSTejun Heo /** 3357dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3358dcd989cbSTejun Heo * @wq: target workqueue 3359dcd989cbSTejun Heo * @max_active: new max_active value. 3360dcd989cbSTejun Heo * 3361dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3362dcd989cbSTejun Heo * 3363dcd989cbSTejun Heo * CONTEXT: 3364dcd989cbSTejun Heo * Don't call from IRQ context. 3365dcd989cbSTejun Heo */ 3366dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3367dcd989cbSTejun Heo { 3368dcd989cbSTejun Heo unsigned int cpu; 3369dcd989cbSTejun Heo 3370f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3371dcd989cbSTejun Heo 3372dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3373dcd989cbSTejun Heo 3374dcd989cbSTejun Heo wq->saved_max_active = max_active; 3375dcd989cbSTejun Heo 3376f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3377dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3378dcd989cbSTejun Heo 3379dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3380dcd989cbSTejun Heo 338158a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3382dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3383dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3384dcd989cbSTejun Heo 3385dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3386dcd989cbSTejun Heo } 3387dcd989cbSTejun Heo 3388dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3389dcd989cbSTejun Heo } 3390dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3391dcd989cbSTejun Heo 3392dcd989cbSTejun Heo /** 3393dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3394dcd989cbSTejun Heo * @cpu: CPU in question 3395dcd989cbSTejun Heo * @wq: target workqueue 3396dcd989cbSTejun Heo * 3397dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3398dcd989cbSTejun Heo * no synchronization around this function and the test result is 3399dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3400dcd989cbSTejun Heo * 3401dcd989cbSTejun Heo * RETURNS: 3402dcd989cbSTejun Heo * %true if congested, %false otherwise. 3403dcd989cbSTejun Heo */ 3404dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3405dcd989cbSTejun Heo { 3406dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3407dcd989cbSTejun Heo 3408dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3409dcd989cbSTejun Heo } 3410dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3411dcd989cbSTejun Heo 3412dcd989cbSTejun Heo /** 3413dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3414dcd989cbSTejun Heo * @work: the work of interest 3415dcd989cbSTejun Heo * 3416dcd989cbSTejun Heo * RETURNS: 3417bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3418dcd989cbSTejun Heo */ 3419dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3420dcd989cbSTejun Heo { 3421dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3422dcd989cbSTejun Heo 3423bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3424dcd989cbSTejun Heo } 3425dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3426dcd989cbSTejun Heo 3427dcd989cbSTejun Heo /** 3428dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3429dcd989cbSTejun Heo * @work: the work to be tested 3430dcd989cbSTejun Heo * 3431dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3432dcd989cbSTejun Heo * synchronization around this function and the test result is 3433dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3434dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3435dcd989cbSTejun Heo * running state. 3436dcd989cbSTejun Heo * 3437dcd989cbSTejun Heo * RETURNS: 3438dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3439dcd989cbSTejun Heo */ 3440dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3441dcd989cbSTejun Heo { 3442dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3443dcd989cbSTejun Heo unsigned long flags; 3444dcd989cbSTejun Heo unsigned int ret = 0; 3445dcd989cbSTejun Heo 3446dcd989cbSTejun Heo if (!gcwq) 3447dcd989cbSTejun Heo return false; 3448dcd989cbSTejun Heo 3449dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3450dcd989cbSTejun Heo 3451dcd989cbSTejun Heo if (work_pending(work)) 3452dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3453dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3454dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3455dcd989cbSTejun Heo 3456dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3457dcd989cbSTejun Heo 3458dcd989cbSTejun Heo return ret; 3459dcd989cbSTejun Heo } 3460dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3461dcd989cbSTejun Heo 3462db7bccf4STejun Heo /* 3463db7bccf4STejun Heo * CPU hotplug. 3464db7bccf4STejun Heo * 3465e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3466e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3467e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3468e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3469e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3470e22bee78STejun Heo * blocked draining impractical. 3471e22bee78STejun Heo * 3472628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3473628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3474628c78e7STejun Heo * cpu comes back online. 3475db7bccf4STejun Heo */ 3476db7bccf4STejun Heo 347760373152STejun Heo /* claim manager positions of all pools */ 34788db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 347960373152STejun Heo { 348060373152STejun Heo struct worker_pool *pool; 348160373152STejun Heo 348260373152STejun Heo for_each_worker_pool(pool, gcwq) 348360373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 34848db25e78STejun Heo spin_lock_irq(&gcwq->lock); 348560373152STejun Heo } 348660373152STejun Heo 348760373152STejun Heo /* release manager positions */ 34888db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 348960373152STejun Heo { 349060373152STejun Heo struct worker_pool *pool; 349160373152STejun Heo 34928db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 349360373152STejun Heo for_each_worker_pool(pool, gcwq) 349460373152STejun Heo mutex_unlock(&pool->manager_mutex); 349560373152STejun Heo } 349660373152STejun Heo 3497628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3498db7bccf4STejun Heo { 3499628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 35004ce62e9eSTejun Heo struct worker_pool *pool; 3501db7bccf4STejun Heo struct worker *worker; 3502db7bccf4STejun Heo struct hlist_node *pos; 3503db7bccf4STejun Heo int i; 3504db7bccf4STejun Heo 3505db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3506db7bccf4STejun Heo 35078db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3508e22bee78STejun Heo 3509f2d5a0eeSTejun Heo /* 3510f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3511f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3512f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3513f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3514f2d5a0eeSTejun Heo */ 351560373152STejun Heo for_each_worker_pool(pool, gcwq) 35164ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3517403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3518db7bccf4STejun Heo 3519db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3520403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3521db7bccf4STejun Heo 3522f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3523f2d5a0eeSTejun Heo 35248db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3525e22bee78STejun Heo 3526e22bee78STejun Heo /* 3527628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3528628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3529628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3530628c78e7STejun Heo */ 3531628c78e7STejun Heo schedule(); 3532628c78e7STejun Heo 3533628c78e7STejun Heo /* 3534628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3535628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3536628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3537628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3538628c78e7STejun Heo * which is served by workers tied to the CPU. 3539628c78e7STejun Heo * 3540628c78e7STejun Heo * On return from this function, the current worker would trigger 3541628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3542628c78e7STejun Heo * didn't already. 3543e22bee78STejun Heo */ 35444ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 35454ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3546db7bccf4STejun Heo } 3547db7bccf4STejun Heo 35488db25e78STejun Heo /* 35498db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 35508db25e78STejun Heo * This will be registered high priority CPU notifier. 35518db25e78STejun Heo */ 35528db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 35531da177e4SLinus Torvalds unsigned long action, 35541da177e4SLinus Torvalds void *hcpu) 35551da177e4SLinus Torvalds { 35563af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3557db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 35584ce62e9eSTejun Heo struct worker_pool *pool; 35591da177e4SLinus Torvalds 35608db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 35613af24433SOleg Nesterov case CPU_UP_PREPARE: 35624ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 35633ce63377STejun Heo struct worker *worker; 35643ce63377STejun Heo 35653ce63377STejun Heo if (pool->nr_workers) 35663ce63377STejun Heo continue; 35673ce63377STejun Heo 35683ce63377STejun Heo worker = create_worker(pool); 35693ce63377STejun Heo if (!worker) 35703ce63377STejun Heo return NOTIFY_BAD; 35713ce63377STejun Heo 35723ce63377STejun Heo spin_lock_irq(&gcwq->lock); 35733ce63377STejun Heo start_worker(worker); 35743ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 35753af24433SOleg Nesterov } 35761da177e4SLinus Torvalds break; 35771da177e4SLinus Torvalds 357865758202STejun Heo case CPU_DOWN_FAILED: 357965758202STejun Heo case CPU_ONLINE: 35808db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 35818db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 35828db25e78STejun Heo rebind_workers(gcwq); 35838db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 35848db25e78STejun Heo break; 358565758202STejun Heo } 358665758202STejun Heo return NOTIFY_OK; 358765758202STejun Heo } 358865758202STejun Heo 358965758202STejun Heo /* 359065758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 359165758202STejun Heo * This will be registered as low priority CPU notifier. 359265758202STejun Heo */ 359365758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 359465758202STejun Heo unsigned long action, 359565758202STejun Heo void *hcpu) 359665758202STejun Heo { 35978db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35988db25e78STejun Heo struct work_struct unbind_work; 35998db25e78STejun Heo 360065758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 360165758202STejun Heo case CPU_DOWN_PREPARE: 36028db25e78STejun Heo /* unbinding should happen on the local CPU */ 36038db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 36048db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 36058db25e78STejun Heo flush_work(&unbind_work); 36068db25e78STejun Heo break; 360765758202STejun Heo } 360865758202STejun Heo return NOTIFY_OK; 360965758202STejun Heo } 361065758202STejun Heo 36112d3854a3SRusty Russell #ifdef CONFIG_SMP 36128ccad40dSRusty Russell 36132d3854a3SRusty Russell struct work_for_cpu { 36146b44003eSAndrew Morton struct completion completion; 36152d3854a3SRusty Russell long (*fn)(void *); 36162d3854a3SRusty Russell void *arg; 36172d3854a3SRusty Russell long ret; 36182d3854a3SRusty Russell }; 36192d3854a3SRusty Russell 36206b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 36212d3854a3SRusty Russell { 36226b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 36232d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 36246b44003eSAndrew Morton complete(&wfc->completion); 36256b44003eSAndrew Morton return 0; 36262d3854a3SRusty Russell } 36272d3854a3SRusty Russell 36282d3854a3SRusty Russell /** 36292d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 36302d3854a3SRusty Russell * @cpu: the cpu to run on 36312d3854a3SRusty Russell * @fn: the function to run 36322d3854a3SRusty Russell * @arg: the function arg 36332d3854a3SRusty Russell * 363431ad9081SRusty Russell * This will return the value @fn returns. 363531ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 36366b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 36372d3854a3SRusty Russell */ 36382d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 36392d3854a3SRusty Russell { 36406b44003eSAndrew Morton struct task_struct *sub_thread; 36416b44003eSAndrew Morton struct work_for_cpu wfc = { 36426b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 36436b44003eSAndrew Morton .fn = fn, 36446b44003eSAndrew Morton .arg = arg, 36456b44003eSAndrew Morton }; 36462d3854a3SRusty Russell 36476b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 36486b44003eSAndrew Morton if (IS_ERR(sub_thread)) 36496b44003eSAndrew Morton return PTR_ERR(sub_thread); 36506b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 36516b44003eSAndrew Morton wake_up_process(sub_thread); 36526b44003eSAndrew Morton wait_for_completion(&wfc.completion); 36532d3854a3SRusty Russell return wfc.ret; 36542d3854a3SRusty Russell } 36552d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 36562d3854a3SRusty Russell #endif /* CONFIG_SMP */ 36572d3854a3SRusty Russell 3658a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3659e7577c50SRusty Russell 3660a0a1a5fdSTejun Heo /** 3661a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3662a0a1a5fdSTejun Heo * 366358a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 366458a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 366558a69cb4STejun Heo * gcwq->worklist. 3666a0a1a5fdSTejun Heo * 3667a0a1a5fdSTejun Heo * CONTEXT: 36688b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3669a0a1a5fdSTejun Heo */ 3670a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3671a0a1a5fdSTejun Heo { 3672a0a1a5fdSTejun Heo unsigned int cpu; 3673a0a1a5fdSTejun Heo 3674a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3675a0a1a5fdSTejun Heo 3676a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3677a0a1a5fdSTejun Heo workqueue_freezing = true; 3678a0a1a5fdSTejun Heo 3679f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36808b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3681bdbc5dd7STejun Heo struct workqueue_struct *wq; 36828b03ae3cSTejun Heo 36838b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36848b03ae3cSTejun Heo 3685db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3686db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3687db7bccf4STejun Heo 3688a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3689a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3690a0a1a5fdSTejun Heo 369158a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3692a0a1a5fdSTejun Heo cwq->max_active = 0; 36931da177e4SLinus Torvalds } 36948b03ae3cSTejun Heo 36958b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3696a0a1a5fdSTejun Heo } 3697a0a1a5fdSTejun Heo 3698a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3699a0a1a5fdSTejun Heo } 3700a0a1a5fdSTejun Heo 3701a0a1a5fdSTejun Heo /** 370258a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3703a0a1a5fdSTejun Heo * 3704a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3705a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3706a0a1a5fdSTejun Heo * 3707a0a1a5fdSTejun Heo * CONTEXT: 3708a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3709a0a1a5fdSTejun Heo * 3710a0a1a5fdSTejun Heo * RETURNS: 371158a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 371258a69cb4STejun Heo * is complete. 3713a0a1a5fdSTejun Heo */ 3714a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3715a0a1a5fdSTejun Heo { 3716a0a1a5fdSTejun Heo unsigned int cpu; 3717a0a1a5fdSTejun Heo bool busy = false; 3718a0a1a5fdSTejun Heo 3719a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3720a0a1a5fdSTejun Heo 3721a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3722a0a1a5fdSTejun Heo 3723f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3724bdbc5dd7STejun Heo struct workqueue_struct *wq; 3725a0a1a5fdSTejun Heo /* 3726a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3727a0a1a5fdSTejun Heo * to peek without lock. 3728a0a1a5fdSTejun Heo */ 3729a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3730a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3731a0a1a5fdSTejun Heo 373258a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3733a0a1a5fdSTejun Heo continue; 3734a0a1a5fdSTejun Heo 3735a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3736a0a1a5fdSTejun Heo if (cwq->nr_active) { 3737a0a1a5fdSTejun Heo busy = true; 3738a0a1a5fdSTejun Heo goto out_unlock; 3739a0a1a5fdSTejun Heo } 3740a0a1a5fdSTejun Heo } 3741a0a1a5fdSTejun Heo } 3742a0a1a5fdSTejun Heo out_unlock: 3743a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3744a0a1a5fdSTejun Heo return busy; 3745a0a1a5fdSTejun Heo } 3746a0a1a5fdSTejun Heo 3747a0a1a5fdSTejun Heo /** 3748a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3749a0a1a5fdSTejun Heo * 3750a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 37517e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3752a0a1a5fdSTejun Heo * 3753a0a1a5fdSTejun Heo * CONTEXT: 37548b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3755a0a1a5fdSTejun Heo */ 3756a0a1a5fdSTejun Heo void thaw_workqueues(void) 3757a0a1a5fdSTejun Heo { 3758a0a1a5fdSTejun Heo unsigned int cpu; 3759a0a1a5fdSTejun Heo 3760a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3761a0a1a5fdSTejun Heo 3762a0a1a5fdSTejun Heo if (!workqueue_freezing) 3763a0a1a5fdSTejun Heo goto out_unlock; 3764a0a1a5fdSTejun Heo 3765f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37668b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37674ce62e9eSTejun Heo struct worker_pool *pool; 3768bdbc5dd7STejun Heo struct workqueue_struct *wq; 37698b03ae3cSTejun Heo 37708b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 37718b03ae3cSTejun Heo 3772db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3773db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3774db7bccf4STejun Heo 3775a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3776a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3777a0a1a5fdSTejun Heo 377858a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3779a0a1a5fdSTejun Heo continue; 3780a0a1a5fdSTejun Heo 3781a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3782a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3783a0a1a5fdSTejun Heo 3784a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3785a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3786a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3787a0a1a5fdSTejun Heo } 37888b03ae3cSTejun Heo 37894ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37904ce62e9eSTejun Heo wake_up_worker(pool); 3791e22bee78STejun Heo 37928b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3793a0a1a5fdSTejun Heo } 3794a0a1a5fdSTejun Heo 3795a0a1a5fdSTejun Heo workqueue_freezing = false; 3796a0a1a5fdSTejun Heo out_unlock: 3797a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3798a0a1a5fdSTejun Heo } 3799a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3800a0a1a5fdSTejun Heo 38016ee0578bSSuresh Siddha static int __init init_workqueues(void) 38021da177e4SLinus Torvalds { 3803c34056a3STejun Heo unsigned int cpu; 3804c8e55f36STejun Heo int i; 3805c34056a3STejun Heo 3806b5490077STejun Heo /* make sure we have enough bits for OFFQ CPU number */ 3807b5490077STejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) < 3808b5490077STejun Heo WORK_CPU_LAST); 3809b5490077STejun Heo 381065758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 381165758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 38128b03ae3cSTejun Heo 38138b03ae3cSTejun Heo /* initialize gcwqs */ 3814f3421797STejun Heo for_each_gcwq_cpu(cpu) { 38158b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 38164ce62e9eSTejun Heo struct worker_pool *pool; 38178b03ae3cSTejun Heo 38188b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 38198b03ae3cSTejun Heo gcwq->cpu = cpu; 3820f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 38218b03ae3cSTejun Heo 3822c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3823c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3824c8e55f36STejun Heo 38254ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 38264ce62e9eSTejun Heo pool->gcwq = gcwq; 38274ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 38284ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3829e22bee78STejun Heo 38304ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 38314ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 38324ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3833e22bee78STejun Heo 38344ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 38354ce62e9eSTejun Heo (unsigned long)pool); 38364ce62e9eSTejun Heo 383760373152STejun Heo mutex_init(&pool->manager_mutex); 38384ce62e9eSTejun Heo ida_init(&pool->worker_ida); 38394ce62e9eSTejun Heo } 3840db7bccf4STejun Heo 384125511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 38428b03ae3cSTejun Heo } 38438b03ae3cSTejun Heo 3844e22bee78STejun Heo /* create the initial worker */ 3845f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3846e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 38474ce62e9eSTejun Heo struct worker_pool *pool; 3848e22bee78STejun Heo 3849477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3850477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 38514ce62e9eSTejun Heo 38524ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 38534ce62e9eSTejun Heo struct worker *worker; 38544ce62e9eSTejun Heo 3855bc2ae0f5STejun Heo worker = create_worker(pool); 3856e22bee78STejun Heo BUG_ON(!worker); 3857e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3858e22bee78STejun Heo start_worker(worker); 3859e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3860e22bee78STejun Heo } 38614ce62e9eSTejun Heo } 3862e22bee78STejun Heo 3863d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3864d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3865d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3866f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3867f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 386824d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 386924d51addSTejun Heo WQ_FREEZABLE, 0); 387062d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 387162d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3872e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 387362d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 387462d3c543SAlan Stern !system_nrt_freezable_wq); 38756ee0578bSSuresh Siddha return 0; 38761da177e4SLinus Torvalds } 38776ee0578bSSuresh Siddha early_initcall(init_workqueues); 3878