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 /* 536e120153dSTejun Heo * A work's data points to the cwq with WORK_STRUCT_CWQ set while the 537e120153dSTejun Heo * work is on queue. Once execution starts, WORK_STRUCT_CWQ is 538e120153dSTejun Heo * cleared and the work data contains the cpu number it was last on. 5397a22ad75STejun Heo * 5407a22ad75STejun Heo * set_work_{cwq|cpu}() and clear_work_data() can be used to set the 5417a22ad75STejun Heo * cwq, cpu or clear work->data. These functions should only be 5427a22ad75STejun Heo * called while the work is owned - ie. while the PENDING bit is set. 5437a22ad75STejun Heo * 5447a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 5457a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 5467a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 5477a22ad75STejun Heo * queueing until execution starts. 5484594bf15SDavid Howells */ 5497a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5507a22ad75STejun Heo unsigned long flags) 5517a22ad75STejun Heo { 5527a22ad75STejun Heo BUG_ON(!work_pending(work)); 5537a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5547a22ad75STejun Heo } 5557a22ad75STejun Heo 5567a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5574690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5584690c4abSTejun Heo unsigned long extra_flags) 559365970a1SDavid Howells { 5607a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 561e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 562365970a1SDavid Howells } 563365970a1SDavid Howells 5647a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu) 5654d707b9fSOleg Nesterov { 5667a22ad75STejun Heo set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING); 5674d707b9fSOleg Nesterov } 5684d707b9fSOleg Nesterov 5697a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 570365970a1SDavid Howells { 5717a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5727a22ad75STejun Heo } 5737a22ad75STejun Heo 5747a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5757a22ad75STejun Heo { 576e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5777a22ad75STejun Heo 578e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 579e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 580e120153dSTejun Heo else 581e120153dSTejun Heo return NULL; 5827a22ad75STejun Heo } 5837a22ad75STejun Heo 5847a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5857a22ad75STejun Heo { 586e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5877a22ad75STejun Heo unsigned int cpu; 5887a22ad75STejun Heo 589e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 590e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 591bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5927a22ad75STejun Heo 5937a22ad75STejun Heo cpu = data >> WORK_STRUCT_FLAG_BITS; 594bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 5957a22ad75STejun Heo return NULL; 5967a22ad75STejun Heo 597f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 5987a22ad75STejun Heo return get_gcwq(cpu); 599365970a1SDavid Howells } 600365970a1SDavid Howells 601e22bee78STejun Heo /* 6023270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6033270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6043270476aSTejun Heo * they're being called with gcwq->lock held. 605e22bee78STejun Heo */ 606e22bee78STejun Heo 60763d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 608649027d7STejun Heo { 6093270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 610649027d7STejun Heo } 611649027d7STejun Heo 612e22bee78STejun Heo /* 613e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 614e22bee78STejun Heo * running workers. 615974271c4STejun Heo * 616974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 617974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 618974271c4STejun Heo * worklist isn't empty. 619e22bee78STejun Heo */ 62063d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 621e22bee78STejun Heo { 62263d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 623e22bee78STejun Heo } 624e22bee78STejun Heo 625e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 62663d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 627e22bee78STejun Heo { 62863d95a91STejun Heo return pool->nr_idle; 629e22bee78STejun Heo } 630e22bee78STejun Heo 631e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 63263d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 633e22bee78STejun Heo { 63463d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 635e22bee78STejun Heo 6363270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 637e22bee78STejun Heo } 638e22bee78STejun Heo 639e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 64063d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 641e22bee78STejun Heo { 64263d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 643e22bee78STejun Heo } 644e22bee78STejun Heo 645e22bee78STejun Heo /* Do I need to be the manager? */ 64663d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 647e22bee78STejun Heo { 64863d95a91STejun Heo return need_to_create_worker(pool) || 64911ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 650e22bee78STejun Heo } 651e22bee78STejun Heo 652e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 65363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 654e22bee78STejun Heo { 65560373152STejun Heo bool managing = mutex_is_locked(&pool->manager_mutex); 65663d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 65763d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 658e22bee78STejun Heo 659e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 660e22bee78STejun Heo } 661e22bee78STejun Heo 662e22bee78STejun Heo /* 663e22bee78STejun Heo * Wake up functions. 664e22bee78STejun Heo */ 665e22bee78STejun Heo 6667e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 66763d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6687e11629dSTejun Heo { 66963d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6707e11629dSTejun Heo return NULL; 6717e11629dSTejun Heo 67263d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6737e11629dSTejun Heo } 6747e11629dSTejun Heo 6757e11629dSTejun Heo /** 6767e11629dSTejun Heo * wake_up_worker - wake up an idle worker 67763d95a91STejun Heo * @pool: worker pool to wake worker from 6787e11629dSTejun Heo * 67963d95a91STejun Heo * Wake up the first idle worker of @pool. 6807e11629dSTejun Heo * 6817e11629dSTejun Heo * CONTEXT: 6827e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 6837e11629dSTejun Heo */ 68463d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 6857e11629dSTejun Heo { 68663d95a91STejun Heo struct worker *worker = first_worker(pool); 6877e11629dSTejun Heo 6887e11629dSTejun Heo if (likely(worker)) 6897e11629dSTejun Heo wake_up_process(worker->task); 6907e11629dSTejun Heo } 6917e11629dSTejun Heo 6924690c4abSTejun Heo /** 693e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 694e22bee78STejun Heo * @task: task waking up 695e22bee78STejun Heo * @cpu: CPU @task is waking up to 696e22bee78STejun Heo * 697e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 698e22bee78STejun Heo * being awoken. 699e22bee78STejun Heo * 700e22bee78STejun Heo * CONTEXT: 701e22bee78STejun Heo * spin_lock_irq(rq->lock) 702e22bee78STejun Heo */ 703e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 704e22bee78STejun Heo { 705e22bee78STejun Heo struct worker *worker = kthread_data(task); 706e22bee78STejun Heo 7072d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 70863d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 709e22bee78STejun Heo } 710e22bee78STejun Heo 711e22bee78STejun Heo /** 712e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 713e22bee78STejun Heo * @task: task going to sleep 714e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 715e22bee78STejun Heo * 716e22bee78STejun Heo * This function is called during schedule() when a busy worker is 717e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 718e22bee78STejun Heo * returning pointer to its task. 719e22bee78STejun Heo * 720e22bee78STejun Heo * CONTEXT: 721e22bee78STejun Heo * spin_lock_irq(rq->lock) 722e22bee78STejun Heo * 723e22bee78STejun Heo * RETURNS: 724e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 725e22bee78STejun Heo */ 726e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 727e22bee78STejun Heo unsigned int cpu) 728e22bee78STejun Heo { 729e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 730bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 73163d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 732e22bee78STejun Heo 7332d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 734e22bee78STejun Heo return NULL; 735e22bee78STejun Heo 736e22bee78STejun Heo /* this can only happen on the local cpu */ 737e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 738e22bee78STejun Heo 739e22bee78STejun Heo /* 740e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 741e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 742e22bee78STejun Heo * Please read comment there. 743e22bee78STejun Heo * 744628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 745628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 746628c78e7STejun Heo * disabled, which in turn means that none else could be 747628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 748628c78e7STejun Heo * lock is safe. 749e22bee78STejun Heo */ 750bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 75163d95a91STejun Heo to_wakeup = first_worker(pool); 752e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 753e22bee78STejun Heo } 754e22bee78STejun Heo 755e22bee78STejun Heo /** 756e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 757cb444766STejun Heo * @worker: self 758d302f017STejun Heo * @flags: flags to set 759d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 760d302f017STejun Heo * 761e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 762e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 763e22bee78STejun Heo * woken up. 764d302f017STejun Heo * 765cb444766STejun Heo * CONTEXT: 766cb444766STejun Heo * spin_lock_irq(gcwq->lock) 767d302f017STejun Heo */ 768d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 769d302f017STejun Heo bool wakeup) 770d302f017STejun Heo { 771bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 772e22bee78STejun Heo 773cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 774cb444766STejun Heo 775e22bee78STejun Heo /* 776e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 777e22bee78STejun Heo * wake up an idle worker as necessary if requested by 778e22bee78STejun Heo * @wakeup. 779e22bee78STejun Heo */ 780e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 781e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 78263d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 783e22bee78STejun Heo 784e22bee78STejun Heo if (wakeup) { 785e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 786bd7bdd43STejun Heo !list_empty(&pool->worklist)) 78763d95a91STejun Heo wake_up_worker(pool); 788e22bee78STejun Heo } else 789e22bee78STejun Heo atomic_dec(nr_running); 790e22bee78STejun Heo } 791e22bee78STejun Heo 792d302f017STejun Heo worker->flags |= flags; 793d302f017STejun Heo } 794d302f017STejun Heo 795d302f017STejun Heo /** 796e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 797cb444766STejun Heo * @worker: self 798d302f017STejun Heo * @flags: flags to clear 799d302f017STejun Heo * 800e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 801d302f017STejun Heo * 802cb444766STejun Heo * CONTEXT: 803cb444766STejun Heo * spin_lock_irq(gcwq->lock) 804d302f017STejun Heo */ 805d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 806d302f017STejun Heo { 80763d95a91STejun Heo struct worker_pool *pool = worker->pool; 808e22bee78STejun Heo unsigned int oflags = worker->flags; 809e22bee78STejun Heo 810cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 811cb444766STejun Heo 812d302f017STejun Heo worker->flags &= ~flags; 813e22bee78STejun Heo 81442c025f3STejun Heo /* 81542c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 81642c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 81742c025f3STejun Heo * of multiple flags, not a single flag. 81842c025f3STejun Heo */ 819e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 820e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 82163d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 822d302f017STejun Heo } 823d302f017STejun Heo 824d302f017STejun Heo /** 825c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 826c8e55f36STejun Heo * @gcwq: gcwq of interest 827c8e55f36STejun Heo * @work: work to be hashed 828c8e55f36STejun Heo * 829c8e55f36STejun Heo * Return hash head of @gcwq for @work. 830c8e55f36STejun Heo * 831c8e55f36STejun Heo * CONTEXT: 832c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 833c8e55f36STejun Heo * 834c8e55f36STejun Heo * RETURNS: 835c8e55f36STejun Heo * Pointer to the hash head. 836c8e55f36STejun Heo */ 837c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 838c8e55f36STejun Heo struct work_struct *work) 839c8e55f36STejun Heo { 840c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 841c8e55f36STejun Heo unsigned long v = (unsigned long)work; 842c8e55f36STejun Heo 843c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 844c8e55f36STejun Heo v >>= base_shift; 845c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 846c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 847c8e55f36STejun Heo 848c8e55f36STejun Heo return &gcwq->busy_hash[v]; 849c8e55f36STejun Heo } 850c8e55f36STejun Heo 851c8e55f36STejun Heo /** 8528cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8538cca0eeaSTejun Heo * @gcwq: gcwq of interest 8548cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8558cca0eeaSTejun Heo * @work: work to find worker for 8568cca0eeaSTejun Heo * 8578cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8588cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8598cca0eeaSTejun Heo * work. 8608cca0eeaSTejun Heo * 8618cca0eeaSTejun Heo * CONTEXT: 8628cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8638cca0eeaSTejun Heo * 8648cca0eeaSTejun Heo * RETURNS: 8658cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8668cca0eeaSTejun Heo * otherwise. 8678cca0eeaSTejun Heo */ 8688cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8698cca0eeaSTejun Heo struct hlist_head *bwh, 8708cca0eeaSTejun Heo struct work_struct *work) 8718cca0eeaSTejun Heo { 8728cca0eeaSTejun Heo struct worker *worker; 8738cca0eeaSTejun Heo struct hlist_node *tmp; 8748cca0eeaSTejun Heo 8758cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8768cca0eeaSTejun Heo if (worker->current_work == work) 8778cca0eeaSTejun Heo return worker; 8788cca0eeaSTejun Heo return NULL; 8798cca0eeaSTejun Heo } 8808cca0eeaSTejun Heo 8818cca0eeaSTejun Heo /** 8828cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 8838cca0eeaSTejun Heo * @gcwq: gcwq of interest 8848cca0eeaSTejun Heo * @work: work to find worker for 8858cca0eeaSTejun Heo * 8868cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 8878cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 8888cca0eeaSTejun Heo * function calculates @bwh itself. 8898cca0eeaSTejun Heo * 8908cca0eeaSTejun Heo * CONTEXT: 8918cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8928cca0eeaSTejun Heo * 8938cca0eeaSTejun Heo * RETURNS: 8948cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8958cca0eeaSTejun Heo * otherwise. 8968cca0eeaSTejun Heo */ 8978cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 8988cca0eeaSTejun Heo struct work_struct *work) 8998cca0eeaSTejun Heo { 9008cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9018cca0eeaSTejun Heo work); 9028cca0eeaSTejun Heo } 9038cca0eeaSTejun Heo 9048cca0eeaSTejun Heo /** 9057e11629dSTejun Heo * insert_work - insert a work into gcwq 9064690c4abSTejun Heo * @cwq: cwq @work belongs to 9074690c4abSTejun Heo * @work: work to insert 9084690c4abSTejun Heo * @head: insertion point 9094690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 9104690c4abSTejun Heo * 9117e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 9127e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 9134690c4abSTejun Heo * 9144690c4abSTejun Heo * CONTEXT: 9158b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 9161da177e4SLinus Torvalds */ 917b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 9184690c4abSTejun Heo struct work_struct *work, struct list_head *head, 9194690c4abSTejun Heo unsigned int extra_flags) 920b89deed3SOleg Nesterov { 92163d95a91STejun Heo struct worker_pool *pool = cwq->pool; 922e1d8aa9fSFrederic Weisbecker 9234690c4abSTejun Heo /* we own @work, set data and link */ 9247a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 9254690c4abSTejun Heo 9266e84d644SOleg Nesterov /* 9276e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 9286e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 9296e84d644SOleg Nesterov */ 9306e84d644SOleg Nesterov smp_wmb(); 9314690c4abSTejun Heo 9321a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 933e22bee78STejun Heo 934e22bee78STejun Heo /* 935e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 936e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 937e22bee78STejun Heo * lying around lazily while there are works to be processed. 938e22bee78STejun Heo */ 939e22bee78STejun Heo smp_mb(); 940e22bee78STejun Heo 94163d95a91STejun Heo if (__need_more_worker(pool)) 94263d95a91STejun Heo wake_up_worker(pool); 943b89deed3SOleg Nesterov } 944b89deed3SOleg Nesterov 945c8efcc25STejun Heo /* 946c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 947c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 948c8efcc25STejun Heo * cold paths. 949c8efcc25STejun Heo */ 950c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 951c8efcc25STejun Heo { 952c8efcc25STejun Heo unsigned long flags; 953c8efcc25STejun Heo unsigned int cpu; 954c8efcc25STejun Heo 955c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 956c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 957c8efcc25STejun Heo struct worker *worker; 958c8efcc25STejun Heo struct hlist_node *pos; 959c8efcc25STejun Heo int i; 960c8efcc25STejun Heo 961c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 962c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 963c8efcc25STejun Heo if (worker->task != current) 964c8efcc25STejun Heo continue; 965c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 966c8efcc25STejun Heo /* 967c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 968c8efcc25STejun Heo * is headed to the same workqueue. 969c8efcc25STejun Heo */ 970c8efcc25STejun Heo return worker->current_cwq->wq == wq; 971c8efcc25STejun Heo } 972c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 973c8efcc25STejun Heo } 974c8efcc25STejun Heo return false; 975c8efcc25STejun Heo } 976c8efcc25STejun Heo 9774690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 9781da177e4SLinus Torvalds struct work_struct *work) 9791da177e4SLinus Torvalds { 980502ca9d8STejun Heo struct global_cwq *gcwq; 981502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 9821e19ffc6STejun Heo struct list_head *worklist; 9838a2e8e5dSTejun Heo unsigned int work_flags; 9841da177e4SLinus Torvalds unsigned long flags; 9851da177e4SLinus Torvalds 986dc186ad7SThomas Gleixner debug_work_activate(work); 9871e19ffc6STejun Heo 988c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 9899c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 990c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 991e41e704bSTejun Heo return; 992e41e704bSTejun Heo 993c7fc77f7STejun Heo /* determine gcwq to use */ 994c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 995c7fc77f7STejun Heo struct global_cwq *last_gcwq; 996c7fc77f7STejun Heo 997f3421797STejun Heo if (unlikely(cpu == WORK_CPU_UNBOUND)) 998f3421797STejun Heo cpu = raw_smp_processor_id(); 999f3421797STejun Heo 100018aa9effSTejun Heo /* 100118aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 100218aa9effSTejun Heo * was previously on a different cpu, it might still 100318aa9effSTejun Heo * be running there, in which case the work needs to 100418aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 100518aa9effSTejun Heo */ 1006502ca9d8STejun Heo gcwq = get_gcwq(cpu); 100718aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 100818aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 100918aa9effSTejun Heo struct worker *worker; 101018aa9effSTejun Heo 101118aa9effSTejun Heo spin_lock_irqsave(&last_gcwq->lock, flags); 101218aa9effSTejun Heo 101318aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 101418aa9effSTejun Heo 101518aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 101618aa9effSTejun Heo gcwq = last_gcwq; 101718aa9effSTejun Heo else { 101818aa9effSTejun Heo /* meh... not running there, queue here */ 101918aa9effSTejun Heo spin_unlock_irqrestore(&last_gcwq->lock, flags); 102018aa9effSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 102118aa9effSTejun Heo } 102218aa9effSTejun Heo } else 10238b03ae3cSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1024f3421797STejun Heo } else { 1025f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 1026f3421797STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1027502ca9d8STejun Heo } 1028502ca9d8STejun Heo 1029502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1030502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1031cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1032502ca9d8STejun Heo 1033f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1034f5b2552bSDan Carpenter spin_unlock_irqrestore(&gcwq->lock, flags); 1035f5b2552bSDan Carpenter return; 1036f5b2552bSDan Carpenter } 10371e19ffc6STejun Heo 103873f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 10398a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 10401e19ffc6STejun Heo 10411e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1042cdadf009STejun Heo trace_workqueue_activate_work(work); 10431e19ffc6STejun Heo cwq->nr_active++; 10443270476aSTejun Heo worklist = &cwq->pool->worklist; 10458a2e8e5dSTejun Heo } else { 10468a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 10471e19ffc6STejun Heo worklist = &cwq->delayed_works; 10488a2e8e5dSTejun Heo } 10491e19ffc6STejun Heo 10508a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 10511e19ffc6STejun Heo 10528b03ae3cSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10550fcb78c2SRolf Eike Beer /** 10560fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 10570fcb78c2SRolf Eike Beer * @wq: workqueue to use 10580fcb78c2SRolf Eike Beer * @work: work to queue 10590fcb78c2SRolf Eike Beer * 1060057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 10611da177e4SLinus Torvalds * 106200dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 106300dfcaf7SOleg Nesterov * it can be processed by another CPU. 10641da177e4SLinus Torvalds */ 10657ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 10661da177e4SLinus Torvalds { 1067ef1ca236SOleg Nesterov int ret; 10681da177e4SLinus Torvalds 1069ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 1070a848e3b6SOleg Nesterov put_cpu(); 1071ef1ca236SOleg Nesterov 10721da177e4SLinus Torvalds return ret; 10731da177e4SLinus Torvalds } 1074ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 10751da177e4SLinus Torvalds 1076c1a220e7SZhang Rui /** 1077c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1078c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1079c1a220e7SZhang Rui * @wq: workqueue to use 1080c1a220e7SZhang Rui * @work: work to queue 1081c1a220e7SZhang Rui * 1082c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 1083c1a220e7SZhang Rui * 1084c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1085c1a220e7SZhang Rui * can't go away. 1086c1a220e7SZhang Rui */ 1087c1a220e7SZhang Rui int 1088c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 1089c1a220e7SZhang Rui { 1090c1a220e7SZhang Rui int ret = 0; 1091c1a220e7SZhang Rui 109222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 10934690c4abSTejun Heo __queue_work(cpu, wq, work); 1094c1a220e7SZhang Rui ret = 1; 1095c1a220e7SZhang Rui } 1096c1a220e7SZhang Rui return ret; 1097c1a220e7SZhang Rui } 1098c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1099c1a220e7SZhang Rui 11006d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 11011da177e4SLinus Torvalds { 110252bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 11037a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 11041da177e4SLinus Torvalds 11054690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11080fcb78c2SRolf Eike Beer /** 11090fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 11100fcb78c2SRolf Eike Beer * @wq: workqueue to use 1111af9997e4SRandy Dunlap * @dwork: delayable work to queue 11120fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11130fcb78c2SRolf Eike Beer * 1114057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11150fcb78c2SRolf Eike Beer */ 11167ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 111752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11181da177e4SLinus Torvalds { 111952bad64dSDavid Howells if (delay == 0) 112063bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 11211da177e4SLinus Torvalds 112263bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 11231da177e4SLinus Torvalds } 1124ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 11251da177e4SLinus Torvalds 11260fcb78c2SRolf Eike Beer /** 11270fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 11280fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 11290fcb78c2SRolf Eike Beer * @wq: workqueue to use 1130af9997e4SRandy Dunlap * @dwork: work to queue 11310fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11320fcb78c2SRolf Eike Beer * 1133057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11340fcb78c2SRolf Eike Beer */ 11357a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 113652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11377a6bc1cdSVenkatesh Pallipadi { 11387a6bc1cdSVenkatesh Pallipadi int ret = 0; 113952bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 114052bad64dSDavid Howells struct work_struct *work = &dwork->work; 11417a6bc1cdSVenkatesh Pallipadi 114222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1143c7fc77f7STejun Heo unsigned int lcpu; 11447a22ad75STejun Heo 11457a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 11467a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 11477a6bc1cdSVenkatesh Pallipadi 11488a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 11498a3e77ccSAndrew Liu 11507a22ad75STejun Heo /* 11517a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 11527a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 11537a22ad75STejun Heo * reentrance detection for delayed works. 11547a22ad75STejun Heo */ 1155c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1156c7fc77f7STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 1157c7fc77f7STejun Heo 1158c7fc77f7STejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 1159c7fc77f7STejun Heo lcpu = gcwq->cpu; 1160c7fc77f7STejun Heo else 1161c7fc77f7STejun Heo lcpu = raw_smp_processor_id(); 1162c7fc77f7STejun Heo } else 1163c7fc77f7STejun Heo lcpu = WORK_CPU_UNBOUND; 1164c7fc77f7STejun Heo 11657a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 1166c7fc77f7STejun Heo 11677a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 116852bad64dSDavid Howells timer->data = (unsigned long)dwork; 11697a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 117063bc0362SOleg Nesterov 117163bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 11727a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 117363bc0362SOleg Nesterov else 117463bc0362SOleg Nesterov add_timer(timer); 11757a6bc1cdSVenkatesh Pallipadi ret = 1; 11767a6bc1cdSVenkatesh Pallipadi } 11777a6bc1cdSVenkatesh Pallipadi return ret; 11787a6bc1cdSVenkatesh Pallipadi } 1179ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 11801da177e4SLinus Torvalds 1181c8e55f36STejun Heo /** 1182c8e55f36STejun Heo * worker_enter_idle - enter idle state 1183c8e55f36STejun Heo * @worker: worker which is entering idle state 1184c8e55f36STejun Heo * 1185c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1186c8e55f36STejun Heo * necessary. 1187c8e55f36STejun Heo * 1188c8e55f36STejun Heo * LOCKING: 1189c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1190c8e55f36STejun Heo */ 1191c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 11921da177e4SLinus Torvalds { 1193bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1194bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1195c8e55f36STejun Heo 1196c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1197c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1198c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1199c8e55f36STejun Heo 1200cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1201cb444766STejun Heo worker->flags |= WORKER_IDLE; 1202bd7bdd43STejun Heo pool->nr_idle++; 1203e22bee78STejun Heo worker->last_active = jiffies; 1204c8e55f36STejun Heo 1205c8e55f36STejun Heo /* idle_list is LIFO */ 1206bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1207db7bccf4STejun Heo 120863d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1209628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1210cb444766STejun Heo 1211544ecf31STejun Heo /* 1212628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1213628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1214628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1215628c78e7STejun Heo * unbind is not in progress. 1216544ecf31STejun Heo */ 1217628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1218bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 121963d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1220c8e55f36STejun Heo } 1221c8e55f36STejun Heo 1222c8e55f36STejun Heo /** 1223c8e55f36STejun Heo * worker_leave_idle - leave idle state 1224c8e55f36STejun Heo * @worker: worker which is leaving idle state 1225c8e55f36STejun Heo * 1226c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1227c8e55f36STejun Heo * 1228c8e55f36STejun Heo * LOCKING: 1229c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1230c8e55f36STejun Heo */ 1231c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1232c8e55f36STejun Heo { 1233bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1234c8e55f36STejun Heo 1235c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1236d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1237bd7bdd43STejun Heo pool->nr_idle--; 1238c8e55f36STejun Heo list_del_init(&worker->entry); 1239c8e55f36STejun Heo } 1240c8e55f36STejun Heo 1241e22bee78STejun Heo /** 1242e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1243e22bee78STejun Heo * @worker: self 1244e22bee78STejun Heo * 1245e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1246e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1247e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1248e22bee78STejun Heo * guaranteed to execute on the cpu. 1249e22bee78STejun Heo * 1250e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1251e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1252e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1253e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1254e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1255e22bee78STejun Heo * [dis]associated in the meantime. 1256e22bee78STejun Heo * 1257f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1258f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1259f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1260f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1261f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1262e22bee78STejun Heo * 1263e22bee78STejun Heo * CONTEXT: 1264e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1265e22bee78STejun Heo * held. 1266e22bee78STejun Heo * 1267e22bee78STejun Heo * RETURNS: 1268e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1269e22bee78STejun Heo * bound), %false if offline. 1270e22bee78STejun Heo */ 1271e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1272972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1273e22bee78STejun Heo { 1274bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1275e22bee78STejun Heo struct task_struct *task = worker->task; 1276e22bee78STejun Heo 1277e22bee78STejun Heo while (true) { 1278e22bee78STejun Heo /* 1279e22bee78STejun Heo * The following call may fail, succeed or succeed 1280e22bee78STejun Heo * without actually migrating the task to the cpu if 1281e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1282e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1283e22bee78STejun Heo */ 1284f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1285e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1286e22bee78STejun Heo 1287e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1288e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1289e22bee78STejun Heo return false; 1290e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1291e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1292e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1293e22bee78STejun Heo return true; 1294e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1295e22bee78STejun Heo 12965035b20fSTejun Heo /* 12975035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 12985035b20fSTejun Heo * and retry migration. cond_resched() is required here; 12995035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 13005035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 13015035b20fSTejun Heo */ 1302e22bee78STejun Heo cpu_relax(); 13035035b20fSTejun Heo cond_resched(); 1304e22bee78STejun Heo } 1305e22bee78STejun Heo } 1306e22bee78STejun Heo 130725511a47STejun Heo struct idle_rebind { 130825511a47STejun Heo int cnt; /* # workers to be rebound */ 130925511a47STejun Heo struct completion done; /* all workers rebound */ 131025511a47STejun Heo }; 131125511a47STejun Heo 1312e22bee78STejun Heo /* 131325511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 131425511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 131525511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 131625511a47STejun Heo */ 131725511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 131825511a47STejun Heo { 131925511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 132025511a47STejun Heo 132125511a47STejun Heo /* CPU must be online at this point */ 132225511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 132325511a47STejun Heo if (!--worker->idle_rebind->cnt) 132425511a47STejun Heo complete(&worker->idle_rebind->done); 132525511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 132625511a47STejun Heo 132725511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 132825511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 132925511a47STejun Heo } 133025511a47STejun Heo 133125511a47STejun Heo /* 133225511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1333403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1334403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1335403c821dSTejun Heo * executed twice without intervening cpu down. 1336e22bee78STejun Heo */ 133725511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1338e22bee78STejun Heo { 1339e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1340bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1341e22bee78STejun Heo 1342e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1343e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1344e22bee78STejun Heo 1345e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1346e22bee78STejun Heo } 1347e22bee78STejun Heo 134825511a47STejun Heo /** 134925511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 135025511a47STejun Heo * @gcwq: gcwq of interest 135125511a47STejun Heo * 135225511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 135325511a47STejun Heo * is different for idle and busy ones. 135425511a47STejun Heo * 135525511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 135625511a47STejun Heo * be complete before any worker starts executing work items with 135725511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 135825511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 135925511a47STejun Heo * 136025511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 136125511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 136225511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 136325511a47STejun Heo * 136425511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 136525511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 136625511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 136725511a47STejun Heo * be properbly bumped as busy workers rebind. 136825511a47STejun Heo * 136925511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 137025511a47STejun Heo * work item scheduled. 137125511a47STejun Heo */ 137225511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 137325511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 137425511a47STejun Heo { 137525511a47STejun Heo struct idle_rebind idle_rebind; 137625511a47STejun Heo struct worker_pool *pool; 137725511a47STejun Heo struct worker *worker; 137825511a47STejun Heo struct hlist_node *pos; 137925511a47STejun Heo int i; 138025511a47STejun Heo 138125511a47STejun Heo lockdep_assert_held(&gcwq->lock); 138225511a47STejun Heo 138325511a47STejun Heo for_each_worker_pool(pool, gcwq) 138425511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 138525511a47STejun Heo 138625511a47STejun Heo /* 138725511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 138825511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 138925511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 139025511a47STejun Heo */ 139125511a47STejun Heo init_completion(&idle_rebind.done); 139225511a47STejun Heo retry: 139325511a47STejun Heo idle_rebind.cnt = 1; 139425511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 139525511a47STejun Heo 139625511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 139725511a47STejun Heo for_each_worker_pool(pool, gcwq) { 139825511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 139996e65306SLai Jiangshan unsigned long worker_flags = worker->flags; 140096e65306SLai Jiangshan 140125511a47STejun Heo if (worker->flags & WORKER_REBIND) 140225511a47STejun Heo continue; 140325511a47STejun Heo 140496e65306SLai Jiangshan /* morph UNBOUND to REBIND atomically */ 140596e65306SLai Jiangshan worker_flags &= ~WORKER_UNBOUND; 140696e65306SLai Jiangshan worker_flags |= WORKER_REBIND; 140796e65306SLai Jiangshan ACCESS_ONCE(worker->flags) = worker_flags; 140825511a47STejun Heo 140925511a47STejun Heo idle_rebind.cnt++; 141025511a47STejun Heo worker->idle_rebind = &idle_rebind; 141125511a47STejun Heo 141225511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 141325511a47STejun Heo wake_up_process(worker->task); 141425511a47STejun Heo } 141525511a47STejun Heo } 141625511a47STejun Heo 141725511a47STejun Heo if (--idle_rebind.cnt) { 141825511a47STejun Heo spin_unlock_irq(&gcwq->lock); 141925511a47STejun Heo wait_for_completion(&idle_rebind.done); 142025511a47STejun Heo spin_lock_irq(&gcwq->lock); 142125511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 142225511a47STejun Heo goto retry; 142325511a47STejun Heo } 142425511a47STejun Heo 1425*90beca5dSTejun Heo /* all idle workers are rebound, rebind busy workers */ 142625511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 142725511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 142896e65306SLai Jiangshan unsigned long worker_flags = worker->flags; 142925511a47STejun Heo 143096e65306SLai Jiangshan /* morph UNBOUND to REBIND atomically */ 143196e65306SLai Jiangshan worker_flags &= ~WORKER_UNBOUND; 143296e65306SLai Jiangshan worker_flags |= WORKER_REBIND; 143396e65306SLai Jiangshan ACCESS_ONCE(worker->flags) = worker_flags; 143425511a47STejun Heo 143525511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 143625511a47STejun Heo work_data_bits(rebind_work))) 143725511a47STejun Heo continue; 143825511a47STejun Heo 143925511a47STejun Heo /* wq doesn't matter, use the default one */ 144025511a47STejun Heo debug_work_activate(rebind_work); 144125511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 144225511a47STejun Heo worker->scheduled.next, 144325511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 144425511a47STejun Heo } 1445*90beca5dSTejun Heo 1446*90beca5dSTejun Heo /* 1447*90beca5dSTejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 1448*90beca5dSTejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 1449*90beca5dSTejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 1450*90beca5dSTejun Heo * because these workers are still guaranteed to be idle. 1451*90beca5dSTejun Heo */ 1452*90beca5dSTejun Heo for_each_worker_pool(pool, gcwq) 1453*90beca5dSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 1454*90beca5dSTejun Heo worker->flags &= ~WORKER_REBIND; 1455*90beca5dSTejun Heo 1456*90beca5dSTejun Heo wake_up_all(&gcwq->rebind_hold); 145725511a47STejun Heo } 145825511a47STejun Heo 1459c34056a3STejun Heo static struct worker *alloc_worker(void) 1460c34056a3STejun Heo { 1461c34056a3STejun Heo struct worker *worker; 1462c34056a3STejun Heo 1463c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1464c8e55f36STejun Heo if (worker) { 1465c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1466affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 146725511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1468e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1469e22bee78STejun Heo worker->flags = WORKER_PREP; 1470c8e55f36STejun Heo } 1471c34056a3STejun Heo return worker; 1472c34056a3STejun Heo } 1473c34056a3STejun Heo 1474c34056a3STejun Heo /** 1475c34056a3STejun Heo * create_worker - create a new workqueue worker 147663d95a91STejun Heo * @pool: pool the new worker will belong to 1477c34056a3STejun Heo * 147863d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1479c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1480c34056a3STejun Heo * destroy_worker(). 1481c34056a3STejun Heo * 1482c34056a3STejun Heo * CONTEXT: 1483c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1484c34056a3STejun Heo * 1485c34056a3STejun Heo * RETURNS: 1486c34056a3STejun Heo * Pointer to the newly created worker. 1487c34056a3STejun Heo */ 1488bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1489c34056a3STejun Heo { 149063d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 14913270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1492c34056a3STejun Heo struct worker *worker = NULL; 1493f3421797STejun Heo int id = -1; 1494c34056a3STejun Heo 14958b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1496bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 14978b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1498bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1499c34056a3STejun Heo goto fail; 15008b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1501c34056a3STejun Heo } 15028b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1503c34056a3STejun Heo 1504c34056a3STejun Heo worker = alloc_worker(); 1505c34056a3STejun Heo if (!worker) 1506c34056a3STejun Heo goto fail; 1507c34056a3STejun Heo 1508bd7bdd43STejun Heo worker->pool = pool; 1509c34056a3STejun Heo worker->id = id; 1510c34056a3STejun Heo 1511bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 151294dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 15133270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 15143270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1515f3421797STejun Heo else 1516f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 15173270476aSTejun Heo "kworker/u:%d%s", id, pri); 1518c34056a3STejun Heo if (IS_ERR(worker->task)) 1519c34056a3STejun Heo goto fail; 1520c34056a3STejun Heo 15213270476aSTejun Heo if (worker_pool_pri(pool)) 15223270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 15233270476aSTejun Heo 1524db7bccf4STejun Heo /* 1525bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1526bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1527bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1528bc2ae0f5STejun Heo * above the flag definition for details. 1529bc2ae0f5STejun Heo * 1530bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1531bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1532db7bccf4STejun Heo */ 1533bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 15348b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1535bc2ae0f5STejun Heo } else { 1536db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1537f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1538f3421797STejun Heo } 1539c34056a3STejun Heo 1540c34056a3STejun Heo return worker; 1541c34056a3STejun Heo fail: 1542c34056a3STejun Heo if (id >= 0) { 15438b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1544bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 15458b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1546c34056a3STejun Heo } 1547c34056a3STejun Heo kfree(worker); 1548c34056a3STejun Heo return NULL; 1549c34056a3STejun Heo } 1550c34056a3STejun Heo 1551c34056a3STejun Heo /** 1552c34056a3STejun Heo * start_worker - start a newly created worker 1553c34056a3STejun Heo * @worker: worker to start 1554c34056a3STejun Heo * 1555c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1556c34056a3STejun Heo * 1557c34056a3STejun Heo * CONTEXT: 15588b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1559c34056a3STejun Heo */ 1560c34056a3STejun Heo static void start_worker(struct worker *worker) 1561c34056a3STejun Heo { 1562cb444766STejun Heo worker->flags |= WORKER_STARTED; 1563bd7bdd43STejun Heo worker->pool->nr_workers++; 1564c8e55f36STejun Heo worker_enter_idle(worker); 1565c34056a3STejun Heo wake_up_process(worker->task); 1566c34056a3STejun Heo } 1567c34056a3STejun Heo 1568c34056a3STejun Heo /** 1569c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1570c34056a3STejun Heo * @worker: worker to be destroyed 1571c34056a3STejun Heo * 1572c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1573c8e55f36STejun Heo * 1574c8e55f36STejun Heo * CONTEXT: 1575c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1576c34056a3STejun Heo */ 1577c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1578c34056a3STejun Heo { 1579bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1580bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1581c34056a3STejun Heo int id = worker->id; 1582c34056a3STejun Heo 1583c34056a3STejun Heo /* sanity check frenzy */ 1584c34056a3STejun Heo BUG_ON(worker->current_work); 1585affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1586c34056a3STejun Heo 1587c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1588bd7bdd43STejun Heo pool->nr_workers--; 1589c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1590bd7bdd43STejun Heo pool->nr_idle--; 1591c8e55f36STejun Heo 1592c8e55f36STejun Heo list_del_init(&worker->entry); 1593cb444766STejun Heo worker->flags |= WORKER_DIE; 1594c8e55f36STejun Heo 1595c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1596c8e55f36STejun Heo 1597c34056a3STejun Heo kthread_stop(worker->task); 1598c34056a3STejun Heo kfree(worker); 1599c34056a3STejun Heo 16008b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1601bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1602c34056a3STejun Heo } 1603c34056a3STejun Heo 160463d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1605e22bee78STejun Heo { 160663d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 160763d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1608e22bee78STejun Heo 1609e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1610e22bee78STejun Heo 161163d95a91STejun Heo if (too_many_workers(pool)) { 1612e22bee78STejun Heo struct worker *worker; 1613e22bee78STejun Heo unsigned long expires; 1614e22bee78STejun Heo 1615e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 161663d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1617e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1618e22bee78STejun Heo 1619e22bee78STejun Heo if (time_before(jiffies, expires)) 162063d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1621e22bee78STejun Heo else { 1622e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 162311ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 162463d95a91STejun Heo wake_up_worker(pool); 1625e22bee78STejun Heo } 1626e22bee78STejun Heo } 1627e22bee78STejun Heo 1628e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1629e22bee78STejun Heo } 1630e22bee78STejun Heo 1631e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1632e22bee78STejun Heo { 1633e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1634e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1635f3421797STejun Heo unsigned int cpu; 1636e22bee78STejun Heo 1637e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1638e22bee78STejun Heo return false; 1639e22bee78STejun Heo 1640e22bee78STejun Heo /* mayday mayday mayday */ 1641bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1642f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1643f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1644f3421797STejun Heo cpu = 0; 1645f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1646e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1647e22bee78STejun Heo return true; 1648e22bee78STejun Heo } 1649e22bee78STejun Heo 165063d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1651e22bee78STejun Heo { 165263d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 165363d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1654e22bee78STejun Heo struct work_struct *work; 1655e22bee78STejun Heo 1656e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1657e22bee78STejun Heo 165863d95a91STejun Heo if (need_to_create_worker(pool)) { 1659e22bee78STejun Heo /* 1660e22bee78STejun Heo * We've been trying to create a new worker but 1661e22bee78STejun Heo * haven't been successful. We might be hitting an 1662e22bee78STejun Heo * allocation deadlock. Send distress signals to 1663e22bee78STejun Heo * rescuers. 1664e22bee78STejun Heo */ 166563d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1666e22bee78STejun Heo send_mayday(work); 1667e22bee78STejun Heo } 1668e22bee78STejun Heo 1669e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1670e22bee78STejun Heo 167163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1672e22bee78STejun Heo } 1673e22bee78STejun Heo 1674e22bee78STejun Heo /** 1675e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 167663d95a91STejun Heo * @pool: pool to create a new worker for 1677e22bee78STejun Heo * 167863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1679e22bee78STejun Heo * have at least one idle worker on return from this function. If 1680e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 168163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1682e22bee78STejun Heo * possible allocation deadlock. 1683e22bee78STejun Heo * 1684e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1685e22bee78STejun Heo * may_start_working() true. 1686e22bee78STejun Heo * 1687e22bee78STejun Heo * LOCKING: 1688e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1689e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1690e22bee78STejun Heo * manager. 1691e22bee78STejun Heo * 1692e22bee78STejun Heo * RETURNS: 1693e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1694e22bee78STejun Heo * otherwise. 1695e22bee78STejun Heo */ 169663d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 169706bd6ebfSNamhyung Kim __releases(&gcwq->lock) 169806bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1699e22bee78STejun Heo { 170063d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 170163d95a91STejun Heo 170263d95a91STejun Heo if (!need_to_create_worker(pool)) 1703e22bee78STejun Heo return false; 1704e22bee78STejun Heo restart: 17059f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 17069f9c2364STejun Heo 1707e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 170863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1709e22bee78STejun Heo 1710e22bee78STejun Heo while (true) { 1711e22bee78STejun Heo struct worker *worker; 1712e22bee78STejun Heo 1713bc2ae0f5STejun Heo worker = create_worker(pool); 1714e22bee78STejun Heo if (worker) { 171563d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1716e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1717e22bee78STejun Heo start_worker(worker); 171863d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1719e22bee78STejun Heo return true; 1720e22bee78STejun Heo } 1721e22bee78STejun Heo 172263d95a91STejun Heo if (!need_to_create_worker(pool)) 1723e22bee78STejun Heo break; 1724e22bee78STejun Heo 1725e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1726e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 17279f9c2364STejun Heo 172863d95a91STejun Heo if (!need_to_create_worker(pool)) 1729e22bee78STejun Heo break; 1730e22bee78STejun Heo } 1731e22bee78STejun Heo 173263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1733e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 173463d95a91STejun Heo if (need_to_create_worker(pool)) 1735e22bee78STejun Heo goto restart; 1736e22bee78STejun Heo return true; 1737e22bee78STejun Heo } 1738e22bee78STejun Heo 1739e22bee78STejun Heo /** 1740e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 174163d95a91STejun Heo * @pool: pool to destroy workers for 1742e22bee78STejun Heo * 174363d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1744e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1745e22bee78STejun Heo * 1746e22bee78STejun Heo * LOCKING: 1747e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1748e22bee78STejun Heo * multiple times. Called only from manager. 1749e22bee78STejun Heo * 1750e22bee78STejun Heo * RETURNS: 1751e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1752e22bee78STejun Heo * otherwise. 1753e22bee78STejun Heo */ 175463d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1755e22bee78STejun Heo { 1756e22bee78STejun Heo bool ret = false; 1757e22bee78STejun Heo 175863d95a91STejun Heo while (too_many_workers(pool)) { 1759e22bee78STejun Heo struct worker *worker; 1760e22bee78STejun Heo unsigned long expires; 1761e22bee78STejun Heo 176263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1763e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1764e22bee78STejun Heo 1765e22bee78STejun Heo if (time_before(jiffies, expires)) { 176663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1767e22bee78STejun Heo break; 1768e22bee78STejun Heo } 1769e22bee78STejun Heo 1770e22bee78STejun Heo destroy_worker(worker); 1771e22bee78STejun Heo ret = true; 1772e22bee78STejun Heo } 1773e22bee78STejun Heo 1774e22bee78STejun Heo return ret; 1775e22bee78STejun Heo } 1776e22bee78STejun Heo 1777e22bee78STejun Heo /** 1778e22bee78STejun Heo * manage_workers - manage worker pool 1779e22bee78STejun Heo * @worker: self 1780e22bee78STejun Heo * 1781e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1782e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1783e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1784e22bee78STejun Heo * 1785e22bee78STejun Heo * The caller can safely start processing works on false return. On 1786e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1787e22bee78STejun Heo * and may_start_working() is true. 1788e22bee78STejun Heo * 1789e22bee78STejun Heo * CONTEXT: 1790e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1791e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1792e22bee78STejun Heo * 1793e22bee78STejun Heo * RETURNS: 1794e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1795e22bee78STejun Heo * some action was taken. 1796e22bee78STejun Heo */ 1797e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1798e22bee78STejun Heo { 179963d95a91STejun Heo struct worker_pool *pool = worker->pool; 1800e22bee78STejun Heo bool ret = false; 1801e22bee78STejun Heo 180260373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1803e22bee78STejun Heo return ret; 1804e22bee78STejun Heo 180511ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1806e22bee78STejun Heo 1807e22bee78STejun Heo /* 1808e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1809e22bee78STejun Heo * on return. 1810e22bee78STejun Heo */ 181163d95a91STejun Heo ret |= maybe_destroy_workers(pool); 181263d95a91STejun Heo ret |= maybe_create_worker(pool); 1813e22bee78STejun Heo 181460373152STejun Heo mutex_unlock(&pool->manager_mutex); 1815e22bee78STejun Heo return ret; 1816e22bee78STejun Heo } 1817e22bee78STejun Heo 1818a62428c0STejun Heo /** 1819affee4b2STejun Heo * move_linked_works - move linked works to a list 1820affee4b2STejun Heo * @work: start of series of works to be scheduled 1821affee4b2STejun Heo * @head: target list to append @work to 1822affee4b2STejun Heo * @nextp: out paramter for nested worklist walking 1823affee4b2STejun Heo * 1824affee4b2STejun Heo * Schedule linked works starting from @work to @head. Work series to 1825affee4b2STejun Heo * be scheduled starts at @work and includes any consecutive work with 1826affee4b2STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1827affee4b2STejun Heo * 1828affee4b2STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1829affee4b2STejun Heo * the last scheduled work. This allows move_linked_works() to be 1830affee4b2STejun Heo * nested inside outer list_for_each_entry_safe(). 1831affee4b2STejun Heo * 1832affee4b2STejun Heo * CONTEXT: 18338b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1834affee4b2STejun Heo */ 1835affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1836affee4b2STejun Heo struct work_struct **nextp) 1837affee4b2STejun Heo { 1838affee4b2STejun Heo struct work_struct *n; 1839affee4b2STejun Heo 1840affee4b2STejun Heo /* 1841affee4b2STejun Heo * Linked worklist will always end before the end of the list, 1842affee4b2STejun Heo * use NULL for list head. 1843affee4b2STejun Heo */ 1844affee4b2STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1845affee4b2STejun Heo list_move_tail(&work->entry, head); 1846affee4b2STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1847affee4b2STejun Heo break; 1848affee4b2STejun Heo } 1849affee4b2STejun Heo 1850affee4b2STejun Heo /* 1851affee4b2STejun Heo * If we're already inside safe list traversal and have moved 1852affee4b2STejun Heo * multiple works to the scheduled queue, the next position 1853affee4b2STejun Heo * needs to be updated. 1854affee4b2STejun Heo */ 1855affee4b2STejun Heo if (nextp) 1856affee4b2STejun Heo *nextp = n; 1857affee4b2STejun Heo } 1858affee4b2STejun Heo 18591e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 18601e19ffc6STejun Heo { 18611e19ffc6STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 18621da177e4SLinus Torvalds struct work_struct, entry); 18631e19ffc6STejun Heo 1864cdadf009STejun Heo trace_workqueue_activate_work(work); 18653270476aSTejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 18668a2e8e5dSTejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 18671e19ffc6STejun Heo cwq->nr_active++; 18681e19ffc6STejun Heo } 18691e19ffc6STejun Heo 1870affee4b2STejun Heo /** 187173f53c4aSTejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 187273f53c4aSTejun Heo * @cwq: cwq of interest 187373f53c4aSTejun Heo * @color: color of work which left the queue 18748a2e8e5dSTejun Heo * @delayed: for a delayed work 187573f53c4aSTejun Heo * 187673f53c4aSTejun Heo * A work either has completed or is removed from pending queue, 187773f53c4aSTejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 187873f53c4aSTejun Heo * 187973f53c4aSTejun Heo * CONTEXT: 18808b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 188173f53c4aSTejun Heo */ 18828a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 18838a2e8e5dSTejun Heo bool delayed) 188473f53c4aSTejun Heo { 188573f53c4aSTejun Heo /* ignore uncolored works */ 188673f53c4aSTejun Heo if (color == WORK_NO_COLOR) 188773f53c4aSTejun Heo return; 188873f53c4aSTejun Heo 188973f53c4aSTejun Heo cwq->nr_in_flight[color]--; 18901e19ffc6STejun Heo 18918a2e8e5dSTejun Heo if (!delayed) { 18928a2e8e5dSTejun Heo cwq->nr_active--; 1893502ca9d8STejun Heo if (!list_empty(&cwq->delayed_works)) { 18941e19ffc6STejun Heo /* one down, submit a delayed one */ 1895502ca9d8STejun Heo if (cwq->nr_active < cwq->max_active) 18961e19ffc6STejun Heo cwq_activate_first_delayed(cwq); 1897502ca9d8STejun Heo } 18988a2e8e5dSTejun Heo } 189973f53c4aSTejun Heo 190073f53c4aSTejun Heo /* is flush in progress and are we at the flushing tip? */ 190173f53c4aSTejun Heo if (likely(cwq->flush_color != color)) 190273f53c4aSTejun Heo return; 190373f53c4aSTejun Heo 190473f53c4aSTejun Heo /* are there still in-flight works? */ 190573f53c4aSTejun Heo if (cwq->nr_in_flight[color]) 190673f53c4aSTejun Heo return; 190773f53c4aSTejun Heo 190873f53c4aSTejun Heo /* this cwq is done, clear flush_color */ 190973f53c4aSTejun Heo cwq->flush_color = -1; 191073f53c4aSTejun Heo 191173f53c4aSTejun Heo /* 191273f53c4aSTejun Heo * If this was the last cwq, wake up the first flusher. It 191373f53c4aSTejun Heo * will handle the rest. 191473f53c4aSTejun Heo */ 191573f53c4aSTejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 191673f53c4aSTejun Heo complete(&cwq->wq->first_flusher->done); 191773f53c4aSTejun Heo } 191873f53c4aSTejun Heo 191973f53c4aSTejun Heo /** 1920a62428c0STejun Heo * process_one_work - process single work 1921c34056a3STejun Heo * @worker: self 1922a62428c0STejun Heo * @work: work to process 1923a62428c0STejun Heo * 1924a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1925a62428c0STejun Heo * process a single work including synchronization against and 1926a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1927a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1928a62428c0STejun Heo * call this function to process a work. 1929a62428c0STejun Heo * 1930a62428c0STejun Heo * CONTEXT: 19318b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1932a62428c0STejun Heo */ 1933c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 193406bd6ebfSNamhyung Kim __releases(&gcwq->lock) 193506bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 19361da177e4SLinus Torvalds { 19377e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1938bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1939bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1940c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 1941fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 19426bb49e59SDavid Howells work_func_t f = work->func; 194373f53c4aSTejun Heo int work_color; 19447e11629dSTejun Heo struct worker *collision; 19454e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 19464e6045f1SJohannes Berg /* 1947a62428c0STejun Heo * It is permissible to free the struct work_struct from 1948a62428c0STejun Heo * inside the function that is called from it, this we need to 1949a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1950a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1951a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 19524e6045f1SJohannes Berg */ 19534d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 19544d82a1deSPeter Zijlstra 19554d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 19564e6045f1SJohannes Berg #endif 19576fec10a1STejun Heo /* 19586fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 19596fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 19606fec10a1STejun Heo * unbound or a disassociated gcwq. 19616fec10a1STejun Heo */ 196225511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 19636fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 196425511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 196525511a47STejun Heo 19667e11629dSTejun Heo /* 19677e11629dSTejun Heo * A single work shouldn't be executed concurrently by 19687e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 19697e11629dSTejun Heo * already processing the work. If so, defer the work to the 19707e11629dSTejun Heo * currently executing one. 19717e11629dSTejun Heo */ 19727e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 19737e11629dSTejun Heo if (unlikely(collision)) { 19747e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 19757e11629dSTejun Heo return; 19767e11629dSTejun Heo } 19771da177e4SLinus Torvalds 1978a62428c0STejun Heo /* claim and process */ 19791da177e4SLinus Torvalds debug_work_deactivate(work); 1980c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 1981c34056a3STejun Heo worker->current_work = work; 19828cca0eeaSTejun Heo worker->current_cwq = cwq; 198373f53c4aSTejun Heo work_color = get_work_color(work); 19847a22ad75STejun Heo 19857a22ad75STejun Heo /* record the current cpu number in the work data and dequeue */ 19867a22ad75STejun Heo set_work_cpu(work, gcwq->cpu); 1987a62428c0STejun Heo list_del_init(&work->entry); 1988a62428c0STejun Heo 1989649027d7STejun Heo /* 1990fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 1991fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 1992fb0e7bebSTejun Heo */ 1993fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 1994fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 1995fb0e7bebSTejun Heo 1996974271c4STejun Heo /* 1997974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 1998974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 1999974271c4STejun Heo */ 200063d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 200163d95a91STejun Heo wake_up_worker(pool); 2002974271c4STejun Heo 20038b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 20041da177e4SLinus Torvalds 200523b2e599SOleg Nesterov work_clear_pending(work); 2006e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 20073295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2008e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 200965f27f38SDavid Howells f(work); 2010e36c886aSArjan van de Ven /* 2011e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2012e36c886aSArjan van de Ven * point will only record its address. 2013e36c886aSArjan van de Ven */ 2014e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20153295f0efSIngo Molnar lock_map_release(&lockdep_map); 20163295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 20171da177e4SLinus Torvalds 2018d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2019d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2020d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2021a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2022d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2023d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2024d5abe669SPeter Zijlstra debug_show_held_locks(current); 2025d5abe669SPeter Zijlstra dump_stack(); 2026d5abe669SPeter Zijlstra } 2027d5abe669SPeter Zijlstra 20288b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2029a62428c0STejun Heo 2030fb0e7bebSTejun Heo /* clear cpu intensive status */ 2031fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2032fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2033fb0e7bebSTejun Heo 2034a62428c0STejun Heo /* we're done with it, release */ 2035c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2036c34056a3STejun Heo worker->current_work = NULL; 20378cca0eeaSTejun Heo worker->current_cwq = NULL; 20388a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 2041affee4b2STejun Heo /** 2042affee4b2STejun Heo * process_scheduled_works - process scheduled works 2043affee4b2STejun Heo * @worker: self 2044affee4b2STejun Heo * 2045affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2046affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2047affee4b2STejun Heo * fetches a work from the top and executes it. 2048affee4b2STejun Heo * 2049affee4b2STejun Heo * CONTEXT: 20508b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2051affee4b2STejun Heo * multiple times. 2052affee4b2STejun Heo */ 2053affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 20541da177e4SLinus Torvalds { 2055affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2056affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2057a62428c0STejun Heo struct work_struct, entry); 2058c34056a3STejun Heo process_one_work(worker, work); 2059a62428c0STejun Heo } 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 20624690c4abSTejun Heo /** 20634690c4abSTejun Heo * worker_thread - the worker thread function 2064c34056a3STejun Heo * @__worker: self 20654690c4abSTejun Heo * 2066e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2067e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2068e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2069e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2070e22bee78STejun Heo * rescuer_thread(). 20714690c4abSTejun Heo */ 2072c34056a3STejun Heo static int worker_thread(void *__worker) 20731da177e4SLinus Torvalds { 2074c34056a3STejun Heo struct worker *worker = __worker; 2075bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2076bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 20771da177e4SLinus Torvalds 2078e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2079e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2080c8e55f36STejun Heo woke_up: 20818b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2082affee4b2STejun Heo 208325511a47STejun Heo /* 208425511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 208525511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 208625511a47STejun Heo */ 208725511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2088c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 208925511a47STejun Heo 209025511a47STejun Heo if (worker->flags & WORKER_DIE) { 2091e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2092c8e55f36STejun Heo return 0; 2093c8e55f36STejun Heo } 2094c8e55f36STejun Heo 209525511a47STejun Heo idle_worker_rebind(worker); 209625511a47STejun Heo goto woke_up; 209725511a47STejun Heo } 209825511a47STejun Heo 2099c8e55f36STejun Heo worker_leave_idle(worker); 2100db7bccf4STejun Heo recheck: 2101e22bee78STejun Heo /* no more worker necessary? */ 210263d95a91STejun Heo if (!need_more_worker(pool)) 2103e22bee78STejun Heo goto sleep; 2104e22bee78STejun Heo 2105e22bee78STejun Heo /* do we need to manage? */ 210663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2107e22bee78STejun Heo goto recheck; 2108e22bee78STejun Heo 2109c8e55f36STejun Heo /* 2110c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2111c8e55f36STejun Heo * preparing to process a work or actually processing it. 2112c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2113c8e55f36STejun Heo */ 2114c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2115c8e55f36STejun Heo 2116e22bee78STejun Heo /* 2117e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2118e22bee78STejun Heo * at least one idle worker or that someone else has already 2119e22bee78STejun Heo * assumed the manager role. 2120e22bee78STejun Heo */ 2121e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2122e22bee78STejun Heo 2123e22bee78STejun Heo do { 2124affee4b2STejun Heo struct work_struct *work = 2125bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2126affee4b2STejun Heo struct work_struct, entry); 2127affee4b2STejun Heo 2128c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2129affee4b2STejun Heo /* optimization path, not strictly necessary */ 2130affee4b2STejun Heo process_one_work(worker, work); 2131affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2132affee4b2STejun Heo process_scheduled_works(worker); 2133affee4b2STejun Heo } else { 2134c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2135affee4b2STejun Heo process_scheduled_works(worker); 2136affee4b2STejun Heo } 213763d95a91STejun Heo } while (keep_working(pool)); 2138affee4b2STejun Heo 2139e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2140d313dd85STejun Heo sleep: 214163d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2142e22bee78STejun Heo goto recheck; 2143d313dd85STejun Heo 2144c8e55f36STejun Heo /* 2145e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2146e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2147e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2148e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2149e22bee78STejun Heo * prevent losing any event. 2150c8e55f36STejun Heo */ 2151c8e55f36STejun Heo worker_enter_idle(worker); 2152c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 21538b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 21541da177e4SLinus Torvalds schedule(); 2155c8e55f36STejun Heo goto woke_up; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 2158e22bee78STejun Heo /** 2159e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2160e22bee78STejun Heo * @__wq: the associated workqueue 2161e22bee78STejun Heo * 2162e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2163e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2164e22bee78STejun Heo * 2165e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2166e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2167e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2168e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2169e22bee78STejun Heo * the problem rescuer solves. 2170e22bee78STejun Heo * 2171e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2172e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2173e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2174e22bee78STejun Heo * 2175e22bee78STejun Heo * This should happen rarely. 2176e22bee78STejun Heo */ 2177e22bee78STejun Heo static int rescuer_thread(void *__wq) 2178e22bee78STejun Heo { 2179e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2180e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2181e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2182f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2183e22bee78STejun Heo unsigned int cpu; 2184e22bee78STejun Heo 2185e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2186e22bee78STejun Heo repeat: 2187e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds if (kthread_should_stop()) 2190e22bee78STejun Heo return 0; 21911da177e4SLinus Torvalds 2192f3421797STejun Heo /* 2193f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2194f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2195f3421797STejun Heo */ 2196f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2197f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2198f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2199bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2200bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2201e22bee78STejun Heo struct work_struct *work, *n; 2202e22bee78STejun Heo 2203e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2204f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2205e22bee78STejun Heo 2206e22bee78STejun Heo /* migrate to the target cpu if possible */ 2207bd7bdd43STejun Heo rescuer->pool = pool; 2208e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2209e22bee78STejun Heo 2210e22bee78STejun Heo /* 2211e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2212e22bee78STejun Heo * process'em. 2213e22bee78STejun Heo */ 2214e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2215bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2216e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2217e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2218e22bee78STejun Heo 2219e22bee78STejun Heo process_scheduled_works(rescuer); 22207576958aSTejun Heo 22217576958aSTejun Heo /* 22227576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 22237576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22247576958aSTejun Heo * and stalling the execution. 22257576958aSTejun Heo */ 222663d95a91STejun Heo if (keep_working(pool)) 222763d95a91STejun Heo wake_up_worker(pool); 22287576958aSTejun Heo 2229e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 2232e22bee78STejun Heo schedule(); 2233e22bee78STejun Heo goto repeat; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 2236fc2e4d70SOleg Nesterov struct wq_barrier { 2237fc2e4d70SOleg Nesterov struct work_struct work; 2238fc2e4d70SOleg Nesterov struct completion done; 2239fc2e4d70SOleg Nesterov }; 2240fc2e4d70SOleg Nesterov 2241fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2242fc2e4d70SOleg Nesterov { 2243fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2244fc2e4d70SOleg Nesterov complete(&barr->done); 2245fc2e4d70SOleg Nesterov } 2246fc2e4d70SOleg Nesterov 22474690c4abSTejun Heo /** 22484690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 22494690c4abSTejun Heo * @cwq: cwq to insert barrier into 22504690c4abSTejun Heo * @barr: wq_barrier to insert 2251affee4b2STejun Heo * @target: target work to attach @barr to 2252affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 22534690c4abSTejun Heo * 2254affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2255affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2256affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2257affee4b2STejun Heo * cpu. 2258affee4b2STejun Heo * 2259affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2260affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2261affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2262affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2263affee4b2STejun Heo * after a work with LINKED flag set. 2264affee4b2STejun Heo * 2265affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2266affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 22674690c4abSTejun Heo * 22684690c4abSTejun Heo * CONTEXT: 22698b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 22704690c4abSTejun Heo */ 227183c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2272affee4b2STejun Heo struct wq_barrier *barr, 2273affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2274fc2e4d70SOleg Nesterov { 2275affee4b2STejun Heo struct list_head *head; 2276affee4b2STejun Heo unsigned int linked = 0; 2277affee4b2STejun Heo 2278dc186ad7SThomas Gleixner /* 22798b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2280dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2281dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2282dc186ad7SThomas Gleixner * might deadlock. 2283dc186ad7SThomas Gleixner */ 2284ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 228522df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2286fc2e4d70SOleg Nesterov init_completion(&barr->done); 228783c22520SOleg Nesterov 2288affee4b2STejun Heo /* 2289affee4b2STejun Heo * If @target is currently being executed, schedule the 2290affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2291affee4b2STejun Heo */ 2292affee4b2STejun Heo if (worker) 2293affee4b2STejun Heo head = worker->scheduled.next; 2294affee4b2STejun Heo else { 2295affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2296affee4b2STejun Heo 2297affee4b2STejun Heo head = target->entry.next; 2298affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2299affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2300affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2301affee4b2STejun Heo } 2302affee4b2STejun Heo 2303dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2304affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2305affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2306fc2e4d70SOleg Nesterov } 2307fc2e4d70SOleg Nesterov 230873f53c4aSTejun Heo /** 230973f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 231073f53c4aSTejun Heo * @wq: workqueue being flushed 231173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 231273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 231373f53c4aSTejun Heo * 231473f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 231573f53c4aSTejun Heo * 231673f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 231773f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 231873f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 231973f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 232073f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 232173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 232273f53c4aSTejun Heo * 232373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 232473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 232573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 232673f53c4aSTejun Heo * is returned. 232773f53c4aSTejun Heo * 232873f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 232973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 233073f53c4aSTejun Heo * advanced to @work_color. 233173f53c4aSTejun Heo * 233273f53c4aSTejun Heo * CONTEXT: 233373f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 233473f53c4aSTejun Heo * 233573f53c4aSTejun Heo * RETURNS: 233673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 233773f53c4aSTejun Heo * otherwise. 233873f53c4aSTejun Heo */ 233973f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 234073f53c4aSTejun Heo int flush_color, int work_color) 23411da177e4SLinus Torvalds { 234273f53c4aSTejun Heo bool wait = false; 234373f53c4aSTejun Heo unsigned int cpu; 23441da177e4SLinus Torvalds 234573f53c4aSTejun Heo if (flush_color >= 0) { 234673f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 234773f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2348dc186ad7SThomas Gleixner } 234914441960SOleg Nesterov 2350f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 235173f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2352bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 23531da177e4SLinus Torvalds 23548b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 235573f53c4aSTejun Heo 235673f53c4aSTejun Heo if (flush_color >= 0) { 235773f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 235873f53c4aSTejun Heo 235973f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 236073f53c4aSTejun Heo cwq->flush_color = flush_color; 236173f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 236273f53c4aSTejun Heo wait = true; 23631da177e4SLinus Torvalds } 236473f53c4aSTejun Heo } 236573f53c4aSTejun Heo 236673f53c4aSTejun Heo if (work_color >= 0) { 236773f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 236873f53c4aSTejun Heo cwq->work_color = work_color; 236973f53c4aSTejun Heo } 237073f53c4aSTejun Heo 23718b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 237473f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 237573f53c4aSTejun Heo complete(&wq->first_flusher->done); 237673f53c4aSTejun Heo 237773f53c4aSTejun Heo return wait; 237883c22520SOleg Nesterov } 23791da177e4SLinus Torvalds 23800fcb78c2SRolf Eike Beer /** 23811da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 23820fcb78c2SRolf Eike Beer * @wq: workqueue to flush 23831da177e4SLinus Torvalds * 23841da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 23851da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 23861da177e4SLinus Torvalds * 2387fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2388fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 23891da177e4SLinus Torvalds */ 23907ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 23911da177e4SLinus Torvalds { 239273f53c4aSTejun Heo struct wq_flusher this_flusher = { 239373f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 239473f53c4aSTejun Heo .flush_color = -1, 239573f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 239673f53c4aSTejun Heo }; 239773f53c4aSTejun Heo int next_color; 2398b1f4ec17SOleg Nesterov 23993295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24003295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 240173f53c4aSTejun Heo 240273f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 240373f53c4aSTejun Heo 240473f53c4aSTejun Heo /* 240573f53c4aSTejun Heo * Start-to-wait phase 240673f53c4aSTejun Heo */ 240773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 240873f53c4aSTejun Heo 240973f53c4aSTejun Heo if (next_color != wq->flush_color) { 241073f53c4aSTejun Heo /* 241173f53c4aSTejun Heo * Color space is not full. The current work_color 241273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 241373f53c4aSTejun Heo * by one. 241473f53c4aSTejun Heo */ 241573f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 241673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 241773f53c4aSTejun Heo wq->work_color = next_color; 241873f53c4aSTejun Heo 241973f53c4aSTejun Heo if (!wq->first_flusher) { 242073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 242173f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 242273f53c4aSTejun Heo 242373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 242473f53c4aSTejun Heo 242573f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 242673f53c4aSTejun Heo wq->work_color)) { 242773f53c4aSTejun Heo /* nothing to flush, done */ 242873f53c4aSTejun Heo wq->flush_color = next_color; 242973f53c4aSTejun Heo wq->first_flusher = NULL; 243073f53c4aSTejun Heo goto out_unlock; 243173f53c4aSTejun Heo } 243273f53c4aSTejun Heo } else { 243373f53c4aSTejun Heo /* wait in queue */ 243473f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 243573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 243673f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 243773f53c4aSTejun Heo } 243873f53c4aSTejun Heo } else { 243973f53c4aSTejun Heo /* 244073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 244173f53c4aSTejun Heo * The next flush completion will assign us 244273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 244373f53c4aSTejun Heo */ 244473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 244573f53c4aSTejun Heo } 244673f53c4aSTejun Heo 244773f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 244873f53c4aSTejun Heo 244973f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 245073f53c4aSTejun Heo 245173f53c4aSTejun Heo /* 245273f53c4aSTejun Heo * Wake-up-and-cascade phase 245373f53c4aSTejun Heo * 245473f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 245573f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 245673f53c4aSTejun Heo */ 245773f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 245873f53c4aSTejun Heo return; 245973f53c4aSTejun Heo 246073f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 246173f53c4aSTejun Heo 24624ce48b37STejun Heo /* we might have raced, check again with mutex held */ 24634ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 24644ce48b37STejun Heo goto out_unlock; 24654ce48b37STejun Heo 246673f53c4aSTejun Heo wq->first_flusher = NULL; 246773f53c4aSTejun Heo 246873f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 246973f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 247073f53c4aSTejun Heo 247173f53c4aSTejun Heo while (true) { 247273f53c4aSTejun Heo struct wq_flusher *next, *tmp; 247373f53c4aSTejun Heo 247473f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 247573f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 247673f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 247773f53c4aSTejun Heo break; 247873f53c4aSTejun Heo list_del_init(&next->list); 247973f53c4aSTejun Heo complete(&next->done); 248073f53c4aSTejun Heo } 248173f53c4aSTejun Heo 248273f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 248373f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 248473f53c4aSTejun Heo 248573f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 248673f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 248773f53c4aSTejun Heo 248873f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 248973f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 249073f53c4aSTejun Heo /* 249173f53c4aSTejun Heo * Assign the same color to all overflowed 249273f53c4aSTejun Heo * flushers, advance work_color and append to 249373f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 249473f53c4aSTejun Heo * phase for these overflowed flushers. 249573f53c4aSTejun Heo */ 249673f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 249773f53c4aSTejun Heo tmp->flush_color = wq->work_color; 249873f53c4aSTejun Heo 249973f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 250073f53c4aSTejun Heo 250173f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 250273f53c4aSTejun Heo &wq->flusher_queue); 250373f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 250473f53c4aSTejun Heo } 250573f53c4aSTejun Heo 250673f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 250773f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 250873f53c4aSTejun Heo break; 250973f53c4aSTejun Heo } 251073f53c4aSTejun Heo 251173f53c4aSTejun Heo /* 251273f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 251373f53c4aSTejun Heo * the new first flusher and arm cwqs. 251473f53c4aSTejun Heo */ 251573f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 251673f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 251773f53c4aSTejun Heo 251873f53c4aSTejun Heo list_del_init(&next->list); 251973f53c4aSTejun Heo wq->first_flusher = next; 252073f53c4aSTejun Heo 252173f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 252273f53c4aSTejun Heo break; 252373f53c4aSTejun Heo 252473f53c4aSTejun Heo /* 252573f53c4aSTejun Heo * Meh... this color is already done, clear first 252673f53c4aSTejun Heo * flusher and repeat cascading. 252773f53c4aSTejun Heo */ 252873f53c4aSTejun Heo wq->first_flusher = NULL; 252973f53c4aSTejun Heo } 253073f53c4aSTejun Heo 253173f53c4aSTejun Heo out_unlock: 253273f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 25331da177e4SLinus Torvalds } 2534ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 25351da177e4SLinus Torvalds 25369c5a2ba7STejun Heo /** 25379c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 25389c5a2ba7STejun Heo * @wq: workqueue to drain 25399c5a2ba7STejun Heo * 25409c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 25419c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 25429c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 25439c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 25449c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 25459c5a2ba7STejun Heo * takes too long. 25469c5a2ba7STejun Heo */ 25479c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 25489c5a2ba7STejun Heo { 25499c5a2ba7STejun Heo unsigned int flush_cnt = 0; 25509c5a2ba7STejun Heo unsigned int cpu; 25519c5a2ba7STejun Heo 25529c5a2ba7STejun Heo /* 25539c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 25549c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 25559c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 25569c5a2ba7STejun Heo */ 25579c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25589c5a2ba7STejun Heo if (!wq->nr_drainers++) 25599c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 25609c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25619c5a2ba7STejun Heo reflush: 25629c5a2ba7STejun Heo flush_workqueue(wq); 25639c5a2ba7STejun Heo 25649c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 25659c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2566fa2563e4SThomas Tuttle bool drained; 25679c5a2ba7STejun Heo 2568bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2569fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2570bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2571fa2563e4SThomas Tuttle 2572fa2563e4SThomas Tuttle if (drained) 25739c5a2ba7STejun Heo continue; 25749c5a2ba7STejun Heo 25759c5a2ba7STejun Heo if (++flush_cnt == 10 || 25769c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 25779c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 25789c5a2ba7STejun Heo wq->name, flush_cnt); 25799c5a2ba7STejun Heo goto reflush; 25809c5a2ba7STejun Heo } 25819c5a2ba7STejun Heo 25829c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25839c5a2ba7STejun Heo if (!--wq->nr_drainers) 25849c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 25859c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25869c5a2ba7STejun Heo } 25879c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 25889c5a2ba7STejun Heo 2589baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2590baf59022STejun Heo bool wait_executing) 2591baf59022STejun Heo { 2592baf59022STejun Heo struct worker *worker = NULL; 2593baf59022STejun Heo struct global_cwq *gcwq; 2594baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2595baf59022STejun Heo 2596baf59022STejun Heo might_sleep(); 2597baf59022STejun Heo gcwq = get_work_gcwq(work); 2598baf59022STejun Heo if (!gcwq) 2599baf59022STejun Heo return false; 2600baf59022STejun Heo 2601baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2602baf59022STejun Heo if (!list_empty(&work->entry)) { 2603baf59022STejun Heo /* 2604baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2605baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2606baf59022STejun Heo * are not going to wait. 2607baf59022STejun Heo */ 2608baf59022STejun Heo smp_rmb(); 2609baf59022STejun Heo cwq = get_work_cwq(work); 2610bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2611baf59022STejun Heo goto already_gone; 2612baf59022STejun Heo } else if (wait_executing) { 2613baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2614baf59022STejun Heo if (!worker) 2615baf59022STejun Heo goto already_gone; 2616baf59022STejun Heo cwq = worker->current_cwq; 2617baf59022STejun Heo } else 2618baf59022STejun Heo goto already_gone; 2619baf59022STejun Heo 2620baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2621baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2622baf59022STejun Heo 2623e159489bSTejun Heo /* 2624e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2625e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2626e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2627e159489bSTejun Heo * access. 2628e159489bSTejun Heo */ 2629e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2630baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2631e159489bSTejun Heo else 2632e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2633baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2634e159489bSTejun Heo 2635baf59022STejun Heo return true; 2636baf59022STejun Heo already_gone: 2637baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2638baf59022STejun Heo return false; 2639baf59022STejun Heo } 2640baf59022STejun Heo 2641db700897SOleg Nesterov /** 2642401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2643401a8d04STejun Heo * @work: the work to flush 2644db700897SOleg Nesterov * 2645401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2646401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2647401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2648401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2649401a8d04STejun Heo * some of the CPUs from earlier queueing. 2650a67da70dSOleg Nesterov * 2651401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2652401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2653401a8d04STejun Heo * been requeued since flush started. 2654401a8d04STejun Heo * 2655401a8d04STejun Heo * RETURNS: 2656401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2657401a8d04STejun Heo * %false if it was already idle. 2658db700897SOleg Nesterov */ 2659401a8d04STejun Heo bool flush_work(struct work_struct *work) 2660db700897SOleg Nesterov { 2661db700897SOleg Nesterov struct wq_barrier barr; 2662db700897SOleg Nesterov 26630976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 26640976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 26650976dfc1SStephen Boyd 2666baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2667db700897SOleg Nesterov wait_for_completion(&barr.done); 2668dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2669401a8d04STejun Heo return true; 2670baf59022STejun Heo } else 2671401a8d04STejun Heo return false; 2672db700897SOleg Nesterov } 2673db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2674db700897SOleg Nesterov 2675401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2676401a8d04STejun Heo { 2677401a8d04STejun Heo struct wq_barrier barr; 2678401a8d04STejun Heo struct worker *worker; 2679401a8d04STejun Heo 2680401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2681401a8d04STejun Heo 2682401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2683401a8d04STejun Heo if (unlikely(worker)) 2684401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2685401a8d04STejun Heo 2686401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2687401a8d04STejun Heo 2688401a8d04STejun Heo if (unlikely(worker)) { 2689401a8d04STejun Heo wait_for_completion(&barr.done); 2690401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2691401a8d04STejun Heo return true; 2692401a8d04STejun Heo } else 2693401a8d04STejun Heo return false; 2694401a8d04STejun Heo } 2695401a8d04STejun Heo 2696401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2697401a8d04STejun Heo { 2698401a8d04STejun Heo bool ret = false; 2699401a8d04STejun Heo int cpu; 2700401a8d04STejun Heo 2701401a8d04STejun Heo might_sleep(); 2702401a8d04STejun Heo 2703401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2704401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2705401a8d04STejun Heo 2706401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2707401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2708401a8d04STejun Heo return ret; 2709401a8d04STejun Heo } 2710401a8d04STejun Heo 271109383498STejun Heo /** 271209383498STejun Heo * flush_work_sync - wait until a work has finished execution 271309383498STejun Heo * @work: the work to flush 271409383498STejun Heo * 271509383498STejun Heo * Wait until @work has finished execution. On return, it's 271609383498STejun Heo * guaranteed that all queueing instances of @work which happened 271709383498STejun Heo * before this function is called are finished. In other words, if 271809383498STejun Heo * @work hasn't been requeued since this function was called, @work is 271909383498STejun Heo * guaranteed to be idle on return. 272009383498STejun Heo * 272109383498STejun Heo * RETURNS: 272209383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 272309383498STejun Heo * %false if it was already idle. 272409383498STejun Heo */ 272509383498STejun Heo bool flush_work_sync(struct work_struct *work) 272609383498STejun Heo { 272709383498STejun Heo struct wq_barrier barr; 272809383498STejun Heo bool pending, waited; 272909383498STejun Heo 273009383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 273109383498STejun Heo pending = start_flush_work(work, &barr, false); 273209383498STejun Heo 273309383498STejun Heo /* wait for executions to finish */ 273409383498STejun Heo waited = wait_on_work(work); 273509383498STejun Heo 273609383498STejun Heo /* wait for the pending one */ 273709383498STejun Heo if (pending) { 273809383498STejun Heo wait_for_completion(&barr.done); 273909383498STejun Heo destroy_work_on_stack(&barr.work); 274009383498STejun Heo } 274109383498STejun Heo 274209383498STejun Heo return pending || waited; 274309383498STejun Heo } 274409383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 274509383498STejun Heo 27466e84d644SOleg Nesterov /* 27471f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 27486e84d644SOleg Nesterov * so this work can't be re-armed in any way. 27496e84d644SOleg Nesterov */ 27506e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 27516e84d644SOleg Nesterov { 27528b03ae3cSTejun Heo struct global_cwq *gcwq; 27531f1f642eSOleg Nesterov int ret = -1; 27546e84d644SOleg Nesterov 275522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 27561f1f642eSOleg Nesterov return 0; 27576e84d644SOleg Nesterov 27586e84d644SOleg Nesterov /* 27596e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 27606e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 27616e84d644SOleg Nesterov */ 27627a22ad75STejun Heo gcwq = get_work_gcwq(work); 27637a22ad75STejun Heo if (!gcwq) 27646e84d644SOleg Nesterov return ret; 27656e84d644SOleg Nesterov 27668b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 27676e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 27686e84d644SOleg Nesterov /* 27697a22ad75STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 27706e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 27716e84d644SOleg Nesterov * insert_work()->wmb(). 27726e84d644SOleg Nesterov */ 27736e84d644SOleg Nesterov smp_rmb(); 27747a22ad75STejun Heo if (gcwq == get_work_gcwq(work)) { 2775dc186ad7SThomas Gleixner debug_work_deactivate(work); 27766e84d644SOleg Nesterov list_del_init(&work->entry); 27777a22ad75STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 27788a2e8e5dSTejun Heo get_work_color(work), 27798a2e8e5dSTejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 27806e84d644SOleg Nesterov ret = 1; 27816e84d644SOleg Nesterov } 27826e84d644SOleg Nesterov } 27838b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 27846e84d644SOleg Nesterov 27856e84d644SOleg Nesterov return ret; 27866e84d644SOleg Nesterov } 27876e84d644SOleg Nesterov 2788401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work, 27891f1f642eSOleg Nesterov struct timer_list* timer) 27901f1f642eSOleg Nesterov { 27911f1f642eSOleg Nesterov int ret; 27921f1f642eSOleg Nesterov 27931f1f642eSOleg Nesterov do { 27941f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 27951f1f642eSOleg Nesterov if (!ret) 27961f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 27971f1f642eSOleg Nesterov wait_on_work(work); 27981f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 27991f1f642eSOleg Nesterov 28007a22ad75STejun Heo clear_work_data(work); 28011f1f642eSOleg Nesterov return ret; 28021f1f642eSOleg Nesterov } 28031f1f642eSOleg Nesterov 28046e84d644SOleg Nesterov /** 2805401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2806401a8d04STejun Heo * @work: the work to cancel 28076e84d644SOleg Nesterov * 2808401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2809401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2810401a8d04STejun Heo * another workqueue. On return from this function, @work is 2811401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28121f1f642eSOleg Nesterov * 2813401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2814401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28156e84d644SOleg Nesterov * 2816401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28176e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2818401a8d04STejun Heo * 2819401a8d04STejun Heo * RETURNS: 2820401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28216e84d644SOleg Nesterov */ 2822401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28236e84d644SOleg Nesterov { 28241f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 2825b89deed3SOleg Nesterov } 282628e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2827b89deed3SOleg Nesterov 28286e84d644SOleg Nesterov /** 2829401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2830401a8d04STejun Heo * @dwork: the delayed work to flush 28316e84d644SOleg Nesterov * 2832401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2833401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2834401a8d04STejun Heo * considers the last queueing instance of @dwork. 28351f1f642eSOleg Nesterov * 2836401a8d04STejun Heo * RETURNS: 2837401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2838401a8d04STejun Heo * %false if it was already idle. 28396e84d644SOleg Nesterov */ 2840401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2841401a8d04STejun Heo { 2842401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 2843401a8d04STejun Heo __queue_work(raw_smp_processor_id(), 2844401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 2845401a8d04STejun Heo return flush_work(&dwork->work); 2846401a8d04STejun Heo } 2847401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2848401a8d04STejun Heo 2849401a8d04STejun Heo /** 285009383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 285109383498STejun Heo * @dwork: the delayed work to flush 285209383498STejun Heo * 285309383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 285409383498STejun Heo * execution immediately. Other than timer handling, its behavior 285509383498STejun Heo * is identical to flush_work_sync(). 285609383498STejun Heo * 285709383498STejun Heo * RETURNS: 285809383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 285909383498STejun Heo * %false if it was already idle. 286009383498STejun Heo */ 286109383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 286209383498STejun Heo { 286309383498STejun Heo if (del_timer_sync(&dwork->timer)) 286409383498STejun Heo __queue_work(raw_smp_processor_id(), 286509383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 286609383498STejun Heo return flush_work_sync(&dwork->work); 286709383498STejun Heo } 286809383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 286909383498STejun Heo 287009383498STejun Heo /** 2871401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2872401a8d04STejun Heo * @dwork: the delayed work cancel 2873401a8d04STejun Heo * 2874401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2875401a8d04STejun Heo * 2876401a8d04STejun Heo * RETURNS: 2877401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2878401a8d04STejun Heo */ 2879401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 28806e84d644SOleg Nesterov { 28811f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 28826e84d644SOleg Nesterov } 2883f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 28841da177e4SLinus Torvalds 28850fcb78c2SRolf Eike Beer /** 28860fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 28870fcb78c2SRolf Eike Beer * @work: job to be done 28880fcb78c2SRolf Eike Beer * 28895b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 28905b0f437dSBart Van Assche * non-zero otherwise. 28915b0f437dSBart Van Assche * 28925b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 28935b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 28945b0f437dSBart Van Assche * workqueue otherwise. 28950fcb78c2SRolf Eike Beer */ 28967ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 28971da177e4SLinus Torvalds { 2898d320c038STejun Heo return queue_work(system_wq, work); 28991da177e4SLinus Torvalds } 2900ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 29011da177e4SLinus Torvalds 2902c1a220e7SZhang Rui /* 2903c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2904c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2905c1a220e7SZhang Rui * @work: job to be done 2906c1a220e7SZhang Rui * 2907c1a220e7SZhang Rui * This puts a job on a specific cpu 2908c1a220e7SZhang Rui */ 2909c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 2910c1a220e7SZhang Rui { 2911d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2912c1a220e7SZhang Rui } 2913c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2914c1a220e7SZhang Rui 29150fcb78c2SRolf Eike Beer /** 29160fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 291752bad64dSDavid Howells * @dwork: job to be done 291852bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 29190fcb78c2SRolf Eike Beer * 29200fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29210fcb78c2SRolf Eike Beer * workqueue. 29220fcb78c2SRolf Eike Beer */ 29237ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 292482f67cd9SIngo Molnar unsigned long delay) 29251da177e4SLinus Torvalds { 2926d320c038STejun Heo return queue_delayed_work(system_wq, dwork, delay); 29271da177e4SLinus Torvalds } 2928ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 29291da177e4SLinus Torvalds 29300fcb78c2SRolf Eike Beer /** 29310fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29320fcb78c2SRolf Eike Beer * @cpu: cpu to use 293352bad64dSDavid Howells * @dwork: job to be done 29340fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29350fcb78c2SRolf Eike Beer * 29360fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29370fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29380fcb78c2SRolf Eike Beer */ 29391da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 294052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 29411da177e4SLinus Torvalds { 2942d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29431da177e4SLinus Torvalds } 2944ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29451da177e4SLinus Torvalds 2946b6136773SAndrew Morton /** 294731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2948b6136773SAndrew Morton * @func: the function to call 2949b6136773SAndrew Morton * 295031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 295131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2952b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 295331ddd871STejun Heo * 295431ddd871STejun Heo * RETURNS: 295531ddd871STejun Heo * 0 on success, -errno on failure. 2956b6136773SAndrew Morton */ 295765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 295815316ba8SChristoph Lameter { 295915316ba8SChristoph Lameter int cpu; 296038f51568SNamhyung Kim struct work_struct __percpu *works; 296115316ba8SChristoph Lameter 2962b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2963b6136773SAndrew Morton if (!works) 296415316ba8SChristoph Lameter return -ENOMEM; 2965b6136773SAndrew Morton 296695402b38SGautham R Shenoy get_online_cpus(); 296793981800STejun Heo 296815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29699bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29709bfb1839SIngo Molnar 29719bfb1839SIngo Molnar INIT_WORK(work, func); 29728de6d308SOleg Nesterov schedule_work_on(cpu, work); 297315316ba8SChristoph Lameter } 297493981800STejun Heo 297593981800STejun Heo for_each_online_cpu(cpu) 29768616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 297793981800STejun Heo 297895402b38SGautham R Shenoy put_online_cpus(); 2979b6136773SAndrew Morton free_percpu(works); 298015316ba8SChristoph Lameter return 0; 298115316ba8SChristoph Lameter } 298215316ba8SChristoph Lameter 2983eef6a7d5SAlan Stern /** 2984eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 2985eef6a7d5SAlan Stern * 2986eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 2987eef6a7d5SAlan Stern * completion. 2988eef6a7d5SAlan Stern * 2989eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 2990eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 2991eef6a7d5SAlan Stern * will lead to deadlock: 2992eef6a7d5SAlan Stern * 2993eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 2994eef6a7d5SAlan Stern * a lock held by your code or its caller. 2995eef6a7d5SAlan Stern * 2996eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 2997eef6a7d5SAlan Stern * 2998eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 2999eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3000eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3001eef6a7d5SAlan Stern * 3002eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3003eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3004eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3005eef6a7d5SAlan Stern * cancel_work_sync() instead. 3006eef6a7d5SAlan Stern */ 30071da177e4SLinus Torvalds void flush_scheduled_work(void) 30081da177e4SLinus Torvalds { 3009d320c038STejun Heo flush_workqueue(system_wq); 30101da177e4SLinus Torvalds } 3011ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds /** 30141fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30151fa44ecaSJames Bottomley * @fn: the function to execute 30161fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30171fa44ecaSJames Bottomley * be available when the work executes) 30181fa44ecaSJames Bottomley * 30191fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30201fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30211fa44ecaSJames Bottomley * 30221fa44ecaSJames Bottomley * Returns: 0 - function was executed 30231fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30241fa44ecaSJames Bottomley */ 302565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30261fa44ecaSJames Bottomley { 30271fa44ecaSJames Bottomley if (!in_interrupt()) { 302865f27f38SDavid Howells fn(&ew->work); 30291fa44ecaSJames Bottomley return 0; 30301fa44ecaSJames Bottomley } 30311fa44ecaSJames Bottomley 303265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30331fa44ecaSJames Bottomley schedule_work(&ew->work); 30341fa44ecaSJames Bottomley 30351fa44ecaSJames Bottomley return 1; 30361fa44ecaSJames Bottomley } 30371fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30381fa44ecaSJames Bottomley 30391da177e4SLinus Torvalds int keventd_up(void) 30401da177e4SLinus Torvalds { 3041d320c038STejun Heo return system_wq != NULL; 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 3044bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30451da177e4SLinus Torvalds { 30463af24433SOleg Nesterov /* 30470f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30480f900049STejun Heo * Make sure that the alignment isn't lower than that of 30490f900049STejun Heo * unsigned long long. 30503af24433SOleg Nesterov */ 30510f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 30520f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 30530f900049STejun Heo __alignof__(unsigned long long)); 30543af24433SOleg Nesterov 3055e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3056f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3057931ac77eSTejun Heo else { 30580f900049STejun Heo void *ptr; 3059e1d8aa9fSFrederic Weisbecker 30600f900049STejun Heo /* 3061f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3062f3421797STejun Heo * pointer at the end pointing back to the originally 3063f3421797STejun Heo * allocated pointer which will be used for free. 30640f900049STejun Heo */ 3065bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3066bdbc5dd7STejun Heo if (ptr) { 3067bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3068bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3069bdbc5dd7STejun Heo } 30703af24433SOleg Nesterov } 30713af24433SOleg Nesterov 30720415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3073bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3074bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 30750f900049STejun Heo } 30760f900049STejun Heo 3077bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 307806ba38a9SOleg Nesterov { 3079e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3080bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3081f3421797STejun Heo else if (wq->cpu_wq.single) { 3082f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3083f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 308406ba38a9SOleg Nesterov } 308506ba38a9SOleg Nesterov } 308606ba38a9SOleg Nesterov 3087f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3088f3421797STejun Heo const char *name) 3089b71ab8c2STejun Heo { 3090f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3091f3421797STejun Heo 3092f3421797STejun Heo if (max_active < 1 || max_active > lim) 3093b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3094b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3095f3421797STejun Heo max_active, name, 1, lim); 3096b71ab8c2STejun Heo 3097f3421797STejun Heo return clamp_val(max_active, 1, lim); 3098b71ab8c2STejun Heo } 3099b71ab8c2STejun Heo 3100b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 310197e37d7bSTejun Heo unsigned int flags, 31021e19ffc6STejun Heo int max_active, 3103eb13ba87SJohannes Berg struct lock_class_key *key, 3104b196be89STejun Heo const char *lock_name, ...) 31053af24433SOleg Nesterov { 3106b196be89STejun Heo va_list args, args1; 31073af24433SOleg Nesterov struct workqueue_struct *wq; 3108c34056a3STejun Heo unsigned int cpu; 3109b196be89STejun Heo size_t namelen; 3110b196be89STejun Heo 3111b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3112b196be89STejun Heo va_start(args, lock_name); 3113b196be89STejun Heo va_copy(args1, args); 3114b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3115b196be89STejun Heo 3116b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3117b196be89STejun Heo if (!wq) 3118b196be89STejun Heo goto err; 3119b196be89STejun Heo 3120b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3121b196be89STejun Heo va_end(args); 3122b196be89STejun Heo va_end(args1); 31233af24433SOleg Nesterov 3124f3421797STejun Heo /* 31256370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31266370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31276370a6adSTejun Heo */ 31286370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31296370a6adSTejun Heo flags |= WQ_RESCUER; 31306370a6adSTejun Heo 3131d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3132b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31333af24433SOleg Nesterov 3134b196be89STejun Heo /* init wq */ 313597e37d7bSTejun Heo wq->flags = flags; 3136a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 313773f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 313873f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 313973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 314073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31413af24433SOleg Nesterov 3142eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3143cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31443af24433SOleg Nesterov 3145bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3146bdbc5dd7STejun Heo goto err; 3147bdbc5dd7STejun Heo 3148f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 31491537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 31508b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 31513270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 31521537663fSTejun Heo 31530f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 31543270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3155c34056a3STejun Heo cwq->wq = wq; 315673f53c4aSTejun Heo cwq->flush_color = -1; 31571e19ffc6STejun Heo cwq->max_active = max_active; 31581e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3159e22bee78STejun Heo } 31601537663fSTejun Heo 3161e22bee78STejun Heo if (flags & WQ_RESCUER) { 3162e22bee78STejun Heo struct worker *rescuer; 3163e22bee78STejun Heo 3164f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3165e22bee78STejun Heo goto err; 3166e22bee78STejun Heo 3167e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3168e22bee78STejun Heo if (!rescuer) 3169e22bee78STejun Heo goto err; 3170e22bee78STejun Heo 3171b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3172b196be89STejun Heo wq->name); 3173e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3174e22bee78STejun Heo goto err; 3175e22bee78STejun Heo 3176e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3177e22bee78STejun Heo wake_up_process(rescuer->task); 31783af24433SOleg Nesterov } 31791537663fSTejun Heo 31803af24433SOleg Nesterov /* 3181a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3182a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3183a0a1a5fdSTejun Heo * workqueue to workqueues list. 31843af24433SOleg Nesterov */ 31853af24433SOleg Nesterov spin_lock(&workqueue_lock); 3186a0a1a5fdSTejun Heo 318758a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3188f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3189a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3190a0a1a5fdSTejun Heo 31913af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3192a0a1a5fdSTejun Heo 31933af24433SOleg Nesterov spin_unlock(&workqueue_lock); 31943af24433SOleg Nesterov 31953af24433SOleg Nesterov return wq; 31964690c4abSTejun Heo err: 31974690c4abSTejun Heo if (wq) { 3198bdbc5dd7STejun Heo free_cwqs(wq); 3199f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3200e22bee78STejun Heo kfree(wq->rescuer); 32014690c4abSTejun Heo kfree(wq); 32023af24433SOleg Nesterov } 32034690c4abSTejun Heo return NULL; 32041da177e4SLinus Torvalds } 3205d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32061da177e4SLinus Torvalds 32073af24433SOleg Nesterov /** 32083af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32093af24433SOleg Nesterov * @wq: target workqueue 32103af24433SOleg Nesterov * 32113af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32123af24433SOleg Nesterov */ 32133af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32143af24433SOleg Nesterov { 3215c8e55f36STejun Heo unsigned int cpu; 32163af24433SOleg Nesterov 32179c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32189c5a2ba7STejun Heo drain_workqueue(wq); 3219c8efcc25STejun Heo 3220a0a1a5fdSTejun Heo /* 3221a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3222a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3223a0a1a5fdSTejun Heo */ 322495402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32253af24433SOleg Nesterov list_del(&wq->list); 322695402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32273af24433SOleg Nesterov 3228e22bee78STejun Heo /* sanity check */ 3229f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 323073f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 323173f53c4aSTejun Heo int i; 32323af24433SOleg Nesterov 323373f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 323473f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32351e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32361e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 323773f53c4aSTejun Heo } 32381537663fSTejun Heo 3239e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3240e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3241f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32428d9df9f0SXiaotian Feng kfree(wq->rescuer); 3243e22bee78STejun Heo } 3244e22bee78STejun Heo 3245bdbc5dd7STejun Heo free_cwqs(wq); 32463af24433SOleg Nesterov kfree(wq); 32473af24433SOleg Nesterov } 32483af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32493af24433SOleg Nesterov 3250dcd989cbSTejun Heo /** 3251dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3252dcd989cbSTejun Heo * @wq: target workqueue 3253dcd989cbSTejun Heo * @max_active: new max_active value. 3254dcd989cbSTejun Heo * 3255dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3256dcd989cbSTejun Heo * 3257dcd989cbSTejun Heo * CONTEXT: 3258dcd989cbSTejun Heo * Don't call from IRQ context. 3259dcd989cbSTejun Heo */ 3260dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3261dcd989cbSTejun Heo { 3262dcd989cbSTejun Heo unsigned int cpu; 3263dcd989cbSTejun Heo 3264f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3265dcd989cbSTejun Heo 3266dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3267dcd989cbSTejun Heo 3268dcd989cbSTejun Heo wq->saved_max_active = max_active; 3269dcd989cbSTejun Heo 3270f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3271dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3272dcd989cbSTejun Heo 3273dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3274dcd989cbSTejun Heo 327558a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3276dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3277dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3278dcd989cbSTejun Heo 3279dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3280dcd989cbSTejun Heo } 3281dcd989cbSTejun Heo 3282dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3283dcd989cbSTejun Heo } 3284dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3285dcd989cbSTejun Heo 3286dcd989cbSTejun Heo /** 3287dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3288dcd989cbSTejun Heo * @cpu: CPU in question 3289dcd989cbSTejun Heo * @wq: target workqueue 3290dcd989cbSTejun Heo * 3291dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3292dcd989cbSTejun Heo * no synchronization around this function and the test result is 3293dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3294dcd989cbSTejun Heo * 3295dcd989cbSTejun Heo * RETURNS: 3296dcd989cbSTejun Heo * %true if congested, %false otherwise. 3297dcd989cbSTejun Heo */ 3298dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3299dcd989cbSTejun Heo { 3300dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3301dcd989cbSTejun Heo 3302dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3303dcd989cbSTejun Heo } 3304dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3305dcd989cbSTejun Heo 3306dcd989cbSTejun Heo /** 3307dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3308dcd989cbSTejun Heo * @work: the work of interest 3309dcd989cbSTejun Heo * 3310dcd989cbSTejun Heo * RETURNS: 3311bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3312dcd989cbSTejun Heo */ 3313dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3314dcd989cbSTejun Heo { 3315dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3316dcd989cbSTejun Heo 3317bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3318dcd989cbSTejun Heo } 3319dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3320dcd989cbSTejun Heo 3321dcd989cbSTejun Heo /** 3322dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3323dcd989cbSTejun Heo * @work: the work to be tested 3324dcd989cbSTejun Heo * 3325dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3326dcd989cbSTejun Heo * synchronization around this function and the test result is 3327dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3328dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3329dcd989cbSTejun Heo * running state. 3330dcd989cbSTejun Heo * 3331dcd989cbSTejun Heo * RETURNS: 3332dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3333dcd989cbSTejun Heo */ 3334dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3335dcd989cbSTejun Heo { 3336dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3337dcd989cbSTejun Heo unsigned long flags; 3338dcd989cbSTejun Heo unsigned int ret = 0; 3339dcd989cbSTejun Heo 3340dcd989cbSTejun Heo if (!gcwq) 3341dcd989cbSTejun Heo return false; 3342dcd989cbSTejun Heo 3343dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3344dcd989cbSTejun Heo 3345dcd989cbSTejun Heo if (work_pending(work)) 3346dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3347dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3348dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3349dcd989cbSTejun Heo 3350dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3351dcd989cbSTejun Heo 3352dcd989cbSTejun Heo return ret; 3353dcd989cbSTejun Heo } 3354dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3355dcd989cbSTejun Heo 3356db7bccf4STejun Heo /* 3357db7bccf4STejun Heo * CPU hotplug. 3358db7bccf4STejun Heo * 3359e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3360e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3361e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3362e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3363e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3364e22bee78STejun Heo * blocked draining impractical. 3365e22bee78STejun Heo * 3366628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3367628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3368628c78e7STejun Heo * cpu comes back online. 3369db7bccf4STejun Heo */ 3370db7bccf4STejun Heo 337160373152STejun Heo /* claim manager positions of all pools */ 33728db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 337360373152STejun Heo { 337460373152STejun Heo struct worker_pool *pool; 337560373152STejun Heo 337660373152STejun Heo for_each_worker_pool(pool, gcwq) 337760373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 33788db25e78STejun Heo spin_lock_irq(&gcwq->lock); 337960373152STejun Heo } 338060373152STejun Heo 338160373152STejun Heo /* release manager positions */ 33828db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 338360373152STejun Heo { 338460373152STejun Heo struct worker_pool *pool; 338560373152STejun Heo 33868db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 338760373152STejun Heo for_each_worker_pool(pool, gcwq) 338860373152STejun Heo mutex_unlock(&pool->manager_mutex); 338960373152STejun Heo } 339060373152STejun Heo 3391628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3392db7bccf4STejun Heo { 3393628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 33944ce62e9eSTejun Heo struct worker_pool *pool; 3395db7bccf4STejun Heo struct worker *worker; 3396db7bccf4STejun Heo struct hlist_node *pos; 3397db7bccf4STejun Heo int i; 3398db7bccf4STejun Heo 3399db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3400db7bccf4STejun Heo 34018db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3402e22bee78STejun Heo 3403f2d5a0eeSTejun Heo /* 3404f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3405f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3406f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3407f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3408f2d5a0eeSTejun Heo */ 340960373152STejun Heo for_each_worker_pool(pool, gcwq) 34104ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3411403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3412db7bccf4STejun Heo 3413db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3414403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3415db7bccf4STejun Heo 3416f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3417f2d5a0eeSTejun Heo 34188db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3419e22bee78STejun Heo 3420e22bee78STejun Heo /* 3421628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3422628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3423628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3424628c78e7STejun Heo */ 3425628c78e7STejun Heo schedule(); 3426628c78e7STejun Heo 3427628c78e7STejun Heo /* 3428628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3429628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3430628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3431628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3432628c78e7STejun Heo * which is served by workers tied to the CPU. 3433628c78e7STejun Heo * 3434628c78e7STejun Heo * On return from this function, the current worker would trigger 3435628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3436628c78e7STejun Heo * didn't already. 3437e22bee78STejun Heo */ 34384ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34394ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3440db7bccf4STejun Heo } 3441db7bccf4STejun Heo 34428db25e78STejun Heo /* 34438db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34448db25e78STejun Heo * This will be registered high priority CPU notifier. 34458db25e78STejun Heo */ 34468db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34471da177e4SLinus Torvalds unsigned long action, 34481da177e4SLinus Torvalds void *hcpu) 34491da177e4SLinus Torvalds { 34503af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3451db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 34524ce62e9eSTejun Heo struct worker_pool *pool; 34531da177e4SLinus Torvalds 34548db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34553af24433SOleg Nesterov case CPU_UP_PREPARE: 34564ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 34573ce63377STejun Heo struct worker *worker; 34583ce63377STejun Heo 34593ce63377STejun Heo if (pool->nr_workers) 34603ce63377STejun Heo continue; 34613ce63377STejun Heo 34623ce63377STejun Heo worker = create_worker(pool); 34633ce63377STejun Heo if (!worker) 34643ce63377STejun Heo return NOTIFY_BAD; 34653ce63377STejun Heo 34663ce63377STejun Heo spin_lock_irq(&gcwq->lock); 34673ce63377STejun Heo start_worker(worker); 34683ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 34693af24433SOleg Nesterov } 34701da177e4SLinus Torvalds break; 34711da177e4SLinus Torvalds 347265758202STejun Heo case CPU_DOWN_FAILED: 347365758202STejun Heo case CPU_ONLINE: 34748db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 34758db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 34768db25e78STejun Heo rebind_workers(gcwq); 34778db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 34788db25e78STejun Heo break; 347965758202STejun Heo } 348065758202STejun Heo return NOTIFY_OK; 348165758202STejun Heo } 348265758202STejun Heo 348365758202STejun Heo /* 348465758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 348565758202STejun Heo * This will be registered as low priority CPU notifier. 348665758202STejun Heo */ 348765758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 348865758202STejun Heo unsigned long action, 348965758202STejun Heo void *hcpu) 349065758202STejun Heo { 34918db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 34928db25e78STejun Heo struct work_struct unbind_work; 34938db25e78STejun Heo 349465758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 349565758202STejun Heo case CPU_DOWN_PREPARE: 34968db25e78STejun Heo /* unbinding should happen on the local CPU */ 34978db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 34988db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 34998db25e78STejun Heo flush_work(&unbind_work); 35008db25e78STejun Heo break; 350165758202STejun Heo } 350265758202STejun Heo return NOTIFY_OK; 350365758202STejun Heo } 350465758202STejun Heo 35052d3854a3SRusty Russell #ifdef CONFIG_SMP 35068ccad40dSRusty Russell 35072d3854a3SRusty Russell struct work_for_cpu { 35086b44003eSAndrew Morton struct completion completion; 35092d3854a3SRusty Russell long (*fn)(void *); 35102d3854a3SRusty Russell void *arg; 35112d3854a3SRusty Russell long ret; 35122d3854a3SRusty Russell }; 35132d3854a3SRusty Russell 35146b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35152d3854a3SRusty Russell { 35166b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35172d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35186b44003eSAndrew Morton complete(&wfc->completion); 35196b44003eSAndrew Morton return 0; 35202d3854a3SRusty Russell } 35212d3854a3SRusty Russell 35222d3854a3SRusty Russell /** 35232d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35242d3854a3SRusty Russell * @cpu: the cpu to run on 35252d3854a3SRusty Russell * @fn: the function to run 35262d3854a3SRusty Russell * @arg: the function arg 35272d3854a3SRusty Russell * 352831ad9081SRusty Russell * This will return the value @fn returns. 352931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35306b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35312d3854a3SRusty Russell */ 35322d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35332d3854a3SRusty Russell { 35346b44003eSAndrew Morton struct task_struct *sub_thread; 35356b44003eSAndrew Morton struct work_for_cpu wfc = { 35366b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35376b44003eSAndrew Morton .fn = fn, 35386b44003eSAndrew Morton .arg = arg, 35396b44003eSAndrew Morton }; 35402d3854a3SRusty Russell 35416b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35426b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35436b44003eSAndrew Morton return PTR_ERR(sub_thread); 35446b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35456b44003eSAndrew Morton wake_up_process(sub_thread); 35466b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35472d3854a3SRusty Russell return wfc.ret; 35482d3854a3SRusty Russell } 35492d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35502d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35512d3854a3SRusty Russell 3552a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3553e7577c50SRusty Russell 3554a0a1a5fdSTejun Heo /** 3555a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3556a0a1a5fdSTejun Heo * 355758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 355858a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 355958a69cb4STejun Heo * gcwq->worklist. 3560a0a1a5fdSTejun Heo * 3561a0a1a5fdSTejun Heo * CONTEXT: 35628b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3563a0a1a5fdSTejun Heo */ 3564a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3565a0a1a5fdSTejun Heo { 3566a0a1a5fdSTejun Heo unsigned int cpu; 3567a0a1a5fdSTejun Heo 3568a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3569a0a1a5fdSTejun Heo 3570a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3571a0a1a5fdSTejun Heo workqueue_freezing = true; 3572a0a1a5fdSTejun Heo 3573f3421797STejun Heo for_each_gcwq_cpu(cpu) { 35748b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3575bdbc5dd7STejun Heo struct workqueue_struct *wq; 35768b03ae3cSTejun Heo 35778b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 35788b03ae3cSTejun Heo 3579db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3580db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3581db7bccf4STejun Heo 3582a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3583a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3584a0a1a5fdSTejun Heo 358558a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3586a0a1a5fdSTejun Heo cwq->max_active = 0; 35871da177e4SLinus Torvalds } 35888b03ae3cSTejun Heo 35898b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3590a0a1a5fdSTejun Heo } 3591a0a1a5fdSTejun Heo 3592a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3593a0a1a5fdSTejun Heo } 3594a0a1a5fdSTejun Heo 3595a0a1a5fdSTejun Heo /** 359658a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3597a0a1a5fdSTejun Heo * 3598a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3599a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3600a0a1a5fdSTejun Heo * 3601a0a1a5fdSTejun Heo * CONTEXT: 3602a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3603a0a1a5fdSTejun Heo * 3604a0a1a5fdSTejun Heo * RETURNS: 360558a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 360658a69cb4STejun Heo * is complete. 3607a0a1a5fdSTejun Heo */ 3608a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3609a0a1a5fdSTejun Heo { 3610a0a1a5fdSTejun Heo unsigned int cpu; 3611a0a1a5fdSTejun Heo bool busy = false; 3612a0a1a5fdSTejun Heo 3613a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3614a0a1a5fdSTejun Heo 3615a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3616a0a1a5fdSTejun Heo 3617f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3618bdbc5dd7STejun Heo struct workqueue_struct *wq; 3619a0a1a5fdSTejun Heo /* 3620a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3621a0a1a5fdSTejun Heo * to peek without lock. 3622a0a1a5fdSTejun Heo */ 3623a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3624a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3625a0a1a5fdSTejun Heo 362658a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3627a0a1a5fdSTejun Heo continue; 3628a0a1a5fdSTejun Heo 3629a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3630a0a1a5fdSTejun Heo if (cwq->nr_active) { 3631a0a1a5fdSTejun Heo busy = true; 3632a0a1a5fdSTejun Heo goto out_unlock; 3633a0a1a5fdSTejun Heo } 3634a0a1a5fdSTejun Heo } 3635a0a1a5fdSTejun Heo } 3636a0a1a5fdSTejun Heo out_unlock: 3637a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3638a0a1a5fdSTejun Heo return busy; 3639a0a1a5fdSTejun Heo } 3640a0a1a5fdSTejun Heo 3641a0a1a5fdSTejun Heo /** 3642a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3643a0a1a5fdSTejun Heo * 3644a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36457e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3646a0a1a5fdSTejun Heo * 3647a0a1a5fdSTejun Heo * CONTEXT: 36488b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3649a0a1a5fdSTejun Heo */ 3650a0a1a5fdSTejun Heo void thaw_workqueues(void) 3651a0a1a5fdSTejun Heo { 3652a0a1a5fdSTejun Heo unsigned int cpu; 3653a0a1a5fdSTejun Heo 3654a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3655a0a1a5fdSTejun Heo 3656a0a1a5fdSTejun Heo if (!workqueue_freezing) 3657a0a1a5fdSTejun Heo goto out_unlock; 3658a0a1a5fdSTejun Heo 3659f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36608b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 36614ce62e9eSTejun Heo struct worker_pool *pool; 3662bdbc5dd7STejun Heo struct workqueue_struct *wq; 36638b03ae3cSTejun Heo 36648b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36658b03ae3cSTejun Heo 3666db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3667db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3668db7bccf4STejun Heo 3669a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3670a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3671a0a1a5fdSTejun Heo 367258a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3673a0a1a5fdSTejun Heo continue; 3674a0a1a5fdSTejun Heo 3675a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3676a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3677a0a1a5fdSTejun Heo 3678a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3679a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3680a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3681a0a1a5fdSTejun Heo } 36828b03ae3cSTejun Heo 36834ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 36844ce62e9eSTejun Heo wake_up_worker(pool); 3685e22bee78STejun Heo 36868b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3687a0a1a5fdSTejun Heo } 3688a0a1a5fdSTejun Heo 3689a0a1a5fdSTejun Heo workqueue_freezing = false; 3690a0a1a5fdSTejun Heo out_unlock: 3691a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3692a0a1a5fdSTejun Heo } 3693a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3694a0a1a5fdSTejun Heo 36956ee0578bSSuresh Siddha static int __init init_workqueues(void) 36961da177e4SLinus Torvalds { 3697c34056a3STejun Heo unsigned int cpu; 3698c8e55f36STejun Heo int i; 3699c34056a3STejun Heo 370065758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 370165758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37028b03ae3cSTejun Heo 37038b03ae3cSTejun Heo /* initialize gcwqs */ 3704f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37058b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37064ce62e9eSTejun Heo struct worker_pool *pool; 37078b03ae3cSTejun Heo 37088b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37098b03ae3cSTejun Heo gcwq->cpu = cpu; 3710f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37118b03ae3cSTejun Heo 3712c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3713c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3714c8e55f36STejun Heo 37154ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37164ce62e9eSTejun Heo pool->gcwq = gcwq; 37174ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37184ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3719e22bee78STejun Heo 37204ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37214ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37224ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3723e22bee78STejun Heo 37244ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37254ce62e9eSTejun Heo (unsigned long)pool); 37264ce62e9eSTejun Heo 372760373152STejun Heo mutex_init(&pool->manager_mutex); 37284ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37294ce62e9eSTejun Heo } 3730db7bccf4STejun Heo 373125511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37328b03ae3cSTejun Heo } 37338b03ae3cSTejun Heo 3734e22bee78STejun Heo /* create the initial worker */ 3735f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3736e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37374ce62e9eSTejun Heo struct worker_pool *pool; 3738e22bee78STejun Heo 3739477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3740477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37414ce62e9eSTejun Heo 37424ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37434ce62e9eSTejun Heo struct worker *worker; 37444ce62e9eSTejun Heo 3745bc2ae0f5STejun Heo worker = create_worker(pool); 3746e22bee78STejun Heo BUG_ON(!worker); 3747e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3748e22bee78STejun Heo start_worker(worker); 3749e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3750e22bee78STejun Heo } 37514ce62e9eSTejun Heo } 3752e22bee78STejun Heo 3753d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3754d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3755d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3756f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3757f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 375824d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 375924d51addSTejun Heo WQ_FREEZABLE, 0); 376062d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 376162d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3762e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 376362d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 376462d3c543SAlan Stern !system_nrt_freezable_wq); 37656ee0578bSSuresh Siddha return 0; 37661da177e4SLinus Torvalds } 37676ee0578bSSuresh Siddha early_initcall(init_workqueues); 3768